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 */
48 #define SOCK_CLOEXEC 0
57 /* Replacement for fdwalk, if the system doesn't define it. Walks all
58 open file descriptors (though this implementation may walk closed
59 ones as well, depending on the host platform's capabilities) and
60 call FUNC with ARG. If FUNC returns non-zero, stops immediately
61 and returns the same value. Otherwise, returns zero when
65 fdwalk (int (*func
) (void *, int), void *arg
)
67 /* Checking __linux__ isn't great but it isn't clear what would be
68 better. There doesn't seem to be a good way to check for this in
73 dir
= opendir ("/proc/self/fd");
79 for (entry
= readdir (dir
); entry
!= NULL
; entry
= readdir (dir
))
85 fd
= strtol (entry
->d_name
, &tail
, 10);
86 if (*tail
!= '\0' || errno
!= 0)
90 /* What can we do here really? */
94 if (fd
== dirfd (dir
))
97 result
= func (arg
, fd
);
105 /* We may fall through to the next case. */
111 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
114 if (getrlimit (RLIMIT_NOFILE
, &rlim
) == 0 && rlim
.rlim_max
!= RLIM_INFINITY
)
120 max
= sysconf (_SC_OPEN_MAX
);
124 #endif /* _SC_OPEN_MAX */
127 for (fd
= 0; fd
< max
; ++fd
)
132 /* Only call FUNC for open fds. */
133 if (fstat (fd
, &sb
) == -1)
136 result
= func (arg
, fd
);
145 #endif /* HAVE_FDWALK */
149 /* A vector holding all the fds open when notice_open_fds was called. We
150 don't use a hashtab because we don't expect there to be many open fds. */
152 static std::vector
<int> open_fds
;
154 /* An fdwalk callback function used by notice_open_fds. It puts the
155 given file descriptor into the vec. */
158 do_mark_open_fd (void *ignore
, int fd
)
160 open_fds
.push_back (fd
);
164 /* See filestuff.h. */
167 notice_open_fds (void)
169 fdwalk (do_mark_open_fd
, NULL
);
172 /* See filestuff.h. */
175 mark_fd_no_cloexec (int fd
)
177 do_mark_open_fd (NULL
, fd
);
180 /* See filestuff.h. */
183 unmark_fd_no_cloexec (int fd
)
185 auto it
= std::remove (open_fds
.begin (), open_fds
.end (), fd
);
187 if (it
!= open_fds
.end ())
190 gdb_assert_not_reached (_("fd not found in open_fds"));
193 /* Helper function for close_most_fds that closes the file descriptor
197 do_close (void *ignore
, int fd
)
199 for (int val
: open_fds
)
203 /* Keep this one open. */
212 /* See filestuff.h. */
215 close_most_fds (void)
217 fdwalk (do_close
, NULL
);
222 /* This is a tri-state flag. When zero it means we haven't yet tried
223 O_CLOEXEC. When positive it means that O_CLOEXEC works on this
224 host. When negative, it means that O_CLOEXEC doesn't work. We
225 track this state because, while gdb might have been compiled
226 against a libc that supplies O_CLOEXEC, there is no guarantee that
227 the kernel supports it. */
229 static int trust_o_cloexec
;
231 /* Mark FD as close-on-exec, ignoring errors. Update
235 mark_cloexec (int fd
)
238 int old
= fcntl (fd
, F_GETFD
, 0);
242 fcntl (fd
, F_SETFD
, old
| FD_CLOEXEC
);
244 if (trust_o_cloexec
== 0)
246 if ((old
& FD_CLOEXEC
) != 0)
249 trust_o_cloexec
= -1;
252 #endif /* HAVE_F_GETFD */
255 /* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec. */
258 maybe_mark_cloexec (int fd
)
260 if (trust_o_cloexec
<= 0)
266 /* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC. */
269 socket_mark_cloexec (int fd
)
271 if (SOCK_CLOEXEC
== 0 || trust_o_cloexec
<= 0)
279 /* See filestuff.h. */
282 gdb_open_cloexec (const char *filename
, int flags
, unsigned long mode
)
284 int fd
= open (filename
, flags
| O_CLOEXEC
, mode
);
287 maybe_mark_cloexec (fd
);
292 /* See filestuff.h. */
295 gdb_fopen_cloexec (const char *filename
, const char *opentype
)
298 /* Probe for "e" support once. But, if we can tell the operating
299 system doesn't know about close on exec mode "e" without probing,
300 skip it. E.g., the Windows runtime issues an "Invalid parameter
301 passed to C runtime function" OutputDebugString warning for
302 unknown modes. Assume that if O_CLOEXEC is zero, then "e" isn't
303 supported. On MinGW, O_CLOEXEC is an alias of O_NOINHERIT, and
304 "e" isn't supported. */
305 static int fopen_e_ever_failed_einval
=
306 O_CLOEXEC
== 0 || O_CLOEXEC
== O_NOINHERIT
;
308 if (!fopen_e_ever_failed_einval
)
312 copy
= (char *) alloca (strlen (opentype
) + 2);
313 strcpy (copy
, opentype
);
314 /* This is a glibc extension but we try it unconditionally on
317 result
= fopen (filename
, copy
);
319 if (result
== NULL
&& errno
== EINVAL
)
321 result
= fopen (filename
, opentype
);
323 fopen_e_ever_failed_einval
= 1;
327 result
= fopen (filename
, opentype
);
330 maybe_mark_cloexec (fileno (result
));
332 return gdb_file_up (result
);
336 /* See filestuff.h. */
339 gdb_socketpair_cloexec (int domain
, int style
, int protocol
,
342 #ifdef HAVE_SOCKETPAIR
343 int result
= socketpair (domain
, style
| SOCK_CLOEXEC
, protocol
, filedes
);
347 socket_mark_cloexec (filedes
[0]);
348 socket_mark_cloexec (filedes
[1]);
353 gdb_assert_not_reached (_("socketpair not available on this host"));
357 /* See filestuff.h. */
360 gdb_socket_cloexec (int domain
, int style
, int protocol
)
362 int result
= socket (domain
, style
| SOCK_CLOEXEC
, protocol
);
365 socket_mark_cloexec (result
);
371 /* See filestuff.h. */
374 gdb_pipe_cloexec (int filedes
[2])
379 result
= pipe2 (filedes
, O_CLOEXEC
);
382 maybe_mark_cloexec (filedes
[0]);
383 maybe_mark_cloexec (filedes
[1]);
387 result
= pipe (filedes
);
390 mark_cloexec (filedes
[0]);
391 mark_cloexec (filedes
[1]);
393 #else /* HAVE_PIPE */
394 gdb_assert_not_reached (_("pipe not available on this host"));
395 #endif /* HAVE_PIPE */
396 #endif /* HAVE_PIPE2 */
401 /* Helper function which does the work for make_cleanup_close. */
404 do_close_cleanup (void *arg
)
406 int *fd
= (int *) arg
;
411 /* See filestuff.h. */
414 make_cleanup_close (int fd
)
416 int *saved_fd
= XNEW (int);
419 return make_cleanup_dtor (do_close_cleanup
, saved_fd
, xfree
);
422 /* See common/filestuff.h. */
425 is_regular_file (const char *name
, int *errno_ptr
)
428 const int status
= stat (name
, &st
);
430 /* Stat should never fail except when the file does not exist.
431 If stat fails, analyze the source of error and return true
432 unless the file does not exist, to avoid returning false results
433 on obscure systems where stat does not work as expected. */
443 if (S_ISREG (st
.st_mode
))
446 if (S_ISDIR (st
.st_mode
))
453 /* See common/filestuff.h. */
456 mkdir_recursive (const char *dir
)
458 gdb::unique_xmalloc_ptr
<char> holder (xstrdup (dir
));
459 char * const start
= holder
.get ();
460 char *component_start
= start
;
461 char *component_end
= start
;
465 /* Find the beginning of the next component. */
466 while (*component_start
== '/')
470 if (*component_start
== '\0')
473 /* Find the slash or null-terminator after this component. */
474 component_end
= component_start
;
475 while (*component_end
!= '/' && *component_end
!= '\0')
478 /* Temporarily replace the slash with a null terminator, so we can create
479 the directory up to this component. */
480 char saved_char
= *component_end
;
481 *component_end
= '\0';
483 /* If we get EEXIST and the existing path is a directory, then we're
484 happy. If it exists, but it's a regular file and this is not the last
485 component, we'll fail at the next component. If this is the last
486 component, the caller will fail with ENOTDIR when trying to
487 open/create a file under that path. */
488 if (mkdir (start
, 0700) != 0)
492 /* Restore the overwritten char. */
493 *component_end
= saved_char
;
494 component_start
= component_end
;