]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/journald.c
pcrextend: add documentation for varlink api
[thirdparty/systemd.git] / src / journal / journald.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
87d2c1ff 2
eea9eb1f 3#include <sys/stat.h>
87d2c1ff 4#include <unistd.h>
87d2c1ff 5
eea9eb1f 6#include "sd-event.h"
cf0fbc49 7#include "sd-messages.h"
81527be1 8
f97b34a6 9#include "format-util.h"
89fef990 10#include "journal-authenticate.h"
ef63833d 11#include "journald-kmsg.h"
c55f8706 12#include "journald-manager.h"
35e2e347 13#include "journald-syslog.h"
eea9eb1f 14#include "log.h"
80b1156b 15#include "main-func.h"
dccca82b 16#include "process-util.h"
fa6ac760 17#include "sigbus.h"
eea9eb1f 18#include "string-util.h"
dd9c8da8 19#include "terminal-util.h"
eea9eb1f 20#include "time-util.h"
fa6ac760 21
80b1156b 22static int run(int argc, char *argv[]) {
c55f8706 23 _cleanup_(manager_freep) Manager *m = NULL;
b1852c48 24 const char *namespace;
b3d6eb01 25 LogTarget log_target;
87d2c1ff
LP
26 int r;
27
80b1156b
YW
28 if (argc > 2)
29 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes one or no arguments.");
87d2c1ff 30
b1852c48
LP
31 namespace = argc > 1 ? empty_to_null(argv[1]) : NULL;
32
3eff4208 33 log_set_facility(LOG_SYSLOG);
e68778a3
LP
34
35 if (namespace)
36 /* If we run for a log namespace, then we ourselves can log to the main journald. */
37 log_setup();
38 else {
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
42 * otherwise. */
300b7e76 43 log_target = isatty_safe(STDERR_FILENO) ? LOG_TARGET_CONSOLE : LOG_TARGET_KMSG;
e68778a3
LP
44
45 log_set_prohibit_ipc(true); /* better safe than sorry */
46 log_set_target(log_target);
47 log_parse_environment();
48 log_open();
49 }
87d2c1ff
LP
50
51 umask(0022);
52
fa6ac760
LP
53 sigbus_install();
54
32bd43d7 55 r = manager_new(&m);
80b1156b
YW
56 if (r < 0)
57 return log_oom();
58
32bd43d7
YW
59 r = manager_set_namespace(m, namespace);
60 if (r < 0)
61 return r;
62
63 manager_load_config(m);
64
df5b3426 65 r = manager_init(m);
87d2c1ff 66 if (r < 0)
80b1156b 67 return r;
87d2c1ff 68
c55f8706
LP
69 manager_vacuum(m, /* verbose = */ false);
70 manager_flush_to_var(m, /* require_flag_file = */ true);
71 manager_flush_dev_kmsg(m);
e6960940 72
c55f8706
LP
73 if (m->namespace)
74 log_debug("systemd-journald running as PID "PID_FMT" for namespace '%s'.", getpid_cached(), m->namespace);
b1852c48
LP
75 else
76 log_debug("systemd-journald running as PID "PID_FMT" for the system.", getpid_cached());
77
c55f8706
LP
78 manager_driver_message(m, 0,
79 LOG_MESSAGE_ID(SD_MESSAGE_JOURNAL_START_STR),
80 LOG_MESSAGE("Journal started"));
87d2c1ff 81
18e758bf
FB
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. */
c55f8706 85 manager_space_usage_message(m, NULL);
18e758bf 86
87d2c1ff 87 for (;;) {
90c27eb4 88 usec_t t, n;
87d2c1ff 89
c55f8706 90 r = sd_event_get_state(m->event);
80b1156b
YW
91 if (r < 0)
92 return log_error_errno(r, "Failed to get event loop state: %m");
565a9388
LP
93 if (r == SD_EVENT_FINISHED)
94 break;
95
c55f8706 96 r = sd_event_now(m->event, CLOCK_REALTIME, &n);
2ae16d36
YW
97 if (r < 0)
98 return log_error_errno(r, "Failed to get the current time: %m");
89fef990 99
f48cf2a9 100 if (m->config.max_retention_usec > 0 && m->oldest_file_usec > 0) {
90c27eb4 101 /* Calculate when to rotate the next time */
f48cf2a9 102 t = usec_sub_unsigned(usec_add(m->oldest_file_usec, m->config.max_retention_usec), n);
89fef990 103
fb0951b0 104 /* The retention time is reached, so let's vacuum! */
90c27eb4 105 if (t <= 0) {
b63c18db 106 log_info("Retention time reached, vacuuming.");
c55f8706 107 manager_vacuum(m, /* verbose = */ false);
fb0951b0
LP
108 continue;
109 }
90c27eb4
YW
110 } else
111 t = USEC_INFINITY;
fb0951b0 112
349cc4a5 113#if HAVE_GCRYPT
c55f8706 114 if (m->system_journal) {
fb0951b0
LP
115 usec_t u;
116
c55f8706 117 if (journal_file_next_evolve_usec(m->system_journal, &u))
90c27eb4 118 t = MIN(t, usec_sub_unsigned(u, n));
fb0951b0 119 }
89fef990 120#endif
89fef990 121
c55f8706 122 r = sd_event_run(m->event, t);
80b1156b
YW
123 if (r < 0)
124 return log_error_errno(r, "Failed to run event loop: %m");
87d2c1ff 125
c55f8706
LP
126 manager_maybe_append_tags(m);
127 manager_maybe_warn_forward_syslog_missed(m);
87d2c1ff
LP
128 }
129
c55f8706
LP
130 if (m->namespace)
131 log_debug("systemd-journald stopped as PID "PID_FMT" for namespace '%s'.", getpid_cached(), m->namespace);
b1852c48
LP
132 else
133 log_debug("systemd-journald stopped as PID "PID_FMT" for the system.", getpid_cached());
134
c55f8706
LP
135 manager_driver_message(m, 0,
136 LOG_MESSAGE_ID(SD_MESSAGE_JOURNAL_STOP_STR),
137 LOG_MESSAGE("Journal stopped"));
fe652127 138
80b1156b 139 return 0;
87d2c1ff 140}
80b1156b
YW
141
142DEFINE_MAIN_FUNCTION(run);