]>
Commit | Line | Data |
---|---|---|
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_set_namespace(m, namespace); | |
60 | if (r < 0) | |
61 | return r; | |
62 | ||
63 | manager_load_config(m); | |
64 | ||
65 | r = manager_init(m); | |
66 | if (r < 0) | |
67 | return r; | |
68 | ||
69 | manager_vacuum(m, /* verbose = */ false); | |
70 | manager_flush_to_var(m, /* require_flag_file = */ true); | |
71 | manager_flush_dev_kmsg(m); | |
72 | ||
73 | if (m->namespace) | |
74 | log_debug("systemd-journald running as PID "PID_FMT" for namespace '%s'.", getpid_cached(), m->namespace); | |
75 | else | |
76 | log_debug("systemd-journald running as PID "PID_FMT" for the system.", getpid_cached()); | |
77 | ||
78 | manager_driver_message(m, 0, | |
79 | LOG_MESSAGE_ID(SD_MESSAGE_JOURNAL_START_STR), | |
80 | LOG_MESSAGE("Journal started")); | |
81 | ||
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); | |
86 | ||
87 | for (;;) { | |
88 | usec_t t, n; | |
89 | ||
90 | r = sd_event_get_state(m->event); | |
91 | if (r < 0) | |
92 | return log_error_errno(r, "Failed to get event loop state: %m"); | |
93 | if (r == SD_EVENT_FINISHED) | |
94 | break; | |
95 | ||
96 | r = sd_event_now(m->event, CLOCK_REALTIME, &n); | |
97 | if (r < 0) | |
98 | return log_error_errno(r, "Failed to get the current time: %m"); | |
99 | ||
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); | |
103 | ||
104 | /* The retention time is reached, so let's vacuum! */ | |
105 | if (t <= 0) { | |
106 | log_info("Retention time reached, vacuuming."); | |
107 | manager_vacuum(m, /* verbose = */ false); | |
108 | continue; | |
109 | } | |
110 | } else | |
111 | t = USEC_INFINITY; | |
112 | ||
113 | #if HAVE_GCRYPT | |
114 | if (m->system_journal) { | |
115 | usec_t u; | |
116 | ||
117 | if (journal_file_next_evolve_usec(m->system_journal, &u)) | |
118 | t = MIN(t, usec_sub_unsigned(u, n)); | |
119 | } | |
120 | #endif | |
121 | ||
122 | r = sd_event_run(m->event, t); | |
123 | if (r < 0) | |
124 | return log_error_errno(r, "Failed to run event loop: %m"); | |
125 | ||
126 | manager_maybe_append_tags(m); | |
127 | manager_maybe_warn_forward_syslog_missed(m); | |
128 | } | |
129 | ||
130 | if (m->namespace) | |
131 | log_debug("systemd-journald stopped as PID "PID_FMT" for namespace '%s'.", getpid_cached(), m->namespace); | |
132 | else | |
133 | log_debug("systemd-journald stopped as PID "PID_FMT" for the system.", getpid_cached()); | |
134 | ||
135 | manager_driver_message(m, 0, | |
136 | LOG_MESSAGE_ID(SD_MESSAGE_JOURNAL_STOP_STR), | |
137 | LOG_MESSAGE("Journal stopped")); | |
138 | ||
139 | return 0; | |
140 | } | |
141 | ||
142 | DEFINE_MAIN_FUNCTION(run); |