chiark / gitweb /
core: replace readdir_r with readdir
[elogind.git] / src / core / execute.c
index a0d63a41f9a521b82b806537d4db1e19505da468..4265717895e196c147f47d47566acc99dc403992 100644 (file)
@@ -41,6 +41,7 @@
 #include <linux/seccomp-bpf.h>
 #include <glob.h>
 #include <libgen.h>
+#undef basename
 
 #ifdef HAVE_PAM
 #include <security/pam_appl.h>
@@ -74,6 +75,8 @@
 /* This assumes there is a 'tty' group */
 #define TTY_MODE 0620
 
+#define SNDBUF_SIZE (8*1024*1024)
+
 static int shift_fds(int fds[], unsigned n_fds) {
         int start, restart_from;
 
@@ -220,6 +223,8 @@ static int connect_logger_as(const ExecContext *context, ExecOutput output, cons
                 return -errno;
         }
 
+        fd_inc_sndbuf(fd, SNDBUF_SIZE);
+
         dprintf(fd,
                 "%s\n"
                 "%s\n"
@@ -908,7 +913,7 @@ static void rename_process_from_path(const char *path) {
         /* This resulting string must fit in 10 chars (i.e. the length
          * of "/sbin/init") to look pretty in /bin/ps */
 
-        p = path_get_file_name(path);
+        p = basename(path);
         if (isempty(p)) {
                 rename_process("(...)");
                 return;
@@ -1012,6 +1017,81 @@ static void do_idle_pipe_dance(int idle_pipe[4]) {
                 close_nointr_nofail(idle_pipe[3]);
 }
 
+static int build_environment(
+                ExecContext *c,
+                unsigned n_fds,
+                const char *home,
+                const char *username,
+                const char *shell,
+                char ***ret) {
+
+        _cleanup_strv_free_ char **our_env = NULL;
+        unsigned n_env = 0;
+        char *x;
+
+        assert(c);
+        assert(ret);
+
+        our_env = new(char*, 8);
+        if (!our_env)
+                return -ENOMEM;
+
+        if (n_fds > 0) {
+                if (asprintf(&x, "LISTEN_PID=%lu", (unsigned long) getpid()) < 0)
+                        return -ENOMEM;
+                our_env[n_env++] = x;
+
+                if (asprintf(&x, "LISTEN_FDS=%u", n_fds) < 0)
+                        return -ENOMEM;
+                our_env[n_env++] = x;
+        }
+
+        if (home) {
+                x = strappend("HOME=", home);
+                if (!x)
+                        return -ENOMEM;
+                our_env[n_env++] = x;
+        }
+
+        if (username) {
+                x = strappend("LOGNAME=", username);
+                if (!x)
+                        return -ENOMEM;
+                our_env[n_env++] = x;
+
+                x = strappend("USER=", username);
+                if (!x)
+                        return -ENOMEM;
+                our_env[n_env++] = x;
+        }
+
+        if (shell) {
+                x = strappend("SHELL=", shell);
+                if (!x)
+                        return -ENOMEM;
+                our_env[n_env++] = x;
+        }
+
+        if (is_terminal_input(c->std_input) ||
+            c->std_output == EXEC_OUTPUT_TTY ||
+            c->std_error == EXEC_OUTPUT_TTY ||
+            c->tty_path) {
+
+                x = strdup(default_term_for_tty(tty_path(c)));
+                if (!x)
+                        return -ENOMEM;
+                our_env[n_env++] = x;
+        }
+
+        our_env[n_env++] = NULL;
+        assert(n_env <= 8);
+
+        *ret = our_env;
+        our_env = NULL;
+
+        return 0;
+}
+
 int exec_spawn(ExecCommand *command,
                char **argv,
                ExecContext *context,
@@ -1084,7 +1164,7 @@ int exec_spawn(ExecCommand *command,
         if (pid == 0) {
                 _cleanup_strv_free_ char **our_env = NULL, **pam_env = NULL, **final_env = NULL, **final_argv = NULL;
                 const char *username = NULL, *home = NULL, *shell = NULL;
-                unsigned n_dont_close = 0, n_env = 0;
+                unsigned n_dont_close = 0;
                 int dont_close[n_fds + 3];
                 uid_t uid = (uid_t) -1;
                 gid_t gid = (gid_t) -1;
@@ -1197,13 +1277,13 @@ int exec_spawn(ExecCommand *command,
                         goto fail_child;
                 }
 
-                err = setup_output(context, STDOUT_FILENO, socket_fd, path_get_file_name(command->path), unit_id, apply_tty_stdin);
+                err = setup_output(context, STDOUT_FILENO, socket_fd, basename(command->path), unit_id, apply_tty_stdin);
                 if (err < 0) {
                         r = EXIT_STDOUT;
                         goto fail_child;
                 }
 
-                err = setup_output(context, STDERR_FILENO, socket_fd, path_get_file_name(command->path), unit_id, apply_tty_stdin);
+                err = setup_output(context, STDERR_FILENO, socket_fd, basename(command->path), unit_id, apply_tty_stdin);
                 if (err < 0) {
                         r = EXIT_STDERR;
                         goto fail_child;
@@ -1480,29 +1560,12 @@ int exec_spawn(ExecCommand *command,
                         }
                 }
 
-                our_env = new(char*, 8);
-                if (!our_env ||
-                    (n_fds > 0 && (
-                            asprintf(our_env + n_env++, "LISTEN_PID=%lu", (unsigned long) getpid()) < 0 ||
-                            asprintf(our_env + n_env++, "LISTEN_FDS=%u", n_fds) < 0)) ||
-                    (home && asprintf(our_env + n_env++, "HOME=%s", home) < 0) ||
-                    (username && (
-                            asprintf(our_env + n_env++, "LOGNAME=%s", username) < 0 ||
-                            asprintf(our_env + n_env++, "USER=%s", username) < 0)) ||
-                    (shell && asprintf(our_env + n_env++, "SHELL=%s", shell) < 0) ||
-                    ((is_terminal_input(context->std_input) ||
-                      context->std_output == EXEC_OUTPUT_TTY ||
-                      context->std_error == EXEC_OUTPUT_TTY) && (
-                              !(our_env[n_env++] = strdup(default_term_for_tty(tty_path(context))))))) {
-
-                        err = -ENOMEM;
+                err = build_environment(context, n_fds, home, username, shell, &our_env);
+                if (r < 0) {
                         r = EXIT_MEMORY;
                         goto fail_child;
                 }
 
-                our_env[n_env++] = NULL;
-                assert(n_env <= 8);
-
                 final_env = strv_env_merge(5,
                                            environment,
                                            our_env,