filestuff.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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. #include "common-defs.h"
  15. #include "filestuff.h"
  16. #include "gdb_vecs.h"
  17. #include <fcntl.h>
  18. #include <unistd.h>
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <algorithm>
  22. #ifdef USE_WIN32API
  23. #include <winsock2.h>
  24. #include <windows.h>
  25. #define HAVE_SOCKETS 1
  26. #elif defined HAVE_SYS_SOCKET_H
  27. #include <sys/socket.h>
  28. /* Define HAVE_F_GETFD if we plan to use F_GETFD. */
  29. #define HAVE_F_GETFD F_GETFD
  30. #define HAVE_SOCKETS 1
  31. #endif
  32. #ifdef HAVE_KINFO_GETFILE
  33. #include <sys/user.h>
  34. #include <libutil.h>
  35. #endif
  36. #ifdef HAVE_SYS_RESOURCE_H
  37. #include <sys/resource.h>
  38. #endif /* HAVE_SYS_RESOURCE_H */
  39. #ifndef O_CLOEXEC
  40. #define O_CLOEXEC 0
  41. #endif
  42. #ifndef O_NOINHERIT
  43. #define O_NOINHERIT 0
  44. #endif
  45. #ifndef SOCK_CLOEXEC
  46. #define SOCK_CLOEXEC 0
  47. #endif
  48. #ifndef HAVE_FDWALK
  49. #include <dirent.h>
  50. /* Replacement for fdwalk, if the system doesn't define it. Walks all
  51. open file descriptors (though this implementation may walk closed
  52. ones as well, depending on the host platform's capabilities) and
  53. call FUNC with ARG. If FUNC returns non-zero, stops immediately
  54. and returns the same value. Otherwise, returns zero when
  55. finished. */
  56. static int
  57. fdwalk (int (*func) (void *, int), void *arg)
  58. {
  59. /* Checking __linux__ isn't great but it isn't clear what would be
  60. better. There doesn't seem to be a good way to check for this in
  61. configure. */
  62. #ifdef __linux__
  63. DIR *dir;
  64. dir = opendir ("/proc/self/fd");
  65. if (dir != NULL)
  66. {
  67. struct dirent *entry;
  68. int result = 0;
  69. for (entry = readdir (dir); entry != NULL; entry = readdir (dir))
  70. {
  71. long fd;
  72. char *tail;
  73. errno = 0;
  74. fd = strtol (entry->d_name, &tail, 10);
  75. if (*tail != '\0' || errno != 0)
  76. continue;
  77. if ((int) fd != fd)
  78. {
  79. /* What can we do here really? */
  80. continue;
  81. }
  82. if (fd == dirfd (dir))
  83. continue;
  84. result = func (arg, fd);
  85. if (result != 0)
  86. break;
  87. }
  88. closedir (dir);
  89. return result;
  90. }
  91. /* We may fall through to the next case. */
  92. #endif
  93. #ifdef HAVE_KINFO_GETFILE
  94. int nfd;
  95. gdb::unique_xmalloc_ptr<struct kinfo_file[]> fdtbl
  96. (kinfo_getfile (getpid (), &nfd));
  97. if (fdtbl != NULL)
  98. {
  99. for (int i = 0; i < nfd; i++)
  100. {
  101. if (fdtbl[i].kf_fd >= 0)
  102. {
  103. int result = func (arg, fdtbl[i].kf_fd);
  104. if (result != 0)
  105. return result;
  106. }
  107. }
  108. return 0;
  109. }
  110. /* We may fall through to the next case. */
  111. #endif
  112. {
  113. int max, fd;
  114. #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
  115. struct rlimit rlim;
  116. if (getrlimit (RLIMIT_NOFILE, &rlim) == 0 && rlim.rlim_max != RLIM_INFINITY)
  117. max = rlim.rlim_max;
  118. else
  119. #endif
  120. {
  121. #ifdef _SC_OPEN_MAX
  122. max = sysconf (_SC_OPEN_MAX);
  123. #else
  124. /* Whoops. */
  125. return 0;
  126. #endif /* _SC_OPEN_MAX */
  127. }
  128. for (fd = 0; fd < max; ++fd)
  129. {
  130. struct stat sb;
  131. int result;
  132. /* Only call FUNC for open fds. */
  133. if (fstat (fd, &sb) == -1)
  134. continue;
  135. result = func (arg, fd);
  136. if (result != 0)
  137. return result;
  138. }
  139. return 0;
  140. }
  141. }
  142. #endif /* HAVE_FDWALK */
  143. /* A vector holding all the fds open when notice_open_fds was called. We
  144. don't use a hashtab because we don't expect there to be many open fds. */
  145. static std::vector<int> open_fds;
  146. /* An fdwalk callback function used by notice_open_fds. It puts the
  147. given file descriptor into the vec. */
  148. static int
  149. do_mark_open_fd (void *ignore, int fd)
  150. {
  151. open_fds.push_back (fd);
  152. return 0;
  153. }
  154. /* See filestuff.h. */
  155. void
  156. notice_open_fds (void)
  157. {
  158. fdwalk (do_mark_open_fd, NULL);
  159. }
  160. /* See filestuff.h. */
  161. void
  162. mark_fd_no_cloexec (int fd)
  163. {
  164. do_mark_open_fd (NULL, fd);
  165. }
  166. /* See filestuff.h. */
  167. void
  168. unmark_fd_no_cloexec (int fd)
  169. {
  170. auto it = std::remove (open_fds.begin (), open_fds.end (), fd);
  171. if (it != open_fds.end ())
  172. open_fds.erase (it);
  173. else
  174. gdb_assert_not_reached ("fd not found in open_fds");
  175. }
  176. /* Helper function for close_most_fds that closes the file descriptor
  177. if appropriate. */
  178. static int
  179. do_close (void *ignore, int fd)
  180. {
  181. for (int val : open_fds)
  182. {
  183. if (fd == val)
  184. {
  185. /* Keep this one open. */
  186. return 0;
  187. }
  188. }
  189. close (fd);
  190. return 0;
  191. }
  192. /* See filestuff.h. */
  193. void
  194. close_most_fds (void)
  195. {
  196. fdwalk (do_close, NULL);
  197. }
  198. /* This is a tri-state flag. When zero it means we haven't yet tried
  199. O_CLOEXEC. When positive it means that O_CLOEXEC works on this
  200. host. When negative, it means that O_CLOEXEC doesn't work. We
  201. track this state because, while gdb might have been compiled
  202. against a libc that supplies O_CLOEXEC, there is no guarantee that
  203. the kernel supports it. */
  204. static int trust_o_cloexec;
  205. /* Mark FD as close-on-exec, ignoring errors. Update
  206. TRUST_O_CLOEXEC. */
  207. static void
  208. mark_cloexec (int fd)
  209. {
  210. #ifdef HAVE_F_GETFD
  211. int old = fcntl (fd, F_GETFD, 0);
  212. if (old != -1)
  213. {
  214. fcntl (fd, F_SETFD, old | FD_CLOEXEC);
  215. if (trust_o_cloexec == 0)
  216. {
  217. if ((old & FD_CLOEXEC) != 0)
  218. trust_o_cloexec = 1;
  219. else
  220. trust_o_cloexec = -1;
  221. }
  222. }
  223. #endif /* HAVE_F_GETFD */
  224. }
  225. /* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec. */
  226. static void
  227. maybe_mark_cloexec (int fd)
  228. {
  229. if (trust_o_cloexec <= 0)
  230. mark_cloexec (fd);
  231. }
  232. #ifdef HAVE_SOCKETS
  233. /* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC. */
  234. static void
  235. socket_mark_cloexec (int fd)
  236. {
  237. if (SOCK_CLOEXEC == 0 || trust_o_cloexec <= 0)
  238. mark_cloexec (fd);
  239. }
  240. #endif
  241. /* See filestuff.h. */
  242. scoped_fd
  243. gdb_open_cloexec (const char *filename, int flags, unsigned long mode)
  244. {
  245. scoped_fd fd (open (filename, flags | O_CLOEXEC, mode));
  246. if (fd.get () >= 0)
  247. maybe_mark_cloexec (fd.get ());
  248. return fd;
  249. }
  250. /* See filestuff.h. */
  251. gdb_file_up
  252. gdb_fopen_cloexec (const char *filename, const char *opentype)
  253. {
  254. FILE *result;
  255. /* Probe for "e" support once. But, if we can tell the operating
  256. system doesn't know about close on exec mode "e" without probing,
  257. skip it. E.g., the Windows runtime issues an "Invalid parameter
  258. passed to C runtime function" OutputDebugString warning for
  259. unknown modes. Assume that if O_CLOEXEC is zero, then "e" isn't
  260. supported. On MinGW, O_CLOEXEC is an alias of O_NOINHERIT, and
  261. "e" isn't supported. */
  262. static int fopen_e_ever_failed_einval =
  263. O_CLOEXEC == 0 || O_CLOEXEC == O_NOINHERIT;
  264. if (!fopen_e_ever_failed_einval)
  265. {
  266. char *copy;
  267. copy = (char *) alloca (strlen (opentype) + 2);
  268. strcpy (copy, opentype);
  269. /* This is a glibc extension but we try it unconditionally on
  270. this path. */
  271. strcat (copy, "e");
  272. result = fopen (filename, copy);
  273. if (result == NULL && errno == EINVAL)
  274. {
  275. result = fopen (filename, opentype);
  276. if (result != NULL)
  277. fopen_e_ever_failed_einval = 1;
  278. }
  279. }
  280. else
  281. result = fopen (filename, opentype);
  282. if (result != NULL)
  283. maybe_mark_cloexec (fileno (result));
  284. return gdb_file_up (result);
  285. }
  286. #ifdef HAVE_SOCKETS
  287. /* See filestuff.h. */
  288. int
  289. gdb_socketpair_cloexec (int domain, int style, int protocol,
  290. int filedes[2])
  291. {
  292. #ifdef HAVE_SOCKETPAIR
  293. int result = socketpair (domain, style | SOCK_CLOEXEC, protocol, filedes);
  294. if (result != -1)
  295. {
  296. socket_mark_cloexec (filedes[0]);
  297. socket_mark_cloexec (filedes[1]);
  298. }
  299. return result;
  300. #else
  301. gdb_assert_not_reached ("socketpair not available on this host");
  302. #endif
  303. }
  304. /* See filestuff.h. */
  305. int
  306. gdb_socket_cloexec (int domain, int style, int protocol)
  307. {
  308. int result = socket (domain, style | SOCK_CLOEXEC, protocol);
  309. if (result != -1)
  310. socket_mark_cloexec (result);
  311. return result;
  312. }
  313. #endif
  314. /* See filestuff.h. */
  315. int
  316. gdb_pipe_cloexec (int filedes[2])
  317. {
  318. int result;
  319. #ifdef HAVE_PIPE2
  320. result = pipe2 (filedes, O_CLOEXEC);
  321. if (result != -1)
  322. {
  323. maybe_mark_cloexec (filedes[0]);
  324. maybe_mark_cloexec (filedes[1]);
  325. }
  326. #else
  327. #ifdef HAVE_PIPE
  328. result = pipe (filedes);
  329. if (result != -1)
  330. {
  331. mark_cloexec (filedes[0]);
  332. mark_cloexec (filedes[1]);
  333. }
  334. #else /* HAVE_PIPE */
  335. gdb_assert_not_reached ("pipe not available on this host");
  336. #endif /* HAVE_PIPE */
  337. #endif /* HAVE_PIPE2 */
  338. return result;
  339. }
  340. /* See gdbsupport/filestuff.h. */
  341. bool
  342. is_regular_file (const char *name, int *errno_ptr)
  343. {
  344. struct stat st;
  345. const int status = stat (name, &st);
  346. /* Stat should never fail except when the file does not exist.
  347. If stat fails, analyze the source of error and return true
  348. unless the file does not exist, to avoid returning false results
  349. on obscure systems where stat does not work as expected. */
  350. if (status != 0)
  351. {
  352. if (errno != ENOENT)
  353. return true;
  354. *errno_ptr = ENOENT;
  355. return false;
  356. }
  357. if (S_ISREG (st.st_mode))
  358. return true;
  359. if (S_ISDIR (st.st_mode))
  360. *errno_ptr = EISDIR;
  361. else
  362. *errno_ptr = EINVAL;
  363. return false;
  364. }
  365. /* See gdbsupport/filestuff.h. */
  366. bool
  367. mkdir_recursive (const char *dir)
  368. {
  369. auto holder = make_unique_xstrdup (dir);
  370. char * const start = holder.get ();
  371. char *component_start = start;
  372. char *component_end = start;
  373. while (1)
  374. {
  375. /* Find the beginning of the next component. */
  376. while (*component_start == '/')
  377. component_start++;
  378. /* Are we done? */
  379. if (*component_start == '\0')
  380. return true;
  381. /* Find the slash or null-terminator after this component. */
  382. component_end = component_start;
  383. while (*component_end != '/' && *component_end != '\0')
  384. component_end++;
  385. /* Temporarily replace the slash with a null terminator, so we can create
  386. the directory up to this component. */
  387. char saved_char = *component_end;
  388. *component_end = '\0';
  389. /* If we get EEXIST and the existing path is a directory, then we're
  390. happy. If it exists, but it's a regular file and this is not the last
  391. component, we'll fail at the next component. If this is the last
  392. component, the caller will fail with ENOTDIR when trying to
  393. open/create a file under that path. */
  394. if (mkdir (start, 0700) != 0)
  395. if (errno != EEXIST)
  396. return false;
  397. /* Restore the overwritten char. */
  398. *component_end = saved_char;
  399. component_start = component_end;
  400. }
  401. }