3 * Copyright (C) 2007 Simone Piccardi
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.
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.
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.
19 /*****************************************************************************
21 * File inotufy_monitor.c:
23 * An example for shared memory use: monitor a directory status,
24 * saving data in a shared memory segment
26 * Author: S. Piccardi Jul. 2007
28 *****************************************************************************/
29 #include <sys/types.h>
30 #include <sys/inotify.h>
31 #include <stdlib.h> /* C standard library */
33 #include <errno.h> /* error definitions and routines */
34 #include <stdio.h> /* standard I/O library */
35 #include <string.h> /* string functions */
40 /* Help printing routine */
42 void printevent(unsigned int mask);
44 int main(int argc, char *argv[])
48 char buffer[128*sizeof(struct inotify_event)];
50 struct inotify_event * event;
52 * Input section: decode command line parameters
55 opterr = 0; /* don't want writing to stderr */
56 while ( (i = getopt(argc, argv, "hrwcda")) != -1) {
61 case 'h': /* help option */
62 printf("Wrong -h option use\n");
64 case 'r': /* read access */
67 case 'w': /* write access */
70 case 'c': /* creation */
73 case 'd': /* deletion */
76 case 'a': /* all events */
77 mask |= IN_ALL_EVENTS;
79 case '?': /* unrecognized options */
80 printf("Unrecognized options -%c\n",optopt);
82 default: /* should not reached */
86 /* ***********************************************************
88 * Options processing completed
92 * ***********************************************************/
93 /* There must be one argument */
94 if ((argc - optind) != 1) {
95 printf("Wrong number of arguments %d\n", argc - optind);
98 /* initalialize inotify */
101 perror("Failing on inotify_init");
104 wd = inotify_add_watch(fd, argv[optind], mask);
106 printf("Failing to add watched file %s, mask %i; %s\n",
107 argv[optind], mask, strerror(errno));
111 * Main Loop: read events and print them
114 size = read(fd, buffer, sizeof(buffer));
116 event = (struct inotify_event *) buffer;
117 if (wd != event->wd) {
118 printf("Error, getting different watch descriptor, %i and %i\n",
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);
131 * routine to print usage info and exit
134 printf("Program inotify_monitor: monitor file changes \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");
146 void printevent(unsigned int mask) {
149 char * inotify_event[] = {
169 for (i=0; i<16; i++) {
171 printf("%s, ", inotify_event[i]);