static int file_tree_callback(const char *fpath, const struct stat *sb, int typeflag, __unused__ struct FTW *ftwbuf) { if (typeflag == FTW_DNR) return FTW_CONTINUE; if (typeflag == FTW_NS) return FTW_CONTINUE; if (ignore_files(fpath)) return FTW_SKIP_SUBTREE; // Check we can read it. if (check_stat_file(sb) == -1) return FTW_CONTINUE; if (shm->exit_reason != STILL_RUNNING) return FTW_STOP; add_to_namelist(fpath); files_in_index++; return FTW_CONTINUE; }
static int open_file(void) { const char *filename; const char *modestr; struct stat sb; int fd; int ret; int tries = 0; int fcntl_flags; int flags, randflags; bool opened_with_fopen = FALSE; retry: filename = get_filename(); ret = lstat(filename, &sb); if (ret == -1) goto retry; flags = check_stat_file(&sb); if (flags == -1) goto retry; /* OR in some random flags. */ retry_flags: if (rand_bool()) { randflags = get_o_flags(); fd = open(filename, flags | randflags | O_NONBLOCK); } else { fd = open_with_fopen(filename, flags); fcntl_flags = random_fcntl_setfl_flags(); fcntl(fd, F_SETFL, fcntl_flags); opened_with_fopen = TRUE; } if (fd < 0) { /* * if we failed to open the file, retry with different flags. * we should eventually succeed, but set an arbitary upper limit of * 50 tries before just giving up. */ tries++; if (tries == 50) { output(2, "Couldn't open %s : %s\n", filename, strerror(errno)); return fd; } goto retry_flags; } switch (flags) { case O_RDONLY: modestr = "read-only"; break; case O_WRONLY: modestr = "write-only"; break; case O_RDWR: modestr = "read-write"; break; default: modestr = "unknown"; break; } if (opened_with_fopen == FALSE) { output(2, "fd[%i] = open %s (%s) flags:%x\n", fd, filename, modestr, flags | randflags); if (victim_dev!=NULL) victim_fd=0; } else { output(2, "fd[%i] = fopen %s (%s) flags:%x fcntl_flags:%x\n", fd, filename, modestr, flags, fcntl_flags); //try to keep track of the victim fd in case victim_dev was specified //and taking it valid only if the fd is read/write or write if (victim_dev!=NULL && (flags==O_WRONLY||flags==O_RDWR)) victim_fd=fd; else victim_fd=0; } return fd; }
static int open_file(void) { const char *filename; const char *modestr; struct stat sb; int fd; int ret; int tries = 0; int fcntl_flags = 0; int flags, randflags = 0; bool opened_with_fopen = FALSE; retry: filename = get_filename(); ret = lstat(filename, &sb); if (ret == -1) goto retry; flags = check_stat_file(&sb); if (flags == -1) goto retry; /* OR in some random flags. */ retry_flags: if (RAND_BOOL()) { randflags = get_o_flags(); fd = open(filename, flags | randflags | O_NONBLOCK, 0666); } else { fd = open_with_fopen(filename, flags); fcntl_flags = random_fcntl_setfl_flags(); fcntl(fd, F_SETFL, fcntl_flags); opened_with_fopen = TRUE; } if (fd < 0) { /* * if we failed to open the file, retry with different flags. * we should eventually succeed, but set an arbitary upper limit of * 50 tries before just giving up. */ tries++; if (tries == 50) { output(2, "Couldn't open %s : %s\n", filename, strerror(errno)); return fd; } goto retry_flags; } switch (flags) { case O_RDONLY: modestr = "read-only"; break; case O_WRONLY: modestr = "write-only"; break; case O_RDWR: modestr = "read-write"; break; default: modestr = "unknown"; break; } if (opened_with_fopen == FALSE) output(2, "fd[%i] = open %s (%s) flags:%x\n", fd, filename, modestr, flags | randflags); else output(2, "fd[%i] = fopen %s (%s) flags:%x fcntl_flags:%x\n", fd, filename, modestr, flags, fcntl_flags); return fd; }