From 957aed63a8caed3107d53fe818e1e1ee77247574 Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Mon, 19 Jun 2023 17:11:16 +0200 Subject: iexec: cleanup --- iexec.c | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/iexec.c b/iexec.c index 04e8e91..9853eb1 100644 --- a/iexec.c +++ b/iexec.c @@ -1,7 +1,7 @@ /* * iexec.c: run a program and restart on file change * - * Copyright (c) 2017, Přemysl Eric Janouch
+ * Copyright (c) 2017 - 2023, Přemysl Eric Janouch
* * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted. @@ -28,22 +28,27 @@ static pid_t g_child; static bool g_restarting = false; static int g_inotify_fd, g_inotify_wd; +static void +handle_inotify_event (const struct inotify_event *e, const char *base) +{ + if (e->wd != g_inotify_wd || strcmp (e->name, base)) + return; + + print_debug ("file changed, killing child"); + g_restarting = true; + if (g_child >= 0 && kill (g_child, SIGINT)) + print_error ("kill: %s", strerror (errno)); +} + static void handle_file_change (const char *base) { - char buf[4096]; ssize_t len; const struct inotify_event *e; + 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) - { - e = (const struct inotify_event *) buf; - if (e->wd != g_inotify_wd || strcmp (e->name, base)) - continue; - - print_debug ("file changed, killing child"); - g_restarting = true; - if (kill (g_child, SIGINT)) - print_error ("kill: %s", strerror (errno)); - } + for (char *ptr = buf; ptr < buf + len; ptr += sizeof *e + e->len) + handle_inotify_event ((e = (struct inotify_event *) buf), base); } static void @@ -148,12 +153,13 @@ main (int argc, char *argv[]) if (sigprocmask (SIG_BLOCK, &chld, &orig)) 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) exit_fatal ("inotify_init1: %s", strerror (errno)); 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)); free (path); -- cgit v1.2.3-70-g09d2