1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "alloc-util.h"
6 #include "journal-remote.h"
10 static int do_rotate(JournalFile
**f
, MMapCache
*m
, JournalFileFlags file_flags
) {
13 r
= journal_file_rotate(f
, m
, file_flags
, UINT64_MAX
, NULL
);
16 log_error_errno(r
, "Failed to rotate %s: %m", (*f
)->path
);
18 log_error_errno(r
, "Failed to create rotated journal: %m");
24 int writer_new(RemoteServer
*server
, Writer
**ret
) {
25 _cleanup_(writer_unrefp
) Writer
*w
= NULL
;
37 .metrics
= server
->metrics
,
41 w
->mmap
= mmap_cache_new();
45 if (is_dir(server
->output
, /* follow = */ true) > 0) {
46 w
->output
= strdup(server
->output
);
50 r
= path_extract_directory(server
->output
, &w
->output
);
59 static Writer
* writer_free(Writer
*w
) {
64 log_debug("Closing journal file %s.", w
->journal
->path
);
65 journal_file_offline_close(w
->journal
);
68 if (w
->server
&& w
->hashmap_key
)
69 hashmap_remove(w
->server
->writers
, w
->hashmap_key
);
74 mmap_cache_unref(w
->mmap
);
81 DEFINE_TRIVIAL_REF_UNREF_FUNC(Writer
, writer
, writer_free
);
83 int writer_write(Writer
*w
,
84 const struct iovec_wrapper
*iovw
,
85 const dual_timestamp
*ts
,
86 const sd_id128_t
*boot_id
,
87 JournalFileFlags file_flags
) {
91 assert(!iovw_isempty(iovw
));
93 if (journal_file_rotate_suggested(w
->journal
, 0, LOG_DEBUG
)) {
94 log_info("%s: Journal header limits reached or header out-of-date, rotating",
96 r
= do_rotate(&w
->journal
, w
->mmap
, file_flags
);
99 r
= journal_directory_vacuum(w
->output
, w
->metrics
.max_use
, w
->metrics
.n_max_files
, 0, NULL
, /* verbose = */ true);
104 r
= journal_file_append_entry(
111 /* seqnum_id= */ NULL
,
112 /* ret_object= */ NULL
,
113 /* ret_offset= */ NULL
);
116 w
->server
->event_count
+= 1;
118 } else if (r
== -EBADMSG
)
121 log_debug_errno(r
, "%s: Write failed, rotating: %m", w
->journal
->path
);
122 r
= do_rotate(&w
->journal
, w
->mmap
, file_flags
);
126 log_debug("%s: Successfully rotated journal", w
->journal
->path
);
127 r
= journal_directory_vacuum(w
->output
, w
->metrics
.max_use
, w
->metrics
.n_max_files
, 0, NULL
, /* verbose = */ true);
131 log_debug("Retrying write.");
132 r
= journal_file_append_entry(
136 iovw
->iovec
, iovw
->count
,
138 /* seqnum_id= */ NULL
,
139 /* ret_object= */ NULL
,
140 /* ret_offset= */ NULL
);
145 w
->server
->event_count
+= 1;