-
Notifications
You must be signed in to change notification settings - Fork 6
/
inotify-test.c
200 lines (161 loc) · 6.07 KB
/
inotify-test.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*******************************************************************************
Copyright (c) 2011 Dmitry Matveev <me@dmitrymatveev.co.uk>
Copyright (c) 2024 Serenity Cyber Security, LLC
Author: Gleb Popov <arrowd@FreeBSD.org>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*******************************************************************************/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <sys/uio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/inotify.h>
void get_event (int fd, const char * target);
void get_event_direct (int fd, const char * target);
void handle_error (int error);
/* ----------------------------------------------------------------- */
int main (int argc, char *argv[])
{
char target[FILENAME_MAX];
int ev_count = 0;
int fd;
int wd; /* watch descriptor */
int direct = 0;
struct rlimit rl;
rl.rlim_cur = 3072;
rl.rlim_max = 8172;
setrlimit (RLIMIT_NOFILE, &rl);
if (argc < 2) {
fprintf (stderr, "Watching the current directory\n");
strcpy (target, ".");
}
else {
fprintf (stderr, "Watching %s\n", argv[1]);
strcpy (target, argv[1]);
}
if (argc == 3 && !strcmp(argv[2], "direct"))
direct = 1;
fd = inotify_init1(direct ? IN_DIRECT : 0);
if (fd < 0) {
printf("inotify_init failed\n");
handle_error (errno);
return 1;
}
wd = inotify_add_watch (fd, target, IN_ALL_EVENTS);
if (wd < 0) {
printf("add_watch failed\n");
handle_error (errno);
return 1;
}
while (1) {
/* if (ev_count == 3) { */
/* printf("stopping watch\n"); */
/* inotify_rm_watch (fd, wd); */
/* } */
++ev_count;
if (direct)
get_event_direct(fd, target);
else
get_event(fd, target);
}
return 0;
}
/* ----------------------------------------------------------------- */
static void dump_event (struct inotify_event *pevent, char* action)
{
if (pevent->mask & IN_ACCESS)
strcat(action, " was read");
if (pevent->mask & IN_ATTRIB)
strcat(action, " Metadata changed");
if (pevent->mask & IN_CLOSE_WRITE)
strcat(action, " opened for writing was closed");
if (pevent->mask & IN_CLOSE_NOWRITE)
strcat(action, " not opened for writing was closed");
if (pevent->mask & IN_CREATE)
strcat(action, " created in watched directory");
if (pevent->mask & IN_DELETE)
strcat(action, " deleted from watched directory");
if (pevent->mask & IN_DELETE_SELF)
strcat(action, " watched file/directory was itself deleted");
if (pevent->mask & IN_MODIFY)
strcat(action, " was modified");
if (pevent->mask & IN_MOVE_SELF)
strcat(action, " watched file/directory was itself moved");
if (pevent->mask & IN_MOVED_FROM)
strcat(action, " moved out of watched directory");
if (pevent->mask & IN_MOVED_TO)
strcat(action, " moved into watched directory");
if (pevent->mask & IN_OPEN)
strcat(action, " was opened");
if (pevent->mask & IN_IGNORED)
strcat(action, " was ignored");
if (pevent->mask & IN_UNMOUNT)
strcat(action, " was unmounted");
/*
printf ("wd=%d mask=%x cookie=%d len=%d dir=%s\n",
pevent->wd, pevent->mask, pevent->cookie, pevent->len,
(pevent->mask & IN_ISDIR)?"yes":"no");
if (pevent->len) printf ("name=%s\n", pevent->name);
*/
printf ("%s [%s]\n", action, pevent->len ? pevent->name : "");
}
#define BUFF_SIZE (16*1024)
void get_event (int fd, const char * target)
{
ssize_t len, i = 0;
char buff[BUFF_SIZE] = {0};
len = read (fd, buff, BUFF_SIZE);
while (i < len) {
struct inotify_event *pevent = (struct inotify_event *)&buff[i];
char action[81+FILENAME_MAX] = {0};
if (pevent->len)
strcpy (action, pevent->name);
else
strcpy (action, target);
dump_event (pevent, action);
i += sizeof(struct inotify_event) + pevent->len;
}
} /* get_event */
void get_event_direct (int fd, const char * target)
{
struct iovec *received[5];
int num_events = libinotify_direct_readv (fd, received, 5, 0);
for (int i = 0; i < num_events; i++) {
struct iovec *cur_event = received[i];
while (cur_event->iov_base) {
struct inotify_event *pevent = (struct inotify_event *) cur_event->iov_base;
char action[81+FILENAME_MAX] = {0};
if (pevent->len)
strcpy (action, pevent->name);
else
strcpy (action, target);
dump_event (pevent, action);
cur_event++;
}
libinotify_free_iovec (received[i]);
}
} /* get_event */
/* ----------------------------------------------------------------- */
void handle_error (int error)
{
fprintf (stderr, "Error: %s\n", strerror(error));
} /* handle_error */
/* ----------------------------------------------------------------- */