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