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
304 static int fopen_e_ever_failed_einval
= O_CLOEXEC
== 0;
306 if (!fopen_e_ever_failed_einval
)
310 copy
= (char *) alloca (strlen (opentype
) + 2);
311 strcpy (copy
, opentype
);
312 /* This is a glibc extension but we try it unconditionally on
315 result
= fopen (filename
, copy
);
317 if (result
== NULL
&& errno
== EINVAL
)
319 result
= fopen (filename
, opentype
);
321 fopen_e_ever_failed_einval
= 1;
325 result
= fopen (filename
, opentype
);
328 maybe_mark_cloexec (fileno (result
));
330 return gdb_file_up (result
);
334 /* See filestuff.h. */
337 gdb_socketpair_cloexec (int domain
, int style
, int protocol
,
340 #ifdef HAVE_SOCKETPAIR
341 int result
= socketpair (domain
, style
| SOCK_CLOEXEC
, protocol
, filedes
);
345 socket_mark_cloexec (filedes
[0]);
346 socket_mark_cloexec (filedes
[1]);
351 gdb_assert_not_reached (_("socketpair not available on this host"));
355 /* See filestuff.h. */
358 gdb_socket_cloexec (int domain
, int style
, int protocol
)
360 int result
= socket (domain
, style
| SOCK_CLOEXEC
, protocol
);
363 socket_mark_cloexec (result
);
369 /* See filestuff.h. */
372 gdb_pipe_cloexec (int filedes
[2])
377 result
= pipe2 (filedes
, O_CLOEXEC
);
380 maybe_mark_cloexec (filedes
[0]);
381 maybe_mark_cloexec (filedes
[1]);
385 result
= pipe (filedes
);
388 mark_cloexec (filedes
[0]);
389 mark_cloexec (filedes
[1]);
391 #else /* HAVE_PIPE */
392 gdb_assert_not_reached (_("pipe not available on this host"));
393 #endif /* HAVE_PIPE */
394 #endif /* HAVE_PIPE2 */
399 /* Helper function which does the work for make_cleanup_close. */
402 do_close_cleanup (void *arg
)
404 int *fd
= (int *) arg
;
409 /* See filestuff.h. */
412 make_cleanup_close (int fd
)
414 int *saved_fd
= XNEW (int);
417 return make_cleanup_dtor (do_close_cleanup
, saved_fd
, xfree
);
420 /* See common/filestuff.h. */
423 is_regular_file (const char *name
, int *errno_ptr
)
426 const int status
= stat (name
, &st
);
428 /* Stat should never fail except when the file does not exist.
429 If stat fails, analyze the source of error and return true
430 unless the file does not exist, to avoid returning false results
431 on obscure systems where stat does not work as expected. */
441 if (S_ISREG (st
.st_mode
))
444 if (S_ISDIR (st
.st_mode
))