Correzioni estetiche al codice con maggiori controlli sugli errori.
authorSimone Piccardi <piccardi@gnulinux.it>
Sun, 29 Jul 2007 15:41:51 +0000 (15:41 +0000)
committerSimone Piccardi <piccardi@gnulinux.it>
Sun, 29 Jul 2007 15:41:51 +0000 (15:41 +0000)
sources/inotify_monitor.c

index 1acfdb0c65d58b0fa99961ce42b286730958f50c..e5e04d9aaf369543a59de9093971197fccb5b563 100644 (file)
  */
 /*****************************************************************************
  *
  */
 /*****************************************************************************
  *
- * File inotufy_monitor.c: 
+ * File inotify_monitor.c: 
  *
  *
- * An example for shared memory use: monitor a directory status,
- * saving data in a shared memory segment
+ * An example of the inotify interface: use inotify to watch the
+ * status of a directory or a file
  *
  * Author: S. Piccardi Jul. 2007
  *
  *
  * Author: S. Piccardi Jul. 2007
  *
@@ -36,7 +36,7 @@
 #include <stdio.h>       /* standard I/O library */
 #include <string.h>      /* string functions */
 #include <fcntl.h>       /* fcntl function */
 #include <stdio.h>       /* standard I/O library */
 #include <string.h>      /* string functions */
 #include <fcntl.h>       /* fcntl function */
-#include <sys/ioctl.h>       /* fcntl function */
+#include <sys/ioctl.h>   /* ioctl function */
 
 
 #include "macros.h"
 
 
 #include "macros.h"
@@ -58,7 +58,7 @@ int main(int argc, char *argv[])
      * Use getopt function
      */
     opterr = 0;         /* don't want writing to stderr */
      * Use getopt function
      */
     opterr = 0;         /* don't want writing to stderr */
-    while ( (i = getopt(argc, argv, "hrwcda")) != -1) {
+    while ((i = getopt(argc, argv, "hrwcda")) != -1) {
        switch (i) {
        /* 
         * Handling options 
        switch (i) {
        /* 
         * Handling options 
@@ -95,54 +95,55 @@ int main(int argc, char *argv[])
      *               Main code beginning
      * 
      * ***********************************************************/
      *               Main code beginning
      * 
      * ***********************************************************/
-    /* There must be one argument */
-    if ((argc - optind) != 1) {  
+    if ((argc - optind) != 1) {           /* There must be one argument */
        printf("Wrong number of arguments %d\n", argc - optind);
         usage();
     }
        printf("Wrong number of arguments %d\n", argc - optind);
         usage();
     }
-    /* initializa epoll */
-    epfd = epoll_create(5);
-    if (epfd < 0)
+    epfd = epoll_create(5);               /* initialize epoll */
+    if (epfd < 0) {
         perror("Failing on epoll_create");
         perror("Failing on epoll_create");
-
-    /* initalialize inotify */
-    fd = inotify_init();
-    if (fd < 0)
+       exit(-1);
+    }
+    fd = inotify_init();                  /* initialize inotify */
+    if (fd < 0) {
         perror("Failing on inotify_init");
         perror("Failing on inotify_init");
-    if (fcntl(fd, F_SETFL, O_NONBLOCK)) 
+       exit(-1);
+    }
+    if (fcntl(fd, F_SETFL, O_NONBLOCK)) { /* no blocking I/O on inotify */
        perror("Cannot set noblocking I/O on inotify fd");
        perror("Cannot set noblocking I/O on inotify fd");
-    
-    /* add watch */
-    wd = inotify_add_watch(fd, argv[optind], mask);
-    if ( wd <= 0) {
+       exit(-1);
+    }
+    wd = inotify_add_watch(fd, argv[optind], mask);  /* add watch */
+    if (wd <= 0) {
        printf("Failing to add watched file %s, mask %i; %s\n", 
              argv[optind], mask, strerror(errno));
        exit(-1);
     }
        printf("Failing to add watched file %s, mask %i; %s\n", 
              argv[optind], mask, strerror(errno));
        exit(-1);
     }
-    
-    /* add inotify fd to epoll */
-    epev.data.fd = fd;
+    epev.data.fd = fd;        /* add inotify fd to epoll */
     epev.events = EPOLLIN;
     epev.events = EPOLLIN;
-    if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &epev))
+    if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &epev)) {
         perror("Failing on epoll_ctl");
         perror("Failing on epoll_ctl");
-
+       exit(-1);
+    }
     /* 
      * Main Loop: read events and print them
      */
     while (1) {
     /* 
      * Main Loop: read events and print them
      */
     while (1) {
-       if (epoll_wait(epfd, &epev, 1, -1) < 0 )
+       if (epoll_wait(epfd, &epev, 1, -1) < 0) {
            perror("error on epoll_wait");
            perror("error on epoll_wait");
-       
+           exit(-1);
+       }
        if (epev.data.fd != fd) 
            printf("something wrong, epoll activity on %i instead of %i\n",
                   epev.data.fd, fd);
        
        if (epev.data.fd != fd) 
            printf("something wrong, epoll activity on %i instead of %i\n",
                   epev.data.fd, fd);
        
-       if (ioctl(fd, FIONREAD, &size))
+       if (ioctl(fd, FIONREAD, &size)) {
            perror("error on getting inotify event size");
            perror("error on getting inotify event size");
-
+           exit(-1);
+       }
        if (size > sizeof(buffer)) {
            printf("Too many %i data to read, something wrong\n", size);
        if (size > sizeof(buffer)) {
            printf("Too many %i data to read, something wrong\n", size);
-           exit(1);
+           exit(-1);
        }
        i = 0;
        while (i < size) {
        }
        i = 0;
        while (i < size) {
@@ -152,21 +153,19 @@ int main(int argc, char *argv[])
                exit(1);
            }
            i += nread; 
                exit(1);
            }
            i += nread; 
-
            event = (struct inotify_event *) buffer;
            if (wd != event->wd) {
                printf("Getting different watch descriptor, %i and %i\n",
                       wd, event->wd); 
            event = (struct inotify_event *) buffer;
            if (wd != event->wd) {
                printf("Getting different watch descriptor, %i and %i\n",
                       wd, event->wd); 
-               exit(1);
+           } else {
+               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);
            }
            }
-           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;
        }
     }
     return 0;
-
 }
 /*
  * routine to print usage info and exit
 }
 /*
  * routine to print usage info and exit