1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "sd-messages.h"
8 #include "format-util.h"
9 #include "journal-authenticate.h"
10 #include "journald-kmsg.h"
11 #include "journald-server.h"
12 #include "journald-syslog.h"
13 #include "main-func.h"
14 #include "process-util.h"
16 #include "terminal-util.h"
18 static int run(int argc
, char *argv
[]) {
19 _cleanup_(server_freep
) Server
*s
= NULL
;
20 const char *namespace;
25 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "This program takes one or no arguments.");
27 namespace = argc
> 1 ? empty_to_null(argv
[1]) : NULL
;
29 log_set_facility(LOG_SYSLOG
);
32 /* If we run for a log namespace, then we ourselves can log to the main journald. */
35 /* So here's the deal if we run as the main journald: we can't be considered as regular
36 * daemon when it comes to logging hence LOG_TARGET_AUTO won't do the right thing for
37 * us. Hence explicitly log to the console if we're started from a console or to kmsg
39 log_target
= isatty(STDERR_FILENO
) ? LOG_TARGET_CONSOLE
: LOG_TARGET_KMSG
;
41 log_set_prohibit_ipc(true); /* better safe than sorry */
42 log_set_target(log_target
);
43 log_parse_environment();
55 r
= server_init(s
, namespace);
59 server_vacuum(s
, /* verbose = */ false);
60 server_flush_to_var(s
, /* require_flag_file = */ true);
61 server_flush_dev_kmsg(s
);
64 log_debug("systemd-journald running as PID "PID_FMT
" for namespace '%s'.", getpid_cached(), s
->namespace);
66 log_debug("systemd-journald running as PID "PID_FMT
" for the system.", getpid_cached());
68 server_driver_message(s
, 0,
69 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_START_STR
,
70 LOG_MESSAGE("Journal started"),
73 /* Make sure to send the usage message *after* flushing the
74 * journal so entries from the runtime journals are ordered
75 * before this message. See #4190 for some details. */
76 server_space_usage_message(s
, NULL
);
81 r
= sd_event_get_state(s
->event
);
83 return log_error_errno(r
, "Failed to get event loop state: %m");
84 if (r
== SD_EVENT_FINISHED
)
87 r
= sd_event_now(s
->event
, CLOCK_REALTIME
, &n
);
89 return log_error_errno(r
, "Failed to get the current time: %m");
91 if (s
->max_retention_usec
> 0 && s
->oldest_file_usec
> 0) {
92 /* Calculate when to rotate the next time */
93 t
= usec_sub_unsigned(usec_add(s
->oldest_file_usec
, s
->max_retention_usec
), n
);
95 /* The retention time is reached, so let's vacuum! */
97 log_info("Retention time reached, rotating.");
99 server_vacuum(s
, /* verbose = */ false);
106 if (s
->system_journal
) {
109 if (journal_file_next_evolve_usec(s
->system_journal
, &u
))
110 t
= MIN(t
, usec_sub_unsigned(u
, n
));
114 r
= sd_event_run(s
->event
, t
);
116 return log_error_errno(r
, "Failed to run event loop: %m");
118 server_maybe_append_tags(s
);
119 server_maybe_warn_forward_syslog_missed(s
);
123 log_debug("systemd-journald stopped as PID "PID_FMT
" for namespace '%s'.", getpid_cached(), s
->namespace);
125 log_debug("systemd-journald stopped as PID "PID_FMT
" for the system.", getpid_cached());
127 server_driver_message(s
, 0,
128 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_STOP_STR
,
129 LOG_MESSAGE("Journal stopped"),
135 DEFINE_MAIN_FUNCTION(run
);