From 4bee2333cfe43152773c554d2c0b952dfe24789c Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 22 Jun 2021 14:41:45 +0900 Subject: [PATCH 1/3] mount-util: add one more assertion --- src/shared/mount-util.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/shared/mount-util.c b/src/shared/mount-util.c index c4261b24944..fa0d5cd8806 100644 --- a/src/shared/mount-util.c +++ b/src/shared/mount-util.c @@ -564,6 +564,8 @@ static int mount_flags_to_string(long unsigned flags, char **ret) { }; _cleanup_free_ char *str = NULL; + assert(ret); + for (size_t i = 0; i < ELEMENTSOF(map); i++) if (flags & map[i].flag) { if (!strextend_with_separator(&str, "|", map[i].name)) From 917578880fa1e4392c95b6a22e713ef31012125b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Mon, 21 Jun 2021 19:20:41 +0200 Subject: [PATCH 2/3] test-mount-util: add usual print headers --- src/test/test-mount-util.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/test/test-mount-util.c b/src/test/test-mount-util.c index ac64e750598..e25b4464fd7 100644 --- a/src/test/test-mount-util.c +++ b/src/test/test-mount-util.c @@ -21,6 +21,8 @@ static void test_mount_option_mangle(void) { char *opts = NULL; unsigned long f; + log_info("/* %s */", __func__); + assert_se(mount_option_mangle(NULL, MS_RDONLY|MS_NOSUID, &f, &opts) == 0); assert_se(f == (MS_RDONLY|MS_NOSUID)); assert_se(opts == NULL); @@ -81,6 +83,8 @@ static void test_bind_remount_recursive(void) { _cleanup_free_ char *subdir = NULL; const char *p; + log_info("/* %s */", __func__); + if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0) { (void) log_tests_skipped("not running privileged"); return; @@ -134,6 +138,8 @@ static void test_bind_remount_recursive(void) { static void test_bind_remount_one(void) { pid_t pid; + log_info("/* %s */", __func__); + if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0) { (void) log_tests_skipped("not running privileged"); return; From 51bb6a103e13b156766ba76a62acd397537ec9fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Mon, 21 Jun 2021 19:21:10 +0200 Subject: [PATCH 3/3] test-mount-util: add output test for mount_flags_to_string() --- src/shared/mount-util.c | 2 +- src/shared/mount-util.h | 1 + src/test/test-mount-util.c | 50 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/shared/mount-util.c b/src/shared/mount-util.c index fa0d5cd8806..ef128dd5955 100644 --- a/src/shared/mount-util.c +++ b/src/shared/mount-util.c @@ -531,7 +531,7 @@ int mode_to_inaccessible_node( return 0; } -static int mount_flags_to_string(long unsigned flags, char **ret) { +int mount_flags_to_string(long unsigned flags, char **ret) { static const struct { long unsigned flag; const char *name; diff --git a/src/shared/mount-util.h b/src/shared/mount-util.h index 837afc2e872..0169083980d 100644 --- a/src/shared/mount-util.h +++ b/src/shared/mount-util.h @@ -89,6 +89,7 @@ int mount_option_mangle( char **ret_remaining_options); int mode_to_inaccessible_node(const char *runtime_dir, mode_t mode, char **dest); +int mount_flags_to_string(long unsigned flags, char **ret); /* Useful for usage with _cleanup_(), unmounts, removes a directory and frees the pointer */ static inline char* umount_and_rmdir_and_free(char *p) { diff --git a/src/test/test-mount-util.c b/src/test/test-mount-util.c index e25b4464fd7..0cbf68aa00b 100644 --- a/src/test/test-mount-util.c +++ b/src/test/test-mount-util.c @@ -7,6 +7,7 @@ #include "capability-util.h" #include "fd-util.h" #include "fileio.h" +#include "missing_mount.h" #include "mount-util.h" #include "namespace-util.h" #include "path-util.h" @@ -78,6 +79,54 @@ static void test_mount_option_mangle(void) { opts = mfree(opts); } +static void test_mount_flags_to_string_one(unsigned long flags, const char *expected) { + _cleanup_free_ char *x = NULL; + int r; + + r = mount_flags_to_string(flags, &x); + log_info("flags: %#lX → %d/\"%s\"", flags, r, strnull(x)); + assert_se(r >= 0); + assert_se(streq(x, expected)); +} + +static void test_mount_flags_to_string(void) { + log_info("/* %s */", __func__); + + test_mount_flags_to_string_one(0, "0"); + test_mount_flags_to_string_one(MS_RDONLY, "MS_RDONLY"); + test_mount_flags_to_string_one(MS_NOSUID, "MS_NOSUID"); + test_mount_flags_to_string_one(MS_NODEV, "MS_NODEV"); + test_mount_flags_to_string_one(MS_NOEXEC, "MS_NOEXEC"); + test_mount_flags_to_string_one(MS_SYNCHRONOUS, "MS_SYNCHRONOUS"); + test_mount_flags_to_string_one(MS_REMOUNT, "MS_REMOUNT"); + test_mount_flags_to_string_one(MS_MANDLOCK, "MS_MANDLOCK"); + test_mount_flags_to_string_one(MS_DIRSYNC, "MS_DIRSYNC"); + test_mount_flags_to_string_one(MS_NOSYMFOLLOW, "MS_NOSYMFOLLOW"); + test_mount_flags_to_string_one(MS_NOATIME, "MS_NOATIME"); + test_mount_flags_to_string_one(MS_NODIRATIME, "MS_NODIRATIME"); + test_mount_flags_to_string_one(MS_BIND, "MS_BIND"); + test_mount_flags_to_string_one(MS_MOVE, "MS_MOVE"); + test_mount_flags_to_string_one(MS_REC, "MS_REC"); + test_mount_flags_to_string_one(MS_SILENT, "MS_SILENT"); + test_mount_flags_to_string_one(MS_POSIXACL, "MS_POSIXACL"); + test_mount_flags_to_string_one(MS_UNBINDABLE, "MS_UNBINDABLE"); + test_mount_flags_to_string_one(MS_PRIVATE, "MS_PRIVATE"); + test_mount_flags_to_string_one(MS_SLAVE, "MS_SLAVE"); + test_mount_flags_to_string_one(MS_SHARED, "MS_SHARED"); + test_mount_flags_to_string_one(MS_RELATIME, "MS_RELATIME"); + test_mount_flags_to_string_one(MS_KERNMOUNT, "MS_KERNMOUNT"); + test_mount_flags_to_string_one(MS_I_VERSION, "MS_I_VERSION"); + test_mount_flags_to_string_one(MS_STRICTATIME, "MS_STRICTATIME"); + test_mount_flags_to_string_one(MS_LAZYTIME, "MS_LAZYTIME"); + test_mount_flags_to_string_one(MS_LAZYTIME|MS_STRICTATIME, "MS_STRICTATIME|MS_LAZYTIME"); + test_mount_flags_to_string_one(UINT_MAX, + "MS_RDONLY|MS_NOSUID|MS_NODEV|MS_NOEXEC|MS_SYNCHRONOUS|MS_REMOUNT|" + "MS_MANDLOCK|MS_DIRSYNC|MS_NOSYMFOLLOW|MS_NOATIME|MS_NODIRATIME|" + "MS_BIND|MS_MOVE|MS_REC|MS_SILENT|MS_POSIXACL|MS_UNBINDABLE|" + "MS_PRIVATE|MS_SLAVE|MS_SHARED|MS_RELATIME|MS_KERNMOUNT|" + "MS_I_VERSION|MS_STRICTATIME|MS_LAZYTIME|fc000200"); +} + static void test_bind_remount_recursive(void) { _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL; _cleanup_free_ char *subdir = NULL; @@ -172,6 +221,7 @@ int main(int argc, char *argv[]) { test_setup_logging(LOG_DEBUG); test_mount_option_mangle(); + test_mount_flags_to_string(); test_bind_remount_recursive(); test_bind_remount_one();