]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journald.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / journal / journald.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
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
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
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <unistd.h>
22
23 #include "sd-daemon.h"
24 #include "sd-messages.h"
25
26 #include "format-util.h"
27 #include "journal-authenticate.h"
28 #include "journald-kmsg.h"
29 #include "journald-server.h"
30 #include "journald-syslog.h"
31 #include "sigbus.h"
32
33 int main(int argc, char *argv[]) {
34 Server server;
35 int r;
36
37 if (argc > 1) {
38 log_error("This program does not take arguments.");
39 return EXIT_FAILURE;
40 }
41
42 log_set_target(LOG_TARGET_SAFE);
43 log_set_facility(LOG_SYSLOG);
44 log_parse_environment();
45 log_open();
46
47 umask(0022);
48
49 sigbus_install();
50
51 r = server_init(&server);
52 if (r < 0)
53 goto finish;
54
55 server_vacuum(&server, false);
56 server_flush_to_var(&server, true);
57 server_flush_dev_kmsg(&server);
58
59 log_debug("systemd-journald running as pid "PID_FMT, getpid_cached());
60 server_driver_message(&server, 0,
61 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_START_STR,
62 LOG_MESSAGE("Journal started"),
63 NULL);
64
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
70 for (;;) {
71 usec_t t = USEC_INFINITY, n;
72
73 r = sd_event_get_state(server.event);
74 if (r < 0)
75 goto finish;
76 if (r == SD_EVENT_FINISHED)
77 break;
78
79 n = now(CLOCK_REALTIME);
80
81 if (server.max_retention_usec > 0 && server.oldest_file_usec > 0) {
82
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);
87 server_vacuum(&server, false);
88 continue;
89 }
90
91 /* Calculate when to rotate the next time */
92 t = server.oldest_file_usec + server.max_retention_usec - n;
93 }
94
95 #if HAVE_GCRYPT
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
103 t = MIN(t, u - n);
104 }
105 }
106 #endif
107
108 r = sd_event_run(server.event, t);
109 if (r < 0) {
110 log_error_errno(r, "Failed to run event loop: %m");
111 goto finish;
112 }
113
114 server_maybe_append_tags(&server);
115 server_maybe_warn_forward_syslog_missed(&server);
116 }
117
118 log_debug("systemd-journald stopped as pid "PID_FMT, getpid_cached());
119 server_driver_message(&server, 0,
120 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_STOP_STR,
121 LOG_MESSAGE("Journal stopped"),
122 NULL);
123
124 finish:
125 server_done(&server);
126
127 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
128 }