From 8a383bf2c0dab1e9f45bd9ff4e21438d1d1c1a95 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 3 Apr 2019 16:52:19 +0200 Subject: [PATCH 1/3] shared: no need to initialize variable --- src/shared/base-filesystem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/base-filesystem.c b/src/shared/base-filesystem.c index 52460cacad..a55b2a017f 100644 --- a/src/shared/base-filesystem.c +++ b/src/shared/base-filesystem.c @@ -45,8 +45,8 @@ static const BaseFilesystem table[] = { int base_filesystem_create(const char *root, uid_t uid, gid_t gid) { _cleanup_close_ int fd = -1; - int r = 0; size_t i; + int r; fd = open(root, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW); if (fd < 0) From 65290fbf3f88ced422d219679fdcd4dc7ec9699b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 3 Apr 2019 16:53:14 +0200 Subject: [PATCH 2/3] shared: path_join() is your friend --- src/shared/base-filesystem.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/shared/base-filesystem.c b/src/shared/base-filesystem.c index a55b2a017f..33c13af6c4 100644 --- a/src/shared/base-filesystem.c +++ b/src/shared/base-filesystem.c @@ -14,6 +14,7 @@ #include "log.h" #include "macro.h" #include "nulstr-util.h" +#include "path-util.h" #include "string-util.h" #include "umask-util.h" #include "user-util.h" @@ -68,7 +69,7 @@ int base_filesystem_create(const char *root, uid_t uid, gid_t gid) { if (table[i].exists) { _cleanup_free_ char *p = NULL; - p = strjoin(s, "/", table[i].exists); + p = path_join(s, table[i].exists); if (!p) return log_oom(); From 8258578fb12396d66334d9147a51ef7a0fc692be Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 3 Apr 2019 16:55:01 +0200 Subject: [PATCH 3/3] shared: be friendly to EROFS images There are environments where /lib might not be necessary (think: statically compiled portable service binary), hence don't insist on it if the image is read-only. --- src/shared/base-filesystem.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/shared/base-filesystem.c b/src/shared/base-filesystem.c index 33c13af6c4..657407da2d 100644 --- a/src/shared/base-filesystem.c +++ b/src/shared/base-filesystem.c @@ -84,9 +84,15 @@ int base_filesystem_create(const char *root, uid_t uid, gid_t gid) { if (!target) continue; - r = symlinkat(target, fd, table[i].dir); - if (r < 0 && errno != EEXIST) - return log_error_errno(errno, "Failed to create symlink at %s/%s: %m", root, table[i].dir); + if (symlinkat(target, fd, table[i].dir) < 0) { + log_full_errno(IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno, + "Failed to create symlink at %s/%s: %m", root, table[i].dir); + + if (IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure) + continue; + + return -errno; + } if (uid_is_valid(uid) || gid_is_valid(gid)) { if (fchownat(fd, table[i].dir, uid, gid, AT_SYMLINK_NOFOLLOW) < 0) @@ -98,14 +104,14 @@ int base_filesystem_create(const char *root, uid_t uid, gid_t gid) { RUN_WITH_UMASK(0000) r = mkdirat(fd, table[i].dir, table[i].mode); - if (r < 0 && errno != EEXIST) { - log_full_errno(table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno, + if (r < 0) { + log_full_errno(IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure ? LOG_DEBUG : LOG_ERR, errno, "Failed to create directory at %s/%s: %m", root, table[i].dir); - if (!table[i].ignore_failure) - return -errno; + if (IN_SET(errno, EEXIST, EROFS) || table[i].ignore_failure) + continue; - continue; + return -errno; } if (uid != UID_INVALID || gid != UID_INVALID) {