Added callbacks to handle child process initialization.
authorFredrik Tolf <fredrik@dolda2000.com>
Sun, 5 Sep 2010 03:57:43 +0000 (05:57 +0200)
committerFredrik Tolf <fredrik@dolda2000.com>
Sun, 5 Sep 2010 03:57:43 +0000 (05:57 +0200)
lib/cf.c
lib/cf.h
lib/proc.c
lib/proc.h
src/dirplex/dirplex.c
src/htparser.c
src/patplex.c

index 719fcad..432f78a 100644 (file)
--- a/lib/cf.c
+++ b/lib/cf.c
@@ -326,16 +326,16 @@ struct child *parsechild(struct cfstate *s)
     return(ch);
 }
 
-int childhandle(struct child *ch, struct hthead *req, int fd)
+int childhandle(struct child *ch, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
 {
     if(ch->type == CH_SOCKET) {
        if(ch->fd < 0)
-           ch->fd = stdmkchild(ch->argv);
+           ch->fd = stdmkchild(ch->argv, chinit, idata);
        if(sendreq(ch->fd, req, fd)) {
            if(errno == EPIPE) {
                /* Assume that the child has crashed and restart it. */
                close(ch->fd);
-               ch->fd = stdmkchild(ch->argv);
+               ch->fd = stdmkchild(ch->argv, chinit, idata);
                if(!sendreq(ch->fd, req, fd))
                    return(0);
            }
@@ -345,7 +345,7 @@ int childhandle(struct child *ch, struct hthead *req, int fd)
            return(-1);
        }
     } else if(ch->type == CH_FORK) {
-       if(stdforkserve(ch->argv, req, fd) < 0)
+       if(stdforkserve(ch->argv, req, fd, chinit, idata) < 0)
            return(-1);
     }
     return(0);
index 45e7698..a1eadb0 100644 (file)
--- a/lib/cf.h
+++ b/lib/cf.h
@@ -28,6 +28,6 @@ char *findstdconf(char *name);
 
 void freechild(struct child *ch);
 struct child *parsechild(struct cfstate *s);
-int childhandle(struct child *ch, struct hthead *req, int fd);
+int childhandle(struct child *ch, struct hthead *req, int fd, void (*chinit)(void *), void *idata);
 
 #endif
index 2e61e92..8646434 100644 (file)
@@ -31,7 +31,7 @@
 #include <proc.h>
 #include <req.h>
 
-int stdmkchild(char **argv)
+int stdmkchild(char **argv, void (*chinit)(void *), void *idata)
 {
     int i;
     pid_t pid;
@@ -42,6 +42,8 @@ int stdmkchild(char **argv)
     if((pid = fork()) < 0)
        return(-1);
     if(pid == 0) {
+       if(chinit != NULL)
+           chinit(idata);
        for(i = 3; i < FD_SETSIZE; i++) {
            if(i != fd[0])
                close(i);
@@ -124,7 +126,7 @@ int recvfd(int sock, char **data, size_t *datalen)
     return(fd);
 }
 
-pid_t stdforkserve(char **argv, struct hthead *req, int fd)
+pid_t stdforkserve(char **argv, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
 {
     int i;
     char *ebuf, *p;
@@ -134,6 +136,9 @@ pid_t stdforkserve(char **argv, struct hthead *req, int fd)
     if((pid = fork()) < 0)
        return(-1);
     if(pid == 0) {
+       if(chinit != NULL)
+           chinit(idata);
+       
        dup2(fd, 0);
        dup2(fd, 1);
        for(i = 3; i < FD_SETSIZE; i++)
index da230b3..72bff20 100644 (file)
@@ -3,9 +3,9 @@
 
 #include <req.h>
 
-int stdmkchild(char **argv);
+int stdmkchild(char **argv, void (*chinit)(void *), void *idata);
 int sendfd(int sock, int fd, char *data, size_t datalen);
 int recvfd(int sock, char **data, size_t *datalen);
-pid_t stdforkserve(char **argv, struct hthead *req, int fd);
+pid_t stdforkserve(char **argv, struct hthead *req, int fd, void (*chinit)(void *), void *idata);
 
 #endif
index f1f99e4..c31c6cd 100644 (file)
@@ -47,14 +47,14 @@ static void handle(struct hthead *req, int fd, char *path, struct pattern *pat)
 
     headappheader(req, "X-Ash-File", path);
     if(pat->fchild) {
-       stdforkserve(pat->fchild, req, fd);
+       stdforkserve(pat->fchild, req, fd, NULL, NULL);
     } else {
        if((ch = findchild(path, pat->childnm)) == 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))
+       if(childhandle(ch, req, fd, NULL, NULL))
            simpleerror(fd, 500, "Server Error", "The request handler crashed.");
     }
 }
index c8064f2..503df99 100644 (file)
@@ -454,7 +454,7 @@ int main(int argc, char **argv)
        usage(stderr);
        exit(1);
     }
-    if((plex = stdmkchild(argv + ++i)) < 0) {
+    if((plex = stdmkchild(argv + ++i, NULL, NULL)) < 0) {
        flog(LOG_ERR, "could not spawn root multiplexer: %s", strerror(errno));
        return(1);
     }
index d296e04..04b7a10 100644 (file)
@@ -438,7 +438,7 @@ static void serve(struct hthead *req, int fd)
        return;
     }
     
-    if(childhandle(ch, req, fd))
+    if(childhandle(ch, req, fd, NULL, NULL))
        simpleerror(fd, 500, "Server Error", "The request handler crashed.");
 }