blob: b64e0abb92494c0b1f0852a8f2fe7fe7594ec52e [file] [log] [blame]
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +00001/* vi: set sw=4 ts=4: */
2/*
3 * simple inotify daemon
4 * reports filesystem changes via userspace agent
5 *
6 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
7 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +00009 */
10
11/*
12 * Use as follows:
13 * # inotifyd /user/space/agent dir/or/file/being/watched[:mask] ...
14 *
15 * When a filesystem event matching the specified mask is occured on specified file (or directory)
16 * a userspace agent is spawned and given the following parameters:
17 * $1. actual event(s)
18 * $2. file (or directory) name
19 * $3. name of subfile (if any), in case of watching a directory
20 *
21 * E.g. inotifyd ./dev-watcher /dev:n
22 *
23 * ./dev-watcher can be, say:
24 * #!/bin/sh
25 * echo "We have new device in here! Hello, $3!"
26 *
27 * See below for mask names explanation.
28 */
29
Pere Orga5bc8c002011-04-11 03:29:49 +020030//usage:#define inotifyd_trivial_usage
31//usage: "PROG FILE1[:MASK]..."
32//usage:#define inotifyd_full_usage "\n\n"
33//usage: "Run PROG on filesystem changes."
34//usage: "\nWhen a filesystem event matching MASK occurs on FILEn,"
35//usage: "\nPROG ACTUAL_EVENTS FILEn [SUBFILE] is run."
36//usage: "\nEvents:"
37//usage: "\n a File is accessed"
38//usage: "\n c File is modified"
39//usage: "\n e Metadata changed"
40//usage: "\n w Writable file is closed"
41//usage: "\n 0 Unwritable file is closed"
42//usage: "\n r File is opened"
43//usage: "\n D File is deleted"
44//usage: "\n M File is moved"
45//usage: "\n u Backing fs is unmounted"
46//usage: "\n o Event queue overflowed"
47//usage: "\n x File can't be watched anymore"
48//usage: "\nIf watching a directory:"
49//usage: "\n m Subfile is moved into dir"
50//usage: "\n y Subfile is moved out of dir"
51//usage: "\n n Subfile is created"
52//usage: "\n d Subfile is deleted"
53//usage: "\n"
54//usage: "\ninotifyd waits for PROG to exit."
55//usage: "\nWhen x event happens for all FILEs, inotifyd exits."
56
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +000057#include "libbb.h"
Bernhard Reutner-Fischer01ea9712008-10-21 17:26:10 +000058#include <sys/inotify.h>
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +000059
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +000060static const char mask_names[] ALIGN1 =
61 "a" // 0x00000001 File was accessed
62 "c" // 0x00000002 File was modified
63 "e" // 0x00000004 Metadata changed
Denys Vlasenko5370bfb2009-09-06 02:58:59 +020064 "w" // 0x00000008 Writable file was closed
65 "0" // 0x00000010 Unwritable file closed
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +000066 "r" // 0x00000020 File was opened
67 "m" // 0x00000040 File was moved from X
68 "y" // 0x00000080 File was moved to Y
69 "n" // 0x00000100 Subfile was created
70 "d" // 0x00000200 Subfile was deleted
71 "D" // 0x00000400 Self was deleted
72 "M" // 0x00000800 Self was moved
Denis Vlasenkoa09a42c2008-11-17 22:19:18 +000073 "\0" // 0x00001000 (unused)
Denis Vlasenko9a4c0d52008-11-20 01:24:39 +000074 // Kernel events, always reported:
Denis Vlasenkoa09a42c2008-11-17 22:19:18 +000075 "u" // 0x00002000 Backing fs was unmounted
76 "o" // 0x00004000 Event queued overflowed
77 "x" // 0x00008000 File is no longer watched (usually deleted)
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +000078;
Denis Vlasenkoa09a42c2008-11-17 22:19:18 +000079enum {
80 MASK_BITS = sizeof(mask_names) - 1
81};
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +000082
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +000083int inotifyd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9a4c0d52008-11-20 01:24:39 +000084int inotifyd_main(int argc, char **argv)
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +000085{
Denis Vlasenkob0e5d422008-11-14 21:34:13 +000086 int n;
Denis Vlasenko9a4c0d52008-11-20 01:24:39 +000087 unsigned mask;
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +000088 struct pollfd pfd;
Denis Vlasenko9a4c0d52008-11-20 01:24:39 +000089 char **watches; // names of files being watched
90 const char *args[5];
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +000091
92 // sanity check: agent and at least one watch must be given
Denis Vlasenko9a4c0d52008-11-20 01:24:39 +000093 if (!argv[1] || !argv[2])
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +000094 bb_show_usage();
95
Denis Vlasenko9a4c0d52008-11-20 01:24:39 +000096 argv++;
97 // inotify_add_watch will number watched files
98 // starting from 1, thus watches[0] is unimportant,
99 // and 1st file name is watches[1].
100 watches = argv;
101 args[0] = *argv;
102 args[4] = NULL;
103 argc -= 2; // number of files we watch
104
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +0000105 // open inotify
106 pfd.fd = inotify_init();
107 if (pfd.fd < 0)
108 bb_perror_msg_and_die("no kernel support");
109
Denis Vlasenko9a4c0d52008-11-20 01:24:39 +0000110 // setup watches
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +0000111 while (*++argv) {
112 char *path = *argv;
113 char *masks = strchr(path, ':');
Denis Vlasenko9a4c0d52008-11-20 01:24:39 +0000114
115 mask = 0x0fff; // assuming we want all non-kernel events
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +0000116 // if mask is specified ->
117 if (masks) {
118 *masks = '\0'; // split path and mask
119 // convert mask names to mask bitset
120 mask = 0;
121 while (*++masks) {
Denis Vlasenkoa09a42c2008-11-17 22:19:18 +0000122 const char *found;
123 found = memchr(mask_names, *masks, MASK_BITS);
124 if (found)
125 mask |= (1 << (found - mask_names));
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +0000126 }
127 }
128 // add watch
Denis Vlasenkob0e5d422008-11-14 21:34:13 +0000129 n = inotify_add_watch(pfd.fd, path, mask);
130 if (n < 0)
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +0000131 bb_perror_msg_and_die("add watch (%s) failed", path);
Denis Vlasenkob0e5d422008-11-14 21:34:13 +0000132 //bb_error_msg("added %d [%s]:%4X", n, path, mask);
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +0000133 }
134
135 // setup signals
Denis Vlasenkob0e5d422008-11-14 21:34:13 +0000136 bb_signals(BB_FATAL_SIGS, record_signo);
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +0000137
138 // do watch
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +0000139 pfd.events = POLLIN;
Denis Vlasenkob0e5d422008-11-14 21:34:13 +0000140 while (1) {
Denis Vlasenko9a4c0d52008-11-20 01:24:39 +0000141 int len;
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +0000142 void *buf;
143 struct inotify_event *ie;
Denis Vlasenkob0e5d422008-11-14 21:34:13 +0000144 again:
145 if (bb_got_signal)
146 break;
147 n = poll(&pfd, 1, -1);
Denis Vlasenko289ff0e2008-11-14 21:52:16 +0000148 // Signal interrupted us?
Denis Vlasenkob0e5d422008-11-14 21:34:13 +0000149 if (n < 0 && errno == EINTR)
150 goto again;
151 // Under Linux, above if() is not necessary.
152 // Non-fatal signals, e.g. SIGCHLD, when set to SIG_DFL,
153 // are not interrupting poll().
154 // Thus we can just break if n <= 0 (see below),
155 // because EINTR will happen only on SIGTERM et al.
156 // But this might be not true under other Unixes,
157 // and is generally way too subtle to depend on.
158 if (n <= 0) // strange error?
159 break;
160
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +0000161 // read out all pending events
Denis Vlasenko9a4c0d52008-11-20 01:24:39 +0000162 // (NB: len must be int, not ssize_t or long!)
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +0000163 xioctl(pfd.fd, FIONREAD, &len);
164#define eventbuf bb_common_bufsiz1
165 ie = buf = (len <= sizeof(eventbuf)) ? eventbuf : xmalloc(len);
166 len = full_read(pfd.fd, buf, len);
167 // process events. N.B. events may vary in length
168 while (len > 0) {
169 int i;
Denis Vlasenkoa09a42c2008-11-17 22:19:18 +0000170 // cache relevant events mask
171 unsigned m = ie->mask & ((1 << MASK_BITS) - 1);
172 if (m) {
173 char events[MASK_BITS + 1];
174 char *s = events;
175 for (i = 0; i < MASK_BITS; ++i, m >>= 1) {
176 if ((m & 1) && (mask_names[i] != '\0'))
177 *s++ = mask_names[i];
178 }
179 *s = '\0';
180// bb_error_msg("exec %s %08X\t%s\t%s\t%s", args[0],
Denis Vlasenko9a4c0d52008-11-20 01:24:39 +0000181// ie->mask, events, watches[ie->wd], ie->len ? ie->name : "");
Denis Vlasenkoa09a42c2008-11-17 22:19:18 +0000182 args[1] = events;
Denis Vlasenko9a4c0d52008-11-20 01:24:39 +0000183 args[2] = watches[ie->wd];
Denis Vlasenkoa09a42c2008-11-17 22:19:18 +0000184 args[3] = ie->len ? ie->name : NULL;
Denys Vlasenko8531d762010-03-18 22:44:00 +0100185 spawn_and_wait((char **)args);
Denis Vlasenko9a4c0d52008-11-20 01:24:39 +0000186 // we are done if all files got final x event
Denis Vlasenkod723fb12008-11-29 09:07:24 +0000187 if (ie->mask & 0x8000) {
Denis Vlasenko9a4c0d52008-11-20 01:24:39 +0000188 if (--argc <= 0)
189 goto done;
Denis Vlasenkod723fb12008-11-29 09:07:24 +0000190 inotify_rm_watch(pfd.fd, ie->wd);
191 }
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +0000192 }
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +0000193 // next event
194 i = sizeof(struct inotify_event) + ie->len;
195 len -= i;
196 ie = (void*)((char*)ie + i);
197 }
198 if (eventbuf != buf)
199 free(buf);
Denis Vlasenko9a4c0d52008-11-20 01:24:39 +0000200 } // while (1)
201 done:
202 return bb_got_signal;
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +0000203}