gdb/common/filestuff.c - gdb

Global variables defined

Functions defined

Macros defined

Source code

  1. /* Low-level file-handling.
  2.    Copyright (C) 2012-2015 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. #ifdef USE_WIN32API
  22. #include <winsock2.h>
  23. #include <windows.h>
  24. #define HAVE_SOCKETS 1
  25. #elif defined HAVE_SYS_SOCKET_H
  26. #include <sys/socket.h>
  27. /* Define HAVE_F_GETFD if we plan to use F_GETFD.  */
  28. #define HAVE_F_GETFD F_GETFD
  29. #define HAVE_SOCKETS 1
  30. #endif

  31. #ifdef HAVE_SYS_RESOURCE_H
  32. #include <sys/resource.h>
  33. #endif /* HAVE_SYS_RESOURCE_H */

  34. #ifndef O_CLOEXEC
  35. #define O_CLOEXEC 0
  36. #endif

  37. #ifndef SOCK_CLOEXEC
  38. #define SOCK_CLOEXEC 0
  39. #endif



  40. #ifndef HAVE_FDWALK

  41. #include <dirent.h>

  42. /* Replacement for fdwalk, if the system doesn't define it.  Walks all
  43.    open file descriptors (though this implementation may walk closed
  44.    ones as well, depending on the host platform's capabilities) and
  45.    call FUNC with ARG.  If FUNC returns non-zero, stops immediately
  46.    and returns the same value.  Otherwise, returns zero when
  47.    finished.  */

  48. static int
  49. fdwalk (int (*func) (void *, int), void *arg)
  50. {
  51.   /* Checking __linux__ isn't great but it isn't clear what would be
  52.      better.  There doesn't seem to be a good way to check for this in
  53.      configure.  */
  54. #ifdef __linux__
  55.   DIR *dir;

  56.   dir = opendir ("/proc/self/fd");
  57.   if (dir != NULL)
  58.     {
  59.       struct dirent *entry;
  60.       int result = 0;

  61.       for (entry = readdir (dir); entry != NULL; entry = readdir (dir))
  62.         {
  63.           long fd;
  64.           char *tail;
  65.           int result;

  66.           errno = 0;
  67.           fd = strtol (entry->d_name, &tail, 10);
  68.           if (*tail != '\0' || errno != 0)
  69.             continue;
  70.           if ((int) fd != fd)
  71.             {
  72.               /* What can we do here really?  */
  73.               continue;
  74.             }

  75.           if (fd == dirfd (dir))
  76.             continue;

  77.           result = func (arg, fd);
  78.           if (result != 0)
  79.             break;
  80.         }

  81.       closedir (dir);
  82.       return result;
  83.     }
  84.   /* We may fall through to the next case.  */
  85. #endif

  86.   {
  87.     int max, fd;

  88. #if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
  89.     struct rlimit rlim;

  90.     if (getrlimit (RLIMIT_NOFILE, &rlim) == 0 && rlim.rlim_max != RLIM_INFINITY)
  91.       max = rlim.rlim_max;
  92.     else
  93. #endif
  94.       {
  95. #ifdef _SC_OPEN_MAX
  96.         max = sysconf (_SC_OPEN_MAX);
  97. #else
  98.         /* Whoops.  */
  99.         return 0;
  100. #endif /* _SC_OPEN_MAX */
  101.       }

  102.     for (fd = 0; fd < max; ++fd)
  103.       {
  104.         struct stat sb;
  105.         int result;

  106.         /* Only call FUNC for open fds.  */
  107.         if (fstat (fd, &sb) == -1)
  108.           continue;

  109.         result = func (arg, fd);
  110.         if (result != 0)
  111.           return result;
  112.       }

  113.     return 0;
  114.   }
  115. }

  116. #endif /* HAVE_FDWALK */



  117. /* A VEC holding all the fds open when notice_open_fds was called.  We
  118.    don't use a hashtab because libiberty isn't linked into gdbserver;
  119.    and anyway we don't expect there to be many open fds.  */

  120. static VEC (int) *open_fds;

  121. /* An fdwalk callback function used by notice_open_fds.  It puts the
  122.    given file descriptor into the vec.  */

  123. static int
  124. do_mark_open_fd (void *ignore, int fd)
  125. {
  126.   VEC_safe_push (int, open_fds, fd);
  127.   return 0;
  128. }

  129. /* See filestuff.h.  */

  130. void
  131. notice_open_fds (void)
  132. {
  133.   fdwalk (do_mark_open_fd, NULL);
  134. }

  135. /* See filestuff.h.  */

  136. void
  137. mark_fd_no_cloexec (int fd)
  138. {
  139.   do_mark_open_fd (NULL, fd);
  140. }

  141. /* See filestuff.h.  */

  142. void
  143. unmark_fd_no_cloexec (int fd)
  144. {
  145.   int i, val;

  146.   for (i = 0; VEC_iterate (int, open_fds, i, val); ++i)
  147.     {
  148.       if (fd == val)
  149.         {
  150.           VEC_unordered_remove (int, open_fds, i);
  151.           return;
  152.         }
  153.     }

  154.   gdb_assert_not_reached (_("fd not found in open_fds"));
  155. }

  156. /* Helper function for close_most_fds that closes the file descriptor
  157.    if appropriate.  */

  158. static int
  159. do_close (void *ignore, int fd)
  160. {
  161.   int i, val;

  162.   for (i = 0; VEC_iterate (int, open_fds, i, val); ++i)
  163.     {
  164.       if (fd == val)
  165.         {
  166.           /* Keep this one open.  */
  167.           return 0;
  168.         }
  169.     }

  170.   close (fd);
  171.   return 0;
  172. }

  173. /* See filestuff.h.  */

  174. void
  175. close_most_fds (void)
  176. {
  177.   fdwalk (do_close, NULL);
  178. }



  179. /* This is a tri-state flag.  When zero it means we haven't yet tried
  180.    O_CLOEXEC.  When positive it means that O_CLOEXEC works on this
  181.    host.  When negative, it means that O_CLOEXEC doesn't work.  We
  182.    track this state because, while gdb might have been compiled
  183.    against a libc that supplies O_CLOEXEC, there is no guarantee that
  184.    the kernel supports it.  */

  185. static int trust_o_cloexec;

  186. /* Mark FD as close-on-exec, ignoring errors.  Update
  187.    TRUST_O_CLOEXEC.  */

  188. static void
  189. mark_cloexec (int fd)
  190. {
  191. #ifdef HAVE_F_GETFD
  192.   int old = fcntl (fd, F_GETFD, 0);

  193.   if (old != -1)
  194.     {
  195.       fcntl (fd, F_SETFD, old | FD_CLOEXEC);

  196.       if (trust_o_cloexec == 0)
  197.         {
  198.           if ((old & FD_CLOEXEC) != 0)
  199.             trust_o_cloexec = 1;
  200.           else
  201.             trust_o_cloexec = -1;
  202.         }
  203.     }
  204. #endif /* HAVE_F_GETFD */
  205. }

  206. /* Depending on TRUST_O_CLOEXEC, mark FD as close-on-exec.  */

  207. static void
  208. maybe_mark_cloexec (int fd)
  209. {
  210.   if (trust_o_cloexec <= 0)
  211.     mark_cloexec (fd);
  212. }

  213. #ifdef HAVE_SOCKETS

  214. /* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC.  */

  215. static void
  216. socket_mark_cloexec (int fd)
  217. {
  218.   if (SOCK_CLOEXEC == 0 || trust_o_cloexec <= 0)
  219.     mark_cloexec (fd);
  220. }

  221. #endif



  222. /* See filestuff.h.  */

  223. int
  224. gdb_open_cloexec (const char *filename, int flags, unsigned long mode)
  225. {
  226.   int fd = open (filename, flags | O_CLOEXEC, mode);

  227.   if (fd >= 0)
  228.     maybe_mark_cloexec (fd);

  229.   return fd;
  230. }

  231. /* See filestuff.h.  */

  232. FILE *
  233. gdb_fopen_cloexec (const char *filename, const char *opentype)
  234. {
  235.   FILE *result;
  236.   /* Probe for "e" support once.  But, if we can tell the operating
  237.      system doesn't know about close on exec mode "e" without probing,
  238.      skip it.  E.g., the Windows runtime issues an "Invalid parameter
  239.      passed to C runtime function" OutputDebugString warning for
  240.      unknown modes.  Assume that if O_CLOEXEC is zero, then "e" isn't
  241.      supported.  */
  242.   static int fopen_e_ever_failed_einval = O_CLOEXEC == 0;

  243.   if (!fopen_e_ever_failed_einval)
  244.     {
  245.       char *copy;

  246.       copy = alloca (strlen (opentype) + 2);
  247.       strcpy (copy, opentype);
  248.       /* This is a glibc extension but we try it unconditionally on
  249.          this path.  */
  250.       strcat (copy, "e");
  251.       result = fopen (filename, copy);

  252.       if (result == NULL && errno == EINVAL)
  253.         {
  254.           result = fopen (filename, opentype);
  255.           if (result != NULL)
  256.             fopen_e_ever_failed_einval = 1;
  257.         }
  258.     }
  259.   else
  260.     result = fopen (filename, opentype);

  261.   if (result != NULL)
  262.     maybe_mark_cloexec (fileno (result));

  263.   return result;
  264. }

  265. #ifdef HAVE_SOCKETS
  266. /* See filestuff.h.  */

  267. int
  268. gdb_socketpair_cloexec (int namespace, int style, int protocol, int filedes[2])
  269. {
  270. #ifdef HAVE_SOCKETPAIR
  271.   int result = socketpair (namespace, style | SOCK_CLOEXEC, protocol, filedes);

  272.   if (result != -1)
  273.     {
  274.       socket_mark_cloexec (filedes[0]);
  275.       socket_mark_cloexec (filedes[1]);
  276.     }

  277.   return result;
  278. #else
  279.   gdb_assert_not_reached (_("socketpair not available on this host"));
  280. #endif
  281. }

  282. /* See filestuff.h.  */

  283. int
  284. gdb_socket_cloexec (int namespace, int style, int protocol)
  285. {
  286.   int result = socket (namespace, style | SOCK_CLOEXEC, protocol);

  287.   if (result != -1)
  288.     socket_mark_cloexec (result);

  289.   return result;
  290. }
  291. #endif

  292. /* See filestuff.h.  */

  293. int
  294. gdb_pipe_cloexec (int filedes[2])
  295. {
  296.   int result;

  297. #ifdef HAVE_PIPE2
  298.   result = pipe2 (filedes, O_CLOEXEC);
  299.   if (result != -1)
  300.     {
  301.       maybe_mark_cloexec (filedes[0]);
  302.       maybe_mark_cloexec (filedes[1]);
  303.     }
  304. #else
  305. #ifdef HAVE_PIPE
  306.   result = pipe (filedes);
  307.   if (result != -1)
  308.     {
  309.       mark_cloexec (filedes[0]);
  310.       mark_cloexec (filedes[1]);
  311.     }
  312. #else /* HAVE_PIPE */
  313.   gdb_assert_not_reached (_("pipe not available on this host"));
  314. #endif /* HAVE_PIPE */
  315. #endif /* HAVE_PIPE2 */

  316.   return result;
  317. }