]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal-remote/journal-remote-write.c
fix(journal-gatewayd): use relative urls (not starting with '/')
[thirdparty/systemd.git] / src / journal-remote / journal-remote-write.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "alloc-util.h"
4 #include "journal-remote.h"
5
6 static int do_rotate(JournalFile **f, bool compress, bool seal) {
7 int r = journal_file_rotate(f, compress, (uint64_t) -1, seal, NULL);
8 if (r < 0) {
9 if (*f)
10 log_error_errno(r, "Failed to rotate %s: %m", (*f)->path);
11 else
12 log_error_errno(r, "Failed to create rotated journal: %m");
13 }
14
15 return r;
16 }
17
18 Writer* writer_new(RemoteServer *server) {
19 Writer *w;
20
21 w = new0(Writer, 1);
22 if (!w)
23 return NULL;
24
25 memset(&w->metrics, 0xFF, sizeof(w->metrics));
26
27 w->mmap = mmap_cache_new();
28 if (!w->mmap)
29 return mfree(w);
30
31 w->n_ref = 1;
32 w->server = server;
33
34 return w;
35 }
36
37 static Writer* writer_free(Writer *w) {
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);
44 }
45
46 if (w->server && w->hashmap_key)
47 hashmap_remove(w->server->writers, w->hashmap_key);
48
49 free(w->hashmap_key);
50
51 if (w->mmap)
52 mmap_cache_unref(w->mmap);
53
54 return mfree(w);
55 }
56
57 DEFINE_TRIVIAL_REF_UNREF_FUNC(Writer, writer, writer_free);
58
59 int writer_write(Writer *w,
60 struct iovec_wrapper *iovw,
61 dual_timestamp *ts,
62 sd_id128_t *boot_id,
63 bool compress,
64 bool seal) {
65 int r;
66
67 assert(w);
68 assert(iovw);
69 assert(iovw->count > 0);
70
71 if (journal_file_rotate_suggested(w->journal, 0)) {
72 log_info("%s: Journal header limits reached or header out-of-date, rotating",
73 w->journal->path);
74 r = do_rotate(&w->journal, compress, seal);
75 if (r < 0)
76 return r;
77 }
78
79 r = journal_file_append_entry(w->journal, ts, boot_id,
80 iovw->iovec, iovw->count,
81 &w->seqnum, NULL, NULL);
82 if (r >= 0) {
83 if (w->server)
84 w->server->event_count += 1;
85 return 0;
86 } else if (r == -EBADMSG)
87 return r;
88
89 log_debug_errno(r, "%s: Write failed, rotating: %m", w->journal->path);
90 r = do_rotate(&w->journal, compress, seal);
91 if (r < 0)
92 return r;
93 else
94 log_debug("%s: Successfully rotated journal", w->journal->path);
95
96 log_debug("Retrying write.");
97 r = journal_file_append_entry(w->journal, ts, boot_id,
98 iovw->iovec, iovw->count,
99 &w->seqnum, NULL, NULL);
100 if (r < 0)
101 return r;
102
103 if (w->server)
104 w->server->event_count += 1;
105 return 0;
106 }