guile-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Guile-commits] 01/01: Fix accept4 gnulib update.


From: Andy Wingo
Subject: [Guile-commits] 01/01: Fix accept4 gnulib update.
Date: Wed, 15 Feb 2017 16:16:13 -0500 (EST)

wingo pushed a commit to branch master
in repository guile.

commit 4706d6982457498c60e575a026229e03820381d3
Author: Andy Wingo <address@hidden>
Date:   Wed Feb 15 22:14:37 2017 +0100

    Fix accept4 gnulib update.
    
    * m4/accept4.m4: Add missing file.
    * lib/accept4.c: Add missing file.
    * .gitignore: Update.
---
 .gitignore    |   2 +
 lib/accept4.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 m4/accept4.m4 |  18 +++++++++
 3 files changed, 148 insertions(+)

diff --git a/.gitignore b/.gitignore
index 2c1f9d8..fb9e53b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -165,3 +165,5 @@ INSTALL
 /test-suite/standalone/test-foreign-object-c
 /test-suite/standalone/test-srfi-4
 /meta/build-env
+/lib/limits.h
+/lib/stdint.h
diff --git a/lib/accept4.c b/lib/accept4.c
new file mode 100644
index 0000000..9fab9c6
--- /dev/null
+++ b/lib/accept4.c
@@ -0,0 +1,128 @@
+/* Accept a connection on a socket, with specific opening flags.
+   Copyright (C) 2009-2017 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as published by
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License 
along
+   with this program; if not, see <http://www.gnu.org/licenses/>.  */
+
+#include <config.h>
+
+/* Specification.  */
+#include <sys/socket.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include "binary-io.h"
+#include "msvc-nothrow.h"
+
+#ifndef SOCK_CLOEXEC
+# define SOCK_CLOEXEC 0
+#endif
+
+int
+accept4 (int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags)
+{
+  int fd;
+
+#if HAVE_ACCEPT4
+# undef accept4
+  /* Try the system call first, if it exists.  (We may be running with a glibc
+     that has the function but with an older kernel that lacks it.)  */
+  {
+    /* Cache the information whether the system call really exists.  */
+    static int have_accept4_really; /* 0 = unknown, 1 = yes, -1 = no */
+    if (have_accept4_really >= 0)
+      {
+        int result = accept4 (sockfd, addr, addrlen, flags);
+        if (!(result < 0 && errno == ENOSYS))
+          {
+            have_accept4_really = 1;
+            return result;
+          }
+        have_accept4_really = -1;
+      }
+  }
+#endif
+
+  /* Check the supported flags.  */
+  if ((flags & ~(SOCK_CLOEXEC | O_TEXT | O_BINARY)) != 0)
+    {
+      errno = EINVAL;
+      return -1;
+    }
+
+  fd = accept (sockfd, addr, addrlen);
+  if (fd < 0)
+    return -1;
+
+#if SOCK_CLOEXEC
+# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+/* Native Windows API.  */
+  if (flags & SOCK_CLOEXEC)
+    {
+      HANDLE curr_process = GetCurrentProcess ();
+      HANDLE old_handle = (HANDLE) _get_osfhandle (fd);
+      HANDLE new_handle;
+      int nfd;
+
+      if (!DuplicateHandle (curr_process,           /* SourceProcessHandle */
+                            old_handle,             /* SourceHandle */
+                            curr_process,           /* TargetProcessHandle */
+                            (PHANDLE) &new_handle,  /* TargetHandle */
+                            (DWORD) 0,              /* DesiredAccess */
+                            FALSE,                  /* InheritHandle */
+                            DUPLICATE_SAME_ACCESS)) /* Options */
+        {
+          close (fd);
+          errno = EBADF; /* arbitrary */
+          return -1;
+        }
+
+      /* Closing fd before allocating the new fd ensures that the new fd will
+         have the minimum possible value.  */
+      close (fd);
+      nfd = _open_osfhandle ((intptr_t) new_handle,
+                             O_NOINHERIT | (flags & (O_TEXT | O_BINARY)));
+      if (nfd < 0)
+        {
+          CloseHandle (new_handle);
+          return -1;
+        }
+      return nfd;
+    }
+# else
+/* Unix API.  */
+  if (flags & SOCK_CLOEXEC)
+    {
+      int fcntl_flags;
+
+      if ((fcntl_flags = fcntl (fd, F_GETFD, 0)) < 0
+          || fcntl (fd, F_SETFD, fcntl_flags | FD_CLOEXEC) == -1)
+        {
+          int saved_errno = errno;
+          close (fd);
+          errno = saved_errno;
+          return -1;
+        }
+    }
+# endif
+#endif
+
+#if O_BINARY
+  if (flags & O_BINARY)
+    set_binary_mode (fd, O_BINARY);
+  else if (flags & O_TEXT)
+    set_binary_mode (fd, O_TEXT);
+#endif
+
+  return fd;
+}
diff --git a/m4/accept4.m4 b/m4/accept4.m4
new file mode 100644
index 0000000..841e9b5
--- /dev/null
+++ b/m4/accept4.m4
@@ -0,0 +1,18 @@
+# accept4.m4 serial 2
+dnl Copyright (C) 2009-2017 Free Software Foundation, Inc.
+dnl This file is free software; the Free Software Foundation
+dnl gives unlimited permission to copy and/or distribute it,
+dnl with or without modifications, as long as this notice is preserved.
+
+AC_DEFUN([gl_FUNC_ACCEPT4],
+[
+  AC_REQUIRE([gl_SYS_SOCKET_H_DEFAULTS])
+
+  dnl Persuade glibc <sys/socket.h> to declare accept4().
+  AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
+
+  AC_CHECK_FUNCS_ONCE([accept4])
+  if test $ac_cv_func_accept4 != yes; then
+    HAVE_ACCEPT4=0
+  fi
+])



reply via email to

[Prev in Thread] Current Thread [Next in Thread]