]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journald.c
Revert "CI: add manual workflow to publish pages to fix submodule issue"
[thirdparty/systemd.git] / src / journal / journald.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <unistd.h>
4
5 #include "sd-daemon.h"
6 #include "sd-messages.h"
7
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"
15 #include "sigbus.h"
16 #include "terminal-util.h"
17
18 static int run(int argc, char *argv[]) {
19 _cleanup_(server_freep) Server *s = NULL;
20 const char *namespace;
21 LogTarget log_target;
22 int r;
23
24 if (argc > 2)
25 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program takes one or no arguments.");
26
27 namespace = argc > 1 ? empty_to_null(argv[1]) : NULL;
28
29 log_set_facility(LOG_SYSLOG);
30
31 if (namespace)
32 /* If we run for a log namespace, then we ourselves can log to the main journald. */
33 log_setup();
34 else {
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
38 * otherwise. */
39 log_target = isatty(STDERR_FILENO) ? LOG_TARGET_CONSOLE : LOG_TARGET_KMSG;
40
41 log_set_prohibit_ipc(true); /* better safe than sorry */
42 log_set_target(log_target);
43 log_parse_environment();
44 log_open();
45 }
46
47 umask(0022);
48
49 sigbus_install();
50
51 r = server_new(&s);
52 if (r < 0)
53 return log_oom();
54
55 r = server_init(s, namespace);
56 if (r < 0)
57 return r;
58
59 server_vacuum(s, /* verbose = */ false);
60 server_flush_to_var(s, /* require_flag_file = */ true);
61 server_flush_dev_kmsg(s);
62
63 if (s->namespace)
64 log_debug("systemd-journald running as PID "PID_FMT" for namespace '%s'.", getpid_cached(), s->namespace);
65 else
66 log_debug("systemd-journald running as PID "PID_FMT" for the system.", getpid_cached());
67
68 server_driver_message(s, 0,
69 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_START_STR,
70 LOG_MESSAGE("Journal started"),
71 NULL);
72
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);
77
78 for (;;) {
79 usec_t t, n;
80
81 r = sd_event_get_state(s->event);
82 if (r < 0)
83 return log_error_errno(r, "Failed to get event loop state: %m");
84 if (r == SD_EVENT_FINISHED)
85 break;
86
87 r = sd_event_now(s->event, CLOCK_REALTIME, &n);
88 if (r < 0)
89 return log_error_errno(r, "Failed to get the current time: %m");
90
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);
94
95 /* The retention time is reached, so let's vacuum! */
96 if (t <= 0) {
97 log_info("Retention time reached, rotating.");
98 server_rotate(s);
99 server_vacuum(s, /* verbose = */ false);
100 continue;
101 }
102 } else
103 t = USEC_INFINITY;
104
105 #if HAVE_GCRYPT
106 if (s->system_journal) {
107 usec_t u;
108
109 if (journal_file_next_evolve_usec(s->system_journal, &u))
110 t = MIN(t, usec_sub_unsigned(u, n));
111 }
112 #endif
113
114 r = sd_event_run(s->event, t);
115 if (r < 0)
116 return log_error_errno(r, "Failed to run event loop: %m");
117
118 server_maybe_append_tags(s);
119 server_maybe_warn_forward_syslog_missed(s);
120 }
121
122 if (s->namespace)
123 log_debug("systemd-journald stopped as PID "PID_FMT" for namespace '%s'.", getpid_cached(), s->namespace);
124 else
125 log_debug("systemd-journald stopped as PID "PID_FMT" for the system.", getpid_cached());
126
127 server_driver_message(s, 0,
128 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_STOP_STR,
129 LOG_MESSAGE("Journal stopped"),
130 NULL);
131
132 return 0;
133 }
134
135 DEFINE_MAIN_FUNCTION(run);