void setup() { TEST_PAUSE; tst_tmpdir(); }
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"); }
/* * setup(void) - performs all ONE TIME setup for this test. * Exit the test program on receipt of unexpected signals. * Create a temporary directory used to hold test directories created * and change the directory to it. * Verify that pid of process executing the test is root. * Create a test directory on temporary directory and set the ownership * of test directory to nobody user. * Set the effective uid/gid of the process to that of nobody user. */ void setup() { /* Capture unexpected signals */ tst_sig(NOFORK, DEF_HANDLER, cleanup); /* Check that the test process id is super/root */ if (geteuid() != 0) { tst_brkm(TBROK, NULL, "Must be super/root for this test!"); tst_exit(); } /* Pause if that option was specified */ TEST_PAUSE; /* Make a temp dir and cd to it */ tst_tmpdir(); /* fix permissions on the tmpdir */ if (chmod(".", 0711) != 0) { tst_brkm(TBROK, cleanup, "chmod() failed"); } /* Save the real user id of the test process */ save_myuid = getuid(); /* Save the process id of the test process */ mypid = getpid(); /* Get the node name to be created in the test */ sprintf(node_name, TNODE, mypid); /* Get the uid/gid of guest user - nobody */ if ((user1 = getpwnam(LTPUSER)) == NULL) { tst_brkm(TBROK, cleanup, "%s not in /etc/passwd", LTPUSER); } user1_uid = user1->pw_uid; group1_gid = user1->pw_gid; /* Get effective group id of the test process */ group2_gid = getegid(); /* * Create a test directory under temporary directory with the * specified mode permissions, with uid/gid set to that of guest * user and the test process. */ if (mkdir(DIR_TEMP, MODE_RWX) < 0) { tst_brkm(TBROK, cleanup, "mkdir(2) of %s failed", DIR_TEMP); } if (chown(DIR_TEMP, user1_uid, group2_gid) < 0) { tst_brkm(TBROK, cleanup, "chown(2) of %s failed", DIR_TEMP); } /* * Verify that test directory created with expected permission modes * and ownerships. */ if (stat(DIR_TEMP, &buf) < 0) { tst_brkm(TBROK, cleanup, "stat(2) of %s failed", DIR_TEMP); } /* Verify modes of test directory */ if (buf.st_mode & S_ISGID) { tst_brkm(TBROK, cleanup, "%s: Incorrect modes, setgid bit set", DIR_TEMP); } /* Verify group ID */ if (buf.st_gid != group2_gid) { tst_brkm(TBROK, cleanup, "%s: Incorrect group", DIR_TEMP); } /* * Set the effective group id and user id of the test process * to that of guest user. */ if (setgid(group1_gid) < 0) { tst_brkm(TBROK, cleanup, "Unable to set process gid to that of ltp user"); } if (setreuid(-1, user1_uid) < 0) { tst_brkm(TBROK, cleanup, "Unable to set process uid to that of ltp user"); } /* Save the real group ID of the current process */ mygid = getgid(); /* Change directory to DIR_TEMP */ if (chdir(DIR_TEMP) < 0) { tst_brkm(TBROK, cleanup, "Unable to change to %s directory", DIR_TEMP); } }
/* setup() - performs all ONE TIME setup for this test */ void setup() { char nobody_uid[] = "nobody"; struct passwd *ltpuser; tst_sig(FORK, DEF_HANDLER, cleanup); /* Check whether we are root */ if (geteuid() != 0) { if (Type != NULL) { free(Type); } tst_brkm(TBROK, NULL, "Test must be run as root"); } /* Switch to nobody user */ if ((ltpuser = getpwnam(nobody_uid)) == NULL) { if (Type != NULL) { free(Type); } tst_brkm(TBROK, NULL, "\"nobody\" user not present"); } if (seteuid(ltpuser->pw_uid) == -1) { if (Type != NULL) { free(Type); } tst_brkm(TBROK, NULL, "setuid failed to set the " "effective uid to %d", ltpuser->pw_uid); } /* make a temp directory */ tst_tmpdir(); (void)sprintf(mntpoint, "mnt_%d", getpid()); if (mkdir(mntpoint, DIR_MODE)) { tst_brkm(TBROK, cleanup1, "mkdir(%s, %#o) failed; errno = %d:" " %s", mntpoint, DIR_MODE, errno, strerror(errno)); } if (seteuid(0) == -1) { tst_brkm(TBROK, cleanup1, "setuid failed to set the effective" " uid to %d", ltpuser->pw_uid); } /* set up expected error numbers */ TEST_EXP_ENOS(exp_enos); if (access(device,F_OK)) { tst_brkm(TBROK, cleanup1, "Device '%s' does not exist", device); } TEST(mount(device, mntpoint, Type, 0, NULL)); if (TEST_RETURN != 0) { tst_brkm(TBROK, cleanup1, "mount(2) failed to mount device %s " "errno = %d : %s", device, TEST_ERRNO, strerror(TEST_ERRNO)); } TEST_PAUSE; return; }
static void setup(void) { tst_tmpdir(); }
int main(int ac, char **av) { int lc; char *msg; int Hflag = 0; int sflag = 0; int huge_pagesize = 0; option_t options[] = { {"H:", &Hflag, &Hopt}, {"s:", &sflag, &nr_opt}, {NULL, NULL, NULL} }; msg = parse_opts(ac, av, options, &help); if (msg) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s," " use -help", msg); if (!Hflag) { tst_tmpdir(); Hopt = tst_get_tmpdir(); } if (sflag) hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX); setup(); for (lc = 0; TEST_LOOPING(lc); lc++) { /* Creat a temporary file used for huge mapping */ fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666); if (fildes < 0) tst_brkm(TFAIL | TERRNO, cleanup, "open %s failed", TEMPFILE); tst_count = 0; /* Note the number of free huge pages BEFORE testing */ freepages = read_meminfo("HugePages_Free:"); beforetest = freepages; /* Note the size of huge page size BEFORE testing */ huge_pagesize = read_meminfo("Hugepagesize:"); tst_resm(TINFO, "Size of huge pages is %d KB", huge_pagesize); #if __WORDSIZE == 32 tst_resm(TINFO, "Total amount of free huge pages is %d", freepages); tst_resm(TINFO, "Max number allowed for 1 mmap file in" " 32-bits is 128"); if (freepages > 128) freepages = 128; #endif mapsize = (long long)freepages *huge_pagesize * 1024; addr = mmap(NULL, mapsize, PROT_READ | PROT_WRITE, MAP_SHARED, fildes, 0); sleep(2); if (addr == MAP_FAILED) { tst_resm(TFAIL | TERRNO, "mmap() Failed on %s", TEMPFILE); close(fildes); continue; } else { tst_resm(TPASS, "Succeeded mapping file using %ld pages", freepages); /* force to allocate page and change HugePages_Free */ *(int *)addr = 0; } /* * Make sure the number of free huge pages * AFTER testing decreased */ aftertest = read_meminfo("HugePages_Free:"); hugepagesmapped = beforetest - aftertest; if (hugepagesmapped < 1) tst_resm(TWARN, "Number of HUGEPAGES_FREE stayed the" " same. Okay if multiple copies running due" " to test collision."); /* Clean up things in case we are looping */ /* Unmap the mapped memory */ if (munmap(addr, mapsize) != 0) tst_brkm(TFAIL | TERRNO, NULL, "munmap failed"); close(fildes); } cleanup(); tst_exit(); }
int main(int argc, char *argv[]) { char *progname; int fd; int c; extern char *optarg; unsigned nprocs = 0; unsigned procno; pid_t *pidarray = NULL; pid_t pid; pid_t wr_pid = 0; uchar_t *buf = NULL; unsigned int seed; int pagesize = sysconf(_SC_PAGE_SIZE); float alarmtime = 0; struct sigaction sa; unsigned i; int write_cnt; uchar_t data; int no_prob = 0; int wait_stat; time_t t; #ifdef LARGE_FILE off64_t bytes_left; #else /* LARGE_FILE */ off_t bytes_left; #endif /* LARGE_FILE */ progname = *argv; tst_tmpdir(); if (argc < 2) { (void)fprintf(stderr, "usage: %s %s\n", progname, usage); exit(1); } while ((c = getopt(argc, argv, "S:omdlrf:p:t:w:s:")) != -1) { switch (c) { case 'd': debug = 1; break; case 't': alarmtime = atof(optarg) * 60; break; case 'p': nprocs = atoi(optarg); break; case 'l': leavefile = 1; break; case 's': sleeptime = atoi(optarg); if (sleeptime < 0) { (void)fprintf(stderr, "error: negative " "sleeptime\n"); anyfail(); } break; case 'w': growsize = atoi(optarg); if (growsize < 0) { (void)fprintf(stderr, "error: negative write " "size\n"); anyfail(); } break; case 'f': #if defined(__linux__) #ifdef LARGE_FILE filesize = atoll(optarg); #else /* LARGE_FILE */ filesize = atoi(optarg); #endif /* LARGE_FILE */ #elif defined(__FreeBSD__) #else filesize = atol(optarg); #endif if (filesize < 0) { (void)fprintf(stderr, "error: negative " "filesize\n"); anyfail(); } break; case 'r': randloops = 1; break; case 'm': dosync = 1; break; case 'o': do_offset = 1; break; case 'S': #if defined(__linux__) #ifdef LARGE_FILE sparseoffset = atoll(optarg); #else /* LARGE_FILE */ sparseoffset = atoi(optarg); #endif /* LARGE_FILE */ #elif defined(__FreeBSD__) #else sparseoffset = atol(optarg); #endif if (sparseoffset % pagesize != 0) { fprintf(stderr, "sparseoffset must be pagesize multiple\n"); anyfail(); } break; default: (void)fprintf(stderr, "usage: %s %s\n", progname, usage); anyfail(); } } if (nprocs > 255) { (void)fprintf(stderr, "invalid nprocs %d - (range 0-255)\n", nprocs); anyfail(); } (void)time(&t); //(void)printf("%s: Started %s", argv[0], ctime(&t)); LTP Port (void)sprintf(filename, "%sout.%d", progname, getpid()); seed = initrand(); pattern = seed & 0xff; if (debug) { #ifdef LARGE_FILE (void)printf("creating file <%s> with %Ld bytes, pattern %d\n", filename, filesize, pattern); #else /* LARGE_FILE */ (void)printf("creating file <%s> with %ld bytes, pattern %d\n", filename, filesize, pattern); #endif /* LARGE_FILE */ if (alarmtime) (void)printf("running for %f minutes\n", alarmtime / 60); else (void)printf("running with no time limit\n"); } /* * Plan for death by signal. User may have specified * a time limit, in which case set an alarm and catch SIGALRM. * Also catch and cleanup with SIGINT, SIGQUIT, and SIGTERM. */ sa.sa_handler = finish; sa.sa_flags = 0; if (sigemptyset(&sa.sa_mask)) { perror("sigempty error"); goto cleanup; } if (sigaction(SIGINT, &sa, 0) == -1) { perror("sigaction error SIGINT"); goto cleanup; } if (alarmtime) { if (sigaction(SIGALRM, &sa, 0) == -1) { perror("sigaction error"); goto cleanup; } (void)alarm(alarmtime); } /* If we get a SIGQUIT or SIGTERM, clean up and exit immediately. */ sa.sa_handler = clean_up_file; if (sigaction(SIGQUIT, &sa, 0) == -1) { perror("sigaction error SIGQUIT"); goto cleanup; } if (sigaction(SIGTERM, &sa, 0) == -1) { perror("sigaction error SIGTERM"); goto cleanup; } #ifdef LARGE_FILE if ((fd = open64(filename, O_CREAT | O_TRUNC | O_RDWR, 0664)) == -1) { #else /* LARGE_FILE */ if ((fd = open(filename, O_CREAT | O_TRUNC | O_RDWR, 0664)) == -1) { #endif /* LARGE_FILE */ perror("open error"); anyfail(); } if ((buf = (uchar_t *) malloc(pagesize + growsize)) == NULL || (pidarray = (pid_t *) malloc(nprocs * sizeof(pid_t))) == NULL) { perror("malloc error"); anyfail(); } for (i = 0; i < nprocs; i++) *(pidarray + i) = 0; for (i = 0, data = 0; i < pagesize; i++) { *(buf + i) = (data + pattern) & 0xff; if (++data == nprocs) data = 0; } for (data = 0; i < pagesize + growsize; i++) { *(buf + i) = (data + pattern) & 0xff; if (++data == nprocs) data = 0; } #ifdef LARGE_FILE if (lseek64(fd, sparseoffset, SEEK_SET) < 0) { #else /* LARGE_FILE */ if (lseek(fd, sparseoffset, SEEK_SET) < 0) { #endif /* LARGE_FILE */ perror("lseek"); anyfail(); } for (bytes_left = filesize; bytes_left; bytes_left -= c) { write_cnt = min(pagesize, bytes_left); if ((c = write(fd, (char *)buf, write_cnt)) != write_cnt) { if (c == -1) { perror("write error"); } else { (void)fprintf(stderr, "write: wrote %d of %d " "bytes\n", c, write_cnt); } (void)close(fd); (void)unlink(filename); anyfail(); } } (void)close(fd); /* * Fork off mmap children. */ for (procno = 0; procno < nprocs; procno++) { switch (pid = fork()) { case -1: perror("fork error"); goto cleanup; case 0: child_mapper(filename, procno, nprocs); exit(0); default: pidarray[procno] = pid; } } /* * Now fork off an additional process to continually * write to (and grow) the file. */ if ((wr_pid = fork()) == -1) { perror("fork error"); goto cleanup; } else if (wr_pid == 0) { /* child */ child_writer(filename, buf); exit(0); } /* * Now wait for children and refork them as needed. */ while (!finished) { pid = wait(&wait_stat); /* * Block signals while processing child exit. */ if (sighold(SIGALRM) || sighold(SIGINT)) { perror("sighold error"); goto cleanup; } if (pid != -1) { /* * Check exit status, then refork with the * appropriate procno. */ if (!WIFEXITED(wait_stat) || WEXITSTATUS(wait_stat) != 0) { (void)fprintf(stderr, "child exit with err " "<x%x>\n", wait_stat); goto cleanup; } for (i = 0; i < nprocs; i++) if (pid == pidarray[i]) break; if (i == nprocs) { if (pid == wr_pid) { (void)fprintf(stderr, "writer child unexpected exit <x%x>\n", wait_stat); wr_pid = 0; } else (void)fprintf(stderr, "unknown child " "pid %d, <x%x>\n", pid, wait_stat); goto cleanup; } if ((pid = fork()) == -1) { perror("fork error"); pidarray[i] = 0; goto cleanup; } else if (pid == 0) { /* child */ child_mapper(filename, i, nprocs); exit(0); } else pidarray[i] = pid; } else { /* * wait returned an error. If EINTR, then * normal finish, else it's an unexpected * error... */ if (errno != EINTR || !finished) { perror("unexpected wait error"); goto cleanup; } } if (sigrelse(SIGALRM) || sigrelse(SIGINT)) { perror("sigrelse error"); goto cleanup; } } /* * Finished! Check the file for sanity, then kill all * the children and done!. */ (void)alarm(0); no_prob = 1; cleanup: for (i = 0; i < nprocs; i++) (void)kill(pidarray[i], SIGUSR1); (void)kill(wr_pid, SIGUSR1); while (wait(&wait_stat) != -1 || errno != ECHILD) continue; if (no_prob) { /* only check file if no errors */ if (!fileokay(filename, buf)) { (void)fprintf(stderr, "file data incorrect!\n"); (void)printf(" leaving file <%s>\n", filename); anyfail(); } else { (void)printf("file data okay\n"); if (!leavefile) (void)unlink(filename); } } else (void)printf(" leaving file <%s>\n", filename); (void)time(&t); // (void)printf("%s: Finished %s", argv[0], ctime(&t)); LTP Port ok_exit(); tst_exit(); } /* * Child process that reads/writes map. The child stats the file * to determine the size, maps the size of the file, then reads/writes * its own locations on random pages of the map (its locations being * determined based on nprocs & procno). After a specific number of * iterations, it exits. */ void child_mapper(char *file, unsigned procno, unsigned nprocs) { #ifdef LARGE_FILE struct stat64 statbuf; off64_t filesize; off64_t offset; #else /* LARGE_FILE */ struct stat statbuf; off_t filesize; off_t offset; #endif /* LARGE_FILE */ size_t validsize; caddr_t paddr; int pagesize = sysconf(_SC_PAGE_SIZE); unsigned randpage; unsigned int seed; unsigned loopcnt; unsigned nloops; unsigned mappages; unsigned mapflags; unsigned i; struct sigaction sa_mapper; mapflags = MAP_SHARED; seed = initrand(); /* initialize random seed */ sa_mapper.sa_handler = clean_mapper; sa_mapper.sa_flags = 0; if (sigemptyset(&sa_mapper.sa_mask)) { perror("sigempty error"); anyfail(); } if (sigaction(SIGUSR1, &sa_mapper, 0) == -1) { perror("sigaction error SIGUSR1"); anyfail(); } #ifdef LARGE_FILE if ((fd_mapper = open64(file, O_RDWR)) == -1) { #else /* LARGE_FILE */ if ((fd_mapper = open(file, O_RDWR)) == -1) { #endif /* LARGE_FILE */ perror("open error"); anyfail(); } #ifdef LARGE_FILE if (fstat64(fd_mapper, &statbuf) == -1) { #else /* LARGE_FILE */ if (fstat(fd_mapper, &statbuf) == -1) { #endif /* LARGE_FILE */ perror("stat error"); anyfail(); } filesize = statbuf.st_size; if (statbuf.st_size - sparseoffset > SIZE_MAX) { fprintf(stderr, "size_t overflow when setting up map\n"); anyfail(); } mapsize_mapper = (size_t) (statbuf.st_size - sparseoffset); mappages = roundup(mapsize_mapper, pagesize) / pagesize; offset = sparseoffset; if (do_offset) { int pageoffset = lrand48() % mappages; int byteoffset = pageoffset * pagesize; offset += byteoffset; mapsize_mapper -= byteoffset; mappages -= pageoffset; } #ifdef LARGE_FILE if ((maddr_mapper = mmap64(0, mapsize_mapper, PROT_READ | PROT_WRITE, mapflags, fd_mapper, offset)) == (caddr_t) - 1) { #else /* LARGE_FILE */ if ((maddr_mapper = mmap(0, mapsize_mapper, PROT_READ | PROT_WRITE, mapflags, fd_mapper, offset)) == (caddr_t) - 1) { #endif /* LARGE_FILE */ perror("mmap error"); anyfail(); } (void)close(fd_mapper); nloops = (randloops) ? (lrand48() % MAXLOOPS) : MAXLOOPS; if (debug) { #ifdef LARGE_FILE (void)printf("child %d (pid %ld): seed %d, fsize %Ld, " "mapsize %d, off %Ld, loop %d\n", procno, getpid(), seed, filesize, mapsize_mapper, offset / pagesize, nloops); #else /* LARGE_FILE */ (void)printf("child %d (pid %d): seed %d, fsize %ld, " "mapsize %ld, off %ld, loop %d\n", procno, getpid(), seed, filesize, (long)mapsize_mapper, offset / pagesize, nloops); #endif /* LARGE_FILE */ } /* * Now loop read/writing random pages. */ for (loopcnt = 0; loopcnt < nloops; loopcnt++) { randpage = lrand48() % mappages; paddr = maddr_mapper + (randpage * pagesize); /* page address */ if (randpage < mappages - 1 || !(mapsize_mapper % pagesize)) validsize = pagesize; else validsize = mapsize_mapper % pagesize; /* * Because one child is mapping file in extend mode, * it may be padded with zeros at end. So we can't * do an exact check -- accept known pattern OR zeros. */ for (i = procno; i < validsize; i += nprocs) { if (*((unsigned char *)(paddr + i)) != ((procno + pattern) & 0xff) && *((unsigned char *)(paddr + i)) != 0) { (void)fprintf(stderr, "child %d: invalid data " "<x%x>", procno, *((unsigned char *)(paddr + i))); (void)fprintf(stderr, " at pg %d off %d, exp " "<x%x>\n", randpage, i, (procno + pattern) & 0xff); anyfail(); } /* * Now write it. */ *(paddr + i) = (procno + pattern) & 0xff; } } if (dosync) { /* * Exercise msync() as well! */ randpage = lrand48() % mappages; paddr = maddr_mapper + (randpage * pagesize); /* page address */ if (msync(paddr, (mappages - randpage) * pagesize, MS_SYNC) == -1) { perror("msync error"); anyfail(); } } if (munmap(maddr_mapper, mapsize_mapper) == -1) { perror("munmap failed"); anyfail(); } exit(0); } /* * child_writer * The child process that continually (and slowly!!) grows * the file. The purpose of this is to exercise the code * supporting mapping of fragments. The map children are * constantly reforking and will pick up the map changes, etc. * This process executes until signalled (i.e. has no exit!) * unless error. */ void child_writer(char *file, uchar_t * buf) { /* buf already set up in main */ struct sigaction sa_writer; sa_writer.sa_handler = clean_writer; sa_writer.sa_flags = 0; if (sigemptyset(&sa_writer.sa_mask)) { perror("sigempty error"); anyfail(); } if (sigaction(SIGUSR1, &sa_writer, 0) == -1) { perror("sigaction error SIGUSR1"); anyfail(); } #ifdef LARGE_FILE struct stat64 statbuf; off64_t off; #else /* LARGE_FILE */ struct stat statbuf; off_t off; #endif /* LARGE_FILE */ int pagesize = sysconf(_SC_PAGE_SIZE); uchar_t *p; int cnt; #ifdef LARGE_FILE if ((fd_writer = open64(file, O_RDWR)) == -1) { #else /* LARGE_FILE */ if ((fd_writer = open(file, O_RDWR)) == -1) { #endif /* LARGE_FILE */ perror("open error"); anyfail(); } #ifdef LARGE_FILE if ((off = lseek64(fd_writer, 0, SEEK_END)) == -1) { #else /* LARGE_FILE */ if ((off = lseek(fd_writer, 0, SEEK_END)) == -1) { #endif /* LARGE_FILE */ perror("lseek error"); anyfail(); } for (;;) { #ifdef LARGE_FILE if (fstat64(fd_writer, &statbuf) == -1) { #else /* LARGE_FILE */ if (fstat(fd_writer, &statbuf) == -1) { #endif /* LARGE_FILE */ perror("fstat error"); anyfail(); } #ifdef LARGE_FILE if (debug) (void)printf("writer %d bytes at off %Ld, size %Ld\n", growsize, off, statbuf.st_size); #else /* LARGE_FILE */ if (debug) (void)printf("writer %d bytes at off %ld, size %ld\n", growsize, off, statbuf.st_size); #endif /* LARGE_FILE */ /* * Write some number of bytes, then sleep some * number of seconds... * Need to keep track of our offset so write the * right bytes. */ p = buf + (off % pagesize); if ((cnt = write(fd_writer, p, growsize)) != growsize) { if (cnt == -1) perror("write error"); else (void)fprintf(stderr, "wrote %d of %d bytes\n", cnt, growsize); anyfail(); } off += growsize; (void)sleep(sleeptime); if (dosync) { if (fsync(fd_writer) == -1) { perror("fsync error"); anyfail(); } } } close(fd_writer); } /* * Make sure file has all the correct data. */ int fileokay(char *file, uchar_t * expbuf) { #ifdef LARGE_FILE struct stat64 statbuf; #else /* LARGE_FILE */ struct stat statbuf; #endif /* LARGE_FILE */ size_t mapsize; uchar_t *readbuf; unsigned mappages; unsigned pagesize = sysconf(_SC_PAGE_SIZE); int fd; int cnt; unsigned i, j; #ifdef LARGE_FILE if ((fd = open64(file, O_RDONLY)) == -1) { #else /* LARGE_FILE */ if ((fd = open(file, O_RDONLY)) == -1) { #endif /* LARGE_FILE */ perror("open error"); anyfail(); } #ifdef LARGE_FILE if (fstat64(fd, &statbuf) == -1) { #else /* LARGE_FILE */ if (fstat(fd, &statbuf) == -1) { #endif /* LARGE_FILE */ perror("stat error"); anyfail(); } #ifdef LARGE_FILE if (lseek64(fd, sparseoffset, SEEK_SET) < 0) { #else /* LARGE_FILE */ if (lseek(fd, sparseoffset, SEEK_SET) < 0) { #endif /* LARGE_FILE */ perror("lseek"); exit(1); } readbuf = (uchar_t *) malloc(pagesize); if (statbuf.st_size - sparseoffset > SIZE_MAX) { fprintf(stderr, "size_t overflow when setting up map\n"); exit(1); } mapsize = (size_t) (statbuf.st_size - sparseoffset); mappages = roundup(mapsize, pagesize) / pagesize; for (i = 0; i < mappages; i++) { cnt = read(fd, (char *)readbuf, pagesize); if (cnt == -1) { perror("read error"); close(fd); return 0; } else if (cnt != pagesize) { /* * Okay if at last page in file... */ if ((i * pagesize) + cnt != mapsize) { (void)fprintf(stderr, "read %d of %ld bytes\n", (i * pagesize) + cnt, (long)mapsize); close(fd); return 0; } } /* * Compare read bytes of data. * May have zeros from map extend... */ for (j = 0; j < cnt; j++) { if (expbuf[j] != readbuf[j] && readbuf[j] != 0) { (void)fprintf(stderr, "read bad data: exp %c got %c", expbuf[j], readbuf[j]); #ifdef LARGE_FILE (void)fprintf(stderr, ", pg %d off %d, " "(fsize %Ld)\n", i, j, statbuf.st_size); #else /* LARGE_FILE */ (void)fprintf(stderr, ", pg %d off %d, " "(fsize %ld)\n", i, j, statbuf.st_size); #endif /* LARGE_FILE */ close(fd); return 0; } } } close(fd); return 1; } /*ARGSUSED*/ void finish(int sig) { finished++; /* finish nicely and check the file contents */ } /*ARGSUSED*/ void clean_up_file(int sig) { if (!leavefile) (void)unlink(filename); exit(1); } void clean_mapper(int sig) { if (fd_mapper) close(fd_mapper); munmap(maddr_mapper, mapsize_mapper); exit(0); } void clean_writer(int sig) { if (fd_writer) close(fd_writer); exit(0); } unsigned int initrand(void) { unsigned int seed; /* * Initialize random seed... Got this from a test written * by scooter: * Use srand/rand to diffuse the information from the * time and pid. If you start several processes, then * the time and pid information don't provide much * variation. */ srand((unsigned int)getpid()); seed = rand(); srand((unsigned int)time((time_t *) 0)); seed = (seed ^ rand()) % 100000; srand48((long int)seed); return (seed); } /***** LTP Port *****/ void ok_exit() { tst_resm(TPASS, "Test passed\n"); tst_rmdir(); tst_exit(); } int anyfail() { tst_resm(TFAIL, "Test failed\n"); tst_rmdir(); tst_exit(); return 0; }
int main() { int wjh_ret = -1, wjh_f = -1, count = 0; //used for the 2nd test //make str > trunc_size characters long char str[] = "THIS IS JAYS TEST FILE DATA"; int trunc_size = 4; int flag = O_RDONLY; #ifdef DEBUG printf("Starting test, possible errnos are; EBADF(%d) EINVAL(%d)\n", EBADF, EINVAL); printf("\t\tENOENT(%d) EACCES(%d) EPERM(%d)\n\n", ENOENT, EACCES, EPERM); #endif tst_tmpdir(); //TEST1: ftruncate on a socket is not valid, should fail w/ EINVAL #ifdef DEBUG printf("Starting test1\n"); #endif wjh_f = socket(PF_INET, SOCK_STREAM, 0); wjh_ret = ftruncate(wjh_f, 1); #ifdef DEBUG printf("DEBUG: fd: %d ret: %d errno(%d) %s\n", wjh_f, wjh_ret, errno, strerror(errno)); #endif if (wjh_ret == -1 && errno == EINVAL) { tst_resm(TPASS, "Test Passed"); } else { tst_resm(TFAIL, "ftruncate(socket)=%i (wanted -1), errno=%i (wanted EINVAL %i)", wjh_ret, errno, EINVAL); } close(wjh_f); errno = 0; wjh_ret = 0; wjh_f = -1; //TEST2: ftruncate on fd not open for writing should be EINVAL #ifdef DEBUG printf("\nStarting test2\n"); #endif //create a file and fill it so we can truncate it in ReadOnly mode //delete it first, ignore if it doesn't exist unlink(TESTFILE); errno = 0; wjh_f = open(TESTFILE, O_RDWR | O_CREAT, 0644); if (wjh_f == -1) { tst_resm(TFAIL | TERRNO, "open(%s) failed", TESTFILE); tst_rmdir(); tst_exit(); } while (count < strlen(str)) { if ((count += write(wjh_f, str, strlen(str))) == -1) { tst_resm(TFAIL | TERRNO, "write() failed"); close(wjh_f); tst_rmdir(); tst_exit(); } } close(wjh_f); errno = 0; //Uncomment below if you want it to succeed, O_RDWR => success // flag = O_RDWR; #ifdef DEBUG if (flag == O_RDWR) { printf("\tLooks like it should succeed!\n"); } #endif wjh_f = open(TESTFILE, flag); if (wjh_f == -1) { tst_resm(TFAIL | TERRNO, "open(%s) failed", TESTFILE); tst_rmdir(); tst_exit(); } wjh_ret = ftruncate(wjh_f, trunc_size); #ifdef DEBUG printf("DEBUG: fd: %d ret: %d @ errno(%d) %s\n", wjh_f, wjh_ret, errno, strerror(errno)); #endif if ((flag == O_RDONLY) && (wjh_ret == -1) && (errno == EINVAL)) { tst_resm(TPASS, "Test Passed"); } else if ((flag == O_RDWR)) { if (wjh_ret == 0) { tst_resm(TPASS, "Test Succeeded!"); } else { tst_resm(TFAIL | TERRNO, "ftruncate(%s) should have succeeded, but didn't! ret=" "%d (wanted 0)", TESTFILE, wjh_ret); } } else //flag was O_RDONLY but return codes wrong { tst_resm(TFAIL, "ftruncate(rd_only_fd)=%i (wanted -1), errno=%i (wanted %i EINVAL)", wjh_ret, errno, EINVAL); } close(wjh_f); errno = 0; wjh_ret = 0; wjh_f = -1; //TEST3: invalid socket descriptor should fail w/ EBADF #ifdef DEBUG printf("\nStarting test3\n"); #endif wjh_f = -999999; //should be a bad file descriptor wjh_ret = ftruncate(wjh_f, trunc_size); #ifdef DEBUG printf("DEBUG: fd: %d ret: %d @ errno(%d) %s\n", wjh_f, wjh_ret, errno, strerror(errno)); #endif if (wjh_ret != -1 || errno != EBADF) { tst_resm(TFAIL | TERRNO, "ftruncate(invalid_fd)=%d (wanted -1 and EBADF)", wjh_ret); } else { tst_resm(TPASS, "Test Passed"); } tst_rmdir(); //Done Testing tst_exit(); }
int main(int argc, char **argv) { int c, i; int file_size; int num_iter; double exec_time; int fd; void *status; pthread_t thid[2]; long chld_args[3]; extern char *optarg; struct sigaction sigptr; int ret; /* set up the default values */ file_size = 1024; num_iter = 1000; exec_time = 24; while ((c = getopt(argc, argv, "hvl:s:x:")) != -1) { switch (c) { case 'h': usage(argv[0]); break; case 'l': if ((num_iter = atoi(optarg)) == 0) OPT_MISSING(argv[0], optopt); else if (num_iter < 0) printf ("WARNING: bad argument. Using default %d\n", (num_iter = 1000)); break; case 's': if ((file_size = atoi(optarg)) == 0) OPT_MISSING(argv[0], optopt); else if (file_size < 0) printf ("WARNING: bad argument. Using default %d\n", (file_size = 1024)); break; case 'v': verbose_print = 1; break; case 'x': exec_time = atof(optarg); if (exec_time == 0) OPT_MISSING(argv[0], optopt); else if (exec_time < 0) printf ("WARNING: bad argument. Using default %.0f\n", (exec_time = 24)); break; default: usage(argv[0]); break; } } /* We don't want other mmap calls to map into same area as is * used for test (mmap_address). The test expects read to return * test pattern or read must fail with SIGSEGV. Find an area * that we can use, which is unlikely to be chosen for other * mmap calls. */ distant_area = mmap(0, DISTANT_MMAP_SIZE, PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); if (distant_area == (void *)-1) tst_brkm(TBROK | TERRNO, NULL, "distant_area: mmap()"); SAFE_MUNMAP(NULL, distant_area, (size_t)DISTANT_MMAP_SIZE); distant_area += DISTANT_MMAP_SIZE / 2; if (verbose_print) tst_resm(TINFO, "Input parameters are: File size: %d; " "Scheduled to run: %lf hours; " "Number of mmap/write/read: %d", file_size, exec_time, num_iter); alarm(exec_time * 3600); /* Do not mask SIGSEGV, as we are interested in handling it. */ sigptr.sa_sigaction = sig_handler; sigfillset(&sigptr.sa_mask); sigdelset(&sigptr.sa_mask, SIGSEGV); sigptr.sa_flags = SA_SIGINFO | SA_NODEFER; for (i = 0; sig_info[i].signum != -1; i++) { if (sigaction(sig_info[i].signum, &sigptr, NULL) == -1) { perror("man(): sigaction()"); fprintf(stderr, "could not set handler for %s, errno = %d\n", sig_info[i].signame, errno); exit(-1); } } tst_tmpdir(); for (;;) { if ((fd = mkfile(file_size)) == -1) tst_brkm(TBROK, NULL, "main(): mkfile(): Failed to create temp file"); if (verbose_print) tst_resm(TINFO, "Tmp file created"); chld_args[0] = fd; chld_args[1] = file_size; chld_args[2] = num_iter; if ((ret = pthread_create(&thid[0], NULL, map_write_unmap, chld_args))) tst_brkm(TBROK, NULL, "main(): pthread_create(): %s", strerror(ret)); tst_resm(TINFO, "created writing thread[%lu]", thid[0]); if ((ret = pthread_create(&thid[1], NULL, read_mem, chld_args))) tst_brkm(TBROK, NULL, "main(): pthread_create(): %s", strerror(ret)); tst_resm(TINFO, "created reading thread[%lu]", thid[1]); for (i = 0; i < 2; i++) { if ((ret = pthread_join(thid[i], &status))) tst_brkm(TBROK, NULL, "main(): pthread_join(): %s", strerror(ret)); if (status) tst_brkm(TFAIL, NULL, "thread [%lu] - process exited " "with %ld", thid[i], (long)status); } close(fd); } tst_rmdir(); exit(0); }
/* * setup() - performs all ONE TIME setup for this test. * * Get system page size, allocate and initialize the string dummy. * Initialize addr such that it is more than one page below the break * address of the process, and initialize one page region from addr * with char 'A'. * Creat a temporary directory and a file under it. * Write some known data into file and get the size of the file. */ void setup() { struct stat stat_buf; char Path_name[PATH_MAX]; /* pathname of temporary file*/ char write_buf[] = "hello world\n"; /* capture signals */ tst_sig(FORK, DEF_HANDLER, cleanup); /* Pause if that option was specified */ TEST_PAUSE; /* make a temp directory and cd to it */ tst_tmpdir(); /* Get the path of temporary file to be created */ if (getcwd(Path_name, sizeof(Path_name)) == NULL) { tst_brkm(TFAIL, cleanup, "getcwd fails to get current working directory"); } /* Creat a temporary file used for mapping */ if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) { tst_brkm(TFAIL, cleanup, "open() on %s Failed, errno=%d : %s", TEMPFILE, errno, strerror(errno)); } /* Write some data into temporary file */ if (write(fildes, write_buf, strlen(write_buf)) != strlen(write_buf)) { tst_brkm(TFAIL, cleanup, "write() on %s Failed, errno=%d : %s", TEMPFILE, errno, strerror(errno)); } /* Get the size of temporary file */ if (stat(TEMPFILE, &stat_buf) < 0) { tst_brkm(TFAIL, cleanup, "stat() on %s Failed, errno=%d : %s", TEMPFILE, errno, strerror(errno)); } file_sz = stat_buf.st_size; /* Get the system page size */ if ((page_sz = getpagesize()) < 0) { tst_brkm(TFAIL, cleanup, "getpagesize() fails to get system page size"); tst_exit(); } /* Allocate and initialize dummy string of system page size bytes */ if ((dummy = (char *)calloc(page_sz, sizeof(char))) == NULL) { tst_brkm(TFAIL, cleanup, "calloc() failed to allocate space"); tst_exit(); } /* * Initialize addr to align with the first page boundary above the * break address of the process. */ addr = (void *) (((intptr_t)sbrk(0) + (page_sz - 1)) & ~(page_sz - 1)); /* Increase the break address of the process by 2 page size bytes */ if ((intptr_t)sbrk(2 * page_sz) == -1) { tst_brkm(TFAIL, cleanup, "sbrk(2 * page_sz) failed"); } /* Initialize one page region from addr with 'A' */ memset(addr, 'A', page_sz); /* Create the command which will be executed in the test */ sprintf(Cmd_buffer, "grep XYZ %s/%s > /dev/null", Path_name, TEMPFILE); }
int main(void) { int k, j, pid, child, status, count; char name[128]; /* * Default values for run conditions. */ iterations = 50; nchild = 5; if (signal(SIGTERM, term) == SIG_ERR) { tst_resm(TFAIL, "first signal failed"); } /* * Make a directory to do this in; ignore error if already exists. */ local_flag = PASSED; parent_pid = getpid(); tst_tmpdir(); if (!startdir[0]) { if (getcwd(startdir, MAXPATHLEN) == NULL) { tst_resm(TBROK, "getcwd failed"); } } cwd = startdir; strcat(dirname, cwd); sprintf(tmpname, "/ftest02.%d", getpid()); strcat(dirname, tmpname); strcat(homedir, cwd); sprintf(tmpname, "/ftest02h.%d", getpid()); strcat(homedir, tmpname); mkdir(dirname, 0755); mkdir(homedir, 0755); if (chdir(dirname) < 0) { tst_resm(TBROK, "\tCan't chdir(%s), error %d.", dirname, errno); cleanup(); } dirlen = strlen(dirname); if (chdir(homedir) < 0) { tst_resm(TBROK, "\tCan't chdir(%s), error %d.", homedir, errno); cleanup(); } for (k = 0; k < nchild; k++) { if ((child = fork()) == 0) { dotest(k, iterations); exit(0); } if (child < 0) { tst_brkm(TBROK | TERRNO, cleanup, "fork failed"); } pidlist[k] = child; } /* * Wait for children to finish. */ count = 0; while ((child = wait(&status)) > 0) { //tst_resm(TINFO,"Test{%d} exited status = 0x%x", child, status); //tst_resm(TINFO,"status is %d",status); if (status) { tst_resm(TFAIL, "Test{%d} failed, expected 0 exit.", child); local_flag = FAILED; } ++count; } /* * Should have collected all children. */ if (count != nchild) { tst_resm(TFAIL, "Wrong # children waited on, count = %d", count); local_flag = FAILED; } if (local_flag == FAILED) tst_resm(TFAIL, "Test failed in fork-wait part."); else tst_resm(TPASS, "Test passed in fork-wait part."); if (iterations > 26) iterations = 26; for (k = 0; k < nchild; k++) for (j = 0; j < iterations + 1; j++) { ft_mkname(name, dirname, k, j); rmdir(name); unlink(name); } chdir(startdir); pid = fork(); if (pid < 0) { tst_brkm(TBROK | TERRNO, cleanup, "fork failed"); } if (pid == 0) { execl("/bin/rm", "rm", "-rf", homedir, NULL); exit(1); } else wait(&status); if (status) tst_resm(TINFO, "CAUTION - ftest02, '%s' may not have been removed.", homedir); pid = fork(); if (pid < 0) { tst_brkm(TBROK | TERRNO, cleanup, "fork failed"); } if (pid == 0) { execl("/bin/rm", "rm", "-rf", dirname, NULL); exit(1); } else wait(&status); if (status) { tst_resm(TINFO, "CAUTION - ftest02, '%s' may not have been removed.", dirname); } sync(); cleanup(); tst_exit(); }
/* * void * setup() - performs all ONE TIME setup for this test. * Create a temporary directory and change directory to it. * Create a test file under temporary directory and close it * Change the ownership of testfile to that of "bin" user. * Record the current time. */ void setup(void) { int fildes; /* file handle for temp file */ char *tmpd = NULL; tst_require_root(NULL); tst_sig(FORK, DEF_HANDLER, cleanup); /* Pause if that option was specified * TEST_PAUSE contains the code to fork the test with the -i option. * You want to make sure you do this before you create your temporary * directory. */ TEST_PAUSE; tst_tmpdir(); /* get the name of the temporary directory */ if ((tmpd = getcwd(tmpd, 0)) == NULL) { tst_brkm(TBROK, NULL, "getcwd failed"); } /* Creat a temporary file under above directory */ if ((fildes = creat(TEMP_FILE, FILE_MODE)) == -1) { tst_brkm(TBROK, cleanup, "creat(%s, %#o) Failed, errno=%d :%s", TEMP_FILE, FILE_MODE, errno, strerror(errno)); } /* Close the temporary file created */ if (close(fildes) < 0) { tst_brkm(TBROK, cleanup, "close(%s) Failed, errno=%d : %s:", TEMP_FILE, errno, strerror(errno)); } /* * Make sure that specified Mode permissions set as * umask value may be different. */ if (chmod(TEMP_FILE, FILE_MODE) < 0) { tst_brkm(TBROK, cleanup, "chmod(%s) Failed, errno=%d : %s:", TEMP_FILE, errno, strerror(errno)); } if (chmod(tmpd, 0711) != 0) { tst_brkm(TBROK, cleanup, "chmod() failed"); } if ((ltpuser = getpwnam(LTPUSER2)) == NULL) { tst_brkm(TBROK, cleanup, "%s not found in /etc/passwd", LTPUSER2); } /* get uid/gid of user accordingly */ user_uid = ltpuser->pw_uid; group_gid = ltpuser->pw_gid; /* * Change the ownership of test directory/file specified by * pathname to that of user_uid and group_gid. */ if (chown(TEMP_FILE, user_uid, group_gid) < 0) { tst_brkm(TBROK, cleanup, "chown() of %s failed, error %d", TEMP_FILE, errno); } /* Get the current time */ if ((curr_time = time(&tloc)) < 0) { tst_brkm(TBROK, cleanup, "time() failed to get current time, errno=%d", errno); } /* * Sleep for a second so that mod time and access times will be * different from the current time */ sleep(2); /* sleep(1) on IA64 sometimes sleeps < 1 sec!! */ }
int main(int argc, char **argv) { int lc; /* loop counter */ char *msg; /* message returned from parse_opts */ char cmd[50]; /* parse standard options */ if ((msg = parse_opts(argc, argv, (option_t*) NULL, NULL)) != (char *) NULL) { tst_brkm(TBROK, tst_exit, "OPTION PARSING ERROR - %s", msg); } if(STD_COPIES != 1) { tst_resm(TINFO, "-c option has no effect for this testcase - " "doesn't allow running more than one instance " "at a time"); STD_COPIES = 1; } /* Load first kernel module */ if( sprintf(cmd, "/sbin/insmod %s/%s.ko", dirname(argv[0]), DUMMY_MOD) <= 0) { tst_resm(TBROK, "sprintf failed"); return 1; } if( (system(cmd)) != 0 ) { tst_resm(TBROK, "Failed to load %s module", DUMMY_MOD); return 1; } /* Load dependant kernel module */ if( sprintf(cmd, "/sbin/insmod %s/%s.ko", dirname(argv[0]), DUMMY_MOD_DEP) <= 0) { tst_resm(TBROK, "sprintf failed"); goto END; } if( (system(cmd)) != 0 ) { tst_resm(TBROK, "Failed to load %s module", DUMMY_MOD_DEP); goto END; } tst_tmpdir(); if(setup() != 0) { return 1; } /* check looping state if -i option is given */ for (lc = 0; TEST_LOOPING(lc); lc++) { /* reset Tst_count in case we are looping */ Tst_count = 0; /* Test the system call */ TEST(delete_module(DUMMY_MOD)); TEST_ERROR_LOG(TEST_ERRNO); if ( (TEST_RETURN == (int) EXP_RET_VAL ) && (TEST_ERRNO == EXP_ERRNO) ) { tst_resm(TPASS, "Expected failure for module in-use, " "errno: %d", TEST_ERRNO); } else { tst_resm(TFAIL, "Unexpected results for module in-use; " "returned %d (expected %d), errno %d " "(expected %d)", TEST_RETURN, EXP_RET_VAL, TEST_ERRNO, EXP_ERRNO); } } cleanup(); END: if(system("rmmod "DUMMY_MOD) != 0) { tst_resm(TBROK, "Failed to unload %s module", DUMMY_MOD); return 1; } /*NOTREACHED*/ return 0; }
/* * setup() - performs all ONE TIME setup for this test. */ void setup() { char *cwdname = NULL; int fd; umask(0); /* capture signals */ tst_sig(NOFORK, DEF_HANDLER, cleanup); /* Pause if that option was specified */ TEST_PAUSE; ltpuser = getpwnam(nobody_uid); if (setgid(ltpuser->pw_uid) == -1) { tst_resm(TINFO, "setgid failed to " "to set the gid to %d", ltpuser->pw_uid); perror("setgid"); } if (setuid(ltpuser->pw_uid) == -1) { tst_resm(TINFO, "setuid failed to " "to set the uid to %d", ltpuser->pw_uid); perror("setuid"); } /* make a temporary directory and cd to it */ tst_tmpdir(); /* * set up a name that should generate an ENOTDIR error */ if ((cwdname = getcwd(cwdname, 0)) == NULL) { tst_brkm(TBROK, cleanup, "could not get currect directory"); } sprintf(test_name5, "%s/fake.%d", cwdname, getpid()); if ((fileHandle = creat(test_name5, 0444)) == -1) { tst_brkm(TBROK, cleanup, "creat(2) FAILED to create temp file"); } sprintf(test_name3, "%s/fake.%d", test_name5, getpid()); /* creat() and close a zero length file with executeable permission */ sprintf(test_name6, "%s/execve03.%d", cwdname, getpid()); if ((fd = creat(test_name6, 0755)) == -1) { tst_brkm(TBROK, cleanup, "creat() failed"); } if (close(fd) == -1) { tst_brkm(TBROK, cleanup, "close() failed"); } bad_addr = mmap(0, 1, PROT_NONE, MAP_PRIVATE_EXCEPT_UCLINUX|MAP_ANONYMOUS, 0, 0); if (bad_addr == MAP_FAILED) { tst_brkm(TBROK, cleanup, "mmap failed"); } TC[3].tname = bad_addr; }
/*--------------------------------------------------------------------*/ int main(int ac, char *av[]) { int fildes; int ret = 0; char pbuf[BUFSIZ]; int lc; /* loop counter */ char *msg; /* message returned from parse_opts */ int fail_count = 0; /* * parse standard options */ if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { tst_resm(TBROK, "OPTION PARSING ERROR - %s", msg); tst_exit(); } tst_tmpdir(); local_flag = PASSED; sprintf(tempfile, "open09.%d", getpid()); /*--------------------------------------------------------------------*/ for (lc = 0; TEST_LOOPING(lc); lc++) { local_flag = PASSED; //block0: if ((fildes = creat(tempfile, 0600)) == -1) { tst_resm(TBROK, "\t\t\tcan't create '%s'", tempfile); tst_exit(); } else { close(fildes); if ((fildes = open(tempfile, 1)) == -1) { tst_resm(TFAIL, "\t\t\topen failed"); tst_exit(); } } ret = read(fildes, pbuf, 1); if (ret != -1) { tst_resm(TFAIL, "read should not succeed"); local_flag = FAILED; } close(fildes); if (local_flag == PASSED) { tst_resm(TPASS, "Test passed in block0."); } else { tst_resm(TFAIL, "Test failed in block0."); fail_count++; } local_flag = PASSED; /*--------------------------------------------------------------------*/ //block1: if ((fildes = open(tempfile, 0)) == -1) { tst_resm(TFAIL, "\t\t\topen failed"); local_flag = FAILED; } else { ret = write(fildes, pbuf, 1); if (ret != -1) { tst_resm(TFAIL, "writeshould not succeed"); local_flag = FAILED; } } close(fildes); if (local_flag == PASSED) { tst_resm(TPASS, "Test passed in block1."); } else { tst_resm(TFAIL, "Test failed in block1."); fail_count++; } /*--------------------------------------------------------------------*/ unlink(tempfile); tst_rmdir(); if (fail_count == 0) { tst_resm(TPASS, "Test passed."); } else { tst_resm(TFAIL, "Test failed due to above failures."); } } /* end for */ tst_exit(); }
static void setup(void) { int ret; tst_sig(NOFORK, DEF_HANDLER, cleanup); TEST_PAUSE; fs_type = tst_dev_fs_type(); tst_tmpdir(); device = tst_acquire_device(cleanup); if (!device) tst_brkm(TCONF, cleanup, "Failed to obtain block device"); tst_mkfs(cleanup, device, fs_type, NULL, NULL); if (mkdir(mntpoint, DIR_MODE) < 0) { tst_brkm(TBROK | TERRNO, cleanup, "mkdir(%s, %#o) failed", mntpoint, DIR_MODE); } /* Call mount(2) */ tst_resm(TINFO, "mount %s to %s fs_type=%s", device, mntpoint, fs_type); TEST(mount(device, mntpoint, fs_type, 0, NULL)); /* check return code */ if (TEST_RETURN != 0) { tst_brkm(TBROK | TTERRNO, cleanup, "mount(2) failed"); } mount_flag = 1; sprintf(fname, "%s/tfile_%d", mntpoint, getpid()); fd = open(fname, O_RDWR | O_CREAT, 0700); if (fd == -1) { tst_brkm(TBROK | TERRNO, cleanup, "open(%s, O_RDWR|O_CREAT,0700) failed", fname); } ret = write(fd, fname, 1); if (ret == -1) { tst_brkm(TBROK | TERRNO, cleanup, "write(%d, %s, 1) failed", fd, fname); } /* close the file we have open */ if (close(fd) == -1) tst_brkm(TBROK | TERRNO, cleanup, "close(%s) failed", fname); fd_notify = myinotify_init(); if (fd_notify < 0) { if (errno == ENOSYS) tst_brkm(TCONF, cleanup, "inotify is not configured in this kernel."); else tst_brkm(TBROK | TERRNO, cleanup, "inotify_init failed"); } wd = myinotify_add_watch(fd_notify, fname, IN_ALL_EVENTS); if (wd < 0) tst_brkm(TBROK | TERRNO, cleanup, "inotify_add_watch (%d, %s, IN_ALL_EVENTS) failed.", fd_notify, fname); }
/*--------------------------------------------------------------------*/ int main(int ac, char *av[]) { FILE *stream; char *junk = "abcdefghijklmnopqrstuvwxyz"; char *inbuf; int ret; int lc; const char *msg; /* * parse standard options */ if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); tst_tmpdir(); for (lc = 0; TEST_LOOPING(lc); lc++) { local_flag = PASSED; sprintf(tempfile1, "stream04.%d", getpid()); /*--------------------------------------------------------------------*/ //block0: if ((stream = fopen(tempfile1, "a+")) == NULL) { tst_resm(TFAIL | TERRNO, "fopen(%s) a+ failed", tempfile1); tst_rmdir(); tst_exit(); } /* write something and check */ if ((ret = fwrite(junk, sizeof(*junk), strlen(junk), stream)) == 0) { tst_resm(TFAIL, "fwrite failed: %s", strerror(errno)); tst_rmdir(); tst_exit(); } if ((size_t) ret != strlen(junk)) { tst_resm(TFAIL, "strlen(junk) = %zi != return value from fwrite = %zi", strlen(junk), ret); local_flag = FAILED; } fclose(stream); if ((stream = fopen(tempfile1, "r+")) == NULL) { tst_resm(TFAIL, "fopen(%s) r+ failed: %s", tempfile1, strerror(errno)); tst_rmdir(); tst_exit(); } if ((inbuf = malloc(strlen(junk))) == 0) { tst_resm(TBROK, "test failed because of malloc: %s", strerror(errno)); tst_rmdir(); tst_exit(); } if ((ret = fread(inbuf, sizeof(*junk), strlen(junk), stream)) == 0) { tst_resm(TFAIL, "fread failed: %s", strerror(errno)); tst_rmdir(); tst_exit(); } if ((size_t) ret != strlen(junk)) { tst_resm(TFAIL, "strlen(junk) = %zi != return value from fread = %zi", strlen(junk), ret); local_flag = FAILED; } fclose(stream); if (local_flag == PASSED) { tst_resm(TPASS, "Test passed."); } else { tst_resm(TFAIL, "Test failed."); } /*--------------------------------------------------------------------*/ unlink(tempfile1); } /* end for */ tst_rmdir(); tst_exit(); }
/* * setup() - performs all ONE TIME setup for this test. * Get the system page size. * Create a temporary directory and a file under it. * Write some known data into file and close it. * Change the mode permissions on file to 0555. * Re-open the file for reading. */ void setup() { char *tst_buff; /* test buffer to hold known data */ /* capture signals */ tst_sig(NOFORK, sig_handler, cleanup); /* Pause if that option was specified */ TEST_PAUSE; /* Get the system page size */ if ((page_sz = getpagesize()) < 0) { tst_brkm(TFAIL, NULL, "getpagesize() fails to get system page size"); tst_exit(); } /* Allocate space for the test buffer */ if ((tst_buff = (char *)calloc(page_sz, sizeof(char))) == NULL) { tst_brkm(TFAIL, NULL, "calloc() failed to allocate space for tst_buff"); tst_exit(); } /* Fill the test buffer with the known data */ memset(tst_buff, 'A', page_sz); /* make a temp directory and cd to it */ tst_tmpdir(); /* Creat a temporary file used for mapping */ if ((fildes = open(TEMPFILE, O_WRONLY | O_CREAT, 0666)) < 0) { tst_brkm(TFAIL, NULL, "open() on %s Failed, errno=%d : %s", TEMPFILE, errno, strerror(errno)); free(tst_buff); cleanup(); } /* Write test buffer contents into temporary file */ if (write(fildes, tst_buff, page_sz) < page_sz) { tst_brkm(TFAIL, NULL, "write() on %s Failed, errno=%d : %s", TEMPFILE, errno, strerror(errno)); free(tst_buff); cleanup(); } /* Free the memory allocated for test buffer */ free(tst_buff); /* Make sure proper permissions set on file */ if (chmod(TEMPFILE, 0555) < 0) { tst_brkm(TFAIL, cleanup, "chmod() on %s Failed, errno=%d : %s", TEMPFILE, errno, strerror(errno)); } /* Close the temporary file opened for write */ if (close(fildes) < 0) { tst_brkm(TFAIL, cleanup, "close() on %s Failed, errno=%d : %s", TEMPFILE, errno, strerror(errno)); } /* Allocate and initialize dummy string of system page size bytes */ if ((dummy = (char *)calloc(page_sz, sizeof(char))) == NULL) { tst_brkm(TFAIL, cleanup, "calloc() failed to allocate memory for dummy"); } /* Open the temporary file again for reading */ if ((fildes = open(TEMPFILE, O_RDONLY)) < 0) { tst_brkm(TFAIL, cleanup, "open(%s) with read-only Failed, " "errno:%d", TEMPFILE, errno); } }
static void setup(void) { tst_require_root(NULL); tst_tmpdir(); }
/*--------------------------------------------------------------------*/ int main(int ac, char *av[]) { FILE *stream; char buf[10]; int nr, fd; int lc; /* * parse standard options */ tst_parse_opts(ac, av, NULL, NULL); tst_tmpdir(); local_flag = PASSED; for (lc = 0; TEST_LOOPING(lc); lc++) { local_flag = PASSED; sprintf(tempfile, "stream05.%d", getpid()); /*--------------------------------------------------------------------*/ //block0: if ((stream = fopen(tempfile, "a+")) == NULL) { tst_brkm(TFAIL, NULL, "fopen(%s) a+ failed: %s", tempfile, strerror(errno)); } fprintf(stream, "a"); fclose(stream); if ((stream = fopen(tempfile, "r+")) == NULL) { tst_brkm(TFAIL, NULL, "fopen(%s) r+ failed: %s", tempfile, strerror(errno)); } /* check that ferror returns zero */ if (ferror(stream) != 0) { tst_resm(TFAIL, "ferror did not return zero: %s", strerror(errno)); local_flag = FAILED; } if (local_flag == PASSED) { tst_resm(TPASS, "Test passed in block0."); } else { tst_resm(TFAIL, "Test failed in block0."); } local_flag = PASSED; /*--------------------------------------------------------------------*/ //block1: /* check that fileno returns valid file descriptor */ fd = fileno(stream); if ((nr = read(fd, buf, 1)) < 0) { tst_brkm(TFAIL, NULL, "read failed: %s", strerror(errno)); } if (nr != 1) { tst_resm(TFAIL, "read did not read right number"); local_flag = FAILED; } if (buf[0] != 'a') { tst_resm(TFAIL, "read returned bad values"); local_flag = FAILED; } if (local_flag == PASSED) { tst_resm(TPASS, "Test passed in block1."); } else { tst_resm(TFAIL, "Test failed in block1."); } local_flag = PASSED; /*--------------------------------------------------------------------*/ //block2: /* read to EOF and ensure feof returns non-zero */ fclose(stream); if ((stream = fopen(tempfile, "r+")) == NULL) { tst_brkm(TFAIL, NULL, "fopen(%s) r+ failed: %s", tempfile, strerror(errno)); } if (feof(stream) != 0) { tst_resm(TFAIL, "feof returned non-zero when it should not: %s", strerror(errno)); local_flag = FAILED; } fread(buf, 1, 2, stream); /* read to EOF */ if (feof(stream) == 0) { tst_resm(TFAIL, "feof returned zero when it should not: %s", strerror(errno)); local_flag = FAILED; } if (local_flag == PASSED) { tst_resm(TPASS, "Test passed in block2."); } else { tst_resm(TFAIL, "Test failed in block2."); } local_flag = PASSED; /*--------------------------------------------------------------------*/ //block3: /* ensure clearerr works */ clearerr(stream); if (feof(stream) != 0) { tst_resm(TFAIL, "clearerr failed: %s", strerror(errno)); local_flag = FAILED; } if (local_flag == PASSED) { tst_resm(TPASS, "Test passed in block3."); } else { tst_resm(TFAIL, "Test failed in block3."); } local_flag = PASSED; /*--------------------------------------------------------------------*/ //block4: /* test fopen "b" flags -- should be allowed but ignored */ (void)fclose(stream); if ((stream = fopen(tempfile, "rb")) == NULL) { tst_brkm(TFAIL, NULL, "fopen(%s) rb failed: %s", tempfile, strerror(errno)); } (void)fclose(stream); if ((stream = fopen(tempfile, "wb")) == NULL) { tst_brkm(TFAIL, NULL, "fopen(%s) wb failed: %s", tempfile, strerror(errno)); } (void)fclose(stream); if ((stream = fopen(tempfile, "ab")) == NULL) { tst_brkm(TFAIL, NULL, "fopen(%s) ab failed: %s", tempfile, strerror(errno)); } (void)fclose(stream); if ((stream = fopen(tempfile, "rb+")) == NULL) { tst_brkm(TFAIL, NULL, "fopen(%s) rb+ failed: %s", tempfile, strerror(errno)); } (void)fclose(stream); if ((stream = fopen(tempfile, "wb+")) == NULL) { tst_brkm(TFAIL, NULL, "fopen(%s) wb+ failed: %s", tempfile, strerror(errno)); } (void)fclose(stream); if ((stream = fopen(tempfile, "ab+")) == NULL) { tst_brkm(TFAIL, NULL, "fopen(%s) ab+ failed: %s", tempfile, strerror(errno)); } (void)fclose(stream); tst_resm(TPASS, "Test passed in block4."); /*--------------------------------------------------------------------*/ unlink(tempfile); } /* end for */ tst_rmdir(); tst_exit(); }
/*--------------------------------------------------------------------*/ int main(int ac, char *av[]) { FILE *stream; int fd; int lc; char *msg; /* * parse standard options */ if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); local_flag = PASSED; tst_tmpdir(); for (lc = 0; TEST_LOOPING(lc); lc++) { sprintf(tempfile1, "stream1.%d", getpid()); /*--------------------------------------------------------------------*/ //block0: if (mknod(tempfile1, (S_IFIFO|0666), 0) != 0) { tst_resm(TFAIL,"mknod failed in block0: %s", strerror(errno)); local_flag = FAILED; goto block1; } if ((stream=fopen(tempfile1,"w+")) == NULL) { tst_resm(TFAIL,"fopen(%s) w+ failed for pipe file: %s", tempfile1, strerror(errno)); local_flag = FAILED; } else { fclose(stream); } if ((stream=fopen(tempfile1,"a+")) == NULL) { tst_resm(TFAIL,"fopen(%s) a+ failed: %s", tempfile1, strerror(errno)); local_flag = FAILED; } else { fclose(stream); unlink(tempfile1); } if (local_flag == PASSED) { tst_resm(TPASS, "Test passed in block0."); } else { tst_resm(TFAIL, "Test failed in block0."); } local_flag = PASSED; /*--------------------------------------------------------------------*/ block1 : if ((fd = open("/dev/tty", O_WRONLY)) >= 0) { close(fd); if (( stream = fopen("/dev/tty","w"))==NULL) { tst_resm(TFAIL|TERRNO,"fopen(/dev/tty) write failed"); local_flag = FAILED; } else { fclose(stream); } } if (local_flag == PASSED) { tst_resm(TPASS, "Test passed in block1."); } else { tst_resm(TFAIL, "Test failed in block1."); } /*--------------------------------------------------------------------*/ } /* end for */ tst_rmdir(); tst_exit(); }
/* * setup() - performs all the ONE TIME setup for this test. */ void setup(void) { key_t shmkey2; /* Switch to nobody user for correct error code collection */ if (geteuid() != 0) { tst_brkm(TBROK, tst_exit, "Test must be run as root"); } ltpuser = getpwnam(nobody_uid); if (setuid(ltpuser->pw_uid) == -1) { tst_resm(TINFO, "setuid failed to " "to set the effective uid to %d", ltpuser->pw_uid); perror("setuid"); } /* capture signals */ tst_sig(NOFORK, DEF_HANDLER, cleanup); /* Set up the expected error numbers for -e option */ TEST_EXP_ENOS(exp_enos); /* Pause if that option was specified */ TEST_PAUSE; /* * Create a temporary directory and cd into it. * This helps to ensure that a unique msgkey is created. * See ../lib/libipc.c for more information. */ tst_tmpdir(); /* get an IPC resource key */ shmkey = getipckey(); /* create a shared memory resource with read and write permissions */ /* also post increment the shmkey for the next shmget call */ if ((shm_id_2 = shmget(shmkey, INT_SIZE, SHM_RW | IPC_CREAT | IPC_EXCL)) == -1) { tst_brkm(TBROK, cleanup, "Failed to create shared memory " "resource #1 in setup()"); } /* Get an new IPC resource key. */ shmkey2 = getipckey(); /* create a shared memory resource without read and write permissions */ if ((shm_id_3 = shmget(shmkey2, INT_SIZE, IPC_CREAT | IPC_EXCL)) == -1) { tst_brkm(TBROK, cleanup, "Failed to create shared memory " "resource #2 in setup()"); } /* Probe an available linear address for attachment */ if ((base_addr = shmat(shm_id_2, NULL, 0)) == (void *)-1) { tst_brkm(TBROK, cleanup, "Couldn't attach shared memory"); } if (shmdt((const void *)base_addr) == -1) { tst_brkm(TBROK, cleanup, "Couldn't detach shared memory"); } /* some architectures (e.g. parisc) are strange, so better always align to * next SHMLBA address. */ base_addr = (void *)(((unsigned long)(base_addr) & ~(SHMLBA - 1)) + SHMLBA); }
void setup() { /* Capture signals if any */ /* Create temporary directories */ TEST_PAUSE; tst_tmpdir(); }
/* * void * setup() - Performs setup function for the test. * Creat a temporary directory and change directory to it. * Creat a testfile and write some data into it. * Modify the mode permissions of testfile such that test process * has read-only access to testfile. */ void setup() { int i, fd; /* counter, file handle for file */ char tst_buff[BUF_SIZE]; /* data buffer for file */ int wbytes; /* no. of bytes written to file */ int write_len = 0; /* capture signals */ tst_sig(NOFORK, DEF_HANDLER, cleanup); #if 0 /* Switch to nobody user for correct error code collection */ if (geteuid() != 0) { tst_brkm(TBROK, tst_exit, "Test must be run as root"); } ltpuser = getpwnam(nobody_uid); if (setuid(ltpuser->pw_uid) == -1) { tst_resm(TINFO, "setuid failed to " "to set the effective uid to %d", ltpuser->pw_uid); perror("setuid"); } #endif /* Pause if that option was specified * TEST_PAUSE contains the code to fork the test with the -i option. * You want to make sure you do this before you create your temporary * directory. */ TEST_PAUSE; /* make a temp directory and cd to it */ tst_tmpdir(); if ((fd = open(TESTFILE, O_RDWR|O_CREAT, FILE_MODE)) == -1) { tst_brkm(TBROK, cleanup, "open(%s, O_RDWR|O_CREAT, %#o) Failed, errno=%d : %s", TESTFILE, FILE_MODE, errno, strerror(errno)); /*NOTREACHED*/ } /* Fill the test buffer with the known data */ for (i = 0; i < BUF_SIZE; i++) { tst_buff[i] = 'a'; } /* Write to the file 1k data from the buffer */ while (write_len < FILE_SIZE) { if ((wbytes = write(fd, tst_buff, sizeof(tst_buff))) <= 0) { tst_brkm(TBROK, cleanup, "write(2) on %s Failed, errno=%d : %s", TESTFILE, errno, strerror(errno)); /*NOTREACHED*/ } else { write_len += wbytes; } } if (close(fd) == -1) { tst_resm(TWARN, "close(%s) Failed, errno=%d : %s", TESTFILE, errno, strerror(errno)); } /* Modify mode permissions on the testfile */ if (chmod(TESTFILE, NEW_MODE) < 0) { tst_brkm(TBROK, cleanup, "chmod(2) on %s Failed, errno=%d : %s", TESTFILE, errno, strerror(errno)); /*NOTREACHED*/ } /* Get the uid/gid of the process */ User_id = getuid(); Group_id = getgid(); } /* End setup() */
/* * setup() - performs all ONE TIME setup for this test. * * Set up signal handler to catch SIGSEGV. * Get system page size, create a temporary file for reading/writing, * write one byte data into it, map the open file for the specified * map length. */ void setup() { size_t page_sz; /* system page size */ tst_sig(NOFORK, DEF_HANDLER, cleanup); /* call signal function to trap the signal generated */ if (signal(SIGSEGV, sig_handler) == SIG_ERR) { tst_brkm(TBROK, cleanup, "signal fails to catch signal"); tst_exit(); } TEST_PAUSE; /* Get the system page size */ if ((page_sz = getpagesize()) < 0) { tst_brkm(TBROK, cleanup, "getpagesize() fails to get system page size"); tst_exit(); } /* * Get the length of the open file to be mapped into process * address space. */ map_len = 3 * page_sz; tst_tmpdir(); /* Creat a temporary file used for mapping */ if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) { tst_brkm(TBROK, cleanup, "open() on %s Failed, errno=%d : %s", TEMPFILE, errno, strerror(errno)); tst_exit(); } /* * move the file pointer to maplength position from the beginning * of the file. */ if (lseek(fildes, map_len, SEEK_SET) == -1) { tst_brkm(TBROK, cleanup, "lseek() fails on %s, errno=%d : %s", TEMPFILE, errno, strerror(errno)); tst_exit(); } /* Write one byte into temporary file */ if (write(fildes, "a", 1) != 1) { tst_brkm(TBROK, cleanup, "write() on %s Failed, errno=%d : %s", TEMPFILE, errno, strerror(errno)); tst_exit(); } /* * map the open file 'TEMPFILE' from its beginning up to the maplength * into the calling process's address space at the system choosen * with read/write permissions to the the mapped region. */ #ifdef UCLINUX /* MAP_SHARED is not implemented on uClinux */ addr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_FILE | MAP_PRIVATE, fildes, 0); #else addr = mmap(0, map_len, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fildes, 0); #endif /* check for the return value of mmap system call */ if (addr == (char *)MAP_FAILED) { tst_brkm(TBROK, cleanup, "mmap() Failed on %s, errno=%d : %s", TEMPFILE, errno, strerror(errno)); tst_exit(); } }
static void setup(void) { FILE *fp; int fd; struct stat stat_buf; tst_require_root(NULL); if (stat(pathover, &stat_buf) == -1) { if (errno == ENOENT || errno == ENOTDIR) tst_brkm(TCONF, NULL, "file %s does not exist in the system", pathover); } tst_sig(FORK, DEF_HANDLER, cleanup); TEST_PAUSE; tst_tmpdir(); if (shmid != -1) { fp = fopen(PATH_SHMMAX, "r"); if (fp == NULL) tst_brkm(TBROK|TERRNO, cleanup, "fopen"); if (fgets(shmmax, BUFSIZ, fp) == NULL) tst_brkm(TBROK|TERRNO, cleanup, "fgets"); fclose(fp); if (atol(shmmax) < (long)(length / 2 * hugepagesize)) { restore_shmmax = 1; fd = open(PATH_SHMMAX, O_RDWR); if (fd == -1) tst_brkm(TBROK|TERRNO, cleanup, "open"); snprintf(buf, BUFSIZ, "%ld", (long)(length / 2 * hugepagesize)); if (write(fd, buf, strlen(buf)) != strlen(buf)) tst_brkm(TBROK|TERRNO, cleanup, "failed to change shmmax."); } } fp = fopen(path, "r+"); if (fp == NULL) tst_brkm(TBROK|TERRNO, cleanup, "fopen"); if (fgets(nr_hugepages, BUFSIZ, fp) == NULL) tst_brkm(TBROK|TERRNO, cleanup, "fgets"); fclose(fp); /* Remove trailing newline. */ nr_hugepages[strlen(nr_hugepages) - 1] = '\0'; tst_resm(TINFO, "original nr_hugepages is %s", nr_hugepages); fd = open(path, O_RDWR); if (fd == -1) tst_brkm(TBROK|TERRNO, cleanup, "open"); /* Reset. */ if (write(fd, "0", 1) != 1) tst_brkm(TBROK|TERRNO, cleanup, "write"); if (lseek(fd, 0, SEEK_SET) == -1) tst_brkm(TBROK|TERRNO, cleanup, "lseek"); snprintf(buf, BUFSIZ, "%zd", size); if (write(fd, buf, strlen(buf)) != strlen(buf)) tst_brkm(TBROK|TERRNO, cleanup, "failed to change nr_hugepages."); close(fd); fp = fopen(pathover, "r+"); if (fp == NULL) tst_brkm(TBROK|TERRNO, cleanup, "fopen"); if (fgets(nr_overcommit_hugepages, BUFSIZ, fp) == NULL) tst_brkm(TBROK|TERRNO, cleanup, "fgets"); fclose(fp); nr_overcommit_hugepages[strlen(nr_overcommit_hugepages) - 1] = '\0'; tst_resm(TINFO, "original nr_overcommit_hugepages is %s", nr_overcommit_hugepages); fd = open(pathover, O_RDWR); if (fd == -1) tst_brkm(TBROK|TERRNO, cleanup, "open"); /* Reset. */ if (write(fd, "0", 1) != 1) tst_brkm(TBROK|TERRNO, cleanup, "write"); if (lseek(fd, 0, SEEK_SET) == -1) tst_brkm(TBROK|TERRNO, cleanup, "lseek"); snprintf(buf, BUFSIZ, "%zd", size); if (write(fd, buf, strlen(buf)) != strlen(buf)) tst_brkm(TBROK|TERRNO, cleanup, "failed to change nr_hugepages."); close(fd); /* XXX (garrcoop): memory leak. */ snprintf(buf, BUFSIZ, "%s/hugemmap05", get_tst_tmpdir()); if (mkdir(buf, 0700) == -1) tst_brkm(TBROK|TERRNO, cleanup, "mkdir"); if (mount(NULL, buf, "hugetlbfs", 0, NULL) == -1) tst_brkm(TBROK|TERRNO, cleanup, "mount"); }
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 (unlink(tmpname) == -1) { ERROR("unlink failed"); anyfail(); } (void)time(&t); // (void)printf("%s: Finished %s", argv[0], ctime(&t)); ok_exit(); /* LTP Port */ return 0; }
static void setup(void) { TEST_PAUSE; tst_tmpdir(); }
/*--------------------------------------------------------------------*/ int main(int ac, char *av[]) { FILE *stream; char buf[10]; int i; int lc; char *msg; /* * parse standard options */ if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); local_flag = PASSED; tst_tmpdir(); for (lc = 0; TEST_LOOPING(lc); lc++) { sprintf(tempfile1, "stream011.%d", getpid()); sprintf(tempfile2, "stream012.%d", getpid()); /*--------------------------------------------------------------------*/ //block0: if ((stream = fopen(tempfile1, "a+")) == NULL) { tst_resm(TFAIL, "fopen(%s) a+ failed: %s", tempfile1, strerror(errno)); tst_exit(); } fwrite("a", 1, 1, stream); if ((stream = freopen(tempfile2, "a+", stream)) == NULL) { tst_brkm(TFAIL | TERRNO, NULL, "freopen(%s) a+ failed", tempfile2); } fwrite("a", 1, 1, stream); fclose(stream); /* now check that a single "a" is in each file */ if ((stream = fopen(tempfile1, "r")) == NULL) { tst_brkm(TFAIL | TERRNO, NULL, "fopen(%s) r failed", tempfile1); } else { for (i = 0; i < 10; i++) buf[i] = 0; fread(buf, 1, 1, stream); if ((buf[0] != 'a') || (buf[1] != 0)) { tst_resm(TFAIL, "bad contents in %s", tempfile1); local_flag = FAILED; } fclose(stream); } if ((stream = fopen(tempfile2, "r")) == NULL) { tst_brkm(TFAIL | TERRNO, NULL, "fopen(%s) r failed", tempfile2); } else { for (i = 0; i < 10; i++) buf[i] = 0; fread(buf, 1, 1, stream); if ((buf[0] != 'a') || (buf[1] != 0)) { tst_resm(TFAIL, "bad contents in %s", tempfile2); local_flag = FAILED; } fclose(stream); } if (local_flag == PASSED) { tst_resm(TPASS, "Test passed."); } else { tst_resm(TFAIL, "Test failed."); } local_flag = PASSED; /*--------------------------------------------------------------------*/ unlink(tempfile1); unlink(tempfile2); } /* end for */ tst_rmdir(); tst_exit(); }
/* * setup(void) - performs all ONE TIME setup for this test. * Exit the test program on receipt of unexpected signals. * Create a temporary directory used to hold test directories created * and change the directory to it. * Verify that pid of process executing the test is root. * Create a test directory on temporary directory and set the ownership * of test directory to guest user and process, change mode permissions * to set group id bit on it. * Set the effective uid/gid of the process to that of guest user. */ void setup(void) { tst_require_root(); /* Capture unexpected signals */ tst_sig(NOFORK, DEF_HANDLER, cleanup); TEST_PAUSE; /* Make a temp dir and cd to it */ tst_tmpdir(); /* fix permissions on the tmpdir */ if (chmod(".", 0711) != 0) { tst_brkm(TBROK, cleanup, "chmod() failed"); } /* Save the real user id of the current test process */ save_myuid = getuid(); /* Save the process id of the current test process */ mypid = getpid(); /* Get the node name to be created in the test */ sprintf(node_name, TNODE, mypid); /* Get the uid/gid of ltpuser user */ if ((user1 = getpwnam(LTPUSER)) == NULL) { tst_brkm(TBROK, cleanup, "%s not in /etc/passwd", LTPUSER); } user1_uid = user1->pw_uid; group1_gid = user1->pw_gid; /* Get the effective group id of the test process */ group2_gid = getegid(); /* * Create a test directory under temporary directory with the * specified mode permissions, with uid/gid set to that of guest * user and the test process. */ SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX); SAFE_CHOWN(cleanup, DIR_TEMP, user1_uid, group2_gid); SAFE_CHMOD(cleanup, DIR_TEMP, MODE_SGID); /* * Verify that test directory created with expected permission modes * and ownerships. */ SAFE_STAT(cleanup, DIR_TEMP, &buf); /* Verify modes of test directory */ if (!(buf.st_mode & S_ISGID)) { tst_brkm(TBROK, cleanup, "%s: Incorrect modes, setgid bit not set", DIR_TEMP); } /* Verify group ID of test directory */ if (buf.st_gid != group2_gid) { tst_brkm(TBROK, cleanup, "%s: Incorrect group", DIR_TEMP); } /* * Set the effective group id and user id of the test process * to that of guest user (nobody) */ SAFE_SETGID(cleanup, group1_gid); if (setreuid(-1, user1_uid) < 0) { tst_brkm(TBROK, cleanup, "Unable to set process uid to that of ltp user"); } /* Save the real group ID of the current process */ mygid = getgid(); /* Change directory to DIR_TEMP */ SAFE_CHDIR(cleanup, DIR_TEMP); }