diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/execute.c | 43 | ||||
-rw-r--r-- | src/core/execute.h | 10 | ||||
-rw-r--r-- | src/core/manager.c | 6 | ||||
-rw-r--r-- | src/core/manager.h | 3 | ||||
-rw-r--r-- | src/core/mount-setup.c | 1 | ||||
-rw-r--r-- | src/core/mount.c | 21 | ||||
-rw-r--r-- | src/core/namespace.c | 226 | ||||
-rw-r--r-- | src/core/namespace.h | 16 | ||||
-rw-r--r-- | src/core/service.c | 29 | ||||
-rw-r--r-- | src/core/socket.c | 20 | ||||
-rw-r--r-- | src/core/swap.c | 20 | ||||
-rw-r--r-- | src/shared/util.c | 44 | ||||
-rw-r--r-- | src/shared/util.h | 1 | ||||
-rw-r--r-- | src/test/test-ns.c | 14 |
14 files changed, 302 insertions, 152 deletions
diff --git a/src/core/execute.c b/src/core/execute.c index 92cf174641..18e25fa6e6 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -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) { diff --git a/src/core/execute.h b/src/core/execute.h index 001eb0e7cc..3ce9221ab1 100644 --- a/src/core/execute.h +++ b/src/core/execute.h @@ -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); diff --git a/src/core/manager.c b/src/core/manager.c index 8e66732cd7..a01710f445 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -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; diff --git a/src/core/manager.h b/src/core/manager.h index c486a16887..9d8d9439d2 100644 --- a/src/core/manager.h +++ b/src/core/manager.h @@ -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); diff --git a/src/core/mount-setup.c b/src/core/mount-setup.c index ce10be944a..6140f56d83 100644 --- a/src/core/mount-setup.c +++ b/src/core/mount-setup.c @@ -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; } diff --git a/src/core/mount.c b/src/core/mount.c index 7a1b411c7b..0adf04e9bf 100644 --- a/src/core/mount.c +++ b/src/core/mount.c @@ -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); diff --git a/src/core/namespace.c b/src/core/namespace.c index ba18ddc5b0..ceeed2e1ae 100644 --- a/src/core/namespace.c +++ b/src/core/namespace.c @@ -36,23 +36,24 @@ #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; } diff --git a/src/core/namespace.h b/src/core/namespace.h index 5d72ed91fb..7b886b8abf 100644 --- a/src/core/namespace.h +++ b/src/core/namespace.h @@ -23,9 +23,13 @@ #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); diff --git a/src/core/service.c b/src/core/service.c index fd90ceba05..080d583b69 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -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; diff --git a/src/core/socket.c b/src/core/socket.c index ee9de4e14c..a3e3631dac 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -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); diff --git a/src/core/swap.c b/src/core/swap.c index a0e55a0c03..dc98f47387 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -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); diff --git a/src/shared/util.c b/src/shared/util.c index dc2651f3f2..34c5330838 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -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; +} diff --git a/src/shared/util.h b/src/shared/util.h index f0dfe19ad4..8ac4bbc249 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -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 (;;) \ diff --git a/src/test/test-ns.c b/src/test/test-ns.c index b1c759fc20..ad0d0419c4 100644 --- a/src/test/test-ns.c +++ b/src/test/test-ns.c @@ -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; |