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 tarball for details.
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.
31 #include <linux/inotify.h>
33 static volatile smallint signalled;
35 static void signal_handler(int signo)
40 static const char mask_names[] ALIGN1 =
41 "a" // 0x00000001 File was accessed
42 "c" // 0x00000002 File was modified
43 "e" // 0x00000004 Metadata changed
44 "w" // 0x00000008 Writtable file was closed
45 "0" // 0x00000010 Unwrittable file closed
46 "r" // 0x00000020 File was opened
47 "m" // 0x00000040 File was moved from X
48 "y" // 0x00000080 File was moved to Y
49 "n" // 0x00000100 Subfile was created
50 "d" // 0x00000200 Subfile was deleted
51 "D" // 0x00000400 Self was deleted
52 "M" // 0x00000800 Self was moved
55 extern int inotify_init(void);
56 extern int inotify_add_watch(int fd, const char *path, uint32_t mask);
58 int inotifyd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
59 int inotifyd_main(int argc ATTRIBUTE_UNUSED, char **argv)
61 unsigned mask = IN_ALL_EVENTS; // assume we want all events
63 char **watched = ++argv; // watched name list
64 const char *args[] = { *argv, NULL, NULL, NULL, NULL };
66 // sanity check: agent and at least one watch must be given
71 pfd.fd = inotify_init();
73 bb_perror_msg_and_die("no kernel support");
78 char *masks = strchr(path, ':');
79 int wd; // watch descriptor
80 // if mask is specified ->
82 *masks = '\0'; // split path and mask
83 // convert mask names to mask bitset
86 int i = strchr(mask_names, *masks) - mask_names;
93 wd = inotify_add_watch(pfd.fd, path, mask);
95 bb_perror_msg_and_die("add watch (%s) failed", path);
97 // bb_error_msg("added %d [%s]:%4X", wd, path, mask);
114 while (!signalled && poll(&pfd, 1, -1) > 0) {
117 struct inotify_event *ie;
119 // read out all pending events
120 xioctl(pfd.fd, FIONREAD, &len);
121 #define eventbuf bb_common_bufsiz1
122 ie = buf = (len <= sizeof(eventbuf)) ? eventbuf : xmalloc(len);
123 len = full_read(pfd.fd, buf, len);
124 // process events. N.B. events may vary in length
129 unsigned m = ie->mask;
131 for (i = 0; i < 12; ++i, m >>= 1) {
133 *s++ = mask_names[i];
137 // bb_error_msg("exec %s %08X\t%s\t%s\t%s", agent, ie->mask, events, watched[ie->wd], ie->len ? ie->name : "");
139 args[2] = watched[ie->wd];
140 args[3] = ie->len ? ie->name : NULL;
141 xspawn((char **)args);
143 i = sizeof(struct inotify_event) + ie->len;
145 ie = (void*)((char*)ie + i);