X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=lib%2Fproc.c;h=2c05608eb098eb313dcc085630d2cdd1d7a675dd;hb=d5ee5cdea79ab756f4a7908c19b8232d57a1a3a8;hp=957cfce7becb6d100b42bc807df759db10fda925;hpb=992ce9ef8adb25fdcc47264187450a3edb287167;p=ashd.git diff --git a/lib/proc.c b/lib/proc.c index 957cfce..2c05608 100644 --- a/lib/proc.c +++ b/lib/proc.c @@ -22,6 +22,7 @@ #include #include #include +#include #ifdef HAVE_CONFIG_H #include @@ -31,9 +32,8 @@ #include #include -int stdmkchild(char **argv) +int stdmkchild(char **argv, void (*chinit)(void *), void *idata) { - int i; pid_t pid; int fd[2]; @@ -42,17 +42,17 @@ int stdmkchild(char **argv) if((pid = fork()) < 0) return(-1); if(pid == 0) { - for(i = 3; i < FD_SETSIZE; i++) { - if(i != fd[0]) - close(i); - } + if(chinit != NULL) + chinit(idata); dup2(fd[0], 0); close(fd[0]); + close(fd[1]); execvp(argv[0], argv); flog(LOG_WARNING, "could not exec child program %s: %s", argv[0], strerror(errno)); exit(127); } close(fd[0]); + fcntl(fd[1], F_SETFD, FD_CLOEXEC); return(fd[1]); } @@ -84,7 +84,7 @@ int sendfd(int sock, int fd, char *data, size_t datalen) int recvfd(int sock, char **data, size_t *datalen) { int ret, fd; - char *buf, cbuf[1024];; + char *buf, cbuf[1024]; struct msghdr msg; struct cmsghdr *cmsg; struct iovec bufvec; @@ -124,7 +124,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,10 +134,12 @@ 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++) - close(i); + close(fd); bufinit(args); for(i = 0; argv[i]; i++) @@ -163,6 +165,5 @@ pid_t stdforkserve(char **argv, struct hthead *req, int fd) flog(LOG_WARNING, "could not exec child program %s: %s", argv[0], strerror(errno)); exit(127); } - close(fd); return(pid); }