From 82481b6a39062fa4cf74595c717abb7a47f655ef Mon Sep 17 00:00:00 2001 From: Alexander Mikhalitsyn Date: Fri, 12 Apr 2024 15:18:12 +0200 Subject: [PATCH 1/2] lxcfs: use macro to generate liblxcfs call helpers Let's reduce code duplication by using macro for this. Signed-off-by: Alexander Mikhalitsyn --- src/lxcfs.c | 538 +++++++++------------------------------------------- 1 file changed, 90 insertions(+), 448 deletions(-) diff --git a/src/lxcfs.c b/src/lxcfs.c index 7d5076d3..35264f9b 100644 --- a/src/lxcfs.c +++ b/src/lxcfs.c @@ -243,455 +243,97 @@ static void sigusr1_reload(int signo, siginfo_t *info, void *extra) } /* Functions to run the library methods */ -static int do_cg_getattr(const char *path, struct stat *sb) -{ - char *error; - int (*__cg_getattr)(const char *path, struct stat *sb); - - dlerror(); - __cg_getattr = (int (*)(const char *, struct stat *))dlsym(dlopen_handle, "cg_getattr"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find cg_getattr()", error); - - return __cg_getattr(path, sb); -} - -static int do_proc_getattr(const char *path, struct stat *sb) -{ - char *error; - int (*__proc_getattr)(const char *path, struct stat *sb); - - dlerror(); - __proc_getattr = (int (*)(const char *, struct stat *)) dlsym(dlopen_handle, "proc_getattr"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find proc_getattr()", error); - - return __proc_getattr(path, sb); -} - -static int do_sys_getattr(const char *path, struct stat *sb) -{ - char *error; - int (*__sys_getattr)(const char *path, struct stat *sb); - - dlerror(); - __sys_getattr = (int (*)(const char *, struct stat *)) dlsym(dlopen_handle, "sys_getattr"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find sys_getattr()", error); - - return __sys_getattr(path, sb); -} - -static int do_cg_read(const char *path, char *buf, size_t size, off_t offset, - struct fuse_file_info *fi) -{ - char *error; - int (*__cg_read)(const char *path, char *buf, size_t size, off_t offset, - struct fuse_file_info *fi); - - dlerror(); - __cg_read = (int (*)(const char *, char *, size_t, off_t, struct fuse_file_info *))dlsym(dlopen_handle, "cg_read"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find cg_read()", error); - - return __cg_read(path, buf, size, offset, fi); -} - -static int do_proc_read(const char *path, char *buf, size_t size, off_t offset, - struct fuse_file_info *fi) -{ - char *error; - int (*__proc_read)(const char *path, char *buf, size_t size, - off_t offset, struct fuse_file_info *fi); - - dlerror(); - __proc_read = (int (*)(const char *, char *, size_t, off_t, struct fuse_file_info *))dlsym(dlopen_handle, "proc_read"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find proc_read()", error); - - return __proc_read(path, buf, size, offset, fi); -} - -static int do_sys_read(const char *path, char *buf, size_t size, off_t offset, - struct fuse_file_info *fi) -{ - char *error; - int (*__sys_read)(const char *path, char *buf, size_t size, - off_t offset, struct fuse_file_info *fi); - - dlerror(); - __sys_read = (int (*)(const char *, char *, size_t, off_t, struct fuse_file_info *))dlsym(dlopen_handle, "sys_read"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find sys_read()", error); - - return __sys_read(path, buf, size, offset, fi); -} - -static int do_cg_write(const char *path, const char *buf, size_t size, - off_t offset, struct fuse_file_info *fi) -{ - char *error; - int (*__cg_write)(const char *path, const char *buf, size_t size, - off_t offset, struct fuse_file_info *fi); - - dlerror(); - __cg_write = (int (*)(const char *, const char *, size_t, off_t, struct fuse_file_info *))dlsym(dlopen_handle, "cg_write"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find cg_write()", error); - - return __cg_write(path, buf, size, offset, fi); -} - -static int do_sys_write(const char *path, const char *buf, size_t size, - off_t offset, struct fuse_file_info *fi) -{ - char *error; - int (*__sys_write)(const char *path, const char *buf, size_t size, - off_t offset, struct fuse_file_info *fi); - - dlerror(); - __sys_write = (int (*)(const char *, const char *, size_t, off_t, struct fuse_file_info *))dlsym(dlopen_handle, "sys_write"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find sys_write()", error); - return __sys_write(path, buf, size, offset, fi); -} - -static int do_cg_mkdir(const char *path, mode_t mode) -{ - char *error; - int (*__cg_mkdir)(const char *path, mode_t mode); - - dlerror(); - __cg_mkdir = (int (*)(const char *, mode_t))dlsym(dlopen_handle, "cg_mkdir"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find cg_mkdir()", error); - - return __cg_mkdir(path, mode); -} - -static int do_cg_chown(const char *path, uid_t uid, gid_t gid) -{ - char *error; - int (*__cg_chown)(const char *path, uid_t uid, gid_t gid); - - dlerror(); - __cg_chown = (int (*)(const char *, uid_t, gid_t))dlsym(dlopen_handle, "cg_chown"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find cg_chown()", error); - - return __cg_chown(path, uid, gid); -} - -static int do_cg_rmdir(const char *path) -{ - char *error; - int (*__cg_rmdir)(const char *path); - - dlerror(); - __cg_rmdir = (int (*)(const char *path))dlsym(dlopen_handle, "cg_rmdir"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find cg_rmdir()", error); - - return __cg_rmdir(path); -} - -static int do_cg_chmod(const char *path, mode_t mode) -{ - char *error; - int (*__cg_chmod)(const char *path, mode_t mode); - - dlerror(); - __cg_chmod = (int (*)(const char *, mode_t))dlsym(dlopen_handle, "cg_chmod"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find cg_chmod()", error); - - return __cg_chmod(path, mode); -} - -static int do_cg_readdir(const char *path, void *buf, fuse_fill_dir_t filler, - off_t offset, struct fuse_file_info *fi) -{ - char *error; - int (*__cg_readdir)(const char *path, void *buf, fuse_fill_dir_t filler, - off_t offset, struct fuse_file_info *fi); - - dlerror(); - __cg_readdir = (int (*)(const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info *))dlsym(dlopen_handle, "cg_readdir"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find cg_readdir()", error); - - return __cg_readdir(path, buf, filler, offset, fi); -} - -static int do_proc_readdir(const char *path, void *buf, fuse_fill_dir_t filler, - off_t offset, struct fuse_file_info *fi) -{ - char *error; - int (*__proc_readdir)(const char *path, void *buf, fuse_fill_dir_t filler, - off_t offset, struct fuse_file_info *fi); - - dlerror(); - __proc_readdir = (int (*)(const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info *))dlsym(dlopen_handle, "proc_readdir"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find proc_readdir()", error); - - return __proc_readdir(path, buf, filler, offset, fi); -} - -static int do_sys_readdir(const char *path, void *buf, fuse_fill_dir_t filler, - off_t offset, struct fuse_file_info *fi) -{ - char *error; - int (*__sys_readdir)(const char *path, void *buf, fuse_fill_dir_t filler, - off_t offset, struct fuse_file_info *fi); - - dlerror(); - __sys_readdir = (int (*)(const char *, void *, fuse_fill_dir_t, off_t, struct fuse_file_info *))dlsym(dlopen_handle, "sys_readdir"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find sys_readdir()", error); - - return __sys_readdir(path, buf, filler, offset, fi); -} - -static int do_sys_readlink(const char *path, char *buf, size_t size) -{ - char *error; - int (*__sys_readlink)(const char *path, char *buf, size_t size); - - dlerror(); - __sys_readlink = (int (*)(const char *, char *, size_t))dlsym(dlopen_handle, "sys_readlink"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find sys_readlink()", error); - - return __sys_readlink(path, buf, size); -} - -static int do_cg_open(const char *path, struct fuse_file_info *fi) -{ - char *error; - int (*__cg_open)(const char *path, struct fuse_file_info *fi); - - dlerror(); - __cg_open = (int (*)(const char *, struct fuse_file_info *))dlsym(dlopen_handle, "cg_open"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find cg_open()", error); - - return __cg_open(path, fi); -} - -static int do_cg_access(const char *path, int mode) -{ - char *error; - int (*__cg_access)(const char *path, int mode); - - dlerror(); - __cg_access = (int (*)(const char *, int mode))dlsym(dlopen_handle, "cg_access"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find cg_access()", error); - - return __cg_access(path, mode); -} - -static int do_proc_open(const char *path, struct fuse_file_info *fi) -{ - char *error; - int (*__proc_open)(const char *path, struct fuse_file_info *fi); - - dlerror(); - __proc_open = (int (*)(const char *path, struct fuse_file_info *fi))dlsym(dlopen_handle, "proc_open"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find proc_open()", error); - - return __proc_open(path, fi); -} - -static int do_proc_opendir(const char *path, struct fuse_file_info *fi) -{ - char *error; - int (*__proc_opendir)(const char *path, struct fuse_file_info *fi); - - dlerror(); - __proc_opendir = (int (*)(const char *path, struct fuse_file_info *fi))dlsym(dlopen_handle, "proc_opendir"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find proc_opendir()", error); - - return __proc_opendir(path, fi); -} - -static int do_proc_access(const char *path, int mode) -{ - char *error; - int (*__proc_access)(const char *path, int mode); - - dlerror(); - __proc_access = (int (*)(const char *, int mode))dlsym(dlopen_handle, "proc_access"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find proc_access()", error); - - return __proc_access(path, mode); -} - -static int do_sys_open(const char *path, struct fuse_file_info *fi) -{ - char *error; - int (*__sys_open)(const char *path, struct fuse_file_info *fi); - - dlerror(); - __sys_open = (int (*)(const char *path, struct fuse_file_info *fi))dlsym(dlopen_handle, "sys_open"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find sys_open()", error); - - return __sys_open(path, fi); -} - -static int do_sys_opendir(const char *path, struct fuse_file_info *fi) -{ - char *error; - int (*__sys_opendir)(const char *path, struct fuse_file_info *fi); - - dlerror(); - __sys_opendir = (int (*)(const char *path, struct fuse_file_info *fi))dlsym(dlopen_handle, "sys_opendir"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find sys_opendir()", error); - - return __sys_opendir(path, fi); -} - -static int do_sys_access(const char *path, int mode) -{ - char *error; - int (*__sys_access)(const char *path, int mode); - - dlerror(); - __sys_access = (int (*)(const char *, int mode))dlsym(dlopen_handle, "sys_access"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find sys_access()", error); - - return __sys_access(path, mode); -} - -static int do_cg_release(const char *path, struct fuse_file_info *fi) -{ - char *error; - int (*__cg_release)(const char *path, struct fuse_file_info *fi); - - dlerror(); - __cg_release = (int (*)(const char *path, struct fuse_file_info *))dlsym(dlopen_handle, "cg_release"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find cg_release()", error); - - return __cg_release(path, fi); -} - -static int do_proc_release(const char *path, struct fuse_file_info *fi) -{ - char *error; - int (*__proc_release)(const char *path, struct fuse_file_info *fi); - - dlerror(); - __proc_release = (int (*)(const char *path, struct fuse_file_info *)) dlsym(dlopen_handle, "proc_release"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find proc_release()", error); - - return __proc_release(path, fi); -} - -static int do_proc_releasedir(const char *path, struct fuse_file_info *fi) -{ - char *error; - int (*__proc_releasedir)(const char *path, struct fuse_file_info *fi); - - dlerror(); - __proc_releasedir = (int (*)(const char *path, struct fuse_file_info *)) dlsym(dlopen_handle, "proc_releasedir"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find proc_releasedir()", error); - - return __proc_releasedir(path, fi); -} - -static int do_sys_release(const char *path, struct fuse_file_info *fi) -{ - char *error; - int (*__sys_release)(const char *path, struct fuse_file_info *fi); - - dlerror(); - __sys_release = (int (*)(const char *path, struct fuse_file_info *))dlsym(dlopen_handle, "sys_release"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find sys_release()", error); - - return __sys_release(path, fi); -} - -static int do_cg_opendir(const char *path, struct fuse_file_info *fi) -{ - char *error; - int (*__cg_opendir)(const char *path, struct fuse_file_info *fi); - - dlerror(); - __cg_opendir = (int (*)(const char *path, struct fuse_file_info *fi))dlsym(dlopen_handle, "cg_opendir"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find cg_opendir()", error); - - return __cg_opendir(path, fi); -} - -static int do_cg_releasedir(const char *path, struct fuse_file_info *fi) -{ - char *error; - int (*__cg_releasedir)(const char *path, struct fuse_file_info *fi); - - dlerror(); - __cg_releasedir = (int (*)(const char *path, struct fuse_file_info *))dlsym(dlopen_handle, "cg_releasedir"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find cg_releasedir()", error); - - return __cg_releasedir(path, fi); -} - -static int do_sys_releasedir(const char *path, struct fuse_file_info *fi) -{ - char *error; - int (*__sys_releasedir)(const char *path, struct fuse_file_info *fi); - - dlerror(); - __sys_releasedir = (int (*)(const char *path, struct fuse_file_info *))dlsym(dlopen_handle, "sys_releasedir"); - error = dlerror(); - if (error) - return log_error(-1, "%s - Failed to find sys_releasedir()", error); - - return __sys_releasedir(path, fi); -} +#define DEF_LIB_FS_OP(type, fsop) \ +static int do_##type##_##fsop(LIB_FS_##fsop##_OP_ARGS_TYPE) \ +{ \ + char *error; \ + int (*__##type##_##fsop)(LIB_FS_##fsop##_OP_ARGS_TYPE); \ + \ + dlerror(); \ + __##type##_##fsop = (int (*)(LIB_FS_##fsop##_OP_ARGS_TYPE))dlsym(dlopen_handle, #type"_"#fsop); \ + error = dlerror(); \ + if (error) \ + return log_error(-1, "%s - Failed to find "#type"_"#fsop"()", error); \ + \ + return __##type##_##fsop(LIB_FS_##fsop##_OP_ARGS); \ +} + +#define LIB_FS_getattr_OP_ARGS_TYPE const char *path, struct stat *sb +#define LIB_FS_getattr_OP_ARGS path, sb +DEF_LIB_FS_OP(cg , getattr) +DEF_LIB_FS_OP(proc , getattr) +DEF_LIB_FS_OP(sys , getattr) + +#define LIB_FS_read_OP_ARGS_TYPE const char *path, char *buf, size_t size, \ + off_t offset, struct fuse_file_info *fi +#define LIB_FS_read_OP_ARGS path, buf, size, offset, fi +DEF_LIB_FS_OP(cg , read) +DEF_LIB_FS_OP(proc , read) +DEF_LIB_FS_OP(sys , read) + +#define LIB_FS_write_OP_ARGS_TYPE const char *path, const char *buf, size_t size, \ + off_t offset, struct fuse_file_info *fi +#define LIB_FS_write_OP_ARGS path, buf, size, offset, fi +DEF_LIB_FS_OP(cg , write) +DEF_LIB_FS_OP(sys , write) + +#define LIB_FS_mkdir_OP_ARGS_TYPE const char *path, mode_t mode +#define LIB_FS_mkdir_OP_ARGS path, mode +DEF_LIB_FS_OP(cg, mkdir) + +#define LIB_FS_chown_OP_ARGS_TYPE const char *path, uid_t uid, gid_t gid +#define LIB_FS_chown_OP_ARGS path, uid, gid +DEF_LIB_FS_OP(cg, chown) + +#define LIB_FS_rmdir_OP_ARGS_TYPE const char *path +#define LIB_FS_rmdir_OP_ARGS path +DEF_LIB_FS_OP(cg, rmdir) + +#define LIB_FS_chmod_OP_ARGS_TYPE const char *path, mode_t mode +#define LIB_FS_chmod_OP_ARGS path, mode +DEF_LIB_FS_OP(cg, chmod) + +#define LIB_FS_readdir_OP_ARGS_TYPE const char *path, void *buf, fuse_fill_dir_t filler, \ + off_t offset, struct fuse_file_info *fi +#define LIB_FS_readdir_OP_ARGS path, buf, filler, offset, fi +DEF_LIB_FS_OP(cg , readdir) +DEF_LIB_FS_OP(proc , readdir) +DEF_LIB_FS_OP(sys , readdir) + +#define LIB_FS_readlink_OP_ARGS_TYPE const char *path, char *buf, size_t size +#define LIB_FS_readlink_OP_ARGS path, buf, size +DEF_LIB_FS_OP(sys , readlink) + +#define LIB_FS_open_OP_ARGS_TYPE const char *path, struct fuse_file_info *fi +#define LIB_FS_open_OP_ARGS path, fi +DEF_LIB_FS_OP(cg , open) +DEF_LIB_FS_OP(proc , open) +DEF_LIB_FS_OP(sys , open) + +#define LIB_FS_access_OP_ARGS_TYPE const char *path, int mode +#define LIB_FS_access_OP_ARGS path, mode +DEF_LIB_FS_OP(cg , access) +DEF_LIB_FS_OP(proc , access) +DEF_LIB_FS_OP(sys , access) + +#define LIB_FS_opendir_OP_ARGS_TYPE const char *path, struct fuse_file_info *fi +#define LIB_FS_opendir_OP_ARGS path, fi +DEF_LIB_FS_OP(cg , opendir) +DEF_LIB_FS_OP(proc , opendir) +DEF_LIB_FS_OP(sys , opendir) + +#define LIB_FS_release_OP_ARGS_TYPE const char *path, struct fuse_file_info *fi +#define LIB_FS_release_OP_ARGS path, fi +DEF_LIB_FS_OP(cg , release) +DEF_LIB_FS_OP(proc , release) +DEF_LIB_FS_OP(sys , release) + +#define LIB_FS_releasedir_OP_ARGS_TYPE const char *path, struct fuse_file_info *fi +#define LIB_FS_releasedir_OP_ARGS path, fi +DEF_LIB_FS_OP(cg , releasedir) +DEF_LIB_FS_OP(proc , releasedir) +DEF_LIB_FS_OP(sys , releasedir) static bool cgroup_is_enabled = false; From af454ab8d4cf4c98b83e8519101e2cb21e4d9140 Mon Sep 17 00:00:00 2001 From: Alexander Mikhalitsyn Date: Sun, 28 Apr 2024 22:06:36 +0200 Subject: [PATCH 2/2] src/utils: fix in_same_namespace helper Signed-off-by: Alexander Mikhalitsyn --- src/utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.c b/src/utils.c index 60b47011..c1592951 100644 --- a/src/utils.c +++ b/src/utils.c @@ -113,7 +113,7 @@ bool is_shared_pidns(pid_t pid) return false; fd = in_same_namespace(pid, getpid(), "pid"); - if (fd == EINVAL) + if (fd == -EINVAL) return true; return false;