Commit | Line | Data |
---|---|---|
614c279d | 1 | /* Low-level file-handling. |
e2882c85 | 2 | Copyright (C) 2012-2018 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 | ||
19 | #ifndef FILESTUFF_H | |
20 | #define FILESTUFF_H | |
21 | ||
f0b3976b TT |
22 | #include <dirent.h> |
23 | ||
614c279d TT |
24 | /* Note all the file descriptors which are open when this is called. |
25 | These file descriptors will not be closed by close_most_fds. */ | |
26 | ||
27 | extern void notice_open_fds (void); | |
28 | ||
21ff4686 TT |
29 | /* Mark a file descriptor as inheritable across an exec. */ |
30 | ||
31 | extern void mark_fd_no_cloexec (int fd); | |
32 | ||
33 | /* Mark a file descriptor as no longer being inheritable across an | |
34 | exec. This is only meaningful when FD was previously passed to | |
35 | mark_fd_no_cloexec. */ | |
36 | ||
37 | extern void unmark_fd_no_cloexec (int fd); | |
38 | ||
614c279d TT |
39 | /* Close all open file descriptors other than those marked by |
40 | 'notice_open_fds', and stdin, stdout, and stderr. Errors that | |
41 | occur while closing are ignored. */ | |
42 | ||
43 | extern void close_most_fds (void); | |
44 | ||
45 | /* Like 'open', but ensures that the returned file descriptor has the | |
46 | close-on-exec flag set. */ | |
47 | ||
5d71132c TT |
48 | extern int gdb_open_cloexec (const char *filename, int flags, |
49 | /* mode_t */ unsigned long mode); | |
614c279d | 50 | |
d419f42d TT |
51 | struct gdb_file_deleter |
52 | { | |
53 | void operator() (FILE *file) const | |
54 | { | |
55 | fclose (file); | |
56 | } | |
57 | }; | |
58 | ||
59 | /* A unique pointer to a FILE. */ | |
60 | ||
61 | typedef std::unique_ptr<FILE, gdb_file_deleter> gdb_file_up; | |
62 | ||
614c279d TT |
63 | /* Like 'fopen', but ensures that the returned file descriptor has the |
64 | close-on-exec flag set. */ | |
65 | ||
d419f42d TT |
66 | extern gdb_file_up gdb_fopen_cloexec (const char *filename, |
67 | const char *opentype); | |
614c279d TT |
68 | |
69 | /* Like 'socketpair', but ensures that the returned file descriptors | |
70 | have the close-on-exec flag set. */ | |
71 | ||
fe978cb0 | 72 | extern int gdb_socketpair_cloexec (int domain, int style, int protocol, |
614c279d TT |
73 | int filedes[2]); |
74 | ||
75 | /* Like 'socket', but ensures that the returned file descriptor has | |
76 | the close-on-exec flag set. */ | |
77 | ||
fe978cb0 | 78 | extern int gdb_socket_cloexec (int domain, int style, int protocol); |
614c279d TT |
79 | |
80 | /* Like 'pipe', but ensures that the returned file descriptors have | |
81 | the close-on-exec flag set. */ | |
82 | ||
83 | extern int gdb_pipe_cloexec (int filedes[2]); | |
84 | ||
ca095836 GB |
85 | /* Return a new cleanup that closes FD. */ |
86 | ||
87 | extern struct cleanup *make_cleanup_close (int fd); | |
88 | ||
f0b3976b TT |
89 | struct gdb_dir_deleter |
90 | { | |
91 | void operator() (DIR *dir) const | |
92 | { | |
93 | closedir (dir); | |
94 | } | |
95 | }; | |
96 | ||
97 | /* A unique pointer to a DIR. */ | |
98 | ||
99 | typedef std::unique_ptr<DIR, gdb_dir_deleter> gdb_dir_up; | |
100 | ||
614c279d | 101 | #endif /* FILESTUFF_H */ |