/* 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; xrun(prog_args); } 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; }