123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- #include "libbb.h"
- #include "common_bufsiz.h"
- #include <sys/inotify.h>
- static const char mask_names[] ALIGN1 =
- "a"
- "c"
- "e"
- "w"
- "0"
- "r"
- "m"
- "y"
- "n"
- "d"
- "D"
- "M"
- "\0"
-
- "u"
- "o"
- "x"
- ;
- enum {
- MASK_BITS = sizeof(mask_names) - 1
- };
- int inotifyd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int inotifyd_main(int argc, char **argv)
- {
- int n;
- unsigned mask;
- struct pollfd pfd;
- char **watches;
- const char *args[5];
-
- if (!argv[1] || !argv[2])
- bb_show_usage();
- argv++;
-
-
-
- watches = argv;
- args[0] = *argv;
- args[4] = NULL;
- argc -= 2;
-
- pfd.fd = inotify_init();
- if (pfd.fd < 0)
- bb_perror_msg_and_die("no kernel support");
-
- while (*++argv) {
- char *path = *argv;
- char *masks = strchr(path, ':');
- mask = 0x0fff;
-
- if (masks) {
- *masks = '\0';
-
- mask = 0;
- while (*++masks) {
- const char *found;
- found = memchr(mask_names, *masks, MASK_BITS);
- if (found)
- mask |= (1 << (found - mask_names));
- }
- }
-
- n = inotify_add_watch(pfd.fd, path, mask);
- if (n < 0)
- bb_perror_msg_and_die("add watch (%s) failed", path);
-
- }
-
- bb_signals(BB_FATAL_SIGS, record_signo);
-
- pfd.events = POLLIN;
- while (1) {
- int len;
- void *buf;
- struct inotify_event *ie;
- again:
- if (bb_got_signal)
- break;
- n = poll(&pfd, 1, -1);
-
- if (n < 0 && errno == EINTR)
- goto again;
-
-
-
-
-
-
-
- if (n <= 0)
- break;
-
-
- #define eventbuf bb_common_bufsiz1
- setup_common_bufsiz();
- xioctl(pfd.fd, FIONREAD, &len);
- ie = buf = (len <= COMMON_BUFSIZE) ? eventbuf : xmalloc(len);
- len = full_read(pfd.fd, buf, len);
-
- while (len > 0) {
- int i;
-
- unsigned m = ie->mask & ((1 << MASK_BITS) - 1);
- if (m) {
- char events[MASK_BITS + 1];
- char *s = events;
- for (i = 0; i < MASK_BITS; ++i, m >>= 1) {
- if ((m & 1) && (mask_names[i] != '\0'))
- *s++ = mask_names[i];
- }
- *s = '\0';
- if (LONE_CHAR(args[0], '-')) {
-
- printf(ie->len ? "%s\t%s\t%s\n" : "%s\t%s\n", events,
- watches[ie->wd],
- ie->name);
- fflush(stdout);
- } else {
- args[1] = events;
- args[2] = watches[ie->wd];
- args[3] = ie->len ? ie->name : NULL;
- spawn_and_wait((char **)args);
- }
-
- if (ie->mask & 0x8000) {
- if (--argc <= 0)
- goto done;
- inotify_rm_watch(pfd.fd, ie->wd);
- }
- }
-
- i = sizeof(struct inotify_event) + ie->len;
- len -= i;
- ie = (void*)((char*)ie + i);
- }
- if (eventbuf != buf)
- free(buf);
- }
- done:
- return bb_got_signal;
- }
|