chiark / gitweb /
bus-proxyd: explicitly address messages to unique and well-known name
[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 int main(int argc, char *argv[]) {
37         Server server;
38         int r;
39
40         if (argc > 1) {
41                 log_error("This program does not take arguments.");
42                 return EXIT_FAILURE;
43         }
44
45         log_set_target(LOG_TARGET_SAFE);
46         log_set_facility(LOG_SYSLOG);
47         log_parse_environment();
48         log_open();
49
50         umask(0022);
51
52         r = server_init(&server);
53         if (r < 0)
54                 goto finish;
55
56         server_vacuum(&server);
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, "Journal started");
62
63         sd_notify(false,
64                   "READY=1\n"
65                   "STATUS=Processing requests...");
66
67         for (;;) {
68                 usec_t t = USEC_INFINITY, n;
69
70                 r = sd_event_get_state(server.event);
71                 if (r < 0)
72                         goto finish;
73                 if (r == SD_EVENT_FINISHED)
74                         break;
75
76                 n = now(CLOCK_REALTIME);
77
78                 if (server.max_retention_usec > 0 && server.oldest_file_usec > 0) {
79
80                         /* The retention time is reached, so let's vacuum! */
81                         if (server.oldest_file_usec + server.max_retention_usec < n) {
82                                 log_info("Retention time reached.");
83                                 server_rotate(&server);
84                                 server_vacuum(&server);
85                                 continue;
86                         }
87
88                         /* Calculate when to rotate the next time */
89                         t = server.oldest_file_usec + server.max_retention_usec - n;
90                 }
91
92 #ifdef HAVE_GCRYPT
93                 if (server.system_journal) {
94                         usec_t u;
95
96                         if (journal_file_next_evolve_usec(server.system_journal, &u)) {
97                                 if (n >= u)
98                                         t = 0;
99                                 else
100                                         t = MIN(t, u - n);
101                         }
102                 }
103 #endif
104
105                 r = sd_event_run(server.event, t);
106                 if (r < 0) {
107                         log_error("Failed to run event loop: %s", strerror(-r));
108                         goto finish;
109                 }
110
111                 server_maybe_append_tags(&server);
112                 server_maybe_warn_forward_syslog_missed(&server);
113         }
114
115         log_debug("systemd-journald stopped as pid "PID_FMT, getpid());
116         server_driver_message(&server, SD_MESSAGE_JOURNAL_STOP, "Journal stopped");
117
118 finish:
119         sd_notify(false,
120                   "STOPPING=1\n"
121                   "STATUS=Shutting down...");
122
123         server_done(&server);
124
125         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
126 }