+static struct chandler stdhandler = {
+ .handle = stdhandle,
+ .merge = stdmerge,
+ .destroy = stddestroy,
+};
+
+static int stdhandle(struct child *ch, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
+{
+ struct stdchild *i = ch->pdata;
+
+ if(i->type == CH_SOCKET) {
+ if(i->fd < 0)
+ i->fd = stdmkchild(i->argv, chinit, idata);
+ if(sendreq(i->fd, req, fd)) {
+ if((errno == EPIPE) || (errno == ECONNRESET)) {
+ /* Assume that the child has crashed and restart it. */
+ close(i->fd);
+ i->fd = stdmkchild(i->argv, chinit, idata);
+ if(!sendreq(i->fd, req, fd))
+ return(0);
+ }
+ flog(LOG_ERR, "could not pass on request to child %s: %s", ch->name, strerror(errno));
+ close(i->fd);
+ i->fd = -1;
+ return(-1);
+ }
+ } else if(i->type == CH_FORK) {
+ if(stdforkserve(i->argv, req, fd, chinit, idata) < 0)
+ return(-1);
+ }
+ return(0);
+}
+
+static void stdmerge(struct child *dst, struct child *src)
+{
+ struct stdchild *od, *nd;
+
+ if(src->iface == &stdhandler) {
+ nd = dst->pdata;
+ od = src->pdata;
+ nd->fd = od->fd;
+ od->fd = -1;
+ }
+}
+
+static void stddestroy(struct child *ch)
+{
+ struct stdchild *d = ch->pdata;
+
+ if(d->fd >= 0)
+ close(d->fd);
+ if(d->argv)
+ freeca(d->argv);
+ free(d);
+}
+