(root)/
strace-6.5/
tests/
unix-pair-sendto-recvfrom.c
       1  /*
       2   * Check decoding and dumping of sendto and recvfrom syscalls.
       3   *
       4   * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@strace.io>
       5   * Copyright (c) 2016-2021 The strace developers.
       6   * All rights reserved.
       7   *
       8   * SPDX-License-Identifier: GPL-2.0-or-later
       9   */
      10  
      11  #include "tests.h"
      12  #include <assert.h>
      13  #include <string.h>
      14  #include <unistd.h>
      15  #include <sys/socket.h>
      16  #include <sys/wait.h>
      17  
      18  static void
      19  transpose(char *str, int len)
      20  {
      21  	for (int i = 0; i < len / 2; ++i) {
      22  		char c = str[i];
      23  		str[i] = str[len - 1 - i];
      24  		str[len - 1 - i] = c;
      25  	}
      26  }
      27  
      28  int
      29  main(int ac, char **av)
      30  {
      31  	assert(ac == 2);
      32  	const int len = strlen(av[1]);
      33  	assert(len);
      34  
      35  	(void) close(0);
      36  	(void) close(1);
      37  
      38  	int sv[2];
      39  	if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
      40  		perror_msg_and_skip("socketpair");
      41  
      42  	pid_t pid = fork();
      43  	if (pid < 0)
      44  		perror_msg_and_fail("fork");
      45  
      46  	if (pid) {
      47  		assert(close(1) == 0);
      48  		transpose(av[1], len);
      49  		assert(sendto(0, av[1], len, MSG_DONTROUTE, NULL, 0) == len);
      50  		assert(recvfrom(0, av[1], len, MSG_WAITALL, NULL, NULL) == len);
      51  		assert(close(0) == 0);
      52  
      53  		int status;
      54  		assert(waitpid(pid, &status, 0) == pid);
      55  		assert(status == 0);
      56  	} else {
      57  		assert(close(0) == 0);
      58  		assert(recvfrom(1, av[1], len, MSG_WAITALL, NULL, NULL) == len);
      59  		transpose(av[1], len);
      60  		assert(sendto(1, av[1], len, MSG_DONTROUTE, NULL, 0) == len);
      61  		assert(recvfrom(1, av[1], len, MSG_WAITALL, NULL, NULL) == 0);
      62  		assert(close(1) == 0);
      63  	}
      64  
      65  	return 0;
      66  }