chiark / gitweb /
automount: implement automount unit type
[elogind.git] / execute.c
index 0374eaec5dae56ff2e339d225f1f2ec9078475cd..357fd5c20874aea7a6c670b89f7e737f64aa3fb1 100644 (file)
--- a/execute.c
+++ b/execute.c
@@ -44,6 +44,9 @@
 #include "securebits.h"
 #include "cgroup.h"
 
+/* This assumes there is a 'tty' group */
+#define TTY_MODE 0620
+
 static int shift_fds(int fds[], unsigned n_fds) {
         int start, restart_from;
 
@@ -70,7 +73,7 @@ static int shift_fds(int fds[], unsigned n_fds) {
                         if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0)
                                 return -errno;
 
-                        assert_se(close_nointr(fds[i]) == 0);
+                        close_nointr_nofail(fds[i]);
                         fds[i] = nfd;
 
                         /* Hmm, the fd we wanted isn't free? Then
@@ -88,7 +91,7 @@ static int shift_fds(int fds[], unsigned n_fds) {
         return 0;
 }
 
-static int flags_fds(int fds[], unsigned n_fds, bool nonblock) {
+static int flags_fds(const int fds[], unsigned n_fds, bool nonblock) {
         unsigned i;
         int r;
 
@@ -115,128 +118,395 @@ static int flags_fds(int fds[], unsigned n_fds, bool nonblock) {
         return 0;
 }
 
-static int replace_null_fd(int fd, int flags) {
-        int nfd;
-        assert(fd >= 0);
+static const char *tty_path(const ExecContext *context) {
+        assert(context);
+
+        if (context->tty_path)
+                return context->tty_path;
+
+        return "/dev/console";
+}
+
+static int open_null_as(int flags, int nfd) {
+        int fd, r;
+
+        assert(nfd >= 0);
+
+        if ((fd = open("/dev/null", flags|O_NOCTTY)) < 0)
+                return -errno;
+
+        if (fd != nfd) {
+                r = dup2(fd, nfd) < 0 ? -errno : nfd;
+                close_nointr_nofail(fd);
+        } else
+                r = nfd;
+
+        return r;
+}
 
-        close_nointr(fd);
+static int connect_logger_as(const ExecContext *context, ExecOutput output, const char *ident, int nfd) {
+        int fd, r;
+        union {
+                struct sockaddr sa;
+                struct sockaddr_un un;
+        } sa;
 
-        if ((nfd = open("/dev/null", flags|O_NOCTTY)) < 0)
+        assert(context);
+        assert(output < _EXEC_OUTPUT_MAX);
+        assert(ident);
+        assert(nfd >= 0);
+
+        if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
                 return -errno;
 
-        if (nfd != fd) {
-                close_nointr_nofail(nfd);
-                return -EIO;
+        zero(sa);
+        sa.sa.sa_family = AF_UNIX;
+        strncpy(sa.un.sun_path+1, LOGGER_SOCKET, sizeof(sa.un.sun_path)-1);
+
+        if (connect(fd, &sa.sa, sizeof(sa)) < 0) {
+                close_nointr_nofail(fd);
+                return -errno;
         }
 
-        return 0;
+        if (shutdown(fd, SHUT_RD) < 0) {
+                close_nointr_nofail(fd);
+                return -errno;
+        }
+
+        /* We speak a very simple protocol between log server
+         * and client: one line for the log destination (kmsg
+         * or syslog), followed by the priority field,
+         * followed by the process name. Since we replaced
+         * stdin/stderr we simple use stdio to write to
+         * it. Note that we use stderr, to minimize buffer
+         * flushing issues. */
+
+        dprintf(fd,
+                "%s\n"
+                "%i\n"
+                "%s\n",
+                output == EXEC_OUTPUT_KERNEL ? "kmsg" : "syslog",
+                context->syslog_priority,
+                context->syslog_identifier ? context->syslog_identifier : ident);
+
+        if (fd != nfd) {
+                r = dup2(fd, nfd) < 0 ? -errno : nfd;
+                close_nointr_nofail(fd);
+        } else
+                r = nfd;
+
+        return r;
 }
