Esempio di inotify, e qualche correzione sui sysctl dei socket
[gapil.git] / sources / inotify_monitor.c
index d2c4f2dd543b684d340442327ff94297349782f9..92391cf39b5133c8ab49435f6c76a1878f0f8dee 100644 (file)
 
 /* Help printing routine */
 void usage(void);
+void printevent(unsigned int mask);
 
 int main(int argc, char *argv[]) 
 {
-    int i;
-    int fd;
+    int i, size;
+    int fd, wd;
+    char buffer[128*sizeof(struct inotify_event)];
     unsigned int mask=0;
+    struct inotify_event * event;
     /*
      * Input section: decode command line parameters 
      * Use getopt function
@@ -55,30 +58,28 @@ int main(int argc, char *argv[])
        /* 
         * Handling options 
         */ 
-       case 'h':                                             /* help option */
+       case 'h':       /* help option */
            printf("Wrong -h option use\n");
            usage();
-           return -1;
-           break;
-       case 'r':                                             /* read access */
+       case 'r':       /* read access */
            mask |= IN_ACCESS;
            break;
-       case 'w':                                            /* write access */
+       case 'w':       /* write access */
            mask |= IN_MODIFY;
            break;
-       case 'c':                                                /* creation */
+       case 'c':       /* creation */
            mask |= IN_CREATE;
            break;
-       case 'd':                                                /* deletion */
+       case 'd':       /* deletion */
            mask |= IN_DELETE;
            break;
-       case 'a':                                              /* all events */
+       case 'a':       /* all events */
            mask |= IN_ALL_EVENTS;
            break;
-       case '?':                                    /* unrecognized options */
+       case '?':       /* unrecognized options */
            printf("Unrecognized options -%c\n",optopt);
            usage();
-       default:                                       /* should not reached */
+       default:        /* should not reached */
            usage();
        }
     }
@@ -89,22 +90,40 @@ int main(int argc, char *argv[])
      *               Main code beginning
      * 
      * ***********************************************************/
-    if ((argc - optind) != 1) {          /* There must be remaing parameters */
+    /* There must be one argument */
+    if ((argc - optind) != 1) {  
        printf("Wrong number of arguments %d\n", argc - optind);
         usage();
     }
-    /* initalialize */
+    /* initalialize inotify */
     fd = inotify_init ();
     if (fd < 0)
         perror("Failing on inotify_init");
 
     /* add watch */
-    if (inotify_add_watch(fd, argv[1], mask) != 0) {
-       printf("Failing to add watched file %s; %s\n", 
-             argv[1], strerror(errno));
+    wd = inotify_add_watch(fd, argv[optind], mask);
+    if ( wd <= 0) {
+       printf("Failing to add watched file %s, mask %i; %s\n", 
+             argv[optind], mask, strerror(errno));
        exit(-1);
+    }    
+    /* 
+     * Main Loop: read events and print them
+     */
+    while (1) {
+       size = read(fd, buffer, sizeof(buffer));
+       
+       event = (struct inotify_event *) buffer;
+        if (wd != event->wd) {
+           printf("Error, getting different watch descriptor, %i and %i\n",
+                  wd, event->wd); 
+           exit(1);
+       }
+       printf("Observed event on %s\n", argv[optind-1+event->wd]);
+       if (event->name != NULL)
+           printf("On file %s\n", event->name);
+       printevent(event->mask);
     }
-
     return 0;
 
 }
@@ -114,7 +133,7 @@ int main(int argc, char *argv[])
 void usage(void) {
     printf("Program inotify_monitor: monitor file changes \n");
     printf("Usage:\n");
-    printf(" inotify_monitor [-h] -rwcd dirname/filename \n");
+    printf(" inotify_monitor [-h] -rwcd dirname/filename dir/file ... \n");
     printf("  -h          print this help\n");
     printf("  -w          watch write\n");
     printf("  -r          watch read\n");
@@ -123,3 +142,34 @@ void usage(void) {
     printf("  -a          watch all\n");
     exit(1);
 }
+
+void printevent(unsigned int mask) {
+    int i;
+    int val;
+    char * inotify_event[] = {
+       "IN_ACCESS", 
+       "IN_MODIFY", 
+       "IN_ATTRIB", 
+       "IN_CLOSE_WRITE", 
+       "IN_CLOSE_NOWRITE", 
+       "IN_OPEN", 
+       "IN_MOVED_FROM", 
+       "IN_MOVED_TO",
+       "IN_CREATE", 
+       "IN_DELETE", 
+       "IN_DELETE_SELF", 
+       "IN_MOVE_SELF", 
+       "ERROR!!!", 
+       "IN_UNMOUNT", 
+       "IN_Q_OVERFLOW", 
+       "IN_IGNORED"
+    };
+    
+    val=1;
+    for (i=0; i<16; i++) {
+       if (mask & val) 
+           printf("%s, ", inotify_event[i]); 
+       val = val << 1;
+    }
+    printf("\n");
+}