Update copyright year range in all GDB files
[deliverable/binutils-gdb.git] / gdb / common / filestuff.c
index 68f66ca6d0f96edc640fd04520f1884829a21730..f5a754ffa66471734299b3bc9d0f692b3e6140ac 100644 (file)
@@ -1,5 +1,5 @@
 /* Low-level file-handling.
-   Copyright (C) 2012, 2013 Free Software Foundation, Inc.
+   Copyright (C) 2012-2018 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
-#ifdef GDBSERVER
-#include "server.h"
-#else
-#include "defs.h"
-#include "gdb_string.h"
-#endif
+#include "common-defs.h"
 #include "filestuff.h"
 #include "gdb_vecs.h"
-
-#include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/types.h>
-#include "gdb_stat.h"
+#include <sys/stat.h>
+#include <algorithm>
 
 #ifdef USE_WIN32API
 #include <winsock2.h>
 #include <windows.h>
-#else
+#define HAVE_SOCKETS 1
+#elif defined HAVE_SYS_SOCKET_H
 #include <sys/socket.h>
 /* Define HAVE_F_GETFD if we plan to use F_GETFD.  */
 #define HAVE_F_GETFD F_GETFD
+#define HAVE_SOCKETS 1
 #endif
 
 #ifdef HAVE_SYS_RESOURCE_H
@@ -56,7 +52,7 @@
 
 #ifndef HAVE_FDWALK
 
-#include "gdb_dirent.h"
+#include <dirent.h>
 
 /* Replacement for fdwalk, if the system doesn't define it.  Walks all
    open file descriptors (though this implementation may walk closed
@@ -113,7 +109,7 @@ fdwalk (int (*func) (void *, int), void *arg)
   {
     int max, fd;
 
-#ifdef HAVE_GETRLIMIT
+#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
     struct rlimit rlim;
 
     if (getrlimit (RLIMIT_NOFILE, &rlim) == 0 && rlim.rlim_max != RLIM_INFINITY)
@@ -151,13 +147,10 @@ fdwalk (int (*func) (void *, int), void *arg)
 
 \f
 
-/* A VEC holding all the fds open when notice_open_fds was called.  We
-   don't use a hashtab because libiberty isn't linked into gdbserver;
-   and anyway we don't expect there to be many open fds.  */
-
-DEF_VEC_I (int);
+/* A vector holding all the fds open when notice_open_fds was called.  We
+   don't use a hashtab because we don't expect there to be many open fds.  */
 
-static VEC (int) *open_fds;
+static std::vector<int> open_fds;
 
 /* An fdwalk callback function used by notice_open_fds.  It puts the
    given file descriptor into the vec.  */
@@ -165,7 +158,7 @@ static VEC (int) *open_fds;
 static int
 do_mark_open_fd (void *ignore, int fd)
 {
-  VEC_safe_push (int, open_fds, fd);
+  open_fds.push_back (fd);
   return 0;
 }
 
@@ -177,15 +170,34 @@ notice_open_fds (void)
   fdwalk (do_mark_open_fd, NULL);
 }
 
