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