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/>. */
23 #include "gdb_string.h"
25 #include "filestuff.h"
31 #include <sys/types.h>
38 #include <sys/socket.h>
39 /* Define HAVE_F_GETFD if we plan to use F_GETFD. */
40 #define HAVE_F_GETFD F_GETFD
43 #ifdef HAVE_SYS_RESOURCE_H
44 #include <sys/resource.h>
45 #endif /* HAVE_SYS_RESOURCE_H */
52 #define SOCK_CLOEXEC 0
61 /* Replacement for fdwalk, if the system doesn't define it. Walks all
62 open file descriptors (though this implementation may walk closed
63 ones as well, depending on the host platform's capabilities) and
64 call FUNC with ARG. If FUNC returns non-zero, stops immediately
65 and returns the same value. Otherwise, returns zero when
69 fdwalk (int (*func
) (void *, int), void *arg
)
71 /* Checking __linux__ isn't great but it isn't clear what would be
72 better. There doesn't seem to be a good way to check for this in
77 dir
= opendir ("/proc/self/fd");
83 for (entry
= readdir (dir
); entry
!= NULL
; entry
= readdir (dir
))
90 fd
= strtol (entry
->d_name
, &tail
, 10);
91 if (*tail
!= '\0' || errno
!= 0)
95 /* What can we do here really? */
99 if (fd
== dirfd (dir
))
102 result
= func (arg
, fd
);
110 /* We may fall through to the next case. */
116 #ifdef HAVE_GETRLIMIT
119 if (getrlimit (RLIMIT_NOFILE
, &rlim
) == 0 && rlim
.rlim_max
!= RLIM_INFINITY
)
125 max
= sysconf (_SC_OPEN_MAX
);
129 #endif /* _SC_OPEN_MAX */
132 for (fd
= 0; fd
< max
; ++fd
)
137 /* Only call FUNC for open fds. */
138 if (fstat (fd
, &sb
) == -1)
141 result
= func (arg
, fd
);
150 #endif /* HAVE_FDWALK */
154 /* A VEC holding all the fds open when notice_open_fds was called. We
155 don't use a hashtab because libiberty isn't linked into gdbserver;
156 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 /* Helper function for close_most_fds that closes the file descriptor
184 do_close (void *ignore
, int fd
)
188 for (i
= 0; VEC_iterate (int, open_fds
, i
, val
); ++i
)
192 /* Keep this one open. */
201 /* See filestuff.h. */
204 close_most_fds (void)
206 fdwalk (do_close
, NULL
);
211 /* This is a tri-state flag. When zero it means we haven't yet tried
212 O_CLOEXEC. When positive it means that O_CLOEXEC works on this
213 host. When negative, it means that O_CLOEXEC doesn't work. We
214 track this state because, while gdb might have been compiled
215 against a libc that supplies O_CLOEXEC, there is no guarantee that
216 the kernel supports it. */
218 static int trust_o_cloexec
;
220 /* Mark FD as close-on-exec, ignoring errors. Update
224 mark_cloexec (int fd
)
227 int old
= fcntl (fd
, F_GETFD
, 0);
231 fcntl (fd
, F_SETFD
, old
| FD_CLOEXEC
);
233 if (trust_o_cloexec
== 0)
235 if ((old
& FD_CLOEXEC
) != 0)
238 trust_o_cloexec
= -1;
241 #endif /* HAVE_F_GETFD */
244 /* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec. */
247 maybe_mark_cloexec (int fd
)
249 if (trust_o_cloexec
<= 0)
253 /* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC. */
256 socket_mark_cloexec (int fd
)
258 if (SOCK_CLOEXEC
== 0 || trust_o_cloexec
<= 0)
264 /* See filestuff.h. */
267 gdb_open_cloexec (const char *filename
, int flags
, unsigned long mode
)
269 int fd
= open (filename
, flags
| O_CLOEXEC
, mode
);
272 maybe_mark_cloexec (fd
);
277 /* See filestuff.h. */
280 gdb_fopen_cloexec (const char *filename
, const char *opentype
)
283 static int fopen_e_ever_failed
;
285 if (!fopen_e_ever_failed
)
289 copy
= alloca (strlen (opentype
) + 2);
290 strcpy (copy
, opentype
);
291 /* This is a glibc extension but we try it unconditionally on
294 result
= fopen (filename
, copy
);
300 result
= fopen (filename
, opentype
);
302 fopen_e_ever_failed
= 1;
306 maybe_mark_cloexec (fileno (result
));
311 /* See filestuff.h. */
314 gdb_socketpair_cloexec (int namespace, int style
, int protocol
, int filedes
[2])
316 #ifdef HAVE_SOCKETPAIR
317 int result
= socketpair (namespace, style
| SOCK_CLOEXEC
, protocol
, filedes
);
321 socket_mark_cloexec (filedes
[0]);
322 socket_mark_cloexec (filedes
[1]);
327 gdb_assert_not_reached (_("socketpair not available on this host"));
331 /* See filestuff.h. */
334 gdb_socket_cloexec (int namespace, int style
, int protocol
)
336 int result
= socket (namespace, style
| SOCK_CLOEXEC
, protocol
);
339 socket_mark_cloexec (result
);
344 /* See filestuff.h. */
347 gdb_pipe_cloexec (int filedes
[2])
352 result
= pipe2 (filedes
, O_CLOEXEC
);
355 maybe_mark_cloexec (filedes
[0]);
356 maybe_mark_cloexec (filedes
[1]);
360 result
= pipe (filedes
);
363 mark_cloexec (filedes
[0]);
364 mark_cloexec (filedes
[1]);
366 #else /* HAVE_PIPE */
367 gdb_assert_not_reached (_("pipe not available on this host"));
368 #endif /* HAVE_PIPE */
369 #endif /* HAVE_PIPE2 */
This page took 0.059401 seconds and 5 git commands to generate.