blob: 3b11dc271e6dacb6678d94d68a58f60dc394ded7 [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 *
8 * Licensed under GPLv2, see file LICENSE in this tarball for details.
9 */
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
30#include "libbb.h"
31#include <linux/inotify.h>
32
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +000033static const char mask_names[] ALIGN1 =
34 "a" // 0x00000001 File was accessed
35 "c" // 0x00000002 File was modified
36 "e" // 0x00000004 Metadata changed
37 "w" // 0x00000008 Writtable file was closed
38 "0" // 0x00000010 Unwrittable file closed
39 "r" // 0x00000020 File was opened
40 "m" // 0x00000040 File was moved from X
41 "y" // 0x00000080 File was moved to Y
42 "n" // 0x00000100 Subfile was created
43 "d" // 0x00000200 Subfile was deleted
44 "D" // 0x00000400 Self was deleted
45 "M" // 0x00000800 Self was moved
46;
47
48extern int inotify_init(void);
49extern int inotify_add_watch(int fd, const char *path, uint32_t mask);
50
51int inotifyd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000052int inotifyd_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +000053{
54 unsigned mask = IN_ALL_EVENTS; // assume we want all events
55 struct pollfd pfd;
56 char **watched = ++argv; // watched name list
57 const char *args[] = { *argv, NULL, NULL, NULL, NULL };
58
59 // sanity check: agent and at least one watch must be given
60 if (!argv[1])
61 bb_show_usage();
62
63 // open inotify
64 pfd.fd = inotify_init();
65 if (pfd.fd < 0)
66 bb_perror_msg_and_die("no kernel support");
67
68 // setup watched
69 while (*++argv) {
70 char *path = *argv;
71 char *masks = strchr(path, ':');
72 int wd; // watch descriptor
73 // if mask is specified ->
74 if (masks) {
75 *masks = '\0'; // split path and mask
76 // convert mask names to mask bitset
77 mask = 0;
78 while (*++masks) {
79 int i = strchr(mask_names, *masks) - mask_names;
80 if (i >= 0) {
81 mask |= (1 << i);
82 }
83 }
84 }
85 // add watch
86 wd = inotify_add_watch(pfd.fd, path, mask);
87 if (wd < 0) {
88 bb_perror_msg_and_die("add watch (%s) failed", path);
89// } else {
90// bb_error_msg("added %d [%s]:%4X", wd, path, mask);
91 }
92 }
93
94 // setup signals
95 bb_signals(0
96 + (1 << SIGHUP)
97 + (1 << SIGINT)
98 + (1 << SIGTERM)
99 + (1 << SIGPIPE)
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000100 , record_signo);
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +0000101
102 // do watch
103
104// pfd.fd = fd;
105 pfd.events = POLLIN;
106
Denis Vlasenko08ea11a2008-09-11 19:51:11 +0000107 while (!bb_got_signal && poll(&pfd, 1, -1) > 0) {
Denis Vlasenko8e2c9e12008-05-24 20:47:18 +0000108 ssize_t len;
109 void *buf;
110 struct inotify_event *ie;
111
112 // read out all pending events
113 xioctl(pfd.fd, FIONREAD, &len);
114#define eventbuf bb_common_bufsiz1
115 ie = buf = (len <= sizeof(eventbuf)) ? eventbuf : xmalloc(len);
116 len = full_read(pfd.fd, buf, len);
117 // process events. N.B. events may vary in length
118 while (len > 0) {
119 int i;
120 char events[12];
121 char *s = events;
122 unsigned m = ie->mask;
123
124 for (i = 0; i < 12; ++i, m >>= 1) {
125 if (m & 1) {
126 *s++ = mask_names[i];
127 }
128 }
129 *s = '\0';
130// bb_error_msg("exec %s %08X\t%s\t%s\t%s", agent, ie->mask, events, watched[ie->wd], ie->len ? ie->name : "");
131 args[1] = events;
132 args[2] = watched[ie->wd];
133 args[3] = ie->len ? ie->name : NULL;
134 xspawn((char **)args);
135 // next event
136 i = sizeof(struct inotify_event) + ie->len;
137 len -= i;
138 ie = (void*)((char*)ie + i);
139 }
140 if (eventbuf != buf)
141 free(buf);
142 }
143
144 return EXIT_SUCCESS;
145}