]> git.proxmox.com Git - mirror_lxcfs.git/commitdiff
utils: add and use opathdir()
authorChristian Brauner (Microsoft) <christian.brauner@ubuntu.com>
Sun, 13 Mar 2022 08:46:37 +0000 (09:46 +0100)
committerChristian Brauner (Microsoft) <christian.brauner@ubuntu.com>
Sun, 13 Mar 2022 08:47:07 +0000 (09:47 +0100)
Signed-off-by: Christian Brauner (Microsoft) <christian.brauner@ubuntu.com>
src/sysfs_fuse.c
src/utils.c
src/utils.h

index 30333bd9e07077c8ad736bf95c9c7636c50e47a6..1ce848406e7750b9aa740213d02b717aef9c2515 100644 (file)
@@ -516,7 +516,7 @@ __lxcfs_fuse_ops int sys_readdir(const char *path, void *buf,
                                 fuse_fill_dir_t filler, off_t offset,
                                 struct fuse_file_info *fi)
 {
-       __do_closedir DIR *dir = NULL;
+       __do_closedir DIR *dirp = NULL;
        struct dirent *dirent;
        struct file_info *f = INTTYPE_TO_PTR(fi->fh);
 
@@ -567,12 +567,12 @@ __lxcfs_fuse_ops int sys_readdir(const char *path, void *buf,
 
                return filler_sys_devices_system_cpu(path, buf, filler);
        case LXC_TYPE_SYS_DEVICES_SYSTEM_CPU_SUBDIR: {
-                       dir = opendir(path);
-                       if (!dir)
-                               return -ENOENT;
+                       dirp = opathdir(path);
+                       if (!dirp)
+                               return -errno;
 
-                       while ((dirent = readdir(dir))) {
-                               if (dirent_fillerat(filler, dir, dirent, buf, 0) != 0)
+                       while ((dirent = readdir(dirp))) {
+                               if (dirent_fillerat(filler, dirp, dirent, buf, 0) != 0)
                                        return -ENOENT;
                        }
 
index bcd026ac993767e66201819e58fe52380e27e0a8..b7ac721b0019116d976a66cc1dcb0f394e7d9031 100644 (file)
@@ -634,3 +634,19 @@ char *read_file_at(int dfd, const char *fnam, unsigned int o_flags)
 
        return move_ptr(buf);
 }
+
+DIR *opathdir(const char *path)
+{
+       __do_close int dfd = -EBADF;
+       DIR *dirp;
+
+       dfd = open(path, O_DIRECTORY | O_PATH | O_CLOEXEC | O_NOFOLLOW);
+       if (dfd < 0)
+               return NULL;
+
+       dirp = fdopendir(dfd);
+       if (dirp)
+               move_fd(dfd); /* Transfer ownership to fdopendir(). */
+
+       return dirp;
+}
index 6df50ee85a68ba37eae40d97b724d31855a470f9..0e8d3c86c106fa08ea6325811b0ff085c12fd8f1 100644 (file)
@@ -59,6 +59,7 @@ static inline int pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
 extern FILE *fopen_cached(const char *path, const char *mode,
                          void **caller_freed_buffer);
 extern FILE *fdopen_cached(int fd, const char *mode, void **caller_freed_buffer);
+extern DIR *opathdir(const char *path);
 extern ssize_t write_nointr(int fd, const void *buf, size_t count);
 extern int safe_uint64(const char *numstr, uint64_t *converted, int base);
 extern char *trim_whitespace_in_place(char *buffer);