Debian Codegraph @

lib/openat.c

raw
1 /* provide a replacement openat function
2 Copyright (C) 2004-2025 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 /* written by Jim Meyering */
18
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>
24
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
29
30 #if HAVE_OPENAT
31 static int
32 orig_openat (int fd, char const *filename, int flags, mode_t mode)
33 {
34 return openat (fd, filename, flags, mode);
35 }
36 #endif
37
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
45
46 #include "openat.h"
47
48 #include "cloexec.h"
49
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>
56
57 #if HAVE_OPENAT
58
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, ...)
63 {
64 mode_t mode = 0;
65
66 if (flags & O_CREAT)
67 {
68 va_list arg;
69 va_start (arg, flags);
70
71 /* We have to use PROMOTED_MODE_T instead of mode_t, otherwise GCC 4
72 creates crashing code when 'mode_t' is smaller than 'int'. */
73 mode = va_arg (arg, PROMOTED_MODE_T);
74
75 va_end (arg);
76 }
77
78 # if OPEN_TRAILING_SLASH_BUG
79 /* Fail if one of O_CREAT, O_WRONLY, O_RDWR is specified and the filename
80 ends in a slash, as POSIX says such a filename must name a directory
81 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>:
82 "A pathname that contains at least one non-<slash> character and that
83 ends with one or more trailing <slash> characters shall not be resolved
84 successfully unless the last pathname component before the trailing
85 <slash> characters names an existing directory"
86 If the named file already exists as a directory, then
87 - if O_CREAT is specified, open() must fail because of the semantics
88 of O_CREAT,
89 - if O_WRONLY or O_RDWR is specified, open() must fail because POSIX
90 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/openat.html>
91 says that it fails with errno = EISDIR in this case.
92 If the named file does not exist or does not name a directory, then
93 - if O_CREAT is specified, open() must fail since open() cannot create
94 directories,
95 - if O_WRONLY or O_RDWR is specified, open() must fail because the
96 file does not contain a '.' directory. */
97 if ((flags & O_CREAT)
98 || (flags & O_ACCMODE) == O_RDWR
99 || (flags & O_ACCMODE) == O_WRONLY)
100 {
101 size_t len = strlen (filename);
102 if (len > 0 && filename[len - 1] == '/')
103 {
104 errno = EISDIR;
105 return -1;
106 }
107 }
108 # endif
109
110 /* 0 = unknown, 1 = yes, -1 = no. */
111 #if GNULIB_defined_O_CLOEXEC
112 int have_cloexec = -1;
113 #else
114 static int have_cloexec;
115 #endif
116
117 int fd = orig_openat (dfd, filename,
118 flags & ~(have_cloexec < 0 ? O_CLOEXEC : 0), mode);
119
120 if (flags & O_CLOEXEC)
121 {
122 if (! have_cloexec)
123 {
124 if (0 <= fd)
125 have_cloexec = 1;
126 else if (errno == EINVAL)
127 {
128 fd = orig_openat (dfd, filename, flags & ~O_CLOEXEC, mode);
129 have_cloexec = -1;
130 }
131 }
132 if (have_cloexec < 0 && 0 <= fd)
133 set_cloexec_flag (fd, true);
134 }
135
136
137 # if OPEN_TRAILING_SLASH_BUG
138 /* If the filename ends in a slash and fd does not refer to a directory,
139 then fail.
140 Rationale: POSIX says such a filename must name a directory
141 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>:
142 "A pathname that contains at least one non-<slash> character and that
143 ends with one or more trailing <slash> characters shall not be resolved
144 successfully unless the last pathname component before the trailing
145 <slash> characters names an existing directory"
146 If the named file without the slash is not a directory, open() must fail
147 with ENOTDIR. */
148 if (fd >= 0)
149 {
150 /* We know len is positive, since open did not fail with ENOENT. */
151 size_t len = strlen (filename);
152 if (filename[len - 1] == '/')
153 {
154 struct stat statbuf;
155
156 if (fstat (fd, &statbuf) >= 0 && !S_ISDIR (statbuf.st_mode))
157 {
158 close (fd);
159 errno = ENOTDIR;
160 return -1;
161 }
162 }
163 }
164 # endif
165
166 return fd;
167 }
168
169 #else /* !HAVE_OPENAT */
170
171 # include "filename.h" /* solely for definition of IS_ABSOLUTE_FILE_NAME */
172 # include "openat-priv.h"
173 # include "save-cwd.h"
174
175 /* Replacement for Solaris' openat function.
176 <https://www.google.com/search?q=openat+site:docs.oracle.com>
177 First, try to simulate it via open ("/proc/self/fd/FD/FILE").
178 Failing that, simulate it by doing save_cwd/fchdir/open/restore_cwd.
179 If either the save_cwd or the restore_cwd fails (relatively unlikely),
180 then give a diagnostic and exit nonzero.
181 Otherwise, upon failure, set errno and return -1, as openat does.
182 Upon successful completion, return a file descriptor. */
183 int
184 openat (int fd, char const *file, int flags, ...)
185 {
186 mode_t mode = 0;
187
188 if (flags & O_CREAT)
189 {
190 va_list arg;
191 va_start (arg, flags);
192
193 /* We have to use PROMOTED_MODE_T instead of mode_t, otherwise GCC 4
194 creates crashing code when 'mode_t' is smaller than 'int'. */
195 mode = va_arg (arg, PROMOTED_MODE_T);
196
197 va_end (arg);
198 }
199
200 return openat_permissive (fd, file, flags, mode, NULL);
201 }
202
203 /* Like openat (FD, FILE, FLAGS, MODE), but if CWD_ERRNO is
204 nonnull, set *CWD_ERRNO to an errno value if unable to save
205 or restore the initial working directory. This is needed only
206 the first time remove.c's remove_dir opens a command-line
207 directory argument.
208
209 If a previous attempt to restore the current working directory
210 failed, then we must not even try to access a '.'-relative name.
211 It is the caller's responsibility not to call this function
212 in that case. */
213
214 int
215 openat_permissive (int fd, char const *file, int flags, mode_t mode,
216 int *cwd_errno)
217 {
218 if (fd == AT_FDCWD || IS_ABSOLUTE_FILE_NAME (file))
219 return open (file, flags, mode);
220
221 {
222 char buf[OPENAT_BUFFER_SIZE];
223 char *proc_file = openat_proc_name (buf, fd, file);
224 if (proc_file)
225 {
226 int open_result = open (proc_file, flags, mode);
227 int open_errno = errno;
228 if (proc_file != buf)
229 free (proc_file);
230 /* If the syscall succeeds, or if it fails with an unexpected
231 errno value, then return right away. Otherwise, fall through
232 and resort to using save_cwd/restore_cwd. */
233 if (0 <= open_result || ! EXPECTED_ERRNO (open_errno))
234 {
235 errno = open_errno;
236 return open_result;
237 }
238 }
239 }
240
241 struct saved_cwd saved_cwd;
242 int save_failed = save_cwd (&saved_cwd) < 0 ? errno : 0;
243
244 /* If save_cwd allocated a descriptor DFD other than FD, do another
245 save_cwd and then close DFD, so that the later open (if successful)
246 returns DFD (the lowest-numbered descriptor) as POSIX requires. */
247 int dfd = saved_cwd.desc;
248 if (0 <= dfd && dfd != fd)
249 {
250 save_failed = save_cwd (&saved_cwd) < 0 ? errno : 0;
251 close (dfd);
252 dfd = saved_cwd.desc;
253 }
254
255 /* If saving the working directory collides with the user's requested fd,
256 then the user's fd must have been closed to begin with. */
257 if (0 <= dfd && dfd == fd)
258 {
259 free_cwd (&saved_cwd);
260 errno = EBADF;
261 return -1;
262 }
263
264 if (save_failed)
265 {
266 if (! cwd_errno)
267 openat_save_fail (save_failed);
268 *cwd_errno = save_failed;
269 }
270
271 int err = fchdir (fd);
272 int saved_errno = errno;
273
274 if (0 <= err)
275 {
276 err = open (file, flags, mode);
277 saved_errno = errno;
278 if (!save_failed && restore_cwd (&saved_cwd) < 0)
279 {
280 int restore_cwd_errno = errno;
281 if (! cwd_errno)
282 {
283 /* Do not leak ERR. This also stops openat_restore_fail
284 from messing up if ERR happens to equal STDERR_FILENO. */
285 if (0 <= err)
286 close (err);
287 openat_restore_fail (restore_cwd_errno);
288 }
289 *cwd_errno = restore_cwd_errno;
290 }
291 }
292
293 free_cwd (&saved_cwd);
294 errno = saved_errno;
295 return err;
296 }
297
298 /* Return true if our openat implementation must resort to
299 using save_cwd and restore_cwd. */
300 bool
301 openat_needs_fchdir (void)
302 {
303 bool needs_fchdir = true;
304 int fd = open ("/", O_SEARCH | O_CLOEXEC);
305
306 if (0 <= fd)
307 {
308 char buf[OPENAT_BUFFER_SIZE];
309 char *proc_file = openat_proc_name (buf, fd, ".");
310 if (proc_file)
311 {
312 needs_fchdir = false;
313 if (proc_file != buf)
314 free (proc_file);
315 }
316 close (fd);
317 }
318
319 return needs_fchdir;
320 }
321
322 #endif /* !HAVE_OPENAT */