commit 9f1bb21cfb715ea4e2e095ea9697a28e9d529c32
parent 0aff97c697511ce29bd95b2bb1a68f5a95d6f2ed
Author: Willy Goiffon <dev@z3bra.org>
Date: Mon, 2 Mar 2020 15:01:56 +0100
Run specified command on triggered events
Diffstat:
1 file changed, 19 insertions(+), 2 deletions(-)
diff --git a/wendy.c b/wendy.c
@@ -42,7 +42,7 @@ int verbose = 0;
void
usage(char *name)
{
- fprintf(stderr, "usage: %s [-vdr] [-m mask] [-f file]\n", name);
+ fprintf(stderr, "usage: %s [-vdr] [-m mask] [-f file] [command [args...]]\n", name);
exit(1);
}
@@ -110,7 +110,7 @@ main (int argc, char **argv)
uint32_t mask = MASK;
ssize_t len, off = 0;
char path[PATH_MAX];
- char *argv0 = NULL;
+ char **cmd, *argv0 = NULL;
struct watcher *tmp, *w;
struct inotify_event *e;
@@ -139,6 +139,9 @@ main (int argc, char **argv)
usage(argv0);
} ARGEND;
+ /* remaining arguments is the command to run on event */
+ cmd = (argc > 0) ? argv : NULL;
+
/* ensure that only directories are watched for */
if (dflag)
mask |= IN_ONLYDIR;
@@ -159,6 +162,20 @@ main (int argc, char **argv)
fflush(stdout);
}
+ /* orphan process */
+ if (cmd) {
+ if (!fork()) {
+ if (!fork()) {
+ setenv("WENDY_INODE", wdpath(e, w), 1);
+ setenv("WENDY_EVENT", evname[e->mask & IN_ALL_EVENTS], 1);
+ execvp(cmd[0], cmd);
+ return -1; /* NOTREACHED */
+ }
+ return 0;
+ }
+ wait(NULL);
+ }
+
switch(e->mask & IN_ALL_EVENTS) {
case IN_CREATE:
/* Watch subdirectories upon creation */