chiark / gitweb /
prefork-interp: New protocol
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 15 Aug 2022 17:43:25 +0000 (18:43 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 21 Aug 2022 20:21:10 +0000 (21:21 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
cprogs/prefork-interp.c
cprogs/prefork.c

index 4883a2dd684e7b01f224824dfa9bd9361f6b3acb..ae6b9e563aa952529add9ccb15aa03cef9b068bf 100644 (file)
@@ -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 [<options>]\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",
index 08958ce8a66e338da5c168a9e64a244f4e513d8c..909d0658ebf7f407f317f32164299b8306ff7101 100644 (file)
@@ -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); }