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"
33 int main(int argc
, char *argv
[]) {
38 log_error("This program does not take arguments.");
42 log_set_target(LOG_TARGET_SAFE
);
43 log_set_facility(LOG_SYSLOG
);
44 log_parse_environment();
51 r
= server_init(&server
);
55 server_vacuum(&server
, false);
56 server_flush_to_var(&server
, true);
57 server_flush_dev_kmsg(&server
);
59 log_debug("systemd-journald running as pid "PID_FMT
, getpid_cached());
60 server_driver_message(&server
, 0,
61 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_START_STR
,
62 LOG_MESSAGE("Journal started"),
65 /* Make sure to send the usage message *after* flushing the
66 * journal so entries from the runtime journals are ordered
67 * before this message. See #4190 for some details. */
68 server_space_usage_message(&server
, NULL
);
71 usec_t t
= USEC_INFINITY
, n
;
73 r
= sd_event_get_state(server
.event
);
76 if (r
== SD_EVENT_FINISHED
)
79 n
= now(CLOCK_REALTIME
);
81 if (server
.max_retention_usec
> 0 && server
.oldest_file_usec
> 0) {
83 /* The retention time is reached, so let's vacuum! */
84 if (server
.oldest_file_usec
+ server
.max_retention_usec
< n
) {
85 log_info("Retention time reached.");
86 server_rotate(&server
);
87 server_vacuum(&server
, false);
91 /* Calculate when to rotate the next time */
92 t
= server
.oldest_file_usec
+ server
.max_retention_usec
- n
;
96 if (server
.system_journal
) {
99 if (journal_file_next_evolve_usec(server
.system_journal
, &u
)) {
108 r
= sd_event_run(server
.event
, t
);
110 log_error_errno(r
, "Failed to run event loop: %m");
114 server_maybe_append_tags(&server
);
115 server_maybe_warn_forward_syslog_missed(&server
);
118 log_debug("systemd-journald stopped as pid "PID_FMT
, getpid_cached());
119 server_driver_message(&server
, 0,
120 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_STOP_STR
,
121 LOG_MESSAGE("Journal stopped"),
125 server_done(&server
);
127 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;