From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from bee.birch.relay.mailchannels.net (bee.birch.relay.mailchannels.net [23.83.209.14]) by sourceware.org (Postfix) with ESMTPS id E3A783858412 for ; Mon, 17 Jan 2022 03:28:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E3A783858412 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gotplt.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gotplt.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 7D8F9861BCB; Mon, 17 Jan 2022 03:28:16 +0000 (UTC) Received: from pdx1-sub0-mail-a306.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id A4D8B861B23; Mon, 17 Jan 2022 03:28:15 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a306.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.114.196.230 (trex/6.4.3); Mon, 17 Jan 2022 03:28:16 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Blushing-Soft: 51e5f36a424f2747_1642390096330_3454268817 X-MC-Loop-Signature: 1642390096330:1433763987 X-MC-Ingress-Time: 1642390096329 Received: from [192.168.1.174] (unknown [1.186.224.209]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a306.dreamhost.com (Postfix) with ESMTPSA id 4Jccmt3qtwz32; Sun, 16 Jan 2022 19:28:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gotplt.org; s=gotplt.org; t=1642390095; bh=FJhNL42K+H0Of1jqEHMFZJH9GbU=; h=Date:Subject:To:From:Content-Type:Content-Transfer-Encoding; b=NEccBqK0ZsaWfYHd9c+psTw7yXVUrCWl2gv3/x/URPPeoYcoLiE8mpCQyiSUtfp9K gQihnzTi7SlYNCEF1F2otFDAf0QEISC/68gAvuk4PlloIcCFF8RZhAObnkfaC8vErg J0S5YSN3+P7IQ5AK3huDdoFDwVb6DHZXpdvOEs4w= Message-ID: <3edfe5a7-0c49-d1fb-974d-73fa17e5ba08@gotplt.org> Date: Mon, 17 Jan 2022 08:58:09 +0530 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Subject: Re: [PATCH 1/4] socket: Add the __sockaddr_un_set function Content-Language: en-US To: Florian Weimer , libc-alpha@sourceware.org References: From: Siddhesh Poyarekar In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-3036.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, NICE_REPLY_A, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 17 Jan 2022 03:28:20 -0000 On 14/01/2022 13:53, Florian Weimer via Libc-alpha wrote: > --- > include/sys/un.h | 12 +++++++ > socket/Makefile | 6 +++- > socket/sockaddr_un_set.c | 41 ++++++++++++++++++++++++ > socket/tst-sockaddr_un_set.c | 62 ++++++++++++++++++++++++++++++++++++ > 4 files changed, 120 insertions(+), 1 deletion(-) > create mode 100644 socket/sockaddr_un_set.c > create mode 100644 socket/tst-sockaddr_un_set.c LGTM. Reviewed-by: Siddhesh Poyarekar > > diff --git a/include/sys/un.h b/include/sys/un.h > index bdbee99980..152afd9fc7 100644 > --- a/include/sys/un.h > +++ b/include/sys/un.h > @@ -1 +1,13 @@ > #include > + > +#ifndef _ISOMAC > + > +/* Set ADDR->sun_family to AF_UNIX and ADDR->sun_path to PATHNAME. > + Return 0 on success or -1 on failure (due to overlong PATHNAME). > + The caller should always use sizeof (struct sockaddr_un) as the > + socket address length, disregaring the length of PATHNAME. > + Only concrete (non-abstract) pathnames are supported. */ > +int __sockaddr_un_set (struct sockaddr_un *addr, const char *pathname) > + attribute_hidden; > + > +#endif /* _ISOMAC */ > diff --git a/socket/Makefile b/socket/Makefile > index 39333e10ca..156eec6c85 100644 > --- a/socket/Makefile > +++ b/socket/Makefile > @@ -29,13 +29,17 @@ headers := sys/socket.h sys/un.h bits/sockaddr.h bits/socket.h \ > routines := accept bind connect getpeername getsockname getsockopt \ > listen recv recvfrom recvmsg send sendmsg sendto \ > setsockopt shutdown socket socketpair isfdtype opensock \ > - sockatmark accept4 recvmmsg sendmmsg > + sockatmark accept4 recvmmsg sendmmsg sockaddr_un_set > > tests := \ > tst-accept4 \ > tst-sockopt \ > # tests > > +tests-internal := \ > + tst-sockaddr_un_set \ > + # tests-internal > + > tests-time64 := \ > tst-sockopt-time64 \ > # tests > diff --git a/socket/sockaddr_un_set.c b/socket/sockaddr_un_set.c > new file mode 100644 > index 0000000000..0bd40dc34e > --- /dev/null > +++ b/socket/sockaddr_un_set.c > @@ -0,0 +1,41 @@ > +/* Set the sun_path member of struct sockaddr_un. > + Copyright (C) 2022 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library 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 > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + . */ > + > +#include > +#include > +#include > +#include > + > +int > +__sockaddr_un_set (struct sockaddr_un *addr, const char *pathname) > +{ > + size_t name_length = strlen (pathname); > + > + /* The kernel supports names of exactly sizeof (addr->sun_path) > + bytes, without a null terminator, but userspace does not; see the > + SUN_LEN macro. */ > + if (name_length >= sizeof (addr->sun_path)) > + { > + __set_errno (EINVAL); /* Error code used by the kernel. */ > + return -1; > + } > + > + addr->sun_family = AF_UNIX; > + memcpy (addr->sun_path, pathname, name_length + 1); > + return 0; > +} > diff --git a/socket/tst-sockaddr_un_set.c b/socket/tst-sockaddr_un_set.c > new file mode 100644 > index 0000000000..29c2a81afd > --- /dev/null > +++ b/socket/tst-sockaddr_un_set.c > @@ -0,0 +1,62 @@ > +/* Test the __sockaddr_un_set function. > + Copyright (C) 2022 Free Software Foundation, Inc. > + This file is part of the GNU C Library. > + > + The GNU C Library is free software; you can redistribute it and/or > + modify it under the terms of the GNU Lesser General Public > + License as published by the Free Software Foundation; either > + version 2.1 of the License, or (at your option) any later version. > + > + The GNU C Library 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 > + Lesser General Public License for more details. > + > + You should have received a copy of the GNU Lesser General Public > + License along with the GNU C Library; if not, see > + . */ > + > +/* Re-compile the function because the version in libc is not > + exported. */ > +#include "sockaddr_un_set.c" > + > +#include > + > +static int > +do_test (void) > +{ > + struct sockaddr_un sun; > + > + memset (&sun, 0xcc, sizeof (sun)); > + __sockaddr_un_set (&sun, ""); > + TEST_COMPARE (sun.sun_family, AF_UNIX); > + TEST_COMPARE (__sockaddr_un_set (&sun, ""), 0); > + > + memset (&sun, 0xcc, sizeof (sun)); > + TEST_COMPARE (__sockaddr_un_set (&sun, "/example"), 0); > + TEST_COMPARE_STRING (sun.sun_path, "/example"); > + > + { > + char pathname[108]; /* Length of sun_path (ABI constant). */ > + memset (pathname, 'x', sizeof (pathname)); > + pathname[sizeof (pathname) - 1] = '\0'; > + memset (&sun, 0xcc, sizeof (sun)); > + TEST_COMPARE (__sockaddr_un_set (&sun, pathname), 0); > + TEST_COMPARE (sun.sun_family, AF_UNIX); > + TEST_COMPARE_STRING (sun.sun_path, pathname); > + } > + > + { > + char pathname[109]; > + memset (pathname, 'x', sizeof (pathname)); > + pathname[sizeof (pathname) - 1] = '\0'; > + memset (&sun, 0xcc, sizeof (sun)); > + errno = 0; > + TEST_COMPARE (__sockaddr_un_set (&sun, pathname), -1); > + TEST_COMPARE (errno, EINVAL); > + } > + > + return 0; > +} > + > +#include