chiark / gitweb /
80f4634f6746ec32ae219ba95e10dec6259e6735
[elogind.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 <sys/epoll.h>
23 #include <sys/socket.h>
24 #include <errno.h>
25 #include <unistd.h>
26
27 #include "systemd/sd-journal.h"
28 #include "systemd/sd-messages.h"
29 #include "systemd/sd-daemon.h"
30
31 #include "journal-authenticate.h"
32 #include "journald-server.h"
33 #include "journald-kmsg.h"
34 #include "journald-syslog.h"
35
36 #include "sigbus.h"
37
38 int main(int argc, char *argv[]) {
39         Server server;
40         int r;
41
42         if (argc > 1) {
43                 log_error("This program does not take arguments.");
44                 return EXIT_FAILURE;
45         }
46
47         log_set_target(LOG_TARGET_SAFE);
48         log_set_facility(LOG_SYSLOG);
49         log_parse_environment();
50         log_open();
51
52         umask(0022);
53
54         sigbus_install();
55
56         r = server_init(&server);
57         if (r < 0)
58                 goto finish;
59
60         server_vacuum(&server);
61         server_flush_to_var(&server);
62         server_flush_dev_kmsg(&server);
63
64         log_debug("systemd-journald running as pid "PID_FMT, getpid());
65         server_driver_message(&server, SD_MESSAGE_JOURNAL_START, "Journal started");
66
67         sd_notify(false,
68                   "READY=1\n"
69                   "STATUS=Processing requests...");
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);
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 #ifdef 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());
120         server_driver_message(&server, SD_MESSAGE_JOURNAL_STOP, "Journal stopped");
121
122 finish:
123         sd_notify(false,
124                   "STOPPING=1\n"
125                   "STATUS=Shutting down...");
126
127         server_done(&server);
128
129         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
130 }