int main(int argc, char *argv[]) { caddr_t mmapaddr; size_t pagesize = sysconf(_SC_PAGE_SIZE); time_t t; int i; struct sigaction sa; tst_tmpdir(); if (!argc) { (void)fprintf(stderr, "argc == 0\n"); return 1; } if (argc != 1) { (void)fprintf(stderr, "usage: %s\n", argv[0]); return 1; } (void)time(&t); if ((fd = mkstemp(tmpname)) == -1) { ERROR("mkstemp failed"); anyfail(); } sa.sa_handler = cleanup; sa.sa_flags = 0; if (sigemptyset(&sa.sa_mask)) { ERROR("sigemptyset failed"); anyfail(); } CATCH_SIG(SIGINT); CATCH_SIG(SIGQUIT); CATCH_SIG(SIGTERM); if (sbrk(2*pagesize - ((ulong)sbrk(0) & (pagesize-1))) == (char *)-1) { CLEANERROR("couldn't round up brk"); anyfail(); } if ((mmapaddr = sbrk(0)) == (caddr_t)-1) { CLEANERROR("couldn't find top of brk"); anyfail(); } /* Write a page of garbage into the file, so we can mmap it without * asking for PROT_WRITE. */ for (i = pagesize; i; i--) *(mmapaddr-i) = 'a'; if (write(fd, (char *)mmapaddr-pagesize, pagesize) != pagesize) { CLEANERROR("write failed"); anyfail(); } if (mmap(mmapaddr, pagesize, PROT_NONE, MAP_FIXED|MAP_PRIVATE|MAP_FILE, fd, 0) != mmapaddr) { CLEANERROR("couldn't mmap file"); anyfail(); } /* * Since the file is mmapped, mmreg_new and uiomove_phys handle all * I/O */ if (lseek(fd, 0, SEEK_SET) != 0) { CLEANERROR("lseek failed"); anyfail(); } if (read(fd, (char *)mmapaddr, pagesize) != -1) { CLEANERROR("read succeded"); anyfail(); } if (errno != EFAULT) { CLEANERROR("read didn't set errno = EFAULT"); anyfail(); } if (write(fd, (char *)mmapaddr, pagesize) != -1) { CLEANERROR("write succeded"); anyfail(); } if (errno != EFAULT) { CLEANERROR("write didn't set errno = EFAULT"); anyfail(); } if (close(fd) == -1) { CLEANERROR("close failed"); anyfail(); } if (munmap(mmapaddr, pagesize) == -1) { CLEANERROR("munmap failed"); anyfail(); } if (unlink(tmpname) == -1) { ERROR("unlink failed"); anyfail(); } (void)time(&t); // (void)printf("%s: Finished %s", argv[0], ctime(&t)); ok_exit(); /* LTP Port */ tst_exit(); }
int main(int argc, char *argv[]) { char *buf; size_t pagesize = (size_t) sysconf(_SC_PAGE_SIZE); caddr_t mmapaddr; time_t t; int i, j; struct sigaction sa; #ifdef LARGE_FILE off64_t startoffset; off64_t seekoff; off64_t mapoff; #else /* LARGE_FILE */ off_t startoffset; off_t seekoff; off_t mapoff; #endif /* LARGE_FILE */ if (argc < 2 || argc > 3) { (void)fprintf(stderr, "Usage: %s filename startoffset\n", argv[0]); return 1; } filename = argv[1]; if (argc >= 3) { #ifdef LARGE_FILE startoffset = atoll(argv[2]); #else /* LARGE_FILE */ startoffset = atoi(argv[2]); #endif /* LARGE_FILE */ } else startoffset = pagesize; if (startoffset % pagesize != 0) { fprintf(stderr, "pagesize=%ld\n", (long)pagesize); fprintf(stderr, "startoffset must be a pagesize multiple\n"); anyfail(); //LTP Port } (void)time(&t); // (void)printf("%s: Started %s", argv[0], ctime(&t)); if ((buf = sbrk(6 * pagesize)) == (char *)-1) { ERROR("couldn't allocate buf"); anyfail(); //LTP Port } if (sbrk(pagesize - ((ulong) sbrk(0) & (pagesize - 1))) == (char *)-1) { ERROR("couldn't round up brk"); anyfail(); //LTP Port } if ((mmapaddr = (caddr_t) sbrk(0)) == (caddr_t) - 1) { ERROR("couldn't find top of brk"); anyfail(); //LTP Port } sa.sa_handler = cleanup; sa.sa_flags = 0; if (sigemptyset(&sa.sa_mask)) { ERROR("sigemptyset failed"); anyfail(); //LTP Port } CATCH_SIG(SIGINT); CATCH_SIG(SIGQUIT); CATCH_SIG(SIGTERM); tst_tmpdir(); #ifdef LARGE_FILE if ((rofd = open64(filename, O_RDONLY | O_CREAT, 0777)) == -1) { #else /* LARGE_FILE */ if ((rofd = open(filename, O_RDONLY | O_CREAT, 0777)) == -1) { #endif /* LARGE_FILE */ ERROR("read only open failed"); anyfail(); //LTP Port } #ifdef LARGE_FILE if ((rwfd = open64(filename, O_RDWR)) == -1) { #else /* LARGE_FILE */ if ((rwfd = open(filename, O_RDWR)) == -1) { #endif /* LARGE_FILE */ (void)close(rofd); (void)unlink(filename); ERROR("read/write open failed"); anyfail(); //LTP Port } #ifdef LARGE_FILE seekoff = startoffset + (off64_t) 64 *(off64_t) 6 *(off64_t) pagesize; if (lseek64(rwfd, seekoff, SEEK_SET) != seekoff) { #else /* LARGE_FILE */ seekoff = startoffset + (off_t) 64 *(off_t) 6 *(off_t) pagesize; if (lseek(rwfd, seekoff, SEEK_SET) != seekoff) { #endif /* LARGE_FILE */ CLEANERROR("first lseek failed"); anyfail(); //LTP Port } i = 0; while (i < pagesize && write(rwfd, "b", 1) == 1) i++; if (i != pagesize) { CLEANERROR("write to extend file failed"); anyfail(); //LTP Port } /* The file is now really big, and empty. * Assuming disk blocks are 8k, and logical pages are 4k, there are * two maps per page. In order to test mapping at the beginning and * ends of the block, mapping the whole block, or none of the block * with different mappings on preceding and following blocks, each * 3 blocks with 6 pages can be thought of as a binary number from 0 to * 64 with a bit set for mapped or cleared for unmapped. This number * is represented by i. The value j is used to look at the bits of i * and decided to map the page or not. * NOTE: None of the above assumptions are critical. */ for (i = 0; i < 64; i++) { for (j = 0; j < 6; j++) { if (i & (1 << j)) { #ifdef LARGE_FILE mapoff = startoffset + (off64_t) pagesize *(off64_t) (6 + i + j); if (mmap64(mmapaddr + pagesize * (6 * i + j), pagesize, PROT_READ, MAP_FILE | MAP_PRIVATE | MAP_FIXED, rofd, mapoff) == (caddr_t) - 1) { #else /* LARGE_FILE */ mapoff = startoffset + (off_t) pagesize *(off_t) (6 + i + j); if (mmap(mmapaddr + pagesize * (6 * i + j), pagesize, PROT_READ, MAP_FILE | MAP_PRIVATE | MAP_FIXED, rofd, mapoff) == (caddr_t) - 1) { #endif /* LARGE_FILE */ CLEANERROR("mmap failed"); anyfail(); //LTP Port } } } } /* done mapping */ for (i = 0; i < 6 * pagesize; i++) buf[i] = 'a'; /* write out 6 pages of stuff into each of the 64 six page sections */ #ifdef LARGE_FILE if (lseek64(rwfd, startoffset, SEEK_SET) != startoffset) { #else /* LARGE_FILE */ if (lseek(rwfd, startoffset, SEEK_SET) != startoffset) { #endif /* LARGE_FILE */ CLEANERROR("second lseek failed"); anyfail(); //LTP Port } for (i = 0; i < 64; i++) { if (write(rwfd, buf, 6 * pagesize) != 6 * pagesize) { CLEANERROR("write failed"); anyfail(); //LTP Port } } /* Just finished scribbling all over interwoven mmapped and unmapped * regions. */ for (i = 0; i < 64; i++) { for (j = 0; j < 6; j++) { /* if mmaped && not updated */ if ((i & (1 << j)) && *(mmapaddr + pagesize * (6 * i + j)) != 'a') { CLEANERROR("'a' missing from mmap"); (void)fprintf(stderr, "i=%d\nj=%d\n" "val=0x%x\n", i, j, (int)(* (mmapaddr + pagesize * (6 * i + j)))); anyfail(); //LTP Port } } } /* Just checked to see that each mmapped page at least had an 'a' at * the beginning. */ CLEAN; (void)time(&t); // (void)printf("%s: Finished %s", argv[0], ctime(&t)); LTP Port (local_flag == FAILED) ? tst_resm(TFAIL, "Test failed\n") : tst_resm(TPASS, "Test passed\n"); //LTP Port tst_rmdir(); tst_exit(); //LTP Port tst_exit(); } /***** LTP Port *****/ int anyfail(void) { tst_brkm(TFAIL, tst_rmdir, "Test failed\n"); }