Aggiornamento note copyright
[gapil.git] / sources / epoll_inotify_monitor.c
1 /* epoll_inotify_monitor.c
2  * 
3  * Copyright (C) 2007 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /*****************************************************************************
20  *
21  * File epoll_inotify_monitor.c: 
22  *
23  * An example of the inotify and epoll interface: use inotify to watch the
24  * status of a directory or a file and epoll to wait for inotify events.
25  *
26  * Author: S. Piccardi Jul. 2007
27  *
28  *****************************************************************************/
29 #include <sys/types.h>   /* primitive system data types */
30 #include <sys/stat.h>    /* file characteristics constants and functions */
31 #include <sys/inotify.h> /* Linux inotify interface */
32 #include <sys/epoll.h>   /* Linux epoll interface */
33 #include <stdlib.h>      /* C standard library */
34 #include <unistd.h>      /* unix standard library */
35 #include <errno.h>       /* error definitions and routines */ 
36 #include <stdio.h>       /* standard I/O library */
37 #include <string.h>      /* C strings library */
38 #include <fcntl.h>       /* file control function */
39 #include <sys/ioctl.h>   /* ioctl syscall and constants */
40
41
42 #include "macros.h"
43
44 /* Help printing routine */
45 void usage(void);
46 void printevent(unsigned int mask);
47
48 int main(int argc, char *argv[]) 
49 {
50     int i, size, nread;
51     int fd, wd, epfd;
52     char buffer[1024 + sizeof(struct inotify_event)];
53     unsigned int mask=0;
54     struct inotify_event * event;
55     struct epoll_event epev;
56     /*
57      * Input section: decode command line parameters 
58      * Use getopt function
59      */
60     opterr = 0;  /* don't want writing to stderr */
61     while ((i = getopt(argc, argv, "hrwcda")) != -1) {
62         switch (i) {
63         /* 
64          * Handling options 
65          */ 
66         case 'h':       /* help option */
67             printf("Wrong -h option use\n");
68             usage();
69         case 'r':       /* read access */
70             mask |= IN_ACCESS;
71             break;
72         case 'w':       /* write access */
73             mask |= IN_MODIFY;
74             break;
75         case 'c':       /* creation */
76             mask |= IN_CREATE;
77             break;
78         case 'd':       /* deletion */
79             mask |= IN_DELETE;
80             break;
81         case 'a':       /* all events */
82             mask |= IN_ALL_EVENTS;
83             break;
84         case '?':       /* unrecognized options */
85             printf("Unrecognized options -%c\n",optopt);
86             usage();
87         default:        /* should not reached */
88             usage();
89         }
90     }
91     /* ***********************************************************
92      * 
93      *           Options processing completed
94      *
95      *                Main code beginning
96      * 
97      * ***********************************************************/
98     if ((argc - optind) != 1) {           /* There must be one argument */
99         printf("Wrong number of arguments %d\n", argc - optind);
100         usage();
101     }
102     epfd = epoll_create(5);               /* initialize epoll */
103     if (epfd < 0) {
104         perror("Failing on epoll_create");
105         exit(-1);
106     }
107     fd = inotify_init();                  /* initialize inotify */
108     if (fd < 0) {
109         perror("Failing on inotify_init");
110         exit(-1);
111     }
112     if (fcntl(fd, F_SETFL, O_NONBLOCK)) { /* no blocking I/O on inotify */
113         perror("Cannot set noblocking I/O on inotify fd");
114         exit(-1);
115     }
116     wd = inotify_add_watch(fd, argv[optind], mask);  /* add watch */
117     if (wd <= 0) {
118         printf("Failing to add watched file %s, mask %i; %s\n", 
119               argv[optind], mask, strerror(errno));
120         exit(-1);
121     }
122     epev.data.fd = fd;        /* add inotify fd to epoll */
123     epev.events = EPOLLIN;
124     if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &epev)) {
125         perror("Failing on epoll_ctl");
126         exit(-1);
127     }
128     /* 
129      * Main Loop: read events and print them
130      */
131     while (1) {
132         if (epoll_wait(epfd, &epev, 1, -1) < 0) {
133             perror("error on epoll_wait");
134             exit(-1);
135         }
136         if (epev.data.fd != fd) 
137             printf("something wrong, epoll activity on %i instead of %i\n",
138                    epev.data.fd, fd);
139         
140         if (ioctl(fd, FIONREAD, &size)) {
141             perror("error on getting inotify event size");
142             exit(-1);
143         }
144         if (size > sizeof(buffer)) {
145             printf("Too many %i data to read, something wrong\n", size);
146             exit(-1);
147         }
148         i = 0;
149         while (i < size) {
150             nread = read(fd, buffer, size);
151             if (nread < 0) {
152                 perror("error reading inotify data");
153                 exit(1);
154             }
155             i += nread; 
156             event = (struct inotify_event *) buffer;
157             if (wd != event->wd) {
158                 printf("Getting different watch descriptor, %i and %i\n",
159                        wd, event->wd); 
160             } else {
161                 printf("Observed event on %s\n", argv[optind-1+event->wd]);
162                 if (event->name != NULL)
163                     printf("On file %s\n", event->name);
164                 printevent(event->mask);
165             }
166         }
167     }
168     return 0;
169 }
170 /*
171  * routine to print usage info and exit
172  */
173 void usage(void) {
174     printf("Program inotify_monitor: monitor file changes \n");
175     printf("Usage:\n");
176     printf(" inotify_monitor [-h] -rwcd dirname/filename dir/file ... \n");
177     printf("  -h           print this help\n");
178     printf("  -w           watch write\n");
179     printf("  -r           watch read\n");
180     printf("  -c           watch create\n");
181     printf("  -d           watch delete\n");
182     printf("  -a           watch all\n");
183     exit(1);
184 }
185
186 void printevent(unsigned int mask) {
187     int i;
188     int val;
189     char * inotify_event[] = {
190         "IN_ACCESS", 
191         "IN_MODIFY", 
192         "IN_ATTRIB", 
193         "IN_CLOSE_WRITE", 
194         "IN_CLOSE_NOWRITE", 
195         "IN_OPEN", 
196         "IN_MOVED_FROM", 
197         "IN_MOVED_TO",
198         "IN_CREATE", 
199         "IN_DELETE", 
200         "IN_DELETE_SELF", 
201         "IN_MOVE_SELF", 
202         "ERROR!!!", 
203         "IN_UNMOUNT", 
204         "IN_Q_OVERFLOW", 
205         "IN_IGNORED"
206     };
207     
208     val=1;
209     for (i=0; i<16; i++) {
210         if (mask & val) 
211             printf("%s, ", inotify_event[i]); 
212         val = val << 1;
213     }
214     printf("\n");
215 }