From 5978f91ad3d8159b6e3ff4f349c755a4dcfcf357 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 23 Sep 2018 16:17:03 +0900 Subject: [PATCH] (backport) fs-util: make symlink_idempotent() optionally create relative link Change-Id: I24d38ca9a7e0f63095cd8fdf33b9686206ebf6a5 --- 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 2d28428..1406ff6 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 2856677..1421173 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 4ae838e..953c0f0 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -2031,7 +2031,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 @@ -2096,18 +2096,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 254ee2e..b69353e 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -1329,7 +1329,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 @@ -1337,7 +1337,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 0be9119..49a9dbb 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