87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/***
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering This file is part of systemd.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering Copyright 2011 Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering systemd is free software; you can redistribute it and/or modify it
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering under the terms of the GNU Lesser General Public License as published by
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering (at your option) any later version.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering systemd is distributed in the hope that it will be useful, but
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering Lesser General Public License for more details.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering You should have received a copy of the GNU Lesser General Public License
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering***/
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include <unistd.h>
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering#include "sd-daemon.h"
cf0fbc49e67b55f8d346fc94de28c90113505297Thomas Hindoe Paaboel Andersen#include "sd-messages.h"
81527be142678057215665be66e4b3c8306a7ab3Lennart Poettering
cf0fbc49e67b55f8d346fc94de28c90113505297Thomas Hindoe Paaboel Andersen#include "formats-util.h"
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering#include "journal-authenticate.h"
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering#include "journald-kmsg.h"
cf0fbc49e67b55f8d346fc94de28c90113505297Thomas Hindoe Paaboel Andersen#include "journald-server.h"
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering#include "journald-syslog.h"
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering#include "sigbus.h"
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringint main(int argc, char *argv[]) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering Server server;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering int r;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (argc > 1) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering log_error("This program does not take arguments.");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return EXIT_FAILURE;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
a6903061530cac5fbaa99a080a93221c02c349f9Lennart Poettering log_set_target(LOG_TARGET_SAFE);
3eff4208ffecedd778fec260f0d4b18e94dab443Lennart Poettering log_set_facility(LOG_SYSLOG);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering log_parse_environment();
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering log_open();
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering umask(0022);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering sigbus_install();
fa6ac76083b8ffc1309876459f54f9f0e2843731Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering r = server_init(&server);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (r < 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering goto finish;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering server_vacuum(&server, false, false);
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering server_flush_to_var(&server);
51abe64c859c01ffad4f80840396ad9f6d5c9e62Lennart Poettering server_flush_dev_kmsg(&server);
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek log_debug("systemd-journald running as pid "PID_FMT, getpid());
8a03c9ef744e13dc700a7e7ca6cae8afdcf0d71cZbigniew Jędrzejewski-Szmek server_driver_message(&server, SD_MESSAGE_JOURNAL_START,
8a03c9ef744e13dc700a7e7ca6cae8afdcf0d71cZbigniew Jędrzejewski-Szmek LOG_MESSAGE("Journal started"),
8a03c9ef744e13dc700a7e7ca6cae8afdcf0d71cZbigniew Jędrzejewski-Szmek NULL);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (;;) {
3a43da2832dc5360a638d043f469a6dcbe025582Kay Sievers usec_t t = USEC_INFINITY, n;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
565a9388f261c6e459e1726e358284ff687ec941Lennart Poettering r = sd_event_get_state(server.event);
565a9388f261c6e459e1726e358284ff687ec941Lennart Poettering if (r < 0)
565a9388f261c6e459e1726e358284ff687ec941Lennart Poettering goto finish;
565a9388f261c6e459e1726e358284ff687ec941Lennart Poettering if (r == SD_EVENT_FINISHED)
565a9388f261c6e459e1726e358284ff687ec941Lennart Poettering break;
565a9388f261c6e459e1726e358284ff687ec941Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering n = now(CLOCK_REALTIME);
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering if (server.max_retention_usec > 0 && server.oldest_file_usec > 0) {
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering /* The retention time is reached, so let's vacuum! */
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering if (server.oldest_file_usec + server.max_retention_usec < n) {
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering log_info("Retention time reached.");
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering server_rotate(&server);
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering server_vacuum(&server, false, false);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering continue;
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering }
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering /* Calculate when to rotate the next time */
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering t = server.oldest_file_usec + server.max_retention_usec - n;
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering }
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering#ifdef HAVE_GCRYPT
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering if (server.system_journal) {
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering usec_t u;
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering if (journal_file_next_evolve_usec(server.system_journal, &u)) {
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering if (n >= u)
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering t = 0;
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering else
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering t = MIN(t, u - n);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering }
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering }
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering#endif
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering
f9a810bedacf1da7c505c1786a2416d592665926Lennart Poettering r = sd_event_run(server.event, t);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (r < 0) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_error_errno(r, "Failed to run event loop: %m");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering goto finish;
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
99add6fd0b8f4307ed79256e25f38da318e795c4Lennart Poettering server_maybe_append_tags(&server);
178cc7700c23ac088cd7190d7854282075028d91Lennart Poettering server_maybe_warn_forward_syslog_missed(&server);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
de0671ee7fe465e108f62dcbbbe9366f81dd9e9aZbigniew Jędrzejewski-Szmek log_debug("systemd-journald stopped as pid "PID_FMT, getpid());
8a03c9ef744e13dc700a7e7ca6cae8afdcf0d71cZbigniew Jędrzejewski-Szmek server_driver_message(&server, SD_MESSAGE_JOURNAL_STOP,
8a03c9ef744e13dc700a7e7ca6cae8afdcf0d71cZbigniew Jędrzejewski-Szmek LOG_MESSAGE("Journal stopped"),
8a03c9ef744e13dc700a7e7ca6cae8afdcf0d71cZbigniew Jędrzejewski-Szmek NULL);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringfinish:
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering server_done(&server);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}