revert code that did not affect memory
[gitlive] / fanotify / fanotify-example.c
1 /*
2  *   A simple tester of fanotify in the Linux kernel.
3  *
4  *   This program is released in the Public Domain.
5  *
6  *   Compile with:
7  *     $> gcc -o /tmp/fanotify-example fanotify-example.c
8  * 
9  *   Run as:
10  *     $> /tmp/fanotify-example 
11  */
12
13 #define _GNU_SOURCE
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <limits.h>
17 #include <poll.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <sys/types.h>
21 #include <sys/fanotify.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25  int main(int argc, char *argv[]){
26     int fan;
27     char buf[4096];
28     char fdpath[32];
29     char path[PATH_MAX + 1];
30     ssize_t buflen, linklen;
31     struct fanotify_event_metadata *metadata;
32
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);
38     
39     if(fan == -1){
40         perror("fanotify_init");
41         // exit(EXIT_FAILURE);
42     }
43  
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"
46     );  
47        
48        
49     if(ret == -1){
50         perror("fanotify_mark");
51         exit(EXIT_FAILURE);
52     }
53
54     while(1){
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");
61                 continue;
62             }
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);
67             path[linklen] = '\0';
68             printf("%s\n", path);
69
70             close(metadata->fd);
71             metadata = FAN_EVENT_NEXT(metadata, buflen);
72         }
73     }
74 }
75
76  
77