From patchwork Mon Apr 17 13:39:02 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sergey Bugaev X-Patchwork-Id: 1769645 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=2620:52:3:1:0:246e:9693:128c; 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=Z+9YMYeU; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4Q0SpJ67HRz1yZr for ; Mon, 17 Apr 2023 23:39:36 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D3923385B514 for ; Mon, 17 Apr 2023 13:39:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D3923385B514 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1681738774; bh=9mZ53YntpJdzEoL0u7Kca3+NKxpCegyLbzlusnZ4EqA=; 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=Z+9YMYeU1RiWhWb/XuhaxVgIflX6s6Qk7BN7HHyGjFDG/y1iVAyvoHHtxMLepY4vj lv3NIx8QU0Re21kOU3K2Q4UtyWoMXyTaZZnWWaye4MWpYIM7eHehnTEuSU0br9ZVBS 94KXzBSQUNYxa0LKzGqagJcyHczUN27dWxwv9rfQ= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-lj1-x236.google.com (mail-lj1-x236.google.com [IPv6:2a00:1450:4864:20::236]) by sourceware.org (Postfix) with ESMTPS id 2809C3858C2C for ; Mon, 17 Apr 2023 13:39:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2809C3858C2C Received: by mail-lj1-x236.google.com with SMTP id e8so8308794ljn.2 for ; Mon, 17 Apr 2023 06:39:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681738750; x=1684330750; 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=9mZ53YntpJdzEoL0u7Kca3+NKxpCegyLbzlusnZ4EqA=; b=FC9iMhAEwPQ8eWKcYNMk1MN4wxAyWUY0vhXcmwdmVki888Nw3OKG6kqjW+qoKOv4XF DQXxVFK3hfahy/BcvpGq8F3Wr/OZO+CZHQJFlKsuqZPx/rlueartkIZ7f6+hqWMw1xSu 9QeHzc66Kc1cFquK0RZX22R4gL33zTOW/YsR8bHOl5uqNTj74TBlYf14685C8KHXXc34 lYDVbRnLJ7oVidVJejP25Hgq4jirvh8AnbMGCN30OpGPhwPtShAiNbxjXItaAFuUIELa IUNh2+EEbw+PMoS7ilo30kVrb+sUOyFqp/F5BKnFDh6yBtLsmuW2J67N4WLwSJK/r1S+ pvmA== X-Gm-Message-State: AAQBX9dq5s5hKT6fg4/4kqR+83zg3OAU/xOLe/RsJAOIVUpoDUzoxlDV bzPD3O4j/h4S9Nd/WHKmG7vE77dJXes= X-Google-Smtp-Source: AKy350aRZlnS16W1CGIWdq17IMSD6xhf/11T4mae0Ga2tQ8HLDOH5A/l2rp9EQ85IuKoxw5OzvsaaQ== X-Received: by 2002:a2e:9793:0:b0:2a8:b8c3:37f7 with SMTP id y19-20020a2e9793000000b002a8b8c337f7mr1811414lji.23.1681738750353; Mon, 17 Apr 2023 06:39:10 -0700 (PDT) Received: from surface-pro-6.. ([194.190.106.50]) by smtp.gmail.com with ESMTPSA id u27-20020ac24c3b000000b004db3e445f1fsm2067105lfq.97.2023.04.17.06.39.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Apr 2023 06:39:09 -0700 (PDT) To: libc-alpha@sourceware.org, bug-hurd@gnu.org Cc: Samuel Thibault , Emilio Pozuelo Monfort , Sergey Bugaev Subject: [RFC PATCH 4/4] socket: Add a test for MSG_CMSG_CLOEXEC Date: Mon, 17 Apr 2023 16:39:02 +0300 Message-Id: <20230417133902.99040-4-bugaevc@gmail.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230417133902.99040-1-bugaevc@gmail.com> References: <20230417133902.99040-1-bugaevc@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 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 Signed-off-by: Sergey Bugaev Reviewed-by: Adhemerval Zanella --- 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"; + 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