Minor O_CLOEXEC optimization, "regression" fix
[deliverable/binutils-gdb.git] / gdb / common / filestuff.c
index 68f66ca6d0f96edc640fd04520f1884829a21730..4032f36328041c829196349296281813a8dd381b 100644 (file)
 #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
@@ -155,8 +157,6 @@ fdwalk (int (*func) (void *, int), void *arg)
    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);
-
 static VEC (int) *open_fds;
 
 /* An fdwalk callback function used by notice_open_fds.  It puts the
@@ -177,6 +177,33 @@ 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)
+{
+  int i, val;
+
+  for (i = 0; VEC_iterate (int, open_fds, i, val); ++i)
+    {
+      if (fd == val)
+       {
+         VEC_unordered_remove (int, open_fds, i);
+         return;
+       }
+    }
+
+  gdb_assert_not_reached (_("fd not found in open_fds"));
+}
+
 /* Helper function for close_most_fds that closes the file descriptor
    if appropriate.  */
 
@@ -250,6 +277,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 +288,8 @@ socket_mark_cloexec (int fd)
     mark_cloexec (fd);
 }
 
+#endif
+
 \f
 
 /* See filestuff.h.  */
@@ -279,10 +310,16 @@ gdb_open_cloexec (const char *filename, int flags, unsigned long mode)
 FILE *
 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;
 
@@ -292,15 +329,16 @@ gdb_fopen_cloexec (const char *filename, const char *opentype)
         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));
@@ -308,6 +346,7 @@ gdb_fopen_cloexec (const char *filename, const char *opentype)
   return result;
 }
 
+#ifdef HAVE_SOCKETS
 /* See filestuff.h.  */
 
 int
@@ -340,6 +379,7 @@ gdb_socket_cloexec (int namespace, int style, int protocol)
 
   return result;
 }
+#endif
 
 /* See filestuff.h.  */
 
This page took 0.024888 seconds and 4 git commands to generate.