Skip to content

Commit

Permalink
Merge branch 'topic/process-groups'
Browse files Browse the repository at this point in the history
  • Loading branch information
hmh committed Sep 9, 2021
2 parents 84f1053 + 9aa9504 commit 9a06d84
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/runner.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <inttypes.h>
#include <fnmatch.h>
#include <errno.h>
#include <unistd.h>

#include "lmap.h"
#include "lmapd.h"
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 9a06d84

Please sign in to comment.