8 #include <sys/fanotify.h>
15 const struct fanotify_event_metadata *metadata;
16 struct fanotify_event_metadata buf[200];
20 char procfd_path[PATH_MAX];
21 struct fanotify_response response;
29 len = read(fd, (
void *) &buf,
sizeof(buf));
30 if (len == -1 && errno != EAGAIN) {
47 while (FAN_EVENT_OK(metadata, len)) {
51 if (metadata->vers != FANOTIFY_METADATA_VERSION) {
53 "Mismatch of fanotify metadata version.\n");
61 if (metadata->fd >= 0) {
66 if (metadata->mask & FAN_OPEN_PERM) {
67 printf(
"FAN_OPEN_PERM: ");
71 response.fd = metadata->fd;
72 response.response = FAN_ALLOW;
74 sizeof(
struct fanotify_response));
79 if (metadata->mask & FAN_CLOSE_WRITE) {
80 printf(
"FAN_CLOSE_WRITE: ");
85 snprintf(procfd_path,
sizeof(procfd_path),
86 "/proc/self/fd/%d", metadata->fd);
87 path_len = readlink(procfd_path, path,
94 path[path_len] =
'\0';
95 printf(
"File %s\n", path);
104 metadata = FAN_EVENT_NEXT(metadata, len);
114 struct pollfd fds[2];
119 fprintf(stderr,
"Usage: %s MOUNT\n", argv[0]);
123 printf(
"Press enter key to terminate.\n");
127 fd = fanotify_init(FAN_CLOEXEC | FAN_CLASS_CONTENT | FAN_NONBLOCK,
128 O_RDONLY | O_LARGEFILE);
130 perror(
"fanotify_init");
139 if (fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT,
140 FAN_CLOSE_WRITE, AT_FDCWD,
142 perror(
"fanotify_mark");
152 fds[0].fd = STDIN_FILENO;
153 fds[0].events = POLLIN;
158 fds[1].events = POLLIN;
161 printf(
"Listening for events.\n");
164 poll_num = poll(fds, nfds, -1);
165 if (poll_num == -1) {
166 if (errno == EINTR) {
175 if (fds[0].revents & POLLIN) {
179 while (read(STDIN_FILENO, &buf, 1) > 0 && buf !=
'\n') {
185 if (fds[1].revents & POLLIN) {
192 if (fds[0].revents & POLLIN) {
196 while (read(STDIN_FILENO, &buf, 1) > 0 && buf !=
'\n') {
202 if (fds[1].revents & POLLIN) {
211 printf(
"Listening for events stopped.\n");