chiark / gitweb /
journal: allow journal_file_copy_entry() to work on non-local files
[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 (getppid() != 1) { */
41         /*         log_error("This program should be invoked by init only."); */
42         /*         return EXIT_FAILURE; */
43         /* } */
44
45         if (argc > 1) {
46                 log_error("This program does not take arguments.");
47                 return EXIT_FAILURE;
48         }
49
50         log_set_target(LOG_TARGET_SAFE);
51         log_set_facility(LOG_SYSLOG);
52         log_parse_environment();
53         log_open();
54
55         umask(0022);
56
57         r = server_init(&server);
58         if (r < 0)
59                 goto finish;
60
61         server_vacuum(&server);
62         server_flush_to_var(&server);
63         server_flush_dev_kmsg(&server);
64
65         log_debug("systemd-journald running as pid %lu", (unsigned long) getpid());
66         server_driver_message(&server, SD_MESSAGE_JOURNAL_START, "Journal started");
67
68         sd_notify(false,
69                   "READY=1\n"
70                   "STATUS=Processing requests...");
71
72         for (;;) {
73                 struct epoll_event event;
74                 int t = -1;
75                 usec_t n;
76
77                 n = now(CLOCK_REALTIME);
78
79                 if (server.max_retention_usec > 0 && server.oldest_file_usec > 0) {
80
81                         /* The retention time is reached, so let's vacuum! */
82                         if (server.oldest_file_usec + server.max_retention_usec < n) {
83                                 log_info("Retention time reached.");
84                                 server_rotate(&server);
85                                 server_vacuum(&server);
86                                 continue;
87                         }
88
89                         /* Calculate when to rotate the next time */
90                         t = (int) ((server.oldest_file_usec + server.max_retention_usec - n + USEC_PER_MSEC - 1) / USEC_PER_MSEC);
91                 }
92
93 #ifdef HAVE_GCRYPT
94                 if (server.system_journal) {
95                         usec_t u;
96
97                         if (journal_file_next_evolve_usec(server.system_journal, &u)) {
98                                 if (n >= u)
99                                         t = 0;
100                                 else
101                                         t = MIN(t, (int) ((u - n + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
102                         }
103                 }
104 #endif
105
106                 r = epoll_wait(server.epoll_fd, &event, 1, t);
107                 if (r < 0) {
108
109                         if (errno == EINTR)
110                                 continue;
111
112                         log_error("epoll_wait() failed: %m");
113                         r = -errno;
114                         goto finish;
115                 }
116
117                 if (r > 0) {
118                         r = process_event(&server, &event);
119                         if (r < 0)
120                                 goto finish;
121                         else if (r == 0)
122                                 break;
123                 }
124
125                 server_maybe_append_tags(&server);
126                 server_maybe_warn_forward_syslog_missed(&server);
127         }
128
129         log_debug("systemd-journald stopped as pid %lu", (unsigned long) getpid());
130         server_driver_message(&server, SD_MESSAGE_JOURNAL_STOP, "Journal stopped");
131
132 finish:
133         sd_notify(false,
134                   "STATUS=Shutting down...");
135
136         server_done(&server);
137
138         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
139 }