X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Faccept4.c;h=1e7e7f4521db224e1d0ec36a3b6aa6be12facd33;hb=a6b16b69fe1cad695b270dd5bf3deb2850fc4dd1;hp=1203d9aff901c0297c077a09f3c3528a062a7a6f;hpb=e8c71d94ae203a4c88cd0a90b6e40ff1ad36b193;p=gnulib.git diff --git a/lib/accept4.c b/lib/accept4.c index 1203d9aff..1e7e7f452 100644 --- a/lib/accept4.c +++ b/lib/accept4.c @@ -1,5 +1,5 @@ /* Accept a connection on a socket, with specific opening flags. - Copyright (C) 2009 Free Software Foundation, Inc. + Copyright (C) 2009-2011 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 @@ -33,6 +33,26 @@ 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) { @@ -54,29 +74,29 @@ accept4 (int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) 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; - } + 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. */ + have the minimum possible value. */ close (fd); nfd = _open_osfhandle ((long) new_handle, - O_NOINHERIT | (flags & (O_TEXT | O_BINARY))); + O_NOINHERIT | (flags & (O_TEXT | O_BINARY))); if (nfd < 0) - { - CloseHandle (new_handle); - return -1; - } + { + CloseHandle (new_handle); + return -1; + } return nfd; } # else @@ -86,13 +106,13 @@ accept4 (int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags) 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; - } + || fcntl (fd, F_SETFD, fcntl_flags | FD_CLOEXEC) == -1) + { + int saved_errno = errno; + close (fd); + errno = saved_errno; + return -1; + } } # endif #endif