1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "sd-daemon.h"
24 #include "sd-messages.h"
26 #include "format-util.h"
27 #include "journal-authenticate.h"
28 #include "journald-kmsg.h"
29 #include "journald-server.h"
30 #include "journald-syslog.h"
31 #include "process-util.h"
34 int main(int argc
, char *argv
[]) {
39 log_error("This program does not take arguments.");
43 log_set_target(LOG_TARGET_SAFE
);
44 log_set_facility(LOG_SYSLOG
);
45 log_parse_environment();
52 r
= server_init(&server
);
56 server_vacuum(&server
, false);
57 server_flush_to_var(&server
, true);
58 server_flush_dev_kmsg(&server
);
60 log_debug("systemd-journald running as pid "PID_FMT
, getpid_cached());
61 server_driver_message(&server
, 0,
62 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_START_STR
,
63 LOG_MESSAGE("Journal started"),
66 /* Make sure to send the usage message *after* flushing the
67 * journal so entries from the runtime journals are ordered
68 * before this message. See #4190 for some details. */
69 server_space_usage_message(&server
, NULL
);
72 usec_t t
= USEC_INFINITY
, n
;
74 r
= sd_event_get_state(server
.event
);
77 if (r
== SD_EVENT_FINISHED
)
80 n
= now(CLOCK_REALTIME
);
82 if (server
.max_retention_usec
> 0 && server
.oldest_file_usec
> 0) {
84 /* The retention time is reached, so let's vacuum! */
85 if (server
.oldest_file_usec
+ server
.max_retention_usec
< n
) {
86 log_info("Retention time reached.");
87 server_rotate(&server
);
88 server_vacuum(&server
, false);
92 /* Calculate when to rotate the next time */
93 t
= server
.oldest_file_usec
+ server
.max_retention_usec
- n
;
97 if (server
.system_journal
) {
100 if (journal_file_next_evolve_usec(server
.system_journal
, &u
)) {
109 r
= sd_event_run(server
.event
, t
);
111 log_error_errno(r
, "Failed to run event loop: %m");
115 server_maybe_append_tags(&server
);
116 server_maybe_warn_forward_syslog_missed(&server
);
119 log_debug("systemd-journald stopped as pid "PID_FMT
, getpid_cached());
120 server_driver_message(&server
, 0,
121 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_STOP_STR
,
122 LOG_MESSAGE("Journal stopped"),
126 server_done(&server
);
128 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;