From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ot1-x32e.google.com (mail-ot1-x32e.google.com [IPv6:2607:f8b0:4864:20::32e]) by sourceware.org (Postfix) with ESMTPS id 219AF3844740 for ; Tue, 18 Apr 2023 12:13:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 219AF3844740 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-ot1-x32e.google.com with SMTP id 46e09a7af769-6a437526b7cso347650a34.1 for ; Tue, 18 Apr 2023 05:13:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1681819999; x=1684411999; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:from:to:cc:subject:date:message-id:reply-to; bh=DtcPw3gixuZM8iaDErgLoT8vnfNLJdk68cjsW0/vWmM=; b=GVHVWuJQT0IlA2vJE963GrMNQ51nR8Sknw9tGByqNss/sXn1FQXQrcMhg8Pusf6iPg SysJDyQZVK3OFigQjEji60P8YfHb7HUHQqxuorNHH3R6X0BEKFH8E2spMbviPI95SvOl NvLIjB6LC+oJzZ7hxbWY0ksk+K8xNDTyU088rnjp9hneflQmCqJ0onlxj1AxjoUus36K qyr+9IkC3yuNyn6qZNODIJCMgyQobWebzYkqPY0ru9SU4TyLzF4Edbk4QUjP5LC8/GWW sumC+1tPfErZuF/X6AmbGxwwm1AjPohd1SGIyu6ziJkFPD44Wj3DJx763fdEjwXOGjoC yq8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681819999; x=1684411999; h=content-transfer-encoding:in-reply-to:organization:from:references :cc:to:content-language:subject:user-agent:mime-version:date :message-id:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=DtcPw3gixuZM8iaDErgLoT8vnfNLJdk68cjsW0/vWmM=; b=LdLblOSA5aK9bqqU6HfMTppGzPEvzRwXE2LoPYb46xztQDoXUUZVzMdVfcC14S3HjD F0h7yMJgfRg/f8Lr8rCApgnvCIdItovCLjgvNqRezQNSZGRWY0aKrMputo5KDZTDrHXA kSHg83Au1vtWYso2S5y+RvG2NwmHcskJp4AAbVdPjhoi+VNSm0ILeuDe3X4FiE4lkWfv ADLzfWMyIu5bknShZtlXKX8fUvijzvPWP5WZ91gSDChCofxgZp2LW4z8gjQ53FTqFtQY zVB4jPeR8m3atju9vQ9vJRyW515BLtXblUwZ6xxQjTj3AcnJ3KOmgSfTimoMH9s1Ao/p 3Ptg== X-Gm-Message-State: AAQBX9d4TgGJugCQXlKlK3y2YlmTolUhsDdwYe6IBflG2E6hqq3jCgKL VwRVBJ0KcgpcW+lsyLzBcdxBIQ== X-Google-Smtp-Source: AKy350YRIeq3pR0PJEvuy+Z/WjTKQlI71UUsKZ4QkbrsiIlll11EFGqfJmwf/aC4+N7E/2JteQWT1A== X-Received: by 2002:a9d:7a58:0:b0:6a1:37f1:9863 with SMTP id z24-20020a9d7a58000000b006a137f19863mr772658otm.11.1681819999449; Tue, 18 Apr 2023 05:13:19 -0700 (PDT) Received: from ?IPV6:2804:1b3:a7c3:333:6c1b:2ac3:fde1:4edf? ([2804:1b3:a7c3:333:6c1b:2ac3:fde1:4edf]) by smtp.gmail.com with ESMTPSA id y20-20020a056830209400b006a305c68617sm5444883otq.53.2023.04.18.05.13.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 18 Apr 2023 05:13:18 -0700 (PDT) Message-ID: Date: Tue, 18 Apr 2023 09:13:15 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.10.0 Subject: Re: [RFC PATCH 4/4] socket: Add a test for MSG_CMSG_CLOEXEC Content-Language: en-US To: Sergey Bugaev , libc-alpha@sourceware.org, bug-hurd@gnu.org Cc: Samuel Thibault , Emilio Pozuelo Monfort References: <20230417133902.99040-1-bugaevc@gmail.com> <20230417133902.99040-4-bugaevc@gmail.com> From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: <20230417133902.99040-4-bugaevc@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-14.5 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_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 17/04/23 10:39, Sergey Bugaev via Libc-alpha wrote: > Signed-off-by: Sergey Bugaev Add some description of what the test is accomplishing here. Some small nits below, the patch LGTM. > --- > > This is an attempt to write a test, roughly based on > sysdeps/unix/sysv/linux/tst-scm_rights.c > > Please tell me if this needs any changes. For instance, I opted to do > my own error checking for sendmsg/recvmsg/socketpair, but I could've > alternatively added xsendmsg/xrecvmsg/xsocketpair. This is also looser > on the coding style side, e.g. variables are defined mid-function > instead of upfront, since that's what I saw in tst-scm_rights.c; I can > get rid of that if that's not intended. > > I placed this into the generic socket/ since it should also work on > Linux (though I only tested it on i686-gnu myself). Please tell me if > this would be better suited for sysdeps/mach/hurd/ instead. > > socket/Makefile | 1 + > socket/tst-cmsg_cloexec.c | 124 ++++++++++++++++++++++++++++++++++++++ > 2 files changed, 125 insertions(+) > create mode 100644 socket/tst-cmsg_cloexec.c > > diff --git a/socket/Makefile b/socket/Makefile > index fffed7dd..94951ae3 100644 > --- a/socket/Makefile > +++ b/socket/Makefile > @@ -35,6 +35,7 @@ tests := \ > tst-accept4 \ > tst-sockopt \ > tst-cmsghdr \ > + tst-cmsg_cloexec \ > # tests > > tests-internal := \ > diff --git a/socket/tst-cmsg_cloexec.c b/socket/tst-cmsg_cloexec.c > new file mode 100644 > index 00000000..237c3db2 > --- /dev/null > +++ b/socket/tst-cmsg_cloexec.c > @@ -0,0 +1,124 @@ > +/* Smoke test for MSG_CMSG_CLOEXEC. > + Copyright (C) 2021-2023 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 > +#include > +#include > + > +static void > +send_fd (int sockfd, int fd) > +{ > + /* const */ char data[] = "hello"; Just remove the 'const' comment here. > + struct iovec iov = { .iov_base = data, .iov_len = sizeof (data) }; > + > + union > + { > + struct cmsghdr header; > + char bytes[CMSG_SPACE (sizeof (fd))]; > + } cmsg_storage; > + > + struct msghdr msg = > + { > + .msg_iov = &iov, > + .msg_iovlen = 1, > + .msg_control = cmsg_storage.bytes, > + .msg_controllen = CMSG_LEN (sizeof (fd)) > + }; > + > + struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg); > + cmsg->cmsg_level = SOL_SOCKET; > + cmsg->cmsg_type = SCM_RIGHTS; > + cmsg->cmsg_len = CMSG_LEN (sizeof (fd)); > + memcpy (CMSG_DATA (cmsg), &fd, sizeof (fd)); > + > + ssize_t nsent = sendmsg (sockfd, &msg, 0); > + if (nsent < 0) > + FAIL_EXIT1 ("sendmsg (%d): %m", sockfd); > + TEST_COMPARE (nsent, sizeof (data)); > +} > + > +static int > +recv_fd (int sockfd, int flags) > +{ > + char buffer[100]; > + struct iovec iov = { .iov_base = buffer, .iov_len = sizeof (buffer) }; > + > + union > + { > + struct cmsghdr header; > + char bytes[100]; > + } cmsg_storage; > + > + struct msghdr msg = > + { > + .msg_iov = &iov, > + .msg_iovlen = 1, > + .msg_control = cmsg_storage.bytes, > + .msg_controllen = sizeof (cmsg_storage) > + }; > + > + ssize_t nrecv = recvmsg (sockfd, &msg, flags); > + if (nrecv < 0) > + FAIL_EXIT1 ("recvmsg (%d): %m", sockfd); > + > + TEST_COMPARE (msg.msg_controllen, CMSG_LEN (sizeof (int))); > + struct cmsghdr *cmsg = CMSG_FIRSTHDR (&msg); > + TEST_COMPARE (cmsg->cmsg_level, SOL_SOCKET); > + TEST_COMPARE (cmsg->cmsg_type, SCM_RIGHTS); > + TEST_COMPARE (cmsg->cmsg_len, CMSG_LEN (sizeof (int))); > + > + int fd; > + memcpy (&fd, CMSG_DATA (cmsg), sizeof (fd)); > + return fd; > +} > + > +static int > +do_test (void) > +{ > + int sockfd[2]; > + int newfd; > + int flags; > + int rc = socketpair (AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockfd); > + if (rc < 0) > + FAIL_EXIT1 ("socketpair: %m"); > + > + send_fd (sockfd[1], STDIN_FILENO); > + newfd = recv_fd (sockfd[0], 0); > + TEST_VERIFY_EXIT (newfd > 0); > + flags = fcntl (newfd, F_GETFD, 0); > + TEST_VERIFY_EXIT (flags != -1); > + TEST_VERIFY (!(flags & FD_CLOEXEC)); > + xclose (newfd); > + > + send_fd (sockfd[1], STDIN_FILENO); > + newfd = recv_fd (sockfd[0], MSG_CMSG_CLOEXEC); > + TEST_VERIFY_EXIT (newfd > 0); > + flags = fcntl (newfd, F_GETFD, 0); > + TEST_VERIFY_EXIT (flags != -1); > + TEST_VERIFY (flags & FD_CLOEXEC); > + xclose (newfd); > + > + xclose (sockfd[0]); > + xclose (sockfd[1]); > + return 0; > +} > + > +#include