chiark / gitweb /
journal: remove all of /run/log/journal when serializing, since the machine ID might...
[elogind.git] / src / journal / journald.c
index 5c6c4cae9ea9d50141610a09803f315d46964485..3e7581815b562d849ea2698a605f501d0befa990 100644 (file)
@@ -69,8 +69,6 @@
 
 #define RECHECK_AVAILABLE_SPACE_USEC (30*USEC_PER_SEC)
 
-#define RECHECK_VAR_AVAILABLE_USEC (30*USEC_PER_SEC)
-
 #define N_IOVEC_META_FIELDS 17
 
 #define ENTRY_SIZE_MAX (1024*1024*32)
@@ -111,7 +109,15 @@ struct StdoutStream {
         LIST_FIELDS(StdoutStream, stdout_stream);
 };
 
-static int server_flush_to_var(Server *s);
+static const char* const storage_table[] = {
+        [STORAGE_AUTO] = "auto",
+        [STORAGE_VOLATILE] = "volatile",
+        [STORAGE_PERSISTENT] = "persistent",
+        [STORAGE_NONE] = "none"
+};
+
+DEFINE_STRING_TABLE_LOOKUP(storage, Storage);
+DEFINE_CONFIG_PARSE_ENUM(config_parse_storage, storage, Storage, "Failed to parse storage setting");
 
 static uint64_t available_space(Server *s) {
         char ids[33], *p;
@@ -307,7 +313,7 @@ static JournalFile* find_journal(Server *s, uid_t uid) {
                 journal_file_close(f);
         }
 
-        r = journal_file_open_reliably(p, O_RDWR|O_CREAT, 0640, s->system_journal, &f);
+        r = journal_file_open_reliably(p, O_RDWR|O_CREAT, 0640, &s->system_metrics, s->system_journal, &f);
         free(p);
 
         if (r < 0)
@@ -397,7 +403,6 @@ static void server_vacuum(Server *s) {
                 free(p);
         }
 
-
         if (s->runtime_journal) {
                 if (asprintf(&p, "/run/log/journal/%s", ids) < 0) {
                         log_error("Out of memory.");
@@ -455,6 +460,71 @@ static char *shortened_cgroup_path(pid_t pid) {
         return path;
 }
 
+static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned n) {
+        JournalFile *f;
+        bool vacuumed = false;
+        int r;
+
+        assert(s);
+        assert(iovec);
+        assert(n > 0);
+
+        f = find_journal(s, uid);
+        if (!f)
+                return;
+
+        if (journal_file_rotate_suggested(f)) {
+                log_info("Journal header limits reached or header out-of-date, rotating.");
+                server_rotate(s);
+                server_vacuum(s);
+                vacuumed = true;
+
+                f = find_journal(s, uid);
+                if (!f)
+                        return;
+        }
+
+        for (;;) {
+                r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
+                if (r >= 0)
+                        return;
+
+                if (vacuumed ||
+                    (r != -E2BIG && /* hit limit */
+                     r != -EFBIG && /* hit fs limit */
+                     r != -EDQUOT && /* quota hit */
+                     r != -ENOSPC && /* disk full */
+                     r != -EBADMSG && /* corrupted */
+                     r != -ENODATA && /* truncated */
+                     r != -EHOSTDOWN && /* other machine */
+                     r != -EPROTONOSUPPORT && /* unsupported feature */
+                     r != -EBUSY && /* unclean shutdown */
+                     r != -ESHUTDOWN /* already archived */)) {
+                        log_error("Failed to write entry, ignoring: %s", strerror(-r));
+                        return;
+                }
+
+                if (r == -E2BIG || r == -EFBIG || r == EDQUOT || r == ENOSPC)
+                        log_info("Allocation limit reached, rotating.");
+                else if (r == -EHOSTDOWN)
+                        log_info("Journal file from other machine, rotating.");
+                else if (r == -EBUSY)
+                        log_info("Unlcean shutdown, rotating.");
+                else
+                        log_warning("Journal file corrupted, rotating.");
+
+                server_rotate(s);
+                server_vacuum(s);
+                vacuumed = true;
+
+                f = find_journal(s, uid);
+                if (!f)
+                        return;
+
+                log_info("Retrying write.");
+        }
+}
+
 static void dispatch_message_real(
                 Server *s,
                 struct iovec *iovec, unsigned n, unsigned m,
@@ -475,8 +545,6 @@ static void dispatch_message_real(
         int r;
         char *t;
         uid_t loginuid = 0, realuid = 0;
-        JournalFile *f;
-        bool vacuumed = false;
 
         assert(s);
         assert(iovec);
@@ -621,41 +689,7 @@ static void dispatch_message_real(
 
         assert(n <= m);
 
-        server_flush_to_var(s);
-
-retry:
-        f = find_journal(s, realuid == 0 ? 0 : loginuid);
-        if (!f)
-                log_warning("Dropping message, as we can't find a place to store the data.");
-        else {
-                r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
-
-                if ((r == -E2BIG || /* hit limit */
-                     r == -EFBIG || /* hit fs limit */
-                     r == -EDQUOT || /* quota hit */
-                     r == -ENOSPC || /* disk full */
-                     r == -EBADMSG || /* corrupted */
-                     r == -ENODATA || /* truncated */
-                     r == -EHOSTDOWN || /* other machine */
-                     r == -EPROTONOSUPPORT) && /* unsupported feature */
-                    !vacuumed) {
-
-                        if (r == -E2BIG)
-                                log_info("Allocation limit reached, rotating.");
-                        else
-                                log_warning("Journal file corrupted, rotating.");
-
-                        server_rotate(s);
-                        server_vacuum(s);
-                        vacuumed = true;
-
-                        log_info("Retrying write.");
-                        goto retry;
-                }
-
-                if (r < 0)
-                        log_error("Failed to write entry, ignoring: %s", strerror(-r));
-        }
+        write_to_journal(s, realuid == 0 ? 0 : loginuid, iovec, n);
 
         free(pid);
         free(uid);
@@ -834,7 +868,8 @@ static void forward_syslog_iovec(Server *s, const struct iovec *iovec, unsigned
                         return;
         }
 
-        log_debug("Failed to forward syslog message: %m");
+        if (errno != ENOENT)
+                log_debug("Failed to forward syslog message: %m");
 }
 
 static void forward_syslog_raw(Server *s, int priority, const char *buffer, struct ucred *ucred, struct timeval *tv) {
@@ -1955,27 +1990,34 @@ static int system_journal_open(Server *s) {
 
         sd_id128_to_string(machine, ids);
 
-        if (!s->system_journal) {
+        if (!s->system_journal &&
+            (s->storage == STORAGE_PERSISTENT || s->storage == STORAGE_AUTO) &&
+            access("/run/systemd/journal/flushed", F_OK) >= 0) {
+
+                /* If in auto mode: first try to create the machine
+                 * path, but not the prefix.
+                 *
+                 * If in persistent mode: create /var/log/journal and
+                 * the machine path */
+
+                if (s->storage == STORAGE_PERSISTENT)
+                        (void) mkdir("/var/log/journal/", 0755);
 
-                /* First try to create the machine path, but not the prefix */
                 fn = strappend("/var/log/journal/", ids);
                 if (!fn)
                         return -ENOMEM;
+
                 (void) mkdir(fn, 0755);
                 free(fn);
 
-                /* The create the system journal file */
-                fn = join("/var/log/journal/", ids, "/system.journal", NULL);
+                fn = strjoin("/var/log/journal/", ids, "/system.journal", NULL);
                 if (!fn)
                         return -ENOMEM;
 
-                r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, NULL, &s->system_journal);
+                r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, &s->system_metrics, NULL, &s->system_journal);
                 free(fn);
 
                 if (r >= 0) {
-                        journal_default_metrics(&s->system_metrics, s->system_journal->fd);
-
-                        s->system_journal->metrics = s->system_metrics;
                         s->system_journal->compress = s->compress;
 
                         server_fix_perms(s, s->system_journal, 0);
@@ -1988,9 +2030,10 @@ static int system_journal_open(Server *s) {
                 }
         }
 
-        if (!s->runtime_journal) {
+        if (!s->runtime_journal &&
+            (s->storage != STORAGE_NONE)) {
 
-                fn = join("/run/log/journal/", ids, "/system.journal", NULL);
+                fn = strjoin("/run/log/journal/", ids, "/system.journal", NULL);
                 if (!fn)
                         return -ENOMEM;
 
@@ -2000,7 +2043,7 @@ static int system_journal_open(Server *s) {
                          * if it already exists, so that we can flush
                          * it into the system journal */
 
-                        r = journal_file_open(fn, O_RDWR, 0640, NULL, &s->runtime_journal);
+                        r = journal_file_open(fn, O_RDWR, 0640, &s->runtime_metrics, NULL, &s->runtime_journal);
                         free(fn);
 
                         if (r < 0) {
@@ -2015,8 +2058,8 @@ static int system_journal_open(Server *s) {
                         /* OK, we really need the runtime journal, so create
                          * it if necessary. */
 
-                        (void) mkdir_parents_label(fn, 0755);
-                        r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, NULL, &s->runtime_journal);
+                        (void) mkdir_parents(fn, 0755);
+                        r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, &s->runtime_metrics, NULL, &s->runtime_journal);
                         free(fn);
 
                         if (r < 0) {
@@ -2026,9 +2069,6 @@ static int system_journal_open(Server *s) {
                 }
 
                 if (s->runtime_journal) {
-                        journal_default_metrics(&s->runtime_metrics, s->runtime_journal->fd);
-
-                        s->runtime_journal->metrics = s->runtime_metrics;
                         s->runtime_journal->compress = s->compress;
 
                         server_fix_perms(s, s->runtime_journal, 0);
@@ -2039,24 +2079,20 @@ static int system_journal_open(Server *s) {
 }
 
 static int server_flush_to_var(Server *s) {
-        char path[] = "/run/log/journal/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
         Object *o = NULL;
         int r;
         sd_id128_t machine;
         sd_journal *j;
-        usec_t ts;
 
         assert(s);
 
-        if (!s->runtime_journal)
+        if (s->storage != STORAGE_AUTO &&
+            s->storage != STORAGE_PERSISTENT)
                 return 0;
 
-        ts = now(CLOCK_MONOTONIC);
-        if (s->var_available_timestamp + RECHECK_VAR_AVAILABLE_USEC > ts)
+        if (!s->runtime_journal)
                 return 0;
 
-        s->var_available_timestamp = ts;
-
         system_journal_open(s);
 
         if (!s->system_journal)
@@ -2111,10 +2147,8 @@ finish:
         journal_file_close(s->runtime_journal);
         s->runtime_journal = NULL;
 
-        if (r >= 0) {
-                sd_id128_to_string(machine, path + 17);
-                rm_rf(path, false, true, false);
-        }
+        if (r >= 0)
+                rm_rf("/run/log/journal", false, true, false);
 
         return r;
 }
@@ -2125,6 +2159,10 @@ static int server_read_proc_kmsg(Server *s) {
         assert(s->proc_kmsg_fd >= 0);
 
         l = read(s->proc_kmsg_fd, s->proc_kmsg_buffer + s->proc_kmsg_length, sizeof(s->proc_kmsg_buffer) - 1 - s->proc_kmsg_length);
+        if (l == 0) /* the kernel is stupid and in some race
+                     * conditions returns 0 in the middle of the
+                     * stream. */
+                return 0;
         if (l < 0) {
 
                 if (errno == EAGAIN || errno == EINTR)
@@ -2188,6 +2226,7 @@ static int process_event(Server *s, struct epoll_event *ev) {
                 }
 
                 if (sfsi.ssi_signo == SIGUSR1) {
+                        touch("/run/systemd/journal/flushed");
                         server_flush_to_var(s);
                         return 1;
                 }
@@ -2630,25 +2669,26 @@ static int server_parse_proc_cmdline(Server *s) {
                         goto finish;
                 }
 
-                if (startswith(word, "systemd_journald.forward_to_syslog=")) {
+                if (startswith(word, "systemd.journald.forward_to_syslog=")) {
                         r = parse_boolean(word + 35);
                         if (r < 0)
                                 log_warning("Failed to parse forward to syslog switch %s. Ignoring.", word + 35);
                         else
                                 s->forward_to_syslog = r;
-                } else if (startswith(word, "systemd_journald.forward_to_kmsg=")) {
+                } else if (startswith(word, "systemd.journald.forward_to_kmsg=")) {
                         r = parse_boolean(word + 33);
                         if (r < 0)
                                 log_warning("Failed to parse forward to kmsg switch %s. Ignoring.", word + 33);
                         else
                                 s->forward_to_kmsg = r;
-                } else if (startswith(word, "systemd_journald.forward_to_console=")) {
+                } else if (startswith(word, "systemd.journald.forward_to_console=")) {
                         r = parse_boolean(word + 36);
                         if (r < 0)
                                 log_warning("Failed to parse forward to console switch %s. Ignoring.", word + 36);
                         else
                                 s->forward_to_console = r;
-                }
+                } else if (startswith(word, "systemd.journald"))
+                        log_warning("Invalid systemd.journald parameter. Ignoring.");
 
                 free(word);
         }
@@ -2699,7 +2739,6 @@ static int server_init(Server *s) {
         s->rate_limit_burst = DEFAULT_RATE_LIMIT_BURST;
 
         s->forward_to_syslog = true;
-        s->import_proc_kmsg = true;
 
         s->max_level_store = LOG_DEBUG;
         s->max_level_syslog = LOG_DEBUG;