Add some more casts (1/2)
[external/binutils.git] / gdb / common / filestuff.c
1 /* Low-level file-handling.
2    Copyright (C) 2012-2015 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 #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>
26
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
37
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
54 #include <dirent.h>
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
63 static int
64 fdwalk (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
111 #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
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
153 static 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
158 static int
159 do_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
167 void
168 notice_open_fds (void)
169 {
170   fdwalk (do_mark_open_fd, NULL);
171 }
172
173 /* See filestuff.h.  */
174
175 void
176 mark_fd_no_cloexec (int fd)
177 {
178   do_mark_open_fd (NULL, fd);
179 }
180
181 /* See filestuff.h.  */
182
183 void
184 unmark_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
200 /* Helper function for close_most_fds that closes the file descriptor
201    if appropriate.  */
202
203 static int
204 do_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
223 void
224 close_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
238 static int trust_o_cloexec;
239
240 /* Mark FD as close-on-exec, ignoring errors.  Update
241    TRUST_O_CLOEXEC.  */
242
243 static void
244 mark_cloexec (int fd)
245 {
246 #ifdef HAVE_F_GETFD
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     }
261 #endif /* HAVE_F_GETFD */
262 }
263
264 /* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec.  */
265
266 static void
267 maybe_mark_cloexec (int fd)
268 {
269   if (trust_o_cloexec <= 0)
270     mark_cloexec (fd);
271 }
272
273 #ifdef HAVE_SOCKETS
274
275 /* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC.  */
276
277 static void
278 socket_mark_cloexec (int fd)
279 {
280   if (SOCK_CLOEXEC == 0 || trust_o_cloexec <= 0)
281     mark_cloexec (fd);
282 }
283
284 #endif
285
286 \f
287
288 /* See filestuff.h.  */
289
290 int
291 gdb_open_cloexec (const char *filename, int flags, unsigned long mode)
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
303 FILE *
304 gdb_fopen_cloexec (const char *filename, const char *opentype)
305 {
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;
314
315   if (!fopen_e_ever_failed_einval)
316     {
317       char *copy;
318
319       copy = (char *) 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);
325
326       if (result == NULL && errno == EINVAL)
327         {
328           result = fopen (filename, opentype);
329           if (result != NULL)
330             fopen_e_ever_failed_einval = 1;
331         }
332     }
333   else
334     result = fopen (filename, opentype);
335
336   if (result != NULL)
337     maybe_mark_cloexec (fileno (result));
338
339   return result;
340 }
341
342 #ifdef HAVE_SOCKETS
343 /* See filestuff.h.  */
344
345 int
346 gdb_socketpair_cloexec (int domain, int style, int protocol,
347                         int filedes[2])
348 {
349 #ifdef HAVE_SOCKETPAIR
350   int result = socketpair (domain, style | SOCK_CLOEXEC, protocol, filedes);
351
352   if (result != -1)
353     {
354       socket_mark_cloexec (filedes[0]);
355       socket_mark_cloexec (filedes[1]);
356     }
357
358   return result;
359 #else
360   gdb_assert_not_reached (_("socketpair not available on this host"));
361 #endif
362 }
363
364 /* See filestuff.h.  */
365
366 int
367 gdb_socket_cloexec (int domain, int style, int protocol)
368 {
369   int result = socket (domain, style | SOCK_CLOEXEC, protocol);
370
371   if (result != -1)
372     socket_mark_cloexec (result);
373
374   return result;
375 }
376 #endif
377
378 /* See filestuff.h.  */
379
380 int
381 gdb_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
393 #ifdef HAVE_PIPE
394   result = pipe (filedes);
395   if (result != -1)
396     {
397       mark_cloexec (filedes[0]);
398       mark_cloexec (filedes[1]);
399     }
400 #else /* HAVE_PIPE */
401   gdb_assert_not_reached (_("pipe not available on this host"));
402 #endif /* HAVE_PIPE */
403 #endif /* HAVE_PIPE2 */
404
405   return result;
406 }
407
408 /* Helper function which does the work for make_cleanup_close.  */
409
410 static void
411 do_close_cleanup (void *arg)
412 {
413   int *fd = (int *) arg;
414
415   close (*fd);
416 }
417
418 /* See cleanup-utils.h.  */
419
420 struct cleanup *
421 make_cleanup_close (int fd)
422 {
423   int *saved_fd = XNEW (int);
424
425   *saved_fd = fd;
426   return make_cleanup_dtor (do_close_cleanup, saved_fd, xfree);
427 }