Correzioni ed esempio con inotify (ed epoll).
[gapil.git] / sources / inotify_monitor.c
1 /* 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 inotufy_monitor.c: 
22  *
23  * An example for shared memory use: monitor a directory status,
24  * saving data in a shared memory segment
25  *
26  * Author: S. Piccardi Jul. 2007
27  *
28  *****************************************************************************/
29 #include <sys/types.h>
30 #include <sys/stat.h>
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>      /* string functions */
38 #include <fcntl.h>       /* fcntl function */
39 #include <sys/ioctl.h>       /* fcntl function */
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     /* There must be one argument */
99     if ((argc - optind) != 1) {  
100         printf("Wrong number of arguments %d\n", argc - optind);
101         usage();
102     }
103     /* initializa epoll */
104     epfd = epoll_create(5);
105     if (epfd < 0)
106         perror("Failing on epoll_create");
107
108     /* initalialize inotify */
109     fd = inotify_init();
110     if (fd < 0)
111         perror("Failing on inotify_init");
112     if (fcntl(fd, F_SETFL, O_NONBLOCK)) 
113         perror("Cannot set noblocking I/O on inotify fd");
114     
115     /* add watch */
116     wd = inotify_add_watch(fd, argv[optind], mask);
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     
123     /* add inotify fd to epoll */
124     epev.data.fd = fd;
125     epev.events = EPOLLIN;
126     if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &epev))
127         perror("Failing on epoll_ctl");
128
129     /* 
130      * Main Loop: read events and print them
131      */
132     while (1) {
133         if (epoll_wait(epfd, &epev, 1, -1) < 0 )
134             perror("error on epoll_wait");
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
143         if (size > sizeof(buffer)) {
144             printf("Too many %i data to read, something wrong\n", size);
145             exit(1);
146         }
147         i = 0;
148         while (i < size) {
149             nread = read(fd, buffer, size);
150             if (nread < 0) {
151                 perror("error reading inotify data");
152                 exit(1);
153             }
154             i += nread; 
155
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                 exit(1);
161             }
162             printf("Observed event on %s\n", argv[optind-1+event->wd]);
163             if (event->name != NULL)
164                 printf("On file %s\n", event->name);
165             printevent(event->mask);
166         }
167     }
168     return 0;
169
170 }
171 /*
172  * routine to print usage info and exit
173  */
174 void usage(void) {
175     printf("Program inotify_monitor: monitor file changes \n");
176     printf("Usage:\n");
177     printf(" inotify_monitor [-h] -rwcd dirname/filename dir/file ... \n");
178     printf("  -h           print this help\n");
179     printf("  -w           watch write\n");
180     printf("  -r           watch read\n");
181     printf("  -c           watch create\n");
182     printf("  -d           watch delete\n");
183     printf("  -a           watch all\n");
184     exit(1);
185 }
186
187 void printevent(unsigned int mask) {
188     int i;
189     int val;
190     char * inotify_event[] = {
191         "IN_ACCESS", 
192         "IN_MODIFY", 
193         "IN_ATTRIB", 
194         "IN_CLOSE_WRITE", 
195         "IN_CLOSE_NOWRITE", 
196         "IN_OPEN", 
197         "IN_MOVED_FROM", 
198         "IN_MOVED_TO",
199         "IN_CREATE", 
200         "IN_DELETE", 
201         "IN_DELETE_SELF", 
202         "IN_MOVE_SELF", 
203         "ERROR!!!", 
204         "IN_UNMOUNT", 
205         "IN_Q_OVERFLOW", 
206         "IN_IGNORED"
207     };
208     
209     val=1;
210     for (i=0; i<16; i++) {
211         if (mask & val) 
212             printf("%s, ", inotify_event[i]); 
213         val = val << 1;
214     }
215     printf("\n");
216 }