X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2Finotify_monitor.c;h=60fa1035f8d5e4b2af625e92277c4648e3ecd8a1;hp=e5e04d9aaf369543a59de9093971197fccb5b563;hb=26f7a8bb19c6cb198c213757a97b6ac79e40db4b;hpb=05e740c9a3031a4359e9a895b0fb3d582b497d2f diff --git a/sources/inotify_monitor.c b/sources/inotify_monitor.c index e5e04d9..60fa103 100644 --- a/sources/inotify_monitor.c +++ b/sources/inotify_monitor.c @@ -26,17 +26,16 @@ * Author: S. Piccardi Jul. 2007 * *****************************************************************************/ -#include -#include +#include /* primitive system data types */ +#include /* file characteristics constants and functions */ #include /* Linux inotify interface */ -#include /* Linux epoll interface */ #include /* C standard library */ -#include /* Unix standard library */ +#include /* unix standard library */ #include /* error definitions and routines */ #include /* standard I/O library */ -#include /* string functions */ -#include /* fcntl function */ -#include /* ioctl function */ +#include /* C strings library */ +#include /* file control functions */ +#include /* ioctl syscall and constants */ #include "macros.h" @@ -47,18 +46,17 @@ void printevent(unsigned int mask); int main(int argc, char *argv[]) { - int i, size, nread; - int fd, wd, epfd; - char buffer[1024 + sizeof(struct inotify_event)]; + int i, narg, nread; + int fd, wd; + char buffer[512 * (sizeof(struct inotify_event) + 16)]; unsigned int mask=0; struct inotify_event * event; - struct epoll_event epev; /* * Input section: decode command line parameters * Use getopt function */ opterr = 0; /* don't want writing to stderr */ - while ((i = getopt(argc, argv, "hrwcda")) != -1) { + while ((i = getopt(argc, argv, "hrwcdaCM")) != -1) { switch (i) { /* * Handling options @@ -81,6 +79,12 @@ int main(int argc, char *argv[]) case 'a': /* all events */ mask |= IN_ALL_EVENTS; break; + case 'C': /* creation */ + mask |= IN_CLOSE; + break; + case 'M': /* creation */ + mask |= IN_MOVE; + break; case '?': /* unrecognized options */ printf("Unrecognized options -%c\n",optopt); usage(); @@ -88,6 +92,10 @@ int main(int argc, char *argv[]) usage(); } } + if (mask == 0) { + printf("No events to monitor\n"); + usage(); + } /* *********************************************************** * * Options processing completed @@ -95,73 +103,49 @@ int main(int argc, char *argv[]) * Main code beginning * * ***********************************************************/ - if ((argc - optind) != 1) { /* There must be one argument */ + narg = argc - optind; + if (narg < 1) { /* There must be at least one argument */ printf("Wrong number of arguments %d\n", argc - optind); usage(); } - epfd = epoll_create(5); /* initialize epoll */ - if (epfd < 0) { - perror("Failing on epoll_create"); - exit(-1); - } - fd = inotify_init(); /* initialize inotify */ + fd = inotify_init(); /* initialize inotify */ if (fd < 0) { - perror("Failing on inotify_init"); + perror("Failing on inotify_init"); exit(-1); } - if (fcntl(fd, F_SETFL, O_NONBLOCK)) { /* no blocking I/O on inotify */ - perror("Cannot set noblocking I/O on inotify fd"); - 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); - } - epev.data.fd = fd; /* add inotify fd to epoll */ - epev.events = EPOLLIN; - if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &epev)) { - perror("Failing on epoll_ctl"); - exit(-1); + i = 0; + while (i < narg) { + wd = inotify_add_watch(fd, argv[optind+i], mask); /* add watch */ + if (wd <= 0) { + printf("Failing to add watched file %s, mask %i; %s\n", + argv[optind+i], mask, strerror(errno)); + exit(-1); + } + i++; } /* * Main Loop: read events and print them */ while (1) { - if (epoll_wait(epfd, &epev, 1, -1) < 0) { - 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 (ioctl(fd, FIONREAD, &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); - exit(-1); - } - i = 0; - while (i < size) { - nread = read(fd, buffer, size); - if (nread < 0) { + nread = read(fd, buffer, sizeof(buffer)); + if (nread < 0) { + if (errno == EINTR) { + continue; + } else { perror("error reading inotify data"); 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); - } else { + } else { + i = 0; + while (i < nread) { + event = (struct inotify_event *) buffer + i; + printf("Watch descriptor %i\n", event->wd); printf("Observed event on %s\n", argv[optind-1+event->wd]); - if (event->name != NULL) + if (event->len) { printf("On file %s\n", event->name); + } printevent(event->mask); + i += sizeof(struct inotify_event) + event->len; } } } @@ -173,12 +157,14 @@ 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 dir/file ... \n"); + printf(" inotify_monitor [-h] -rwcdCMa dirname/filename dir/file ... \n"); printf(" -h print this help\n"); printf(" -w watch write\n"); printf(" -r watch read\n"); printf(" -c watch create\n"); printf(" -d watch delete\n"); + printf(" -C watch closing\n"); + printf(" -M watch moving\n"); printf(" -a watch all\n"); exit(1); }