chiark / gitweb /
prefork-interp: break out stabs_same_inode
[chiark-utils.git] / cprogs / prefork-interp.c
index 11256015fe2d46b19a4ad51926c15f275c4212dc..2a7fdc35c54dd8a24b56316abf94c64be1f1b722 100644 (file)
@@ -271,13 +271,14 @@ static void send_request(void) {
   size_t len = 0;
   prepare_message(&len, 0);
 
-  char *m = xmalloc(len + 4);
+  size_t tlen = len + 4;
+  char *m = xmalloc(tlen);
   char *p = m;
   prepare_length(0, &p, len);
   prepare_message(0, &p);
-  assert(p == m + len + 4);
+  assert(p == m + tlen);
 
-  ssize_t sr = fwrite(p, len, 1, call_sock);
+  ssize_t sr = fwrite(m, tlen, 1, call_sock);
   if (sr != 1) diee("write request (buffer)");
 
   if (fflush(call_sock)) diee("write request");
@@ -390,8 +391,7 @@ static void watcher_cb_sockpath(uv_fs_event_t *handle, const char *filename,
       if (errno==EINTR) continue;
       diee("stat socket: %s", socket_path);
     }
-    if (!(now_stab.st_dev == initial_stab.st_dev &&
-         now_stab.st_ino == initial_stab.st_ino))
+    if (!stabs_same_inode(&now_stab, &initial_stab))
       _exit(0);
   }
 }
@@ -404,7 +404,7 @@ void become_watcher(void) {
   uv_fs_event_t uvhandle_sockpath;
   int r;
 
-  if (fcntl(0, F_SETFL, O_NONBLOCK)) diee("watcher set stdin nonblocking");
+  nonblock(0);
 
   errno= -uv_loop_init(&loop);
   if (errno) diee("watcher: uv_loop_init");
@@ -446,16 +446,19 @@ void become_setup(int sfd, int fake_pair[2],
 
   int null_0 = open("/dev/null", O_RDONLY);  if (null_0 < 0) diee("open null");
   if (dup2(null_0, 0)) diee("dup2 /dev/null onto stdin");
+  close(null_0);
   if (dup2(2, 1) != 1) die("dup2 stderr onto stdout");
 
+  nonblock(sfd);
+
   // Extension could work like this:
   //
   // We advertise a new protocol (perhaps one which is nearly entirely
   // different after the connect) by putting a name for it comma-separated
   // next to "v1".  Simple extension can be done by having the script
   // side say something about it in the ack xdata, which we currently ignore.
-  putenv(m_asprintf("PREFORK_INTERP=v1 %d,%d %s",
-                   sfd, call_fd, socket_path, watcher_stdin, watcher_stderr));
+  putenv(m_asprintf("PREFORK_INTERP=v1 %d,%d,%d,%d",
+                   sfd, call_fd, watcher_stdin, watcher_stderr));
 
   execvp(executor_argv[0], (char**)executor_argv);
   diee("execute %s", executor_argv[0]);
@@ -509,7 +512,7 @@ static void connect_or_spawn(void) {
     close(watcher_stdin[1]);
     close(watcher_stderr[0]);
     if (dup2(watcher_stdin[0], 0) != 0 ||
-       dup2(watcher_stderr[1], 1) != 0)
+       dup2(watcher_stderr[1], 1) != 1)
       diee("initial dup2() for watcher");
     close(watcher_stdin[0]);
     close(watcher_stderr[1]);
@@ -518,8 +521,7 @@ static void connect_or_spawn(void) {
 
   close(watcher_stdin[0]);
   close(watcher_stderr[1]);
-  if (fcntl(watcher_stderr[0], F_SETFL, O_NONBLOCK))
-    diee("parent set watcher stderr nonblocking");
+  nonblock(watcher_stderr[0]);
 
   // Fork setup