dirplex, patplex: Really wait for children.
[ashd.git] / src / dirplex / dirplex.c
index f1f99e4..d5aabea 100644 (file)
@@ -25,6 +25,7 @@
 #include <ctype.h>
 #include <dirent.h>
 #include <time.h>
+#include <sys/wait.h>
 #include <sys/signal.h>
 
 #ifdef HAVE_CONFIG_H
 
 time_t now;
 
+static void chinit(void *idata)
+{
+    char *twd = idata;
+    
+    if(twd != NULL) {
+       /* This should never be able to fail other than for critical
+        * I/O errors or some such, since the path has already been
+        * traversed. */
+       if(chdir(twd))
+           exit(127);
+    }
+}
+
 static void handle(struct hthead *req, int fd, char *path, struct pattern *pat)
 {
     struct child *ch;
+    struct config *ccf;
+    char *twd;
 
-    headappheader(req, "X-Ash-File", path);
     if(pat->fchild) {
-       stdforkserve(pat->fchild, req, fd);
+       headappheader(req, "X-Ash-File", path);
+       stdforkserve(pat->fchild, req, fd, NULL, NULL);
     } else {
-       if((ch = findchild(path, pat->childnm)) == NULL) {
+       if((ch = findchild(path, pat->childnm, &ccf)) == NULL) {
            flog(LOG_ERR, "child %s requested, but was not declared", pat->childnm);
            simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", pat->childnm);
            return;
        }
-       if(childhandle(ch, req, fd))
+       twd = NULL;
+       if((twd = ccf->path) != NULL) {
+           if(!strcmp(twd, ".")) {
+               twd = NULL;
+           } else if(strncmp(path, twd, strlen(twd)) || (path[strlen(twd)] != '/')) {
+               /* Should be an impossible case under the current (and
+                * foreseeable) scheme. */
+               simpleerror(fd, 500, "Server Error", "An internal server error occurred.");
+               return;
+           } else {
+               path = path + strlen(twd) + 1;
+           }
+       }
+       headappheader(req, "X-Ash-File", path);
+       if(childhandle(ch, req, fd, chinit, twd))
            simpleerror(fd, 500, "Server Error", "The request handler crashed.");
     }
 }
@@ -188,9 +218,34 @@ static int checkentry(struct hthead *req, int fd, char *path, char *rest, char *
     return(0);
 }
 
+static int checkdir(struct hthead *req, int fd, char *path, char *rest)
+{
+    char *cpath;
+    struct config *cf, *ccf;
+    struct child *ch;
+    
+    cf = getconfig(path);
+    if(cf->capture != NULL) {
+       cpath = sprintf2("%s/", path);
+       if((ch = findchild(cpath, cf->capture, &ccf)) == NULL) {
+           free(cpath);
+           flog(LOG_ERR, "child %s requested for capture, but was not declared", cf->capture);
+           simpleerror(fd, 500, "Configuration Error", "The server is erroneously configured. Handler %s was requested, but not declared.", cf->capture);
+           return(1);
+       }
+       free(cpath);
+       if(*rest == '/')
+           rest++;
+       replrest(req, rest);
+       if(childhandle(ch, req, fd, chinit, ccf->path))
+           simpleerror(fd, 500, "Server Error", "The request handler crashed.");
+       return(1);
+    }
+    return(0);
+}
+
 static int checkpath(struct hthead *req, int fd, char *path, char *rest)
 {
-    struct config *cf;
     char *p, *el;
     int rv;
     
@@ -199,7 +254,8 @@ static int checkpath(struct hthead *req, int fd, char *path, char *rest)
     
     if(!strncmp(path, "./", 2))
        path += 2;
-    cf = getconfig(path);
+    if(checkdir(req, fd, path, rest))
+       return(1);
     
     if((p = strchr(rest, '/')) == NULL) {
        el = unquoteurl(rest);
@@ -241,6 +297,19 @@ static void serve(struct hthead *req, int fd)
        simpleerror(fd, 404, "Not Found", "The requested URL has no corresponding resource.");
 }
 
+static void chldhandler(int sig)
+{
+    pid_t pid;
+    
+    do {
+       pid = waitpid(-1, NULL, WNOHANG);
+    } while(pid > 0);
+}
+
+static void sighandler(int sig)
+{
+}
+
 static void usage(FILE *out)
 {
     fprintf(out, "usage: dirplex [-hN] [-c CONFIG] DIR\n");
@@ -300,7 +369,8 @@ int main(int argc, char **argv)
        flog(LOG_ERR, "could not change directory to %s: %s", argv[optind], strerror(errno));
        exit(1);
     }
-    signal(SIGCHLD, SIG_IGN);
+    signal(SIGCHLD, chldhandler);
+    signal(SIGPIPE, sighandler);
     while(1) {
        if((fd = recvreq(0, &req)) < 0) {
            if(errno != 0)