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>
34 int stdmkchild(char **argv, void (*chinit)(void *), void *idata)
40 if(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fd))
42 if((pid = fork()) < 0)
47 for(i = 3; i < FD_SETSIZE; i++) {
53 execvp(argv[0], argv);
54 flog(LOG_WARNING, "could not exec child program %s: %s", argv[0], strerror(errno));
61 int sendfd(int sock, int fd, char *data, size_t datalen)
65 char cmbuf[CMSG_SPACE(sizeof(int))];
68 memset(&msg, 0, sizeof(msg));
69 msg.msg_iov = &bufvec;
71 bufvec.iov_base = data;
72 bufvec.iov_len = datalen;
74 msg.msg_control = cmbuf;
75 msg.msg_controllen = sizeof(cmbuf);
76 cmsg = CMSG_FIRSTHDR(&msg);
77 cmsg->cmsg_level = SOL_SOCKET;
78 cmsg->cmsg_type = SCM_RIGHTS;
79 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
80 *((int *)CMSG_DATA(cmsg)) = fd;
81 msg.msg_controllen = cmsg->cmsg_len;
83 return(sendmsg(sock, &msg, MSG_NOSIGNAL | MSG_DONTWAIT));
86 int recvfd(int sock, char **data, size_t *datalen)
89 char *buf, cbuf[1024];
95 memset(&msg, 0, sizeof(msg));
97 msg.msg_iov = &bufvec;
99 bufvec.iov_base = buf;
100 bufvec.iov_len = 65536;
101 msg.msg_control = cbuf;
102 msg.msg_controllen = sizeof(cbuf);
104 ret = recvmsg(sock, &msg, 0);
113 for(cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
114 if((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SCM_RIGHTS)) {
115 fd = *((int *)CMSG_DATA(cmsg));
123 buf = realloc(buf, ret);
129 pid_t stdforkserve(char **argv, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
134 struct charvbuf args;
136 if((pid = fork()) < 0)
144 for(i = 3; i < FD_SETSIZE; i++)
148 for(i = 0; argv[i]; i++)
149 bufadd(args, argv[i]);
150 bufadd(args, req->method);
151 bufadd(args, req->url);
152 bufadd(args, req->rest);
155 for(i = 0; i < req->noheaders; i++) {
156 ebuf = sstrdup(req->headers[i][0]);
157 for(p = ebuf; *p; p++) {
163 putenv(sprintf2("REQ_%s=%s", ebuf, req->headers[i][1]));
165 putenv(sprintf2("HTTP_VERSION=%s", req->ver));
167 execvp(args.b[0], args.b);
168 flog(LOG_WARNING, "could not exec child program %s: %s", argv[0], strerror(errno));