libinotifytools
inotifytools.c
1 // kate: replace-tabs off; space-indent off;
2 
15 #include "../../config.h"
17 #include "inotifytools_p.h"
18 
19 #include <string.h>
20 #include <strings.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <errno.h>
24 #include <sys/select.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/ioctl.h>
28 #include <unistd.h>
29 #include <dirent.h>
30 #include <time.h>
31 #include <regex.h>
32 #include <setjmp.h>
33 
34 #include "inotifytools/inotify.h"
35 
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"
128 
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;
145 
146 struct rbtree *tree_wd = 0;
147 struct rbtree *tree_filename = 0;
148 static int error = 0;
149 static int init = 0;
150 static char* timefmt = 0;
151 static regex_t* regex = 0;
152 
153 int isdir( char const * path );
154 void record_stats( struct inotify_event const * event );
155 int onestr_to_event(char const * event);
156 
174 #define niceassert(cond,mesg) _niceassert((long)cond, __LINE__, __FILE__, \
175  #cond, mesg)
176 
177 #define nasprintf(...) niceassert( -1 != asprintf(__VA_ARGS__), "out of memory")
178 
196 void _niceassert( long cond, int line, char const * file, char const * condstr,
197  char const * mesg ) {
198  if ( cond ) return;
199 
200  if ( mesg ) {
201  fprintf(stderr, "%s:%d assertion ( %s ) failed: %s\n", file, line,
202  condstr, mesg );
203  }
204  else {
205  fprintf(stderr, "%s:%d assertion ( %s ) failed.\n", file, line, condstr);
206  }
207 }
208 
218 char * chrtostr(char ch) {
219  static char str[2] = { '\0', '\0' };
220  str[0] = ch;
221  return str;
222 }
223 
227 int read_num_from_file( char * filename, int * num ) {
228  FILE * file = fopen( filename, "r" );
229  if ( !file ) {
230  error = errno;
231  return 0;
232  }
233 
234  if ( EOF == fscanf( file, "%d", num ) ) {
235  error = errno;
236  return 0;
237  }
238 
239  niceassert( 0 == fclose( file ), 0 );
240 
241  return 1;
242 }
243 
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;
247 }
248 
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);
252 }
253 
257 watch *watch_from_wd( int wd ) {
258  watch w;
259  w.wd = wd;
260  return (watch*)rbfind(&w, tree_wd);
261 }
262 
266 watch *watch_from_filename( char const *filename ) {
267  watch w;
268  w.filename = (char*)filename;
269  return (watch*)rbfind(&w, tree_filename);
270 }
271 
282  if (init) return 1;
283 
284  error = 0;
285  // Try to initialise inotify
286  inotify_fd = inotify_init();
287  if (inotify_fd < 0) {
288  error = inotify_fd;
289  return 0;
290  }
291 
292  collect_stats = 0;
293  init = 1;
294  tree_wd = rbinit(wd_compare, 0);
295  tree_filename = rbinit(filename_compare, 0);
296  timefmt = 0;
297 
298  return 1;
299 }
300 
304 void destroy_watch(watch *w) {
305  if (w->filename) free(w->filename);
306  free(w);
307 }
308 
312 void cleanup_tree(const void *nodep,
313  const VISIT which,
314  const int depth, void* arg) {
315  if (which != endorder && which != leaf) return;
316  watch *w = (watch*)nodep;
317  destroy_watch(w);
318 }
319 
327  if (!init) return;
328 
329  init = 0;
330  close(inotify_fd);
331  collect_stats = 0;
332  error = 0;
333  timefmt = 0;
334 
335  if (regex) {
336  regfree(regex);
337  free(regex);
338  regex = 0;
339  }
340 
341  rbwalk(tree_wd, cleanup_tree, 0);
342  rbdestroy(tree_wd); tree_wd = 0;
343  rbdestroy(tree_filename); tree_filename = 0;
344 }
345 
349 void empty_stats(const void *nodep,
350  const VISIT which,
351  const int depth, void *arg) {
352  if (which != endorder && which != leaf) return;
353  watch *w = (watch*)nodep;
354  w->hit_access = 0;
355  w->hit_modify = 0;
356  w->hit_attrib = 0;
357  w->hit_close_nowrite = 0;
358  w->hit_close_write = 0;
359  w->hit_open = 0;
360  w->hit_move_self = 0;
361  w->hit_moved_from = 0;
362  w->hit_moved_to = 0;
363  w->hit_create = 0;
364  w->hit_delete = 0;
365  w->hit_delete_self = 0;
366  w->hit_unmount = 0;
367  w->hit_total = 0;
368 }
369 
373 void replace_filename(const void *nodep,
374  const VISIT which,
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]);
381  char *name;
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 )) {
385  free(name);
386  } else {
387  rbdelete(w, tree_filename);
388  free( w->filename );
389  w->filename = name;
390  rbsearch(w, tree_filename);
391  }
392  }
393 }
394 
398 void get_num(const void *nodep,
399  const VISIT which,
400  const int depth, void *arg) {
401  if (which != endorder && which != leaf) return;
402  ++(*((int*)arg));
403 }
404 
405 
419  niceassert( init, "inotifytools_initialize not called yet" );
420 
421  // if already collecting stats, reset stats
422  if (collect_stats) {
423  rbwalk(tree_wd, empty_stats, 0);
424  }
425 
426  num_access = 0;
427  num_modify = 0;
428  num_attrib = 0;
429  num_close_nowrite = 0;
430  num_close_write = 0;
431  num_open = 0;
432  num_move_self = 0;
433  num_moved_from = 0;
434  num_moved_to = 0;
435  num_create = 0;
436  num_delete = 0;
437  num_delete_self = 0;
438  num_unmount = 0;
439  num_total = 0;
440 
441  collect_stats = 1;
442 }
443 
471 int inotifytools_str_to_event_sep(char const * event, char sep) {
472  if ( strchr( "_" "abcdefghijklmnopqrstuvwxyz"
473  "ABCDEFGHIJKLMNOPQRSTUVWXYZ", sep ) ) {
474  return -1;
475  }
476 
477  int ret, ret1, len;
478  char * event1, * event2;
479  char eventstr[4096];
480  ret = 0;
481 
482  if ( !event || !event[0] ) return 0;
483 
484  event1 = (char *)event;
485  event2 = strchr( event1, sep );
486  while ( event1 && event1[0] ) {
487  if ( event2 ) {
488  len = event2 - event1;
489  niceassert( len < 4096, "malformed event string (very long)" );
490  }
491  else {
492  len = strlen(event1);
493  }
494  if ( len > 4095 ) len = 4095;
495  strncpy( eventstr, event1, len );
496  eventstr[len] = 0;
497 
498  ret1 = onestr_to_event( eventstr );
499  if ( 0 == ret1 || -1 == ret1 ) {
500  ret = ret1;
501  break;
502  }
503  ret |= ret1;
504 
505  event1 = event2;
506  if ( event1 && event1[0] ) {
507  // jump over 'sep' character
508  ++event1;
509  // if last character was 'sep'...
510  if ( !event1[0] ) return 0;
511  event2 = strchr( event1, sep );
512  }
513  }
514 
515  return ret;
516 }
517 
541 int inotifytools_str_to_event(char const * event) {
542  return inotifytools_str_to_event_sep( event, ',' );
543 }
544 
556 int onestr_to_event(char const * event)
557 {
558  static int ret;
559  ret = -1;
560 
561  if ( !event || !event[0] )
562  ret = 0;
563  else if ( 0 == strcasecmp(event, "ACCESS") )
564  ret = IN_ACCESS;
565  else if ( 0 == strcasecmp(event, "MODIFY") )
566  ret = IN_MODIFY;
567  else if ( 0 == strcasecmp(event, "ATTRIB") )
568  ret = IN_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") )
574  ret = IN_OPEN;
575  else if ( 0 == strcasecmp(event, "MOVED_FROM") )
576  ret = IN_MOVED_FROM;
577  else if ( 0 == strcasecmp(event, "MOVED_TO") )
578  ret = IN_MOVED_TO;
579  else if ( 0 == strcasecmp(event, "CREATE") )
580  ret = IN_CREATE;
581  else if ( 0 == strcasecmp(event, "DELETE") )
582  ret = IN_DELETE;
583  else if ( 0 == strcasecmp(event, "DELETE_SELF") )
584  ret = IN_DELETE_SELF;
585  else if ( 0 == strcasecmp(event, "UNMOUNT") )
586  ret = IN_UNMOUNT;
587  else if ( 0 == strcasecmp(event, "Q_OVERFLOW") )
588  ret = IN_Q_OVERFLOW;
589  else if ( 0 == strcasecmp(event, "IGNORED") )
590  ret = IN_IGNORED;
591  else if ( 0 == strcasecmp(event, "CLOSE") )
592  ret = IN_CLOSE;
593  else if ( 0 == strcasecmp(event, "MOVE_SELF") )
594  ret = IN_MOVE_SELF;
595  else if ( 0 == strcasecmp(event, "MOVE") )
596  ret = IN_MOVE;
597  else if ( 0 == strcasecmp(event, "ISDIR") )
598  ret = IN_ISDIR;
599  else if ( 0 == strcasecmp(event, "ONESHOT") )
600  ret = IN_ONESHOT;
601  else if ( 0 == strcasecmp(event, "ALL_EVENTS") )
602  ret = IN_ALL_EVENTS;
603 
604  return ret;
605 }
606 
628 char * inotifytools_event_to_str(int events) {
629  return inotifytools_event_to_str_sep(events, ',');
630 }
631 
656 char * inotifytools_event_to_str_sep(int events, char sep)
657 {
658  static char ret[1024];
659  ret[0] = '\0';
660  ret[1] = '\0';
661 
662  if ( IN_ACCESS & events ) {
663  strcat( ret, chrtostr(sep) );
664  strcat( ret, "ACCESS" );
665  }
666  if ( IN_MODIFY & events ) {
667  strcat( ret, chrtostr(sep) );
668  strcat( ret, "MODIFY" );
669  }
670  if ( IN_ATTRIB & events ) {
671  strcat( ret, chrtostr(sep) );
672  strcat( ret, "ATTRIB" );
673  }
674  if ( IN_CLOSE_WRITE & events ) {
675  strcat( ret, chrtostr(sep) );
676  strcat( ret, "CLOSE_WRITE" );
677  }
678  if ( IN_CLOSE_NOWRITE & events ) {
679  strcat( ret, chrtostr(sep) );
680  strcat( ret, "CLOSE_NOWRITE" );
681  }
682  if ( IN_OPEN & events ) {
683  strcat( ret, chrtostr(sep) );
684  strcat( ret, "OPEN" );
685  }
686  if ( IN_MOVED_FROM & events ) {
687  strcat( ret, chrtostr(sep) );
688  strcat( ret, "MOVED_FROM" );
689  }
690  if ( IN_MOVED_TO & events ) {
691  strcat( ret, chrtostr(sep) );
692  strcat( ret, "MOVED_TO" );
693  }
694  if ( IN_CREATE & events ) {
695  strcat( ret, chrtostr(sep) );
696  strcat( ret, "CREATE" );
697  }
698  if ( IN_DELETE & events ) {
699  strcat( ret, chrtostr(sep) );
700  strcat( ret, "DELETE" );
701  }
702  if ( IN_DELETE_SELF & events ) {
703  strcat( ret, chrtostr(sep) );
704  strcat( ret, "DELETE_SELF" );
705  }
706  if ( IN_UNMOUNT & events ) {
707  strcat( ret, chrtostr(sep) );
708  strcat( ret, "UNMOUNT" );
709  }
710  if ( IN_Q_OVERFLOW & events ) {
711  strcat( ret, chrtostr(sep) );
712  strcat( ret, "Q_OVERFLOW" );
713  }
714  if ( IN_IGNORED & events ) {
715  strcat( ret, chrtostr(sep) );
716  strcat( ret, "IGNORED" );
717  }
718  if ( IN_CLOSE & events ) {
719  strcat( ret, chrtostr(sep) );
720  strcat( ret, "CLOSE" );
721  }
722  if ( IN_MOVE_SELF & events ) {
723  strcat( ret, chrtostr(sep) );
724  strcat( ret, "MOVE_SELF" );
725  }
726  if ( IN_ISDIR & events ) {
727  strcat( ret, chrtostr(sep) );
728  strcat( ret, "ISDIR" );
729  }
730  if ( IN_ONESHOT & events ) {
731  strcat( ret, chrtostr(sep) );
732  strcat( ret, "ONESHOT" );
733  }
734 
735  // Maybe we didn't match any... ?
736  if (ret[0] == '\0') {
737  niceassert( -1 != sprintf( ret, "%c0x%08x", sep, events ), 0 );
738  }
739 
740  return &ret[1];
741 }
742 
764  niceassert( init, "inotifytools_initialize not called yet" );
765  watch *w = watch_from_wd(wd);
766  if (!w) return 0;
767  return w->filename;
768 }
769 
784 int inotifytools_wd_from_filename( char const * filename ) {
785  niceassert( init, "inotifytools_initialize not called yet" );
786  watch *w = watch_from_filename(filename);
787  if (!w) return -1;
788  return w->wd;
789 }
790 
805 void inotifytools_set_filename_by_wd( int wd, char const * filename ) {
806  niceassert( init, "inotifytools_initialize not called yet" );
807  watch *w = watch_from_wd(wd);
808  if (!w) return;
809  if (w->filename) free(w->filename);
810  w->filename = strdup(filename);
811 }
812 
827 void inotifytools_set_filename_by_filename( char const * oldname,
828  char const * newname ) {
829  watch *w = watch_from_filename(oldname);
830  if (!w) return;
831  if (w->filename) free(w->filename);
832  w->filename = strdup(newname);
833 }
834 
857 void inotifytools_replace_filename( char const * oldname,
858  char const * newname ) {
859  if ( !oldname || !newname ) return;
860  char *names[2+sizeof(int)/sizeof(char*)];
861  names[0] = (char*)oldname;
862  names[1] = (char*)newname;
863  *((int*)&names[2]) = strlen(oldname);
864  rbwalk(tree_filename, replace_filename, (void*)names);
865 }
866 
870 int remove_inotify_watch(watch *w) {
871  error = 0;
872  int status = inotify_rm_watch( inotify_fd, w->wd );
873  if ( status < 0 ) {
874  fprintf(stderr, "Failed to remove watch on %s: %s\n", w->filename,
875  strerror(status) );
876  error = status;
877  return 0;
878  }
879  return 1;
880 }
881 
885 watch *create_watch(int wd, char *filename) {
886  if ( wd <= 0 || !filename) return 0;
887 
888  watch *w = (watch*)calloc(1, sizeof(watch));
889  w->wd = wd;
890  w->filename = strdup(filename);
891  rbsearch(w, tree_wd);
892  rbsearch(w, tree_filename);
893 }
894 
908  niceassert( init, "inotifytools_initialize not called yet" );
909  watch *w = watch_from_wd(wd);
910  if (!w) return 1;
911 
912  if (!remove_inotify_watch(w)) return 0;
913  rbdelete(w, tree_wd);
914  rbdelete(w, tree_filename);
915  destroy_watch(w);
916  return 1;
917 }
918 
930 int inotifytools_remove_watch_by_filename( char const * filename ) {
931  niceassert( init, "inotifytools_initialize not called yet" );
932  watch *w = watch_from_filename(filename);
933  if (!w) return 1;
934 
935  if (!remove_inotify_watch(w)) return 0;
936  rbdelete(w, tree_wd);
937  rbdelete(w, tree_filename);
938  destroy_watch(w);
939  return 1;
940 }
941 
953 int inotifytools_watch_file( char const * filename, int events ) {
954  static char const * filenames[2];
955  filenames[0] = filename;
956  filenames[1] = NULL;
957  return inotifytools_watch_files( filenames, events );
958 }
959 
975 int inotifytools_watch_files( char const * filenames[], int events ) {
976  niceassert( init, "inotifytools_initialize not called yet" );
977  error = 0;
978 
979  static int i;
980  for ( i = 0; filenames[i]; ++i ) {
981  static int wd;
982  wd = inotify_add_watch( inotify_fd, filenames[i], events );
983  if ( wd < 0 ) {
984  if ( wd == -1 ) {
985  error = errno;
986  return 0;
987  } // if ( wd == -1 )
988  else {
989  fprintf( stderr, "Failed to watch %s: returned wd was %d "
990  "(expected -1 or >0 )", filenames[i], wd );
991  // no appropriate value for error
992  return 0;
993  } // else
994  } // if ( wd < 0 )
995 
996  char *filename;
997  // Always end filename with / if it is a directory
998  if ( !isdir(filenames[i])
999  || filenames[i][strlen(filenames[i])-1] == '/') {
1000  filename = strdup(filenames[i]);
1001  }
1002  else {
1003  nasprintf( &filename, "%s/", filenames[i] );
1004  }
1005  create_watch(wd, filename);
1006  free(filename);
1007  } // for
1008 
1009  return 1;
1010 }
1011 
1038 struct inotify_event * inotifytools_next_event( int timeout ) {
1039  return inotifytools_next_events( timeout, 1 );
1040 }
1041 
1042 
1092 struct inotify_event * inotifytools_next_events( int timeout, int num_events ) {
1093  niceassert( init, "inotifytools_initialize not called yet" );
1094  niceassert( num_events <= MAX_EVENTS, "too many events requested" );
1095 
1096  if ( num_events < 1 ) return NULL;
1097 
1098  static struct inotify_event event[MAX_EVENTS];
1099  static struct inotify_event * ret;
1100  static int first_byte = 0;
1101  static ssize_t bytes;
1102  static jmp_buf jmp;
1103  static char match_name[MAX_STRLEN];
1104 
1105 #define RETURN(A) {\
1106  if (regex) {\
1107  inotifytools_snprintf(match_name, MAX_STRLEN, A, "%w%f");\
1108  if (0 == regexec(regex, match_name, 0, 0, 0)) {\
1109  longjmp(jmp,0);\
1110  }\
1111  }\
1112  if ( collect_stats ) {\
1113  record_stats( A );\
1114  }\
1115  return A;\
1116 }
1117 
1118  setjmp(jmp);
1119 
1120  error = 0;
1121 
1122  // first_byte is index into event buffer
1123  if ( first_byte != 0
1124  && first_byte <= (int)(bytes - sizeof(struct inotify_event)) ) {
1125 
1126  ret = (struct inotify_event *)((char *)&event[0] + first_byte);
1127  first_byte += sizeof(struct inotify_event) + ret->len;
1128 
1129  // if the pointer to the next event exactly hits end of bytes read,
1130  // that's good. next time we're called, we'll read.
1131  if ( first_byte == bytes ) {
1132  first_byte = 0;
1133  }
1134  else if ( first_byte > bytes ) {
1135  // oh... no. this can't be happening. An incomplete event.
1136  // Copy what we currently have into first element, call self to
1137  // read remainder.
1138  // oh, and they BETTER NOT overlap.
1139  // Boy I hope this code works.
1140  // But I think this can never happen due to how inotify is written.
1141  niceassert( (long)((char *)&event[0] +
1142  sizeof(struct inotify_event) +
1143  event[0].len) <= (long)ret,
1144  "extremely unlucky user, death imminent" );
1145  // how much of the event do we have?
1146  bytes = (char *)&event[0] + bytes - (char *)ret;
1147  memcpy( &event[0], ret, bytes );
1148  return inotifytools_next_events( timeout, num_events );
1149  }
1150  RETURN(ret);
1151 
1152  }
1153 
1154  else if ( first_byte == 0 ) {
1155  bytes = 0;
1156  }
1157 
1158 
1159  static ssize_t this_bytes;
1160  static unsigned int bytes_to_read;
1161  static int rc;
1162  static fd_set read_fds;
1163 
1164  static struct timeval read_timeout;
1165  read_timeout.tv_sec = timeout;
1166  read_timeout.tv_usec = 0;
1167  static struct timeval * read_timeout_ptr;
1168  read_timeout_ptr = ( timeout <= 0 ? NULL : &read_timeout );
1169 
1170  FD_ZERO(&read_fds);
1171  FD_SET(inotify_fd, &read_fds);
1172  rc = select(inotify_fd + 1, &read_fds,
1173  NULL, NULL, read_timeout_ptr);
1174  if ( rc < 0 ) {
1175  // error
1176  error = errno;
1177  return NULL;
1178  }
1179  else if ( rc == 0 ) {
1180  // timeout
1181  return NULL;
1182  }
1183 
1184  // wait until we have enough bytes to read
1185  do {
1186  rc = ioctl( inotify_fd, FIONREAD, &bytes_to_read );
1187  } while ( !rc &&
1188  bytes_to_read < sizeof(struct inotify_event)*num_events );
1189 
1190  if ( rc == -1 ) {
1191  error = errno;
1192  return NULL;
1193  }
1194 
1195  this_bytes = read(inotify_fd, &event[0] + bytes,
1196  sizeof(struct inotify_event)*MAX_EVENTS - bytes);
1197  if ( this_bytes < 0 ) {
1198  error = errno;
1199  return NULL;
1200  }
1201  if ( this_bytes == 0 ) {
1202  fprintf(stderr, "Inotify reported end-of-file. Possibly too many "
1203  "events occurred at once.\n");
1204  return NULL;
1205  }
1206  bytes += this_bytes;
1207 
1208  ret = &event[0];
1209  first_byte = sizeof(struct inotify_event) + ret->len;
1210  niceassert( first_byte <= bytes, "ridiculously long filename, things will "
1211  "almost certainly screw up." );
1212  if ( first_byte == bytes ) {
1213  first_byte = 0;
1214  }
1215 
1216  RETURN(ret);
1217 
1218 #undef RETURN
1219 }
1220 
1246 int inotifytools_watch_recursively( char const * path, int events ) {
1247  return inotifytools_watch_recursively_with_exclude( path, events, 0 );
1248 }
1249 
1282 int inotifytools_watch_recursively_with_exclude( char const * path, int events,
1283  char const ** exclude_list ) {
1284  niceassert( init, "inotifytools_initialize not called yet" );
1285 
1286  DIR * dir;
1287  char * my_path;
1288  error = 0;
1289  dir = opendir( path );
1290  if ( !dir ) {
1291  // If not a directory, don't need to do anything special
1292  if ( errno == ENOTDIR ) {
1293  return inotifytools_watch_file( path, events );
1294  }
1295  else {
1296  error = errno;
1297  return 0;
1298  }
1299  }
1300 
1301  if ( path[strlen(path)-1] != '/' ) {
1302  nasprintf( &my_path, "%s/", path );
1303  }
1304  else {
1305  my_path = (char *)path;
1306  }
1307 
1308  static struct dirent * ent;
1309  char * next_file;
1310  static struct stat64 my_stat;
1311  ent = readdir( dir );
1312  // Watch each directory within this directory
1313  while ( ent ) {
1314  if ( (0 != strcmp( ent->d_name, "." )) &&
1315  (0 != strcmp( ent->d_name, ".." )) ) {
1316  nasprintf(&next_file,"%s%s", my_path, ent->d_name);
1317  if ( -1 == lstat64( next_file, &my_stat ) ) {
1318  error = errno;
1319  free( next_file );
1320  if ( errno != EACCES ) {
1321  error = errno;
1322  if ( my_path != path ) free( my_path );
1323  closedir( dir );
1324  return 0;
1325  }
1326  }
1327  else if ( S_ISDIR( my_stat.st_mode ) &&
1328  !S_ISLNK( my_stat.st_mode )) {
1329  free( next_file );
1330  nasprintf(&next_file,"%s%s/", my_path, ent->d_name);
1331  static unsigned int no_watch;
1332  static char const ** exclude_entry;
1333 
1334  no_watch = 0;
1335  for (exclude_entry = exclude_list;
1336  exclude_entry && *exclude_entry && !no_watch;
1337  ++exclude_entry) {
1338  static int exclude_length;
1339 
1340  exclude_length = strlen(*exclude_entry);
1341  if ((*exclude_entry)[exclude_length-1] == '/') {
1342  --exclude_length;
1343  }
1344  if ( strlen(next_file) == (unsigned)(exclude_length + 1) &&
1345  !strncmp(*exclude_entry, next_file, exclude_length)) {
1346  // directory found in exclude list
1347  no_watch = 1;
1348  }
1349  }
1350  if (!no_watch) {
1351  static int status;
1353  next_file,
1354  events,
1355  exclude_list );
1356  // For some errors, we will continue.
1357  if ( !status && (EACCES != error) && (ENOENT != error) &&
1358  (ELOOP != error) ) {
1359  free( next_file );
1360  if ( my_path != path ) free( my_path );
1361  closedir( dir );
1362  return 0;
1363  }
1364  } // if !no_watch
1365  free( next_file );
1366  } // if isdir and not islnk
1367  else {
1368  free( next_file );
1369  }
1370  }
1371  ent = readdir( dir );
1372  error = 0;
1373  }
1374 
1375  closedir( dir );
1376 
1377  int ret = inotifytools_watch_file( my_path, events );
1378  if ( my_path != path ) free( my_path );
1379  return ret;
1380 }
1381 
1385 void record_stats( struct inotify_event const * event ) {
1386  if (!event) return;
1387  watch *w = watch_from_wd(event->wd);
1388  if (!w) return;
1389  if ( IN_ACCESS & event->mask ) {
1390  ++w->hit_access;
1391  ++num_access;
1392  }
1393  if ( IN_MODIFY & event->mask ) {
1394  ++w->hit_modify;
1395  ++num_modify;
1396  }
1397  if ( IN_ATTRIB & event->mask ) {
1398  ++w->hit_attrib;
1399  ++num_attrib;
1400  }
1401  if ( IN_CLOSE_WRITE & event->mask ) {
1402  ++w->hit_close_write;
1403  ++num_close_write;
1404  }
1405  if ( IN_CLOSE_NOWRITE & event->mask ) {
1406  ++w->hit_close_nowrite;
1407  ++num_close_nowrite;
1408  }
1409  if ( IN_OPEN & event->mask ) {
1410  ++w->hit_open;
1411  ++num_open;
1412  }
1413  if ( IN_MOVED_FROM & event->mask ) {
1414  ++w->hit_moved_from;
1415  ++num_moved_from;
1416  }
1417  if ( IN_MOVED_TO & event->mask ) {
1418  ++w->hit_moved_to;
1419  ++num_moved_to;
1420  }
1421  if ( IN_CREATE & event->mask ) {
1422  ++w->hit_create;
1423  ++num_create;
1424  }
1425  if ( IN_DELETE & event->mask ) {
1426  ++w->hit_delete;
1427  ++num_delete;
1428  }
1429  if ( IN_DELETE_SELF & event->mask ) {
1430  ++w->hit_delete_self;
1431  ++num_delete_self;
1432  }
1433  if ( IN_UNMOUNT & event->mask ) {
1434  ++w->hit_unmount;
1435  ++num_unmount;
1436  }
1437  if ( IN_MOVE_SELF & event->mask ) {
1438  ++w->hit_move_self;
1439  ++num_move_self;
1440  }
1441 
1442  ++w->hit_total;
1443  ++num_total;
1444 
1445 }
1446 
1447 int *stat_ptr(watch *w, int event)
1448 {
1449  if ( IN_ACCESS == event )
1450  return &w->hit_access;
1451  if ( IN_MODIFY == event )
1452  return &w->hit_modify;
1453  if ( IN_ATTRIB == event )
1454  return &w->hit_attrib;
1455  if ( IN_CLOSE_WRITE == event )
1456  return &w->hit_close_write;
1457  if ( IN_CLOSE_NOWRITE == event )
1458  return &w->hit_close_nowrite;
1459  if ( IN_OPEN == event )
1460  return &w->hit_open;
1461  if ( IN_MOVED_FROM == event )
1462  return &w->hit_moved_from;
1463  if ( IN_MOVED_TO == event )
1464  return &w->hit_moved_to;
1465  if ( IN_CREATE == event )
1466  return &w->hit_create;
1467  if ( IN_DELETE == event )
1468  return &w->hit_delete;
1469  if ( IN_DELETE_SELF == event )
1470  return &w->hit_delete_self;
1471  if ( IN_UNMOUNT == event )
1472  return &w->hit_unmount;
1473  if ( IN_MOVE_SELF == event )
1474  return &w->hit_move_self;
1475  if ( 0 == event )
1476  return &w->hit_total;
1477  return 0;
1478 }
1479 
1495 int inotifytools_get_stat_by_wd( int wd, int event ) {
1496  if (!collect_stats) return -1;
1497 
1498  watch *w = watch_from_wd(wd);
1499  if (!w) return -1;
1500  int *i = stat_ptr(w, event);
1501  if (!i) return -1;
1502  return *i;
1503 }
1504 
1519  if (!collect_stats) return -1;
1520  if ( IN_ACCESS == event )
1521  return num_access;
1522  if ( IN_MODIFY == event )
1523  return num_modify;
1524  if ( IN_ATTRIB == event )
1525  return num_attrib;
1526  if ( IN_CLOSE_WRITE == event )
1527  return num_close_write;
1528  if ( IN_CLOSE_NOWRITE == event )
1529  return num_close_nowrite;
1530  if ( IN_OPEN == event )
1531  return num_open;
1532  if ( IN_MOVED_FROM == event )
1533  return num_moved_from;
1534  if ( IN_MOVED_TO == event )
1535  return num_moved_to;
1536  if ( IN_CREATE == event )
1537  return num_create;
1538  if ( IN_DELETE == event )
1539  return num_delete;
1540  if ( IN_DELETE_SELF == event )
1541  return num_delete_self;
1542  if ( IN_UNMOUNT == event )
1543  return num_unmount;
1544  if ( IN_MOVE_SELF == event )
1545  return num_move_self;
1546 
1547  if ( 0 == event )
1548  return num_total;
1549 
1550  return -1;
1551 }
1552 
1572 int inotifytools_get_stat_by_filename( char const * filename,
1573  int event ) {
1575  filename ), event );
1576 }
1577 
1589  return error;
1590 }
1591 
1595 int isdir( char const * path ) {
1596  static struct stat64 my_stat;
1597 
1598  if ( -1 == lstat64( path, &my_stat ) ) {
1599  if (errno == ENOENT) return 0;
1600  fprintf(stderr, "Stat failed on %s: %s\n", path, strerror(errno));
1601  return 0;
1602  }
1603 
1604  return S_ISDIR( my_stat.st_mode ) && !S_ISLNK( my_stat.st_mode );
1605 }
1606 
1607 
1615  int ret = 0;
1616  rbwalk(tree_filename, get_num, (void*)&ret);
1617  return ret;
1618 }
1619 
1660 int inotifytools_printf( struct inotify_event* event, char* fmt ) {
1661  return inotifytools_fprintf( stdout, event, fmt );
1662 }
1663 
1705 int inotifytools_fprintf( FILE* file, struct inotify_event* event, char* fmt ) {
1706  static char out[MAX_STRLEN+1];
1707  static int ret;
1708  ret = inotifytools_sprintf( out, event, fmt );
1709  if ( -1 != ret ) fprintf( file, "%s", out );
1710  return ret;
1711 }
1712 
1763 int inotifytools_sprintf( char * out, struct inotify_event* event, char* fmt ) {
1764  return inotifytools_snprintf( out, MAX_STRLEN, event, fmt );
1765 }
1766 
1767 
1814 int inotifytools_snprintf( char * out, int size,
1815  struct inotify_event* event, char* fmt ) {
1816  static char * filename, * eventname, * eventstr;
1817  static unsigned int i, ind;
1818  static char ch1;
1819  static char timestr[MAX_STRLEN];
1820  static time_t now;
1821 
1822 
1823  if ( event->len > 0 ) {
1824  eventname = event->name;
1825  }
1826  else {
1827  eventname = NULL;
1828  }
1829 
1830 
1831  filename = inotifytools_filename_from_wd( event->wd );
1832 
1833  if ( !fmt || 0 == strlen(fmt) ) {
1834  error = EINVAL;
1835  return -1;
1836  }
1837  if ( strlen(fmt) > MAX_STRLEN || size > MAX_STRLEN) {
1838  error = EMSGSIZE;
1839  return -1;
1840  }
1841 
1842  ind = 0;
1843  for ( i = 0; i < strlen(fmt) &&
1844  (int)ind < size - 1; ++i ) {
1845  if ( fmt[i] != '%' ) {
1846  out[ind++] = fmt[i];
1847  continue;
1848  }
1849 
1850  if ( i == strlen(fmt) - 1 ) {
1851  // last character is %, invalid
1852  error = EINVAL;
1853  return ind;
1854  }
1855 
1856  ch1 = fmt[i+1];
1857 
1858  if ( ch1 == '%' ) {
1859  out[ind++] = '%';
1860  ++i;
1861  continue;
1862  }
1863 
1864  if ( ch1 == 'w' ) {
1865  if ( filename ) {
1866  strncpy( &out[ind], filename, size - ind );
1867  ind += strlen(filename);
1868  }
1869  ++i;
1870  continue;
1871  }
1872 
1873  if ( ch1 == 'f' ) {
1874  if ( eventname ) {
1875  strncpy( &out[ind], eventname, size - ind );
1876  ind += strlen(eventname);
1877  }
1878  ++i;
1879  continue;
1880  }
1881 
1882  if ( ch1 == 'e' ) {
1883  eventstr = inotifytools_event_to_str( event->mask );
1884  strncpy( &out[ind], eventstr, size - ind );
1885  ind += strlen(eventstr);
1886  ++i;
1887  continue;
1888  }
1889 
1890  if ( ch1 == 'T' ) {
1891 
1892  if ( timefmt ) {
1893 
1894  now = time(0);
1895  if ( 0 >= strftime( timestr, MAX_STRLEN-1, timefmt,
1896  localtime( &now ) ) ) {
1897 
1898  // time format probably invalid
1899  error = EINVAL;
1900  return ind;
1901  }
1902  }
1903  else {
1904  timestr[0] = 0;
1905  }
1906 
1907  strncpy( &out[ind], timestr, size - ind );
1908  ind += strlen(timestr);
1909  ++i;
1910  continue;
1911  }
1912 
1913  // Check if next char in fmt is e
1914  if ( i < strlen(fmt) - 2 && fmt[i+2] == 'e' ) {
1915  eventstr = inotifytools_event_to_str_sep( event->mask, ch1 );
1916  strncpy( &out[ind], eventstr, size - ind );
1917  ind += strlen(eventstr);
1918  i += 2;
1919  continue;
1920  }
1921 
1922  // OK, this wasn't a special format character, just output it as normal
1923  if ( ind < MAX_STRLEN ) out[ind++] = '%';
1924  if ( ind < MAX_STRLEN ) out[ind++] = ch1;
1925  ++i;
1926  }
1927  out[ind] = 0;
1928 
1929  return ind - 1;
1930 }
1931 
1942  timefmt = fmt;
1943 }
1944 
1954  int ret;
1955  if ( !read_num_from_file( QUEUE_SIZE_PATH, &ret ) ) return -1;
1956  return ret;
1957 }
1958 
1969  int ret;
1970  if ( !read_num_from_file( INSTANCES_PATH, &ret ) ) return -1;
1971  return ret;
1972 }
1973 
1984  int ret;
1985  if ( !read_num_from_file( WATCHES_SIZE_PATH, &ret ) ) return -1;
1986  return ret;
1987 }
1988 
2000 int inotifytools_ignore_events_by_regex( char const *pattern, int flags ) {
2001  if (!pattern) {
2002  if (regex) {
2003  regfree(regex);
2004  free(regex);
2005  regex = 0;
2006  }
2007  return 1;
2008  }
2009 
2010  if (regex) { regfree(regex); }
2011  else { regex = (regex_t *)malloc(sizeof(regex_t)); }
2012 
2013  int ret = regcomp(regex, pattern, flags | REG_NOSUB);
2014  if (0 == ret) return 1;
2015 
2016  regfree(regex);
2017  free(regex);
2018  regex = 0;
2019  error = EINVAL;
2020  return 0;
2021 }
2022 
2023 int event_compare(const void *p1, const void *p2, const void *config)
2024 {
2025  if (!p1 || !p2) return p1 - p2;
2026  char asc = 1;
2027  int sort_event = (int)config;
2028  if (sort_event == -1) {
2029  sort_event = 0;
2030  asc = 0;
2031  } else if (sort_event < 0) {
2032  sort_event = -sort_event;
2033  asc = 0;
2034  }
2035  int *i1 = stat_ptr((watch*)p1, sort_event);
2036  int *i2 = stat_ptr((watch*)p2, sort_event);
2037  if (0 == *i1 - *i2) {
2038  return ((watch*)p1)->wd - ((watch*)p2)->wd;
2039  }
2040  if (asc)
2041  return *i1 - *i2;
2042  else
2043  return *i2 - *i1;
2044 }
2045 
2046 struct rbtree *inotifytools_wd_sorted_by_event(int sort_event)
2047 {
2048  struct rbtree *ret = rbinit(event_compare, (void*)sort_event);
2049  RBLIST *all = rbopenlist(tree_wd);
2050  void const *p = rbreadlist(all);
2051  while (p) {
2052  void const *r = rbsearch(p, ret);
2053  niceassert((int)(r == p), "Couldn't insert watch into new tree");
2054  p = rbreadlist(all);
2055  }
2056  rbcloselist(all);
2057  return ret;
2058 }
char * inotifytools_filename_from_wd(int wd)
Definition: inotifytools.c:763
int inotifytools_snprintf(char *out, int size, struct inotify_event *event, char *fmt)
int inotifytools_printf(struct inotify_event *event, char *fmt)
int inotifytools_remove_watch_by_wd(int wd)
Definition: inotifytools.c:907
inotifytools library public interface.
void inotifytools_set_printf_timefmt(char *fmt)
int inotifytools_get_max_user_watches()
int inotifytools_watch_recursively_with_exclude(char const *path, int events, char const **exclude_list)
int inotifytools_sprintf(char *out, struct inotify_event *event, char *fmt)
void inotifytools_initialize_stats()
Definition: inotifytools.c:418
int inotifytools_fprintf(FILE *file, struct inotify_event *event, char *fmt)
struct inotify_event * inotifytools_next_event(int timeout)
char * inotifytools_event_to_str(int events)
Definition: inotifytools.c:628
int inotifytools_get_stat_total(int event)
void inotifytools_set_filename_by_filename(char const *oldname, char const *newname)
Definition: inotifytools.c:827
int inotifytools_wd_from_filename(char const *filename)
Definition: inotifytools.c:784
struct inotify_event * inotifytools_next_events(int timeout, int num_events)
void inotifytools_set_filename_by_wd(int wd, char const *filename)
Definition: inotifytools.c:805
int inotifytools_watch_files(char const *filenames[], int events)
Definition: inotifytools.c:975
int inotifytools_get_num_watches()
int inotifytools_get_stat_by_wd(int wd, int event)
int inotifytools_error()
int inotifytools_remove_watch_by_filename(char const *filename)
Definition: inotifytools.c:930
int inotifytools_watch_file(char const *filename, int events)
Definition: inotifytools.c:953
int inotifytools_get_max_user_instances()
void inotifytools_cleanup()
Definition: inotifytools.c:326
int inotifytools_ignore_events_by_regex(char const *pattern, int flags)
void inotifytools_replace_filename(char const *oldname, char const *newname)
Definition: inotifytools.c:857
int inotifytools_str_to_event(char const *event)
Definition: inotifytools.c:541
int inotifytools_initialize()
Definition: inotifytools.c:281
int inotifytools_get_stat_by_filename(char const *filename, int event)
int inotifytools_str_to_event_sep(char const *event, char sep)
Definition: inotifytools.c:471
int inotifytools_get_max_queued_events()
char * inotifytools_event_to_str_sep(int events, char sep)
Definition: inotifytools.c:656
int inotifytools_watch_recursively(char const *path, int events)