xref: /freebsd/tests/sys/kern/pipe/pipe_fstat_bug_test.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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 <sys/event.h>
30 #include <assert.h>
31 #include <err.h>
32 #include <errno.h>
33 #include <inttypes.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 
38 /*
39  * $FreeBSD$
40  * The goal of this program is to see if fstat reports the correct
41  * data count for a pipe.  Prior to revision 1.172 of sys_pipe.c,
42  * 0 would be returned once the pipe entered direct write mode.
43  *
44  * Linux (2.6) always returns zero, so it's not a valuable platform
45  * for comparison.
46  */
47 
48 int
49 main(void)
50 {
51 	char buffer[32768], buffer2[32768], go[] = "go", go2[] = "go2";
52 	int desc[2], ipc_coord[2];
53 	struct kevent event, ke;
54 	ssize_t error;
55 	int successes = 0;
56 	struct stat status;
57 	pid_t new_pid;
58 	int kq;
59 
60 	error = pipe(desc);
61 	if (error == -1)
62 		err(1, "Couldn't allocate data pipe");
63 
64 	error = pipe(ipc_coord);
65 	if (error == -1)
66 		err(1, "Couldn't allocate IPC coordination pipe");
67 
68 	new_pid = fork();
69 	assert(new_pid != -1);
70 
71 	close(new_pid == 0 ? desc[0] : desc[1]);
72 
73 #define	SYNC_R(i, _buf) do {	\
74 	int _error = errno; \
75 	warnx("%d: waiting for synchronization", __LINE__); \
76 	if (read(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \
77 		err(1, "failed to synchronize (%s)", (i == 0 ? "parent" : "child")); \
78 	errno = _error; \
79 	} while(0)
80 
81 #define	SYNC_W(i, _buf) do {	\
82 	int _error = errno; \
83 	warnx("%d: sending synchronization", __LINE__); \
84 	if (write(ipc_coord[i], &_buf, sizeof(_buf)) != sizeof(_buf)) \
85 		err(1, "failed to synchronize (%s)", (i == 0 ? "child" : "parent")); \
86 	errno = _error; \
87 	} while(0)
88 
89 #define	WRITE(s) do { 							\
90 	ssize_t _size; 							\
91 	if ((_size = write(desc[1], &buffer, s)) != s)			\
92 		warn("short write; wrote %zd, expected %d", _size, s);	\
93 	} while(0)
94 
95 	if (new_pid == 0) {
96 
97 		SYNC_R(0, go);
98 		WRITE(145);
99 		SYNC_W(0, go2);
100 
101 		SYNC_R(0, go);
102 		WRITE(2048);
103 		SYNC_W(0, go2);
104 
105 		SYNC_R(0, go);
106 		WRITE(4096);
107 		SYNC_W(0, go2);
108 
109 		SYNC_R(0, go);
110 		WRITE(8191);
111 		SYNC_W(0, go2);
112 
113 		SYNC_R(0, go);
114 		SYNC_W(0, go2); /* XXX: why is this required? */
115 		WRITE(8192);
116 		SYNC_W(0, go2);
117 
118 		close(ipc_coord[0]);
119 		close(ipc_coord[1]);
120 
121 		_exit(0);
122 	}
123 
124 	kq = kqueue();
125 	if (kq == -1)
126 		_exit(1);
127 
128 	EV_SET(&ke, desc[0], EVFILT_READ, EV_ADD, 0, 0, NULL);
129 
130 	/* Attach event to the kqueue. */
131 	if (kevent(kq, &ke, 1, NULL, 0, NULL) != 0)
132 		_exit(2);
133 
134 	while (successes < 5) {
135 		SYNC_W(1, go);
136 		SYNC_R(1, go2);
137 
138 		/* Ensure data is available to read */
139 		if (kevent(kq, NULL, 0, &event, 1, NULL) != 1)
140 			_exit(3);
141 
142 		fstat(desc[0], &status);
143 		error = read(desc[0], &buffer2, sizeof(buffer2));
144 
145 		if (status.st_size != error)
146 			err(1, "FAILURE: stat size %jd read size %zd",
147 			    (intmax_t)status.st_size, error);
148 		if (error > 0) {
149 			printf("SUCCESS at stat size %jd read size %zd\n",
150 			    (intmax_t)status.st_size, error);
151 			successes++;
152 		}
153 	}
154 
155 	exit(0);
156 }
157