windows-nat: Also ignore ERROR_INVALID_HANDLE from SuspendThread()
[deliverable/binutils-gdb.git] / gdb / common / filestuff.c
CommitLineData
614c279d 1/* Low-level file-handling.
32d0add0 2 Copyright (C) 2012-2015 Free Software Foundation, Inc.
614c279d
TT
3
4 This file is part of GDB.
5
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.
10
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.
15
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/>. */
18
727605ca 19#include "common-defs.h"
614c279d
TT
20#include "filestuff.h"
21#include "gdb_vecs.h"
614c279d
TT
22#include <fcntl.h>
23#include <unistd.h>
614c279d 24#include <sys/types.h>
53ce3c39 25#include <sys/stat.h>
614c279d 26
5d71132c
TT
27#ifdef USE_WIN32API
28#include <winsock2.h>
29#include <windows.h>
8658d16d
PA
30#define HAVE_SOCKETS 1
31#elif defined HAVE_SYS_SOCKET_H
5d71132c
TT
32#include <sys/socket.h>
33/* Define HAVE_F_GETFD if we plan to use F_GETFD. */
34#define HAVE_F_GETFD F_GETFD
8658d16d 35#define HAVE_SOCKETS 1
5d71132c
TT
36#endif
37
614c279d
TT
38#ifdef HAVE_SYS_RESOURCE_H
39#include <sys/resource.h>
40#endif /* HAVE_SYS_RESOURCE_H */
41
42#ifndef O_CLOEXEC
43#define O_CLOEXEC 0
44#endif
45
46#ifndef SOCK_CLOEXEC
47#define SOCK_CLOEXEC 0
48#endif
49
50\f
51
52#ifndef HAVE_FDWALK
53
2978b111 54#include <dirent.h>
614c279d
TT
55
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. */
62
63static int
64fdwalk (int (*func) (void *, int), void *arg)
65{
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;
71
72 dir = opendir ("/proc/self/fd");
73 if (dir != NULL)
74 {
75 struct dirent *entry;
76 int result = 0;
77
78 for (entry = readdir (dir); entry != NULL; entry = readdir (dir))
79 {
80 long fd;
81 char *tail;
82 int result;
83
84 errno = 0;
85 fd = strtol (entry->d_name, &tail, 10);
86 if (*tail != '\0' || errno != 0)
87 continue;
88 if ((int) fd != fd)
89 {
90 /* What can we do here really? */
91 continue;
92 }
93
94 if (fd == dirfd (dir))
95 continue;
96
97 result = func (arg, fd);
98 if (result != 0)
99 break;
100 }
101
102 closedir (dir);
103 return result;
104 }
105 /* We may fall through to the next case. */
106#endif
107
108 {
109 int max, fd;
110
f9b0da3d 111#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
614c279d
TT
112 struct rlimit rlim;
113
114 if (getrlimit (RLIMIT_NOFILE, &rlim) == 0 && rlim.rlim_max != RLIM_INFINITY)
115 max = rlim.rlim_max;
116 else
117#endif
118 {
119#ifdef _SC_OPEN_MAX
120 max = sysconf (_SC_OPEN_MAX);
121#else
122 /* Whoops. */
123 return 0;
124#endif /* _SC_OPEN_MAX */
125 }
126
127 for (fd = 0; fd < max; ++fd)
128 {
129 struct stat sb;
130 int result;
131
132 /* Only call FUNC for open fds. */
133 if (fstat (fd, &sb) == -1)
134 continue;
135
136 result = func (arg, fd);
137 if (result != 0)
138 return result;
139 }
140
141 return 0;
142 }
143}
144
145#endif /* HAVE_FDWALK */
146
147\f
148
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. */
152
614c279d
TT
153static VEC (int) *open_fds;
154
155/* An fdwalk callback function used by notice_open_fds. It puts the
156 given file descriptor into the vec. */
157
158static int
159do_mark_open_fd (void *ignore, int fd)
160{
161 VEC_safe_push (int, open_fds, fd);
162 return 0;
163}
164
165/* See filestuff.h. */
166
167void
168notice_open_fds (void)
169{
170 fdwalk (do_mark_open_fd, NULL);
171}
172
21ff4686
TT
173/* See filestuff.h. */
174
175void
176mark_fd_no_cloexec (int fd)
177{
178 do_mark_open_fd (NULL, fd);
179}
180
181/* See filestuff.h. */
182
183void
184unmark_fd_no_cloexec (int fd)
185{
186 int i, val;
187
188 for (i = 0; VEC_iterate (int, open_fds, i, val); ++i)
189 {
190 if (fd == val)
191 {
192 VEC_unordered_remove (int, open_fds, i);
193 return;
194 }
195 }
196
197 gdb_assert_not_reached (_("fd not found in open_fds"));
198}
199
614c279d
TT
200/* Helper function for close_most_fds that closes the file descriptor
201 if appropriate. */
202
203static int
204do_close (void *ignore, int fd)
205{
206 int i, val;
207
208 for (i = 0; VEC_iterate (int, open_fds, i, val); ++i)
209 {
210 if (fd == val)
211 {
212 /* Keep this one open. */
213 return 0;
214 }
215 }
216
217 close (fd);
218 return 0;
219}
220
221/* See filestuff.h. */
222
223void
224close_most_fds (void)
225{
226 fdwalk (do_close, NULL);
227}
228
229\f
230
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. */
237
238static int trust_o_cloexec;
239
240/* Mark FD as close-on-exec, ignoring errors. Update
241 TRUST_O_CLOEXEC. */
242
243static void
244mark_cloexec (int fd)
245{
5d71132c 246#ifdef HAVE_F_GETFD
614c279d
TT
247 int old = fcntl (fd, F_GETFD, 0);
248
249 if (old != -1)
250 {
251 fcntl (fd, F_SETFD, old | FD_CLOEXEC);
252
253 if (trust_o_cloexec == 0)
254 {
255 if ((old & FD_CLOEXEC) != 0)
256 trust_o_cloexec = 1;
257 else
258 trust_o_cloexec = -1;
259 }
260 }
5d71132c 261#endif /* HAVE_F_GETFD */
614c279d
TT
262}
263
264/* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec. */
265
266static void
267maybe_mark_cloexec (int fd)
268{
269 if (trust_o_cloexec <= 0)
270 mark_cloexec (fd);
271}
272
8658d16d
PA
273#ifdef HAVE_SOCKETS
274
614c279d
TT
275/* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC. */
276
277static void
278socket_mark_cloexec (int fd)
279{
280 if (SOCK_CLOEXEC == 0 || trust_o_cloexec <= 0)
281 mark_cloexec (fd);
282}
283
8658d16d
PA
284#endif
285
614c279d
TT
286\f
287
288/* See filestuff.h. */
289
290int
5d71132c 291gdb_open_cloexec (const char *filename, int flags, unsigned long mode)
614c279d
TT
292{
293 int fd = open (filename, flags | O_CLOEXEC, mode);
294
295 if (fd >= 0)
296 maybe_mark_cloexec (fd);
297
298 return fd;
299}
300
301/* See filestuff.h. */
302
303FILE *
304gdb_fopen_cloexec (const char *filename, const char *opentype)
305{
c74e1ccf 306 FILE *result;
88505fac
PM
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. */
c74e1ccf 313 static int fopen_e_ever_failed_einval = O_CLOEXEC == 0;
614c279d 314
c74e1ccf 315 if (!fopen_e_ever_failed_einval)
614c279d
TT
316 {
317 char *copy;
318
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);
614c279d 325
c74e1ccf
JK
326 if (result == NULL && errno == EINVAL)
327 {
328 result = fopen (filename, opentype);
329 if (result != NULL)
330 fopen_e_ever_failed_einval = 1;
331 }
614c279d 332 }
c74e1ccf
JK
333 else
334 result = fopen (filename, opentype);
614c279d
TT
335
336 if (result != NULL)
337 maybe_mark_cloexec (fileno (result));
338
339 return result;
340}
341
8658d16d 342#ifdef HAVE_SOCKETS
614c279d
TT
343/* See filestuff.h. */
344
345int
fe978cb0
PA
346gdb_socketpair_cloexec (int domain, int style, int protocol,
347 int filedes[2])
614c279d 348{
5d71132c 349#ifdef HAVE_SOCKETPAIR
fe978cb0 350 int result = socketpair (domain, style | SOCK_CLOEXEC, protocol, filedes);
614c279d
TT
351
352 if (result != -1)
353 {
354 socket_mark_cloexec (filedes[0]);
355 socket_mark_cloexec (filedes[1]);
356 }
357
358 return result;
5d71132c
TT
359#else
360 gdb_assert_not_reached (_("socketpair not available on this host"));
361#endif
614c279d
TT
362}
363
364/* See filestuff.h. */
365
366int
fe978cb0 367gdb_socket_cloexec (int domain, int style, int protocol)
614c279d 368{
fe978cb0 369 int result = socket (domain, style | SOCK_CLOEXEC, protocol);
614c279d
TT
370
371 if (result != -1)
372 socket_mark_cloexec (result);
373
374 return result;
375}
8658d16d 376#endif
614c279d
TT
377
378/* See filestuff.h. */
379
380int
381gdb_pipe_cloexec (int filedes[2])
382{
383 int result;
384
385#ifdef HAVE_PIPE2
386 result = pipe2 (filedes, O_CLOEXEC);
387 if (result != -1)
388 {
389 maybe_mark_cloexec (filedes[0]);
390 maybe_mark_cloexec (filedes[1]);
391 }
392#else
5d71132c 393#ifdef HAVE_PIPE
614c279d
TT
394 result = pipe (filedes);
395 if (result != -1)
396 {
397 mark_cloexec (filedes[0]);
398 mark_cloexec (filedes[1]);
399 }
5d71132c
TT
400#else /* HAVE_PIPE */
401 gdb_assert_not_reached (_("pipe not available on this host"));
402#endif /* HAVE_PIPE */
403#endif /* HAVE_PIPE2 */
614c279d
TT
404
405 return result;
406}
This page took 0.191114 seconds and 4 git commands to generate.