int main(int ac, char **av) { int lc; const char *msg; if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); setup(); for (lc = 0; TEST_LOOPING(lc); lc++) { /* Check ONLYDIR on a directory */ CHECK_MARK(".", FAN_MARK_ONLYDIR, 0, NULL); /* Check ONLYDIR without a directory */ CHECK_MARK(fname, FAN_MARK_ONLYDIR, -1, NULL); /* Check DONT_FOLLOW for a symlink */ CHECK_MARK(sname, FAN_MARK_DONT_FOLLOW, 0, test_open_symlink); /* Check without DONT_FOLLOW for a symlink */ CHECK_MARK(sname, 0, 0, test_open_file); /* Verify FAN_MARK_FLUSH destroys all inode marks */ if (myfanotify_mark(fd_notify, FAN_MARK_ADD, FAN_OPEN, AT_FDCWD, fname) < 0) { tst_brkm(TBROK | TERRNO, cleanup, "fanotify_mark (%d, FAN_MARK_ADD, FAN_OPEN, " "AT_FDCWD, '%s') failed", fd_notify, fname); } if (myfanotify_mark(fd_notify, FAN_MARK_ADD, FAN_OPEN | FAN_ONDIR, AT_FDCWD, dir) < 0) { tst_brkm(TBROK | TERRNO, cleanup, "fanotify_mark (%d, FAN_MARK_ADD, FAN_OPEN | " "FAN_ONDIR, AT_FDCWD, '%s') failed", fd_notify, dir); } open_file(fname); verify_event(S_IFREG); open_dir(dir); verify_event(S_IFDIR); if (myfanotify_mark(fd_notify, FAN_MARK_FLUSH, 0, AT_FDCWD, ".") < 0) { tst_brkm(TBROK | TERRNO, cleanup, "fanotify_mark (%d, FAN_MARK_FLUSH, 0, " "AT_FDCWD, '.') failed", fd_notify); } open_dir(dir); verify_no_event(); } cleanup(); tst_exit(); }
void test01(void) { /* Check ONLYDIR on a directory */ CHECK_MARK(".", FAN_MARK_ONLYDIR, 0, NULL); /* Check ONLYDIR without a directory */ CHECK_MARK(fname, FAN_MARK_ONLYDIR, -1, NULL); /* Check DONT_FOLLOW for a symlink */ CHECK_MARK(sname, FAN_MARK_DONT_FOLLOW, 0, test_open_symlink); /* Check without DONT_FOLLOW for a symlink */ CHECK_MARK(sname, 0, 0, test_open_file); /* Verify FAN_MARK_FLUSH destroys all inode marks */ if (fanotify_mark(fd_notify, FAN_MARK_ADD, FAN_OPEN, AT_FDCWD, fname) < 0) { tst_brk(TBROK | TERRNO, "fanotify_mark (%d, FAN_MARK_ADD, FAN_OPEN, " "AT_FDCWD, '%s') failed", fd_notify, fname); } if (fanotify_mark(fd_notify, FAN_MARK_ADD, FAN_OPEN | FAN_ONDIR, AT_FDCWD, dir) < 0) { tst_brk(TBROK | TERRNO, "fanotify_mark (%d, FAN_MARK_ADD, FAN_OPEN | " "FAN_ONDIR, AT_FDCWD, '%s') failed", fd_notify, dir); } open_file(fname); verify_event(S_IFREG); open_dir(dir); verify_event(S_IFDIR); if (fanotify_mark(fd_notify, FAN_MARK_FLUSH, 0, AT_FDCWD, ".") < 0) { tst_brk(TBROK | TERRNO, "fanotify_mark (%d, FAN_MARK_FLUSH, 0, " "AT_FDCWD, '.') failed", fd_notify); } open_dir(dir); verify_no_event(); }
static void do_open_test(char *file, int flag, char *flagstr, int mask) { do_open(file, flag, flagstr); verify_event(mask); }
static void test_fanotify(unsigned int n) { int ret, dirfd; unsigned int i; struct fanotify_event_metadata *event, *ev; struct tcase *tc = &tcases[n]; tst_res(TINFO, "Test #%d: %s", n, tc->tname); create_fanotify_groups(tc->ondir); /* * generate MODIFY event and no FAN_CLOSE_NOWRITE event. */ SAFE_FILE_PRINTF(fname, "1"); /* * generate FAN_CLOSE_NOWRITE event on a child subdir. */ dirfd = SAFE_OPEN(DIR_NAME, O_RDONLY); if (dirfd >= 0) SAFE_CLOSE(dirfd); /* * First verify the first group got the file MODIFY event and got just * one FAN_CLOSE_NOWRITE event. */ ret = read(fd_notify[0], event_buf, EVENT_BUF_LEN); if (ret < 0) { if (errno == EAGAIN) { tst_res(TFAIL, "first group did not get event"); } else { tst_brk(TBROK | TERRNO, "reading fanotify events failed"); } } if (ret < tc->nevents * (int)FAN_EVENT_METADATA_LEN) { tst_brk(TBROK, "short read when reading fanotify events (%d < %d)", ret, tc->nevents * (int)FAN_EVENT_METADATA_LEN); } event = (struct fanotify_event_metadata *)event_buf; verify_event(0, event, FAN_MODIFY); if (tc->ondir) verify_event(0, event + 1, FAN_CLOSE_NOWRITE); if (ret > tc->nevents * (int)FAN_EVENT_METADATA_LEN) { tst_res(TFAIL, "first group got more than %d events (%d > %d)", tc->nevents, ret, tc->nevents * (int)FAN_EVENT_METADATA_LEN); } /* Close all file descriptors of read events */ for (ev = event; ret >= (int)FAN_EVENT_METADATA_LEN; ev++) { if (ev->fd != FAN_NOFD) SAFE_CLOSE(ev->fd); ret -= (int)FAN_EVENT_METADATA_LEN; } /* * Then verify the rest of the groups did not get the MODIFY event and * did not get the FAN_CLOSE_NOWRITE event on subdir. */ for (i = 1; i < NUM_GROUPS; i++) { ret = read(fd_notify[i], event_buf, FAN_EVENT_METADATA_LEN); if (ret > 0) { tst_res(TFAIL, "group %d got event", i); verify_event(i, event, FAN_CLOSE_NOWRITE); if (event->fd != FAN_NOFD) SAFE_CLOSE(event->fd); continue; } if (ret == 0) { tst_brk(TBROK, "zero length read from fanotify fd"); } if (errno != EAGAIN) { tst_brk(TBROK | TERRNO, "reading fanotify events failed"); } tst_res(TPASS, "group %d got no event", i); } cleanup_fanotify_groups(); }