1 /*	$OpenBSD: imsg.c,v 1.13 2015/12/09 11:54:12 tb Exp $	*/
2 
3 /*
4  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  * $FreeBSD: head/lib/libopenbsd/imsg.c 292023 2015-12-09 19:22:20Z rodrigc $
19  */
20 
21 #include <sys/types.h>
22 #include <sys/queue.h>
23 #include <sys/socket.h>
24 #include <sys/uio.h>
25 
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 
31 #include "imsg.h"
32 
33 int	 imsg_fd_overhead = 0;
34 
35 int	 imsg_get_fd(struct imsgbuf *);
36 
37 void
imsg_init(struct imsgbuf * ibuf,int fd)38 imsg_init(struct imsgbuf *ibuf, int fd)
39 {
40 	msgbuf_init(&ibuf->w);
41 	memset(&ibuf->r, 0, sizeof(ibuf->r));
42 	ibuf->fd = fd;
43 	ibuf->w.fd = fd;
44 	ibuf->pid = getpid();
45 	TAILQ_INIT(&ibuf->fds);
46 }
47 
48 ssize_t
imsg_read(struct imsgbuf * ibuf)49 imsg_read(struct imsgbuf *ibuf)
50 {
51 	struct msghdr		 msg;
52 	struct cmsghdr		*cmsg;
53 	union {
54 		struct cmsghdr hdr;
55 		char	buf[CMSG_SPACE(sizeof(int) * 1)];
56 	} cmsgbuf;
57 	struct iovec		 iov;
58 	ssize_t			 n = -1;
59 	int			 fd;
60 	struct imsg_fd		*ifd;
61 
62 	memset(&msg, 0, sizeof(msg));
63 	memset(&cmsgbuf, 0, sizeof(cmsgbuf));
64 
65 	iov.iov_base = ibuf->r.buf + ibuf->r.wpos;
66 	iov.iov_len = sizeof(ibuf->r.buf) - ibuf->r.wpos;
67 	msg.msg_iov = &iov;
68 	msg.msg_iovlen = 1;
69 	msg.msg_control = &cmsgbuf.buf;
70 	msg.msg_controllen = sizeof(cmsgbuf.buf);
71 
72 	if ((ifd = calloc(1, sizeof(struct imsg_fd))) == NULL)
73 		return (-1);
74 
75 again:
76 	if (getdtablecount() + imsg_fd_overhead +
77 	    (int)((CMSG_SPACE(sizeof(int))-CMSG_SPACE(0))/sizeof(int))
78 	    >= getdtablesize()) {
79 		errno = EAGAIN;
80 		free(ifd);
81 		return (-1);
82 	}
83 
84 	if ((n = recvmsg(ibuf->fd, &msg, 0)) == -1) {
85 		if (errno == EINTR)
86 			goto again;
87 		goto fail;
88 	}
89 
90 	ibuf->r.wpos += n;
91 
92 	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
93 	    cmsg = CMSG_NXTHDR(&msg, cmsg)) {
94 		if (cmsg->cmsg_level == SOL_SOCKET &&
95 		    cmsg->cmsg_type == SCM_RIGHTS) {
96 			int i;
97 			int j;
98 
99 			/*
100 			 * We only accept one file descriptor.  Due to C
101 			 * padding rules, our control buffer might contain
102 			 * more than one fd, and we must close them.
103 			 */
104 			j = ((char *)cmsg + cmsg->cmsg_len -
105 			    (char *)CMSG_DATA(cmsg)) / sizeof(int);
106 			for (i = 0; i < j; i++) {
107 				fd = ((int *)CMSG_DATA(cmsg))[i];
108 				if (ifd != NULL) {
109 					ifd->fd = fd;
110 					TAILQ_INSERT_TAIL(&ibuf->fds, ifd,
111 					    entry);
112 					ifd = NULL;
113 				} else
114 					close(fd);
115 			}
116 		}
117 		/* we do not handle other ctl data level */
118 	}
119 
120 fail:
121 	free(ifd);
122 	return (n);
123 }
124 
125 ssize_t
imsg_get(struct imsgbuf * ibuf,struct imsg * imsg)126 imsg_get(struct imsgbuf *ibuf, struct imsg *imsg)
127 {
128 	size_t			 av, left, datalen;
129 
130 	av = ibuf->r.wpos;
131 
132 	if (IMSG_HEADER_SIZE > av)
133 		return (0);
134 
135 	memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr));
136 	if (imsg->hdr.len < IMSG_HEADER_SIZE ||
137 	    imsg->hdr.len > MAX_IMSGSIZE) {
138 		errno = ERANGE;
139 		return (-1);
140 	}
141 	if (imsg->hdr.len > av)
142 		return (0);
143 	datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
144 	ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE;
145 	if (datalen == 0)
146 		imsg->data = NULL;
147 	else if ((imsg->data = malloc(datalen)) == NULL)
148 		return (-1);
149 
150 	if (imsg->hdr.flags & IMSGF_HASFD)
151 		imsg->fd = imsg_get_fd(ibuf);
152 	else
153 		imsg->fd = -1;
154 
155 	memcpy(imsg->data, ibuf->r.rptr, datalen);
156 
157 	if (imsg->hdr.len < av) {
158 		left = av - imsg->hdr.len;
159 		memmove(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left);
160 		ibuf->r.wpos = left;
161 	} else
162 		ibuf->r.wpos = 0;
163 
164 	return (datalen + IMSG_HEADER_SIZE);
165 }
166 
167 int
imsg_compose(struct imsgbuf * ibuf,u_int32_t type,u_int32_t peerid,pid_t pid,int fd,const void * data,u_int16_t datalen)168 imsg_compose(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
169     pid_t pid, int fd, const void *data, u_int16_t datalen)
170 {
171 	struct ibuf	*wbuf;
172 
173 	if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
174 		return (-1);
175 
176 	if (imsg_add(wbuf, data, datalen) == -1)
177 		return (-1);
178 
179 	wbuf->fd = fd;
180 
181 	imsg_close(ibuf, wbuf);
182 
183 	return (1);
184 }
185 
186 int
imsg_composev(struct imsgbuf * ibuf,u_int32_t type,u_int32_t peerid,pid_t pid,int fd,const struct iovec * iov,int iovcnt)187 imsg_composev(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
188     pid_t pid, int fd, const struct iovec *iov, int iovcnt)
189 {
190 	struct ibuf	*wbuf;
191 	int		 i, datalen = 0;
192 
193 	for (i = 0; i < iovcnt; i++)
194 		datalen += iov[i].iov_len;
195 
196 	if ((wbuf = imsg_create(ibuf, type, peerid, pid, datalen)) == NULL)
197 		return (-1);
198 
199 	for (i = 0; i < iovcnt; i++)
200 		if (imsg_add(wbuf, iov[i].iov_base, iov[i].iov_len) == -1)
201 			return (-1);
202 
203 	wbuf->fd = fd;
204 
205 	imsg_close(ibuf, wbuf);
206 
207 	return (1);
208 }
209 
210 /* ARGSUSED */
211 struct ibuf *
imsg_create(struct imsgbuf * ibuf,u_int32_t type,u_int32_t peerid,pid_t pid,u_int16_t datalen)212 imsg_create(struct imsgbuf *ibuf, u_int32_t type, u_int32_t peerid,
213     pid_t pid, u_int16_t datalen)
214 {
215 	struct ibuf	*wbuf;
216 	struct imsg_hdr	 hdr;
217 
218 	datalen += IMSG_HEADER_SIZE;
219 	if (datalen > MAX_IMSGSIZE) {
220 		errno = ERANGE;
221 		return (NULL);
222 	}
223 
224 	hdr.type = type;
225 	hdr.flags = 0;
226 	hdr.peerid = peerid;
227 	if ((hdr.pid = pid) == 0)
228 		hdr.pid = ibuf->pid;
229 	if ((wbuf = ibuf_dynamic(datalen, MAX_IMSGSIZE)) == NULL) {
230 		return (NULL);
231 	}
232 	if (imsg_add(wbuf, &hdr, sizeof(hdr)) == -1)
233 		return (NULL);
234 
235 	return (wbuf);
236 }
237 
238 int
imsg_add(struct ibuf * msg,const void * data,u_int16_t datalen)239 imsg_add(struct ibuf *msg, const void *data, u_int16_t datalen)
240 {
241 	if (datalen)
242 		if (ibuf_add(msg, data, datalen) == -1) {
243 			ibuf_free(msg);
244 			return (-1);
245 		}
246 	return (datalen);
247 }
248 
249 void
imsg_close(struct imsgbuf * ibuf,struct ibuf * msg)250 imsg_close(struct imsgbuf *ibuf, struct ibuf *msg)
251 {
252 	struct imsg_hdr	*hdr;
253 
254 	hdr = (struct imsg_hdr *)msg->buf;
255 
256 	hdr->flags &= ~IMSGF_HASFD;
257 	if (msg->fd != -1)
258 		hdr->flags |= IMSGF_HASFD;
259 
260 	hdr->len = (u_int16_t)msg->wpos;
261 
262 	ibuf_close(&ibuf->w, msg);
263 }
264 
265 void
imsg_free(struct imsg * imsg)266 imsg_free(struct imsg *imsg)
267 {
268 	free(imsg->data);
269 }
270 
271 int
imsg_get_fd(struct imsgbuf * ibuf)272 imsg_get_fd(struct imsgbuf *ibuf)
273 {
274 	int		 fd;
275 	struct imsg_fd	*ifd;
276 
277 	if ((ifd = TAILQ_FIRST(&ibuf->fds)) == NULL)
278 		return (-1);
279 
280 	fd = ifd->fd;
281 	TAILQ_REMOVE(&ibuf->fds, ifd, entry);
282 	free(ifd);
283 
284 	return (fd);
285 }
286 
287 int
imsg_flush(struct imsgbuf * ibuf)288 imsg_flush(struct imsgbuf *ibuf)
289 {
290 	while (ibuf->w.queued)
291 		if (msgbuf_write(&ibuf->w) <= 0)
292 			return (-1);
293 	return (0);
294 }
295 
296 void
imsg_clear(struct imsgbuf * ibuf)297 imsg_clear(struct imsgbuf *ibuf)
298 {
299 	int	fd;
300 
301 	msgbuf_clear(&ibuf->w);
302 	while ((fd = imsg_get_fd(ibuf)) != -1)
303 		close(fd);
304 }
305