From 7113c4cf792ec44ec04a54e9723fa31a97bee5a7 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Fri, 8 Sep 2023 08:38:06 +0000 Subject: Refactor s6-tls[cd] so they're ready to port to posix_spawn Signed-off-by: Laurent Bercot --- package/deps.mak | 9 ++++---- src/tls/deps-lib/s6tls | 4 ++-- src/tls/s6-tlsc.c | 9 +++++--- src/tls/s6-tlsd.c | 7 ++++-- src/tls/s6-ucspitlsc.c | 9 +++++--- src/tls/s6-ucspitlsd.c | 9 +++++--- src/tls/s6tls-internal.h | 8 +++++-- src/tls/s6tls_exec_tlscio.c | 52 ------------------------------------------ src/tls/s6tls_exec_tlsdio.c | 55 --------------------------------------------- src/tls/s6tls_prep_tlscio.c | 50 +++++++++++++++++++++++++++++++++++++++++ src/tls/s6tls_prep_tlsdio.c | 53 +++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 138 insertions(+), 127 deletions(-) delete mode 100644 src/tls/s6tls_exec_tlscio.c delete mode 100644 src/tls/s6tls_exec_tlsdio.c create mode 100644 src/tls/s6tls_prep_tlscio.c create mode 100644 src/tls/s6tls_prep_tlsdio.c diff --git a/package/deps.mak b/package/deps.mak index 0ba4787..f88348a 100644 --- a/package/deps.mak +++ b/package/deps.mak @@ -20,7 +20,6 @@ src/conn-tools/s6-tcpserver4d.o src/conn-tools/s6-tcpserver4d.lo: src/conn-tools src/conn-tools/s6-tcpserver6-socketbinder.o src/conn-tools/s6-tcpserver6-socketbinder.lo: src/conn-tools/s6-tcpserver6-socketbinder.c src/conn-tools/s6-tcpserver6.o src/conn-tools/s6-tcpserver6.lo: src/conn-tools/s6-tcpserver6.c src/include/s6-networking/config.h src/conn-tools/s6-tcpserver6d.o src/conn-tools/s6-tcpserver6d.lo: src/conn-tools/s6-tcpserver6d.c -src/conn-tools/tcpserverd_spawn.o src/conn-tools/tcpserverd_spawn.lo: src/conn-tools/tcpserverd_spawn.c src/conn-tools/tcpserverd-internal.h src/libs6net/s6net_ident_client.o src/libs6net/s6net_ident_client.lo: src/libs6net/s6net_ident_client.c src/include/s6-networking/ident.h src/libs6net/s6net_ident_error.o src/libs6net/s6net_ident_error.lo: src/libs6net/s6net_ident_error.c src/include/s6-networking/ident.h src/libs6net/s6net_ident_reply_get.o src/libs6net/s6net_ident_reply_get.lo: src/libs6net/s6net_ident_reply_get.c src/include/s6-networking/ident.h @@ -99,8 +98,8 @@ src/tls/s6-tlsserver.o src/tls/s6-tlsserver.lo: src/tls/s6-tlsserver.c src/inclu src/tls/s6-ucspitlsc.o src/tls/s6-ucspitlsc.lo: src/tls/s6-ucspitlsc.c src/include/s6-networking/config.h src/tls/s6tls-internal.h src/tls/s6-ucspitlsd.o src/tls/s6-ucspitlsd.lo: src/tls/s6-ucspitlsd.c src/include/s6-networking/config.h src/tls/s6tls-internal.h src/tls/s6tls_clean_and_exec.o src/tls/s6tls_clean_and_exec.lo: src/tls/s6tls_clean_and_exec.c src/tls/s6tls-internal.h -src/tls/s6tls_exec_tlscio.o src/tls/s6tls_exec_tlscio.lo: src/tls/s6tls_exec_tlscio.c src/include/s6-networking/config.h src/tls/s6tls-internal.h -src/tls/s6tls_exec_tlsdio.o src/tls/s6tls_exec_tlsdio.lo: src/tls/s6tls_exec_tlsdio.c src/include/s6-networking/config.h src/tls/s6tls-internal.h +src/tls/s6tls_prep_tlscio.o src/tls/s6tls_prep_tlscio.lo: src/tls/s6tls_prep_tlscio.c src/include/s6-networking/config.h src/tls/s6tls-internal.h +src/tls/s6tls_prep_tlsdio.o src/tls/s6tls_prep_tlsdio.lo: src/tls/s6tls_prep_tlsdio.c src/include/s6-networking/config.h src/tls/s6tls-internal.h src/tls/s6tls_sync_and_exec_app.o src/tls/s6tls_sync_and_exec_app.lo: src/tls/s6tls_sync_and_exec_app.c src/tls/s6tls-internal.h src/tls/s6tls_ucspi_exec_app.o src/tls/s6tls_ucspi_exec_app.lo: src/tls/s6tls_ucspi_exec_app.c src/tls/s6tls-internal.h @@ -158,9 +157,9 @@ endif libstls.so.xyzzy: EXTRA_LIBS := -lskarnet ${CRYPTO_LIB} ${TIMER_LIB} libstls.so.xyzzy: src/stls/stls_drop.lo src/stls/stls_handshake.lo src/stls/stls_run.lo src/stls/stls_client_init_and_handshake.lo src/stls/stls_server_init_and_handshake.lo src/stls/stls_send_environment.lo ifeq ($(strip $(STATIC_LIBS_ARE_PIC)),) -libs6tls.a.xyzzy: src/tls/s6tls_clean_and_exec.o src/tls/s6tls_exec_tlscio.o src/tls/s6tls_exec_tlsdio.o src/tls/s6tls_sync_and_exec_app.o src/tls/s6tls_ucspi_exec_app.o +libs6tls.a.xyzzy: src/tls/s6tls_clean_and_exec.o src/tls/s6tls_prep_tlscio.o src/tls/s6tls_prep_tlsdio.o src/tls/s6tls_sync_and_exec_app.o src/tls/s6tls_ucspi_exec_app.o else -libs6tls.a.xyzzy: src/tls/s6tls_clean_and_exec.lo src/tls/s6tls_exec_tlscio.lo src/tls/s6tls_exec_tlsdio.lo src/tls/s6tls_sync_and_exec_app.lo src/tls/s6tls_ucspi_exec_app.lo +libs6tls.a.xyzzy: src/tls/s6tls_clean_and_exec.lo src/tls/s6tls_prep_tlscio.lo src/tls/s6tls_prep_tlsdio.lo src/tls/s6tls_sync_and_exec_app.lo src/tls/s6tls_ucspi_exec_app.lo endif s6-tlsc: EXTRA_LIBS := -lskarnet s6-tlsc: src/tls/s6-tlsc.o libs6tls.a.xyzzy diff --git a/src/tls/deps-lib/s6tls b/src/tls/deps-lib/s6tls index f2306ac..07ad2f2 100644 --- a/src/tls/deps-lib/s6tls +++ b/src/tls/deps-lib/s6tls @@ -1,6 +1,6 @@ s6tls_clean_and_exec.o -s6tls_exec_tlscio.o -s6tls_exec_tlsdio.o +s6tls_prep_tlscio.o +s6tls_prep_tlsdio.o s6tls_sync_and_exec_app.o s6tls_ucspi_exec_app.o -lskarnet diff --git a/src/tls/s6-tlsc.c b/src/tls/s6-tlsc.c index 2cca21c..26703ba 100644 --- a/src/tls/s6-tlsc.c +++ b/src/tls/s6-tlsc.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "s6tls-internal.h" @@ -19,14 +20,16 @@ static void child (int const [4][2], uint32_t, unsigned int, unsigned int, char const *) gccattr_noreturn ; static void child (int const p[4][2], uint32_t options, unsigned int verbosity, unsigned int kimeout, char const *servername) { - int fds[3] = { p[0][0], p[1][1], p[2][1] } ; + char const *newargv[S6TLS_PREP_IO_ARGC] ; + char buf[S6TLS_PREP_IO_BUFLEN] ; PROG = "s6-tlsc (child)" ; close(p[2][0]) ; close(p[0][1]) ; close(p[1][0]) ; - if (fd_move(0, p[3][0]) < 0 || fd_move(1, p[3][1]) < 0) + if (fd_move(0, p[3][0]) == -1 || fd_move(1, p[3][1]) == -1) strerr_diefu1sys(111, "move network fds to stdin/stdout") ; - s6tls_exec_tlscio(fds, options, verbosity, kimeout, servername) ; + s6tls_prep_tlscio(newargv, buf, p[0][0], p[1][1], p[2][1], options, verbosity, kimeout, servername) ; + xexec(newargv) ; } int main (int argc, char const *const *argv) diff --git a/src/tls/s6-tlsd.c b/src/tls/s6-tlsd.c index 49c38c1..c9ef5d2 100644 --- a/src/tls/s6-tlsd.c +++ b/src/tls/s6-tlsd.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "s6tls-internal.h" @@ -18,12 +19,14 @@ static void child (int const [4][2], uint32_t, unsigned int, unsigned int, unsigned int) gccattr_noreturn ; static void child (int const p[4][2], uint32_t options, unsigned int verbosity, unsigned int kimeout, unsigned int snilevel) { - int fds[3] = { p[0][0], p[1][1], p[2][1] } ; + char const *newargv[S6TLS_PREP_IO_ARGC] ; + char buf[S6TLS_PREP_IO_BUFLEN] ; PROG = "s6-tlsd (child)" ; close(p[2][0]) ; close(p[0][1]) ; close(p[1][0]) ; - s6tls_exec_tlsdio(fds, options, verbosity, kimeout, snilevel) ; + s6tls_prep_tlsdio(newargv, buf, p[0][0], p[1][1], p[2][1], options, verbosity, kimeout, snilevel) ; + xexec(newargv) ; } int main (int argc, char const *const *argv) diff --git a/src/tls/s6-ucspitlsc.c b/src/tls/s6-ucspitlsc.c index 521bb4d..2d728b5 100644 --- a/src/tls/s6-ucspitlsc.c +++ b/src/tls/s6-ucspitlsc.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "s6tls-internal.h" @@ -20,15 +21,17 @@ static inline void child (int [4][2], uint32_t, unsigned int, unsigned int, char const *, pid_t) gccattr_noreturn ; static inline void child (int p[4][2], uint32_t options, unsigned int verbosity, unsigned int kimeout, char const *servername, pid_t pid) { - int fds[3] = { p[0][0], p[1][1], p[2][1] } ; + char const *newargv[S6TLS_PREP_IO_ARGC] ; + char buf[S6TLS_PREP_IO_BUFLEN] ; ssize_t r ; char c ; PROG = "s6-ucspitlsc" ; close(p[2][0]) ; close(p[0][1]) ; close(p[1][0]) ; - if (fd_move(0, p[3][0]) < 0 || fd_move(1, p[3][1]) < 0) + if (fd_move(0, p[3][0]) == -1 || fd_move(1, p[3][1]) == -1) strerr_diefu1sys(111, "move network fds to stdin/stdout") ; + s6tls_prep_tlscio(newargv, buf, p[0][0], p[1][1], p[2][1], options, verbosity, kimeout, servername) ; r = read(p[2][1], &c, 1) ; if (r < 0) strerr_diefu1sys(111, "read from control socket") ; if (!r) @@ -59,7 +62,7 @@ static inline void child (int p[4][2], uint32_t options, unsigned int verbosity, fmt[pid_fmt(fmt, pid)] = 0 ; strerr_warni4x("pid ", fmt, " accepted", " opportunistic TLS") ; } - s6tls_exec_tlscio(fds, options, verbosity, kimeout, servername) ; + xexec(newargv) ; } int main (int argc, char const *const *argv, char const *const *envp) diff --git a/src/tls/s6-ucspitlsd.c b/src/tls/s6-ucspitlsd.c index 37ec435..ecce9d5 100644 --- a/src/tls/s6-ucspitlsd.c +++ b/src/tls/s6-ucspitlsd.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include "s6tls-internal.h" @@ -19,13 +20,15 @@ static inline void child (int [4][2], uint32_t, unsigned int, unsigned int, unsigned int, pid_t) gccattr_noreturn ; static inline void child (int p[4][2], uint32_t options, unsigned int verbosity, unsigned int kimeout, unsigned int snilevel, pid_t pid) { - int fds[3] = { p[0][0], p[1][1], p[2][1] } ; + char const *newargv[S6TLS_PREP_IO_ARGC] ; + char buf[S6TLS_PREP_IO_BUFLEN] ; ssize_t r ; char c ; PROG = "s6-ucspitlsd" ; close(p[2][0]) ; close(p[0][1]) ; close(p[1][0]) ; + s6tls_prep_tlsdio(newargv, buf, p[0][0], p[1][1], p[2][1], options, verbosity, kimeout, snilevel) ; r = read(p[2][1], &c, 1) ; if (r < 0) strerr_diefu1sys(111, "read from control socket") ; if (!r) @@ -42,7 +45,7 @@ static inline void child (int p[4][2], uint32_t options, unsigned int verbosity, { case 'y' : close(p[2][1]) ; - p[2][1] = 0 ; /* we know 0 is open so it's a correct invalid value */ + p[2][1] = 0 ; /* we know 0 is open so it's a suitable invalid value */ break ; case 'Y' : fd_shutdown(p[2][1], 0) ; @@ -56,7 +59,7 @@ static inline void child (int p[4][2], uint32_t options, unsigned int verbosity, fmt[pid_fmt(fmt, pid)] = 0 ; strerr_warni4x("pid ", fmt, " accepted", " opportunistic TLS") ; } - s6tls_exec_tlsdio(fds, options, verbosity, kimeout, snilevel) ; + xexec(newargv) ; } int main (int argc, char const *const *argv) diff --git a/src/tls/s6tls-internal.h b/src/tls/s6tls-internal.h index d232266..cd96e87 100644 --- a/src/tls/s6tls-internal.h +++ b/src/tls/s6tls-internal.h @@ -7,9 +7,13 @@ #include #include +#include -extern void s6tls_exec_tlscio (int const *, uint32_t, unsigned int, unsigned int, char const *) gccattr_noreturn ; -extern void s6tls_exec_tlsdio (int const *, uint32_t, unsigned int, unsigned int, unsigned int) gccattr_noreturn ; +#define S6TLS_PREP_IO_ARGC 15 +#define S6TLS_PREP_IO_BUFLEN (5 * UINT_FMT) + +extern void s6tls_prep_tlscio (char const **, char *, int, int, int, uint32_t, unsigned int, unsigned int, char const *) ; +extern void s6tls_prep_tlsdio (char const **, char *, int, int, int, uint32_t, unsigned int, unsigned int, unsigned int) ; extern void s6tls_sync_and_exec_app (char const *const *, int const [4][2], pid_t, uint32_t) gccattr_noreturn ; extern void s6tls_ucspi_exec_app (char const *const *, int const [4][2], uint32_t) gccattr_noreturn ; extern void s6tls_clean_and_exec (char const *const *, uint32_t, char const *, size_t) gccattr_noreturn ; diff --git a/src/tls/s6tls_exec_tlscio.c b/src/tls/s6tls_exec_tlscio.c deleted file mode 100644 index e4e84f9..0000000 --- a/src/tls/s6tls_exec_tlscio.c +++ /dev/null @@ -1,52 +0,0 @@ -/* ISC license. */ - -#include -#include - -#include -#include "s6tls-internal.h" - -void s6tls_exec_tlscio (int const *fds, uint32_t options, unsigned int verbosity, unsigned int kimeout, char const *servername) -{ - char const *newargv[15] ; - unsigned int m = 0 ; - char fmtv[UINT_FMT] ; - char fmtd[UINT_FMT] ; - char fmtk[UINT_FMT] ; - char fmtr[UINT_FMT] ; - char fmtw[UINT_FMT] ; - - newargv[m++] = S6_NETWORKING_BINPREFIX "s6-tlsc-io" ; - if (verbosity != 1) - { - newargv[m++] = "-v" ; - newargv[m++] = fmtv ; - fmtv[uint_fmt(fmtv, verbosity)] = 0 ; - } - if (fds[2]) - { - newargv[m++] = "-d" ; - newargv[m++] = fmtd ; - fmtd[uint_fmt(fmtd, fds[2])] = 0 ; - } - newargv[m++] = options & 4 ? "-S" : "-s" ; - newargv[m++] = options & 1 ? "-y" : "-Y" ; - if (kimeout) - { - newargv[m++] = "-K" ; - newargv[m++] = fmtk ; - fmtk[uint_fmt(fmtk, kimeout)] = 0 ; - } - if (servername) - { - newargv[m++] = "-k" ; - newargv[m++] = servername ; - } - newargv[m++] = "--" ; - newargv[m++] = fmtr ; - fmtr[uint_fmt(fmtr, fds[0])] = 0 ; - newargv[m++] = fmtw ; - fmtw[uint_fmt(fmtw, fds[1])] = 0 ; - newargv[m++] = 0 ; - xexec(newargv) ; -} diff --git a/src/tls/s6tls_exec_tlsdio.c b/src/tls/s6tls_exec_tlsdio.c deleted file mode 100644 index f3bc999..0000000 --- a/src/tls/s6tls_exec_tlsdio.c +++ /dev/null @@ -1,55 +0,0 @@ -/* ISC license. */ - -#include -#include - -#include -#include "s6tls-internal.h" - -void s6tls_exec_tlsdio (int const *fds, uint32_t options, unsigned int verbosity, unsigned int kimeout, unsigned int snilevel) -{ - char const *newargv[15] ; - unsigned int m = 0 ; - char fmtv[UINT_FMT] ; - char fmtd[UINT_FMT] ; - char fmtk[UINT_FMT] ; - char fmtr[UINT_FMT] ; - char fmtw[UINT_FMT] ; - char fmti[UINT_FMT] ; - - newargv[m++] = S6_NETWORKING_BINPREFIX "s6-tlsd-io" ; - if (verbosity != 1) - { - newargv[m++] = "-v" ; - newargv[m++] = fmtv ; - fmtv[uint_fmt(fmtv, verbosity)] = 0 ; - } - if (fds[2]) - { - newargv[m++] = "-d" ; - newargv[m++] = fmtd ; - fmtd[uint_fmt(fmtd, fds[2])] = 0 ; - } - newargv[m++] = options & 4 ? "-S" : "-s" ; - if (options & 1) - newargv[m++] = options & 2 ? "-y" : "-Y" ; - if (kimeout) - { - newargv[m++] = "-K" ; - newargv[m++] = fmtk ; - fmtk[uint_fmt(fmtk, kimeout)] = 0 ; - } - if (snilevel) - { - newargv[m++] = "-k" ; - newargv[m++] = fmti ; - fmti[uint_fmt(fmti, snilevel)] = 0 ; - } - newargv[m++] = "--" ; - newargv[m++] = fmtr ; - fmtr[uint_fmt(fmtr, fds[0])] = 0 ; - newargv[m++] = fmtw ; - fmtw[uint_fmt(fmtw, fds[1])] = 0 ; - newargv[m++] = 0 ; - xexec(newargv) ; -} diff --git a/src/tls/s6tls_prep_tlscio.c b/src/tls/s6tls_prep_tlscio.c new file mode 100644 index 0000000..cf44511 --- /dev/null +++ b/src/tls/s6tls_prep_tlscio.c @@ -0,0 +1,50 @@ +/* ISC license. */ + +#include + +#include +#include "s6tls-internal.h" + +void s6tls_prep_tlscio (char const **argv, char *buf, int fdr, int fdw, int fdnotif, uint32_t options, unsigned int verbosity, unsigned int kimeout, char const *servername) +{ + size_t m = 0 ; + size_t n = 0 ; + + argv[m++] = S6_NETWORKING_BINPREFIX "s6-tlsc-io" ; + if (verbosity != 1) + { + argv[m++] = "-v" ; + argv[m++] = buf + n ; + n += uint_fmt(buf + n, verbosity) ; + buf[n++] = 0 ; + } + if (fdnotif) + { + argv[m++] = "-d" ; + argv[m++] = buf + n ; + n += uint_fmt(buf + n, fdnotif) ; + buf[n++] = 0 ; + } + argv[m++] = options & 4 ? "-S" : "-s" ; + argv[m++] = options & 1 ? "-y" : "-Y" ; + if (kimeout) + { + argv[m++] = "-K" ; + argv[m++] = buf + n ; + n += uint_fmt(buf + n, kimeout) ; + buf[n++] = 0 ; + } + if (servername) + { + argv[m++] = "-k" ; + argv[m++] = servername ; + } + argv[m++] = "--" ; + argv[m++] = buf + n ; + n += uint_fmt(buf + n, fdr) ; + buf[n++] = 0 ; + argv[m++] = buf + n ; + n += uint_fmt(buf + n, fdw) ; + buf[n++] = 0 ; + argv[m++] = 0 ; +} diff --git a/src/tls/s6tls_prep_tlsdio.c b/src/tls/s6tls_prep_tlsdio.c new file mode 100644 index 0000000..942425b --- /dev/null +++ b/src/tls/s6tls_prep_tlsdio.c @@ -0,0 +1,53 @@ +/* ISC license. */ + +#include + +#include +#include "s6tls-internal.h" + +void s6tls_prep_tlsdio (char const **argv, char *buf, int fdr, int fdw, int fdnotif, uint32_t options, unsigned int verbosity, unsigned int kimeout, unsigned int snilevel) +{ + size_t m = 0 ; + size_t n = 0 ; + + argv[m++] = S6_NETWORKING_BINPREFIX "s6-tlsd-io" ; + if (verbosity != 1) + { + argv[m++] = "-v" ; + argv[m++] = buf + n ; + n += uint_fmt(buf + n, verbosity) ; + buf[n++] = 0 ; + } + if (fdnotif) + { + argv[m++] = "-d" ; + argv[m++] = buf + n ; + n += uint_fmt(buf + n, fdnotif) ; + buf[n++] = 0 ; + } + argv[m++] = options & 4 ? "-S" : "-s" ; + if (options & 1) + argv[m++] = options & 2 ? "-y" : "-Y" ; + if (kimeout) + { + argv[m++] = "-K" ; + argv[m++] = buf + n ; + n += uint_fmt(buf + n, kimeout) ; + buf[n++] = 0 ; + } + if (snilevel) + { + argv[m++] = "-k" ; + argv[m++] = buf + n ; + n += uint_fmt(buf + n, snilevel) ; + buf[n++] = 0 ; + } + argv[m++] = "--" ; + argv[m++] = buf + n ; + n += uint_fmt(buf + n, fdr) ; + buf[n++] = 0 ; + argv[m++] = buf + n ; + n += uint_fmt(buf + n, fdw) ; + buf[n++] = 0 ; + argv[m++] = 0 ; +} -- cgit v1.2.3