From e70eea41eeed05e0ac938a6aceab27c918090fde Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 20 Aug 2014 22:23:39 -0500 Subject: Promote inotifyd to other. --- toys/other/inotifyd.c | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 toys/other/inotifyd.c (limited to 'toys/other/inotifyd.c') diff --git a/toys/other/inotifyd.c b/toys/other/inotifyd.c new file mode 100644 index 00000000..59c9a50c --- /dev/null +++ b/toys/other/inotifyd.c @@ -0,0 +1,126 @@ +/* inotifyd.c - inotify daemon. + * + * Copyright 2013 Ashwini Kumar + * Copyright 2013 Kyungwan Han + * + * No Standard. + +USE_INOTIFYD(NEWTOY(inotifyd, "<2", TOYFLAG_USR|TOYFLAG_BIN)) + +config INOTIFYD + bool "inotifyd" + default y + help + usage: inotifyd PROG FILE[:MASK] ... + + When a filesystem event matching MASK occurs to a FILE, run PROG as: + + PROG EVENTS FILE [DIRFILE] + + If PROG is "-" events are sent to stdout. + + This file is: + a accessed c modified e metadata change w closed (writable) + r opened D deleted M moved 0 closed (unwritable) + u unmounted o overflow x unwatchable + + A file in this directory is: + m moved in y moved out n created d deleted + + When x event happens for all FILEs, inotifyd exits (after waiting for PROG). +*/ + +#define FOR_inotifyd +#include "toys.h" +#include + +void inotifyd_main(void) +{ + struct pollfd fds; + char *prog_args[5], **ss = toys.optargs; + char *masklist ="acew0rmyndDM uox"; + + fds.events = POLLIN; + + *prog_args = *toys.optargs; + prog_args[4] = 0; + if ((fds.fd = inotify_init()) == -1) perror_exit(0); + + // Track number of watched files. First one was program to run. + toys.optc--; + + while (*++ss) { + char *path = *ss, *masks = strchr(*ss, ':'); + int i, mask = 0; + + if (!masks) mask = 0xfff; // default to all + else{ + *masks++ = 0; + for (*masks++ = 0; *masks; masks++) { + i = stridx(masklist, *masks);; + if (i == -1) error_exit("bad mask '%c'", *masks); + mask |= 1<len is off end of bufer + if (left >= size) size += event->len; + if (left < size) break; + + if (event->mask) { + char *s = toybuf, *m; + + for (m = masklist; *m; m++) + if (event->mask & (1<<(m-masklist))) *s++ = *m; + *s = 0; + + if (**prog_args == '-' && !prog_args[0][1]) { + xprintf("%s\t%s\t%s\n" + 3*!event->len, toybuf, + toys.optargs[event->wd], event->name); + } else { + prog_args[1] = toybuf; + prog_args[2] = toys.optargs[event->wd]; + prog_args[3] = event->len ? event->name : 0; + xpclose(xpopen(prog_args, 0), 0); + } + + if (event->mask & IN_IGNORED) { + if (--toys.optc <= 0) { + free(buf); + + goto done; + } + inotify_rm_watch(fds.fd, event->wd); + } + } + event = (void*)(size + (char*)event); + } + free(buf); + } + +done: + toys.exitval = !!toys.signal; +} -- cgit v1.2.3