我想在Linux中查找进程打开的所有文件描述符。
我能用GLib库函数来实现吗?
我想在Linux中查找进程打开的所有文件描述符。
我能用GLib库函数来实现吗?
这是我以前使用的一些代码,当时不知道有 /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;
}
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提问的答案的,但是看完答案后,我记得我已经几年前就写过这段代码了。享受吧。
由于您使用的是Linux系统,您(几乎肯定)已经挂载了/proc
文件系统。这意味着最简单的方法是获取/proc/self/fd
目录的内容列表;其中每个文件的名称都是FD。 (当然要使用g_dir_open
、g_dir_read_name
和g_dir_close
来进行列表操作)。
以其他方式获取信息则有些棘手(例如,没有有用的POSIX API;这是一个未标准化的领域)。
/proc/PID/fd
(其中PID是所讨论进程的进程标识符)。如果您不是root用户,则只能在某些进程中看到它们。请注意,翻译后的内容与原文意思相同,更通俗易懂,没有解释,并且没有其他额外信息返回。 - Donal Fellows/proc/self/fd
目录时,该目录本身也会被视为一个打开的文件描述符。 - C2H5OHls -l /proc/<pid>/fd | wc - l
#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;
}
/proc/self/fd
或稍微更便携的/dev/fd
比基于当前进程ID组装路径名更容易。 - David Foerstergetrlimit()
读取RLIMIT_NOFILE
以查找范围),对每个描述符调用类似fcntl(fd,F_GETFD,0)
的内容,并检查EBADF响应以查看哪些未打开。lsof -p <pid>
。fstat命令列出了系统中所有正在运行的进程及其打开的描述符,此外它还列出了描述符的类型(文件、套接字、管道等),并尝试提示描述符正在读取或写入的内容,例如是哪个文件系统以及该文件系统上的哪个inode号码。
int fd = open(...); // fd =3
fd = open(...); // fd=4
close(4)
该函数将无法到达fd=4。 - kfir/proc/self/fd
或稍微更便携的/dev/fd
比基于当前进程ID组装路径名更容易。 - David Foerster