]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal-remote/journal-remote-parse.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / journal-remote / journal-remote-parse.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2014 Zbigniew Jędrzejewski-Szmek
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include "alloc-util.h"
22 #include "fd-util.h"
23 #include "journal-remote-parse.h"
24 #include "journald-native.h"
25 #include "parse-util.h"
26 #include "string-util.h"
27
28 void source_free(RemoteSource *source) {
29 if (!source)
30 return;
31
32 journal_importer_cleanup(&source->importer);
33
34 log_debug("Writer ref count %i", source->writer->n_ref);
35 writer_unref(source->writer);
36
37 sd_event_source_unref(source->event);
38 sd_event_source_unref(source->buffer_event);
39
40 free(source);
41 }
42
43 /**
44 * Initialize zero-filled source with given values. On success, takes
45 * ownership of fd, name, and writer, otherwise does not touch them.
46 */
47 RemoteSource* source_new(int fd, bool passive_fd, char *name, Writer *writer) {
48
49 RemoteSource *source;
50
51 log_debug("Creating source for %sfd:%d (%s)",
52 passive_fd ? "passive " : "", fd, name);
53
54 assert(fd >= 0);
55
56 source = new0(RemoteSource, 1);
57 if (!source)
58 return NULL;
59
60 source->importer.fd = fd;
61 source->importer.passive_fd = passive_fd;
62 source->importer.name = name;
63
64 source->writer = writer;
65
66 return source;
67 }
68
69 int process_source(RemoteSource *source, bool compress, bool seal) {
70 int r;
71
72 assert(source);
73 assert(source->writer);
74
75 r = journal_importer_process_data(&source->importer);
76 if (r <= 0)
77 return r;
78
79 /* We have a full event */
80 log_trace("Received full event from source@%p fd:%d (%s)",
81 source, source->importer.fd, source->importer.name);
82
83 if (source->importer.iovw.count == 0) {
84 log_warning("Entry with no payload, skipping");
85 goto freeing;
86 }
87
88 assert(source->importer.iovw.iovec);
89
90 r = writer_write(source->writer, &source->importer.iovw, &source->importer.ts, compress, seal);
91 if (r < 0)
92 log_error_errno(r, "Failed to write entry of %zu bytes: %m",
93 iovw_size(&source->importer.iovw));
94 else
95 r = 1;
96
97 freeing:
98 journal_importer_drop_iovw(&source->importer);
99 return r;
100 }