From bfb36b0690bde76283c62505f7031b6b161d429d Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 15 Aug 2022 18:43:25 +0100 Subject: [PATCH] prefork-interp: New protocol Signed-off-by: Ian Jackson --- cprogs/prefork-interp.c | 36 ++++++++++++++++++++---------------- cprogs/prefork.c | 2 -- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/cprogs/prefork-interp.c b/cprogs/prefork-interp.c index 4883a2d..ae6b9e5 100644 --- a/cprogs/prefork-interp.c +++ b/cprogs/prefork-interp.c @@ -97,6 +97,8 @@ struct sockaddr_un sockaddr_sun; static const char *const *executor_argv; +static const char header_magic[4] = "PFI\n"; + void fusagemessage(FILE *f) { fprintf(f, "usage: #!/usr/bin/prefork-interp []\n"); } @@ -295,24 +297,21 @@ static void protocol_read(FILE *call_sock, void *data, size_t sz) { die("monitor process quit unexpectedly"); } -// Does protocol exchange. Returns 0 if OK, error msg if peer was garbage. -static const char *protocol_exchange(FILE *call_sock) { - char magic[sizeof(header_magic)]; +// Returns 0 if OK, error msg if peer was garbage. +static const char *protocol_greeting(FILE *call_sock) { + char got_magic[sizeof(header_magic)]; - if (protocol_read_maybe(&magic, sizeof(magic)) < 0) + if (protocol_read_maybe(call_sock, &got_magic, sizeof(got_magic)) < 0) return "initial monitor process quit"; - if (memcmp(header, header_magic, sizeof(header_magic))) - die("got unexpected protocol magic 0x%04lx", - header[0], header[1], header[2], header[3]); + if (memcmp(got_magic, header_magic, sizeof(header_magic))) + die("got unexpected protocol magic 0x%02x%02x%02x%02x", + got_magic[0], got_magic[1], got_magic[2], got_magic[3]); uint32_t xdata_len; - protocol_read(&xdata_len, sizeof(xdata_len)); + protocol_read(call_sock, &xdata_len, sizeof(xdata_len)); void *xdata = xmalloc(xdata_len); - protocol_read(xdata, xdata_len); - - // We're committed now, send the request (or bail out) - send_request(call_sock); + protocol_read(call_sock, xdata, xdata_len); return 0; } @@ -337,7 +336,7 @@ static FILE *connect_existing(void) { call_sock = call_sock_from_fd(fd); fd = -1; - if (protocol_exchange(call_sock)) + if (protocol_greeting(call_sock)) goto x_garbage; return call_sock; @@ -411,8 +410,6 @@ static FILE *connect_or_spawn(void) { close(sfd); call_sock = call_sock_from_fd(fake_pair[0]); - const char *emsg = protocol_exchange(call_sock); - if (emsg) die("setup failed: %s", emsg); int status; pid_t got = waitpid(setup_pid, &status, 0); @@ -421,6 +418,9 @@ static FILE *connect_or_spawn(void) { (long)setup_pid, (long)got); if (status != 0) propagate_exit_status(status, "setup"); + const char *emsg = protocol_greeting(call_sock); + if (emsg) die("setup failed: %s", emsg); + close(lockfd); return call_sock; } @@ -467,8 +467,12 @@ int main(int argc_unused, const char *const *argv) { strncpy(sockaddr_sun.sun_path, socket_path, sizeof(sockaddr_sun.sun_path)); FILE *call_sock = connect_or_spawn(); + + // We're committed now, send the request (or bail out) + send_request(call_sock); + uint32_t status; - protocol_read(&status, sizeof(status); + protocol_read(call_sock, &status, sizeof(status)); status = ntohl(status); if (status > INT_MAX) die("status 0x%lx does not fit in an int", diff --git a/cprogs/prefork.c b/cprogs/prefork.c index 08958ce..909d065 100644 --- a/cprogs/prefork.c +++ b/cprogs/prefork.c @@ -10,8 +10,6 @@ static uid_t us; static const char *run_base; static const char *run_base_mkdir_p; -static const char header_magic[4] = "PFI\n"; - void common_diee(const char *m) { diee("%s", m); } void common_die (const char *m) { die ("%s", m); } -- 2.30.2