From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS22989 209.51.188.0/24 X-Spam-Status: No, score=-3.1 required=3.0 tests=AWL,BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id BC2C61F461 for ; Tue, 20 Aug 2019 16:37:35 +0000 (UTC) Received: from localhost ([::1]:39678 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i078Y-0007KT-K7 for normalperson@yhbt.net; Tue, 20 Aug 2019 12:37:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:46527) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i078U-0007KL-HL for bug-gnulib@gnu.org; Tue, 20 Aug 2019 12:37:31 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i078T-0001h9-Do for bug-gnulib@gnu.org; Tue, 20 Aug 2019 12:37:30 -0400 Received: from mx1.redhat.com ([209.132.183.28]:40058) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i078T-0001fe-8H for bug-gnulib@gnu.org; Tue, 20 Aug 2019 12:37:29 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 3EBF13078A3A for ; Tue, 20 Aug 2019 16:37:28 +0000 (UTC) Received: from blue.redhat.com (ovpn-116-234.phx2.redhat.com [10.3.116.234]) by smtp.corp.redhat.com (Postfix) with ESMTP id F11575C206; Tue, 20 Aug 2019 16:37:27 +0000 (UTC) From: Eric Blake To: bug-gnulib@gnu.org Subject: [PATCH] accept4: Support SOCK_NONBLOCK, if defined Date: Tue, 20 Aug 2019 11:37:27 -0500 Message-Id: <20190820163727.5189-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.48]); Tue, 20 Aug 2019 16:37:28 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnulib-bounces+normalperson=yhbt.net@gnu.org Sender: "bug-gnulib" Ideally, we would improve our replacement to define a replacement SOCK_NONBLOCK on all platforms, and teach socket() to honor it as well; but that's a bigger task. In the meantime, if the platform already has SOCK_NONBLOCK, we should honor it when doing a fallback. * lib/accept4.c (accept4): If SOCK_NONBLOCK is defined, honor it. --- ChangeLog | 3 +++ lib/accept4.c | 21 ++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 6253f9221..f5eda2fa3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2019-08-20 Eric Blake + accept4: Support SOCK_NONBLOCK, if defined + * lib/accept4.c (accept4): If SOCK_NONBLOCK is defined, honor it. + accept4: Fix compilation when native accept4() exists. Reported by Richard W.M. Jones in https://lists.gnu.org/archive/html/bug-gnulib/2019-08/msg00029.html diff --git a/lib/accept4.c b/lib/accept4.c index c6e59c955..9ce78fa96 100644 --- a/lib/accept4.c +++ b/lib/accept4.c @@ -31,6 +31,9 @@ #ifndef SOCK_CLOEXEC # define SOCK_CLOEXEC 0 #endif +#ifndef SOCK_NONBLOCK +# define SOCK_NONBLOCK 0 +#endif int accept4 (int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flag= s) @@ -58,7 +61,7 @@ accept4 (int sockfd, struct sockaddr *addr, socklen_t *= addrlen, int flags) #endif /* Check the supported flags. */ - if ((flags & ~(SOCK_CLOEXEC | O_TEXT | O_BINARY)) !=3D 0) + if ((flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK | O_TEXT | O_BINARY)) !=3D= 0) { errno =3D EINVAL; return -1; @@ -121,6 +124,22 @@ accept4 (int sockfd, struct sockaddr *addr, socklen_= t *addrlen, int flags) # endif #endif +#if SOCK_CLOEXEC + if (flags & SOCK_NONBLOCK) + { + int fcntl_flags; + + if ((fcntl_flags =3D fcntl (fd, F_GETFL, 0)) < 0 + || fcntl (fd, F_SETFL, fcntl_flags | O_NONBLOCK) =3D=3D -1) + { + int saved_errno =3D errno; + close (fd); + errno =3D saved_errno; + return -1; + } + } +#endif + #if O_BINARY if (flags & O_BINARY) set_binary_mode (fd, O_BINARY); --=20 2.21.0