1 /* Low-level file-handling.
2 Copyright (C) 2012-2014 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/>. */
24 #include "filestuff.h"
28 #include <sys/types.h>
34 #define HAVE_SOCKETS 1
35 #elif defined HAVE_SYS_SOCKET_H
36 #include <sys/socket.h>
37 /* Define HAVE_F_GETFD if we plan to use F_GETFD. */
38 #define HAVE_F_GETFD F_GETFD
39 #define HAVE_SOCKETS 1
42 #ifdef HAVE_SYS_RESOURCE_H
43 #include <sys/resource.h>
44 #endif /* HAVE_SYS_RESOURCE_H */
51 #define SOCK_CLOEXEC 0
60 /* Replacement for fdwalk, if the system doesn't define it. Walks all
61 open file descriptors (though this implementation may walk closed
62 ones as well, depending on the host platform's capabilities) and
63 call FUNC with ARG. If FUNC returns non-zero, stops immediately
64 and returns the same value. Otherwise, returns zero when
68 fdwalk (int (*func
) (void *, int), void *arg
)
70 /* Checking __linux__ isn't great but it isn't clear what would be
71 better. There doesn't seem to be a good way to check for this in
76 dir
= opendir ("/proc/self/fd");
82 for (entry
= readdir (dir
); entry
!= NULL
; entry
= readdir (dir
))
89 fd
= strtol (entry
->d_name
, &tail
, 10);
90 if (*tail
!= '\0' || errno
!= 0)
94 /* What can we do here really? */
98 if (fd
== dirfd (dir
))
101 result
= func (arg
, fd
);
109 /* We may fall through to the next case. */
115 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
118 if (getrlimit (RLIMIT_NOFILE
, &rlim
) == 0 && rlim
.rlim_max
!= RLIM_INFINITY
)
124 max
= sysconf (_SC_OPEN_MAX
);
128 #endif /* _SC_OPEN_MAX */
131 for (fd
= 0; fd
< max
; ++fd
)
136 /* Only call FUNC for open fds. */
137 if (fstat (fd
, &sb
) == -1)
140 result
= func (arg
, fd
);
149 #endif /* HAVE_FDWALK */
153 /* A VEC holding all the fds open when notice_open_fds was called. We
154 don't use a hashtab because libiberty isn't linked into gdbserver;
155 and anyway we don't expect there to be many open fds. */
157 static VEC (int) *open_fds
;
159 /* An fdwalk callback function used by notice_open_fds. It puts the
160 given file descriptor into the vec. */
163 do_mark_open_fd (void *ignore
, int fd
)
165 VEC_safe_push (int, open_fds
, fd
);
169 /* See filestuff.h. */
172 notice_open_fds (void)
174 fdwalk (do_mark_open_fd
, NULL
);
177 /* See filestuff.h. */
180 mark_fd_no_cloexec (int fd
)
182 do_mark_open_fd (NULL
, fd
);
185 /* See filestuff.h. */
188 unmark_fd_no_cloexec (int fd
)
192 for (i
= 0; VEC_iterate (int, open_fds
, i
, val
); ++i
)
196 VEC_unordered_remove (int, open_fds
, i
);
201 gdb_assert_not_reached (_("fd not found in open_fds"));
204 /* Helper function for close_most_fds that closes the file descriptor
208 do_close (void *ignore
, int fd
)
212 for (i
= 0; VEC_iterate (int, open_fds
, i
, val
); ++i
)
216 /* Keep this one open. */
225 /* See filestuff.h. */
228 close_most_fds (void)
230 fdwalk (do_close
, NULL
);
235 /* This is a tri-state flag. When zero it means we haven't yet tried
236 O_CLOEXEC. When positive it means that O_CLOEXEC works on this
237 host. When negative, it means that O_CLOEXEC doesn't work. We
238 track this state because, while gdb might have been compiled
239 against a libc that supplies O_CLOEXEC, there is no guarantee that
240 the kernel supports it. */
242 static int trust_o_cloexec
;
244 /* Mark FD as close-on-exec, ignoring errors. Update
248 mark_cloexec (int fd
)
251 int old
= fcntl (fd
, F_GETFD
, 0);
255 fcntl (fd
, F_SETFD
, old
| FD_CLOEXEC
);
257 if (trust_o_cloexec
== 0)
259 if ((old
& FD_CLOEXEC
) != 0)
262 trust_o_cloexec
= -1;
265 #endif /* HAVE_F_GETFD */
268 /* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec. */
271 maybe_mark_cloexec (int fd
)
273 if (trust_o_cloexec
<= 0)
279 /* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC. */
282 socket_mark_cloexec (int fd
)
284 if (SOCK_CLOEXEC
== 0 || trust_o_cloexec
<= 0)
292 /* See filestuff.h. */
295 gdb_open_cloexec (const char *filename
, int flags
, unsigned long mode
)
297 int fd
= open (filename
, flags
| O_CLOEXEC
, mode
);
300 maybe_mark_cloexec (fd
);
305 /* See filestuff.h. */
308 gdb_fopen_cloexec (const char *filename
, const char *opentype
)
311 /* Probe for "e" support once. But, if we can tell the operating
312 system doesn't know about close on exec mode "e" without probing,
313 skip it. E.g., the Windows runtime issues an "Invalid parameter
314 passed to C runtime function" OutputDebugString warning for
315 unknown modes. Assume that if O_CLOEXEC is zero, then "e" isn't
317 static int fopen_e_ever_failed_einval
= O_CLOEXEC
== 0;
319 if (!fopen_e_ever_failed_einval
)
323 copy
= alloca (strlen (opentype
) + 2);
324 strcpy (copy
, opentype
);
325 /* This is a glibc extension but we try it unconditionally on
328 result
= fopen (filename
, copy
);
330 if (result
== NULL
&& errno
== EINVAL
)
332 result
= fopen (filename
, opentype
);
334 fopen_e_ever_failed_einval
= 1;
338 result
= fopen (filename
, opentype
);
341 maybe_mark_cloexec (fileno (result
));
347 /* See filestuff.h. */
350 gdb_socketpair_cloexec (int namespace, int style
, int protocol
, int filedes
[2])
352 #ifdef HAVE_SOCKETPAIR
353 int result
= socketpair (namespace, style
| SOCK_CLOEXEC
, protocol
, filedes
);
357 socket_mark_cloexec (filedes
[0]);
358 socket_mark_cloexec (filedes
[1]);
363 gdb_assert_not_reached (_("socketpair not available on this host"));
367 /* See filestuff.h. */
370 gdb_socket_cloexec (int namespace, int style
, int protocol
)
372 int result
= socket (namespace, style
| SOCK_CLOEXEC
, protocol
);
375 socket_mark_cloexec (result
);
381 /* See filestuff.h. */
384 gdb_pipe_cloexec (int filedes
[2])
389 result
= pipe2 (filedes
, O_CLOEXEC
);
392 maybe_mark_cloexec (filedes
[0]);
393 maybe_mark_cloexec (filedes
[1]);
397 result
= pipe (filedes
);
400 mark_cloexec (filedes
[0]);
401 mark_cloexec (filedes
[1]);
403 #else /* HAVE_PIPE */
404 gdb_assert_not_reached (_("pipe not available on this host"));
405 #endif /* HAVE_PIPE */
406 #endif /* HAVE_PIPE2 */