1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Zbigniew Jędrzejewski-Szmek
11 #include "alloc-util.h"
14 #include "journal-importer.h"
15 #include "parse-util.h"
16 #include "string-util.h"
17 #include "unaligned.h"
20 IMPORTER_STATE_LINE
= 0, /* waiting to read, or reading line */
21 IMPORTER_STATE_DATA_START
, /* reading binary data header */
22 IMPORTER_STATE_DATA
, /* reading binary data */
23 IMPORTER_STATE_DATA_FINISH
, /* expecting newline */
24 IMPORTER_STATE_EOF
, /* done */
27 static int iovw_put(struct iovec_wrapper
*iovw
, void* data
, size_t len
) {
28 if (!GREEDY_REALLOC(iovw
->iovec
, iovw
->size_bytes
, iovw
->count
+ 1))
31 iovw
->iovec
[iovw
->count
++] = IOVEC_MAKE(data
, len
);
35 static void iovw_free_contents(struct iovec_wrapper
*iovw
) {
36 iovw
->iovec
= mfree(iovw
->iovec
);
37 iovw
->size_bytes
= iovw
->count
= 0;
40 static void iovw_rebase(struct iovec_wrapper
*iovw
, char *old
, char *new) {
43 for (i
= 0; i
< iovw
->count
; i
++)
44 iovw
->iovec
[i
].iov_base
= (char*) iovw
->iovec
[i
].iov_base
- old
+ new;
47 size_t iovw_size(struct iovec_wrapper
*iovw
) {
50 for (i
= 0; i
< iovw
->count
; i
++)
51 n
+= iovw
->iovec
[i
].iov_len
;
56 void journal_importer_cleanup(JournalImporter
*imp
) {
57 if (imp
->fd
>= 0 && !imp
->passive_fd
) {
58 log_debug("Closing %s (fd=%d)", imp
->name
?: "importer", imp
->fd
);
64 iovw_free_contents(&imp
->iovw
);
67 static char* realloc_buffer(JournalImporter
*imp
, size_t size
) {
68 char *b
, *old
= imp
->buf
;
70 b
= GREEDY_REALLOC(imp
->buf
, imp
->size
, size
);
74 iovw_rebase(&imp
->iovw
, old
, imp
->buf
);
79 static int get_line(JournalImporter
*imp
, char **line
, size_t *size
) {
84 assert(imp
->state
== IMPORTER_STATE_LINE
);
85 assert(imp
->offset
<= imp
->filled
);
86 assert(imp
->filled
<= imp
->size
);
87 assert(!imp
->buf
|| imp
->size
> 0);
92 size_t start
= MAX(imp
->scanned
, imp
->offset
);
94 c
= memchr(imp
->buf
+ start
, '\n',
100 imp
->scanned
= imp
->filled
;
101 if (imp
->scanned
>= DATA_SIZE_MAX
) {
102 log_error("Entry is bigger than %u bytes.", DATA_SIZE_MAX
);
107 /* we have to wait for some data to come to us */
110 /* We know that imp->filled is at most DATA_SIZE_MAX, so if
111 we reallocate it, we'll increase the size at least a bit. */
112 assert_cc(DATA_SIZE_MAX
< ENTRY_SIZE_MAX
);
113 if (imp
->size
- imp
->filled
< LINE_CHUNK
&&
114 !realloc_buffer(imp
, MIN(imp
->filled
+ LINE_CHUNK
, ENTRY_SIZE_MAX
)))
118 assert(imp
->size
- imp
->filled
>= LINE_CHUNK
||
119 imp
->size
== ENTRY_SIZE_MAX
);
122 imp
->buf
+ imp
->filled
,
123 imp
->size
- imp
->filled
);
126 log_error_errno(errno
, "read(%d, ..., %zu): %m",
128 imp
->size
- imp
->filled
);
136 *line
= imp
->buf
+ imp
->offset
;
137 *size
= c
+ 1 - imp
->buf
- imp
->offset
;
138 imp
->offset
+= *size
;
143 static int fill_fixed_size(JournalImporter
*imp
, void **data
, size_t size
) {
146 assert(IN_SET(imp
->state
, IMPORTER_STATE_DATA_START
, IMPORTER_STATE_DATA
, IMPORTER_STATE_DATA_FINISH
));
147 assert(size
<= DATA_SIZE_MAX
);
148 assert(imp
->offset
<= imp
->filled
);
149 assert(imp
->filled
<= imp
->size
);
150 assert(imp
->buf
|| imp
->size
== 0);
151 assert(!imp
->buf
|| imp
->size
> 0);
152 assert(imp
->fd
>= 0);
155 while (imp
->filled
- imp
->offset
< size
) {
159 /* we have to wait for some data to come to us */
162 if (!realloc_buffer(imp
, imp
->offset
+ size
))
165 n
= read(imp
->fd
, imp
->buf
+ imp
->filled
,
166 imp
->size
- imp
->filled
);
169 log_error_errno(errno
, "read(%d, ..., %zu): %m", imp
->fd
,
170 imp
->size
- imp
->filled
);
178 *data
= imp
->buf
+ imp
->offset
;
184 static int get_data_size(JournalImporter
*imp
) {
189 assert(imp
->state
== IMPORTER_STATE_DATA_START
);
190 assert(imp
->data_size
== 0);
192 r
= fill_fixed_size(imp
, &data
, sizeof(uint64_t));
196 imp
->data_size
= unaligned_read_le64(data
);
197 if (imp
->data_size
> DATA_SIZE_MAX
) {
198 log_error("Stream declares field with size %zu > DATA_SIZE_MAX = %u",
199 imp
->data_size
, DATA_SIZE_MAX
);
202 if (imp
->data_size
== 0)
203 log_warning("Binary field with zero length");
208 static int get_data_data(JournalImporter
*imp
, void **data
) {
213 assert(imp
->state
== IMPORTER_STATE_DATA
);
215 r
= fill_fixed_size(imp
, data
, imp
->data_size
);
222 static int get_data_newline(JournalImporter
*imp
) {
227 assert(imp
->state
== IMPORTER_STATE_DATA_FINISH
);
229 r
= fill_fixed_size(imp
, (void**) &data
, 1);
235 log_error("expected newline, got '%c'", *data
);
242 static int process_dunder(JournalImporter
*imp
, char *line
, size_t n
) {
243 const char *timestamp
;
248 assert(line
[n
-1] == '\n');
250 /* XXX: is it worth to support timestamps in extended format?
251 * We don't produce them, but who knows... */
253 timestamp
= startswith(line
, "__CURSOR=");
255 /* ignore __CURSOR */
258 timestamp
= startswith(line
, "__REALTIME_TIMESTAMP=");
260 long long unsigned x
;
262 r
= safe_atollu(timestamp
, &x
);
264 log_warning("Failed to parse __REALTIME_TIMESTAMP: '%s'", timestamp
);
266 imp
->ts
.realtime
= x
;
267 return r
< 0 ? r
: 1;
270 timestamp
= startswith(line
, "__MONOTONIC_TIMESTAMP=");
272 long long unsigned x
;
274 r
= safe_atollu(timestamp
, &x
);
276 log_warning("Failed to parse __MONOTONIC_TIMESTAMP: '%s'", timestamp
);
278 imp
->ts
.monotonic
= x
;
279 return r
< 0 ? r
: 1;
282 timestamp
= startswith(line
, "__");
284 log_notice("Unknown dunder line %s", line
);
292 int journal_importer_process_data(JournalImporter
*imp
) {
296 case IMPORTER_STATE_LINE
: {
300 assert(imp
->data_size
== 0);
302 r
= get_line(imp
, &line
, &n
);
306 imp
->state
= IMPORTER_STATE_EOF
;
310 assert(line
[n
-1] == '\n');
313 log_trace("Received empty line, event is ready");
317 r
= process_dunder(imp
, line
, n
);
319 return r
< 0 ? r
: 0;
324 LLLLLLLL0011223344...\n
326 sep
= memchr(line
, '=', n
);
331 r
= iovw_put(&imp
->iovw
, line
, n
);
335 /* replace \n with = */
339 imp
->state
= IMPORTER_STATE_DATA_START
;
341 /* we cannot put the field in iovec until we have all data */
344 log_trace("Received: %.*s (%s)", (int) n
, line
, sep
? "text" : "binary");
346 return 0; /* continue */
349 case IMPORTER_STATE_DATA_START
:
350 assert(imp
->data_size
== 0);
352 r
= get_data_size(imp
);
353 // log_debug("get_data_size() -> %d", r);
357 imp
->state
= IMPORTER_STATE_EOF
;
361 imp
->state
= imp
->data_size
> 0 ?
362 IMPORTER_STATE_DATA
: IMPORTER_STATE_DATA_FINISH
;
364 return 0; /* continue */
366 case IMPORTER_STATE_DATA
: {
370 assert(imp
->data_size
> 0);
372 r
= get_data_data(imp
, &data
);
373 // log_debug("get_data_data() -> %d", r);
377 imp
->state
= IMPORTER_STATE_EOF
;
383 field
= (char*) data
- sizeof(uint64_t) - imp
->field_len
;
384 memmove(field
+ sizeof(uint64_t), field
, imp
->field_len
);
386 r
= iovw_put(&imp
->iovw
, field
+ sizeof(uint64_t), imp
->field_len
+ imp
->data_size
);
390 imp
->state
= IMPORTER_STATE_DATA_FINISH
;
392 return 0; /* continue */
395 case IMPORTER_STATE_DATA_FINISH
:
396 r
= get_data_newline(imp
);
397 // log_debug("get_data_newline() -> %d", r);
401 imp
->state
= IMPORTER_STATE_EOF
;
406 imp
->state
= IMPORTER_STATE_LINE
;
408 return 0; /* continue */
410 assert_not_reached("wtf?");
414 int journal_importer_push_data(JournalImporter
*imp
, const char *data
, size_t size
) {
416 assert(imp
->state
!= IMPORTER_STATE_EOF
);
418 if (!realloc_buffer(imp
, imp
->filled
+ size
)) {
419 log_error("Failed to store received data of size %zu "
420 "(in addition to existing %zu bytes with %zu filled): %s",
421 size
, imp
->size
, imp
->filled
, strerror(ENOMEM
));
425 memcpy(imp
->buf
+ imp
->filled
, data
, size
);
431 void journal_importer_drop_iovw(JournalImporter
*imp
) {
432 size_t remain
, target
;
434 /* This function drops processed data that along with the iovw that points at it */
436 iovw_free_contents(&imp
->iovw
);
438 /* possibly reset buffer position */
439 remain
= imp
->filled
- imp
->offset
;
441 if (remain
== 0) /* no brainer */
442 imp
->offset
= imp
->scanned
= imp
->filled
= 0;
443 else if (imp
->offset
> imp
->size
- imp
->filled
&&
444 imp
->offset
> remain
) {
445 memcpy(imp
->buf
, imp
->buf
+ imp
->offset
, remain
);
446 imp
->offset
= imp
->scanned
= 0;
447 imp
->filled
= remain
;
451 while (target
> 16 * LINE_CHUNK
&& imp
->filled
< target
/ 2)
453 if (target
< imp
->size
) {
456 tmp
= realloc(imp
->buf
, target
);
458 log_warning("Failed to reallocate buffer to (smaller) size %zu",
461 log_debug("Reallocated buffer from %zu to %zu bytes",
469 bool journal_importer_eof(const JournalImporter
*imp
) {
470 return imp
->state
== IMPORTER_STATE_EOF
;