]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
87d2c1ff LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2011 Lennart Poettering | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
8 | under the terms of the GNU Lesser General Public License as published by |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
87d2c1ff LP |
10 | (at your option) any later version. |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 15 | Lesser General Public License for more details. |
87d2c1ff | 16 | |
5430f7f2 | 17 | You should have received a copy of the GNU Lesser General Public License |
87d2c1ff LP |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
19 | ***/ | |
20 | ||
87d2c1ff | 21 | #include <unistd.h> |
87d2c1ff | 22 | |
8580d1f7 | 23 | #include "sd-daemon.h" |
cf0fbc49 | 24 | #include "sd-messages.h" |
81527be1 | 25 | |
f97b34a6 | 26 | #include "format-util.h" |
89fef990 | 27 | #include "journal-authenticate.h" |
ef63833d | 28 | #include "journald-kmsg.h" |
cf0fbc49 | 29 | #include "journald-server.h" |
35e2e347 | 30 | #include "journald-syslog.h" |
fa6ac760 LP |
31 | #include "sigbus.h" |
32 | ||
87d2c1ff LP |
33 | int main(int argc, char *argv[]) { |
34 | Server server; | |
35 | int r; | |
36 | ||
87d2c1ff LP |
37 | if (argc > 1) { |
38 | log_error("This program does not take arguments."); | |
39 | return EXIT_FAILURE; | |
40 | } | |
41 | ||
a6903061 | 42 | log_set_target(LOG_TARGET_SAFE); |
3eff4208 | 43 | log_set_facility(LOG_SYSLOG); |
87d2c1ff LP |
44 | log_parse_environment(); |
45 | log_open(); | |
46 | ||
47 | umask(0022); | |
48 | ||
fa6ac760 LP |
49 | sigbus_install(); |
50 | ||
87d2c1ff LP |
51 | r = server_init(&server); |
52 | if (r < 0) | |
53 | goto finish; | |
54 | ||
3a19f215 | 55 | server_vacuum(&server, false); |
f78273c8 | 56 | server_flush_to_var(&server, true); |
51abe64c | 57 | server_flush_dev_kmsg(&server); |
e6960940 | 58 | |
df0ff127 | 59 | log_debug("systemd-journald running as pid "PID_FMT, getpid_cached()); |
13181942 | 60 | server_driver_message(&server, 0, |
2b044526 | 61 | "MESSAGE_ID=" SD_MESSAGE_JOURNAL_START_STR, |
8a03c9ef ZJS |
62 | LOG_MESSAGE("Journal started"), |
63 | NULL); | |
87d2c1ff | 64 | |
18e758bf FB |
65 | /* Make sure to send the usage message *after* flushing the |
66 | * journal so entries from the runtime journals are ordered | |
67 | * before this message. See #4190 for some details. */ | |
68 | server_space_usage_message(&server, NULL); | |
69 | ||
87d2c1ff | 70 | for (;;) { |
3a43da28 | 71 | usec_t t = USEC_INFINITY, n; |
87d2c1ff | 72 | |
565a9388 LP |
73 | r = sd_event_get_state(server.event); |
74 | if (r < 0) | |
75 | goto finish; | |
76 | if (r == SD_EVENT_FINISHED) | |
77 | break; | |
78 | ||
fb0951b0 | 79 | n = now(CLOCK_REALTIME); |
89fef990 | 80 | |
fb0951b0 | 81 | if (server.max_retention_usec > 0 && server.oldest_file_usec > 0) { |
89fef990 | 82 | |
fb0951b0 LP |
83 | /* The retention time is reached, so let's vacuum! */ |
84 | if (server.oldest_file_usec + server.max_retention_usec < n) { | |
85 | log_info("Retention time reached."); | |
86 | server_rotate(&server); | |
3a19f215 | 87 | server_vacuum(&server, false); |
fb0951b0 LP |
88 | continue; |
89 | } | |
89fef990 | 90 | |
fb0951b0 | 91 | /* Calculate when to rotate the next time */ |
f9a810be | 92 | t = server.oldest_file_usec + server.max_retention_usec - n; |
fb0951b0 LP |
93 | } |
94 | ||
349cc4a5 | 95 | #if HAVE_GCRYPT |
fb0951b0 LP |
96 | if (server.system_journal) { |
97 | usec_t u; | |
98 | ||
99 | if (journal_file_next_evolve_usec(server.system_journal, &u)) { | |
100 | if (n >= u) | |
101 | t = 0; | |
102 | else | |
f9a810be | 103 | t = MIN(t, u - n); |
fb0951b0 LP |
104 | } |
105 | } | |
89fef990 | 106 | #endif |
89fef990 | 107 | |
f9a810be | 108 | r = sd_event_run(server.event, t); |
87d2c1ff | 109 | if (r < 0) { |
da927ba9 | 110 | log_error_errno(r, "Failed to run event loop: %m"); |
87d2c1ff | 111 | goto finish; |
89fef990 | 112 | } |
87d2c1ff | 113 | |
99add6fd | 114 | server_maybe_append_tags(&server); |
178cc770 | 115 | server_maybe_warn_forward_syslog_missed(&server); |
87d2c1ff LP |
116 | } |
117 | ||
df0ff127 | 118 | log_debug("systemd-journald stopped as pid "PID_FMT, getpid_cached()); |
13181942 | 119 | server_driver_message(&server, 0, |
2b044526 | 120 | "MESSAGE_ID=" SD_MESSAGE_JOURNAL_STOP_STR, |
8a03c9ef ZJS |
121 | LOG_MESSAGE("Journal stopped"), |
122 | NULL); | |
fe652127 | 123 | |
87d2c1ff | 124 | finish: |
87d2c1ff LP |
125 | server_done(&server); |
126 | ||
127 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
128 | } |