-
Notifications
You must be signed in to change notification settings - Fork 0
/
inotify_add_watches.c
153 lines (133 loc) · 3.79 KB
/
inotify_add_watches.c
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#define _XOPEN_SOURCE 500
#include <unistd.h>
#include <fcntl.h>
#include "inotify_helper.h"
#include <sys/stat.h>
#include <stdio.h>
#include <sys/inotify.h>
#include <sys/types.h>
#include <limits.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <ftw.h>
#include <stdint.h>
char command[MAXBUF];
char dir[MAXBUF];
bool tool;
bool implicit;
bool verbose; // If true, prints additional output
#define CACHE_CONTROL_FILE "/proc/sys/vm/drop_caches"
#define CACHE_DROP_ALL_FLAG "3"
#define CACHE_DROP_CACHES_FLAG "2"
char *dfname;
int inotifyFd;
static int dirTree(const char *pathname, const struct stat *sbuf,
int typeflag, struct FTW *ftwb)
{
int wd;
if(typeflag & FTW_D) {
wd = inotify_add_watch(inotifyFd, pathname, IN_ALL_EVENTS);
if(wd == -1)
{
perror("inotify_add_watch");
}
printf("inotfyFD %d Adding watch to %s, wd %d \n",inotifyFd, pathname, wd);
}
return 0;
}
static int flush_caches()
{
int retVal = 0;
int fd;
if (geteuid() != 0) {
fprintf(stderr, "Cache flushing requires root.\n");
}
else {
sync();
fd = open(CACHE_CONTROL_FILE, O_RDWR);
if (fd == -1) {
fprintf(stderr, "%s: %s\n", strerror(errno),
CACHE_CONTROL_FILE);
}
else {
char drop_all_message[] = CACHE_DROP_ALL_FLAG;
int length = strlen(drop_all_message);
int written = write(fd, drop_all_message,
length);
if (written == -1) {
fprintf(stderr, "%s: %s\n", strerror(errno),
drop_all_message);
}
else if (written != length) {
fprintf(stderr, "Error: only wrote %d of %d bytes.\n",
written, length);
}
else {
//successful write
retVal = 1;
}
close(fd);
}
}
return retVal;
}
int main(int argc, char **argv)
{
char c;
int i;
char *fname = NULL;
int flags = 0;
FILE *log_fp;
// Parse the command line
while ((c = getopt(argc, argv, "hvid:")) != EOF) {
switch (c) {
case 'h': // Prints help message
break;
case 'v': // Emits additional diagnostic info
verbose = true;
break;
case 'i':
implicit = true;
break;
case 'd':
dfname = strdup(optarg);
dfname[strlen(dfname)] = '\0';
break;
default :
printf("Wrong usage. try again\n");
break;
}
}
flush_caches();
if (optind < argc)
printf("First nonoption argument is \"%s\" at argv[%d]\n",
argv[optind], optind);
printf(" Running test using %s \n", tool ? "inotify_tool" : (implicit ? "implicit watch" : "explicit watch"));
inotifyFd = inotify_init();
if(inotifyFd == -1) {
perror("inotify_init");
}
printf("dfname %s\n", dfname);
if(implicit)
{
int wd;
wd = inotify_add_watch(inotifyFd, dfname, (IN_ALL_EVENTS | 0x00800000));
if(wd == -1)
{
perror("inotify_add_watch implicit");
exit(-1);
}
printf("Added watch to %s, wsd %d \n", dfname, wd);
}
else
{
flags = FTW_DEPTH;
if(nftw(dfname, dirTree, 10, flags) == -1) {
perror("nftw");
exit(-1);
}
}
flush_caches();
while(1);
}