]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal-remote/journal-remote-parse.c
Merge pull request #5600 from fbuihuu/make-logind-restartable
[thirdparty/systemd.git] / src / journal-remote / journal-remote-parse.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2014 Zbigniew Jędrzejewski-Szmek
5
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.
10
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.
15
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/>.
18 ***/
19
20 #include "alloc-util.h"
21 #include "fd-util.h"
22 #include "journal-remote-parse.h"
23 #include "journald-native.h"
24 #include "parse-util.h"
25 #include "string-util.h"
26
27 void source_free(RemoteSource *source) {
28 if (!source)
29 return;
30
31 journal_importer_cleanup(&source->importer);
32
33 log_debug("Writer ref count %i", source->writer->n_ref);
34 writer_unref(source->writer);
35
36 sd_event_source_unref(source->event);
37 sd_event_source_unref(source->buffer_event);
38
39 free(source);
40 }
41
42 /**
43 * Initialize zero-filled source with given values. On success, takes
44 * ownership of fd, name, and writer, otherwise does not touch them.
45 */
46 RemoteSource* source_new(int fd, bool passive_fd, char *name, Writer *writer) {
47
48 RemoteSource *source;
49
50 log_debug("Creating source for %sfd:%d (%s)",
51 passive_fd ? "passive " : "", fd, name);
52
53 assert(fd >= 0);
54
55 source = new0(RemoteSource, 1);
56 if (!source)
57 return NULL;
58
59 source->importer.fd = fd;
60 source->importer.passive_fd = passive_fd;
61 source->importer.name = name;
62
63 source->writer = writer;
64
65 return source;
66 }
67
68 int process_source(RemoteSource *source, bool compress, bool seal) {
69 int r;
70
71 assert(source);
72 assert(source->writer);
73
74 r = journal_importer_process_data(&source->importer);
75 if (r <= 0)
76 return r;
77
78 /* We have a full event */
79 log_trace("Received full event from source@%p fd:%d (%s)",
80 source, source->importer.fd, source->importer.name);
81
82 if (source->importer.iovw.count == 0) {
83 log_warning("Entry with no payload, skipping");
84 goto freeing;
85 }
86
87 assert(source->importer.iovw.iovec);
88
89 r = writer_write(source->writer, &source->importer.iovw, &source->importer.ts, compress, seal);
90 if (r < 0)
91 log_error_errno(r, "Failed to write entry of %zu bytes: %m",
92 iovw_size(&source->importer.iovw));
93 else
94 r = 1;
95
96 freeing:
97 journal_importer_drop_iovw(&source->importer);
98 return r;
99 }