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/socket.h>
32 #include <sys/types.h>
35 #ifdef HAVE_SYS_RESOURCE_H
36 #include <sys/resource.h>
37 #endif /* HAVE_SYS_RESOURCE_H */
44 #define SOCK_CLOEXEC 0
53 /* Replacement for fdwalk, if the system doesn't define it. Walks all
54 open file descriptors (though this implementation may walk closed
55 ones as well, depending on the host platform's capabilities) and
56 call FUNC with ARG. If FUNC returns non-zero, stops immediately
57 and returns the same value. Otherwise, returns zero when
61 fdwalk (int (*func
) (void *, int), void *arg
)
63 /* Checking __linux__ isn't great but it isn't clear what would be
64 better. There doesn't seem to be a good way to check for this in
69 dir
= opendir ("/proc/self/fd");
75 for (entry
= readdir (dir
); entry
!= NULL
; entry
= readdir (dir
))
82 fd
= strtol (entry
->d_name
, &tail
, 10);
83 if (*tail
!= '\0' || errno
!= 0)
87 /* What can we do here really? */
91 if (fd
== dirfd (dir
))
94 result
= func (arg
, fd
);
102 /* We may fall through to the next case. */
108 #ifdef HAVE_GETRLIMIT
111 if (getrlimit (RLIMIT_NOFILE
, &rlim
) == 0 && rlim
.rlim_max
!= RLIM_INFINITY
)
117 max
= sysconf (_SC_OPEN_MAX
);
121 #endif /* _SC_OPEN_MAX */
124 for (fd
= 0; fd
< max
; ++fd
)
129 /* Only call FUNC for open fds. */
130 if (fstat (fd
, &sb
) == -1)
133 result
= func (arg
, fd
);
142 #endif /* HAVE_FDWALK */
146 /* A VEC holding all the fds open when notice_open_fds was called. We
147 don't use a hashtab because libiberty isn't linked into gdbserver;
148 and anyway we don't expect there to be many open fds. */
152 static VEC (int) *open_fds
;
154 /* An fdwalk callback function used by notice_open_fds. It puts the
155 given file descriptor into the vec. */
158 do_mark_open_fd (void *ignore
, int fd
)
160 VEC_safe_push (int, open_fds
, fd
);
164 /* See filestuff.h. */
167 notice_open_fds (void)
169 fdwalk (do_mark_open_fd
, NULL
);
172 /* Helper function for close_most_fds that closes the file descriptor
176 do_close (void *ignore
, int fd
)
180 for (i
= 0; VEC_iterate (int, open_fds
, i
, val
); ++i
)
184 /* Keep this one open. */
193 /* See filestuff.h. */
196 close_most_fds (void)
198 fdwalk (do_close
, NULL
);
203 /* This is a tri-state flag. When zero it means we haven't yet tried
204 O_CLOEXEC. When positive it means that O_CLOEXEC works on this
205 host. When negative, it means that O_CLOEXEC doesn't work. We
206 track this state because, while gdb might have been compiled
207 against a libc that supplies O_CLOEXEC, there is no guarantee that
208 the kernel supports it. */
210 static int trust_o_cloexec
;
212 /* Mark FD as close-on-exec, ignoring errors. Update
216 mark_cloexec (int fd
)
218 int old
= fcntl (fd
, F_GETFD
, 0);
222 fcntl (fd
, F_SETFD
, old
| FD_CLOEXEC
);
224 if (trust_o_cloexec
== 0)
226 if ((old
& FD_CLOEXEC
) != 0)
229 trust_o_cloexec
= -1;
234 /* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec. */
237 maybe_mark_cloexec (int fd
)
239 if (trust_o_cloexec
<= 0)
243 /* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC. */
246 socket_mark_cloexec (int fd
)
248 if (SOCK_CLOEXEC
== 0 || trust_o_cloexec
<= 0)
254 /* See filestuff.h. */
257 gdb_open_cloexec (const char *filename
, int flags
, mode_t mode
)
259 int fd
= open (filename
, flags
| O_CLOEXEC
, mode
);
262 maybe_mark_cloexec (fd
);
267 /* See filestuff.h. */
270 gdb_fopen_cloexec (const char *filename
, const char *opentype
)
273 static int fopen_e_ever_failed
;
275 if (!fopen_e_ever_failed
)
279 copy
= alloca (strlen (opentype
) + 2);
280 strcpy (copy
, opentype
);
281 /* This is a glibc extension but we try it unconditionally on
284 result
= fopen (filename
, copy
);
290 result
= fopen (filename
, opentype
);
292 fopen_e_ever_failed
= 1;
296 maybe_mark_cloexec (fileno (result
));
301 /* See filestuff.h. */
304 gdb_socketpair_cloexec (int namespace, int style
, int protocol
, int filedes
[2])
306 int result
= socketpair (namespace, style
| SOCK_CLOEXEC
, protocol
, filedes
);
310 socket_mark_cloexec (filedes
[0]);
311 socket_mark_cloexec (filedes
[1]);
317 /* See filestuff.h. */
320 gdb_socket_cloexec (int namespace, int style
, int protocol
)
322 int result
= socket (namespace, style
| SOCK_CLOEXEC
, protocol
);
325 socket_mark_cloexec (result
);
330 /* See filestuff.h. */
333 gdb_pipe_cloexec (int filedes
[2])
338 result
= pipe2 (filedes
, O_CLOEXEC
);
341 maybe_mark_cloexec (filedes
[0]);
342 maybe_mark_cloexec (filedes
[1]);
345 result
= pipe (filedes
);
348 mark_cloexec (filedes
[0]);
349 mark_cloexec (filedes
[1]);
This page took 0.041528 seconds and 4 git commands to generate.