1132438Ssilby/*
2132438SsilbyCopyright (C) 2004 Michael J. Silbersack. All rights reserved.
3132438Ssilby
4132438SsilbyRedistribution and use in source and binary forms, with or without
5132438Ssilbymodification, are permitted provided that the following conditions
6132438Ssilbyare met:
7132438Ssilby1. Redistributions of source code must retain the above copyright
8132438Ssilby   notice, this list of conditions and the following disclaimer.
9132438Ssilby2. Redistributions in binary form must reproduce the above copyright
10132438Ssilby   notice, this list of conditions and the following disclaimer in the
11132438Ssilby   documentation and/or other materials provided with the distribution.
12132438Ssilby
13132438SsilbyTHIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14132438SsilbyANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15132438SsilbyIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16132438SsilbyARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17132438SsilbyFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18132438SsilbyDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19132438SsilbyOR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20132438SsilbyHOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21132438SsilbyLIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22132438SsilbyOUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23132438SsilbySUCH DAMAGE.
24132438Ssilby*/
25132438Ssilby
26132438Ssilby#include <stdio.h>
27132438Ssilby#include <unistd.h>
28132438Ssilby#include <sys/stat.h>
29132438Ssilby
30132438Ssilby/*
31132438Ssilby * $FreeBSD$
32132438Ssilby * The goal of this program is to see if fstat reports the correct
33132438Ssilby * data count for a pipe.  Prior to revision 1.172 of sys_pipe.c,
34132438Ssilby * 0 would be returned once the pipe entered direct write mode.
35132438Ssilby *
36132438Ssilby * Linux (2.6) always returns zero, so it's not a valuable platform
37132438Ssilby * for comparison.
38132438Ssilby */
39132438Ssilby
40132438Ssilbyint main (void)
41132438Ssilby{
42132438Ssilbychar buffer[32768], buffer2[32768];
43132438Ssilbyint desc[2];
44231832Seadlerint error, successes = 0;
45132438Ssilbystruct stat status;
46132438Ssilbypid_t new_pid;
47132438Ssilby
48132438Ssilbyerror = pipe(desc);
49132438Ssilby
50132438Ssilbyif (error)
51132438Ssilby	err(0, "Couldn't allocate fds\n");
52132438Ssilby
53132438Ssilbynew_pid = fork();
54132438Ssilby
55132438Ssilbyif (new_pid == 0) {
56132438Ssilby	write(desc[1], &buffer, 145);
57132438Ssilby	usleep(1000000);
58132438Ssilby	write(desc[1], &buffer, 2048);
59132438Ssilby	usleep(1000000);
60132438Ssilby	write(desc[1], &buffer, 4096);
61132438Ssilby	usleep(1000000);
62132438Ssilby	write(desc[1], &buffer, 8191);
63132438Ssilby	usleep(1000000);
64132438Ssilby	write(desc[1], &buffer, 8192);
65132438Ssilby	usleep(1000000);
66132438Ssilby} else {
67132438Ssilby	while (successes < 5) {
68132438Ssilby		usleep(3000);
69132438Ssilby		fstat(desc[0], &status);
70132438Ssilby		error = read(desc[0], &buffer2, 32768);
71132438Ssilby		if (status.st_size != error)
72132438Ssilby			err(0, "FAILURE: stat size %d read size %d\n", (int)status.st_size, error);
73132438Ssilby		if (error > 0) {
74132438Ssilby			printf("SUCCESS at stat size %d read size %d\n", (int)status.st_size, error);
75132438Ssilby			successes++;
76132438Ssilby			/* Sleep to avoid the natural race in reading st_size. */
77132438Ssilby			usleep(1000000);
78132438Ssilby		}
79132438Ssilby	}
80132438Ssilby}
81132438Ssilby
82132438Ssilby}
83