]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
fdfccdbc | 2 | |
b5efdb8a | 3 | #include "alloc-util.h" |
9ff48d09 | 4 | #include "journal-remote.h" |
fdfccdbc | 5 | |
fdfccdbc | 6 | static int do_rotate(JournalFile **f, bool compress, bool seal) { |
57850536 | 7 | int r = journal_file_rotate(f, compress, (uint64_t) -1, seal, NULL); |
fdfccdbc ZJS |
8 | if (r < 0) { |
9 | if (*f) | |
c33b3297 | 10 | log_error_errno(r, "Failed to rotate %s: %m", (*f)->path); |
fdfccdbc | 11 | else |
c33b3297 | 12 | log_error_errno(r, "Failed to create rotated journal: %m"); |
fdfccdbc ZJS |
13 | } |
14 | ||
15 | return r; | |
16 | } | |
17 | ||
9ff48d09 ZJS |
18 | Writer* writer_new(RemoteServer *server) { |
19 | Writer *w; | |
fdfccdbc | 20 | |
9ff48d09 ZJS |
21 | w = new0(Writer, 1); |
22 | if (!w) | |
23 | return NULL; | |
fdfccdbc | 24 | |
9ff48d09 | 25 | memset(&w->metrics, 0xFF, sizeof(w->metrics)); |
fdfccdbc | 26 | |
9ff48d09 | 27 | w->mmap = mmap_cache_new(); |
6b430fdb ZJS |
28 | if (!w->mmap) |
29 | return mfree(w); | |
fdfccdbc | 30 | |
9ff48d09 ZJS |
31 | w->n_ref = 1; |
32 | w->server = server; | |
fdfccdbc | 33 | |
9ff48d09 | 34 | return w; |
fdfccdbc ZJS |
35 | } |
36 | ||
8301aa0b | 37 | static Writer* writer_free(Writer *w) { |
9ff48d09 ZJS |
38 | if (!w) |
39 | return NULL; | |
40 | ||
41 | if (w->journal) { | |
42 | log_debug("Closing journal file %s.", w->journal->path); | |
43 | journal_file_close(w->journal); | |
eacbb4d3 | 44 | } |
9ff48d09 | 45 | |
dd87b184 ZJS |
46 | if (w->server && w->hashmap_key) |
47 | hashmap_remove(w->server->writers, w->hashmap_key); | |
9ff48d09 ZJS |
48 | |
49 | free(w->hashmap_key); | |
50 | ||
51 | if (w->mmap) | |
52 | mmap_cache_unref(w->mmap); | |
53 | ||
6b430fdb | 54 | return mfree(w); |
9ff48d09 ZJS |
55 | } |
56 | ||
8301aa0b | 57 | DEFINE_TRIVIAL_REF_UNREF_FUNC(Writer, writer, writer_free); |
9ff48d09 | 58 | |
dd87b184 | 59 | int writer_write(Writer *w, |
fdfccdbc ZJS |
60 | struct iovec_wrapper *iovw, |
61 | dual_timestamp *ts, | |
62 | bool compress, | |
63 | bool seal) { | |
64 | int r; | |
65 | ||
dd87b184 | 66 | assert(w); |
fdfccdbc ZJS |
67 | assert(iovw); |
68 | assert(iovw->count > 0); | |
69 | ||
dd87b184 | 70 | if (journal_file_rotate_suggested(w->journal, 0)) { |
fdfccdbc | 71 | log_info("%s: Journal header limits reached or header out-of-date, rotating", |
dd87b184 ZJS |
72 | w->journal->path); |
73 | r = do_rotate(&w->journal, compress, seal); | |
fdfccdbc ZJS |
74 | if (r < 0) |
75 | return r; | |
76 | } | |
77 | ||
d180c349 ZJS |
78 | r = journal_file_append_entry(w->journal, ts, NULL, |
79 | iovw->iovec, iovw->count, | |
dd87b184 ZJS |
80 | &w->seqnum, NULL, NULL); |
81 | if (r >= 0) { | |
82 | if (w->server) | |
83 | w->server->event_count += 1; | |
e743ce70 ZJS |
84 | return 0; |
85 | } else if (r == -EBADMSG) | |
86 | return r; | |
fdfccdbc | 87 | |
da927ba9 | 88 | log_debug_errno(r, "%s: Write failed, rotating: %m", w->journal->path); |
dd87b184 | 89 | r = do_rotate(&w->journal, compress, seal); |
fdfccdbc ZJS |
90 | if (r < 0) |
91 | return r; | |
a83f4037 | 92 | else |
0e72da6f | 93 | log_debug("%s: Successfully rotated journal", w->journal->path); |
fdfccdbc ZJS |
94 | |
95 | log_debug("Retrying write."); | |
d180c349 ZJS |
96 | r = journal_file_append_entry(w->journal, ts, NULL, |
97 | iovw->iovec, iovw->count, | |
dd87b184 ZJS |
98 | &w->seqnum, NULL, NULL); |
99 | if (r < 0) | |
100 | return r; | |
101 | ||
102 | if (w->server) | |
103 | w->server->event_count += 1; | |
e743ce70 | 104 | return 0; |
fdfccdbc | 105 | } |