diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2024-05-07 22:06:36 +0000 |
---|---|---|
committer | Laurent Bercot <ska@appnovation.com> | 2024-05-07 22:06:36 +0000 |
commit | 15e3e402c59089514f590bab8804f0ed60f0fee3 (patch) | |
tree | e29847fcb9b5e3913c3e4f83b11af62b30d225bc /src/supervision/s6-supervise.c | |
parent | b827410052a640f65650d1b72d5f63d9ada28933 (diff) | |
download | s6-15e3e402c59089514f590bab8804f0ed60f0fee3.tar.xz |
Add process group support to s6-supervise
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/supervision/s6-supervise.c')
-rw-r--r-- | src/supervision/s6-supervise.c | 69 |
1 files changed, 52 insertions, 17 deletions
diff --git a/src/supervision/s6-supervise.c b/src/supervision/s6-supervise.c index 2f3b9a7..3201127 100644 --- a/src/supervision/s6-supervise.c +++ b/src/supervision/s6-supervise.c @@ -43,7 +43,7 @@ typedef enum trans_e trans_t, *trans_t_ref ; enum trans_e { V_TIMEOUT, V_CHLD, V_TERM, V_HUP, V_QUIT, V_INT, - V_a, V_b, V_q, V_h, V_k, V_t, V_i, V_1, V_2, V_p, V_c, V_y, V_r, + V_a, V_b, V_q, V_h, V_k, V_t, V_i, V_1, V_2, V_p, V_c, V_y, V_r, V_P, V_C, V_K, V_o, V_d, V_u, V_D, V_U, V_x, V_O, V_Q } ; @@ -73,6 +73,7 @@ typedef action_t *action_t_ref ; static tain deadline ; static tain nextstart = TAIN_ZERO ; static s6_svstatus_t status = S6_SVSTATUS_ZERO ; +static int finish_wstat ; static state_t state = DOWN ; static int notifyfd = -1 ; static char const *servicename = 0 ; @@ -130,6 +131,7 @@ static inline int read_downsig (void) static void set_down_and_ready (char const *s, unsigned int n) { status.pid = 0 ; + status.pgid = 0 ; status.flagfinishing = 0 ; status.flagready = 1 ; state = DOWN ; @@ -152,11 +154,14 @@ static void bail (void) gflags.cont = 0 ; } +static void killI (void) +{ + if (status.pgid > 0) killpg(status.pgid, SIGINT) ; +} + static void sigint (void) { - pid_t pgid = getpgid(status.pid) ; - if (pgid == -1) strerr_warnwu1sys("getpgid") ; - else killpg(pgid, SIGINT) ; + killI() ; bail() ; } @@ -253,6 +258,31 @@ static void killr (void) kill(status.pid, read_downsig()) ; } +static void killP (void) +{ + if (status.pgid > 0) + { + killpg(status.pgid, SIGSTOP) ; + status.flagpaused = 1 ; + announce() ; + } +} + +static void killC (void) +{ + if (status.pgid > 0) + { + killpg(status.pgid, SIGCONT) ; + status.flagpaused = 0 ; + announce() ; + } +} + +static void killK (void) +{ + if (status.pgid > 0) killpg(status.pgid, SIGKILL) ; +} + static void trystart (void) { cspawn_fileaction fa[2] = @@ -341,6 +371,9 @@ static void trystart (void) fd_close(notifyp[1]) ; notifyfd = notifyp[0] ; } + status.pgid = getpgid(status.pid) ; + if (status.pgid == -1) + strerr_warnwu1sys("getpgid() (process group control commands will have no effect)") ; settimeout_infinite() ; nextstart = tain_zero ; state = UP ; @@ -411,9 +444,9 @@ static int uplastup_z (void) unsigned int n ; char fmt0[UINT_FMT] ; char fmt1[UINT_FMT] ; - char const *cargv[5] = { "finish", fmt0, fmt1, servicename, 0 } ; + char fmt2[PID_FMT] ; + char const *cargv[6] = { "finish", fmt0, fmt1, servicename, status.pgid > 0 ? fmt2 : "-1", 0 } ; - status.wstat = (int)status.pid ; status.flagpaused = 0 ; status.flagready = 0 ; gflags.dying = 0 ; @@ -425,6 +458,7 @@ static int uplastup_z (void) } fmt0[uint_fmt(fmt0, WIFSIGNALED(status.wstat) ? 256 : WEXITSTATUS(status.wstat))] = 0 ; fmt1[uint_fmt(fmt1, WTERMSIG(status.wstat))] = 0 ; + if (status.pgid > 0) fmt2[pid_fmt(fmt2, status.pgid)] = 0 ; if (!read_uint("max-death-tally", &n)) n = 100 ; if (n > S6_MAX_DEATH_TALLY) n = S6_MAX_DEATH_TALLY ; @@ -458,6 +492,7 @@ static int uplastup_z (void) tain_add_g(&deadline, &tto) ; else settimeout_infinite() ; } + status.pgid = getpgid(status.pid) ; status.flagfinishing = 1 ; announce() ; ftrigw_notifyb_nosig(S6_SUPERVISE_EVENTDIR, "d", 1) ; @@ -532,8 +567,7 @@ static void finishtimeout (void) static void finish_z (void) { - int wstat = (int)status.pid ; - if (WIFEXITED(wstat) && WEXITSTATUS(wstat) == 125) + if (WIFEXITED(finish_wstat) && WEXITSTATUS(finish_wstat) == 125) { status.flagwantup = 0 ; set_down_and_ready("OD", 2) ; @@ -553,22 +587,22 @@ static void lastfinish_z (void) bail() ; } -static action_t_ref const actions[5][27] = +static action_t_ref const actions[5][30] = { { &downtimeout, &nop, &bail, &bail, &bail, &bail, - &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, + &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &down_o, &wantdown, &down_u, &wantDOWN, &down_U, &bail, &wantdown, &wantDOWN }, { &uptimeout, &up_z, &up_term, &up_x, &bail, &sigint, - &killa, &killb, &killq, &killh, &killk, &killt, &killi, &kill1, &kill2, &killp, &killc, &killy, &killr, + &killa, &killb, &killq, &killh, &killk, &killt, &killi, &kill1, &kill2, &killp, &killc, &killy, &killr, &killP, &killC, &killK, &wantdown, &up_d, &wantup, &up_D, &wantUP, &up_x, &wantdown, &wantDOWN }, { &finishtimeout, &finish_z, &finish_x, &finish_x, &bail, &sigint, - &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, + &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &wantdown, &wantdown, &wantup, &wantDOWN, &wantUP, &finish_x, &wantdown, &wantDOWN }, { &uptimeout, &lastup_z, &up_d, &closethem, &bail, &sigint, - &killa, &killb, &killq, &killh, &killk, &killt, &killi, &kill1, &kill2, &killp, &killc, &killy, &killr, + &killa, &killb, &killq, &killh, &killk, &killt, &killi, &kill1, &kill2, &killp, &killc, &killy, &killr, &killP, &killC, &killK, &wantdown, &up_d, &wantup, &up_D, &wantUP, &closethem, &wantdown, &wantDOWN }, { &finishtimeout, &lastfinish_z, &nop, &closethem, &bail, &sigint, - &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, + &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &nop, &wantdown, &wantdown, &wantup, &wantDOWN, &wantUP, &closethem, &wantdown, &wantDOWN } } ; @@ -620,7 +654,8 @@ static inline void handle_signals (void) if (errno != ECHILD) strerr_diefu1sys(111, "wait_pid_nohang") ; else break ; else if (!r) break ; - status.pid = (pid_t)wstat ; /* don't overwrite status.wstat if it's ./finish */ + if (status.flagfinishing) finish_wstat = wstat ; + else status.wstat = wstat ; (*actions[state][V_CHLD])() ; } break ; @@ -652,8 +687,8 @@ static inline void handle_control (int fd) else if (!r) break ; else { - size_t pos = byte_chr("abqhkti12pcyroduDUxOQ", 21, c) ; - if (pos < 21) (*actions[state][V_a + pos])() ; + size_t pos = byte_chr("abqhkti12pcyrPCKoduDUxOQ", 24, c) ; + if (pos < 24) (*actions[state][V_a + pos])() ; } } } |