diff --git a/src/runner.c b/src/runner.c index 6e63e16..fe463af 100644 --- a/src/runner.c +++ b/src/runner.c @@ -30,6 +30,7 @@ #include #include #include +#include #include "lmap.h" #include "lmapd.h" @@ -278,6 +279,10 @@ action_exec(struct lmapd *lmapd, struct schedule *schedule, struct action *actio action->last_invocation = t.tv_sec; action->state = LMAP_ACTION_STATE_RUNNING; action->cnt_invocations++; + + /* In case we run for too long before the child can setpgid() */ + (void) setpgid(pid, pid); + return 1; } @@ -327,6 +332,10 @@ action_exec(struct lmapd *lmapd, struct schedule *schedule, struct action *actio lmap_err("failed to change directory"); exit(EXIT_FAILURE); } + if (setpgid(0, 0) == -1) { + lmap_err("action '%s': setpgid failed: %s", action->name, strerror(errno)); + exit(EXIT_FAILURE); + } execvp(task->program, argv); lmap_err("failed to execute action '%s'", action->name); exit(EXIT_FAILURE); @@ -398,7 +407,13 @@ action_kill(struct lmapd *lmapd, struct action *action) if (action->state == LMAP_ACTION_STATE_RUNNING) { if (action->pid) { - (void) kill(action->pid, SIGTERM); + if (kill(-(action->pid), SIGTERM)) { + if (errno != ESRCH) + lmap_wrn("action %s, pid %ld: failed to send SIGTERM to process group: %s", + action->name, (long) action->pid, strerror(errno)); + /* try again, just the direct child this time */ + (void) kill(action->pid, SIGTERM); + } } } } @@ -568,7 +583,7 @@ lmapd_cleanup(struct lmapd *lmapd) event_base_gettimeofday_cached(lmapd->base, &t); while (1) { - pid = waitpid(0, &status, WNOHANG); + pid = waitpid(-1, &status, WNOHANG); if (pid == 0 || pid == -1) { return; }