c++ - inotify watcher stops working after ls or watch commands -


i took code below many examples on internet how use inotify.

i tried following experiment:
1) run watcher below
2) in separate shell, cd '/mypath' create files folder watching. example, 'date > output.txt' 1 ore more times.
3) see notifications watcher.
4) type 'ls /mypath' (or 'watch -n 1 /mypath')
5) try 'date > output.txt' in /mypath. no longer see notifications watcher. or @ least, happened when tested ubuntu 12/13.

any ideas how fix it?

#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <sys/types.h> #include <sys/inotify.h> #include <limits.h> #include <unistd.h>   #define max_events 1024 /*max. number of events process @ 1 go*/ #define len_name 16 /*assuming length of filename won't exceed 16 bytes*/ #define event_size  ( sizeof (struct inotify_event) ) /*size of 1 event*/ #define buf_len     ( max_events * ( event_size + len_name )) /*buffer store data of events*/  int main()  {   int length, = 0, wd;   int fd;   char buffer[buf_len];    /* initialize inotify*/    fd = inotify_init();   if ( fd < 0 ) {     perror( "couldn't initialize inotify");   }    /* add watch starting directory */   wd = inotify_add_watch(fd, "/mypath", in_close_write | in_close_nowrite);     if (wd == -1)     {       printf("couldn't add watch %s\n","/mypath");     }   else     {       printf("watching:: %s\n","/mypath");     }    /* forever*/   while(1)     {         = 0;       length = read( fd, buffer, buf_len );           if ( length < 0 ) {         perror( "read" );       }          while ( < length ) {         struct inotify_event *event = ( struct inotify_event * ) &buffer[ ];         if ( event->len ) {           if ( event->mask & in_close_write) {             if (event->mask & in_isdir)               printf( "the directory %s created.\n", event->name );                    else               printf( "the file %s closed (write) wd %d\n", event->name, event->wd );                  }           if ( event->mask & in_close_nowrite) {             if (event->mask & in_isdir)               printf( "the directory %s created.\n", event->name );                    else               printf( "the file %s closed (nowrite) wd %d\n", event->name, event->wd );                  }              += event_size + event->len;         }       }          }    /* clean up*/   inotify_rm_watch( fd, wd );   close( fd );    return 0; } 

you should not put i += event_size + event->len; inside if ( event->len ) block. if event has zero-length name, pointer should still incremented event_size (which happen if put statement outside block). think might seeing infinite loop in inotify program, kicked off first event happens have zero-length name. (which happens ls: directory being opened, not files, there's nothing in name field.)


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -