【问题标题】:Finding open file descriptors for a process linux ( C code )?查找进程 linux(C 代码)的打开文件描述符?
【发布时间】:2011-09-28 19:35:14
【问题描述】:

我想在 linux 中查找为某个进程打开的所有 fd。

我可以使用 glib 库函数吗?

【问题讨论】:

    标签: c linux process file-descriptor


    【解决方案1】:

    这是我以前使用的一些代码,我不知道 /proc/self(感谢 Donal!),但无论如何这种方式可能更通用。我已经在顶部包含了所有功能所需的包含。

    #include <string.h>
    #include <stdio.h>
    #include <dirent.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <errno.h>
    #include <sys/resource.h>
    
    #ifndef FALSE
    #define FALSE (0)
    #endif
    #ifndef TRUE
    #define TRUE (!FALSE)
    #endif
    
    /* implementation of Donal Fellows method */ 
    int get_num_fds()
    {
         int fd_count;
         char buf[64];
         struct dirent *dp;
    
         snprintf(buf, 64, "/proc/%i/fd/", getpid());
    
         fd_count = 0;
         DIR *dir = opendir(buf);
         while ((dp = readdir(dir)) != NULL) {
              fd_count++;
         }
         closedir(dir);
         return fd_count;
    }
    

    我曾经遇到过一个非常糟糕的文件句柄泄漏问题,结果证明我实际上编写了 Tom H. 建议的解决方案:

    /* check whether a file-descriptor is valid */
    int pth_util_fd_valid(int fd)
    {
         if (fd < 3 || fd >= FD_SETSIZE)
              return FALSE;
         if (fcntl(fd, F_GETFL) == -1 && errno == EBADF)
              return FALSE;
         return TRUE;
    }
    
    /* check first 1024 (usual size of FD_SESIZE) file handles */
    int test_fds()
    {
         int i;
         int fd_dup;
         char errst[64];
         for (i = 0; i < FD_SETSIZE; i++) {
              *errst = 0;
              fd_dup = dup(i);
              if (fd_dup == -1) {
                    strcpy(errst, strerror(errno));
                    // EBADF  oldfd isn’t an open file descriptor, or newfd is out of the allowed range for file descriptors.
                    // EBUSY  (Linux only) This may be returned by dup2() during a race condition with open(2) and dup().
                    // EINTR  The dup2() call was interrupted by a signal; see signal(7).
                    // EMFILE The process already has the maximum number of file descriptors open and tried to open a new one.
              } else {
                    close(fd_dup);
                    strcpy(errst, "dup() ok");
              }
              printf("%4i: %5i %24s %s\n", i, fcntl(i, F_GETOWN), fd_info(i), errst);
         }
         return 0;
    }
    

    你可能也想要这些,以满足上面的最后一个 printf...

    char *fcntl_flags(int flags)
    {
        static char output[128];
        *output = 0;
    
        if (flags & O_RDONLY)
            strcat(output, "O_RDONLY ");
        if (flags & O_WRONLY)
            strcat(output, "O_WRONLY ");
        if (flags & O_RDWR)
            strcat(output, "O_RDWR ");
        if (flags & O_CREAT)
            strcat(output, "O_CREAT ");
        if (flags & O_EXCL)
            strcat(output, "O_EXCL ");
        if (flags & O_NOCTTY)
            strcat(output, "O_NOCTTY ");
        if (flags & O_TRUNC)
            strcat(output, "O_TRUNC ");
        if (flags & O_APPEND)
            strcat(output, "O_APPEND ");
        if (flags & O_NONBLOCK)
            strcat(output, "O_NONBLOCK ");
        if (flags & O_SYNC)
            strcat(output, "O_SYNC ");
        if (flags & O_ASYNC)
            strcat(output, "O_ASYNC ");
    
        return output;
    }
    
    char *fd_info(int fd)
    {
        if (fd < 0 || fd >= FD_SETSIZE)
            return FALSE;
        // if (fcntl(fd, F_GETFL) == -1 && errno == EBADF)
        int rv = fcntl(fd, F_GETFL);
        return (rv == -1) ? strerror(errno) : fcntl_flags(rv);
    }
    

    FD_SETSIZE 通常为 1024,每个进程的最大文件数通常为 1024。如果您想确定,可以将其替换为调用此函数,如 TomH 所述。

    #include <sys/time.h>
    #include <sys/resource.h>
    
    rlim_t get_rlimit_files()
    {
        struct rlimit rlim;
        getrlimit(RLIMIT_NOFILE, &rlim);
        return rlim.rlim_cur;
    }   
    

    如果您将所有这些放在一个文件中(我这样做只是为了检查它),您可以生成与此类似的输出,以确认它像宣传的那样工作:

    0:     0                  O_RDWR  dup() ok
    1:     0                O_WRONLY  dup() ok
    2:     0                  O_RDWR  dup() ok
    3:     0              O_NONBLOCK  dup() ok
    4:     0     O_WRONLY O_NONBLOCK  dup() ok
    5:    -1      Bad file descriptor Bad file descriptor
    6:    -1      Bad file descriptor Bad file descriptor
    7:    -1      Bad file descriptor Bad file descriptor
    8:    -1      Bad file descriptor Bad file descriptor
    9:    -1      Bad file descriptor Bad file descriptor
    

    我希望能回答您的任何问题,如果您想知道,我实际上是来这里寻找 OP 提出的问题的答案,在阅读答案后,请记住我几年前就已经编写了代码。享受吧。

    【讨论】:

    • 请注意 pth_util_fd_valid 不适用于 fd 表中的漏洞。考虑以下情况:int fd = open(...); // fd =3fd = open(...); // fd=4close(4)函数将达不到fd=4
    • @kfir – pth_util_fd_valid 只报告给定文件句柄的有效性,它不包含循环。
    • 第二个代码示例需要包含 sys/select.h 来定义 FD_SETSIZE。
    • 扫描/proc/self/fd 或者更便携的/dev/fd 比根据当前进程ID 组装路径名更容易。
    【解决方案2】:

    由于您使用的是 Linux,因此您(几乎可以肯定)已经安装了 /proc 文件系统。这意味着最简单的方法是获取/proc/self/fd 的内容列表;那里的每个文件都以 FD 命名。 (当然,使用g_dir_openg_dir_read_nameg_dir_close 进行列表。)

    否则获取信息会有点尴尬(例如,没有有用的 POSIX API;这是一个没有标准化的领域)。

    【讨论】:

    • 要为另一个进程列出它们,请改为列出目录/proc/PID/fd(其中 PID 是相关进程的进程 ID)。如果您不是 root,则只能在某些进程中看到它们。
    • 使用 /proc/*/pid 当然是非常特定于 linux 的,而且根本不是可移植的,但如果这不是问题,那么它应该可以正常工作。
    • @Tom:问题标记为linux...
    • 有一个小陷阱:/proc/self/fd 目录本身在扫描时也算作打开的文件描述符。
    • @C2H5OH 是的。但是替代方案是可怕的或同样有问题(例如,运行一个子进程来进行探测将打开 更多 FD)。
    【解决方案3】:

    如果你可以通过 pid 识别进程,你可以简单地做

    ls -l /proc/<pid>/fd | wc - l
    

    在 C 中,您可以通过管道传输所有内容并重复使用输出,或者您可以自己计算上述目录中的文件(计数方法,例如这里 Counting the number of files in a directory using C

    【讨论】:

      【解决方案4】:

      有时 C++ 是一种选择,Donal 使用 boost::filesystem 的解决方案:

      #include <iostream>
      #include <string>
      #include <boost/filesystem.hpp>
      #include <unistd.h>
      
      namespace fs = boost::filesystem;
      
      int main()
      {
          std::string path = "/proc/" + std::to_string(::getpid()) + "/fd/";
          unsigned count = std::distance(fs::directory_iterator(path),
                                         fs::directory_iterator());
          std::cout << "Number of opened FDs: " << count << std::endl;
      }
      

      【讨论】:

      • 用 C 语言而不是 C++ 编写的 OP 程序。这个答案是无关紧要的。
      • 扫描/proc/self/fd 或更便携的/dev/fd 比根据当前进程ID 组装路径名更容易。
      【解决方案5】:

      如果您的意思是如何从进程中以编程方式执行此操作,那么正常(如果有点可怕)方法是执行类似循环所有可能描述符的操作(使用 getrlimit() 读取 RLIMIT_NOFILE 以查找范围)在每一个上调用fcntl(fd, F_GETFD, 0) 之类的东西并检查 EBADF 响应以查看哪些未打开。

      如果你的意思是你想从 shell 中找出进程打开了哪些文件,那么lsof -p &lt;pid&gt; 就是你想要的。

      【讨论】:

      • 我看到相当多的进程具有打开的 FD 0、1、2 和 255。获得 252 个失败的系统调用并不好......
      • 这完全取决于 - 显然您不想在性能关键的代码中执行此操作,但除此之外这不是一个大问题。无论如何,在现代 linux 系统上,这个限制更有可能是 2048 之类的东西。
      • RLIMIT_NOFILE 只告诉您新创建的文件描述符的最大值,而不是当前打开的文件描述符的限制,因此您不能使用 getrlimit 来发现文件描述符编号的上限。
      • 是的,我现在正在尝试将其作为一些沙盒代码的一部分。我想运行一些任意设置代码,然后列出所有打开的文件句柄并关闭它们,然后使用 setrlimit 来防止打开任何新的文件句柄。
      【解决方案6】:

      fstat 命令列出了系统所有正在运行的进程及其打开的描述符,此外它还列出了它是什么类型的描述符(文件、套接字、管道等),并尝试给出描述符正在读取或写入的提示作为该文件系统上的什么文件系统和什么 inode 号

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-22
        • 2020-05-01
        • 2011-01-31
        • 1970-01-01
        • 2021-05-23
        • 1970-01-01
        相关资源
        最近更新 更多