1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Zbigniew Jędrzejewski-Szmek
11 #include "sd-daemon.h"
13 #include "alloc-util.h"
14 #include "journal-upload.h"
16 #include "string-util.h"
21 * Write up to size bytes to buf. Return negative on error, and number of
22 * bytes written otherwise. The last case is a kind of an error too.
24 static ssize_t
write_entry(char *buf
, size_t size
, Uploader
*u
) {
28 assert(size
<= SSIZE_MAX
);
32 switch(u
->entry_state
) {
34 u
->current_cursor
= mfree(u
->current_cursor
);
36 r
= sd_journal_get_cursor(u
->journal
, &u
->current_cursor
);
38 return log_error_errno(r
, "Failed to get cursor: %m");
40 r
= snprintf(buf
+ pos
, size
- pos
,
41 "__CURSOR=%s\n", u
->current_cursor
);
43 /* not enough space */
48 if (pos
+ r
== size
) {
49 /* exactly one character short, but we don't need it */
57 case ENTRY_REALTIME
: {
60 r
= sd_journal_get_realtime_usec(u
->journal
, &realtime
);
62 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
64 r
= snprintf(buf
+ pos
, size
- pos
,
65 "__REALTIME_TIMESTAMP="USEC_FMT
"\n", realtime
);
67 /* not enough space */
72 if (r
+ pos
== size
) {
73 /* exactly one character short, but we don't need it */
81 case ENTRY_MONOTONIC
: {
85 r
= sd_journal_get_monotonic_usec(u
->journal
, &monotonic
, &boot_id
);
87 return log_error_errno(r
, "Failed to get monotonic timestamp: %m");
89 r
= snprintf(buf
+ pos
, size
- pos
,
90 "__MONOTONIC_TIMESTAMP="USEC_FMT
"\n", monotonic
);
92 /* not enough space */
97 if (r
+ pos
== size
) {
98 /* exactly one character short, but we don't need it */
106 case ENTRY_BOOT_ID
: {
110 r
= sd_journal_get_monotonic_usec(u
->journal
, NULL
, &boot_id
);
112 return log_error_errno(r
, "Failed to get monotonic timestamp: %m");
114 r
= snprintf(buf
+ pos
, size
- pos
,
115 "_BOOT_ID=%s\n", sd_id128_to_string(boot_id
, sid
));
117 /* not enough space */
122 if (r
+ pos
== size
) {
123 /* exactly one character short, but we don't need it */
124 buf
[size
- 1] = '\n';
131 case ENTRY_NEW_FIELD
: {
134 r
= sd_journal_enumerate_data(u
->journal
,
138 return log_error_errno(r
, "Failed to move to next field in entry: %m");
140 u
->entry_state
= ENTRY_OUTRO
;
144 /* We already printed the boot id from the data in
145 * the header, hence let's suppress it here */
146 if (memory_startswith(u
->field_data
, u
->field_length
, "_BOOT_ID="))
149 if (!utf8_is_printable_newline(u
->field_data
, u
->field_length
, false)) {
150 u
->entry_state
= ENTRY_BINARY_FIELD_START
;
157 case ENTRY_TEXT_FIELD
:
158 case ENTRY_BINARY_FIELD
: {
162 done
= size
- pos
> u
->field_length
- u
->field_pos
;
164 tocopy
= u
->field_length
- u
->field_pos
;
169 (char*) u
->field_data
+ u
->field_pos
,
173 buf
[pos
+ tocopy
] = '\n';
175 u
->entry_state
= ENTRY_NEW_FIELD
;
178 u
->field_pos
+= tocopy
;
183 case ENTRY_BINARY_FIELD_START
: {
187 c
= memchr(u
->field_data
, '=', u
->field_length
);
188 if (!c
|| c
== u
->field_data
) {
189 log_error("Invalid field.");
193 len
= c
- (const char*)u
->field_data
;
195 /* need space for label + '\n' */
196 if (size
- pos
< len
+ 1)
199 memcpy(buf
+ pos
, u
->field_data
, len
);
200 buf
[pos
+ len
] = '\n';
203 u
->field_pos
= len
+ 1;
207 case ENTRY_BINARY_FIELD_SIZE
: {
210 /* need space for uint64_t */
214 le64
= htole64(u
->field_length
- u
->field_pos
);
215 memcpy(buf
+ pos
, &le64
, 8);
223 /* need space for '\n' */
234 assert_not_reached("WTF?");
237 assert_not_reached("WTF?");
240 static inline void check_update_watchdog(Uploader
*u
) {
244 if (u
->watchdog_usec
<= 0)
247 after
= now(CLOCK_MONOTONIC
);
248 elapsed_time
= usec_sub_unsigned(after
, u
->watchdog_timestamp
);
249 if (elapsed_time
> u
->watchdog_usec
/ 2) {
250 log_debug("Update watchdog timer");
251 sd_notify(false, "WATCHDOG=1");
252 u
->watchdog_timestamp
= after
;
256 static size_t journal_input_callback(void *buf
, size_t size
, size_t nmemb
, void *userp
) {
264 assert(nmemb
<= SSIZE_MAX
/ size
);
266 check_update_watchdog(u
);
270 while (j
&& filled
< size
* nmemb
) {
271 if (u
->entry_state
== ENTRY_DONE
) {
272 r
= sd_journal_next(j
);
274 log_error_errno(r
, "Failed to move to next entry in journal: %m");
275 return CURL_READFUNC_ABORT
;
278 log_debug("No more entries, waiting for journal.");
280 log_info("No more entries, closing journal.");
281 close_journal_input(u
);
284 u
->uploading
= false;
289 u
->entry_state
= ENTRY_CURSOR
;
292 w
= write_entry((char*)buf
+ filled
, size
* nmemb
- filled
, u
);
294 return CURL_READFUNC_ABORT
;
298 log_error("Buffer space is too small to write entry.");
299 return CURL_READFUNC_ABORT
;
300 } else if (u
->entry_state
!= ENTRY_DONE
)
301 /* This means that all available space was used up */
304 log_debug("Entry %zu (%s) has been uploaded.",
305 u
->entries_sent
, u
->current_cursor
);
311 void close_journal_input(Uploader
*u
) {
315 log_debug("Closing journal input.");
317 sd_journal_close(u
->journal
);
323 static int process_journal_input(Uploader
*u
, int skip
) {
329 r
= sd_journal_next_skip(u
->journal
, skip
);
331 return log_error_errno(r
, "Failed to skip to next entry: %m");
336 u
->entry_state
= ENTRY_CURSOR
;
337 return start_upload(u
, journal_input_callback
, u
);
340 int check_journal_input(Uploader
*u
) {
341 if (u
->input_event
) {
344 r
= sd_journal_process(u
->journal
);
346 log_error_errno(r
, "Failed to process journal: %m");
347 close_journal_input(u
);
351 if (r
== SD_JOURNAL_NOP
)
355 return process_journal_input(u
, 1);
358 static int dispatch_journal_input(sd_event_source
*event
,
369 log_debug("Detected journal input, checking for new data.");
370 return check_journal_input(u
);
373 int open_journal_for_upload(Uploader
*u
,
382 sd_journal_set_data_threshold(j
, 0);
385 fd
= sd_journal_get_fd(j
);
387 return log_error_errno(fd
, "sd_journal_get_fd failed: %m");
389 events
= sd_journal_get_events(j
);
391 r
= sd_journal_reliable_fd(j
);
396 u
->timeout
= JOURNAL_UPLOAD_POLL_TIMEOUT
;
398 r
= sd_event_add_io(u
->events
, &u
->input_event
,
399 fd
, events
, dispatch_journal_input
, u
);
401 return log_error_errno(r
, "Failed to register input event: %m");
403 log_debug("Listening for journal events on fd:%d, timeout %d",
404 fd
, u
->timeout
== (uint64_t) -1 ? -1 : (int) u
->timeout
);
406 log_debug("Not listening for journal events.");
409 r
= sd_journal_seek_cursor(j
, cursor
);
411 return log_error_errno(r
, "Failed to seek to cursor %s: %m",
415 return process_journal_input(u
, 1 + !!after_cursor
);