1/* Low-level file-handling. 2 Copyright (C) 2012-2023 Free Software Foundation, Inc. 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 COMMON_FILESTUFF_H 20#define COMMON_FILESTUFF_H 21 22#include <dirent.h> 23#include <fcntl.h> 24#include "gdb_file.h" 25#include "scoped_fd.h" 26 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 30extern void notice_open_fds (void); 31 32/* Mark a file descriptor as inheritable across an exec. */ 33 34extern 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 40extern void unmark_fd_no_cloexec (int fd); 41 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 46extern 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 51extern scoped_fd gdb_open_cloexec (const char *filename, int flags, 52 /* mode_t */ unsigned long mode); 53 54/* Like mkstemp, but ensures that the file descriptor is 55 close-on-exec. */ 56 57static inline scoped_fd 58gdb_mkostemp_cloexec (char *name_template, int flags = 0) 59{ 60 /* gnulib provides a mkostemp replacement if needed. */ 61 return scoped_fd (mkostemp (name_template, flags | O_CLOEXEC)); 62} 63 64/* Convenience wrapper for the above, which takes the filename as an 65 std::string. */ 66 67static inline scoped_fd 68gdb_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 74/* Like 'fopen', but ensures that the returned file descriptor has the 75 close-on-exec flag set. */ 76 77extern gdb_file_up gdb_fopen_cloexec (const char *filename, 78 const char *opentype); 79 80/* Convenience wrapper for the above, which takes the filename as an 81 std::string. */ 82 83static inline gdb_file_up 84gdb_fopen_cloexec (const std::string &filename, const char *opentype) 85{ 86 return gdb_fopen_cloexec (filename.c_str (), opentype); 87} 88 89/* Like 'socketpair', but ensures that the returned file descriptors 90 have the close-on-exec flag set. */ 91 92extern int gdb_socketpair_cloexec (int domain, int style, int protocol, 93 int filedes[2]); 94 95/* Like 'socket', but ensures that the returned file descriptor has 96 the close-on-exec flag set. */ 97 98extern int gdb_socket_cloexec (int domain, int style, int protocol); 99 100/* Like 'pipe', but ensures that the returned file descriptors have 101 the close-on-exec flag set. */ 102 103extern int gdb_pipe_cloexec (int filedes[2]); 104 105struct 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 115typedef std::unique_ptr<DIR, gdb_dir_deleter> gdb_dir_up; 116 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. */ 120extern bool is_regular_file (const char *name, int *errno_ptr); 121 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 130extern bool mkdir_recursive (const char *dir); 131 132/* Read the entire content of file PATH into an std::string. */ 133 134extern gdb::optional<std::string> read_text_file_to_string (const char *path); 135 136#endif /* COMMON_FILESTUFF_H */ 137