]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal-remote/journal-remote-write.c
journal: asynchronous journal_file_set_offline()
[thirdparty/systemd.git] / src / journal-remote / journal-remote-write.c
CommitLineData
fdfccdbc
ZJS
1/***
2 This file is part of systemd.
3
4 Copyright 2012 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
b5efdb8a 20#include "alloc-util.h"
9ff48d09 21#include "journal-remote.h"
fdfccdbc
ZJS
22
23int iovw_put(struct iovec_wrapper *iovw, void* data, size_t len) {
24 if (!GREEDY_REALLOC(iovw->iovec, iovw->size_bytes, iovw->count + 1))
25 return log_oom();
26
27 iovw->iovec[iovw->count++] = (struct iovec) {data, len};
28 return 0;
29}
30
31void iovw_free_contents(struct iovec_wrapper *iovw) {
a1e58e8e 32 iovw->iovec = mfree(iovw->iovec);
fdfccdbc
ZJS
33 iovw->size_bytes = iovw->count = 0;
34}
35
36size_t iovw_size(struct iovec_wrapper *iovw) {
37 size_t n = 0, i;
38
92b10cbc 39 for (i = 0; i < iovw->count; i++)
fdfccdbc
ZJS
40 n += iovw->iovec[i].iov_len;
41
42 return n;
43}
44
92b10cbc
ZJS
45void iovw_rebase(struct iovec_wrapper *iovw, char *old, char *new) {
46 size_t i;
47
48 for (i = 0; i < iovw->count; i++)
49 iovw->iovec[i].iov_base = (char*) iovw->iovec[i].iov_base - old + new;
50}
51
fdfccdbc
ZJS
52/**********************************************************************
53 **********************************************************************
54 **********************************************************************/
55
56static int do_rotate(JournalFile **f, bool compress, bool seal) {
57 int r = journal_file_rotate(f, compress, seal);
58 if (r < 0) {
59 if (*f)
c33b3297 60 log_error_errno(r, "Failed to rotate %s: %m", (*f)->path);
fdfccdbc 61 else
c33b3297 62 log_error_errno(r, "Failed to create rotated journal: %m");
fdfccdbc
ZJS
63 }
64
65 return r;
66}
67
9ff48d09
ZJS
68Writer* writer_new(RemoteServer *server) {
69 Writer *w;
fdfccdbc 70
9ff48d09
ZJS
71 w = new0(Writer, 1);
72 if (!w)
73 return NULL;
fdfccdbc 74
9ff48d09 75 memset(&w->metrics, 0xFF, sizeof(w->metrics));
fdfccdbc 76
9ff48d09
ZJS
77 w->mmap = mmap_cache_new();
78 if (!w->mmap) {
79 free(w);
80 return NULL;
81 }
fdfccdbc 82
9ff48d09
ZJS
83 w->n_ref = 1;
84 w->server = server;
fdfccdbc 85
9ff48d09 86 return w;
fdfccdbc
ZJS
87}
88
9ff48d09
ZJS
89Writer* writer_free(Writer *w) {
90 if (!w)
91 return NULL;
92
93 if (w->journal) {
94 log_debug("Closing journal file %s.", w->journal->path);
95 journal_file_close(w->journal);
eacbb4d3 96 }
9ff48d09 97
dd87b184
ZJS
98 if (w->server && w->hashmap_key)
99 hashmap_remove(w->server->writers, w->hashmap_key);
9ff48d09
ZJS
100
101 free(w->hashmap_key);
102
103 if (w->mmap)
104 mmap_cache_unref(w->mmap);
105
106 free(w);
107
108 return NULL;
109}
110
111Writer* writer_unref(Writer *w) {
112 if (w && (-- w->n_ref <= 0))
113 writer_free(w);
114
115 return NULL;
fdfccdbc
ZJS
116}
117
9ff48d09
ZJS
118Writer* writer_ref(Writer *w) {
119 if (w)
120 assert_se(++ w->n_ref >= 2);
121
122 return w;
123}
124
dd87b184 125int writer_write(Writer *w,
fdfccdbc
ZJS
126 struct iovec_wrapper *iovw,
127 dual_timestamp *ts,
128 bool compress,
129 bool seal) {
130 int r;
131
dd87b184 132 assert(w);
fdfccdbc
ZJS
133 assert(iovw);
134 assert(iovw->count > 0);
135
dd87b184 136 if (journal_file_rotate_suggested(w->journal, 0)) {
fdfccdbc 137 log_info("%s: Journal header limits reached or header out-of-date, rotating",
dd87b184
ZJS
138 w->journal->path);
139 r = do_rotate(&w->journal, compress, seal);
fdfccdbc
ZJS
140 if (r < 0)
141 return r;
142 }
143
dd87b184
ZJS
144 r = journal_file_append_entry(w->journal, ts, iovw->iovec, iovw->count,
145 &w->seqnum, NULL, NULL);
146 if (r >= 0) {
147 if (w->server)
148 w->server->event_count += 1;
fdfccdbc 149 return 1;
dd87b184 150 }
fdfccdbc 151
da927ba9 152 log_debug_errno(r, "%s: Write failed, rotating: %m", w->journal->path);
dd87b184 153 r = do_rotate(&w->journal, compress, seal);
fdfccdbc
ZJS
154 if (r < 0)
155 return r;
a83f4037 156 else
0e72da6f 157 log_debug("%s: Successfully rotated journal", w->journal->path);
fdfccdbc
ZJS
158
159 log_debug("Retrying write.");
dd87b184
ZJS
160 r = journal_file_append_entry(w->journal, ts, iovw->iovec, iovw->count,
161 &w->seqnum, NULL, NULL);
162 if (r < 0)
163 return r;
164
165 if (w->server)
166 w->server->event_count += 1;
167 return 1;
fdfccdbc 168}