public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
To: Sergey Bugaev <bugaevc@gmail.com>
Cc: libc-alpha@sourceware.org, bug-hurd@gnu.org,
	Samuel Thibault <samuel.thibault@gnu.org>,
	Emilio Pozuelo Monfort <pochu27@gmail.com>
Subject: Re: [RFC PATCH 4/4] socket: Add a test for MSG_CMSG_CLOEXEC
Date: Tue, 18 Apr 2023 13:50:48 -0300	[thread overview]
Message-ID: <917ddab7-53fa-aac6-038b-a6c7da3972b2@linaro.org> (raw)
In-Reply-To: <20230418153753.114974-1-bugaevc@gmail.com>



On 18/04/23 12:37, Sergey Bugaev wrote:
> Hello,
> 
> On Tue, Apr 18, 2023 at 3:13 PM Adhemerval Zanella Netto wrote:
>> On 17/04/23 10:39, Sergey Bugaev via Libc-alpha wrote:
>>> Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
>>
>> Add some description of what the test is accomplishing here.
> 
> Do you mean in the commit message? Done.
> 
> Also changed:
> * in send_fd, memset the cmsg_storage to zero before filling it
> * in send_fd, set msg_controllen to sizeof (cmsg_storage) (aka
>   CMSG_SPACE (sizeof (fd))), not CMSG_LEN (sizeof (fd))
> * dropped the /* const */
> 
> And here's the test passing on x86_64-linux:
> 
> socketpair(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0, [3, 4]) = 0
> sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="hello\0", iov_len=6}], msg_iovlen=1, msg_control=[{cmsg_len=20, cmsg_level=SOL_SOCKET, cmsg_type=SCM_RIGHTS, cmsg_data=[0]}], msg_controllen=24, msg_flags=0}, 0) = 6
> recvmsg(3, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="hello\0", iov_len=100}], msg_iovlen=1, msg_control=[{cmsg_len=20, cmsg_level=SOL_SOCKET, cmsg_type=SCM_RIGHTS, cmsg_data=[5]}], msg_controllen=24, msg_flags=0}, 0) = 6
> fcntl(5, F_GETFD)                       = 0
> close(5)                                = 0
> sendmsg(4, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="hello\0", iov_len=6}], msg_iovlen=1, msg_control=[{cmsg_len=20, cmsg_level=SOL_SOCKET, cmsg_type=SCM_RIGHTS, cmsg_data=[0]}], msg_controllen=24, msg_flags=0}, 0) = 6
> recvmsg(3, {msg_name=NULL, msg_namelen=0, msg_iov=[{iov_base="hello\0", iov_len=100}], msg_iovlen=1, msg_control=[{cmsg_len=20, cmsg_level=SOL_SOCKET, cmsg_type=SCM_RIGHTS, cmsg_data=[5]}], msg_controllen=24, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 6
> fcntl(5, F_GETFD)                       = 0x1 (flags FD_CLOEXEC)
> close(5)                                = 0
> close(3)                                = 0
> close(4)                                = 0
> 
> Sergey
> 
> ---- >8 ----
> 
> From 256a774a16e1bb3d721e2be82dc4bbab119bf6ea Mon Sep 17 00:00:00 2001
> From: Sergey Bugaev <bugaevc@gmail.com>
> Date: Mon, 17 Apr 2023 16:28:35 +0300
> Subject: [RFC PATCH v3 4/4] socket: Add a test for MSG_CMSG_CLOEXEC
> 
> This checks that:
> * We can send and receive fds over Unix domain sockets using SCM_RIGHTS;
> * msg_controllen, cmsg_level, cmsg_type, cmsg_len are all filled in
>   correctly on receive;
> * Most importantly, the received fd has or has not the close-on-exec
>   flag set depending on whether we pass MSG_CMSG_CLOEXEC to recvmsg ().
> 
> Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
>  socket/Makefile           |   1 +
>  socket/tst-cmsg_cloexec.c | 126 ++++++++++++++++++++++++++++++++++++++
>  2 files changed, 127 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..a18f4912
> --- /dev/null
> +++ b/socket/tst-cmsg_cloexec.c
> @@ -0,0 +1,126 @@
> +/* 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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <support/xunistd.h>
> +#include <support/check.h>
> +#include <sys/socket.h>
> +#include <sys/un.h>
> +#include <string.h>
> +#include <fcntl.h>
> +
> +static void
> +send_fd (int sockfd, int fd)
> +{
> +  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 = sizeof (cmsg_storage)
> +    };
> +
> +  memset (&cmsg_storage, 0, sizeof (cmsg_storage));
> +
> +  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 <support/test-driver.c>

  reply	other threads:[~2023-04-18 16:50 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-17 13:38 [PATCH 1/4] hurd: Don't pass fd flags in CMSG_DATA Sergey Bugaev
2023-04-17 13:39 ` [PATCH 2/4] hurd: Implement MSG_CMSG_CLOEXEC Sergey Bugaev
2023-04-17 13:39 ` [PATCH 3/4] hurd: Only deallocate addrport when it's valid Sergey Bugaev
2023-04-17 13:39 ` [RFC PATCH 4/4] socket: Add a test for MSG_CMSG_CLOEXEC Sergey Bugaev
2023-04-17 22:14   ` [RFC PATCH v2 " Sergey Bugaev
2023-04-18 12:13   ` [RFC PATCH " Adhemerval Zanella Netto
2023-04-18 15:37     ` Sergey Bugaev
2023-04-18 16:50       ` Adhemerval Zanella Netto [this message]
2023-04-20 21:14 ` [PATCH 1/4] hurd: Don't pass fd flags in CMSG_DATA Samuel Thibault
2023-04-20 21:47   ` Sergey Bugaev
2023-04-21  0:56     ` Samuel Thibault

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=917ddab7-53fa-aac6-038b-a6c7da3972b2@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=bug-hurd@gnu.org \
    --cc=bugaevc@gmail.com \
    --cc=libc-alpha@sourceware.org \
    --cc=pochu27@gmail.com \
    --cc=samuel.thibault@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).