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