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