#include <sys/resource.h>
#include <time.h>
#include <arpa/inet.h>
+#include <sys/wait.h>
#include "event.h"
#include "mem.h"
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);
(char *)0);
fatal(errno, "error invoking %s", prog);
}
- if(lfd != -1) xclose(lfd);
return pid;
}
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);
}
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) {