diff options
author | Lennart Poettering <lennart@poettering.net> | 2023-04-20 18:42:36 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2023-04-25 14:00:38 +0200 |
commit | c8ab89e569e156f968b1797aa0abce41f924afb6 (patch) | |
tree | a32db60c52f25d9f1fd982432cbce0f2b4717728 | |
parent | 9203abf79f1d05fdef9b039e7addf9fc5a27752d (diff) | |
download | systemd-c8ab89e569e156f968b1797aa0abce41f924afb6.tar.gz |
mountpoint-util: make path_get_mnt_id_at() work with a NULL path
-rw-r--r-- | src/basic/mountpoint-util.c | 5 | ||||
-rw-r--r-- | src/test/test-mountpoint-util.c | 47 |
2 files changed, 49 insertions, 3 deletions
diff --git a/src/basic/mountpoint-util.c b/src/basic/mountpoint-util.c index 7237930a76..3584f31787 100644 --- a/src/basic/mountpoint-util.c +++ b/src/basic/mountpoint-util.c @@ -63,7 +63,7 @@ int name_to_handle_at_loop( h->handle_bytes = n; - if (name_to_handle_at(fd, path, h, &mnt_id, flags) >= 0) { + if (name_to_handle_at(fd, strempty(path), h, &mnt_id, flags) >= 0) { if (ret_handle) *ret_handle = TAKE_PTR(h); @@ -362,11 +362,10 @@ int path_get_mnt_id_at(int dir_fd, const char *path, int *ret) { int r; assert(dir_fd >= 0 || dir_fd == AT_FDCWD); - assert(path); assert(ret); if (statx(dir_fd, - path, + strempty(path), (isempty(path) ? AT_EMPTY_PATH : AT_SYMLINK_NOFOLLOW) | AT_NO_AUTOMOUNT | /* don't trigger automounts, mnt_id is a local concept */ AT_STATX_DONT_SYNC, /* don't go to the network, mnt_id is a local concept */ diff --git a/src/test/test-mountpoint-util.c b/src/test/test-mountpoint-util.c index f5481b5d6b..669b078100 100644 --- a/src/test/test-mountpoint-util.c +++ b/src/test/test-mountpoint-util.c @@ -366,6 +366,53 @@ TEST(fstype_can_umask) { assert_se(!fstype_can_umask("tmpfs")); } +TEST(path_get_mnt_id_at_null) { + _cleanup_close_ int root_fd = -EBADF, run_fd = -EBADF; + int id1, id2; + + assert_se(path_get_mnt_id_at(AT_FDCWD, "/run/", &id1) >= 0); + assert_se(id1 > 0); + + assert_se(path_get_mnt_id_at(AT_FDCWD, "/run", &id2) >= 0); + assert_se(id1 == id2); + id2 = -1; + + root_fd = open("/", O_DIRECTORY|O_CLOEXEC); + assert_se(root_fd >= 0); + + assert_se(path_get_mnt_id_at(root_fd, "/run/", &id2) >= 0); + assert_se(id1 = id2); + id2 = -1; + + assert_se(path_get_mnt_id_at(root_fd, "/run", &id2) >= 0); + assert_se(id1 = id2); + id2 = -1; + + assert_se(path_get_mnt_id_at(root_fd, "run", &id2) >= 0); + assert_se(id1 = id2); + id2 = -1; + + assert_se(path_get_mnt_id_at(root_fd, "run/", &id2) >= 0); + assert_se(id1 = id2); + id2 = -1; + + run_fd = openat(root_fd, "run", O_DIRECTORY|O_CLOEXEC); + assert_se(run_fd >= 0); + + id2 = -1; + assert_se(path_get_mnt_id_at(run_fd, "", &id2) >= 0); + assert_se(id1 = id2); + id2 = -1; + + assert_se(path_get_mnt_id_at(run_fd, NULL, &id2) >= 0); + assert_se(id1 = id2); + id2 = -1; + + assert_se(path_get_mnt_id_at(run_fd, ".", &id2) >= 0); + assert_se(id1 = id2); + id2 = -1; +} + static int intro(void) { /* 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. */ |