chiark / gitweb /
support chrooting/setting of ioprio when spawning
[elogind.git] / execute.c
index ccf951a25a437974f681ca0d12763237dfe9839c..f2f2be74cf8b9a7d4710b465e05d2df5204ae848 100644 (file)
--- a/execute.c
+++ b/execute.c
@@ -15,6 +15,7 @@
 #include "macro.h"
 #include "util.h"
 #include "log.h"
+#include "ioprio.h"
 
 static int close_fds(int except[], unsigned n_except) {
         DIR *d;
@@ -263,7 +264,6 @@ int exec_spawn(const ExecCommand *command, const ExecContext *context, int *fds,
         if (pid == 0) {
                 char **e, **f = NULL;
                 int i, r;
-                char t[16];
                 sigset_t ss;
 
                 /* child */
@@ -274,31 +274,53 @@ int exec_spawn(const ExecCommand *command, const ExecContext *context, int *fds,
                         goto fail;
                 }
 
-                umask(context->umask);
-
-                if (chdir(context->directory ? context->directory : "/") < 0) {
-                        r = EXIT_CHDIR;
+                if (setpgid(0, 0) < 0) {
+                        r = EXIT_PGID;
                         goto fail;
                 }
 
+                umask(context->umask);
+
                 if (setup_output(context, file_name_from_path(command->path)) < 0) {
                         r = EXIT_OUTPUT;
                         goto fail;
                 }
 
-                snprintf(t, sizeof(t), "%i", context->oom_adjust);
-                char_array_0(t);
+                if (context->oom_adjust_set) {
+                        char t[16];
 
-                if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
-                        r = EXIT_OOM_ADJUST;
-                        goto fail;
+                        snprintf(t, sizeof(t), "%i", context->oom_adjust);
+                        char_array_0(t);
+
+                        if (write_one_line_file("/proc/self/oom_adj", t) < 0) {
+                                r = EXIT_OOM_ADJUST;
+                                goto fail;
+                        }
                 }
 
-                if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
-                        r = EXIT_NICE;
+                if (context->root_directory)
+                        if (chroot(context->root_directory) < 0) {
+                                r = EXIT_CHROOT;
+                                goto fail;
+                        }
+
+                if (chdir(context->working_directory ? context->working_directory : "/") < 0) {
+                        r = EXIT_CHDIR;
                         goto fail;
                 }
 
+                if (context->nice_set)
+                        if (setpriority(PRIO_PROCESS, 0, context->nice) < 0) {
+                                r = EXIT_NICE;
+                                goto fail;
+                        }
+
+                if (context->ioprio_set)
+                        if (ioprio_set(IOPRIO_WHO_PROCESS, 0, context->ioprio) < 0) {
+                                r = EXIT_IOPRIO;
+                                goto fail;
+                        }
+
                 if (close_fds(fds, n_fds) < 0 ||
                     shift_fds(fds, n_fds) < 0 ||
                     flags_fds(fds, n_fds) < 0) {
@@ -362,8 +384,13 @@ void exec_context_init(ExecContext *c) {
 
         c->umask = 0002;
         cap_clear(c->capabilities);
+        c->capabilities_set = false;
         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->output = 0;
         c->syslog_priority = LOG_DAEMON|LOG_INFO;
@@ -382,8 +409,10 @@ void exec_context_done(ExecContext *c) {
                 c->rlimit[l] = NULL;
         }
 
-        free(c->directory);
-        c->directory = NULL;
+        free(c->working_directory);
+        c->working_directory = NULL;
+        free(c->root_directory);
+        c->root_directory = NULL;
 
         free(c->syslog_identifier);
         c->syslog_identifier = NULL;
@@ -420,6 +449,14 @@ void exec_command_free_array(ExecCommand **c, unsigned n) {
 }
 
 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
+
+        static const char * const table[] = {
+                [IOPRIO_CLASS_NONE] = "none",
+                [IOPRIO_CLASS_RT] = "realtime",
+                [IOPRIO_CLASS_BE] = "best-effort",
+                [IOPRIO_CLASS_IDLE] = "idle"
+        };
+
         assert(c);
         assert(f);
 
@@ -428,13 +465,28 @@ void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) {
 
         fprintf(f,
                 "%sUmask: %04o\n"
-                "%sDirectory: %s\n"
-                "%sNice: %i\n"
-                "%sOOMAdjust: %i\n",
+                "%sWorking Directory: %s\n"
+                "%sRoot Directory: %s\n",
                 prefix, c->umask,
-                prefix, c->directory ? c->directory : "/",
-                prefix, c->nice,
-                prefix, c->oom_adjust);
+                prefix, c->working_directory ? c->working_directory : "/",
+                prefix, c->root_directory ? c->root_directory : "/");
+
+        if (c->nice_set)
+                fprintf(f,
+                        "%sNice: %i\n",
+                        prefix, c->nice);
+
+        if (c->oom_adjust_set)
+                fprintf(f,
+                        "%sOOMAdjust: %i\n",
+                        prefix, c->oom_adjust);
+
+        if (c->ioprio_set)
+                fprintf(f,
+                        "%sIOSchedulingClass: %s\n"
+                        "%sIOPriority: %i\n",
+                        prefix, table[IOPRIO_PRIO_CLASS(c->ioprio)],
+                        prefix, (int) IOPRIO_PRIO_DATA(c->ioprio));
 }
 
 void exec_status_fill(ExecStatus *s, pid_t pid, int code, int status) {