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 sendfd(int sock, int fd, char *data, size_t datalen)
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, MSG_NOSIGNAL | MSG_DONTWAIT));
84 int recvfd(int sock, char **data, size_t *datalen)
87 char *buf, cbuf[1024];
93 memset(&msg, 0, sizeof(msg));
95 msg.msg_iov = &bufvec;
97 bufvec.iov_base = buf;
98 bufvec.iov_len = 65536;
99 msg.msg_control = cbuf;
100 msg.msg_controllen = sizeof(cbuf);
102 ret = recvmsg(sock, &msg, 0);
111 for(cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
112 if((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SCM_RIGHTS)) {
113 fd = *((int *)CMSG_DATA(cmsg));
121 buf = realloc(buf, ret);
127 pid_t stdforkserve(char **argv, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
132 struct charvbuf args;
134 if((pid = fork()) < 0)
145 for(i = 0; argv[i]; i++)
146 bufadd(args, argv[i]);
147 bufadd(args, req->method);
148 bufadd(args, req->url);
149 bufadd(args, req->rest);
152 for(i = 0; i < req->noheaders; i++) {
153 ebuf = sstrdup(req->headers[i][0]);
154 for(p = ebuf; *p; p++) {
160 putenv(sprintf2("REQ_%s=%s", ebuf, req->headers[i][1]));
162 putenv(sprintf2("HTTP_VERSION=%s", req->ver));
164 execvp(args.b[0], args.b);
165 flog(LOG_WARNING, "could not exec child program %s: %s", argv[0], strerror(errno));