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