Debian Codegraph @

lib/open.c

raw
1 /* Open a descriptor to a file.
2 Copyright (C) 2007-2026 Free Software Foundation, Inc.
3
4 This file is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
8
9 This file 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 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 /* Written by Bruno Haible <[email protected]>, 2007. */
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_open doesn't recurse to
21 rpl_open. */
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 static int
31 orig_open (const char *filename, int flags, mode_t mode)
32 {
33 #if defined _WIN32 && !defined __CYGWIN__
34 return _open (filename, flags, mode);
35 #else
36 return open (filename, flags, mode);
37 #endif
38 }
39
40 /* Specification. */
41 #include <fcntl.h>
42
43 #include "cloexec.h"
44
45 #include <errno.h>
46 #include <stdarg.h>
47 #include <string.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <unistd.h>
51
52 #ifndef HAVE_WORKING_O_DIRECTORY
53 # define HAVE_WORKING_O_DIRECTORY false
54 #endif
55
56 #ifndef OPEN_TRAILING_SLASH_BUG
57 # define OPEN_TRAILING_SLASH_BUG false
58 #endif
59
60 #ifndef REPLACE_OPEN_DIRECTORY
61 # define REPLACE_OPEN_DIRECTORY false
62 #endif
63
64 static int
65 lstatif (char const *filename, struct stat *st, int flags)
66 {
67 return flags & O_NOFOLLOW ? lstat (filename, st) : stat (filename, st);
68 }
69
70 int
71 open (const char *filename, int flags, ...)
72 {
73 mode_t mode = 0;
74
75 if (flags & O_CREAT)
76 {
77 va_list arg;
78 va_start (arg, flags);
79
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);
83
84 va_end (arg);
85 }
86
87 #if GNULIB_defined_O_NONBLOCK
88 /* The only known platform that lacks O_NONBLOCK is mingw, but it
89 also lacks named pipes and Unix sockets, which are the only two
90 file types that require non-blocking handling in open().
91 Therefore, it is safe to ignore O_NONBLOCK here. It is handy
92 that mingw also lacks openat(), so that is also covered here. */
93 flags &= ~O_NONBLOCK;
94 #endif
95
96 #if defined _WIN32 && ! defined __CYGWIN__
97 if (streq (filename, "/dev/null"))
98 filename = "NUL";
99 #endif
100
101 /* Fail if one of O_CREAT, O_WRONLY, O_RDWR is specified and the filename
102 ends in a slash, as POSIX says such a filename must name a directory
103 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>:
104 "A pathname that contains at least one non-<slash> character and that
105 ends with one or more trailing <slash> characters shall not be resolved
106 successfully unless the last pathname component before the trailing
107 <slash> characters names an existing directory"
108 If the named file already exists as a directory, then
109 - if O_CREAT is specified, open() must fail because of the semantics
110 of O_CREAT,
111 - if O_WRONLY or O_RDWR is specified, open() must fail because POSIX
112 <https://pubs.opengroup.org/onlinepubs/9699919799/functions/open.html>
113 says that it fails with errno = EISDIR in this case.
114 If the named file does not exist or does not name a directory, then
115 - if O_CREAT is specified, open() must fail since open() cannot create
116 directories,
117 - if O_WRONLY or O_RDWR is specified, open() must fail because the
118 file does not contain a '.' directory. */
119 bool check_for_slash_bug;
120 if (OPEN_TRAILING_SLASH_BUG)
121 {
122 size_t len = strlen (filename);
123 check_for_slash_bug = len && filename[len - 1] == '/';
124 }
125 else
126 check_for_slash_bug = false;
127
128 if (check_for_slash_bug
129 && (flags & O_CREAT
130 || (flags & O_ACCMODE) == O_RDWR
131 || (flags & O_ACCMODE) == O_WRONLY))
132 {
133 errno = EISDIR;
134 return -1;
135 }
136
137 /* With the trailing slash bug or without working O_DIRECTORY, check with
138 stat first lest we hang trying to open a fifo. Although there is
139 a race between this and opening the file, we can do no better.
140 After opening the file we will check again with fstat. */
141 bool check_directory =
142 (check_for_slash_bug
143 || (!HAVE_WORKING_O_DIRECTORY && flags & O_DIRECTORY));
144 if (check_directory)
145 {
146 struct stat statbuf;
147 if (lstatif (filename, &statbuf, flags) < 0)
148 {
149 if (! (flags & O_CREAT && errno == ENOENT))
150 return -1;
151 }
152 else if (!S_ISDIR (statbuf.st_mode))
153 {
154 errno = ENOTDIR;
155 return -1;
156 }
157 }
158
159 /* 0 = unknown, 1 = yes, -1 = no. */
160 #if GNULIB_defined_O_CLOEXEC
161 int have_cloexec = -1;
162 #else
163 static int have_cloexec;
164 #endif
165
166 int fd = orig_open (filename,
167 flags & ~(have_cloexec < 0 ? O_CLOEXEC : 0), mode);
168
169 if (flags & O_CLOEXEC)
170 {
171 if (! have_cloexec)
172 {
173 if (0 <= fd)
174 have_cloexec = 1;
175 else if (errno == EINVAL)
176 {
177 fd = orig_open (filename, flags & ~O_CLOEXEC, mode);
178 have_cloexec = -1;
179 }
180 }
181 if (have_cloexec < 0 && 0 <= fd)
182 set_cloexec_flag (fd, true);
183 }
184
185
186 #if REPLACE_FCHDIR
187 /* Implementing fchdir and fdopendir requires the ability to open a
188 directory file descriptor. If open doesn't support that (as on
189 mingw), use a dummy file that behaves the same as directories
190 on Linux (ie. always reports EOF on attempts to read()), and
191 override fstat in fchdir.c to hide the dummy. */
192 if (REPLACE_OPEN_DIRECTORY && fd < 0 && errno == EACCES
193 && ((flags & (O_ACCMODE | O_CREAT)) == O_RDONLY
194 || (O_SEARCH != O_RDONLY
195 && (flags & (O_ACCMODE | O_CREAT)) == O_SEARCH)))
196 {
197 struct stat statbuf;
198 if (check_directory
199 || (lstatif (filename, &statbuf, flags) == 0
200 && S_ISDIR (statbuf.st_mode)))
201 {
202 /* Maximum recursion depth of 1. */
203 fd = open ("/dev/null", flags & ~O_DIRECTORY, mode);
204 if (0 <= fd)
205 fd = _gl_register_fd (fd, filename);
206 }
207 else
208 errno = EACCES;
209 }
210 #endif
211
212 /* If checking for directories, fail if fd does not refer to a directory.
213 Rationale: A filename ending in slash cannot name a non-directory
214 <https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13>:
215 "A pathname that contains at least one non-<slash> character and that
216 ends with one or more trailing <slash> characters shall not be resolved
217 successfully unless the last pathname component before the trailing
218 <slash> characters names an existing directory"
219 If the named file without the slash is not a directory, open() must fail
220 with ENOTDIR. */
221 if (check_directory && 0 <= fd)
222 {
223 struct stat statbuf;
224 int r = fstat (fd, &statbuf);
225 if (r < 0 || !S_ISDIR (statbuf.st_mode))
226 {
227 int err = r < 0 ? errno : ENOTDIR;
228 close (fd);
229 errno = err;
230 return -1;
231 }
232 }
233
234 #if REPLACE_FCHDIR
235 if (!REPLACE_OPEN_DIRECTORY && 0 <= fd)
236 fd = _gl_register_fd (fd, filename);
237 #endif
238
239 return fd;
240 }