chiark / gitweb /
core: add minimal templating system
[elogind.git] / execute.c
index bf9a087c050d354ace8477fb6dfb8989e8ec0ccb..9b407258ff20f00307af6c2a00832aa0c7a1e688 100644 (file)
--- a/execute.c
+++ b/execute.c
@@ -73,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
@@ -91,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;
 
@@ -137,7 +137,7 @@ static int open_null_as(int flags, int nfd) {
 
         if (fd != nfd) {
                 r = dup2(fd, nfd) < 0 ? -errno : nfd;
-                close_nointr(fd);
+                close_nointr_nofail(fd);
         } else
                 r = nfd;
 
@@ -191,7 +191,7 @@ static int connect_logger_as(const ExecContext *context, ExecOutput output, cons
 
         if (fd != nfd) {
                 r = dup2(fd, nfd) < 0 ? -errno : nfd;
-                close_nointr(fd);
+                close_nointr_nofail(fd);
         } else
                 r = nfd;
 
@@ -334,7 +334,6 @@ static int chown_terminal(int fd, uid_t uid) {
         struct stat st;
 
         assert(fd >= 0);
-        assert(uid >= 0);
 
         /* This might fail. What matters are the results. */
         fchown(fd, uid, -1);
@@ -343,8 +342,7 @@ static int chown_terminal(int fd, uid_t uid) {
         if (fstat(fd, &st) < 0)
                 return -errno;
 
-        if (st.st_uid != uid ||
-            st.st_mode != TTY_MODE)
+        if (st.st_uid != uid || (st.st_mode & 0777) != TTY_MODE)
                 return -EPERM;
 
         return 0;
@@ -667,8 +665,9 @@ static int enforce_user(const ExecContext *context, uid_t uid) {
 }
 
 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,
@@ -684,7 +683,10 @@ int exec_spawn(ExecCommand *command,
         assert(ret);
         assert(fds || n_fds <= 0);
 
-        if (!(line = exec_command_line(command)))
+        if (!argv)
+                argv = command->argv;
+
+        if (!(line = exec_command_line(argv)))
                 return -ENOMEM;
 
         log_debug("About to execute: %s", line);
@@ -734,7 +736,7 @@ int exec_spawn(ExecCommand *command,
                                 goto fail;
 
                         /* Now ask the question. */
-                        if (!(line = exec_command_line(command))) {
+                        if (!(line = exec_command_line(argv))) {
                                 r = EXIT_MEMORY;
                                 goto fail;
                         }
@@ -835,6 +837,12 @@ int exec_spawn(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)
@@ -843,12 +851,6 @@ int exec_spawn(ExecCommand *command,
                                 goto fail;
                         }
 
-                if (is_terminal_input(context->std_input))
-                        if (chown_terminal(STDIN_FILENO, uid) < 0) {
-                                r = EXIT_STDIN;
-                                goto fail;
-                        }
-
                 if (apply_chroot) {
                         if (context->root_directory)
                                 if (chroot(context->root_directory) < 0) {
@@ -952,7 +954,7 @@ int exec_spawn(ExecCommand *command,
                         goto fail;
                 }
 
-                execve(command->path, command->argv, final_env);
+                execve(command->path, argv, final_env);
                 r = EXIT_EXEC;
 
         fail:
@@ -1272,23 +1274,22 @@ void exec_status_dump(ExecStatus *s, FILE *f, const char *prefix) {
                         prefix, s->status);
 }
 
-char *exec_command_line(ExecCommand *c) {
+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++) = ' ';
@@ -1326,7 +1327,7 @@ void exec_command_dump(ExecCommand *c, FILE *f, const char *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",