Changeset 364
- Timestamp:
- Oct 21, 2011, 10:30:44 PM (13 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Makefile.in
r353 r364 1779 1779 sh_nmail.o: $(srcsrc)/sh_nmail.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_pthread.h $(srcinc)/sh_mem.h $(srcinc)/sh_mail.h $(srcinc)/sh_tiger.h $(srcinc)/sh_string.h $(srcinc)/sh_utils.h $(srcinc)/sh_fifo.h $(srcinc)/sh_filter.h $(srcinc)/sh_mail_int.h $(srcinc)/zAVLTree.h 1780 1780 sh_filter.o: $(srcsrc)/sh_filter.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_utils.h $(srcinc)/sh_mem.h $(srcinc)/sh_filter.h 1781 sh_inotify.o: $(srcsrc)/sh_inotify.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_pthread.h $(srcinc)/sh_calls.h $(srcinc)/sh_inotify.h $(srcinc)/sh_mem.h $(srcinc)/s lib.h $(srcinc)/sh_calls.h $(srcinc)/sh_inotify.h1781 sh_inotify.o: $(srcsrc)/sh_inotify.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_pthread.h $(srcinc)/sh_calls.h $(srcinc)/sh_inotify.h $(srcinc)/sh_mem.h $(srcinc)/sh_utils.h $(srcinc)/slib.h $(srcinc)/zAVLTree.h $(srcinc)/sh_calls.h $(srcinc)/sh_inotify.h 1782 1782 sh_log_correlate.o: $(srcsrc)/sh_log_correlate.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_pthread.h $(srcinc)/sh_utils.h $(srcinc)/sh_string.h $(srcinc)/sh_log_check.h $(srcinc)/sh_log_evalrule.h 1783 1783 sh_log_mark.o: $(srcsrc)/sh_log_mark.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_pthread.h $(srcinc)/sh_mem.h $(srcinc)/sh_string.h $(srcinc)/sh_error_min.h $(srcinc)/sh_log_check.h $(srcinc)/sh_log_evalrule.h $(srcinc)/zAVLTree.h -
trunk/depend.dep
r335 r364 77 77 sh_nmail.o: $(srcsrc)/sh_nmail.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_pthread.h $(srcinc)/sh_mem.h $(srcinc)/sh_mail.h $(srcinc)/sh_tiger.h $(srcinc)/sh_string.h $(srcinc)/sh_utils.h $(srcinc)/sh_fifo.h $(srcinc)/sh_filter.h $(srcinc)/sh_mail_int.h $(srcinc)/zAVLTree.h 78 78 sh_filter.o: $(srcsrc)/sh_filter.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_utils.h $(srcinc)/sh_mem.h $(srcinc)/sh_filter.h 79 sh_inotify.o: $(srcsrc)/sh_inotify.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_pthread.h $(srcinc)/sh_calls.h $(srcinc)/sh_inotify.h $(srcinc)/sh_mem.h $(srcinc)/s lib.h $(srcinc)/sh_calls.h $(srcinc)/sh_inotify.h79 sh_inotify.o: $(srcsrc)/sh_inotify.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_pthread.h $(srcinc)/sh_calls.h $(srcinc)/sh_inotify.h $(srcinc)/sh_mem.h $(srcinc)/sh_utils.h $(srcinc)/slib.h $(srcinc)/zAVLTree.h $(srcinc)/sh_calls.h $(srcinc)/sh_inotify.h 80 80 sh_log_correlate.o: $(srcsrc)/sh_log_correlate.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_pthread.h $(srcinc)/sh_utils.h $(srcinc)/sh_string.h $(srcinc)/sh_log_check.h $(srcinc)/sh_log_evalrule.h 81 81 sh_log_mark.o: $(srcsrc)/sh_log_mark.c Makefile config_xor.h $(srcinc)/samhain.h $(srcinc)/sh_pthread.h $(srcinc)/sh_mem.h $(srcinc)/sh_string.h $(srcinc)/sh_error_min.h $(srcinc)/sh_log_check.h $(srcinc)/sh_log_evalrule.h $(srcinc)/zAVLTree.h -
trunk/depend.sum
r335 r364 1 443657224 1 2156182040 -
trunk/include/sh_inotify.h
r363 r364 6 6 typedef struct 7 7 { 8 void * list_of_watches; 9 void * dormant_watches; 10 8 11 /* 9 void * list_of_watches;10 */11 12 12 int watch[SH_INOTIFY_MAX]; 13 13 int flag[SH_INOTIFY_MAX]; 14 14 char * file[SH_INOTIFY_MAX]; 15 */ 15 16 16 17 int count; -
trunk/src/sh_inotify.c
r269 r364 39 39 #include "sh_inotify.h" 40 40 #include "sh_mem.h" 41 #include "sh_utils.h" 41 42 #include "slib.h" 42 43 … … 115 116 /*--- nothing thread-related below this point --- */ 116 117 118 #include "zAVLTree.h" 119 120 typedef struct 121 { 122 int watch; 123 int flag; 124 char * file; 125 } sh_watch; 117 126 118 127 /************************************************** … … 181 190 #define SH_INOTIFY_MODIFY 1 182 191 192 static void sh_inotify_init(sh_watches * watches) 193 { 194 watches->list_of_watches = NULL; 195 watches->count = 0; 196 watches->max_count = 0; 197 return; 198 } 199 200 static void sh_inotify_free_watch(void * item) 201 { 202 sh_watch * this = (sh_watch *) item; 203 204 if (this->file) 205 SH_FREE(this->file); 206 SH_FREE(this); 207 return; 208 } 209 210 static sh_watch * sh_inotify_create_watch(char * file, int nwatch, int flag) 211 { 212 sh_watch * this = SH_ALLOC(sizeof(sh_watch)); 213 214 this->file = sh_util_strdup(file); 215 this->watch = nwatch; 216 this->flag = flag; 217 return this; 218 } 219 220 /********** List Handling ******************/ 221 222 struct sh_inotify_litem 223 { 224 sh_watch * watch; 225 struct sh_inotify_litem * next; 226 }; 227 228 typedef struct { 229 struct sh_inotify_litem *prenode; 230 struct sh_inotify_litem *curnode; 231 } sh_inotify_listCursor; 232 233 static sh_watch * sh_inotify_list_first(sh_inotify_listCursor * listcursor, sh_watches * watches) 234 { 235 listcursor->prenode = watches->dormant_watches; 236 listcursor->curnode = watches->dormant_watches; 237 238 return listcursor->curnode->watch; 239 } 240 241 static sh_watch * sh_inotify_list_next(sh_inotify_listCursor * listcursor, sh_watches * watches) 242 { 243 (void) watches; 244 245 listcursor->prenode = listcursor->curnode; 246 listcursor->curnode = listcursor->curnode->next; 247 248 if (listcursor->curnode) 249 return listcursor->curnode->watch; 250 return NULL; 251 } 252 253 static void sh_inotify_listitem_destroy(struct sh_inotify_litem * this) 254 { 255 SH_FREE(this); 256 return; 257 } 258 259 static sh_watch * sh_inotify_list_del_current(sh_inotify_listCursor * listcursor, sh_watches * watches) 260 { 261 sh_watch * ret = NULL; 262 263 if (listcursor->curnode) 264 { 265 struct sh_inotify_litem * this = listcursor->curnode; 266 267 if (listcursor->prenode == this) 268 { 269 watches->dormant_watches = this->next; 270 271 listcursor->prenode = watches->dormant_watches; 272 listcursor->curnode = watches->dormant_watches; 273 } 274 else 275 { 276 listcursor->prenode->next = this->next; 277 listcursor->curnode = this->next; 278 } 279 ret = listcursor->curnode->watch; 280 sh_inotify_listitem_destroy(this); 281 } 282 return ret; 283 } 284 285 static int sh_inotify_add_dormant(sh_watches * watches, sh_watch * item) 286 { 287 struct sh_inotify_litem * this = SH_ALLOC(sizeof(struct sh_inotify_litem)); 288 289 this->watch = item; 290 this->next = (struct sh_inotify_litem *) watches->dormant_watches; 291 292 watches->dormant_watches = this; 293 return 0; 294 } 295 296 /********** End List Handling **************/ 297 298 static zAVLKey sh_inotify_getkey(void const *item) 299 { 300 return (&((sh_watch *)item)->watch); 301 } 302 303 304 /* This function removes all watches from the list, 305 * and closes the inode file descriptor in this thread. 306 */ 183 307 void sh_inotify_remove(sh_watches * watches) 184 308 { 185 int i;186 309 int ifd = sh_inotify_getfd(); 187 188 for (i = 0; i < watches->count; ++i) 189 { 190 if (watches->file[i]) 191 { 192 SH_FREE (watches->file[i]); 193 watches->file[i] = 0; 194 } 195 watches->watch[i] = 0; 196 watches->flag[i] = 0; 197 } 198 watches->count = 0; 310 zAVLTree * all_watches = (zAVLTree *)(watches->list_of_watches); 311 312 if (all_watches) 313 zAVLFreeTree(all_watches, sh_inotify_free_watch); 314 315 sh_inotify_init(watches); 316 199 317 if (ifd >= 0) 200 318 close(ifd); 201 319 sh_set_inotify_fd(-1); 202 320 321 watches->count = 0; 322 203 323 return; 204 324 } … … 206 326 static int index_watched_file(char * filename, sh_watches * watches) 207 327 { 208 int i; 209 210 for (i = 0; i < watches->count; ++i) 211 { 212 if (0 == strcmp(filename, watches->file[i])) 213 return i; 328 sh_watch * item; 329 zAVLCursor avlcursor; 330 zAVLTree * all_watches = (zAVLTree *)(watches->list_of_watches); 331 332 if (all_watches) 333 { 334 for (item = (sh_watch *) zAVLFirst(&avlcursor, all_watches); item; 335 item = (sh_watch *) zAVLNext(&avlcursor)) 336 { 337 if (item->file) 338 { 339 if (0 == strcmp(filename, item->file)) 340 return item->watch; 341 } 342 } 214 343 } 215 344 return -1; … … 220 349 int sh_inotify_add_watch(char * filename, sh_watches * watches, int * errnum) 221 350 { 222 size_t len;223 351 *errnum = 0; 224 352 … … 226 354 { 227 355 int nwatch; 356 sh_watch * item; 228 357 int index = index_watched_file(filename, watches); 229 358 … … 231 360 { 232 361 int ifd = sh_inotify_getfd(); 362 363 /************************************* 233 364 234 365 if (watches->count == SH_INOTIFY_MAX) … … 241 372 return -1; 242 373 } 374 **************************************/ 243 375 244 376 nwatch = inotify_add_watch (ifd, filename, … … 249 381 return -1; 250 382 } 383 384 item = sh_inotify_create_watch(filename, nwatch, /* flag */ 0); 251 385 252 watches->watch[watches->count] = nwatch; 253 watches->flag[watches->count] = 0; 254 255 len = strlen(filename) + 1; 256 watches->file[watches->count] = SH_ALLOC(len); 257 sl_strlcpy(watches->file[watches->count], filename, len); 386 if (NULL == watches->list_of_watches) 387 watches->list_of_watches = zAVLAllocTree (sh_inotify_getkey, zAVL_KEY_INT); 388 389 if (watches->list_of_watches) 390 { 391 *errnum = zAVLInsert((zAVLTree *)(watches->list_of_watches), item); 392 if (*errnum != 0) 393 { 394 sh_inotify_free_watch(item); 395 return -1; 396 } 397 } 398 else 399 { 400 *errnum = -1; 401 return -1; 402 } 258 403 259 404 ++(watches->count); … … 266 411 int * errnum, int waitsec) 267 412 { 268 int ifd = sh_inotify_getfd(); 413 sh_watch * item; 414 zAVLTree * all_watches = (zAVLTree *)(watches->list_of_watches); 415 int ifd = sh_inotify_getfd(); 269 416 270 417 *errnum = 0; … … 279 426 char buffer[1024]; 280 427 428 sh_inotify_listCursor listcursor; 429 281 430 /* -- Add watch if required 282 431 */ … … 290 439 } 291 440 292 for (i = 0; i < watches->count; ++i) 293 { 294 if (watches->watch[i] == -1) 295 watches->watch[i] = inotify_add_watch (ifd, watches->file[i], 296 IN_MODIFY|IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT); 441 /* -- Check dormant watches for reopening. 442 */ 443 for (item = sh_inotify_list_first(&listcursor, watches); item; 444 item = sh_inotify_list_next(&listcursor, watches)) 445 { 446 have_next: 447 if (item->file && item->watch == -1) 448 { 449 item->watch = inotify_add_watch (ifd, item->file, 450 IN_MODIFY|IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT); 451 if (item->watch >= 0) 452 { 453 zAVLInsert(all_watches, item); 454 item = sh_inotify_list_del_current(&listcursor, watches); 455 goto have_next; 456 } 457 } 297 458 } 298 459 … … 306 467 if (len > 0) 307 468 { 308 int j;309 469 struct inotify_event *event; 310 470 … … 315 475 event = (struct inotify_event *) &buffer[i]; 316 476 317 for (j = 0; j < watches->count; ++j) 477 item = zAVLSearch(all_watches, &(event->wd)); 478 479 if (item) 318 480 { 319 if ( watches->watch[j] == event->wd)481 if (event->mask & IN_MODIFY) 320 482 { 321 if (event->mask & IN_MODIFY) 322 { 323 watches->flag[j] |= SH_INOTIFY_MODIFY; 324 flag |= SH_INOTIFY_MODIFY; 325 } 326 else if (event->mask & IN_DELETE_SELF || 327 event->mask & IN_UNMOUNT || 328 event->mask & IN_MOVE_SELF ) 329 { 330 watches->flag[j] |= SH_INOTIFY_REOPEN; 331 (void) inotify_rm_watch(ifd, watches->watch[j]); 332 watches->watch[j] = -1; 333 flag |= SH_INOTIFY_REOPEN; 334 } 483 item->flag |= SH_INOTIFY_MODIFY; 484 flag |= SH_INOTIFY_MODIFY; 485 } 486 else if (event->mask & IN_DELETE_SELF || 487 event->mask & IN_UNMOUNT || 488 event->mask & IN_MOVE_SELF ) 489 { 490 item->flag |= SH_INOTIFY_REOPEN; 491 (void) inotify_rm_watch(ifd, item->watch); 492 zAVLDelete(all_watches, item); 493 sh_inotify_add_dormant(watches, item); 494 item->watch = -1; 495 flag |= SH_INOTIFY_REOPEN; 335 496 } 336 497 } 498 337 499 i += sizeof (struct inotify_event) + event->len; 338 500 }
Note:
See TracChangeset
for help on using the changeset viewer.