ignore invalid DOF provider sections
[binutils-gdb.git] / gdb / common / filestuff.c
blob25ea8fa61b4fe81b04e1d03b8fb8387d5cb4fa4b
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"
21 #include "gdb_vecs.h"
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
27 #ifdef USE_WIN32API
28 #include <winsock2.h>
29 #include <windows.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
36 #endif
38 #ifdef HAVE_SYS_RESOURCE_H
39 #include <sys/resource.h>
40 #endif /* HAVE_SYS_RESOURCE_H */
42 #ifndef O_CLOEXEC
43 #define O_CLOEXEC 0
44 #endif
46 #ifndef SOCK_CLOEXEC
47 #define SOCK_CLOEXEC 0
48 #endif
52 #ifndef HAVE_FDWALK
54 #include <dirent.h>
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
61 finished. */
63 static int
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
68 configure. */
69 #ifdef __linux__
70 DIR *dir;
72 dir = opendir ("/proc/self/fd");
73 if (dir != NULL)
75 struct dirent *entry;
76 int result = 0;
78 for (entry = readdir (dir); entry != NULL; entry = readdir (dir))
80 long fd;
81 char *tail;
82 int result;
84 errno = 0;
85 fd = strtol (entry->d_name, &tail, 10);
86 if (*tail != '\0' || errno != 0)
87 continue;
88 if ((int) fd != fd)
90 /* What can we do here really? */
91 continue;
94 if (fd == dirfd (dir))
95 continue;
97 result = func (arg, fd);
98 if (result != 0)
99 break;
102 closedir (dir);
103 return result;
105 /* We may fall through to the next case. */
106 #endif
109 int max, fd;
111 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
112 struct rlimit rlim;
114 if (getrlimit (RLIMIT_NOFILE, &rlim) == 0 && rlim.rlim_max != RLIM_INFINITY)
115 max = rlim.rlim_max;
116 else
117 #endif
119 #ifdef _SC_OPEN_MAX
120 max = sysconf (_SC_OPEN_MAX);
121 #else
122 /* Whoops. */
123 return 0;
124 #endif /* _SC_OPEN_MAX */
127 for (fd = 0; fd < max; ++fd)
129 struct stat sb;
130 int result;
132 /* Only call FUNC for open fds. */
133 if (fstat (fd, &sb) == -1)
134 continue;
136 result = func (arg, fd);
137 if (result != 0)
138 return result;
141 return 0;
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. */
158 static int
159 do_mark_open_fd (void *ignore, int fd)
161 VEC_safe_push (int, open_fds, fd);
162 return 0;
165 /* See filestuff.h. */
167 void
168 notice_open_fds (void)
170 fdwalk (do_mark_open_fd, NULL);
173 /* See filestuff.h. */
175 void
176 mark_fd_no_cloexec (int fd)
178 do_mark_open_fd (NULL, fd);
181 /* See filestuff.h. */
183 void
184 unmark_fd_no_cloexec (int fd)
186 int i, val;
188 for (i = 0; VEC_iterate (int, open_fds, i, val); ++i)
190 if (fd == val)
192 VEC_unordered_remove (int, open_fds, i);
193 return;
197 gdb_assert_not_reached (_("fd not found in open_fds"));
200 /* Helper function for close_most_fds that closes the file descriptor
201 if appropriate. */
203 static int
204 do_close (void *ignore, int fd)
206 int i, val;
208 for (i = 0; VEC_iterate (int, open_fds, i, val); ++i)
210 if (fd == val)
212 /* Keep this one open. */
213 return 0;
217 close (fd);
218 return 0;
221 /* See filestuff.h. */
223 void
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
241 TRUST_O_CLOEXEC. */
243 static void
244 mark_cloexec (int fd)
246 #ifdef HAVE_F_GETFD
247 int old = fcntl (fd, F_GETFD, 0);
249 if (old != -1)
251 fcntl (fd, F_SETFD, old | FD_CLOEXEC);
253 if (trust_o_cloexec == 0)
255 if ((old & FD_CLOEXEC) != 0)
256 trust_o_cloexec = 1;
257 else
258 trust_o_cloexec = -1;
261 #endif /* HAVE_F_GETFD */
264 /* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec. */
266 static void
267 maybe_mark_cloexec (int fd)
269 if (trust_o_cloexec <= 0)
270 mark_cloexec (fd);
273 #ifdef HAVE_SOCKETS
275 /* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC. */
277 static void
278 socket_mark_cloexec (int fd)
280 if (SOCK_CLOEXEC == 0 || trust_o_cloexec <= 0)
281 mark_cloexec (fd);
284 #endif
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);
295 if (fd >= 0)
296 maybe_mark_cloexec (fd);
298 return fd;
301 /* See filestuff.h. */
303 FILE *
304 gdb_fopen_cloexec (const char *filename, const char *opentype)
306 FILE *result;
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
312 supported. */
313 static int fopen_e_ever_failed_einval = O_CLOEXEC == 0;
315 if (!fopen_e_ever_failed_einval)
317 char *copy;
319 copy = alloca (strlen (opentype) + 2);
320 strcpy (copy, opentype);
321 /* This is a glibc extension but we try it unconditionally on
322 this path. */
323 strcat (copy, "e");
324 result = fopen (filename, copy);
326 if (result == NULL && errno == EINVAL)
328 result = fopen (filename, opentype);
329 if (result != NULL)
330 fopen_e_ever_failed_einval = 1;
333 else
334 result = fopen (filename, opentype);
336 if (result != NULL)
337 maybe_mark_cloexec (fileno (result));
339 return result;
342 #ifdef HAVE_SOCKETS
343 /* See filestuff.h. */
346 gdb_socketpair_cloexec (int domain, int style, int protocol,
347 int filedes[2])
349 #ifdef HAVE_SOCKETPAIR
350 int result = socketpair (domain, style | SOCK_CLOEXEC, protocol, filedes);
352 if (result != -1)
354 socket_mark_cloexec (filedes[0]);
355 socket_mark_cloexec (filedes[1]);
358 return result;
359 #else
360 gdb_assert_not_reached (_("socketpair not available on this host"));
361 #endif
364 /* See filestuff.h. */
367 gdb_socket_cloexec (int domain, int style, int protocol)
369 int result = socket (domain, style | SOCK_CLOEXEC, protocol);
371 if (result != -1)
372 socket_mark_cloexec (result);
374 return result;
376 #endif
378 /* See filestuff.h. */
381 gdb_pipe_cloexec (int filedes[2])
383 int result;
385 #ifdef HAVE_PIPE2
386 result = pipe2 (filedes, O_CLOEXEC);
387 if (result != -1)
389 maybe_mark_cloexec (filedes[0]);
390 maybe_mark_cloexec (filedes[1]);
392 #else
393 #ifdef HAVE_PIPE
394 result = pipe (filedes);
395 if (result != -1)
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 */
405 return result;
408 /* Helper function which does the work for make_cleanup_close. */
410 static void
411 do_close_cleanup (void *arg)
413 int *fd = arg;
415 close (*fd);
418 /* See cleanup-utils.h. */
420 struct cleanup *
421 make_cleanup_close (int fd)
423 int *saved_fd = xmalloc (sizeof (fd));
425 *saved_fd = fd;
426 return make_cleanup_dtor (do_close_cleanup, saved_fd, xfree);