]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journald.c
log: minimize includes in log.h
[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 "process-util.h"
32 #include "sigbus.h"
33
34 int main(int argc, char *argv[]) {
35 Server server;
36 int r;
37
38 if (argc > 1) {
39 log_error("This program does not take arguments.");
40 return EXIT_FAILURE;
41 }
42
43 log_set_target(LOG_TARGET_SAFE);
44 log_set_facility(LOG_SYSLOG);
45 log_parse_environment();
46 log_open();
47
48 umask(0022);
49
50 sigbus_install();
51
52 r = server_init(&server);
53 if (r < 0)
54 goto finish;
55
56 server_vacuum(&server, false);
57 server_flush_to_var(&server, true);
58 server_flush_dev_kmsg(&server);
59
60 log_debug("systemd-journald running as pid "PID_FMT, getpid_cached());
61 server_driver_message(&server, 0,
62 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_START_STR,
63 LOG_MESSAGE("Journal started"),
64 NULL);
65
66 /* Make sure to send the usage message *after* flushing the
67 * journal so entries from the runtime journals are ordered
68 * before this message. See #4190 for some details. */
69 server_space_usage_message(&server, NULL);
70
71 for (;;) {
72 usec_t t = USEC_INFINITY, n;
73
74 r = sd_event_get_state(server.event);
75 if (r < 0)
76 goto finish;
77 if (r == SD_EVENT_FINISHED)
78 break;
79
80 n = now(CLOCK_REALTIME);
81
82 if (server.max_retention_usec > 0 && server.oldest_file_usec > 0) {
83
84 /* The retention time is reached, so let's vacuum! */
85 if (server.oldest_file_usec + server.max_retention_usec < n) {
86 log_info("Retention time reached.");
87 server_rotate(&server);
88 server_vacuum(&server, false);
89 continue;
90 }
91
92 /* Calculate when to rotate the next time */
93 t = server.oldest_file_usec + server.max_retention_usec - n;
94 }
95
96 #if HAVE_GCRYPT
97 if (server.system_journal) {
98 usec_t u;
99
100 if (journal_file_next_evolve_usec(server.system_journal, &u)) {
101 if (n >= u)
102 t = 0;
103 else
104 t = MIN(t, u - n);
105 }
106 }
107 #endif
108
109 r = sd_event_run(server.event, t);
110 if (r < 0) {
111 log_error_errno(r, "Failed to run event loop: %m");
112 goto finish;
113 }
114
115 server_maybe_append_tags(&server);
116 server_maybe_warn_forward_syslog_missed(&server);
117 }
118
119 log_debug("systemd-journald stopped as pid "PID_FMT, getpid_cached());
120 server_driver_message(&server, 0,
121 "MESSAGE_ID=" SD_MESSAGE_JOURNAL_STOP_STR,
122 LOG_MESSAGE("Journal stopped"),
123 NULL);
124
125 finish:
126 server_done(&server);
127
128 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
129 }