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  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. The name of the author may not be used to endorse or promote products
17  *    derived from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "tests.h"
32 #include <assert.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <sys/socket.h>
36 #include <sys/wait.h>
37 
38 static void
transpose(char * str,int len)39 transpose(char *str, int len)
40 {
41 	int i;
42 
43 	for (i = 0; i < len / 2; ++i) {
44 		char c = str[i];
45 		str[i] = str[len - 1 - i];
46 		str[len - 1 - i] = c;
47 	}
48 }
49 
50 int
main(int ac,char ** av)51 main(int ac, char **av)
52 {
53 	assert(ac == 2);
54 	const int len = strlen(av[1]);
55 	assert(len);
56 
57 	(void) close(0);
58 	(void) close(1);
59 
60 	int sv[2];
61 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv))
62 		perror_msg_and_skip("socketpair");
63 
64 	pid_t pid = fork();
65 	if (pid < 0)
66 		perror_msg_and_fail("fork");
67 
68 	if (pid) {
69 		assert(close(1) == 0);
70 		transpose(av[1], len);
71 		assert(sendto(0, av[1], len, MSG_DONTROUTE, NULL, 0) == len);
72 		assert(recvfrom(0, av[1], len, MSG_WAITALL, NULL, NULL) == len);
73 		assert(close(0) == 0);
74 
75 		int status;
76 		assert(waitpid(pid, &status, 0) == pid);
77 		assert(status == 0);
78 	} else {
79 		assert(close(0) == 0);
80 		assert(recvfrom(1, av[1], len, MSG_WAITALL, NULL, NULL) == len);
81 		transpose(av[1], len);
82 		assert(sendto(1, av[1], len, MSG_DONTROUTE, NULL, 0) == len);
83 		assert(recvfrom(1, av[1], len, MSG_WAITALL, NULL, NULL) == 0);
84 		assert(close(1) == 0);
85 	}
86 
87 	return 0;
88 }
89