]>
Commit | Line | Data |
---|---|---|
87d2c1ff LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2011 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
7 | under the terms of the GNU Lesser General Public License as published by |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
87d2c1ff LP |
9 | (at your option) any later version. |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 14 | Lesser General Public License for more details. |
87d2c1ff | 15 | |
5430f7f2 | 16 | You should have received a copy of the GNU Lesser General Public License |
87d2c1ff LP |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
18 | ***/ | |
19 | ||
87d2c1ff | 20 | #include <unistd.h> |
87d2c1ff | 21 | |
8580d1f7 | 22 | #include "sd-daemon.h" |
cf0fbc49 | 23 | #include "sd-messages.h" |
81527be1 | 24 | |
f97b34a6 | 25 | #include "format-util.h" |
89fef990 | 26 | #include "journal-authenticate.h" |
ef63833d | 27 | #include "journald-kmsg.h" |
cf0fbc49 | 28 | #include "journald-server.h" |
35e2e347 | 29 | #include "journald-syslog.h" |
fa6ac760 LP |
30 | #include "sigbus.h" |
31 | ||
87d2c1ff LP |
32 | int main(int argc, char *argv[]) { |
33 | Server server; | |
34 | int r; | |
35 | ||
87d2c1ff LP |
36 | if (argc > 1) { |
37 | log_error("This program does not take arguments."); | |
38 | return EXIT_FAILURE; | |
39 | } | |
40 | ||
a6903061 | 41 | log_set_target(LOG_TARGET_SAFE); |
3eff4208 | 42 | log_set_facility(LOG_SYSLOG); |
87d2c1ff LP |
43 | log_parse_environment(); |
44 | log_open(); | |
45 | ||
46 | umask(0022); | |
47 | ||
fa6ac760 LP |
48 | sigbus_install(); |
49 | ||
87d2c1ff LP |
50 | r = server_init(&server); |
51 | if (r < 0) | |
52 | goto finish; | |
53 | ||
3a19f215 | 54 | server_vacuum(&server, false); |
f78273c8 | 55 | server_flush_to_var(&server, true); |
51abe64c | 56 | server_flush_dev_kmsg(&server); |
e6960940 | 57 | |
df0ff127 | 58 | log_debug("systemd-journald running as pid "PID_FMT, getpid_cached()); |
2b044526 ZJS |
59 | server_driver_message(&server, |
60 | "MESSAGE_ID=" SD_MESSAGE_JOURNAL_START_STR, | |
8a03c9ef ZJS |
61 | LOG_MESSAGE("Journal started"), |
62 | NULL); | |
87d2c1ff | 63 | |
18e758bf FB |
64 | /* Make sure to send the usage message *after* flushing the |
65 | * journal so entries from the runtime journals are ordered | |
66 | * before this message. See #4190 for some details. */ | |
67 | server_space_usage_message(&server, NULL); | |
68 | ||
87d2c1ff | 69 | for (;;) { |
3a43da28 | 70 | usec_t t = USEC_INFINITY, n; |
87d2c1ff | 71 | |
565a9388 LP |
72 | r = sd_event_get_state(server.event); |
73 | if (r < 0) | |
74 | goto finish; | |
75 | if (r == SD_EVENT_FINISHED) | |
76 | break; | |
77 | ||
fb0951b0 | 78 | n = now(CLOCK_REALTIME); |
89fef990 | 79 | |
fb0951b0 | 80 | if (server.max_retention_usec > 0 && server.oldest_file_usec > 0) { |
89fef990 | 81 | |
fb0951b0 LP |
82 | /* The retention time is reached, so let's vacuum! */ |
83 | if (server.oldest_file_usec + server.max_retention_usec < n) { | |
84 | log_info("Retention time reached."); | |
85 | server_rotate(&server); | |
3a19f215 | 86 | server_vacuum(&server, false); |
fb0951b0 LP |
87 | continue; |
88 | } | |
89fef990 | 89 | |
fb0951b0 | 90 | /* Calculate when to rotate the next time */ |
f9a810be | 91 | t = server.oldest_file_usec + server.max_retention_usec - n; |
fb0951b0 LP |
92 | } |
93 | ||
349cc4a5 | 94 | #if HAVE_GCRYPT |
fb0951b0 LP |
95 | if (server.system_journal) { |
96 | usec_t u; | |
97 | ||
98 | if (journal_file_next_evolve_usec(server.system_journal, &u)) { | |
99 | if (n >= u) | |
100 | t = 0; | |
101 | else | |
f9a810be | 102 | t = MIN(t, u - n); |
fb0951b0 LP |
103 | } |
104 | } | |
89fef990 | 105 | #endif |
89fef990 | 106 | |
f9a810be | 107 | r = sd_event_run(server.event, t); |
87d2c1ff | 108 | if (r < 0) { |
da927ba9 | 109 | log_error_errno(r, "Failed to run event loop: %m"); |
87d2c1ff | 110 | goto finish; |
89fef990 | 111 | } |
87d2c1ff | 112 | |
99add6fd | 113 | server_maybe_append_tags(&server); |
178cc770 | 114 | server_maybe_warn_forward_syslog_missed(&server); |
87d2c1ff LP |
115 | } |
116 | ||
df0ff127 | 117 | log_debug("systemd-journald stopped as pid "PID_FMT, getpid_cached()); |
2b044526 ZJS |
118 | server_driver_message(&server, |
119 | "MESSAGE_ID=" SD_MESSAGE_JOURNAL_STOP_STR, | |
8a03c9ef ZJS |
120 | LOG_MESSAGE("Journal stopped"), |
121 | NULL); | |
fe652127 | 122 | |
87d2c1ff | 123 | finish: |
87d2c1ff LP |
124 | server_done(&server); |
125 | ||
126 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
127 | } |