Merge branch 'master' into timeheap
[ashd.git] / lib / proc.c
CommitLineData
0c16b406
FT
1/*
2 ashd - A Sane HTTP Daemon
3 Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com>
4
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.
9
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.
14
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/>.
17*/
18
19#include <stdlib.h>
20#include <string.h>
21#include <unistd.h>
22#include <sys/socket.h>
23#include <errno.h>
992ce9ef 24#include <ctype.h>
470938bd 25#include <fcntl.h>
0c16b406
FT
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30#include <utils.h>
31#include <log.h>
32#include <proc.h>
992ce9ef 33#include <req.h>
0c16b406 34
6a7a868e 35int stdmkchild(char **argv, void (*chinit)(void *), void *idata)
0c16b406 36{
0c16b406
FT
37 pid_t pid;
38 int fd[2];
39
820f9137 40 if(socketpair(PF_UNIX, SOCK_SEQPACKET, 0, fd))
0c16b406
FT
41 return(-1);
42 if((pid = fork()) < 0)
43 return(-1);
44 if(pid == 0) {
6a7a868e
FT
45 if(chinit != NULL)
46 chinit(idata);
0c16b406
FT
47 dup2(fd[0], 0);
48 close(fd[0]);
470938bd 49 close(fd[1]);
0c16b406
FT
50 execvp(argv[0], argv);
51 flog(LOG_WARNING, "could not exec child program %s: %s", argv[0], strerror(errno));
52 exit(127);
53 }
54 close(fd[0]);
470938bd 55 fcntl(fd[1], F_SETFD, FD_CLOEXEC);
0c16b406
FT
56 return(fd[1]);
57}
58
d9f67fea 59int sendfd2(int sock, int fd, char *data, size_t datalen, int flags)
0c16b406
FT
60{
61 struct msghdr msg;
62 struct cmsghdr *cmsg;
63 char cmbuf[CMSG_SPACE(sizeof(int))];
64 struct iovec bufvec;
65
66 memset(&msg, 0, sizeof(msg));
67 msg.msg_iov = &bufvec;
68 msg.msg_iovlen = 1;
69 bufvec.iov_base = data;
70 bufvec.iov_len = datalen;
71
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;
80
d9f67fea
FT
81 return(sendmsg(sock, &msg, flags));
82}
83
84int sendfd(int sock, int fd, char *data, size_t datalen)
85{
096c79fd 86 return(sendfd2(sock, fd, data, datalen, MSG_NOSIGNAL));
0c16b406
FT
87}
88
89int recvfd(int sock, char **data, size_t *datalen)
90{
91 int ret, fd;
3a42b6b1 92 char *buf, cbuf[1024];
0c16b406
FT
93 struct msghdr msg;
94 struct cmsghdr *cmsg;
95 struct iovec bufvec;
96
97 buf = smalloc(65536);
98 memset(&msg, 0, sizeof(msg));
99
100 msg.msg_iov = &bufvec;
101 msg.msg_iovlen = 1;
102 bufvec.iov_base = buf;
103 bufvec.iov_len = 65536;
104 msg.msg_control = cbuf;
105 msg.msg_controllen = sizeof(cbuf);
106
107 ret = recvmsg(sock, &msg, 0);
820f9137 108 if(ret <= 0) {
0c16b406 109 free(buf);
820f9137
FT
110 if(ret == 0)
111 errno = 0;
0c16b406
FT
112 return(-1);
113 }
114
115 fd = -1;
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));
119 }
120 }
121 if(fd < 0) {
122 free(buf);
123 errno = EPROTO;
124 return(-1);
125 }
126 buf = realloc(buf, ret);
127 *data = buf;
128 *datalen = ret;
129 return(fd);
130}
992ce9ef 131
6a7a868e 132pid_t stdforkserve(char **argv, struct hthead *req, int fd, void (*chinit)(void *), void *idata)
992ce9ef
FT
133{
134 int i;
135 char *ebuf, *p;
136 pid_t pid;
137 struct charvbuf args;
138
139 if((pid = fork()) < 0)
140 return(-1);
141 if(pid == 0) {
6a7a868e
FT
142 if(chinit != NULL)
143 chinit(idata);
144
992ce9ef
FT
145 dup2(fd, 0);
146 dup2(fd, 1);
470938bd 147 close(fd);
992ce9ef
FT
148
149 bufinit(args);
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);
155 bufadd(args, NULL);
156
157 for(i = 0; i < req->noheaders; i++) {
158 ebuf = sstrdup(req->headers[i][0]);
159 for(p = ebuf; *p; p++) {
160 if(isalnum(*p))
161 *p = toupper(*p);
162 else
163 *p = '_';
164 }
165 putenv(sprintf2("REQ_%s=%s", ebuf, req->headers[i][1]));
166 }
167 putenv(sprintf2("HTTP_VERSION=%s", req->ver));
168
169 execvp(args.b[0], args.b);
170 flog(LOG_WARNING, "could not exec child program %s: %s", argv[0], strerror(errno));
171 exit(127);
172 }
992ce9ef
FT
173 return(pid);
174}