]>
Commit | Line | Data |
---|---|---|
87d2c1ff LP |
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 | |
5430f7f2 LP |
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 | |
87d2c1ff LP |
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 | |
5430f7f2 | 16 | Lesser General Public License for more details. |
87d2c1ff | 17 | |
5430f7f2 | 18 | You should have received a copy of the GNU Lesser General Public License |
87d2c1ff LP |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
20 | ***/ | |
21 | ||
87d2c1ff | 22 | #include <unistd.h> |
87d2c1ff | 23 | |
73f860db ZJS |
24 | #include "systemd/sd-messages.h" |
25 | #include "systemd/sd-daemon.h" | |
81527be1 | 26 | |
89fef990 | 27 | #include "journal-authenticate.h" |
d025f1e4 | 28 | #include "journald-server.h" |
ef63833d | 29 | #include "journald-kmsg.h" |
35e2e347 | 30 | #include "journald-syslog.h" |
87d2c1ff | 31 | |
fa6ac760 | 32 | #include "sigbus.h" |
6482f626 | 33 | #include "formats-util.h" |
fa6ac760 | 34 | |
87d2c1ff LP |
35 | int main(int argc, char *argv[]) { |
36 | Server server; | |
37 | int r; | |
38 | ||
87d2c1ff LP |
39 | if (argc > 1) { |
40 | log_error("This program does not take arguments."); | |
41 | return EXIT_FAILURE; | |
42 | } | |
43 | ||
a6903061 | 44 | log_set_target(LOG_TARGET_SAFE); |
3eff4208 | 45 | log_set_facility(LOG_SYSLOG); |
87d2c1ff LP |
46 | log_parse_environment(); |
47 | log_open(); | |
48 | ||
49 | umask(0022); | |
50 | ||
fa6ac760 LP |
51 | sigbus_install(); |
52 | ||
87d2c1ff LP |
53 | r = server_init(&server); |
54 | if (r < 0) | |
55 | goto finish; | |
56 | ||
e6960940 LP |
57 | server_vacuum(&server); |
58 | server_flush_to_var(&server); | |
51abe64c | 59 | server_flush_dev_kmsg(&server); |
e6960940 | 60 | |
de0671ee | 61 | log_debug("systemd-journald running as pid "PID_FMT, getpid()); |
ef63833d | 62 | server_driver_message(&server, SD_MESSAGE_JOURNAL_START, "Journal started"); |
87d2c1ff LP |
63 | |
64 | sd_notify(false, | |
65 | "READY=1\n" | |
fe652127 | 66 | "STATUS=Processing requests..."); |
50f20cfd | 67 | |
87d2c1ff | 68 | for (;;) { |
3a43da28 | 69 | usec_t t = USEC_INFINITY, n; |
87d2c1ff | 70 | |
565a9388 LP |
71 | r = sd_event_get_state(server.event); |
72 | if (r < 0) | |
73 | goto finish; | |
74 | if (r == SD_EVENT_FINISHED) | |
75 | break; | |
76 | ||
fb0951b0 | 77 | n = now(CLOCK_REALTIME); |
89fef990 | 78 | |
fb0951b0 | 79 | if (server.max_retention_usec > 0 && server.oldest_file_usec > 0) { |
89fef990 | 80 | |
fb0951b0 LP |
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 | } | |
89fef990 | 88 | |
fb0951b0 | 89 | /* Calculate when to rotate the next time */ |
f9a810be | 90 | t = server.oldest_file_usec + server.max_retention_usec - n; |
fb0951b0 LP |
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 | |
f9a810be | 101 | t = MIN(t, u - n); |
fb0951b0 LP |
102 | } |
103 | } | |
89fef990 | 104 | #endif |
89fef990 | 105 | |
f9a810be | 106 | r = sd_event_run(server.event, t); |
87d2c1ff | 107 | if (r < 0) { |
da927ba9 | 108 | log_error_errno(r, "Failed to run event loop: %m"); |
87d2c1ff | 109 | goto finish; |
89fef990 | 110 | } |
87d2c1ff | 111 | |
99add6fd | 112 | server_maybe_append_tags(&server); |
178cc770 | 113 | server_maybe_warn_forward_syslog_missed(&server); |
87d2c1ff LP |
114 | } |
115 | ||
de0671ee | 116 | log_debug("systemd-journald stopped as pid "PID_FMT, getpid()); |
ef63833d | 117 | server_driver_message(&server, SD_MESSAGE_JOURNAL_STOP, "Journal stopped"); |
fe652127 | 118 | |
87d2c1ff | 119 | finish: |
af4ec430 LP |
120 | sd_notify(false, |
121 | "STOPPING=1\n" | |
122 | "STATUS=Shutting down..."); | |
87d2c1ff LP |
123 | |
124 | server_done(&server); | |
125 | ||
126 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
127 | } |