+#include "log.h"
+#include "ioprio.h"
+#include "securebits.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;
+}
+
+static int shift_fds(int fds[], unsigned n_fds) {
+ int start, restart_from;
+
+ if (n_fds <= 0)
+ return 0;
+
+ assert(fds);
+
+ start = 0;
+ for (;;) {
+ int i;
+
+ restart_from = -1;
+
+ for (i = start; i < (int) n_fds; i++) {
+ int nfd;
+
+ /* Already at right index? */
+ if (fds[i] == i+3)
+ continue;
+
+ if ((nfd = fcntl(fds[i], F_DUPFD, i+3)) < 0)
+ return -errno;
+
+ assert_se(close_nointr(fds[i]) == 0);
+ fds[i] = nfd;
+
+ /* Hmm, the fd we wanted isn't free? Then
+ * let's remember that and try again from here*/
+ if (nfd != i+3 && restart_from < 0)
+ restart_from = i;
+ }
+
+ if (restart_from < 0)
+ break;
+
+ start = restart_from;
+ }
+
+ return 0;
+}
+
+static int flags_fds(int fds[], unsigned n_fds, bool nonblock) {
+ unsigned i;
+
+ if (n_fds <= 0)
+ return 0;
+
+ assert(fds);
+
+ /* 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;
+
+ /* 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;
+ }
+
+ return 0;
+}
+
+static int replace_null_fd(int fd, int flags) {
+ int nfd;
+ assert(fd >= 0);
+
+ close_nointr(fd);
+
+ if ((nfd = open("/dev/null", flags|O_NOCTTY)) < 0)
+ return -errno;
+
+ if (nfd != fd) {
+ close_nointr_nofail(nfd);
+ return -EIO;
+ }
+
+ return 0;
+}
+
+static int setup_output(const ExecContext *context, const char *ident) {
+ int r;
+
+ assert(context);
+
+ switch (context->output) {
+
+ case EXEC_OUTPUT_CONSOLE:
+ return 0;
+
+ case EXEC_OUTPUT_NULL:
+
+ if ((r = replace_null_fd(STDOUT_FILENO, O_WRONLY)) < 0 ||
+ (r = replace_null_fd(STDERR_FILENO, O_WRONLY)) < 0)
+ return r;
+
+ return 0;
+
+ case EXEC_OUTPUT_KERNEL:
+ case EXEC_OUTPUT_SYSLOG: {
+
+ int fd;
+ union {
+ struct sockaddr sa;
+ struct sockaddr_un un;
+ } sa;
+
+ close_nointr(STDOUT_FILENO);
+ close_nointr(STDERR_FILENO);
+
+ if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
+ return -errno;
+
+ if (fd != STDOUT_FILENO) {
+ close_nointr_nofail(fd);
+ 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;
+ }
+
+ if (shutdown(fd, SHUT_RD) < 0) {
+ close_nointr_nofail(fd);
+ return -errno;
+ }
+
+ if ((fd = dup(fd)) < 0) {
+ close_nointr_nofail(fd);
+ return -errno;
+ }
+
+ if (fd != STDERR_FILENO) {
+ close_nointr_nofail(fd);
+ return -EIO;
+ }
+
+ /* 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 0;
+ }
+
+ default:
+ assert_not_reached("Unknown output type");
+ }
+}
+
+static int setup_input(const ExecContext *context) {
+ int r;
+
+ assert(context);
+
+ switch (context->input) {
+
+ case EXEC_INPUT_CONSOLE:
+ return 0;
+
+ case EXEC_INPUT_NULL:
+ if ((r = replace_null_fd(STDIN_FILENO, O_RDONLY)) < 0)
+ return r;
+
+ return 0;
+
+ default:
+ assert_not_reached("Unknown input type");
+ }
+}
+
+int exec_spawn(const ExecCommand *command, const ExecContext *context, int *fds, unsigned n_fds, pid_t *ret) {
+ pid_t pid;