1 /* Low-level file-handling.
2 Copyright (C) 2012-2018 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
19 #include "common-defs.h"
20 #include "filestuff.h"
24 #include <sys/types.h>
31 #define HAVE_SOCKETS 1
32 #elif defined HAVE_SYS_SOCKET_H
33 #include <sys/socket.h>
34 /* Define HAVE_F_GETFD if we plan to use F_GETFD. */
35 #define HAVE_F_GETFD F_GETFD
36 #define HAVE_SOCKETS 1
39 #ifdef HAVE_SYS_RESOURCE_H
40 #include <sys/resource.h>
41 #endif /* HAVE_SYS_RESOURCE_H */
52 #define SOCK_CLOEXEC 0
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
69 fdwalk (int (*func) (void *, int), void *arg)
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
77 dir = opendir ("/proc/self/fd");
83 for (entry = readdir (dir); entry != NULL; entry = readdir (dir))
89 fd = strtol (entry->d_name, &tail, 10);
90 if (*tail != '\0' || errno != 0)
94 /* What can we do here really? */
98 if (fd == dirfd (dir))
101 result = func (arg, fd);
109 /* We may fall through to the next case. */
115 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
118 if (getrlimit (RLIMIT_NOFILE, &rlim) == 0 && rlim.rlim_max != RLIM_INFINITY)
124 max = sysconf (_SC_OPEN_MAX);
128 #endif /* _SC_OPEN_MAX */
131 for (fd = 0; fd < max; ++fd)
136 /* Only call FUNC for open fds. */
137 if (fstat (fd, &sb) == -1)
140 result = func (arg, fd);
149 #endif /* HAVE_FDWALK */
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. */
156 static std::vector<int> open_fds;
158 /* An fdwalk callback function used by notice_open_fds. It puts the
159 given file descriptor into the vec. */
162 do_mark_open_fd (void *ignore, int fd)
164 open_fds.push_back (fd);
168 /* See filestuff.h. */
171 notice_open_fds (void)
173 fdwalk (do_mark_open_fd, NULL);
176 /* See filestuff.h. */
179 mark_fd_no_cloexec (int fd)
181 do_mark_open_fd (NULL, fd);
184 /* See filestuff.h. */
187 unmark_fd_no_cloexec (int fd)
189 auto it = std::remove (open_fds.begin (), open_fds.end (), fd);
191 if (it != open_fds.end ())
194 gdb_assert_not_reached (_("fd not found in open_fds"));
197 /* Helper function for close_most_fds that closes the file descriptor
201 do_close (void *ignore, int fd)
203 for (int val : open_fds)
207 /* Keep this one open. */
216 /* See filestuff.h. */
219 close_most_fds (void)
221 fdwalk (do_close, NULL);
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. */
233 static int trust_o_cloexec;
235 /* Mark FD as close-on-exec, ignoring errors. Update
239 mark_cloexec (int fd)
242 int old = fcntl (fd, F_GETFD, 0);
246 fcntl (fd, F_SETFD, old | FD_CLOEXEC);
248 if (trust_o_cloexec == 0)
250 if ((old & FD_CLOEXEC) != 0)
253 trust_o_cloexec = -1;
256 #endif /* HAVE_F_GETFD */
259 /* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec. */
262 maybe_mark_cloexec (int fd)
264 if (trust_o_cloexec <= 0)
270 /* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC. */
273 socket_mark_cloexec (int fd)
275 if (SOCK_CLOEXEC == 0 || trust_o_cloexec <= 0)
283 /* See filestuff.h. */
286 gdb_open_cloexec (const char *filename, int flags, unsigned long mode)
288 int fd = open (filename, flags | O_CLOEXEC, mode);
291 maybe_mark_cloexec (fd);
296 /* See filestuff.h. */
299 gdb_fopen_cloexec (const char *filename, const char *opentype)
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
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;
312 if (!fopen_e_ever_failed_einval)
316 copy = (char *) alloca (strlen (opentype) + 2);
317 strcpy (copy, opentype);
318 /* This is a glibc extension but we try it unconditionally on
321 result = fopen (filename, copy);
323 if (result == NULL && errno == EINVAL)
325 result = fopen (filename, opentype);
327 fopen_e_ever_failed_einval = 1;
331 result = fopen (filename, opentype);
334 maybe_mark_cloexec (fileno (result));
336 return gdb_file_up (result);
340 /* See filestuff.h. */
343 gdb_socketpair_cloexec (int domain, int style, int protocol,
346 #ifdef HAVE_SOCKETPAIR
347 int result = socketpair (domain, style | SOCK_CLOEXEC, protocol, filedes);
351 socket_mark_cloexec (filedes[0]);
352 socket_mark_cloexec (filedes[1]);
357 gdb_assert_not_reached (_("socketpair not available on this host"));
361 /* See filestuff.h. */
364 gdb_socket_cloexec (int domain, int style, int protocol)
366 int result = socket (domain, style | SOCK_CLOEXEC, protocol);
369 socket_mark_cloexec (result);
375 /* See filestuff.h. */
378 gdb_pipe_cloexec (int filedes[2])
383 result = pipe2 (filedes, O_CLOEXEC);
386 maybe_mark_cloexec (filedes[0]);
387 maybe_mark_cloexec (filedes[1]);
391 result = pipe (filedes);
394 mark_cloexec (filedes[0]);
395 mark_cloexec (filedes[1]);
397 #else /* HAVE_PIPE */
398 gdb_assert_not_reached (_("pipe not available on this host"));
399 #endif /* HAVE_PIPE */
400 #endif /* HAVE_PIPE2 */
405 /* Helper function which does the work for make_cleanup_close. */
408 do_close_cleanup (void *arg)
410 int *fd = (int *) arg;
415 /* See filestuff.h. */
418 make_cleanup_close (int fd)
420 int *saved_fd = XNEW (int);
423 return make_cleanup_dtor (do_close_cleanup, saved_fd, xfree);
426 /* See common/filestuff.h. */
429 is_regular_file (const char *name, int *errno_ptr)
432 const int status = stat (name, &st);
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. */
447 if (S_ISREG (st.st_mode))
450 if (S_ISDIR (st.st_mode))
457 /* See common/filestuff.h. */
460 mkdir_recursive (const char *dir)
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;
469 /* Find the beginning of the next component. */
470 while (*component_start == '/')
474 if (*component_start == '\0')
477 /* Find the slash or null-terminator after this component. */
478 component_end = component_start;
479 while (*component_end != '/' && *component_end != '\0')
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';
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)
496 /* Restore the overwritten char. */
497 *component_end = saved_char;
498 component_start = component_end;