1 /* Low-level file-handling.
2 Copyright (C) 2012, 2013 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/>. */
25 #include "filestuff.h"
31 #include <sys/types.h>
37 #define HAVE_SOCKETS 1
38 #elif defined HAVE_SYS_SOCKET_H
39 #include <sys/socket.h>
40 /* Define HAVE_F_GETFD if we plan to use F_GETFD. */
41 #define HAVE_F_GETFD F_GETFD
42 #define HAVE_SOCKETS 1
45 #ifdef HAVE_SYS_RESOURCE_H
46 #include <sys/resource.h>
47 #endif /* HAVE_SYS_RESOURCE_H */
54 #define SOCK_CLOEXEC 0
63 /* Replacement for fdwalk, if the system doesn't define it. Walks all
64 open file descriptors (though this implementation may walk closed
65 ones as well, depending on the host platform's capabilities) and
66 call FUNC with ARG. If FUNC returns non-zero, stops immediately
67 and returns the same value. Otherwise, returns zero when
71 fdwalk (int (*func
) (void *, int), void *arg
)
73 /* Checking __linux__ isn't great but it isn't clear what would be
74 better. There doesn't seem to be a good way to check for this in
79 dir
= opendir ("/proc/self/fd");
85 for (entry
= readdir (dir
); entry
!= NULL
; entry
= readdir (dir
))
92 fd
= strtol (entry
->d_name
, &tail
, 10);
93 if (*tail
!= '\0' || errno
!= 0)
97 /* What can we do here really? */
101 if (fd
== dirfd (dir
))
104 result
= func (arg
, fd
);
112 /* We may fall through to the next case. */
118 #ifdef HAVE_GETRLIMIT
121 if (getrlimit (RLIMIT_NOFILE
, &rlim
) == 0 && rlim
.rlim_max
!= RLIM_INFINITY
)
127 max
= sysconf (_SC_OPEN_MAX
);
131 #endif /* _SC_OPEN_MAX */
134 for (fd
= 0; fd
< max
; ++fd
)
139 /* Only call FUNC for open fds. */
140 if (fstat (fd
, &sb
) == -1)
143 result
= func (arg
, fd
);
152 #endif /* HAVE_FDWALK */
156 /* A VEC holding all the fds open when notice_open_fds was called. We
157 don't use a hashtab because libiberty isn't linked into gdbserver;
158 and anyway we don't expect there to be many open fds. */
160 static VEC (int) *open_fds
;
162 /* An fdwalk callback function used by notice_open_fds. It puts the
163 given file descriptor into the vec. */
166 do_mark_open_fd (void *ignore
, int fd
)
168 VEC_safe_push (int, open_fds
, fd
);
172 /* See filestuff.h. */
175 notice_open_fds (void)
177 fdwalk (do_mark_open_fd
, NULL
);
180 /* See filestuff.h. */
183 mark_fd_no_cloexec (int fd
)
185 do_mark_open_fd (NULL
, fd
);
188 /* See filestuff.h. */
191 unmark_fd_no_cloexec (int fd
)
195 for (i
= 0; VEC_iterate (int, open_fds
, i
, val
); ++i
)
199 VEC_unordered_remove (int, open_fds
, i
);
204 gdb_assert_not_reached (_("fd not found in open_fds"));
207 /* Helper function for close_most_fds that closes the file descriptor
211 do_close (void *ignore
, int fd
)
215 for (i
= 0; VEC_iterate (int, open_fds
, i
, val
); ++i
)
219 /* Keep this one open. */
228 /* See filestuff.h. */
231 close_most_fds (void)
233 fdwalk (do_close
, NULL
);
238 /* This is a tri-state flag. When zero it means we haven't yet tried
239 O_CLOEXEC. When positive it means that O_CLOEXEC works on this
240 host. When negative, it means that O_CLOEXEC doesn't work. We
241 track this state because, while gdb might have been compiled
242 against a libc that supplies O_CLOEXEC, there is no guarantee that
243 the kernel supports it. */
245 static int trust_o_cloexec
;
247 /* Mark FD as close-on-exec, ignoring errors. Update
251 mark_cloexec (int fd
)
254 int old
= fcntl (fd
, F_GETFD
, 0);
258 fcntl (fd
, F_SETFD
, old
| FD_CLOEXEC
);
260 if (trust_o_cloexec
== 0)
262 if ((old
& FD_CLOEXEC
) != 0)
265 trust_o_cloexec
= -1;
268 #endif /* HAVE_F_GETFD */
271 /* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec. */
274 maybe_mark_cloexec (int fd
)
276 if (trust_o_cloexec
<= 0)
282 /* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC. */
285 socket_mark_cloexec (int fd
)
287 if (SOCK_CLOEXEC
== 0 || trust_o_cloexec
<= 0)
295 /* See filestuff.h. */
298 gdb_open_cloexec (const char *filename
, int flags
, unsigned long mode
)
300 int fd
= open (filename
, flags
| O_CLOEXEC
, mode
);
303 maybe_mark_cloexec (fd
);
308 /* See filestuff.h. */
311 gdb_fopen_cloexec (const char *filename
, const char *opentype
)
314 /* Probe for "e" support once. But, if we can tell the operating
315 system doesn't know about close on exec mode "e" without probing,
316 skip it. E.g., the Windows runtime issues an "Invalid parameter
317 passed to C runtime function" OutputDebugString warning for
318 unknown modes. Assume that if O_CLOEXEC is zero, then "e" isn't
320 static int fopen_e_ever_failed_einval
= O_CLOEXEC
== 0;
322 if (!fopen_e_ever_failed_einval
)
326 copy
= alloca (strlen (opentype
) + 2);
327 strcpy (copy
, opentype
);
328 /* This is a glibc extension but we try it unconditionally on
331 result
= fopen (filename
, copy
);
333 if (result
== NULL
&& errno
== EINVAL
)
335 result
= fopen (filename
, opentype
);
337 fopen_e_ever_failed_einval
= 1;
341 result
= fopen (filename
, opentype
);
344 maybe_mark_cloexec (fileno (result
));
350 /* See filestuff.h. */
353 gdb_socketpair_cloexec (int namespace, int style
, int protocol
, int filedes
[2])
355 #ifdef HAVE_SOCKETPAIR
356 int result
= socketpair (namespace, style
| SOCK_CLOEXEC
, protocol
, filedes
);
360 socket_mark_cloexec (filedes
[0]);
361 socket_mark_cloexec (filedes
[1]);
366 gdb_assert_not_reached (_("socketpair not available on this host"));
370 /* See filestuff.h. */
373 gdb_socket_cloexec (int namespace, int style
, int protocol
)
375 int result
= socket (namespace, style
| SOCK_CLOEXEC
, protocol
);
378 socket_mark_cloexec (result
);
384 /* See filestuff.h. */
387 gdb_pipe_cloexec (int filedes
[2])
392 result
= pipe2 (filedes
, O_CLOEXEC
);
395 maybe_mark_cloexec (filedes
[0]);
396 maybe_mark_cloexec (filedes
[1]);
400 result
= pipe (filedes
);
403 mark_cloexec (filedes
[0]);
404 mark_cloexec (filedes
[1]);
406 #else /* HAVE_PIPE */
407 gdb_assert_not_reached (_("pipe not available on this host"));
408 #endif /* HAVE_PIPE */
409 #endif /* HAVE_PIPE2 */