1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Zbigniew Jędrzejewski-Szmek
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include "journal-remote.h"
23 #include "journal-remote-write.h"
25 int iovw_put(struct iovec_wrapper *iovw, void* data, size_t len) {
26 if (!GREEDY_REALLOC(iovw->iovec, iovw->size_bytes, iovw->count + 1))
29 iovw->iovec[iovw->count++] = (struct iovec) {data, len};
33 void iovw_free_contents(struct iovec_wrapper *iovw) {
36 iovw->size_bytes = iovw->count = 0;
39 size_t iovw_size(struct iovec_wrapper *iovw) {
42 for (i = 0; i < iovw->count; i++)
43 n += iovw->iovec[i].iov_len;
48 void iovw_rebase(struct iovec_wrapper *iovw, char *old, char *new) {
51 for (i = 0; i < iovw->count; i++)
52 iovw->iovec[i].iov_base = (char*) iovw->iovec[i].iov_base - old + new;
55 /**********************************************************************
56 **********************************************************************
57 **********************************************************************/
59 static int do_rotate(JournalFile **f, bool compress, bool seal) {
60 int r = journal_file_rotate(f, compress, seal);
63 log_error("Failed to rotate %s: %s", (*f)->path,
66 log_error("Failed to create rotated journal: %s",
73 Writer* writer_new(RemoteServer *server) {
80 memset(&w->metrics, 0xFF, sizeof(w->metrics));
82 w->mmap = mmap_cache_new();
94 Writer* writer_free(Writer *w) {
99 log_debug("Closing journal file %s.", w->journal->path);
100 journal_file_close(w->journal);
104 w->server->event_count += w->seqnum;
106 hashmap_remove(w->server->writers, w->hashmap_key);
109 free(w->hashmap_key);
112 mmap_cache_unref(w->mmap);
119 Writer* writer_unref(Writer *w) {
120 if (w && (-- w->n_ref <= 0))
126 Writer* writer_ref(Writer *w) {
128 assert_se(++ w->n_ref >= 2);
134 int writer_write(Writer *s,
135 struct iovec_wrapper *iovw,
143 assert(iovw->count > 0);
145 if (journal_file_rotate_suggested(s->journal, 0)) {
146 log_info("%s: Journal header limits reached or header out-of-date, rotating",
148 r = do_rotate(&s->journal, compress, seal);
153 r = journal_file_append_entry(s->journal, ts, iovw->iovec, iovw->count,
154 &s->seqnum, NULL, NULL);
158 log_debug("%s: Write failed, rotating: %s", s->journal->path, strerror(-r));
159 r = do_rotate(&s->journal, compress, seal);
163 log_info("%s: Successfully rotated journal", s->journal->path);
165 log_debug("Retrying write.");
166 r = journal_file_append_entry(s->journal, ts, iovw->iovec, iovw->count,
167 &s->seqnum, NULL, NULL);
168 return r < 0 ? r : 1;