X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=execute.c;h=38547677cfe758bc5b5858a6b9cccd34a45e161b;hp=bdb0f921794ddb612f821861a0087e15aa3474d7;hb=a16e112358ea8fea381ee106b89e645aed8b0a8c;hpb=81a2b7ceda7100fbc0dd8ce423b9599e03656851 diff --git a/execute.c b/execute.c index bdb0f9217..38547677c 100644 --- a/execute.c +++ b/execute.c @@ -42,55 +42,10 @@ #include "log.h" #include "ioprio.h" #include "securebits.h" +#include "cgroup.h" -static int close_fds(int except[], unsigned n_except) { - DIR *d; - struct dirent *de; - int r = 0; - - /* Modifies the fds array! (sorts it) */ - - if (!(d = opendir("/proc/self/fd"))) - return -errno; - - while ((de = readdir(d))) { - int fd; - - if (de->d_name[0] == '.') - continue; - - if ((r = safe_atoi(de->d_name, &fd)) < 0) - goto finish; - - if (fd < 3) - continue; - - if (fd == dirfd(d)) - continue; - - if (except) { - bool found; - unsigned i; - - found = false; - for (i = 0; i < n_except; i++) - if (except[i] == fd) { - found = true; - break; - } - - if (found) - continue; - } - - if ((r = close_nointr(fd)) < 0) - goto finish; - } - -finish: - closedir(d); - return r; -} +/* This assumes there is a 'tty' group */ +#define TTY_MODE 0620 static int shift_fds(int fds[], unsigned n_fds) { int start, restart_from; @@ -98,6 +53,8 @@ static int shift_fds(int fds[], unsigned n_fds) { if (n_fds <= 0) return 0; + /* Modifies the fds array! (sorts it) */ + assert(fds); start = 0; @@ -116,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 @@ -134,8 +91,9 @@ 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; if (n_fds <= 0) return 0; @@ -145,154 +103,410 @@ static int flags_fds(int fds[], unsigned n_fds, bool nonblock) { /* Drops/Sets O_NONBLOCK and FD_CLOEXEC from the file flags */ for (i = 0; i < n_fds; i++) { - int flags; - - if ((flags = fcntl(fds[i], F_GETFL, 0)) < 0) - return -errno; - - if (nonblock) - flags |= O_NONBLOCK; - else - flags &= ~O_NONBLOCK; - if (fcntl(fds[i], F_SETFL, flags) < 0) - return -errno; + if ((r = fd_nonblock(fds[i], nonblock)) < 0) + return r; /* We unconditionally drop FD_CLOEXEC from the fds, * since after all we want to pass these fds to our * children */ - if ((flags = fcntl(fds[i], F_GETFD, 0)) < 0) - return -errno; - if (fcntl(fds[i], F_SETFD, flags &~FD_CLOEXEC) < 0) - return -errno; + if ((r = fd_cloexec(fds[i], false)) < 0) + return r; } 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; - close_nointr(fd); + if (fd != nfd) { + r = dup2(fd, nfd) < 0 ? -errno : nfd; + close_nointr_nofail(fd); + } else + r = nfd; + + return r; +} + +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) { @@ -461,9 +675,13 @@ static int enforce_user(const ExecContext *context, uid_t uid) { /* First step: If we need to keep capabilities but * drop privileges we need to make sure we keep our * caps, whiel we drop priviliges. */ - if (uid != 0) - if (prctl(PR_SET_SECUREBITS, context->secure_bits|SECURE_KEEP_CAPS) < 0) - return -errno; + if (uid != 0) { + int sb = context->secure_bits|SECURE_KEEP_CAPS; + + if (prctl(PR_GET_SECUREBITS) != sb) + if (prctl(PR_SET_SECUREBITS, sb) < 0) + return -errno; + } /* Second step: set the capabilites. This will reduce * the capabilities to the minimum we need. */ @@ -499,59 +717,140 @@ 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))) + return r; if ((pid = fork()) < 0) return -errno; if (pid == 0) { - int i, r; + int i; sigset_t ss; const char *username = NULL, *home = NULL; uid_t uid = (uid_t) -1; 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 (setpgid(0, 0) < 0) { - r = EXIT_PGID; - goto fail; - } + if (!context->no_setsid) + if (setsid() < 0) { + r = EXIT_SETSID; + goto fail; + } umask(context->umask); - if (setup_input(context) < 0) { - r = EXIT_INPUT; - goto fail; + 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; + } + + r = ask(&response, "yns", "Execute %s? [Yes, No, Skip] ", line); + free(line); + + 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; } + if (cgroup_bondings) + if ((r = cgroup_bonding_install_list(cgroup_bondings, 0)) < 0) { + r = EXIT_CGROUP; + goto fail; + } + if (context->oom_adjust_set) { char t[16]; @@ -607,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) @@ -646,7 +951,7 @@ int exec_spawn(const ExecCommand *command, free(d); } - if (close_fds(fds, n_fds) < 0 || + if (close_all_fds(fds, n_fds) < 0 || shift_fds(fds, n_fds) < 0 || flags_fds(fds, n_fds, context->non_blocking) < 0) { r = EXIT_FDS; @@ -671,10 +976,15 @@ int exec_spawn(const ExecCommand *command, goto fail; } - if (prctl(PR_SET_SECUREBITS, context->secure_bits) < 0) { - r = EXIT_SECUREBITS; - goto fail; - } + /* PR_GET_SECUREBITS is not priviliged, while + * PR_SET_SECUREBITS is. So to suppress + * potential EPERMs we'll try not to call + * PR_SET_SECUREBITS unless necessary. */ + if (prctl(PR_GET_SECUREBITS) != context->secure_bits) + if (prctl(PR_SET_SECUREBITS, context->secure_bits) < 0) { + r = EXIT_SECUREBITS; + goto fail; + } if (context->capabilities) if (cap_set_proc(context->capabilities) < 0) { @@ -713,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; } @@ -734,24 +1063,9 @@ void exec_context_init(ExecContext *c) { assert(c); c->umask = 0002; - c->oom_adjust = 0; - c->oom_adjust_set = false; - c->nice = 0; - c->nice_set = false; c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 0); - c->ioprio_set = false; c->cpu_sched_policy = SCHED_OTHER; - c->cpu_sched_priority = 0; - c->cpu_sched_set = false; - CPU_ZERO(&c->cpu_affinity); - c->cpu_affinity_set = false; - - c->input = 0; - c->output = 0; c->syslog_priority = LOG_DAEMON|LOG_INFO; - - c->secure_bits = 0; - c->capability_bounding_set_drop = 0; } void exec_context_done(ExecContext *c) { @@ -772,6 +1086,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; @@ -790,14 +1107,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); } } @@ -877,12 +1209,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", @@ -945,28 +1285,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++) = ' '; @@ -991,6 +1362,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); @@ -998,14 +1372,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) { @@ -1032,18 +1412,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);