1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #pragma once
4 
5 #include <stddef.h>
6 #include <stdbool.h>
7 #include <sys/uio.h>
8 
9 #include "sd-id128.h"
10 
11 #include "io-util.h"
12 #include "time-util.h"
13 
14 /* Make sure not to make this smaller than the maximum coredump size.
15  * See JOURNAL_SIZE_MAX in coredump.c */
16 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
17 #define ENTRY_SIZE_MAX (1024*1024*770u)
18 #define DATA_SIZE_MAX (1024*1024*768u)
19 #else
20 #define ENTRY_SIZE_MAX (1024*1024*13u)
21 #define DATA_SIZE_MAX (1024*1024*11u)
22 #endif
23 #define LINE_CHUNK 8*1024u
24 
25 /* The maximum number of fields in an entry */
26 #define ENTRY_FIELD_COUNT_MAX 1024
27 
28 typedef struct JournalImporter {
29         int fd;
30         bool passive_fd;
31         char *name;
32 
33         char *buf;
34         size_t offset;     /* offset to the beginning of live data in the buffer */
35         size_t scanned;    /* number of bytes since the beginning of data without a newline */
36         size_t filled;     /* total number of bytes in the buffer */
37 
38         size_t field_len;  /* used for binary fields: the field name length */
39         size_t data_size;  /* and the size of the binary data chunk being processed */
40 
41         struct iovec_wrapper iovw;
42 
43         int state;
44         dual_timestamp ts;
45         sd_id128_t boot_id;
46 } JournalImporter;
47 
48 #define JOURNAL_IMPORTER_INIT(_fd) { .fd = (_fd), .iovw = {} }
49 #define JOURNAL_IMPORTER_MAKE(_fd) (JournalImporter) JOURNAL_IMPORTER_INIT(_fd)
50 
51 void journal_importer_cleanup(JournalImporter *);
52 int journal_importer_process_data(JournalImporter *);
53 int journal_importer_push_data(JournalImporter *, const char *data, size_t size);
54 void journal_importer_drop_iovw(JournalImporter *);
55 bool journal_importer_eof(const JournalImporter *);
56 
journal_importer_bytes_remaining(const JournalImporter * imp)57 static inline size_t journal_importer_bytes_remaining(const JournalImporter *imp) {
58         return imp->filled;
59 }
60