From: Karel Zak Date: Thu, 24 Apr 2025 16:16:07 +0000 (+0200) Subject: libmount: clean up statmount syscall-related functions X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b3bb829106e18c01c1fd984b3f68f22eba2c0c19;p=thirdparty%2Futil-linux.git libmount: clean up statmount syscall-related functions * Rename the raw version of the syscall from ul_statmount() to ul_statmount_syscall(). * Rename `sys_statmount()` to `ul_statmount()`. The goal is to use the same naming convention as `ul_listmount()` for a function that is expected to be called from code. * Move ul_statmount() to be in the same #ifdef block as the rest of the statmount code in the header file. * Add has_statmount() to make it easy to verify the usability of the statmount syscall. Signed-off-by: Karel Zak --- diff --git a/include/mount-api-utils.h b/include/mount-api-utils.h index 02bb326c6..ea9aeb816 100644 --- a/include/mount-api-utils.h +++ b/include/mount-api-utils.h @@ -348,8 +348,9 @@ struct ul_statmount { # define LISTMOUNT_REVERSE BIT(0) /* List later mounts first */ #endif +/* Don't use this "raw" version. See ul_statmount() below. */ #if defined(SYS_statmount) -static inline int ul_statmount(uint64_t mnt_id, +static inline int ul_statmount_syscall(uint64_t mnt_id, uint64_t ns_id, uint64_t mask, struct ul_statmount *buf, @@ -364,31 +365,21 @@ static inline int ul_statmount(uint64_t mnt_id, return syscall(SYS_statmount, &req, buf, bufsize, flags); } -#endif -#if defined(SYS_listmount) -static inline ssize_t ul_listmount(uint64_t mnt_id, - uint64_t ns_id, - uint64_t last_mnt_id, - uint64_t list[], size_t num, - unsigned int flags) +static inline int has_statmount(void) { - struct ul_mnt_id_req req = { - .size = MNT_ID_REQ_SIZE_VER1, - .mnt_id = mnt_id, - .param = last_mnt_id, - .mnt_ns_id = ns_id - }; + errno = 0; - return syscall(SYS_listmount, &req, list, num, flags); + if (ul_statmount_syscall(0, 0, 0, NULL, 0, 0) < 0 && errno == ENOSYS) + return 0; + return 1; } -#endif /* This is a version of statmount() that reallocates @buf to be large enough to * store data for the requested @id. This function never deallocates; it is the * caller's responsibility. */ -static inline int sys_statmount(uint64_t id, +static inline int ul_statmount(uint64_t id, uint64_t ns_id, uint64_t mask, struct ul_statmount **buf, @@ -415,7 +406,7 @@ static inline int sys_statmount(uint64_t id, } errno = 0; - rc = ul_statmount(id, ns_id, mask, *buf, *bufsiz, flags); + rc = ul_statmount_syscall(id, ns_id, mask, *buf, *bufsiz, flags); if (!rc) break; if (errno != EOVERFLOW) @@ -427,6 +418,26 @@ static inline int sys_statmount(uint64_t id, return rc; } +#endif /* SYS_statmount */ + + +#if defined(SYS_listmount) +static inline ssize_t ul_listmount(uint64_t mnt_id, + uint64_t ns_id, + uint64_t last_mnt_id, + uint64_t list[], size_t num, + unsigned int flags) +{ + struct ul_mnt_id_req req = { + .size = MNT_ID_REQ_SIZE_VER1, + .mnt_id = mnt_id, + .param = last_mnt_id, + .mnt_ns_id = ns_id + }; + + return syscall(SYS_listmount, &req, list, num, flags); +} +#endif #endif /* HAVE_STATMOUNT_API */ diff --git a/libmount/src/fs_statmount.c b/libmount/src/fs_statmount.c index a998ee23c..3993c7b69 100644 --- a/libmount/src/fs_statmount.c +++ b/libmount/src/fs_statmount.c @@ -33,7 +33,7 @@ struct libmnt_statmnt *mnt_new_statmnt(void) struct libmnt_statmnt *sm; errno = 0; - if (ul_statmount(0, 0, 0, NULL, 0, 0) < 0 && errno == ENOSYS) { + if (!has_statmount()) { DBG(FS, ul_debug("statmount: unsuppported")); return NULL; } @@ -361,13 +361,14 @@ int mnt_fs_fetch_statmount(struct libmnt_fs *fs, uint64_t mask) if (fs->stmnt) { DBG(FS, ul_debugobj(fs, " reuse libmnt_stmnt")); memset(fs->stmnt->buf, 0, fs->stmnt->bufsiz); - rc = sys_statmount(fs->uniq_id, 0, mask, + + rc = ul_statmount(fs->uniq_id, 0, mask, &fs->stmnt->buf, &fs->stmnt->bufsiz, 0); buf = fs->stmnt->buf; bufsiz = fs->stmnt->bufsiz; } else { DBG(FS, ul_debugobj(fs, " use private buffer")); - rc = sys_statmount(fs->uniq_id, 0, mask, &buf, &bufsiz, 0); + rc = ul_statmount(fs->uniq_id, 0, mask, &buf, &bufsiz, 0); } DBG(FS, ul_debugobj(fs, " statmount [rc=%d bufsiz=%zu ns=%" PRIu64 " mask: %s%s%s%s%s%s%s]", rc, bufsiz, ns, @@ -378,11 +379,9 @@ int mnt_fs_fetch_statmount(struct libmnt_fs *fs, uint64_t mask) mask & STATMOUNT_FS_TYPE ? "fs-type " : "", mask & STATMOUNT_MNT_OPTS ? "mnt-opts " : "", mask & STATMOUNT_SB_SOURCE ? "sb-source " : "")); - if (!rc) rc = apply_statmount(fs, buf); done: - if (fs->stmnt) mnt_statmnt_disable_fetching(fs->stmnt, status); else diff --git a/tests/helpers/test_sysinfo.c b/tests/helpers/test_sysinfo.c index 95f50f3f6..00bf12727 100644 --- a/tests/helpers/test_sysinfo.c +++ b/tests/helpers/test_sysinfo.c @@ -145,13 +145,13 @@ static int hlp_fsopen_ok(void) static int hlp_statmount_ok(void) { + printf("%d\n", #ifdef HAVE_STATMOUNT_API - errno = 0; - ul_statmount(0, 0, 0, NULL, 0, 0); + has_statmount() #else - errno = ENOSYS; + 0 #endif - printf("%d\n", errno != ENOSYS); + ); return 0; }