+static int open_terminal_as(const char *path, mode_t mode, int nfd) {
+        int fd, r;
 
-static int setup_output(const ExecContext *context, const char *ident) {
-        int r;
+        assert(path);
+        assert(nfd >= 0);
 
+        if ((fd = open_terminal(path, mode | O_NOCTTY)) < 0)
+                return fd;
+
+        if (fd != nfd) {
+                r = dup2(fd, nfd) < 0 ? -errno : nfd;
+                close_nointr_nofail(fd);
+        } else
+                r = nfd;
+
+        return r;
+}
+
+static bool is_terminal_input(ExecInput i) {
+        return
+                i == EXEC_INPUT_TTY ||
+                i == EXEC_INPUT_TTY_FORCE ||
+                i == EXEC_INPUT_TTY_FAIL;
+}
+
+static int fixup_input(const ExecContext *context, int socket_fd) {
         assert(context);
 
-        switch (context->output) {
+        if (socket_fd < 0 && context->std_input == EXEC_INPUT_SOCKET)
+                return EXEC_INPUT_NULL;
 
-        case EXEC_OUTPUT_CONSOLE:
-                return 0;
+        return context->std_input;
+}
 
-        case EXEC_OUTPUT_NULL:
+static int fixup_output(const ExecContext *context, int socket_fd) {
+        assert(context);
 
-                if ((r = replace_null_fd(STDOUT_FILENO, O_WRONLY)) < 0 ||
-                    (r = replace_null_fd(STDERR_FILENO, O_WRONLY)) < 0)
-                        return r;
+        if (socket_fd < 0 && context->std_output == EXEC_OUTPUT_SOCKET)
+                return EXEC_OUTPUT_INHERIT;
 
-                return 0;
+        return context->std_output;
+}
 
-        case EXEC_OUTPUT_KERNEL:
-        case EXEC_OUTPUT_SYSLOG: {
+static int fixup_error(const ExecContext *context, int socket_fd) {
+        assert(context);
 
-                int fd;
-                union {
-                        struct sockaddr sa;
-                        struct sockaddr_un un;
-                } sa;
+        if (socket_fd < 0 && context->std_error == EXEC_OUTPUT_SOCKET)
+                return EXEC_OUTPUT_INHERIT;
 
-                close_nointr(STDOUT_FILENO);
-                close_nointr(STDERR_FILENO);
+        return context->std_error;
+}
 
-                if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
-                        return -errno;
+static int setup_input(const ExecContext *context, int socket_fd) {
+        ExecInput i;
 
-                if (fd != STDOUT_FILENO) {
-                        close_nointr_nofail(fd);
-                        return -EIO;
-                }
+        assert(context);
 
-                zero(sa);
-                sa.sa.sa_family = AF_UNIX;
-                strncpy(sa.un.sun_path+1, LOGGER_SOCKET, sizeof(sa.un.sun_path)-1);
+        i = fixup_input(context, socket_fd);
 
-                if (connect(fd, &sa.sa, sizeof(sa)) < 0) {
-                        close_nointr_nofail(fd);
-                        return -errno;
-                }
+        switch (i) {
 
-                if (shutdown(fd, SHUT_RD) < 0) {
-                        close_nointr_nofail(fd);
-                        return -errno;
-                }
+        case EXEC_INPUT_NULL:
+                return open_null_as(O_RDONLY, STDIN_FILENO);
 
-                if ((fd = dup(fd)) < 0) {
-                        close_nointr_nofail(fd);
-                        return -errno;
-                }
+        case EXEC_INPUT_TTY:
+        case EXEC_INPUT_TTY_FORCE:
+        case EXEC_INPUT_TTY_FAIL: {
+                int fd, r;
+
+                if ((fd = acquire_terminal(
+                                     tty_path(context),
+                                     i == EXEC_INPUT_TTY_FAIL,
+                                     i == EXEC_INPUT_TTY_FORCE)) < 0)
+                        return fd;
 
-                if (fd != STDERR_FILENO) {
+                if (fd != STDIN_FILENO) {
+                        r = dup2(fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO;
                         close_nointr_nofail(fd);
-                        return -EIO;
-                }
+                } else
+                        r = STDIN_FILENO;
 
-                /* We speak a very simple protocol between log server
-                 * and client: one line for the log destination (kmsg
-                 * or syslog), followed by the priority field,
-                 * followed by the process name. Since we replaced
-                 * stdin/stderr we simple use stdio to write to
-                 * it. Note that we use stderr, to minimize buffer
-                 * flushing issues. */
-
-                fprintf(stderr,
-                        "%s\n"
-                        "%i\n"
-                        "%s\n",
-                        context->output == EXEC_OUTPUT_KERNEL ? "kmsg" : "syslog",
-                        context->syslog_priority,
-                        context->syslog_identifier ? context->syslog_identifier : ident);
+                return r;
+        }
 
-                return 0;
+        case EXEC_INPUT_SOCKET:
+                return dup2(socket_fd, STDIN_FILENO) < 0 ? -errno : STDIN_FILENO;
+
+        default:
+                assert_not_reached("Unknown input type");
         }
+}
+
+static int setup_output(const ExecContext *context, int socket_fd, const char *ident) {
+        ExecOutput o;
+        ExecInput i;
+
+        assert(context);
+        assert(ident);
+
+        i = fixup_input(context, socket_fd);
+        o = fixup_output(context, socket_fd);
+
+        /* This expects the input is already set up */
+
+        switch (o) {
+
+        case EXEC_OUTPUT_INHERIT:
+
+                /* If the input is connected to a terminal, inherit that... */
+                if (is_terminal_input(i) || i == EXEC_INPUT_SOCKET)
+                        return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
+
+                return STDIN_FILENO;
+
+        case EXEC_OUTPUT_NULL:
+                return open_null_as(O_WRONLY, STDOUT_FILENO);
+
+        case EXEC_OUTPUT_TTY:
+                if (is_terminal_input(i))
+                        return dup2(STDIN_FILENO, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
+
+                /* We don't reset the terminal if this is just about output */
+                return open_terminal_as(tty_path(context), O_WRONLY, STDOUT_FILENO);
+
+        case EXEC_OUTPUT_SYSLOG:
+        case EXEC_OUTPUT_KERNEL:
+                return connect_logger_as(context, o, ident, STDOUT_FILENO);
+
+        case EXEC_OUTPUT_SOCKET:
+                assert(socket_fd >= 0);
+                return dup2(socket_fd, STDOUT_FILENO) < 0 ? -errno : STDOUT_FILENO;
 
         default:
                 assert_not_reached("Unknown output type");
         }
 }
 
-static int setup_input(const ExecContext *context) {
-        int r;
+static int setup_error(const ExecContext *context, int socket_fd, const char *ident) {
+        ExecOutput o, e;
+        ExecInput i;
 
         assert(context);
+        assert(ident);
 
-        switch (context->input) {
+        i = fixup_input(context, socket_fd);
+        o = fixup_output(context, socket_fd);
+        e = fixup_error(context, socket_fd);
 
-        case EXEC_INPUT_CONSOLE:
-                return 0;
+        /* This expects the input and output are already set up */
 
-        case EXEC_INPUT_NULL:
-                if ((r = replace_null_fd(STDIN_FILENO, O_RDONLY)) < 0)
-                        return r;
+        /* Don't change the stderr file descriptor if we inherit all
+         * the way and are not on a tty */
+        if (e == EXEC_OUTPUT_INHERIT &&
+            o == EXEC_OUTPUT_INHERIT &&
+            !is_terminal_input(i))
+                return STDERR_FILENO;
 
-                return 0;
+        /* Duplicate form stdout if possible */
+        if (e == o || e == EXEC_OUTPUT_INHERIT)
+                return dup2(STDOUT_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
+
+        switch (e) {
+
+        case EXEC_OUTPUT_NULL:
+                return open_null_as(O_WRONLY, STDERR_FILENO);
+
+        case EXEC_OUTPUT_TTY:
+                if (is_terminal_input(i))
+                        return dup2(STDIN_FILENO, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
+
+                /* We don't reset the terminal if this is just about output */
+                return open_terminal_as(tty_path(context), O_WRONLY, STDERR_FILENO);
+
+        case EXEC_OUTPUT_SYSLOG:
+        case EXEC_OUTPUT_KERNEL:
+                return connect_logger_as(context, e, ident, STDERR_FILENO);
+
+        case EXEC_OUTPUT_SOCKET:
+                assert(socket_fd >= 0);
+                return dup2(socket_fd, STDERR_FILENO) < 0 ? -errno : STDERR_FILENO;
 
         default:
-                assert_not_reached("Unknown input type");
+                assert_not_reached("Unknown error type");
+        }
+}
+
+static int chown_terminal(int fd, uid_t uid) {
+        struct stat st;
+
+        assert(fd >= 0);
+
+        /* This might fail. What matters are the results. */
+        fchown(fd, uid, -1);
+        fchmod(fd, TTY_MODE);
+
+        if (fstat(fd, &st) < 0)
+                return -errno;
+
+        if (st.st_uid != uid || (st.st_mode & 0777) != TTY_MODE)
+                return -EPERM;
+
+        return 0;
+}
+
+static int setup_confirm_stdio(const ExecContext *context,
+                               int *_saved_stdin,
+                               int *_saved_stdout) {
+        int fd = -1, saved_stdin, saved_stdout = -1, r;
+
+        assert(context);
+        assert(_saved_stdin);
+        assert(_saved_stdout);
+
+        /* This returns positive EXIT_xxx return values instead of
+         * negative errno style values! */
+
+        if ((saved_stdin = fcntl(STDIN_FILENO, F_DUPFD, 3)) < 0)
+                return EXIT_STDIN;
+
+        if ((saved_stdout = fcntl(STDOUT_FILENO, F_DUPFD, 3)) < 0) {
+                r = EXIT_STDOUT;
+                goto fail;
+        }
+
+        if ((fd = acquire_terminal(
+                             tty_path(context),
+                             context->std_input == EXEC_INPUT_TTY_FAIL,
+                             context->std_input == EXEC_INPUT_TTY_FORCE)) < 0) {
+                r = EXIT_STDIN;
+                goto fail;
+        }
+
+        if (chown_terminal(fd, getuid()) < 0) {
+                r = EXIT_STDIN;
+                goto fail;
+        }
+
+        if (dup2(fd, STDIN_FILENO) < 0) {
+                r = EXIT_STDIN;
+                goto fail;
+        }
+
+        if (dup2(fd, STDOUT_FILENO) < 0) {
+                r = EXIT_STDOUT;
+                goto fail;
+        }
+
+        if (fd >= 2)
+                close_nointr_nofail(fd);
+
+        *_saved_stdin = saved_stdin;
+        *_saved_stdout = saved_stdout;
+
+        return 0;
+
+fail:
+        if (saved_stdout >= 0)
+                close_nointr_nofail(saved_stdout);
+
+        if (saved_stdin >= 0)
+                close_nointr_nofail(saved_stdin);
+
+        if (fd >= 0)
+                close_nointr_nofail(fd);
+
+        return r;
+}
+
+static int restore_conform_stdio(const ExecContext *context,
+                                 int *saved_stdin,
+                                 int *saved_stdout,
+                                 bool *keep_stdin,
+                                 bool *keep_stdout) {
+
+        assert(context);
+        assert(saved_stdin);
+        assert(*saved_stdin >= 0);
+        assert(saved_stdout);
+        assert(*saved_stdout >= 0);
+
+        /* This returns positive EXIT_xxx return values instead of
+         * negative errno style values! */
+
+        if (is_terminal_input(context->std_input)) {
+
+                /* The service wants terminal input. */
+
+                *keep_stdin = true;
+                *keep_stdout =
+                        context->std_output == EXEC_OUTPUT_INHERIT ||
+                        context->std_output == EXEC_OUTPUT_TTY;
+
+        } else {
+                /* If the service doesn't want a controlling terminal,
+                 * then we need to get rid entirely of what we have
+                 * already. */
+
+                if (release_terminal() < 0)
+                        return EXIT_STDIN;
+
+                if (dup2(*saved_stdin, STDIN_FILENO) < 0)
+                        return EXIT_STDIN;
+
+                if (dup2(*saved_stdout, STDOUT_FILENO) < 0)
+                        return EXIT_STDOUT;
+
+                *keep_stdout = *keep_stdin = false;
         }
+
+        return 0;
 }
 
 static int get_group_creds(const char *groupname, gid_t *gid) {
@@ -447,23 +717,48 @@ static int enforce_user(const ExecContext *context, uid_t uid) {
         return 0;
 }
 
-int exec_spawn(const ExecCommand *command,
+int exec_spawn(ExecCommand *command,
+               char **argv,
                const ExecContext *context,
-               int *fds, unsigned n_fds,
+               int fds[], unsigned n_fds,
                bool apply_permissions,
                bool apply_chroot,
+               bool confirm_spawn,
                CGroupBonding *cgroup_bondings,
                pid_t *ret) {
 
         pid_t pid;
         int r;
+        char *line;
+        int socket_fd;
 
         assert(command);
         assert(context);
         assert(ret);
         assert(fds || n_fds <= 0);
 
-        log_debug("About to execute %s", command->path);
+        if (context->std_input == EXEC_INPUT_SOCKET ||
+            context->std_output == EXEC_OUTPUT_SOCKET ||
+            context->std_error == EXEC_OUTPUT_SOCKET) {
+
+                if (n_fds != 1)
+                        return -EINVAL;
+
+                socket_fd = fds[0];
+
+                fds = NULL;
+                n_fds = 0;
+        } else
+                socket_fd = -1;
+
+        if (!argv)
+                argv = command->argv;
+
+        if (!(line = exec_command_line(argv)))
+                return -ENOMEM;
+
+        log_debug("About to execute: %s", line);
+        free(line);
 
         if (cgroup_bondings)
                 if ((r = cgroup_bonding_realize_list(cgroup_bondings)))
@@ -480,36 +775,73 @@ int exec_spawn(const ExecCommand *command,
                 gid_t gid = (gid_t) -1;
                 char **our_env = NULL, **final_env = NULL;
                 unsigned n_env = 0;
+                int saved_stdout = -1, saved_stdin = -1;
+                bool keep_stdout = false, keep_stdin = false;
 
                 /* child */
 
+                reset_all_signal_handlers();
+
                 if (sigemptyset(&ss) < 0 ||
                     sigprocmask(SIG_SETMASK, &ss, NULL) < 0) {
                         r = EXIT_SIGNAL_MASK;
                         goto fail;
                 }
 
-                if (context->new_session) {
+                if (!context->no_setsid)
                         if (setsid() < 0) {
                                 r = EXIT_SETSID;
                                 goto fail;
                         }
-                } else {
-                        if (setpgid(0, 0) < 0) {
-                                r = EXIT_PGID;
+
+                umask(context->umask);
+
+                if (confirm_spawn) {
+                        char response;
+
+                        /* Set up terminal for the question */
+                        if ((r = setup_confirm_stdio(context,
+                                                     &saved_stdin, &saved_stdout)))
+                                goto fail;
+
+                        /* Now ask the question. */
+                        if (!(line = exec_command_line(argv))) {
+                                r = EXIT_MEMORY;
                                 goto fail;
                         }
-                }
 
-                umask(context->umask);
+                        r = ask(&response, "yns", "Execute %s? [Yes, No, Skip] ", line);
+                        free(line);
 
-                if (setup_input(context) < 0) {
-                        r = EXIT_INPUT;
-                        goto fail;
+                        if (r < 0 || response == 'n') {
+                                r = EXIT_CONFIRM;
+                                goto fail;
+                        } else if (response == 's') {
+                                r = 0;
+                                goto fail;
+                        }
+
+                        /* Release terminal for the question */
+                        if ((r = restore_conform_stdio(context,
+                                                       &saved_stdin, &saved_stdout,
+                                                       &keep_stdin, &keep_stdout)))
+                                goto fail;
                 }
 
-                if (setup_output(context, file_name_from_path(command->path)) < 0) {
-                        r = EXIT_OUTPUT;
+                if (!keep_stdin)
+                        if (setup_input(context, socket_fd) < 0) {
+                                r = EXIT_STDIN;
+                                goto fail;
+                        }
+
+                if (!keep_stdout)
+                        if (setup_output(context, socket_fd, file_name_from_path(command->path)) < 0) {
+                                r = EXIT_STDOUT;
+                                goto fail;
+                        }
+
+                if (setup_error(context, socket_fd, file_name_from_path(command->path)) < 0) {
+                        r = EXIT_STDERR;
                         goto fail;
                 }
 
@@ -574,6 +906,12 @@ int exec_spawn(const ExecCommand *command,
                                 r = EXIT_USER;
                                 goto fail;
                         }
+
+                        if (is_terminal_input(context->std_input))
+                                if (chown_terminal(STDIN_FILENO, uid) < 0) {
+                                        r = EXIT_STDIN;
+                                        goto fail;
+                                }
                 }
 
                 if (apply_permissions)
@@ -685,19 +1023,38 @@ int exec_spawn(const ExecCommand *command,
                         goto fail;
                 }
 
-                execve(command->path, command->argv, final_env);
+                execve(command->path, argv, final_env);
                 r = EXIT_EXEC;
 
         fail:
                 strv_free(our_env);
                 strv_free(final_env);
 
+                if (saved_stdin >= 0)
+                        close_nointr_nofail(saved_stdin);
+
+                if (saved_stdout >= 0)
+                        close_nointr_nofail(saved_stdout);
+
                 _exit(r);
         }
 
+        /* We add the new process to the cgroup both in the child (so
+         * that we can be sure that no user code is ever executed
+         * outside of the cgroup) and in the parent (so that we can be
+         * sure that when we kill the cgroup the process will be
+         * killed too). */
+        if (cgroup_bondings)
+                if ((r = cgroup_bonding_install_list(cgroup_bondings, pid)) < 0) {
+                        r = EXIT_CGROUP;
+                        goto fail;
+                }
 
         log_debug("Forked %s as %llu", command->path, (unsigned long long) pid);
 
+        command->exec_status.pid = pid;
+        command->exec_status.start_timestamp = now(CLOCK_REALTIME);
+
         *ret = pid;
         return 0;
 }
@@ -722,10 +1079,10 @@ void exec_context_init(ExecContext *c) {
 
         c->cpu_sched_reset_on_fork = false;
         c->non_blocking = false;
-        c->new_session = false;
 
-        c->input = 0;
-        c->output = 0;
+        c->std_input = 0;
+        c->std_output = 0;
+        c->std_error = 0;
         c->syslog_priority = LOG_DAEMON|LOG_INFO;
 
         c->secure_bits = 0;
@@ -750,6 +1107,9 @@ void exec_context_done(ExecContext *c) {
         free(c->root_directory);
         c->root_directory = NULL;
 
+        free(c->tty_path);
+        c->tty_path = NULL;
+
         free(c->syslog_identifier);
         c->syslog_identifier = NULL;
 
@@ -768,14 +1128,29 @@ void exec_context_done(ExecContext *c) {
         }
 }
 
+void exec_command_done(ExecCommand *c) {
+        assert(c);
+
+        free(c->path);
+        c->path = NULL;
+
+        strv_free(c->argv);
+        c->argv = NULL;
+}
+
+void exec_command_done_array(ExecCommand *c, unsigned n) {
+        unsigned i;
+
+        for (i = 0; i < n; i++)
+                exec_command_done(c+i);
+}
+
 void exec_command_free_list(ExecCommand *c) {
         ExecCommand *i;
 
         while ((i = c)) {
                 LIST_REMOVE(ExecCommand, command, c, i);
-
-                free(i->path);
-                strv_free(i->argv);
+                exec_command_done(i);
                 free(i);
         }
 }
@@ -803,13 +1178,11 @@ void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
                 "%sUMask: %04o\n"
                 "%sWorkingDirectory: %s\n"
                 "%sRootDirectory: %s\n"
-                "%sNonBlocking: %s\n"
-                "%sNewSession: %s\n",
+                "%sNonBlocking: %s\n",
                 prefix, c->umask,
                 prefix, c->working_directory ? c->working_directory : "/",
                 prefix, c->root_directory ? c->root_directory : "/",
-                prefix, yes_no(c->non_blocking),
-                prefix, yes_no(c->new_session));
+                prefix, yes_no(c->non_blocking));
 
         if (c->environment)
                 for (e = c->environment; *e; e++)
@@ -857,12 +1230,20 @@ void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
                 fprintf(f, "%sTimerSlackNS: %lu\n", prefix, c->timer_slack_ns);
 
         fprintf(f,
-                "%sInput: %s\n"
-                "%sOutput: %s\n",
-                prefix, exec_input_to_string(c->input),
-                prefix, exec_output_to_string(c->output));
+                "%sStandardInput: %s\n"
+                "%sStandardOutput: %s\n"
+                "%sStandardError: %s\n",
+                prefix, exec_input_to_string(c->std_input),
+                prefix, exec_output_to_string(c->std_output),
+                prefix, exec_output_to_string(c->std_error));
+
+        if (c->tty_path)
+                fprintf(f,
+                        "%sTTYPath: %s\n",
+                        prefix, c->tty_path);
 
-        if (c->output == EXEC_OUTPUT_SYSLOG || c->output == EXEC_OUTPUT_KERNEL)
+        if (c->std_output == EXEC_OUTPUT_SYSLOG || c->std_output == EXEC_OUTPUT_KERNEL ||
+            c->std_error == EXEC_OUTPUT_SYSLOG || c->std_error == EXEC_OUTPUT_KERNEL)
                 fprintf(f,
                         "%sSyslogFacility: %s\n"
                         "%sSyslogLevel: %s\n",
@@ -925,28 +1306,59 @@ void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {
         assert(s);
 
         s->pid = pid;
+        s->exit_timestamp = now(CLOCK_REALTIME);
+
         s->code = code;
         s->status = status;
-        s->timestamp = now(CLOCK_REALTIME);
 }
 
-char *exec_command_line(ExecCommand *c) {
+void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix) {
+        char buf[FORMAT_TIMESTAMP_MAX];
+
+        assert(s);
+        assert(f);
+
+        if (!prefix)
+                prefix = "";
+
+        if (s->pid <= 0)
+                return;
+
+        fprintf(f,
+                "%sPID: %llu\n",
+                prefix, (unsigned long long) s->pid);
+
+        if (s->start_timestamp > 0)
+                fprintf(f,
+                        "%sStart Timestamp: %s\n",
+                        prefix, format_timestamp(buf, sizeof(buf), s->start_timestamp));
+
+        if (s->exit_timestamp > 0)
+                fprintf(f,
+                        "%sExit Timestamp: %s\n"
+                        "%sExit Code: %s\n"
+                        "%sExit Status: %i\n",
+                        prefix, format_timestamp(buf, sizeof(buf), s->exit_timestamp),
+                        prefix, sigchld_code_to_string(s->code),
+                        prefix, s->status);
+}
+
+char *exec_command_line(char **argv) {
         size_t k;
         char *n, *p, **a;
         bool first = true;
 
-        assert(c);
-        assert(c->argv);
+        assert(argv);
 
         k = 1;
-        STRV_FOREACH(a, c->argv)
+        STRV_FOREACH(a, argv)
                 k += strlen(*a)+3;
 
         if (!(n = new(char, k)))
                 return NULL;
 
         p = n;
-        STRV_FOREACH(a, c->argv) {
+        STRV_FOREACH(a, argv) {
 
                 if (!first)
                         *(p++) = ' ';
@@ -971,6 +1383,9 @@ char *exec_command_line(ExecCommand *c) {
 }
 
 void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
+        char *p2;
+        const char *prefix2;
+
         char *cmd;
 
         assert(c);
@@ -978,14 +1393,20 @@ void exec_command_dump(ExecCommand *c, FILE *f, const char *prefix) {
 
         if (!prefix)
                 prefix = "";
+        p2 = strappend(prefix, "\t");
+        prefix2 = p2 ? p2 : prefix;
 
-        cmd = exec_command_line(c);
+        cmd = exec_command_line(c->argv);
 
         fprintf(f,
                 "%sCommand Line: %s\n",
                 prefix, cmd ? cmd : strerror(ENOMEM));
 
         free(cmd);
+
+        exec_status_dump(&c->exec_status, f, prefix2);
+
+        free(p2);
 }
 
 void exec_command_dump_list(ExecCommand *c, FILE *f, const char *prefix) {
@@ -1012,18 +1433,156 @@ void exec_command_append_list(ExecCommand **l, ExecCommand *e) {
               *l = e;
 }
 
+int exec_command_set(ExecCommand *c, const char *path, ...) {
+        va_list ap;
+        char **l, *p;
+
+        assert(c);
+        assert(path);
+
+        va_start(ap, path);
+        l = strv_new_ap(path, ap);
+        va_end(ap);
+
+        if (!l)
+                return -ENOMEM;
+
+        if (!(p = strdup(path))) {
+                strv_free(l);
+                return -ENOMEM;
+        }
+
+        free(c->path);
+        c->path = p;
+
+        strv_free(c->argv);
+        c->argv = l;
+
+        return 0;
+}
+
+const char* exit_status_to_string(ExitStatus status) {
+
+        /* We cast to int here, so that -Wenum doesn't complain that
+         * EXIT_SUCCESS/EXIT_FAILURE aren't in the enum */
+
+        switch ((int) status) {
+
+        case EXIT_SUCCESS:
+                return "SUCCESS";
+
+        case EXIT_FAILURE:
+                return "FAILURE";
+
+        case EXIT_INVALIDARGUMENT:
+                return "INVALIDARGUMENT";
+
+        case EXIT_NOTIMPLEMENTED:
+                return "NOTIMPLEMENTED";
+
+        case EXIT_NOPERMISSION:
+                return "NOPERMISSION";
+
+        case EXIT_NOTINSTALLED:
+                return "NOTINSSTALLED";
+
+        case EXIT_NOTCONFIGURED:
+                return "NOTCONFIGURED";
+
+        case EXIT_NOTRUNNING:
+                return "NOTRUNNING";
+
+        case EXIT_CHDIR:
+                return "CHDIR";
+
+        case EXIT_NICE:
+                return "NICE";
+
+        case EXIT_FDS:
+                return "FDS";
+
+        case EXIT_EXEC:
+                return "EXEC";
+
+        case EXIT_MEMORY:
+                return "MEMORY";
+
+        case EXIT_LIMITS:
+                return "LIMITS";
+
+        case EXIT_OOM_ADJUST:
+                return "OOM_ADJUST";
+
+        case EXIT_SIGNAL_MASK:
+                return "SIGNAL_MASK";
+
+        case EXIT_STDIN:
+                return "STDIN";
+
+        case EXIT_STDOUT:
+                return "STDOUT";
+
+        case EXIT_CHROOT:
+                return "CHROOT";
+
+        case EXIT_IOPRIO:
+                return "IOPRIO";
+
+        case EXIT_TIMERSLACK:
+                return "TIMERSLACK";
+
+        case EXIT_SECUREBITS:
+                return "SECUREBITS";
+
+        case EXIT_SETSCHEDULER:
+                return "SETSCHEDULER";
+
+        case EXIT_CPUAFFINITY:
+                return "CPUAFFINITY";
+
+        case EXIT_GROUP:
+                return "GROUP";
+
+        case EXIT_USER:
+                return "USER";
+
+        case EXIT_CAPABILITIES:
+                return "CAPABILITIES";
+
+        case EXIT_CGROUP:
+                return "CGROUP";
+
+        case EXIT_SETSID:
+                return "SETSID";
+
+        case EXIT_CONFIRM:
+                return "CONFIRM";
+
+        case EXIT_STDERR:
+                return "STDERR";
+
+        default:
+                return NULL;
+        }
+}
+
+static const char* const exec_input_table[_EXEC_INPUT_MAX] = {
+        [EXEC_INPUT_NULL] = "null",
+        [EXEC_INPUT_TTY] = "tty",
+        [EXEC_INPUT_TTY_FORCE] = "tty-force",
+        [EXEC_INPUT_TTY_FAIL] = "tty-fail",
+        [EXEC_INPUT_SOCKET] = "socket"
+};
+
 static const char* const exec_output_table[_EXEC_OUTPUT_MAX] = {
-        [EXEC_OUTPUT_CONSOLE] = "console",
+        [EXEC_OUTPUT_INHERIT] = "inherit",
         [EXEC_OUTPUT_NULL] = "null",
+        [EXEC_OUTPUT_TTY] = "tty",
         [EXEC_OUTPUT_SYSLOG] = "syslog",
-        [EXEC_OUTPUT_KERNEL] = "kernel"
+        [EXEC_OUTPUT_KERNEL] = "kernel",
+        [EXEC_OUTPUT_SOCKET] = "socket"
 };
 
 DEFINE_STRING_TABLE_LOOKUP(exec_output, ExecOutput);
 
-static const char* const exec_input_table[_EXEC_INPUT_MAX] = {
-        [EXEC_INPUT_NULL] = "null",
-        [EXEC_INPUT_CONSOLE] = "console"
-};
-
 DEFINE_STRING_TABLE_LOOKUP(exec_input, ExecInput);