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);
}
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);
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
#include <proc.h>
#include <req.h>
-int stdmkchild(char **argv)
+int stdmkchild(char **argv, void (*chinit)(void *), void *idata)
{
int i;
pid_t pid;
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);
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;
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++)
#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
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.");
}
}
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);
}
return;
}
- if(childhandle(ch, req, fd))
+ if(childhandle(ch, req, fd, NULL, NULL))
simpleerror(fd, 500, "Server Error", "The request handler crashed.");
}