From 6c9c51e5e2fcc2bf91d739f6d31ae475e6772af6 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 23 Sep 2018 16:17:03 +0900 Subject: [PATCH] fs-util: make symlink_idempotent() optionally create relative link --- src/basic/fs-util.c | 17 ++++++++++++++++- src/basic/fs-util.h | 2 +- src/core/execute.c | 14 ++------------ src/core/socket.c | 4 ++-- src/nspawn/nspawn-cgroup.c | 4 ++-- 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/basic/fs-util.c b/src/basic/fs-util.c index 09fcc32..1fa76bd 100644 --- a/src/basic/fs-util.c +++ b/src/basic/fs-util.c @@ -346,12 +346,27 @@ int touch(const char *path) { return touch_file(path, false, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID); } -int symlink_idempotent(const char *from, const char *to) { +int symlink_idempotent(const char *from, const char *to, bool make_relative) { + _cleanup_free_ char *relpath = NULL; int r; assert(from); assert(to); + if (make_relative) { + _cleanup_free_ char *parent = NULL; + + parent = dirname_malloc(to); + if (!parent) + return -ENOMEM; + + r = path_make_relative(parent, from, &relpath); + if (r < 0) + return r; + + from = relpath; + } + if (symlink(from, to) < 0) { _cleanup_free_ char *p = NULL; diff --git a/src/basic/fs-util.h b/src/basic/fs-util.h index 4b65625..bc753d5 100644 --- a/src/basic/fs-util.h +++ b/src/basic/fs-util.h @@ -37,7 +37,7 @@ int fd_warn_permissions(const char *path, int fd); int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode); int touch(const char *path); -int symlink_idempotent(const char *from, const char *to); +int symlink_idempotent(const char *from, const char *to, bool make_relative); int symlink_atomic(const char *from, const char *to); int mknod_atomic(const char *path, mode_t mode, dev_t dev); diff --git a/src/core/execute.c b/src/core/execute.c index 35dd389..77e3ce8 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -2027,7 +2027,7 @@ static int setup_exec_directory( if (context->dynamic_user && !IN_SET(type, EXEC_DIRECTORY_RUNTIME, EXEC_DIRECTORY_CONFIGURATION)) { - _cleanup_free_ char *private_root = NULL, *relative = NULL, *parent = NULL; + _cleanup_free_ char *private_root = NULL; /* So, here's one extra complication when dealing with DynamicUser=1 units. In that case we * want to avoid leaving a directory around fully accessible that is owned by a dynamic user @@ -2092,18 +2092,8 @@ static int setup_exec_directory( goto fail; } - parent = dirname_malloc(p); - if (!parent) { - r = -ENOMEM; - goto fail; - } - - r = path_make_relative(parent, pp, &relative); - if (r < 0) - goto fail; - /* And link it up from the original place */ - r = symlink_idempotent(relative, p); + r = symlink_idempotent(pp, p, true); if (r < 0) goto fail; diff --git a/src/core/socket.c b/src/core/socket.c index 8775bc8..aac80f3 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -1313,7 +1313,7 @@ static int socket_symlink(Socket *s) { STRV_FOREACH(i, s->symlinks) { (void) mkdir_parents_label(*i, s->directory_mode); - r = symlink_idempotent(p, *i); + r = symlink_idempotent(p, *i, false); if (r == -EEXIST && s->remove_on_stop) { /* If there's already something where we want to create the symlink, and the destructive @@ -1321,7 +1321,7 @@ static int socket_symlink(Socket *s) { * again. */ if (unlink(*i) >= 0) - r = symlink_idempotent(p, *i); + r = symlink_idempotent(p, *i, false); } if (r < 0) diff --git a/src/nspawn/nspawn-cgroup.c b/src/nspawn/nspawn-cgroup.c index 4a3cd29..f7ec077 100644 --- a/src/nspawn/nspawn-cgroup.c +++ b/src/nspawn/nspawn-cgroup.c @@ -373,7 +373,7 @@ static int mount_legacy_cgns_supported( if (!target) return log_oom(); - r = symlink_idempotent(controller, target); + r = symlink_idempotent(controller, target, false); if (r == -EINVAL) return log_error_errno(r, "Invalid existing symlink for combined hierarchy: %m"); if (r < 0) @@ -482,7 +482,7 @@ static int mount_legacy_cgns_unsupported( if (r < 0) return r; - r = symlink_idempotent(combined, target); + r = symlink_idempotent(combined, target, false); if (r == -EINVAL) return log_error_errno(r, "Invalid existing symlink for combined hierarchy: %m"); if (r < 0) -- 2.7.4