source: trunk/src/sh_inotify.c@ 427

Last change on this file since 427 was 405, checked in by katerina, 12 years ago

Fix for tickets #303, #304, #305. #306, and #307. Update version number.

File size: 23.4 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
[382]248static sh_watch * sh_inotify_create_watch(const char * file,
249 int nwatch, int flag)
[364]250{
251 sh_watch * this = SH_ALLOC(sizeof(sh_watch));
252
[382]253 this->file = sh_util_strdup_track(file, __FILE__, __LINE__);
[364]254 this->watch = nwatch;
255 this->flag = flag;
256 return this;
257}
258
259/********** List Handling ******************/
260
261struct sh_inotify_litem
262{
263 sh_watch * watch;
264 struct sh_inotify_litem * next;
265};
266
[367]267static void sh_inotify_listitem_destroy(struct sh_inotify_litem * this)
268{
269 if (this)
270 SH_FREE(this);
271 return;
272}
273
274/* No Mutex in the list cursor functions, must be in the caller
275 * function...
276 */
[364]277typedef struct {
278 struct sh_inotify_litem *prenode;
279 struct sh_inotify_litem *curnode;
280} sh_inotify_listCursor;
281
[367]282static sh_watch * sh_inotify_list_first(sh_inotify_listCursor * listcursor,
283 sh_watches * watches)
[364]284{
285 listcursor->prenode = watches->dormant_watches;
286 listcursor->curnode = watches->dormant_watches;
287
[367]288 if (listcursor->curnode)
289 return listcursor->curnode->watch;
290 return NULL;
[364]291}
292
[367]293static sh_watch * sh_inotify_list_next(sh_inotify_listCursor * listcursor,
294 sh_watches * watches)
[364]295{
296 (void) watches;
297
298 listcursor->prenode = listcursor->curnode;
299
300 if (listcursor->curnode)
[367]301 {
302 listcursor->curnode = listcursor->curnode->next;
[371]303 if (listcursor->curnode)
304 return listcursor->curnode->watch;
305 else
306 return NULL;
[367]307 }
308
[364]309 return NULL;
310}
311
[367]312static sh_watch * sh_inotify_list_del_cur(sh_inotify_listCursor * listcursor,
313 sh_watches * watches)
[364]314{
315 sh_watch * ret = NULL;
316
317 if (listcursor->curnode)
[261]318 {
[364]319 struct sh_inotify_litem * this = listcursor->curnode;
320
321 if (listcursor->prenode == this)
[261]322 {
[364]323 watches->dormant_watches = this->next;
324
325 listcursor->prenode = watches->dormant_watches;
326 listcursor->curnode = watches->dormant_watches;
[261]327 }
[364]328 else
329 {
330 listcursor->prenode->next = this->next;
331 listcursor->curnode = this->next;
332 }
[367]333 if (listcursor->curnode)
334 ret = listcursor->curnode->watch;
335 else
336 ret = NULL;
[364]337 sh_inotify_listitem_destroy(this);
[261]338 }
[364]339 return ret;
340}
341
342static int sh_inotify_add_dormant(sh_watches * watches, sh_watch * item)
343{
[367]344 struct sh_inotify_litem * this;
[364]345
[367]346 SH_MUTEX_LOCK(mutex_list_dormant);
347 this = SH_ALLOC(sizeof(struct sh_inotify_litem));
348
[364]349 this->watch = item;
350 this->next = (struct sh_inotify_litem *) watches->dormant_watches;
351
352 watches->dormant_watches = this;
[367]353 SH_MUTEX_UNLOCK(mutex_list_dormant);
[364]354 return 0;
355}
356
[367]357static void * sh_dummy_popret = NULL;
358
359char * sh_inotify_pop_dormant(sh_watches * watches,
[373]360 int * class, unsigned long * check_mask,
361 int * type, int * rdepth)
[367]362{
363 char * popret = NULL;
364 struct sh_inotify_litem * this;
365
366 /* Take the address to keep gcc from putting it into a register.
367 * Avoids the 'clobbered by longjmp' warning.
368 */
369 sh_dummy_popret = (void *) &popret;
370
371 SH_MUTEX_LOCK(mutex_list_dormant);
372
373 this = (struct sh_inotify_litem *) watches->dormant_watches;
374
375 if (this)
376 {
[373]377 *class = this->watch->class;
378 *type = this->watch->type;
379 *rdepth = this->watch->rdepth;
[367]380 *check_mask = this->watch->check_mask;
[382]381 popret = sh_util_strdup_track(this->watch->file, __FILE__, __LINE__);
[367]382
383 watches->dormant_watches = this->next;
384
385 sh_inotify_free_watch(this->watch);
386 SH_FREE(this);
387 }
388 SH_MUTEX_UNLOCK(mutex_list_dormant);
389
[383]390 sh_dummy_popret = NULL;
[367]391 return popret;
392}
393
394void sh_inotify_purge_dormant(sh_watches * watches)
395{
396 struct sh_inotify_litem * this;
397
398 SH_MUTEX_LOCK(mutex_list_dormant);
399 this = (struct sh_inotify_litem *) watches->dormant_watches;
400
401 watches->dormant_watches = NULL;
402
403 while (this)
404 {
405 struct sh_inotify_litem * cur = this;
406
407 this = this->next;
408
409 sh_inotify_free_watch(cur->watch);
410 SH_FREE(cur);
411 }
412 SH_MUTEX_UNLOCK(mutex_list_dormant);
413 return;
414}
415
[364]416/********** End List Handling **************/
417
418static zAVLKey sh_inotify_getkey(void const *item)
419{
420 return (&((sh_watch *)item)->watch);
421}
422
423
424/* This function removes all watches from the list,
425 * and closes the inode file descriptor in this thread.
426 */
427void sh_inotify_remove(sh_watches * watches)
428{
429 int ifd = sh_inotify_getfd();
[367]430 zAVLTree * all_watches;
[364]431
[367]432 SH_MUTEX_LOCK(mutex_watches);
433 all_watches = (zAVLTree *)(watches->list_of_watches);
434
[364]435 if (all_watches)
436 zAVLFreeTree(all_watches, sh_inotify_free_watch);
437
[367]438 watches->list_of_watches = NULL;
439 watches->count = 0;
440 SH_MUTEX_UNLOCK(mutex_watches);
[364]441
[261]442 if (ifd >= 0)
443 close(ifd);
444 sh_set_inotify_fd(-1);
445
446 return;
447}
448
449static int index_watched_file(char * filename, sh_watches * watches)
450{
[364]451 sh_watch * item;
452 zAVLCursor avlcursor;
453 zAVLTree * all_watches = (zAVLTree *)(watches->list_of_watches);
[261]454
[364]455 if (all_watches)
[261]456 {
[364]457 for (item = (sh_watch *) zAVLFirst(&avlcursor, all_watches); item;
458 item = (sh_watch *) zAVLNext(&avlcursor))
459 {
460 if (item->file)
461 {
462 if (0 == strcmp(filename, item->file))
463 return item->watch;
464 }
465 }
[261]466 }
467 return -1;
468}
469
[405]470#if !defined(IN_DONT_FOLLOW)
471#define IN_DONT_FOLLOW 0
472#endif
473
[367]474#define SH_INOTIFY_FILEFLAGS \
[405]475 (IN_ATTRIB|IN_MODIFY|IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|IN_DONT_FOLLOW)
[367]476#define SH_INOTIFY_DIRFLAGS \
477 (SH_INOTIFY_FILEFLAGS|IN_DELETE|IN_CREATE|IN_MOVED_FROM|IN_MOVED_TO)
478
479#define SH_INOTIFY_FLAGS (SH_INOTIFY_FILEFLAGS|SH_INOTIFY_DIRFLAGS)
480
[405]481
[367]482/* Create an item and put it on the 'dormant' list for later watch creation
483 */
[373]484int sh_inotify_add_watch_later(const char * filename, sh_watches * watches,
[367]485 int * errnum,
[382]486 int class, unsigned long check_mask, int type,
487 int rdepth)
[367]488{
489 sh_watch * item;
490
491 item = sh_inotify_create_watch(filename, -1, /* flag */ 0);
492
493 item->class = class;
[372]494 item->type = (short) type;
[373]495 item->rdepth = (short) rdepth;
[367]496 item->check_mask = check_mask;
497
498 sh_inotify_add_dormant(watches, item);
499 if (errnum)
500 *errnum = 0;
501
502 return 0;
503}
504
505int sh_inotify_rm_watch (sh_watches * watches, sh_watches * save, int wd)
506{
507 int ifd = sh_get_inotify_fd();
508
509 if (watches)
510 {
511 sh_watch * item;
512
513 SH_MUTEX_LOCK(mutex_watches);
514 item = zAVLSearch(watches->list_of_watches, &wd);
515
516 if (item)
517 {
518 zAVLDelete(watches->list_of_watches, &wd);
519 if (save) /* optionally save the item */
520 {
521 item->watch = -1;
522 sh_inotify_add_dormant(save, item);
523 }
524 else
525 {
526 sh_inotify_free_watch(item);
527 }
528 }
529 SH_MUTEX_UNLOCK(mutex_watches);
530 }
531 return inotify_rm_watch(ifd, wd);
532}
533
534#if (defined(SH_WITH_CLIENT) || defined(SH_STANDALONE))
535static void * sh_dummy_litem;
536
537int sh_inotify_recheck_watches (sh_watches * watches, sh_watches * save)
538{
539 sh_watch * litem;
540 sh_inotify_listCursor listcursor;
541 int ifd = sh_get_inotify_fd();
542
[371]543 extern void sh_fInotify_report_add(char * path,
544 int class, unsigned long check_mask);
[367]545
546 sh_dummy_litem = (void*) &litem;
547
548 /* -- Check dormant watches for reopening.
549 */
550 SH_MUTEX_LOCK(mutex_list_dormant);
551
[371]552 litem = sh_inotify_list_first(&listcursor, save);
553
554 while (litem)
[367]555 {
556 have_next:
557
558 /* sh_inotify_list_del_cur may return NULL */
559 if (litem && litem->file && litem->watch == -1)
560 {
561 litem->watch = inotify_add_watch (ifd, litem->file,
562 SH_INOTIFY_FLAGS);
563
564 if (litem->watch >= 0)
565 {
566 SH_MUTEX_LOCK(mutex_watches);
567 if (watches->list_of_watches)
568 zAVLInsert(watches->list_of_watches, litem);
569 SH_MUTEX_UNLOCK(mutex_watches);
570
571 sh_fInotify_report_add(litem->file, litem->class, litem->check_mask);
572
573 litem = sh_inotify_list_del_cur(&listcursor, save);
574
575 goto have_next;
576 }
577 }
[371]578 litem = sh_inotify_list_next(&listcursor, save);
[367]579 }
580 SH_MUTEX_UNLOCK(mutex_list_dormant);
581 return 0;
582}
583#endif
584
[261]585/* This function is idempotent; it will add the watch only once
586 */
[367]587int sh_inotify_add_watch(char * filename, sh_watches * watches, int * errnum,
[373]588 int class, unsigned long check_mask, int type, int rdepth)
[261]589{
[367]590 volatile int retval = 0;
591
592 SH_MUTEX_LOCK(mutex_watches);
593
[261]594 *errnum = 0;
595
596 if (filename)
597 {
598 int nwatch;
[364]599 sh_watch * item;
[261]600 int index = index_watched_file(filename, watches);
601
602 if (index < 0)
603 {
604 int ifd = sh_inotify_getfd();
605
[364]606 /*************************************
607
[261]608 if (watches->count == SH_INOTIFY_MAX)
609 {
610#ifdef EMFILE
611 *errnum = EMFILE;
612#else
613 *errnum = 24;
614#endif
615 return -1;
616 }
[364]617 **************************************/
[261]618
619 nwatch = inotify_add_watch (ifd, filename,
[367]620 SH_INOTIFY_FLAGS);
[261]621 if (nwatch < 0)
622 {
623 *errnum = errno;
[367]624 retval = -1;
625 goto retpoint;
[261]626 }
[364]627
628 item = sh_inotify_create_watch(filename, nwatch, /* flag */ 0);
[367]629
630 item->class = class;
[372]631 item->type = type;
[373]632 item->rdepth = rdepth;
[367]633 item->check_mask = check_mask;
[261]634
[364]635 if (NULL == watches->list_of_watches)
[367]636 watches->list_of_watches = zAVLAllocTree (sh_inotify_getkey,
637 zAVL_KEY_INT);
[364]638
639 if (watches->list_of_watches)
640 {
[367]641 *errnum = zAVLInsert((zAVLTree *)(watches->list_of_watches),
642 item);
643
[364]644 if (*errnum != 0)
645 {
[405]646 /* zAVLInsert returns -1 on malloc() error and 3 if
647 * the node already exists.
648 */
649 *errnum = (*errnum == -1) ? ENOMEM : EEXIST;
[364]650 sh_inotify_free_watch(item);
[367]651 retval = -1;
652 goto retpoint;
[364]653 }
654 }
655 else
656 {
[367]657 *errnum = ENOMEM;
[382]658 sh_inotify_free_watch(item);
[367]659 retval = -1;
660 goto retpoint;
[364]661 }
[261]662
663 ++(watches->count);
664 }
[373]665 else if (type == SH_INOTIFY_DIR) /* watch exists */
666 {
667 /* This covers the case that a directory has been added,
668 * but is watched as file at first because it is also
669 * specified as file in the config.
670 */
671 item = zAVLSearch(watches->list_of_watches, &index);
672
673 if (item && item->type == SH_INOTIFY_FILE)
674 {
675 item->type = SH_INOTIFY_DIR;
676 }
677 }
[261]678 }
[367]679 retpoint:
[377]680 ; /* 'label at end of compound statement' */
[367]681 SH_MUTEX_UNLOCK(mutex_watches);
682 return retval;
[261]683}
684
[367]685static void * sh_dummy_sret = NULL;
686
687char * sh_inotify_search_item(sh_watches * watches, int watch,
[373]688 int * class, unsigned long * check_mask,
689 int * type, int * rdepth)
[367]690{
691 sh_watch * item;
692 char * sret = NULL;
693
694 /* Take the address to keep gcc from putting it into a register.
695 * Avoids the 'clobbered by longjmp' warning.
696 */
697 sh_dummy_sret = (void *) &sret;
698
699 SH_MUTEX_LOCK(mutex_watches);
700 item = zAVLSearch(watches->list_of_watches, &watch);
701
702 if (item)
703 {
704 *class = item->class;
705 *check_mask = item->check_mask;
[372]706 *type = item->type;
[373]707 *rdepth = item->rdepth;
[382]708 sret = sh_util_strdup_track(item->file, __FILE__, __LINE__);
[367]709 }
710 SH_MUTEX_UNLOCK(mutex_watches);
711 return sret;
712}
713
714static void * sh_dummy_litem = NULL;
715
[261]716int sh_inotify_wait_for_change(char * filename, sh_watches * watches,
717 int * errnum, int waitsec)
718{
[367]719 sh_watch * litem;
720 sh_watch * zitem;
[364]721 int ifd = sh_inotify_getfd();
[367]722
723 /* Take the address to keep gcc from putting it into a register.
724 * Avoids the 'clobbered by longjmp' warning.
725 */
726 sh_dummy_litem = (void*) &litem;
727
[261]728 *errnum = 0;
729
730 start_it:
731
732 if (ifd >= 0)
733 {
[367]734 volatile ssize_t i = 0;
[261]735 ssize_t len = -1;
736 int flag = 0;
737 char buffer[1024];
738
[364]739 sh_inotify_listCursor listcursor;
740
[261]741 /* -- Add watch if required
742 */
743 if (filename)
744 {
[373]745 if (sh_inotify_add_watch(filename, watches, errnum,
746 0, 0, SH_INOTIFY_FILE, 0) < 0)
[261]747 {
748 retry_msleep(waitsec, 0);
749 return -1;
750 }
751 }
752
[364]753 /* -- Check dormant watches for reopening.
754 */
[367]755 SH_MUTEX_LOCK(mutex_list_dormant);
756
757 for (litem = sh_inotify_list_first(&listcursor, watches); litem;
758 litem = sh_inotify_list_next(&listcursor, watches))
[261]759 {
[364]760 have_next:
[367]761 /* sh_inotify_list_del_cur may return NULL */
762 if (litem && litem->file && litem->watch == -1)
[364]763 {
[367]764 litem->watch = inotify_add_watch (ifd, litem->file,
765 SH_INOTIFY_FLAGS);
766
767 if (litem->watch >= 0)
[364]768 {
[367]769 SH_MUTEX_LOCK(mutex_watches);
770 if (watches->list_of_watches)
771 zAVLInsert(watches->list_of_watches, litem);
772 SH_MUTEX_UNLOCK(mutex_watches);
773 litem = sh_inotify_list_del_cur(&listcursor, watches);
[364]774 goto have_next;
775 }
776 }
[261]777 }
[367]778 SH_MUTEX_UNLOCK(mutex_list_dormant);
[261]779
780
781 /* -- Blocking read on inotify file descriptor
782 */
[371]783 len = sh_inotify_read(buffer, sizeof(buffer));
[261]784
785 if (len > 0)
786 {
787 struct inotify_event *event;
788
789 i = 0;
790
791 while (i < len) {
792
793 event = (struct inotify_event *) &buffer[i];
794
[367]795 SH_MUTEX_LOCK(mutex_watches);
796 zitem = zAVLSearch(watches->list_of_watches, &(event->wd));
[364]797
[367]798 if (zitem)
[261]799 {
[364]800 if (event->mask & IN_MODIFY)
[261]801 {
[367]802 zitem->flag |= SH_INOTIFY_MODIFY;
[364]803 flag |= SH_INOTIFY_MODIFY;
[261]804 }
[364]805 else if (event->mask & IN_DELETE_SELF ||
806 event->mask & IN_UNMOUNT ||
807 event->mask & IN_MOVE_SELF )
808 {
[367]809 zitem->flag |= SH_INOTIFY_REOPEN;
810 (void) inotify_rm_watch(ifd, zitem->watch);
811 zAVLDelete(watches->list_of_watches, zitem);
812 sh_inotify_add_dormant(watches, zitem);
813 zitem->watch = -1;
[364]814 flag |= SH_INOTIFY_REOPEN;
815 }
[261]816 }
[367]817 SH_MUTEX_UNLOCK(mutex_watches);
[364]818
[261]819 i += sizeof (struct inotify_event) + event->len;
820 }
821 }
822 else if (len == -1)
823 {
824 *errnum = errno;
825 retry_msleep(waitsec, 0);
826
827 return -1;
828 }
829
830 if (flag & SH_INOTIFY_REOPEN)
831 {
832 if (flag & SH_INOTIFY_MODIFY)
833 return 0;
834 else
835 goto start_it;
836 }
837
838 return 0;
839 }
840
841 /* Inotify not working, sleep
842 */
843 retry_msleep(waitsec, 0);
844
845 *errnum = 0;
846 return -1;
847}
848
[367]849
[261]850/* !defined(HAVE_SYS_INOTIFY_H) */
851#else
852
853#include "sh_calls.h"
[269]854#include "sh_inotify.h"
[261]855
856void sh_inotify_remove(sh_watches * watches)
857{
858 (void) watches;
859 return;
860}
861
862int sh_inotify_wait_for_change(char * filename, sh_watches * watches,
863 int * errnum, int waitsec)
864{
865 (void) filename;
866 (void) watches;
867
868 /* Inotify not working, sleep for waitsec seconds
869 */
870 retry_msleep(waitsec, 0);
871
[388]872 if (errnum)
873 *errnum = 0;
[261]874 return -1;
875}
876
[367]877int sh_inotify_add_watch(char * filename, sh_watches * watches, int * errnum,
[373]878 int class, unsigned long check_mask, int type, int rdepth)
[261]879{
880 (void) filename;
881 (void) watches;
[367]882 (void) class;
883 (void) check_mask;
[373]884 (void) type;
885 (void) rdepth;
[388]886
887 if (errnum)
888 *errnum = 0;
[261]889 return 0;
890}
891
[373]892int sh_inotify_add_watch_later(const char * filename, sh_watches * watches,
[367]893 int * errnum,
[373]894 int class, unsigned long check_mask, int type, int rdepth)
[367]895{
896 (void) filename;
897 (void) watches;
898 (void) class;
899 (void) check_mask;
[373]900 (void) type;
901 (void) rdepth;
[388]902
903 if (errnum)
904 *errnum = 0;
[367]905 return 0;
906}
907
[261]908#endif
[367]909
910#ifdef SH_CUTEST
911#include "CuTest.h"
912void Test_inotify(CuTest *tc) {
913#if defined(HAVE_SYS_INOTIFY_H) && (defined(SH_WITH_CLIENT) || defined(SH_STANDALONE))
914
915 int ret;
916 sh_watches twatch = SH_INOTIFY_INITIALIZER;
917 sh_watch * litem;
918 sh_inotify_listCursor listcursor;
919 char * p;
920 int class;
[373]921 int type;
922 int rdepth;
[367]923 unsigned long check_mask;
924 int nrun = 0;
925
[373]926 sh_watch aw1 = { -1, 0, 0, 1, 99, 1, "a1" };
927 sh_watch aw2 = { -1, 0, 0, 2, 99, 1, "a2" };
928 sh_watch aw3 = { 2, 0, 0, 3, 99, 1, "a3" };
929 sh_watch aw4 = { -1, 0, 0, 4, 99, 1, "a4" };
930 sh_watch aw5 = { 5, 0, 0, 5, 99, 1, "a5" };
[367]931
932 do {
933
934 int count = 0;
935
936 sh_watch * w1 = SH_ALLOC(sizeof(sh_watch));
937 sh_watch * w2 = SH_ALLOC(sizeof(sh_watch));
938 sh_watch * w3 = SH_ALLOC(sizeof(sh_watch));
939 sh_watch * w4 = SH_ALLOC(sizeof(sh_watch));
940 sh_watch * w5 = SH_ALLOC(sizeof(sh_watch));
941
942 memcpy(w1, &aw1, sizeof(sh_watch));
943 w1->file = sh_util_strdup(aw1.file);
944 memcpy(w2, &aw2, sizeof(sh_watch));
945 w2->file = sh_util_strdup(aw2.file);
946 memcpy(w3, &aw3, sizeof(sh_watch));
947 w3->file = sh_util_strdup(aw3.file);
948 memcpy(w4, &aw4, sizeof(sh_watch));
949 w4->file = sh_util_strdup(aw4.file);
950 memcpy(w5, &aw5, sizeof(sh_watch));
951 w5->file = sh_util_strdup(aw5.file);
952
953 ret = sh_inotify_add_dormant(&twatch, w1);
954 CuAssertIntEquals(tc, ret, 0);
955 ret = sh_inotify_add_dormant(&twatch, w2);
956 CuAssertIntEquals(tc, ret, 0);
957 ret = sh_inotify_add_dormant(&twatch, w3);
958 CuAssertIntEquals(tc, ret, 0);
959 ret = sh_inotify_add_dormant(&twatch, w4);
960 CuAssertIntEquals(tc, ret, 0);
961 ret = sh_inotify_add_dormant(&twatch, w5);
962 CuAssertIntEquals(tc, ret, 0);
963
964 /* -- Check dormant watches for reopening.
965 */
966 for (litem = sh_inotify_list_first(&listcursor, &twatch); litem;
967 litem = sh_inotify_list_next(&listcursor, &twatch))
968 {
969 have_next:
970
971 /* sh_inotify_list_del_cur may return NULL */
972 if (litem)
973 {
974 ++count;
975
976 if (litem->file && litem->watch == -1)
977 {
978
979 switch (litem->class)
980 {
981 case 1:
982 CuAssertStrEquals(tc, litem->file, "a1");
983 break;
984 case 2:
985 CuAssertStrEquals(tc, litem->file, "a2");
986 break;
987 case 3:
988 CuAssertStrEquals(tc, litem->file, "deadbeef");
989 break;
990 case 4:
991 CuAssertStrEquals(tc, litem->file, "a4");
992 break;
993 case 5:
994 CuAssertStrEquals(tc, litem->file, "deadbeef");
995 break;
996 default:
997 CuAssertStrEquals(tc, litem->file, "deadbeef");
998 }
999 litem = sh_inotify_list_del_cur(&listcursor, &twatch);
1000 goto have_next;
1001 }
1002 switch (litem->class)
1003 {
1004 case 3:
1005 CuAssertStrEquals(tc, litem->file, "a3");
1006 break;
1007 case 5:
1008 CuAssertStrEquals(tc, litem->file, "a5");
1009 break;
1010 default:
1011 CuAssertStrEquals(tc, litem->file, "foobar");
1012 }
1013 }
1014 }
1015
1016 CuAssertIntEquals(tc, count, 5);
1017
[373]1018 p = sh_inotify_pop_dormant(&twatch, &class, &check_mask, &type, &rdepth);
[367]1019 CuAssertStrEquals(tc, p, "a5");
1020
[373]1021 p = sh_inotify_pop_dormant(&twatch, &class, &check_mask, &type, &rdepth);
[367]1022 CuAssertStrEquals(tc, p, "a3");
1023 CuAssertIntEquals(tc, class, 3);
1024
[373]1025 p = sh_inotify_pop_dormant(&twatch, &class, &check_mask, &type, &rdepth);
[367]1026 CuAssertTrue(tc, NULL == p);
1027 CuAssertTrue(tc, NULL == twatch.dormant_watches);
1028
1029 ++nrun;
1030
1031 } while (nrun < 100);
1032
1033#else
1034 (void) tc;
1035#endif
1036
1037 return;
1038}
1039#endif
Note: See TracBrowser for help on using the repository browser.