filestuff.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Low-level file-handling.
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef COMMON_FILESTUFF_H
  15. #define COMMON_FILESTUFF_H
  16. #include <dirent.h>
  17. #include <fcntl.h>
  18. #include "gdb_file.h"
  19. #include "scoped_fd.h"
  20. /* Note all the file descriptors which are open when this is called.
  21. These file descriptors will not be closed by close_most_fds. */
  22. extern void notice_open_fds (void);
  23. /* Mark a file descriptor as inheritable across an exec. */
  24. extern void mark_fd_no_cloexec (int fd);
  25. /* Mark a file descriptor as no longer being inheritable across an
  26. exec. This is only meaningful when FD was previously passed to
  27. mark_fd_no_cloexec. */
  28. extern void unmark_fd_no_cloexec (int fd);
  29. /* Close all open file descriptors other than those marked by
  30. 'notice_open_fds', and stdin, stdout, and stderr. Errors that
  31. occur while closing are ignored. */
  32. extern void close_most_fds (void);
  33. /* Like 'open', but ensures that the returned file descriptor has the
  34. close-on-exec flag set. */
  35. extern scoped_fd gdb_open_cloexec (const char *filename, int flags,
  36. /* mode_t */ unsigned long mode);
  37. /* Like mkstemp, but ensures that the file descriptor is
  38. close-on-exec. */
  39. static inline scoped_fd
  40. gdb_mkostemp_cloexec (char *name_template, int flags = 0)
  41. {
  42. /* gnulib provides a mkostemp replacement if needed. */
  43. return scoped_fd (mkostemp (name_template, flags | O_CLOEXEC));
  44. }
  45. /* Convenience wrapper for the above, which takes the filename as an
  46. std::string. */
  47. static inline scoped_fd
  48. gdb_open_cloexec (const std::string &filename, int flags,
  49. /* mode_t */ unsigned long mode)
  50. {
  51. return gdb_open_cloexec (filename.c_str (), flags, mode);
  52. }
  53. /* Like 'fopen', but ensures that the returned file descriptor has the
  54. close-on-exec flag set. */
  55. extern gdb_file_up gdb_fopen_cloexec (const char *filename,
  56. const char *opentype);
  57. /* Convenience wrapper for the above, which takes the filename as an
  58. std::string. */
  59. static inline gdb_file_up
  60. gdb_fopen_cloexec (const std::string &filename, const char *opentype)
  61. {
  62. return gdb_fopen_cloexec (filename.c_str (), opentype);
  63. }
  64. /* Like 'socketpair', but ensures that the returned file descriptors
  65. have the close-on-exec flag set. */
  66. extern int gdb_socketpair_cloexec (int domain, int style, int protocol,
  67. int filedes[2]);
  68. /* Like 'socket', but ensures that the returned file descriptor has
  69. the close-on-exec flag set. */
  70. extern int gdb_socket_cloexec (int domain, int style, int protocol);
  71. /* Like 'pipe', but ensures that the returned file descriptors have
  72. the close-on-exec flag set. */
  73. extern int gdb_pipe_cloexec (int filedes[2]);
  74. struct gdb_dir_deleter
  75. {
  76. void operator() (DIR *dir) const
  77. {
  78. closedir (dir);
  79. }
  80. };
  81. /* A unique pointer to a DIR. */
  82. typedef std::unique_ptr<DIR, gdb_dir_deleter> gdb_dir_up;
  83. /* Return true if the file NAME exists and is a regular file.
  84. If the result is false then *ERRNO_PTR is set to a useful value assuming
  85. we're expecting a regular file. */
  86. extern bool is_regular_file (const char *name, int *errno_ptr);
  87. /* A cheap (as in low-quality) recursive mkdir. Try to create all the
  88. parents directories up to DIR and DIR itself. Stop if we hit an
  89. error along the way. There is no attempt to remove created
  90. directories in case of failure.
  91. Returns false on failure and sets errno. */
  92. extern bool mkdir_recursive (const char *dir);
  93. #endif /* COMMON_FILESTUFF_H */