2 * A simple tester of fanotify in the Linux kernel.
4 * This program is released in the Public Domain.
7 * $> gcc -o /tmp/fanotify-example fanotify-example.c
10 * $> /tmp/fanotify-example
20 #include <sys/types.h>
21 #include <sys/fanotify.h>
25 int main(int argc, char *argv[]){
29 char path[PATH_MAX + 1];
30 ssize_t buflen, linklen;
31 struct fanotify_event_metadata *metadata;
33 // Init fanotify structure
34 // fan = fanotify_init(FAN_REPORT_FID, O_RDWR);
35 // FAN_CLASS_CONTENT << only for root.
36 fan = fanotify_init(FAN_CLOEXEC | FAN_CLASS_NOTIF | FAN_REPORT_FID ,
37 O_RDONLY | O_LARGEFILE);
40 perror("fanotify_init");
41 // exit(EXIT_FAILURE);
44 int ret = fanotify_mark(fan, FAN_MARK_ADD , FAN_CREATE | FAN_DELETE | FAN_MODIFY | FAN_EVENT_ON_CHILD,
45 AT_FDCWD, "/home/alan/gitlive/gitlive/fanotify"
50 perror("fanotify_mark");
55 buflen = read(fan, buf, sizeof(buf));
56 metadata = (struct fanotify_event_metadata*)&buf;
57 printf("got something\n");
58 while(FAN_EVENT_OK(metadata, buflen)){
59 if (metadata->mask & FAN_Q_OVERFLOW){
60 printf("Queue overflow!\n");
63 printf("look at /proc/self/fd/%d\n", metadata->fd);
64 // Resolve path, using automatically opened fd
65 sprintf(fdpath, "/proc/self/fd/%d", metadata->fd);
66 linklen = readlink(fdpath, path, sizeof(path) - 1);
71 metadata = FAN_EVENT_NEXT(metadata, buflen);