15 #include "../../config.h"
17 #include "inotifytools_p.h"
25 #include <sys/select.h>
26 #include <sys/types.h>
28 #include <sys/ioctl.h>
35 #include "inotifytools/inotify.h"
123 #define MAX_EVENTS 4096
124 #define MAX_STRLEN 4096
125 #define INOTIFY_PROCDIR "/proc/sys/fs/inotify/"
126 #define WATCHES_SIZE_PATH INOTIFY_PROCDIR "max_user_watches"
127 #define QUEUE_SIZE_PATH INOTIFY_PROCDIR "max_queued_watches"
128 #define INSTANCES_PATH INOTIFY_PROCDIR "max_user_instances"
130 static int inotify_fd;
131 static unsigned num_access;
132 static unsigned num_modify;
133 static unsigned num_attrib;
134 static unsigned num_close_nowrite;
135 static unsigned num_close_write;
136 static unsigned num_open;
137 static unsigned num_move_self;
138 static unsigned num_moved_to;
139 static unsigned num_moved_from;
140 static unsigned num_create;
141 static unsigned num_delete;
142 static unsigned num_delete_self;
143 static unsigned num_unmount;
144 static unsigned num_total;
145 static int collect_stats = 0;
147 struct rbtree *tree_wd = 0;
148 struct rbtree *tree_filename = 0;
149 static int error = 0;
151 static char* timefmt = 0;
152 static regex_t* regex = 0;
154 static int invert_regexp = 0;
156 static int isdir(
char const * path );
157 void record_stats(
struct inotify_event
const * event );
158 int onestr_to_event(
char const * event);
177 #define niceassert(cond,mesg) _niceassert((long)cond, __LINE__, __FILE__, \
180 #define nasprintf(...) niceassert( -1 != asprintf(__VA_ARGS__), "out of memory")
199 static void _niceassert(
long cond,
int line,
char const * file,
200 char const * condstr,
char const * mesg ) {
204 fprintf(stderr,
"%s:%d assertion ( %s ) failed: %s\n", file, line,
208 fprintf(stderr,
"%s:%d assertion ( %s ) failed.\n", file, line, condstr);
221 char * chrtostr(
char ch) {
222 static char str[2] = {
'\0',
'\0' };
230 int read_num_from_file(
char * filename,
int * num ) {
231 FILE * file = fopen( filename,
"r" );
237 if ( EOF == fscanf( file,
"%d", num ) ) {
242 niceassert( 0 == fclose( file ), 0 );
247 int wd_compare(
const void *d1,
const void *d2,
const void *config) {
248 if (!d1 || !d2)
return d1 - d2;
249 return ((watch*)d1)->wd - ((watch*)d2)->wd;
252 int filename_compare(
const void *d1,
const void *d2,
const void *config) {
253 if (!d1 || !d2)
return d1 - d2;
254 return strcmp(((watch*)d1)->filename, ((watch*)d2)->filename);
260 watch *watch_from_wd(
int wd ) {
263 return (watch*)rbfind(&w, tree_wd);
269 watch *watch_from_filename(
char const *filename ) {
271 w.filename = (
char*)filename;
272 return (watch*)rbfind(&w, tree_filename);
289 inotify_fd = inotify_init();
290 if (inotify_fd < 0) {
297 tree_wd = rbinit(wd_compare, 0);
298 tree_filename = rbinit(filename_compare, 0);
307 void destroy_watch(watch *w) {
308 if (w->filename) free(w->filename);
315 void cleanup_tree(
const void *nodep,
317 const int depth,
void* arg) {
318 if (which != endorder && which != leaf)
return;
319 watch *w = (watch*)nodep;
344 rbwalk(tree_wd, cleanup_tree, 0);
345 rbdestroy(tree_wd); tree_wd = 0;
346 rbdestroy(tree_filename); tree_filename = 0;
352 void empty_stats(
const void *nodep,
354 const int depth,
void *arg) {
355 if (which != endorder && which != leaf)
return;
356 watch *w = (watch*)nodep;
360 w->hit_close_nowrite = 0;
361 w->hit_close_write = 0;
363 w->hit_move_self = 0;
364 w->hit_moved_from = 0;
368 w->hit_delete_self = 0;
376 struct replace_filename_data {
377 char const *old_name;
378 char const *new_name;
385 void replace_filename(
const void *nodep,
const VISIT which,
const int depth,
386 const struct replace_filename_data *data) {
387 if (which != endorder && which != leaf)
return;
388 watch *w = (watch*)nodep;
390 if ( 0 == strncmp( data->old_name, w->filename, data->old_len ) ) {
391 nasprintf( &name,
"%s%s", data->new_name, &(w->filename[data->old_len]) );
392 if (!strcmp( w->filename, data->new_name )) {
395 rbdelete(w, tree_filename);
398 rbsearch(w, tree_filename);
406 void get_num(
const void *nodep,
408 const int depth,
void *arg) {
409 if (which != endorder && which != leaf)
return;
427 niceassert( init,
"inotifytools_initialize not called yet" );
431 rbwalk(tree_wd, empty_stats, 0);
437 num_close_nowrite = 0;
480 if ( strchr(
"_" "abcdefghijklmnopqrstuvwxyz"
481 "ABCDEFGHIJKLMNOPQRSTUVWXYZ", sep ) ) {
486 char * event1, * event2;
490 if ( !event || !event[0] )
return 0;
492 event1 = (
char *)event;
493 event2 = strchr( event1, sep );
494 while ( event1 && event1[0] ) {
496 len = event2 - event1;
497 niceassert( len < 4096,
"malformed event string (very long)" );
500 len = strlen(event1);
502 if ( len > 4095 ) len = 4095;
505 strncpy(eventstr, event1, len);
507 strcpy(eventstr, event1);
512 ret1 = onestr_to_event( eventstr );
513 if ( 0 == ret1 || -1 == ret1 ) {
520 if ( event1 && event1[0] ) {
524 if ( !event1[0] )
return 0;
525 event2 = strchr( event1, sep );
570 int onestr_to_event(
char const * event)
575 if ( !event || !event[0] )
577 else if ( 0 == strcasecmp(event,
"ACCESS") )
579 else if ( 0 == strcasecmp(event,
"MODIFY") )
581 else if ( 0 == strcasecmp(event,
"ATTRIB") )
583 else if ( 0 == strcasecmp(event,
"CLOSE_WRITE") )
584 ret = IN_CLOSE_WRITE;
585 else if ( 0 == strcasecmp(event,
"CLOSE_NOWRITE") )
586 ret = IN_CLOSE_NOWRITE;
587 else if ( 0 == strcasecmp(event,
"OPEN") )
589 else if ( 0 == strcasecmp(event,
"MOVED_FROM") )
591 else if ( 0 == strcasecmp(event,
"MOVED_TO") )
593 else if ( 0 == strcasecmp(event,
"CREATE") )
595 else if ( 0 == strcasecmp(event,
"DELETE") )
597 else if ( 0 == strcasecmp(event,
"DELETE_SELF") )
598 ret = IN_DELETE_SELF;
599 else if ( 0 == strcasecmp(event,
"UNMOUNT") )
601 else if ( 0 == strcasecmp(event,
"Q_OVERFLOW") )
603 else if ( 0 == strcasecmp(event,
"IGNORED") )
605 else if ( 0 == strcasecmp(event,
"CLOSE") )
607 else if ( 0 == strcasecmp(event,
"MOVE_SELF") )
609 else if ( 0 == strcasecmp(event,
"MOVE") )
611 else if ( 0 == strcasecmp(event,
"ISDIR") )
613 else if ( 0 == strcasecmp(event,
"ONESHOT") )
615 else if ( 0 == strcasecmp(event,
"ALL_EVENTS") )
672 static char ret[1024];
676 if ( IN_ACCESS & events ) {
677 strcat( ret, chrtostr(sep) );
678 strcat( ret,
"ACCESS" );
680 if ( IN_MODIFY & events ) {
681 strcat( ret, chrtostr(sep) );
682 strcat( ret,
"MODIFY" );
684 if ( IN_ATTRIB & events ) {
685 strcat( ret, chrtostr(sep) );
686 strcat( ret,
"ATTRIB" );
688 if ( IN_CLOSE_WRITE & events ) {
689 strcat( ret, chrtostr(sep) );
690 strcat( ret,
"CLOSE_WRITE" );
692 if ( IN_CLOSE_NOWRITE & events ) {
693 strcat( ret, chrtostr(sep) );
694 strcat( ret,
"CLOSE_NOWRITE" );
696 if ( IN_OPEN & events ) {
697 strcat( ret, chrtostr(sep) );
698 strcat( ret,
"OPEN" );
700 if ( IN_MOVED_FROM & events ) {
701 strcat( ret, chrtostr(sep) );
702 strcat( ret,
"MOVED_FROM" );
704 if ( IN_MOVED_TO & events ) {
705 strcat( ret, chrtostr(sep) );
706 strcat( ret,
"MOVED_TO" );
708 if ( IN_CREATE & events ) {
709 strcat( ret, chrtostr(sep) );
710 strcat( ret,
"CREATE" );
712 if ( IN_DELETE & events ) {
713 strcat( ret, chrtostr(sep) );
714 strcat( ret,
"DELETE" );
716 if ( IN_DELETE_SELF & events ) {
717 strcat( ret, chrtostr(sep) );
718 strcat( ret,
"DELETE_SELF" );
720 if ( IN_UNMOUNT & events ) {
721 strcat( ret, chrtostr(sep) );
722 strcat( ret,
"UNMOUNT" );
724 if ( IN_Q_OVERFLOW & events ) {
725 strcat( ret, chrtostr(sep) );
726 strcat( ret,
"Q_OVERFLOW" );
728 if ( IN_IGNORED & events ) {
729 strcat( ret, chrtostr(sep) );
730 strcat( ret,
"IGNORED" );
732 if ( IN_CLOSE & events ) {
733 strcat( ret, chrtostr(sep) );
734 strcat( ret,
"CLOSE" );
736 if ( IN_MOVE_SELF & events ) {
737 strcat( ret, chrtostr(sep) );
738 strcat( ret,
"MOVE_SELF" );
740 if ( IN_ISDIR & events ) {
741 strcat( ret, chrtostr(sep) );
742 strcat( ret,
"ISDIR" );
744 if ( IN_ONESHOT & events ) {
745 strcat( ret, chrtostr(sep) );
746 strcat( ret,
"ONESHOT" );
750 if (ret[0] ==
'\0') {
751 niceassert( -1 != sprintf( ret,
"%c0x%08x", sep, events ), 0 );
778 niceassert( init,
"inotifytools_initialize not called yet" );
779 watch *w = watch_from_wd(wd);
801 niceassert( init,
"inotifytools_initialize not called yet" );
802 watch *w = watch_from_filename(filename);
822 niceassert( init,
"inotifytools_initialize not called yet" );
823 watch *w = watch_from_wd(wd);
825 if (w->filename) free(w->filename);
826 w->filename = strdup(filename);
844 char const * newname ) {
845 watch *w = watch_from_filename(oldname);
847 if (w->filename) free(w->filename);
848 w->filename = strdup(newname);
874 char const * newname ) {
875 if ( !oldname || !newname )
return;
876 struct replace_filename_data data;
877 data.old_name = oldname;
878 data.new_name = newname;
879 data.old_len = strlen(oldname);
880 rbwalk(tree_filename, (
void *)replace_filename, (
void *)&data);
886 int remove_inotify_watch(watch *w) {
888 int status = inotify_rm_watch( inotify_fd, w->wd );
890 fprintf(stderr,
"Failed to remove watch on %s: %s\n", w->filename,
901 watch *create_watch(
int wd,
char *filename) {
902 if ( wd <= 0 || !filename)
return 0;
904 watch *w = (watch*)calloc(1,
sizeof(watch));
906 w->filename = strdup(filename);
907 rbsearch(w, tree_wd);
908 rbsearch(w, tree_filename);
925 niceassert( init,
"inotifytools_initialize not called yet" );
926 watch *w = watch_from_wd(wd);
929 if (!remove_inotify_watch(w))
return 0;
930 rbdelete(w, tree_wd);
931 rbdelete(w, tree_filename);
948 niceassert( init,
"inotifytools_initialize not called yet" );
949 watch *w = watch_from_filename(filename);
952 if (!remove_inotify_watch(w))
return 0;
953 rbdelete(w, tree_wd);
954 rbdelete(w, tree_filename);
971 static char const * filenames[2];
972 filenames[0] = filename;
993 niceassert( init,
"inotifytools_initialize not called yet" );
997 for ( i = 0; filenames[i]; ++i ) {
999 wd = inotify_add_watch( inotify_fd, filenames[i], events );
1006 fprintf( stderr,
"Failed to watch %s: returned wd was %d "
1007 "(expected -1 or >0 )", filenames[i], wd );
1015 if ( !isdir(filenames[i])
1016 || filenames[i][strlen(filenames[i])-1] ==
'/') {
1017 filename = strdup(filenames[i]);
1020 nasprintf( &filename,
"%s/", filenames[i] );
1022 create_watch(wd, filename);
1109 niceassert( init,
"inotifytools_initialize not called yet" );
1110 niceassert( num_events <= MAX_EVENTS,
"too many events requested" );
1112 if ( num_events < 1 )
return NULL;
1114 static struct inotify_event event[MAX_EVENTS];
1115 static struct inotify_event * ret;
1116 static int first_byte = 0;
1117 static ssize_t bytes;
1119 static char match_name[MAX_STRLEN];
1121 #define RETURN(A) {\
1123 inotifytools_snprintf(match_name, MAX_STRLEN, A, "%w%f");\
1124 if (0 == regexec(regex, match_name, 0, 0, 0)) {\
1125 if (!invert_regexp)\
1132 if ( collect_stats ) {\
1143 if ( first_byte != 0
1144 && first_byte <= (
int)(bytes -
sizeof(
struct inotify_event)) ) {
1146 ret = (
struct inotify_event *)((
char *)&
event[0] + first_byte);
1147 first_byte +=
sizeof(
struct inotify_event) + ret->len;
1151 if ( first_byte == bytes ) {
1154 else if ( first_byte > bytes ) {
1161 niceassert( (
long)((
char *)&event[0] +
1162 sizeof(
struct inotify_event) +
1163 event[0].len) <= (
long)ret,
1164 "extremely unlucky user, death imminent" );
1166 bytes = (
char *)&event[0] + bytes - (
char *)ret;
1167 memcpy( &event[0], ret, bytes );
1174 else if ( first_byte == 0 ) {
1179 static ssize_t this_bytes;
1180 static unsigned int bytes_to_read;
1182 static fd_set read_fds;
1184 static struct timeval read_timeout;
1185 read_timeout.tv_sec = timeout;
1186 read_timeout.tv_usec = 0;
1187 static struct timeval * read_timeout_ptr;
1188 read_timeout_ptr = ( timeout < 0 ? NULL : &read_timeout );
1191 FD_SET(inotify_fd, &read_fds);
1192 rc = select(inotify_fd + 1, &read_fds,
1193 NULL, NULL, read_timeout_ptr);
1199 else if ( rc == 0 ) {
1206 rc = ioctl( inotify_fd, FIONREAD, &bytes_to_read );
1208 bytes_to_read <
sizeof(
struct inotify_event)*num_events );
1215 this_bytes = read(inotify_fd, &event[0] + bytes,
1216 sizeof(
struct inotify_event)*MAX_EVENTS - bytes);
1217 if ( this_bytes < 0 ) {
1221 if ( this_bytes == 0 ) {
1222 fprintf(stderr,
"Inotify reported end-of-file. Possibly too many "
1223 "events occurred at once.\n");
1226 bytes += this_bytes;
1229 first_byte =
sizeof(
struct inotify_event) + ret->len;
1230 niceassert( first_byte <= bytes,
"ridiculously long filename, things will "
1231 "almost certainly screw up." );
1232 if ( first_byte == bytes ) {
1303 char const ** exclude_list ) {
1304 niceassert( init,
"inotifytools_initialize not called yet" );
1309 dir = opendir( path );
1312 if ( errno == ENOTDIR ) {
1321 if ( path[strlen(path)-1] !=
'/' ) {
1322 nasprintf( &my_path,
"%s/", path );
1325 my_path = (
char *)path;
1328 static struct dirent * ent;
1330 static struct stat64 my_stat;
1331 ent = readdir( dir );
1334 if ( (0 != strcmp( ent->d_name,
"." )) &&
1335 (0 != strcmp( ent->d_name,
".." )) ) {
1336 nasprintf(&next_file,
"%s%s", my_path, ent->d_name);
1337 if ( -1 == lstat64( next_file, &my_stat ) ) {
1340 if ( errno != EACCES ) {
1342 if ( my_path != path ) free( my_path );
1347 else if ( S_ISDIR( my_stat.st_mode ) &&
1348 !S_ISLNK( my_stat.st_mode )) {
1350 nasprintf(&next_file,
"%s%s/", my_path, ent->d_name);
1351 static unsigned int no_watch;
1352 static char const ** exclude_entry;
1355 for (exclude_entry = exclude_list;
1356 exclude_entry && *exclude_entry && !no_watch;
1358 static int exclude_length;
1360 exclude_length = strlen(*exclude_entry);
1361 if ((*exclude_entry)[exclude_length-1] ==
'/') {
1364 if ( strlen(next_file) == (
unsigned)(exclude_length + 1) &&
1365 !strncmp(*exclude_entry, next_file, exclude_length)) {
1377 if ( !status && (EACCES != error) && (ENOENT != error) &&
1378 (ELOOP != error) ) {
1380 if ( my_path != path ) free( my_path );
1391 ent = readdir( dir );
1398 if ( my_path != path ) free( my_path );
1405 void record_stats(
struct inotify_event
const * event ) {
1407 watch *w = watch_from_wd(event->wd);
1409 if ( IN_ACCESS & event->mask ) {
1413 if ( IN_MODIFY & event->mask ) {
1417 if ( IN_ATTRIB & event->mask ) {
1421 if ( IN_CLOSE_WRITE & event->mask ) {
1422 ++w->hit_close_write;
1425 if ( IN_CLOSE_NOWRITE & event->mask ) {
1426 ++w->hit_close_nowrite;
1427 ++num_close_nowrite;
1429 if ( IN_OPEN & event->mask ) {
1433 if ( IN_MOVED_FROM & event->mask ) {
1434 ++w->hit_moved_from;
1437 if ( IN_MOVED_TO & event->mask ) {
1441 if ( IN_CREATE & event->mask ) {
1445 if ( IN_DELETE & event->mask ) {
1449 if ( IN_DELETE_SELF & event->mask ) {
1450 ++w->hit_delete_self;
1453 if ( IN_UNMOUNT & event->mask ) {
1457 if ( IN_MOVE_SELF & event->mask ) {
1467 unsigned int *stat_ptr(watch *w,
int event)
1469 if ( IN_ACCESS == event )
1470 return &w->hit_access;
1471 if ( IN_MODIFY == event )
1472 return &w->hit_modify;
1473 if ( IN_ATTRIB == event )
1474 return &w->hit_attrib;
1475 if ( IN_CLOSE_WRITE == event )
1476 return &w->hit_close_write;
1477 if ( IN_CLOSE_NOWRITE == event )
1478 return &w->hit_close_nowrite;
1479 if ( IN_OPEN == event )
1480 return &w->hit_open;
1481 if ( IN_MOVED_FROM == event )
1482 return &w->hit_moved_from;
1483 if ( IN_MOVED_TO == event )
1484 return &w->hit_moved_to;
1485 if ( IN_CREATE == event )
1486 return &w->hit_create;
1487 if ( IN_DELETE == event )
1488 return &w->hit_delete;
1489 if ( IN_DELETE_SELF == event )
1490 return &w->hit_delete_self;
1491 if ( IN_UNMOUNT == event )
1492 return &w->hit_unmount;
1493 if ( IN_MOVE_SELF == event )
1494 return &w->hit_move_self;
1496 return &w->hit_total;
1516 if (!collect_stats)
return -1;
1518 watch *w = watch_from_wd(wd);
1520 unsigned int *i = stat_ptr(w, event);
1539 if (!collect_stats)
return -1;
1540 if ( IN_ACCESS == event )
1542 if ( IN_MODIFY == event )
1544 if ( IN_ATTRIB == event )
1546 if ( IN_CLOSE_WRITE == event )
1547 return num_close_write;
1548 if ( IN_CLOSE_NOWRITE == event )
1549 return num_close_nowrite;
1550 if ( IN_OPEN == event )
1552 if ( IN_MOVED_FROM == event )
1553 return num_moved_from;
1554 if ( IN_MOVED_TO == event )
1555 return num_moved_to;
1556 if ( IN_CREATE == event )
1558 if ( IN_DELETE == event )
1560 if ( IN_DELETE_SELF == event )
1561 return num_delete_self;
1562 if ( IN_UNMOUNT == event )
1564 if ( IN_MOVE_SELF == event )
1565 return num_move_self;
1595 filename ), event );
1615 static int isdir(
char const * path ) {
1616 static struct stat64 my_stat;
1618 if ( -1 == lstat64( path, &my_stat ) ) {
1619 if (errno == ENOENT)
return 0;
1620 fprintf(stderr,
"Stat failed on %s: %s\n", path, strerror(errno));
1624 return S_ISDIR( my_stat.st_mode ) && !S_ISLNK( my_stat.st_mode );
1636 rbwalk(tree_filename, get_num, (
void*)&ret);
1726 static char out[MAX_STRLEN+1];
1729 if ( -1 != ret ) fprintf( file,
"%s", out );
1835 struct inotify_event* event,
char* fmt ) {
1836 static char * filename, * eventname, * eventstr;
1837 static unsigned int i, ind;
1839 static char timestr[MAX_STRLEN];
1843 if ( event->len > 0 ) {
1844 eventname =
event->name;
1853 if ( !fmt || 0 == strlen(fmt) ) {
1857 if ( strlen(fmt) > MAX_STRLEN || size > MAX_STRLEN) {
1863 for ( i = 0; i < strlen(fmt) &&
1864 (int)ind < size - 1; ++i ) {
1865 if ( fmt[i] !=
'%' ) {
1866 out[ind++] = fmt[i];
1870 if ( i == strlen(fmt) - 1 ) {
1886 strncpy( &out[ind], filename, size - ind );
1887 ind += strlen(filename);
1895 strncpy( &out[ind], eventname, size - ind );
1896 ind += strlen(eventname);
1904 strncpy( &out[ind], eventstr, size - ind );
1905 ind += strlen(eventstr);
1915 if ( 0 >= strftime( timestr, MAX_STRLEN-1, timefmt,
1916 localtime( &now ) ) ) {
1927 strncpy( &out[ind], timestr, size - ind );
1928 ind += strlen(timestr);
1934 if ( i < strlen(fmt) - 2 && fmt[i+2] ==
'e' ) {
1936 strncpy( &out[ind], eventstr, size - ind );
1937 ind += strlen(eventstr);
1943 if ( ind < MAX_STRLEN ) out[ind++] =
'%';
1944 if ( ind < MAX_STRLEN ) out[ind++] = ch1;
1975 if ( !read_num_from_file( QUEUE_SIZE_PATH, &ret ) )
return -1;
1990 if ( !read_num_from_file( INSTANCES_PATH, &ret ) )
return -1;
2005 if ( !read_num_from_file( WATCHES_SIZE_PATH, &ret ) )
return -1;
2022 static int do_ignore_events_by_regex(
char const *pattern,
int flags,
int invert ) {
2032 if (regex) { regfree(regex); }
2033 else { regex = (regex_t *)malloc(
sizeof(regex_t)); }
2035 invert_regexp = invert;
2036 int ret = regcomp(regex, pattern, flags | REG_NOSUB);
2037 if (0 == ret)
return 1;
2058 return do_ignore_events_by_regex(pattern, flags, 0);
2073 return do_ignore_events_by_regex(pattern, flags, 1);
2076 int event_compare(
const void *p1,
const void *p2,
const void *config)
2078 if (!p1 || !p2)
return p1 - p2;
2080 int sort_event = *(
int*)config;
2081 if (sort_event == -1) {
2084 }
else if (sort_event < 0) {
2085 sort_event = -sort_event;
2088 unsigned int *i1 = stat_ptr((watch*)p1, sort_event);
2089 unsigned int *i2 = stat_ptr((watch*)p2, sort_event);
2090 if (0 == *i1 - *i2) {
2091 return ((watch*)p1)->wd - ((watch*)p2)->wd;
2099 struct rbtree *inotifytools_wd_sorted_by_event(
int sort_event)
2101 struct rbtree *ret = rbinit(event_compare, (
void*)(uintptr_t)sort_event);
2102 RBLIST *all = rbopenlist(tree_wd);
2103 void const *p = rbreadlist(all);
2105 void const *r = rbsearch(p, ret);
2106 niceassert((
int)(r == p),
"Couldn't insert watch into new tree");
2107 p = rbreadlist(all);