X-Git-Url: https://gapil.gnulinux.it/gitweb/?p=gapil.git;a=blobdiff_plain;f=sources%2Finotify_monitor.c;fp=sources%2Finotify_monitor.c;h=1acfdb0c65d58b0fa99961ce42b286730958f50c;hp=92391cf39b5133c8ab49435f6c76a1878f0f8dee;hb=96c2ad52857ad188cd53292a581a10c7c24eb0da;hpb=e62b1d15cd14bfe362613091cad4d621c7ffc904 diff --git a/sources/inotify_monitor.c b/sources/inotify_monitor.c index 92391cf..1acfdb0 100644 --- a/sources/inotify_monitor.c +++ b/sources/inotify_monitor.c @@ -27,12 +27,16 @@ * *****************************************************************************/ #include -#include -#include /* C standard library */ -#include +#include +#include /* Linux inotify interface */ +#include /* Linux epoll interface */ +#include /* C standard library */ +#include /* Unix standard library */ #include /* error definitions and routines */ #include /* standard I/O library */ #include /* string functions */ +#include /* fcntl function */ +#include /* fcntl function */ #include "macros.h" @@ -43,11 +47,12 @@ void printevent(unsigned int mask); int main(int argc, char *argv[]) { - int i, size; - int fd, wd; - char buffer[128*sizeof(struct inotify_event)]; + int i, size, nread; + int fd, wd, epfd; + char buffer[1024 + sizeof(struct inotify_event)]; unsigned int mask=0; struct inotify_event * event; + struct epoll_event epev; /* * Input section: decode command line parameters * Use getopt function @@ -95,34 +100,70 @@ int main(int argc, char *argv[]) printf("Wrong number of arguments %d\n", argc - optind); usage(); } + /* initializa epoll */ + epfd = epoll_create(5); + if (epfd < 0) + perror("Failing on epoll_create"); + /* initalialize inotify */ - fd = inotify_init (); + fd = inotify_init(); if (fd < 0) perror("Failing on inotify_init"); - + if (fcntl(fd, F_SETFL, O_NONBLOCK)) + perror("Cannot set noblocking I/O on inotify fd"); + /* add watch */ 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); - } + } + + /* add inotify fd to epoll */ + epev.data.fd = fd; + epev.events = EPOLLIN; + if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &epev)) + perror("Failing on epoll_ctl"); + /* * Main Loop: read events and print them */ while (1) { - size = read(fd, buffer, sizeof(buffer)); + if (epoll_wait(epfd, &epev, 1, -1) < 0 ) + perror("error on epoll_wait"); + + if (epev.data.fd != fd) + printf("something wrong, epoll activity on %i instead of %i\n", + epev.data.fd, fd); - event = (struct inotify_event *) buffer; - if (wd != event->wd) { - printf("Error, getting different watch descriptor, %i and %i\n", - wd, event->wd); + if (ioctl(fd, FIONREAD, &size)) + perror("error on getting inotify event size"); + + if (size > sizeof(buffer)) { + printf("Too many %i data to read, something wrong\n", size); 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); + i = 0; + while (i < size) { + nread = read(fd, buffer, size); + if (nread < 0) { + 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); + 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;