]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journald.c
Merge pull request #2440 from poettering/journal-fix
[thirdparty/systemd.git] / src / journal / journald.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2011 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <unistd.h>
23
24 #include "sd-daemon.h"
25 #include "sd-messages.h"
26
27 #include "formats-util.h"
28 #include "journal-authenticate.h"
29 #include "journald-kmsg.h"
30 #include "journald-server.h"
31 #include "journald-syslog.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, false);
57 server_flush_to_var(&server);
58 server_flush_dev_kmsg(&server);
59
60 log_debug("systemd-journald running as pid "PID_FMT, getpid());
61 server_driver_message(&server, SD_MESSAGE_JOURNAL_START,
62 LOG_MESSAGE("Journal started"),
63 NULL);
64
65 for (;;) {
66 usec_t t = USEC_INFINITY, n;
67
68 r = sd_event_get_state(server.event);
69 if (r < 0)
70 goto finish;
71 if (r == SD_EVENT_FINISHED)
72 break;
73
74 n = now(CLOCK_REALTIME);
75
76 if (server.max_retention_usec > 0 && server.oldest_file_usec > 0) {
77
78 /* The retention time is reached, so let's vacuum! */
79 if (server.oldest_file_usec + server.max_retention_usec < n) {
80 log_info("Retention time reached.");
81 server_rotate(&server);
82 server_vacuum(&server, false, false);
83 continue;
84 }
85
86 /* Calculate when to rotate the next time */
87 t = server.oldest_file_usec + server.max_retention_usec - n;
88 }
89
90 #ifdef HAVE_GCRYPT
91 if (server.system_journal) {
92 usec_t u;
93
94 if (journal_file_next_evolve_usec(server.system_journal, &u)) {
95 if (n >= u)
96 t = 0;
97 else
98 t = MIN(t, u - n);
99 }
100 }
101 #endif
102
103 r = sd_event_run(server.event, t);
104 if (r < 0) {
105 log_error_errno(r, "Failed to run event loop: %m");
106 goto finish;
107 }
108
109 server_maybe_append_tags(&server);
110 server_maybe_warn_forward_syslog_missed(&server);
111 }
112
113 log_debug("systemd-journald stopped as pid "PID_FMT, getpid());
114 server_driver_message(&server, SD_MESSAGE_JOURNAL_STOP,
115 LOG_MESSAGE("Journal stopped"),
116 NULL);
117
118 finish:
119 server_done(&server);
120
121 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
122 }