From patchwork Thu Sep 19 13:28:19 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre-Marie de Rodat X-Patchwork-Id: 1164628 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-509289-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="SNmlnfVq"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 46YySz3WDKz9sN1 for ; Thu, 19 Sep 2019 23:32:35 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=QzJSWjL5LG4OrTLiVSUGHtkBN3hh83FlLo57xKzLxA2i9J3Xvc 67oRJKzvangfWl955Id8Hn9u2Gff6JHtBiPmkgt7zR3Jp0iNPCR3vcVt729rvE5c wDJlr/hf8lVcQbBfnUIgYEZCiTs1kI4zstKYRMAhTjtEEf+DinXI3vY3U= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=CbH+x9tp2SXrzDkiGiglYHtrvyQ=; b=SNmlnfVqhcjrh9KFWQqk nwFt80iLOLkRcTtsidGPq0KYuQTONrwatVw+bjVWWlSoejSZWweOpgLP7h3mvs6e 6+y2rmkqayMgrJWJl/fffSoodseOPlURqFuVJJyvMXkumEpu6dY1kK4PqdzEphYj PsiurDT4B7ODZyePy9ggdrY= Received: (qmail 6016 invoked by alias); 19 Sep 2019 13:28:59 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 2846 invoked by uid 89); 19 Sep 2019 13:28:36 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-10.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, KAM_SHORT, SPF_NEUTRAL autolearn=ham version=3.3.1 spammy=Connect, Sin, libgnat, Portable X-HELO: eggs.gnu.org Received: from eggs.gnu.org (HELO eggs.gnu.org) (209.51.188.92) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 19 Sep 2019 13:28:31 +0000 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iAwTy-0001QK-RC for gcc-patches@gcc.gnu.org; Thu, 19 Sep 2019 09:28:30 -0400 Received: from rock.gnat.com ([2620:20:4000:0:a9e:1ff:fe9b:1d1]:58721) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iAwTy-0001OT-2F for gcc-patches@gcc.gnu.org; Thu, 19 Sep 2019 09:28:26 -0400 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id F2EEC56033; Thu, 19 Sep 2019 09:28:19 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id cKcB2KGxtukT; Thu, 19 Sep 2019 09:28:19 -0400 (EDT) Received: from tron.gnat.com (tron.gnat.com [205.232.38.10]) by rock.gnat.com (Postfix) with ESMTP id BB10B5602D; Thu, 19 Sep 2019 09:28:19 -0400 (EDT) Received: by tron.gnat.com (Postfix, from userid 4862) id B9EBA6B4; Thu, 19 Sep 2019 09:28:19 -0400 (EDT) Date: Thu, 19 Sep 2019 09:28:19 -0400 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Cc: Dmitriy Anisimkov Subject: [Ada] New routine GNAT.Sockets.Create_Socket_Pair Message-ID: <20190919132819.GA41853@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 2620:20:4000:0:a9e:1ff:fe9b:1d1 X-IsSubscribed: yes New routine to create 2 connected sockets. This routine is analog of the UNIX system call socketpair. On UNIX platforms it is implemented on the base of socketpair. On other platforms it is implemented by conecting network sockets over loopback interface. Tested on x86_64-pc-linux-gnu, committed on trunk 2019-09-19 Dmitriy Anisimkov gcc/ada/ * libgnat/g-socket.ads, libgnat/g-socket.adb (Create_Socket_Pair): New routine. * libgnat/g-socthi.ads (OS_Has_Socketpair): Boolean constant. (C_Socketpair): New imported routine. * libgnat/g-socthi__mingw.ads, libgnat/g-socthi__vxworks.ads (Default_Socket_Pair_Family): New constant. (C_Socketpair): New routine. * libgnat/g-socthi__mingw.adb, libgnat/g-socthi__vxworks.adb (C_Socketpair): Is separated in anouther file. * libgnat/g-sthcso.adb (C_Socketpair): Non UNIX implementation. * libgnat/g-stsifd__sockets.adb: Reuse C_Socketpair. --- gcc/ada/libgnat/g-socket.adb +++ gcc/ada/libgnat/g-socket.adb @@ -867,6 +867,34 @@ package body GNAT.Sockets is Socket := Socket_Type (Res); end Create_Socket; + ------------------------ + -- Create_Socket_Pair -- + ------------------------ + + procedure Create_Socket_Pair + (Left : out Socket_Type; + Right : out Socket_Type; + Family : Family_Type := Family_Unspec; + Mode : Mode_Type := Socket_Stream; + Level : Level_Type := IP_Protocol_For_IP_Level) + is + Res : C.int; + Pair : aliased Thin_Common.Fd_Pair; + + begin + Res := C_Socketpair + ((if Family = Family_Unspec then Default_Socket_Pair_Family + else Families (Family)), + Modes (Mode), Levels (Level), Pair'Access); + + if Res = Failure then + Raise_Socket_Error (Socket_Errno); + end if; + + Left := Socket_Type (Pair (Pair'First)); + Right := Socket_Type (Pair (Pair'Last)); + end Create_Socket_Pair; + ----------- -- Empty -- ----------- --- gcc/ada/libgnat/g-socket.ads +++ gcc/ada/libgnat/g-socket.ads @@ -1104,7 +1104,17 @@ package GNAT.Sockets is Family : Family_Type := Family_Inet; Mode : Mode_Type := Socket_Stream; Level : Level_Type := IP_Protocol_For_IP_Level); - -- Create an endpoint for communication. Raises Socket_Error on error + -- Create an endpoint for communication. Raises Socket_Error on error. + + procedure Create_Socket_Pair + (Left : out Socket_Type; + Right : out Socket_Type; + Family : Family_Type := Family_Unspec; + Mode : Mode_Type := Socket_Stream; + Level : Level_Type := IP_Protocol_For_IP_Level); + -- Create two connected sockets. Raises Socket_Error on error. + -- If Family is unspecified, it creates Family_Unix sockets on UNIX and + -- Family_Inet sockets on non UNIX platforms. procedure Accept_Socket (Server : Socket_Type; --- gcc/ada/libgnat/g-socthi.ads +++ gcc/ada/libgnat/g-socthi.ads @@ -184,6 +184,16 @@ package GNAT.Sockets.Thin is function C_System (Command : System.Address) return C.int; + Default_Socket_Pair_Family : constant := SOSC.AF_UNIX; + -- UNIX has socketpair system call and AF_UNIX family is widely supported + + function C_Socketpair + (Domain : C.int; + Typ : C.int; + Protocol : C.int; + Fds : not null access Fd_Pair) return C.int; + -- Creates pair of connected sockets + ------------------------------------------------------- -- Signalling file descriptors for selector abortion -- ------------------------------------------------------- @@ -249,6 +259,7 @@ private pragma Import (C, C_Select, "select"); pragma Import (C, C_Setsockopt, "setsockopt"); pragma Import (C, C_Shutdown, "shutdown"); + pragma Import (C, C_Socketpair, "socketpair"); pragma Import (C, C_System, "system"); pragma Import (C, Nonreentrant_Gethostbyname, "gethostbyname"); --- gcc/ada/libgnat/g-socthi__mingw.adb +++ gcc/ada/libgnat/g-socthi__mingw.adb @@ -511,6 +511,16 @@ package body GNAT.Sockets.Thin is return System.CRTL.ssize_t (Count); end C_Sendmsg; + ------------------ + -- C_Socketpair -- + ------------------ + + function C_Socketpair + (Domain : C.int; + Typ : C.int; + Protocol : C.int; + Fds : not null access Fd_Pair) return C.int is separate; + -------------- -- Finalize -- -------------- --- gcc/ada/libgnat/g-socthi__mingw.ads +++ gcc/ada/libgnat/g-socthi__mingw.ads @@ -177,6 +177,17 @@ package GNAT.Sockets.Thin is Typ : C.int; Protocol : C.int) return C.int; + Default_Socket_Pair_Family : constant := SOSC.AF_INET; + -- Windows has not socketpair system call, and C_Socketpair below is + -- implemented on loopback connected network sockets. + + function C_Socketpair + (Domain : C.int; + Typ : C.int; + Protocol : C.int; + Fds : not null access Fd_Pair) return C.int; + -- Creates pair of connected sockets + function C_System (Command : System.Address) return C.int; --- gcc/ada/libgnat/g-socthi__vxworks.adb +++ gcc/ada/libgnat/g-socthi__vxworks.adb @@ -419,6 +419,16 @@ package body GNAT.Sockets.Thin is return R; end C_Socket; + ------------------ + -- C_Socketpair -- + ------------------ + + function C_Socketpair + (Domain : C.int; + Typ : C.int; + Protocol : C.int; + Fds : not null access Fd_Pair) return C.int is separate; + -------------- -- Finalize -- -------------- --- gcc/ada/libgnat/g-socthi__vxworks.ads +++ gcc/ada/libgnat/g-socthi__vxworks.ads @@ -178,6 +178,17 @@ package GNAT.Sockets.Thin is Typ : C.int; Protocol : C.int) return C.int; + Default_Socket_Pair_Family : constant := SOSC.AF_INET; + -- VxWorks has not socketpair system call, and C_Socketpair below is + -- implemented on loopback connected network sockets. + + function C_Socketpair + (Domain : C.int; + Typ : C.int; + Protocol : C.int; + Fds : not null access Fd_Pair) return C.int; + -- Creates pair of connected sockets + function C_System (Command : System.Address) return C.int; --- /dev/null new file mode 100644 +++ gcc/ada/libgnat/g-sthcso.adb @@ -0,0 +1,213 @@ +------------------------------------------------------------------------------ +-- -- +-- GNAT COMPILER COMPONENTS -- +-- -- +-- G N A T . S O C K E T S . T H I N . C _ S O C K E T P A I R -- +-- -- +-- B o d y -- +-- -- +-- Copyright (C) 2001-2019, AdaCore -- +-- -- +-- GNAT is free software; you can redistribute it and/or modify it under -- +-- terms of the GNU General Public License as published by the Free Soft- -- +-- ware Foundation; either version 3, or (at your option) any later ver- -- +-- sion. GNAT is distributed in the hope that it will be useful, but WITH- -- +-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -- +-- or FITNESS FOR A PARTICULAR PURPOSE. -- +-- -- +-- As a special exception under Section 7 of GPL version 3, you are granted -- +-- additional permissions described in the GCC Runtime Library Exception, -- +-- version 3.1, as published by the Free Software Foundation. -- +-- -- +-- You should have received a copy of the GNU General Public License and -- +-- a copy of the GCC Runtime Library Exception along with this program; -- +-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see -- +-- . -- +-- -- +-- GNAT was originally developed by the GNAT team at New York University. -- +-- Extensive contributions were provided by Ada Core Technologies Inc. -- +-- -- +------------------------------------------------------------------------------ + +-- Portable sockets-based implementation of the C_Socketpair used for +-- platforms that do not support UNIX socketpair system call. + +-- Note: this code is only for non-UNIX platforms. + +separate (GNAT.Sockets.Thin) +function C_Socketpair + (Domain : C.int; + Typ : C.int; + Protocol : C.int; + Fds : not null access Fd_Pair) return C.int +is + use type C.char_array; + + L_Sock, C_Sock, P_Sock : C.int := Failure; + -- Listening socket, client socket and peer socket + + Family : constant Family_Type := + (case Domain is + when SOSC.AF_INET => Family_Inet, + when SOSC.AF_INET6 => Family_Inet6, + when others => Family_Unspec); + + Len : aliased C.int := C.int (Lengths (Family)); + + C_Sin : aliased Sockaddr; + C_Bin : aliased C.char_array (1 .. C.size_t (Len)); + for C_Bin'Address use C_Sin'Address; + -- Address of listening and client socket and it's binary representation. + -- We need binary representation because Ada does not allow to compare + -- unchecked union if either of the operands lacks inferable discriminants. + -- RM-B-3-3 23/2. + + P_Sin : aliased Sockaddr; + P_Bin : aliased C.char_array (1 .. C.size_t (Len)); + for P_Bin'Address use P_Sin'Address; + -- Address of peer socket and it's binary representation + + T_Sin : aliased Sockaddr; + T_Bin : aliased C.char_array (1 .. C.size_t (Len)); + for T_Bin'Address use T_Sin'Address; + -- Temporary address to compare and check that address and port of the + -- socket equal to peer address and port of the opposite connected socket. + + Res : C.int with Warnings => Off; + +begin + Set_Family (C_Sin.Sin_Family, Family); + + case Family is + when Family_Inet => + C_Sin.Sin_Addr.S_B1 := 127; + C_Sin.Sin_Addr.S_B4 := 1; + + when Family_Inet6 => + C_Sin.Sin6_Addr (C_Sin.Sin6_Addr'Last) := 1; + + when others => + Set_Socket_Errno (SOSC.EAFNOSUPPORT); + return Failure; + end case; + + for J in 1 .. 10 loop + -- Retry loop, in case the C_Connect below fails + + C_Sin.Sin_Port := 0; + + -- Create a listening socket + + L_Sock := C_Socket (Domain, Typ, Protocol); + exit when L_Sock = Failure; + + -- Bind the socket to an available port on localhost + + Res := C_Bind (L_Sock, C_Sin'Address, Len); + exit when Res = Failure; + + -- Get assigned port + + Res := C_Getsockname (L_Sock, C_Sin'Address, Len'Access); + exit when Res = Failure; + + -- Set socket to listen mode, with a backlog of 1 to guarantee that + -- exactly one call to connect(2) succeeds. + + Res := C_Listen (L_Sock, 1); + exit when Res = Failure; + + -- Create read end (client) socket + + C_Sock := C_Socket (Domain, Typ, Protocol); + exit when C_Sock = Failure; + + -- Connect listening socket + + Res := C_Connect (C_Sock, C_Sin'Address, Len); + + if Res = Failure then + -- In rare cases, the above C_Bind chooses a port that is still + -- marked "in use", even though it has been closed (perhaps by some + -- other process that has already exited). This causes the above + -- C_Connect to fail with EADDRINUSE. In this case, we close the + -- ports, and loop back to try again. This mysterious Windows + -- behavior is documented. See, for example: + -- http://msdn2.microsoft.com/en-us/library/ms737625.aspx + -- In an experiment with 2000 calls, 21 required exactly one retry, 7 + -- required two, and none required three or more. Note that no delay + -- is needed between retries; retrying C_Bind will typically produce + -- a different port. + + exit when Socket_Errno /= SOSC.EADDRINUSE; + + goto Repeat; + end if; + + -- Since the call to connect(2) has succeeded and the backlog limit + -- on the listening socket is 1, we know that there is now exactly + -- one pending connection on L_Sock, which is the one from R_Sock. + + P_Sin.Sun_Path := (others => C.nul); + + P_Sock := C_Accept (L_Sock, P_Sin'Address, Len'Access); + exit when P_Sock = Failure; + + -- Address and port of the socket equal to peer address and port of the + -- opposite connected socket. + + Res := C_Getsockname (P_Sock, T_Sin'Address, Len'Access); + exit when Res = Failure; + + if T_Bin /= C_Bin then + goto Repeat; + end if; + + -- Address and port of the socket equal to peer address and port of the + -- opposite connected socket. + + Res := C_Getsockname (C_Sock, T_Sin'Address, Len'Access); + exit when Res = Failure; + + if T_Bin /= P_Bin then + goto Repeat; + end if; + + -- Close listening socket (ignore exit status) + + Res := C_Close (L_Sock); + + Fds.all := (Read_End => C_Sock, Write_End => P_Sock); + + return Thin_Common.Success; + + <> + Res := C_Close (C_Sock); + C_Sock := Failure; + Res := C_Close (P_Sock); + P_Sock := Failure; + Res := C_Close (L_Sock); + L_Sock := Failure; + end loop; + + declare + Saved_Errno : constant Integer := Socket_Errno; + + begin + if P_Sock /= Failure then + Res := C_Close (P_Sock); + end if; + + if C_Sock /= Failure then + Res := C_Close (C_Sock); + end if; + + if L_Sock /= Failure then + Res := C_Close (L_Sock); + end if; + + Set_Socket_Errno (Saved_Errno); + end; + + return Failure; +end C_Socketpair; --- gcc/ada/libgnat/g-stsifd__sockets.adb +++ gcc/ada/libgnat/g-stsifd__sockets.adb @@ -57,154 +57,20 @@ package body Signalling_Fds is ------------ function Create (Fds : not null access Fd_Pair) return C.int is - L_Sock, R_Sock, W_Sock : C.int := Failure; - -- Listening socket, read socket and write socket - - Sin : aliased Sockaddr; - Len : aliased C.int; - -- Address of listening socket - - Res : C.int; - pragma Warnings (Off, Res); - -- Return status of system calls (usually ignored, hence warnings off) - + Res : constant C.int := + C_Socketpair (SOSC.AF_INET, SOSC.SOCK_STREAM, 0, Fds); begin - Fds.all := (Read_End | Write_End => Failure); - - -- We open two signalling sockets. One of them is used to send data - -- to the other, which is included in a C_Select socket set. The - -- communication is used to force the call to C_Select to complete, - -- and the waiting task to resume its execution. - - loop - -- Retry loop, in case the C_Connect below fails - - -- Create a listening socket - - L_Sock := C_Socket (SOSC.AF_INET, SOSC.SOCK_STREAM, 0); - - if L_Sock = Failure then - goto Fail; - end if; - - -- Bind the socket to an available port on localhost - - Set_Family (Sin.Sin_Family, Family_Inet); - Sin.Sin_Addr.S_B1 := 127; - Sin.Sin_Addr.S_B2 := 0; - Sin.Sin_Addr.S_B3 := 0; - Sin.Sin_Addr.S_B4 := 1; - Sin.Sin_Port := 0; - - Len := C.int (Lengths (Family_Inet)); - Res := C_Bind (L_Sock, Sin'Address, Len); - - if Res = Failure then - goto Fail; - end if; - - -- Get assigned port - - Res := C_Getsockname (L_Sock, Sin'Address, Len'Access); - if Res = Failure then - goto Fail; - end if; - - -- Set socket to listen mode, with a backlog of 1 to guarantee that - -- exactly one call to connect(2) succeeds. - - Res := C_Listen (L_Sock, 1); - - if Res = Failure then - goto Fail; - end if; - - -- Create read end (client) socket - - R_Sock := C_Socket (SOSC.AF_INET, SOSC.SOCK_STREAM, 0); - - if R_Sock = Failure then - goto Fail; - end if; - - -- Connect listening socket - - Res := C_Connect (R_Sock, Sin'Address, Len); - - exit when Res /= Failure; - - if Socket_Errno /= SOSC.EADDRINUSE then - goto Fail; - end if; - - -- In rare cases, the above C_Bind chooses a port that is still - -- marked "in use", even though it has been closed (perhaps by some - -- other process that has already exited). This causes the above - -- C_Connect to fail with EADDRINUSE. In this case, we close the - -- ports, and loop back to try again. This mysterious Windows - -- behavior is documented. See, for example: - -- http://msdn2.microsoft.com/en-us/library/ms737625.aspx - -- In an experiment with 2000 calls, 21 required exactly one retry, 7 - -- required two, and none required three or more. Note that no delay - -- is needed between retries; retrying C_Bind will typically produce - -- a different port. - - pragma Assert (Res = Failure - and then - Socket_Errno = SOSC.EADDRINUSE); - Res := C_Close (W_Sock); - W_Sock := Failure; - Res := C_Close (R_Sock); - R_Sock := Failure; - end loop; - - -- Since the call to connect(2) has succeeded and the backlog limit on - -- the listening socket is 1, we know that there is now exactly one - -- pending connection on L_Sock, which is the one from R_Sock. - - W_Sock := C_Accept (L_Sock, Sin'Address, Len'Access); - - if W_Sock = Failure then - goto Fail; + if Res /= Failure then + -- Set TCP_NODELAY on Fds (Write_End), since we always want to send + -- the data out immediately. + + Set_Socket_Option + (Socket => Socket_Type (Fds (Write_End)), + Level => IP_Protocol_For_TCP_Level, + Option => (Name => No_Delay, Enabled => True)); end if; - -- Set TCP_NODELAY on W_Sock, since we always want to send the data out - -- immediately. - - Set_Socket_Option - (Socket => Socket_Type (W_Sock), - Level => IP_Protocol_For_TCP_Level, - Option => (Name => No_Delay, Enabled => True)); - - -- Close listening socket (ignore exit status) - - Res := C_Close (L_Sock); - - Fds.all := (Read_End => R_Sock, Write_End => W_Sock); - - return Thin_Common.Success; - - <> - declare - Saved_Errno : constant Integer := Socket_Errno; - - begin - if W_Sock /= Failure then - Res := C_Close (W_Sock); - end if; - - if R_Sock /= Failure then - Res := C_Close (R_Sock); - end if; - - if L_Sock /= Failure then - Res := C_Close (L_Sock); - end if; - - Set_Socket_Errno (Saved_Errno); - end; - - return Failure; + return Res; end Create; ----------