io: Add closefrom [BZ #10353]
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] https://github.com/systemd/systemd/blob/5238e9575906297608ff802a27e2ff9effa3b338/src/basic/fd-util.c#L217
[2] https://github.com/lxc/lxc/blob/ddf4b77e11a4d08f09b7b9cd13e593f8c047edc5/src/lxc/start.c#L236
[3] https://github.com/python/cpython/blob/9e4f2f3a6b8ee995c365e86d976937c141d867f8/Modules/_posixsubprocess.c#L220
[4] https://github.com/rust-lang/rust/blob/5f47c0613ed4eb46fca3633c1297364c09e5e451/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
2021-03-10 15:26:32 +00:00
|
|
|
/* Close a range of file descriptors. Linux version.
|
|
|
|
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 <arch-fd_to_filename.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <not-cancel.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
/* Fallback code: iterates over /proc/self/fd, closing each file descriptor
|
2021-03-10 15:26:33 +00:00
|
|
|
that fall on the criteria. If DIRFD_FALLBACK is set, a failure on
|
|
|
|
/proc/self/fd open will trigger a fallback that tries to close a file
|
|
|
|
descriptor before proceed. */
|
io: Add closefrom [BZ #10353]
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] https://github.com/systemd/systemd/blob/5238e9575906297608ff802a27e2ff9effa3b338/src/basic/fd-util.c#L217
[2] https://github.com/lxc/lxc/blob/ddf4b77e11a4d08f09b7b9cd13e593f8c047edc5/src/lxc/start.c#L236
[3] https://github.com/python/cpython/blob/9e4f2f3a6b8ee995c365e86d976937c141d867f8/Modules/_posixsubprocess.c#L220
[4] https://github.com/rust-lang/rust/blob/5f47c0613ed4eb46fca3633c1297364c09e5e451/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
2021-03-10 15:26:32 +00:00
|
|
|
_Bool
|
2021-03-10 15:26:33 +00:00
|
|
|
__closefrom_fallback (int from, _Bool dirfd_fallback)
|
io: Add closefrom [BZ #10353]
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] https://github.com/systemd/systemd/blob/5238e9575906297608ff802a27e2ff9effa3b338/src/basic/fd-util.c#L217
[2] https://github.com/lxc/lxc/blob/ddf4b77e11a4d08f09b7b9cd13e593f8c047edc5/src/lxc/start.c#L236
[3] https://github.com/python/cpython/blob/9e4f2f3a6b8ee995c365e86d976937c141d867f8/Modules/_posixsubprocess.c#L220
[4] https://github.com/rust-lang/rust/blob/5f47c0613ed4eb46fca3633c1297364c09e5e451/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
2021-03-10 15:26:32 +00:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
int dirfd = __open_nocancel (FD_TO_FILENAME_PREFIX, O_RDONLY | O_DIRECTORY,
|
|
|
|
0);
|
|
|
|
if (dirfd == -1)
|
|
|
|
{
|
|
|
|
/* The closefrom should work even when process can't open new files. */
|
2021-03-10 15:26:33 +00:00
|
|
|
if (errno == ENOENT || !dirfd_fallback)
|
io: Add closefrom [BZ #10353]
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] https://github.com/systemd/systemd/blob/5238e9575906297608ff802a27e2ff9effa3b338/src/basic/fd-util.c#L217
[2] https://github.com/lxc/lxc/blob/ddf4b77e11a4d08f09b7b9cd13e593f8c047edc5/src/lxc/start.c#L236
[3] https://github.com/python/cpython/blob/9e4f2f3a6b8ee995c365e86d976937c141d867f8/Modules/_posixsubprocess.c#L220
[4] https://github.com/rust-lang/rust/blob/5f47c0613ed4eb46fca3633c1297364c09e5e451/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
2021-03-10 15:26:32 +00:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
for (int i = from; i < INT_MAX; i++)
|
|
|
|
{
|
|
|
|
int r = __close_nocancel (i);
|
|
|
|
if (r == 0 || (r == -1 && errno != EBADF))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dirfd = __open_nocancel (FD_TO_FILENAME_PREFIX, O_RDONLY | O_DIRECTORY,
|
|
|
|
0);
|
|
|
|
if (dirfd == -1)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buffer[1024];
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
ssize_t ret = __getdents64 (dirfd, buffer, sizeof (buffer));
|
|
|
|
if (ret == -1)
|
|
|
|
goto err;
|
|
|
|
else if (ret == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* If any file descriptor is closed it resets the /proc/self position
|
|
|
|
read again from the start (to obtain any possible kernel update). */
|
|
|
|
bool closed = false;
|
|
|
|
char *begin = buffer, *end = buffer + ret;
|
|
|
|
while (begin != end)
|
|
|
|
{
|
|
|
|
unsigned short int d_reclen;
|
|
|
|
memcpy (&d_reclen, begin + offsetof (struct dirent64, d_reclen),
|
|
|
|
sizeof (d_reclen));
|
|
|
|
const char *dname = begin + offsetof (struct dirent64, d_name);
|
|
|
|
begin += d_reclen;
|
|
|
|
|
|
|
|
if (dname[0] == '.')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int fd = 0;
|
|
|
|
for (const char *s = dname; (unsigned int) (*s) - '0' < 10; s++)
|
|
|
|
fd = 10 * fd + (*s - '0');
|
|
|
|
|
|
|
|
if (fd == dirfd || fd < from)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* We ignore close errors because EBADF, EINTR, and EIO means the
|
|
|
|
descriptor has been released. */
|
|
|
|
__close_nocancel (fd);
|
|
|
|
closed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (closed && __lseek (dirfd, 0, SEEK_SET) < 0)
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
err:
|
|
|
|
__close_nocancel (dirfd);
|
|
|
|
return ret;
|
|
|
|
}
|