diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2008-11-20 01:24:39 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2008-11-20 01:24:39 +0000 |
| commit | 9a4c0d59a79efeba4e1c5feed7d07b7c6b6b04ce (patch) | |
| tree | 666efa8898107af6d15d3b910e5e2459dbea5c33 | |
| parent | 4ae1e13d3f4716a35acf6186f5485e53da39829d (diff) | |
| download | busybox-w32-9a4c0d59a79efeba4e1c5feed7d07b7c6b6b04ce.tar.gz busybox-w32-9a4c0d59a79efeba4e1c5feed7d07b7c6b6b04ce.tar.bz2 busybox-w32-9a4c0d59a79efeba4e1c5feed7d07b7c6b6b04ce.zip | |
inotifyd: exit if x event happened for all files
fix FIONREAD parameter type
fix default mask code
shrink help text
function old new delta
inotifyd_main 497 506 +9
packed_usage 25446 25431 -15
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 9/-15) Total: -6 bytes
| -rw-r--r-- | include/usage.h | 16 | ||||
| -rw-r--r-- | miscutils/inotifyd.c | 42 |
2 files changed, 36 insertions, 22 deletions
diff --git a/include/usage.h b/include/usage.h index 9c0c9d761..5223e9e18 100644 --- a/include/usage.h +++ b/include/usage.h | |||
| @@ -1838,16 +1838,11 @@ | |||
| 1838 | " ::shutdown:/sbin/swapoff -a\n" | 1838 | " ::shutdown:/sbin/swapoff -a\n" |
| 1839 | 1839 | ||
| 1840 | #define inotifyd_trivial_usage \ | 1840 | #define inotifyd_trivial_usage \ |
| 1841 | "/user/space/agent dir/or/file/being/watched[:mask] ..." | 1841 | "PROG FILE1[:MASK] ..." |
| 1842 | #define inotifyd_full_usage "\n\n" \ | 1842 | #define inotifyd_full_usage "\n\n" \ |
| 1843 | "Run userspace agent on filesystem changes." \ | 1843 | "Run PROG on filesystem changes." \ |
| 1844 | "\nWhen a filesystem event matching the mask occurs" \ | 1844 | "\nWhen a filesystem event matching MASK occurs on FILEn," \ |
| 1845 | "\non specified file /user/space/agent is run" \ | 1845 | "\nPROG <actual_event(s)> <FILEn> [<subfile_name>] is run." \ |
| 1846 | "\nwith the parameters:" \ | ||
| 1847 | "\n1. actual event(s)" \ | ||
| 1848 | "\n2. file name" \ | ||
| 1849 | "\n3. name of subfile (if any)" \ | ||
| 1850 | "\ninotifyd waits for agent to exit." \ | ||
| 1851 | "\nEvents:" \ | 1846 | "\nEvents:" \ |
| 1852 | "\n a File is accessed" \ | 1847 | "\n a File is accessed" \ |
| 1853 | "\n c File is modified" \ | 1848 | "\n c File is modified" \ |
| @@ -1865,6 +1860,9 @@ | |||
| 1865 | "\n y Subfile is moved out of dir" \ | 1860 | "\n y Subfile is moved out of dir" \ |
| 1866 | "\n n Subfile is created" \ | 1861 | "\n n Subfile is created" \ |
| 1867 | "\n d Subfile is deleted" \ | 1862 | "\n d Subfile is deleted" \ |
| 1863 | "\n" \ | ||
| 1864 | "\ninotifyd waits for PROG to exit." \ | ||
| 1865 | "\nWhen x event happens for all FILEs, inotifyd exits" \ | ||
| 1868 | 1866 | ||
| 1869 | #define insmod_trivial_usage \ | 1867 | #define insmod_trivial_usage \ |
| 1870 | USE_FEATURE_2_4_MODULES("[OPTION]... ") "MODULE [symbol=value]..." | 1868 | USE_FEATURE_2_4_MODULES("[OPTION]... ") "MODULE [symbol=value]..." |
diff --git a/miscutils/inotifyd.c b/miscutils/inotifyd.c index 656510453..fdf2a2c2b 100644 --- a/miscutils/inotifyd.c +++ b/miscutils/inotifyd.c | |||
| @@ -44,6 +44,7 @@ static const char mask_names[] ALIGN1 = | |||
| 44 | "D" // 0x00000400 Self was deleted | 44 | "D" // 0x00000400 Self was deleted |
| 45 | "M" // 0x00000800 Self was moved | 45 | "M" // 0x00000800 Self was moved |
| 46 | "\0" // 0x00001000 (unused) | 46 | "\0" // 0x00001000 (unused) |
| 47 | // Kernel events, always reported: | ||
| 47 | "u" // 0x00002000 Backing fs was unmounted | 48 | "u" // 0x00002000 Backing fs was unmounted |
| 48 | "o" // 0x00004000 Event queued overflowed | 49 | "o" // 0x00004000 Event queued overflowed |
| 49 | "x" // 0x00008000 File is no longer watched (usually deleted) | 50 | "x" // 0x00008000 File is no longer watched (usually deleted) |
| @@ -56,27 +57,38 @@ extern int inotify_init(void); | |||
| 56 | extern int inotify_add_watch(int fd, const char *path, uint32_t mask); | 57 | extern int inotify_add_watch(int fd, const char *path, uint32_t mask); |
| 57 | 58 | ||
| 58 | int inotifyd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 59 | int inotifyd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 59 | int inotifyd_main(int argc UNUSED_PARAM, char **argv) | 60 | int inotifyd_main(int argc, char **argv) |
| 60 | { | 61 | { |
| 61 | int n; | 62 | int n; |
| 62 | unsigned mask = IN_ALL_EVENTS; // assume we want all events | 63 | unsigned mask; |
| 63 | struct pollfd pfd; | 64 | struct pollfd pfd; |
| 64 | char **watched = ++argv; // watched name list | 65 | char **watches; // names of files being watched |
| 65 | const char *args[] = { *argv, NULL, NULL, NULL, NULL }; | 66 | const char *args[5]; |
| 66 | 67 | ||
| 67 | // sanity check: agent and at least one watch must be given | 68 | // sanity check: agent and at least one watch must be given |
| 68 | if (!argv[0] || !argv[1]) | 69 | if (!argv[1] || !argv[2]) |
| 69 | bb_show_usage(); | 70 | bb_show_usage(); |
| 70 | 71 | ||
| 72 | argv++; | ||
| 73 | // inotify_add_watch will number watched files | ||
| 74 | // starting from 1, thus watches[0] is unimportant, | ||
| 75 | // and 1st file name is watches[1]. | ||
| 76 | watches = argv; | ||
| 77 | args[0] = *argv; | ||
| 78 | args[4] = NULL; | ||
| 79 | argc -= 2; // number of files we watch | ||
| 80 | |||
| 71 | // open inotify | 81 | // open inotify |
| 72 | pfd.fd = inotify_init(); | 82 | pfd.fd = inotify_init(); |
| 73 | if (pfd.fd < 0) | 83 | if (pfd.fd < 0) |
| 74 | bb_perror_msg_and_die("no kernel support"); | 84 | bb_perror_msg_and_die("no kernel support"); |
| 75 | 85 | ||
| 76 | // setup watched | 86 | // setup watches |
| 77 | while (*++argv) { | 87 | while (*++argv) { |
| 78 | char *path = *argv; | 88 | char *path = *argv; |
| 79 | char *masks = strchr(path, ':'); | 89 | char *masks = strchr(path, ':'); |
| 90 | |||
| 91 | mask = 0x0fff; // assuming we want all non-kernel events | ||
| 80 | // if mask is specified -> | 92 | // if mask is specified -> |
| 81 | if (masks) { | 93 | if (masks) { |
| 82 | *masks = '\0'; // split path and mask | 94 | *masks = '\0'; // split path and mask |
| @@ -102,10 +114,9 @@ int inotifyd_main(int argc UNUSED_PARAM, char **argv) | |||
| 102 | // do watch | 114 | // do watch |
| 103 | pfd.events = POLLIN; | 115 | pfd.events = POLLIN; |
| 104 | while (1) { | 116 | while (1) { |
| 105 | ssize_t len; | 117 | int len; |
| 106 | void *buf; | 118 | void *buf; |
| 107 | struct inotify_event *ie; | 119 | struct inotify_event *ie; |
| 108 | |||
| 109 | again: | 120 | again: |
| 110 | if (bb_got_signal) | 121 | if (bb_got_signal) |
| 111 | break; | 122 | break; |
| @@ -124,6 +135,7 @@ int inotifyd_main(int argc UNUSED_PARAM, char **argv) | |||
| 124 | break; | 135 | break; |
| 125 | 136 | ||
| 126 | // read out all pending events | 137 | // read out all pending events |
| 138 | // (NB: len must be int, not ssize_t or long!) | ||
| 127 | xioctl(pfd.fd, FIONREAD, &len); | 139 | xioctl(pfd.fd, FIONREAD, &len); |
| 128 | #define eventbuf bb_common_bufsiz1 | 140 | #define eventbuf bb_common_bufsiz1 |
| 129 | ie = buf = (len <= sizeof(eventbuf)) ? eventbuf : xmalloc(len); | 141 | ie = buf = (len <= sizeof(eventbuf)) ? eventbuf : xmalloc(len); |
| @@ -142,11 +154,15 @@ int inotifyd_main(int argc UNUSED_PARAM, char **argv) | |||
| 142 | } | 154 | } |
| 143 | *s = '\0'; | 155 | *s = '\0'; |
| 144 | // bb_error_msg("exec %s %08X\t%s\t%s\t%s", args[0], | 156 | // bb_error_msg("exec %s %08X\t%s\t%s\t%s", args[0], |
| 145 | // ie->mask, events, watched[ie->wd], ie->len ? ie->name : ""); | 157 | // ie->mask, events, watches[ie->wd], ie->len ? ie->name : ""); |
| 146 | args[1] = events; | 158 | args[1] = events; |
| 147 | args[2] = watched[ie->wd]; | 159 | args[2] = watches[ie->wd]; |
| 148 | args[3] = ie->len ? ie->name : NULL; | 160 | args[3] = ie->len ? ie->name : NULL; |
| 149 | wait4pid(xspawn((char **)args)); | 161 | wait4pid(xspawn((char **)args)); |
| 162 | // we are done if all files got final x event | ||
| 163 | if (ie->mask & 0x8000) | ||
| 164 | if (--argc <= 0) | ||
| 165 | goto done; | ||
| 150 | } | 166 | } |
| 151 | // next event | 167 | // next event |
| 152 | i = sizeof(struct inotify_event) + ie->len; | 168 | i = sizeof(struct inotify_event) + ie->len; |
| @@ -155,7 +171,7 @@ int inotifyd_main(int argc UNUSED_PARAM, char **argv) | |||
| 155 | } | 171 | } |
| 156 | if (eventbuf != buf) | 172 | if (eventbuf != buf) |
| 157 | free(buf); | 173 | free(buf); |
| 158 | } | 174 | } // while (1) |
| 159 | 175 | done: | |
| 160 | return EXIT_SUCCESS; | 176 | return bb_got_signal; |
| 161 | } | 177 | } |
