Varie correzioni, completata revisione capitolo sull'I/O su file
[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 inotify_monitor.c: 
22  *
23  * An example of the inotify interface: use inotify to watch the
24  * status of a directory or a file
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 <stdlib.h>      /* C standard library */
33 #include <unistd.h>      /* unix standard library */
34 #include <errno.h>       /* error definitions and routines */ 
35 #include <stdio.h>       /* standard I/O library */
36 #include <string.h>      /* C strings library */
37 #include <fcntl.h>       /* file control functions */
38 #include <sys/ioctl.h>   /* ioctl syscall and constants */
39
40
41 #include "macros.h"
42
43 /* Help printing routine */
44 void usage(void);
45 void printevent(unsigned int mask);
46
47 int main(int argc, char *argv[]) 
48 {
49     int i, narg, nread;
50     int fd, wd;
51     char buffer[512 * (sizeof(struct inotify_event) + 16)];
52     unsigned int mask=0;
53     struct inotify_event * event;
54     /*
55      * Input section: decode command line parameters 
56      * Use getopt function
57      */
58     opterr = 0;  /* don't want writing to stderr */
59     while ((i = getopt(argc, argv, "hrwcdaCM")) != -1) {
60         switch (i) {
61         /* 
62          * Handling options 
63          */ 
64         case 'h':       /* help option */
65             printf("Wrong -h option use\n");
66             usage();
67         case 'r':       /* read access */
68             mask |= IN_ACCESS;
69             break;
70         case 'w':       /* write access */
71             mask |= IN_MODIFY;
72             break;
73         case 'c':       /* creation */
74             mask |= IN_CREATE;
75             break;
76         case 'd':       /* deletion */
77             mask |= IN_DELETE;
78             break;
79         case 'a':       /* all events */
80             mask |= IN_ALL_EVENTS;
81             break;
82         case 'C':       /* creation */
83             mask |= IN_CLOSE;
84             break;
85         case 'M':       /* creation */
86             mask |= IN_MOVE;
87             break;
88         case '?':       /* unrecognized options */
89             printf("Unrecognized options -%c\n",optopt);
90             usage();
91         default:        /* should not reached */
92             usage();
93         }
94     }
95     if (mask == 0) {
96         printf("No events to monitor\n");
97         usage();
98     }
99     /* ***********************************************************
100      * 
101      *           Options processing completed
102      *
103      *                Main code beginning
104      * 
105      * ***********************************************************/
106     narg = argc - optind; 
107     if (narg < 1) { /* There must be at least one argument */
108         printf("Wrong number of arguments %d\n", argc - optind);
109         usage();
110     }
111     fd = inotify_init();       /* initialize inotify */
112     if (fd < 0) {
113         perror("Failing on inotify_init");
114         exit(-1);
115     }
116     i = 0;
117     while (i < narg) {
118         wd = inotify_add_watch(fd, argv[optind+i], mask);  /* add watch */
119         if (wd <= 0) {
120             printf("Failing to add watched file %s, mask %i; %s\n", 
121                    argv[optind+i], mask, strerror(errno));
122             exit(-1);
123         }
124         i++;
125     }
126     /* 
127      * Main Loop: read events and print them
128      */
129     while (1) {
130         nread = read(fd, buffer, sizeof(buffer));
131         if (nread < 0) {
132             if (errno == EINTR) {
133                 continue;
134             } else {
135                 perror("error reading inotify data");
136                 exit(1);
137             }
138         } else {
139             i = 0;
140             while (i < nread) { 
141                 event = (struct inotify_event *) buffer + i;
142                 printf("Watch descriptor %i\n", event->wd);
143                 printf("Observed event on %s\n", argv[optind-1+event->wd]);
144                 if (event->len) {
145                     printf("On file %s\n", event->name);
146                 }
147                 printevent(event->mask);
148                 i += sizeof(struct inotify_event) + event->len;
149             }
150         }
151     }
152     return 0;
153 }
154 /*
155  * routine to print usage info and exit
156  */
157 void usage(void) {
158     printf("Program inotify_monitor: monitor file changes \n");
159     printf("Usage:\n");
160     printf(" inotify_monitor [-h] -rwcdCMa dirname/filename dir/file ... \n");
161     printf("  -h           print this help\n");
162     printf("  -w           watch write\n");
163     printf("  -r           watch read\n");
164     printf("  -c           watch create\n");
165     printf("  -d           watch delete\n");
166     printf("  -C           watch closing\n");
167     printf("  -M           watch moving\n");
168     printf("  -a           watch all\n");
169     exit(1);
170 }
171
172 void printevent(unsigned int mask) {
173     int i;
174     int val;
175     char * inotify_event[] = {
176         "IN_ACCESS", 
177         "IN_MODIFY", 
178         "IN_ATTRIB", 
179         "IN_CLOSE_WRITE", 
180         "IN_CLOSE_NOWRITE", 
181         "IN_OPEN", 
182         "IN_MOVED_FROM", 
183         "IN_MOVED_TO",
184         "IN_CREATE", 
185         "IN_DELETE", 
186         "IN_DELETE_SELF", 
187         "IN_MOVE_SELF", 
188         "ERROR!!!", 
189         "IN_UNMOUNT", 
190         "IN_Q_OVERFLOW", 
191         "IN_IGNORED"
192     };
193     
194     val=1;
195     for (i=0; i<16; i++) {
196         if (mask & val) 
197             printf("%s, ", inotify_event[i]); 
198         val = val << 1;
199     }
200     printf("\n");
201 }