bug-gnulib
[Top][All Lists]
Advanced

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

proposal: module 'accept4'


From: Bruno Haible
Subject: proposal: module 'accept4'
Date: Sat, 22 Aug 2009 23:39:16 +0200
User-agent: KMail/1.9.9

Hi,

Here's the proposed module for function 'accept4'.

It does not define the macro SOCK_CLOEXEC. I think this macro should be
replaced by a separate module, that would then also influence socket(),
socketpair() - and what about connect()?

Opinions? Objections?


2009-08-22  Bruno Haible  <address@hidden>

        * lib/sys_socket.in.h (accept4): New declaration.
        * lib/accept4.c: New file.
        * m4/accept4.m4: New file.
        * m4/sys_socket_h.m4 (gl_SYS_SOCKET_H_DEFAULTS): Initialize
        GNULIB_ACCEPT4, HAVE_ACCEPT4.
        * modules/sys_socket (Makefile.am): Substitute GNULIB_ACCEPT4,
        HAVE_ACCEPT4.
        * modules/accept4: New file.
        * doc/glibc-functions/accept4.texi: Mention the new module.

================================ lib/accept4.c ================================
/* Accept a connection on a socket, with specific opening flags.
   Copyright (C) 2009 Free Software Foundation, Inc.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU 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 General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */

#include <config.h>

/* Specification.  */
#include <sys/socket.h>

#include <errno.h>
#include <fcntl.h>
#include "binary-io.h"

#ifndef SOCK_CLOEXEC
# define SOCK_CLOEXEC 0
#endif

