获取最高分配的文件描述符
是否有一种可移植的方法(POSIX)来获取当前进程的最高分配文件描述符编号?
例如,我知道有一种很好的方法可以获取 AIX 上的号码,但我正在寻找一种可移植的方法。
我问的原因是我想关闭所有打开的文件描述符。 我的程序是一个以 root 身份运行的服务器,并为非 root 用户分叉和执行子程序。 在子进程中保持特权文件描述符打开是一个安全问题。 某些文件描述符可能由我无法控制的代码(C 库、第三方库等)打开,因此我也不能依赖 FD_CLOEXEC
。
Is there a portable way (POSIX) to get the highest allocated file descriptor number for the current process?
I know that there's a nice way to get the number on AIX, for example, but I'm looking for a portable method.
The reason I'm asking is that I want to close all open file descriptors. My program is a server which runs as root and forks and execs child programs for non-root users. Leaving the privileged file descriptors open in the child process is a security problem. Some file descriptors may be opened by code I cannot control (the C library, third party libraries, etc.), so I cannot rely on FD_CLOEXEC
either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
虽然可移植,但关闭
sysconf(_SC_OPEN_MAX)
之前的所有文件描述符并不可靠,因为在大多数系统上,此调用会返回当前文件描述符软限制,该限制可能已降低到最高使用的文件描述符以下。 另一个问题是,在许多系统上sysconf(_SC_OPEN_MAX)
可能会返回INT_MAX
,这可能导致此方法速度慢得令人无法接受。 不幸的是,没有可靠、可移植的替代方案不涉及迭代每个可能的非负 int 文件描述符。虽然不可移植,但当今常用的大多数操作系统都针对此问题提供了以下一个或多个解决方案:
用于关闭所有打开的文件描述符的库函数 >= fd< /em> 或在一个范围内。 对于关闭所有文件描述符的常见情况,这是最简单的解决方案,尽管它不能用于其他用途。 要关闭除特定集合之外的所有文件描述符,可以使用
dup2
将它们预先移至低端,并在必要时将它们移回。closefrom(fd)
(Linux with glibc 2.34+、Solaris 9+、FreeBSD 7.3+、NetBSD 3.0+、OpenBSD 3.5+)fcntl(fd, F_CLOSEM, 0)
(AIX、IRIX、NetBSD)close_range(lowfd, highfd, 0)
(Linux 内核 5.9+,带 glibc 2.34+,FreeBSD 12.2+)提供当前最大文件描述符的库函数过程中使用。 要关闭超过一定数量的所有文件描述符,可以将所有文件描述符关闭到该最大值,或者在循环中不断获取并关闭最高的文件描述符,直到达到下限。 哪个更有效取决于文件描述符密度。
fcntl(0, F_MAXFD)
(NetBSD)pstat_getproc(&ps, sizeof(struct pst_status), (size_t)0, (int)getpid())
返回有关进程的信息,包括当前在 pst.pst_highestfd 中打开的最高文件描述符。 (HP-UX)
用于列出进程当前使用的所有文件描述符的库函数。 这更加灵活,因为它允许关闭所有文件描述符,查找最高的文件描述符,或者对每个打开的文件描述符(甚至可能是另一个进程的文件描述符)执行任何其他操作。 示例 (OpenSSH)
proc_pidinfo(getpid(), PROC_PIDLISTFDS, 0, fdinfo_buf, sz)
(macOS)当前为进程分配的文件描述符槽数提供了上限绑定到当前使用的文件描述符编号。 示例 (Ruby)
/proc/
pid/status
或/proc/self/status
中的“FDSize:”行(Linux)A 包含每个打开的文件描述符的条目的目录。 这与 #3 类似,只是它不是库函数。 这可能比其他常见用途的方法更复杂,并且可能会因各种原因而失败,例如未安装 proc/fdescfs、chroot 环境或没有可用于打开目录的文件描述符(进程或系统限制)。 因此,这种方法的使用通常与回退机制结合使用。 示例 (OpenSSH), 另一个示例 (glib) .
/proc/
pid/fd/
或/proc/self/fd/
( Linux、Solaris、AIX、Cygwin、NetBSD)(AIX 不支持“
self
”)/dev/fd/
(FreeBSD、macOS)使用这种方法可能很难可靠地处理所有极端情况。 例如,考虑以下情况:所有文件描述符 >= fd 将被关闭,但所有文件描述符 fd 将被关闭。 使用了fd,当前进程资源限制为fd,并且有文件描述符>=fd正在使用。 由于已达到进程资源限制,因此无法打开目录。 如果通过资源限制关闭 fd 中的每个文件描述符或将 sysconf(_SC_OPEN_MAX) 用作后备,则不会关闭任何内容。
While portable, closing all file descriptors up to
sysconf(_SC_OPEN_MAX)
is not reliable, because on most systems this call returns the current file descriptor soft limit, which could have been lowered below the highest used file descriptor. Another issue is that on many systemssysconf(_SC_OPEN_MAX)
may returnINT_MAX
, which can cause this approach to be unacceptably slow. Unfortunately, there is no reliable, portable alternative that does not involve iterating over every possible non-negative int file descriptor.Although not portable, most operating systems in common use today provide one or more of the following solutions to this problem:
A library function to close all open file descriptors >= fd or within a range. This is the simplest solution for the common case of closing all file descriptors, although it cannot be used for much else. To close all file descriptors except for a certain set,
dup2
can be used to move them to the low end beforehand, and to move them back afterward if necessary.closefrom(fd)
(Linux with glibc 2.34+, Solaris 9+, FreeBSD 7.3+, NetBSD 3.0+, OpenBSD 3.5+)fcntl(fd, F_CLOSEM, 0)
(AIX, IRIX, NetBSD)close_range(lowfd, highfd, 0)
(Linux kernel 5.9+ with glibc 2.34+, FreeBSD 12.2+)A library function to provide the maximum file descriptor currently in use by the process. To close all file descriptors above a certain number, either close all of them up to this maximum, or continually get and close the highest file descriptor in a loop until the low bound is reached. Which is more efficient depends on the file descriptor density.
fcntl(0, F_MAXFD)
(NetBSD)pstat_getproc(&ps, sizeof(struct pst_status), (size_t)0, (int)getpid())
Returns information about the process, including the highest file descriptor currently open in
ps.pst_highestfd
. (HP-UX)A library function to list all file descriptors currently in use by the process. This is more flexible in that it allows for closing all file descriptors, finding the highest file descriptor, or doing just about anything else on every open file descriptor, possibly even those of another process. Example (OpenSSH)
proc_pidinfo(getpid(), PROC_PIDLISTFDS, 0, fdinfo_buf, sz)
(macOS)The number of file descriptor slots currently allocated for a process provides an upper bound on the file descriptor numbers currently in use. Example (Ruby)
/proc/
pid/status
or/proc/self/status
(Linux)A directory containing an entry for each open file descriptor. This is similar to #3 except that it isn't a library function. This can be more complicated than the other approaches for the common uses, and can fail for a variety of reasons such as proc/fdescfs not mounted, a chroot environment, or no file descriptors available to open the directory (process or system limit). Therefore use of this approach is often combined with a fallback mechanism. Example (OpenSSH), another example (glib).
/proc/
pid/fd/
or/proc/self/fd/
(Linux, Solaris, AIX, Cygwin, NetBSD)(AIX does not support "
self
")/dev/fd/
(FreeBSD, macOS)It can be difficult to handle all corner cases reliably with this approach. For example consider the situation where all file descriptors >= fd are to be closed, but all file descriptors < fd are used, the current process resource limit is fd, and there are file descriptors >= fd in use. Because the process resource limit has been reached the directory cannot be opened. If closing every file descriptor from fd through the resource limit or
sysconf(_SC_OPEN_MAX)
is used as a fallback, nothing will be closed.POSIX 方式是:(
请注意,从 3 开始关闭,以保持 stdin/stdout/stderr 打开)
如果文件描述符未打开,close() 无害地返回 EBADF。 没有必要浪费另一个系统调用检查。
一些 Unix 支持 closefrom()。 这可以避免根据最大可能的文件描述符数量对 close() 进行过多的调用。 虽然这是我所知道的最好的解决方案,但它完全不可移植。
The POSIX way is:
(note that's closing from 3 up, to keep stdin/stdout/stderr open)
close() harmlessly returns EBADF if the file descriptor is not open. There's no need to waste another system call checking.
Some Unixes support a closefrom(). This avoids the excessive number of calls to close() depending on the maximum possible file descriptor number. While the best solution I'm aware of, it's completely nonportable.
我已经编写了代码来处理所有特定于平台的功能。 所有函数都是异步信号安全的。 我认为人们可能会发现这很有用。 目前仅在 OS X 上进行了测试,请随意改进/修复。
I've written code to deal with all platform-specific features. All functions are async-signal safe. Thought people might find this useful. Only tested on OS X right now, feel free to improve/fix.
在 MacOS 上,您可以将
posix_spawn
与通过posix_spawnattr_setflags
设置的 Apple 扩展POSIX_SPAWN_CLOEXEC_DEFAULT
结合使用。这将只留下在 posix_spawn 调用 open 中显式设置的文件描述符,关闭调用其他文件描述符。
On MacOS, you can use
posix_spawn
with the Apple extensionPOSIX_SPAWN_CLOEXEC_DEFAULT
set withposix_spawnattr_setflags
.This will leave only the file descriptors set up explicitly in the
posix_spawn
call open, closing call the others.当您的程序启动但尚未打开任何内容时。 例如,像 main() 的开头一样。 pipeline 和 fork 立即启动执行服务器。 这样它的内存和其他细节就干净了,你可以给它一些东西来 fork & 执行。
如果你想对执行的程序进行 IO,执行器服务器将必须进行套接字重定向,并且你可以使用 unix 套接字。
Right when your program started and hasn't opened anything. E.g. like the start of main(). pipe and fork immediately starting an executer server. This way it's memory and other details is clean and you can just give it things to fork & exec.
If you want to do IO on the executed program the executor server will have to do socket redirects and you can use unix sockets.
为什么不关闭从 0 到 10000 的所有描述符。
这会非常快,而且最糟糕的情况是 EBADF。
Why don't you close all descriptors from 0 to, say, 10000.
It would be pretty fast, and the worst thing that would happen is EBADF.