chiark / gitweb /
fixes from last change l-(
[disorder] / server / trackdb.c
index b0eb38333f8d3460ba8e5f605ba492231de218f9..432d212925405bf5bd9fd9ba4156451ef77edd24 100644 (file)
@@ -34,6 +34,7 @@
 #include <sys/resource.h>
 #include <time.h>
 #include <arpa/inet.h>
+#include <sys/wait.h>
 
 #include "event.h"
 #include "mem.h"
@@ -157,22 +158,13 @@ static int reap_db_deadlock(ev_source attribute((unused)) *ev,
 static pid_t subprogram(ev_source *ev, const char *prog,
                         int outputfd) {
   pid_t pid;
-  int lfd;
 
   /* If we're in the background then trap subprocess stdout/stderr */
-  if(!isatty(2))
-    lfd = logfd(ev, prog);
-  else
-    lfd = -1;
   if(!(pid = xfork())) {
     exitfn = _exit;
-    ev_signal_atfork(ev);
+    if(ev)
+      ev_signal_atfork(ev);
     signal(SIGPIPE, SIG_DFL);
-    if(lfd != -1) {
-      xdup2(lfd, 1);
-      xdup2(lfd, 2);
-      xclose(lfd);
-    }
     if(outputfd != -1) {
       xdup2(outputfd, 1);
       xclose(outputfd);
@@ -186,7 +178,6 @@ static pid_t subprogram(ev_source *ev, const char *prog,
            (char *)0);
     fatal(errno, "error invoking %s", prog);
   }
-  if(lfd != -1) xclose(lfd);
   return pid;
 }
 
@@ -1827,9 +1818,9 @@ static int reap_rescan(ev_source attribute((unused)) *ev,
                        void attribute((unused)) *u) {
   if(pid == rescan_pid) rescan_pid = -1;
   if(status)
-    error(0, "disorderd-rescan: %s", wstat(status));
+    error(0, RESCAN": %s", wstat(status));
   else
-    D(("disorderd-rescan terminate: %s", wstat(status)));
+    D((RESCAN" terminated: %s", wstat(status)));
   /* Our cache of file lookups is out of date now */
   cache_clean(&cache_files_type);
   eventlog("rescanned", (char *)0);
@@ -1837,14 +1828,22 @@ static int reap_rescan(ev_source attribute((unused)) *ev,
 }
 
 void trackdb_rescan(ev_source *ev) {
+  int w;
+
   if(rescan_pid != -1) {
     error(0, "rescan already underway");
     return;
   }
   rescan_pid = subprogram(ev, RESCAN, -1);
-  ev_child(ev, rescan_pid, 0, reap_rescan, 0);
-  D(("started rescanner"));
-  
+  if(ev) {
+    ev_child(ev, rescan_pid, 0, reap_rescan, 0);
+    D(("started rescanner"));
+  } else {
+    /* This is the first rescan, we block until it is complete */
+    while(waitpid(rescan_pid, &w, 0) < 0 && errno == EINTR)
+      ;
+    reap_rescan(0, rescan_pid, w, 0, 0);
+  }
 }
 
 int trackdb_rescan_cancel(void) {