]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
fdfccdbc | 2 | |
b5efdb8a | 3 | #include "alloc-util.h" |
3ffd4af2 LP |
4 | #include "fd-util.h" |
5 | #include "journal-remote-parse.h" | |
fdfccdbc | 6 | #include "journald-native.h" |
6bedfcbb | 7 | #include "parse-util.h" |
07630cea | 8 | #include "string-util.h" |
fdfccdbc | 9 | |
fdfccdbc ZJS |
10 | void source_free(RemoteSource *source) { |
11 | if (!source) | |
12 | return; | |
13 | ||
b18453ed | 14 | journal_importer_cleanup(&source->importer); |
8201af08 | 15 | |
1fa2f38f | 16 | log_debug("Writer ref count %i", source->writer->n_ref); |
9ff48d09 ZJS |
17 | writer_unref(source->writer); |
18 | ||
8201af08 | 19 | sd_event_source_unref(source->event); |
043945b9 | 20 | sd_event_source_unref(source->buffer_event); |
8201af08 | 21 | |
fdfccdbc ZJS |
22 | free(source); |
23 | } | |
24 | ||
9ff48d09 ZJS |
25 | /** |
26 | * Initialize zero-filled source with given values. On success, takes | |
2ddb70d2 | 27 | * ownership of fd, name, and writer, otherwise does not touch them. |
9ff48d09 ZJS |
28 | */ |
29 | RemoteSource* source_new(int fd, bool passive_fd, char *name, Writer *writer) { | |
9ff48d09 ZJS |
30 | RemoteSource *source; |
31 | ||
32 | log_debug("Creating source for %sfd:%d (%s)", | |
33 | passive_fd ? "passive " : "", fd, name); | |
34 | ||
35 | assert(fd >= 0); | |
36 | ||
37 | source = new0(RemoteSource, 1); | |
38 | if (!source) | |
39 | return NULL; | |
40 | ||
11e6d971 | 41 | source->importer = JOURNAL_IMPORTER_MAKE(fd); |
b18453ed ZJS |
42 | source->importer.passive_fd = passive_fd; |
43 | source->importer.name = name; | |
44 | ||
9ff48d09 ZJS |
45 | source->writer = writer; |
46 | ||
47 | return source; | |
48 | } | |
49 | ||
9ff48d09 | 50 | int process_source(RemoteSource *source, bool compress, bool seal) { |
fdfccdbc ZJS |
51 | int r; |
52 | ||
53 | assert(source); | |
9ff48d09 | 54 | assert(source->writer); |
fdfccdbc | 55 | |
b18453ed | 56 | r = journal_importer_process_data(&source->importer); |
fdfccdbc ZJS |
57 | if (r <= 0) |
58 | return r; | |
59 | ||
60 | /* We have a full event */ | |
0e72da6f | 61 | log_trace("Received full event from source@%p fd:%d (%s)", |
b18453ed | 62 | source, source->importer.fd, source->importer.name); |
fdfccdbc | 63 | |
b18453ed | 64 | if (source->importer.iovw.count == 0) { |
fdfccdbc ZJS |
65 | log_warning("Entry with no payload, skipping"); |
66 | goto freeing; | |
67 | } | |
68 | ||
b18453ed | 69 | assert(source->importer.iovw.iovec); |
fdfccdbc | 70 | |
924426a7 CM |
71 | r = writer_write(source->writer, |
72 | &source->importer.iovw, | |
73 | &source->importer.ts, | |
74 | &source->importer.boot_id, | |
75 | compress, seal); | |
e743ce70 ZJS |
76 | if (r == -EBADMSG) { |
77 | log_error_errno(r, "Entry is invalid, ignoring."); | |
78 | r = 0; | |
79 | } else if (r < 0) | |
c33b3297 | 80 | log_error_errno(r, "Failed to write entry of %zu bytes: %m", |
b18453ed | 81 | iovw_size(&source->importer.iovw)); |
fdfccdbc ZJS |
82 | else |
83 | r = 1; | |
84 | ||
85 | freeing: | |
b18453ed | 86 | journal_importer_drop_iovw(&source->importer); |
fdfccdbc ZJS |
87 | return r; |
88 | } |