mirror of
https://sourceware.org/git/glibc.git
synced 2024-12-26 20:51:11 +00:00
607449506f
The function closes all open file descriptors greater than or equal to input argument. Negative values are clamped to 0, i.e, it will close all file descriptors. As indicated by the bug report, this is a common symbol provided by different systems (Solaris, OpenBSD, NetBSD, FreeBSD) and, although its has inherent issues with not taking in consideration internal libc file descriptors (such as syslog), this is also a common feature used in multiple projects [1][2][3][4][5]. The Linux fallback implementation iterates over /proc and close all file descriptors sequentially. Although it was raised the questioning whether getdents on /proc/self/fd might return disjointed entries when file descriptor are closed; it does not seems the case on my testing on multiple kernel (v4.18, v5.4, v5.9) and the same strategy is used on different projects [1][2][3][5]. Also, the interface is set a fail-safe meaning that a failure in the fallback results in a process abort. Checked on x86_64-linux-gnu and i686-linux-gnu on kernel 5.11 and 4.15. [1]5238e95759/src/basic/fd-util.c (L217)
[2]ddf4b77e11/src/lxc/start.c (L236)
[3]9e4f2f3a6b/Modules/_posixsubprocess.c (L220)
[4]5f47c0613e/src/libstd/sys/unix/process2.rs (L303-L308)
[5] https://github.com/openjdk/jdk/blob/master/src/java.base/unix/native/libjava/childproc.c#L82
35 lines
1.2 KiB
C
35 lines
1.2 KiB
C
/* Close a range of file descriptors.
|
|
Copyright (C) 2021 Free Software Foundation, Inc.
|
|
This file is part of the GNU C Library.
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with the GNU C Library; if not, see
|
|
<https://www.gnu.org/licenses/>. */
|
|
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <not-cancel.h>
|
|
|
|
void
|
|
__closefrom (int lowfd)
|
|
{
|
|
int maxfd = __getdtablesize ();
|
|
if (maxfd == -1)
|
|
__fortify_fail ("closefrom failed to get the file descriptor table size");
|
|
|
|
for (int i = 0; i < maxfd; i++)
|
|
if (i >= lowfd)
|
|
__close_nocancel_nostatus (i);
|
|
}
|
|
weak_alias (__closefrom, closefrom)
|