aboutsummaryrefslogtreecommitdiff
path: root/miscutils
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-11-20 01:24:39 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-11-20 01:24:39 +0000
commit9a4c0d59a79efeba4e1c5feed7d07b7c6b6b04ce (patch)
tree666efa8898107af6d15d3b910e5e2459dbea5c33 /miscutils
parent4ae1e13d3f4716a35acf6186f5485e53da39829d (diff)
downloadbusybox-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
Diffstat (limited to 'miscutils')
-rw-r--r--miscutils/inotifyd.c42
1 files changed, 29 insertions, 13 deletions
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);
56extern int inotify_add_watch(int fd, const char *path, uint32_t mask); 57extern int inotify_add_watch(int fd, const char *path, uint32_t mask);
57 58
58int inotifyd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 59int inotifyd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
59int inotifyd_main(int argc UNUSED_PARAM, char **argv) 60int 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}