1 /* Low-level file-handling.
2 Copyright (C) 2012-2015 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>
30 #define HAVE_SOCKETS 1
31 #elif defined HAVE_SYS_SOCKET_H
32 #include <sys/socket.h>
33 /* Define HAVE_F_GETFD if we plan to use F_GETFD. */
34 #define HAVE_F_GETFD F_GETFD
35 #define HAVE_SOCKETS 1
38 #ifdef HAVE_SYS_RESOURCE_H
39 #include <sys/resource.h>
40 #endif /* HAVE_SYS_RESOURCE_H */
47 #define SOCK_CLOEXEC 0
56 /* Replacement for fdwalk, if the system doesn't define it. Walks all
57 open file descriptors (though this implementation may walk closed
58 ones as well, depending on the host platform's capabilities) and
59 call FUNC with ARG. If FUNC returns non-zero, stops immediately
60 and returns the same value. Otherwise, returns zero when
64 fdwalk (int (*func
) (void *, int), void *arg
)
66 /* Checking __linux__ isn't great but it isn't clear what would be
67 better. There doesn't seem to be a good way to check for this in
72 dir
= opendir ("/proc/self/fd");
78 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 VEC holding all the fds open when notice_open_fds was called. We
150 don't use a hashtab because libiberty isn't linked into gdbserver;
151 and anyway we don't expect there to be many open fds. */
153 static VEC (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 VEC_safe_push (int, open_fds
, 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
)
188 for (i
= 0; VEC_iterate (int, open_fds
, i
, val
); ++i
)
192 VEC_unordered_remove (int, open_fds
, i
);
197 gdb_assert_not_reached (_("fd not found in open_fds"));
200 /* Helper function for close_most_fds that closes the file descriptor
204 do_close (void *ignore
, int fd
)
208 for (i
= 0; VEC_iterate (int, open_fds
, i
, val
); ++i
)
212 /* Keep this one open. */
221 /* See filestuff.h. */
224 close_most_fds (void)
226 fdwalk (do_close
, NULL
);
231 /* This is a tri-state flag. When zero it means we haven't yet tried
232 O_CLOEXEC. When positive it means that O_CLOEXEC works on this
233 host. When negative, it means that O_CLOEXEC doesn't work. We
234 track this state because, while gdb might have been compiled
235 against a libc that supplies O_CLOEXEC, there is no guarantee that
236 the kernel supports it. */
238 static int trust_o_cloexec
;
240 /* Mark FD as close-on-exec, ignoring errors. Update
244 mark_cloexec (int fd
)
247 int old
= fcntl (fd
, F_GETFD
, 0);
251 fcntl (fd
, F_SETFD
, old
| FD_CLOEXEC
);
253 if (trust_o_cloexec
== 0)
255 if ((old
& FD_CLOEXEC
) != 0)
258 trust_o_cloexec
= -1;
261 #endif /* HAVE_F_GETFD */
264 /* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec. */
267 maybe_mark_cloexec (int fd
)
269 if (trust_o_cloexec
<= 0)
275 /* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC. */
278 socket_mark_cloexec (int fd
)
280 if (SOCK_CLOEXEC
== 0 || trust_o_cloexec
<= 0)
288 /* See filestuff.h. */
291 gdb_open_cloexec (const char *filename
, int flags
, unsigned long mode
)
293 int fd
= open (filename
, flags
| O_CLOEXEC
, mode
);
296 maybe_mark_cloexec (fd
);
301 /* See filestuff.h. */
304 gdb_fopen_cloexec (const char *filename
, const char *opentype
)
307 /* Probe for "e" support once. But, if we can tell the operating
308 system doesn't know about close on exec mode "e" without probing,
309 skip it. E.g., the Windows runtime issues an "Invalid parameter
310 passed to C runtime function" OutputDebugString warning for
311 unknown modes. Assume that if O_CLOEXEC is zero, then "e" isn't
313 static int fopen_e_ever_failed_einval
= O_CLOEXEC
== 0;
315 if (!fopen_e_ever_failed_einval
)
319 copy
= alloca (strlen (opentype
) + 2);
320 strcpy (copy
, opentype
);
321 /* This is a glibc extension but we try it unconditionally on
324 result
= fopen (filename
, copy
);
326 if (result
== NULL
&& errno
== EINVAL
)
328 result
= fopen (filename
, opentype
);
330 fopen_e_ever_failed_einval
= 1;
334 result
= fopen (filename
, opentype
);
337 maybe_mark_cloexec (fileno (result
));
343 /* See filestuff.h. */
346 gdb_socketpair_cloexec (int domain
, int style
, int protocol
,
349 #ifdef HAVE_SOCKETPAIR
350 int result
= socketpair (domain
, style
| SOCK_CLOEXEC
, protocol
, filedes
);
354 socket_mark_cloexec (filedes
[0]);
355 socket_mark_cloexec (filedes
[1]);
360 gdb_assert_not_reached (_("socketpair not available on this host"));
364 /* See filestuff.h. */
367 gdb_socket_cloexec (int domain
, int style
, int protocol
)
369 int result
= socket (domain
, style
| SOCK_CLOEXEC
, protocol
);
372 socket_mark_cloexec (result
);
378 /* See filestuff.h. */
381 gdb_pipe_cloexec (int filedes
[2])
386 result
= pipe2 (filedes
, O_CLOEXEC
);
389 maybe_mark_cloexec (filedes
[0]);
390 maybe_mark_cloexec (filedes
[1]);
394 result
= pipe (filedes
);
397 mark_cloexec (filedes
[0]);
398 mark_cloexec (filedes
[1]);
400 #else /* HAVE_PIPE */
401 gdb_assert_not_reached (_("pipe not available on this host"));
402 #endif /* HAVE_PIPE */
403 #endif /* HAVE_PIPE2 */
408 /* Helper function which does the work for make_cleanup_close. */
411 do_close_cleanup (void *arg
)
418 /* See cleanup-utils.h. */
421 make_cleanup_close (int fd
)
423 int *saved_fd
= xmalloc (sizeof (fd
));
426 return make_cleanup_dtor (do_close_cleanup
, saved_fd
, xfree
);