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