Mercurial > hg > octave-nkf > gnulib-hg
changeset 11898:90fb3f330caf
New module 'accept4'.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Tue, 25 Aug 2009 02:17:09 +0200 |
parents | 341123ec9461 |
children | f0c8cf1802a2 |
files | ChangeLog doc/glibc-functions/accept4.texi lib/accept4.c lib/sys_socket.in.h m4/accept4.m4 m4/sys_socket_h.m4 modules/accept4 modules/sys_socket |
diffstat | 8 files changed, 204 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2009-08-23 Bruno Haible <bruno@clisp.org> + + 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 <meyering@redhat.com> progname: also set global program_invocation_name, when possible
--- 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 -@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 + +Portability problems not fixed by Gnulib: +@itemize +@end itemize
new file mode 100644 --- /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 <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; + } + + /* 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; +}
--- a/lib/sys_socket.in.h +++ b/lib/sys_socket.in.h @@ -423,5 +423,31 @@ #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 <fcntl.h>) + and O_TEXT, O_BINARY (defined in "binary-io.h"). + See also the Linux man page at + <http://www.kernel.org/doc/man-pages/online/pages/man2/accept4.2.html>. */ +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 */
new file mode 100644 --- /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 <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 +])
--- 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 @@ 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]) ])
new file mode 100644 --- /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: +<sys/socket.h> + +License: +LGPL + +Maintainer: +Bruno Haible, Simon Josefsson
--- a/modules/sys_socket +++ b/modules/sys_socket @@ -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; \ } > $@-t && \