]> git.ipfire.org Git - thirdparty/systemd.git/blame - 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
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
fdfccdbc
ZJS
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
b5efdb8a 21#include "alloc-util.h"
3ffd4af2
LP
22#include "fd-util.h"
23#include "journal-remote-parse.h"
fdfccdbc 24#include "journald-native.h"
6bedfcbb 25#include "parse-util.h"
07630cea 26#include "string-util.h"
fdfccdbc 27
fdfccdbc
ZJS
28void source_free(RemoteSource *source) {
29 if (!source)
30 return;
31
b18453ed 32 journal_importer_cleanup(&source->importer);
8201af08 33
1fa2f38f 34 log_debug("Writer ref count %i", source->writer->n_ref);
9ff48d09
ZJS
35 writer_unref(source->writer);
36
8201af08 37 sd_event_source_unref(source->event);
043945b9 38 sd_event_source_unref(source->buffer_event);
8201af08 39
fdfccdbc
ZJS
40 free(source);
41}
42
9ff48d09
ZJS
43/**
44 * Initialize zero-filled source with given values. On success, takes
2ddb70d2 45 * ownership of fd, name, and writer, otherwise does not touch them.
9ff48d09
ZJS
46 */
47RemoteSource* 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
b18453ed
ZJS
60 source->importer.fd = fd;
61 source->importer.passive_fd = passive_fd;
62 source->importer.name = name;
63
9ff48d09
ZJS
64 source->writer = writer;
65
66 return source;
67}
68
9ff48d09 69int process_source(RemoteSource *source, bool compress, bool seal) {
fdfccdbc
ZJS
70 int r;
71
72 assert(source);
9ff48d09 73 assert(source->writer);
fdfccdbc 74
b18453ed 75 r = journal_importer_process_data(&source->importer);
fdfccdbc
ZJS
76 if (r <= 0)
77 return r;
78
79 /* We have a full event */
0e72da6f 80 log_trace("Received full event from source@%p fd:%d (%s)",
b18453ed 81 source, source->importer.fd, source->importer.name);
fdfccdbc 82
b18453ed 83 if (source->importer.iovw.count == 0) {
fdfccdbc
ZJS
84 log_warning("Entry with no payload, skipping");
85 goto freeing;
86 }
87
b18453ed 88 assert(source->importer.iovw.iovec);
fdfccdbc 89
b18453ed 90 r = writer_write(source->writer, &source->importer.iovw, &source->importer.ts, compress, seal);
fdfccdbc 91 if (r < 0)
c33b3297 92 log_error_errno(r, "Failed to write entry of %zu bytes: %m",
b18453ed 93 iovw_size(&source->importer.iovw));
fdfccdbc
ZJS
94 else
95 r = 1;
96
97 freeing:
b18453ed 98 journal_importer_drop_iovw(&source->importer);
fdfccdbc
ZJS
99 return r;
100}