]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal-remote/journal-remote-write.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / journal-remote / journal-remote-write.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2012 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 "journal-remote.h"
23
24 static int do_rotate(JournalFile **f, bool compress, bool seal) {
25 int r = journal_file_rotate(f, compress, seal, NULL);
26 if (r < 0) {
27 if (*f)
28 log_error_errno(r, "Failed to rotate %s: %m", (*f)->path);
29 else
30 log_error_errno(r, "Failed to create rotated journal: %m");
31 }
32
33 return r;
34 }
35
36 Writer* writer_new(RemoteServer *server) {
37 Writer *w;
38
39 w = new0(Writer, 1);
40 if (!w)
41 return NULL;
42
43 memset(&w->metrics, 0xFF, sizeof(w->metrics));
44
45 w->mmap = mmap_cache_new();
46 if (!w->mmap)
47 return mfree(w);
48
49 w->n_ref = 1;
50 w->server = server;
51
52 return w;
53 }
54
55 Writer* writer_free(Writer *w) {
56 if (!w)
57 return NULL;
58
59 if (w->journal) {
60 log_debug("Closing journal file %s.", w->journal->path);
61 journal_file_close(w->journal);
62 }
63
64 if (w->server && w->hashmap_key)
65 hashmap_remove(w->server->writers, w->hashmap_key);
66
67 free(w->hashmap_key);
68
69 if (w->mmap)
70 mmap_cache_unref(w->mmap);
71
72 return mfree(w);
73 }
74
75 Writer* writer_unref(Writer *w) {
76 if (w && (-- w->n_ref <= 0))
77 writer_free(w);
78
79 return NULL;
80 }
81
82 Writer* writer_ref(Writer *w) {
83 if (w)
84 assert_se(++ w->n_ref >= 2);
85
86 return w;
87 }
88
89 int writer_write(Writer *w,
90 struct iovec_wrapper *iovw,
91 dual_timestamp *ts,
92 bool compress,
93 bool seal) {
94 int r;
95
96 assert(w);
97 assert(iovw);
98 assert(iovw->count > 0);
99
100 if (journal_file_rotate_suggested(w->journal, 0)) {
101 log_info("%s: Journal header limits reached or header out-of-date, rotating",
102 w->journal->path);
103 r = do_rotate(&w->journal, compress, seal);
104 if (r < 0)
105 return r;
106 }
107
108 r = journal_file_append_entry(w->journal, ts, iovw->iovec, iovw->count,
109 &w->seqnum, NULL, NULL);
110 if (r >= 0) {
111 if (w->server)
112 w->server->event_count += 1;
113 return 1;
114 }
115
116 log_debug_errno(r, "%s: Write failed, rotating: %m", w->journal->path);
117 r = do_rotate(&w->journal, compress, seal);
118 if (r < 0)
119 return r;
120 else
121 log_debug("%s: Successfully rotated journal", w->journal->path);
122
123 log_debug("Retrying write.");
124 r = journal_file_append_entry(w->journal, ts, iovw->iovec, iovw->count,
125 &w->seqnum, NULL, NULL);
126 if (r < 0)
127 return r;
128
129 if (w->server)
130 w->server->event_count += 1;
131 return 1;
132 }