chiark / gitweb /
journal: set the _SYSTEMD_UNIT field for messages from terminated processes
authorEelco Dolstra <eelco.dolstra@logicblox.com>
Thu, 21 Jun 2012 20:40:47 +0000 (16:40 -0400)
committerLennart Poettering <lennart@poettering.net>
Thu, 21 Jun 2012 22:14:08 +0000 (00:14 +0200)
As described in

  https://bugs.freedesktop.org/show_bug.cgi?id=50184

the journal currently doesn't set fields such as _SYSTEMD_UNIT
properly for messages coming from processes that have already
terminated.  This means among other things that "systemctl status" may
not show some of the output of services that wrote messages just
before they exited.

This patch fixes this by having processes that log to the journal
write their unit identifier to journald when the connection to
/run/systemd/journal/stdout is opened.  Journald stores the unit ID
and uses it to fill in _SYSTEMD_UNIT when it cannot be obtained
normally (i.e. from the cgroup).  To prevent impersonating another
unit, this information is only used when the caller is root.

This doesn't fix the general problem of getting metadata about
messages from terminated processes (which requires some kernel
support), but it allows "systemctl status" and similar queries to do
the Right Thing for units that log via stdout/stderr.

src/core/execute.c
src/core/execute.h
src/core/mount.c
src/core/service.c
src/core/socket.c
src/core/swap.c
src/journal/journald.c

index bbc430e0556d685e79a6188edfeb257d3ceb328e..01dceb0788f9a78b755e75728b95ec7f44be8865 100644 (file)
@@ -176,7 +176,7 @@ static int open_null_as(int flags, int nfd) {
         return r;
 }
 
