1 /* Low-level file-handling.
2 Copyright (C) 2012-2022 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
19 #ifndef COMMON_FILESTUFF_H
20 #define COMMON_FILESTUFF_H
25 #include "scoped_fd.h"
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. */
30 extern void notice_open_fds (void);
32 /* Mark a file descriptor as inheritable across an exec. */
34 extern void mark_fd_no_cloexec (int fd
);
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. */
40 extern void unmark_fd_no_cloexec (int fd
);
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. */
46 extern void close_most_fds (void);
48 /* Like 'open', but ensures that the returned file descriptor has the
49 close-on-exec flag set. */
51 extern scoped_fd
gdb_open_cloexec (const char *filename
, int flags
,
52 /* mode_t */ unsigned long mode
);
54 /* Like mkstemp, but ensures that the file descriptor is
57 static inline scoped_fd
58 gdb_mkostemp_cloexec (char *name_template
, int flags
= 0)
60 /* gnulib provides a mkostemp replacement if needed. */
61 return scoped_fd (mkostemp (name_template
, flags
| O_CLOEXEC
));
64 /* Convenience wrapper for the above, which takes the filename as an
67 static inline scoped_fd
68 gdb_open_cloexec (const std::string
&filename
, int flags
,
69 /* mode_t */ unsigned long mode
)
71 return gdb_open_cloexec (filename
.c_str (), flags
, mode
);
74 /* Like 'fopen', but ensures that the returned file descriptor has the
75 close-on-exec flag set. */
77 extern gdb_file_up
gdb_fopen_cloexec (const char *filename
,
78 const char *opentype
);
80 /* Convenience wrapper for the above, which takes the filename as an
83 static inline gdb_file_up
84 gdb_fopen_cloexec (const std::string
&filename
, const char *opentype
)
86 return gdb_fopen_cloexec (filename
.c_str (), opentype
);
89 /* Like 'socketpair', but ensures that the returned file descriptors
90 have the close-on-exec flag set. */
92 extern int gdb_socketpair_cloexec (int domain
, int style
, int protocol
,
95 /* Like 'socket', but ensures that the returned file descriptor has
96 the close-on-exec flag set. */
98 extern int gdb_socket_cloexec (int domain
, int style
, int protocol
);
100 /* Like 'pipe', but ensures that the returned file descriptors have
101 the close-on-exec flag set. */
103 extern int gdb_pipe_cloexec (int filedes
[2]);
105 struct gdb_dir_deleter
107 void operator() (DIR *dir
) const
113 /* A unique pointer to a DIR. */
115 typedef std::unique_ptr
<DIR, gdb_dir_deleter
> gdb_dir_up
;
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
);
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.
128 Returns false on failure and sets errno. */
130 extern bool mkdir_recursive (const char *dir
);
132 /* Read the entire content of file PATH into an std::string. */
134 extern gdb::optional
<std::string
> read_text_file_to_string (const char *path
);
136 #endif /* COMMON_FILESTUFF_H */