int
accept4 (int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags)
{
  int fd;

  /* 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 Woe32 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;
        }

      nfd = _open_osfhandle ((long) new_handle,
                             O_NOINHERIT | (flags & (O_TEXT | O_BINARY)));
      if (nfd < 0)
        {
          int saved_errno = errno;
          close (fd);
          errno = saved_errno;
          return -1;
        }

      close (fd);
      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) < 0)
        {
          int saved_errno = errno;
          close (fd);
          errno = saved_errno;
          return -1;
        }
    }
# endif
#endif
#if O_BINARY
  if (flags & O_BINARY)
    setmode (fd, O_BINARY);
  else if (flags & O_TEXT)
    setmode (fd, O_TEXT);
#endif

  return fd;
}
================================ m4/accept4.m4 ================================
# accept4.m4 serial 1
dnl Copyright (C) 2009 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
    AC_LIBOBJ([accept4])
  fi
])
=============================== modules/accept4 ===============================
Description:
accept4() function: accept a connection on a socket, with specific opening
flags.

Files:
lib/accept4.c
m4/accept4.m4

Depends-on:
sys_socket
accept
fcntl
binary-io

configure.ac:
gl_FUNC_ACCEPT4
gl_SYS_SOCKET_MODULE_INDICATOR([accept4])

Makefile.am:

Include:
<sys/socket.h>

License:
GPL

Maintainer:
Bruno Haible, Simon Josefsson
===============================================================================
--- doc/glibc-functions/accept4.texi.orig       2009-08-22 23:23:41.000000000 
+0200
+++ doc/glibc-functions/accept4.texi    2009-08-22 19:57:25.000000000 +0200
@@ -2,15 +2,15 @@
 @subsection @code{accept4}
 @findex accept4
 
-Gnulib module: ---
+Gnulib module: accept4
 
 Portability problems fixed by Gnulib:
 @itemize
address@hidden
+This function is missing on all non-glibc platforms:
+MacOS X 10.3, FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8, AIX 5.1, HP-UX 11, IRIX 
6.5, OSF/1 5.1, Solaris 10, Cygwin, mingw, Interix 3.5, BeOS.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
address@hidden
-This function is missing on all non-glibc platforms:
-MacOS X 10.3, FreeBSD 6.0, NetBSD 3.0, OpenBSD 3.8, AIX 5.1, HP-UX 11, IRIX 
6.5, OSF/1 5.1, Solaris 10, Cygwin, mingw, Interix 3.5, BeOS.
 @end itemize
--- lib/sys_socket.in.h.orig    2009-08-22 23:23:41.000000000 +0200
+++ lib/sys_socket.in.h 2009-08-22 19:40:15.000000000 +0200
@@ -423,5 +423,29 @@
 
 #endif /* HAVE_SYS_SOCKET_H */
 
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if @GNULIB_ACCEPT4@
+# if address@hidden@
+/* Accept a connection on a socket, with specific opening flags.
+   The flags are a bitmask, possibly including O_CLOEXEC (defined in <fcntl.h>)
+   and O_TEXT, O_BINARY (defined in "binary-io.h").  */
+extern int accept4 (int sockfd, struct sockaddr *addr, socklen_t *addrlen,
+                   int flags);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef accept4
+# define accept4(s,a,l,f) \
+    (GL_LINK_WARNING ("accept4 is unportable - " \
+                      "use gnulib module accept4 for portability"), \
+     accept4 (s, a, l, f))
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
 #endif /* _GL_SYS_SOCKET_H */
 #endif /* _GL_SYS_SOCKET_H */
--- m4/sys_socket_h.m4.orig     2009-08-22 23:23:41.000000000 +0200
+++ m4/sys_socket_h.m4  2009-08-22 19:37:00.000000000 +0200
@@ -1,4 +1,4 @@
-# sys_socket_h.m4 serial 12
+# sys_socket_h.m4 serial 13
 dnl Copyright (C) 2005-2009 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -140,6 +140,8 @@
   GNULIB_SENDTO=0;      AC_SUBST([GNULIB_SENDTO])
   GNULIB_SETSOCKOPT=0;  AC_SUBST([GNULIB_SETSOCKOPT])
   GNULIB_SHUTDOWN=0;    AC_SUBST([GNULIB_SHUTDOWN])
+  GNULIB_ACCEPT4=0;     AC_SUBST([GNULIB_ACCEPT4])
   HAVE_STRUCT_SOCKADDR_STORAGE=1; AC_SUBST([HAVE_STRUCT_SOCKADDR_STORAGE])
   HAVE_SA_FAMILY_T=1;   AC_SUBST([HAVE_SA_FAMILY_T])
+  HAVE_ACCEPT4=1;       AC_SUBST([HAVE_ACCEPT4])
 ])
--- modules/sys_socket.orig     2009-08-22 23:23:41.000000000 +0200
+++ modules/sys_socket  2009-08-22 19:36:23.000000000 +0200
@@ -44,10 +44,12 @@
              -e 's|@''GNULIB_SENDTO''@|$(GNULIB_SENDTO)|g' \
              -e 's|@''GNULIB_SETSOCKOPT''@|$(GNULIB_SETSOCKOPT)|g' \
              -e 's|@''GNULIB_SHUTDOWN''@|$(GNULIB_SHUTDOWN)|g' \
+             -e 's|@''GNULIB_ACCEPT4''@|$(GNULIB_ACCEPT4)|g' \
              -e 's|@''HAVE_WINSOCK2_H''@|$(HAVE_WINSOCK2_H)|g' \
              -e 's|@''HAVE_WS2TCPIP_H''@|$(HAVE_WS2TCPIP_H)|g' \
              -e 
's|@''HAVE_STRUCT_SOCKADDR_STORAGE''@|$(HAVE_STRUCT_SOCKADDR_STORAGE)|g' \
              -e 's|@''HAVE_SA_FAMILY_T''@|$(HAVE_SA_FAMILY_T)|g' \
+             -e 's|@''HAVE_ACCEPT4''@|$(HAVE_ACCEPT4)|g' \
              -e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \
              < $(srcdir)/sys_socket.in.h; \
        } > address@hidden && \




reply via email to

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