2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/socket.h>
35 int stdmkchild(char **argv, void (*chinit)(void *), void *idata)
40 if(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fd))
42 if((pid = fork()) < 0)
50 execvp(argv[0], argv);
51 flog(LOG_WARNING, "could not exec child program %s: %s", argv[0], strerror(errno));
55 fcntl(fd[1], F_SETFD, FD_CLOEXEC);
59 int sendfd2(int sock, int fd, char *data, size_t datalen, int flags)
63 char cmbuf[CMSG_SPACE(sizeof(int))];
66 memset(&msg, 0, sizeof(msg));
67 msg.msg_iov = &bufvec;
69 bufvec.iov_base = data;
70 bufvec.iov_len = datalen;
72 msg.msg_control = cmbuf;
73 msg.msg_controllen = sizeof(cmbuf);
74 cmsg = CMSG_FIRSTHDR(&msg);
75 cmsg->cmsg_level = SOL_SOCKET;
76 cmsg->cmsg_type = SCM_RIGHTS;
77 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
78 *((int *)CMSG_DATA(cmsg)) = fd;
79 msg.msg_controllen = cmsg->cmsg_len;
81 return(sendmsg(sock, &msg, flags));
84 int sendfd(int sock, int fd, char *data, size_t datalen)
86 return(sendfd2(sock, fd, data, datalen, MSG_NOSIGNAL));
89 int recvfd(int sock, char **data, size_t *datalen)
92 char *buf, cbuf[1024];
98 memset(&msg, 0, sizeof(msg));
100 msg.msg_iov = &bufvec;
102 bufvec.iov_base = buf;
103 bufvec.iov_len = 65536;
104 msg.msg_control = cbuf;
105 msg.msg_controllen = sizeof(cbuf);
107 ret = recvmsg(sock, &msg, 0);
116 for(cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
117 if((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SCM_RIGHTS)) {
118 fd = *((int *)CMSG_DATA(cmsg));
126 buf = realloc(buf, ret);
132 pid_t stdforkserve(char **argv, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
137 struct charvbuf args;
139 if((pid = fork()) < 0)
150 for(i = 0; argv[i]; i++)
151 bufadd(args, argv[i]);
152 bufadd(args, req->method);
153 bufadd(args, req->url);
154 bufadd(args, req->rest);
157 for(i = 0; i < req->noheaders; i++) {
158 ebuf = sstrdup(req->headers[i][0]);
159 for(p = ebuf; *p; p++) {
165 putenv(sprintf2("REQ_%s=%s", ebuf, req->headers[i][1]));
167 putenv(sprintf2("HTTP_VERSION=%s", req->ver));
169 execvp(args.b[0], args.b);
170 flog(LOG_WARNING, "could not exec child program %s: %s", argv[0], strerror(errno));