+/* See filestuff.h.  */
+
+void
+mark_fd_no_cloexec (int fd)
+{
+  do_mark_open_fd (NULL, fd);
+}
+
+/* See filestuff.h.  */
+
+void
+unmark_fd_no_cloexec (int fd)
+{
+  auto it = std::remove (open_fds.begin (), open_fds.end (), fd);
+
+  if (it != open_fds.end ())
+    open_fds.erase (it);
+  else
+    gdb_assert_not_reached (_("fd not found in open_fds"));
+}
+
 /* Helper function for close_most_fds that closes the file descriptor
    if appropriate.  */
 
 static int
 do_close (void *ignore, int fd)
 {
-  int i, val;
-
-  for (i = 0; VEC_iterate (int, open_fds, i, val); ++i)
+  for (int val : open_fds)
     {
       if (fd == val)
        {
@@ -250,6 +262,8 @@ maybe_mark_cloexec (int fd)
     mark_cloexec (fd);
 }
 
+#ifdef HAVE_SOCKETS
+
 /* Like maybe_mark_cloexec, but for callers that use SOCK_CLOEXEC.  */
 
 static void
@@ -259,6 +273,8 @@ socket_mark_cloexec (int fd)
     mark_cloexec (fd);
 }
 
+#endif
+
 \f
 
 /* See filestuff.h.  */
@@ -276,45 +292,54 @@ gdb_open_cloexec (const char *filename, int flags, unsigned long mode)
 
 /* See filestuff.h.  */
 
-FILE *
+gdb_file_up
 gdb_fopen_cloexec (const char *filename, const char *opentype)
 {
-  FILE *result = NULL;
-  static int fopen_e_ever_failed;
-
-  if (!fopen_e_ever_failed)
+  FILE *result;
+  /* Probe for "e" support once.  But, if we can tell the operating
+     system doesn't know about close on exec mode "e" without probing,
+     skip it.  E.g., the Windows runtime issues an "Invalid parameter
+     passed to C runtime function" OutputDebugString warning for
+     unknown modes.  Assume that if O_CLOEXEC is zero, then "e" isn't
+     supported.  */
+  static int fopen_e_ever_failed_einval = O_CLOEXEC == 0;
+
+  if (!fopen_e_ever_failed_einval)
     {
       char *copy;
 
-      copy = alloca (strlen (opentype) + 2);
+      copy = (char *) alloca (strlen (opentype) + 2);
       strcpy (copy, opentype);
       /* This is a glibc extension but we try it unconditionally on
         this path.  */
       strcat (copy, "e");
       result = fopen (filename, copy);
-    }
 
-  if (result == NULL)
-    {
-      /* Fallback.  */
-      result = fopen (filename, opentype);
-      if (result != NULL)
-       fopen_e_ever_failed = 1;
+      if (result == NULL && errno == EINVAL)
+       {
+         result = fopen (filename, opentype);
+         if (result != NULL)
+           fopen_e_ever_failed_einval = 1;
+       }
     }
+  else
+    result = fopen (filename, opentype);
 
   if (result != NULL)
     maybe_mark_cloexec (fileno (result));
 
-  return result;
+  return gdb_file_up (result);
 }
 
+#ifdef HAVE_SOCKETS
 /* See filestuff.h.  */
 
 int
-gdb_socketpair_cloexec (int namespace, int style, int protocol, int filedes[2])
+gdb_socketpair_cloexec (int domain, int style, int protocol,
+                       int filedes[2])
 {
 #ifdef HAVE_SOCKETPAIR
-  int result = socketpair (namespace, style | SOCK_CLOEXEC, protocol, filedes);
+  int result = socketpair (domain, style | SOCK_CLOEXEC, protocol, filedes);
 
   if (result != -1)
     {
@@ -331,15 +356,16 @@ gdb_socketpair_cloexec (int namespace, int style, int protocol, int filedes[2])
 /* See filestuff.h.  */
 
 int
-gdb_socket_cloexec (int namespace, int style, int protocol)
+gdb_socket_cloexec (int domain, int style, int protocol)
 {
-  int result = socket (namespace, style | SOCK_CLOEXEC, protocol);
+  int result = socket (domain, style | SOCK_CLOEXEC, protocol);
 
   if (result != -1)
     socket_mark_cloexec (result);
 
   return result;
 }
+#endif
 
 /* See filestuff.h.  */
 
@@ -370,3 +396,24 @@ gdb_pipe_cloexec (int filedes[2])
 
   return result;
 }
+
+/* Helper function which does the work for make_cleanup_close.  */
+
+static void
+do_close_cleanup (void *arg)
+{
+  int *fd = (int *) arg;
+
+  close (*fd);
+}
+
+/* See filestuff.h.  */
+
+struct cleanup *
+make_cleanup_close (int fd)
+{
+  int *saved_fd = XNEW (int);
+
+  *saved_fd = fd;
+  return make_cleanup_dtor (do_close_cleanup, saved_fd, xfree);
+}
This page took 0.029624 seconds and 4 git commands to generate.