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