1 #include <assert.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <sys/socket.h>
5 #include <sys/wait.h>
6
7 static void
transpose(char * str,int len)8 transpose(char *str, int len)
9 {
10 int i;
11
12 for (i = 0; i < len / 2; ++i) {
13 char c = str[i];
14 str[i] = str[len - 1 - i];
15 str[len - 1 - i] = c;
16 }
17 }
18
19 int
main(int ac,char ** av)20 main(int ac, char **av)
21 {
22 assert(ac == 2);
23 const int len = strlen(av[1]);
24 assert(len);
25
26 (void) close(0);
27 (void) close(1);
28
29 int sv[2];
30 assert(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
31 assert(sv[0] == 0);
32 assert(sv[1] == 1);
33
34 pid_t pid = fork();
35 assert(pid >= 0);
36
37 if (pid) {
38 assert(close(1) == 0);
39 transpose(av[1], len);
40 assert(sendto(0, av[1], len, MSG_DONTROUTE, NULL, 0) == len);
41 assert(recvfrom(0, av[1], len, MSG_WAITALL, NULL, NULL) == len);
42 assert(close(0) == 0);
43
44 int status;
45 assert(waitpid(pid, &status, 0) == pid);
46 assert(status == 0);
47 } else {
48 assert(close(0) == 0);
49 assert(recvfrom(1, av[1], len, MSG_WAITALL, NULL, NULL) == len);
50 transpose(av[1], len);
51 assert(sendto(1, av[1], len, MSG_DONTROUTE, NULL, 0) == len);
52 assert(recvfrom(1, av[1], len, MSG_WAITALL, NULL, NULL) == 0);
53 assert(close(1) == 0);
54 }
55
56 return 0;
57 }
58