source: trunk/src/sh_inotify.c@ 373

Last change on this file since 373 was 373, checked in by katerina, 13 years ago

Patch for ticket #265 (inotify). Handling of added subdirectories and file list rescan.

File size: 23.0 KB
RevLine 
[261]1/* SAMHAIN file system integrity testing */
2/* Copyright (C) 2009 Rainer Wichmann */
3/* */
4/* This program is free software; you can redistribute it */
5/* and/or modify */
6/* it under the terms of the GNU General Public License as */
7/* published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14/* GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
18/* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20#include "config_xor.h"
21
22#if defined(HAVE_SYS_INOTIFY_H)
23
24#undef FIL__
25#define FIL__ _("sh_inotify.c")
26
27/* printf */
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#include <sys/inotify.h>
32#include <errno.h>
33#include <unistd.h>
34#include <fcntl.h>
35
36#include "samhain.h"
37#include "sh_pthread.h"
38#include "sh_calls.h"
39#include "sh_inotify.h"
40#include "sh_mem.h"
[364]41#include "sh_utils.h"
[261]42#include "slib.h"
43
44/**************************************************
45 *
46 * Make the inotify fd thread-specific by
47 * encapsulating it in get/set functions:
48 * sh_get_inotify_fd() / sh_set_inotify_fd()
49 *
50 **************************************************/
51
52#if defined(HAVE_PTHREAD)
[367]53
54SH_MUTEX_STATIC(mutex_list_dormant, PTHREAD_MUTEX_INITIALIZER);
55SH_MUTEX_STATIC(mutex_watches, PTHREAD_MUTEX_INITIALIZER);
56
[261]57static pthread_key_t inotify_key;
58static pthread_once_t inotify_key_once = PTHREAD_ONCE_INIT;
59
60static void make_inotify_key()
61{
62 (void) pthread_key_create(&inotify_key, free);
63}
64
65static int sh_get_inotify_fd()
66{
67 void * ptr;
68 int * fp;
69
70 (void) pthread_once(&inotify_key_once, make_inotify_key);
71
72 if ((ptr = pthread_getspecific(inotify_key)) == NULL)
73 {
74 ptr = malloc(sizeof(int));
75 if (ptr)
76 {
77 fp = (int*) ptr;
78 *fp = -1;
79 (void) pthread_setspecific(inotify_key, ptr);
80 }
81 else
82 {
83 return -1;
84 }
85 }
86 else
87 {
88 fp = (int*) ptr;
89 }
90 return *fp;
91}
92
93static void sh_set_inotify_fd(int fd)
94{
95 int * fp;
96
97 fp = (int*) pthread_getspecific(inotify_key);
98 if (fp)
99 *fp = fd;
100 return;
101}
102
103/* !defined(HAVE_PTHREAD) */
104#else
105
106static int sh_inotify_fd = -1;
107
108static inline int sh_get_inotify_fd()
109{
110 return sh_inotify_fd;
111}
112
113static inline void sh_set_inotify_fd(int fd)
114{
115 sh_inotify_fd = fd;
116}
117
118#endif
119
120/*--- nothing thread-related below this point --- */
121
[364]122#include "zAVLTree.h"
[261]123
[364]124typedef struct
125{
126 int watch;
[372]127 short flag;
128 short type;
[367]129 int class;
[373]130 int rdepth;
[367]131 unsigned long check_mask;
[364]132 char * file;
133} sh_watch;
134
[261]135/**************************************************
136 *
137 * Get inotify fd, initialize inotify if necessary
138 *
139 **************************************************/
140#define SH_INOTIFY_FAILED -2
141
142static int sh_inotify_getfd()
143{
144 int ifd = sh_get_inotify_fd();
145
146 if (ifd >= 0)
147 {
148 return ifd;
149 }
150
151 else if (ifd == SH_INOTIFY_FAILED)
152 {
153 return -1;
154 }
155
156 else /* if (ifd == -1) */
157 {
158#if defined(HAVE_INOTIFY_INIT1)
159 ifd = inotify_init1(IN_CLOEXEC);
160#else
161 ifd = inotify_init();
162 if (ifd >= 0)
163 {
164 long sflags;
165
[268]166 sflags = retry_fcntl(FIL__, __LINE__, ifd, F_GETFD, 0);
167 retry_fcntl(FIL__, __LINE__, ifd, F_SETFD, sflags|FD_CLOEXEC);
[261]168 }
169#endif
170
171 if (ifd < 0)
172 {
173 sh_set_inotify_fd(SH_INOTIFY_FAILED);
174 return -1;
175 }
176
177 sh_set_inotify_fd(ifd);
178 return ifd;
179 }
180}
181
182/**************************************************
183 *
184 * Public function:
185 * int sh_inotify_wait_for_change(char * filename,
186 * int watch,
187 * int * errnum,
188 * int waitsec);
189 * Returns: watch, if nonnegative
190 * -1 on error or reopen required
191 * (check errnum != 0)
192 *
193 * Caller needs to keep track of watch descriptor
194 *
195 **************************************************/
196
197#define SH_INOTIFY_REOPEN 0
198#define SH_INOTIFY_MODIFY 1
199
[367]200void sh_inotify_init(sh_watches * watches)
[261]201{
[367]202 SH_MUTEX_LOCK_UNSAFE(mutex_watches);
[364]203 watches->list_of_watches = NULL;
204 watches->count = 0;
205 watches->max_count = 0;
[367]206 SH_MUTEX_UNLOCK_UNSAFE(mutex_watches);
207
208 SH_MUTEX_LOCK_UNSAFE(mutex_list_dormant);
209 watches->dormant_watches = NULL;
210 SH_MUTEX_UNLOCK_UNSAFE(mutex_list_dormant);
211
[364]212 return;
213}
[261]214
[367]215ssize_t sh_inotify_read(char * buffer, size_t count)
216{
[371]217 ssize_t len = -1;
[367]218 int ifd = sh_inotify_getfd();
219
220 do {
[369]221 len = read (ifd, buffer, count);
[371]222 } while (len < 0 && (errno == EINTR || errno == EAGAIN));
[367]223
224 return len;
225}
226
[371]227ssize_t sh_inotify_read_timeout(char * buffer, size_t count, int timeout)
228{
229 ssize_t len;
230 int ifd = sh_inotify_getfd();
231
232 len = sl_read_timeout_fd (ifd, buffer, count, timeout, SL_FALSE);
233
234 return len;
235}
236
237
[364]238static void sh_inotify_free_watch(void * item)
239{
240 sh_watch * this = (sh_watch *) item;
241
242 if (this->file)
243 SH_FREE(this->file);
244 SH_FREE(this);
245 return;
246}
247
[373]248static sh_watch * sh_inotify_create_watch(const char * file, int nwatch, int flag)
[364]249{
250 sh_watch * this = SH_ALLOC(sizeof(sh_watch));
251
252 this->file = sh_util_strdup(file);
253 this->watch = nwatch;
254 this->flag = flag;
255 return this;
256}
257
258/********** List Handling ******************/
259
260struct sh_inotify_litem
261{
262 sh_watch * watch;
263 struct sh_inotify_litem * next;
264};
265
[367]266static void sh_inotify_listitem_destroy(struct sh_inotify_litem * this)
267{
268 if (this)
269 SH_FREE(this);
270 return;
271}
272
273/* No Mutex in the list cursor functions, must be in the caller
274 * function...
275 */
[364]276typedef struct {
277 struct sh_inotify_litem *prenode;
278 struct sh_inotify_litem *curnode;
279} sh_inotify_listCursor;
280
[367]281static sh_watch * sh_inotify_list_first(sh_inotify_listCursor * listcursor,
282 sh_watches * watches)
[364]283{
284 listcursor->prenode = watches->dormant_watches;
285 listcursor->curnode = watches->dormant_watches;
286
[367]287 if (listcursor->curnode)
288 return listcursor->curnode->watch;
289 return NULL;
[364]290}
291
[367]292static sh_watch * sh_inotify_list_next(sh_inotify_listCursor * listcursor,
293 sh_watches * watches)
[364]294{
295 (void) watches;
296
297 listcursor->prenode = listcursor->curnode;
298
299 if (listcursor->curnode)
[367]300 {
301 listcursor->curnode = listcursor->curnode->next;
[371]302 if (listcursor->curnode)
303 return listcursor->curnode->watch;
304 else
305 return NULL;
[367]306 }
307
[364]308 return NULL;
309}
310
[367]311static sh_watch * sh_inotify_list_del_cur(sh_inotify_listCursor * listcursor,
312 sh_watches * watches)
[364]313{
314 sh_watch * ret = NULL;
315
316 if (listcursor->curnode)
[261]317 {
[364]318 struct sh_inotify_litem * this = listcursor->curnode;
319
320 if (listcursor->prenode == this)
[261]321 {
[364]322 watches->dormant_watches = this->next;
323
324 listcursor->prenode = watches->dormant_watches;
325 listcursor->curnode = watches->dormant_watches;
[261]326 }
[364]327 else
328 {
329 listcursor->prenode->next = this->next;
330 listcursor->curnode = this->next;
331 }
[367]332 if (listcursor->curnode)
333 ret = listcursor->curnode->watch;
334 else
335 ret = NULL;
[364]336 sh_inotify_listitem_destroy(this);
[261]337 }
[364]338 return ret;
339}
340
341static int sh_inotify_add_dormant(sh_watches * watches, sh_watch * item)
342{
[367]343 struct sh_inotify_litem * this;
[364]344
[367]345 SH_MUTEX_LOCK(mutex_list_dormant);
346 this = SH_ALLOC(sizeof(struct sh_inotify_litem));
347
[364]348 this->watch = item;
349 this->next = (struct sh_inotify_litem *) watches->dormant_watches;
350
351 watches->dormant_watches = this;
[367]352 SH_MUTEX_UNLOCK(mutex_list_dormant);
[364]353 return 0;
354}
355
[367]356static void * sh_dummy_popret = NULL;
357
358char * sh_inotify_pop_dormant(sh_watches * watches,
[373]359 int * class, unsigned long * check_mask,
360 int * type, int * rdepth)
[367]361{
362 char * popret = NULL;
363 struct sh_inotify_litem * this;
364
365 /* Take the address to keep gcc from putting it into a register.
366 * Avoids the 'clobbered by longjmp' warning.
367 */
368 sh_dummy_popret = (void *) &popret;
369
370 SH_MUTEX_LOCK(mutex_list_dormant);
371
372 this = (struct sh_inotify_litem *) watches->dormant_watches;
373
374 if (this)
375 {
[373]376 *class = this->watch->class;
377 *type = this->watch->type;
378 *rdepth = this->watch->rdepth;
[367]379 *check_mask = this->watch->check_mask;
[373]380 popret = sh_util_strdup(this->watch->file);
[367]381
382 watches->dormant_watches = this->next;
383
384 sh_inotify_free_watch(this->watch);
385 SH_FREE(this);
386 }
387 SH_MUTEX_UNLOCK(mutex_list_dormant);
388
389 return popret;
390}
391
392void sh_inotify_purge_dormant(sh_watches * watches)
393{
394 struct sh_inotify_litem * this;
395
396 SH_MUTEX_LOCK(mutex_list_dormant);
397 this = (struct sh_inotify_litem *) watches->dormant_watches;
398
399 watches->dormant_watches = NULL;
400
401 while (this)
402 {
403 struct sh_inotify_litem * cur = this;
404
405 this = this->next;
406
407 sh_inotify_free_watch(cur->watch);
408 SH_FREE(cur);
409 }
410 SH_MUTEX_UNLOCK(mutex_list_dormant);
411 return;
412}
413
[364]414/********** End List Handling **************/
415
416static zAVLKey sh_inotify_getkey(void const *item)
417{
418 return (&((sh_watch *)item)->watch);
419}
420
421
422/* This function removes all watches from the list,
423 * and closes the inode file descriptor in this thread.
424 */
425void sh_inotify_remove(sh_watches * watches)
426{
427 int ifd = sh_inotify_getfd();
[367]428 zAVLTree * all_watches;
[364]429
[367]430 SH_MUTEX_LOCK(mutex_watches);
431 all_watches = (zAVLTree *)(watches->list_of_watches);
432
[364]433 if (all_watches)
434 zAVLFreeTree(all_watches, sh_inotify_free_watch);
435
[367]436 watches->list_of_watches = NULL;
437 watches->count = 0;
438 SH_MUTEX_UNLOCK(mutex_watches);
[364]439
[261]440 if (ifd >= 0)
441 close(ifd);
442 sh_set_inotify_fd(-1);
443
444 return;
445}
446
447static int index_watched_file(char * filename, sh_watches * watches)
448{
[364]449 sh_watch * item;
450 zAVLCursor avlcursor;
451 zAVLTree * all_watches = (zAVLTree *)(watches->list_of_watches);
[261]452
[364]453 if (all_watches)
[261]454 {
[364]455 for (item = (sh_watch *) zAVLFirst(&avlcursor, all_watches); item;
456 item = (sh_watch *) zAVLNext(&avlcursor))
457 {
458 if (item->file)
459 {
460 if (0 == strcmp(filename, item->file))
461 return item->watch;
462 }
463 }
[261]464 }
465 return -1;
466}
467
[367]468#define SH_INOTIFY_FILEFLAGS \
469 (IN_ATTRIB|IN_MODIFY|IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT)
470#define SH_INOTIFY_DIRFLAGS \
471 (SH_INOTIFY_FILEFLAGS|IN_DELETE|IN_CREATE|IN_MOVED_FROM|IN_MOVED_TO)
472
473#define SH_INOTIFY_FLAGS (SH_INOTIFY_FILEFLAGS|SH_INOTIFY_DIRFLAGS)
474
475/* Create an item and put it on the 'dormant' list for later watch creation
476 */
[373]477int sh_inotify_add_watch_later(const char * filename, sh_watches * watches,
[367]478 int * errnum,
[373]479 int class, unsigned long check_mask, int type, int rdepth)
[367]480{
481 sh_watch * item;
482
483 item = sh_inotify_create_watch(filename, -1, /* flag */ 0);
484
485 item->class = class;
[372]486 item->type = (short) type;
[373]487 item->rdepth = (short) rdepth;
[367]488 item->check_mask = check_mask;
489
490 sh_inotify_add_dormant(watches, item);
491 if (errnum)
492 *errnum = 0;
493
494 return 0;
495}
496
497int sh_inotify_rm_watch (sh_watches * watches, sh_watches * save, int wd)
498{
499 int ifd = sh_get_inotify_fd();
500
501 if (watches)
502 {
503 sh_watch * item;
504
505 SH_MUTEX_LOCK(mutex_watches);
506 item = zAVLSearch(watches->list_of_watches, &wd);
507
508 if (item)
509 {
510 zAVLDelete(watches->list_of_watches, &wd);
511 if (save) /* optionally save the item */
512 {
513 item->watch = -1;
514 sh_inotify_add_dormant(save, item);
515 }
516 else
517 {
518 sh_inotify_free_watch(item);
519 }
520 }
521 SH_MUTEX_UNLOCK(mutex_watches);
522 }
523 return inotify_rm_watch(ifd, wd);
524}
525
526#if (defined(SH_WITH_CLIENT) || defined(SH_STANDALONE))
527static void * sh_dummy_litem;
528
529int sh_inotify_recheck_watches (sh_watches * watches, sh_watches * save)
530{
531 sh_watch * litem;
532 sh_inotify_listCursor listcursor;
533 int ifd = sh_get_inotify_fd();
534
[371]535 extern void sh_fInotify_report_add(char * path,
536 int class, unsigned long check_mask);
[367]537
538 sh_dummy_litem = (void*) &litem;
539
540 /* -- Check dormant watches for reopening.
541 */
542 SH_MUTEX_LOCK(mutex_list_dormant);
543
[371]544 litem = sh_inotify_list_first(&listcursor, save);
545
546 while (litem)
[367]547 {
548 have_next:
549
550 /* sh_inotify_list_del_cur may return NULL */
551 if (litem && litem->file && litem->watch == -1)
552 {
553 litem->watch = inotify_add_watch (ifd, litem->file,
554 SH_INOTIFY_FLAGS);
555
556 if (litem->watch >= 0)
557 {
558 SH_MUTEX_LOCK(mutex_watches);
559 if (watches->list_of_watches)
560 zAVLInsert(watches->list_of_watches, litem);
561 SH_MUTEX_UNLOCK(mutex_watches);
562
563 sh_fInotify_report_add(litem->file, litem->class, litem->check_mask);
564
565 litem = sh_inotify_list_del_cur(&listcursor, save);
566
567 goto have_next;
568 }
569 }
[371]570 litem = sh_inotify_list_next(&listcursor, save);
[367]571 }
572 SH_MUTEX_UNLOCK(mutex_list_dormant);
573 return 0;
574}
575#endif
576
[261]577/* This function is idempotent; it will add the watch only once
578 */
[367]579int sh_inotify_add_watch(char * filename, sh_watches * watches, int * errnum,
[373]580 int class, unsigned long check_mask, int type, int rdepth)
[261]581{
[367]582 volatile int retval = 0;
583
584 SH_MUTEX_LOCK(mutex_watches);
585
[261]586 *errnum = 0;
587
588 if (filename)
589 {
590 int nwatch;
[364]591 sh_watch * item;
[261]592 int index = index_watched_file(filename, watches);
593
594 if (index < 0)
595 {
596 int ifd = sh_inotify_getfd();
597
[364]598 /*************************************
599
[261]600 if (watches->count == SH_INOTIFY_MAX)
601 {
602#ifdef EMFILE
603 *errnum = EMFILE;
604#else
605 *errnum = 24;
606#endif
607 return -1;
608 }
[364]609 **************************************/
[261]610
611 nwatch = inotify_add_watch (ifd, filename,
[367]612 SH_INOTIFY_FLAGS);
[261]613 if (nwatch < 0)
614 {
615 *errnum = errno;
[367]616 retval = -1;
617 goto retpoint;
[261]618 }
[364]619
620 item = sh_inotify_create_watch(filename, nwatch, /* flag */ 0);
[367]621
622 item->class = class;
[372]623 item->type = type;
[373]624 item->rdepth = rdepth;
[367]625 item->check_mask = check_mask;
[261]626
[364]627 if (NULL == watches->list_of_watches)
[367]628 watches->list_of_watches = zAVLAllocTree (sh_inotify_getkey,
629 zAVL_KEY_INT);
[364]630
631 if (watches->list_of_watches)
632 {
[367]633 *errnum = zAVLInsert((zAVLTree *)(watches->list_of_watches),
634 item);
635
[364]636 if (*errnum != 0)
637 {
[367]638 (*errnum == -1) ? *errnum = ENOMEM : EEXIST;
[364]639 sh_inotify_free_watch(item);
[367]640 retval = -1;
641 goto retpoint;
[364]642 }
643 }
644 else
645 {
[367]646 *errnum = ENOMEM;
647 retval = -1;
648 goto retpoint;
[364]649 }
[261]650
651 ++(watches->count);
652 }
[373]653 else if (type == SH_INOTIFY_DIR) /* watch exists */
654 {
655 /* This covers the case that a directory has been added,
656 * but is watched as file at first because it is also
657 * specified as file in the config.
658 */
659 item = zAVLSearch(watches->list_of_watches, &index);
660
661 if (item && item->type == SH_INOTIFY_FILE)
662 {
663 item->type = SH_INOTIFY_DIR;
664 }
665 }
[261]666 }
[367]667 retpoint:
668 SH_MUTEX_UNLOCK(mutex_watches);
669 return retval;
[261]670}
671
[367]672static void * sh_dummy_sret = NULL;
673
674char * sh_inotify_search_item(sh_watches * watches, int watch,
[373]675 int * class, unsigned long * check_mask,
676 int * type, int * rdepth)
[367]677{
678 sh_watch * item;
679 char * sret = NULL;
680
681 /* Take the address to keep gcc from putting it into a register.
682 * Avoids the 'clobbered by longjmp' warning.
683 */
684 sh_dummy_sret = (void *) &sret;
685
686 SH_MUTEX_LOCK(mutex_watches);
687 item = zAVLSearch(watches->list_of_watches, &watch);
688
689 if (item)
690 {
691 *class = item->class;
692 *check_mask = item->check_mask;
[372]693 *type = item->type;
[373]694 *rdepth = item->rdepth;
[367]695 sret = sh_util_strdup(item->file);
696 }
697 SH_MUTEX_UNLOCK(mutex_watches);
698 return sret;
699}
700
701static void * sh_dummy_litem = NULL;
702
[261]703int sh_inotify_wait_for_change(char * filename, sh_watches * watches,
704 int * errnum, int waitsec)
705{
[367]706 sh_watch * litem;
707 sh_watch * zitem;
[364]708 int ifd = sh_inotify_getfd();
[367]709
710 /* Take the address to keep gcc from putting it into a register.
711 * Avoids the 'clobbered by longjmp' warning.
712 */
713 sh_dummy_litem = (void*) &litem;
714
[261]715 *errnum = 0;
716
717 start_it:
718
719 if (ifd >= 0)
720 {
[367]721 volatile ssize_t i = 0;
[261]722 ssize_t len = -1;
723 int flag = 0;
724 char buffer[1024];
725
[364]726 sh_inotify_listCursor listcursor;
727
[261]728 /* -- Add watch if required
729 */
730 if (filename)
731 {
[373]732 if (sh_inotify_add_watch(filename, watches, errnum,
733 0, 0, SH_INOTIFY_FILE, 0) < 0)
[261]734 {
735 retry_msleep(waitsec, 0);
736 return -1;
737 }
738 }
739
[364]740 /* -- Check dormant watches for reopening.
741 */
[367]742 SH_MUTEX_LOCK(mutex_list_dormant);
743
744 for (litem = sh_inotify_list_first(&listcursor, watches); litem;
745 litem = sh_inotify_list_next(&listcursor, watches))
[261]746 {
[364]747 have_next:
[367]748 /* sh_inotify_list_del_cur may return NULL */
749 if (litem && litem->file && litem->watch == -1)
[364]750 {
[367]751 litem->watch = inotify_add_watch (ifd, litem->file,
752 SH_INOTIFY_FLAGS);
753
754 if (litem->watch >= 0)
[364]755 {
[367]756 SH_MUTEX_LOCK(mutex_watches);
757 if (watches->list_of_watches)
758 zAVLInsert(watches->list_of_watches, litem);
759 SH_MUTEX_UNLOCK(mutex_watches);
760 litem = sh_inotify_list_del_cur(&listcursor, watches);
[364]761 goto have_next;
762 }
763 }
[261]764 }
[367]765 SH_MUTEX_UNLOCK(mutex_list_dormant);
[261]766
767
768 /* -- Blocking read on inotify file descriptor
769 */
[371]770 len = sh_inotify_read(buffer, sizeof(buffer));
[261]771
772 if (len > 0)
773 {
774 struct inotify_event *event;
775
776 i = 0;
777
778 while (i < len) {
779
780 event = (struct inotify_event *) &buffer[i];
781
[367]782 SH_MUTEX_LOCK(mutex_watches);
783 zitem = zAVLSearch(watches->list_of_watches, &(event->wd));
[364]784
[367]785 if (zitem)
[261]786 {
[364]787 if (event->mask & IN_MODIFY)
[261]788 {
[367]789 zitem->flag |= SH_INOTIFY_MODIFY;
[364]790 flag |= SH_INOTIFY_MODIFY;
[261]791 }
[364]792 else if (event->mask & IN_DELETE_SELF ||
793 event->mask & IN_UNMOUNT ||
794 event->mask & IN_MOVE_SELF )
795 {
[367]796 zitem->flag |= SH_INOTIFY_REOPEN;
797 (void) inotify_rm_watch(ifd, zitem->watch);
798 zAVLDelete(watches->list_of_watches, zitem);
799 sh_inotify_add_dormant(watches, zitem);
800 zitem->watch = -1;
[364]801 flag |= SH_INOTIFY_REOPEN;
802 }
[261]803 }
[367]804 SH_MUTEX_UNLOCK(mutex_watches);
[364]805
[261]806 i += sizeof (struct inotify_event) + event->len;
807 }
808 }
809 else if (len == -1)
810 {
811 *errnum = errno;
812 retry_msleep(waitsec, 0);
813
814 return -1;
815 }
816
817 if (flag & SH_INOTIFY_REOPEN)
818 {
819 if (flag & SH_INOTIFY_MODIFY)
820 return 0;
821 else
822 goto start_it;
823 }
824
825 return 0;
826 }
827
828 /* Inotify not working, sleep
829 */
830 retry_msleep(waitsec, 0);
831
832 *errnum = 0;
833 return -1;
834}
835
[367]836
[261]837/* !defined(HAVE_SYS_INOTIFY_H) */
838#else
839
840#include "sh_calls.h"
[269]841#include "sh_inotify.h"
[261]842
843void sh_inotify_remove(sh_watches * watches)
844{
845 (void) watches;
846 return;
847}
848
849int sh_inotify_wait_for_change(char * filename, sh_watches * watches,
850 int * errnum, int waitsec)
851{
852 (void) filename;
853 (void) watches;
854
855 /* Inotify not working, sleep for waitsec seconds
856 */
857 retry_msleep(waitsec, 0);
858
859 *errnum = 0;
860 return -1;
861}
862
[367]863int sh_inotify_add_watch(char * filename, sh_watches * watches, int * errnum,
[373]864 int class, unsigned long check_mask, int type, int rdepth)
[261]865{
866 (void) filename;
867 (void) watches;
[367]868 (void) class;
869 (void) check_mask;
[373]870 (void) type;
871 (void) rdepth;
[261]872 *errnum = 0;
873 return 0;
874}
875
[373]876int sh_inotify_add_watch_later(const char * filename, sh_watches * watches,
[367]877 int * errnum,
[373]878 int class, unsigned long check_mask, int type, int rdepth)
[367]879{
880 (void) filename;
881 (void) watches;
882 (void) class;
883 (void) check_mask;
[373]884 (void) type;
885 (void) rdepth;
[367]886 *errnum = 0;
887 return 0;
888}
889
[261]890#endif
[367]891
892#ifdef SH_CUTEST
893#include "CuTest.h"
894void Test_inotify(CuTest *tc) {
895#if defined(HAVE_SYS_INOTIFY_H) && (defined(SH_WITH_CLIENT) || defined(SH_STANDALONE))
896
897 int ret;
898 sh_watches twatch = SH_INOTIFY_INITIALIZER;
899 sh_watch * litem;
900 sh_inotify_listCursor listcursor;
901 char * p;
902 int class;
[373]903 int type;
904 int rdepth;
[367]905 unsigned long check_mask;
906 int nrun = 0;
907
[373]908 sh_watch aw1 = { -1, 0, 0, 1, 99, 1, "a1" };
909 sh_watch aw2 = { -1, 0, 0, 2, 99, 1, "a2" };
910 sh_watch aw3 = { 2, 0, 0, 3, 99, 1, "a3" };
911 sh_watch aw4 = { -1, 0, 0, 4, 99, 1, "a4" };
912 sh_watch aw5 = { 5, 0, 0, 5, 99, 1, "a5" };
[367]913
914 do {
915
916 int count = 0;
917
918 sh_watch * w1 = SH_ALLOC(sizeof(sh_watch));
919 sh_watch * w2 = SH_ALLOC(sizeof(sh_watch));
920 sh_watch * w3 = SH_ALLOC(sizeof(sh_watch));
921 sh_watch * w4 = SH_ALLOC(sizeof(sh_watch));
922 sh_watch * w5 = SH_ALLOC(sizeof(sh_watch));
923
924 memcpy(w1, &aw1, sizeof(sh_watch));
925 w1->file = sh_util_strdup(aw1.file);
926 memcpy(w2, &aw2, sizeof(sh_watch));
927 w2->file = sh_util_strdup(aw2.file);
928 memcpy(w3, &aw3, sizeof(sh_watch));
929 w3->file = sh_util_strdup(aw3.file);
930 memcpy(w4, &aw4, sizeof(sh_watch));
931 w4->file = sh_util_strdup(aw4.file);
932 memcpy(w5, &aw5, sizeof(sh_watch));
933 w5->file = sh_util_strdup(aw5.file);
934
935 ret = sh_inotify_add_dormant(&twatch, w1);
936 CuAssertIntEquals(tc, ret, 0);
937 ret = sh_inotify_add_dormant(&twatch, w2);
938 CuAssertIntEquals(tc, ret, 0);
939 ret = sh_inotify_add_dormant(&twatch, w3);
940 CuAssertIntEquals(tc, ret, 0);
941 ret = sh_inotify_add_dormant(&twatch, w4);
942 CuAssertIntEquals(tc, ret, 0);
943 ret = sh_inotify_add_dormant(&twatch, w5);
944 CuAssertIntEquals(tc, ret, 0);
945
946 /* -- Check dormant watches for reopening.
947 */
948 for (litem = sh_inotify_list_first(&listcursor, &twatch); litem;
949 litem = sh_inotify_list_next(&listcursor, &twatch))
950 {
951 have_next:
952
953 /* sh_inotify_list_del_cur may return NULL */
954 if (litem)
955 {
956 ++count;
957
958 if (litem->file && litem->watch == -1)
959 {
960
961 switch (litem->class)
962 {
963 case 1:
964 CuAssertStrEquals(tc, litem->file, "a1");
965 break;
966 case 2:
967 CuAssertStrEquals(tc, litem->file, "a2");
968 break;
969 case 3:
970 CuAssertStrEquals(tc, litem->file, "deadbeef");
971 break;
972 case 4:
973 CuAssertStrEquals(tc, litem->file, "a4");
974 break;
975 case 5:
976 CuAssertStrEquals(tc, litem->file, "deadbeef");
977 break;
978 default:
979 CuAssertStrEquals(tc, litem->file, "deadbeef");
980 }
981 litem = sh_inotify_list_del_cur(&listcursor, &twatch);
982 goto have_next;
983 }
984 switch (litem->class)
985 {
986 case 3:
987 CuAssertStrEquals(tc, litem->file, "a3");
988 break;
989 case 5:
990 CuAssertStrEquals(tc, litem->file, "a5");
991 break;
992 default:
993 CuAssertStrEquals(tc, litem->file, "foobar");
994 }
995 }
996 }
997
998 CuAssertIntEquals(tc, count, 5);
999
[373]1000 p = sh_inotify_pop_dormant(&twatch, &class, &check_mask, &type, &rdepth);
[367]1001 CuAssertStrEquals(tc, p, "a5");
1002
[373]1003 p = sh_inotify_pop_dormant(&twatch, &class, &check_mask, &type, &rdepth);
[367]1004 CuAssertStrEquals(tc, p, "a3");
1005 CuAssertIntEquals(tc, class, 3);
1006
[373]1007 p = sh_inotify_pop_dormant(&twatch, &class, &check_mask, &type, &rdepth);
[367]1008 CuAssertTrue(tc, NULL == p);
1009 CuAssertTrue(tc, NULL == twatch.dormant_watches);
1010
1011 ++nrun;
1012
1013 } while (nrun < 100);
1014
1015#else
1016 (void) tc;
1017#endif
1018
1019 return;
1020}
1021#endif
Note: See TracBrowser for help on using the repository browser.