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
))
86 fd
= strtol (entry
->d_name
, &tail
, 10);
87 if (*tail
!= '\0' || errno
!= 0)
91 /* What can we do here really? */
95 if (fd
== dirfd (dir
))
98 result
= func (arg
, fd
);
106 /* We may fall through to the next case. */
112 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
115 if (getrlimit (RLIMIT_NOFILE
, &rlim
) == 0 && rlim
.rlim_max
!= RLIM_INFINITY
)
121 max
= sysconf (_SC_OPEN_MAX
);
125 #endif /* _SC_OPEN_MAX */
128 for (fd
= 0; fd
< max
; ++fd
)
133 /* Only call FUNC for open fds. */
134 if (fstat (fd
, &sb
) == -1)
137 result
= func (arg
, fd
);
146 #endif /* HAVE_FDWALK */
150 /* A vector holding all the fds open when notice_open_fds was called. We
151 don't use a hashtab because we don't expect there to be many open fds. */
153 static std::vector
<int> open_fds
;
155 /* An fdwalk callback function used by notice_open_fds. It puts the
156 given file descriptor into the vec. */
159 do_mark_open_fd (void *ignore
, int fd
)
161 open_fds
.push_back (fd
);
165 /* See filestuff.h. */
168 notice_open_fds (void)
170 fdwalk (do_mark_open_fd
, NULL
);
173 /* See filestuff.h. */
176 mark_fd_no_cloexec (int fd
)
178 do_mark_open_fd (NULL
, fd
);
181 /* See filestuff.h. */
184 unmark_fd_no_cloexec (int fd
)
186 auto it
= std::remove (open_fds
.begin (), open_fds
.end (), fd
);
188 if (it
!= open_fds
.end ())
191 gdb_assert_not_reached (_("fd not found in open_fds"));
194 /* Helper function for close_most_fds that closes the file descriptor
198 do_close (void *ignore
, int fd
)
200 for (int val
: open_fds
)
204 /* Keep this one open. */
213 /* See filestuff.h. */
216 close_most_fds (void)
218 fdwalk (do_close
, NULL
);
223 /* This is a tri-state flag. When zero it means we haven't yet tried
224 O_CLOEXEC. When positive it means that O_CLOEXEC works on this
225 host. When negative, it means that O_CLOEXEC doesn't work. We
226 track this state because, while gdb might have been compiled
227 against a libc that supplies O_CLOEXEC, there is no guarantee that
228 the kernel supports it. */
230 static int trust_o_cloexec
;
232 /* Mark FD as close-on-exec, ignoring errors. Update
236 mark_cloexec (int fd
)
239 int old
= fcntl (fd
, F_GETFD
, 0);
243 fcntl (fd
, F_SETFD
, old
| FD_CLOEXEC
);
245 if (trust_o_cloexec
== 0)
247 if ((old
& FD_CLOEXEC
) != 0)
250 trust_o_cloexec
= -1;
253 #endif /* HAVE_F_GETFD */
256 /* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec. */
259 maybe_mark_cloexec (int fd
)
261 if (trust_o_cloexec
<= 0)
267 /* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC. */
270 socket_mark_cloexec (int fd
)
272 if (SOCK_CLOEXEC
== 0 || trust_o_cloexec
<= 0)
280 /* See filestuff.h. */
283 gdb_open_cloexec (const char *filename
, int flags
, unsigned long mode
)
285 int fd
= open (filename
, flags
| O_CLOEXEC
, mode
);
288 maybe_mark_cloexec (fd
);
293 /* See filestuff.h. */
296 gdb_fopen_cloexec (const char *filename
, const char *opentype
)
299 /* Probe for "e" support once. But, if we can tell the operating
300 system doesn't know about close on exec mode "e" without probing,
301 skip it. E.g., the Windows runtime issues an "Invalid parameter
302 passed to C runtime function" OutputDebugString warning for
303 unknown modes. Assume that if O_CLOEXEC is zero, then "e" isn't
305 static int fopen_e_ever_failed_einval
= O_CLOEXEC
== 0;
307 if (!fopen_e_ever_failed_einval
)
311 copy
= (char *) alloca (strlen (opentype
) + 2);
312 strcpy (copy
, opentype
);
313 /* This is a glibc extension but we try it unconditionally on
316 result
= fopen (filename
, copy
);
318 if (result
== NULL
&& errno
== EINVAL
)
320 result
= fopen (filename
, opentype
);
322 fopen_e_ever_failed_einval
= 1;
326 result
= fopen (filename
, opentype
);
329 maybe_mark_cloexec (fileno (result
));
331 return gdb_file_up (result
);
335 /* See filestuff.h. */
338 gdb_socketpair_cloexec (int domain
, int style
, int protocol
,
341 #ifdef HAVE_SOCKETPAIR
342 int result
= socketpair (domain
, style
| SOCK_CLOEXEC
, protocol
, filedes
);
346 socket_mark_cloexec (filedes
[0]);
347 socket_mark_cloexec (filedes
[1]);
352 gdb_assert_not_reached (_("socketpair not available on this host"));
356 /* See filestuff.h. */
359 gdb_socket_cloexec (int domain
, int style
, int protocol
)
361 int result
= socket (domain
, style
| SOCK_CLOEXEC
, protocol
);
364 socket_mark_cloexec (result
);
370 /* See filestuff.h. */
373 gdb_pipe_cloexec (int filedes
[2])
378 result
= pipe2 (filedes
, O_CLOEXEC
);
381 maybe_mark_cloexec (filedes
[0]);
382 maybe_mark_cloexec (filedes
[1]);
386 result
= pipe (filedes
);
389 mark_cloexec (filedes
[0]);
390 mark_cloexec (filedes
[1]);
392 #else /* HAVE_PIPE */
393 gdb_assert_not_reached (_("pipe not available on this host"));
394 #endif /* HAVE_PIPE */
395 #endif /* HAVE_PIPE2 */
400 /* Helper function which does the work for make_cleanup_close. */
403 do_close_cleanup (void *arg
)
405 int *fd
= (int *) arg
;
410 /* See filestuff.h. */
413 make_cleanup_close (int fd
)
415 int *saved_fd
= XNEW (int);
418 return make_cleanup_dtor (do_close_cleanup
, saved_fd
, xfree
);