1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #pragma once
4 
5 #include <inttypes.h>
6 
7 #include "sd-event.h"
8 #include "sd-journal.h"
9 
10 #include "time-util.h"
11 
12 typedef enum {
13         ENTRY_CURSOR = 0,           /* Nothing actually written yet. */
14         ENTRY_REALTIME,
15         ENTRY_MONOTONIC,
16         ENTRY_BOOT_ID,
17         ENTRY_NEW_FIELD,            /* In between fields. */
18         ENTRY_TEXT_FIELD,           /* In the middle of a text field. */
19         ENTRY_BINARY_FIELD_START,   /* Writing the name of a binary field. */
20         ENTRY_BINARY_FIELD_SIZE,    /* Writing the size of a binary field. */
21         ENTRY_BINARY_FIELD,         /* In the middle of a binary field. */
22         ENTRY_OUTRO,                /* Writing '\n' */
23         ENTRY_DONE,                 /* Need to move to a new field. */
24 } entry_state;
25 
26 typedef struct Uploader {
27         sd_event *events;
28         sd_event_source *sigint_event, *sigterm_event;
29 
30         char *url;
31         CURL *easy;
32         bool uploading;
33         char error[CURL_ERROR_SIZE];
34         struct curl_slist *header;
35         char *answer;
36 
37         sd_event_source *input_event;
38         uint64_t timeout;
39 
40         /* fd stuff */
41         int input;
42 
43         /* journal stuff */
44         sd_journal* journal;
45 
46         entry_state entry_state;
47         const void *field_data;
48         size_t field_pos, field_length;
49 
50         /* general metrics */
51         const char *state_file;
52 
53         size_t entries_sent;
54         char *last_cursor, *current_cursor;
55         usec_t watchdog_timestamp;
56         usec_t watchdog_usec;
57 } Uploader;
58 
59 #define JOURNAL_UPLOAD_POLL_TIMEOUT (10 * USEC_PER_SEC)
60 
61 int start_upload(Uploader *u,
62                  size_t (*input_callback)(void *ptr,
63                                           size_t size,
64                                           size_t nmemb,
65                                           void *userdata),
66                  void *data);
67 
68 int open_journal_for_upload(Uploader *u,
69                             sd_journal *j,
70                             const char *cursor,
71                             bool after_cursor,
72                             bool follow);
73 void close_journal_input(Uploader *u);
74 int check_journal_input(Uploader *u);
75