From 6a7a868e28e79f5502cc4b37578c263f640439c8 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Sun, 5 Sep 2010 05:57:43 +0200 Subject: [PATCH] Added callbacks to handle child process initialization. --- lib/cf.c | 8 ++++---- lib/cf.h | 2 +- lib/proc.c | 9 +++++++-- lib/proc.h | 4 ++-- src/dirplex/dirplex.c | 4 ++-- src/htparser.c | 2 +- src/patplex.c | 2 +- 7 files changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/cf.c b/lib/cf.c index 719fcad..432f78a 100644 --- 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); diff --git a/lib/cf.h b/lib/cf.h index 45e7698..a1eadb0 100644 --- 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 diff --git a/lib/proc.c b/lib/proc.c index 2e61e92..8646434 100644 --- a/lib/proc.c +++ b/lib/proc.c @@ -31,7 +31,7 @@ #include #include -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++) diff --git a/lib/proc.h b/lib/proc.h index da230b3..72bff20 100644 --- a/lib/proc.h +++ b/lib/proc.h @@ -3,9 +3,9 @@ #include -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 diff --git a/src/dirplex/dirplex.c b/src/dirplex/dirplex.c index f1f99e4..c31c6cd 100644 --- a/src/dirplex/dirplex.c +++ b/src/dirplex/dirplex.c @@ -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."); } } diff --git a/src/htparser.c b/src/htparser.c index c8064f2..503df99 100644 --- a/src/htparser.c +++ b/src/htparser.c @@ -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); } diff --git a/src/patplex.c b/src/patplex.c index d296e04..04b7a10 100644 --- a/src/patplex.c +++ b/src/patplex.c @@ -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."); } -- 2.11.0