/* * setup() - performs all ONE TIME setup for this test. */ void setup() { tst_require_root(NULL); tst_sig(NOFORK, DEF_HANDLER, cleanup); TEST_PAUSE; /* make a temporary directory and cd to it */ tst_tmpdir(); if (chmod(get_tst_tmpdir(), S_IRWXU) == -1) tst_brkm(TBROK | TERRNO, cleanup, "chmod(%s, 700) failed", get_tst_tmpdir()); /* create a test file */ sprintf(fname, "%s.%d", fname, getpid()); if (mkdir(fname, 0444) == -1) { tst_resm(TFAIL, "creat(2) FAILED to creat temp file"); } else { sprintf(path, "%s/%s", fname, fname); if ((fileHandle = creat(path, 0444)) == -1) tst_brkm(TFAIL | TERRNO, cleanup, "creat failed"); } ltpuser = getpwnam(nobody_uid); if (ltpuser == NULL) tst_brkm(TBROK | TERRNO, cleanup, "getpwnam failed"); if (seteuid(ltpuser->pw_uid) == -1) { tst_resm(TINFO | TERRNO, "seteuid failed to " "to set the effective uid to %d", ltpuser->pw_uid); } }
int main(int ac, char **av) { int lc; /* loop counter */ char *msg; /* message returned from parse_opts */ int pid, status, retval; /* parse standard options */ if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); setup(); /* Check for 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; if ((pid = FORK_OR_VFORK()) == -1) { tst_brkm(TBROK, cleanup, "Could not fork"); } if (pid == 0) { retval = 0; if (chroot(get_tst_tmpdir()) == -1) { perror("chroot failed"); retval = 1; } else { if (STD_FUNCTIONAL_TEST) { if (stat("/" TMP_FILENAME, &buf) == -1) { retval = 1; perror("stat failed"); } } } exit(retval); } /* parent */ wait(&status); /* make sure the child returned a good exit status */ if (WIFSIGNALED(status) || (WIFEXITED(status) && WEXITSTATUS(status) != 0)) tst_resm(TFAIL, "chroot functionality incorrect"); else tst_resm(TPASS, "chroot functionality correct"); } cleanup(); tst_exit(); }
int main(int ac, char **av) { int lc; char *msg; #if __WORDSIZE == 32 tst_brkm(TCONF, NULL, "This test is only for 64bit"); #endif msg = parse_opts(ac, av, options, &help); if (msg) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); if (!Hflag) { tst_tmpdir(); Hopt = get_tst_tmpdir(); } if (sflag) hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX); setup(); map_sz = read_meminfo("Hugepagesize:") * 1024; for (lc = 0; TEST_LOOPING(lc); lc++) { fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666); if (fildes < 0) tst_brkm(TBROK | TERRNO, cleanup, "open %s", TEMPFILE); Tst_count = 0; /* Attempt to mmap into highmem addr, should get ENOMEM */ addr = mmap(HIGH_ADDR, map_sz, PROT_READ, MAP_SHARED | MAP_FIXED, fildes, 0); if (addr != MAP_FAILED) { tst_resm(TFAIL, "mmap into high region " "succeeded unexpectedly"); goto fail; } if (errno != ENOMEM) tst_resm(TFAIL | TERRNO, "mmap into high region " "failed unexpectedly - expect " "errno=ENOMEM, got"); else tst_resm(TPASS | TERRNO, "mmap into high region " "failed as expected"); fail: close(fildes); } cleanup(); tst_exit(); }
/* * test_FS() - This function changes the current working directory * of the child process. If CLONE_FS flag is set, this * will be visible to parent also. */ static int test_FS(void) { char *test_tmpdir; int rval; test_tmpdir = get_tst_tmpdir(); if (test_tmpdir == NULL) { tst_resm(TWARN | TERRNO, "get_tst_tmpdir failed"); rval = -1; } else if (chdir(test_tmpdir) == -1) { tst_resm(TWARN | TERRNO, "chdir failed in test_FS"); rval = -1; } else { rval = 0; } free(test_tmpdir); return rval; }
static void cleanup(void) { int fd; if (restore_shmmax) { fd = open(PATH_SHMMAX, O_WRONLY); if (fd == -1) tst_resm(TWARN|TERRNO, "open"); if (write(fd, shmmax, strlen(shmmax)) != strlen(shmmax)) tst_resm(TWARN|TERRNO, "write"); close(fd); } fd = open(path, O_WRONLY); if (fd == -1) tst_resm(TWARN|TERRNO, "open"); tst_resm(TINFO, "restore nr_hugepages to %s.", nr_hugepages); if (write(fd, nr_hugepages, strlen(nr_hugepages)) != strlen(nr_hugepages)) tst_resm(TWARN|TERRNO, "write"); close(fd); fd = open(pathover, O_WRONLY); if (fd == -1) tst_resm(TWARN|TERRNO, "open"); tst_resm(TINFO, "restore nr_overcommit_hugepages to %s.", nr_overcommit_hugepages); if (write(fd, nr_overcommit_hugepages, strlen(nr_overcommit_hugepages)) != strlen(nr_overcommit_hugepages)) tst_resm(TWARN|TERRNO, "write"); close(fd); /* XXX (garrcoop): memory leak. */ snprintf(buf, BUFSIZ, "%s/hugemmap05", get_tst_tmpdir()); if (umount(buf) == -1) tst_resm(TWARN|TERRNO, "umount"); if (shmid != -1) { tst_resm(TINFO, "shmdt cleaning"); shmctl(shmid, IPC_RMID, NULL); } TEST_CLEANUP; tst_rmdir(); }
int main(void) { char *tmp_dir; char *start_dir = getcwd(NULL, PATH_MAX); char *changed_dir; int fail_counter = 0; tst_tmpdir(); tmp_dir = get_tst_tmpdir(); changed_dir = getcwd(NULL, PATH_MAX); if (strcmp(tmp_dir, changed_dir) == 0 && strcmp(tmp_dir, start_dir) != 0) { printf("Temp directory successfully created and switched to\n"); } else { printf("Temp directory is wrong!\n"); fail_counter++; } tst_rmdir(); if (chdir(tmp_dir) == -1 && errno == ENOENT) { printf("The temp directory was removed successfully\n"); } else { printf("Failed to remove the temp directory!\n"); fail_counter++; } if (fail_counter > 0) printf("Something failed please review!!\n"); else printf("Test completed successfully!\n"); return 0; }
int main(int ac, char **av) { int lc; char *msg; int Hflag = 0; long page_sz = 0; int sflag = 0; option_t options[] = { { "H:", &Hflag, &Hopt }, { "s:", &sflag, &nr_opt }, { NULL, NULL, NULL } }; msg = parse_opts(ac, av, options, &help); if (msg != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s," "use -help", msg); if (!Hflag) { tst_tmpdir(); Hopt = get_tst_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 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 */ beforetest = read_meminfo("HugePages_Free:"); /* Note the size of huge page size BEFORE testing */ page_sz = read_meminfo("Hugepagesize:") * 1024; addr = mmap(NULL, page_sz, PROT_READ | PROT_WRITE, MAP_SHARED, fildes, 0); if (addr == MAP_FAILED) { tst_resm(TFAIL|TERRNO, "mmap() Failed on %s", TEMPFILE); close(fildes); continue; } else { close(fildes); tst_resm(TPASS, "call succeeded"); /* 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, page_sz) != 0) tst_brkm(TFAIL|TERRNO, NULL, "munmap failed"); close(fildes); } cleanup(); tst_exit(); }
int main(int ac, char **av) { int lc; char *msg; int Hflag = 0; long page_sz, map_sz; int sflag = 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 = get_tst_tmpdir(); } if (sflag) hugepages = SAFE_STRTOL(NULL, nr_opt, 0, LONG_MAX); page_sz = getpagesize(); map_sz = read_meminfo("Hugepagesize:") * 1024 * 2; 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(TBROK | TERRNO, cleanup, "opening %s failed", TEMPFILE); /* Creat a file used for normal mapping */ nfildes = open("/dev/zero", O_RDONLY, 0666); if (nfildes < 0) tst_brkm(TBROK | TERRNO, cleanup, "opening /dev/zero failed"); tst_count = 0; /* * Call mmap on /dev/zero 5 times */ for (i = 0; i < 5; i++) { addr = mmap(0, 256 * 1024 * 1024, PROT_READ, MAP_SHARED, nfildes, 0); addrlist[i] = addr; } /* mmap using normal pages and a low memory address */ addr = mmap(LOW_ADDR, page_sz, PROT_READ, MAP_SHARED | MAP_FIXED, nfildes, 0); if (addr == MAP_FAILED) tst_brkm(TBROK | TERRNO, cleanup, "mmap failed on nfildes"); /* Attempt to mmap a huge page into a low memory address */ addr2 = mmap(LOW_ADDR2, map_sz, PROT_READ | PROT_WRITE, MAP_SHARED, fildes, 0); #if __WORDSIZE == 64 /* 64-bit process */ if (addr2 == MAP_FAILED) { tst_resm(TFAIL | TERRNO, "huge mmap failed unexpectedly" " with %s (64-bit)", TEMPFILE); close(fildes); continue; } else { tst_resm(TPASS, "huge mmap succeeded (64-bit)"); } #else /* 32-bit process */ if (addr2 == MAP_FAILED) tst_resm(TFAIL | TERRNO, "huge mmap failed unexpectedly" " with %s (32-bit)", TEMPFILE); else if (addr2 > 0) { tst_resm(TCONF, "huge mmap failed to test the scenario"); close(fildes); continue; } else if (addr == 0) tst_resm(TPASS, "huge mmap succeeded (32-bit)"); #endif /* Clean up things in case we are looping */ for (i = 0; i < 5; i++) { if (munmap(addrlist[i], 256 * 1024 * 1024) == -1) tst_resm(TBROK | TERRNO, "munmap of addrlist[%d] failed", i); } #if __WORDSIZE == 64 if (munmap(addr2, map_sz) == -1) tst_brkm(TFAIL | TERRNO, NULL, "huge munmap failed"); #endif if (munmap(addr, page_sz) == -1) tst_brkm(TFAIL | TERRNO, NULL, "munmap failed"); close(fildes); } cleanup(); 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"); }
static void overcommit(void) { void *addr = NULL, *shmaddr = NULL; int fd = -1, key = -1; char s[BUFSIZ]; FILE *fp; if (shmid != -1) { /* Use /proc/meminfo to generate an IPC key. */ key = ftok(PATH_MEMINFO, strlen(PATH_MEMINFO)); if (key == -1) tst_brkm(TBROK|TERRNO, cleanup, "ftok"); shmid = shmget(key, (long)(length / 2 * hugepagesize), SHM_HUGETLB | IPC_CREAT | SHM_R | SHM_W); if (shmid == -1) tst_brkm(TBROK|TERRNO, cleanup, "shmget"); } else { /* XXX (garrcoop): memory leak. */ snprintf(s, BUFSIZ, "%s/hugemmap05/file", get_tst_tmpdir()); fd = open(s, O_CREAT | O_RDWR, 0755); if (fd == -1) tst_brkm(TBROK|TERRNO, cleanup, "open"); addr = mmap(ADDR, (long)(length / 2 * hugepagesize), PROTECTION, FLAGS, fd, 0); if (addr == MAP_FAILED) tst_brkm(TBROK|TERRNO, cleanup, "mmap"); } if (opt_sysfs) { tst_resm(TINFO, "check sysfs before allocation."); if (checksys(path_sys_sz_huge, "HugePages_Total", length / 2) != 0) return; if (checksys(path_sys_sz_free, "HugePages_Free", length / 2) != 0) return; if (checksys(path_sys_sz_surp, "HugePages_Surp", length / 2 - size) != 0) return; if (checksys(path_sys_sz_resv, "HugePages_Rsvd", length / 2) != 0) return; } else { tst_resm(TINFO, "check /proc/meminfo before allocation."); fp = fopen(PATH_MEMINFO, "r"); if (fp == NULL) tst_brkm(TBROK|TERRNO, cleanup, "fopen"); if (checkproc(fp, "HugePages_Total", length / 2) != 0) return; if (checkproc(fp, "HugePages_Free", length / 2 ) != 0) return; if (checkproc(fp, "HugePages_Surp", length / 2 - size) != 0) return; if (checkproc(fp, "HugePages_Rsvd", length / 2) != 0) return; fclose(fp); } if (shmid != -1) { tst_resm(TINFO, "shmid: 0x%x", shmid); shmaddr = shmat(shmid, ADDR, SHMAT_FLAGS); if (shmaddr == (void *)-1) tst_brkm(TBROK|TERRNO, cleanup, "shmat"); write_bytes(shmaddr); read_bytes(shmaddr); } else { write_bytes(addr); read_bytes(addr); } if (opt_sysfs) { tst_resm(TINFO, "check sysfs."); if (checksys(path_sys_sz_huge, "HugePages_Total", length / 2) != 0) return; if (checksys(path_sys_sz_free, "HugePages_Free", 0) != 0) return; if (checksys(path_sys_sz_surp, "HugePages_Surp", length / 2 - size) != 0) return; if (checksys(path_sys_sz_resv, "HugePages_Rsvd", 0) != 0) return; } else { tst_resm(TINFO, "check /proc/meminfo."); fp = fopen(PATH_MEMINFO, "r"); if (fp == NULL) tst_brkm(TBROK|TERRNO, cleanup, "fopen"); if (checkproc(fp, "HugePages_Total", length / 2) != 0) return; if (checkproc(fp, "HugePages_Free", 0) != 0) return; if (checkproc(fp, "HugePages_Surp", length / 2 - size) != 0) return; if (checkproc(fp, "HugePages_Rsvd", 0) != 0) return; fclose(fp); } if (shmid != -1) { if (shmdt(shmaddr) != 0) tst_brkm(TBROK|TERRNO, cleanup, "shmdt"); } else { munmap(addr, (long)(length / 2 * hugepagesize)); close(fd); unlink(s); } }