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 | * |
| 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" |
Bernhard Reutner-Fischer | 01ea971 | 2008-10-21 17:26:10 +0000 | [diff] [blame] | 31 | #include <sys/inotify.h> |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 33 | static 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 | |
| 48 | extern int inotify_init(void); |
| 49 | extern int inotify_add_watch(int fd, const char *path, uint32_t mask); |
| 50 | |
| 51 | int inotifyd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 52 | int inotifyd_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 53 | { |
Denis Vlasenko | b0e5d42 | 2008-11-14 21:34:13 +0000 | [diff] [blame] | 54 | int n; |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 55 | unsigned mask = IN_ALL_EVENTS; // assume we want all events |
| 56 | struct pollfd pfd; |
| 57 | char **watched = ++argv; // watched name list |
| 58 | const char *args[] = { *argv, NULL, NULL, NULL, NULL }; |
| 59 | |
| 60 | // sanity check: agent and at least one watch must be given |
Denis Vlasenko | 2479cd4 | 2008-11-14 22:14:12 +0000 | [diff] [blame] | 61 | if (!argv[0] || !argv[1]) |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 62 | bb_show_usage(); |
| 63 | |
| 64 | // open inotify |
| 65 | pfd.fd = inotify_init(); |
| 66 | if (pfd.fd < 0) |
| 67 | bb_perror_msg_and_die("no kernel support"); |
| 68 | |
| 69 | // setup watched |
| 70 | while (*++argv) { |
| 71 | char *path = *argv; |
| 72 | char *masks = strchr(path, ':'); |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 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 |
Denis Vlasenko | b0e5d42 | 2008-11-14 21:34:13 +0000 | [diff] [blame] | 86 | n = inotify_add_watch(pfd.fd, path, mask); |
| 87 | if (n < 0) |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 88 | bb_perror_msg_and_die("add watch (%s) failed", path); |
Denis Vlasenko | b0e5d42 | 2008-11-14 21:34:13 +0000 | [diff] [blame] | 89 | //bb_error_msg("added %d [%s]:%4X", n, path, mask); |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // setup signals |
Denis Vlasenko | b0e5d42 | 2008-11-14 21:34:13 +0000 | [diff] [blame] | 93 | bb_signals(BB_FATAL_SIGS, record_signo); |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 94 | |
| 95 | // do watch |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 96 | pfd.events = POLLIN; |
Denis Vlasenko | b0e5d42 | 2008-11-14 21:34:13 +0000 | [diff] [blame] | 97 | while (1) { |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 98 | ssize_t len; |
| 99 | void *buf; |
| 100 | struct inotify_event *ie; |
| 101 | |
Denis Vlasenko | b0e5d42 | 2008-11-14 21:34:13 +0000 | [diff] [blame] | 102 | again: |
| 103 | if (bb_got_signal) |
| 104 | break; |
| 105 | n = poll(&pfd, 1, -1); |
Denis Vlasenko | 289ff0e | 2008-11-14 21:52:16 +0000 | [diff] [blame] | 106 | // Signal interrupted us? |
Denis Vlasenko | b0e5d42 | 2008-11-14 21:34:13 +0000 | [diff] [blame] | 107 | if (n < 0 && errno == EINTR) |
| 108 | goto again; |
| 109 | // Under Linux, above if() is not necessary. |
| 110 | // Non-fatal signals, e.g. SIGCHLD, when set to SIG_DFL, |
| 111 | // are not interrupting poll(). |
| 112 | // Thus we can just break if n <= 0 (see below), |
| 113 | // because EINTR will happen only on SIGTERM et al. |
| 114 | // But this might be not true under other Unixes, |
| 115 | // and is generally way too subtle to depend on. |
| 116 | if (n <= 0) // strange error? |
| 117 | break; |
| 118 | |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 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 |
| 125 | while (len > 0) { |
| 126 | int i; |
Denis Vlasenko | b0e5d42 | 2008-11-14 21:34:13 +0000 | [diff] [blame] | 127 | char events[sizeof(mask_names)]; |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 128 | char *s = events; |
| 129 | unsigned m = ie->mask; |
| 130 | |
Denis Vlasenko | b0e5d42 | 2008-11-14 21:34:13 +0000 | [diff] [blame] | 131 | for (i = 0; i < sizeof(mask_names)-1; ++i, m >>= 1) { |
| 132 | if (m & 1) |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 133 | *s++ = mask_names[i]; |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 134 | } |
| 135 | *s = '\0'; |
Denis Vlasenko | b0e5d42 | 2008-11-14 21:34:13 +0000 | [diff] [blame] | 136 | //bb_error_msg("exec %s %08X\t%s\t%s\t%s", agent, |
| 137 | // ie->mask, events, watched[ie->wd], ie->len ? ie->name : ""); |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 138 | args[1] = events; |
| 139 | args[2] = watched[ie->wd]; |
| 140 | args[3] = ie->len ? ie->name : NULL; |
Denis Vlasenko | b0e5d42 | 2008-11-14 21:34:13 +0000 | [diff] [blame] | 141 | wait4pid(xspawn((char **)args)); |
Denis Vlasenko | 8e2c9e1 | 2008-05-24 20:47:18 +0000 | [diff] [blame] | 142 | // next event |
| 143 | i = sizeof(struct inotify_event) + ie->len; |
| 144 | len -= i; |
| 145 | ie = (void*)((char*)ie + i); |
| 146 | } |
| 147 | if (eventbuf != buf) |
| 148 | free(buf); |
| 149 | } |
| 150 | |
| 151 | return EXIT_SUCCESS; |
| 152 | } |