Esempio di inotify, e qualche correzione sui sysctl dei socket
[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/inotify.h>
31 #include <stdlib.h>        /* C standard library */
32 #include <unistd.h>
33 #include <errno.h>       /* error definitions and routines */ 
34 #include <stdio.h>       /* standard I/O library */
35 #include <string.h>      /* string functions */
36
37
38 #include "macros.h"
39
40 /* Help printing routine */
41 void usage(void);
42 void printevent(unsigned int mask);
43
44 int main(int argc, char *argv[]) 
45 {
46     int i, size;
47     int fd, wd;
48     char buffer[128*sizeof(struct inotify_event)];
49     unsigned int mask=0;
50     struct inotify_event * event;
51     /*
52      * Input section: decode command line parameters 
53      * Use getopt function
54      */
55     opterr = 0;  /* don't want writing to stderr */
56     while ( (i = getopt(argc, argv, "hrwcda")) != -1) {
57         switch (i) {
58         /* 
59          * Handling options 
60          */ 
61         case 'h':       /* help option */
62             printf("Wrong -h option use\n");
63             usage();
64         case 'r':       /* read access */
65             mask |= IN_ACCESS;
66             break;
67         case 'w':       /* write access */
68             mask |= IN_MODIFY;
69             break;
70         case 'c':       /* creation */
71             mask |= IN_CREATE;
72             break;
73         case 'd':       /* deletion */
74             mask |= IN_DELETE;
75             break;
76         case 'a':       /* all events */
77             mask |= IN_ALL_EVENTS;
78             break;
79         case '?':       /* unrecognized options */
80             printf("Unrecognized options -%c\n",optopt);
81             usage();
82         default:        /* should not reached */
83             usage();
84         }
85     }
86     /* ***********************************************************
87      * 
88      *           Options processing completed
89      *
90      *                Main code beginning
91      * 
92      * ***********************************************************/
93     /* There must be one argument */
94     if ((argc - optind) != 1) {  
95         printf("Wrong number of arguments %d\n", argc - optind);
96         usage();
97     }
98     /* initalialize inotify */
99     fd = inotify_init ();
100     if (fd < 0)
101         perror("Failing on inotify_init");
102
103     /* add watch */
104     wd = inotify_add_watch(fd, argv[optind], mask);
105     if ( wd <= 0) {
106         printf("Failing to add watched file %s, mask %i; %s\n", 
107               argv[optind], mask, strerror(errno));
108         exit(-1);
109     }    
110     /* 
111      * Main Loop: read events and print them
112      */
113     while (1) {
114         size = read(fd, buffer, sizeof(buffer));
115         
116         event = (struct inotify_event *) buffer;
117         if (wd != event->wd) {
118             printf("Error, getting different watch descriptor, %i and %i\n",
119                    wd, event->wd); 
120             exit(1);
121         }
122         printf("Observed event on %s\n", argv[optind-1+event->wd]);
123         if (event->name != NULL)
124             printf("On file %s\n", event->name);
125         printevent(event->mask);
126     }
127     return 0;
128
129 }
130 /*
131  * routine to print usage info and exit
132  */
133 void usage(void) {
134     printf("Program inotify_monitor: monitor file changes \n");
135     printf("Usage:\n");
136     printf(" inotify_monitor [-h] -rwcd dirname/filename dir/file ... \n");
137     printf("  -h           print this help\n");
138     printf("  -w           watch write\n");
139     printf("  -r           watch read\n");
140     printf("  -c           watch create\n");
141     printf("  -d           watch delete\n");
142     printf("  -a           watch all\n");
143     exit(1);
144 }
145
146 void printevent(unsigned int mask) {
147     int i;
148     int val;
149     char * inotify_event[] = {
150         "IN_ACCESS", 
151         "IN_MODIFY", 
152         "IN_ATTRIB", 
153         "IN_CLOSE_WRITE", 
154         "IN_CLOSE_NOWRITE", 
155         "IN_OPEN", 
156         "IN_MOVED_FROM", 
157         "IN_MOVED_TO",
158         "IN_CREATE", 
159         "IN_DELETE", 
160         "IN_DELETE_SELF", 
161         "IN_MOVE_SELF", 
162         "ERROR!!!", 
163         "IN_UNMOUNT", 
164         "IN_Q_OVERFLOW", 
165         "IN_IGNORED"
166     };
167     
168     val=1;
169     for (i=0; i<16; i++) {
170         if (mask & val) 
171             printf("%s, ", inotify_event[i]); 
172         val = val << 1;
173     }
174     printf("\n");
175 }