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  return w;
894 }
895 
909  niceassert( init, "inotifytools_initialize not called yet" );
910  watch *w = watch_from_wd(wd);
911  if (!w) return 1;
912 
913  if (!remove_inotify_watch(w)) return 0;
914  rbdelete(w, tree_wd);
915  rbdelete(w, tree_filename);
916  destroy_watch(w);
917  return 1;
918 }
919 
931 int inotifytools_remove_watch_by_filename( char const * filename ) {
932  niceassert( init, "inotifytools_initialize not called yet" );
933  watch *w = watch_from_filename(filename);
934  if (!w) return 1;
935 
936  if (!remove_inotify_watch(w)) return 0;
937  rbdelete(w, tree_wd);
938  rbdelete(w, tree_filename);
939  destroy_watch(w);
940  return 1;
941 }
942 
954 int inotifytools_watch_file( char const * filename, int events ) {
955  static char const * filenames[2];
956  filenames[0] = filename;
957  filenames[1] = NULL;
958  return inotifytools_watch_files( filenames, events );
959 }
960 
976 int inotifytools_watch_files( char const * filenames[], int events ) {
977  niceassert( init, "inotifytools_initialize not called yet" );
978  error = 0;
979 
980  static int i;
981  for ( i = 0; filenames[i]; ++i ) {
982  static int wd;
983  wd = inotify_add_watch( inotify_fd, filenames[i], events );
984  if ( wd < 0 ) {
985  if ( wd == -1 ) {
986  error = errno;
987  return 0;
988  } // if ( wd == -1 )
989  else {
990  fprintf( stderr, "Failed to watch %s: returned wd was %d "
991  "(expected -1 or >0 )", filenames[i], wd );
992  // no appropriate value for error
993  return 0;
994  } // else
995  } // if ( wd < 0 )
996 
997  char *filename;
998  // Always end filename with / if it is a directory
999  if ( !isdir(filenames[i])
1000  || filenames[i][strlen(filenames[i])-1] == '/') {
1001  filename = strdup(filenames[i]);
1002  }
1003  else {
1004  nasprintf( &filename, "%s/", filenames[i] );
1005  }
1006  create_watch(wd, filename);
1007  free(filename);
1008  } // for
1009 
1010  return 1;
1011 }
1012 
1039 struct inotify_event * inotifytools_next_event( int timeout ) {
1040  return inotifytools_next_events( timeout, 1 );
1041 }
1042 
1043 
1093 struct inotify_event * inotifytools_next_events( int timeout, int num_events ) {
1094  niceassert( init, "inotifytools_initialize not called yet" );
1095  niceassert( num_events <= MAX_EVENTS, "too many events requested" );
1096 
1097  if ( num_events < 1 ) return NULL;
1098 
1099  static struct inotify_event event[MAX_EVENTS];
1100  static struct inotify_event * ret;
1101  static int first_byte = 0;
1102  static ssize_t bytes;
1103  static jmp_buf jmp;
1104  static char match_name[MAX_STRLEN];
1105 
1106 #define RETURN(A) {\
1107  if (regex) {\
1108  inotifytools_snprintf(match_name, MAX_STRLEN, A, "%w%f");\
1109  if (0 == regexec(regex, match_name, 0, 0, 0)) {\
1110  longjmp(jmp,0);\
1111  }\
1112  }\
1113  if ( collect_stats ) {\
1114  record_stats( A );\
1115  }\
1116  return A;\
1117 }
1118 
1119  setjmp(jmp);
1120 
1121  error = 0;
1122 
1123  // first_byte is index into event buffer
1124  if ( first_byte != 0
1125  && first_byte <= (int)(bytes - sizeof(struct inotify_event)) ) {
1126 
1127  ret = (struct inotify_event *)((char *)&event[0] + first_byte);
1128  first_byte += sizeof(struct inotify_event) + ret->len;
1129 
1130  // if the pointer to the next event exactly hits end of bytes read,
1131  // that's good. next time we're called, we'll read.
1132  if ( first_byte == bytes ) {
1133  first_byte = 0;
1134  }
1135  else if ( first_byte > bytes ) {
1136  // oh... no. this can't be happening. An incomplete event.
1137  // Copy what we currently have into first element, call self to
1138  // read remainder.
1139  // oh, and they BETTER NOT overlap.
1140  // Boy I hope this code works.
1141  // But I think this can never happen due to how inotify is written.
1142  niceassert( (long)((char *)&event[0] +
1143  sizeof(struct inotify_event) +
1144  event[0].len) <= (long)ret,
1145  "extremely unlucky user, death imminent" );
1146  // how much of the event do we have?
1147  bytes = (char *)&event[0] + bytes - (char *)ret;
1148  memcpy( &event[0], ret, bytes );
1149  return inotifytools_next_events( timeout, num_events );
1150  }
1151  RETURN(ret);
1152 
1153  }
1154 
1155  else if ( first_byte == 0 ) {
1156  bytes = 0;
1157  }
1158 
1159 
1160  static ssize_t this_bytes;
1161  static unsigned int bytes_to_read;
1162  static int rc;
1163  static fd_set read_fds;
1164 
1165  static struct timeval read_timeout;
1166  read_timeout.tv_sec = timeout;
1167  read_timeout.tv_usec = 0;
1168  static struct timeval * read_timeout_ptr;
1169  read_timeout_ptr = ( timeout <= 0 ? NULL : &read_timeout );
1170 
1171  FD_ZERO(&read_fds);
1172  FD_SET(inotify_fd, &read_fds);
1173  rc = select(inotify_fd + 1, &read_fds,
1174  NULL, NULL, read_timeout_ptr);
1175  if ( rc < 0 ) {
1176  // error
1177  error = errno;
1178  return NULL;
1179  }
1180  else if ( rc == 0 ) {
1181  // timeout
1182  return NULL;
1183  }
1184 
1185  // wait until we have enough bytes to read
1186  do {
1187  rc = ioctl( inotify_fd, FIONREAD, &bytes_to_read );
1188  } while ( !rc &&
1189  bytes_to_read < sizeof(struct inotify_event)*num_events );
1190 
1191  if ( rc == -1 ) {
1192  error = errno;
1193  return NULL;
1194  }
1195 
1196  this_bytes = read(inotify_fd, &event[0] + bytes,
1197  sizeof(struct inotify_event)*MAX_EVENTS - bytes);
1198  if ( this_bytes < 0 ) {
1199  error = errno;
1200  return NULL;
1201  }
1202  if ( this_bytes == 0 ) {
1203  fprintf(stderr, "Inotify reported end-of-file. Possibly too many "
1204  "events occurred at once.\n");
1205  return NULL;
1206  }
1207  bytes += this_bytes;
1208 
1209  ret = &event[0];
1210  first_byte = sizeof(struct inotify_event) + ret->len;
1211  niceassert( first_byte <= bytes, "ridiculously long filename, things will "
1212  "almost certainly screw up." );
1213  if ( first_byte == bytes ) {
1214  first_byte = 0;
1215  }
1216 
1217  RETURN(ret);
1218 
1219 #undef RETURN
1220 }
1221 
1247 int inotifytools_watch_recursively( char const * path, int events ) {
1248  return inotifytools_watch_recursively_with_exclude( path, events, 0 );
1249 }
1250 
1283 int inotifytools_watch_recursively_with_exclude( char const * path, int events,
1284  char const ** exclude_list ) {
1285  niceassert( init, "inotifytools_initialize not called yet" );
1286 
1287  DIR * dir;
1288  char * my_path;
1289  error = 0;
1290  dir = opendir( path );
1291  if ( !dir ) {
1292  // If not a directory, don't need to do anything special
1293  if ( errno == ENOTDIR ) {
1294  return inotifytools_watch_file( path, events );
1295  }
1296  else {
1297  error = errno;
1298  return 0;
1299  }
1300  }
1301 
1302  if ( path[strlen(path)-1] != '/' ) {
1303  nasprintf( &my_path, "%s/", path );
1304  }
1305  else {
1306  my_path = (char *)path;
1307  }
1308 
1309  static struct dirent * ent;
1310  char * next_file;
1311  static struct stat64 my_stat;
1312  ent = readdir( dir );
1313  // Watch each directory within this directory
1314  while ( ent ) {
1315  if ( (0 != strcmp( ent->d_name, "." )) &&
1316  (0 != strcmp( ent->d_name, ".." )) ) {
1317  nasprintf(&next_file,"%s%s", my_path, ent->d_name);
1318  if ( -1 == lstat64( next_file, &my_stat ) ) {
1319  error = errno;
1320  free( next_file );
1321  if ( errno != EACCES ) {
1322  error = errno;
1323  if ( my_path != path ) free( my_path );
1324  closedir( dir );
1325  return 0;
1326  }
1327  }
1328  else if ( S_ISDIR( my_stat.st_mode ) &&
1329  !S_ISLNK( my_stat.st_mode )) {
1330  free( next_file );
1331  nasprintf(&next_file,"%s%s/", my_path, ent->d_name);
1332  static unsigned int no_watch;
1333  static char const ** exclude_entry;
1334 
1335  no_watch = 0;
1336  for (exclude_entry = exclude_list;
1337  exclude_entry && *exclude_entry && !no_watch;
1338  ++exclude_entry) {
1339  static int exclude_length;
1340 
1341  exclude_length = strlen(*exclude_entry);
1342  if ((*exclude_entry)[exclude_length-1] == '/') {
1343  --exclude_length;
1344  }
1345  if ( strlen(next_file) == (unsigned)(exclude_length + 1) &&
1346  !strncmp(*exclude_entry, next_file, exclude_length)) {
1347  // directory found in exclude list
1348  no_watch = 1;
1349  }
1350  }
1351  if (!no_watch) {
1352  static int status;
1354  next_file,
1355  events,
1356  exclude_list );
1357  // For some errors, we will continue.
1358  if ( !status && (EACCES != error) && (ENOENT != error) &&
1359  (ELOOP != error) ) {
1360  free( next_file );
1361  if ( my_path != path ) free( my_path );
1362  closedir( dir );
1363  return 0;
1364  }
1365  } // if !no_watch
1366  free( next_file );
1367  } // if isdir and not islnk
1368  else {
1369  free( next_file );
1370  }
1371  }
1372  ent = readdir( dir );
1373  error = 0;
1374  }
1375 
1376  closedir( dir );
1377 
1378  int ret = inotifytools_watch_file( my_path, events );
1379  if ( my_path != path ) free( my_path );
1380  return ret;
1381 }
1382 
1386 void record_stats( struct inotify_event const * event ) {
1387  if (!event) return;
1388  watch *w = watch_from_wd(event->wd);
1389  if (!w) return;
1390  if ( IN_ACCESS & event->mask ) {
1391  ++w->hit_access;
1392  ++num_access;
1393  }
1394  if ( IN_MODIFY & event->mask ) {
1395  ++w->hit_modify;
1396  ++num_modify;
1397  }
1398  if ( IN_ATTRIB & event->mask ) {
1399  ++w->hit_attrib;
1400  ++num_attrib;
1401  }
1402  if ( IN_CLOSE_WRITE & event->mask ) {
1403  ++w->hit_close_write;
1404  ++num_close_write;
1405  }
1406  if ( IN_CLOSE_NOWRITE & event->mask ) {
1407  ++w->hit_close_nowrite;
1408  ++num_close_nowrite;
1409  }
1410  if ( IN_OPEN & event->mask ) {
1411  ++w->hit_open;
1412  ++num_open;
1413  }
1414  if ( IN_MOVED_FROM & event->mask ) {
1415  ++w->hit_moved_from;
1416  ++num_moved_from;
1417  }
1418  if ( IN_MOVED_TO & event->mask ) {
1419  ++w->hit_moved_to;
1420  ++num_moved_to;
1421  }
1422  if ( IN_CREATE & event->mask ) {
1423  ++w->hit_create;
1424  ++num_create;
1425  }
1426  if ( IN_DELETE & event->mask ) {
1427  ++w->hit_delete;
1428  ++num_delete;
1429  }
1430  if ( IN_DELETE_SELF & event->mask ) {
1431  ++w->hit_delete_self;
1432  ++num_delete_self;
1433  }
1434  if ( IN_UNMOUNT & event->mask ) {
1435  ++w->hit_unmount;
1436  ++num_unmount;
1437  }
1438  if ( IN_MOVE_SELF & event->mask ) {
1439  ++w->hit_move_self;
1440  ++num_move_self;
1441  }
1442 
1443  ++w->hit_total;
1444  ++num_total;
1445 
1446 }
1447 
1448 int *stat_ptr(watch *w, int event)
1449 {
1450  if ( IN_ACCESS == event )
1451  return &w->hit_access;
1452  if ( IN_MODIFY == event )
1453  return &w->hit_modify;
1454  if ( IN_ATTRIB == event )
1455  return &w->hit_attrib;
1456  if ( IN_CLOSE_WRITE == event )
1457  return &w->hit_close_write;
1458  if ( IN_CLOSE_NOWRITE == event )
1459  return &w->hit_close_nowrite;
1460  if ( IN_OPEN == event )
1461  return &w->hit_open;
1462  if ( IN_MOVED_FROM == event )
1463  return &w->hit_moved_from;
1464  if ( IN_MOVED_TO == event )
1465  return &w->hit_moved_to;
1466  if ( IN_CREATE == event )
1467  return &w->hit_create;
1468  if ( IN_DELETE == event )
1469  return &w->hit_delete;
1470  if ( IN_DELETE_SELF == event )
1471  return &w->hit_delete_self;
1472  if ( IN_UNMOUNT == event )
1473  return &w->hit_unmount;
1474  if ( IN_MOVE_SELF == event )
1475  return &w->hit_move_self;
1476  if ( 0 == event )
1477  return &w->hit_total;
1478  return 0;
1479 }
1480 
1496 int inotifytools_get_stat_by_wd( int wd, int event ) {
1497  if (!collect_stats) return -1;
1498 
1499  watch *w = watch_from_wd(wd);
1500  if (!w) return -1;
1501  int *i = stat_ptr(w, event);
1502  if (!i) return -1;
1503  return *i;
1504 }
1505 
1520  if (!collect_stats) return -1;
1521  if ( IN_ACCESS == event )
1522  return num_access;
1523  if ( IN_MODIFY == event )
1524  return num_modify;
1525  if ( IN_ATTRIB == event )
1526  return num_attrib;
1527  if ( IN_CLOSE_WRITE == event )
1528  return num_close_write;
1529  if ( IN_CLOSE_NOWRITE == event )
1530  return num_close_nowrite;
1531  if ( IN_OPEN == event )
1532  return num_open;
1533  if ( IN_MOVED_FROM == event )
1534  return num_moved_from;
1535  if ( IN_MOVED_TO == event )
1536  return num_moved_to;
1537  if ( IN_CREATE == event )
1538  return num_create;
1539  if ( IN_DELETE == event )
1540  return num_delete;
1541  if ( IN_DELETE_SELF == event )
1542  return num_delete_self;
1543  if ( IN_UNMOUNT == event )
1544  return num_unmount;
1545  if ( IN_MOVE_SELF == event )
1546  return num_move_self;
1547 
1548  if ( 0 == event )
1549  return num_total;
1550 
1551  return -1;
1552 }
1553 
1573 int inotifytools_get_stat_by_filename( char const * filename,
1574  int event ) {
1576  filename ), event );
1577 }
1578 
1590  return error;
1591 }
1592 
1596 int isdir( char const * path ) {
1597  static struct stat64 my_stat;
1598 
1599  if ( -1 == lstat64( path, &my_stat ) ) {
1600  if (errno == ENOENT) return 0;
1601  fprintf(stderr, "Stat failed on %s: %s\n", path, strerror(errno));
1602  return 0;
1603  }
1604 
1605  return S_ISDIR( my_stat.st_mode ) && !S_ISLNK( my_stat.st_mode );
1606 }
1607 
1608 
1616  int ret = 0;
1617  rbwalk(tree_filename, get_num, (void*)&ret);
1618  return ret;
1619 }
1620 
1661 int inotifytools_printf( struct inotify_event* event, char* fmt ) {
1662  return inotifytools_fprintf( stdout, event, fmt );
1663 }
1664 
1706 int inotifytools_fprintf( FILE* file, struct inotify_event* event, char* fmt ) {
1707  static char out[MAX_STRLEN+1];
1708  static int ret;
1709  ret = inotifytools_sprintf( out, event, fmt );
1710  if ( -1 != ret ) fprintf( file, "%s", out );
1711  return ret;
1712 }
1713 
1764 int inotifytools_sprintf( char * out, struct inotify_event* event, char* fmt ) {
1765  return inotifytools_snprintf( out, MAX_STRLEN, event, fmt );
1766 }
1767 
1768 
1815 int inotifytools_snprintf( char * out, int size,
1816  struct inotify_event* event, char* fmt ) {
1817  static char * filename, * eventname, * eventstr;
1818  static unsigned int i, ind;
1819  static char ch1;
1820  static char timestr[MAX_STRLEN];
1821  static time_t now;
1822 
1823 
1824  if ( event->len > 0 ) {
1825  eventname = event->name;
1826  }
1827  else {
1828  eventname = NULL;
1829  }
1830 
1831 
1832  filename = inotifytools_filename_from_wd( event->wd );
1833 
1834  if ( !fmt || 0 == strlen(fmt) ) {
1835  error = EINVAL;
1836  return -1;
1837  }
1838  if ( strlen(fmt) > MAX_STRLEN || size > MAX_STRLEN) {
1839  error = EMSGSIZE;
1840  return -1;
1841  }
1842 
1843  ind = 0;
1844  for ( i = 0; i < strlen(fmt) &&
1845  (int)ind < size - 1; ++i ) {
1846  if ( fmt[i] != '%' ) {
1847  out[ind++] = fmt[i];
1848  continue;
1849  }
1850 
1851  if ( i == strlen(fmt) - 1 ) {
1852  // last character is %, invalid
1853  error = EINVAL;
1854  return ind;
1855  }
1856 
1857  ch1 = fmt[i+1];
1858 
1859  if ( ch1 == '%' ) {
1860  out[ind++] = '%';
1861  ++i;
1862  continue;
1863  }
1864 
1865  if ( ch1 == 'w' ) {
1866  if ( filename ) {
1867  strncpy( &out[ind], filename, size - ind );
1868  ind += strlen(filename);
1869  }
1870  ++i;
1871  continue;
1872  }
1873 
1874  if ( ch1 == 'f' ) {
1875  if ( eventname ) {
1876  strncpy( &out[ind], eventname, size - ind );
1877  ind += strlen(eventname);
1878  }
1879  ++i;
1880  continue;
1881  }
1882 
1883  if ( ch1 == 'e' ) {
1884  eventstr = inotifytools_event_to_str( event->mask );
1885  strncpy( &out[ind], eventstr, size - ind );
1886  ind += strlen(eventstr);
1887  ++i;
1888  continue;
1889  }
1890 
1891  if ( ch1 == 'T' ) {
1892 
1893  if ( timefmt ) {
1894 
1895  now = time(0);
1896  if ( 0 >= strftime( timestr, MAX_STRLEN-1, timefmt,
1897  localtime( &now ) ) ) {
1898 
1899  // time format probably invalid
1900  error = EINVAL;
1901  return ind;
1902  }
1903  }
1904  else {
1905  timestr[0] = 0;
1906  }
1907 
1908  strncpy( &out[ind], timestr, size - ind );
1909  ind += strlen(timestr);
1910  ++i;
1911  continue;
1912  }
1913 
1914  // Check if next char in fmt is e
1915  if ( i < strlen(fmt) - 2 && fmt[i+2] == 'e' ) {
1916  eventstr = inotifytools_event_to_str_sep( event->mask, ch1 );
1917  strncpy( &out[ind], eventstr, size - ind );
1918  ind += strlen(eventstr);
1919  i += 2;
1920  continue;
1921  }
1922 
1923  // OK, this wasn't a special format character, just output it as normal
1924  if ( ind < MAX_STRLEN ) out[ind++] = '%';
1925  if ( ind < MAX_STRLEN ) out[ind++] = ch1;
1926  ++i;
1927  }
1928  out[ind] = 0;
1929 
1930  return ind - 1;
1931 }
1932 
1943  timefmt = fmt;
1944 }
1945 
1955  int ret;
1956  if ( !read_num_from_file( QUEUE_SIZE_PATH, &ret ) ) return -1;
1957  return ret;
1958 }
1959 
1970  int ret;
1971  if ( !read_num_from_file( INSTANCES_PATH, &ret ) ) return -1;
1972  return ret;
1973 }
1974 
1985  int ret;
1986  if ( !read_num_from_file( WATCHES_SIZE_PATH, &ret ) ) return -1;
1987  return ret;
1988 }
1989 
2001 int inotifytools_ignore_events_by_regex( char const *pattern, int flags ) {
2002  if (!pattern) {
2003  if (regex) {
2004  regfree(regex);
2005  free(regex);
2006  regex = 0;
2007  }
2008  return 1;
2009  }
2010 
2011  if (regex) { regfree(regex); }
2012  else { regex = (regex_t *)malloc(sizeof(regex_t)); }
2013 
2014  int ret = regcomp(regex, pattern, flags | REG_NOSUB);
2015  if (0 == ret) return 1;
2016 
2017  regfree(regex);
2018  free(regex);
2019  regex = 0;
2020  error = EINVAL;
2021  return 0;
2022 }
2023 
2024 int event_compare(const void *p1, const void *p2, const void *config)
2025 {
2026  if (!p1 || !p2) return p1 - p2;
2027  char asc = 1;
2028  int sort_event = (int)config;
2029  if (sort_event == -1) {
2030  sort_event = 0;
2031  asc = 0;
2032  } else if (sort_event < 0) {
2033  sort_event = -sort_event;
2034  asc = 0;
2035  }
2036  int *i1 = stat_ptr((watch*)p1, sort_event);
2037  int *i2 = stat_ptr((watch*)p2, sort_event);
2038  if (0 == *i1 - *i2) {
2039  return ((watch*)p1)->wd - ((watch*)p2)->wd;
2040  }
2041  if (asc)
2042  return *i1 - *i2;
2043  else
2044  return *i2 - *i1;
2045 }
2046 
2047 struct rbtree *inotifytools_wd_sorted_by_event(int sort_event)
2048 {
2049  struct rbtree *ret = rbinit(event_compare, (void*)sort_event);
2050  RBLIST *all = rbopenlist(tree_wd);
2051  void const *p = rbreadlist(all);
2052  while (p) {
2053  void const *r = rbsearch(p, ret);
2054  niceassert((int)(r == p), "Couldn't insert watch into new tree");
2055  p = rbreadlist(all);
2056  }
2057  rbcloselist(all);
2058  return ret;
2059 }
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:908
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:976
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:931
int inotifytools_watch_file(char const *filename, int events)
Definition: inotifytools.c:954
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)