查找进程linux(C代码)的打开文件描述符?

Ash*_*ish 26 c linux process file-descriptor

我想在Linux中找到为进程打开的所有fds.

我可以使用glib库函数吗?

Orw*_*ile 29

这是我以前使用的一些代码,我不知道/ proc/self(thx 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;
}
Run Code Online (Sandbox Code Playgroud)

我遇到一个非常糟糕的问题,一次泄漏文件句柄,事实证明我实际编码了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;
}
Run Code Online (Sandbox Code Playgroud)

您可能也想要这些,以满足上面的最后一个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);
}
Run Code Online (Sandbox Code Playgroud)

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;
}   
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

我希望能回答你的任何问题,如果你想知道,我实际上来到这里寻找OP提出的问题的答案,并在阅读答案后,记得我多年前已经编写了代码.请享用.


Don*_*ows 27

因为你在Linux上,你(几乎可以肯定)/proc安装了文件系统.这意味着最简单的方法是获取内容列表/proc/self/fd; 其中的每个文件都以FD命名.(当然,使用g_dir_open,g_dir_read_nameg_dir_close进行列表.)

否则获取信息是适度的尴尬(例如,没有有用的POSIX API;这是一个未标准化的区域).

  • 要将它们列为另一个进程,请列出目录`/ proc/PID/fd`(其中PID是相关进程的进程ID).如果你不是root用户,你只能在某些进程中看到它们. (4认同)
  • 对于什么来说有一个小缺陷:`/ proc/self/fd`目录本身在扫描时也算作一个打开的文件描述符. (3认同)
  • 使用 /proc/*/pid 当然是 Linux 特定的并且根本不可移植,但如果这不是问题那么它应该可以正常工作。 (2认同)
  • @Tom:问题_was_标有`linux` ... (2认同)

fyr*_*fyr 6

如果您可以通过pid识别流程,那么您可以这样做

ls -l /proc/<pid>/fd | wc - l
Run Code Online (Sandbox Code Playgroud)

在C中你可以管道所有内容并重用输出,或者你可以在上面提到的目录中自己计算文件(count方法,例如这里使用C计算目录中的文件数)