mirror of
git://git.sv.gnu.org/emacs.git
synced 2025-12-30 09:00:31 -08:00
dc4e6b1329; Update copyright years in more files64b3777631; Run set-copyright from admin.el8e1c56ae46; Add 2024 to copyright years # Conflicts: # doc/misc/modus-themes.org # doc/misc/texinfo.tex # etc/NEWS # etc/refcards/ru-refcard.tex # etc/themes/modus-operandi-theme.el # etc/themes/modus-themes.el # etc/themes/modus-vivendi-theme.el # lib/alloca.in.h # lib/binary-io.h # lib/c-ctype.h # lib/c-strcasecmp.c # lib/c-strncasecmp.c # lib/careadlinkat.c # lib/cloexec.c # lib/close-stream.c # lib/diffseq.h # lib/dup2.c # lib/filemode.h # lib/fpending.c # lib/fpending.h # lib/fsusage.c # lib/getgroups.c # lib/getloadavg.c # lib/gettext.h # lib/gettime.c # lib/gettimeofday.c # lib/group-member.c # lib/malloc.c # lib/md5-stream.c # lib/md5.c # lib/md5.h # lib/memmem.c # lib/memrchr.c # lib/nanosleep.c # lib/save-cwd.h # lib/sha1.c # lib/sig2str.c # lib/stdlib.in.h # lib/strtoimax.c # lib/strtol.c # lib/strtoll.c # lib/time_r.c # lib/xalloc-oversized.h # lisp/auth-source-pass.el # lisp/emacs-lisp/lisp-mnt.el # lisp/emacs-lisp/timer.el # lisp/info-look.el # lisp/jit-lock.el # lisp/loadhist.el # lisp/mail/rmail.el # lisp/net/ntlm.el # lisp/net/webjump.el # lisp/progmodes/asm-mode.el # lisp/progmodes/project.el # lisp/progmodes/sh-script.el # lisp/textmodes/flyspell.el # lisp/textmodes/reftex-toc.el # lisp/textmodes/reftex.el # lisp/textmodes/tex-mode.el # lisp/url/url-gw.el # m4/alloca.m4 # m4/clock_time.m4 # m4/d-type.m4 # m4/dirent_h.m4 # m4/dup2.m4 # m4/euidaccess.m4 # m4/fchmodat.m4 # m4/filemode.m4 # m4/fsusage.m4 # m4/getgroups.m4 # m4/getloadavg.m4 # m4/getrandom.m4 # m4/gettime.m4 # m4/gettimeofday.m4 # m4/gnulib-common.m4 # m4/group-member.m4 # m4/inttypes.m4 # m4/malloc.m4 # m4/manywarnings.m4 # m4/mempcpy.m4 # m4/memrchr.m4 # m4/mkostemp.m4 # m4/mktime.m4 # m4/nproc.m4 # m4/nstrftime.m4 # m4/pathmax.m4 # m4/pipe2.m4 # m4/pselect.m4 # m4/pthread_sigmask.m4 # m4/readlink.m4 # m4/realloc.m4 # m4/sig2str.m4 # m4/ssize_t.m4 # m4/stat-time.m4 # m4/stddef_h.m4 # m4/stdint.m4 # m4/stdio_h.m4 # m4/stdlib_h.m4 # m4/stpcpy.m4 # m4/strnlen.m4 # m4/strtoimax.m4 # m4/strtoll.m4 # m4/time_h.m4 # m4/timegm.m4 # m4/timer_time.m4 # m4/timespec.m4 # m4/unistd_h.m4 # m4/warnings.m4 # nt/configure.bat # nt/preprep.c # test/lisp/register-tests.el
274 lines
7.1 KiB
C
274 lines
7.1 KiB
C
/* provide a replacement fdopendir function
|
|
Copyright (C) 2004-2024 Free Software Foundation, Inc.
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
|
|
|
/* written by Jim Meyering */
|
|
|
|
#include <config.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
#if !HAVE_FDOPENDIR
|
|
|
|
# if GNULIB_defined_DIR
|
|
/* We are in control of the file descriptor of a DIR. */
|
|
|
|
# include "dirent-private.h"
|
|
|
|
# if !REPLACE_FCHDIR
|
|
# error "unexpected configuration: GNULIB_defined_DIR but fchdir not replaced"
|
|
# endif
|
|
|
|
DIR *
|
|
fdopendir (int fd)
|
|
{
|
|
char const *name = _gl_directory_name (fd);
|
|
DIR *dirp = name ? opendir (name) : NULL;
|
|
if (dirp != NULL)
|
|
dirp->fd_to_close = fd;
|
|
return dirp;
|
|
}
|
|
|
|
# elif defined __KLIBC__
|
|
|
|
# include <InnoTekLIBC/backend.h>
|
|
|
|
DIR *
|
|
fdopendir (int fd)
|
|
{
|
|
char path[_MAX_PATH];
|
|
DIR *dirp;
|
|
|
|
/* Get a path from fd */
|
|
if (__libc_Back_ioFHToPath (fd, path, sizeof (path)))
|
|
return NULL;
|
|
|
|
dirp = opendir (path);
|
|
if (!dirp)
|
|
return NULL;
|
|
|
|
/* Unregister fd registered by opendir() */
|
|
_gl_unregister_dirp_fd (dirfd (dirp));
|
|
|
|
/* Register our fd */
|
|
if (_gl_register_dirp_fd (fd, dirp))
|
|
{
|
|
int saved_errno = errno;
|
|
|
|
closedir (dirp);
|
|
|
|
errno = saved_errno;
|
|
|
|
dirp = NULL;
|
|
}
|
|
|
|
return dirp;
|
|
}
|
|
|
|
# else
|
|
/* We are not in control of the file descriptor of a DIR, and therefore have to
|
|
play tricks with file descriptors before and after a call to opendir(). */
|
|
|
|
# include "openat.h"
|
|
# include "openat-priv.h"
|
|
# include "save-cwd.h"
|
|
|
|
# if GNULIB_DIRENT_SAFER
|
|
# include "dirent--.h"
|
|
# endif
|
|
|
|
# ifndef REPLACE_FCHDIR
|
|
# define REPLACE_FCHDIR 0
|
|
# endif
|
|
|
|
static DIR *fdopendir_with_dup (int, int, struct saved_cwd const *);
|
|
static DIR *fd_clone_opendir (int, struct saved_cwd const *);
|
|
|
|
/* Replacement for POSIX fdopendir.
|
|
|
|
First, try to simulate it via opendir ("/proc/self/fd/..."). Failing
|
|
that, simulate it by using fchdir metadata, or by doing
|
|
save_cwd/fchdir/opendir(".")/restore_cwd.
|
|
If either the save_cwd or the restore_cwd fails (relatively unlikely),
|
|
then give a diagnostic and exit nonzero.
|
|
|
|
If successful, the resulting stream is based on FD in
|
|
implementations where streams are based on file descriptors and in
|
|
applications where no other thread or signal handler allocates or
|
|
frees file descriptors. In other cases, consult dirfd on the result
|
|
to find out whether FD is still being used.
|
|
|
|
Otherwise, this function works just like POSIX fdopendir.
|
|
|
|
W A R N I N G:
|
|
|
|
Unlike other fd-related functions, this one places constraints on FD.
|
|
If this function returns successfully, FD is under control of the
|
|
dirent.h system, and the caller should not close or modify the state of
|
|
FD other than by the dirent.h functions. */
|
|
DIR *
|
|
fdopendir (int fd)
|
|
{
|
|
DIR *dir = fdopendir_with_dup (fd, -1, NULL);
|
|
|
|
if (! REPLACE_FCHDIR && ! dir)
|
|
{
|
|
int saved_errno = errno;
|
|
if (EXPECTED_ERRNO (saved_errno))
|
|
{
|
|
struct saved_cwd cwd;
|
|
if (save_cwd (&cwd) != 0)
|
|
openat_save_fail (errno);
|
|
dir = fdopendir_with_dup (fd, -1, &cwd);
|
|
saved_errno = errno;
|
|
free_cwd (&cwd);
|
|
errno = saved_errno;
|
|
}
|
|
}
|
|
|
|
return dir;
|
|
}
|
|
|
|
/* Like fdopendir, except that if OLDER_DUPFD is not -1, it is known
|
|
to be a dup of FD which is less than FD - 1 and which will be
|
|
closed by the caller and not otherwise used by the caller. This
|
|
function makes sure that FD is closed and all file descriptors less
|
|
than FD are open, and then calls fd_clone_opendir on a dup of FD.
|
|
That way, barring race conditions, fd_clone_opendir returns a
|
|
stream whose file descriptor is FD.
|
|
|
|
If REPLACE_FCHDIR or CWD is null, use opendir ("/proc/self/fd/...",
|
|
falling back on fchdir metadata. Otherwise, CWD is a saved version
|
|
of the working directory; use fchdir/opendir(".")/restore_cwd(CWD). */
|
|
static DIR *
|
|
fdopendir_with_dup (int fd, int older_dupfd, struct saved_cwd const *cwd)
|
|
{
|
|
int dupfd = dup (fd);
|
|
if (dupfd < 0 && errno == EMFILE)
|
|
dupfd = older_dupfd;
|
|
if (dupfd < 0)
|
|
return NULL;
|
|
else
|
|
{
|
|
DIR *dir;
|
|
int saved_errno;
|
|
if (dupfd < fd - 1 && dupfd != older_dupfd)
|
|
{
|
|
dir = fdopendir_with_dup (fd, dupfd, cwd);
|
|
saved_errno = errno;
|
|
}
|
|
else
|
|
{
|
|
close (fd);
|
|
dir = fd_clone_opendir (dupfd, cwd);
|
|
saved_errno = errno;
|
|
if (! dir)
|
|
{
|
|
int fd1 = dup (dupfd);
|
|
if (fd1 != fd)
|
|
openat_save_fail (fd1 < 0 ? errno : EBADF);
|
|
}
|
|
}
|
|
|
|
if (dupfd != older_dupfd)
|
|
close (dupfd);
|
|
errno = saved_errno;
|
|
return dir;
|
|
}
|
|
}
|
|
|
|
/* Like fdopendir, except the result controls a clone of FD. It is
|
|
the caller's responsibility both to close FD and (if the result is
|
|
not null) to closedir the result. */
|
|
static DIR *
|
|
fd_clone_opendir (int fd, struct saved_cwd const *cwd)
|
|
{
|
|
if (REPLACE_FCHDIR || ! cwd)
|
|
{
|
|
DIR *dir = NULL;
|
|
int saved_errno = EOPNOTSUPP;
|
|
char buf[OPENAT_BUFFER_SIZE];
|
|
char *proc_file = openat_proc_name (buf, fd, ".");
|
|
if (proc_file)
|
|
{
|
|
dir = opendir (proc_file);
|
|
saved_errno = errno;
|
|
if (proc_file != buf)
|
|
free (proc_file);
|
|
}
|
|
# if REPLACE_FCHDIR
|
|
if (! dir && EXPECTED_ERRNO (saved_errno))
|
|
{
|
|
char const *name = _gl_directory_name (fd);
|
|
DIR *dp = name ? opendir (name) : NULL;
|
|
|
|
/* The caller has done an elaborate dance to arrange for opendir to
|
|
consume just the right file descriptor. If dirfd returns -1,
|
|
though, we're on a system like mingw where opendir does not
|
|
consume a file descriptor. Consume it via 'dup' instead. */
|
|
if (dp && dirfd (dp) < 0)
|
|
dup (fd);
|
|
|
|
return dp;
|
|
}
|
|
# endif
|
|
errno = saved_errno;
|
|
return dir;
|
|
}
|
|
else
|
|
{
|
|
if (fchdir (fd) != 0)
|
|
return NULL;
|
|
else
|
|
{
|
|
DIR *dir = opendir (".");
|
|
int saved_errno = errno;
|
|
if (restore_cwd (cwd) != 0)
|
|
openat_restore_fail (errno);
|
|
errno = saved_errno;
|
|
return dir;
|
|
}
|
|
}
|
|
}
|
|
|
|
# endif
|
|
|
|
#else /* HAVE_FDOPENDIR */
|
|
|
|
# include <errno.h>
|
|
# include <sys/stat.h>
|
|
|
|
# undef fdopendir
|
|
|
|
/* Like fdopendir, but work around GNU/Hurd bug by validating FD. */
|
|
|
|
DIR *
|
|
rpl_fdopendir (int fd)
|
|
{
|
|
struct stat st;
|
|
if (fstat (fd, &st))
|
|
return NULL;
|
|
if (!S_ISDIR (st.st_mode))
|
|
{
|
|
errno = ENOTDIR;
|
|
return NULL;
|
|
}
|
|
return fdopendir (fd);
|
|
}
|
|
|
|
#endif /* HAVE_FDOPENDIR */
|