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_set_namespace(m
, namespace);
63 manager_load_config(m
);
69 manager_vacuum(m
, /* verbose = */ false);
70 manager_flush_to_var(m
, /* require_flag_file = */ true);
71 manager_flush_dev_kmsg(m
);
74 log_debug("systemd-journald running as PID "PID_FMT
" for namespace '%s'.", getpid_cached(), m
->namespace);
76 log_debug("systemd-journald running as PID "PID_FMT
" for the system.", getpid_cached());
78 manager_driver_message(m
, 0,
79 LOG_MESSAGE_ID(SD_MESSAGE_JOURNAL_START_STR
),
80 LOG_MESSAGE("Journal started"));
82 /* Make sure to send the usage message *after* flushing the
83 * journal so entries from the runtime journals are ordered
84 * before this message. See #4190 for some details. */
85 manager_space_usage_message(m
, NULL
);
90 r
= sd_event_get_state(m
->event
);
92 return log_error_errno(r
, "Failed to get event loop state: %m");
93 if (r
== SD_EVENT_FINISHED
)
96 r
= sd_event_now(m
->event
, CLOCK_REALTIME
, &n
);
98 return log_error_errno(r
, "Failed to get the current time: %m");
100 if (m
->config
.max_retention_usec
> 0 && m
->oldest_file_usec
> 0) {
101 /* Calculate when to rotate the next time */
102 t
= usec_sub_unsigned(usec_add(m
->oldest_file_usec
, m
->config
.max_retention_usec
), n
);
104 /* The retention time is reached, so let's vacuum! */
106 log_info("Retention time reached, vacuuming.");
107 manager_vacuum(m
, /* verbose = */ false);
114 if (m
->system_journal
) {
117 if (journal_file_next_evolve_usec(m
->system_journal
, &u
))
118 t
= MIN(t
, usec_sub_unsigned(u
, n
));
122 r
= sd_event_run(m
->event
, t
);
124 return log_error_errno(r
, "Failed to run event loop: %m");
126 manager_maybe_append_tags(m
);
127 manager_maybe_warn_forward_syslog_missed(m
);
131 log_debug("systemd-journald stopped as PID "PID_FMT
" for namespace '%s'.", getpid_cached(), m
->namespace);
133 log_debug("systemd-journald stopped as PID "PID_FMT
" for the system.", getpid_cached());
135 manager_driver_message(m
, 0,
136 LOG_MESSAGE_ID(SD_MESSAGE_JOURNAL_STOP_STR
),
137 LOG_MESSAGE("Journal stopped"));
142 DEFINE_MAIN_FUNCTION(run
);