BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file sys_watch_directory.c
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @section DESCRIPTION
30 *
31 * Directory watcher.
32 *
33 * Synopsis: sys.watch_directory(string dir)
34 * Description: reports directory entry events. Transitions up when an event is detected, and
35 * goes down waiting for the next event when sys.watch_directory::nextevent() is called.
36 * The directory is first scanned and "added" events are reported for all files.
37 * Variables:
38 * string event_type - what happened with the file: "added", "removed" or "changed"
39 * string filename - name of the file in the directory the event refers to
40 * string filepath - "dir/filename"
41 *
42 * Synopsis: sys.watch_directory::nextevent()
43 * Description: makes the watch_directory module transition down in order to report the next event.
44 */
45  
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include <sys/inotify.h>
50 #include <sys/types.h>
51 #include <dirent.h>
52 #include <errno.h>
53  
54 #include <misc/nonblocking.h>
55 #include <misc/concat_strings.h>
56  
57 #include <ncd/module_common.h>
58  
59 #include <generated/blog_channel_ncd_sys_watch_directory.h>
60  
61 #define MAX_INOTIFY_EVENTS 128
62  
63 struct instance {
64 NCDModuleInst *i;
65 NCDValNullTermString dir_nts;
66 DIR *dir_handle;
67 int inotify_fd;
68 BFileDescriptor bfd;
69 struct inotify_event events[MAX_INOTIFY_EVENTS];
70 int events_count;
71 int events_index;
72 int processing;
73 const char *processing_file;
74 const char *processing_type;
75 };
76  
77 static void instance_free (struct instance *o, int is_error);
78  
79 static void next_dir_event (struct instance *o)
80 {
81 ASSERT(!o->processing)
82 ASSERT(o->dir_handle)
83  
84 struct dirent *entry;
85  
86 do {
87 // get next entry
88 errno = 0;
89 if (!(entry = readdir(o->dir_handle))) {
90 if (errno != 0) {
91 ModuleLog(o->i, BLOG_ERROR, "readdir failed");
92 instance_free(o, 1);
93 return;
94 }
95  
96 // close directory
97 if (closedir(o->dir_handle) < 0) {
98 ModuleLog(o->i, BLOG_ERROR, "closedir failed");
99 o->dir_handle = NULL;
100 instance_free(o, 1);
101 return;
102 }
103  
104 // set no dir handle
105 o->dir_handle = NULL;
106  
107 // start receiving inotify events
108 BReactor_SetFileDescriptorEvents(o->i->params->iparams->reactor, &o->bfd, BREACTOR_READ);
109 return;
110 }
111 } while (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."));
112  
113 // set event
114 o->processing_file = entry->d_name;
115 o->processing_type = "added";
116 o->processing = 1;
117  
118 // signal up
119 NCDModuleInst_Backend_Up(o->i);
120 }
121  
122 static void assert_inotify_event (struct instance *o)
123 {
124 ASSERT(o->events_index < o->events_count)
125 ASSERT(o->events[o->events_index].len % sizeof(o->events[0]) == 0)
126 ASSERT(o->events[o->events_index].len / sizeof(o->events[0]) <= o->events_count - (o->events_index + 1))
127 }
128  
129 static const char * translate_inotify_event (struct instance *o)
130 {
131 assert_inotify_event(o);
132  
133 struct inotify_event *event = &o->events[o->events_index];
134  
135 if (strlen(event->name) > 0) {
136 if ((event->mask & (IN_CREATE | IN_MOVED_TO))) {
137 return "added";
138 }
139 if ((event->mask & (IN_DELETE | IN_MOVED_FROM))) {
140 return "removed";
141 }
142 if ((event->mask & IN_MODIFY)) {
143 return "changed";
144 }
145 }
146  
147 return NULL;
148 }
149  
150 static void skip_inotify_event (struct instance *o)
151 {
152 assert_inotify_event(o);
153  
154 o->events_index += 1 + o->events[o->events_index].len / sizeof(o->events[0]);
155 }
156  
157 static void next_inotify_event (struct instance *o)
158 {
159 ASSERT(!o->processing)
160 ASSERT(!o->dir_handle)
161  
162 // skip any bad events
163 while (o->events_index < o->events_count && !translate_inotify_event(o)) {
164 ModuleLog(o->i, BLOG_ERROR, "unknown inotify event");
165 skip_inotify_event(o);
166 }
167  
168 if (o->events_index == o->events_count) {
169 // wait for more events
170 BReactor_SetFileDescriptorEvents(o->i->params->iparams->reactor, &o->bfd, BREACTOR_READ);
171 return;
172 }
173  
174 // set event
175 o->processing_file = o->events[o->events_index].name;
176 o->processing_type = translate_inotify_event(o);
177 o->processing = 1;
178  
179 // consume this event
180 skip_inotify_event(o);
181  
182 // signal up
183 NCDModuleInst_Backend_Up(o->i);
184 }
185  
186 static void inotify_fd_handler (struct instance *o, int events)
187 {
188 if (o->processing) {
189 ModuleLog(o->i, BLOG_ERROR, "file descriptor error");
190 instance_free(o, 1);
191 return;
192 }
193  
194 ASSERT(!o->dir_handle)
195  
196 int res = read(o->inotify_fd, o->events, sizeof(o->events));
197 if (res < 0) {
198 ModuleLog(o->i, BLOG_ERROR, "read failed");
199 instance_free(o, 1);
200 return;
201 }
202  
203 // stop waiting for inotify events
204 BReactor_SetFileDescriptorEvents(o->i->params->iparams->reactor, &o->bfd, 0);
205  
206 ASSERT(res <= sizeof(o->events))
207 ASSERT(res % sizeof(o->events[0]) == 0)
208  
209 // setup buffer state
210 o->events_count = res / sizeof(o->events[0]);
211 o->events_index = 0;
212  
213 // process inotify events
214 next_inotify_event(o);
215 }
216  
217 static void next_event (struct instance *o)
218 {
219 ASSERT(o->processing)
220  
221 // set not processing
222 o->processing = 0;
223  
224 // signal down
225 NCDModuleInst_Backend_Down(o->i);
226  
227 if (o->dir_handle) {
228 next_dir_event(o);
229 return;
230 } else {
231 next_inotify_event(o);
232 return;
233 }
234 }
235  
236 static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
237 {
238 struct instance *o = vo;
239 o->i = i;
240  
241 // check arguments
242 NCDValRef dir_arg;
243 if (!NCDVal_ListRead(params->args, 1, &dir_arg)) {
244 ModuleLog(o->i, BLOG_ERROR, "wrong arity");
245 goto fail0;
246 }
247 if (!NCDVal_IsStringNoNulls(dir_arg)) {
248 ModuleLog(o->i, BLOG_ERROR, "wrong type");
249 goto fail0;
250 }
251  
252 // null terminate dir
253 if (!NCDVal_StringNullTerminate(dir_arg, &o->dir_nts)) {
254 ModuleLog(o->i, BLOG_ERROR, "NCDVal_StringNullTerminate failed");
255 goto fail0;
256 }
257  
258 // open inotify
259 if ((o->inotify_fd = inotify_init()) < 0) {
260 ModuleLog(o->i, BLOG_ERROR, "inotify_init failed");
261 goto fail1;
262 }
263  
264 // add watch
265 if (inotify_add_watch(o->inotify_fd, o->dir_nts.data, IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_FROM | IN_MOVED_TO) < 0) {
266 ModuleLog(o->i, BLOG_ERROR, "inotify_add_watch failed");
267 goto fail2;
268 }
269  
270 // set non-blocking
271 if (!badvpn_set_nonblocking(o->inotify_fd)) {
272 ModuleLog(o->i, BLOG_ERROR, "badvpn_set_nonblocking failed");
273 goto fail2;
274 }
275  
276 // init BFileDescriptor
277 BFileDescriptor_Init(&o->bfd, o->inotify_fd, (BFileDescriptor_handler)inotify_fd_handler, o);
278 if (!BReactor_AddFileDescriptor(o->i->params->iparams->reactor, &o->bfd)) {
279 ModuleLog(o->i, BLOG_ERROR, "BReactor_AddFileDescriptor failed");
280 goto fail2;
281 }
282  
283 // open directory
284 if (!(o->dir_handle = opendir(o->dir_nts.data))) {
285 ModuleLog(o->i, BLOG_ERROR, "opendir failed");
286 goto fail3;
287 }
288  
289 // set not processing
290 o->processing = 0;
291  
292 // read first directory entry
293 next_dir_event(o);
294 return;
295  
296 fail3:
297 BReactor_RemoveFileDescriptor(o->i->params->iparams->reactor, &o->bfd);
298 fail2:
299 if (close(o->inotify_fd) < 0) {
300 ModuleLog(o->i, BLOG_ERROR, "close failed");
301 }
302 fail1:
303 NCDValNullTermString_Free(&o->dir_nts);
304 fail0:
305 NCDModuleInst_Backend_DeadError(i);
306 }
307  
308 void instance_free (struct instance *o, int is_error)
309 {
310 // close directory
311 if (o->dir_handle) {
312 if (closedir(o->dir_handle) < 0) {
313 ModuleLog(o->i, BLOG_ERROR, "closedir failed");
314 }
315 }
316  
317 // free BFileDescriptor
318 BReactor_RemoveFileDescriptor(o->i->params->iparams->reactor, &o->bfd);
319  
320 // close inotify
321 if (close(o->inotify_fd) < 0) {
322 ModuleLog(o->i, BLOG_ERROR, "close failed");
323 }
324  
325 // free dir nts
326 NCDValNullTermString_Free(&o->dir_nts);
327  
328 if (is_error) {
329 NCDModuleInst_Backend_DeadError(o->i);
330 } else {
331 NCDModuleInst_Backend_Dead(o->i);
332 }
333 }
334  
335 static void func_die (void *vo)
336 {
337 struct instance *o = vo;
338 instance_free(o, 0);
339 }
340  
341 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
342 {
343 struct instance *o = vo;
344 ASSERT(o->processing)
345  
346 if (!strcmp(name, "event_type")) {
347 *out = NCDVal_NewString(mem, o->processing_type);
348 return 1;
349 }
350  
351 if (!strcmp(name, "filename")) {
352 *out = NCDVal_NewString(mem, o->processing_file);
353 return 1;
354 }
355  
356 if (!strcmp(name, "filepath")) {
357 char *str = concat_strings(3, o->dir_nts.data, "/", o->processing_file);
358 if (!str) {
359 ModuleLog(o->i, BLOG_ERROR, "concat_strings failed");
360 goto fail;
361 }
362  
363 *out = NCDVal_NewString(mem, str);
364  
365 free(str);
366 return 1;
367 }
368  
369 return 0;
370  
371 fail:
372 *out = NCDVal_NewInvalid();
373 return 1;
374 }
375  
376 static void nextevent_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
377 {
378 // check arguments
379 if (!NCDVal_ListRead(params->args, 0)) {
380 ModuleLog(i, BLOG_ERROR, "wrong arity");
381 goto fail0;
382 }
383  
384 // get method object
385 struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
386  
387 // make sure we are currently reporting an event
388 if (!mo->processing) {
389 ModuleLog(i, BLOG_ERROR, "not reporting an event");
390 goto fail0;
391 }
392  
393 // signal up.
394 // Do it before finishing the event so our process does not advance any further if
395 // we would be killed the event provider going down.
396 NCDModuleInst_Backend_Up(i);
397  
398 // wait for next event
399 next_event(mo);
400 return;
401  
402 fail0:
403 NCDModuleInst_Backend_DeadError(i);
404 }
405  
406 static struct NCDModule modules[] = {
407 {
408 .type = "sys.watch_directory",
409 .func_new2 = func_new,
410 .func_die = func_die,
411 .func_getvar = func_getvar,
412 .alloc_size = sizeof(struct instance)
413 }, {
414 .type = "sys.watch_directory::nextevent",
415 .func_new2 = nextevent_func_new
416 }, {
417 .type = NULL
418 }
419 };
420  
421 const struct NCDModuleGroup ncdmodule_sys_watch_directory = {
422 .modules = modules
423 };