Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 1 | /* 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 Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * Use as follows: |
| 13 | * # inotifyd /user/space/agent dir/or/file/being/watched[:mask] ... |
| 14 | * |
Denys Vlasenko | 10ad622 | 2017-04-17 16:13:32 +0200 | [diff] [blame] | 15 | * When a filesystem event matching the specified mask is occurred on specified file (or directory) |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 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 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 29 | //config:config INOTIFYD |
| 30 | //config: bool "inotifyd" |
| 31 | //config: default n # doesn't build on Knoppix 5 |
| 32 | //config: help |
| 33 | //config: Simple inotify daemon. Reports filesystem changes. Requires |
| 34 | //config: kernel >= 2.6.13 |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 35 | |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame] | 36 | //applet:IF_INOTIFYD(APPLET(inotifyd, BB_DIR_SBIN, BB_SUID_DROP)) |
| 37 | |
| 38 | //kbuild:lib-$(CONFIG_INOTIFYD) += inotifyd.o |
| 39 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 40 | //usage:#define inotifyd_trivial_usage |
| 41 | //usage: "PROG FILE1[:MASK]..." |
| 42 | //usage:#define inotifyd_full_usage "\n\n" |
| 43 | //usage: "Run PROG on filesystem changes." |
| 44 | //usage: "\nWhen a filesystem event matching MASK occurs on FILEn," |
| 45 | //usage: "\nPROG ACTUAL_EVENTS FILEn [SUBFILE] is run." |
Flemming Madsen | ecccbac | 2011-10-10 20:24:02 +0200 | [diff] [blame] | 46 | //usage: "\nIf PROG is -, events are sent to stdout." |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 47 | //usage: "\nEvents:" |
| 48 | //usage: "\n a File is accessed" |
| 49 | //usage: "\n c File is modified" |
| 50 | //usage: "\n e Metadata changed" |
| 51 | //usage: "\n w Writable file is closed" |
| 52 | //usage: "\n 0 Unwritable file is closed" |
| 53 | //usage: "\n r File is opened" |
| 54 | //usage: "\n D File is deleted" |
| 55 | //usage: "\n M File is moved" |
| 56 | //usage: "\n u Backing fs is unmounted" |
| 57 | //usage: "\n o Event queue overflowed" |
| 58 | //usage: "\n x File can't be watched anymore" |
| 59 | //usage: "\nIf watching a directory:" |
Denys Vlasenko | 7849ccb | 2016-03-06 17:35:16 +0100 | [diff] [blame] | 60 | //usage: "\n y Subfile is moved into dir" |
| 61 | //usage: "\n m Subfile is moved out of dir" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 62 | //usage: "\n n Subfile is created" |
| 63 | //usage: "\n d Subfile is deleted" |
| 64 | //usage: "\n" |
| 65 | //usage: "\ninotifyd waits for PROG to exit." |
| 66 | //usage: "\nWhen x event happens for all FILEs, inotifyd exits." |
| 67 | |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 68 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 69 | #include "common_bufsiz.h" |
Bernhard Reutner-Fischer | 01ea971 | 2008-10-21 17:26:10 +0000 | [diff] [blame] | 70 | #include <sys/inotify.h> |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 71 | |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 72 | static const char mask_names[] ALIGN1 = |
| 73 | "a" // 0x00000001 File was accessed |
| 74 | "c" // 0x00000002 File was modified |
| 75 | "e" // 0x00000004 Metadata changed |
Denys Vlasenko | 5370bfb | 2009-09-06 02:58:59 +0200 | [diff] [blame] | 76 | "w" // 0x00000008 Writable file was closed |
| 77 | "0" // 0x00000010 Unwritable file closed |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 78 | "r" // 0x00000020 File was opened |
| 79 | "m" // 0x00000040 File was moved from X |
| 80 | "y" // 0x00000080 File was moved to Y |
| 81 | "n" // 0x00000100 Subfile was created |
| 82 | "d" // 0x00000200 Subfile was deleted |
| 83 | "D" // 0x00000400 Self was deleted |
| 84 | "M" // 0x00000800 Self was moved |
Denis Vlasenko | a09a42c | 2008-11-17 22:19:18 +0000 | [diff] [blame] | 85 | "\0" // 0x00001000 (unused) |
Denis Vlasenko | 9a4c0d5 | 2008-11-20 01:24:39 +0000 | [diff] [blame] | 86 | // Kernel events, always reported: |
Denis Vlasenko | a09a42c | 2008-11-17 22:19:18 +0000 | [diff] [blame] | 87 | "u" // 0x00002000 Backing fs was unmounted |
| 88 | "o" // 0x00004000 Event queued overflowed |
| 89 | "x" // 0x00008000 File is no longer watched (usually deleted) |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 90 | ; |
Denis Vlasenko | a09a42c | 2008-11-17 22:19:18 +0000 | [diff] [blame] | 91 | enum { |
| 92 | MASK_BITS = sizeof(mask_names) - 1 |
| 93 | }; |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 94 | |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 95 | int inotifyd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 9a4c0d5 | 2008-11-20 01:24:39 +0000 | [diff] [blame] | 96 | int inotifyd_main(int argc, char **argv) |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 97 | { |
Denis Vlasenko | b0e5d42 | 2008-11-14 21:34:13 +0000 | [diff] [blame] | 98 | int n; |
Denis Vlasenko | 9a4c0d5 | 2008-11-20 01:24:39 +0000 | [diff] [blame] | 99 | unsigned mask; |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 100 | struct pollfd pfd; |
Denis Vlasenko | 9a4c0d5 | 2008-11-20 01:24:39 +0000 | [diff] [blame] | 101 | char **watches; // names of files being watched |
| 102 | const char *args[5]; |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 103 | |
| 104 | // sanity check: agent and at least one watch must be given |
Denis Vlasenko | 9a4c0d5 | 2008-11-20 01:24:39 +0000 | [diff] [blame] | 105 | if (!argv[1] || !argv[2]) |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 106 | bb_show_usage(); |
| 107 | |
Denis Vlasenko | 9a4c0d5 | 2008-11-20 01:24:39 +0000 | [diff] [blame] | 108 | argv++; |
| 109 | // inotify_add_watch will number watched files |
| 110 | // starting from 1, thus watches[0] is unimportant, |
| 111 | // and 1st file name is watches[1]. |
| 112 | watches = argv; |
| 113 | args[0] = *argv; |
| 114 | args[4] = NULL; |
| 115 | argc -= 2; // number of files we watch |
| 116 | |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 117 | // open inotify |
| 118 | pfd.fd = inotify_init(); |
| 119 | if (pfd.fd < 0) |
| 120 | bb_perror_msg_and_die("no kernel support"); |
| 121 | |
Denis Vlasenko | 9a4c0d5 | 2008-11-20 01:24:39 +0000 | [diff] [blame] | 122 | // setup watches |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 123 | while (*++argv) { |
| 124 | char *path = *argv; |
| 125 | char *masks = strchr(path, ':'); |
Denis Vlasenko | 9a4c0d5 | 2008-11-20 01:24:39 +0000 | [diff] [blame] | 126 | |
| 127 | mask = 0x0fff; // assuming we want all non-kernel events |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 128 | // if mask is specified -> |
| 129 | if (masks) { |
| 130 | *masks = '\0'; // split path and mask |
| 131 | // convert mask names to mask bitset |
| 132 | mask = 0; |
| 133 | while (*++masks) { |
Denis Vlasenko | a09a42c | 2008-11-17 22:19:18 +0000 | [diff] [blame] | 134 | const char *found; |
| 135 | found = memchr(mask_names, *masks, MASK_BITS); |
| 136 | if (found) |
| 137 | mask |= (1 << (found - mask_names)); |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | // add watch |
Denis Vlasenko | b0e5d42 | 2008-11-14 21:34:13 +0000 | [diff] [blame] | 141 | n = inotify_add_watch(pfd.fd, path, mask); |
| 142 | if (n < 0) |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 143 | bb_perror_msg_and_die("add watch (%s) failed", path); |
Denis Vlasenko | b0e5d42 | 2008-11-14 21:34:13 +0000 | [diff] [blame] | 144 | //bb_error_msg("added %d [%s]:%4X", n, path, mask); |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // setup signals |
Denis Vlasenko | b0e5d42 | 2008-11-14 21:34:13 +0000 | [diff] [blame] | 148 | bb_signals(BB_FATAL_SIGS, record_signo); |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 149 | |
| 150 | // do watch |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 151 | pfd.events = POLLIN; |
Denis Vlasenko | b0e5d42 | 2008-11-14 21:34:13 +0000 | [diff] [blame] | 152 | while (1) { |
Denis Vlasenko | 9a4c0d5 | 2008-11-20 01:24:39 +0000 | [diff] [blame] | 153 | int len; |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 154 | void *buf; |
| 155 | struct inotify_event *ie; |
Denis Vlasenko | b0e5d42 | 2008-11-14 21:34:13 +0000 | [diff] [blame] | 156 | again: |
| 157 | if (bb_got_signal) |
| 158 | break; |
| 159 | n = poll(&pfd, 1, -1); |
Denis Vlasenko | 289ff0e | 2008-11-14 21:52:16 +0000 | [diff] [blame] | 160 | // Signal interrupted us? |
Denis Vlasenko | b0e5d42 | 2008-11-14 21:34:13 +0000 | [diff] [blame] | 161 | if (n < 0 && errno == EINTR) |
| 162 | goto again; |
| 163 | // Under Linux, above if() is not necessary. |
| 164 | // Non-fatal signals, e.g. SIGCHLD, when set to SIG_DFL, |
| 165 | // are not interrupting poll(). |
| 166 | // Thus we can just break if n <= 0 (see below), |
| 167 | // because EINTR will happen only on SIGTERM et al. |
| 168 | // But this might be not true under other Unixes, |
| 169 | // and is generally way too subtle to depend on. |
| 170 | if (n <= 0) // strange error? |
| 171 | break; |
| 172 | |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 173 | // read out all pending events |
Denis Vlasenko | 9a4c0d5 | 2008-11-20 01:24:39 +0000 | [diff] [blame] | 174 | // (NB: len must be int, not ssize_t or long!) |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 175 | #define eventbuf bb_common_bufsiz1 |
| 176 | setup_common_bufsiz(); |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 177 | xioctl(pfd.fd, FIONREAD, &len); |
Denys Vlasenko | 9de2e5a | 2016-04-21 18:38:51 +0200 | [diff] [blame] | 178 | ie = buf = (len <= COMMON_BUFSIZE) ? eventbuf : xmalloc(len); |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 179 | len = full_read(pfd.fd, buf, len); |
| 180 | // process events. N.B. events may vary in length |
| 181 | while (len > 0) { |
| 182 | int i; |
Denis Vlasenko | a09a42c | 2008-11-17 22:19:18 +0000 | [diff] [blame] | 183 | // cache relevant events mask |
| 184 | unsigned m = ie->mask & ((1 << MASK_BITS) - 1); |
| 185 | if (m) { |
| 186 | char events[MASK_BITS + 1]; |
| 187 | char *s = events; |
| 188 | for (i = 0; i < MASK_BITS; ++i, m >>= 1) { |
| 189 | if ((m & 1) && (mask_names[i] != '\0')) |
| 190 | *s++ = mask_names[i]; |
| 191 | } |
| 192 | *s = '\0'; |
Flemming Madsen | ecccbac | 2011-10-10 20:24:02 +0200 | [diff] [blame] | 193 | if (LONE_CHAR(args[0], '-')) { |
| 194 | /* "inotifyd - FILE": built-in echo */ |
| 195 | printf(ie->len ? "%s\t%s\t%s\n" : "%s\t%s\n", events, |
| 196 | watches[ie->wd], |
| 197 | ie->name); |
| 198 | fflush(stdout); |
| 199 | } else { |
| 200 | // bb_error_msg("exec %s %08X\t%s\t%s\t%s", args[0], |
| 201 | // ie->mask, events, watches[ie->wd], ie->len ? ie->name : ""); |
| 202 | args[1] = events; |
| 203 | args[2] = watches[ie->wd]; |
| 204 | args[3] = ie->len ? ie->name : NULL; |
| 205 | spawn_and_wait((char **)args); |
| 206 | } |
Denis Vlasenko | 9a4c0d5 | 2008-11-20 01:24:39 +0000 | [diff] [blame] | 207 | // we are done if all files got final x event |
Denis Vlasenko | d723fb1 | 2008-11-29 09:07:24 +0000 | [diff] [blame] | 208 | if (ie->mask & 0x8000) { |
Denis Vlasenko | 9a4c0d5 | 2008-11-20 01:24:39 +0000 | [diff] [blame] | 209 | if (--argc <= 0) |
| 210 | goto done; |
Denis Vlasenko | d723fb1 | 2008-11-29 09:07:24 +0000 | [diff] [blame] | 211 | inotify_rm_watch(pfd.fd, ie->wd); |
| 212 | } |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 213 | } |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 214 | // next event |
| 215 | i = sizeof(struct inotify_event) + ie->len; |
| 216 | len -= i; |
| 217 | ie = (void*)((char*)ie + i); |
| 218 | } |
| 219 | if (eventbuf != buf) |
| 220 | free(buf); |
Denis Vlasenko | 9a4c0d5 | 2008-11-20 01:24:39 +0000 | [diff] [blame] | 221 | } // while (1) |
| 222 | done: |
| 223 | return bb_got_signal; |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 224 | } |