1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "sd-messages.h"
9 #include "format-util.h"
10 #include "journal-authenticate.h"
11 #include "journald-kmsg.h"
12 #include "journald-manager.h"
13 #include "journald-syslog.h"
15 #include "main-func.h"
16 #include "process-util.h"
18 #include "string-util.h"
19 #include "terminal-util.h"
20 #include "time-util.h"
22 static int run(int argc
, char *argv
[]) {
23 _cleanup_(manager_freep
) Manager
*m
= NULL
;
24 const char *namespace;
29 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "This program takes one or no arguments.");
31 namespace = argc
> 1 ? empty_to_null(argv
[1]) : NULL
;
33 log_set_facility(LOG_SYSLOG
);
36 /* If we run for a log namespace, then we ourselves can log to the main journald. */
39 /* So here's the deal if we run as the main journald: we can't be considered as regular
40 * daemon when it comes to logging hence LOG_TARGET_AUTO won't do the right thing for
41 * us. Hence explicitly log to the console if we're started from a console or to kmsg
43 log_target
= isatty_safe(STDERR_FILENO
) ? LOG_TARGET_CONSOLE
: LOG_TARGET_KMSG
;
45 log_set_prohibit_ipc(true); /* better safe than sorry */
46 log_set_target(log_target
);
47 log_parse_environment();
59 r
= manager_init(m
, namespace);
63 manager_vacuum(m
, /* verbose = */ false);
64 manager_flush_to_var(m
, /* require_flag_file = */ true);
65 manager_flush_dev_kmsg(m
);
68 log_debug("systemd-journald running as PID "PID_FMT
" for namespace '%s'.", getpid_cached(), m
->namespace);
70 log_debug("systemd-journald running as PID "PID_FMT
" for the system.", getpid_cached());
72 manager_driver_message(m
, 0,
73 LOG_MESSAGE_ID(SD_MESSAGE_JOURNAL_START_STR
),
74 LOG_MESSAGE("Journal started"));
76 /* Make sure to send the usage message *after* flushing the
77 * journal so entries from the runtime journals are ordered
78 * before this message. See #4190 for some details. */
79 manager_space_usage_message(m
, NULL
);
84 r
= sd_event_get_state(m
->event
);
86 return log_error_errno(r
, "Failed to get event loop state: %m");
87 if (r
== SD_EVENT_FINISHED
)
90 r
= sd_event_now(m
->event
, CLOCK_REALTIME
, &n
);
92 return log_error_errno(r
, "Failed to get the current time: %m");
94 if (m
->max_retention_usec
> 0 && m
->oldest_file_usec
> 0) {
95 /* Calculate when to rotate the next time */
96 t
= usec_sub_unsigned(usec_add(m
->oldest_file_usec
, m
->max_retention_usec
), n
);
98 /* The retention time is reached, so let's vacuum! */
100 log_info("Retention time reached, vacuuming.");
101 manager_vacuum(m
, /* verbose = */ false);
108 if (m
->system_journal
) {
111 if (journal_file_next_evolve_usec(m
->system_journal
, &u
))
112 t
= MIN(t
, usec_sub_unsigned(u
, n
));
116 r
= sd_event_run(m
->event
, t
);
118 return log_error_errno(r
, "Failed to run event loop: %m");
120 manager_maybe_append_tags(m
);
121 manager_maybe_warn_forward_syslog_missed(m
);
125 log_debug("systemd-journald stopped as PID "PID_FMT
" for namespace '%s'.", getpid_cached(), m
->namespace);
127 log_debug("systemd-journald stopped as PID "PID_FMT
" for the system.", getpid_cached());
129 manager_driver_message(m
, 0,
130 LOG_MESSAGE_ID(SD_MESSAGE_JOURNAL_STOP_STR
),
131 LOG_MESSAGE("Journal stopped"));
136 DEFINE_MAIN_FUNCTION(run
);