-static int connect_logger_as(const ExecContext *context, ExecOutput output, const char *ident, int nfd) {
+static int connect_logger_as(const ExecContext *context, ExecOutput output, const char *ident, const char *unit_id, int nfd) {
         int fd, r;
         union sockaddr_union sa;
 
@@ -205,6 +205,7 @@ static int connect_logger_as(const ExecContext *context, ExecOutput output, cons
         }
 
         dprintf(fd,
+                "%s\n"
                 "%s\n"
                 "%i\n"
                 "%i\n"
@@ -212,6 +213,7 @@ static int connect_logger_as(const ExecContext *context, ExecOutput output, cons
                 "%i\n"
                 "%i\n",
                 context->syslog_identifier ? context->syslog_identifier : ident,
+                unit_id,
                 context->syslog_priority,
                 !!context->syslog_level_prefix,
                 output == EXEC_OUTPUT_SYSLOG || output == EXEC_OUTPUT_SYSLOG_AND_CONSOLE,
@@ -311,7 +313,7 @@ static int setup_input(const ExecContext *context, int socket_fd, bool apply_tty
         }
 }
 
-static int setup_output(const ExecContext *context, int socket_fd, const char *ident, bool apply_tty_stdin) {
+static int setup_output(const ExecContext *context, int socket_fd, const char *ident, const char *unit_id, bool apply_tty_stdin) {
         ExecOutput o;
         ExecInput i;
 
@@ -358,7 +360,7 @@ static int setup_output(const ExecContext *context, int socket_fd, const char *i
         case EXEC_OUTPUT_KMSG_AND_CONSOLE:
         case EXEC_OUTPUT_JOURNAL:
         case EXEC_OUTPUT_JOURNAL_AND_CONSOLE:
-                return connect_logger_as(context, o, ident, STDOUT_FILENO);
+                return connect_logger_as(context, o, ident, unit_id, STDOUT_FILENO);
 
         case EXEC_OUTPUT_SOCKET:
                 assert(socket_fd >= 0);
@@ -369,7 +371,7 @@ static int setup_output(const ExecContext *context, int socket_fd, const char *i
         }
 }
 
-static int setup_error(const ExecContext *context, int socket_fd, const char *ident, bool apply_tty_stdin) {
+static int setup_error(const ExecContext *context, int socket_fd, const char *ident, const char *unit_id, bool apply_tty_stdin) {
         ExecOutput o, e;
         ExecInput i;
 
@@ -413,7 +415,7 @@ static int setup_error(const ExecContext *context, int socket_fd, const char *id
         case EXEC_OUTPUT_KMSG_AND_CONSOLE:
         case EXEC_OUTPUT_JOURNAL:
         case EXEC_OUTPUT_JOURNAL_AND_CONSOLE:
-                return connect_logger_as(context, e, ident, STDERR_FILENO);
+                return connect_logger_as(context, e, ident, unit_id, STDERR_FILENO);
 
         case EXEC_OUTPUT_SOCKET:
                 assert(socket_fd >= 0);
@@ -913,6 +915,7 @@ int exec_spawn(ExecCommand *command,
                CGroupBonding *cgroup_bondings,
                CGroupAttribute *cgroup_attributes,
                const char *cgroup_suffix,
+               const char *unit_id,
                int idle_pipe[2],
                pid_t *ret) {
 
@@ -1101,14 +1104,14 @@ int exec_spawn(ExecCommand *command,
                 }
 
                 if (!keep_stdout) {
-                        err = setup_output(context, socket_fd, path_get_file_name(command->path), apply_tty_stdin);
+                        err = setup_output(context, socket_fd, path_get_file_name(command->path), unit_id, apply_tty_stdin);
                         if (err < 0) {
                                 r = EXIT_STDOUT;
                                 goto fail_child;
                         }
                 }
 
-                err = setup_error(context, socket_fd, path_get_file_name(command->path), apply_tty_stdin);
+                err = setup_error(context, socket_fd, path_get_file_name(command->path), unit_id, apply_tty_stdin);
                 if (err < 0) {
                         r = EXIT_STDERR;
                         goto fail_child;
index 6c68169a8420cd4fc5e6806bf603ba531b18fb48..2083c2971ba77660c29cd10c220900a911f54671 100644 (file)
@@ -192,6 +192,7 @@ int exec_spawn(ExecCommand *command,
                struct CGroupBonding *cgroup_bondings,
                struct CGroupAttribute *cgroup_attributes,
                const char *cgroup_suffix,
+               const char *unit_id,
                int pipe_fd[2],
                pid_t *ret);
 
index b885baab002bb98b50c8792d86f44454e542fc1d..3f8cf8a68f004cb6b6bedac954c804ee2509f5f9 100644 (file)
@@ -770,6 +770,7 @@ static int mount_spawn(Mount *m, ExecCommand *c, pid_t *_pid) {
                             UNIT(m)->cgroup_bondings,
                             UNIT(m)->cgroup_attributes,
                             NULL,
+                            UNIT(m)->id,
                             NULL,
                             &pid)) < 0)
                 goto fail;
index 8941271679adde950d7471d5b854cc46b254b614..eeabe8fb01ec83b22ee653479360f1be95d39d3e 100644 (file)
@@ -1810,6 +1810,7 @@ static int service_spawn(
                        UNIT(s)->cgroup_bondings,
                        UNIT(s)->cgroup_attributes,
                        is_control ? "control" : NULL,
+                       UNIT(s)->id,
                        s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL,
                        &pid);
 
index 633663e7e0018f19a176a64aa6765e33b4b54078..ea24f4b4433658aabc39c9e5d3b89ba6bfd74c7f 100644 (file)
@@ -1153,6 +1153,7 @@ static int socket_spawn(Socket *s, ExecCommand *c, pid_t *_pid) {
                        UNIT(s)->cgroup_bondings,
                        UNIT(s)->cgroup_attributes,
                        NULL,
+                       UNIT(s)->id,
                        NULL,
                        &pid);
 
index e7ed1b88d728c710a813c87aeef5103d80e4e20c..f3c7fa30766ca3a0272715d7b4245b2854dee3a9 100644 (file)
@@ -590,6 +590,7 @@ static int swap_spawn(Swap *s, ExecCommand *c, pid_t *_pid) {
                             UNIT(s)->cgroup_bondings,
                             UNIT(s)->cgroup_attributes,
                             NULL,
+                            UNIT(s)->id,
                             NULL,
                             &pid)) < 0)
                 goto fail;
index 1bf25e80918524d9d1b391cb8a9e012f8c40c13b..619a98d1f2db252bef61193d3812ea6bbffe63a2 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;
@@ -458,7 +460,7 @@ 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 +562,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) {
@@ -702,7 +705,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 +713,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 +764,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 +1130,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 +1208,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 +1358,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 +1481,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 +1512,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 +1894,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);