xref: /aosp_15_r20/external/ltp/testcases/kernel/mem/mmapstress/mmapstress10.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /* IBM Corporation */
2*49cdfc7eSAndroid Build Coastguard Worker /* 01/02/2003	Port to LTP [email protected] */
3*49cdfc7eSAndroid Build Coastguard Worker /* 06/30/2001	Port to Linux	[email protected] */
4*49cdfc7eSAndroid Build Coastguard Worker /*
5*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE 1
21*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <limits.h>
32*49cdfc7eSAndroid Build Coastguard Worker /*****  LTP Port        *****/
33*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
34*49cdfc7eSAndroid Build Coastguard Worker #define FAILED 0
35*49cdfc7eSAndroid Build Coastguard Worker #define PASSED 1
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker int local_flag = PASSED;
38*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "mmapstress10";
39*49cdfc7eSAndroid Build Coastguard Worker FILE *temp;
40*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker int anyfail();
43*49cdfc7eSAndroid Build Coastguard Worker void ok_exit();
44*49cdfc7eSAndroid Build Coastguard Worker /*****  **      **      *****/
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker /*
47*49cdfc7eSAndroid Build Coastguard Worker  *  This test stresses mmaps, specifically the code dealing with
48*49cdfc7eSAndroid Build Coastguard Worker  *  mapping of fragments.  It forks a specified number of children,
49*49cdfc7eSAndroid Build Coastguard Worker  *  all of whom mmap the same file, make a given number of accesses
50*49cdfc7eSAndroid Build Coastguard Worker  *  to random pages in the map (reading & writing and comparing data).
51*49cdfc7eSAndroid Build Coastguard Worker  *  Then the child exits and the parent forks another to take its place.
52*49cdfc7eSAndroid Build Coastguard Worker  *  Each time a child is forked, it stats the file and maps the full
53*49cdfc7eSAndroid Build Coastguard Worker  *  length of the file.  Meanwhile, another child is forked which
54*49cdfc7eSAndroid Build Coastguard Worker  *  continually writes to the file.  It loops writing some bytes (default
55*49cdfc7eSAndroid Build Coastguard Worker  *  20), then sleeps some seconds (default 1).  This causes the file
56*49cdfc7eSAndroid Build Coastguard Worker  *  to gradually grow, crossing fragment boundaries, etc.
57*49cdfc7eSAndroid Build Coastguard Worker  *  Then it forks yet *another* child who maps the file in extend
58*49cdfc7eSAndroid Build Coastguard Worker  *  mode, just to check out interaction.  (Because this will cause
59*49cdfc7eSAndroid Build Coastguard Worker  *  0 padding at end of file, children can't test as exactly as in tmmap -
60*49cdfc7eSAndroid Build Coastguard Worker  *  have to check for zero or pattern...)
61*49cdfc7eSAndroid Build Coastguard Worker  *
62*49cdfc7eSAndroid Build Coastguard Worker  *  This program continues to run until it either receives a SIGINT,
63*49cdfc7eSAndroid Build Coastguard Worker  *  or times out (if a timeout value is specified).  When either of
64*49cdfc7eSAndroid Build Coastguard Worker  *  these things happens, it cleans up its kids, then checks the
65*49cdfc7eSAndroid Build Coastguard Worker  *  file to make sure it has the correct data.
66*49cdfc7eSAndroid Build Coastguard Worker  *
67*49cdfc7eSAndroid Build Coastguard Worker  *  usage:
68*49cdfc7eSAndroid Build Coastguard Worker  *	mmapstress10 -p nprocs [-t minutes -w nbytes -s secs -f filesize
69*49cdfc7eSAndroid Build Coastguard Worker  *			 -S sparseoffset -r -o -m -l -d]
70*49cdfc7eSAndroid Build Coastguard Worker  *  where:
71*49cdfc7eSAndroid Build Coastguard Worker  *	-p nprocs	- specifies the number of mapping children
72*49cdfc7eSAndroid Build Coastguard Worker  *			  to create.  (nprocs + 1 children actually
73*49cdfc7eSAndroid Build Coastguard Worker  *			  get created, since one is the writer child)
74*49cdfc7eSAndroid Build Coastguard Worker  *	-t minutes	- specifies minutes to run.  If not specified,
75*49cdfc7eSAndroid Build Coastguard Worker  *			  default is to run forever until a SIGINT
76*49cdfc7eSAndroid Build Coastguard Worker  *			  is received.
77*49cdfc7eSAndroid Build Coastguard Worker  *	-w nbytes	- specifies number of bytes for writer process
78*49cdfc7eSAndroid Build Coastguard Worker  *			  to write at a time (i.e. between sleeps).
79*49cdfc7eSAndroid Build Coastguard Worker  *			  defaults to GROWSIZE bytes.
80*49cdfc7eSAndroid Build Coastguard Worker  *	-s secs		- specifies number of seconds for writer process
81*49cdfc7eSAndroid Build Coastguard Worker  *			  to sleep between writes.  Defaults to
82*49cdfc7eSAndroid Build Coastguard Worker  *			  SLEEPTIME seconds.
83*49cdfc7eSAndroid Build Coastguard Worker  *	-f filesize	- initial filesize (defaults to FILESIZE)
84*49cdfc7eSAndroid Build Coastguard Worker  *	-S sparseoffset - when non-zero, causes a sparse area to
85*49cdfc7eSAndroid Build Coastguard Worker  *			  be left before the data, meaning that the
86*49cdfc7eSAndroid Build Coastguard Worker  *			  actual initial file size is sparseoffset +
87*49cdfc7eSAndroid Build Coastguard Worker  *			  filesize.  Useful for testing large files.
88*49cdfc7eSAndroid Build Coastguard Worker  *			  (default is 0).
89*49cdfc7eSAndroid Build Coastguard Worker  *	-r		- randomize number of pages map children check.
90*49cdfc7eSAndroid Build Coastguard Worker  *			  (random % MAXLOOPS).  If not specified, each
91*49cdfc7eSAndroid Build Coastguard Worker  *			  child checks MAXLOOPS pages.
92*49cdfc7eSAndroid Build Coastguard Worker  *	-o		- randomize offset of file to map. (default is 0)
93*49cdfc7eSAndroid Build Coastguard Worker  *	-m		- do random msync/fsyncs as well
94*49cdfc7eSAndroid Build Coastguard Worker  *	-l		- if set, the output file is not removed on
95*49cdfc7eSAndroid Build Coastguard Worker  *			  program exit.
96*49cdfc7eSAndroid Build Coastguard Worker  *	-d		- enable debug outputd
97*49cdfc7eSAndroid Build Coastguard Worker  *
98*49cdfc7eSAndroid Build Coastguard Worker  *  Compile with -DLARGE_FILE to enable file sizes > 2 GB.
99*49cdfc7eSAndroid Build Coastguard Worker  */
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker #define MAXLOOPS	500	/* max pages for map children to write */
102*49cdfc7eSAndroid Build Coastguard Worker #define GROWSIZE	20	/* # bytes to write per write call */
103*49cdfc7eSAndroid Build Coastguard Worker #define SLEEPTIME	1	/* # secs to sleep between writes */
104*49cdfc7eSAndroid Build Coastguard Worker #define	FILESIZE	4096	/* initial filesize set up by parent */
105*49cdfc7eSAndroid Build Coastguard Worker #ifdef roundup
106*49cdfc7eSAndroid Build Coastguard Worker #undef roundup
107*49cdfc7eSAndroid Build Coastguard Worker #endif
108*49cdfc7eSAndroid Build Coastguard Worker #define roundup(x, y)	((((x)+((y)-1))/(y))*(y))
109*49cdfc7eSAndroid Build Coastguard Worker 
110*49cdfc7eSAndroid Build Coastguard Worker #define SIZE_MAX UINT_MAX
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker extern time_t time(time_t *);
113*49cdfc7eSAndroid Build Coastguard Worker extern char *ctime(const time_t *);
114*49cdfc7eSAndroid Build Coastguard Worker extern void *malloc(size_t);
115*49cdfc7eSAndroid Build Coastguard Worker extern void exit(int);
116*49cdfc7eSAndroid Build Coastguard Worker extern long lrand48(void);
117*49cdfc7eSAndroid Build Coastguard Worker extern void srand(unsigned);
118*49cdfc7eSAndroid Build Coastguard Worker extern void srand48(long);
119*49cdfc7eSAndroid Build Coastguard Worker extern int rand(void);
120*49cdfc7eSAndroid Build Coastguard Worker extern int atoi(const char *);
121*49cdfc7eSAndroid Build Coastguard Worker 
122*49cdfc7eSAndroid Build Coastguard Worker char *usage =
123*49cdfc7eSAndroid Build Coastguard Worker     "-p nprocs [-t minutes -w nbytes -s secs -f fsize -S sparseoffset -r -o -m -l -d]";
124*49cdfc7eSAndroid Build Coastguard Worker 
125*49cdfc7eSAndroid Build Coastguard Worker typedef unsigned char uchar_t;	//Ananda 12/17/02
126*49cdfc7eSAndroid Build Coastguard Worker 
127*49cdfc7eSAndroid Build Coastguard Worker void child_mapper(char *file, unsigned procno, unsigned nprocs);
128*49cdfc7eSAndroid Build Coastguard Worker void child_writer(char *file, uchar_t * buf);
129*49cdfc7eSAndroid Build Coastguard Worker int fileokay(char *file, uchar_t * expbuf);
130*49cdfc7eSAndroid Build Coastguard Worker unsigned int initrand(void);
131*49cdfc7eSAndroid Build Coastguard Worker void finish(int sig);
132*49cdfc7eSAndroid Build Coastguard Worker void clean_up_file(int sig);
133*49cdfc7eSAndroid Build Coastguard Worker int finished = 0;
134*49cdfc7eSAndroid Build Coastguard Worker int leavefile = 0;
135*49cdfc7eSAndroid Build Coastguard Worker 
136*49cdfc7eSAndroid Build Coastguard Worker int debug = 0;
137*49cdfc7eSAndroid Build Coastguard Worker int growsize = GROWSIZE;
138*49cdfc7eSAndroid Build Coastguard Worker int sleeptime = SLEEPTIME;
139*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
140*49cdfc7eSAndroid Build Coastguard Worker off64_t filesize = FILESIZE;
141*49cdfc7eSAndroid Build Coastguard Worker off64_t sparseoffset = 0;
142*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
143*49cdfc7eSAndroid Build Coastguard Worker off_t filesize = FILESIZE;
144*49cdfc7eSAndroid Build Coastguard Worker off_t sparseoffset = 0;
145*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
146*49cdfc7eSAndroid Build Coastguard Worker unsigned randloops = 0;
147*49cdfc7eSAndroid Build Coastguard Worker unsigned dosync = 0;
148*49cdfc7eSAndroid Build Coastguard Worker unsigned do_offset = 0;
149*49cdfc7eSAndroid Build Coastguard Worker unsigned pattern = 0;
150*49cdfc7eSAndroid Build Coastguard Worker static const char *filename = "mmapstress10.out";
151*49cdfc7eSAndroid Build Coastguard Worker 
152*49cdfc7eSAndroid Build Coastguard Worker void clean_mapper(int sig);
153*49cdfc7eSAndroid Build Coastguard Worker void clean_writer(int sig);
154*49cdfc7eSAndroid Build Coastguard Worker 
155*49cdfc7eSAndroid Build Coastguard Worker int fd_mapper = 0;
156*49cdfc7eSAndroid Build Coastguard Worker caddr_t maddr_mapper;
157*49cdfc7eSAndroid Build Coastguard Worker size_t mapsize_mapper;
158*49cdfc7eSAndroid Build Coastguard Worker 
159*49cdfc7eSAndroid Build Coastguard Worker int fd_writer = 0;
160*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char * argv[])161*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
162*49cdfc7eSAndroid Build Coastguard Worker {
163*49cdfc7eSAndroid Build Coastguard Worker 	char *progname;
164*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
165*49cdfc7eSAndroid Build Coastguard Worker 	int c;
166*49cdfc7eSAndroid Build Coastguard Worker 	extern char *optarg;
167*49cdfc7eSAndroid Build Coastguard Worker 	unsigned nprocs = 0;
168*49cdfc7eSAndroid Build Coastguard Worker 	unsigned procno;
169*49cdfc7eSAndroid Build Coastguard Worker 	pid_t *pidarray = NULL;
170*49cdfc7eSAndroid Build Coastguard Worker 	pid_t pid;
171*49cdfc7eSAndroid Build Coastguard Worker 	pid_t wr_pid = 0;
172*49cdfc7eSAndroid Build Coastguard Worker 	uchar_t *buf = NULL;
173*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int seed;
174*49cdfc7eSAndroid Build Coastguard Worker 	int pagesize = sysconf(_SC_PAGE_SIZE);
175*49cdfc7eSAndroid Build Coastguard Worker 	float alarmtime = 0;
176*49cdfc7eSAndroid Build Coastguard Worker 	struct sigaction sa;
177*49cdfc7eSAndroid Build Coastguard Worker 	unsigned i;
178*49cdfc7eSAndroid Build Coastguard Worker 	int write_cnt;
179*49cdfc7eSAndroid Build Coastguard Worker 	uchar_t data;
180*49cdfc7eSAndroid Build Coastguard Worker 	int no_prob = 0;
181*49cdfc7eSAndroid Build Coastguard Worker 	int wait_stat;
182*49cdfc7eSAndroid Build Coastguard Worker 	time_t t;
183*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
184*49cdfc7eSAndroid Build Coastguard Worker 	off64_t bytes_left;
185*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
186*49cdfc7eSAndroid Build Coastguard Worker 	off_t bytes_left;
187*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
188*49cdfc7eSAndroid Build Coastguard Worker 
189*49cdfc7eSAndroid Build Coastguard Worker 	progname = *argv;
190*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
191*49cdfc7eSAndroid Build Coastguard Worker 	if (argc < 2) {
192*49cdfc7eSAndroid Build Coastguard Worker 		(void)fprintf(stderr, "usage: %s %s\n", progname, usage);
193*49cdfc7eSAndroid Build Coastguard Worker 		exit(1);
194*49cdfc7eSAndroid Build Coastguard Worker 	}
195*49cdfc7eSAndroid Build Coastguard Worker 
196*49cdfc7eSAndroid Build Coastguard Worker 	while ((c = getopt(argc, argv, "S:omdlrf:p:t:w:s:")) != -1) {
197*49cdfc7eSAndroid Build Coastguard Worker 		switch (c) {
198*49cdfc7eSAndroid Build Coastguard Worker 		case 'd':
199*49cdfc7eSAndroid Build Coastguard Worker 			debug = 1;
200*49cdfc7eSAndroid Build Coastguard Worker 			break;
201*49cdfc7eSAndroid Build Coastguard Worker 		case 't':
202*49cdfc7eSAndroid Build Coastguard Worker 			alarmtime = atof(optarg) * 60;
203*49cdfc7eSAndroid Build Coastguard Worker 			break;
204*49cdfc7eSAndroid Build Coastguard Worker 		case 'p':
205*49cdfc7eSAndroid Build Coastguard Worker 			nprocs = atoi(optarg);
206*49cdfc7eSAndroid Build Coastguard Worker 			break;
207*49cdfc7eSAndroid Build Coastguard Worker 		case 'l':
208*49cdfc7eSAndroid Build Coastguard Worker 			leavefile = 1;
209*49cdfc7eSAndroid Build Coastguard Worker 			break;
210*49cdfc7eSAndroid Build Coastguard Worker 		case 's':
211*49cdfc7eSAndroid Build Coastguard Worker 			sleeptime = atoi(optarg);
212*49cdfc7eSAndroid Build Coastguard Worker 			if (sleeptime < 0) {
213*49cdfc7eSAndroid Build Coastguard Worker 				(void)fprintf(stderr, "error: negative "
214*49cdfc7eSAndroid Build Coastguard Worker 					      "sleeptime\n");
215*49cdfc7eSAndroid Build Coastguard Worker 				anyfail();
216*49cdfc7eSAndroid Build Coastguard Worker 			}
217*49cdfc7eSAndroid Build Coastguard Worker 			break;
218*49cdfc7eSAndroid Build Coastguard Worker 		case 'w':
219*49cdfc7eSAndroid Build Coastguard Worker 			growsize = atoi(optarg);
220*49cdfc7eSAndroid Build Coastguard Worker 			if (growsize < 0) {
221*49cdfc7eSAndroid Build Coastguard Worker 				(void)fprintf(stderr, "error: negative write "
222*49cdfc7eSAndroid Build Coastguard Worker 					      "size\n");
223*49cdfc7eSAndroid Build Coastguard Worker 				anyfail();
224*49cdfc7eSAndroid Build Coastguard Worker 			}
225*49cdfc7eSAndroid Build Coastguard Worker 			break;
226*49cdfc7eSAndroid Build Coastguard Worker 		case 'f':
227*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
228*49cdfc7eSAndroid Build Coastguard Worker 			filesize = atoll(optarg);
229*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
230*49cdfc7eSAndroid Build Coastguard Worker 			filesize = atoi(optarg);
231*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
232*49cdfc7eSAndroid Build Coastguard Worker 			if (filesize < 0) {
233*49cdfc7eSAndroid Build Coastguard Worker 				(void)fprintf(stderr, "error: negative "
234*49cdfc7eSAndroid Build Coastguard Worker 					      "filesize\n");
235*49cdfc7eSAndroid Build Coastguard Worker 				anyfail();
236*49cdfc7eSAndroid Build Coastguard Worker 			}
237*49cdfc7eSAndroid Build Coastguard Worker 			break;
238*49cdfc7eSAndroid Build Coastguard Worker 		case 'r':
239*49cdfc7eSAndroid Build Coastguard Worker 			randloops = 1;
240*49cdfc7eSAndroid Build Coastguard Worker 			break;
241*49cdfc7eSAndroid Build Coastguard Worker 		case 'm':
242*49cdfc7eSAndroid Build Coastguard Worker 			dosync = 1;
243*49cdfc7eSAndroid Build Coastguard Worker 			break;
244*49cdfc7eSAndroid Build Coastguard Worker 		case 'o':
245*49cdfc7eSAndroid Build Coastguard Worker 			do_offset = 1;
246*49cdfc7eSAndroid Build Coastguard Worker 			break;
247*49cdfc7eSAndroid Build Coastguard Worker 		case 'S':
248*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
249*49cdfc7eSAndroid Build Coastguard Worker 			sparseoffset = atoll(optarg);
250*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
251*49cdfc7eSAndroid Build Coastguard Worker 			sparseoffset = atoi(optarg);
252*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
253*49cdfc7eSAndroid Build Coastguard Worker 			if (sparseoffset % pagesize != 0) {
254*49cdfc7eSAndroid Build Coastguard Worker 				fprintf(stderr,
255*49cdfc7eSAndroid Build Coastguard Worker 					"sparseoffset must be pagesize multiple\n");
256*49cdfc7eSAndroid Build Coastguard Worker 				anyfail();
257*49cdfc7eSAndroid Build Coastguard Worker 			}
258*49cdfc7eSAndroid Build Coastguard Worker 			break;
259*49cdfc7eSAndroid Build Coastguard Worker 		default:
260*49cdfc7eSAndroid Build Coastguard Worker 			(void)fprintf(stderr, "usage: %s %s\n", progname,
261*49cdfc7eSAndroid Build Coastguard Worker 				      usage);
262*49cdfc7eSAndroid Build Coastguard Worker 			anyfail();
263*49cdfc7eSAndroid Build Coastguard Worker 		}
264*49cdfc7eSAndroid Build Coastguard Worker 	}
265*49cdfc7eSAndroid Build Coastguard Worker 
266*49cdfc7eSAndroid Build Coastguard Worker 	if (nprocs > 255) {
267*49cdfc7eSAndroid Build Coastguard Worker 		(void)fprintf(stderr, "invalid nprocs %d - (range 0-255)\n",
268*49cdfc7eSAndroid Build Coastguard Worker 			      nprocs);
269*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
270*49cdfc7eSAndroid Build Coastguard Worker 	}
271*49cdfc7eSAndroid Build Coastguard Worker 	(void)time(&t);
272*49cdfc7eSAndroid Build Coastguard Worker 
273*49cdfc7eSAndroid Build Coastguard Worker 	seed = initrand();
274*49cdfc7eSAndroid Build Coastguard Worker 	pattern = seed & 0xff;
275*49cdfc7eSAndroid Build Coastguard Worker 
276*49cdfc7eSAndroid Build Coastguard Worker 	if (debug) {
277*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
278*49cdfc7eSAndroid Build Coastguard Worker 		(void)printf("creating file <%s> with %Ld bytes, pattern %d\n",
279*49cdfc7eSAndroid Build Coastguard Worker 			     filename, filesize, pattern);
280*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
281*49cdfc7eSAndroid Build Coastguard Worker 		(void)printf("creating file <%s> with %ld bytes, pattern %d\n",
282*49cdfc7eSAndroid Build Coastguard Worker 			     filename, filesize, pattern);
283*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
284*49cdfc7eSAndroid Build Coastguard Worker 		if (alarmtime)
285*49cdfc7eSAndroid Build Coastguard Worker 			(void)printf("running for %f minutes\n",
286*49cdfc7eSAndroid Build Coastguard Worker 				     alarmtime / 60);
287*49cdfc7eSAndroid Build Coastguard Worker 		else
288*49cdfc7eSAndroid Build Coastguard Worker 			(void)printf("running with no time limit\n");
289*49cdfc7eSAndroid Build Coastguard Worker 	}
290*49cdfc7eSAndroid Build Coastguard Worker 
291*49cdfc7eSAndroid Build Coastguard Worker 	/*
292*49cdfc7eSAndroid Build Coastguard Worker 	 *  Plan for death by signal.  User may have specified
293*49cdfc7eSAndroid Build Coastguard Worker 	 *  a time limit, in which case set an alarm and catch SIGALRM.
294*49cdfc7eSAndroid Build Coastguard Worker 	 *  Also catch and cleanup with SIGINT, SIGQUIT, and SIGTERM.
295*49cdfc7eSAndroid Build Coastguard Worker 	 */
296*49cdfc7eSAndroid Build Coastguard Worker 	sa.sa_handler = finish;
297*49cdfc7eSAndroid Build Coastguard Worker 	sa.sa_flags = 0;
298*49cdfc7eSAndroid Build Coastguard Worker 	if (sigemptyset(&sa.sa_mask)) {
299*49cdfc7eSAndroid Build Coastguard Worker 		perror("sigempty error");
300*49cdfc7eSAndroid Build Coastguard Worker 		goto cleanup;
301*49cdfc7eSAndroid Build Coastguard Worker 	}
302*49cdfc7eSAndroid Build Coastguard Worker 
303*49cdfc7eSAndroid Build Coastguard Worker 	if (sigaction(SIGINT, &sa, 0) == -1) {
304*49cdfc7eSAndroid Build Coastguard Worker 		perror("sigaction error SIGINT");
305*49cdfc7eSAndroid Build Coastguard Worker 		goto cleanup;
306*49cdfc7eSAndroid Build Coastguard Worker 	}
307*49cdfc7eSAndroid Build Coastguard Worker 	if (alarmtime) {
308*49cdfc7eSAndroid Build Coastguard Worker 		if (sigaction(SIGALRM, &sa, 0) == -1) {
309*49cdfc7eSAndroid Build Coastguard Worker 			perror("sigaction error");
310*49cdfc7eSAndroid Build Coastguard Worker 			goto cleanup;
311*49cdfc7eSAndroid Build Coastguard Worker 		}
312*49cdfc7eSAndroid Build Coastguard Worker 		(void)alarm(alarmtime);
313*49cdfc7eSAndroid Build Coastguard Worker 	}
314*49cdfc7eSAndroid Build Coastguard Worker 	/* If we get a SIGQUIT or SIGTERM, clean up and exit immediately. */
315*49cdfc7eSAndroid Build Coastguard Worker 	sa.sa_handler = clean_up_file;
316*49cdfc7eSAndroid Build Coastguard Worker 	if (sigaction(SIGQUIT, &sa, 0) == -1) {
317*49cdfc7eSAndroid Build Coastguard Worker 		perror("sigaction error SIGQUIT");
318*49cdfc7eSAndroid Build Coastguard Worker 		goto cleanup;
319*49cdfc7eSAndroid Build Coastguard Worker 	}
320*49cdfc7eSAndroid Build Coastguard Worker 	if (sigaction(SIGTERM, &sa, 0) == -1) {
321*49cdfc7eSAndroid Build Coastguard Worker 		perror("sigaction error SIGTERM");
322*49cdfc7eSAndroid Build Coastguard Worker 		goto cleanup;
323*49cdfc7eSAndroid Build Coastguard Worker 	}
324*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
325*49cdfc7eSAndroid Build Coastguard Worker 	if ((fd = open64(filename, O_CREAT | O_TRUNC | O_RDWR, 0664)) == -1) {
326*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
327*49cdfc7eSAndroid Build Coastguard Worker 	if ((fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, 0664)) == -1) {
328*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
329*49cdfc7eSAndroid Build Coastguard Worker 		perror("open error");
330*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
331*49cdfc7eSAndroid Build Coastguard Worker 	}
332*49cdfc7eSAndroid Build Coastguard Worker 
333*49cdfc7eSAndroid Build Coastguard Worker 	if ((buf = malloc(pagesize + growsize)) == NULL
334*49cdfc7eSAndroid Build Coastguard Worker 	    || (pidarray = malloc(nprocs * sizeof(pid_t))) == NULL) {
335*49cdfc7eSAndroid Build Coastguard Worker 		perror("malloc error");
336*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
337*49cdfc7eSAndroid Build Coastguard Worker 	}
338*49cdfc7eSAndroid Build Coastguard Worker 
339*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < nprocs; i++)
340*49cdfc7eSAndroid Build Coastguard Worker 		*(pidarray + i) = 0;
341*49cdfc7eSAndroid Build Coastguard Worker 
342*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0, data = 0; i < pagesize; i++) {
343*49cdfc7eSAndroid Build Coastguard Worker 		*(buf + i) = (data + pattern) & 0xff;
344*49cdfc7eSAndroid Build Coastguard Worker 		if (++data == nprocs)
345*49cdfc7eSAndroid Build Coastguard Worker 			data = 0;
346*49cdfc7eSAndroid Build Coastguard Worker 	}
347*49cdfc7eSAndroid Build Coastguard Worker 	for (data = 0; i < pagesize + growsize; i++) {
348*49cdfc7eSAndroid Build Coastguard Worker 		*(buf + i) = (data + pattern) & 0xff;
349*49cdfc7eSAndroid Build Coastguard Worker 		if (++data == nprocs)
350*49cdfc7eSAndroid Build Coastguard Worker 			data = 0;
351*49cdfc7eSAndroid Build Coastguard Worker 	}
352*49cdfc7eSAndroid Build Coastguard Worker 
353*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
354*49cdfc7eSAndroid Build Coastguard Worker 	if (lseek64(fd, sparseoffset, SEEK_SET) < 0) {
355*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
356*49cdfc7eSAndroid Build Coastguard Worker 	if (lseek(fd, sparseoffset, SEEK_SET) < 0) {
357*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
358*49cdfc7eSAndroid Build Coastguard Worker 		perror("lseek");
359*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
360*49cdfc7eSAndroid Build Coastguard Worker 	}
361*49cdfc7eSAndroid Build Coastguard Worker 
362*49cdfc7eSAndroid Build Coastguard Worker 	for (bytes_left = filesize; bytes_left; bytes_left -= c) {
363*49cdfc7eSAndroid Build Coastguard Worker 		write_cnt = MIN(pagesize, (int)bytes_left);
364*49cdfc7eSAndroid Build Coastguard Worker 		if ((c = write(fd, (char *)buf, write_cnt)) != write_cnt) {
365*49cdfc7eSAndroid Build Coastguard Worker 			if (c == -1) {
366*49cdfc7eSAndroid Build Coastguard Worker 				perror("write error");
367*49cdfc7eSAndroid Build Coastguard Worker 			} else {
368*49cdfc7eSAndroid Build Coastguard Worker 				(void)fprintf(stderr, "write: wrote %d of %d "
369*49cdfc7eSAndroid Build Coastguard Worker 					      "bytes\n", c, write_cnt);
370*49cdfc7eSAndroid Build Coastguard Worker 			}
371*49cdfc7eSAndroid Build Coastguard Worker 			(void)close(fd);
372*49cdfc7eSAndroid Build Coastguard Worker 			(void)unlink(filename);
373*49cdfc7eSAndroid Build Coastguard Worker 			anyfail();
374*49cdfc7eSAndroid Build Coastguard Worker 		}
375*49cdfc7eSAndroid Build Coastguard Worker 	}
376*49cdfc7eSAndroid Build Coastguard Worker 
377*49cdfc7eSAndroid Build Coastguard Worker 	(void)close(fd);
378*49cdfc7eSAndroid Build Coastguard Worker 
379*49cdfc7eSAndroid Build Coastguard Worker 	/*
380*49cdfc7eSAndroid Build Coastguard Worker 	 *  Fork off mmap children.
381*49cdfc7eSAndroid Build Coastguard Worker 	 */
382*49cdfc7eSAndroid Build Coastguard Worker 	for (procno = 0; procno < nprocs; procno++) {
383*49cdfc7eSAndroid Build Coastguard Worker 		switch (pid = fork()) {
384*49cdfc7eSAndroid Build Coastguard Worker 
385*49cdfc7eSAndroid Build Coastguard Worker 		case -1:
386*49cdfc7eSAndroid Build Coastguard Worker 			perror("fork error");
387*49cdfc7eSAndroid Build Coastguard Worker 			goto cleanup;
388*49cdfc7eSAndroid Build Coastguard Worker 
389*49cdfc7eSAndroid Build Coastguard Worker 		case 0:
390*49cdfc7eSAndroid Build Coastguard Worker 			child_mapper(filename, procno, nprocs);
391*49cdfc7eSAndroid Build Coastguard Worker 			exit(0);
392*49cdfc7eSAndroid Build Coastguard Worker 
393*49cdfc7eSAndroid Build Coastguard Worker 		default:
394*49cdfc7eSAndroid Build Coastguard Worker 			pidarray[procno] = pid;
395*49cdfc7eSAndroid Build Coastguard Worker 		}
396*49cdfc7eSAndroid Build Coastguard Worker 	}
397*49cdfc7eSAndroid Build Coastguard Worker 
398*49cdfc7eSAndroid Build Coastguard Worker 	/*
399*49cdfc7eSAndroid Build Coastguard Worker 	 *  Now fork off an additional process to continually
400*49cdfc7eSAndroid Build Coastguard Worker 	 *  write to (and grow) the file.
401*49cdfc7eSAndroid Build Coastguard Worker 	 */
402*49cdfc7eSAndroid Build Coastguard Worker 	if ((wr_pid = fork()) == -1) {
403*49cdfc7eSAndroid Build Coastguard Worker 		perror("fork error");
404*49cdfc7eSAndroid Build Coastguard Worker 		goto cleanup;
405*49cdfc7eSAndroid Build Coastguard Worker 	} else if (wr_pid == 0) {	/* child */
406*49cdfc7eSAndroid Build Coastguard Worker 		child_writer(filename, buf);
407*49cdfc7eSAndroid Build Coastguard Worker 		exit(0);
408*49cdfc7eSAndroid Build Coastguard Worker 	}
409*49cdfc7eSAndroid Build Coastguard Worker 
410*49cdfc7eSAndroid Build Coastguard Worker 	/*
411*49cdfc7eSAndroid Build Coastguard Worker 	 *  Now wait for children and refork them as needed.
412*49cdfc7eSAndroid Build Coastguard Worker 	 */
413*49cdfc7eSAndroid Build Coastguard Worker 
414*49cdfc7eSAndroid Build Coastguard Worker 	while (!finished) {
415*49cdfc7eSAndroid Build Coastguard Worker 		pid = wait(&wait_stat);
416*49cdfc7eSAndroid Build Coastguard Worker 		/*
417*49cdfc7eSAndroid Build Coastguard Worker 		 *  Block signals while processing child exit.
418*49cdfc7eSAndroid Build Coastguard Worker 		 */
419*49cdfc7eSAndroid Build Coastguard Worker 
420*49cdfc7eSAndroid Build Coastguard Worker 		if (sighold(SIGALRM) || sighold(SIGINT)) {
421*49cdfc7eSAndroid Build Coastguard Worker 			perror("sighold error");
422*49cdfc7eSAndroid Build Coastguard Worker 			goto cleanup;
423*49cdfc7eSAndroid Build Coastguard Worker 		}
424*49cdfc7eSAndroid Build Coastguard Worker 
425*49cdfc7eSAndroid Build Coastguard Worker 		if (pid != -1) {
426*49cdfc7eSAndroid Build Coastguard Worker 			/*
427*49cdfc7eSAndroid Build Coastguard Worker 			 *  Check exit status, then refork with the
428*49cdfc7eSAndroid Build Coastguard Worker 			 *  appropriate procno.
429*49cdfc7eSAndroid Build Coastguard Worker 			 */
430*49cdfc7eSAndroid Build Coastguard Worker 			if (!WIFEXITED(wait_stat)
431*49cdfc7eSAndroid Build Coastguard Worker 			    || WEXITSTATUS(wait_stat) != 0) {
432*49cdfc7eSAndroid Build Coastguard Worker 				(void)fprintf(stderr, "child exit with err "
433*49cdfc7eSAndroid Build Coastguard Worker 					      "<x%x>\n", wait_stat);
434*49cdfc7eSAndroid Build Coastguard Worker 				goto cleanup;
435*49cdfc7eSAndroid Build Coastguard Worker 			}
436*49cdfc7eSAndroid Build Coastguard Worker 			for (i = 0; i < nprocs; i++)
437*49cdfc7eSAndroid Build Coastguard Worker 				if (pid == pidarray[i])
438*49cdfc7eSAndroid Build Coastguard Worker 					break;
439*49cdfc7eSAndroid Build Coastguard Worker 			if (i == nprocs) {
440*49cdfc7eSAndroid Build Coastguard Worker 				if (pid == wr_pid) {
441*49cdfc7eSAndroid Build Coastguard Worker 					(void)fprintf(stderr,
442*49cdfc7eSAndroid Build Coastguard Worker 						      "writer child unexpected exit <x%x>\n",
443*49cdfc7eSAndroid Build Coastguard Worker 						      wait_stat);
444*49cdfc7eSAndroid Build Coastguard Worker 					wr_pid = 0;
445*49cdfc7eSAndroid Build Coastguard Worker 				} else
446*49cdfc7eSAndroid Build Coastguard Worker 					(void)fprintf(stderr, "unknown child "
447*49cdfc7eSAndroid Build Coastguard Worker 						      "pid %d, <x%x>\n",
448*49cdfc7eSAndroid Build Coastguard Worker 						      pid, wait_stat);
449*49cdfc7eSAndroid Build Coastguard Worker 				goto cleanup;
450*49cdfc7eSAndroid Build Coastguard Worker 			}
451*49cdfc7eSAndroid Build Coastguard Worker 
452*49cdfc7eSAndroid Build Coastguard Worker 			if ((pid = fork()) == -1) {
453*49cdfc7eSAndroid Build Coastguard Worker 				perror("fork error");
454*49cdfc7eSAndroid Build Coastguard Worker 				pidarray[i] = 0;
455*49cdfc7eSAndroid Build Coastguard Worker 				goto cleanup;
456*49cdfc7eSAndroid Build Coastguard Worker 			} else if (pid == 0) {	/* child */
457*49cdfc7eSAndroid Build Coastguard Worker 				child_mapper(filename, i, nprocs);
458*49cdfc7eSAndroid Build Coastguard Worker 				exit(0);
459*49cdfc7eSAndroid Build Coastguard Worker 			} else
460*49cdfc7eSAndroid Build Coastguard Worker 				pidarray[i] = pid;
461*49cdfc7eSAndroid Build Coastguard Worker 		} else {
462*49cdfc7eSAndroid Build Coastguard Worker 			/*
463*49cdfc7eSAndroid Build Coastguard Worker 			 *  wait returned an error.  If EINTR, then
464*49cdfc7eSAndroid Build Coastguard Worker 			 *  normal finish, else it's an unexpected
465*49cdfc7eSAndroid Build Coastguard Worker 			 *  error...
466*49cdfc7eSAndroid Build Coastguard Worker 			 */
467*49cdfc7eSAndroid Build Coastguard Worker 			if (errno != EINTR || !finished) {
468*49cdfc7eSAndroid Build Coastguard Worker 				perror("unexpected wait error");
469*49cdfc7eSAndroid Build Coastguard Worker 				goto cleanup;
470*49cdfc7eSAndroid Build Coastguard Worker 			}
471*49cdfc7eSAndroid Build Coastguard Worker 		}
472*49cdfc7eSAndroid Build Coastguard Worker 		if (sigrelse(SIGALRM) || sigrelse(SIGINT)) {
473*49cdfc7eSAndroid Build Coastguard Worker 			perror("sigrelse error");
474*49cdfc7eSAndroid Build Coastguard Worker 			goto cleanup;
475*49cdfc7eSAndroid Build Coastguard Worker 		}
476*49cdfc7eSAndroid Build Coastguard Worker 	}
477*49cdfc7eSAndroid Build Coastguard Worker 
478*49cdfc7eSAndroid Build Coastguard Worker 	/*
479*49cdfc7eSAndroid Build Coastguard Worker 	 *  Finished!  Check the file for sanity, then kill all
480*49cdfc7eSAndroid Build Coastguard Worker 	 *  the children and done!.
481*49cdfc7eSAndroid Build Coastguard Worker 	 */
482*49cdfc7eSAndroid Build Coastguard Worker 
483*49cdfc7eSAndroid Build Coastguard Worker 	(void)alarm(0);
484*49cdfc7eSAndroid Build Coastguard Worker 	no_prob = 1;
485*49cdfc7eSAndroid Build Coastguard Worker 
486*49cdfc7eSAndroid Build Coastguard Worker cleanup:
487*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < nprocs; i++)
488*49cdfc7eSAndroid Build Coastguard Worker 		(void)kill(pidarray[i], SIGUSR1);
489*49cdfc7eSAndroid Build Coastguard Worker 	(void)kill(wr_pid, SIGUSR1);
490*49cdfc7eSAndroid Build Coastguard Worker 
491*49cdfc7eSAndroid Build Coastguard Worker 	while (wait(&wait_stat) != -1 || errno != ECHILD)
492*49cdfc7eSAndroid Build Coastguard Worker 		continue;
493*49cdfc7eSAndroid Build Coastguard Worker 
494*49cdfc7eSAndroid Build Coastguard Worker 	if (no_prob) {		/* only check file if no errors */
495*49cdfc7eSAndroid Build Coastguard Worker 		if (!fileokay(filename, buf)) {
496*49cdfc7eSAndroid Build Coastguard Worker 			(void)fprintf(stderr, "file data incorrect!\n");
497*49cdfc7eSAndroid Build Coastguard Worker 			(void)printf("  leaving file <%s>\n", filename);
498*49cdfc7eSAndroid Build Coastguard Worker 			anyfail();
499*49cdfc7eSAndroid Build Coastguard Worker 
500*49cdfc7eSAndroid Build Coastguard Worker 		} else {
501*49cdfc7eSAndroid Build Coastguard Worker 			(void)printf("file data okay\n");
502*49cdfc7eSAndroid Build Coastguard Worker 			if (!leavefile)
503*49cdfc7eSAndroid Build Coastguard Worker 				(void)unlink(filename);
504*49cdfc7eSAndroid Build Coastguard Worker 		}
505*49cdfc7eSAndroid Build Coastguard Worker 	} else
506*49cdfc7eSAndroid Build Coastguard Worker 		(void)printf("  leaving file <%s>\n", filename);
507*49cdfc7eSAndroid Build Coastguard Worker 
508*49cdfc7eSAndroid Build Coastguard Worker 	(void)time(&t);
509*49cdfc7eSAndroid Build Coastguard Worker //      (void)printf("%s: Finished %s", argv[0], ctime(&t)); LTP Port
510*49cdfc7eSAndroid Build Coastguard Worker 	ok_exit();
511*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
512*49cdfc7eSAndroid Build Coastguard Worker }
513*49cdfc7eSAndroid Build Coastguard Worker 
514*49cdfc7eSAndroid Build Coastguard Worker /*
515*49cdfc7eSAndroid Build Coastguard Worker  *  Child process that reads/writes map.  The child stats the file
516*49cdfc7eSAndroid Build Coastguard Worker  *  to determine the size, maps the size of the file, then reads/writes
517*49cdfc7eSAndroid Build Coastguard Worker  *  its own locations on random pages of the map (its locations being
518*49cdfc7eSAndroid Build Coastguard Worker  *  determined based on nprocs & procno).  After a specific number of
519*49cdfc7eSAndroid Build Coastguard Worker  *  iterations, it exits.
520*49cdfc7eSAndroid Build Coastguard Worker  */
521*49cdfc7eSAndroid Build Coastguard Worker void child_mapper(char *file, unsigned procno, unsigned nprocs)
522*49cdfc7eSAndroid Build Coastguard Worker {
523*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
524*49cdfc7eSAndroid Build Coastguard Worker 	struct stat64 statbuf;
525*49cdfc7eSAndroid Build Coastguard Worker 	off64_t filesize;
526*49cdfc7eSAndroid Build Coastguard Worker 	off64_t offset;
527*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
528*49cdfc7eSAndroid Build Coastguard Worker 	struct stat statbuf;
529*49cdfc7eSAndroid Build Coastguard Worker 	off_t filesize;
530*49cdfc7eSAndroid Build Coastguard Worker 	off_t offset;
531*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
532*49cdfc7eSAndroid Build Coastguard Worker 	size_t validsize;
533*49cdfc7eSAndroid Build Coastguard Worker 	caddr_t paddr;
534*49cdfc7eSAndroid Build Coastguard Worker 	int pagesize = sysconf(_SC_PAGE_SIZE);
535*49cdfc7eSAndroid Build Coastguard Worker 	unsigned randpage;
536*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int seed;
537*49cdfc7eSAndroid Build Coastguard Worker 	unsigned loopcnt;
538*49cdfc7eSAndroid Build Coastguard Worker 	unsigned nloops;
539*49cdfc7eSAndroid Build Coastguard Worker 	unsigned mappages;
540*49cdfc7eSAndroid Build Coastguard Worker 	unsigned mapflags;
541*49cdfc7eSAndroid Build Coastguard Worker 	unsigned i;
542*49cdfc7eSAndroid Build Coastguard Worker 	struct sigaction sa_mapper;
543*49cdfc7eSAndroid Build Coastguard Worker 
544*49cdfc7eSAndroid Build Coastguard Worker 	mapflags = MAP_SHARED;
545*49cdfc7eSAndroid Build Coastguard Worker 
546*49cdfc7eSAndroid Build Coastguard Worker 	seed = initrand();	/* initialize random seed */
547*49cdfc7eSAndroid Build Coastguard Worker 
548*49cdfc7eSAndroid Build Coastguard Worker 	sa_mapper.sa_handler = clean_mapper;
549*49cdfc7eSAndroid Build Coastguard Worker 	sa_mapper.sa_flags = 0;
550*49cdfc7eSAndroid Build Coastguard Worker 	if (sigemptyset(&sa_mapper.sa_mask)) {
551*49cdfc7eSAndroid Build Coastguard Worker 		perror("sigempty error");
552*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
553*49cdfc7eSAndroid Build Coastguard Worker 	}
554*49cdfc7eSAndroid Build Coastguard Worker 
555*49cdfc7eSAndroid Build Coastguard Worker 	if (sigaction(SIGUSR1, &sa_mapper, 0) == -1) {
556*49cdfc7eSAndroid Build Coastguard Worker 		perror("sigaction error SIGUSR1");
557*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
558*49cdfc7eSAndroid Build Coastguard Worker 	}
559*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
560*49cdfc7eSAndroid Build Coastguard Worker 	if ((fd_mapper = open64(file, O_RDWR)) == -1) {
561*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
562*49cdfc7eSAndroid Build Coastguard Worker 	if ((fd_mapper = open(file, O_RDWR)) == -1) {
563*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
564*49cdfc7eSAndroid Build Coastguard Worker 		perror("open error");
565*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
566*49cdfc7eSAndroid Build Coastguard Worker 	}
567*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
568*49cdfc7eSAndroid Build Coastguard Worker 	if (fstat64(fd_mapper, &statbuf) == -1) {
569*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
570*49cdfc7eSAndroid Build Coastguard Worker 	if (fstat(fd_mapper, &statbuf) == -1) {
571*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
572*49cdfc7eSAndroid Build Coastguard Worker 		perror("stat error");
573*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
574*49cdfc7eSAndroid Build Coastguard Worker 	}
575*49cdfc7eSAndroid Build Coastguard Worker 	filesize = statbuf.st_size;
576*49cdfc7eSAndroid Build Coastguard Worker 
577*49cdfc7eSAndroid Build Coastguard Worker 	if (statbuf.st_size - sparseoffset > SIZE_MAX) {
578*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "size_t overflow when setting up map\n");
579*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
580*49cdfc7eSAndroid Build Coastguard Worker 	}
581*49cdfc7eSAndroid Build Coastguard Worker 	mapsize_mapper = (size_t) (statbuf.st_size - sparseoffset);
582*49cdfc7eSAndroid Build Coastguard Worker 	mappages = roundup(mapsize_mapper, pagesize) / pagesize;
583*49cdfc7eSAndroid Build Coastguard Worker 	offset = sparseoffset;
584*49cdfc7eSAndroid Build Coastguard Worker 	if (do_offset) {
585*49cdfc7eSAndroid Build Coastguard Worker 		int pageoffset = lrand48() % mappages;
586*49cdfc7eSAndroid Build Coastguard Worker 		int byteoffset = pageoffset * pagesize;
587*49cdfc7eSAndroid Build Coastguard Worker 		offset += byteoffset;
588*49cdfc7eSAndroid Build Coastguard Worker 		mapsize_mapper -= byteoffset;
589*49cdfc7eSAndroid Build Coastguard Worker 		mappages -= pageoffset;
590*49cdfc7eSAndroid Build Coastguard Worker 	}
591*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
592*49cdfc7eSAndroid Build Coastguard Worker 	if ((maddr_mapper = mmap64(0, mapsize_mapper, PROT_READ | PROT_WRITE,
593*49cdfc7eSAndroid Build Coastguard Worker 				   mapflags, fd_mapper,
594*49cdfc7eSAndroid Build Coastguard Worker 				   offset)) == (caddr_t) - 1) {
595*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
596*49cdfc7eSAndroid Build Coastguard Worker 	if ((maddr_mapper = mmap(0, mapsize_mapper, PROT_READ | PROT_WRITE,
597*49cdfc7eSAndroid Build Coastguard Worker 				 mapflags, fd_mapper,
598*49cdfc7eSAndroid Build Coastguard Worker 				 offset)) == (caddr_t) - 1) {
599*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
600*49cdfc7eSAndroid Build Coastguard Worker 		perror("mmap error");
601*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
602*49cdfc7eSAndroid Build Coastguard Worker 	}
603*49cdfc7eSAndroid Build Coastguard Worker 
604*49cdfc7eSAndroid Build Coastguard Worker 	(void)close(fd_mapper);
605*49cdfc7eSAndroid Build Coastguard Worker 
606*49cdfc7eSAndroid Build Coastguard Worker 	nloops = (randloops) ? (lrand48() % MAXLOOPS) : MAXLOOPS;
607*49cdfc7eSAndroid Build Coastguard Worker 
608*49cdfc7eSAndroid Build Coastguard Worker 	if (debug) {
609*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
610*49cdfc7eSAndroid Build Coastguard Worker 		(void)printf("child %d (pid %ld): seed %d, fsize %Ld, "
611*49cdfc7eSAndroid Build Coastguard Worker 			     "mapsize %d, off %Ld, loop %d\n",
612*49cdfc7eSAndroid Build Coastguard Worker 			     procno, getpid(), seed, filesize, mapsize_mapper,
613*49cdfc7eSAndroid Build Coastguard Worker 			     offset / pagesize, nloops);
614*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
615*49cdfc7eSAndroid Build Coastguard Worker 		(void)printf("child %d (pid %d): seed %d, fsize %ld, "
616*49cdfc7eSAndroid Build Coastguard Worker 			     "mapsize %ld, off %ld, loop %d\n",
617*49cdfc7eSAndroid Build Coastguard Worker 			     procno, getpid(), seed, filesize,
618*49cdfc7eSAndroid Build Coastguard Worker 			     (long)mapsize_mapper, offset / pagesize, nloops);
619*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
620*49cdfc7eSAndroid Build Coastguard Worker 	}
621*49cdfc7eSAndroid Build Coastguard Worker 
622*49cdfc7eSAndroid Build Coastguard Worker 	/*
623*49cdfc7eSAndroid Build Coastguard Worker 	 *  Now loop read/writing random pages.
624*49cdfc7eSAndroid Build Coastguard Worker 	 */
625*49cdfc7eSAndroid Build Coastguard Worker 	for (loopcnt = 0; loopcnt < nloops; loopcnt++) {
626*49cdfc7eSAndroid Build Coastguard Worker 		randpage = lrand48() % mappages;
627*49cdfc7eSAndroid Build Coastguard Worker 		paddr = maddr_mapper + (randpage * pagesize);	/* page address */
628*49cdfc7eSAndroid Build Coastguard Worker 
629*49cdfc7eSAndroid Build Coastguard Worker 		if (randpage < mappages - 1 || !(mapsize_mapper % pagesize))
630*49cdfc7eSAndroid Build Coastguard Worker 			validsize = pagesize;
631*49cdfc7eSAndroid Build Coastguard Worker 		else
632*49cdfc7eSAndroid Build Coastguard Worker 			validsize = mapsize_mapper % pagesize;
633*49cdfc7eSAndroid Build Coastguard Worker 
634*49cdfc7eSAndroid Build Coastguard Worker 		/*
635*49cdfc7eSAndroid Build Coastguard Worker 		 * Because one child is mapping file in extend mode,
636*49cdfc7eSAndroid Build Coastguard Worker 		 * it may be padded with zeros at end.  So we can't
637*49cdfc7eSAndroid Build Coastguard Worker 		 * do an exact check -- accept known pattern OR zeros.
638*49cdfc7eSAndroid Build Coastguard Worker 		 */
639*49cdfc7eSAndroid Build Coastguard Worker 		for (i = procno; i < validsize; i += nprocs) {
640*49cdfc7eSAndroid Build Coastguard Worker 			if (*((unsigned char *)(paddr + i))
641*49cdfc7eSAndroid Build Coastguard Worker 			    != ((procno + pattern) & 0xff)
642*49cdfc7eSAndroid Build Coastguard Worker 			    && *((unsigned char *)(paddr + i)) != 0) {
643*49cdfc7eSAndroid Build Coastguard Worker 				(void)fprintf(stderr, "child %d: invalid data "
644*49cdfc7eSAndroid Build Coastguard Worker 					      "<x%x>", procno,
645*49cdfc7eSAndroid Build Coastguard Worker 					      *((unsigned char *)(paddr + i)));
646*49cdfc7eSAndroid Build Coastguard Worker 				(void)fprintf(stderr,
647*49cdfc7eSAndroid Build Coastguard Worker 					      " at pg %d off %d, exp "
648*49cdfc7eSAndroid Build Coastguard Worker 					      "<x%x>\n", randpage, i,
649*49cdfc7eSAndroid Build Coastguard Worker 					      (procno + pattern) & 0xff);
650*49cdfc7eSAndroid Build Coastguard Worker 				anyfail();
651*49cdfc7eSAndroid Build Coastguard Worker 			}
652*49cdfc7eSAndroid Build Coastguard Worker 			/*
653*49cdfc7eSAndroid Build Coastguard Worker 			 *  Now write it.
654*49cdfc7eSAndroid Build Coastguard Worker 			 */
655*49cdfc7eSAndroid Build Coastguard Worker 
656*49cdfc7eSAndroid Build Coastguard Worker 			*(paddr + i) = (procno + pattern) & 0xff;
657*49cdfc7eSAndroid Build Coastguard Worker 		}
658*49cdfc7eSAndroid Build Coastguard Worker 	}
659*49cdfc7eSAndroid Build Coastguard Worker 	if (dosync) {
660*49cdfc7eSAndroid Build Coastguard Worker 		/*
661*49cdfc7eSAndroid Build Coastguard Worker 		 * Exercise msync() as well!
662*49cdfc7eSAndroid Build Coastguard Worker 		 */
663*49cdfc7eSAndroid Build Coastguard Worker 		randpage = lrand48() % mappages;
664*49cdfc7eSAndroid Build Coastguard Worker 		paddr = maddr_mapper + (randpage * pagesize);	/* page address */
665*49cdfc7eSAndroid Build Coastguard Worker 		if (msync(paddr, (mappages - randpage) * pagesize,
666*49cdfc7eSAndroid Build Coastguard Worker 			  MS_SYNC) == -1) {
667*49cdfc7eSAndroid Build Coastguard Worker 			perror("msync error");
668*49cdfc7eSAndroid Build Coastguard Worker 			anyfail();
669*49cdfc7eSAndroid Build Coastguard Worker 		}
670*49cdfc7eSAndroid Build Coastguard Worker 	}
671*49cdfc7eSAndroid Build Coastguard Worker 	if (munmap(maddr_mapper, mapsize_mapper) == -1) {
672*49cdfc7eSAndroid Build Coastguard Worker 		perror("munmap failed");
673*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
674*49cdfc7eSAndroid Build Coastguard Worker 	}
675*49cdfc7eSAndroid Build Coastguard Worker 	exit(0);
676*49cdfc7eSAndroid Build Coastguard Worker }
677*49cdfc7eSAndroid Build Coastguard Worker 
678*49cdfc7eSAndroid Build Coastguard Worker /*
679*49cdfc7eSAndroid Build Coastguard Worker  *  child_writer
680*49cdfc7eSAndroid Build Coastguard Worker  * 	The child process that continually (and slowly!!) grows
681*49cdfc7eSAndroid Build Coastguard Worker  *	the file.  The purpose of this is to exercise the code
682*49cdfc7eSAndroid Build Coastguard Worker  *	supporting mapping of fragments.  The map children are
683*49cdfc7eSAndroid Build Coastguard Worker  *	constantly reforking and will pick up the map changes, etc.
684*49cdfc7eSAndroid Build Coastguard Worker  *	This process executes until signalled (i.e. has no exit!)
685*49cdfc7eSAndroid Build Coastguard Worker  *	unless error.
686*49cdfc7eSAndroid Build Coastguard Worker  */
687*49cdfc7eSAndroid Build Coastguard Worker void child_writer(char *file, uchar_t * buf)
688*49cdfc7eSAndroid Build Coastguard Worker {				/* buf already set up in main */
689*49cdfc7eSAndroid Build Coastguard Worker 	struct sigaction sa_writer;
690*49cdfc7eSAndroid Build Coastguard Worker 
691*49cdfc7eSAndroid Build Coastguard Worker 	sa_writer.sa_handler = clean_writer;
692*49cdfc7eSAndroid Build Coastguard Worker 	sa_writer.sa_flags = 0;
693*49cdfc7eSAndroid Build Coastguard Worker 	if (sigemptyset(&sa_writer.sa_mask)) {
694*49cdfc7eSAndroid Build Coastguard Worker 		perror("sigempty error");
695*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
696*49cdfc7eSAndroid Build Coastguard Worker 	}
697*49cdfc7eSAndroid Build Coastguard Worker 
698*49cdfc7eSAndroid Build Coastguard Worker 	if (sigaction(SIGUSR1, &sa_writer, 0) == -1) {
699*49cdfc7eSAndroid Build Coastguard Worker 		perror("sigaction error SIGUSR1");
700*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
701*49cdfc7eSAndroid Build Coastguard Worker 	}
702*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
703*49cdfc7eSAndroid Build Coastguard Worker 	struct stat64 statbuf;
704*49cdfc7eSAndroid Build Coastguard Worker 	off64_t off;
705*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
706*49cdfc7eSAndroid Build Coastguard Worker 	struct stat statbuf;
707*49cdfc7eSAndroid Build Coastguard Worker 	off_t off;
708*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
709*49cdfc7eSAndroid Build Coastguard Worker 	int pagesize = sysconf(_SC_PAGE_SIZE);
710*49cdfc7eSAndroid Build Coastguard Worker 	uchar_t *p;
711*49cdfc7eSAndroid Build Coastguard Worker 	int cnt;
712*49cdfc7eSAndroid Build Coastguard Worker 
713*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
714*49cdfc7eSAndroid Build Coastguard Worker 	if ((fd_writer = open64(file, O_RDWR)) == -1) {
715*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
716*49cdfc7eSAndroid Build Coastguard Worker 	if ((fd_writer = open(file, O_RDWR)) == -1) {
717*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
718*49cdfc7eSAndroid Build Coastguard Worker 		perror("open error");
719*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
720*49cdfc7eSAndroid Build Coastguard Worker 	}
721*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
722*49cdfc7eSAndroid Build Coastguard Worker 	if ((off = lseek64(fd_writer, 0, SEEK_END)) == -1) {
723*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
724*49cdfc7eSAndroid Build Coastguard Worker 	if ((off = lseek(fd_writer, 0, SEEK_END)) == -1) {
725*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
726*49cdfc7eSAndroid Build Coastguard Worker 		perror("lseek error");
727*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
728*49cdfc7eSAndroid Build Coastguard Worker 	}
729*49cdfc7eSAndroid Build Coastguard Worker 
730*49cdfc7eSAndroid Build Coastguard Worker 	for (;;) {
731*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
732*49cdfc7eSAndroid Build Coastguard Worker 		if (fstat64(fd_writer, &statbuf) == -1) {
733*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
734*49cdfc7eSAndroid Build Coastguard Worker 		if (fstat(fd_writer, &statbuf) == -1) {
735*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
736*49cdfc7eSAndroid Build Coastguard Worker 			perror("fstat error");
737*49cdfc7eSAndroid Build Coastguard Worker 			anyfail();
738*49cdfc7eSAndroid Build Coastguard Worker 		}
739*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
740*49cdfc7eSAndroid Build Coastguard Worker 		if (debug)
741*49cdfc7eSAndroid Build Coastguard Worker 			(void)printf("writer %d bytes at off %Ld, size %Ld\n",
742*49cdfc7eSAndroid Build Coastguard Worker 				     growsize, off, statbuf.st_size);
743*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
744*49cdfc7eSAndroid Build Coastguard Worker 		if (debug)
745*49cdfc7eSAndroid Build Coastguard Worker 			(void)printf("writer %d bytes at off %ld, size %ld\n",
746*49cdfc7eSAndroid Build Coastguard Worker 				     growsize, off, statbuf.st_size);
747*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
748*49cdfc7eSAndroid Build Coastguard Worker 
749*49cdfc7eSAndroid Build Coastguard Worker 		/*
750*49cdfc7eSAndroid Build Coastguard Worker 		 *  Write some number of bytes, then sleep some
751*49cdfc7eSAndroid Build Coastguard Worker 		 *  number of seconds...
752*49cdfc7eSAndroid Build Coastguard Worker 		 *  Need to keep track of our offset so write the
753*49cdfc7eSAndroid Build Coastguard Worker 		 *  right bytes.
754*49cdfc7eSAndroid Build Coastguard Worker 		 */
755*49cdfc7eSAndroid Build Coastguard Worker 
756*49cdfc7eSAndroid Build Coastguard Worker 		p = buf + (off % pagesize);
757*49cdfc7eSAndroid Build Coastguard Worker 
758*49cdfc7eSAndroid Build Coastguard Worker 		if ((cnt = write(fd_writer, p, growsize)) != growsize) {
759*49cdfc7eSAndroid Build Coastguard Worker 			if (cnt == -1)
760*49cdfc7eSAndroid Build Coastguard Worker 				perror("write error");
761*49cdfc7eSAndroid Build Coastguard Worker 			else
762*49cdfc7eSAndroid Build Coastguard Worker 				(void)fprintf(stderr, "wrote %d of %d bytes\n",
763*49cdfc7eSAndroid Build Coastguard Worker 					      cnt, growsize);
764*49cdfc7eSAndroid Build Coastguard Worker 			anyfail();
765*49cdfc7eSAndroid Build Coastguard Worker 		}
766*49cdfc7eSAndroid Build Coastguard Worker 
767*49cdfc7eSAndroid Build Coastguard Worker 		off += growsize;
768*49cdfc7eSAndroid Build Coastguard Worker 
769*49cdfc7eSAndroid Build Coastguard Worker 		(void)sleep(sleeptime);
770*49cdfc7eSAndroid Build Coastguard Worker 		if (dosync) {
771*49cdfc7eSAndroid Build Coastguard Worker 			if (fsync(fd_writer) == -1) {
772*49cdfc7eSAndroid Build Coastguard Worker 				perror("fsync error");
773*49cdfc7eSAndroid Build Coastguard Worker 				anyfail();
774*49cdfc7eSAndroid Build Coastguard Worker 			}
775*49cdfc7eSAndroid Build Coastguard Worker 		}
776*49cdfc7eSAndroid Build Coastguard Worker 	}
777*49cdfc7eSAndroid Build Coastguard Worker 	close(fd_writer);
778*49cdfc7eSAndroid Build Coastguard Worker }
779*49cdfc7eSAndroid Build Coastguard Worker 
780*49cdfc7eSAndroid Build Coastguard Worker /*
781*49cdfc7eSAndroid Build Coastguard Worker  *  Make sure file has all the correct data.
782*49cdfc7eSAndroid Build Coastguard Worker 
783*49cdfc7eSAndroid Build Coastguard Worker  */
784*49cdfc7eSAndroid Build Coastguard Worker int fileokay(char *file, uchar_t * expbuf)
785*49cdfc7eSAndroid Build Coastguard Worker {
786*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
787*49cdfc7eSAndroid Build Coastguard Worker 	struct stat64 statbuf;
788*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
789*49cdfc7eSAndroid Build Coastguard Worker 	struct stat statbuf;
790*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
791*49cdfc7eSAndroid Build Coastguard Worker 	size_t mapsize;
792*49cdfc7eSAndroid Build Coastguard Worker 	uchar_t *readbuf;
793*49cdfc7eSAndroid Build Coastguard Worker 	unsigned mappages;
794*49cdfc7eSAndroid Build Coastguard Worker 	unsigned pagesize = sysconf(_SC_PAGE_SIZE);
795*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
796*49cdfc7eSAndroid Build Coastguard Worker 	int cnt;
797*49cdfc7eSAndroid Build Coastguard Worker 	unsigned i, j;
798*49cdfc7eSAndroid Build Coastguard Worker 
799*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
800*49cdfc7eSAndroid Build Coastguard Worker 	if ((fd = open64(file, O_RDONLY)) == -1) {
801*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
802*49cdfc7eSAndroid Build Coastguard Worker 	if ((fd = open(file, O_RDONLY)) == -1) {
803*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
804*49cdfc7eSAndroid Build Coastguard Worker 		perror("open error");
805*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
806*49cdfc7eSAndroid Build Coastguard Worker 	}
807*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
808*49cdfc7eSAndroid Build Coastguard Worker 	if (fstat64(fd, &statbuf) == -1) {
809*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
810*49cdfc7eSAndroid Build Coastguard Worker 	if (fstat(fd, &statbuf) == -1) {
811*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
812*49cdfc7eSAndroid Build Coastguard Worker 		perror("stat error");
813*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
814*49cdfc7eSAndroid Build Coastguard Worker 	}
815*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
816*49cdfc7eSAndroid Build Coastguard Worker 	if (lseek64(fd, sparseoffset, SEEK_SET) < 0) {
817*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
818*49cdfc7eSAndroid Build Coastguard Worker 	if (lseek(fd, sparseoffset, SEEK_SET) < 0) {
819*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
820*49cdfc7eSAndroid Build Coastguard Worker 		perror("lseek");
821*49cdfc7eSAndroid Build Coastguard Worker 		exit(1);
822*49cdfc7eSAndroid Build Coastguard Worker 	}
823*49cdfc7eSAndroid Build Coastguard Worker 
824*49cdfc7eSAndroid Build Coastguard Worker 	readbuf = malloc(pagesize);
825*49cdfc7eSAndroid Build Coastguard Worker 
826*49cdfc7eSAndroid Build Coastguard Worker 	if (statbuf.st_size - sparseoffset > SIZE_MAX) {
827*49cdfc7eSAndroid Build Coastguard Worker 		fprintf(stderr, "size_t overflow when setting up map\n");
828*49cdfc7eSAndroid Build Coastguard Worker 		exit(1);
829*49cdfc7eSAndroid Build Coastguard Worker 	}
830*49cdfc7eSAndroid Build Coastguard Worker 	mapsize = (size_t) (statbuf.st_size - sparseoffset);
831*49cdfc7eSAndroid Build Coastguard Worker 	mappages = roundup(mapsize, pagesize) / pagesize;
832*49cdfc7eSAndroid Build Coastguard Worker 
833*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < mappages; i++) {
834*49cdfc7eSAndroid Build Coastguard Worker 		cnt = read(fd, (char *)readbuf, pagesize);
835*49cdfc7eSAndroid Build Coastguard Worker 		if (cnt == -1) {
836*49cdfc7eSAndroid Build Coastguard Worker 			perror("read error");
837*49cdfc7eSAndroid Build Coastguard Worker 			close(fd);
838*49cdfc7eSAndroid Build Coastguard Worker 			return 0;
839*49cdfc7eSAndroid Build Coastguard Worker 		} else if (cnt != pagesize) {
840*49cdfc7eSAndroid Build Coastguard Worker 			/*
841*49cdfc7eSAndroid Build Coastguard Worker 			 *  Okay if at last page in file...
842*49cdfc7eSAndroid Build Coastguard Worker 			 */
843*49cdfc7eSAndroid Build Coastguard Worker 			if ((i * pagesize) + cnt != mapsize) {
844*49cdfc7eSAndroid Build Coastguard Worker 				(void)fprintf(stderr, "read %d of %ld bytes\n",
845*49cdfc7eSAndroid Build Coastguard Worker 					      (i * pagesize) + cnt,
846*49cdfc7eSAndroid Build Coastguard Worker 					      (long)mapsize);
847*49cdfc7eSAndroid Build Coastguard Worker 				close(fd);
848*49cdfc7eSAndroid Build Coastguard Worker 				return 0;
849*49cdfc7eSAndroid Build Coastguard Worker 			}
850*49cdfc7eSAndroid Build Coastguard Worker 		}
851*49cdfc7eSAndroid Build Coastguard Worker 		/*
852*49cdfc7eSAndroid Build Coastguard Worker 		 *  Compare read bytes of data.
853*49cdfc7eSAndroid Build Coastguard Worker 		 *  May have zeros from map extend...
854*49cdfc7eSAndroid Build Coastguard Worker 		 */
855*49cdfc7eSAndroid Build Coastguard Worker 		for (j = 0; j < cnt; j++) {
856*49cdfc7eSAndroid Build Coastguard Worker 			if (expbuf[j] != readbuf[j] && readbuf[j] != 0) {
857*49cdfc7eSAndroid Build Coastguard Worker 				(void)fprintf(stderr,
858*49cdfc7eSAndroid Build Coastguard Worker 					      "read bad data: exp %c got %c",
859*49cdfc7eSAndroid Build Coastguard Worker 					      expbuf[j], readbuf[j]);
860*49cdfc7eSAndroid Build Coastguard Worker #ifdef LARGE_FILE
861*49cdfc7eSAndroid Build Coastguard Worker 				(void)fprintf(stderr, ", pg %d off %d, "
862*49cdfc7eSAndroid Build Coastguard Worker 					      "(fsize %Ld)\n", i, j,
863*49cdfc7eSAndroid Build Coastguard Worker 					      statbuf.st_size);
864*49cdfc7eSAndroid Build Coastguard Worker #else /* LARGE_FILE */
865*49cdfc7eSAndroid Build Coastguard Worker 				(void)fprintf(stderr, ", pg %d off %d, "
866*49cdfc7eSAndroid Build Coastguard Worker 					      "(fsize %ld)\n", i, j,
867*49cdfc7eSAndroid Build Coastguard Worker 					      statbuf.st_size);
868*49cdfc7eSAndroid Build Coastguard Worker #endif /* LARGE_FILE */
869*49cdfc7eSAndroid Build Coastguard Worker 				close(fd);
870*49cdfc7eSAndroid Build Coastguard Worker 				return 0;
871*49cdfc7eSAndroid Build Coastguard Worker 			}
872*49cdfc7eSAndroid Build Coastguard Worker 		}
873*49cdfc7eSAndroid Build Coastguard Worker 	}
874*49cdfc7eSAndroid Build Coastguard Worker 
875*49cdfc7eSAndroid Build Coastguard Worker 	close(fd);
876*49cdfc7eSAndroid Build Coastguard Worker 	return 1;
877*49cdfc7eSAndroid Build Coastguard Worker }
878*49cdfc7eSAndroid Build Coastguard Worker 
879*49cdfc7eSAndroid Build Coastguard Worker  /*ARGSUSED*/ void finish(int sig)
880*49cdfc7eSAndroid Build Coastguard Worker {
881*49cdfc7eSAndroid Build Coastguard Worker 	finished++;
882*49cdfc7eSAndroid Build Coastguard Worker 	/* finish nicely and check the file contents */
883*49cdfc7eSAndroid Build Coastguard Worker }
884*49cdfc7eSAndroid Build Coastguard Worker 
885*49cdfc7eSAndroid Build Coastguard Worker  /*ARGSUSED*/ void clean_up_file(int sig)
886*49cdfc7eSAndroid Build Coastguard Worker {
887*49cdfc7eSAndroid Build Coastguard Worker 	if (!leavefile)
888*49cdfc7eSAndroid Build Coastguard Worker 		(void)unlink(filename);
889*49cdfc7eSAndroid Build Coastguard Worker 	_exit(1);
890*49cdfc7eSAndroid Build Coastguard Worker }
891*49cdfc7eSAndroid Build Coastguard Worker 
892*49cdfc7eSAndroid Build Coastguard Worker void clean_mapper(int sig)
893*49cdfc7eSAndroid Build Coastguard Worker {
894*49cdfc7eSAndroid Build Coastguard Worker 	if (fd_mapper)
895*49cdfc7eSAndroid Build Coastguard Worker 		close(fd_mapper);
896*49cdfc7eSAndroid Build Coastguard Worker 	munmap(maddr_mapper, mapsize_mapper);
897*49cdfc7eSAndroid Build Coastguard Worker 	_exit(0);
898*49cdfc7eSAndroid Build Coastguard Worker }
899*49cdfc7eSAndroid Build Coastguard Worker 
900*49cdfc7eSAndroid Build Coastguard Worker void clean_writer(int sig)
901*49cdfc7eSAndroid Build Coastguard Worker {
902*49cdfc7eSAndroid Build Coastguard Worker 	if (fd_writer)
903*49cdfc7eSAndroid Build Coastguard Worker 		close(fd_writer);
904*49cdfc7eSAndroid Build Coastguard Worker 	_exit(0);
905*49cdfc7eSAndroid Build Coastguard Worker }
906*49cdfc7eSAndroid Build Coastguard Worker 
907*49cdfc7eSAndroid Build Coastguard Worker unsigned int initrand(void)
908*49cdfc7eSAndroid Build Coastguard Worker {
909*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int seed;
910*49cdfc7eSAndroid Build Coastguard Worker 
911*49cdfc7eSAndroid Build Coastguard Worker 	/*
912*49cdfc7eSAndroid Build Coastguard Worker 	 *  Initialize random seed...  Got this from a test written
913*49cdfc7eSAndroid Build Coastguard Worker 	 *  by scooter:
914*49cdfc7eSAndroid Build Coastguard Worker 	 *      Use srand/rand to diffuse the information from the
915*49cdfc7eSAndroid Build Coastguard Worker 	 *      time and pid.  If you start several processes, then
916*49cdfc7eSAndroid Build Coastguard Worker 	 *      the time and pid information don't provide much
917*49cdfc7eSAndroid Build Coastguard Worker 	 *      variation.
918*49cdfc7eSAndroid Build Coastguard Worker 	 */
919*49cdfc7eSAndroid Build Coastguard Worker 	srand((unsigned int)getpid());
920*49cdfc7eSAndroid Build Coastguard Worker 	seed = rand();
921*49cdfc7eSAndroid Build Coastguard Worker 	srand((unsigned int)time(NULL));
922*49cdfc7eSAndroid Build Coastguard Worker 	seed = (seed ^ rand()) % 100000;
923*49cdfc7eSAndroid Build Coastguard Worker 	srand48((long int)seed);
924*49cdfc7eSAndroid Build Coastguard Worker 	return (seed);
925*49cdfc7eSAndroid Build Coastguard Worker }
926*49cdfc7eSAndroid Build Coastguard Worker 
927*49cdfc7eSAndroid Build Coastguard Worker /*****  LTP Port        *****/
928*49cdfc7eSAndroid Build Coastguard Worker void ok_exit(void)
929*49cdfc7eSAndroid Build Coastguard Worker {
930*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TPASS, "Test passed");
931*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
932*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
933*49cdfc7eSAndroid Build Coastguard Worker }
934*49cdfc7eSAndroid Build Coastguard Worker 
935*49cdfc7eSAndroid Build Coastguard Worker int anyfail(void)
936*49cdfc7eSAndroid Build Coastguard Worker {
937*49cdfc7eSAndroid Build Coastguard Worker 	tst_brkm(TFAIL, tst_rmdir, "Test failed");
938*49cdfc7eSAndroid Build Coastguard Worker }
939*49cdfc7eSAndroid Build Coastguard Worker 
940*49cdfc7eSAndroid Build Coastguard Worker /*****  **      **      *****/
941