From: Ian Jackson Date: Mon, 15 Aug 2022 20:20:46 +0000 (+0100) Subject: Revert "prefork-interp: Protocol work" X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=f4c97cbe0d8b7648d365c59669811f5121db7d3d;p=chiark-utils.git Revert "prefork-interp: Protocol work" This reverts commit 6ef8d3dd7e4490db0ce5c753e6b8c94c16fc2035. Some of this is a bad idea. Notably, changing the arg/env format is just makework. Signed-off-by: Ian Jackson --- diff --git a/cprogs/prefork-interp.c b/cprogs/prefork-interp.c index 2e773d2..c62f00a 100644 --- a/cprogs/prefork-interp.c +++ b/cprogs/prefork-interp.c @@ -188,22 +188,20 @@ static void prepare_string(size_t *len, char **buf, const char *s) { prepare_data(len, buf, s, sl+1); } -static void prepare_string_list(size_t *len, char **buf, - const char *const *list) { +static void prepare_message(size_t *len, char **buf) { const char *s; - const char *const *p; - - size_t count = 0; - for (p = list; (s = *p++); ) count++; - char *count_s = m_asprintf("%zd", count); - prepare_string(len, buf, count_s); - for (p = list; (s = *p++); ) prepare_string(len, buf, s); -} + const char *const *p = (void*)environ; + while ((s = *p++)) { + if (strchr(s, '=')) + prepare_string(len, buf, s); + } -static void prepare_message(size_t *len, char **buf) { - prepare_string_list(len, buf, (const char* const*)environ); - prepare_string_list(len, buf, executor_argv); + prepare_string(len, buf, ""); + + p = executor_argv; + while ((s = *p++)) + prepare_string(len, buf, s); } static void send_fd(int payload_fd) { @@ -256,14 +254,14 @@ static void send_request(void) { send_fd(1); send_fd(2); - size_t len = 0; + size_t len = 4; prepare_message(&len, 0); - - char *m = xmalloc(len + 4); + char *m = malloc(len); + if (!m) diee("failed to allocate for message"); char *p = m; - prepare_length(0, &p, len); + prepare_length(0, &p, len - 4); prepare_message(0, &p); - assert(p == m + len + 4); + assert(p == m + len); ssize_t sr = fwrite(p, len, 1, call_sock); if (sr != 1) diee("write request"); diff --git a/perl/Prefork.pm b/perl/Prefork.pm index 10c7ec1..bb39265 100644 --- a/perl/Prefork.pm +++ b/perl/Prefork.pm @@ -36,10 +36,6 @@ sub server_quit ($) { # Returns in the executor process sub become_monitor () { - close LISTEN; - eval { protocol_exchange(); 1; } - or fail_log("protocol exchange failed: $@"); - my $child = fork // fail_log("fork executor: $!"); if (!$child) { #---- executor ---- @@ -47,7 +43,6 @@ sub become_monitor () { open ::STDOUT, ">& $call_fds[1]" or fail_log("dup for fd1"); open ::STDERR, ">& $call_fds[2]" or fail_log("dup for fd2"); close_call_fds(); - $! = 0; return; } @@ -59,8 +54,16 @@ sub become_monitor () { _exit(0); } +sub forked_monitor () { + close LISTEN; + eval { protocol_exchange(); 1; } + or fail_log("protocol exchange failed: $@"); + return become_monitor(); +} + sub close_call_fds () { foreach (@call_fds) { + next if $_ <= 2; POSIX::close($_); } close CALL; @@ -90,8 +93,7 @@ sub protocol_read_fail ($) { } sub protocol_exchange () { - my $greeting = "PFI\n\0\0\0\0"; - protocol_write($greeting); + protocol_write("\n"); @call_fds = map { my $r; @@ -221,7 +223,7 @@ sub initialisation_complete { $child = fork // fail_log("fork for accepted call failed: $!"); if (!$child) { #---- monitor [1] ---- - become_monitor(); + forked_monitor(); } close(CALL); $errcount = 0;