chiark / gitweb /
prefork-interp: Fix an uninitialised variable
[chiark-utils.git] / cprogs / prefork-interp.c
index 65aa679a93f2054a6987c97566219c2d09cf75d1..2215ded6dc776668d3b7c873ef017455ac5a40b7 100644 (file)
@@ -91,7 +91,7 @@ struct sockaddr_un sun;
 
 #define ACK_BYTE '\n'
 
-static struct sockaddr_unix socket_sun;
+static struct sockaddr_un socket_sun;
 static const char *const *executor_argv;
 
 static void propagate_exit_status(int status, const char *what) {
@@ -130,11 +130,11 @@ static void propagate_exit_status(int status, const char *what) {
          signame);
     }
 
-    die("setup failed due to signal %d %s%s", sig, signame,
+    die("%s failed due to signal %d %s%s", what, sig, signame,
        WCOREDUMP(status) ? " (core dumped)" : "");
   }
 
-  die("setup failed with weird wait status %d 0x%x", status, status);
+  die("%s failed with weird wait status %d 0x%x", what, status, status);
 }
 
 static __attribute((noreturn)) void die_data_overflow(void) {
@@ -181,7 +181,9 @@ static void prepare_message(size_t *len, char **buf) {
     prepare_string(len, buf, s);
 }
 
-static void send_fd(int via_fd, int payload_fd) {
+static void send_fd(FILE *call_sock, int payload_fd) {
+  int via_fd = fileno(call_sock);
+
   union {
     struct cmsghdr align;
     char buf[CMSG_SPACE(sizeof(payload_fd))];
@@ -222,12 +224,12 @@ static void send_fd(int via_fd, int payload_fd) {
   }
 }
 
-static void send_request(int call_fd) {
+static void send_request(FILE *call_sock) {
   // Sending these first makes it easier for the script to
   // use buffered IO for the message.
-  send_fd(call_fd, 0);
-  send_fd(call_fd, 1);
-  send_fd(call_fd, 2);
+  send_fd(call_sock, 0);
+  send_fd(call_sock, 1);
+  send_fd(call_sock, 2);
 
   size_t len = 4;
   prepare_message(&len, 0);
@@ -238,62 +240,67 @@ static void send_request(int call_fd) {
   prepare_message(0, &p);
   assert(p == m + len);
 
-  p = m;
-  while (len) {
-    ssize_t r = write(call_fd, p, len);
-    if (r==-1) {
-      if (errno == EINTR) continue;
-      diee("write request");
-    }
-    assert(r <= len);
-    assert(r > 0);
-    len -= r;
-    p += r;
-  }
+  ssize_t sr = fwrite(p, len, 1, call_sock);
+  if (sr != 1) diee("write request");
+}
+
+static FILE *call_sock_from_fd(int fd) {
+  int r;
+
+  FILE *call_sock = fdopen(fd, "r+");
+  if (!call_sock) diee("fdopen socket");
+
+  r = setvbuf(call_sock, 0, _IONBF, 0);
+  if (r) die("setvbuf socket");
+
+  return call_sock;
+}
+
+static bool was_eof(FILE *call_sock) {
+  return feof(call_sock) || errno==ECONNRESET;
 }
 
-// Returns: call(client-end) fd, or -1 to mean "is garbage"
+// Returns: call(client-end), or 0 to mean "is garbage"
 // find_socket_path must have been called
-static int connect_existing(void) {
+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");
 
   socklen_t salen = sizeof(sun);
-  r = connect(client, (const struct sockaddr*)&socket_sun, salen);
+  r = connect(fd, (const struct sockaddr*)&socket_sun, salen);
   if (r==-1) {
-    if (errno==ECONNREFUSED || errno==ENOENT) goto x_garbgae;
+    if (errno==ECONNREFUSED || errno==ENOENT) goto x_garbage;
     diee("connect() %s", socket_path);
   }
 
-  for (;;) {
-    char ack;
-    sr = read(fd, &ack, 1);
-    if (sr == -1) {
-      if (errno==ECONNRESET) goto x_garbage;
-      if (errno==EINTR) continue;
-      diee("read() ack byte");
-    }
-    if (sr == 0) { goto x_garbage; }
-    if (ack != '\n') die("got ack byte 0x%02x, not '\n'", ack);
-    break;
+  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, argv);
+  send_request(call_sock);
 
-  return fd;
+  return call_sock;
 
  x_garbage:
+  fclose(call_sock);
   if (fd >= 0) close(fd);
-  return -1;
+  return 0;
 }
 
-static void become_setup(int sfd, int fake_pair[2])
-  __attribute__((noreturn))
-{
+static __attribute__((noreturn))
+void become_setup(int sfd, int fake_pair[2]) {
   close(fake_pair[0]);
   int call_fd = fake_pair[1];
 
@@ -304,25 +311,27 @@ static void become_setup(int sfd, int fake_pair[2])
   if (dup2(null_0, 0)) diee("dup2 /dev/null onto stdin");
   if (dup2(2, 1) != 1) die("dup2 stderr onto stdout");
 
-  putenv(asprintf("PREFORK_INTERP=%d,%d,%d,%d,%s",
-                 sfd, call_fd, fd0_save, fd1_save, socket_path));
+  putenv(m_asprintf("PREFORK_INTERP=%d,%d,%d,%d,%s",
+                   sfd, call_fd, fd0_save, fd1_save, socket_path));
 
-  execvp(executor_argv[0], executor_argv);
+  execvp(executor_argv[0], (char**)executor_argv);
   diee("execute %s", executor_argv[0]);
 }
 
-static int connect_or_spawn(void) {
-  int fd = connect_existing();
-  if (fd >= 0) return fd;
+static FILE *connect_or_spawn(void) {
+  int r;
+
+  FILE *call_sock = connect_existing();
+  if (call_sock) return call_sock;
 
   int lockfd = acquire_lock();
-  fd = connect_existing();
-  if (fd >= 0) { close(lockfd); return fd; }
+  call_sock = connect_existing();
+  if (call_sock) { close(lockfd); return call_sock; }
 
   // We must start a fresh one, and we hold the lock
 
-  r = unlink(socketpath);
-  if (r<0) diee("failed to remove stale socket %s", socketpath);
+  r = unlink(socket_path);
+  if (r<0) diee("failed to remove stale socket %s", socket_path);
 
   int fake_pair[2];
   r = socketpair(AF_UNIX, SOCK_STREAM, 0, fake_pair);
@@ -331,13 +340,13 @@ static int connect_or_spawn(void) {
   int sfd = socket(AF_UNIX, SOCK_STREAM, 0);
   if (sfd<0) diee("socket() for new listener");
 
-  salen_t salen = sizeof(sun);
-  r= bind(sfd, (const struct sockaddr*)&socket_sun, saledn);
+  socklen_t salen = sizeof(sun);
+  r= bind(sfd, (const struct sockaddr*)&socket_sun, salen);
   if (r<0) diee("bind() on new listener");
 
-  // We never want callers to get ECONNREFUSED!.
+  // We never want callers to get ECONNREFUSED.  But:
   // There is a race here: from my RTFM they may get ECONNREFUSED
-  // if they tr between our bind() and listen().  But if they do, they'll
+  // if they try between our bind() and listen().  But if they do, they'll
   // acquire the lock (serialising with us) and retry, and then it will work.
   r = listen(sfd, INT_MAX);
   if (r<0) diee("listen() for new listener");
@@ -353,14 +362,15 @@ static int connect_or_spawn(void) {
   if (got == (pid_t)-1) diee("waitpid setup [%ld]", (long)setup_pid);
   if (got != setup_pid) diee("waitpid setup [%ld] gave [%ld]!",
                             (long)setup_pid, (long)got);
-  if (status != 0) propagate_exit_status(status);
+  if (status != 0) propagate_exit_status(status, "setup");
 
   close(lockfd);
-  return fake_pair[0];
+  return call_sock_from_fd(fake_pair[0]);
 }
 
 static void make_executor_argv(const char *const *argv) {
-  #define EACH_NEW_ARGV(EACH) {                        \
+  const char *arg;
+  #define EACH_NEW_ARG(EACH) {                 \
     arg = interp; { EACH }                     \
     if ((arg = script)) { EACH }               \
     const char *const *walk = argv;            \
@@ -368,13 +378,13 @@ static void make_executor_argv(const char *const *argv) {
   }
 
   size_t count = 1;
-  MAKE_NEW_ARGV( (void)arg; count++; );
+  EACH_NEW_ARG( (void)arg; count++; );
 
-  executor_argv = calloc(count, sizeof(char*));
+  const char **out = calloc(count, sizeof(char*));
+  executor_argv = (const char* const*)out;
   if (!executor_argv) diee("allocate for arguments");
 
-  char **out = executor_argv;
-  MAKE_NEW_ARGV( *out++ = arg; );
+  EACH_NEW_ARG( *out++ = arg; );
   *out++ = 0;
 }  
 
@@ -394,5 +404,17 @@ int main(int argc_unused, const char *const *argv) {
   assert(strlen(socket_path) <= sizeof(sun.sun_path));
   strncpy(sun.sun_path, socket_path, sizeof(sun.sun_path));
 
-  int call_fd = connect_or_spawn();
+  FILE *call_sock = connect_or_spawn();
+  uint32_t status;
+  ssize_t sr = fread(&status, sizeof(status), 1, call_sock);
+  if (sr != 1) {
+    if (was_eof(call_sock)) die("per-call server monitor process quit");
+    diee("read status from call socket");
+  }
+
+  status = ntohl(status);
+  if (status > INT_MAX) die("status 0x%lx does not fit in an int",
+                           (unsigned long)status);
+
+  propagate_exit_status(status, "invocation");
 }