(root)/
strace-6.5/
tests/
unix-pair-send-recv.c
       1  /*
       2   * Copyright (c) 2015-2018 Dmitry V. Levin <ldv@strace.io>
       3   * Copyright (c) 2015-2021 The strace developers.
       4   * All rights reserved.
       5   *
       6   * SPDX-License-Identifier: GPL-2.0-or-later
       7   */
       8  
       9  #include "tests.h"
      10  
      11  #include <assert.h>
      12  #include <errno.h>
      13  #include <string.h>
      14  #include <unistd.h>
      15  #include <sys/socket.h>
      16  #include "scno.h"
      17  
      18  #ifndef __NR_send
      19  # define __NR_send -1
      20  #endif
      21  #define SC_send 9
      22  
      23  #ifndef __NR_recv
      24  # define __NR_recv -1
      25  #endif
      26  #define SC_recv 10
      27  
      28  static int
      29  sys_send(int sockfd, const void *buf, size_t len, int flags)
      30  {
      31  	int rc = socketcall(__NR_send, SC_send,
      32  			    sockfd, (long) buf, len, flags, 0);
      33  	if (rc < 0 && ENOSYS == errno)
      34  		perror_msg_and_skip("send");
      35  	return rc;
      36  }
      37  
      38  static int
      39  sys_recv(int sockfd, const void *buf, size_t len, int flags)
      40  {
      41  	int rc = socketcall(__NR_recv, SC_recv,
      42  			    sockfd, (long) buf, len, flags, 0);
      43  	if (rc < 0 && ENOSYS == errno)
      44  		perror_msg_and_skip("recv");
      45  	return rc;
      46  }
      47  
      48  static void
      49  transpose(char *str, const size_t len)
      50  {
      51  	for (size_t i = 0; i < len / 2; ++i) {
      52  		char c = str[i];
      53  		str[i] = str[len - 1 - i];
      54  		str[len - 1 - i] = c;
      55  	}
      56  }
      57  
      58  int
      59  main(int ac, char **av)
      60  {
      61  	assert(ac == 2);
      62  	const size_t len = strlen(av[1]);
      63  	assert(len);
      64  	char *const buf0 = tail_alloc(len);
      65  	char *const buf1 = tail_alloc(len);
      66  	memcpy(buf0, av[1], len);
      67  
      68  	(void) close(0);
      69  	(void) close(1);
      70  
      71  	int sv[2];
      72  	if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
      73  		perror_msg_and_skip("socketpair");
      74  
      75  	assert(sys_send(0, buf0, len, MSG_DONTROUTE) == (int) len);
      76  	assert(sys_recv(1, buf1, len, MSG_WAITALL) == (int) len);
      77  
      78  	transpose(buf1, len);
      79  	assert(sys_send(1, buf1, len, MSG_DONTROUTE) == (int) len);
      80  	if (close(1))
      81  		perror_msg_and_fail("close(1)");
      82  
      83  	assert(sys_recv(0, buf0, len, MSG_WAITALL) == (int) len);
      84  	if (close(0))
      85  		perror_msg_and_fail("close(0)");
      86  	assert(sys_recv(0, NULL, len, MSG_DONTWAIT) == -1);
      87  
      88  	return 0;
      89  }