chiark / gitweb /
core: reuse the same /tmp, /var/tmp and inaccessible dir
authorMichal Sekletar <msekleta@redhat.com>
Thu, 14 Mar 2013 17:12:27 +0000 (18:12 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 16 Mar 2013 02:56:40 +0000 (22:56 -0400)
All Execs within the service, will get mounted the same
/tmp and /var/tmp directories, if service is configured with
PrivateTmp=yes. Temporary directories are cleaned up by service
itself in addition to systemd-tmpfiles. Directory which is mounted
as inaccessible is created at runtime in /run/systemd.

15 files changed:
man/systemd.exec.xml
src/core/execute.c
src/core/execute.h
src/core/manager.c
src/core/manager.h
src/core/mount-setup.c
src/core/mount.c
src/core/namespace.c
src/core/namespace.h
src/core/service.c
src/core/socket.c
src/core/swap.c
src/shared/util.c
src/shared/util.h
src/test/test-ns.c

index 9c31baf7935dcc96a57d89d169fc946de2dfa3b4..b1cd685cc0ba39be5920ba2e5a0e53e1ea7b573a 100644 (file)
                                 processes via
                                 <filename>/tmp</filename> or
                                 <filename>/var/tmp</filename>
-                                impossible. Defaults to
+                                impossible. All temporary data created
+                                by service will be removed after service
+                                is stopped. Defaults to
                                 false.</para></listitem>
                         </varlistentry>
 
index 92cf1746417e5ab14fd517142f54b8dee3b1d843..18e25fa6e69dd14ad7cf3dae0dde69b53da3195c 100644 (file)
@@ -173,6 +173,18 @@ static bool is_terminal_output(ExecOutput o) {
                 o == EXEC_OUTPUT_JOURNAL_AND_CONSOLE;
 }
 
+void exec_context_serialize(const ExecContext *context, Unit *u, FILE *f) {
+        assert(context);
+        assert(u);
+        assert(f);
+
+        if (context->tmp_dir)
+                unit_serialize_item(u, f, "tmp-dir", context->tmp_dir);
+
+        if (context->var_tmp_dir)
+                unit_serialize_item(u, f, "var-tmp-dir", context->var_tmp_dir);
+}
+
 static int open_null_as(int flags, int nfd) {
         int fd, r;
 
@@ -968,7 +980,7 @@ static int apply_seccomp(uint32_t *syscall_filter) {
 
 int exec_spawn(ExecCommand *command,
                char **argv,
-               const ExecContext *context,
+               ExecContext *context,
                int fds[], unsigned n_fds,
                char **environment,
                bool apply_permissions,
@@ -1036,6 +1048,12 @@ int exec_spawn(ExecCommand *command,
 
         cgroup_attribute_apply_list(cgroup_attributes, cgroup_bondings);
 
+        if (context->private_tmp && !context->tmp_dir && !context->var_tmp_dir) {
+                r = setup_tmpdirs(&context->tmp_dir, &context->var_tmp_dir);
+                if (r < 0)
+                        return r;
+        }
+
         pid = fork();
         if (pid < 0)
                 return -errno;
@@ -1302,6 +1320,8 @@ int exec_spawn(ExecCommand *command,
                         err = setup_namespace(context->read_write_dirs,
                                               context->read_only_dirs,
                                               context->inaccessible_dirs,
+                                              context->tmp_dir,
+                                              context->var_tmp_dir,
                                               context->private_tmp,
                                               context->mount_flags);
                         if (err < 0) {
@@ -1530,7 +1550,23 @@ void exec_context_init(ExecContext *c) {
         c->timer_slack_nsec = (nsec_t) -1;
 }
 
-void exec_context_done(ExecContext *c) {
+void exec_context_tmp_dirs_done(ExecContext *c) {
+        assert(c);
+
+        if (c->tmp_dir) {
+                rm_rf_dangerous(c->tmp_dir, false, true, false);
+                free(c->tmp_dir);
+                c->tmp_dir = NULL;
+        }
+
+        if (c->var_tmp_dir) {
+                rm_rf_dangerous(c->var_tmp_dir, false, true, false);
+                free(c->var_tmp_dir);
+                c->var_tmp_dir = NULL;
+        }
+}
+
+void exec_context_done(ExecContext *c, bool reloading_or_reexecuting) {
         unsigned l;
 
         assert(c);
@@ -1594,6 +1630,9 @@ void exec_context_done(ExecContext *c) {
 
         free(c->syscall_filter);
         c->syscall_filter = NULL;
+
+        if (!reloading_or_reexecuting)
+                exec_context_tmp_dirs_done(c);
 }
 
 void exec_command_done(ExecCommand *c) {
index 001eb0e7cc33aed9a4c2860bb012f52fc8f3ae19..3ce9221ab1c8e7cbc4e9d5cb253ebd7dc513112d 100644 (file)
@@ -36,6 +36,8 @@ typedef struct ExecContext ExecContext;
 struct CGroupBonding;
 struct CGroupAttribute;
 
+typedef struct Unit Unit;
+
 #include "list.h"
 #include "util.h"
 
@@ -141,6 +143,8 @@ struct ExecContext {
         bool non_blocking;
         bool private_tmp;
         bool private_network;
+        char *tmp_dir;
+        char *var_tmp_dir;
 
         bool no_new_privileges;
 
@@ -164,7 +168,7 @@ struct ExecContext {
 
 int exec_spawn(ExecCommand *command,
                char **argv,
-               const ExecContext *context,
+               ExecContext *context,
                int fds[], unsigned n_fds,
                char **environment,
                bool apply_permissions,
@@ -192,13 +196,15 @@ void exec_command_append_list(ExecCommand **l, ExecCommand *e);
 int exec_command_set(ExecCommand *c, const char *path, ...);
 
 void exec_context_init(ExecContext *c);
-void exec_context_done(ExecContext *c);
+void exec_context_done(ExecContext *c, bool reloading_or_reexecuting);
+void exec_context_tmp_dirs_done(ExecContext *c);
 void exec_context_dump(ExecContext *c, FILE* f, const char *prefix);
 void exec_context_tty_reset(const ExecContext *context);
 
 int exec_context_load_environment(const ExecContext *c, char ***l);
 
 bool exec_context_may_touch_console(ExecContext *c);
+void exec_context_serialize(const ExecContext *c, Unit *u, FILE *f);
 
 void exec_status_start(ExecStatus *s, pid_t pid);
 void exec_status_exit(ExecStatus *s, ExecContext *context, pid_t pid, int code, int status);
index 8e66732cd7bf8c68347f6a307b10f37e1aea2246..a01710f445f92f5f990d6602ee3bc7374228ffeb 100644 (file)
@@ -2333,6 +2333,12 @@ static bool manager_is_booting_or_shutting_down(Manager *m) {
         return false;
 }
 
+bool manager_is_reloading_or_reexecuting(Manager *m) {
+        assert(m);
+
+        return m->n_reloading != 0;
+}
+
 void manager_reset_failed(Manager *m) {
         Unit *u;
         Iterator i;
index c486a16887938045f560fe9b27a65f75eddc7c9e..9d8d9439d2a426420042440f9236e4cace6014e1 100644 (file)
@@ -85,6 +85,7 @@ struct Watch {
 #include "set.h"
 #include "dbus.h"
 #include "path-lookup.h"
+#include "execute.h"
 
 struct Manager {
         /* Note that the set of units we know of is allowed to be
@@ -283,6 +284,8 @@ int manager_distribute_fds(Manager *m, FDSet *fds);
 
 int manager_reload(Manager *m);
 
+bool manager_is_reloading_or_reexecuting(Manager *m);
+
 void manager_reset_failed(Manager *m);
 
 void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success);
index ce10be944a1133057ffd4b71ff58f94f1ad47087..6140f56d831ba5ee4b0c2c0d0c3eca6cb4645b43 100644 (file)
@@ -447,6 +447,7 @@ int mount_setup(bool loaded_policy) {
          * systemd. */
         mkdir_label("/run/systemd", 0755);
         mkdir_label("/run/systemd/system", 0755);
+        mkdir_label("/run/systemd/inaccessible", 0000);
 
         return 0;
 }
index 7a1b411c7b67f48f0221db6e43b4a152062aa66f..0adf04e9bf4aaac25c5f256e42a01c9a336f0632 100644 (file)
@@ -25,6 +25,7 @@
 #include <sys/epoll.h>
 #include <signal.h>
 
+#include "manager.h"
 #include "unit.h"
 #include "mount.h"
 #include "load-fragment.h"
@@ -126,7 +127,7 @@ static void mount_done(Unit *u) {
         mount_parameters_done(&m->parameters_proc_self_mountinfo);
         mount_parameters_done(&m->parameters_fragment);
 
-        exec_context_done(&m->exec_context);
+        exec_context_done(&m->exec_context, manager_is_reloading_or_reexecuting(u->manager));
         exec_command_done_array(m->exec_command, _MOUNT_EXEC_COMMAND_MAX);
         m->control_command = NULL;
 
@@ -870,6 +871,7 @@ static void mount_enter_dead(Mount *m, MountResult f) {
         if (f != MOUNT_SUCCESS)
                 m->result = f;
 
+        exec_context_tmp_dirs_done(&m->exec_context);
         mount_set_state(m, m->result != MOUNT_SUCCESS ? MOUNT_FAILED : MOUNT_DEAD);
 }
 
@@ -1163,6 +1165,8 @@ static int mount_serialize(Unit *u, FILE *f, FDSet *fds) {
         if (m->control_command_id >= 0)
                 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(m->control_command_id));
 
+        exec_context_serialize(&m->exec_context, UNIT(m), f);
+
         return 0;
 }
 
@@ -1219,7 +1223,22 @@ static int mount_deserialize_item(Unit *u, const char *key, const char *value, F
                         m->control_command_id = id;
                         m->control_command = m->exec_command + id;
                 }
+        } else if (streq(key, "tmp-dir")) {
+                char *t;
+
+                t = strdup(value);
+                if (!t)
+                        return log_oom();
+
+                m->exec_context.tmp_dir = t;
+        } else if (streq(key, "var-tmp-dir")) {
+                char *t;
+
+                t = strdup(value);
+                if (!t)
+                        return log_oom();
 
+                m->exec_context.var_tmp_dir = t;
         } else
                 log_debug_unit(UNIT(m)->id,
                                "Unknown serialization key '%s'", key);
index ba18ddc5b0049dd005a1a7090850cd78826f0316..ceeed2e1aede3ed09f143620eeb2fecf1cb5422b 100644 (file)
 #include "path-util.h"
 #include "namespace.h"
 #include "missing.h"
+#include "execute.h"
 
-typedef enum PathMode {
+typedef enum MountMode {
         /* This is ordered by priority! */
         INACCESSIBLE,
         READONLY,
         PRIVATE_TMP,
         PRIVATE_VAR_TMP,
         READWRITE
-} PathMode;
+} MountMode;
 
-typedef struct Path {
+typedef struct BindMount {
         const char *path;
-        PathMode mode;
+        MountMode mode;
         bool done;
-} Path;
+} BindMount;
 
-static int append_paths(Path **p, char **strv, PathMode mode) {
+static int append_mounts(BindMount **p, char **strv, MountMode mode) {
         char **i;
 
         STRV_FOREACH(i, strv) {
@@ -68,8 +69,8 @@ static int append_paths(Path **p, char **strv, PathMode mode) {
         return 0;
 }
 
-static int path_compare(const void *a, const void *b) {
-        const Path *p = a, *q = b;
+static int mount_path_compare(const void *a, const void *b) {
+        const BindMount *p = a, *q = b;
 
         if (path_equal(p->path, q->path)) {
 
@@ -93,14 +94,13 @@ static int path_compare(const void *a, const void *b) {
         return 0;
 }
 
-static void drop_duplicates(Path *p, unsigned *n, bool *need_inaccessible) {
-        Path *f, *t, *previous;
+static void drop_duplicates(BindMount *m, unsigned *n) {
+        BindMount *f, *t, *previous;
 
-        assert(p);
+        assert(m);
         assert(n);
-        assert(need_inaccessible);
 
-        for (f = p, t = p, previous = NULL; f < p+*n; f++) {
+        for (f = m, t = m, previous = NULL; f < m+*n; f++) {
 
                 /* The first one wins */
                 if (previous && path_equal(f->path, previous->path))
@@ -109,37 +109,33 @@ static void drop_duplicates(Path *p, unsigned *n, bool *need_inaccessible) {
                 t->path = f->path;
                 t->mode = f->mode;
 
-                if (t->mode == INACCESSIBLE)
-                        *need_inaccessible = true;
-
                 previous = t;
 
                 t++;
         }
 
-        *n = t - p;
+        *n = t - m;
 }
 
 static int apply_mount(
-                Path *p,
+                BindMount *m,
                 const char *tmp_dir,
-                const char *var_tmp_dir,
-                const char *inaccessible_dir) {
+                const char *var_tmp_dir) {
 
         const char *what;
         int r;
 
-        assert(p);
+        assert(m);
 
-        switch (p->mode) {
+        switch (m->mode) {
 
         case INACCESSIBLE:
-                what = inaccessible_dir;
+                what = "/run/systemd/inaccessible";
                 break;
 
         case READONLY:
         case READWRITE:
-                what = p->path;
+                what = m->path;
                 break;
 
         case PRIVATE_TMP:
@@ -156,133 +152,99 @@ static int apply_mount(
 
         assert(what);
 
-        r = mount(what, p->path, NULL, MS_BIND|MS_REC, NULL);
+        r = mount(what, m->path, NULL, MS_BIND|MS_REC, NULL);
         if (r >= 0)
-                log_debug("Successfully mounted %s to %s", what, p->path);
+                log_debug("Successfully mounted %s to %s", what, m->path);
 
         return r;
 }
 
-static int make_read_only(Path *p) {
+static int make_read_only(BindMount *m) {
         int r;
 
-        assert(p);
+        assert(m);
 
-        if (p->mode != INACCESSIBLE && p->mode != READONLY)
+        if (m->mode != INACCESSIBLE && m->mode != READONLY)
                 return 0;
 
-        r = mount(NULL, p->path, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY|MS_REC, NULL);
+        r = mount(NULL, m->path, NULL, MS_BIND|MS_REMOUNT|MS_RDONLY|MS_REC, NULL);
         if (r < 0)
                 return -errno;
 
         return 0;
 }
 
-int setup_namespace(
-                char **writable,
-                char **readable,
-                char **inaccessible,
-                bool private_tmp,
-                unsigned long flags) {
-
-        char
-                tmp_dir[] = "/tmp/systemd-private-XXXXXX",
-                var_tmp_dir[] = "/var/tmp/systemd-private-XXXXXX",
-                inaccessible_dir[] = "/tmp/systemd-inaccessible-XXXXXX";
-
-        Path *paths, *p;
-        unsigned n;
-        bool need_inaccessible = false;
-        bool remove_tmp = false, remove_var_tmp = false, remove_inaccessible = false;
-        int r;
-
-        if (!flags)
-                flags = MS_SHARED;
+int setup_tmpdirs(char **tmp_dir,
+                  char **var_tmp_dir) {
+        int r = 0;
+        char tmp_dir_template[] = "/tmp/systemd-private-XXXXXX",
+             var_tmp_dir_template[] = "/var/tmp/systemd-private-XXXXXX";
 
-        n =
-                strv_length(writable) +
-                strv_length(readable) +
-                strv_length(inaccessible) +
-                (private_tmp ? 2 : 0);
+        assert(tmp_dir);
+        assert(var_tmp_dir);
 
-        p = paths = alloca(sizeof(Path) * n);
-        if ((r = append_paths(&p, writable, READWRITE)) < 0 ||
-            (r = append_paths(&p, readable, READONLY)) < 0 ||
-            (r = append_paths(&p, inaccessible, INACCESSIBLE)) < 0)
-                goto fail;
+        r = create_tmp_dir(tmp_dir_template, 0000, true, tmp_dir);
+        if (r < 0)
+                goto fail2;
 
-        if (private_tmp) {
-                p->path = "/tmp";
-                p->mode = PRIVATE_TMP;
-                p++;
+        r = create_tmp_dir(var_tmp_dir_template, 0000, true, var_tmp_dir);
+        if (r < 0)
+                goto fail1;
 
-                p->path = "/var/tmp";
-                p->mode = PRIVATE_VAR_TMP;
-                p++;
-        }
+        return 0;
 
-        assert(paths + n == p);
+fail1:
+        rmdir(*tmp_dir);
+        free(*tmp_dir);
+        *tmp_dir = NULL;
 
-        qsort(paths, n, sizeof(Path), path_compare);
-        drop_duplicates(paths, &n, &need_inaccessible);
+fail2:
+        return r;
+}
 
-        if (need_inaccessible) {
-                mode_t u;
-                char *d;
+int setup_namespace(char** read_write_dirs,
+                    char** read_only_dirs,
+                    char** inaccessible_dirs,
+                    char* tmp_dir,
+                    char* var_tmp_dir,
+                    bool private_tmp,
+                    unsigned mount_flags) {
 
-                u = umask(0777);
-                d = mkdtemp(inaccessible_dir);
-                umask(u);
+        unsigned n = strv_length(read_write_dirs) +
+                     strv_length(read_only_dirs) +
+                     strv_length(inaccessible_dirs) +
+                     (private_tmp ? 2 : 0);
+        BindMount *m, *mounts;
+        int r = 0;
 
-                if (!d) {
-                        r = -errno;
-                        goto fail;
-                }
+        if (!mount_flags)
+                mount_flags = MS_SHARED;
 
-                remove_inaccessible = true;
+        if (unshare(CLONE_NEWNS) < 0) {
+                r = -errno;
+                goto fail;
         }
 
-        if (private_tmp) {
-                mode_t u;
-                char *d;
-
-                u = umask(0000);
-                d = mkdtemp(tmp_dir);
-                umask(u);
-
-                if (!d) {
-                        r = -errno;
-                        goto fail;
-                }
-
-                remove_tmp = true;
-
-                u = umask(0000);
-                d = mkdtemp(var_tmp_dir);
-                umask(u);
-
-                if (!d) {
-                        r = -errno;
-                        goto fail;
-                }
-
-                remove_var_tmp = true;
+        m = mounts = (BindMount *) alloca(n * sizeof(BindMount));
+        if ((r = append_mounts(&m, read_write_dirs, READWRITE)) < 0 ||
+                (r = append_mounts(&m, read_only_dirs, READONLY)) < 0 ||
+                (r = append_mounts(&m, inaccessible_dirs, INACCESSIBLE)) < 0)
+                goto fail;
 
-                if (chmod(tmp_dir, 0777 + S_ISVTX) < 0) {
-                        r = -errno;
-                        goto fail;
-                }
+        if (private_tmp) {
+                m->path = "/tmp";
+                m->mode = PRIVATE_TMP;
+                m++;
 
-                if (chmod(var_tmp_dir, 0777 + S_ISVTX) < 0) {
-                        r = -errno;
-                        goto fail;
-                }
+                m->path = "/var/tmp";
+                m->mode = PRIVATE_VAR_TMP;
+                m++;
         }
 
-        if (unshare(CLONE_NEWNS) < 0) {
-                r = -errno;
-                goto fail;
-        }
+        assert(mounts + n == m);
+
+        qsort(mounts, n, sizeof(BindMount), mount_path_compare);
+        drop_duplicates(mounts, &n);
 
         /* Remount / as SLAVE so that nothing now mounted in the namespace
            shows up in the parent */
@@ -291,20 +253,20 @@ int setup_namespace(
                 goto fail;
         }
 
-        for (p = paths; p < paths + n; p++) {
-                r = apply_mount(p, tmp_dir, var_tmp_dir, inaccessible_dir);
+        for (m = mounts; m < mounts + n; ++m) {
+                r = apply_mount(m, tmp_dir, var_tmp_dir);
                 if (r < 0)
                         goto undo_mounts;
         }
 
-        for (p = paths; p < paths + n; p++) {
-                r = make_read_only(p);
+        for (m = mounts; m < mounts + n; ++m) {
+                r = make_read_only(m);
                 if (r < 0)
                         goto undo_mounts;
         }
 
         /* Remount / as the desired mode */
-        if (mount(NULL, "/", NULL, flags|MS_REC, NULL) < 0) {
+        if (mount(NULL, "/", NULL, mount_flags | MS_REC, NULL) < 0) {
                 r = -errno;
                 goto undo_mounts;
         }
@@ -312,19 +274,11 @@ int setup_namespace(
         return 0;
 
 undo_mounts:
-        for (p = paths; p < paths + n; p++)
-                if (p->done)
-                        umount2(p->path, MNT_DETACH);
+        for (m = mounts; m < mounts + n; ++m) {
+                if (m->done)
+                        umount2(m->path, MNT_DETACH);
+        }
 
 fail:
-        if (remove_inaccessible)
-                rmdir(inaccessible_dir);
-
-        if (remove_tmp)
-                rmdir(tmp_dir);
-
-        if (remove_var_tmp)
-                rmdir(var_tmp_dir);
-
         return r;
 }
index 5d72ed91fbe6e80ee3bf56e5cc656bf87a6de9be..7b886b8abf8f0965a8cd191f7bc29c7d931398c0 100644 (file)
 
 #include <stdbool.h>
 
-int setup_namespace(
-                char **writable,
-                char **readable,
-                char **inaccessible,
-                bool private_tmp,
-                unsigned long flags);
+typedef struct ExecContext ExecContext;
+
+int setup_tmpdirs(char **tmp_dir, char **var_tmp_dir);
+int setup_namespace(char **read_write_dirs,
+                    char **read_only_dirs,
+                    char **inaccessible_dirs,
+                    char *tmp_dir,
+                    char *var_tmp_dir,
+                    bool private_tmp,
+                    unsigned mount_flags);
index fd90ceba05917a7c89660efab7ffd11acdd32e00..080d583b69e763454a1983c982461dac29ffcb0e 100644 (file)
@@ -283,7 +283,7 @@ static void service_done(Unit *u) {
         free(s->status_text);
         s->status_text = NULL;
 
-        exec_context_done(&s->exec_context);
+        exec_context_done(&s->exec_context, manager_is_reloading_or_reexecuting(u->manager));
         exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
         s->control_command = NULL;
         s->main_command = NULL;
@@ -1932,6 +1932,9 @@ static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart)
 
         s->forbid_restart = false;
 
+        /* we want fresh tmpdirs in case service is started again immediately */
+        exec_context_tmp_dirs_done(&s->exec_context);
+
         return;
 
 fail:
@@ -2638,6 +2641,12 @@ static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
                 dual_timestamp_serialize(f, "watchdog-timestamp",
                                          &s->watchdog_timestamp);
 
+        if (s->exec_context.tmp_dir)
+                unit_serialize_item(u, f, "tmp-dir", s->exec_context.tmp_dir);
+
+        if (s->exec_context.var_tmp_dir)
+                unit_serialize_item(u, f, "var-tmp-dir", s->exec_context.var_tmp_dir);
+
         return 0;
 }
 
@@ -2756,7 +2765,23 @@ static int service_deserialize_item(Unit *u, const char *key, const char *value,
                 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
         else if (streq(key, "watchdog-timestamp"))
                 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
-        else
+        else if (streq(key, "tmp-dir")) {
+                char *t;
+
+                t = strdup(value);
+                if (!t)
+                        return log_oom();
+
+                s->exec_context.tmp_dir = t;
+        } else if (streq(key, "var-tmp-dir")) {
+                char *t;
+
+                t = strdup(value);
+                if (!t)
+                        return log_oom();
+
+                s->exec_context.var_tmp_dir = t;
+        } else
                 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
 
         return 0;
index ee9de4e14c38f8f28e781d44c4a57664aa71ca55..a3e3631daca2aa195e4e1d08bbaad94557fc8007 100644 (file)
@@ -127,7 +127,7 @@ static void socket_done(Unit *u) {
 
         socket_free_ports(s);
 
-        exec_context_done(&s->exec_context);
+        exec_context_done(&s->exec_context, manager_is_reloading_or_reexecuting(u->manager));
         exec_command_free_array(s->exec_command, _SOCKET_EXEC_COMMAND_MAX);
         s->control_command = NULL;
 
@@ -1253,6 +1253,7 @@ static void socket_enter_dead(Socket *s, SocketResult f) {
         if (f != SOCKET_SUCCESS)
                 s->result = f;
 
+        exec_context_tmp_dirs_done(&s->exec_context);
         socket_set_state(s, s->result != SOCKET_SUCCESS ? SOCKET_FAILED : SOCKET_DEAD);
 }
 
@@ -1742,6 +1743,8 @@ static int socket_serialize(Unit *u, FILE *f, FDSet *fds) {
                 }
         }
 
+        exec_context_serialize(&s->exec_context, UNIT(s), f);
+
         return 0;
 }
 
@@ -1901,7 +1904,22 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
                                 p->fd = fdset_remove(fds, fd);
                         }
                 }
+        } else if (streq(key, "tmp-dir")) {
+                char *t;
+
+                t = strdup(value);
+                if (!t)
+                        return log_oom();
+
+                s->exec_context.tmp_dir = t;
+        } else if (streq(key, "var-tmp-dir")) {
+                char *t;
+
+                t = strdup(value);
+                if (!t)
+                        return log_oom();
 
+                s->exec_context.var_tmp_dir = t;
         } else
                 log_debug_unit(UNIT(s)->id,
                                "Unknown serialization key '%s'", key);
index a0e55a0c03d7f0a55defec6046fcc97876969ede..dc98f473871114cac7e5b558bc4111e988910c66 100644 (file)
@@ -125,7 +125,7 @@ static void swap_done(Unit *u) {
         free(s->parameters_fragment.what);
         s->parameters_fragment.what = NULL;
 
-        exec_context_done(&s->exec_context);
+        exec_context_done(&s->exec_context, manager_is_reloading_or_reexecuting(u->manager));
         exec_command_done_array(s->exec_command, _SWAP_EXEC_COMMAND_MAX);
         s->control_command = NULL;
 
@@ -632,6 +632,7 @@ static void swap_enter_dead(Swap *s, SwapResult f) {
         if (f != SWAP_SUCCESS)
                 s->result = f;
 
+        exec_context_tmp_dirs_done(&s->exec_context);
         swap_set_state(s, s->result != SWAP_SUCCESS ? SWAP_FAILED : SWAP_DEAD);
 }
 
@@ -831,6 +832,8 @@ static int swap_serialize(Unit *u, FILE *f, FDSet *fds) {
         if (s->control_command_id >= 0)
                 unit_serialize_item(u, f, "control-command", swap_exec_command_to_string(s->control_command_id));
 
+        exec_context_serialize(&s->exec_context, UNIT(s), f);
+
         return 0;
 }
 
@@ -874,7 +877,22 @@ static int swap_deserialize_item(Unit *u, const char *key, const char *value, FD
                         s->control_command_id = id;
                         s->control_command = s->exec_command + id;
                 }
+        } else if (streq(key, "tmp-dir")) {
+                char *t;
+
+                t = strdup(value);
+                if (!t)
+                        return log_oom();
+
+                s->exec_context.tmp_dir = t;
+        } else if (streq(key, "var-tmp-dir")) {
+                char *t;
+
+                t = strdup(value);
+                if (!t)
+                        return log_oom();
 
+                s->exec_context.var_tmp_dir = t;
         } else
                 log_debug_unit(u->id, "Unknown serialization key '%s'", key);
 
index dc2651f3f2004f0bf8dfe3aa4de56ccdc6ff029c..34c5330838908227199eae79cd85b0d2350e0951 100644 (file)
@@ -5682,3 +5682,47 @@ int search_and_fopen_nulstr(const char *path, const char *mode, const char *sear
 
         return search_and_fopen_internal(path, mode, s, _f);
 }
+
+int create_tmp_dir(char template[], mode_t mask, bool need_sticky, char** dir_name) {
+        int r = 0;
+        char *d = NULL;
+        bool remove = false;
+        mode_t _cleanup_umask_ u;
+
+        assert(dir_name);
+
+        u = umask(mask);
+        d = mkdtemp(template);
+        if (!d) {
+                r = -errno;
+                log_debug("Can't create directory");
+                goto fail;
+        }
+
+        remove = true;
+
+        log_debug("Created temporary directory : %s", template);
+
+        d = strdup(template);
+        if (!d) {
+                r = log_oom();
+                goto fail;
+        }
+
+        if (need_sticky) {
+                r = chmod(template, 0777 | S_ISVTX);
+                if (r < 0) {
+                        r = -errno;
+                        goto fail;
+                }
+                log_debug("Setting sticky bit on : %s", template);
+        }
+
+        *dir_name = d;
+
+        return 0;
+fail:
+        if (remove)
+                rmdir(template);
+        return r;
+}
index f0dfe19ad46252e7b0b65148b2c1eb8fc30e3463..8ac4bbc2499f0cc4ccec3233a4b0aa1d805a9ef3 100644 (file)
@@ -574,6 +574,7 @@ int on_ac_power(void);
 
 int search_and_fopen(const char *path, const char *mode, const char **search, FILE **_f);
 int search_and_fopen_nulstr(const char *path, const char *mode, const char *search, FILE **_f);
+int create_tmp_dir(char template[], mode_t mask, bool need_sticky, char** dir_name);
 
 #define FOREACH_LINE(line, f, on_error)                         \
         for (;;)                                                \
index b1c759fc20add3ff7ac0ea59d1aa5b2e7ddd0316..ad0d0419c424f41311a9c5264283a70c1ea0485b 100644 (file)
@@ -26,6 +26,7 @@
 #include <linux/fs.h>
 
 #include "namespace.h"
+#include "execute.h"
 #include "log.h"
 
 int main(int argc, char *argv[]) {
@@ -47,8 +48,19 @@ int main(int argc, char *argv[]) {
         };
 
         int r;
+        char tmp_dir[] = "/tmp/systemd-private-XXXXXX",
+             var_tmp_dir[] = "/var/tmp/systemd-private-XXXXXX";
 
-        r = setup_namespace((char**) writable, (char**) readonly, (char**) inaccessible, true, 0);
+        assert_se(mkdtemp(tmp_dir));
+        assert_se(mkdtemp(var_tmp_dir));
+
+        r = setup_namespace((char **) writable,
+                            (char **) readonly,
+                            (char **) inaccessible,
+                            tmp_dir,
+                            var_tmp_dir,
+                            true,
+                            0);
         if (r < 0) {
                 log_error("Failed to setup namespace: %s", strerror(-r));
                 return 1;