exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / openat.c
blob65cfc5ede42214447681347b03fac57bbc21d3d8
1 /* provide a replacement openat function
2 Copyright (C) 2004-2024 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 <https://www.gnu.org/licenses/>. */
17 /* written by Jim Meyering */
19 /* If the user's config.h happens to include <fcntl.h>, let it include only
20 the system's <fcntl.h> here, so that orig_openat doesn't recurse to
21 rpl_openat. */
22 #define __need_system_fcntl_h
23 #include <config.h>
25 /* Get the original definition of open. It might be defined as a macro. */
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #undef __need_system_fcntl_h
30 #if HAVE_OPENAT
31 static int
32 orig_openat (int fd, char const *filename, int flags, mode_t mode)
34 return openat (fd, filename, flags, mode);
36 #endif
38 #ifdef __osf__
39 /* Write "fcntl.h" here, not <fcntl.h>, otherwise OSF/1 5.1 DTK cc eliminates
40 this include because of the preliminary #include <fcntl.h> above. */
41 # include "fcntl.h"
42 #else
43 # include <fcntl.h>
44 #endif
46 #include "openat.h"
48 #include "cloexec.h"
50 #include <stdarg.h>
51 #include <stddef.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <sys/stat.h>
55 #include <errno.h>
57 #if HAVE_OPENAT
59 /* Like openat, but support O_CLOEXEC and work around Solaris 9 bugs
60 with trailing slash. */
61 int
62 rpl_openat (int dfd, char const *filename, int flags, ...)
64 /* 0 = unknown, 1 = yes, -1 = no. */
65 #if GNULIB_defined_O_CLOEXEC
66 int have_cloexec = -1;
67 #else
68 static int have_cloexec;
69 #endif
71 mode_t mode;
72 int fd;
74 mode = 0;
75 if (flags & O_CREAT)
77 va_list arg;
78 va_start (arg, flags);
80 /* We have to use PROMOTED_MODE_T instead of mode_t, otherwise GCC 4
81 creates crashing code when 'mode_t' is smaller than 'int'. */
82 mode = va_arg (arg, PROMOTED_MODE_T);
84 va_end (arg);
87 # if OPEN_TRAILING_SLASH_BUG
88 /* Fail if one of O_CREAT, O_WRONLY, O_RDWR is specified and the filename
89 ends in a slash, as POSIX says such a filename must name a directory
90 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>:
91 "A pathname that contains at least one non-<slash> character and that
92 ends with one or more trailing <slash> characters shall not be resolved
93 successfully unless the last pathname component before the trailing
94 <slash> characters names an existing directory"
95 If the named file already exists as a directory, then
96 - if O_CREAT is specified, open() must fail because of the semantics
97 of O_CREAT,
98 - if O_WRONLY or O_RDWR is specified, open() must fail because POSIX
99 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/openat.html>
100 says that it fails with errno = EISDIR in this case.
101 If the named file does not exist or does not name a directory, then
102 - if O_CREAT is specified, open() must fail since open() cannot create
103 directories,
104 - if O_WRONLY or O_RDWR is specified, open() must fail because the
105 file does not contain a '.' directory. */
106 if ((flags & O_CREAT)
107 || (flags & O_ACCMODE) == O_RDWR
108 || (flags & O_ACCMODE) == O_WRONLY)
110 size_t len = strlen (filename);
111 if (len > 0 && filename[len - 1] == '/')
113 errno = EISDIR;
114 return -1;
117 # endif
119 fd = orig_openat (dfd, filename,
120 flags & ~(have_cloexec < 0 ? O_CLOEXEC : 0), mode);
122 if (flags & O_CLOEXEC)
124 if (! have_cloexec)
126 if (0 <= fd)
127 have_cloexec = 1;
128 else if (errno == EINVAL)
130 fd = orig_openat (dfd, filename, flags & ~O_CLOEXEC, mode);
131 have_cloexec = -1;
134 if (have_cloexec < 0 && 0 <= fd)
135 set_cloexec_flag (fd, true);
139 # if OPEN_TRAILING_SLASH_BUG
140 /* If the filename ends in a slash and fd does not refer to a directory,
141 then fail.
142 Rationale: POSIX says such a filename must name a directory
143 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>:
144 "A pathname that contains at least one non-<slash> character and that
145 ends with one or more trailing <slash> characters shall not be resolved
146 successfully unless the last pathname component before the trailing
147 <slash> characters names an existing directory"
148 If the named file without the slash is not a directory, open() must fail
149 with ENOTDIR. */
150 if (fd >= 0)
152 /* We know len is positive, since open did not fail with ENOENT. */
153 size_t len = strlen (filename);
154 if (filename[len - 1] == '/')
156 struct stat statbuf;
158 if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode))
160 close (fd);
161 errno = ENOTDIR;
162 return -1;
166 # endif
168 return fd;
171 #else /* !HAVE_OPENAT */
173 # include "filename.h" /* solely for definition of IS_ABSOLUTE_FILE_NAME */
174 # include "openat-priv.h"
175 # include "save-cwd.h"
177 /* Replacement for Solaris' openat function.
178 <https://www.google.com/search?q=openat+site:docs.oracle.com>
179 First, try to simulate it via open ("/proc/self/fd/FD/FILE").
180 Failing that, simulate it by doing save_cwd/fchdir/open/restore_cwd.
181 If either the save_cwd or the restore_cwd fails (relatively unlikely),
182 then give a diagnostic and exit nonzero.
183 Otherwise, upon failure, set errno and return -1, as openat does.
184 Upon successful completion, return a file descriptor. */
186 openat (int fd, char const *file, int flags, ...)
188 mode_t mode = 0;
190 if (flags & O_CREAT)
192 va_list arg;
193 va_start (arg, flags);
195 /* We have to use PROMOTED_MODE_T instead of mode_t, otherwise GCC 4
196 creates crashing code when 'mode_t' is smaller than 'int'. */
197 mode = va_arg (arg, PROMOTED_MODE_T);
199 va_end (arg);
202 return openat_permissive (fd, file, flags, mode, NULL);
205 /* Like openat (FD, FILE, FLAGS, MODE), but if CWD_ERRNO is
206 nonnull, set *CWD_ERRNO to an errno value if unable to save
207 or restore the initial working directory. This is needed only
208 the first time remove.c's remove_dir opens a command-line
209 directory argument.
211 If a previous attempt to restore the current working directory
212 failed, then we must not even try to access a '.'-relative name.
213 It is the caller's responsibility not to call this function
214 in that case. */
217 openat_permissive (int fd, char const *file, int flags, mode_t mode,
218 int *cwd_errno)
220 struct saved_cwd saved_cwd;
221 int saved_errno;
222 int err;
223 bool save_ok;
225 if (fd == AT_FDCWD || IS_ABSOLUTE_FILE_NAME (file))
226 return open (file, flags, mode);
229 char buf[OPENAT_BUFFER_SIZE];
230 char *proc_file = openat_proc_name (buf, fd, file);
231 if (proc_file)
233 int open_result = open (proc_file, flags, mode);
234 int open_errno = errno;
235 if (proc_file != buf)
236 free (proc_file);
237 /* If the syscall succeeds, or if it fails with an unexpected
238 errno value, then return right away. Otherwise, fall through
239 and resort to using save_cwd/restore_cwd. */
240 if (0 <= open_result || ! EXPECTED_ERRNO (open_errno))
242 errno = open_errno;
243 return open_result;
248 save_ok = (save_cwd (&saved_cwd) == 0);
249 if (! save_ok)
251 if (! cwd_errno)
252 openat_save_fail (errno);
253 *cwd_errno = errno;
255 if (0 <= fd && fd == saved_cwd.desc)
257 /* If saving the working directory collides with the user's
258 requested fd, then the user's fd must have been closed to
259 begin with. */
260 free_cwd (&saved_cwd);
261 errno = EBADF;
262 return -1;
265 err = fchdir (fd);
266 saved_errno = errno;
268 if (! err)
270 err = open (file, flags, mode);
271 saved_errno = errno;
272 if (save_ok && restore_cwd (&saved_cwd) != 0)
274 if (! cwd_errno)
276 /* Don't write a message to just-created fd 2. */
277 saved_errno = errno;
278 if (err == STDERR_FILENO)
279 close (err);
280 openat_restore_fail (saved_errno);
282 *cwd_errno = errno;
286 free_cwd (&saved_cwd);
287 errno = saved_errno;
288 return err;
291 /* Return true if our openat implementation must resort to
292 using save_cwd and restore_cwd. */
293 bool
294 openat_needs_fchdir (void)
296 bool needs_fchdir = true;
297 int fd = open ("/", O_SEARCH | O_CLOEXEC);
299 if (0 <= fd)
301 char buf[OPENAT_BUFFER_SIZE];
302 char *proc_file = openat_proc_name (buf, fd, ".");
303 if (proc_file)
305 needs_fchdir = false;
306 if (proc_file != buf)
307 free (proc_file);
309 close (fd);
312 return needs_fchdir;
315 #endif /* !HAVE_OPENAT */