From c7215e9f5ae0b5df0fe4bf04e0d2433d6063b7da Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 15 Aug 2022 17:03:57 +0100 Subject: [PATCH] prefork-interp: break out protocol_exchange (nfc) Signed-off-by: Ian Jackson --- cprogs/prefork-interp.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/cprogs/prefork-interp.c b/cprogs/prefork-interp.c index 22cdc7d..3ebcf01 100644 --- a/cprogs/prefork-interp.c +++ b/cprogs/prefork-interp.c @@ -280,6 +280,22 @@ static bool was_eof(FILE *call_sock) { return feof(call_sock) || errno==ECONNRESET; } +// Does protocol exchange. Returns 0 if OK, -1 if peer was garbage. +static int protocol_exchange(FILE *call_sock) { + char ack; + size_t sr = fread(&ack, sizeof(ack), 1, call_sock); + if (sr != 1) { + if (was_eof(call_sock)) return -1; + diee("read() ack byte"); + } + if (ack != '\n') die("got ack byte 0x%02x, not '\n'", ack); + + // We're committed now, send the request (or bail out) + send_request(call_sock); + + return 0; +} + // Returns: call(client-end), or 0 to mean "is garbage" // find_socket_path must have been called static FILE *connect_existing(void) { @@ -300,16 +316,8 @@ static FILE *connect_existing(void) { call_sock = call_sock_from_fd(fd); fd = -1; - char ack; - size_t sr = fread(&ack, sizeof(ack), 1, call_sock); - if (sr != 1) { - if (was_eof(call_sock)) goto x_garbage; - diee("read() ack byte"); - } - if (ack != '\n') die("got ack byte 0x%02x, not '\n'", ack); - - // We're committed now, send the request (or bail out) - send_request(call_sock); + if (protocol_exchange(call_sock) < 0) + goto x_garbage; return call_sock; -- 2.30.2