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>
32 int stdmkchild(char **argv)
38 if(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fd))
40 if((pid = fork()) < 0)
43 for(i = 3; i < FD_SETSIZE; i++) {
49 execvp(argv[0], argv);
50 flog(LOG_WARNING, "could not exec child program %s: %s", argv[0], strerror(errno));
57 int sendfd(int sock, int fd, char *data, size_t datalen)
61 char cmbuf[CMSG_SPACE(sizeof(int))];
64 memset(&msg, 0, sizeof(msg));
65 msg.msg_iov = &bufvec;
67 bufvec.iov_base = data;
68 bufvec.iov_len = datalen;
70 msg.msg_control = cmbuf;
71 msg.msg_controllen = sizeof(cmbuf);
72 cmsg = CMSG_FIRSTHDR(&msg);
73 cmsg->cmsg_level = SOL_SOCKET;
74 cmsg->cmsg_type = SCM_RIGHTS;
75 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
76 *((int *)CMSG_DATA(cmsg)) = fd;
77 msg.msg_controllen = cmsg->cmsg_len;
79 return(sendmsg(sock, &msg, MSG_NOSIGNAL | MSG_DONTWAIT));
82 int recvfd(int sock, char **data, size_t *datalen)
85 char *buf, cbuf[1024];;
91 memset(&msg, 0, sizeof(msg));
93 msg.msg_iov = &bufvec;
95 bufvec.iov_base = buf;
96 bufvec.iov_len = 65536;
97 msg.msg_control = cbuf;
98 msg.msg_controllen = sizeof(cbuf);
100 ret = recvmsg(sock, &msg, 0);
109 for(cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
110 if((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SCM_RIGHTS)) {
111 fd = *((int *)CMSG_DATA(cmsg));
119 buf = realloc(buf, ret);