iexec: cleanup

This commit is contained in:
Přemysl Eric Janouch 2023-06-19 17:11:16 +02:00
parent 59b78ebc5c
commit 957aed63a8
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 21 additions and 15 deletions

28
iexec.c
View File

@ -1,7 +1,7 @@
/* /*
* iexec.c: run a program and restart on file change * iexec.c: run a program and restart on file change
* *
* Copyright (c) 2017, Přemysl Eric Janouch <p@janouch.name> * Copyright (c) 2017 - 2023, Přemysl Eric Janouch <p@janouch.name>
* *
* Permission to use, copy, modify, and/or distribute this software for any * Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted. * purpose with or without fee is hereby granted.
@ -29,21 +29,26 @@ static bool g_restarting = false;
static int g_inotify_fd, g_inotify_wd; static int g_inotify_fd, g_inotify_wd;
static void static void
handle_file_change (const char *base) handle_inotify_event (const struct inotify_event *e, const char *base)
{ {
char buf[4096]; ssize_t len; const struct inotify_event *e;
while ((len = read (g_inotify_fd, buf, sizeof buf)) > 0)
for (char *ptr = buf; ptr < buf + len; ptr += sizeof *e + e->len)
{
e = (const struct inotify_event *) buf;
if (e->wd != g_inotify_wd || strcmp (e->name, base)) if (e->wd != g_inotify_wd || strcmp (e->name, base))
continue; return;
print_debug ("file changed, killing child"); print_debug ("file changed, killing child");
g_restarting = true; g_restarting = true;
if (kill (g_child, SIGINT)) if (g_child >= 0 && kill (g_child, SIGINT))
print_error ("kill: %s", strerror (errno)); print_error ("kill: %s", strerror (errno));
} }
static void
handle_file_change (const char *base)
{
char buf[4096];
ssize_t len = 0;
struct inotify_event *e = NULL;
while ((len = read (g_inotify_fd, buf, sizeof buf)) > 0)
for (char *ptr = buf; ptr < buf + len; ptr += sizeof *e + e->len)
handle_inotify_event ((e = (struct inotify_event *) buf), base);
} }
static void static void
@ -148,12 +153,13 @@ main (int argc, char *argv[])
if (sigprocmask (SIG_BLOCK, &chld, &orig)) if (sigprocmask (SIG_BLOCK, &chld, &orig))
exit_fatal ("sigprocmask: %s", strerror (errno)); exit_fatal ("sigprocmask: %s", strerror (errno));
char *path = xstrdup (argv[0]); char *path = NULL;
char *dir = dirname ((path = xstrdup (argv[0])));
if ((g_inotify_fd = inotify_init1 (IN_NONBLOCK)) < 0) if ((g_inotify_fd = inotify_init1 (IN_NONBLOCK)) < 0)
exit_fatal ("inotify_init1: %s", strerror (errno)); exit_fatal ("inotify_init1: %s", strerror (errno));
if ((g_inotify_wd = inotify_add_watch (g_inotify_fd, if ((g_inotify_wd = inotify_add_watch (g_inotify_fd,
dirname (path), IN_MOVED_TO | IN_CLOSE_WRITE)) < 0) dir, IN_MOVED_TO | IN_CLOSE_WRITE)) < 0)
exit_fatal ("inotify_add_watch: %s", strerror (errno)); exit_fatal ("inotify_add_watch: %s", strerror (errno));
free (path); free (path);