1 /* provide a replacement fdopendir function
2 Copyright (C) 2004-2013 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* written by Jim Meyering */
29 # include "openat-priv.h"
30 # include "save-cwd.h"
32 # if GNULIB_DIRENT_SAFER
33 # include "dirent--.h"
36 # ifndef REPLACE_FCHDIR
37 # define REPLACE_FCHDIR 0
40 static DIR *fdopendir_with_dup (int, int, struct saved_cwd
const *);
41 static DIR *fd_clone_opendir (int, struct saved_cwd
const *);
43 /* Replacement for POSIX fdopendir.
45 First, try to simulate it via opendir ("/proc/self/fd/..."). Failing
46 that, simulate it by using fchdir metadata, or by doing
47 save_cwd/fchdir/opendir(".")/restore_cwd.
48 If either the save_cwd or the restore_cwd fails (relatively unlikely),
49 then give a diagnostic and exit nonzero.
51 If successful, the resulting stream is based on FD in
52 implementations where streams are based on file descriptors and in
53 applications where no other thread or signal handler allocates or
54 frees file descriptors. In other cases, consult dirfd on the result
55 to find out whether FD is still being used.
57 Otherwise, this function works just like POSIX fdopendir.
61 Unlike other fd-related functions, this one places constraints on FD.
62 If this function returns successfully, FD is under control of the
63 dirent.h system, and the caller should not close or modify the state of
64 FD other than by the dirent.h functions. */
68 DIR *dir
= fdopendir_with_dup (fd
, -1, NULL
);
70 if (! REPLACE_FCHDIR
&& ! dir
)
72 int saved_errno
= errno
;
73 if (EXPECTED_ERRNO (saved_errno
))
76 if (save_cwd (&cwd
) != 0)
77 openat_save_fail (errno
);
78 dir
= fdopendir_with_dup (fd
, -1, &cwd
);
88 /* Like fdopendir, except that if OLDER_DUPFD is not -1, it is known
89 to be a dup of FD which is less than FD - 1 and which will be
90 closed by the caller and not otherwise used by the caller. This
91 function makes sure that FD is closed and all file descriptors less
92 than FD are open, and then calls fd_clone_opendir on a dup of FD.
93 That way, barring race conditions, fd_clone_opendir returns a
94 stream whose file descriptor is FD.
96 If REPLACE_CHDIR or CWD is null, use opendir ("/proc/self/fd/...",
97 falling back on fchdir metadata. Otherwise, CWD is a saved version
98 of the working directory; use fchdir/opendir(".")/restore_cwd(CWD). */
100 fdopendir_with_dup (int fd
, int older_dupfd
, struct saved_cwd
const *cwd
)
102 int dupfd
= dup (fd
);
103 if (dupfd
< 0 && errno
== EMFILE
)
111 if (dupfd
< fd
- 1 && dupfd
!= older_dupfd
)
113 dir
= fdopendir_with_dup (fd
, dupfd
, cwd
);
119 dir
= fd_clone_opendir (dupfd
, cwd
);
123 int fd1
= dup (dupfd
);
125 openat_save_fail (fd1
< 0 ? errno
: EBADF
);
129 if (dupfd
!= older_dupfd
)
136 /* Like fdopendir, except the result controls a clone of FD. It is
137 the caller's responsibility both to close FD and (if the result is
138 not null) to closedir the result. */
140 fd_clone_opendir (int fd
, struct saved_cwd
const *cwd
)
142 if (REPLACE_FCHDIR
|| ! cwd
)
145 int saved_errno
= EOPNOTSUPP
;
146 char buf
[OPENAT_BUFFER_SIZE
];
147 char *proc_file
= openat_proc_name (buf
, fd
, ".");
150 dir
= opendir (proc_file
);
152 if (proc_file
!= buf
)
156 if (! dir
&& EXPECTED_ERRNO (saved_errno
))
158 char const *name
= _gl_directory_name (fd
);
159 return (name
? opendir (name
) : NULL
);
167 if (fchdir (fd
) != 0)
171 DIR *dir
= opendir (".");
172 int saved_errno
= errno
;
173 if (restore_cwd (cwd
) != 0)
174 openat_restore_fail (errno
);
181 #else /* HAVE_FDOPENDIR */
184 # include <sys/stat.h>
188 /* Like fdopendir, but work around GNU/Hurd bug by validating FD. */
191 rpl_fdopendir (int fd
)
196 if (!S_ISDIR (st
.st_mode
))
201 return fdopendir (fd
);
204 #endif /* HAVE_FDOPENDIR */