1 /* epoll_inotify_monitor.c
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 epoll_inotify_monitor.c:
23 * An example of the inotify and epoll interface: use inotify to watch the
24 * status of a directory or a file and epoll to wait for inotify events.
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 <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> /* C strings library */
38 #include <fcntl.h> /* file control function */
39 #include <sys/ioctl.h> /* ioctl syscall and constants */
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 if ((argc - optind) != 1) { /* There must be one argument */
99 printf("Wrong number of arguments %d\n", argc - optind);
102 epfd = epoll_create(5); /* initialize epoll */
104 perror("Failing on epoll_create");
107 fd = inotify_init(); /* initialize inotify */
109 perror("Failing on inotify_init");
112 if (fcntl(fd, F_SETFL, O_NONBLOCK)) { /* no blocking I/O on inotify */
113 perror("Cannot set noblocking I/O on inotify fd");
116 wd = inotify_add_watch(fd, argv[optind], mask); /* add watch */
118 printf("Failing to add watched file %s, mask %i; %s\n",
119 argv[optind], mask, strerror(errno));
122 epev.data.fd = fd; /* add inotify fd to epoll */
123 epev.events = EPOLLIN;
124 if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &epev)) {
125 perror("Failing on epoll_ctl");
129 * Main Loop: read events and print them
132 if (epoll_wait(epfd, &epev, 1, -1) < 0) {
133 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");
144 if (size > sizeof(buffer)) {
145 printf("Too many %i data to read, something wrong\n", size);
150 nread = read(fd, buffer, size);
152 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",
161 printf("Observed event on %s\n", argv[optind-1+event->wd]);
162 if (event->name != NULL)
163 printf("On file %s\n", event->name);
164 printevent(event->mask);
171 * routine to print usage info and exit
174 printf("Program inotify_monitor: monitor file changes \n");
176 printf(" inotify_monitor [-h] -rwcd dirname/filename dir/file ... \n");
177 printf(" -h print this help\n");
178 printf(" -w watch write\n");
179 printf(" -r watch read\n");
180 printf(" -c watch create\n");
181 printf(" -d watch delete\n");
182 printf(" -a watch all\n");
186 void printevent(unsigned int mask) {
189 char * inotify_event[] = {
209 for (i=0; i<16; i++) {
211 printf("%s, ", inotify_event[i]);