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 inotify_monitor.c:
23 * An example of the inotify interface: use inotify to watch the
24 * status of a directory or a file
26 * Author: S. Piccardi Jul. 2007
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 */
43 /* Help printing routine */
45 void printevent(unsigned int mask);
47 int main(int argc, char *argv[])
51 char buffer[512 * (sizeof(struct inotify_event) + 16)];
53 struct inotify_event * event;
55 * Input section: decode command line parameters
58 opterr = 0; /* don't want writing to stderr */
59 while ((i = getopt(argc, argv, "hrwcdaCM")) != -1) {
64 case 'h': /* help option */
65 printf("Wrong -h option use\n");
67 case 'r': /* read access */
70 case 'w': /* write access */
73 case 'c': /* creation */
76 case 'd': /* deletion */
79 case 'a': /* all events */
80 mask |= IN_ALL_EVENTS;
82 case 'C': /* creation */
85 case 'M': /* creation */
88 case '?': /* unrecognized options */
89 printf("Unrecognized options -%c\n",optopt);
91 default: /* should not reached */
96 printf("No events to monitor\n");
99 /* ***********************************************************
101 * Options processing completed
103 * Main code beginning
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);
111 fd = inotify_init(); /* initialize inotify */
113 perror("Failing on inotify_init");
118 wd = inotify_add_watch(fd, argv[optind+i], mask); /* add watch */
120 printf("Failing to add watched file %s, mask %i; %s\n",
121 argv[optind+i], mask, strerror(errno));
127 * Main Loop: read events and print them
130 nread = read(fd, buffer, sizeof(buffer));
132 if (errno == EINTR) {
135 perror("error reading inotify data");
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]);
145 printf("On file %s\n", event->name);
147 printevent(event->mask);
148 i += sizeof(struct inotify_event) + event->len;
155 * routine to print usage info and exit
158 printf("Program inotify_monitor: monitor file changes \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");
172 void printevent(unsigned int mask) {
175 char * inotify_event[] = {
195 for (i=0; i<16; i++) {
197 printf("%s, ", inotify_event[i]);