journald.c revision da927ba997d68401563b927f92e6e40e021a8e5c
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
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 <sys/epoll.h>
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include <sys/socket.h>
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include <errno.h>
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include <unistd.h>
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
73f860db9893deab6aebceb53dd7d0deb662e832Zbigniew Jędrzejewski-Szmek#include "systemd/sd-journal.h"
73f860db9893deab6aebceb53dd7d0deb662e832Zbigniew Jędrzejewski-Szmek#include "systemd/sd-messages.h"
73f860db9893deab6aebceb53dd7d0deb662e832Zbigniew Jędrzejewski-Szmek#include "systemd/sd-daemon.h"
81527be142678057215665be66e4b3c8306a7ab3Lennart Poettering
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering#include "journal-authenticate.h"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#include "journald-server.h"
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering#include "journald-kmsg.h"
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering#include "journald-syslog.h"
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart 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
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering r = server_init(&server);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (r < 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering goto finish;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering server_vacuum(&server);
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());
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering server_driver_message(&server, SD_MESSAGE_JOURNAL_START, "Journal started");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering sd_notify(false,
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering "READY=1\n"
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering "STATUS=Processing requests...");
50f20cfdb0f127e415ab38c024d9ca7a3602f74bLennart 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);
fb0951b02ebf51a93acf12721d8857d31ce57ba3Lennart Poettering server_vacuum(&server);
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());
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering server_driver_message(&server, SD_MESSAGE_JOURNAL_STOP, "Journal stopped");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringfinish:
af4ec4309e8f82aad87a8d574785c12f8763d5f8Lennart Poettering sd_notify(false,
af4ec4309e8f82aad87a8d574785c12f8763d5f8Lennart Poettering "STOPPING=1\n"
af4ec4309e8f82aad87a8d574785c12f8763d5f8Lennart Poettering "STATUS=Shutting down...");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering server_done(&server);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}