]> granicus.if.org Git - strace/blob - tests/unix-pair-sendto-recvfrom.c
f641005061816c8b889071f609f82c4b718e993a
[strace] / 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@altlinux.org>
5  * Copyright (c) 2016-2017 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         int i;
22
23         for (i = 0; i < len / 2; ++i) {
24                 char c = str[i];
25                 str[i] = str[len - 1 - i];
26                 str[len - 1 - i] = c;
27         }
28 }
29
30 int
31 main(int ac, char **av)
32 {
33         assert(ac == 2);
34         const int len = strlen(av[1]);
35         assert(len);
36
37         (void) close(0);
38         (void) close(1);
39
40         int sv[2];
41         if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
42                 perror_msg_and_skip("socketpair");
43
44         pid_t pid = fork();
45         if (pid < 0)
46                 perror_msg_and_fail("fork");
47
48         if (pid) {
49                 assert(close(1) == 0);
50                 transpose(av[1], len);
51                 assert(sendto(0, av[1], len, MSG_DONTROUTE, NULL, 0) == len);
52                 assert(recvfrom(0, av[1], len, MSG_WAITALL, NULL, NULL) == len);
53                 assert(close(0) == 0);
54
55                 int status;
56                 assert(waitpid(pid, &status, 0) == pid);
57                 assert(status == 0);
58         } else {
59                 assert(close(0) == 0);
60                 assert(recvfrom(1, av[1], len, MSG_WAITALL, NULL, NULL) == len);
61                 transpose(av[1], len);
62                 assert(sendto(1, av[1], len, MSG_DONTROUTE, NULL, 0) == len);
63                 assert(recvfrom(1, av[1], len, MSG_WAITALL, NULL, NULL) == 0);
64                 assert(close(1) == 0);
65         }
66
67         return 0;
68 }