8 #include "journal-upload.h"
11 * Write up to size bytes to buf. Return negative on error, and number of
12 * bytes written otherwise. The last case is a kind of an error too.
14 static ssize_t write_entry(char *buf, size_t size, Uploader *u) {
18 assert(size <= SSIZE_MAX);
22 switch(u->entry_state) {
24 free(u->current_cursor);
25 u->current_cursor = NULL;
27 r = sd_journal_get_cursor(u->journal, &u->current_cursor);
29 log_error_errno(r, "Failed to get cursor: %m");
33 r = snprintf(buf + pos, size - pos,
34 "__CURSOR=%s\n", u->current_cursor);
36 /* not enough space */
41 if (pos + r == size) {
42 /* exactly one character short, but we don't need it */
50 case ENTRY_REALTIME: {
53 r = sd_journal_get_realtime_usec(u->journal, &realtime);
55 log_error_errno(r, "Failed to get realtime timestamp: %m");
59 r = snprintf(buf + pos, size - pos,
60 "__REALTIME_TIMESTAMP="USEC_FMT"\n", realtime);
62 /* not enough space */
67 if (r + pos == size) {
68 /* exactly one character short, but we don't need it */
76 case ENTRY_MONOTONIC: {
80 r = sd_journal_get_monotonic_usec(u->journal, &monotonic, &boot_id);
82 log_error_errno(r, "Failed to get monotonic timestamp: %m");
86 r = snprintf(buf + pos, size - pos,
87 "__MONOTONIC_TIMESTAMP="USEC_FMT"\n", monotonic);
89 /* not enough space */
94 if (r + pos == size) {
95 /* exactly one character short, but we don't need it */
103 case ENTRY_BOOT_ID: {
107 r = sd_journal_get_monotonic_usec(u->journal, NULL, &boot_id);
109 log_error_errno(r, "Failed to get monotonic timestamp: %m");
113 r = snprintf(buf + pos, size - pos,
114 "_BOOT_ID=%s\n", sd_id128_to_string(boot_id, sid));
116 /* not enough space */
121 if (r + pos == size) {
122 /* exactly one character short, but we don't need it */
123 buf[size - 1] = '\n';
130 case ENTRY_NEW_FIELD: {
133 r = sd_journal_enumerate_data(u->journal,
137 log_error_errno(r, "Failed to move to next field in entry: %m");
140 u->entry_state = ENTRY_OUTRO;
144 if (!utf8_is_printable_newline(u->field_data,
145 u->field_length, false)) {
146 u->entry_state = ENTRY_BINARY_FIELD_START;
153 case ENTRY_TEXT_FIELD:
154 case ENTRY_BINARY_FIELD: {
158 done = size - pos > u->field_length - u->field_pos;
160 tocopy = u->field_length - u->field_pos;
165 (char*) u->field_data + u->field_pos,
169 buf[pos + tocopy] = '\n';
171 u->entry_state = ENTRY_NEW_FIELD;
174 u->field_pos += tocopy;
179 case ENTRY_BINARY_FIELD_START: {
183 c = memchr(u->field_data, '=', u->field_length);
184 if (!c || c == u->field_data) {
185 log_error("Invalid field.");
189 len = c - (const char*)u->field_data;
191 /* need space for label + '\n' */
192 if (size - pos < len + 1)
195 memcpy(buf + pos, u->field_data, len);
196 buf[pos + len] = '\n';
199 u->field_pos = len + 1;
203 case ENTRY_BINARY_FIELD_SIZE: {
206 /* need space for uint64_t */
210 le64 = htole64(u->field_length - u->field_pos);
211 memcpy(buf + pos, &le64, 8);
219 /* need space for '\n' */
230 assert_not_reached("WTF?");
233 assert_not_reached("WTF?");
236 static size_t journal_input_callback(void *buf, size_t size, size_t nmemb, void *userp) {
244 assert(nmemb <= SSIZE_MAX / size);
248 while (j && filled < size * nmemb) {
249 if (u->entry_state == ENTRY_DONE) {
250 r = sd_journal_next(j);
252 log_error_errno(r, "Failed to move to next entry in journal: %m");
253 return CURL_READFUNC_ABORT;
256 log_debug("No more entries, waiting for journal.");
258 log_info("No more entries, closing journal.");
259 close_journal_input(u);
262 u->uploading = false;
267 u->entry_state = ENTRY_CURSOR;
270 w = write_entry((char*)buf + filled, size * nmemb - filled, u);
272 return CURL_READFUNC_ABORT;
276 log_error("Buffer space is too small to write entry.");
277 return CURL_READFUNC_ABORT;
278 } else if (u->entry_state != ENTRY_DONE)
279 /* This means that all available space was used up */
282 log_debug("Entry %zu (%s) has been uploaded.",
283 u->entries_sent, u->current_cursor);
289 void close_journal_input(Uploader *u) {
293 log_debug("Closing journal input.");
295 sd_journal_close(u->journal);
301 static int process_journal_input(Uploader *u, int skip) {
304 r = sd_journal_next_skip(u->journal, skip);
306 log_error_errno(r, "Failed to skip to next entry: %m");
312 u->entry_state = ENTRY_CURSOR;
313 return start_upload(u, journal_input_callback, u);
316 int check_journal_input(Uploader *u) {
317 if (u->input_event) {
320 r = sd_journal_process(u->journal);
322 log_error_errno(r, "Failed to process journal: %m");
323 close_journal_input(u);
327 if (r == SD_JOURNAL_NOP)
331 return process_journal_input(u, 1);
334 static int dispatch_journal_input(sd_event_source *event,
343 log_warning("dispatch_journal_input called when uploading, ignoring.");
347 log_debug("Detected journal input, checking for new data.");
348 return check_journal_input(u);
351 int open_journal_for_upload(Uploader *u,
360 sd_journal_set_data_threshold(j, 0);
363 fd = sd_journal_get_fd(j);
365 log_error_errno(fd, "sd_journal_get_fd failed: %m");
369 events = sd_journal_get_events(j);
371 r = sd_journal_reliable_fd(j);
376 u->timeout = JOURNAL_UPLOAD_POLL_TIMEOUT;
378 r = sd_event_add_io(u->events, &u->input_event,
379 fd, events, dispatch_journal_input, u);
381 log_error_errno(r, "Failed to register input event: %m");
385 log_debug("Listening for journal events on fd:%d, timeout %d",
386 fd, u->timeout == (uint64_t) -1 ? -1 : (int) u->timeout);
388 log_debug("Not listening for journal events.");
391 r = sd_journal_seek_cursor(j, cursor);
393 log_error_errno(r, "Failed to seek to cursor %s: %m",
399 return process_journal_input(u, 1 + !!after_cursor);