xref: /freebsd/tests/sys/kern/pipe/pipe_reverse_test.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*
2 Copyright (C) 2004 Michael J. Silbersack. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 1. Redistributions of source code must retain the above copyright
8    notice, this list of conditions and the following disclaimer.
9 2. Redistributions in binary form must reproduce the above copyright
10    notice, this list of conditions and the following disclaimer in the
11    documentation and/or other materials provided with the distribution.
12 
13 THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 SUCH DAMAGE.
24 */
25 
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/wait.h>
29 #include <assert.h>
30 #include <err.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 
37 /*
38  * This program simply tests writing through the reverse direction of
39  * a pipe.  Nothing too fancy, it's only needed because most pipe-using
40  * programs never touch the reverse direction (it doesn't exist on
41  * Linux.)
42  */
43 
44 int
45 main(void)
46 {
47 	char buffer[65535], buffer2[65535], go[] = "go", go2[] = "go2";
48 	int desc[2], ipc_coord[2];
49 	size_t i;
50 	ssize_t total;
51 	int buggy, error;
52 	pid_t new_pid;
53 
54 	buggy = 0;
55 	total = 0;
56 
57 	error = pipe(desc);
58 	if (error == -1)
59 		err(1, "Couldn't allocate data pipe");
60 
61 	error = pipe(ipc_coord);
62 	if (error == -1)
63 		err(1, "Couldn't allocate IPC coordination pipe");
64 
65 	buffer[0] = 'A';
66 
67 	for (i = 1; i < (int)sizeof(buffer); i++) {
68 		buffer[i] = buffer[i - 1] + 1;
69 		if (buffer[i] > 'Z')
70 			buffer[i] = 'A';
71 	}
72 
73 	new_pid = fork();
74 	assert(new_pid != -1);
75 
76 #define	SYNC_R(i, _buf) do {	\
77 	int _error = errno; \
78 	warnx("%d: waiting for synchronization", __LINE__); \
79 	if (read(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \
80 		err(1, "failed to synchronize (%s)", (i == 0 ? "parent" : "child")); \
81 	errno = _error; \
82 	} while(0)
83 
84 #define	SYNC_W(i, _buf) do {	\
85 	int _error = errno; \
86 	warnx("%d: sending synchronization", __LINE__); \
87 	if (write(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \
88 		err(1, "failed to synchronize (%s)", (i == 0 ? "child" : "parent")); \
89 	errno = _error; \
90 	} while(0)
91 
92 #define	WRITE(s) do { 							\
93 	ssize_t _size; 							\
94 	if ((_size = write(desc[1], &buffer[total], s)) != s)		\
95 		warn("short write; wrote %zd, expected %d", _size, s);	\
96 	total += _size;							\
97 	} while(0)
98 
99 	if (new_pid == 0) {
100 		SYNC_R(0, go);
101 		for (i = 0; i < 8; i++)
102 			WRITE(4096);
103 
104 		SYNC_W(0, go2);
105 		SYNC_R(0, go);
106 
107 		for (i = 0; i < 2; i++)
108 			WRITE(4096);
109 
110 		SYNC_W(0, go2);
111 
112 		_exit(0);
113 	}
114 
115 	SYNC_W(1, go);
116 	SYNC_R(1, go2);
117 
118 	error = read(desc[0], &buffer2, 8 * 4096);
119 	total += error;
120 	printf("Read %d bytes\n", error);
121 
122 	SYNC_W(1, go);
123 	SYNC_R(1, go2);
124 
125 	error = read(desc[0], &buffer2[total], 2 * 4096);
126 	total += error;
127 	printf("Read %d bytes, done\n", error);
128 
129 	if (memcmp(buffer, buffer2, total) != 0) {
130 		for (i = 0; i < (size_t)total; i++) {
131 			if (buffer[i] != buffer2[i]) {
132 				buggy = 1;
133 				printf("Location %zu input: %hhx "
134 				    "output: %hhx\n",
135 				    i, buffer[i], buffer2[i]);
136 			}
137 		}
138 	}
139 
140 	waitpid(new_pid, NULL, 0);
141 
142 	if ((buggy == 1) || (total != 10 * 4096))
143 		errx(1, "FAILED");
144 	else
145 		printf("SUCCESS\n");
146 
147 	exit(0);
148 }
149