1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "alloc-util.h"
7 #include "errno-util.h"
11 #include "journal-file.h"
12 #include "journal-importer.h"
13 #include "journal-util.h"
14 #include "parse-util.h"
15 #include "string-util.h"
16 #include "unaligned.h"
19 IMPORTER_STATE_LINE
= 0, /* waiting to read, or reading line */
20 IMPORTER_STATE_DATA_START
, /* reading binary data header */
21 IMPORTER_STATE_DATA
, /* reading binary data */
22 IMPORTER_STATE_DATA_FINISH
, /* expecting newline */
23 IMPORTER_STATE_EOF
, /* done */
26 void journal_importer_cleanup(JournalImporter
*imp
) {
27 if (imp
->fd
>= 0 && !imp
->passive_fd
) {
28 log_debug("Closing %s (fd=%d)", imp
->name
?: "importer", imp
->fd
);
34 iovw_free_contents(&imp
->iovw
, false);
37 static char* realloc_buffer(JournalImporter
*imp
, size_t size
) {
38 char *b
, *old
= imp
->buf
;
40 b
= GREEDY_REALLOC(imp
->buf
, imp
->size
, size
);
44 iovw_rebase(&imp
->iovw
, old
, imp
->buf
);
49 static int get_line(JournalImporter
*imp
, char **line
, size_t *size
) {
54 assert(imp
->state
== IMPORTER_STATE_LINE
);
55 assert(imp
->offset
<= imp
->filled
);
56 assert(imp
->filled
<= imp
->size
);
57 assert(!imp
->buf
|| imp
->size
> 0);
62 size_t start
= MAX(imp
->scanned
, imp
->offset
);
64 c
= memchr(imp
->buf
+ start
, '\n',
70 imp
->scanned
= imp
->filled
;
71 if (imp
->scanned
>= DATA_SIZE_MAX
)
72 return log_error_errno(SYNTHETIC_ERRNO(ENOBUFS
),
73 "Entry is bigger than %u bytes.",
77 /* we have to wait for some data to come to us */
80 /* We know that imp->filled is at most DATA_SIZE_MAX, so if
81 we reallocate it, we'll increase the size at least a bit. */
82 assert_cc(DATA_SIZE_MAX
< ENTRY_SIZE_MAX
);
83 if (imp
->size
- imp
->filled
< LINE_CHUNK
&&
84 !realloc_buffer(imp
, MIN(imp
->filled
+ LINE_CHUNK
, ENTRY_SIZE_MAX
)))
88 assert(imp
->size
- imp
->filled
>= LINE_CHUNK
||
89 imp
->size
== ENTRY_SIZE_MAX
);
92 imp
->buf
+ imp
->filled
,
93 imp
->size
- imp
->filled
);
96 log_error_errno(errno
, "read(%d, ..., %zu): %m",
98 imp
->size
- imp
->filled
);
106 *line
= imp
->buf
+ imp
->offset
;
107 *size
= c
+ 1 - imp
->buf
- imp
->offset
;
108 imp
->offset
+= *size
;
113 static int fill_fixed_size(JournalImporter
*imp
, void **data
, size_t size
) {
116 assert(IN_SET(imp
->state
, IMPORTER_STATE_DATA_START
, IMPORTER_STATE_DATA
, IMPORTER_STATE_DATA_FINISH
));
117 assert(size
<= DATA_SIZE_MAX
);
118 assert(imp
->offset
<= imp
->filled
);
119 assert(imp
->filled
<= imp
->size
);
120 assert(imp
->buf
|| imp
->size
== 0);
121 assert(!imp
->buf
|| imp
->size
> 0);
122 assert(imp
->fd
>= 0);
125 while (imp
->filled
- imp
->offset
< size
) {
129 /* we have to wait for some data to come to us */
132 if (!realloc_buffer(imp
, imp
->offset
+ size
))
135 n
= read(imp
->fd
, imp
->buf
+ imp
->filled
,
136 imp
->size
- imp
->filled
);
139 log_error_errno(errno
, "read(%d, ..., %zu): %m", imp
->fd
,
140 imp
->size
- imp
->filled
);
148 *data
= imp
->buf
+ imp
->offset
;
154 static int get_data_size(JournalImporter
*imp
) {
159 assert(imp
->state
== IMPORTER_STATE_DATA_START
);
160 assert(imp
->data_size
== 0);
162 r
= fill_fixed_size(imp
, &data
, sizeof(uint64_t));
166 imp
->data_size
= unaligned_read_le64(data
);
167 if (imp
->data_size
> DATA_SIZE_MAX
)
168 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
169 "Stream declares field with size %zu > DATA_SIZE_MAX = %u",
170 imp
->data_size
, DATA_SIZE_MAX
);
171 if (imp
->data_size
== 0)
172 log_warning("Binary field with zero length");
177 static int get_data_data(JournalImporter
*imp
, void **data
) {
182 assert(imp
->state
== IMPORTER_STATE_DATA
);
184 r
= fill_fixed_size(imp
, data
, imp
->data_size
);
191 static int get_data_newline(JournalImporter
*imp
) {
196 assert(imp
->state
== IMPORTER_STATE_DATA_FINISH
);
198 r
= fill_fixed_size(imp
, (void**) &data
, 1);
207 l
= cescape_char(*data
, buf
);
208 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
209 "Expected newline, got '%.*s'", l
, buf
);
215 static int process_special_field(JournalImporter
*imp
, char *line
) {
217 char buf
[CELLESCAPE_DEFAULT_LENGTH
];
222 value
= startswith(line
, "__CURSOR=");
224 /* ignore __CURSOR */
227 value
= startswith(line
, "__REALTIME_TIMESTAMP=");
231 r
= safe_atou64(value
, &x
);
233 return log_warning_errno(r
, "Failed to parse __REALTIME_TIMESTAMP '%s': %m",
234 cellescape(buf
, sizeof buf
, value
));
235 else if (!VALID_REALTIME(x
)) {
236 log_warning("__REALTIME_TIMESTAMP out of range, ignoring: %"PRIu64
, x
);
240 imp
->ts
.realtime
= x
;
244 value
= startswith(line
, "__MONOTONIC_TIMESTAMP=");
248 r
= safe_atou64(value
, &x
);
250 return log_warning_errno(r
, "Failed to parse __MONOTONIC_TIMESTAMP '%s': %m",
251 cellescape(buf
, sizeof buf
, value
));
252 else if (!VALID_MONOTONIC(x
)) {
253 log_warning("__MONOTONIC_TIMESTAMP out of range, ignoring: %"PRIu64
, x
);
257 imp
->ts
.monotonic
= x
;
261 /* Just a single underline, but it needs special treatment too. */
262 value
= startswith(line
, "_BOOT_ID=");
264 r
= sd_id128_from_string(value
, &imp
->boot_id
);
266 return log_warning_errno(r
, "Failed to parse _BOOT_ID '%s': %m",
267 cellescape(buf
, sizeof buf
, value
));
269 /* store the field in the usual fashion too */
273 value
= startswith(line
, "__");
275 log_notice("Unknown dunder line __%s, ignoring.", cellescape(buf
, sizeof buf
, value
));
283 int journal_importer_process_data(JournalImporter
*imp
) {
287 case IMPORTER_STATE_LINE
: {
291 assert(imp
->data_size
== 0);
293 r
= get_line(imp
, &line
, &n
);
297 imp
->state
= IMPORTER_STATE_EOF
;
301 assert(line
[n
-1] == '\n');
304 log_trace("Received empty line, event is ready");
311 LLLLLLLL0011223344...\n
313 sep
= memchr(line
, '=', n
);
318 if (!journal_field_valid(line
, sep
- line
, true)) {
321 t
= strndupa(line
, sep
- line
);
322 log_debug("Ignoring invalid field: \"%s\"",
323 cellescape(buf
, sizeof buf
, t
));
329 r
= process_special_field(imp
, line
);
331 return r
< 0 ? r
: 0;
333 r
= iovw_put(&imp
->iovw
, line
, n
);
337 /* replace \n with = */
341 imp
->state
= IMPORTER_STATE_DATA_START
;
343 /* we cannot put the field in iovec until we have all data */
346 log_trace("Received: %.*s (%s)", (int) n
, line
, sep
? "text" : "binary");
348 return 0; /* continue */
351 case IMPORTER_STATE_DATA_START
:
352 assert(imp
->data_size
== 0);
354 r
= get_data_size(imp
);
355 // log_debug("get_data_size() -> %d", r);
359 imp
->state
= IMPORTER_STATE_EOF
;
363 imp
->state
= imp
->data_size
> 0 ?
364 IMPORTER_STATE_DATA
: IMPORTER_STATE_DATA_FINISH
;
366 return 0; /* continue */
368 case IMPORTER_STATE_DATA
: {
372 assert(imp
->data_size
> 0);
374 r
= get_data_data(imp
, &data
);
375 // log_debug("get_data_data() -> %d", r);
379 imp
->state
= IMPORTER_STATE_EOF
;
385 field
= (char*) data
- sizeof(uint64_t) - imp
->field_len
;
386 memmove(field
+ sizeof(uint64_t), field
, imp
->field_len
);
388 r
= iovw_put(&imp
->iovw
, field
+ sizeof(uint64_t), imp
->field_len
+ imp
->data_size
);
392 imp
->state
= IMPORTER_STATE_DATA_FINISH
;
394 return 0; /* continue */
397 case IMPORTER_STATE_DATA_FINISH
:
398 r
= get_data_newline(imp
);
399 // log_debug("get_data_newline() -> %d", r);
403 imp
->state
= IMPORTER_STATE_EOF
;
408 imp
->state
= IMPORTER_STATE_LINE
;
410 return 0; /* continue */
412 assert_not_reached("wtf?");
416 int journal_importer_push_data(JournalImporter
*imp
, const char *data
, size_t size
) {
418 assert(imp
->state
!= IMPORTER_STATE_EOF
);
420 if (!realloc_buffer(imp
, imp
->filled
+ size
))
421 return log_error_errno(SYNTHETIC_ERRNO(ENOMEM
),
422 "Failed to store received data of size %zu "
423 "(in addition to existing %zu bytes with %zu filled): %s",
424 size
, imp
->size
, imp
->filled
,
425 strerror_safe(ENOMEM
));
427 memcpy(imp
->buf
+ imp
->filled
, data
, size
);
433 void journal_importer_drop_iovw(JournalImporter
*imp
) {
434 size_t remain
, target
;
436 /* This function drops processed data that along with the iovw that points at it */
438 iovw_free_contents(&imp
->iovw
, false);
440 /* possibly reset buffer position */
441 remain
= imp
->filled
- imp
->offset
;
443 if (remain
== 0) /* no brainer */
444 imp
->offset
= imp
->scanned
= imp
->filled
= 0;
445 else if (imp
->offset
> imp
->size
- imp
->filled
&&
446 imp
->offset
> remain
) {
447 memcpy(imp
->buf
, imp
->buf
+ imp
->offset
, remain
);
448 imp
->offset
= imp
->scanned
= 0;
449 imp
->filled
= remain
;
453 while (target
> 16 * LINE_CHUNK
&& imp
->filled
< target
/ 2)
455 if (target
< imp
->size
) {
458 tmp
= realloc(imp
->buf
, target
);
460 log_warning("Failed to reallocate buffer to (smaller) size %zu",
463 log_debug("Reallocated buffer from %zu to %zu bytes",
471 bool journal_importer_eof(const JournalImporter
*imp
) {
472 return imp
->state
== IMPORTER_STATE_EOF
;