static void setup_ipc(void) { size_t size = getpagesize(); //TODO: Fallback to tst_tmpdir() if /dev/shm does not exits? snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d", tst_test->tid, getpid()); ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600); if (ipc_fd < 0) tst_brk(TBROK | TERRNO, "open(%s)", shm_path); SAFE_FTRUNCATE(ipc_fd, size); results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0); /* Checkpoints needs to be accessible from processes started by exec() */ if (tst_test->needs_checkpoints) sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path); else SAFE_UNLINK(shm_path); SAFE_CLOSE(ipc_fd); if (tst_test->needs_checkpoints) { tst_futexes = (char*)results + sizeof(struct results); tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t); } }
static void setup(void) { fd1 = SAFE_OPEN("file1", O_RDWR | O_CREAT, 0644); SAFE_FTRUNCATE(fd1, getpagesize()); fd2 = SAFE_OPEN("file2", O_WRONLY | O_CREAT, 0644); fd4 = SAFE_OPEN(".", O_RDONLY); SAFE_PIPE(fd5); }
static void setup(void) { fd1 = SAFE_OPEN("file1", O_RDWR | O_CREAT, 0644); SAFE_FTRUNCATE(fd1, getpagesize()); fd2 = SAFE_OPEN("file2", O_RDONLY | O_CREAT, 0644); SAFE_PIPE(fd4); wr_iovec3[0].iov_base = tst_get_bad_addr(NULL); }
static void do_child(void) { int fd, offset; char buf[FS_BLOCKSIZE]; char *addr = NULL; /* * We have changed SIGBUS' handler in parent process by calling * tst_sig(FORK, DEF_HANDLER, NULL), so here just restore it. */ if (signal(SIGBUS, SIG_DFL) == SIG_ERR) tst_brkm(TBROK | TERRNO, NULL, "signal(SIGBUS) failed"); memset(buf, 'a', FS_BLOCKSIZE); fd = SAFE_OPEN(NULL, "testfilec", O_RDWR); SAFE_PWRITE(NULL, 1, fd, buf, FS_BLOCKSIZE, 0); /* * In case mremap() may fail because that memory area can not be * expanded at current virtual address(MREMAP_MAYMOVE is not set), * we first do a mmap(page_size * 2) operation to reserve some * free address space. */ addr = SAFE_MMAP(NULL, NULL, page_size * 2, PROT_WRITE | PROT_READ, MAP_PRIVATE_EXCEPT_UCLINUX | MAP_ANONYMOUS, -1, 0); SAFE_MUNMAP(NULL, addr, page_size * 2); addr = SAFE_MMAP(NULL, addr, FS_BLOCKSIZE, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0); addr[0] = 'a'; SAFE_FTRUNCATE(NULL, fd, page_size * 2); addr = mremap(addr, FS_BLOCKSIZE, 2 * page_size, 0); if (addr == MAP_FAILED) tst_brkm(TBROK | TERRNO, NULL, "mremap failed unexpectedly"); /* * Let parent process consume FS free blocks as many as possible, then * there'll be no free blocks allocated for this new file mmaping for * offset starting at 1024, 2048, or 3072. If this above kernel bug * has been fixed, usually child process will killed by SIGBUS signal, * if not, the data 'A', 'B', 'C' will be silently discarded later when * kernel writepage is called, that means data corruption. */ TST_SAFE_CHECKPOINT_WAKE(NULL, 0); TST_SAFE_CHECKPOINT_WAIT(NULL, 0); for (offset = FS_BLOCKSIZE; offset < page_size; offset += FS_BLOCKSIZE) addr[offset] = 'a'; SAFE_MUNMAP(NULL, addr, 2 * page_size); SAFE_CLOSE(NULL, fd); exit(TFAIL); }
void tst_checkpoint_init(const char *file, const int lineno, void (*cleanup_fn)(void)) { int fd; unsigned int page_size; if (tst_futexes) { tst_brkm(TBROK, cleanup_fn, "%s: %d checkpoints already initialized", file, lineno); return; } /* * The parent test process is responsible for creating the temporary * directory and therefore must pass non-zero cleanup (to remove the * directory if something went wrong). * * We cannot do this check unconditionally because if we need to init * the checkpoint from a binary that was started by exec() the * tst_tmpdir_created() will return false because the tmpdir was * created by parent. In this case we expect the subprogram can call * the init as a first function with NULL as cleanup function. */ if (cleanup_fn && !tst_tmpdir_created()) { tst_brkm(TBROK, cleanup_fn, "%s:%d You have to create test temporary directory " "first (call tst_tmpdir())", file, lineno); return; } page_size = getpagesize(); fd = SAFE_OPEN(cleanup_fn, "checkpoint_futex_base_file", O_RDWR | O_CREAT, 0666); SAFE_FTRUNCATE(cleanup_fn, fd, page_size); tst_futexes = SAFE_MMAP(cleanup_fn, NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); tst_max_futexes = page_size / sizeof(uint32_t); SAFE_CLOSE(cleanup_fn, fd); }
static void setup_ipc(void) { size_t size = getpagesize(); if (access("/dev/shm", F_OK) == 0) { snprintf(shm_path, sizeof(shm_path), "/dev/shm/ltp_%s_%d", tid, getpid()); } else { char *tmpdir; if (!tst_tmpdir_created()) tst_tmpdir(); tmpdir = tst_get_tmpdir(); snprintf(shm_path, sizeof(shm_path), "%s/ltp_%s_%d", tmpdir, tid, getpid()); free(tmpdir); } ipc_fd = open(shm_path, O_CREAT | O_EXCL | O_RDWR, 0600); if (ipc_fd < 0) tst_brk(TBROK | TERRNO, "open(%s)", shm_path); SAFE_CHMOD(shm_path, 0666); SAFE_FTRUNCATE(ipc_fd, size); results = SAFE_MMAP(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, ipc_fd, 0); /* Checkpoints needs to be accessible from processes started by exec() */ if (tst_test->needs_checkpoints) { sprintf(ipc_path, IPC_ENV_VAR "=%s", shm_path); putenv(ipc_path); } else { SAFE_UNLINK(shm_path); } SAFE_CLOSE(ipc_fd); if (tst_test->needs_checkpoints) { tst_futexes = (char*)results + sizeof(struct results); tst_max_futexes = (size - sizeof(struct results))/sizeof(futex_t); } }
/* * Write zeroes using O_DIRECT into sparse file. */ int dio_sparse(char *filename, int align, int writesize, int filesize, int offset) { int fd; void *bufptr; int i, w; fd = open(filename, O_DIRECT | O_WRONLY | O_CREAT | O_EXCL, 0600); if (fd < 0) { tst_resm(TBROK | TERRNO, "open()"); return 1; } SAFE_FTRUNCATE(cleanup, fd, filesize); TEST(posix_memalign(&bufptr, align, writesize)); if (TEST_RETURN) { tst_resm(TBROK | TRERRNO, "cannot allocate aligned memory"); close(fd); return 1; } memset(bufptr, 0, writesize); lseek(fd, offset, SEEK_SET); for (i = offset; i < filesize;) { if ((w = write(fd, bufptr, writesize)) != writesize) { tst_resm(TBROK | TERRNO, "write() returned %d", w); close(fd); return 1; } i += w; } close(fd); unlink(filename); return 0; }
/* * Write zeroes using O_DIRECT into sparse file. */ int dio_sparse(char *filename, int align, int writesize, int filesize) { int fd; void *bufptr; int i, w; fd = open(filename, O_DIRECT | O_WRONLY | O_CREAT | O_EXCL, 0600); if (fd < 0) { tst_resm(TBROK | TERRNO, "open()"); return 1; } SAFE_FTRUNCATE(cleanup, fd, filesize); if (posix_memalign(&bufptr, align, writesize)) { close(fd); tst_resm(TBROK | TERRNO, "posix_memalign()"); return 1; } memset(bufptr, 0, writesize); for (i = 0; i < filesize;) { if ((w = write(fd, bufptr, writesize)) != writesize) { tst_resm(TBROK | TERRNO, "write() returned %d", w); close(fd); return 1; } i += w; } close(fd); unlink(filename); return 0; }
int main(int argc, char **argv) { char *filename = "dio_sparse"; int pid[NUM_CHILDREN]; int num_children = 1; int i; long alignment = 512; int writesize = 65536; int filesize = 100 * 1024 * 1024; int offset = 0; int c; int children_errors = 0; int ret; while ((c = getopt(argc, argv, "dw:n:a:s:o:")) != -1) { char *endp; switch (c) { case 'd': debug++; break; case 'a': alignment = strtol(optarg, &endp, 0); alignment = scale_by_kmg(alignment, *endp); break; case 'w': writesize = strtol(optarg, &endp, 0); writesize = scale_by_kmg(writesize, *endp); break; case 's': filesize = strtol(optarg, &endp, 0); filesize = scale_by_kmg(filesize, *endp); break; case 'o': offset = strtol(optarg, &endp, 0); offset = scale_by_kmg(offset, *endp); break; case 'n': num_children = atoi(optarg); if (num_children > NUM_CHILDREN) { fprintf(stderr, "number of children limited to %d\n", NUM_CHILDREN); num_children = NUM_CHILDREN; } break; case '?': usage(); break; } } setup(); tst_resm(TINFO, "Dirtying free blocks"); dirty_freeblocks(filesize); fd = SAFE_OPEN(cleanup, filename, O_DIRECT | O_WRONLY | O_CREAT | O_EXCL, 0600); SAFE_FTRUNCATE(cleanup, fd, filesize); tst_resm(TINFO, "Starting I/O tests"); signal(SIGTERM, SIG_DFL); for (i = 0; i < num_children; i++) { switch (pid[i] = fork()) { case 0: SAFE_CLOSE(NULL, fd); read_sparse(filename, filesize); break; case -1: while (i-- > 0) kill(pid[i], SIGTERM); tst_brkm(TBROK | TERRNO, cleanup, "fork()"); default: continue; } } tst_sig(FORK, DEF_HANDLER, cleanup); ret = dio_sparse(fd, alignment, writesize, filesize, offset); tst_resm(TINFO, "Killing childrens(s)"); for (i = 0; i < num_children; i++) kill(pid[i], SIGTERM); for (i = 0; i < num_children; i++) { int status; pid_t p; p = waitpid(pid[i], &status, 0); if (p < 0) { tst_resm(TBROK | TERRNO, "waitpid()"); } else { if (WIFEXITED(status) && WEXITSTATUS(status) == 10) children_errors++; } } if (children_errors) tst_resm(TFAIL, "%i children(s) exited abnormally", children_errors); if (!children_errors && !ret) tst_resm(TPASS, "Test passed"); cleanup(); tst_exit(); }