/* * setup() - performs all ONE TIME setup for this test. */ void setup(void) { tst_sig(NOFORK, DEF_HANDLER, cleanup); TEST_PAUSE; /* Create a temporary directory and make it current. */ tst_tmpdir(); sprintf(fdir, "./tdir_%d", getpid()); sprintf(mdir, "%s/rndir_%d", fdir, getpid()); /* create "old" directory */ if (stat(fdir, &buf1) != -1) { tst_brkm(TBROK, cleanup, "tmp directory %s found!", fdir); } SAFE_MKDIR(cleanup, fdir, 00770); SAFE_STAT(cleanup, fdir, &buf1); /* save "old"'s dev and ino */ olddev = buf1.st_dev; oldino = buf1.st_ino; /* create another directory */ if (stat(mdir, &buf2) != -1) { tst_brkm(TBROK, cleanup, "tmp directory %s found!", mdir); } SAFE_MKDIR(cleanup, mdir, 00770); SAFE_STAT(cleanup, mdir, &buf2); /* save "new"'s dev and ino */ olddev1 = buf2.st_dev; oldino1 = buf2.st_ino; }
static void utimes_verify(const struct test_case_t *tc) { struct stat st; struct timeval tmp_tv[2]; if (tc->exp_errno == 0) { SAFE_STAT(cleanup, tc->pathname, &st); tmp_tv[0].tv_sec = st.st_atime; tmp_tv[0].tv_usec = 0; tmp_tv[1].tv_sec = st.st_mtime; tmp_tv[1].tv_usec = 0; } TEST(utimes(tc->pathname, tc->times)); TEST_ERROR_LOG(TEST_ERRNO); if (TEST_ERRNO == tc->exp_errno) { tst_resm(TPASS | TTERRNO, "utimes() worked as expected"); } else { tst_resm(TFAIL | TTERRNO, "utimes() failed unexpectedly; expected: %d - %s", tc->exp_errno, strerror(tc->exp_errno)); } if (TEST_ERRNO == 0 && utimes(tc->pathname, tmp_tv) == -1) tst_brkm(TBROK | TERRNO, cleanup, "utimes() failed."); }
static void setup(void) { struct stat c_buf, l_buf; if ((tst_kvercmp(2, 6, 16)) < 0) tst_brkm(TCONF, NULL, "This test needs kernel 2.6.16 or newer"); tst_require_root(NULL); tst_sig(NOFORK, DEF_HANDLER, cleanup); TEST_PAUSE; tst_tmpdir(); dirfd = SAFE_OPEN(cleanup, "./", O_DIRECTORY); SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL); SAFE_SYMLINK(cleanup, TESTFILE, TESTFILE_LINK); SAFE_STAT(cleanup, TESTFILE_LINK, &c_buf); SAFE_LSTAT(cleanup, TESTFILE_LINK, &l_buf); if (l_buf.st_uid == set_uid || l_buf.st_gid == set_gid) { tst_brkm(TBROK | TERRNO, cleanup, "link_uid(%d) == set_uid(%d) or link_gid(%d) == " "set_gid(%d)", l_buf.st_uid, set_uid, l_buf.st_gid, set_gid); } }
static void setup(void) { struct stat buf; /* Find a valid device number */ SAFE_STAT("/", &buf); root_dev = buf.st_dev; }
void testfunc_noatime(void) { struct stat file_stat, file_newstat; char buf; const char *flags[] = {"noatime", "relatime", NULL}; int ret; if ((tst_kvercmp(2, 6, 8)) < 0) { tst_resm(TCONF, "test O_NOATIME flags for openat " "needs kernel 2.6.8 or higher"); return; } ret = tst_path_has_mnt_flags(cleanup, NULL, flags); if (ret > 0) { tst_resm(TCONF, "test O_NOATIME flag for openat needs " "filesystems which are mounted without " "noatime and relatime"); return; } SAFE_STAT(cleanup, TEST_FILE, &file_stat); sleep(1); TEST(openat(AT_FDCWD, TEST_FILE, O_NOATIME | O_RDONLY, 0777)); if (TEST_RETURN == -1) { tst_resm(TFAIL | TTERRNO, "openat failed"); return; } SAFE_READ(cleanup, 1, TEST_RETURN, &buf, 1); SAFE_CLOSE(cleanup, TEST_RETURN); SAFE_STAT(cleanup, TEST_FILE, &file_newstat); if (file_stat.st_atime == file_newstat.st_atime) tst_resm(TPASS, "test O_NOATIME for openat success"); else tst_resm(TFAIL, "test O_NOATIME for openat failed"); }
static void setup(void) { struct stat buf; tst_sig(NOFORK, DEF_HANDLER, cleanup); TEST_PAUSE; /* Find a valid device number */ SAFE_STAT(cleanup, "/", &buf); dev_num = buf.st_dev; }
static void test_noatime(void) { char read_buf; struct stat old_stat, new_stat; if ((tst_kvercmp(2, 6, 8)) < 0) { tst_resm(TCONF, "O_NOATIME flags test for open(2) needs kernel 2.6.8 " "or higher"); return; } if (skip_noatime) { tst_resm(TCONF, "test O_NOATIME flag for open needs filesystems which " "is mounted without noatime and relatime"); return; } SAFE_STAT(cleanup, TEST_FILE, &old_stat); sleep(1); TEST(open(TEST_FILE, O_RDONLY | O_NOATIME, 0777)); if (TEST_RETURN == -1) { tst_resm(TFAIL | TTERRNO, "open failed"); return; } SAFE_READ(cleanup, 1, TEST_RETURN, &read_buf, 1); SAFE_CLOSE(cleanup, TEST_RETURN); SAFE_STAT(cleanup, TEST_FILE, &new_stat); if (old_stat.st_atime == new_stat.st_atime) tst_resm(TPASS, "test O_NOATIME for open success"); else tst_resm(TFAIL, "test O_NOATIME for open failed"); }
const char *tst_acquire_device(void (cleanup_fn)(void)) { char *dev; struct stat st; if (device_acquired) tst_brkm(TBROK, cleanup_fn, "Device allready acquired"); if (!tst_tmpdir_created()) { tst_brkm(TBROK, cleanup_fn, "Cannot acquire device without tmpdir() created"); } dev = getenv("LTP_DEV"); if (dev) { tst_resm(TINFO, "Using test device LTP_DEV='%s'", dev); SAFE_STAT(cleanup_fn, dev, &st); if (!S_ISBLK(st.st_mode)) { tst_brkm(TBROK, cleanup_fn, "%s is not a block device", dev); } if (tst_fill_file(dev, 0, 1024, 512)) { tst_brkm(TBROK | TERRNO, cleanup_fn, "Failed to clear the first 512k of %s", dev); } return dev; } if (tst_fill_file(DEV_FILE, 0, 1024, 102400)) { tst_brkm(TBROK | TERRNO, cleanup_fn, "Failed to create " DEV_FILE); } if (find_free_loopdev()) return NULL; attach_device(cleanup_fn, dev_path, DEV_FILE); device_acquired = 1; return dev_path; }
static void setup(void) { char path[PATH_MAX]; struct stat file_stat; tst_sig(FORK, DEF_HANDLER, cleanup); tst_require_root(); tst_tmpdir(); fs_type = tst_dev_fs_type(); device = tst_acquire_device(cleanup); if (!device) tst_brkm(TCONF, cleanup, "Failed to obtain block device"); tst_mkfs(cleanup, device, fs_type, NULL, NULL); SAFE_MKDIR(cleanup, mntpoint, DIR_MODE); if (getcwd(path_name, sizeof(path_name)) == NULL) tst_brkm(TBROK, cleanup, "getcwd failed"); if (chmod(path_name, DIR_MODE) != 0) tst_brkm(TBROK, cleanup, "chmod(%s, %#o) failed", path_name, DIR_MODE); strncpy(path, path_name, PATH_MAX); snprintf(path_name, PATH_MAX, "%s/%s/", path, mntpoint); SAFE_MOUNT(cleanup, device, mntpoint, fs_type, 0, NULL); TST_RESOURCE_COPY(cleanup, "mount03_setuid_test", path_name); snprintf(file, PATH_MAX, "%smount03_setuid_test", path_name); SAFE_STAT(cleanup, file, &file_stat); if (file_stat.st_mode != SUID_MODE && chmod(file, SUID_MODE) < 0) tst_brkm(TBROK, cleanup, "setuid for setuid_test failed"); SAFE_UMOUNT(cleanup, mntpoint); TEST_PAUSE; }
static void test_verify(void) { struct stat c_buf, l_buf; SAFE_STAT(cleanup, TESTFILE_LINK, &c_buf); SAFE_LSTAT(cleanup, TESTFILE_LINK, &l_buf); if (c_buf.st_uid != set_uid && l_buf.st_uid == set_uid && c_buf.st_gid != set_gid && l_buf.st_gid == set_gid) { tst_resm(TPASS, "fchownat() test AT_SYMLINK_NOFOLLOW success"); } else { tst_resm(TFAIL, "fchownat() test AT_SYMLINK_NOFOLLOW fail with uid=%d " "link_uid=%d set_uid=%d | gid=%d link_gid=%d " "set_gid=%d", c_buf.st_uid, l_buf.st_uid, set_uid, c_buf.st_gid, l_buf.st_gid, set_gid); } }
static void setresgid_verify(void) { struct stat buf; TEST(SETRESGID(cleanup, -1, ltpuser->pw_gid, -1)); if (TEST_RETURN != 0) { tst_resm(TFAIL | TTERRNO, "setresgid failed unexpectedly"); return; } SAFE_TOUCH(cleanup, "test_file", 0644, NULL); SAFE_STAT(cleanup, "test_file", &buf); if (ltpuser->pw_gid == buf.st_gid) { tst_resm(TPASS, "setresgid succeeded as expected"); } else { tst_resm(TFAIL, "setresgid failed unexpectedly; egid(%d) - st_gid(%d)", ltpuser->pw_gid, buf.st_gid); } }
int main(int ac, char **av) { struct stat stat_buf; /* struct buffer to hold file info. */ int lc; long type; time_t modf_time, access_time; time_t pres_time; /* file modification/access/present time */ tst_parse_opts(ac, av, NULL, NULL); setup(); switch ((type = tst_fs_type(cleanup, "."))) { case TST_NFS_MAGIC: if (tst_kvercmp(2, 6, 18) < 0) tst_brkm(TCONF, cleanup, "Cannot do utime on a file" " on %s filesystem before 2.6.18", tst_fs_type_name(type)); break; case TST_V9FS_MAGIC: tst_brkm(TCONF, cleanup, "Cannot do utime on a file on %s filesystem", tst_fs_type_name(type)); break; } for (lc = 0; TEST_LOOPING(lc); lc++) { tst_count = 0; /* * Invoke utime(2) to set TEMP_FILE access and * modification times to the current time. */ TEST(utime(TEMP_FILE, NULL)); if (TEST_RETURN == -1) { tst_resm(TFAIL|TTERRNO, "utime(%s) failed", TEMP_FILE); } else { /* * Sleep for a second so that mod time and * access times will be different from the * current time */ sleep(2); /* * Get the current time now, after calling * utime(2) */ pres_time = time(NULL); /* * Get the modification and access times of * temporary file using stat(2). */ SAFE_STAT(cleanup, TEMP_FILE, &stat_buf); modf_time = stat_buf.st_mtime; access_time = stat_buf.st_atime; /* Now do the actual verification */ if (modf_time <= curr_time || modf_time >= pres_time || access_time <= curr_time || access_time >= pres_time) { tst_resm(TFAIL, "%s access and " "modification times not set", TEMP_FILE); } else { tst_resm(TPASS, "Functionality of " "utime(%s, NULL) successful", TEMP_FILE); } } tst_count++; } cleanup(); tst_exit(); }
int main(int ac, char **av) { int lc; const char *msg; struct stat fbuf, lbuf; int cnt; char lname[255]; if ((msg = parse_opts(ac, av, options, &help)) != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); if (links_arg) { nlinks = atoi(links_arg); if (nlinks == 0) { tst_brkm(TBROK, NULL, "nlinks is not a positive number"); } } setup(); for (lc = 0; TEST_LOOPING(lc); lc++) { tst_count = 0; for (cnt = 1; cnt < nlinks; cnt++) { sprintf(lname, "%s%d", fname, cnt); TEST(link(fname, lname)); if (TEST_RETURN == -1) { tst_resm(TFAIL, "link(%s, %s) Failed, errno=%d : %s", fname, lname, TEST_ERRNO, strerror(TEST_ERRNO)); } } SAFE_STAT(cleanup, fname, &fbuf); for (cnt = 1; cnt < nlinks; cnt++) { sprintf(lname, "%s%d", fname, cnt); SAFE_STAT(cleanup, lname, &lbuf); if (fbuf.st_nlink <= 1 || lbuf.st_nlink <= 1 || (fbuf.st_nlink != lbuf.st_nlink)) { tst_resm(TFAIL, "link(%s, %s[1-%d]) ret %ld for %d " "files, stat values do not match %d %d", fname, fname, nlinks, TEST_RETURN, nlinks, (int)fbuf.st_nlink, (int)lbuf.st_nlink); break; } } if (cnt >= nlinks) { tst_resm(TPASS, "link(%s, %s[1-%d]) ret %ld for %d files," "stat linkcounts match %d", fname, fname, nlinks, TEST_RETURN, nlinks, (int)fbuf.st_nlink); } for (cnt = 1; cnt < nlinks; cnt++) { sprintf(lname, "%s%d", fname, cnt); SAFE_UNLINK(cleanup, lname); } } cleanup(); tst_exit(); }
const char *tst_acquire_device_(void (cleanup_fn)(void), unsigned int size) { int fd; char *dev; struct stat st; unsigned int acq_dev_size; uint64_t ltp_dev_size; acq_dev_size = size > 150 ? size : 150; if (device_acquired) tst_brkm(TBROK, cleanup_fn, "Device allready acquired"); if (!tst_tmpdir_created()) { tst_brkm(TBROK, cleanup_fn, "Cannot acquire device without tmpdir() created"); } dev = getenv("LTP_DEV"); if (dev) { tst_resm(TINFO, "Using test device LTP_DEV='%s'", dev); SAFE_STAT(cleanup_fn, dev, &st); if (!S_ISBLK(st.st_mode)) { tst_brkm(TBROK, cleanup_fn, "%s is not a block device", dev); } fd = SAFE_OPEN(cleanup_fn, dev, O_RDONLY); SAFE_IOCTL(cleanup_fn, fd, BLKGETSIZE64, <p_dev_size); SAFE_CLOSE(cleanup_fn, fd); ltp_dev_size = ltp_dev_size/1024/1024; if (acq_dev_size <= ltp_dev_size) { if (tst_fill_file(dev, 0, 1024, 512)) { tst_brkm(TBROK | TERRNO, cleanup_fn, "Failed to clear the first 512k of %s", dev); } return dev; } tst_resm(TINFO, "Skipping $LTP_DEV size %"PRIu64"MB, requested size %uMB", ltp_dev_size, acq_dev_size); } if (tst_fill_file(DEV_FILE, 0, 1024, 1024 * acq_dev_size)) { tst_brkm(TBROK | TERRNO, cleanup_fn, "Failed to create " DEV_FILE); } if (find_free_loopdev()) return NULL; attach_device(cleanup_fn, dev_path, DEV_FILE); device_acquired = 1; return dev_path; }
/* * 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); }
int main(int ac, char **av) { int lc; const char *msg; struct stat buf; int i, links; char lname[255]; if ((msg = parse_opts(ac, av, options, &help)) != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); if (links_arg) { nlinks = atoi(links_arg); if (nlinks == 0) { tst_brkm(TBROK, NULL, "nlinks is not a positive number"); } if (nlinks > 1000) { tst_resm(TINFO, "nlinks > 1000 - may get errno:%d (EMLINK)", EMLINK); } } setup(); for (lc = 0; TEST_LOOPING(lc); lc++) { tst_count = 0; if (nlinks) links = nlinks; else links = (lc % 90) + 10; /* Create links - 1 hardlinks so that the st_nlink == links */ for (i = 1; i < links; i++) { sprintf(lname, "%s%d", fname, i); TEST(link(fname, lname)); if (TEST_RETURN == -1) { tst_brkm(TFAIL | TTERRNO, cleanup, "link(%s, %s) Failed", fname, lname); } } SAFE_STAT(cleanup, fname, &buf); if (buf.st_nlink != (nlink_t)links) { tst_resm(TFAIL, "Wrong number of links for " "'%s' have %i, should be %i", fname, (int)buf.st_nlink, links); goto unlink; } for (i = 1; i < links; i++) { sprintf(lname, "%s%d", fname, i); SAFE_STAT(cleanup, lname, &buf); if (buf.st_nlink != (nlink_t)links) { tst_resm(TFAIL, "Wrong number of links for " "'%s' have %i, should be %i", lname, (int)buf.st_nlink, links); goto unlink; } } tst_resm(TPASS, "link() passed and linkcounts=%d match", links); unlink: for (i = 1; i < links; i++) { sprintf(lname, "%s%d", fname, i); SAFE_UNLINK(cleanup, lname); } } cleanup(); tst_exit(); }