-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.cpp
120 lines (102 loc) · 2.52 KB
/
notify.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <sys/fanotify.h>
#include <unistd.h>
#include "notify.h"
#include "results.h"
static int fd = 0;
static const int BUFFLEN = 200;
static bool keepGoing;
static pthread_t thread;
int my_fanotify_init(char* dir)
{
keepGoing = true;
fd = fanotify_init(FAN_CLASS_NOTIF | FAN_CLOEXEC, O_RDONLY | O_LARGEFILE);
if (fd == -1)
{
perror("fanotify_init");
exit(EXIT_FAILURE);
}
if (fanotify_mark(fd, FAN_MARK_ADD | FAN_MARK_MOUNT, FAN_OPEN | FAN_OPEN_EXEC, AT_FDCWD, dir) == -1)
{
perror("fanotify_mark");
exit(EXIT_FAILURE);
}
return fd;
}
void my_fanotify_start()
{
pthread_create(&thread, NULL, my_fanotify_get_event, NULL);
}
// Functionality here mostly nicked from the fanotify man page example
void* my_fanotify_get_event(void* args)
{
ssize_t len;
ssize_t path_len;
struct fanotify_event_metadata* metadata;
struct fanotify_event_metadata* buff = new struct fanotify_event_metadata[BUFFLEN];
char* path = new char[PATH_MAX];
char* procfd_path = new char[PATH_MAX];
/* Read some events */
while (keepGoing)
{
len = read(fd, (void *) buff, sizeof(fanotify_event_metadata) * BUFFLEN);
if (len == -1 && errno != EAGAIN)
{
perror("read");
exit(EXIT_FAILURE);
}
if (len <= 0)
{
return NULL;
}
metadata = buff;
while (FAN_EVENT_OK(metadata, len))
{
/* Check that run-time and compile-time structures match */
if (metadata->vers != FANOTIFY_METADATA_VERSION)
{
fprintf(stderr, "Mismatch of fanotify metadata version.\n");
exit(EXIT_FAILURE);
}
/* metadata->fd contains either FAN_NOFD, indicating a
queue overflow, or a file descriptor (a nonnegative
integer). Here, we simply ignore queue overflow. */
if (metadata->fd >= 0)
{
/* Retrieve and print pathname of the accessed file */
snprintf(procfd_path, PATH_MAX * sizeof(char), "/proc/self/fd/%d", metadata->fd);
path_len = readlink(procfd_path, path, PATH_MAX * sizeof(char) - 1);
if (path_len == -1)
{
perror("readlink");
exit(EXIT_FAILURE);
}
path[path_len] = '\0';
results_add(path);
/* Close the file descriptor of the event */
close(metadata->fd);
}
/* Advance to next event */
metadata = FAN_EVENT_NEXT(metadata, len);
}
}
delete [] path;
delete [] procfd_path;
delete [] buff;
return NULL;
}
void my_fanotify_stop()
{
keepGoing = false;
}
void my_notify_destroy()
{
pthread_join(thread, NULL);
close(fd);
}