Systemd/src/journal/journald.c
Lennart Poettering f78273c8da journald: don't flush to /var/log/journal before we get asked to
This changes journald to not write to /var/log/journal until it received
SIGUSR1 for the first time, thus having been requested to flush the runtime
journal to disk.

This makes the journal work nicer with systems which have the root file system
writable early, but still need to rearrange /var before journald should start
writing and creating files to it, for example because ACLs need to be applied
first, or because /var is to be mounted from another file system, NFS or tmpfs
(as is the case for systemd.volatile=state).

Before this change we required setupts with /var split out to mount the root
disk read-only early on, and ship an /etc/fstab that remounted it writable only
after having placed /var at the right place. But even that was racy for various
preparations as journald might end up accessing the file system before it was
entirely set up, as soon as it was writable.

With this change we make scheduling when to start writing to /var/log/journal
explicit. This means persistent mode now requires
systemd-journal-flush.service in the mix to work, as otherwise journald would
never write to the directory.

See: #1397
2016-12-21 19:09:29 +01:00

126 lines
4 KiB
C

/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <unistd.h>
#include "sd-daemon.h"
#include "sd-messages.h"
#include "format-util.h"
#include "journal-authenticate.h"
#include "journald-kmsg.h"
#include "journald-server.h"
#include "journald-syslog.h"
#include "sigbus.h"
int main(int argc, char *argv[]) {
Server server;
int r;
if (argc > 1) {
log_error("This program does not take arguments.");
return EXIT_FAILURE;
}
log_set_target(LOG_TARGET_SAFE);
log_set_facility(LOG_SYSLOG);
log_parse_environment();
log_open();
umask(0022);
sigbus_install();
r = server_init(&server);
if (r < 0)
goto finish;
server_vacuum(&server, false);
server_flush_to_var(&server, true);
server_flush_dev_kmsg(&server);
log_debug("systemd-journald running as pid "PID_FMT, getpid());
server_driver_message(&server, SD_MESSAGE_JOURNAL_START,
LOG_MESSAGE("Journal started"),
NULL);
/* Make sure to send the usage message *after* flushing the
* journal so entries from the runtime journals are ordered
* before this message. See #4190 for some details. */
server_space_usage_message(&server, NULL);
for (;;) {
usec_t t = USEC_INFINITY, n;
r = sd_event_get_state(server.event);
if (r < 0)
goto finish;
if (r == SD_EVENT_FINISHED)
break;
n = now(CLOCK_REALTIME);
if (server.max_retention_usec > 0 && server.oldest_file_usec > 0) {
/* The retention time is reached, so let's vacuum! */
if (server.oldest_file_usec + server.max_retention_usec < n) {
log_info("Retention time reached.");
server_rotate(&server);
server_vacuum(&server, false);
continue;
}
/* Calculate when to rotate the next time */
t = server.oldest_file_usec + server.max_retention_usec - n;
}
#ifdef HAVE_GCRYPT
if (server.system_journal) {
usec_t u;
if (journal_file_next_evolve_usec(server.system_journal, &u)) {
if (n >= u)
t = 0;
else
t = MIN(t, u - n);
}
}
#endif
r = sd_event_run(server.event, t);
if (r < 0) {
log_error_errno(r, "Failed to run event loop: %m");
goto finish;
}
server_maybe_append_tags(&server);
server_maybe_warn_forward_syslog_missed(&server);
}
log_debug("systemd-journald stopped as pid "PID_FMT, getpid());
server_driver_message(&server, SD_MESSAGE_JOURNAL_STOP,
LOG_MESSAGE("Journal stopped"),
NULL);
finish:
server_done(&server);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}