1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
10 #include "journal-file.h"
11 #include "journal-importer.h"
12 #include "journal-util.h"
13 #include "parse-util.h"
14 #include "string-util.h"
15 #include "unaligned.h"
18 IMPORTER_STATE_LINE
= 0, /* waiting to read, or reading line */
19 IMPORTER_STATE_DATA_START
, /* reading binary data header */
20 IMPORTER_STATE_DATA
, /* reading binary data */
21 IMPORTER_STATE_DATA_FINISH
, /* expecting newline */
22 IMPORTER_STATE_EOF
, /* done */
25 static int iovw_put(struct iovec_wrapper
*iovw
, void* data
, size_t len
) {
26 if (!GREEDY_REALLOC(iovw
->iovec
, iovw
->size_bytes
, iovw
->count
+ 1))
29 iovw
->iovec
[iovw
->count
++] = IOVEC_MAKE(data
, len
);
33 static void iovw_free_contents(struct iovec_wrapper
*iovw
) {
34 iovw
->iovec
= mfree(iovw
->iovec
);
35 iovw
->size_bytes
= iovw
->count
= 0;
38 static void iovw_rebase(struct iovec_wrapper
*iovw
, char *old
, char *new) {
41 for (i
= 0; i
< iovw
->count
; i
++)
42 iovw
->iovec
[i
].iov_base
= (char*) iovw
->iovec
[i
].iov_base
- old
+ new;
45 size_t iovw_size(struct iovec_wrapper
*iovw
) {
48 for (i
= 0; i
< iovw
->count
; i
++)
49 n
+= iovw
->iovec
[i
].iov_len
;
54 void journal_importer_cleanup(JournalImporter
*imp
) {
55 if (imp
->fd
>= 0 && !imp
->passive_fd
) {
56 log_debug("Closing %s (fd=%d)", imp
->name
?: "importer", imp
->fd
);
62 iovw_free_contents(&imp
->iovw
);
65 static char* realloc_buffer(JournalImporter
*imp
, size_t size
) {
66 char *b
, *old
= imp
->buf
;
68 b
= GREEDY_REALLOC(imp
->buf
, imp
->size
, size
);
72 iovw_rebase(&imp
->iovw
, old
, imp
->buf
);
77 static int get_line(JournalImporter
*imp
, char **line
, size_t *size
) {
82 assert(imp
->state
== IMPORTER_STATE_LINE
);
83 assert(imp
->offset
<= imp
->filled
);
84 assert(imp
->filled
<= imp
->size
);
85 assert(!imp
->buf
|| imp
->size
> 0);
90 size_t start
= MAX(imp
->scanned
, imp
->offset
);
92 c
= memchr(imp
->buf
+ start
, '\n',
98 imp
->scanned
= imp
->filled
;
99 if (imp
->scanned
>= DATA_SIZE_MAX
) {
100 log_error("Entry is bigger than %u bytes.", DATA_SIZE_MAX
);
105 /* we have to wait for some data to come to us */
108 /* We know that imp->filled is at most DATA_SIZE_MAX, so if
109 we reallocate it, we'll increase the size at least a bit. */
110 assert_cc(DATA_SIZE_MAX
< ENTRY_SIZE_MAX
);
111 if (imp
->size
- imp
->filled
< LINE_CHUNK
&&
112 !realloc_buffer(imp
, MIN(imp
->filled
+ LINE_CHUNK
, ENTRY_SIZE_MAX
)))
116 assert(imp
->size
- imp
->filled
>= LINE_CHUNK
||
117 imp
->size
== ENTRY_SIZE_MAX
);
120 imp
->buf
+ imp
->filled
,
121 imp
->size
- imp
->filled
);
124 log_error_errno(errno
, "read(%d, ..., %zu): %m",
126 imp
->size
- imp
->filled
);
134 *line
= imp
->buf
+ imp
->offset
;
135 *size
= c
+ 1 - imp
->buf
- imp
->offset
;
136 imp
->offset
+= *size
;
141 static int fill_fixed_size(JournalImporter
*imp
, void **data
, size_t size
) {
144 assert(IN_SET(imp
->state
, IMPORTER_STATE_DATA_START
, IMPORTER_STATE_DATA
, IMPORTER_STATE_DATA_FINISH
));
145 assert(size
<= DATA_SIZE_MAX
);
146 assert(imp
->offset
<= imp
->filled
);
147 assert(imp
->filled
<= imp
->size
);
148 assert(imp
->buf
|| imp
->size
== 0);
149 assert(!imp
->buf
|| imp
->size
> 0);
150 assert(imp
->fd
>= 0);
153 while (imp
->filled
- imp
->offset
< size
) {
157 /* we have to wait for some data to come to us */
160 if (!realloc_buffer(imp
, imp
->offset
+ size
))
163 n
= read(imp
->fd
, imp
->buf
+ imp
->filled
,
164 imp
->size
- imp
->filled
);
167 log_error_errno(errno
, "read(%d, ..., %zu): %m", imp
->fd
,
168 imp
->size
- imp
->filled
);
176 *data
= imp
->buf
+ imp
->offset
;
182 static int get_data_size(JournalImporter
*imp
) {
187 assert(imp
->state
== IMPORTER_STATE_DATA_START
);
188 assert(imp
->data_size
== 0);
190 r
= fill_fixed_size(imp
, &data
, sizeof(uint64_t));
194 imp
->data_size
= unaligned_read_le64(data
);
195 if (imp
->data_size
> DATA_SIZE_MAX
) {
196 log_error("Stream declares field with size %zu > DATA_SIZE_MAX = %u",
197 imp
->data_size
, DATA_SIZE_MAX
);
200 if (imp
->data_size
== 0)
201 log_warning("Binary field with zero length");
206 static int get_data_data(JournalImporter
*imp
, void **data
) {
211 assert(imp
->state
== IMPORTER_STATE_DATA
);
213 r
= fill_fixed_size(imp
, data
, imp
->data_size
);
220 static int get_data_newline(JournalImporter
*imp
) {
225 assert(imp
->state
== IMPORTER_STATE_DATA_FINISH
);
227 r
= fill_fixed_size(imp
, (void**) &data
, 1);
236 l
= cescape_char(*data
, buf
);
237 log_error("Expected newline, got '%.*s'", l
, buf
);
244 static int process_special_field(JournalImporter
*imp
, char *line
) {
246 char buf
[CELLESCAPE_DEFAULT_LENGTH
];
251 value
= startswith(line
, "__CURSOR=");
253 /* ignore __CURSOR */
256 value
= startswith(line
, "__REALTIME_TIMESTAMP=");
260 r
= safe_atou64(value
, &x
);
262 return log_warning_errno(r
, "Failed to parse __REALTIME_TIMESTAMP '%s': %m",
263 cellescape(buf
, sizeof buf
, value
));
264 else if (!VALID_REALTIME(x
)) {
265 log_warning("__REALTIME_TIMESTAMP out of range, ignoring: %"PRIu64
, x
);
269 imp
->ts
.realtime
= x
;
273 value
= startswith(line
, "__MONOTONIC_TIMESTAMP=");
277 r
= safe_atou64(value
, &x
);
279 return log_warning_errno(r
, "Failed to parse __MONOTONIC_TIMESTAMP '%s': %m",
280 cellescape(buf
, sizeof buf
, value
));
281 else if (!VALID_MONOTONIC(x
)) {
282 log_warning("__MONOTONIC_TIMESTAMP out of range, ignoring: %"PRIu64
, x
);
286 imp
->ts
.monotonic
= x
;
290 /* Just a single underline, but it needs special treatment too. */
291 value
= startswith(line
, "_BOOT_ID=");
293 r
= sd_id128_from_string(value
, &imp
->boot_id
);
295 return log_warning_errno(r
, "Failed to parse _BOOT_ID '%s': %m",
296 cellescape(buf
, sizeof buf
, value
));
298 /* store the field in the usual fashion too */
302 value
= startswith(line
, "__");
304 log_notice("Unknown dunder line __%s, ignoring.", cellescape(buf
, sizeof buf
, value
));
312 int journal_importer_process_data(JournalImporter
*imp
) {
316 case IMPORTER_STATE_LINE
: {
320 assert(imp
->data_size
== 0);
322 r
= get_line(imp
, &line
, &n
);
326 imp
->state
= IMPORTER_STATE_EOF
;
330 assert(line
[n
-1] == '\n');
333 log_trace("Received empty line, event is ready");
340 LLLLLLLL0011223344...\n
342 sep
= memchr(line
, '=', n
);
347 if (!journal_field_valid(line
, sep
- line
, true)) {
350 t
= strndupa(line
, sep
- line
);
351 log_debug("Ignoring invalid field: \"%s\"",
352 cellescape(buf
, sizeof buf
, t
));
358 r
= process_special_field(imp
, line
);
360 return r
< 0 ? r
: 0;
362 r
= iovw_put(&imp
->iovw
, line
, n
);
366 /* replace \n with = */
370 imp
->state
= IMPORTER_STATE_DATA_START
;
372 /* we cannot put the field in iovec until we have all data */
375 log_trace("Received: %.*s (%s)", (int) n
, line
, sep
? "text" : "binary");
377 return 0; /* continue */
380 case IMPORTER_STATE_DATA_START
:
381 assert(imp
->data_size
== 0);
383 r
= get_data_size(imp
);
384 // log_debug("get_data_size() -> %d", r);
388 imp
->state
= IMPORTER_STATE_EOF
;
392 imp
->state
= imp
->data_size
> 0 ?
393 IMPORTER_STATE_DATA
: IMPORTER_STATE_DATA_FINISH
;
395 return 0; /* continue */
397 case IMPORTER_STATE_DATA
: {
401 assert(imp
->data_size
> 0);
403 r
= get_data_data(imp
, &data
);
404 // log_debug("get_data_data() -> %d", r);
408 imp
->state
= IMPORTER_STATE_EOF
;
414 field
= (char*) data
- sizeof(uint64_t) - imp
->field_len
;
415 memmove(field
+ sizeof(uint64_t), field
, imp
->field_len
);
417 r
= iovw_put(&imp
->iovw
, field
+ sizeof(uint64_t), imp
->field_len
+ imp
->data_size
);
421 imp
->state
= IMPORTER_STATE_DATA_FINISH
;
423 return 0; /* continue */
426 case IMPORTER_STATE_DATA_FINISH
:
427 r
= get_data_newline(imp
);
428 // log_debug("get_data_newline() -> %d", r);
432 imp
->state
= IMPORTER_STATE_EOF
;
437 imp
->state
= IMPORTER_STATE_LINE
;
439 return 0; /* continue */
441 assert_not_reached("wtf?");
445 int journal_importer_push_data(JournalImporter
*imp
, const char *data
, size_t size
) {
447 assert(imp
->state
!= IMPORTER_STATE_EOF
);
449 if (!realloc_buffer(imp
, imp
->filled
+ size
)) {
450 log_error("Failed to store received data of size %zu "
451 "(in addition to existing %zu bytes with %zu filled): %s",
452 size
, imp
->size
, imp
->filled
, strerror(ENOMEM
));
456 memcpy(imp
->buf
+ imp
->filled
, data
, size
);
462 void journal_importer_drop_iovw(JournalImporter
*imp
) {
463 size_t remain
, target
;
465 /* This function drops processed data that along with the iovw that points at it */
467 iovw_free_contents(&imp
->iovw
);
469 /* possibly reset buffer position */
470 remain
= imp
->filled
- imp
->offset
;
472 if (remain
== 0) /* no brainer */
473 imp
->offset
= imp
->scanned
= imp
->filled
= 0;
474 else if (imp
->offset
> imp
->size
- imp
->filled
&&
475 imp
->offset
> remain
) {
476 memcpy(imp
->buf
, imp
->buf
+ imp
->offset
, remain
);
477 imp
->offset
= imp
->scanned
= 0;
478 imp
->filled
= remain
;
482 while (target
> 16 * LINE_CHUNK
&& imp
->filled
< target
/ 2)
484 if (target
< imp
->size
) {
487 tmp
= realloc(imp
->buf
, target
);
489 log_warning("Failed to reallocate buffer to (smaller) size %zu",
492 log_debug("Reallocated buffer from %zu to %zu bytes",
500 bool journal_importer_eof(const JournalImporter
*imp
) {
501 return imp
->state
== IMPORTER_STATE_EOF
;