15 #include "../../config.h"
17 #include "inotifytools_p.h"
24 #include <sys/select.h>
25 #include <sys/types.h>
27 #include <sys/ioctl.h>
34 #include "inotifytools/inotify.h"
122 #define MAX_EVENTS 4096
123 #define MAX_STRLEN 4096
124 #define INOTIFY_PROCDIR "/proc/sys/fs/inotify/"
125 #define WATCHES_SIZE_PATH INOTIFY_PROCDIR "max_user_watches"
126 #define QUEUE_SIZE_PATH INOTIFY_PROCDIR "max_queued_watches"
127 #define INSTANCES_PATH INOTIFY_PROCDIR "max_user_instances"
129 static int inotify_fd;
130 static unsigned num_access;
131 static unsigned num_modify;
132 static unsigned num_attrib;
133 static unsigned num_close_nowrite;
134 static unsigned num_close_write;
135 static unsigned num_open;
136 static unsigned num_move_self;
137 static unsigned num_moved_to;
138 static unsigned num_moved_from;
139 static unsigned num_create;
140 static unsigned num_delete;
141 static unsigned num_delete_self;
142 static unsigned num_unmount;
143 static unsigned num_total;
144 static int collect_stats = 0;
146 struct rbtree *tree_wd = 0;
147 struct rbtree *tree_filename = 0;
148 static int error = 0;
150 static char* timefmt = 0;
151 static regex_t* regex = 0;
153 int isdir(
char const * path );
154 void record_stats(
struct inotify_event
const * event );
155 int onestr_to_event(
char const * event);
174 #define niceassert(cond,mesg) _niceassert((long)cond, __LINE__, __FILE__, \
177 #define nasprintf(...) niceassert( -1 != asprintf(__VA_ARGS__), "out of memory")
196 void _niceassert(
long cond,
int line,
char const * file,
char const * condstr,
197 char const * mesg ) {
201 fprintf(stderr,
"%s:%d assertion ( %s ) failed: %s\n", file, line,
205 fprintf(stderr,
"%s:%d assertion ( %s ) failed.\n", file, line, condstr);
218 char * chrtostr(
char ch) {
219 static char str[2] = {
'\0',
'\0' };
227 int read_num_from_file(
char * filename,
int * num ) {
228 FILE * file = fopen( filename,
"r" );
234 if ( EOF == fscanf( file,
"%d", num ) ) {
239 niceassert( 0 == fclose( file ), 0 );
244 int wd_compare(
const void *d1,
const void *d2,
const void *config) {
245 if (!d1 || !d2)
return d1 - d2;
246 return ((watch*)d1)->wd - ((watch*)d2)->wd;
249 int filename_compare(
const void *d1,
const void *d2,
const void *config) {
250 if (!d1 || !d2)
return d1 - d2;
251 return strcmp(((watch*)d1)->filename, ((watch*)d2)->filename);
257 watch *watch_from_wd(
int wd ) {
260 return (watch*)rbfind(&w, tree_wd);
266 watch *watch_from_filename(
char const *filename ) {
268 w.filename = (
char*)filename;
269 return (watch*)rbfind(&w, tree_filename);
286 inotify_fd = inotify_init();
287 if (inotify_fd < 0) {
294 tree_wd = rbinit(wd_compare, 0);
295 tree_filename = rbinit(filename_compare, 0);
304 void destroy_watch(watch *w) {
305 if (w->filename) free(w->filename);
312 void cleanup_tree(
const void *nodep,
314 const int depth,
void* arg) {
315 if (which != endorder && which != leaf)
return;
316 watch *w = (watch*)nodep;
341 rbwalk(tree_wd, cleanup_tree, 0);
342 rbdestroy(tree_wd); tree_wd = 0;
343 rbdestroy(tree_filename); tree_filename = 0;
349 void empty_stats(
const void *nodep,
351 const int depth,
void *arg) {
352 if (which != endorder && which != leaf)
return;
353 watch *w = (watch*)nodep;
357 w->hit_close_nowrite = 0;
358 w->hit_close_write = 0;
360 w->hit_move_self = 0;
361 w->hit_moved_from = 0;
365 w->hit_delete_self = 0;
373 void replace_filename(
const void *nodep,
375 const int depth,
void *arg) {
376 if (which != endorder && which != leaf)
return;
377 watch *w = (watch*)nodep;
378 char *old_name = ((
char**)arg)[0];
379 char *new_name = ((
char**)arg)[1];
380 int old_len = *((
int*)&((
char**)arg)[2]);
382 if ( 0 == strncmp( old_name, w->filename, old_len ) ) {
383 nasprintf( &name,
"%s%s", new_name, &(w->filename[old_len]) );
384 if (!strcmp( w->filename, new_name )) {
387 rbdelete(w, tree_filename);
390 rbsearch(w, tree_filename);
398 void get_num(
const void *nodep,
400 const int depth,
void *arg) {
401 if (which != endorder && which != leaf)
return;
419 niceassert( init,
"inotifytools_initialize not called yet" );
423 rbwalk(tree_wd, empty_stats, 0);
429 num_close_nowrite = 0;
472 if ( strchr(
"_" "abcdefghijklmnopqrstuvwxyz"
473 "ABCDEFGHIJKLMNOPQRSTUVWXYZ", sep ) ) {
478 char * event1, * event2;
482 if ( !event || !event[0] )
return 0;
484 event1 = (
char *)event;
485 event2 = strchr( event1, sep );
486 while ( event1 && event1[0] ) {
488 len = event2 - event1;
489 niceassert( len < 4096,
"malformed event string (very long)" );
492 len = strlen(event1);
494 if ( len > 4095 ) len = 4095;
495 strncpy( eventstr, event1, len );
498 ret1 = onestr_to_event( eventstr );
499 if ( 0 == ret1 || -1 == ret1 ) {
506 if ( event1 && event1[0] ) {
510 if ( !event1[0] )
return 0;
511 event2 = strchr( event1, sep );
556 int onestr_to_event(
char const * event)
561 if ( !event || !event[0] )
563 else if ( 0 == strcasecmp(event,
"ACCESS") )
565 else if ( 0 == strcasecmp(event,
"MODIFY") )
567 else if ( 0 == strcasecmp(event,
"ATTRIB") )
569 else if ( 0 == strcasecmp(event,
"CLOSE_WRITE") )
570 ret = IN_CLOSE_WRITE;
571 else if ( 0 == strcasecmp(event,
"CLOSE_NOWRITE") )
572 ret = IN_CLOSE_NOWRITE;
573 else if ( 0 == strcasecmp(event,
"OPEN") )
575 else if ( 0 == strcasecmp(event,
"MOVED_FROM") )
577 else if ( 0 == strcasecmp(event,
"MOVED_TO") )
579 else if ( 0 == strcasecmp(event,
"CREATE") )
581 else if ( 0 == strcasecmp(event,
"DELETE") )
583 else if ( 0 == strcasecmp(event,
"DELETE_SELF") )
584 ret = IN_DELETE_SELF;
585 else if ( 0 == strcasecmp(event,
"UNMOUNT") )
587 else if ( 0 == strcasecmp(event,
"Q_OVERFLOW") )
589 else if ( 0 == strcasecmp(event,
"IGNORED") )
591 else if ( 0 == strcasecmp(event,
"CLOSE") )
593 else if ( 0 == strcasecmp(event,
"MOVE_SELF") )
595 else if ( 0 == strcasecmp(event,
"MOVE") )
597 else if ( 0 == strcasecmp(event,
"ISDIR") )
599 else if ( 0 == strcasecmp(event,
"ONESHOT") )
601 else if ( 0 == strcasecmp(event,
"ALL_EVENTS") )
658 static char ret[1024];
662 if ( IN_ACCESS & events ) {
663 strcat( ret, chrtostr(sep) );
664 strcat( ret,
"ACCESS" );
666 if ( IN_MODIFY & events ) {
667 strcat( ret, chrtostr(sep) );
668 strcat( ret,
"MODIFY" );
670 if ( IN_ATTRIB & events ) {
671 strcat( ret, chrtostr(sep) );
672 strcat( ret,
"ATTRIB" );
674 if ( IN_CLOSE_WRITE & events ) {
675 strcat( ret, chrtostr(sep) );
676 strcat( ret,
"CLOSE_WRITE" );
678 if ( IN_CLOSE_NOWRITE & events ) {
679 strcat( ret, chrtostr(sep) );
680 strcat( ret,
"CLOSE_NOWRITE" );
682 if ( IN_OPEN & events ) {
683 strcat( ret, chrtostr(sep) );
684 strcat( ret,
"OPEN" );
686 if ( IN_MOVED_FROM & events ) {
687 strcat( ret, chrtostr(sep) );
688 strcat( ret,
"MOVED_FROM" );
690 if ( IN_MOVED_TO & events ) {
691 strcat( ret, chrtostr(sep) );
692 strcat( ret,
"MOVED_TO" );
694 if ( IN_CREATE & events ) {
695 strcat( ret, chrtostr(sep) );
696 strcat( ret,
"CREATE" );
698 if ( IN_DELETE & events ) {
699 strcat( ret, chrtostr(sep) );
700 strcat( ret,
"DELETE" );
702 if ( IN_DELETE_SELF & events ) {
703 strcat( ret, chrtostr(sep) );
704 strcat( ret,
"DELETE_SELF" );
706 if ( IN_UNMOUNT & events ) {
707 strcat( ret, chrtostr(sep) );
708 strcat( ret,
"UNMOUNT" );
710 if ( IN_Q_OVERFLOW & events ) {
711 strcat( ret, chrtostr(sep) );
712 strcat( ret,
"Q_OVERFLOW" );
714 if ( IN_IGNORED & events ) {
715 strcat( ret, chrtostr(sep) );
716 strcat( ret,
"IGNORED" );
718 if ( IN_CLOSE & events ) {
719 strcat( ret, chrtostr(sep) );
720 strcat( ret,
"CLOSE" );
722 if ( IN_MOVE_SELF & events ) {
723 strcat( ret, chrtostr(sep) );
724 strcat( ret,
"MOVE_SELF" );
726 if ( IN_ISDIR & events ) {
727 strcat( ret, chrtostr(sep) );
728 strcat( ret,
"ISDIR" );
730 if ( IN_ONESHOT & events ) {
731 strcat( ret, chrtostr(sep) );
732 strcat( ret,
"ONESHOT" );
736 if (ret[0] ==
'\0') {
737 niceassert( -1 != sprintf( ret,
"%c0x%08x", sep, events ), 0 );
764 niceassert( init,
"inotifytools_initialize not called yet" );
765 watch *w = watch_from_wd(wd);
787 niceassert( init,
"inotifytools_initialize not called yet" );
788 watch *w = watch_from_filename(filename);
808 niceassert( init,
"inotifytools_initialize not called yet" );
809 watch *w = watch_from_wd(wd);
811 if (w->filename) free(w->filename);
812 w->filename = strdup(filename);
830 char const * newname ) {
831 watch *w = watch_from_filename(oldname);
833 if (w->filename) free(w->filename);
834 w->filename = strdup(newname);
860 char const * newname ) {
861 if ( !oldname || !newname )
return;
862 char *names[2+
sizeof(int)/
sizeof(
char*) + 1];
863 names[0] = (
char*)oldname;
864 names[1] = (
char*)newname;
865 *((
int*)&names[2]) = strlen(oldname);
866 rbwalk(tree_filename, replace_filename, (
void*)names);
872 int remove_inotify_watch(watch *w) {
874 int status = inotify_rm_watch( inotify_fd, w->wd );
876 fprintf(stderr,
"Failed to remove watch on %s: %s\n", w->filename,
887 watch *create_watch(
int wd,
char *filename) {
888 if ( wd <= 0 || !filename)
return 0;
890 watch *w = (watch*)calloc(1,
sizeof(watch));
892 w->filename = strdup(filename);
893 rbsearch(w, tree_wd);
894 rbsearch(w, tree_filename);
910 niceassert( init,
"inotifytools_initialize not called yet" );
911 watch *w = watch_from_wd(wd);
914 if (!remove_inotify_watch(w))
return 0;
915 rbdelete(w, tree_wd);
916 rbdelete(w, tree_filename);
933 niceassert( init,
"inotifytools_initialize not called yet" );
934 watch *w = watch_from_filename(filename);
937 if (!remove_inotify_watch(w))
return 0;
938 rbdelete(w, tree_wd);
939 rbdelete(w, tree_filename);
956 static char const * filenames[2];
957 filenames[0] = filename;
978 niceassert( init,
"inotifytools_initialize not called yet" );
982 for ( i = 0; filenames[i]; ++i ) {
984 wd = inotify_add_watch( inotify_fd, filenames[i], events );
991 fprintf( stderr,
"Failed to watch %s: returned wd was %d "
992 "(expected -1 or >0 )", filenames[i], wd );
1000 if ( !isdir(filenames[i])
1001 || filenames[i][strlen(filenames[i])-1] ==
'/') {
1002 filename = strdup(filenames[i]);
1005 nasprintf( &filename,
"%s/", filenames[i] );
1007 create_watch(wd, filename);
1095 niceassert( init,
"inotifytools_initialize not called yet" );
1096 niceassert( num_events <= MAX_EVENTS,
"too many events requested" );
1098 if ( num_events < 1 )
return NULL;
1100 static struct inotify_event event[MAX_EVENTS];
1101 static struct inotify_event * ret;
1102 static int first_byte = 0;
1103 static ssize_t bytes;
1105 static char match_name[MAX_STRLEN];
1107 #define RETURN(A) {\
1109 inotifytools_snprintf(match_name, MAX_STRLEN, A, "%w%f");\
1110 if (0 == regexec(regex, match_name, 0, 0, 0)) {\
1114 if ( collect_stats ) {\
1125 if ( first_byte != 0
1126 && first_byte <= (
int)(bytes -
sizeof(
struct inotify_event)) ) {
1128 ret = (
struct inotify_event *)((
char *)&
event[0] + first_byte);
1129 first_byte +=
sizeof(
struct inotify_event) + ret->len;
1133 if ( first_byte == bytes ) {
1136 else if ( first_byte > bytes ) {
1143 niceassert( (
long)((
char *)&event[0] +
1144 sizeof(
struct inotify_event) +
1145 event[0].len) <= (
long)ret,
1146 "extremely unlucky user, death imminent" );
1148 bytes = (
char *)&event[0] + bytes - (
char *)ret;
1149 memcpy( &event[0], ret, bytes );
1156 else if ( first_byte == 0 ) {
1161 static ssize_t this_bytes;
1162 static unsigned int bytes_to_read;
1164 static fd_set read_fds;
1166 static struct timeval read_timeout;
1167 read_timeout.tv_sec = timeout;
1168 read_timeout.tv_usec = 0;
1169 static struct timeval * read_timeout_ptr;
1170 read_timeout_ptr = ( timeout <= 0 ? NULL : &read_timeout );
1173 FD_SET(inotify_fd, &read_fds);
1174 rc = select(inotify_fd + 1, &read_fds,
1175 NULL, NULL, read_timeout_ptr);
1181 else if ( rc == 0 ) {
1188 rc = ioctl( inotify_fd, FIONREAD, &bytes_to_read );
1190 bytes_to_read <
sizeof(
struct inotify_event)*num_events );
1197 this_bytes = read(inotify_fd, &event[0] + bytes,
1198 sizeof(
struct inotify_event)*MAX_EVENTS - bytes);
1199 if ( this_bytes < 0 ) {
1203 if ( this_bytes == 0 ) {
1204 fprintf(stderr,
"Inotify reported end-of-file. Possibly too many "
1205 "events occurred at once.\n");
1208 bytes += this_bytes;
1211 first_byte =
sizeof(
struct inotify_event) + ret->len;
1212 niceassert( first_byte <= bytes,
"ridiculously long filename, things will "
1213 "almost certainly screw up." );
1214 if ( first_byte == bytes ) {
1285 char const ** exclude_list ) {
1286 niceassert( init,
"inotifytools_initialize not called yet" );
1291 dir = opendir( path );
1294 if ( errno == ENOTDIR ) {
1303 if ( path[strlen(path)-1] !=
'/' ) {
1304 nasprintf( &my_path,
"%s/", path );
1307 my_path = (
char *)path;
1310 static struct dirent * ent;
1312 static struct stat64 my_stat;
1313 ent = readdir( dir );
1316 if ( (0 != strcmp( ent->d_name,
"." )) &&
1317 (0 != strcmp( ent->d_name,
".." )) ) {
1318 nasprintf(&next_file,
"%s%s", my_path, ent->d_name);
1319 if ( -1 == lstat64( next_file, &my_stat ) ) {
1322 if ( errno != EACCES ) {
1324 if ( my_path != path ) free( my_path );
1329 else if ( S_ISDIR( my_stat.st_mode ) &&
1330 !S_ISLNK( my_stat.st_mode )) {
1332 nasprintf(&next_file,
"%s%s/", my_path, ent->d_name);
1333 static unsigned int no_watch;
1334 static char const ** exclude_entry;
1337 for (exclude_entry = exclude_list;
1338 exclude_entry && *exclude_entry && !no_watch;
1340 static int exclude_length;
1342 exclude_length = strlen(*exclude_entry);
1343 if ((*exclude_entry)[exclude_length-1] ==
'/') {
1346 if ( strlen(next_file) == (
unsigned)(exclude_length + 1) &&
1347 !strncmp(*exclude_entry, next_file, exclude_length)) {
1359 if ( !status && (EACCES != error) && (ENOENT != error) &&
1360 (ELOOP != error) ) {
1362 if ( my_path != path ) free( my_path );
1373 ent = readdir( dir );
1380 if ( my_path != path ) free( my_path );
1387 void record_stats(
struct inotify_event
const * event ) {
1389 watch *w = watch_from_wd(event->wd);
1391 if ( IN_ACCESS & event->mask ) {
1395 if ( IN_MODIFY & event->mask ) {
1399 if ( IN_ATTRIB & event->mask ) {
1403 if ( IN_CLOSE_WRITE & event->mask ) {
1404 ++w->hit_close_write;
1407 if ( IN_CLOSE_NOWRITE & event->mask ) {
1408 ++w->hit_close_nowrite;
1409 ++num_close_nowrite;
1411 if ( IN_OPEN & event->mask ) {
1415 if ( IN_MOVED_FROM & event->mask ) {
1416 ++w->hit_moved_from;
1419 if ( IN_MOVED_TO & event->mask ) {
1423 if ( IN_CREATE & event->mask ) {
1427 if ( IN_DELETE & event->mask ) {
1431 if ( IN_DELETE_SELF & event->mask ) {
1432 ++w->hit_delete_self;
1435 if ( IN_UNMOUNT & event->mask ) {
1439 if ( IN_MOVE_SELF & event->mask ) {
1449 int *stat_ptr(watch *w,
int event)
1451 if ( IN_ACCESS == event )
1452 return &w->hit_access;
1453 if ( IN_MODIFY == event )
1454 return &w->hit_modify;
1455 if ( IN_ATTRIB == event )
1456 return &w->hit_attrib;
1457 if ( IN_CLOSE_WRITE == event )
1458 return &w->hit_close_write;
1459 if ( IN_CLOSE_NOWRITE == event )
1460 return &w->hit_close_nowrite;
1461 if ( IN_OPEN == event )
1462 return &w->hit_open;
1463 if ( IN_MOVED_FROM == event )
1464 return &w->hit_moved_from;
1465 if ( IN_MOVED_TO == event )
1466 return &w->hit_moved_to;
1467 if ( IN_CREATE == event )
1468 return &w->hit_create;
1469 if ( IN_DELETE == event )
1470 return &w->hit_delete;
1471 if ( IN_DELETE_SELF == event )
1472 return &w->hit_delete_self;
1473 if ( IN_UNMOUNT == event )
1474 return &w->hit_unmount;
1475 if ( IN_MOVE_SELF == event )
1476 return &w->hit_move_self;
1478 return &w->hit_total;
1498 if (!collect_stats)
return -1;
1500 watch *w = watch_from_wd(wd);
1502 int *i = stat_ptr(w, event);
1521 if (!collect_stats)
return -1;
1522 if ( IN_ACCESS == event )
1524 if ( IN_MODIFY == event )
1526 if ( IN_ATTRIB == event )
1528 if ( IN_CLOSE_WRITE == event )
1529 return num_close_write;
1530 if ( IN_CLOSE_NOWRITE == event )
1531 return num_close_nowrite;
1532 if ( IN_OPEN == event )
1534 if ( IN_MOVED_FROM == event )
1535 return num_moved_from;
1536 if ( IN_MOVED_TO == event )
1537 return num_moved_to;
1538 if ( IN_CREATE == event )
1540 if ( IN_DELETE == event )
1542 if ( IN_DELETE_SELF == event )
1543 return num_delete_self;
1544 if ( IN_UNMOUNT == event )
1546 if ( IN_MOVE_SELF == event )
1547 return num_move_self;
1577 filename ), event );
1597 int isdir(
char const * path ) {
1598 static struct stat64 my_stat;
1600 if ( -1 == lstat64( path, &my_stat ) ) {
1601 if (errno == ENOENT)
return 0;
1602 fprintf(stderr,
"Stat failed on %s: %s\n", path, strerror(errno));
1606 return S_ISDIR( my_stat.st_mode ) && !S_ISLNK( my_stat.st_mode );
1618 rbwalk(tree_filename, get_num, (
void*)&ret);
1708 static char out[MAX_STRLEN+1];
1711 if ( -1 != ret ) fprintf( file,
"%s", out );
1817 struct inotify_event* event,
char* fmt ) {
1818 static char * filename, * eventname, * eventstr;
1819 static unsigned int i, ind;
1821 static char timestr[MAX_STRLEN];
1825 if ( event->len > 0 ) {
1826 eventname =
event->name;
1835 if ( !fmt || 0 == strlen(fmt) ) {
1839 if ( strlen(fmt) > MAX_STRLEN || size > MAX_STRLEN) {
1845 for ( i = 0; i < strlen(fmt) &&
1846 (int)ind < size - 1; ++i ) {
1847 if ( fmt[i] !=
'%' ) {
1848 out[ind++] = fmt[i];
1852 if ( i == strlen(fmt) - 1 ) {
1868 strncpy( &out[ind], filename, size - ind );
1869 ind += strlen(filename);
1877 strncpy( &out[ind], eventname, size - ind );
1878 ind += strlen(eventname);
1886 strncpy( &out[ind], eventstr, size - ind );
1887 ind += strlen(eventstr);
1897 if ( 0 >= strftime( timestr, MAX_STRLEN-1, timefmt,
1898 localtime( &now ) ) ) {
1909 strncpy( &out[ind], timestr, size - ind );
1910 ind += strlen(timestr);
1916 if ( i < strlen(fmt) - 2 && fmt[i+2] ==
'e' ) {
1918 strncpy( &out[ind], eventstr, size - ind );
1919 ind += strlen(eventstr);
1925 if ( ind < MAX_STRLEN ) out[ind++] =
'%';
1926 if ( ind < MAX_STRLEN ) out[ind++] = ch1;
1957 if ( !read_num_from_file( QUEUE_SIZE_PATH, &ret ) )
return -1;
1972 if ( !read_num_from_file( INSTANCES_PATH, &ret ) )
return -1;
1987 if ( !read_num_from_file( WATCHES_SIZE_PATH, &ret ) )
return -1;
2012 if (regex) { regfree(regex); }
2013 else { regex = (regex_t *)malloc(
sizeof(regex_t)); }
2015 int ret = regcomp(regex, pattern, flags | REG_NOSUB);
2016 if (0 == ret)
return 1;
2025 int event_compare(
const void *p1,
const void *p2,
const void *config)
2027 if (!p1 || !p2)
return p1 - p2;
2029 int sort_event = (int)config;
2030 if (sort_event == -1) {
2033 }
else if (sort_event < 0) {
2034 sort_event = -sort_event;
2037 int *i1 = stat_ptr((watch*)p1, sort_event);
2038 int *i2 = stat_ptr((watch*)p2, sort_event);
2039 if (0 == *i1 - *i2) {
2040 return ((watch*)p1)->wd - ((watch*)p2)->wd;
2048 struct rbtree *inotifytools_wd_sorted_by_event(
int sort_event)
2050 struct rbtree *ret = rbinit(event_compare, (
void*)sort_event);
2051 RBLIST *all = rbopenlist(tree_wd);
2052 void const *p = rbreadlist(all);
2054 void const *r = rbsearch(p, ret);
2055 niceassert((
int)(r == p),
"Couldn't insert watch into new tree");
2056 p = rbreadlist(all);