chiark / gitweb /
prefork-interp: make call_sock into a global
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 15 Aug 2022 17:49:42 +0000 (18:49 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 21 Aug 2022 20:21:10 +0000 (21:21 +0100)
This is much less typing.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
cprogs/prefork-interp.c

index 90dde3cd9de684ebea05e5e30ea16f4da458fe4d..9b773a40694a9cb726a95b7c987b3325332a728f 100644 (file)
@@ -92,6 +92,7 @@
 const char our_name[] = "prefork-interp";
 
 static struct sockaddr_un sockaddr_sun;
+static FILE *call_sock;
 
 #define ACK_BYTE '\n'
 
@@ -203,7 +204,7 @@ static void prepare_message(size_t *len, char **buf) {
     prepare_string(len, buf, s);
 }
 
-static void send_fd(FILE *call_sock, int payload_fd) {
+static void send_fd(int payload_fd) {
   int via_fd = fileno(call_sock);
 
   union {
@@ -246,12 +247,12 @@ static void send_fd(FILE *call_sock, int payload_fd) {
   }
 }
 
-static void send_request(FILE *call_sock) {
+static void send_request(void) {
   // Sending these first makes it easier for the script to
   // use buffered IO for the message.
-  send_fd(call_sock, 0);
-  send_fd(call_sock, 1);
-  send_fd(call_sock, 2);
+  send_fd(0);
+  send_fd(1);
+  send_fd(2);
 
   size_t len = 4;
   prepare_message(&len, 0);
@@ -283,7 +284,7 @@ static bool was_eof(FILE *call_sock) {
 }
 
 // Returns -1 on EOF
-static int protocol_read_maybe(FILE *call_sock, void *data, size_t sz) {
+static int protocol_read_maybe(void *data, size_t sz) {
   size_t sr = fread(data, sz, 1, call_sock);
   if (sr != 1) {
     if (was_eof(call_sock)) return -1;
@@ -292,16 +293,16 @@ static int protocol_read_maybe(FILE *call_sock, void *data, size_t sz) {
   return 0;
 }
 
-static void protocol_read(FILE *call_sock, void *data, size_t sz) {
-  if (protocol_read_maybe(call_sock, data, sz) < 0)
+static void protocol_read(void *data, size_t sz) {
+  if (protocol_read_maybe(data, sz) < 0)
     die("monitor process quit unexpectedly");
 }
 
 // Returns 0 if OK, error msg if peer was garbage.
-static const char *protocol_greeting(FILE *call_sock) {
+static const char *protocol_greeting(void) {
   char got_magic[sizeof(header_magic)];
 
-  if (protocol_read_maybe(call_sock, &got_magic, sizeof(got_magic)) < 0)
+  if (protocol_read_maybe(&got_magic, sizeof(got_magic)) < 0)
     return "initial monitor process quit";
 
   if (memcmp(got_magic, header_magic, sizeof(header_magic)))
@@ -309,9 +310,9 @@ static const char *protocol_greeting(FILE *call_sock) {
        got_magic[0], got_magic[1], got_magic[2], got_magic[3]);
 
   uint32_t xdata_len;
-  protocol_read(call_sock, &xdata_len, sizeof(xdata_len));
+  protocol_read(&xdata_len, sizeof(xdata_len));
   void *xdata = xmalloc(xdata_len);
-  protocol_read(call_sock, xdata, xdata_len);
+  protocol_read(xdata, xdata_len);
 
   return 0;
 }
@@ -321,7 +322,6 @@ static const char *protocol_greeting(FILE *call_sock) {
 static FILE *connect_existing(void) {
   int r;
   int fd = -1;
-  FILE *call_sock = 0;
 
   fd = socket(AF_UNIX, SOCK_STREAM, 0);
   if (fd==-1) diee("socket() for client");
@@ -336,13 +336,13 @@ static FILE *connect_existing(void) {
   call_sock = call_sock_from_fd(fd);
   fd = -1;
 
-  if (protocol_greeting(call_sock))
+  if (protocol_greeting())
     goto x_garbage;
 
   return call_sock;
 
  x_garbage:
-  if (call_sock) fclose(call_sock);
+  if (call_sock) { fclose(call_sock); call_sock=0; }
   if (fd >= 0) close(fd);
   return 0;
 }
@@ -369,15 +369,15 @@ void become_setup(int sfd, int fake_pair[2]) {
   diee("execute %s", executor_argv[0]);
 }
 
-static FILE *connect_or_spawn(void) {
+static void connect_or_spawn(void) {
   int r;
 
-  FILE *call_sock = connect_existing();
-  if (call_sock) return call_sock;
+  call_sock = connect_existing();
+  if (call_sock) return;
 
   int lockfd = acquire_lock();
   call_sock = connect_existing();
-  if (call_sock) { close(lockfd); return call_sock; }
+  if (call_sock) { close(lockfd); return; }
 
   // We must start a fresh one, and we hold the lock
 
@@ -418,11 +418,11 @@ 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);
+  const char *emsg = protocol_greeting();
   if (emsg) die("setup failed: %s", emsg);
 
   close(lockfd);
-  return call_sock;
+  return;
 }
 
 static void make_executor_argv(const char *const *argv) {
@@ -466,13 +466,13 @@ int main(int argc_unused, const char *const *argv) {
   assert(strlen(socket_path) <= sizeof(sockaddr_sun.sun_path));
   strncpy(sockaddr_sun.sun_path, socket_path, sizeof(sockaddr_sun.sun_path));
 
-  FILE *call_sock = connect_or_spawn();
+  connect_or_spawn();
 
   // We're committed now, send the request (or bail out)
-  send_request(call_sock);
+  send_request();
 
   uint32_t status;
-  protocol_read(call_sock, &status, sizeof(status));
+  protocol_read(&status, sizeof(status));
 
   status = ntohl(status);
   if (status > INT_MAX) die("status 0x%lx does not fit in an int",