1 /* vi: set sw=4 ts=4: */
3 * simple inotify daemon
4 * reports filesystem changes via userspace agent
6 * Copyright (C) 2008 by Vladimir Dronnikov <dronnikov@gmail.com>
8 * Licensed under GPLv2, see file LICENSE in this source tree.
13 * # inotifyd /user/space/agent dir/or/file/being/watched[:mask] ...
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:
18 * $2. file (or directory) name
19 * $3. name of subfile (if any), in case of watching a directory
21 * E.g. inotifyd ./dev-watcher /dev:n
23 * ./dev-watcher can be, say:
25 * echo "We have new device in here! Hello, $3!"
27 * See below for mask names explanation.
30 //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."
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"
54 //usage: "\ninotifyd waits for PROG to exit."
55 //usage: "\nWhen x event happens for all FILEs, inotifyd exits."
58 #include <sys/inotify.h>
60 static const char mask_names[] ALIGN1 =
61 "a" // 0x00000001 File was accessed
62 "c" // 0x00000002 File was modified
63 "e" // 0x00000004 Metadata changed
64 "w" // 0x00000008 Writable file was closed
65 "0" // 0x00000010 Unwritable file closed
66 "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
73 "\0" // 0x00001000 (unused)
74 // Kernel events, always reported:
75 "u" // 0x00002000 Backing fs was unmounted
76 "o" // 0x00004000 Event queued overflowed
77 "x" // 0x00008000 File is no longer watched (usually deleted)
80 MASK_BITS = sizeof(mask_names) - 1
83 int inotifyd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
84 int inotifyd_main(int argc, char **argv)
89 char **watches; // names of files being watched
92 // sanity check: agent and at least one watch must be given
93 if (!argv[1] || !argv[2])
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].
103 argc -= 2; // number of files we watch
106 pfd.fd = inotify_init();
108 bb_perror_msg_and_die("no kernel support");
113 char *masks = strchr(path, ':');
115 mask = 0x0fff; // assuming we want all non-kernel events
116 // if mask is specified ->
118 *masks = '\0'; // split path and mask
119 // convert mask names to mask bitset
123 found = memchr(mask_names, *masks, MASK_BITS);
125 mask |= (1 << (found - mask_names));
129 n = inotify_add_watch(pfd.fd, path, mask);
131 bb_perror_msg_and_die("add watch (%s) failed", path);
132 //bb_error_msg("added %d [%s]:%4X", n, path, mask);
136 bb_signals(BB_FATAL_SIGS, record_signo);
143 struct inotify_event *ie;
147 n = poll(&pfd, 1, -1);
148 // Signal interrupted us?
149 if (n < 0 && errno == EINTR)
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?
161 // read out all pending events
162 // (NB: len must be int, not ssize_t or long!)
163 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
170 // cache relevant events mask
171 unsigned m = ie->mask & ((1 << MASK_BITS) - 1);
173 char events[MASK_BITS + 1];
175 for (i = 0; i < MASK_BITS; ++i, m >>= 1) {
176 if ((m & 1) && (mask_names[i] != '\0'))
177 *s++ = mask_names[i];
180 // bb_error_msg("exec %s %08X\t%s\t%s\t%s", args[0],
181 // ie->mask, events, watches[ie->wd], ie->len ? ie->name : "");
183 args[2] = watches[ie->wd];
184 args[3] = ie->len ? ie->name : NULL;
185 spawn_and_wait((char **)args);
186 // we are done if all files got final x event
187 if (ie->mask & 0x8000) {
190 inotify_rm_watch(pfd.fd, ie->wd);
194 i = sizeof(struct inotify_event) + ie->len;
196 ie = (void*)((char*)ie + i);
202 return bb_got_signal;