1
0
mirror of https://sourceware.org/git/glibc.git synced 2025-07-29 11:41:21 +03:00

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] 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
This commit is contained in:
Adhemerval Zanella
2021-03-10 12:26:32 -03:00
parent 286286283e
commit 607449506f
44 changed files with 380 additions and 2 deletions

View File

@ -0,0 +1,97 @@
/* 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
that fall on the criteria. */
_Bool
__closefrom_fallback (int from)
{
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. */
if (errno == ENOENT)
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;
}