Commit | Line | Data |
---|---|---|
614c279d | 1 | /* Low-level file-handling. |
e2882c85 | 2 | Copyright (C) 2012-2018 Free Software Foundation, Inc. |
614c279d TT |
3 | |
4 | This file is part of GDB. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 3 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
18 | ||
727605ca | 19 | #include "common-defs.h" |
614c279d TT |
20 | #include "filestuff.h" |
21 | #include "gdb_vecs.h" | |
614c279d TT |
22 | #include <fcntl.h> |
23 | #include <unistd.h> | |
614c279d | 24 | #include <sys/types.h> |
53ce3c39 | 25 | #include <sys/stat.h> |
37269bc9 | 26 | #include <algorithm> |
614c279d | 27 | |
5d71132c TT |
28 | #ifdef USE_WIN32API |
29 | #include <winsock2.h> | |
30 | #include <windows.h> | |
8658d16d PA |
31 | #define HAVE_SOCKETS 1 |
32 | #elif defined HAVE_SYS_SOCKET_H | |
5d71132c TT |
33 | #include <sys/socket.h> |
34 | /* Define HAVE_F_GETFD if we plan to use F_GETFD. */ | |
35 | #define HAVE_F_GETFD F_GETFD | |
8658d16d | 36 | #define HAVE_SOCKETS 1 |
5d71132c TT |
37 | #endif |
38 | ||
614c279d TT |
39 | #ifdef HAVE_SYS_RESOURCE_H |
40 | #include <sys/resource.h> | |
41 | #endif /* HAVE_SYS_RESOURCE_H */ | |
42 | ||
43 | #ifndef O_CLOEXEC | |
44 | #define O_CLOEXEC 0 | |
45 | #endif | |
46 | ||
2fa1b319 EZ |
47 | #ifndef O_NOINHERIT |
48 | #define O_NOINHERIT 0 | |
49 | #endif | |
50 | ||
614c279d TT |
51 | #ifndef SOCK_CLOEXEC |
52 | #define SOCK_CLOEXEC 0 | |
53 | #endif | |
54 | ||
55 | \f | |
56 | ||
57 | #ifndef HAVE_FDWALK | |
58 | ||
2978b111 | 59 | #include <dirent.h> |
614c279d TT |
60 | |
61 | /* Replacement for fdwalk, if the system doesn't define it. Walks all | |
62 | open file descriptors (though this implementation may walk closed | |
63 | ones as well, depending on the host platform's capabilities) and | |
64 | call FUNC with ARG. If FUNC returns non-zero, stops immediately | |
65 | and returns the same value. Otherwise, returns zero when | |
66 | finished. */ | |
67 | ||
68 | static int | |
69 | fdwalk (int (*func) (void *, int), void *arg) | |
70 | { | |
71 | /* Checking __linux__ isn't great but it isn't clear what would be | |
72 | better. There doesn't seem to be a good way to check for this in | |
73 | configure. */ | |
74 | #ifdef __linux__ | |
75 | DIR *dir; | |
76 | ||
77 | dir = opendir ("/proc/self/fd"); | |
78 | if (dir != NULL) | |
79 | { | |
80 | struct dirent *entry; | |
81 | int result = 0; | |
82 | ||
83 | for (entry = readdir (dir); entry != NULL; entry = readdir (dir)) | |
84 | { | |
85 | long fd; | |
86 | char *tail; | |
614c279d TT |
87 | |
88 | errno = 0; | |
89 | fd = strtol (entry->d_name, &tail, 10); | |
90 | if (*tail != '\0' || errno != 0) | |
91 | continue; | |
92 | if ((int) fd != fd) | |
93 | { | |
94 | /* What can we do here really? */ | |
95 | continue; | |
96 | } | |
97 | ||
98 | if (fd == dirfd (dir)) | |
99 | continue; | |
100 | ||
101 | result = func (arg, fd); | |
102 | if (result != 0) | |
103 | break; | |
104 | } | |
105 | ||
106 | closedir (dir); | |
107 | return result; | |
108 | } | |
109 | /* We may fall through to the next case. */ | |
110 | #endif | |
111 | ||
112 | { | |
113 | int max, fd; | |
114 | ||
f9b0da3d | 115 | #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) |
614c279d TT |
116 | struct rlimit rlim; |
117 | ||
118 | if (getrlimit (RLIMIT_NOFILE, &rlim) == 0 && rlim.rlim_max != RLIM_INFINITY) | |
119 | max = rlim.rlim_max; | |
120 | else | |
121 | #endif | |
122 | { | |
123 | #ifdef _SC_OPEN_MAX | |
124 | max = sysconf (_SC_OPEN_MAX); | |
125 | #else | |
126 | /* Whoops. */ | |
127 | return 0; | |
128 | #endif /* _SC_OPEN_MAX */ | |
129 | } | |
130 | ||
131 | for (fd = 0; fd < max; ++fd) | |
132 | { | |
133 | struct stat sb; | |
134 | int result; | |
135 | ||
136 | /* Only call FUNC for open fds. */ | |
137 | if (fstat (fd, &sb) == -1) | |
138 | continue; | |
139 | ||
140 | result = func (arg, fd); | |
141 | if (result != 0) | |
142 | return result; | |
143 | } | |
144 | ||
145 | return 0; | |
146 | } | |
147 | } | |
148 | ||
149 | #endif /* HAVE_FDWALK */ | |
150 | ||
151 | \f | |
152 | ||
37269bc9 SM |
153 | /* A vector holding all the fds open when notice_open_fds was called. We |
154 | don't use a hashtab because we don't expect there to be many open fds. */ | |
614c279d | 155 | |
37269bc9 | 156 | static std::vector<int> open_fds; |
614c279d TT |
157 | |
158 | /* An fdwalk callback function used by notice_open_fds. It puts the | |
159 | given file descriptor into the vec. */ | |
160 | ||
161 | static int | |
162 | do_mark_open_fd (void *ignore, int fd) | |
163 | { | |
37269bc9 | 164 | open_fds.push_back (fd); |
614c279d TT |
165 | return 0; |
166 | } | |
167 | ||
168 | /* See filestuff.h. */ | |
169 | ||
170 | void | |
171 | notice_open_fds (void) | |
172 | { | |
173 | fdwalk (do_mark_open_fd, NULL); | |
174 | } | |
175 | ||
21ff4686 TT |
176 | /* See filestuff.h. */ |
177 | ||
178 | void | |
179 | mark_fd_no_cloexec (int fd) | |
180 | { | |
181 | do_mark_open_fd (NULL, fd); | |
182 | } | |
183 | ||
184 | /* See filestuff.h. */ | |
185 | ||
186 | void | |
187 | unmark_fd_no_cloexec (int fd) | |
188 | { | |
37269bc9 | 189 | auto it = std::remove (open_fds.begin (), open_fds.end (), fd); |
21ff4686 | 190 | |
37269bc9 SM |
191 | if (it != open_fds.end ()) |
192 | open_fds.erase (it); | |
193 | else | |
194 | gdb_assert_not_reached (_("fd not found in open_fds")); | |
21ff4686 TT |
195 | } |
196 | ||
614c279d TT |
197 | /* Helper function for close_most_fds that closes the file descriptor |
198 | if appropriate. */ | |
199 | ||
200 | static int | |
201 | do_close (void *ignore, int fd) | |
202 | { | |
37269bc9 | 203 | for (int val : open_fds) |
614c279d TT |
204 | { |
205 | if (fd == val) | |
206 | { | |
207 | /* Keep this one open. */ | |
208 | return 0; | |
209 | } | |
210 | } | |
211 | ||
212 | close (fd); | |
213 | return 0; | |
214 | } | |
215 | ||
216 | /* See filestuff.h. */ | |
217 | ||
218 | void | |
219 | close_most_fds (void) | |
220 | { | |
221 | fdwalk (do_close, NULL); | |
222 | } | |
223 | ||
224 | \f | |
225 | ||
226 | /* This is a tri-state flag. When zero it means we haven't yet tried | |
227 | O_CLOEXEC. When positive it means that O_CLOEXEC works on this | |
228 | host. When negative, it means that O_CLOEXEC doesn't work. We | |
229 | track this state because, while gdb might have been compiled | |
230 | against a libc that supplies O_CLOEXEC, there is no guarantee that | |
231 | the kernel supports it. */ | |
232 | ||
233 | static int trust_o_cloexec; | |
234 | ||
235 | /* Mark FD as close-on-exec, ignoring errors. Update | |
236 | TRUST_O_CLOEXEC. */ | |
237 | ||
238 | static void | |
239 | mark_cloexec (int fd) | |
240 | { | |
5d71132c | 241 | #ifdef HAVE_F_GETFD |
614c279d TT |
242 | int old = fcntl (fd, F_GETFD, 0); |
243 | ||
244 | if (old != -1) | |
245 | { | |
246 | fcntl (fd, F_SETFD, old | FD_CLOEXEC); | |
247 | ||
248 | if (trust_o_cloexec == 0) | |
249 | { | |
250 | if ((old & FD_CLOEXEC) != 0) | |
251 | trust_o_cloexec = 1; | |
252 | else | |
253 | trust_o_cloexec = -1; | |
254 | } | |
255 | } | |
5d71132c | 256 | #endif /* HAVE_F_GETFD */ |
614c279d TT |
257 | } |
258 | ||
259 | /* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec. */ | |
260 | ||
261 | static void | |
262 | maybe_mark_cloexec (int fd) | |
263 | { | |
264 | if (trust_o_cloexec <= 0) | |
265 | mark_cloexec (fd); | |
266 | } | |
267 | ||
8658d16d PA |
268 | #ifdef HAVE_SOCKETS |
269 | ||
614c279d TT |
270 | /* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC. */ |
271 | ||
272 | static void | |
273 | socket_mark_cloexec (int fd) | |
274 | { | |
275 | if (SOCK_CLOEXEC == 0 || trust_o_cloexec <= 0) | |
276 | mark_cloexec (fd); | |
277 | } | |
278 | ||
8658d16d PA |
279 | #endif |
280 | ||
614c279d TT |
281 | \f |
282 | ||
283 | /* See filestuff.h. */ | |
284 | ||
285 | int | |
5d71132c | 286 | gdb_open_cloexec (const char *filename, int flags, unsigned long mode) |
614c279d TT |
287 | { |
288 | int fd = open (filename, flags | O_CLOEXEC, mode); | |
289 | ||
290 | if (fd >= 0) | |
291 | maybe_mark_cloexec (fd); | |
292 | ||
293 | return fd; | |
294 | } | |
295 | ||
296 | /* See filestuff.h. */ | |
297 | ||
d419f42d | 298 | gdb_file_up |
614c279d TT |
299 | gdb_fopen_cloexec (const char *filename, const char *opentype) |
300 | { | |
c74e1ccf | 301 | FILE *result; |
88505fac PM |
302 | /* Probe for "e" support once. But, if we can tell the operating |
303 | system doesn't know about close on exec mode "e" without probing, | |
304 | skip it. E.g., the Windows runtime issues an "Invalid parameter | |
305 | passed to C runtime function" OutputDebugString warning for | |
306 | unknown modes. Assume that if O_CLOEXEC is zero, then "e" isn't | |
970d89d8 EZ |
307 | supported. On MinGW, O_CLOEXEC is an alias of O_NOINHERIT, and |
308 | "e" isn't supported. */ | |
309 | static int fopen_e_ever_failed_einval = | |
310 | O_CLOEXEC == 0 || O_CLOEXEC == O_NOINHERIT; | |
614c279d | 311 | |
c74e1ccf | 312 | if (!fopen_e_ever_failed_einval) |
614c279d TT |
313 | { |
314 | char *copy; | |
315 | ||
224c3ddb | 316 | copy = (char *) alloca (strlen (opentype) + 2); |
614c279d TT |
317 | strcpy (copy, opentype); |
318 | /* This is a glibc extension but we try it unconditionally on | |
319 | this path. */ | |
320 | strcat (copy, "e"); | |
321 | result = fopen (filename, copy); | |
614c279d | 322 | |
c74e1ccf JK |
323 | if (result == NULL && errno == EINVAL) |
324 | { | |
325 | result = fopen (filename, opentype); | |
326 | if (result != NULL) | |
327 | fopen_e_ever_failed_einval = 1; | |
328 | } | |
614c279d | 329 | } |
c74e1ccf JK |
330 | else |
331 | result = fopen (filename, opentype); | |
614c279d TT |
332 | |
333 | if (result != NULL) | |
334 | maybe_mark_cloexec (fileno (result)); | |
335 | ||
d419f42d | 336 | return gdb_file_up (result); |
614c279d TT |
337 | } |
338 | ||
8658d16d | 339 | #ifdef HAVE_SOCKETS |
614c279d TT |
340 | /* See filestuff.h. */ |
341 | ||
342 | int | |
fe978cb0 PA |
343 | gdb_socketpair_cloexec (int domain, int style, int protocol, |
344 | int filedes[2]) | |
614c279d | 345 | { |
5d71132c | 346 | #ifdef HAVE_SOCKETPAIR |
fe978cb0 | 347 | int result = socketpair (domain, style | SOCK_CLOEXEC, protocol, filedes); |
614c279d TT |
348 | |
349 | if (result != -1) | |
350 | { | |
351 | socket_mark_cloexec (filedes[0]); | |
352 | socket_mark_cloexec (filedes[1]); | |
353 | } | |
354 | ||
355 | return result; | |
5d71132c TT |
356 | #else |
357 | gdb_assert_not_reached (_("socketpair not available on this host")); | |
358 | #endif | |
614c279d TT |
359 | } |
360 | ||
361 | /* See filestuff.h. */ | |
362 | ||
363 | int | |
fe978cb0 | 364 | gdb_socket_cloexec (int domain, int style, int protocol) |
614c279d | 365 | { |
fe978cb0 | 366 | int result = socket (domain, style | SOCK_CLOEXEC, protocol); |
614c279d TT |
367 | |
368 | if (result != -1) | |
369 | socket_mark_cloexec (result); | |
370 | ||
371 | return result; | |
372 | } | |
8658d16d | 373 | #endif |
614c279d TT |
374 | |
375 | /* See filestuff.h. */ | |
376 | ||
377 | int | |
378 | gdb_pipe_cloexec (int filedes[2]) | |
379 | { | |
380 | int result; | |
381 | ||
382 | #ifdef HAVE_PIPE2 | |
383 | result = pipe2 (filedes, O_CLOEXEC); | |
384 | if (result != -1) | |
385 | { | |
386 | maybe_mark_cloexec (filedes[0]); | |
387 | maybe_mark_cloexec (filedes[1]); | |
388 | } | |
389 | #else | |
5d71132c | 390 | #ifdef HAVE_PIPE |
614c279d TT |
391 | result = pipe (filedes); |
392 | if (result != -1) | |
393 | { | |
394 | mark_cloexec (filedes[0]); | |
395 | mark_cloexec (filedes[1]); | |
396 | } | |
5d71132c TT |
397 | #else /* HAVE_PIPE */ |
398 | gdb_assert_not_reached (_("pipe not available on this host")); | |
399 | #endif /* HAVE_PIPE */ | |
400 | #endif /* HAVE_PIPE2 */ | |
614c279d TT |
401 | |
402 | return result; | |
403 | } | |
ca095836 GB |
404 | |
405 | /* Helper function which does the work for make_cleanup_close. */ | |
406 | ||
407 | static void | |
408 | do_close_cleanup (void *arg) | |
409 | { | |
9a3c8263 | 410 | int *fd = (int *) arg; |
ca095836 GB |
411 | |
412 | close (*fd); | |
413 | } | |
414 | ||
c402ef90 | 415 | /* See filestuff.h. */ |
ca095836 GB |
416 | |
417 | struct cleanup * | |
418 | make_cleanup_close (int fd) | |
419 | { | |
8d749320 | 420 | int *saved_fd = XNEW (int); |
ca095836 GB |
421 | |
422 | *saved_fd = fd; | |
423 | return make_cleanup_dtor (do_close_cleanup, saved_fd, xfree); | |
424 | } | |
3c025cfe SDJ |
425 | |
426 | /* See common/filestuff.h. */ | |
427 | ||
428 | bool | |
429 | is_regular_file (const char *name, int *errno_ptr) | |
430 | { | |
431 | struct stat st; | |
432 | const int status = stat (name, &st); | |
433 | ||
434 | /* Stat should never fail except when the file does not exist. | |
435 | If stat fails, analyze the source of error and return true | |
436 | unless the file does not exist, to avoid returning false results | |
437 | on obscure systems where stat does not work as expected. */ | |
438 | ||
439 | if (status != 0) | |
440 | { | |
441 | if (errno != ENOENT) | |
442 | return true; | |
443 | *errno_ptr = ENOENT; | |
444 | return false; | |
445 | } | |
446 | ||
447 | if (S_ISREG (st.st_mode)) | |
448 | return true; | |
449 | ||
450 | if (S_ISDIR (st.st_mode)) | |
451 | *errno_ptr = EISDIR; | |
452 | else | |
453 | *errno_ptr = EINVAL; | |
454 | return false; | |
455 | } | |
e418a61a TT |
456 | |
457 | /* See common/filestuff.h. */ | |
458 | ||
459 | bool | |
460 | mkdir_recursive (const char *dir) | |
461 | { | |
462 | gdb::unique_xmalloc_ptr<char> holder (xstrdup (dir)); | |
463 | char * const start = holder.get (); | |
464 | char *component_start = start; | |
465 | char *component_end = start; | |
466 | ||
467 | while (1) | |
468 | { | |
469 | /* Find the beginning of the next component. */ | |
470 | while (*component_start == '/') | |
471 | component_start++; | |
472 | ||
473 | /* Are we done? */ | |
474 | if (*component_start == '\0') | |
475 | return true; | |
476 | ||
477 | /* Find the slash or null-terminator after this component. */ | |
478 | component_end = component_start; | |
479 | while (*component_end != '/' && *component_end != '\0') | |
480 | component_end++; | |
481 | ||
482 | /* Temporarily replace the slash with a null terminator, so we can create | |
483 | the directory up to this component. */ | |
484 | char saved_char = *component_end; | |
485 | *component_end = '\0'; | |
486 | ||
487 | /* If we get EEXIST and the existing path is a directory, then we're | |
488 | happy. If it exists, but it's a regular file and this is not the last | |
489 | component, we'll fail at the next component. If this is the last | |
490 | component, the caller will fail with ENOTDIR when trying to | |
491 | open/create a file under that path. */ | |
492 | if (mkdir (start, 0700) != 0) | |
493 | if (errno != EEXIST) | |
494 | return false; | |
495 | ||
496 | /* Restore the overwritten char. */ | |
497 | *component_end = saved_char; | |
498 | component_start = component_end; | |
499 | } | |
500 | } |