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 | |
parent | b827410052a640f65650d1b72d5f63d9ada28933 (diff) | |
download | s6-15e3e402c59089514f590bab8804f0ed60f0fee3.tar.gz s6-15e3e402c59089514f590bab8804f0ed60f0fee3.tar.xz s6-15e3e402c59089514f590bab8804f0ed60f0fee3.zip |
Add process group support to s6-supervise
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src/supervision')
-rw-r--r-- | src/supervision/s6-supervise.c | 69 | ||||
-rw-r--r-- | src/supervision/s6-svc.c | 7 | ||||
-rw-r--r-- | src/supervision/s6-svstat.c | 58 |
3 files changed, 95 insertions, 39 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])() ; } } } diff --git a/src/supervision/s6-svc.c b/src/supervision/s6-svc.c index c84d62b..40a2cd8 100644 --- a/src/supervision/s6-svc.c +++ b/src/supervision/s6-svc.c @@ -18,7 +18,7 @@ #include <s6/config.h> #include <s6/supervise.h> -#define USAGE "s6-svc [ -wu | -wU | -wd | -wD | -wr | -wR ] [ -T timeout ] [ -s signal | -abqhkti12pcy ] [ -roduDUxOQ ] servicedir" +#define USAGE "s6-svc [ -wu | -wU | -wd | -wD | -wr | -wR ] [ -T timeout ] [ -s signal | -abqhkti12pcyrPCK ] [ -oduDUxOQ ] servicedir" #define dieusage() strerr_dieusage(100, USAGE) #define DATASIZE 63 @@ -50,7 +50,7 @@ int main (int argc, char const *const *argv) subgetopt l = SUBGETOPT_ZERO ; for (;;) { - int opt = subgetopt_r(argc, argv, "s:abqhkti12pcyroduDUxOQT:w:", &l) ; + int opt = subgetopt_r(argc, argv, "s:abqhkti12pcyrPCKoduDUxOQT:w:", &l) ; if (opt == -1) break ; switch (opt) { @@ -74,6 +74,9 @@ int main (int argc, char const *const *argv) case 'c' : case 'y' : case 'r' : + case 'P' : + case 'C' : + case 'K' : case 'o' : case 'd' : case 'u' : diff --git a/src/supervision/s6-svstat.c b/src/supervision/s6-svstat.c index f683ef8..502991a 100644 --- a/src/supervision/s6-svstat.c +++ b/src/supervision/s6-svstat.c @@ -3,7 +3,9 @@ #include <sys/wait.h> #include <string.h> #include <unistd.h> +#include <stdlib.h> #include <errno.h> + #include <skalibs/uint64.h> #include <skalibs/types.h> #include <skalibs/bytestr.h> @@ -13,9 +15,10 @@ #include <skalibs/sig.h> #include <skalibs/tai.h> #include <skalibs/djbunix.h> + #include <s6/supervise.h> -#define USAGE "s6-svstat [ -uwNrpest | -o up,wantedup,normallyup,ready,paused,pid,exitcode,signal,signum,updownsince,readysince,updownfor,readyfor ] [ -n ] servicedir" +#define USAGE "s6-svstat [ -uwNrpgest | -o up,wantedup,normallyup,ready,paused,pid,pgid,exitcode,signal,signum,updownsince,readysince,updownfor,readyfor ] [ -n ] servicedir" #define dieusage() strerr_dieusage(100, USAGE) #define MAXFIELDS 16 @@ -63,6 +66,16 @@ static void pr_pid (buffer *b, s6_svstatus_t const *st) else buffer_putsnoflush(b, "-1") ; } +static void pr_pgid (buffer *b, s6_svstatus_t const *st) +{ + if (st->pgid > 0) + { + char fmt[PID_FMT] ; + buffer_putnoflush(b, fmt, pid_fmt(fmt, st->pgid)) ; + } + else buffer_putsnoflush(b, "-1") ; +} + static void pr_tain (buffer *b, tain const *a) { char fmt[TIMESTAMP] ; @@ -133,38 +146,40 @@ static void pr_normallyup (buffer *b, s6_svstatus_t const *st) static funcmap_t const fmtable[] = { - { .s = "up", .f = &pr_up }, - { .s = "wantedup", .f = &pr_wantedup }, + { .s = "exitcode", .f = &pr_exitcode }, { .s = "normallyup", .f = &pr_normallyup }, - { .s = "ready", .f = &pr_ready }, { .s = "paused", .f = &pr_paused }, + { .s = "pgid", .f = &pr_pgid }, { .s = "pid", .f = &pr_pid }, - { .s = "exitcode", .f = &pr_exitcode }, + { .s = "ready", .f = &pr_ready }, + { .s = "readyfor", .f = &pr_readyseconds }, + { .s = "readysince", .f = &pr_readystamp }, { .s = "signal", .f = &pr_signal }, { .s = "signum", .f = &pr_signum }, - { .s = "updownsince", .f = &pr_stamp }, - { .s = "readysince", .f = &pr_readystamp }, + { .s = "up", .f = &pr_up }, { .s = "updownfor", .f = &pr_upseconds }, - { .s = "readyfor", .f = &pr_readyseconds }, - { .s = 0, .f = 0 } + { .s = "updownsince", .f = &pr_stamp }, + { .s = "wantedup", .f = &pr_wantedup }, } ; +static int funcmap_bcmp (void const *a, void const *b) +{ + return strcmp((char const *)a, ((funcmap_t const *)b)->s) ; +} + +#define BSEARCH(key, array) bsearch(key, (array), sizeof(array)/sizeof(funcmap_t), sizeof(funcmap_t), &funcmap_bcmp) static unsigned int parse_options (char const *arg, pr_func_ref *fields, unsigned int n) { while (*arg) { + funcmap_t const *p ; size_t pos = str_chr(arg, ',') ; - funcmap_t const *p = fmtable ; - if (!pos) strerr_dief1x(100, "invalid null option field") ; - for (; p->s ; p++) if (!strncmp(arg, p->s, pos)) break ; - if (!p->s) - { - char blah[pos+1] ; - memcpy(blah, arg, pos) ; - blah[pos] = 0 ; - strerr_dief2x(100, "invalid option field: ", blah) ; - } + char blah[pos+1] ; + memcpy(blah, arg, pos) ; + blah[pos] = 0 ; + p = BSEARCH(blah, fmtable) ; + if (!p) strerr_dief2x(100, "invalid option field: ", blah) ; checkfields() ; fields[n++] = p->f ; arg += pos ; if (*arg) arg++ ; @@ -182,6 +197,8 @@ static void legacy (s6_svstatus_t *st, int flagnum) { buffer_putnoflush(buffer_1small,"up (pid ", 8) ; buffer_putnoflush(buffer_1small, fmt, pid_fmt(fmt, status.pid)) ; + buffer_putnoflush(buffer_1small, " pgid ", 6) ; + buffer_putnoflush(buffer_1small, fmt, pid_fmt(fmt, status.pgid)) ; buffer_putnoflush(buffer_1small, ") ", 2) ; } else @@ -242,7 +259,7 @@ int main (int argc, char const *const *argv) subgetopt l = SUBGETOPT_ZERO ; for (;;) { - int opt = subgetopt_r(argc, argv, "no:uwNrpest", &l) ; + int opt = subgetopt_r(argc, argv, "no:uwNrpgest", &l) ; if (opt == -1) break ; switch (opt) { @@ -253,6 +270,7 @@ int main (int argc, char const *const *argv) case 'N' : checkfields() ; fields[n++] = &pr_normallyup ; break ; case 'r' : checkfields() ; fields[n++] = &pr_ready ; break ; case 'p' : checkfields() ; fields[n++] = &pr_pid ; break ; + case 'g' : checkfields() ; fields[n++] = &pr_pgid ; break ; case 'e' : checkfields() ; fields[n++] = &pr_exitcode ; break ; case 's' : checkfields() ; fields[n++] = &pr_signal ; break ; case 't' : checkfields() ; fields[n++] = &pr_upseconds ; break ; |