2 This file is part of systemd.
4 Copyright 2014 Zbigniew Jędrzejewski-Szmek
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "alloc-util.h"
23 #include "journal-importer.h"
25 #include "parse-util.h"
26 #include "string-util.h"
29 IMPORTER_STATE_LINE
= 0, /* waiting to read, or reading line */
30 IMPORTER_STATE_DATA_START
, /* reading binary data header */
31 IMPORTER_STATE_DATA
, /* reading binary data */
32 IMPORTER_STATE_DATA_FINISH
, /* expecting newline */
33 IMPORTER_STATE_EOF
, /* done */
36 static int iovw_put(struct iovec_wrapper
*iovw
, void* data
, size_t len
) {
37 if (!GREEDY_REALLOC(iovw
->iovec
, iovw
->size_bytes
, iovw
->count
+ 1))
40 iovw
->iovec
[iovw
->count
++] = (struct iovec
) {data
, len
};
44 static void iovw_free_contents(struct iovec_wrapper
*iovw
) {
45 iovw
->iovec
= mfree(iovw
->iovec
);
46 iovw
->size_bytes
= iovw
->count
= 0;
49 static void iovw_rebase(struct iovec_wrapper
*iovw
, char *old
, char *new) {
52 for (i
= 0; i
< iovw
->count
; i
++)
53 iovw
->iovec
[i
].iov_base
= (char*) iovw
->iovec
[i
].iov_base
- old
+ new;
56 size_t iovw_size(struct iovec_wrapper
*iovw
) {
59 for (i
= 0; i
< iovw
->count
; i
++)
60 n
+= iovw
->iovec
[i
].iov_len
;
65 void journal_importer_cleanup(JournalImporter
*imp
) {
66 if (imp
->fd
>= 0 && !imp
->passive_fd
) {
67 log_debug("Closing %s (fd=%d)", imp
->name
?: "importer", imp
->fd
);
72 iovw_free_contents(&imp
->iovw
);
75 static char* realloc_buffer(JournalImporter
*imp
, size_t size
) {
76 char *b
, *old
= imp
->buf
;
78 b
= GREEDY_REALLOC(imp
->buf
, imp
->size
, size
);
82 iovw_rebase(&imp
->iovw
, old
, imp
->buf
);
87 static int get_line(JournalImporter
*imp
, char **line
, size_t *size
) {
92 assert(imp
->state
== IMPORTER_STATE_LINE
);
93 assert(imp
->offset
<= imp
->filled
);
94 assert(imp
->filled
<= imp
->size
);
95 assert(imp
->buf
== NULL
|| imp
->size
> 0);
100 size_t start
= MAX(imp
->scanned
, imp
->offset
);
102 c
= memchr(imp
->buf
+ start
, '\n',
103 imp
->filled
- start
);
108 imp
->scanned
= imp
->filled
;
109 if (imp
->scanned
>= DATA_SIZE_MAX
) {
110 log_error("Entry is bigger than %u bytes.", DATA_SIZE_MAX
);
115 /* we have to wait for some data to come to us */
118 /* We know that imp->filled is at most DATA_SIZE_MAX, so if
119 we reallocate it, we'll increase the size at least a bit. */
120 assert_cc(DATA_SIZE_MAX
< ENTRY_SIZE_MAX
);
121 if (imp
->size
- imp
->filled
< LINE_CHUNK
&&
122 !realloc_buffer(imp
, MIN(imp
->filled
+ LINE_CHUNK
, ENTRY_SIZE_MAX
)))
126 assert(imp
->size
- imp
->filled
>= LINE_CHUNK
||
127 imp
->size
== ENTRY_SIZE_MAX
);
130 imp
->buf
+ imp
->filled
,
131 imp
->size
- imp
->filled
);
134 log_error_errno(errno
, "read(%d, ..., %zu): %m",
136 imp
->size
- imp
->filled
);
144 *line
= imp
->buf
+ imp
->offset
;
145 *size
= c
+ 1 - imp
->buf
- imp
->offset
;
146 imp
->offset
+= *size
;
151 static int fill_fixed_size(JournalImporter
*imp
, void **data
, size_t size
) {
154 assert(imp
->state
== IMPORTER_STATE_DATA_START
||
155 imp
->state
== IMPORTER_STATE_DATA
||
156 imp
->state
== IMPORTER_STATE_DATA_FINISH
);
157 assert(size
<= DATA_SIZE_MAX
);
158 assert(imp
->offset
<= imp
->filled
);
159 assert(imp
->filled
<= imp
->size
);
160 assert(imp
->buf
!= NULL
|| imp
->size
== 0);
161 assert(imp
->buf
== NULL
|| imp
->size
> 0);
162 assert(imp
->fd
>= 0);
165 while (imp
->filled
- imp
->offset
< size
) {
169 /* we have to wait for some data to come to us */
172 if (!realloc_buffer(imp
, imp
->offset
+ size
))
175 n
= read(imp
->fd
, imp
->buf
+ imp
->filled
,
176 imp
->size
- imp
->filled
);
179 log_error_errno(errno
, "read(%d, ..., %zu): %m", imp
->fd
,
180 imp
->size
- imp
->filled
);
188 *data
= imp
->buf
+ imp
->offset
;
194 static int get_data_size(JournalImporter
*imp
) {
199 assert(imp
->state
== IMPORTER_STATE_DATA_START
);
200 assert(imp
->data_size
== 0);
202 r
= fill_fixed_size(imp
, &data
, sizeof(uint64_t));
206 imp
->data_size
= le64toh( *(uint64_t *) data
);
207 if (imp
->data_size
> DATA_SIZE_MAX
) {
208 log_error("Stream declares field with size %zu > DATA_SIZE_MAX = %u",
209 imp
->data_size
, DATA_SIZE_MAX
);
212 if (imp
->data_size
== 0)
213 log_warning("Binary field with zero length");
218 static int get_data_data(JournalImporter
*imp
, void **data
) {
223 assert(imp
->state
== IMPORTER_STATE_DATA
);
225 r
= fill_fixed_size(imp
, data
, imp
->data_size
);
232 static int get_data_newline(JournalImporter
*imp
) {
237 assert(imp
->state
== IMPORTER_STATE_DATA_FINISH
);
239 r
= fill_fixed_size(imp
, (void**) &data
, 1);
245 log_error("expected newline, got '%c'", *data
);
252 static int process_dunder(JournalImporter
*imp
, char *line
, size_t n
) {
253 const char *timestamp
;
258 assert(line
[n
-1] == '\n');
260 /* XXX: is it worth to support timestamps in extended format?
261 * We don't produce them, but who knows... */
263 timestamp
= startswith(line
, "__CURSOR=");
265 /* ignore __CURSOR */
268 timestamp
= startswith(line
, "__REALTIME_TIMESTAMP=");
270 long long unsigned x
;
272 r
= safe_atollu(timestamp
, &x
);
274 log_warning("Failed to parse __REALTIME_TIMESTAMP: '%s'", timestamp
);
276 imp
->ts
.realtime
= x
;
277 return r
< 0 ? r
: 1;
280 timestamp
= startswith(line
, "__MONOTONIC_TIMESTAMP=");
282 long long unsigned x
;
284 r
= safe_atollu(timestamp
, &x
);
286 log_warning("Failed to parse __MONOTONIC_TIMESTAMP: '%s'", timestamp
);
288 imp
->ts
.monotonic
= x
;
289 return r
< 0 ? r
: 1;
292 timestamp
= startswith(line
, "__");
294 log_notice("Unknown dunder line %s", line
);
302 int journal_importer_process_data(JournalImporter
*imp
) {
306 case IMPORTER_STATE_LINE
: {
310 assert(imp
->data_size
== 0);
312 r
= get_line(imp
, &line
, &n
);
316 imp
->state
= IMPORTER_STATE_EOF
;
320 assert(line
[n
-1] == '\n');
323 log_trace("Received empty line, event is ready");
327 r
= process_dunder(imp
, line
, n
);
329 return r
< 0 ? r
: 0;
334 LLLLLLLL0011223344...\n
336 sep
= memchr(line
, '=', n
);
341 r
= iovw_put(&imp
->iovw
, line
, n
);
345 /* replace \n with = */
349 imp
->state
= IMPORTER_STATE_DATA_START
;
351 /* we cannot put the field in iovec until we have all data */
354 log_trace("Received: %.*s (%s)", (int) n
, line
, sep
? "text" : "binary");
356 return 0; /* continue */
359 case IMPORTER_STATE_DATA_START
:
360 assert(imp
->data_size
== 0);
362 r
= get_data_size(imp
);
363 // log_debug("get_data_size() -> %d", r);
367 imp
->state
= IMPORTER_STATE_EOF
;
371 imp
->state
= imp
->data_size
> 0 ?
372 IMPORTER_STATE_DATA
: IMPORTER_STATE_DATA_FINISH
;
374 return 0; /* continue */
376 case IMPORTER_STATE_DATA
: {
380 assert(imp
->data_size
> 0);
382 r
= get_data_data(imp
, &data
);
383 // log_debug("get_data_data() -> %d", r);
387 imp
->state
= IMPORTER_STATE_EOF
;
393 field
= (char*) data
- sizeof(uint64_t) - imp
->field_len
;
394 memmove(field
+ sizeof(uint64_t), field
, imp
->field_len
);
396 r
= iovw_put(&imp
->iovw
, field
+ sizeof(uint64_t), imp
->field_len
+ imp
->data_size
);
400 imp
->state
= IMPORTER_STATE_DATA_FINISH
;
402 return 0; /* continue */
405 case IMPORTER_STATE_DATA_FINISH
:
406 r
= get_data_newline(imp
);
407 // log_debug("get_data_newline() -> %d", r);
411 imp
->state
= IMPORTER_STATE_EOF
;
416 imp
->state
= IMPORTER_STATE_LINE
;
418 return 0; /* continue */
420 assert_not_reached("wtf?");
424 int journal_importer_push_data(JournalImporter
*imp
, const char *data
, size_t size
) {
426 assert(imp
->state
!= IMPORTER_STATE_EOF
);
428 if (!realloc_buffer(imp
, imp
->filled
+ size
)) {
429 log_error("Failed to store received data of size %zu "
430 "(in addition to existing %zu bytes with %zu filled): %s",
431 size
, imp
->size
, imp
->filled
, strerror(ENOMEM
));
435 memcpy(imp
->buf
+ imp
->filled
, data
, size
);
441 void journal_importer_drop_iovw(JournalImporter
*imp
) {
442 size_t remain
, target
;
444 /* This function drops processed data that along with the iovw that points at it */
446 iovw_free_contents(&imp
->iovw
);
448 /* possibly reset buffer position */
449 remain
= imp
->filled
- imp
->offset
;
451 if (remain
== 0) /* no brainer */
452 imp
->offset
= imp
->scanned
= imp
->filled
= 0;
453 else if (imp
->offset
> imp
->size
- imp
->filled
&&
454 imp
->offset
> remain
) {
455 memcpy(imp
->buf
, imp
->buf
+ imp
->offset
, remain
);
456 imp
->offset
= imp
->scanned
= 0;
457 imp
->filled
= remain
;
461 while (target
> 16 * LINE_CHUNK
&& imp
->filled
< target
/ 2)
463 if (target
< imp
->size
) {
466 tmp
= realloc(imp
->buf
, target
);
468 log_warning("Failed to reallocate buffer to (smaller) size %zu",
471 log_debug("Reallocated buffer from %zu to %zu bytes",
479 bool journal_importer_eof(const JournalImporter
*imp
) {
480 return imp
->state
== IMPORTER_STATE_EOF
;