static void setup(void) { int i, fd; fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664); pagesize = getpagesize(); /* Writing 16 pages of random data into this file */ for (i = 0; i < (pagesize / 2); i++) SAFE_WRITE(1, fd, STR, sizeof(STR) - 1); SAFE_FSTAT(fd, &st); file1 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); file2 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); file3 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0); shared_anon = SAFE_MMAP(0, MAP_SIZE, PROT_READ, MAP_SHARED | MAP_ANONYMOUS, -1, 0); nonalign = file1 + 100; ptr_addr = SAFE_MALLOC(st.st_size); tmp_addr = (void*)LTP_ALIGN((long)ptr_addr, pagesize); /* unmap as last step to avoid subsequent mmap(s) pick same address */ SAFE_MUNMAP(file2 + st.st_size - pagesize, pagesize); SAFE_CLOSE(fd); tcases_filter(); }
static void setup(void) { unsigned int i; int fd; SAFE_MKDIR(TMP_DIR, 0664); SAFE_MOUNT(TMP_DIR, TMP_DIR, "tmpfs", 0, NULL); fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664); /* Writing 40 KB of random data into this file [32 * 1280 = 40960] */ for (i = 0; i < 1280; i++) SAFE_WRITE(1, fd, STR, strlen(STR)); SAFE_FSTAT(fd, &st); /* Map the input file into shared memory */ sfile = SAFE_MMAP(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); /* Map the input file into private memory. MADV_HUGEPAGE only works * with private anonymous pages */ amem = SAFE_MMAP(NULL, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); SAFE_CLOSE(fd); }
static void setup(void) { int i, fd; fd = SAFE_OPEN(TEST_FILE, O_RDWR | O_CREAT, 0664); pagesize = getpagesize(); /* Writing 16 pages of random data into this file */ for (i = 0; i < (pagesize / 2); i++) SAFE_WRITE(1, fd, STR, sizeof(STR) - 1); SAFE_FSTAT(fd, &st); file1 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); file2 = SAFE_MMAP(0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); SAFE_MUNMAP(file2 + st.st_size - pagesize, pagesize); nonalign = file1 + 100; ptr_addr = SAFE_MALLOC(st.st_size); tmp_addr = (void*)LTP_ALIGN((long)ptr_addr, pagesize); SAFE_CLOSE(fd); tcases_filter(); }
static size_t get_allocsize(void) { struct stat file_stat; SAFE_FSTAT(cleanup, fd, &file_stat); return file_stat.st_blocks * 512; }
static void get_blocksize(void) { struct stat file_stat; SAFE_FSTAT(cleanup, fd, &file_stat); block_size = file_stat.st_blksize; buf_size = NUM_OF_BLOCKS * block_size; }
int main(int ac, char **av) { struct stat stat_buf; int i, lc; const char *msg; uid_t user_id; gid_t group_id; msg = parse_opts(ac, av, NULL, NULL); if (msg != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); setup(); for (lc = 0; TEST_LOOPING(lc); lc++) { tst_count = 0; for (i = 0; tc[i].desc != NULL; i++) { user_id = tc[i].user_id; group_id = tc[i].group_id; TEST(FCHOWN(cleanup, fildes, user_id, group_id)); if (TEST_RETURN == -1) { tst_resm(TFAIL | TTERRNO, "fchown() Fails to %s", tc[i].desc); continue; } SAFE_FSTAT(cleanup, fildes, &stat_buf); if (user_id == (uid_t)-1) user_id = tc[i - 1].user_id; if (group_id == (gid_t)-1) group_id = tc[i - 1].group_id; if ((stat_buf.st_uid != user_id) || (stat_buf.st_gid != group_id)) { tst_resm(TFAIL, "%s: Incorrect owner" "ship set, Expected %d %d", TESTFILE, user_id, group_id); } else { tst_resm(TPASS, "fchown() succeeds to %s of %s", tc[i].desc, TESTFILE); } } } cleanup(); tst_exit(); }
int main(int ac, char **av) { struct stat stat_buf; int lc; uid_t user_id; gid_t group_id; tst_parse_opts(ac, av, NULL, NULL); setup(); for (lc = 0; TEST_LOOPING(lc); lc++) { tst_count = 0; user_id = geteuid(); GID16_CHECK((group_id = getegid()), "fchown", cleanup) TEST(FCHOWN(cleanup, fildes, -1, group_id)); if (TEST_RETURN == -1) { tst_resm(TFAIL, "fchown() on %s Fails, errno=%d", TESTFILE, TEST_ERRNO); continue; } SAFE_FSTAT(cleanup, fildes, &stat_buf); if ((stat_buf.st_uid != user_id) || (stat_buf.st_gid != group_id)) { tst_resm(TFAIL, "%s: Incorrect " "ownership set, Expected %d %d", TESTFILE, user_id, group_id); continue; } if (stat_buf.st_mode != FCHOWN_PERMS) { tst_resm(TFAIL, "%s: Incorrect mode permissions" " %#o, Expected %#o", TESTFILE, stat_buf.st_mode, FCHOWN_PERMS); } else { tst_resm(TPASS, "fchown() on %s succeeds: " "Setuid/gid bits cleared", TESTFILE); } } cleanup(); tst_exit(); }
void testfunc_trunc(void) { struct stat file_stat; TEST(openat(AT_FDCWD, TEST_FILE, O_TRUNC | O_RDWR, 0777)); if (TEST_RETURN == -1) { tst_resm(TFAIL | TTERRNO, "openat failed"); return; } SAFE_FSTAT(cleanup, TEST_RETURN, &file_stat); if (file_stat.st_size == 0) tst_resm(TPASS, "test O_TRUNC for openat success"); else tst_resm(TFAIL, "test O_TRUNC for openat failed"); SAFE_CLOSE(cleanup, TEST_RETURN); }
static void setup(void) { int fd; struct stat stat_buf; tst_sig(NOFORK, DEF_HANDLER, cleanup); tst_tmpdir(); TEST_PAUSE; fd = SAFE_CREAT(cleanup, TEST_FILE, 0644); #define STR "abcdefgh" SAFE_WRITE(cleanup, 1, fd, STR, sizeof(STR) - 1); SAFE_FSTAT(cleanup, fd, &stat_buf); SAFE_CLOSE(cleanup, fd); file_size = stat_buf.st_size; }
static void verify_open(unsigned int n) { struct tcase *tc = &tcases[n]; struct stat buf; TEST(open(tc->filename, tc->flag, tc->mode)); fd = TST_RET; if (fd == -1) { tst_res(TFAIL, "Cannot open a file"); return; } SAFE_FSTAT(fd, &buf); if (!(buf.st_mode & tc->tst_bit)) tst_res(TFAIL, "%s is cleared unexpectedly", tc->desc); else tst_res(TPASS, "%s is set as expected", tc->desc); SAFE_CLOSE(fd); if (S_ISREG(buf.st_mode)) SAFE_UNLINK(tc->filename); }
static void ftruncate_expect_success(int fd, off_t offset, const char *msg) { struct stat sb; TEST(ftruncate(fd, offset)); if (TEST_RETURN != 0) { tst_resm(TFAIL | TTERRNO, "ftruncate() %s failed unexpectedly", msg); return; } SAFE_FSTAT(cleanup, fd, &sb); if (sb.st_size != offset) { tst_resm(TFAIL, "ftruncate() to %zu bytes succeded but fstat() reports size %zu", offset, sb.st_size); return; } tst_resm(TPASS, "ftruncate() %s succeded", msg); }
static int io_tio(char *pathname, int flag, int operation) { int res, fd = 0, i = 0; void *bufptr = NULL; off_t offset = 0; struct timespec timeout; struct stat fi_stat; size_t alignment; io_context_t myctx; struct iocb iocb_array[AIO_MAXIO]; struct iocb *iocbps[AIO_MAXIO]; fd = SAFE_OPEN(pathname, flag, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* determine the alignment from the blksize of the underlying device */ SAFE_FSTAT(fd, &fi_stat); alignment = fi_stat.st_blksize; res = io_queue_init(AIO_MAXIO, &myctx); for (i = 0; i < AIO_MAXIO; i++) { switch (operation) { case IO_CMD_PWRITE: if (posix_memalign(&bufptr, alignment, AIO_BLKSIZE)) { tst_brk(TBROK | TERRNO, "posix_memalign failed"); return -1; } memset(bufptr, 0, AIO_BLKSIZE); io_prep_pwrite(&iocb_array[i], fd, bufptr, AIO_BLKSIZE, offset); io_set_callback(&iocb_array[i], work_done); iocbps[i] = &iocb_array[i]; offset += AIO_BLKSIZE; break; case IO_CMD_PREAD: if (posix_memalign(&bufptr, alignment, AIO_BLKSIZE)) { tst_brk(TBROK | TERRNO, "posix_memalign failed"); return -1; } memset(bufptr, 0, AIO_BLKSIZE); io_prep_pread(&iocb_array[i], fd, bufptr, AIO_BLKSIZE, offset); io_set_callback(&iocb_array[i], work_done); iocbps[i] = &iocb_array[i]; offset += AIO_BLKSIZE; break; default: tst_res(TFAIL, "Command failed; opcode returned: %d\n", operation); return -1; break; } } do { res = io_submit(myctx, AIO_MAXIO, iocbps); } while (res == -EAGAIN); if (res < 0) io_error("io_submit tio", res); /* * We have submitted all the i/o requests. Wait for them to complete and * call the callbacks. */ wait_count = AIO_MAXIO; timeout.tv_sec = 30; timeout.tv_nsec = 0; switch (operation) { case IO_CMD_PREAD: case IO_CMD_PWRITE: { while (wait_count) { res = io_wait_run(myctx, &timeout); if (res < 0) io_error("io_wait_run", res); } } break; } SAFE_CLOSE(fd); for (i = 0; i < AIO_MAXIO; i++) if (iocb_array[i].u.c.buf != NULL) free(iocb_array[i].u.c.buf); io_queue_release(myctx); return 0; }