diff --git a/src/66/66-fdholder-filler.c b/src/66/66-fdholder-filler.c new file mode 100644 index 0000000000000000000000000000000000000000..4edb95d0af94e0211de979e7af554d3ba99099c8 --- /dev/null +++ b/src/66/66-fdholder-filler.c @@ -0,0 +1,182 @@ +/* + * 66-fdholder-filler.c + * + * Copyright (c) 2018-2022 Eric Vidal <eric@obarun.org> + * + * All rights reserved. + * + * This file is part of Obarun. It is subject to the license terms in + * the LICENSE file found in the top-level directory of this + * distribution. + * This file may not be copied, modified, propagated, or distributed + * except according to the terms contained in the LICENSE file./ + * + * This file is a modified copy of s6-rc-fdholder-filler.c file to suit the needs of 66, + * coming from skarnet software at https://skarnet.org/software/s6-rc with an ISC LICENSE. + * All credits goes to Laurent Bercot <ska-remove-this-if-you-are-not-a-bot@skarnet.org> + * + * This program keep the same behavior of the original file. It's expected to run behind + * an s6-ipcclient connection. + * */ + +#include <unistd.h> //access +#include <string.h> + +#include <oblibs/log.h> +#include <oblibs/string.h> +#include <oblibs/files.h> + +#include <skalibs/sgetopt.h> +#include <skalibs/types.h> +#include <skalibs/tai.h> + +#include <66/constants.h> + +#include <s6/fdholder.h> + +#define USAGE "66-fdholder-filler [ -h ] [ -v verbosity ] [ -1 ] [ -t timeout ]" + +static inline void info_help (void) +{ + DEFAULT_MSG = 0 ; + + static char const *help = +"\n" +"options :\n" +" -h: print this help\n" +" -v: increase/decrease verbosity\n" +"\n" +; + + log_info(USAGE,"\n",help) ; +} +#define N 4096 + +static inline unsigned int class (char c) +{ + switch (c) { + + case 0 : return 0 ; + case '\n' : return 1 ; + case '#' : return 2 ; + case ' ' : + case '\r' : + case '\t' : return 3 ; + default : return 4 ; + } +} + +static inline unsigned int parse_servicenames (char *s, unsigned int *indices) +{ + static unsigned char const table[3][5] = { + + { 3, 0, 1, 0, 6 }, + { 3, 0, 1, 1, 1 }, + { 3, 8, 2, 2, 2 } + } ; + + unsigned int pos = 0, n = 0, state = 0 ; + + for (; state < 3 ; pos++) { + + unsigned char c = table[state][class(s[pos])] ; + state = c & 3 ; + + if (c & 4) + indices[n++] = pos ; + + if (c & 8) + s[pos] = 0 ; + } + return n ; +} + +int main(int argc, char const *const *argv) +{ + int notif = 0 ; + unsigned int n = 0, indices[N] ; + char buf[N<<1] ; + + s6_fdholder_t a = S6_FDHOLDER_ZERO ; + + tain deadline ; + + PROG = "66-fdholder-filler" ; + { + unsigned int t = 0 ; + subgetopt l = SUBGETOPT_ZERO ; + + for (;;) + { + int opt = subgetopt_r(argc,argv, "hv:t:1", &l) ; + if (opt == -1) break ; + + switch (opt) + { + case 'h' : info_help(); return 0 ; + case 'v' : if (!uint0_scan(l.arg, &VERBOSITY)) log_usage(USAGE) ; break ; + case '1' : notif = 1 ; break ; + case 't' : if (!uint0_scan(l.arg, &t)) log_usage(USAGE) ; break ; + default : log_usage(USAGE) ; + } + } + argc -= l.ind ; argv += l.ind ; + if (t) tain_from_millisecs(&deadline, t) ; + else deadline = tain_infinite_relative ; + } + + { + size_t r = allread(0, buf, N<<1) ; + if (r >= N<<1) + log_die(LOG_EXIT_USER, "file ", argv[0], " is too big") ; + buf[r] = 0 ; + } + + n = parse_servicenames(buf, indices) ; + + if (n) { + + tain offset = { .sec = TAI_ZERO } ; + int p[2] ; + unsigned int i = 0 ; + s6_fdholder_fd_t dump[n<<1] ; + + close(0) ; + + s6_fdholder_init(&a, 6) ; + + tain_now_set_stopwatch_g() ; + tain_add_g(&deadline, &deadline) ; + + for (; i < n ; i++) { + + size_t len = strlen(buf + indices[i]) ; + + if (len > S6_FDHOLDER_ID_SIZE) { + errno = ENAMETOOLONG ; + log_dieusys(LOG_EXIT_SYS, "create identifier for ", buf + indices[i]) ; + } + + if (pipe(p) < 0) + log_dieusys(LOG_EXIT_SYS, "create pipe") ; + + dump[i<<1].fd = p[0] ; + tain_add_g(&dump[i<<1].limit, &tain_infinite_relative) ; + offset.nano = i << 1 ; + tain_add(&dump[i<<1].limit, &dump[i<<1].limit, &offset) ; + memcpy(dump[i<<1].id, buf + indices[i], len + 1) ; + dump[(i<<1)+1].fd = p[1] ; + offset.nano = 1 ; + tain_add(&dump[(i<<1)+1].limit, &dump[i<<1].limit, &offset) ; + memcpy(dump[(i<<1)+1].id, buf + indices[i], len) ; + dump[(i<<1)+1].id[8] = 'w' ; + } + + if (!s6_fdholder_setdump_g(&a, dump, n << 1, &deadline)) + log_dieusys(LOG_EXIT_SYS, "transfer pipes") ; + } + + if (notif) write(1, "\n", 1) ; + + return 0 ; +} diff --git a/src/66/66-oneshot.c b/src/66/66-oneshot.c new file mode 100644 index 0000000000000000000000000000000000000000..344e10be0092c2ba7ade520670f8eebd705dd223 --- /dev/null +++ b/src/66/66-oneshot.c @@ -0,0 +1,129 @@ +/* + * 66-oneshot.c + * + * Copyright (c) 2018-2022 Eric Vidal <eric@obarun.org> + * + * All rights reserved. + * + * This file is part of Obarun. It is subject to the license terms in + * the LICENSE file found in the top-level directory of this + * distribution. + * This file may not be copied, modified, propagated, or distributed + * except according to the terms contained in the LICENSE file./ + * */ + +#include <unistd.h> +#include <string.h> + +#include <oblibs/log.h> +#include <oblibs/obgetopt.h> +#include <oblibs/string.h> +#include <oblibs/environ.h> + +#include <skalibs/sgetopt.h> +#include <skalibs/types.h> +#include <skalibs/stralloc.h> + +#include <66/constants.h> +#include <66/service.h> +#include <66/resolve.h> + +#define USAGE "66-oneshot [ -h ] [ -v verbosity ] up|down /var/lib/66/system/<tree>/servicedirs/svc/<service>" + +static inline void info_help (void) +{ + DEFAULT_MSG = 0 ; + + static char const *help = +"\n" +"options :\n" +" -h: print this help\n" +" -v: increase/decrease verbosity\n" +; + + log_info(USAGE,"\n",help) ; +} + +int main(int argc, char const *const *argv) +{ + char upath[UINT_FMT] ; + upath[uint_fmt(upath, SS_MAX_PATH)] = 0 ; + char const *path = 0 ; + char *file = "up" ; + + PROG = "66-oneshot" ; + { + subgetopt l = SUBGETOPT_ZERO ; + + for (;;) + { + int opt = getopt_args(argc,argv, ">hv:", &l) ; + if (opt == -1) break ; + if (opt == -2) log_die(LOG_EXIT_USER,"options must be set first") ; + switch (opt) + { + case 'h' : info_help(); return 0 ; + case 'v' : if (!uint0_scan(l.arg, &VERBOSITY)) log_usage(USAGE) ; break ; + default : log_usage(USAGE) ; + } + } + argc -= l.ind ; argv += l.ind ; + } + + if (argc < 1) + log_usage(USAGE) ; + + if (argv[0][0] == 'd') + file = "down" ; + + if (argv[0][0] != 'd' && argv[0][0] != 'u') + log_die(LOG_EXIT_USER, "only up or down signals are allowed") ; + + if (strlen(argv[1]) >= SS_MAX_PATH_LEN) + log_die(LOG_EXIT_USER, "path of script file is too long -- it cannot exceed: ", upath) ; + + else if (argv[1][0] != '/') + log_die(LOG_EXIT_USER, "path of script file must be absolute") ; + + if (access(argv[1], F_OK) < 0) { + + if (file[0] == 'd') + /* really nothing to do here */ + return 0 ; + + else + log_dieusys(LOG_EXIT_SYS, "find: ", argv[1]) ; + } + + path = argv[1] ; + size_t len = strlen(path) ; + + char script[len + 1 + strlen(file) + 1] ; + auto_strings(script, path, "/", file) ; + + if (file[0] == 'd') + /** do not crash if the down file doesn't exist + * considere it down instead*/ + if (access(script, F_OK) < 0) + _exit(0) ; + + /** + * be paranoid and avoid to crash just for a + * not executable script + * */ + if (chmod(script, 0755) < 0) + log_dieusys(LOG_EXIT_SYS,"chmod: ", script) ; + + char const *newargv[3] ; + unsigned int m = 0 ; + newargv[m++] = script ; + newargv[m++] = file ; + newargv[m] = 0 ; + + if (chdir(path) < 0) + log_dieusys(LOG_EXIT_SYS, "chdir to: ", path) ; + + execve(script, (char *const *)newargv, (char *const *)environ) ; + + log_dieusys(errno == ENOENT ? 127 : 126, "exec: ", script) ; +} diff --git a/src/66/66-parse.c b/src/66/66-parse.c new file mode 100644 index 0000000000000000000000000000000000000000..2fd55ae2e3c3a9d8d6fd3d24b0fc42c29a7502f7 --- /dev/null +++ b/src/66/66-parse.c @@ -0,0 +1,50 @@ +/* + * 66-parse.c + * + * Copyright (c) 2018-2021 Eric Vidal <eric@obarun.org> + * + * All rights reserved. + * + * This file is part of Obarun. It is subject to the license terms in + * the LICENSE file found in the top-level directory of this + * distribution. + * This file may not be copied, modified, propagated, or distributed + * except according to the terms contained in the LICENSE file./ + */ + +#include <oblibs/log.h> + +#include <66/ssexec.h> + +int main(int argc, char const *const *argv,char const *const *envp) +{ + PROG = "66-parse" ; + + ssexec_t info = SSEXEC_ZERO ; + + info.prog = PROG ; + info.help = help_parse ; + info.usage = usage_parse ; + + info.skip_opt_tree = 1 ; + + return ssexec_main(argc, argv, envp, &ssexec_parse, &info) ; +} + + + + + + + + + + + + + + + + + + diff --git a/src/66/deps-exe/66-all b/src/66/deps-exe/66-all index f1e60dad80b6949a291fd1320dd53b5888a8da74..306db624e775967d5f8dee9157a531ba4d44f70b 100644 --- a/src/66/deps-exe/66-all +++ b/src/66/deps-exe/66-all @@ -1,5 +1,4 @@ ${LIB66} --ls6rc -ls6 -loblibs -lskarnet diff --git a/src/66/deps-exe/66-disable b/src/66/deps-exe/66-disable index f5a0eec2e83239d530a4cb41fc710973c8fc3924..390fc3b50a19ade5209e13428a74ea3806b77192 100644 --- a/src/66/deps-exe/66-disable +++ b/src/66/deps-exe/66-disable @@ -1,5 +1,4 @@ ${LIB66} --ls6rc -ls6 -loblibs -lskarnet diff --git a/src/66/deps-exe/66-enable b/src/66/deps-exe/66-enable index f1e60dad80b6949a291fd1320dd53b5888a8da74..306db624e775967d5f8dee9157a531ba4d44f70b 100644 --- a/src/66/deps-exe/66-enable +++ b/src/66/deps-exe/66-enable @@ -1,5 +1,4 @@ ${LIB66} --ls6rc -ls6 -loblibs -lskarnet diff --git a/src/66/deps-exe/66-env b/src/66/deps-exe/66-env index 157fe843fafcbb110642e39610f37ba89904fbcd..70626b64cf11b084481656bb85292f5b63f01300 100644 --- a/src/66/deps-exe/66-env +++ b/src/66/deps-exe/66-env @@ -1,5 +1,4 @@ ${LIB66} --ls6rc -ls6 -loblibs -lskarnet diff --git a/src/66/deps-exe/66-fdholder-filler b/src/66/deps-exe/66-fdholder-filler new file mode 100644 index 0000000000000000000000000000000000000000..70626b64cf11b084481656bb85292f5b63f01300 --- /dev/null +++ b/src/66/deps-exe/66-fdholder-filler @@ -0,0 +1,6 @@ +${LIB66} +-ls6 +-loblibs +-lskarnet + + diff --git a/src/66/deps-exe/66-init b/src/66/deps-exe/66-init index f1e60dad80b6949a291fd1320dd53b5888a8da74..306db624e775967d5f8dee9157a531ba4d44f70b 100644 --- a/src/66/deps-exe/66-init +++ b/src/66/deps-exe/66-init @@ -1,5 +1,4 @@ ${LIB66} --ls6rc -ls6 -loblibs -lskarnet diff --git a/src/66/deps-exe/66-oneshot b/src/66/deps-exe/66-oneshot new file mode 100644 index 0000000000000000000000000000000000000000..9c58365fcec451152a7b10b51a36af3a3612a3fa --- /dev/null +++ b/src/66/deps-exe/66-oneshot @@ -0,0 +1,3 @@ +${LIB66} +-loblibs +-lskarnet diff --git a/src/66/deps-exe/66-parse b/src/66/deps-exe/66-parse new file mode 100644 index 0000000000000000000000000000000000000000..306db624e775967d5f8dee9157a531ba4d44f70b --- /dev/null +++ b/src/66/deps-exe/66-parse @@ -0,0 +1,4 @@ +${LIB66} +-ls6 +-loblibs +-lskarnet diff --git a/src/66/deps-exe/66-start b/src/66/deps-exe/66-start index f1e60dad80b6949a291fd1320dd53b5888a8da74..306db624e775967d5f8dee9157a531ba4d44f70b 100644 --- a/src/66/deps-exe/66-start +++ b/src/66/deps-exe/66-start @@ -1,5 +1,4 @@ ${LIB66} --ls6rc -ls6 -loblibs -lskarnet diff --git a/src/66/deps-exe/66-stop b/src/66/deps-exe/66-stop index f1e60dad80b6949a291fd1320dd53b5888a8da74..306db624e775967d5f8dee9157a531ba4d44f70b 100644 --- a/src/66/deps-exe/66-stop +++ b/src/66/deps-exe/66-stop @@ -1,5 +1,4 @@ ${LIB66} --ls6rc -ls6 -loblibs -lskarnet diff --git a/src/66/deps-exe/66-svctl b/src/66/deps-exe/66-svctl index 6ca6412952014263a94e354b4e57f47c8fc25e21..ec930b0468a1ecc85f4399db401081861a34663b 100644 --- a/src/66/deps-exe/66-svctl +++ b/src/66/deps-exe/66-svctl @@ -1,5 +1,4 @@ ${LIB66} --ls6rc -ls6 -loblibs -lskarnet diff --git a/src/66/deps-exe/66-tree b/src/66/deps-exe/66-tree index c34874a1dc66e9c236cd245e418e3c5111d662ed..bc799f3349039873f5dba6b56a175016ea63b5b0 100644 --- a/src/66/deps-exe/66-tree +++ b/src/66/deps-exe/66-tree @@ -1,5 +1,4 @@ ${LIB66} --ls6rc -ls6 -loblibs -lskarnet