Added some process control functions to libht.
[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>
24
25#ifdef HAVE_CONFIG_H
26#include <config.h>
27#endif
28#include <utils.h>
29#include <log.h>
30#include <proc.h>
31
32int stdmkchild(char **argv)
33{
34 int i;
35 pid_t pid;
36 int fd[2];
37
38 if(socketpair(PF_UNIX, SOCK_DGRAM, 0, fd))
39 return(-1);
40 if((pid = fork()) < 0)
41 return(-1);
42 if(pid == 0) {
43 for(i = 3; i < FD_SETSIZE; i++) {
44 if(i != fd[0])
45 close(i);
46 }
47 dup2(fd[0], 0);
48 close(fd[0]);
49 execvp(argv[0], argv);
50 flog(LOG_WARNING, "could not exec child program %s: %s", argv[0], strerror(errno));
51 exit(127);
52 }
53 close(fd[0]);
54 return(fd[1]);
55}
56
57int sendfd(int sock, int fd, char *data, size_t datalen)
58{
59 struct msghdr msg;
60 struct cmsghdr *cmsg;
61 char cmbuf[CMSG_SPACE(sizeof(int))];
62 struct iovec bufvec;
63
64 memset(&msg, 0, sizeof(msg));
65 msg.msg_iov = &bufvec;
66 msg.msg_iovlen = 1;
67 bufvec.iov_base = data;
68 bufvec.iov_len = datalen;
69
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;
78
79 return(sendmsg(sock, &msg, MSG_NOSIGNAL | MSG_DONTWAIT));
80}
81
82int recvfd(int sock, char **data, size_t *datalen)
83{
84 int ret, fd;
85 char *buf, cbuf[1024];;
86 struct msghdr msg;
87 struct cmsghdr *cmsg;
88 struct iovec bufvec;
89
90 buf = smalloc(65536);
91 memset(&msg, 0, sizeof(msg));
92
93 msg.msg_iov = &bufvec;
94 msg.msg_iovlen = 1;
95 bufvec.iov_base = buf;
96 bufvec.iov_len = 65536;
97 msg.msg_control = cbuf;
98 msg.msg_controllen = sizeof(cbuf);
99
100 ret = recvmsg(sock, &msg, 0);
101 if(ret < 0) {
102 free(buf);
103 return(-1);
104 }
105
106 fd = -1;
107 for(cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
108 if((cmsg->cmsg_level == SOL_SOCKET) && (cmsg->cmsg_type == SCM_RIGHTS)) {
109 fd = *((int *)CMSG_DATA(cmsg));
110 }
111 }
112 if(fd < 0) {
113 free(buf);
114 errno = EPROTO;
115 return(-1);
116 }
117 buf = realloc(buf, ret);
118 *data = buf;
119 *datalen = ret;
120 return(fd);
121}