18dffb485Schristos /* Low-level file-handling. 2*5ba1f45fSchristos Copyright (C) 2012-2024 Free Software Foundation, Inc. 38dffb485Schristos 48dffb485Schristos This file is part of GDB. 58dffb485Schristos 68dffb485Schristos This program is free software; you can redistribute it and/or modify 78dffb485Schristos it under the terms of the GNU General Public License as published by 88dffb485Schristos the Free Software Foundation; either version 3 of the License, or 98dffb485Schristos (at your option) any later version. 108dffb485Schristos 118dffb485Schristos This program is distributed in the hope that it will be useful, 128dffb485Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of 138dffb485Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 148dffb485Schristos GNU General Public License for more details. 158dffb485Schristos 168dffb485Schristos You should have received a copy of the GNU General Public License 178dffb485Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 188dffb485Schristos 198dffb485Schristos #ifndef COMMON_FILESTUFF_H 208dffb485Schristos #define COMMON_FILESTUFF_H 218dffb485Schristos 228dffb485Schristos #include <dirent.h> 238dffb485Schristos #include <fcntl.h> 244b169a6bSchristos #include "gdb_file.h" 254b169a6bSchristos #include "scoped_fd.h" 268dffb485Schristos 278dffb485Schristos /* Note all the file descriptors which are open when this is called. 288dffb485Schristos These file descriptors will not be closed by close_most_fds. */ 298dffb485Schristos 308dffb485Schristos extern void notice_open_fds (void); 318dffb485Schristos 328dffb485Schristos /* Mark a file descriptor as inheritable across an exec. */ 338dffb485Schristos 348dffb485Schristos extern void mark_fd_no_cloexec (int fd); 358dffb485Schristos 368dffb485Schristos /* Mark a file descriptor as no longer being inheritable across an 378dffb485Schristos exec. This is only meaningful when FD was previously passed to 388dffb485Schristos mark_fd_no_cloexec. */ 398dffb485Schristos 408dffb485Schristos extern void unmark_fd_no_cloexec (int fd); 418dffb485Schristos 428dffb485Schristos /* Close all open file descriptors other than those marked by 438dffb485Schristos 'notice_open_fds', and stdin, stdout, and stderr. Errors that 448dffb485Schristos occur while closing are ignored. */ 458dffb485Schristos 468dffb485Schristos extern void close_most_fds (void); 478dffb485Schristos 488dffb485Schristos /* Like 'open', but ensures that the returned file descriptor has the 498dffb485Schristos close-on-exec flag set. */ 508dffb485Schristos 514b169a6bSchristos extern scoped_fd gdb_open_cloexec (const char *filename, int flags, 528dffb485Schristos /* mode_t */ unsigned long mode); 538dffb485Schristos 548dffb485Schristos /* Like mkstemp, but ensures that the file descriptor is 558dffb485Schristos close-on-exec. */ 568dffb485Schristos 574b169a6bSchristos static inline scoped_fd 588dffb485Schristos gdb_mkostemp_cloexec (char *name_template, int flags = 0) 598dffb485Schristos { 608dffb485Schristos /* gnulib provides a mkostemp replacement if needed. */ 614b169a6bSchristos return scoped_fd (mkostemp (name_template, flags | O_CLOEXEC)); 628dffb485Schristos } 638dffb485Schristos 648dffb485Schristos /* Convenience wrapper for the above, which takes the filename as an 658dffb485Schristos std::string. */ 668dffb485Schristos 674b169a6bSchristos static inline scoped_fd 688dffb485Schristos gdb_open_cloexec (const std::string &filename, int flags, 698dffb485Schristos /* mode_t */ unsigned long mode) 708dffb485Schristos { 718dffb485Schristos return gdb_open_cloexec (filename.c_str (), flags, mode); 728dffb485Schristos } 738dffb485Schristos 748dffb485Schristos /* Like 'fopen', but ensures that the returned file descriptor has the 758dffb485Schristos close-on-exec flag set. */ 768dffb485Schristos 778dffb485Schristos extern gdb_file_up gdb_fopen_cloexec (const char *filename, 788dffb485Schristos const char *opentype); 798dffb485Schristos 808dffb485Schristos /* Convenience wrapper for the above, which takes the filename as an 818dffb485Schristos std::string. */ 828dffb485Schristos 838dffb485Schristos static inline gdb_file_up 848dffb485Schristos gdb_fopen_cloexec (const std::string &filename, const char *opentype) 858dffb485Schristos { 868dffb485Schristos return gdb_fopen_cloexec (filename.c_str (), opentype); 878dffb485Schristos } 888dffb485Schristos 898dffb485Schristos /* Like 'socketpair', but ensures that the returned file descriptors 908dffb485Schristos have the close-on-exec flag set. */ 918dffb485Schristos 928dffb485Schristos extern int gdb_socketpair_cloexec (int domain, int style, int protocol, 938dffb485Schristos int filedes[2]); 948dffb485Schristos 958dffb485Schristos /* Like 'socket', but ensures that the returned file descriptor has 968dffb485Schristos the close-on-exec flag set. */ 978dffb485Schristos 988dffb485Schristos extern int gdb_socket_cloexec (int domain, int style, int protocol); 998dffb485Schristos 1008dffb485Schristos /* Like 'pipe', but ensures that the returned file descriptors have 1018dffb485Schristos the close-on-exec flag set. */ 1028dffb485Schristos 1038dffb485Schristos extern int gdb_pipe_cloexec (int filedes[2]); 1048dffb485Schristos 1058dffb485Schristos struct gdb_dir_deleter 1068dffb485Schristos { 1078dffb485Schristos void operator() (DIR *dir) const 1088dffb485Schristos { 1098dffb485Schristos closedir (dir); 1108dffb485Schristos } 1118dffb485Schristos }; 1128dffb485Schristos 1138dffb485Schristos /* A unique pointer to a DIR. */ 1148dffb485Schristos 1158dffb485Schristos typedef std::unique_ptr<DIR, gdb_dir_deleter> gdb_dir_up; 1168dffb485Schristos 1178dffb485Schristos /* Return true if the file NAME exists and is a regular file. 1188dffb485Schristos If the result is false then *ERRNO_PTR is set to a useful value assuming 1198dffb485Schristos we're expecting a regular file. */ 1208dffb485Schristos extern bool is_regular_file (const char *name, int *errno_ptr); 1218dffb485Schristos 1228dffb485Schristos 1238dffb485Schristos /* A cheap (as in low-quality) recursive mkdir. Try to create all the 1248dffb485Schristos parents directories up to DIR and DIR itself. Stop if we hit an 1258dffb485Schristos error along the way. There is no attempt to remove created 1268dffb485Schristos directories in case of failure. 1278dffb485Schristos 1288dffb485Schristos Returns false on failure and sets errno. */ 1298dffb485Schristos 1308dffb485Schristos extern bool mkdir_recursive (const char *dir); 1318dffb485Schristos 1324b169a6bSchristos /* Read the entire content of file PATH into an std::string. */ 1334b169a6bSchristos 134*5ba1f45fSchristos extern std::optional<std::string> read_text_file_to_string (const char *path); 135*5ba1f45fSchristos 136*5ba1f45fSchristos /* Read the remaining content from FILE into an std::string. */ 137*5ba1f45fSchristos 138*5ba1f45fSchristos extern std::string read_remainder_of_file (FILE *file); 1394b169a6bSchristos 1408dffb485Schristos #endif /* COMMON_FILESTUFF_H */ 141