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 u
->current_cursor
= mfree(u
->current_cursor
);
26 r
= sd_journal_get_cursor(u
->journal
, &u
->current_cursor
);
28 return log_error_errno(r
, "Failed to get cursor: %m");
30 r
= snprintf(buf
+ pos
, size
- pos
,
31 "__CURSOR=%s\n", u
->current_cursor
);
33 /* not enough space */
38 if (pos
+ r
== size
) {
39 /* exactly one character short, but we don't need it */
47 case ENTRY_REALTIME
: {
50 r
= sd_journal_get_realtime_usec(u
->journal
, &realtime
);
52 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
54 r
= snprintf(buf
+ pos
, size
- pos
,
55 "__REALTIME_TIMESTAMP="USEC_FMT
"\n", realtime
);
57 /* not enough space */
62 if (r
+ pos
== size
) {
63 /* exactly one character short, but we don't need it */
71 case ENTRY_MONOTONIC
: {
75 r
= sd_journal_get_monotonic_usec(u
->journal
, &monotonic
, &boot_id
);
77 return log_error_errno(r
, "Failed to get monotonic timestamp: %m");
79 r
= snprintf(buf
+ pos
, size
- pos
,
80 "__MONOTONIC_TIMESTAMP="USEC_FMT
"\n", monotonic
);
82 /* not enough space */
87 if (r
+ pos
== size
) {
88 /* exactly one character short, but we don't need it */
100 r
= sd_journal_get_monotonic_usec(u
->journal
, NULL
, &boot_id
);
102 return log_error_errno(r
, "Failed to get monotonic timestamp: %m");
104 r
= snprintf(buf
+ pos
, size
- pos
,
105 "_BOOT_ID=%s\n", sd_id128_to_string(boot_id
, sid
));
107 /* not enough space */
112 if (r
+ pos
== size
) {
113 /* exactly one character short, but we don't need it */
114 buf
[size
- 1] = '\n';
121 case ENTRY_NEW_FIELD
: {
124 r
= sd_journal_enumerate_data(u
->journal
,
128 return log_error_errno(r
, "Failed to move to next field in entry: %m");
130 u
->entry_state
= ENTRY_OUTRO
;
134 if (!utf8_is_printable_newline(u
->field_data
,
135 u
->field_length
, false)) {
136 u
->entry_state
= ENTRY_BINARY_FIELD_START
;
143 case ENTRY_TEXT_FIELD
:
144 case ENTRY_BINARY_FIELD
: {
148 done
= size
- pos
> u
->field_length
- u
->field_pos
;
150 tocopy
= u
->field_length
- u
->field_pos
;
155 (char*) u
->field_data
+ u
->field_pos
,
159 buf
[pos
+ tocopy
] = '\n';
161 u
->entry_state
= ENTRY_NEW_FIELD
;
164 u
->field_pos
+= tocopy
;
169 case ENTRY_BINARY_FIELD_START
: {
173 c
= memchr(u
->field_data
, '=', u
->field_length
);
174 if (!c
|| c
== u
->field_data
) {
175 log_error("Invalid field.");
179 len
= c
- (const char*)u
->field_data
;
181 /* need space for label + '\n' */
182 if (size
- pos
< len
+ 1)
185 memcpy(buf
+ pos
, u
->field_data
, len
);
186 buf
[pos
+ len
] = '\n';
189 u
->field_pos
= len
+ 1;
193 case ENTRY_BINARY_FIELD_SIZE
: {
196 /* need space for uint64_t */
200 le64
= htole64(u
->field_length
- u
->field_pos
);
201 memcpy(buf
+ pos
, &le64
, 8);
209 /* need space for '\n' */
220 assert_not_reached("WTF?");
223 assert_not_reached("WTF?");
226 static size_t journal_input_callback(void *buf
, size_t size
, size_t nmemb
, void *userp
) {
234 assert(nmemb
<= SSIZE_MAX
/ size
);
238 while (j
&& filled
< size
* nmemb
) {
239 if (u
->entry_state
== ENTRY_DONE
) {
240 r
= sd_journal_next(j
);
242 log_error_errno(r
, "Failed to move to next entry in journal: %m");
243 return CURL_READFUNC_ABORT
;
246 log_debug("No more entries, waiting for journal.");
248 log_info("No more entries, closing journal.");
249 close_journal_input(u
);
252 u
->uploading
= false;
257 u
->entry_state
= ENTRY_CURSOR
;
260 w
= write_entry((char*)buf
+ filled
, size
* nmemb
- filled
, u
);
262 return CURL_READFUNC_ABORT
;
266 log_error("Buffer space is too small to write entry.");
267 return CURL_READFUNC_ABORT
;
268 } else if (u
->entry_state
!= ENTRY_DONE
)
269 /* This means that all available space was used up */
272 log_debug("Entry %zu (%s) has been uploaded.",
273 u
->entries_sent
, u
->current_cursor
);
279 void close_journal_input(Uploader
*u
) {
283 log_debug("Closing journal input.");
285 sd_journal_close(u
->journal
);
291 static int process_journal_input(Uploader
*u
, int skip
) {
294 r
= sd_journal_next_skip(u
->journal
, skip
);
296 return log_error_errno(r
, "Failed to skip to next entry: %m");
301 u
->entry_state
= ENTRY_CURSOR
;
302 return start_upload(u
, journal_input_callback
, u
);
305 int check_journal_input(Uploader
*u
) {
306 if (u
->input_event
) {
309 r
= sd_journal_process(u
->journal
);
311 log_error_errno(r
, "Failed to process journal: %m");
312 close_journal_input(u
);
316 if (r
== SD_JOURNAL_NOP
)
320 return process_journal_input(u
, 1);
323 static int dispatch_journal_input(sd_event_source
*event
,
332 log_warning("dispatch_journal_input called when uploading, ignoring.");
336 log_debug("Detected journal input, checking for new data.");
337 return check_journal_input(u
);
340 int open_journal_for_upload(Uploader
*u
,
349 sd_journal_set_data_threshold(j
, 0);
352 fd
= sd_journal_get_fd(j
);
354 return log_error_errno(fd
, "sd_journal_get_fd failed: %m");
356 events
= sd_journal_get_events(j
);
358 r
= sd_journal_reliable_fd(j
);
363 u
->timeout
= JOURNAL_UPLOAD_POLL_TIMEOUT
;
365 r
= sd_event_add_io(u
->events
, &u
->input_event
,
366 fd
, events
, dispatch_journal_input
, u
);
368 return log_error_errno(r
, "Failed to register input event: %m");
370 log_debug("Listening for journal events on fd:%d, timeout %d",
371 fd
, u
->timeout
== (uint64_t) -1 ? -1 : (int) u
->timeout
);
373 log_debug("Not listening for journal events.");
376 r
= sd_journal_seek_cursor(j
, cursor
);
378 return log_error_errno(r
, "Failed to seek to cursor %s: %m",
382 return process_journal_input(u
, 1 + !!after_cursor
);