Commit | Line | Data |
---|---|---|
614c279d | 1 | /* Low-level file-handling. |
42a4f53d | 2 | Copyright (C) 2012-2019 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 | 22 | #include <dirent.h> |
b3279b60 | 23 | #include <fcntl.h> |
f0b3976b | 24 | |
614c279d TT |
25 | /* Note all the file descriptors which are open when this is called. |
26 | These file descriptors will not be closed by close_most_fds. */ | |
27 | ||
28 | extern void notice_open_fds (void); | |
29 | ||
21ff4686 TT |
30 | /* Mark a file descriptor as inheritable across an exec. */ |
31 | ||
32 | extern void mark_fd_no_cloexec (int fd); | |
33 | ||
34 | /* Mark a file descriptor as no longer being inheritable across an | |
35 | exec. This is only meaningful when FD was previously passed to | |
36 | mark_fd_no_cloexec. */ | |
37 | ||
38 | extern void unmark_fd_no_cloexec (int fd); | |
39 | ||
614c279d TT |
40 | /* Close all open file descriptors other than those marked by |
41 | 'notice_open_fds', and stdin, stdout, and stderr. Errors that | |
42 | occur while closing are ignored. */ | |
43 | ||
44 | extern void close_most_fds (void); | |
45 | ||
46 | /* Like 'open', but ensures that the returned file descriptor has the | |
47 | close-on-exec flag set. */ | |
48 | ||
5d71132c TT |
49 | extern int gdb_open_cloexec (const char *filename, int flags, |
50 | /* mode_t */ unsigned long mode); | |
614c279d | 51 | |
b3279b60 TT |
52 | /* Like mkstemp, but ensures that the file descriptor is |
53 | close-on-exec. */ | |
54 | ||
55 | static inline int | |
56 | gdb_mkostemp_cloexec (char *name_template, int flags = 0) | |
57 | { | |
58 | /* gnulib provides a mkostemp replacement if needed. */ | |
59 | return mkostemp (name_template, flags | O_CLOEXEC); | |
60 | } | |
61 | ||
528e1572 SM |
62 | /* Convenience wrapper for the above, which takes the filename as an |
63 | std::string. */ | |
64 | ||
65 | static inline int | |
66 | gdb_open_cloexec (const std::string &filename, int flags, | |
67 | /* mode_t */ unsigned long mode) | |
68 | { | |
69 | return gdb_open_cloexec (filename.c_str (), flags, mode); | |
70 | } | |
71 | ||
d419f42d TT |
72 | struct gdb_file_deleter |
73 | { | |
74 | void operator() (FILE *file) const | |
75 | { | |
76 | fclose (file); | |
77 | } | |
78 | }; | |
79 | ||
80 | /* A unique pointer to a FILE. */ | |
81 | ||
82 | typedef std::unique_ptr<FILE, gdb_file_deleter> gdb_file_up; | |
83 | ||
614c279d TT |
84 | /* Like 'fopen', but ensures that the returned file descriptor has the |
85 | close-on-exec flag set. */ | |
86 | ||
d419f42d TT |
87 | extern gdb_file_up gdb_fopen_cloexec (const char *filename, |
88 | const char *opentype); | |
614c279d | 89 | |
528e1572 SM |
90 | /* Convenience wrapper for the above, which takes the filename as an |
91 | std::string. */ | |
92 | ||
93 | static inline gdb_file_up | |
94 | gdb_fopen_cloexec (const std::string &filename, const char *opentype) | |
95 | { | |
96 | return gdb_fopen_cloexec (filename.c_str (), opentype); | |
97 | } | |
98 | ||
614c279d TT |
99 | /* Like 'socketpair', but ensures that the returned file descriptors |
100 | have the close-on-exec flag set. */ | |
101 | ||
fe978cb0 | 102 | extern int gdb_socketpair_cloexec (int domain, int style, int protocol, |
614c279d TT |
103 | int filedes[2]); |
104 | ||
105 | /* Like 'socket', but ensures that the returned file descriptor has | |
106 | the close-on-exec flag set. */ | |
107 | ||
fe978cb0 | 108 | extern int gdb_socket_cloexec (int domain, int style, int protocol); |
614c279d TT |
109 | |
110 | /* Like 'pipe', but ensures that the returned file descriptors have | |
111 | the close-on-exec flag set. */ | |
112 | ||
113 | extern int gdb_pipe_cloexec (int filedes[2]); | |
114 | ||
ca095836 GB |
115 | /* Return a new cleanup that closes FD. */ |
116 | ||
117 | extern struct cleanup *make_cleanup_close (int fd); | |
118 | ||
f0b3976b TT |
119 | struct gdb_dir_deleter |
120 | { | |
121 | void operator() (DIR *dir) const | |
122 | { | |
123 | closedir (dir); | |
124 | } | |
125 | }; | |
126 | ||
127 | /* A unique pointer to a DIR. */ | |
128 | ||
129 | typedef std::unique_ptr<DIR, gdb_dir_deleter> gdb_dir_up; | |
130 | ||
3c025cfe SDJ |
131 | /* Return true if the file NAME exists and is a regular file. |
132 | If the result is false then *ERRNO_PTR is set to a useful value assuming | |
133 | we're expecting a regular file. */ | |
134 | extern bool is_regular_file (const char *name, int *errno_ptr); | |
135 | ||
e418a61a TT |
136 | |
137 | /* A cheap (as in low-quality) recursive mkdir. Try to create all the | |
138 | parents directories up to DIR and DIR itself. Stop if we hit an | |
139 | error along the way. There is no attempt to remove created | |
140 | directories in case of failure. | |
141 | ||
142 | Returns false on failure and sets errno. */ | |
143 | ||
144 | extern bool mkdir_recursive (const char *dir); | |
145 | ||
614c279d | 146 | #endif /* FILESTUFF_H */ |