From patchwork Mon Apr 17 22:14:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 1769893 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=geVWP0jn; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Q0hDd5RGqz1yZk for ; Tue, 18 Apr 2023 08:14:41 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2A53B3857709 for ; Mon, 17 Apr 2023 22:14:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2A53B3857709 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1681769676; bh=pVug52YSEc8Dvp3VIp+6mptJy7aWOVFRm6hKPrzbrzw=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=geVWP0jn7adQ3LwNl9juyrNFe2zVq+lSBaw/ggLpylCoKz6JAR5dhwGJIcNG7loAu 0Ua5c+usuCdDW+4hVP/QX+nClj447EDhtXy/nndYLKd+46bAgPSY2mYIEpxhfIvVG6 vE2c9vajOfGTUGUPdQGYmrnse1G573yPQIxhQHh8= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lj1-x234.google.com (mail-lj1-x234.google.com [IPv6:2a00:1450:4864:20::234]) by sourceware.org (Postfix) with ESMTPS id D3CD23858C50 for ; Mon, 17 Apr 2023 22:14:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D3CD23858C50 Received: by mail-lj1-x234.google.com with SMTP id 38308e7fff4ca-2a8aea2a654so16900921fa.1 for ; Mon, 17 Apr 2023 15:14:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681769658; x=1684361658; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=pVug52YSEc8Dvp3VIp+6mptJy7aWOVFRm6hKPrzbrzw=; b=dlmUxEZABPKnVVqO4YlsyKJh/PL17LqPXWS94FtuivVlP2CNqoBHkzDoIhKh+NgTwj X4Ey52lGb8+uGx/II4PgHdOmVv76tyZqcES5aLa4sMBFNNeJOR5p54aJ6Rnx+dFgxHep wIDZB/j24uynprZNW4q+timYII5a8it5v5u8WcPcr6ZvIdlLYTVoXCYsCYyCWzTbo4FR ZCRcc5rXSMFDs1+g5d9DZyR1XSWjHCPIEzt92tvC7bhGMVS+4SL07RDsGrFvuEFxCPXY VJ7MUa9MOVyUhcn2ogcc3jclLJD3xwnRnOP2wnsSlML1bXpLV5eui4xY7jHNqZIHqWKB 3Tcw== X-Gm-Message-State: AAQBX9daKP3wo3QrWeDGC1THW1OJTPJNieP8QmZ+qX/4aBl+80C561bX C/3po+yh++gtDi/kuQUJNSikqXE4NRP38g== X-Google-Smtp-Source: AKy350a7Uh2MSjixUR57B3RB3bTnNCil0vaBh6bCNsqLRXAvvfLD3jiJEf8FpPkBbqYrPOlb5M4mtA== X-Received: by 2002:ac2:5390:0:b0:4e8:3d24:de6f with SMTP id g16-20020ac25390000000b004e83d24de6fmr2402519lfh.14.1681769657817; Mon, 17 Apr 2023 15:14:17 -0700 (PDT) Received: from surface-pro-6.. ([2a00:1370:818c:4a57:d907:80f6:db70:646a]) by smtp.gmail.com with ESMTPSA id 13-20020ac2482d000000b004e8452972a2sm2156266lft.247.2023.04.17.15.14.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Apr 2023 15:14:17 -0700 (PDT) To: libc-alpha@sourceware.org, bug-hurd@gnu.org Cc: Samuel Thibault , Emilio Pozuelo Monfort , Sergey Bugaev Subject: [RFC PATCH v2 4/4] socket: Add a test for MSG_CMSG_CLOEXEC Date: Tue, 18 Apr 2023 01:14:14 +0300 Message-Id: <20230417221414.1999478-1-bugaevc@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230417133902.99040-4-bugaevc@gmail.com> References: <20230417133902.99040-4-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, 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 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-Patchwork-Original-From: Sergey Bugaev via Libc-alpha From: Sergey Bugaev Reply-To: Sergey Bugaev Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Signed-off-by: Sergey Bugaev --- And now it passes on x86_64-linux as well. 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..9351a052 --- /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"; + 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_SPACE (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