Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. /* Using Inotify to monitor the sub-dirs under the specifiied dir*/
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <sys/types.h>
  8. #include <sys/inotify.h>
  9. #include <dirent.h>
  10. #include <limits.h>
  11.  
  12. #define MAX_LEN 1024 /*Path length for a directory*/
  13. #define MAX_EVENTS 1024 /*Max. number of events to process at one go*/
  14. #define LEN_NAME 16 /*Assuming that the length of the filename won't exceed 16 bytes*/
  15. #define EVENT_SIZE  ( sizeof (struct inotify_event) ) /*size of one event*/
  16. #define BUF_LEN     ( MAX_EVENTS * ( EVENT_SIZE + LEN_NAME )) /*buffer to store the data of events*/
  17.  
  18.  
  19. /* Log file*/
  20. FILE *fp_log;
  21.   
  22.  
  23. /* Add inotify watches to directories immediately under root
  24.  * in addition to itself */
  25.  
  26. void add_watches(int fd, char *root)
  27. {
  28.   int wd;
  29.   char *abs_dir;
  30.   struct dirent *entry;
  31.   DIR *dp;
  32.  
  33.   dp = opendir(root);
  34.   if (dp == NULL)
  35.     {
  36.       perror("Error opening the starting directory");
  37.       exit(0);
  38.     }
  39.  
  40.   /* add watch to starting directory */
  41.   wd = inotify_add_watch(fd, root, IN_CREATE | IN_MODIFY | IN_DELETE);
  42.   if (wd == -1)
  43.     {
  44.       fprintf(fp_log,"Couldn't add watch to %s\n",root);
  45.     }
  46.   else
  47.     {
  48.       printf("Watching:: %s\n",root);
  49.     }
  50.  
  51.   /* Add watches to the Level 1 sub-dirs*/
  52.   abs_dir = (char *)malloc(MAX_LEN);
  53.   while((entry = readdir(dp)))
  54.     {
  55.       /* if its a directory, add a watch*/
  56.       if (entry->d_type == DT_DIR)
  57.         {
  58.           strcpy(abs_dir,root);
  59.           strcat(abs_dir,entry->d_name);
  60.            
  61.           wd = inotify_add_watch(fd, abs_dir, IN_CREATE | IN_MODIFY | IN_DELETE);
  62.           if (wd == -1)
  63.               printf("Couldn't add watch to the directory %s\n",abs_dir);
  64.           else
  65.             printf("Watching:: %s\n",abs_dir);
  66.         }
  67.     }
  68.    
  69.   closedir(dp);
  70.   free(abs_dir);
  71. }
  72.  
  73. /* Main routine*/
  74. int main( int argc, char **argv )
  75. {
  76.   int length, i = 0;
  77.   int fd;
  78.   char buffer[BUF_LEN], root[MAX_LEN];
  79.  
  80.  
  81.   /*Check for supplied path to monitor*/
  82.   switch(argc)
  83.     {
  84.     case 1: printf("No directory specified. Will monitor the entire filesystem...\n\n");
  85.       strcpy(root,"/");
  86.       break;
  87.        
  88.     case 2: strcpy(root,argv[1]);
  89.       if(root[strlen(root)-1]!='/')
  90.         strcat(root,"/");
  91.       puts(root);
  92.  
  93.       break;
  94.        
  95.     default: printf("Ignoring all other arguments after the first\n");
  96.     }
  97.    
  98.  
  99.   /* Set up logger*/
  100.   fp_log = fopen("inotify_logger.log","a");
  101.   if (fp_log == NULL)
  102.     {
  103.       printf("Error opening logger. All output will be redirected to the stdout\n");
  104.       fp_log = stdout;
  105.     }
  106.  
  107.   fd = inotify_init();
  108.   if ( fd < 0 ) {
  109.     perror( "Couldn't initialize inotify");
  110.   }
  111.  
  112.   /* Read the sub-directories at one level under argv[1]
  113.    * and monitor them for access */
  114.   add_watches(fd,root);
  115.    
  116.   /* do it forever*/
  117.   while(1)
  118.     {
  119.       i = 0;
  120.       length = read( fd, buffer, BUF_LEN ); 
  121.  
  122.       if ( length < 0 ) {
  123.         perror( "read" );
  124.       } 
  125.  
  126.       /* Read the events*/
  127.       while ( i < length ) {
  128.         struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ];
  129.         if ( event->len ) {
  130.           if ( event->mask & IN_CREATE) {
  131.             if (event->mask & IN_ISDIR)
  132.               fprintf(fp_log,"%d DIR::%s CREATED\n", event->wd,event->name );      
  133.             else
  134.               fprintf(fp_log, "%d FILE::%s CREATED\n", event->wd, event->name);      
  135.           }
  136.            
  137.           if ( event->mask & IN_MODIFY) {
  138.             if (event->mask & IN_ISDIR)
  139.               fprintf(fp_log,"%d DIR::%s MODIFIED\n", event->wd,event->name );      
  140.             else
  141.               fprintf(fp_log,"%d FILE::%s MODIFIED\n", event->wd,event->name );      
  142.  
  143.           }
  144.            
  145.           if ( event->mask & IN_DELETE) {
  146.             if (event->mask & IN_ISDIR)
  147.               fprintf(fp_log,"%d DIR::%s DELETED\n", event->wd,event->name );      
  148.             else
  149.               fprintf(fp_log,"%d FILE::%s DELETED\n", event->wd,event->name );      
  150.           } 
  151.  
  152.           i += EVENT_SIZE + event->len;
  153.         }
  154.       }
  155.     }
  156.   /* Clean up*/
  157.   ( void ) close( fd );
  158.    
  159.   return 0;
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement