chiark / gitweb /
449636cd8c3180b389776151e69b179bcad4db5a
[elogind.git] / src / journal-remote / journal-remote-write.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2012 Zbigniew JÄ™drzejewski-Szmek
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include "journal-remote-write.h"
23
24 int iovw_put(struct iovec_wrapper *iovw, void* data, size_t len) {
25         if (!GREEDY_REALLOC(iovw->iovec, iovw->size_bytes, iovw->count + 1))
26                 return log_oom();
27
28         iovw->iovec[iovw->count++] = (struct iovec) {data, len};
29         return 0;
30 }
31
32 void iovw_free_contents(struct iovec_wrapper *iovw) {
33         for (size_t j = 0; j < iovw->count; j++)
34                 free(iovw->iovec[j].iov_base);
35         free(iovw->iovec);
36         iovw->iovec = NULL;
37         iovw->size_bytes = iovw->count = 0;
38 }
39
40 size_t iovw_size(struct iovec_wrapper *iovw) {
41         size_t n = 0, i;
42
43         for(i = 0; i < iovw->count; i++)
44                 n += iovw->iovec[i].iov_len;
45
46         return n;
47 }
48
49 /**********************************************************************
50  **********************************************************************
51  **********************************************************************/
52
53 static int do_rotate(JournalFile **f, bool compress, bool seal) {
54         int r = journal_file_rotate(f, compress, seal);
55         if (r < 0) {
56                 if (*f)
57                         log_error("Failed to rotate %s: %s", (*f)->path,
58                                   strerror(-r));
59                 else
60                         log_error("Failed to create rotated journal: %s",
61                                   strerror(-r));
62         }
63
64         return r;
65 }
66
67 int writer_init(Writer *s) {
68         assert(s);
69
70         s->journal = NULL;
71
72         memset(&s->metrics, 0xFF, sizeof(s->metrics));
73
74         s->mmap = mmap_cache_new();
75         if (!s->mmap)
76                 return log_oom();
77
78         s->seqnum = 0;
79
80         return 0;
81 }
82
83 int writer_close(Writer *s) {
84         if (s->journal) {
85                 journal_file_close(s->journal);
86                 log_debug("Journal has been closed.");
87         }
88         if (s->mmap)
89                 mmap_cache_unref(s->mmap);
90         return 0;
91 }
92
93 int writer_write(Writer *s,
94                  struct iovec_wrapper *iovw,
95                  dual_timestamp *ts,
96                  bool compress,
97                  bool seal) {
98         int r;
99
100         assert(s);
101         assert(iovw);
102         assert(iovw->count > 0);
103
104         if (journal_file_rotate_suggested(s->journal, 0)) {
105                 log_info("%s: Journal header limits reached or header out-of-date, rotating",
106                          s->journal->path);
107                 r = do_rotate(&s->journal, compress, seal);
108                 if (r < 0)
109                         return r;
110         }
111
112         r = journal_file_append_entry(s->journal, ts, iovw->iovec, iovw->count,
113                                       &s->seqnum, NULL, NULL);
114         if (r >= 0)
115                 return 1;
116
117         log_info("%s: Write failed, rotating", s->journal->path);
118         r = do_rotate(&s->journal, compress, seal);
119         if (r < 0)
120                 return r;
121
122         log_debug("Retrying write.");
123         r = journal_file_append_entry(s->journal, ts, iovw->iovec, iovw->count,
124                                       &s->seqnum, NULL, NULL);
125         return r < 0 ? r : 1;
126 }