From e8c71d94ae203a4c88cd0a90b6e40ff1ad36b193 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Tue, 25 Aug 2009 02:17:09 +0200 Subject: [PATCH] New module 'accept4'. --- ChangeLog | 13 ++++ doc/glibc-functions/accept4.texi | 8 +-- lib/accept4.c | 108 +++++++++++++++++++++++++++++++ lib/sys_socket.in.h | 26 ++++++++ m4/accept4.m4 | 19 ++++++ m4/sys_socket_h.m4 | 4 +- modules/accept4 | 28 ++++++++ modules/sys_socket | 2 + 8 files changed, 203 insertions(+), 5 deletions(-) create mode 100644 lib/accept4.c create mode 100644 m4/accept4.m4 create mode 100644 modules/accept4 diff --git a/ChangeLog b/ChangeLog index 6ad5647205..96289603d8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2009-08-23 Bruno Haible + + New module 'accept4'. + * 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. + 2009-08-24 Jim Meyering progname: also set global program_invocation_name, when possible diff --git a/doc/glibc-functions/accept4.texi b/doc/glibc-functions/accept4.texi index eaf442946a..f88ab0ad74 100644 --- a/doc/glibc-functions/accept4.texi +++ b/doc/glibc-functions/accept4.texi @@ -2,15 +2,15 @@ @subsection @code{accept4} @findex accept4 -Gnulib module: --- +Gnulib module: accept4 Portability problems fixed by Gnulib: @itemize +@item +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 -@item -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 diff --git a/lib/accept4.c b/lib/accept4.c new file mode 100644 index 0000000000..1203d9aff9 --- /dev/null +++ b/lib/accept4.c @@ -0,0 +1,108 @@ +/* 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 + +/* Specification. */ +#include + +#include +#include +#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; + } + + /* Closing fd before allocating the new fd ensures that the new fd will + have the minimum possible value. */ + close (fd); + nfd = _open_osfhandle ((long) 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) + setmode (fd, O_BINARY); + else if (flags & O_TEXT) + setmode (fd, O_TEXT); +#endif + + return fd; +} diff --git a/lib/sys_socket.in.h b/lib/sys_socket.in.h index 4553f60c9c..c4df33f399 100644 --- a/lib/sys_socket.in.h +++ b/lib/sys_socket.in.h @@ -423,5 +423,31 @@ extern int rpl_shutdown (int, int); #endif /* HAVE_SYS_SOCKET_H */ +#ifdef __cplusplus +extern "C" { +#endif + +#if @GNULIB_ACCEPT4@ +# if !@HAVE_ACCEPT4@ +/* Accept a connection on a socket, with specific opening flags. + The flags are a bitmask, possibly including O_CLOEXEC (defined in ) + and O_TEXT, O_BINARY (defined in "binary-io.h"). + See also the Linux man page at + . */ +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 */ diff --git a/m4/accept4.m4 b/m4/accept4.m4 new file mode 100644 index 0000000000..6828c928e4 --- /dev/null +++ b/m4/accept4.m4 @@ -0,0 +1,19 @@ +# 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 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 +]) diff --git a/m4/sys_socket_h.m4 b/m4/sys_socket_h.m4 index d4873fa810..e864b2c7e2 100644 --- a/m4/sys_socket_h.m4 +++ b/m4/sys_socket_h.m4 @@ -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 @@ AC_DEFUN([gl_SYS_SOCKET_H_DEFAULTS], 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]) ]) diff --git a/modules/accept4 b/modules/accept4 new file mode 100644 index 0000000000..3be57573d9 --- /dev/null +++ b/modules/accept4 @@ -0,0 +1,28 @@ +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: + + +License: +LGPL + +Maintainer: +Bruno Haible, Simon Josefsson diff --git a/modules/sys_socket b/modules/sys_socket index 58158d4d36..e082f94591 100644 --- a/modules/sys_socket +++ b/modules/sys_socket @@ -44,10 +44,12 @@ sys/socket.h: sys_socket.in.h -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; \ } > $@-t && \ -- 2.30.2