diff options
| author | Laurent Bercot <ska-skaware@skarnet.org> | 2026-05-02 01:00:18 +0000 |
|---|---|---|
| committer | Laurent Bercot <ska-skaware@skarnet.org> | 2026-05-02 01:00:18 +0000 |
| commit | 6b157ce56d865d2c2a61d02cfac6f16acc45b7a1 (patch) | |
| tree | a460c670fec7f04c2d57d0e321ad02bdf55e30b6 | |
| parent | 6a0f761c624be0bab8e6dce80842465aff9c3847 (diff) | |
| download | s6-rc-6b157ce56d865d2c2a61d02cfac6f16acc45b7a1.tar.gz | |
Convert s6-rc-init to gol
| -rw-r--r-- | src/s6-rc/s6-rc-init.c | 125 |
1 files changed, 71 insertions, 54 deletions
diff --git a/src/s6-rc/s6-rc-init.c b/src/s6-rc/s6-rc-init.c index 05c5b37..b43710b 100644 --- a/src/s6-rc/s6-rc-init.c +++ b/src/s6-rc/s6-rc-init.c @@ -6,8 +6,10 @@ #include <stdlib.h> #include <skalibs/posixplz.h> +#include <skalibs/uint64.h> #include <skalibs/types.h> -#include <skalibs/sgetopt.h> +#include <skalibs/prog.h> +#include <skalibs/gol.h> #include <skalibs/strerr.h> #include <skalibs/stralloc.h> #include <skalibs/tai.h> @@ -17,9 +19,23 @@ #include <s6-rc/config.h> #include <s6-rc/s6rc.h> -#define USAGE "s6-rc-init [ -c compiled ] [ -l live ] [ -p prefix ] [ -t timeout ] [ -d ] scandir" +#define USAGE "s6-rc-init [ -c bootdb ] [ -l livedir ] [ -p prefix ] [ -t timeout ] [ -d ] scandir" #define dieusage() strerr_dieusage(100, USAGE) +enum golb_e +{ + GOLB_DEREF = 0x01, +} ; + +enum gola_e +{ + GOLA_BOOTDB, + GOLA_LIVEDIR, + GOLA_PREFIX, + GOLA_TIMEOUT, + GOLA_N +} ; + static void cleanup (stralloc *sa) { int e = errno ; @@ -29,106 +45,107 @@ static void cleanup (stralloc *sa) int main (int argc, char const *const *argv) { - tain deadline ; + static gol_bool const rgolb[] = + { + { .so = 0, .lo = "no-dereference", .clear = GOLB_DEREF, .set = 0 }, + { .so = 'd', .lo = "dereference", .clear = 0, .set = GOLB_DEREF }, + } ; + static gol_arg const rgola[] = + { + { .so = 'c', .lo = "bootdb", .i = GOLA_BOOTDB }, + { .so = 'l', .lo = "livedir", .i = GOLA_LIVEDIR }, + { .so = 'p', .lo = "prefix", .i = GOLA_PREFIX }, + { .so = 't', .lo = "timeout", .i = GOLA_TIMEOUT }, + } ; + tain deadline = TAIN_INFINITE_RELATIVE ; stralloc sa = STRALLOC_ZERO ; size_t dirlen ; - char const *live = S6RC_LIVEDIR ; - char const *compiled = S6RC_BOOTDB ; - char const *prefix = "" ; - int deref = 0 ; + uint64_t wgolb = 0 ; + char const *wgola[GOLA_N] = { [GOLA_BOOTDB] = S6RC_BOOTDB, [GOLA_LIVEDIR] = S6RC_LIVEDIR, [GOLA_PREFIX] = "", [GOLA_TIMEOUT] = 0, } ; + unsigned int golc ; + PROG = "s6-rc-init" ; + golc = GOL_main(argc, argv, rgolb, rgola, &wgolb, wgola) ; + argc -= golc ; argv += golc ; + if (!argc) dieusage() ; + + if (wgola[GOLA_TIMEOUT]) { unsigned int t = 0 ; - subgetopt l = SUBGETOPT_ZERO ; - for (;;) - { - int opt = subgetopt_r(argc, argv, "c:l:p:t:bd", &l) ; - if (opt == -1) break ; - switch (opt) - { - case 'c' : compiled = l.arg ; break ; - case 'l' : live = l.arg ; break ; - case 'p' : prefix = l.arg ; break ; - case 't' : if (!uint0_scan(l.arg, &t)) dieusage() ; break ; - case 'b' : break ; - case 'd' : deref = 1 ; break ; - default : dieusage() ; - } - } - argc -= l.ind ; argv += l.ind ; + if (!uint0_scan(wgola[GOLA_TIMEOUT], &t)) + strerr_dief(100, "timeout", " must be an unsigned integer") ; if (t) tain_from_millisecs(&deadline, t) ; - else deadline = tain_infinite_relative ; } - if (!argc) dieusage() ; - if (!deref && compiled[0] != '/') - strerr_dief2x(100, compiled, " is not an absolute path") ; - if (live[0] != '/') - strerr_dief2x(100, live, " is not an absolute path") ; + if (!(wgolb & GOLB_DEREF) && wgola[GOLA_BOOTDB][0] != '/') + strerr_dief(100, "bootdb", " must be an absolute path") ; + if (wgola[GOLA_LIVEDIR][0] != '/') + strerr_dief(100, "livedir", " must be an absolute path") ; if (argv[0][0] != '/') - strerr_dief2x(100, argv[0], " is not an absolute path") ; - if (strchr(prefix, '/') || strchr(prefix, '\n')) - strerr_dief1x(100, "prefix cannot contain a / or a newline") ; + strerr_dief(100, "scandir", " must be an absolute path") ; + if (strchr(wgola[GOLA_PREFIX], '/') || strchr(wgola[GOLA_PREFIX], '\n')) + strerr_dief(100, "prefix", " cannot contain a / or a newline") ; tain_now_set_stopwatch_g() ; tain_add_g(&deadline, &deadline) ; - if (!s6rc_livedir_canon(&live)) + if (!s6rc_livedir_canon(&wgola[GOLA_LIVEDIR])) strerr_diefu1sys(111, "canonicalize livedir") ; - if (deref) + if (wgolb & GOLB_DEREF) { - char *x = realpath(compiled, 0) ; - if (!x) strerr_diefu2sys(111, "realpath ", compiled) ; - compiled = x ; + char *x = realpath(wgola[GOLA_BOOTDB], 0) ; + if (!x) strerr_diefu2sys(111, "realpath ", wgola[GOLA_BOOTDB]) ; + wgola[GOLA_BOOTDB] = x ; } + { s6rc_db_t db ; int r ; - int fdcompiled = open_readb(compiled) ; + int fdcompiled = open_readb(wgola[GOLA_BOOTDB]) ; if (fdcompiled < 0) - strerr_diefu2sys(111, "open ", compiled) ; + strerr_diefusys(111, "open ", wgola[GOLA_BOOTDB]) ; r = s6rc_db_read_sizes(fdcompiled, &db) ; if (r < 0) - strerr_diefu2sys(111, "read database size in ", compiled) ; + strerr_diefusys(111, "read database size in ", wgola[GOLA_BOOTDB]) ; else if (!r) - strerr_dief2x(4, "invalid database size in ", compiled) ; + strerr_dief(4, "invalid database size in ", wgola[GOLA_BOOTDB]) ; close(fdcompiled) ; { unsigned char state[db.nshort + db.nlong] ; memset(state, 0, db.nshort + db.nlong) ; - if (!s6rc_livedir_create(&sa, live, PROG, argv[0], prefix, compiled, state, db.nshort + db.nlong, &dirlen)) - strerr_diefu1sys(111, "create live directory") ; + if (!s6rc_livedir_create(&sa, wgola[GOLA_LIVEDIR], PROG, argv[0], wgola[GOLA_PREFIX], wgola[GOLA_BOOTDB], state, db.nshort + db.nlong, &dirlen)) + strerr_diefusys(111, "create live directory") ; } } { - size_t clen = strlen(compiled) ; + size_t clen = strlen(wgola[GOLA_BOOTDB]) ; char lfn[sa.len + 13] ; char cfn[clen + 13] ; memcpy(lfn, sa.s, sa.len) ; memcpy(lfn + sa.len, "/servicedirs", 13) ; - memcpy(cfn, compiled, clen) ; + memcpy(cfn, wgola[GOLA_BOOTDB], clen) ; memcpy(cfn + clen, "/servicedirs", 13) ; sa.len++ ; if (!hiercopy(cfn, lfn)) { cleanup(&sa) ; - strerr_diefu4sys(111, "recursively copy ", cfn, " to ", lfn) ; + strerr_diefusys(111, "recursively copy ", cfn, " to ", lfn) ; } } - if (!atomic_symlink4(sa.s + dirlen, live, 0, 0)) + if (!atomic_symlink4(sa.s + dirlen, wgola[GOLA_LIVEDIR], 0, 0)) { cleanup(&sa) ; - strerr_diefu4sys(111, "symlink ", sa.s + dirlen, " to ", live) ; + strerr_diefusys(111, "symlink ", sa.s + dirlen, " to ", wgola[GOLA_LIVEDIR]) ; } - if (s6rc_servicedir_manage_g(live, prefix, &deadline) < 0) + if (s6rc_servicedir_manage_g(wgola[GOLA_LIVEDIR], wgola[GOLA_PREFIX], &deadline) < 0) { - unlink_void(live) ; + unlink_void(wgola[GOLA_LIVEDIR]) ; cleanup(&sa) ; if (errno == ENXIO) - strerr_diefu5x(100, "supervise service directories in ", live, "/servicedirs", ": s6-svscan not running on ", argv[0]) ; + strerr_diefu(100, "supervise service directories in ", wgola[GOLA_LIVEDIR], "/servicedirs", ": s6-svscan not running on ", argv[0]) ; else - strerr_diefu3sys(111, "supervise service directories in ", live, "/servicedirs") ; + strerr_diefusys(111, "supervise service directories in ", wgola[GOLA_LIVEDIR], "/servicedirs") ; } - return 0 ; + _exit(0) ; } |
