From ad8e66dcc485425da925a9ef99e911c6e9d8ee5b Mon Sep 17 00:00:00 2001 From: Alan Jenkins Date: Wed, 29 Aug 2018 22:36:37 +0100 Subject: [PATCH] namespace: fix mode for TemporaryFileSystem= ... when no mount options are passed. Change the code, to avoid the following failure in the newly added tests: exec-temporaryfilesystem-rw.service: Executing: /usr/bin/sh -x -c '[ "$(stat -c %a /var)" == 755 ]' ++ stat -c %a /var + '[' 1777 == 755 ']' Received SIGCHLD from PID 30364 (sh). Child 30364 (sh) died (code=exited, status=1/FAILURE) (And I spotted an opportunity to use TAKE_PTR() at the end). --- src/core/namespace.c | 26 ++++++++----------- .../exec-temporaryfilesystem-ro.service | 3 +++ .../exec-temporaryfilesystem-rw.service | 3 +++ 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/core/namespace.c b/src/core/namespace.c index c3bbb406800..c7ccaa51929 100644 --- a/src/core/namespace.c +++ b/src/core/namespace.c @@ -304,7 +304,7 @@ static int append_tmpfs_mounts(MountEntry **p, const TemporaryFileSystem *tmpfs, for (i = 0; i < n; i++) { const TemporaryFileSystem *t = tmpfs + i; _cleanup_free_ char *o = NULL, *str = NULL; - unsigned long flags = MS_NODEV|MS_STRICTATIME; + unsigned long flags; bool ro = false; if (!path_is_absolute(t->path)) { @@ -312,29 +312,25 @@ static int append_tmpfs_mounts(MountEntry **p, const TemporaryFileSystem *tmpfs, return -EINVAL; } - if (!isempty(t->options)) { - str = strjoin("mode=0755,", t->options); - if (!str) - return -ENOMEM; + str = strjoin("mode=0755,", t->options); + if (!str) + return -ENOMEM; - r = mount_option_mangle(str, MS_NODEV|MS_STRICTATIME, &flags, &o); - if (r < 0) - return log_debug_errno(r, "Failed to parse mount option '%s': %m", str); + r = mount_option_mangle(str, MS_NODEV|MS_STRICTATIME, &flags, &o); + if (r < 0) + return log_debug_errno(r, "Failed to parse mount option '%s': %m", str); - ro = flags & MS_RDONLY; - if (ro) - flags ^= MS_RDONLY; - } + ro = flags & MS_RDONLY; + if (ro) + flags ^= MS_RDONLY; *((*p)++) = (MountEntry) { .path_const = t->path, .mode = TMPFS, .read_only = ro, - .options_malloc = o, + .options_malloc = TAKE_PTR(o), .flags = flags, }; - - o = NULL; } return 0; diff --git a/test/test-execute/exec-temporaryfilesystem-ro.service b/test/test-execute/exec-temporaryfilesystem-ro.service index c0e3721a01f..c161aecc30f 100644 --- a/test/test-execute/exec-temporaryfilesystem-ro.service +++ b/test/test-execute/exec-temporaryfilesystem-ro.service @@ -10,6 +10,9 @@ ExecStart=/bin/sh -c 'test -d /var/test-exec-temporaryfilesystem/rw && test -d / # Check TemporaryFileSystem= are empty ExecStart=/bin/sh -c 'for i in $$(ls -A /var); do test $$i = test-exec-temporaryfilesystem || false; done' +# Check default mode +ExecStart=sh -x -c 'test "$$(stat -c %%a /var)" = "755"' + # Cannot create a file in /var ExecStart=/bin/sh -c '! touch /var/hoge' diff --git a/test/test-execute/exec-temporaryfilesystem-rw.service b/test/test-execute/exec-temporaryfilesystem-rw.service index 379ad066fb7..bb830595bc1 100644 --- a/test/test-execute/exec-temporaryfilesystem-rw.service +++ b/test/test-execute/exec-temporaryfilesystem-rw.service @@ -10,6 +10,9 @@ ExecStart=test -d /var/test-exec-temporaryfilesystem/rw -a -d /var/test-exec-tem # Check TemporaryFileSystem= are empty ExecStart=sh -c 'for i in $$(ls -A /var); do test $$i = test-exec-temporaryfilesystem || false; done' +# Check default mode +ExecStart=sh -x -c 'test "$$(stat -c %%a /var)" = "755"' + # Create a file in /var ExecStart=touch /var/hoge