1865695bbc89088b9526ea9045410e5afb70a985cplars/*
2f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz *
4f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * This program is free software; you can redistribute it and/or modify it
5f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * under the terms of version 2 of the GNU General Public License as
6f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * published by the Free Software Foundation.
7f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz *
8f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * This program is distributed in the hope that it would be useful, but
9f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * WITHOUT ANY WARRANTY; without even the implied warranty of
10f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz *
12f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * Further, this software is distributed without any warranty that it is
13f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * free of the rightful claim of any third person regarding infringement
14f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * or the like.  Any license provided herein, whether implied or
15f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * otherwise, applies only to this software file.  Patent licenses, if
16f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * any, provided herein do not apply to combinations of this program with
17f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * other software, or any other product whatsoever.
18f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz *
19f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * You should have received a copy of the GNU General Public License along
20fed9641096e27f79a0f2d9adfe9839dd8d11dc0fWanlong Gao * with this program; if not, write the Free Software Foundation, Inc.,
21fed9641096e27f79a0f2d9adfe9839dd8d11dc0fWanlong Gao * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz *
23f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * Mountain View, CA  94043, or:
25f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz *
26f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * http://www.sgi.com
27f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz *
28f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * For further information regarding this notice, see:
29f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz *
30f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31f471e11e4a5f6918672c3c737c92a3ced340eb6anstraz *
32865695bbc89088b9526ea9045410e5afb70a985cplars */
3325e21873b269e2191ca4b5f4480357261d4b1547subrata_modak/* $Header: /cvsroot/ltp/ltp/testcases/kernel/ipc/pipeio/pipeio.c,v 1.18 2009/03/19 07:10:02 subrata_modak Exp $ */
34865695bbc89088b9526ea9045410e5afb70a985cplars/*
35865695bbc89088b9526ea9045410e5afb70a985cplars *  This tool can be used to beat on system or named pipes.
36865695bbc89088b9526ea9045410e5afb70a985cplars *  See the help() function below for user information.
37865695bbc89088b9526ea9045410e5afb70a985cplars */
38865695bbc89088b9526ea9045410e5afb70a985cplars#include <stdio.h>
39865695bbc89088b9526ea9045410e5afb70a985cplars#include <fcntl.h>
40865695bbc89088b9526ea9045410e5afb70a985cplars#include <stdlib.h>
41865695bbc89088b9526ea9045410e5afb70a985cplars#include <unistd.h>
42865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/types.h>
43865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/param.h>
44865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/wait.h>
45045fd16a63837924a0f20d2419fbd497f8e36764robbiew#include <time.h>
46865695bbc89088b9526ea9045410e5afb70a985cplars#include <errno.h>
47865695bbc89088b9526ea9045410e5afb70a985cplars#include <string.h>
48865695bbc89088b9526ea9045410e5afb70a985cplars#include <signal.h>
49865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/stat.h>
50ea37be8bf7db7f9709cf36fce28521071a4dae40subrata_modak#include <sys/sem.h>
51865695bbc89088b9526ea9045410e5afb70a985cplars
52865695bbc89088b9526ea9045410e5afb70a985cplars#include "tlibio.h"
53865695bbc89088b9526ea9045410e5afb70a985cplars
5492b688b8340e2424643c5d33dcacfbb109529402robbiew#include "test.h"
55e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang#include "safe_macros.h"
56b681672752634a70ba1bd6b1c6b5ae2967ac13e5Stanislav Kholmanskikh#include "lapi/semun.h"
5792b688b8340e2424643c5d33dcacfbb109529402robbiew
58e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangchar *TCID = "pipeio";
59e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangint TST_TOTAL = 1;
6092b688b8340e2424643c5d33dcacfbb109529402robbiew
61df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper#define SAFE_FREE(p) { if (p) { free(p); (p)=NULL; } }
6292b688b8340e2424643c5d33dcacfbb109529402robbiew
63f7298c78ccec20dbf2f96a83265c38811655241amridge#if defined(__linux__)
64354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao#define NBPW sizeof(int)
65865695bbc89088b9526ea9045410e5afb70a985cplars#endif
66865695bbc89088b9526ea9045410e5afb70a985cplars
67865695bbc89088b9526ea9045410e5afb70a985cplars#define OCTAL	'o'
68865695bbc89088b9526ea9045410e5afb70a985cplars#define HEX	'x'
69865695bbc89088b9526ea9045410e5afb70a985cplars#define DECIMAL	'd'
70865695bbc89088b9526ea9045410e5afb70a985cplars#define ASCII	'a'
71865695bbc89088b9526ea9045410e5afb70a985cplars#define NO_OUT	'n'
72865695bbc89088b9526ea9045410e5afb70a985cplars
73865695bbc89088b9526ea9045410e5afb70a985cplars#define PIPE_NAMED	"named pipe,"
74865695bbc89088b9526ea9045410e5afb70a985cplars#define PIPE_UNNAMED	"sys pipe,"
75865695bbc89088b9526ea9045410e5afb70a985cplars
76865695bbc89088b9526ea9045410e5afb70a985cplars#define BLOCKING_IO	"blking,"
77865695bbc89088b9526ea9045410e5afb70a985cplars#define NON_BLOCKING_IO	"non-blking,"
78865695bbc89088b9526ea9045410e5afb70a985cplars#define UNNAMED_IO	""
79865695bbc89088b9526ea9045410e5afb70a985cplars
80ea37be8bf7db7f9709cf36fce28521071a4dae40subrata_modak#define MAX_ERRS 16
81ea37be8bf7db7f9709cf36fce28521071a4dae40subrata_modak#define MAX_EMPTY 256
82ea37be8bf7db7f9709cf36fce28521071a4dae40subrata_modak
83e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int parse_options(int argc, char *argv[]);
84e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic void setup(int argc, char *argv[]);
85e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic void cleanup(void);
86e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
87e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic void do_child(void);
88e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic void do_parent(void);
89e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
90e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic void help(void), usage(void), prt_examples(void);
91e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic void prt_buf(char **addr, char *buf, int length, int format);
92e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic void sig_child(int sig);
93e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int check_rw_buf(void);
94e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
95e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic volatile sig_atomic_t nchildcompleted;
96e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
97e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang/* variables may be modified in setup() */
98e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int num_writers = 1;	/* number of writers */
99e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int num_writes = 1;	/* number of writes per child */
100e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int loop;		/* loop indefinitely */
101e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int exit_error = 1;	/* exit on error #, zero means no exit */
102e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int size = 327;		/* default size */
103e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int unpipe;		/* un-named pipe if non-zero */
104e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int verbose;		/* verbose mode if set */
105e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int quiet;		/* quiet mode if set */
106e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int num_rpt;		/* ping number, how often to print message */
107e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int chld_wait;	/* max time to wait between writes, 1 == no wait */
108e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int parent_wait;	/* max time to wait between reads, 1 == no wait */
109e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int ndelay = O_NDELAY;	/* additional flag to open */
110e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic char *writebuf;
111e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic char *readbuf;
112e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic char pname[PATH_MAX];	/* contains the name of the named pipe */
113e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic char *blk_type = NON_BLOCKING_IO; /* blocking i/o or not */
114e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic char *pipe_type;		/* type of pipe under test */
115e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int format = HEX;
116e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int format_size = -1;
117e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int iotype;		/* sync io */
118e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
119e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang/* variables will be modified in running */
120e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int error;
121e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int count;
122e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int read_fd;
123e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int write_fd;
124e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int empty_read;
125e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int sem_id;
126e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
127b681672752634a70ba1bd6b1c6b5ae2967ac13e5Stanislav Kholmanskikhstatic union semun u;
128e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
129e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangint main(int ac, char *av[])
130e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang{
131e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	int i;
132e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	unsigned int j;
133e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	unsigned int uwait_iter = 1000, uwait_total = 5000000;
134e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	pid_t child;
135865695bbc89088b9526ea9045410e5afb70a985cplars
136e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	setup(ac, av);
137865695bbc89088b9526ea9045410e5afb70a985cplars
138e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	for (i = num_writers; i > 0; --i) {
139e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
140e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		child = tst_fork();
141e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		switch (child) {
142e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		case -1:
143e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
144e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		case 0:
145e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			do_child();
146e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			exit(0);
147e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		default:
148e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			break;
149354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		}
150865695bbc89088b9526ea9045410e5afb70a985cplars	}
151865695bbc89088b9526ea9045410e5afb70a985cplars
152e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	do_parent();
153e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
154e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	if (empty_read)
155e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		tst_resm(TWARN, "%d empty reads", empty_read);
156e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
157e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	if (error) {
158e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		tst_resm(TFAIL, "%d data errors on pipe, read size = %d, %s %s",
159e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			 error, size, pipe_type, blk_type);
160e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	} else if (!quiet) {
161e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		tst_resm(TPASS, "%d pipe reads complete, read size = %d, %s %s",
162e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			 count + 1, size, pipe_type, blk_type);
163e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	}
164e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
165e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	/*
166e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	 * wait for all children to finish, timeout after uwait_total
167e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	 * semtimedop might not be available everywhere
168e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	 */
169e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	for (j = 0; j < uwait_total; j += uwait_iter) {
170e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		if (semctl(sem_id, 1, GETVAL) == 0)
171e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			break;
172e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		usleep(uwait_iter);
173e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	}
174e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
175e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	if (j >= uwait_total) {
176e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		tst_resm(TWARN,
177e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			 "Timed out waiting for child processes to exit");
178e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	}
179e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
180e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	cleanup();
181e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	tst_exit();
182e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang}
183e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
184e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int parse_options(int argc, char *argv[])
185e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang{
186e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	char *cp;
187e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	int c;
188e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	int ret = 0;
189e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	static double d;
190e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
191e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	while ((c = getopt(argc, argv, "T:bc:D:he:Ef:i:I:ln:p:qs:uvW:w:"))
192e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	       != -1) {
193354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		switch (c) {
19492b688b8340e2424643c5d33dcacfbb109529402robbiew		case 'T':
19592b688b8340e2424643c5d33dcacfbb109529402robbiew			TCID = optarg;
19692b688b8340e2424643c5d33dcacfbb109529402robbiew			break;
197865695bbc89088b9526ea9045410e5afb70a985cplars		case 'h':
198865695bbc89088b9526ea9045410e5afb70a985cplars			help();
199e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			ret = 1;
200865695bbc89088b9526ea9045410e5afb70a985cplars			break;
201865695bbc89088b9526ea9045410e5afb70a985cplars		case 'D':	/* pipe name */
202865695bbc89088b9526ea9045410e5afb70a985cplars			strcpy(pname, optarg);
203865695bbc89088b9526ea9045410e5afb70a985cplars			break;
204865695bbc89088b9526ea9045410e5afb70a985cplars		case 'b':	/* blocked */
205354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			ndelay = 0;
206865695bbc89088b9526ea9045410e5afb70a985cplars			blk_type = BLOCKING_IO;
207865695bbc89088b9526ea9045410e5afb70a985cplars			break;
208865695bbc89088b9526ea9045410e5afb70a985cplars		case 'c':	/* number childern */
209e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			if (sscanf(optarg, "%d", &num_writers) != 1) {
210354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				fprintf(stderr,
211354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					"%s: --c option invalid arg '%s'.\n",
212354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					TCID, optarg);
213e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				ret = 1;
214e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			} else if (num_writers <= 0) {
215e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				fprintf(stderr, "%s: --c option must be "
216e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					"greater than zero.\n", TCID);
217e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				ret = 1;
218865695bbc89088b9526ea9045410e5afb70a985cplars			}
219865695bbc89088b9526ea9045410e5afb70a985cplars			break;
220865695bbc89088b9526ea9045410e5afb70a985cplars		case 'e':	/* exit on error # */
221865695bbc89088b9526ea9045410e5afb70a985cplars			if (sscanf(optarg, "%d", &exit_error) != 1) {
222354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				fprintf(stderr,
223354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					"%s: --e option invalid arg '%s'.\n",
224354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					TCID, optarg);
225e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				ret = 1;
226354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			} else if (exit_error < 0) {
227e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				fprintf(stderr, "%s: --e option must be "
228e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					"greater than zero.\n", TCID);
229e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				ret = 1;
230865695bbc89088b9526ea9045410e5afb70a985cplars			}
231865695bbc89088b9526ea9045410e5afb70a985cplars			break;
232865695bbc89088b9526ea9045410e5afb70a985cplars		case 'E':
233865695bbc89088b9526ea9045410e5afb70a985cplars			prt_examples();
234e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			ret = 1;
235865695bbc89088b9526ea9045410e5afb70a985cplars			break;
236865695bbc89088b9526ea9045410e5afb70a985cplars		case 'f':	/* format of buffer on error */
237354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			switch (optarg[0]) {
238865695bbc89088b9526ea9045410e5afb70a985cplars			case 'x':
239865695bbc89088b9526ea9045410e5afb70a985cplars			case 'X':
240865695bbc89088b9526ea9045410e5afb70a985cplars				format = HEX;
241865695bbc89088b9526ea9045410e5afb70a985cplars				break;
242865695bbc89088b9526ea9045410e5afb70a985cplars			case 'o':
243865695bbc89088b9526ea9045410e5afb70a985cplars			case 'O':
244865695bbc89088b9526ea9045410e5afb70a985cplars				format = OCTAL;
245865695bbc89088b9526ea9045410e5afb70a985cplars				break;
246865695bbc89088b9526ea9045410e5afb70a985cplars			case 'd':
247865695bbc89088b9526ea9045410e5afb70a985cplars			case 'D':
248865695bbc89088b9526ea9045410e5afb70a985cplars				format = DECIMAL;
249865695bbc89088b9526ea9045410e5afb70a985cplars				break;
250865695bbc89088b9526ea9045410e5afb70a985cplars			case 'a':
251865695bbc89088b9526ea9045410e5afb70a985cplars			case 'A':
252865695bbc89088b9526ea9045410e5afb70a985cplars				format = ASCII;
253865695bbc89088b9526ea9045410e5afb70a985cplars				break;
254354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			case 'n':	/* not output */
255865695bbc89088b9526ea9045410e5afb70a985cplars			case 'N':
256865695bbc89088b9526ea9045410e5afb70a985cplars				format = NO_OUT;
257865695bbc89088b9526ea9045410e5afb70a985cplars				break;
258865695bbc89088b9526ea9045410e5afb70a985cplars
259354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			default:
260354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				fprintf(stderr,
261354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					"%s: --f option invalid arg '%s'.\n",
262354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					TCID, optarg);
263e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				fprintf(stderr, "\tIt must be x(hex), o(octal),"
264e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					"d(decimal), a(ascii) or n(none) with "
265e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					"opt sz\n");
266e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				ret = 1;
267865695bbc89088b9526ea9045410e5afb70a985cplars				break;
268865695bbc89088b9526ea9045410e5afb70a985cplars			}
269865695bbc89088b9526ea9045410e5afb70a985cplars			cp = optarg;
270865695bbc89088b9526ea9045410e5afb70a985cplars			cp++;
271df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper			if (*cp) {
272354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				if (sscanf(cp, "%i", &format_size) != 1) {
273e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					fprintf(stderr, "%s: --f option invalid"
274e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang						"arg '%s'.\n", TCID, optarg);
275e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					fprintf(stderr, "\tIt must be x(hex),"
276e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang						"o(octal), d(decimal), a(ascii)"
277e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang						" or n(none) with opt sz\n");
278e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					ret = 1;
279354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					break;
280354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				}
281865695bbc89088b9526ea9045410e5afb70a985cplars			}
282865695bbc89088b9526ea9045410e5afb70a985cplars			break;
283865695bbc89088b9526ea9045410e5afb70a985cplars
284354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		case 'I':
285e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			iotype = lio_parse_io_arg1(optarg);
286e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			if (iotype == -1) {
287e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				fprintf(stderr, "%s: --I arg is invalid, "
288e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					"must be s, p, f, a, l, L or r.\n",
289354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					TCID);
290e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				ret = 1;
291354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			}
292865695bbc89088b9526ea9045410e5afb70a985cplars			break;
293865695bbc89088b9526ea9045410e5afb70a985cplars
294865695bbc89088b9526ea9045410e5afb70a985cplars		case 'l':	/* loop forever */
295865695bbc89088b9526ea9045410e5afb70a985cplars			++loop;
296865695bbc89088b9526ea9045410e5afb70a985cplars			break;
297865695bbc89088b9526ea9045410e5afb70a985cplars
298865695bbc89088b9526ea9045410e5afb70a985cplars		case 'i':
299865695bbc89088b9526ea9045410e5afb70a985cplars		case 'n':	/* number writes per child */
300865695bbc89088b9526ea9045410e5afb70a985cplars			if (sscanf(optarg, "%d", &num_writes) != 1) {
301e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				fprintf(stderr, "%s: --i/n option invalid "
302e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					"arg '%s'.\n", TCID, optarg);
303e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				ret = 1;
304354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			} else if (num_writes < 0) {
305e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				fprintf(stderr, "%s: --i/n option must be "
306e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					"greater than equal to zero.\n",
307354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					TCID);
308e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				ret = 1;
309865695bbc89088b9526ea9045410e5afb70a985cplars			}
310865695bbc89088b9526ea9045410e5afb70a985cplars
311865695bbc89088b9526ea9045410e5afb70a985cplars			if (num_writes == 0)	/* loop forever */
312865695bbc89088b9526ea9045410e5afb70a985cplars				++loop;
313865695bbc89088b9526ea9045410e5afb70a985cplars			break;
314865695bbc89088b9526ea9045410e5afb70a985cplars		case 'p':	/* ping */
315865695bbc89088b9526ea9045410e5afb70a985cplars			if (sscanf(optarg, "%d", &num_rpt) != 1) {
316354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				fprintf(stderr,
317354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					"%s: --p option invalid arg '%s'.\n",
318354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					TCID, optarg);
319e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				ret = 1;
320354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			} else if (num_rpt < 0) {
321e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				fprintf(stderr, "%s: --p option must be greater"
322e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					" than equal to zero.\n", TCID);
323e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				ret = 1;
324865695bbc89088b9526ea9045410e5afb70a985cplars			}
325865695bbc89088b9526ea9045410e5afb70a985cplars			break;
326354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		case 'q':	/* Quiet - NOPASS */
327354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			quiet = 1;
328865695bbc89088b9526ea9045410e5afb70a985cplars			break;
329865695bbc89088b9526ea9045410e5afb70a985cplars		case 's':	/* size */
330865695bbc89088b9526ea9045410e5afb70a985cplars			if (sscanf(optarg, "%d", &size) != 1) {
331354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				fprintf(stderr,
332354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					"%s: --s option invalid arg '%s'.\n",
333354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					TCID, optarg);
334e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				ret = 1;
335354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			} else if (size <= 0) {
336e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				fprintf(stderr, "%s: --s option must be greater"
337e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					" than zero.\n", TCID);
338e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				ret = 1;
339865695bbc89088b9526ea9045410e5afb70a985cplars			}
340865695bbc89088b9526ea9045410e5afb70a985cplars			break;
341865695bbc89088b9526ea9045410e5afb70a985cplars		case 'u':
342354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			unpipe = 1;	/* un-named pipe */
343865695bbc89088b9526ea9045410e5afb70a985cplars			break;
344865695bbc89088b9526ea9045410e5afb70a985cplars		case 'v':	/* verbose */
345354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			verbose = 1;
346865695bbc89088b9526ea9045410e5afb70a985cplars			break;
347e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		case 'W':	/* max wait time between reads */
348865695bbc89088b9526ea9045410e5afb70a985cplars			d = strtod(optarg, &cp);
349865695bbc89088b9526ea9045410e5afb70a985cplars			if (*cp != '\0') {
350354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				fprintf(stderr,
351354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					"%s: --w option invalid arg '%s'.\n",
352354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					TCID, optarg);
353e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				ret = 1;
354354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			} else if (d < 0) {
355e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				fprintf(stderr, "%s: --w option must be greater"
356e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					" than zero.\n", TCID);
357e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				ret = 1;
358865695bbc89088b9526ea9045410e5afb70a985cplars			}
359865695bbc89088b9526ea9045410e5afb70a985cplars			parent_wait = (int)(d * 1000000.0);
360865695bbc89088b9526ea9045410e5afb70a985cplars			break;
361865695bbc89088b9526ea9045410e5afb70a985cplars		case 'w':	/* max wait time between writes */
362865695bbc89088b9526ea9045410e5afb70a985cplars			d = strtod(optarg, &cp);
363865695bbc89088b9526ea9045410e5afb70a985cplars			if (*cp != '\0') {
364354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				fprintf(stderr,
365354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					"%s: --w option invalid arg '%s'.\n",
366354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					TCID, optarg);
367e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				ret = 1;
368354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			} else if (d < 0) {
369e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				fprintf(stderr, "%s: --w option must be greater"
370e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					" than zero.\n", TCID);
371e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				ret = 1;
372865695bbc89088b9526ea9045410e5afb70a985cplars			}
373865695bbc89088b9526ea9045410e5afb70a985cplars			chld_wait = (int)(d * 1000000.0);
374865695bbc89088b9526ea9045410e5afb70a985cplars			break;
375865695bbc89088b9526ea9045410e5afb70a985cplars		case '?':
376e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			ret = 1;
377865695bbc89088b9526ea9045410e5afb70a985cplars			break;
378354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		}
379e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
380e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		if (ret == 1) {
381e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			usage();
382e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			return ret;
383e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		}
384e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	}
385e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
386e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	return ret;
387e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang}
388e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
389e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic void setup(int argc, char *argv[])
390e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang{
391e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	int ret;
392e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	char *toutput;
393e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	int fds[2];
394e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
395e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	tst_sig(FORK, DEF_HANDLER, cleanup);
396e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
397e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	TEST_PAUSE;
398e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
399e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	tst_tmpdir();
400e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
401e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	if (signal(SIGCHLD, sig_child) == SIG_ERR) {
402e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		tst_brkm(TBROK | TERRNO, cleanup,
403e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			 "set signal handler for SIGCHLD failed");
404865695bbc89088b9526ea9045410e5afb70a985cplars	}
405865695bbc89088b9526ea9045410e5afb70a985cplars
406e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	toutput = getenv("TOUTPUT");
407e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	if (toutput != NULL && strcmp(toutput, "NOPASS") == 0)
408e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		quiet = 1;
409e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
410e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	sprintf(pname, "%s", "tpipe");
411e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
412e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	ret = parse_options(argc, argv);
413e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	if (ret == 1)
414e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		tst_brkm(TBROK, cleanup, "options parse error");
415e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
4168fb1cdb0538640f295691929650408688537fb7fGarrett Cooper	if (format_size == -1)
417865695bbc89088b9526ea9045410e5afb70a985cplars		format_size = size;
418865695bbc89088b9526ea9045410e5afb70a985cplars
419865695bbc89088b9526ea9045410e5afb70a985cplars	/*
420865695bbc89088b9526ea9045410e5afb70a985cplars	 * If there is more than one writer, all writes and reads
421865695bbc89088b9526ea9045410e5afb70a985cplars	 * must be the same size.  Only writes of a size <= PIPE_BUF
422865695bbc89088b9526ea9045410e5afb70a985cplars	 * are atomic.  T
423865695bbc89088b9526ea9045410e5afb70a985cplars	 * Therefore, if size is greater than PIPE_BUF, we will break
424865695bbc89088b9526ea9045410e5afb70a985cplars	 * the writes into PIPE_BUF chunks.  We will also increase the
4254bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak	 * number of writes to ensure the same (or more) amount of
426865695bbc89088b9526ea9045410e5afb70a985cplars	 * data is written.  This is the same as erroring and telling
427865695bbc89088b9526ea9045410e5afb70a985cplars	 * the user the new cmd line to do the same thing.
428865695bbc89088b9526ea9045410e5afb70a985cplars	 * Example:
429354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	 *      pipeio -s 5000 -n 10 -c 5
430354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	 *      (each child will write at least 50000 bytes, since all
431354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	 *      writes have to be in 4096 chuncks or 13*4096 (53248)
432354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	 *      bytes will be written.)  This is the same as:
433865695bbc89088b9526ea9045410e5afb70a985cplars	 *      pipeio -s 4096 -n 13 -c 5
434865695bbc89088b9526ea9045410e5afb70a985cplars	 */
435e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	if (size > PIPE_BUF && num_writers > 1) {
436354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		if (!loop) {
437e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			/*
438e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			 * we must set num_writes*num_writers
439e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			 * doesn't overflow later
440e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			 */
441e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			num_writes = MIN(((long long)num_writes * size +
442e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					 PIPE_BUF - 1) / PIPE_BUF,
443e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					 INT_MAX / num_writers);
444e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			tst_resm(TINFO, "adjusting i/o size to %d, and # of "
445e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				 "writes to %d", PIPE_BUF, num_writes);
446354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		} else {
447354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			tst_resm(TINFO, "adjusting i/o size to %d", PIPE_BUF);
448354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		}
449354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		size = PIPE_BUF;
450865695bbc89088b9526ea9045410e5afb70a985cplars	}
451865695bbc89088b9526ea9045410e5afb70a985cplars
452e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	writebuf = SAFE_MALLOC(cleanup, size);
453e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	readbuf = SAFE_MALLOC(cleanup, size);
454865695bbc89088b9526ea9045410e5afb70a985cplars
455354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	memset(writebuf, 'Z', size);
456e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	writebuf[size - 1] = 'A';
457865695bbc89088b9526ea9045410e5afb70a985cplars
458e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	sem_id = semget(IPC_PRIVATE, 2, IPC_CREAT | S_IRWXU);
459e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	if (sem_id == -1) {
460e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		tst_brkm(TBROK | TERRNO, cleanup,
461e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			 "Couldn't allocate semaphore");
462ea37be8bf7db7f9709cf36fce28521071a4dae40subrata_modak	}
463ea37be8bf7db7f9709cf36fce28521071a4dae40subrata_modak
464e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	if (semctl(sem_id, 0, SETVAL, u) == -1) {
465e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		tst_brkm(TBROK | TERRNO, cleanup,
466354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			 "Couldn't initialize semaphore 0 value");
467e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	}
468c5bb3eb32575cbb2fe59db6c73e6302b4bea41d3Jan Stancek
469e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	if (semctl(sem_id, 1, SETVAL, u) == -1) {
470e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		tst_brkm(TBROK | TERRNO, cleanup,
471354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			 "Couldn't initialize semaphore 1 value");
472865695bbc89088b9526ea9045410e5afb70a985cplars	}
473865695bbc89088b9526ea9045410e5afb70a985cplars
474df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper	if (unpipe) {
475e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		SAFE_PIPE(cleanup, fds);
476865695bbc89088b9526ea9045410e5afb70a985cplars		read_fd = fds[0];
477865695bbc89088b9526ea9045410e5afb70a985cplars		write_fd = fds[1];
478865695bbc89088b9526ea9045410e5afb70a985cplars		pipe_type = PIPE_UNNAMED;
479865695bbc89088b9526ea9045410e5afb70a985cplars		blk_type = UNNAMED_IO;
480865695bbc89088b9526ea9045410e5afb70a985cplars	} else {
48123a3e801e9392eab21662b277a5558402fa793dbCyril Hrubis		SAFE_MKFIFO(cleanup, pname, 0777);
482865695bbc89088b9526ea9045410e5afb70a985cplars		pipe_type = PIPE_NAMED;
483865695bbc89088b9526ea9045410e5afb70a985cplars	}
484e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang}
485865695bbc89088b9526ea9045410e5afb70a985cplars
486e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic void cleanup(void)
487e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang{
488e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	SAFE_FREE(writebuf);
489e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	SAFE_FREE(readbuf);
490e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
491e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	semctl(sem_id, 0, IPC_RMID);
492e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
493e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	if (!unpipe)
494d96efcf348d74718bdde273efe8a680f7397dd86Jan Stancek		unlink(pname);
495e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
496e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	tst_rmdir();
497e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang}
498e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
499e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic void do_child(void)
500e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang{
501e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	int *count_word;        /* holds address where to write writers count */
502e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	int *pid_word;          /* holds address where to write writers pid */
503e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	int nb, j;
504e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	long clock;
505e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	char *cp;
506e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	long int n;
507e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	struct sembuf sem_op;
508e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	pid_t self_pid =  getpid();
509865695bbc89088b9526ea9045410e5afb70a985cplars
510e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	if (!unpipe) {
511e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		write_fd = open(pname, O_WRONLY);
512e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		if (write_fd == -1) {
513e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			fprintf(stderr, "child pipe open(%s, %#o) failed",
514e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				pname, O_WRONLY | ndelay);
515865695bbc89088b9526ea9045410e5afb70a985cplars			exit(1);
516865695bbc89088b9526ea9045410e5afb70a985cplars		}
517e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		if (ndelay && fcntl(write_fd, F_SETFL, O_NONBLOCK) == -1) {
518e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			fprintf(stderr, "Failed setting the pipe to "
519e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				"nonblocking mode");
520e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			exit(1);
521865695bbc89088b9526ea9045410e5afb70a985cplars		}
522e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	} else {
523e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		close(read_fd);
524e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	}
525865695bbc89088b9526ea9045410e5afb70a985cplars
526e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	sem_op = (struct sembuf) {
527e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		 .sem_num = 0, .sem_op = 1, .sem_flg = 0};
528ea37be8bf7db7f9709cf36fce28521071a4dae40subrata_modak
529e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	if (semop(sem_id, &sem_op, 1) == -1) {
530e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		fprintf(stderr, "child: %d couldn't raise the semaphore 0",
531e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			self_pid);
532e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		exit(1);
533e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	}
534ea37be8bf7db7f9709cf36fce28521071a4dae40subrata_modak
535e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	pid_word = (int *)&writebuf[0];
536e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	count_word = (int *)&writebuf[NBPW];
537865695bbc89088b9526ea9045410e5afb70a985cplars
538e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	for (j = 0; j < num_writes || loop; ++j) {
539e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		/*
540e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		 * writes are only in one unit when the size of the write
541e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		 * is <= PIPE_BUF.
542e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		 * Therefore, if size is greater than PIPE_BUF, we will break
543e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		 * the writes into PIPE_BUF chunks.
544e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		 * All writes and read need to be same size.
545e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		 */
546865695bbc89088b9526ea9045410e5afb70a985cplars
547e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		/*
548e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		 * write pid and count in first two
549e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		 * words of buffer
550e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		 */
551e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		*count_word = j;
552e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		*pid_word = self_pid;
553865695bbc89088b9526ea9045410e5afb70a985cplars
554e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		nb = lio_write_buffer(write_fd, iotype, writebuf, size,
555e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				      SIGUSR1, &cp, 0);
556e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		if (nb < 0) {
557865695bbc89088b9526ea9045410e5afb70a985cplars			/*
558e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			 * If lio_write_buffer returns a negative number,
559e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			 * the return will be -errno.
560865695bbc89088b9526ea9045410e5afb70a985cplars			 */
561e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			fprintf(stderr, "pass %d: lio_write_buffer(%s) failed;"
562e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				" it returned %d: %s",
563e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				j, cp, nb, strerror(-nb));
564ea37be8bf7db7f9709cf36fce28521071a4dae40subrata_modak				exit(1);
565e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		} else if (nb != size) {
566e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			fprintf(stderr, "pass %d: lio_write_buffer(%s) failed,"
567e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				" write count %d, but expected to write %d",
568e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				j, cp, nb, size);
569e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		}
570e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		if (verbose) {
571e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			fprintf(stderr, "pass %d: pid %d: wrote %d bytes,"
572e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				"expected %d bytes",
573e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				j, self_pid, nb, size);
574865695bbc89088b9526ea9045410e5afb70a985cplars		}
575c5bb3eb32575cbb2fe59db6c73e6302b4bea41d3Jan Stancek
576e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		if (chld_wait) {
577e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			clock = time(0);
578e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			srand48(clock);
579e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			n = lrand48() % chld_wait;
580e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			usleep(n);
581e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		}
582e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		fflush(stderr);
583865695bbc89088b9526ea9045410e5afb70a985cplars	}
584865695bbc89088b9526ea9045410e5afb70a985cplars
585e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	/* child waits until parent completes open() */
586e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	sem_op = (struct sembuf) {
587e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		  .sem_num = 1, .sem_op = -1, .sem_flg = 0};
588e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	if (semop(sem_id, &sem_op, 1) == -1)
589e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		fprintf(stderr, "Couldn't lower the semaphore 1");
590e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
591e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	exit(0);
592e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang}
593e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
594e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic int check_rw_buf(void)
595e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang{
596e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	int i;
597e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
598e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	for (i = 2 * NBPW; i < size; ++i) {
599e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		if (writebuf[i] != readbuf[i]) {
600e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			++error;
601e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			tst_resm(TFAIL,
602e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				 "FAIL data error on byte %d; rd# %d, sz= %d, "
603e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				 "%s %s empty_reads= %d, err= %d",
604e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				 i, count, size, pipe_type, blk_type,
605e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				 empty_read, error);
606e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			prt_buf(&readbuf, readbuf, format_size, format);
607e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			fflush(stdout);
608e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			return 1;
609865695bbc89088b9526ea9045410e5afb70a985cplars		}
610e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	}
611865695bbc89088b9526ea9045410e5afb70a985cplars
612e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	return 0;
613e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang}
614c5bb3eb32575cbb2fe59db6c73e6302b4bea41d3Jan Stancek
615e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic void do_parent(void)
616e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang{
617e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	int i, nb;
618e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	long clock;
619e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	time_t start_time, current_time, diff_time;
620e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	char *cp;
621e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	long int n;
622e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	struct sembuf sem_op;
623ea37be8bf7db7f9709cf36fce28521071a4dae40subrata_modak
624e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	start_time = time(0);
625e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	if (!unpipe) {
626e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		read_fd = SAFE_OPEN(cleanup, pname, O_RDONLY);
627e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		if (ndelay && fcntl(read_fd, F_SETFL, O_NONBLOCK) == -1) {
628e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			tst_brkm(TBROK | TERRNO, cleanup,
629e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				 "Failed setting the pipe to nonblocking mode");
630ea37be8bf7db7f9709cf36fce28521071a4dae40subrata_modak		}
631e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	} else {
632e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		SAFE_CLOSE(cleanup, write_fd);
633e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	}
634ea37be8bf7db7f9709cf36fce28521071a4dae40subrata_modak
635e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	/* raise semaphore so children can exit */
636e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	sem_op = (struct sembuf) {
637e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		  .sem_num = 1, .sem_op = num_writers, .sem_flg = 0};
638e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	if (semop(sem_id, &sem_op, 1) == -1) {
639e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		tst_brkm(TBROK | TERRNO, cleanup,
640e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			 "Couldn't raise the semaphore 1");
641e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	}
642e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
643e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	sem_op = (struct sembuf) {
644e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		  .sem_num = 0, .sem_op = -num_writers, .sem_flg = 0};
645e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
646e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	while (nchildcompleted < num_writers
647e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	       && semop(sem_id, &sem_op, 1) == -1) {
648e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		if (errno == EINTR)
649e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			continue;
650e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		tst_brkm(TBROK | TERRNO, cleanup,
651e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			 "Couldn't wait on semaphore 0");
652e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	}
653e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang
654e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	/* parent start to read pipe */
655e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	for (i = num_writers * num_writes; i > 0 || loop; --i) {
656e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		if (error >= MAX_ERRS || empty_read >= MAX_EMPTY)
657e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			break;
658e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		if (parent_wait) {
659e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			clock = time(0);
660e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			srand48(clock);
661e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			n = lrand48() % parent_wait;
662e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			usleep(n);
663e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		}
664e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		++count;
665e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		nb = lio_read_buffer(read_fd, iotype, readbuf, size,
666e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				     SIGUSR1, &cp, 0);
667e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		if (nb < 0) {
668e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			/*
669e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			 * If lio_read_buffer returns a negative number,
670e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			 * the return will be -errno.
671e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			 */
672e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			tst_resm(TFAIL, "pass %d: lio_read_buffer(%s) failed; "
673e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				 "returned %d: %s", i, cp, nb, strerror(-nb));
674e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			++i;
675e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			count--;
676e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			error++;
677e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			continue;
678e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		} else {
679e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			if (nb == 0) {
680e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				if (nchildcompleted >= num_writers && !loop) {
681e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					tst_resm(TWARN, "The children have "
682e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang						 "died prematurely");
683e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					break;	/* All children have died */
684e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				}
685e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				empty_read++;
686354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				++i;
687354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				count--;
688354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				continue;
689e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			} else if (nb < size && size <= PIPE_BUF) {
690e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				tst_resm(TFAIL, "pass %d: partial read from the"
691e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					" pipe: read %d bytes, expected %d, "
692e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					"read count %d", i, nb, size, count);
693e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				++error;
694e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			} else if (nb == size) {
695e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				check_rw_buf();
696e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				if (exit_error && exit_error == error)
697e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					return;
698865695bbc89088b9526ea9045410e5afb70a985cplars			}
699c5bb3eb32575cbb2fe59db6c73e6302b4bea41d3Jan Stancek
700e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			if (verbose || (num_rpt && !(count % num_rpt))) {
701e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				current_time = time(0);
702e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				diff_time = current_time - start_time;
703e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				tst_resm(TFAIL,
704e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					 "(%d) rd# %d, sz= %d, %s %s "
705e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					 "empty_reads= %d, err= %d\n",
706e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					 (int)diff_time, count, size,
707e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					 pipe_type, blk_type,
708e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang					 empty_read, error);
709e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				fflush(stdout);
710e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			}
711c5bb3eb32575cbb2fe59db6c73e6302b4bea41d3Jan Stancek		}
712865695bbc89088b9526ea9045410e5afb70a985cplars	}
713af9d331c222acaadc96b5bb2c0708944dfa6db2cStanislav Kholmanskikh
714af9d331c222acaadc96b5bb2c0708944dfa6db2cStanislav Kholmanskikh	SAFE_CLOSE(cleanup, read_fd);
715865695bbc89088b9526ea9045410e5afb70a985cplars}
716865695bbc89088b9526ea9045410e5afb70a985cplars
717e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic void usage(void)
718865695bbc89088b9526ea9045410e5afb70a985cplars{
719e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	fprintf(stderr, "Usage: %s [-bEv][-c #writers][-D pname][-h]"
720e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		"[-e exit_num][-f fmt][-l][-i #writes][-n #writes][-p num_rpt]"
721e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		"\n\t[-s size][-W max_wait][-w max_wait][-u]\n", TCID);
722865695bbc89088b9526ea9045410e5afb70a985cplars	fflush(stderr);
723865695bbc89088b9526ea9045410e5afb70a985cplars}
724865695bbc89088b9526ea9045410e5afb70a985cplars
725e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic void help(void)
726865695bbc89088b9526ea9045410e5afb70a985cplars{
727354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	usage();
728865695bbc89088b9526ea9045410e5afb70a985cplars
729e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	printf(" -b    - blocking reads and writes. default non-block\n\
730865695bbc89088b9526ea9045410e5afb70a985cplars  -c #writers  - number of writers (childern)\n\
731865695bbc89088b9526ea9045410e5afb70a985cplars  -D pname     - name of fifo (def tpipe<pid>)\n\
732865695bbc89088b9526ea9045410e5afb70a985cplars  -h           - print this help message\n\
733865695bbc89088b9526ea9045410e5afb70a985cplars  -e exit_num  - exit on error exit_num, 0 is ignore errors, 1 is default.\n\
734865695bbc89088b9526ea9045410e5afb70a985cplars  -E           - print cmd line examples and exit\n\
735865695bbc89088b9526ea9045410e5afb70a985cplars  -f format    - define format of bad buffer: h(hex), o(octal)\n\
736865695bbc89088b9526ea9045410e5afb70a985cplars                 d(decimal), a(ascii), n (none). hex is default\n\
737865695bbc89088b9526ea9045410e5afb70a985cplars	         option size can be added to control output\n\
738865695bbc89088b9526ea9045410e5afb70a985cplars  -i #writes   - number write per child, zero means forever.\n\
739865695bbc89088b9526ea9045410e5afb70a985cplars  -I io_type   - Specifies io type: s - sync, p - polled async, a - async (def s)\n\
740865695bbc89088b9526ea9045410e5afb70a985cplars                 l - listio sync, L - listio async, r - random\n\
741865695bbc89088b9526ea9045410e5afb70a985cplars  -l           - loop forever (implied by -n 0).\n\
742865695bbc89088b9526ea9045410e5afb70a985cplars  -n #writes   - same as -i (for compatability).\n\
743865695bbc89088b9526ea9045410e5afb70a985cplars  -p num_rpt   - number of reads before a report\n\
744865695bbc89088b9526ea9045410e5afb70a985cplars  -q           - quiet mode, no PASS results are printed\n\
745865695bbc89088b9526ea9045410e5afb70a985cplars  -s size      - size of read and write (def 327)\n\
746865695bbc89088b9526ea9045410e5afb70a985cplars                 if size >= 4096, i/o will be in 4096 chuncks\n\
747865695bbc89088b9526ea9045410e5afb70a985cplars  -w max_wait  - max time (seconds) for sleep between writes.\n\
748865695bbc89088b9526ea9045410e5afb70a985cplars                 max_wait is interpreted as a double with ms accuracy.\n\
749865695bbc89088b9526ea9045410e5afb70a985cplars  -W max_wait  - max time (seconds) for sleep between reads\n\
750865695bbc89088b9526ea9045410e5afb70a985cplars                 max_wait is interpreted as a double with ms accuracy.\n\
751865695bbc89088b9526ea9045410e5afb70a985cplars  -u           - un-named pipe instead of named pipe\n\
752865695bbc89088b9526ea9045410e5afb70a985cplars  -v           - verbose mode, all writes/reads resutlts printed\n");
753865695bbc89088b9526ea9045410e5afb70a985cplars
754865695bbc89088b9526ea9045410e5afb70a985cplars	fflush(stdout);
755865695bbc89088b9526ea9045410e5afb70a985cplars}
756865695bbc89088b9526ea9045410e5afb70a985cplars
757e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic void prt_buf(char **addr, char *buf, int length, int format)
758865695bbc89088b9526ea9045410e5afb70a985cplars{
759865695bbc89088b9526ea9045410e5afb70a985cplars	int i;
760354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int num_words = length / NBPW;	/* given length in bytes, get length in words */
761865695bbc89088b9526ea9045410e5afb70a985cplars	int width;		/* number of columns */
76233abc163f5df27e0127cc60690268ce0416d0433robbiew	int extra_words = 0;	/* odd or even number of words */
763865695bbc89088b9526ea9045410e5afb70a985cplars	char *a = buf;
764865695bbc89088b9526ea9045410e5afb70a985cplars	char b[NBPW];
765354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	char c[NBPW * 2];
766865695bbc89088b9526ea9045410e5afb70a985cplars	char *p;
767865695bbc89088b9526ea9045410e5afb70a985cplars	long *word;
768865695bbc89088b9526ea9045410e5afb70a985cplars
769354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if (format == NO_OUT)	/* if no output wanted, return */
770865695bbc89088b9526ea9045410e5afb70a985cplars		return;
771865695bbc89088b9526ea9045410e5afb70a985cplars
772354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if (length % NBPW)
773354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		++num_words;	/* is length in full words? */
774865695bbc89088b9526ea9045410e5afb70a985cplars	if (format == ASCII) {
775354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		width = 3;
776865695bbc89088b9526ea9045410e5afb70a985cplars	} else {
777865695bbc89088b9526ea9045410e5afb70a985cplars		width = 2;
778354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		/* do we have an odd number of words? */
779354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		extra_words = num_words % width;
780865695bbc89088b9526ea9045410e5afb70a985cplars	}
781354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	for (i = 0; i < num_words; ++i, a += NBPW, addr++) {
782354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		word = (long *)a;
783354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		if (!(i % width)) {
784865695bbc89088b9526ea9045410e5afb70a985cplars			if (i > 0 && format != ASCII) {
785354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				/*
786354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				 * print the ascii equivalent of the data
787354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				 * before beginning the next line of output.
788354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				 */
789354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				memset(c, 0x00, width * NBPW);
790354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				/*
791354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				 * get the last 2 words printed
792354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				 */
793354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				memcpy(c, a - (width * NBPW), width * NBPW);
794e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang				for (p = c; (p - c) < (int)(width*NBPW); ++p) {
795865695bbc89088b9526ea9045410e5afb70a985cplars					if (*p < '!' || *p > '~')
796865695bbc89088b9526ea9045410e5afb70a985cplars						*p = '.';
797865695bbc89088b9526ea9045410e5afb70a985cplars				}
798354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				printf("\t%16.16s", c);
799865695bbc89088b9526ea9045410e5afb70a985cplars			}
800e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			printf("\n%p: ", addr);
801865695bbc89088b9526ea9045410e5afb70a985cplars			/***printf("\n%7o (%d): ",addr,i);***/
802865695bbc89088b9526ea9045410e5afb70a985cplars		}
803865695bbc89088b9526ea9045410e5afb70a985cplars
804865695bbc89088b9526ea9045410e5afb70a985cplars		switch (format) {
805354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		case HEX:
806354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			printf("%16.16lx ", *word);
807354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			break;
808354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		case DECIMAL:
809354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			printf("%10.10ld ", *word);
810354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			break;
811354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		case ASCII:
812354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			memcpy(b, a, NBPW);
813e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang			for (p = b; (p - b) < (int)NBPW; ++p) {
814354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				if (*p < '!' || *p > '~')
815354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					*p = '.';
816354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			}
817354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			printf("%8.8s ", b);
818354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			break;
819354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		default:
820354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			printf("%22.22lo ", *word);
821354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			break;
822865695bbc89088b9526ea9045410e5afb70a985cplars		}
823865695bbc89088b9526ea9045410e5afb70a985cplars	}
824865695bbc89088b9526ea9045410e5afb70a985cplars	if (format != ASCII) {
825354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		/*
826354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		 * print the ascii equivalent of the last words in the buffer
827354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		 * before returning.
828354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		 */
829354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		memset(c, 0x00, width * NBPW);
830354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		if (extra_words)
831354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			width = extra_words;	/* odd number of words */
832354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		memcpy(c, a - (width * NBPW), width * NBPW);
833e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang		for (p = c; (p - c) < (int)(width * NBPW); ++p) {
834865695bbc89088b9526ea9045410e5afb70a985cplars			if (*p < '!' || *p > '~')
835865695bbc89088b9526ea9045410e5afb70a985cplars				*p = '.';
836865695bbc89088b9526ea9045410e5afb70a985cplars		}
8374bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak		if (width == 2)
838354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			printf("\t%16.16s", c);
839865695bbc89088b9526ea9045410e5afb70a985cplars		else
840354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			printf("\t\t%16.8s", c);
841865695bbc89088b9526ea9045410e5afb70a985cplars	}
842865695bbc89088b9526ea9045410e5afb70a985cplars	printf("\n");
843865695bbc89088b9526ea9045410e5afb70a985cplars	fflush(stdout);
844865695bbc89088b9526ea9045410e5afb70a985cplars}
845865695bbc89088b9526ea9045410e5afb70a985cplars
846e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic void prt_examples(void)
847865695bbc89088b9526ea9045410e5afb70a985cplars{
848354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	printf("%s -c 5 -i 0 -s 4090 -b\n", TCID);
849354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	printf("%s -c 5 -i 0 -s 4090 -b -u \n", TCID);
850354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	printf("%s -c 5 -i 0 -s 4090 -b -W 3 -w 3 \n", TCID);
851865695bbc89088b9526ea9045410e5afb70a985cplars}
852865695bbc89088b9526ea9045410e5afb70a985cplars
853e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wangstatic void sig_child(int sig)
854865695bbc89088b9526ea9045410e5afb70a985cplars{
855354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int status;
856865695bbc89088b9526ea9045410e5afb70a985cplars
857e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	nchildcompleted++;
858865695bbc89088b9526ea9045410e5afb70a985cplars#if DEBUG
859e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	#define STR	"parent: received SIGCHLD\n"
860e3c09dcd8a640a4a82aec6968395c05a8ec95067Xiaoguang Wang	write(STDOUT_FILENO, str, strlen(STR));
861865695bbc89088b9526ea9045410e5afb70a985cplars#endif
862354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	waitpid(-1, &status, WNOHANG);
86347fa92461ea8b666c52f0682223b58a08c0d348dGarrett Cooper}
864