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