dwarf2read.c: Don't assume uint32_t is unsigned int on all hosts.
[deliverable/binutils-gdb.git] / gdb / common / filestuff.c
CommitLineData
614c279d
TT
1/* Low-level file-handling.
2 Copyright (C) 2012, 2013 Free Software Foundation, Inc.
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
19#ifdef GDBSERVER
20#include "server.h"
21#else
22#include "defs.h"
23#include "gdb_string.h"
24#endif
25#include "filestuff.h"
26#include "gdb_vecs.h"
27
28#include <string.h>
29#include <fcntl.h>
30#include <unistd.h>
614c279d 31#include <sys/types.h>
366c6766 32#include "gdb_stat.h"
614c279d 33
5d71132c
TT
34#ifdef USE_WIN32API
35#include <winsock2.h>
36#include <windows.h>
37#else
38#include <sys/socket.h>
39/* Define HAVE_F_GETFD if we plan to use F_GETFD. */
40#define HAVE_F_GETFD F_GETFD
41#endif
42
614c279d
TT
43#ifdef HAVE_SYS_RESOURCE_H
44#include <sys/resource.h>
45#endif /* HAVE_SYS_RESOURCE_H */
46
47#ifndef O_CLOEXEC
48#define O_CLOEXEC 0
49#endif
50
51#ifndef SOCK_CLOEXEC
52#define SOCK_CLOEXEC 0
53#endif
54
55\f
56
57#ifndef HAVE_FDWALK
58
01da98f9 59#include "gdb_dirent.h"
614c279d
TT
60
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
66 finished. */
67
68static int
69fdwalk (int (*func) (void *, int), void *arg)
70{
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
73 configure. */
74#ifdef __linux__
75 DIR *dir;
76
77 dir = opendir ("/proc/self/fd");
78 if (dir != NULL)
79 {
80 struct dirent *entry;
81 int result = 0;
82
83 for (entry = readdir (dir); entry != NULL; entry = readdir (dir))
84 {
85 long fd;
86 char *tail;
87 int result;
88
89 errno = 0;
90 fd = strtol (entry->d_name, &tail, 10);
91 if (*tail != '\0' || errno != 0)
92 continue;
93 if ((int) fd != fd)
94 {
95 /* What can we do here really? */
96 continue;
97 }
98
99 if (fd == dirfd (dir))
100 continue;
101
102 result = func (arg, fd);
103 if (result != 0)
104 break;
105 }
106
107 closedir (dir);
108 return result;
109 }
110 /* We may fall through to the next case. */
111#endif
112
113 {
114 int max, fd;
115
116#ifdef HAVE_GETRLIMIT
117 struct rlimit rlim;
118
119 if (getrlimit (RLIMIT_NOFILE, &rlim) == 0 && rlim.rlim_max != RLIM_INFINITY)
120 max = rlim.rlim_max;
121 else
122#endif
123 {
124#ifdef _SC_OPEN_MAX
125 max = sysconf (_SC_OPEN_MAX);
126#else
127 /* Whoops. */
128 return 0;
129#endif /* _SC_OPEN_MAX */
130 }
131
132 for (fd = 0; fd < max; ++fd)
133 {
134 struct stat sb;
135 int result;
136
137 /* Only call FUNC for open fds. */
138 if (fstat (fd, &sb) == -1)
139 continue;
140
141 result = func (arg, fd);
142 if (result != 0)
143 return result;
144 }
145
146 return 0;
147 }
148}
149
150#endif /* HAVE_FDWALK */
151
152\f
153
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. */
157
158DEF_VEC_I (int);
159
160static VEC (int) *open_fds;
161
162/* An fdwalk callback function used by notice_open_fds. It puts the
163 given file descriptor into the vec. */
164
165static int
166do_mark_open_fd (void *ignore, int fd)
167{
168 VEC_safe_push (int, open_fds, fd);
169 return 0;
170}
171
172/* See filestuff.h. */
173
174void
175notice_open_fds (void)
176{
177 fdwalk (do_mark_open_fd, NULL);
178}
179
21ff4686
TT
180/* See filestuff.h. */
181
182void
183mark_fd_no_cloexec (int fd)
184{
185 do_mark_open_fd (NULL, fd);
186}
187
188/* See filestuff.h. */
189
190void
191unmark_fd_no_cloexec (int fd)
192{
193 int i, val;
194
195 for (i = 0; VEC_iterate (int, open_fds, i, val); ++i)
196 {
197 if (fd == val)
198 {
199 VEC_unordered_remove (int, open_fds, i);
200 return;
201 }
202 }
203
204 gdb_assert_not_reached (_("fd not found in open_fds"));
205}
206
614c279d
TT
207/* Helper function for close_most_fds that closes the file descriptor
208 if appropriate. */
209
210static int
211do_close (void *ignore, int fd)
212{
213 int i, val;
214
215 for (i = 0; VEC_iterate (int, open_fds, i, val); ++i)
216 {
217 if (fd == val)
218 {
219 /* Keep this one open. */
220 return 0;
221 }
222 }
223
224 close (fd);
225 return 0;
226}
227
228/* See filestuff.h. */
229
230void
231close_most_fds (void)
232{
233 fdwalk (do_close, NULL);
234}
235
236\f
237
238/* This is a tri-state flag. When zero it means we haven't yet tried
239 O_CLOEXEC. When positive it means that O_CLOEXEC works on this
240 host. When negative, it means that O_CLOEXEC doesn't work. We
241 track this state because, while gdb might have been compiled
242 against a libc that supplies O_CLOEXEC, there is no guarantee that
243 the kernel supports it. */
244
245static int trust_o_cloexec;
246
247/* Mark FD as close-on-exec, ignoring errors. Update
248 TRUST_O_CLOEXEC. */
249
250static void
251mark_cloexec (int fd)
252{
5d71132c 253#ifdef HAVE_F_GETFD
614c279d
TT
254 int old = fcntl (fd, F_GETFD, 0);
255
256 if (old != -1)
257 {
258 fcntl (fd, F_SETFD, old | FD_CLOEXEC);
259
260 if (trust_o_cloexec == 0)
261 {
262 if ((old & FD_CLOEXEC) != 0)
263 trust_o_cloexec = 1;
264 else
265 trust_o_cloexec = -1;
266 }
267 }
5d71132c 268#endif /* HAVE_F_GETFD */
614c279d
TT
269}
270
271/* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec. */
272
273static void
274maybe_mark_cloexec (int fd)
275{
276 if (trust_o_cloexec <= 0)
277 mark_cloexec (fd);
278}
279
280/* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC. */
281
282static void
283socket_mark_cloexec (int fd)
284{
285 if (SOCK_CLOEXEC == 0 || trust_o_cloexec <= 0)
286 mark_cloexec (fd);
287}
288
289\f
290
291/* See filestuff.h. */
292
293int
5d71132c 294gdb_open_cloexec (const char *filename, int flags, unsigned long mode)
614c279d
TT
295{
296 int fd = open (filename, flags | O_CLOEXEC, mode);
297
298 if (fd >= 0)
299 maybe_mark_cloexec (fd);
300
301 return fd;
302}
303
304/* See filestuff.h. */
305
306FILE *
307gdb_fopen_cloexec (const char *filename, const char *opentype)
308{
309 FILE *result = NULL;
310 static int fopen_e_ever_failed;
311
312 if (!fopen_e_ever_failed)
313 {
314 char *copy;
315
316 copy = alloca (strlen (opentype) + 2);
317 strcpy (copy, opentype);
318 /* This is a glibc extension but we try it unconditionally on
319 this path. */
320 strcat (copy, "e");
321 result = fopen (filename, copy);
322 }
323
324 if (result == NULL)
325 {
326 /* Fallback. */
327 result = fopen (filename, opentype);
328 if (result != NULL)
329 fopen_e_ever_failed = 1;
330 }
331
332 if (result != NULL)
333 maybe_mark_cloexec (fileno (result));
334
335 return result;
336}
337
338/* See filestuff.h. */
339
340int
341gdb_socketpair_cloexec (int namespace, int style, int protocol, int filedes[2])
342{
5d71132c 343#ifdef HAVE_SOCKETPAIR
614c279d
TT
344 int result = socketpair (namespace, style | SOCK_CLOEXEC, protocol, filedes);
345
346 if (result != -1)
347 {
348 socket_mark_cloexec (filedes[0]);
349 socket_mark_cloexec (filedes[1]);
350 }
351
352 return result;
5d71132c
TT
353#else
354 gdb_assert_not_reached (_("socketpair not available on this host"));
355#endif
614c279d
TT
356}
357
358/* See filestuff.h. */
359
360int
361gdb_socket_cloexec (int namespace, int style, int protocol)
362{
363 int result = socket (namespace, style | SOCK_CLOEXEC, protocol);
364
365 if (result != -1)
366 socket_mark_cloexec (result);
367
368 return result;
369}
370
371/* See filestuff.h. */
372
373int
374gdb_pipe_cloexec (int filedes[2])
375{
376 int result;
377
378#ifdef HAVE_PIPE2
379 result = pipe2 (filedes, O_CLOEXEC);
380 if (result != -1)
381 {
382 maybe_mark_cloexec (filedes[0]);
383 maybe_mark_cloexec (filedes[1]);
384 }
385#else
5d71132c 386#ifdef HAVE_PIPE
614c279d
TT
387 result = pipe (filedes);
388 if (result != -1)
389 {
390 mark_cloexec (filedes[0]);
391 mark_cloexec (filedes[1]);
392 }
5d71132c
TT
393#else /* HAVE_PIPE */
394 gdb_assert_not_reached (_("pipe not available on this host"));
395#endif /* HAVE_PIPE */
396#endif /* HAVE_PIPE2 */
614c279d
TT
397
398 return result;
399}
This page took 0.044976 seconds and 4 git commands to generate.