From af918c481864116fdf92eceb3376a014053eaf5f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 20 Oct 2020 14:51:01 +0200 Subject: [PATCH] test-mountpoint-util: run test in private mount namespace This creates a private mount namespace for test-mountpint-util, with all propagation from the host turned off. This gives us the guarantee that /proc/self/mountinfo remains fixed and constant while we operate, removing potential races against other unrelated stuff running on the system that changes the mount table. Prompted-by: #17050 (I doubt this actually fixes 17050, this is mostly to make sure that we aren't possibly affected by such races in our test) --- src/test/test-mountpoint-util.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/test/test-mountpoint-util.c b/src/test/test-mountpoint-util.c index 30b00ae4d8b..ada5b1712c6 100644 --- a/src/test/test-mountpoint-util.c +++ b/src/test/test-mountpoint-util.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ +#include #include #include @@ -258,6 +259,16 @@ static void test_path_is_mount_point(void) { int main(int argc, char *argv[]) { test_setup_logging(LOG_DEBUG); + /* let's move into our own mount namespace with all propagation from the host turned off, so that + * /proc/self/mountinfo is static and constant for the whole time our test runs. */ + if (unshare(CLONE_NEWNS) < 0) { + if (!ERRNO_IS_PRIVILEGE(errno)) + return log_error_errno(errno, "Failed to detach mount namespace: %m"); + + log_notice("Lacking privilege to create separate mount namespace, proceeding in originating mount namespace."); + } else + assert_se(mount(NULL, "/", NULL, MS_PRIVATE | MS_REC, NULL) >= 0); + test_mount_propagation_flags("shared", 0, MS_SHARED); test_mount_propagation_flags("slave", 0, MS_SLAVE); test_mount_propagation_flags("private", 0, MS_PRIVATE); -- 2.47.3