]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journald.c
man/systemd-sysext: list ephemeral/ephemeral-import in the list of options
[thirdparty/systemd.git] / src / journal / journald.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/stat.h>
4 #include <unistd.h>
5
6 #include "sd-event.h"
7 #include "sd-messages.h"
8
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"
14 #include "log.h"
15 #include "main-func.h"
16 #include "process-util.h"
17 #include "sigbus.h"
18 #include "string-util.h"
19 #include "terminal-util.h"
20 #include "time-util.h"
21
22 static int run(int argc, char *argv[]) {
23 _cleanup_(manager_freep) Manager *m = NULL;
24 const char *namespace;
25 LogTarget log_target;
26 int r;
27
28 if (argc > 2)
29 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes one or no arguments.");
30
31 namespace = argc > 1 ? empty_to_null(argv[1]) : NULL;
32
33 log_set_facility(LOG_SYSLOG);
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. */
43 log_target = isatty_safe(STDERR_FILENO) ? LOG_TARGET_CONSOLE : LOG_TARGET_KMSG;
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 }
50
51 umask(0022);
52
53 sigbus_install();
54
55 r = manager_new(&m);
56 if (r < 0)
57 return log_oom();
58
59 r = manager_init(m, namespace);
60 if (r < 0)
61 return r;
62
63 manager_vacuum(m, /* verbose = */ false);
64 manager_flush_to_var(m, /* require_flag_file = */ true);
65 manager_flush_dev_kmsg(m);
66
67 if (m->namespace)
68 log_debug("systemd-journald running as PID "PID_FMT" for namespace '%s'.", getpid_cached(), m->namespace);
69 else
70 log_debug("systemd-journald running as PID "PID_FMT" for the system.", getpid_cached());
71
72 manager_driver_message(m, 0,
73 LOG_MESSAGE_ID(SD_MESSAGE_JOURNAL_START_STR),
74 LOG_MESSAGE("Journal started"));
75
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);
80
81 for (;;) {
82 usec_t t, n;
83
84 r = sd_event_get_state(m->event);
85 if (r < 0)
86 return log_error_errno(r, "Failed to get event loop state: %m");
87 if (r == SD_EVENT_FINISHED)
88 break;
89
90 r = sd_event_now(m->event, CLOCK_REALTIME, &n);
91 if (r < 0)
92 return log_error_errno(r, "Failed to get the current time: %m");
93
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);
97
98 /* The retention time is reached, so let's vacuum! */
99 if (t <= 0) {
100 log_info("Retention time reached, vacuuming.");
101 manager_vacuum(m, /* verbose = */ false);
102 continue;
103 }
104 } else
105 t = USEC_INFINITY;
106
107 #if HAVE_GCRYPT
108 if (m->system_journal) {
109 usec_t u;
110
111 if (journal_file_next_evolve_usec(m->system_journal, &u))
112 t = MIN(t, usec_sub_unsigned(u, n));
113 }
114 #endif
115
116 r = sd_event_run(m->event, t);
117 if (r < 0)
118 return log_error_errno(r, "Failed to run event loop: %m");
119
120 manager_maybe_append_tags(m);
121 manager_maybe_warn_forward_syslog_missed(m);
122 }
123
124 if (m->namespace)
125 log_debug("systemd-journald stopped as PID "PID_FMT" for namespace '%s'.", getpid_cached(), m->namespace);
126 else
127 log_debug("systemd-journald stopped as PID "PID_FMT" for the system.", getpid_cached());
128
129 manager_driver_message(m, 0,
130 LOG_MESSAGE_ID(SD_MESSAGE_JOURNAL_STOP_STR),
131 LOG_MESSAGE("Journal stopped"));
132
133 return 0;
134 }
135
136 DEFINE_MAIN_FUNCTION(run);