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>
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 */
44 /* Help printing routine */
46 void printevent(unsigned int mask);
48 int main(int argc, char *argv[])
52 char buffer[1024 + sizeof(struct inotify_event)];
54 struct inotify_event * event;
55 struct epoll_event epev;
57 * Input section: decode command line parameters
60 opterr = 0; /* don't want writing to stderr */
61 while ( (i = getopt(argc, argv, "hrwcda")) != -1) {
66 case 'h': /* help option */
67 printf("Wrong -h option use\n");
69 case 'r': /* read access */
72 case 'w': /* write access */
75 case 'c': /* creation */
78 case 'd': /* deletion */
81 case 'a': /* all events */
82 mask |= IN_ALL_EVENTS;
84 case '?': /* unrecognized options */
85 printf("Unrecognized options -%c\n",optopt);
87 default: /* should not reached */
91 /* ***********************************************************
93 * Options processing completed
97 * ***********************************************************/
98 /* There must be one argument */
99 if ((argc - optind) != 1) {
100 printf("Wrong number of arguments %d\n", argc - optind);
103 /* initializa epoll */
104 epfd = epoll_create(5);
106 perror("Failing on epoll_create");
108 /* initalialize inotify */
111 perror("Failing on inotify_init");
112 if (fcntl(fd, F_SETFL, O_NONBLOCK))
113 perror("Cannot set noblocking I/O on inotify fd");
116 wd = inotify_add_watch(fd, argv[optind], mask);
118 printf("Failing to add watched file %s, mask %i; %s\n",
119 argv[optind], mask, strerror(errno));
123 /* add inotify fd to epoll */
125 epev.events = EPOLLIN;
126 if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &epev))
127 perror("Failing on epoll_ctl");
130 * Main Loop: read events and print them
133 if (epoll_wait(epfd, &epev, 1, -1) < 0 )
134 perror("error on epoll_wait");
136 if (epev.data.fd != fd)
137 printf("something wrong, epoll activity on %i instead of %i\n",
140 if (ioctl(fd, FIONREAD, &size))
141 perror("error on getting inotify event size");
143 if (size > sizeof(buffer)) {
144 printf("Too many %i data to read, something wrong\n", size);
149 nread = read(fd, buffer, size);
151 perror("error reading inotify data");
156 event = (struct inotify_event *) buffer;
157 if (wd != event->wd) {
158 printf("Getting different watch descriptor, %i and %i\n",
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);
172 * routine to print usage info and exit
175 printf("Program inotify_monitor: monitor file changes \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");
187 void printevent(unsigned int mask) {
190 char * inotify_event[] = {
210 for (i=0; i<16; i++) {
212 printf("%s, ", inotify_event[i]);