From 9ad7d6593ef970467a402ef66354e9e39299d1c7 Mon Sep 17 00:00:00 2001 From: "Christian Brauner (Microsoft)" Date: Sun, 13 Mar 2022 10:06:02 +0100 Subject: [PATCH] sysfs_fuse: generate file info for cpu entries as well Signed-off-by: Christian Brauner (Microsoft) --- src/lxcfs_fuse_compat.h | 44 ++++++++++++++++++++--------------------- src/sysfs_fuse.c | 16 +++++++-------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/lxcfs_fuse_compat.h b/src/lxcfs_fuse_compat.h index d12ecef..347f073 100644 --- a/src/lxcfs_fuse_compat.h +++ b/src/lxcfs_fuse_compat.h @@ -11,29 +11,16 @@ #include "memory_utils.h" -#if HAVE_FUSE3 -static inline int dir_filler(fuse_fill_dir_t filler, void *buf, - const char *name, off_t off) -{ - return filler(buf, name, NULL, off, FUSE_FILL_DIR_PLUS); -} - -static inline int dirent_filler(fuse_fill_dir_t filler, const char *path, - const char *name, void *buf, off_t off) -{ - return filler(buf, name, NULL, off, FUSE_FILL_DIR_PLUS); -} - -static inline int dirent_fillerat(fuse_fill_dir_t filler, DIR *dp, - struct dirent *dentry, void *buf, off_t off) -{ - return filler(buf, dentry->d_name, NULL, off, FUSE_FILL_DIR_PLUS); -} +#ifdef HAVE_FUSE3 +#define DIR_FILLER(F,B,N,S,O) F(B,N,S,O,FUSE_FILL_DIR_PLUS) #else +#define DIR_FILLER(F,B,N,S,O) F(B,N,S,O) +#endif + static inline int dir_filler(fuse_fill_dir_t filler, void *buf, const char *name, off_t off) { - return filler(buf, name, NULL, off); + return DIR_FILLER(filler, buf, name, NULL, off); } static inline int dirent_filler(fuse_fill_dir_t filler, const char *path, @@ -44,9 +31,9 @@ static inline int dirent_filler(fuse_fill_dir_t filler, const char *path, dirp = opendir(path); if (dirp && !fstatat(dirfd(dirp), name, &st, AT_SYMLINK_NOFOLLOW)) - return filler(buf, name, &st, off); + return DIR_FILLER(filler, buf, name, &st, off); - return filler(buf, name, NULL, off); + return DIR_FILLER(filler, buf, name, NULL, off); } static inline int dirent_fillerat(fuse_fill_dir_t filler, DIR *dp, @@ -63,8 +50,19 @@ static inline int dirent_fillerat(fuse_fill_dir_t filler, DIR *dp, }; } - return filler(buf, dentry->d_name, &st, off); + return DIR_FILLER(filler, buf, dentry->d_name, &st, off); +} +static inline int dir_fillerat(fuse_fill_dir_t filler, DIR *dp, + const char *name, void *buf, off_t off) +{ + struct stat st; + int ret; + + ret = fstatat(dirfd(dp), name, &st, AT_SYMLINK_NOFOLLOW); + if (ret) + return DIR_FILLER(filler, buf, name, NULL, off); + + return DIR_FILLER(filler, buf, name, &st, off); } -#endif #endif /* __LXCFS_FUSE_COMPAT_H */ diff --git a/src/sysfs_fuse.c b/src/sysfs_fuse.c index 8220c2b..a1e2e8e 100644 --- a/src/sysfs_fuse.c +++ b/src/sysfs_fuse.c @@ -269,7 +269,7 @@ static int filler_sys_devices_system_cpu(const char *path, void *buf, { __do_free __u32 *bitarr = NULL; __do_free char *cg = NULL, *cpuset = NULL; - __do_closedir DIR *dir = NULL; + __do_closedir DIR *dirp = NULL; struct fuse_context *fc = fuse_get_context(); __u32 last_set_bit = 0; int ret; @@ -293,6 +293,10 @@ static int filler_sys_devices_system_cpu(const char *path, void *buf, if (ret) return ret; + dirp = opendir(path); + if (!dirp) + return -ENOENT; + for (__u32 bit = 0; bit <= last_set_bit; bit++) { char cpu[100]; @@ -303,15 +307,11 @@ static int filler_sys_devices_system_cpu(const char *path, void *buf, if (ret < 0 || (size_t)ret >= sizeof(cpu)) continue; - if (dir_filler(filler, buf, cpu, 0) != 0) + if (dir_fillerat(filler, dirp, cpu, buf, 0) != 0) return -ENOENT; } - dir = opendir(path); - if (!dir) - return -ENOENT; - - while ((dirent = readdir(dir))) { + while ((dirent = readdir(dirp))) { char *entry = dirent->d_name; if (strlen(entry) > 3) { @@ -322,7 +322,7 @@ static int filler_sys_devices_system_cpu(const char *path, void *buf, continue; } - if (dirent_fillerat(filler, dir, dirent, buf, 0) != 0) + if (dirent_fillerat(filler, dirp, dirent, buf, 0) != 0) return -ENOENT; } -- 2.39.5