Commit | Line | Data |
---|---|---|
614c279d | 1 | /* Low-level file-handling. |
61baf725 | 2 | Copyright (C) 2012-2017 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> |
37269bc9 | 26 | #include <algorithm> |
614c279d | 27 | |
5d71132c TT |
28 | #ifdef USE_WIN32API |
29 | #include <winsock2.h> | |
30 | #include <windows.h> | |
8658d16d PA |
31 | #define HAVE_SOCKETS 1 |
32 | #elif defined HAVE_SYS_SOCKET_H | |
5d71132c TT |
33 | #include <sys/socket.h> |
34 | /* Define HAVE_F_GETFD if we plan to use F_GETFD. */ | |
35 | #define HAVE_F_GETFD F_GETFD | |
8658d16d | 36 | #define HAVE_SOCKETS 1 |
5d71132c TT |
37 | #endif |
38 | ||
614c279d TT |
39 | #ifdef HAVE_SYS_RESOURCE_H |
40 | #include <sys/resource.h> | |
41 | #endif /* HAVE_SYS_RESOURCE_H */ | |
42 | ||
43 | #ifndef O_CLOEXEC | |
44 | #define O_CLOEXEC 0 | |
45 | #endif | |
46 | ||
47 | #ifndef SOCK_CLOEXEC | |
48 | #define SOCK_CLOEXEC 0 | |
49 | #endif | |
50 | ||
51 | \f | |
52 | ||
53 | #ifndef HAVE_FDWALK | |
54 | ||
2978b111 | 55 | #include <dirent.h> |
614c279d TT |
56 | |
57 | /* Replacement for fdwalk, if the system doesn't define it. Walks all | |
58 | open file descriptors (though this implementation may walk closed | |
59 | ones as well, depending on the host platform's capabilities) and | |
60 | call FUNC with ARG. If FUNC returns non-zero, stops immediately | |
61 | and returns the same value. Otherwise, returns zero when | |
62 | finished. */ | |
63 | ||
64 | static int | |
65 | fdwalk (int (*func) (void *, int), void *arg) | |
66 | { | |
67 | /* Checking __linux__ isn't great but it isn't clear what would be | |
68 | better. There doesn't seem to be a good way to check for this in | |
69 | configure. */ | |
70 | #ifdef __linux__ | |
71 | DIR *dir; | |
72 | ||
73 | dir = opendir ("/proc/self/fd"); | |
74 | if (dir != NULL) | |
75 | { | |
76 | struct dirent *entry; | |
77 | int result = 0; | |
78 | ||
79 | for (entry = readdir (dir); entry != NULL; entry = readdir (dir)) | |
80 | { | |
81 | long fd; | |
82 | char *tail; | |
83 | int result; | |
84 | ||
85 | errno = 0; | |
86 | fd = strtol (entry->d_name, &tail, 10); | |
87 | if (*tail != '\0' || errno != 0) | |
88 | continue; | |
89 | if ((int) fd != fd) | |
90 | { | |
91 | /* What can we do here really? */ | |
92 | continue; | |
93 | } | |
94 | ||
95 | if (fd == dirfd (dir)) | |
96 | continue; | |
97 | ||
98 | result = func (arg, fd); | |
99 | if (result != 0) | |
100 | break; | |
101 | } | |
102 | ||
103 | closedir (dir); | |
104 | return result; | |
105 | } | |
106 | /* We may fall through to the next case. */ | |
107 | #endif | |
108 | ||
109 | { | |
110 | int max, fd; | |
111 | ||
f9b0da3d | 112 | #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE) |
614c279d TT |
113 | struct rlimit rlim; |
114 | ||
115 | if (getrlimit (RLIMIT_NOFILE, &rlim) == 0 && rlim.rlim_max != RLIM_INFINITY) | |
116 | max = rlim.rlim_max; | |
117 | else | |
118 | #endif | |
119 | { | |
120 | #ifdef _SC_OPEN_MAX | |
121 | max = sysconf (_SC_OPEN_MAX); | |
122 | #else | |
123 | /* Whoops. */ | |
124 | return 0; | |
125 | #endif /* _SC_OPEN_MAX */ | |
126 | } | |
127 | ||
128 | for (fd = 0; fd < max; ++fd) | |
129 | { | |
130 | struct stat sb; | |
131 | int result; | |
132 | ||
133 | /* Only call FUNC for open fds. */ | |
134 | if (fstat (fd, &sb) == -1) | |
135 | continue; | |
136 | ||
137 | result = func (arg, fd); | |
138 | if (result != 0) | |
139 | return result; | |
140 | } | |
141 | ||
142 | return 0; | |
143 | } | |
144 | } | |
145 | ||
146 | #endif /* HAVE_FDWALK */ | |
147 | ||
148 | \f | |
149 | ||
37269bc9 SM |
150 | /* A vector holding all the fds open when notice_open_fds was called. We |
151 | don't use a hashtab because we don't expect there to be many open fds. */ | |
614c279d | 152 | |
37269bc9 | 153 | static std::vector<int> open_fds; |
614c279d TT |
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 | { | |
37269bc9 | 161 | open_fds.push_back (fd); |
614c279d TT |
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 | ||
21ff4686 TT |
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 | { | |
37269bc9 | 186 | auto it = std::remove (open_fds.begin (), open_fds.end (), fd); |
21ff4686 | 187 | |
37269bc9 SM |
188 | if (it != open_fds.end ()) |
189 | open_fds.erase (it); | |
190 | else | |
191 | gdb_assert_not_reached (_("fd not found in open_fds")); | |
21ff4686 TT |
192 | } |
193 | ||
614c279d TT |
194 | /* Helper function for close_most_fds that closes the file descriptor |
195 | if appropriate. */ | |
196 | ||
197 | static int | |
198 | do_close (void *ignore, int fd) | |
199 | { | |
37269bc9 | 200 | for (int val : open_fds) |
614c279d TT |
201 | { |
202 | if (fd == val) | |
203 | { | |
204 | /* Keep this one open. */ | |
205 | return 0; | |
206 | } | |
207 | } | |
208 | ||
209 | close (fd); | |
210 | return 0; | |
211 | } | |
212 | ||
213 | /* See filestuff.h. */ | |
214 | ||
215 | void | |
216 | close_most_fds (void) | |
217 | { | |
218 | fdwalk (do_close, NULL); | |
219 | } | |
220 | ||
221 | \f | |
222 | ||
223 | /* This is a tri-state flag. When zero it means we haven't yet tried | |
224 | O_CLOEXEC. When positive it means that O_CLOEXEC works on this | |
225 | host. When negative, it means that O_CLOEXEC doesn't work. We | |
226 | track this state because, while gdb might have been compiled | |
227 | against a libc that supplies O_CLOEXEC, there is no guarantee that | |
228 | the kernel supports it. */ | |
229 | ||
230 | static int trust_o_cloexec; | |
231 | ||
232 | /* Mark FD as close-on-exec, ignoring errors. Update | |
233 | TRUST_O_CLOEXEC. */ | |
234 | ||
235 | static void | |
236 | mark_cloexec (int fd) | |
237 | { | |
5d71132c | 238 | #ifdef HAVE_F_GETFD |
614c279d TT |
239 | int old = fcntl (fd, F_GETFD, 0); |
240 | ||
241 | if (old != -1) | |
242 | { | |
243 | fcntl (fd, F_SETFD, old | FD_CLOEXEC); | |
244 | ||
245 | if (trust_o_cloexec == 0) | |
246 | { | |
247 | if ((old & FD_CLOEXEC) != 0) | |
248 | trust_o_cloexec = 1; | |
249 | else | |
250 | trust_o_cloexec = -1; | |
251 | } | |
252 | } | |
5d71132c | 253 | #endif /* HAVE_F_GETFD */ |
614c279d TT |
254 | } |
255 | ||
256 | /* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec. */ | |
257 | ||
258 | static void | |
259 | maybe_mark_cloexec (int fd) | |
260 | { | |
261 | if (trust_o_cloexec <= 0) | |
262 | mark_cloexec (fd); | |
263 | } | |
264 | ||
8658d16d PA |
265 | #ifdef HAVE_SOCKETS |
266 | ||
614c279d TT |
267 | /* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC. */ |
268 | ||
269 | static void | |
270 | socket_mark_cloexec (int fd) | |
271 | { | |
272 | if (SOCK_CLOEXEC == 0 || trust_o_cloexec <= 0) | |
273 | mark_cloexec (fd); | |
274 | } | |
275 | ||
8658d16d PA |
276 | #endif |
277 | ||
614c279d TT |
278 | \f |
279 | ||
280 | /* See filestuff.h. */ | |
281 | ||
282 | int | |
5d71132c | 283 | gdb_open_cloexec (const char *filename, int flags, unsigned long mode) |
614c279d TT |
284 | { |
285 | int fd = open (filename, flags | O_CLOEXEC, mode); | |
286 | ||
287 | if (fd >= 0) | |
288 | maybe_mark_cloexec (fd); | |
289 | ||
290 | return fd; | |
291 | } | |
292 | ||
293 | /* See filestuff.h. */ | |
294 | ||
d419f42d | 295 | gdb_file_up |
614c279d TT |
296 | gdb_fopen_cloexec (const char *filename, const char *opentype) |
297 | { | |
c74e1ccf | 298 | FILE *result; |
88505fac PM |
299 | /* Probe for "e" support once. But, if we can tell the operating |
300 | system doesn't know about close on exec mode "e" without probing, | |
301 | skip it. E.g., the Windows runtime issues an "Invalid parameter | |
302 | passed to C runtime function" OutputDebugString warning for | |
303 | unknown modes. Assume that if O_CLOEXEC is zero, then "e" isn't | |
304 | supported. */ | |
c74e1ccf | 305 | static int fopen_e_ever_failed_einval = O_CLOEXEC == 0; |
614c279d | 306 | |
c74e1ccf | 307 | if (!fopen_e_ever_failed_einval) |
614c279d TT |
308 | { |
309 | char *copy; | |
310 | ||
224c3ddb | 311 | copy = (char *) alloca (strlen (opentype) + 2); |
614c279d TT |
312 | strcpy (copy, opentype); |
313 | /* This is a glibc extension but we try it unconditionally on | |
314 | this path. */ | |
315 | strcat (copy, "e"); | |
316 | result = fopen (filename, copy); | |
614c279d | 317 | |
c74e1ccf JK |
318 | if (result == NULL && errno == EINVAL) |
319 | { | |
320 | result = fopen (filename, opentype); | |
321 | if (result != NULL) | |
322 | fopen_e_ever_failed_einval = 1; | |
323 | } | |
614c279d | 324 | } |
c74e1ccf JK |
325 | else |
326 | result = fopen (filename, opentype); | |
614c279d TT |
327 | |
328 | if (result != NULL) | |
329 | maybe_mark_cloexec (fileno (result)); | |
330 | ||
d419f42d | 331 | return gdb_file_up (result); |
614c279d TT |
332 | } |
333 | ||
8658d16d | 334 | #ifdef HAVE_SOCKETS |
614c279d TT |
335 | /* See filestuff.h. */ |
336 | ||
337 | int | |
fe978cb0 PA |
338 | gdb_socketpair_cloexec (int domain, int style, int protocol, |
339 | int filedes[2]) | |
614c279d | 340 | { |
5d71132c | 341 | #ifdef HAVE_SOCKETPAIR |
fe978cb0 | 342 | int result = socketpair (domain, style | SOCK_CLOEXEC, protocol, filedes); |
614c279d TT |
343 | |
344 | if (result != -1) | |
345 | { | |
346 | socket_mark_cloexec (filedes[0]); | |
347 | socket_mark_cloexec (filedes[1]); | |
348 | } | |
349 | ||
350 | return result; | |
5d71132c TT |
351 | #else |
352 | gdb_assert_not_reached (_("socketpair not available on this host")); | |
353 | #endif | |
614c279d TT |
354 | } |
355 | ||
356 | /* See filestuff.h. */ | |
357 | ||
358 | int | |
fe978cb0 | 359 | gdb_socket_cloexec (int domain, int style, int protocol) |
614c279d | 360 | { |
fe978cb0 | 361 | int result = socket (domain, style | SOCK_CLOEXEC, protocol); |
614c279d TT |
362 | |
363 | if (result != -1) | |
364 | socket_mark_cloexec (result); | |
365 | ||
366 | return result; | |
367 | } | |
8658d16d | 368 | #endif |
614c279d TT |
369 | |
370 | /* See filestuff.h. */ | |
371 | ||
372 | int | |
373 | gdb_pipe_cloexec (int filedes[2]) | |
374 | { | |
375 | int result; | |
376 | ||
377 | #ifdef HAVE_PIPE2 | |
378 | result = pipe2 (filedes, O_CLOEXEC); | |
379 | if (result != -1) | |
380 | { | |
381 | maybe_mark_cloexec (filedes[0]); | |
382 | maybe_mark_cloexec (filedes[1]); | |
383 | } | |
384 | #else | |
5d71132c | 385 | #ifdef HAVE_PIPE |
614c279d TT |
386 | result = pipe (filedes); |
387 | if (result != -1) | |
388 | { | |
389 | mark_cloexec (filedes[0]); | |
390 | mark_cloexec (filedes[1]); | |
391 | } | |
5d71132c TT |
392 | #else /* HAVE_PIPE */ |
393 | gdb_assert_not_reached (_("pipe not available on this host")); | |
394 | #endif /* HAVE_PIPE */ | |
395 | #endif /* HAVE_PIPE2 */ | |
614c279d TT |
396 | |
397 | return result; | |
398 | } | |
ca095836 GB |
399 | |
400 | /* Helper function which does the work for make_cleanup_close. */ | |
401 | ||
402 | static void | |
403 | do_close_cleanup (void *arg) | |
404 | { | |
9a3c8263 | 405 | int *fd = (int *) arg; |
ca095836 GB |
406 | |
407 | close (*fd); | |
408 | } | |
409 | ||
c402ef90 | 410 | /* See filestuff.h. */ |
ca095836 GB |
411 | |
412 | struct cleanup * | |
413 | make_cleanup_close (int fd) | |
414 | { | |
8d749320 | 415 | int *saved_fd = XNEW (int); |
ca095836 GB |
416 | |
417 | *saved_fd = fd; | |
418 | return make_cleanup_dtor (do_close_cleanup, saved_fd, xfree); | |
419 | } |