]>
Commit | Line | Data |
---|---|---|
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 | 22 | static 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 | ||
df5b3426 | 55 | r = manager_new(&m, namespace); |
80b1156b YW |
56 | if (r < 0) |
57 | return log_oom(); | |
58 | ||
df5b3426 | 59 | r = manager_init(m); |
87d2c1ff | 60 | if (r < 0) |
80b1156b | 61 | return r; |
87d2c1ff | 62 | |
c55f8706 LP |
63 | manager_vacuum(m, /* verbose = */ false); |
64 | manager_flush_to_var(m, /* require_flag_file = */ true); | |
65 | manager_flush_dev_kmsg(m); | |
e6960940 | 66 | |
c55f8706 LP |
67 | if (m->namespace) |
68 | log_debug("systemd-journald running as PID "PID_FMT" for namespace '%s'.", getpid_cached(), m->namespace); | |
b1852c48 LP |
69 | else |
70 | log_debug("systemd-journald running as PID "PID_FMT" for the system.", getpid_cached()); | |
71 | ||
c55f8706 LP |
72 | manager_driver_message(m, 0, |
73 | LOG_MESSAGE_ID(SD_MESSAGE_JOURNAL_START_STR), | |
74 | LOG_MESSAGE("Journal started")); | |
87d2c1ff | 75 | |
18e758bf FB |
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. */ | |
c55f8706 | 79 | manager_space_usage_message(m, NULL); |
18e758bf | 80 | |
87d2c1ff | 81 | for (;;) { |
90c27eb4 | 82 | usec_t t, n; |
87d2c1ff | 83 | |
c55f8706 | 84 | r = sd_event_get_state(m->event); |
80b1156b YW |
85 | if (r < 0) |
86 | return log_error_errno(r, "Failed to get event loop state: %m"); | |
565a9388 LP |
87 | if (r == SD_EVENT_FINISHED) |
88 | break; | |
89 | ||
c55f8706 | 90 | r = sd_event_now(m->event, CLOCK_REALTIME, &n); |
2ae16d36 YW |
91 | if (r < 0) |
92 | return log_error_errno(r, "Failed to get the current time: %m"); | |
89fef990 | 93 | |
c55f8706 | 94 | if (m->max_retention_usec > 0 && m->oldest_file_usec > 0) { |
90c27eb4 | 95 | /* Calculate when to rotate the next time */ |
c55f8706 | 96 | t = usec_sub_unsigned(usec_add(m->oldest_file_usec, m->max_retention_usec), n); |
89fef990 | 97 | |
fb0951b0 | 98 | /* The retention time is reached, so let's vacuum! */ |
90c27eb4 | 99 | if (t <= 0) { |
b63c18db | 100 | log_info("Retention time reached, vacuuming."); |
c55f8706 | 101 | manager_vacuum(m, /* verbose = */ false); |
fb0951b0 LP |
102 | continue; |
103 | } | |
90c27eb4 YW |
104 | } else |
105 | t = USEC_INFINITY; | |
fb0951b0 | 106 | |
349cc4a5 | 107 | #if HAVE_GCRYPT |
c55f8706 | 108 | if (m->system_journal) { |
fb0951b0 LP |
109 | usec_t u; |
110 | ||
c55f8706 | 111 | if (journal_file_next_evolve_usec(m->system_journal, &u)) |
90c27eb4 | 112 | t = MIN(t, usec_sub_unsigned(u, n)); |
fb0951b0 | 113 | } |
89fef990 | 114 | #endif |
89fef990 | 115 | |
c55f8706 | 116 | r = sd_event_run(m->event, t); |
80b1156b YW |
117 | if (r < 0) |
118 | return log_error_errno(r, "Failed to run event loop: %m"); | |
87d2c1ff | 119 | |
c55f8706 LP |
120 | manager_maybe_append_tags(m); |
121 | manager_maybe_warn_forward_syslog_missed(m); | |
87d2c1ff LP |
122 | } |
123 | ||
c55f8706 LP |
124 | if (m->namespace) |
125 | log_debug("systemd-journald stopped as PID "PID_FMT" for namespace '%s'.", getpid_cached(), m->namespace); | |
b1852c48 LP |
126 | else |
127 | log_debug("systemd-journald stopped as PID "PID_FMT" for the system.", getpid_cached()); | |
128 | ||
c55f8706 LP |
129 | manager_driver_message(m, 0, |
130 | LOG_MESSAGE_ID(SD_MESSAGE_JOURNAL_STOP_STR), | |
131 | LOG_MESSAGE("Journal stopped")); | |
fe652127 | 132 | |
80b1156b | 133 | return 0; |
87d2c1ff | 134 | } |
80b1156b YW |
135 | |
136 | DEFINE_MAIN_FUNCTION(run); |