chiark / gitweb /
journald: use "persistent", not "permanent" as storage option
[elogind.git] / src / journal / journald.c
index 1bf25e80918524d9d1b391cb8a9e012f8c40c13b..da9f52813dbf2a030634d6143e81a64771291f6c 100644 (file)
@@ -77,6 +77,7 @@
 
 typedef enum StdoutStreamState {
         STDOUT_STREAM_IDENTIFIER,
+        STDOUT_STREAM_UNIT_ID,
         STDOUT_STREAM_PRIORITY,
         STDOUT_STREAM_LEVEL_PREFIX,
         STDOUT_STREAM_FORWARD_TO_SYSLOG,
@@ -97,6 +98,7 @@ struct StdoutStream {
 #endif
 
         char *identifier;
+        char *unit_id;
         int priority;
         bool level_prefix:1;
         bool forward_to_syslog:1;
@@ -109,6 +111,16 @@ struct StdoutStream {
         LIST_FIELDS(StdoutStream, stdout_stream);
 };
 
+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 int server_flush_to_var(Server *s);
 
 static uint64_t available_space(Server *s) {
@@ -395,7 +407,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.");
@@ -458,7 +469,8 @@ static void dispatch_message_real(
                 struct iovec *iovec, unsigned n, unsigned m,
                 struct ucred *ucred,
                 struct timeval *tv,
-                const char *label, size_t label_len) {
+                const char *label, size_t label_len,
+                const char *unit_id) {
 
         char *pid = NULL, *uid = NULL, *gid = NULL,
                 *source_time = NULL, *boot_id = NULL, *machine_id = NULL,
@@ -560,10 +572,11 @@ static void dispatch_message_real(
                 if (cg_pid_get_unit(ucred->pid, &t) >= 0) {
                         unit = strappend("_SYSTEMD_UNIT=", t);
                         free(t);
+                } else if (unit_id)
+                        unit = strappend("_SYSTEMD_UNIT=", unit_id);
 
-                        if (unit)
-                                IOVEC_SET_STRING(iovec[n++], unit);
-                }
+                if (unit)
+                        IOVEC_SET_STRING(iovec[n++], unit);
 
 #ifdef HAVE_SELINUX
                 if (label) {
@@ -621,9 +634,7 @@ static void dispatch_message_real(
 
 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 {
+        if (f) {
                 r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
 
                 if ((r == -E2BIG || /* hit limit */
@@ -702,7 +713,7 @@ static void driver_message(Server *s, sd_id128_t message_id, const char *format,
         ucred.uid = getuid();
         ucred.gid = getgid();
 
-        dispatch_message_real(s, iovec, n, ELEMENTSOF(iovec), &ucred, NULL, NULL, 0);
+        dispatch_message_real(s, iovec, n, ELEMENTSOF(iovec), &ucred, NULL, NULL, 0, NULL);
 }
 
 static void dispatch_message(Server *s,
@@ -710,6 +721,7 @@ static void dispatch_message(Server *s,
                              struct ucred *ucred,
                              struct timeval *tv,
                              const char *label, size_t label_len,
+                             const char *unit_id,
                              int priority) {
         int rl;
         char *path = NULL, *c;
@@ -760,7 +772,7 @@ static void dispatch_message(Server *s,
         free(path);
 
 finish:
-        dispatch_message_real(s, iovec, n, m, ucred, tv, label, label_len);
+        dispatch_message_real(s, iovec, n, m, ucred, tv, label, label_len, unit_id);
 }
 
 static void forward_syslog_iovec(Server *s, const struct iovec *iovec, unsigned n_iovec, struct ucred *ucred, struct timeval *tv) {
@@ -1126,7 +1138,7 @@ static void process_syslog_message(Server *s, const char *buf, struct ucred *ucr
         if (message)
                 IOVEC_SET_STRING(iovec[n++], message);
 
-        dispatch_message(s, iovec, n, ELEMENTSOF(iovec), ucred, tv, label, label_len, priority);
+        dispatch_message(s, iovec, n, ELEMENTSOF(iovec), ucred, tv, label, label_len, NULL, priority);
 
         free(message);
         free(identifier);
@@ -1204,7 +1216,7 @@ static void process_native_message(
 
                 if (e == p) {
                         /* Entry separator */
-                        dispatch_message(s, iovec, n, m, ucred, tv, label, label_len, priority);
+                        dispatch_message(s, iovec, n, m, ucred, tv, label, label_len, NULL, priority);
                         n = 0;
                         priority = LOG_INFO;
 
@@ -1354,7 +1366,7 @@ static void process_native_message(
                         forward_console(s, priority, identifier, message, ucred);
         }
 
-        dispatch_message(s, iovec, n, m, ucred, tv, label, label_len, priority);
+        dispatch_message(s, iovec, n, m, ucred, tv, label, label_len, NULL, priority);
 
 finish:
         for (j = 0; j < n; j++)  {
@@ -1477,7 +1489,7 @@ static int stdout_stream_log(StdoutStream *s, const char *p) {
         }
 #endif
 
-        dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL, label, label_len, priority);
+        dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL, label, label_len, s->unit_id, priority);
 
         free(message);
         free(syslog_priority);
@@ -1508,6 +1520,22 @@ static int stdout_stream_line(StdoutStream *s, char *p) {
                         }
                 }
 
+                s->state = STDOUT_STREAM_UNIT_ID;
+                return 0;
+
+        case STDOUT_STREAM_UNIT_ID:
+                if (s->ucred.uid == 0) {
+                        if (isempty(p))
+                                s->unit_id = NULL;
+                        else  {
+                                s->unit_id = strdup(p);
+                                if (!s->unit_id) {
+                                        log_error("Out of memory");
+                                        return -ENOMEM;
+                                }
+                        }
+                }
+
                 s->state = STDOUT_STREAM_PRIORITY;
                 return 0;
 
@@ -1874,7 +1902,7 @@ static void proc_kmsg_line(Server *s, const char *p) {
         if (message)
                 IOVEC_SET_STRING(iovec[n++], message);
 
-        dispatch_message(s, iovec, n, ELEMENTSOF(iovec), NULL, NULL, NULL, 0, priority);
+        dispatch_message(s, iovec, n, ELEMENTSOF(iovec), NULL, NULL, NULL, 0, NULL, priority);
 
 finish:
         free(message);
@@ -1934,12 +1962,23 @@ 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)) {
+
+                /* 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);
 
@@ -1967,7 +2006,8 @@ 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);
                 if (!fn)
@@ -2018,7 +2058,6 @@ 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;
@@ -2027,6 +2066,10 @@ static int server_flush_to_var(Server *s) {
 
         assert(s);
 
+        if (s->storage != STORAGE_AUTO &&
+            s->storage != STORAGE_PERSISTENT)
+                return 0;
+
         if (!s->runtime_journal)
                 return 0;
 
@@ -2091,6 +2134,7 @@ finish:
         s->runtime_journal = NULL;
 
         if (r >= 0) {
+                char path[] = "/run/log/journal/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
                 sd_id128_to_string(machine, path + 17);
                 rm_rf(path, false, true, false);
         }
@@ -2104,6 +2148,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)
@@ -2609,25 +2657,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);
         }
@@ -2678,7 +2727,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;