1 /*	$OpenBSD: imsg-buffer.c,v 1.4 2014/06/30 00:25:17 deraadt 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 
19 #include "config.h"
20 #include "compat/compat.h"
21 
22 #include <sys/types.h>
23 #include <sys/queue.h>
24 #include <sys/socket.h>
25 #include <sys/uio.h>
26 
27 #include <limits.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 
33 #include "imsg.h"
34 
35 int	ibuf_realloc(struct ibuf *, size_t);
36 void	ibuf_enqueue(struct msgbuf *, struct ibuf *);
37 void	ibuf_dequeue(struct msgbuf *, struct ibuf *);
38 
39 struct ibuf *
ibuf_open(size_t len)40 ibuf_open(size_t len)
41 {
42 	struct ibuf	*buf;
43 
44 	if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
45 		return (NULL);
46 	if ((buf->buf = malloc(len)) == NULL) {
47 		free(buf);
48 		return (NULL);
49 	}
50 	buf->size = buf->max = len;
51 	buf->fd = -1;
52 
53 	return (buf);
54 }
55 
56 struct ibuf *
ibuf_dynamic(size_t len,size_t max)57 ibuf_dynamic(size_t len, size_t max)
58 {
59 	struct ibuf	*buf;
60 
61 	if (max < len)
62 		return (NULL);
63 
64 	if ((buf = ibuf_open(len)) == NULL)
65 		return (NULL);
66 
67 	if (max > 0)
68 		buf->max = max;
69 
70 	return (buf);
71 }
72 
73 int
ibuf_realloc(struct ibuf * buf,size_t len)74 ibuf_realloc(struct ibuf *buf, size_t len)
75 {
76 	u_char	*b;
77 
78 	/* on static buffers max is eq size and so the following fails */
79 	if (buf->wpos + len > buf->max) {
80 		errno = ENOMEM;
81 		return (-1);
82 	}
83 
84 	b = realloc(buf->buf, buf->wpos + len);
85 	if (b == NULL)
86 		return (-1);
87 	buf->buf = b;
88 	buf->size = buf->wpos + len;
89 
90 	return (0);
91 }
92 
93 int
ibuf_add(struct ibuf * buf,const void * data,size_t len)94 ibuf_add(struct ibuf *buf, const void *data, size_t len)
95 {
96 	if (buf->wpos + len > buf->size)
97 		if (ibuf_realloc(buf, len) == -1)
98 			return (-1);
99 
100 	memcpy(buf->buf + buf->wpos, data, len);
101 	buf->wpos += len;
102 	return (0);
103 }
104 
105 void *
ibuf_reserve(struct ibuf * buf,size_t len)106 ibuf_reserve(struct ibuf *buf, size_t len)
107 {
108 	void	*b;
109 
110 	if (buf->wpos + len > buf->size)
111 		if (ibuf_realloc(buf, len) == -1)
112 			return (NULL);
113 
114 	b = buf->buf + buf->wpos;
115 	buf->wpos += len;
116 	return (b);
117 }
118 
119 void *
ibuf_seek(struct ibuf * buf,size_t pos,size_t len)120 ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
121 {
122 	/* only allowed to seek in already written parts */
123 	if (pos + len > buf->wpos)
124 		return (NULL);
125 
126 	return (buf->buf + pos);
127 }
128 
129 size_t
ibuf_size(struct ibuf * buf)130 ibuf_size(struct ibuf *buf)
131 {
132 	return (buf->wpos);
133 }
134 
135 size_t
ibuf_left(struct ibuf * buf)136 ibuf_left(struct ibuf *buf)
137 {
138 	return (buf->max - buf->wpos);
139 }
140 
141 void
ibuf_close(struct msgbuf * msgbuf,struct ibuf * buf)142 ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
143 {
144 	ibuf_enqueue(msgbuf, buf);
145 }
146 
147 int
ibuf_write(struct msgbuf * msgbuf)148 ibuf_write(struct msgbuf *msgbuf)
149 {
150 	struct iovec	 iov[IOV_MAX];
151 	struct ibuf	*buf;
152 	unsigned int	 i = 0;
153 	ssize_t	n;
154 
155 	bzero(&iov, sizeof(iov));
156 	TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
157 		if (i >= IOV_MAX)
158 			break;
159 		iov[i].iov_base = buf->buf + buf->rpos;
160 		iov[i].iov_len = buf->wpos - buf->rpos;
161 		i++;
162 	}
163 
164 again:
165 	if ((n = writev(msgbuf->fd, iov, i)) == -1) {
166 		if (errno == EINTR)
167 			goto again;
168 		if (errno == ENOBUFS)
169 			errno = EAGAIN;
170 		return (-1);
171 	}
172 
173 	if (n == 0) {			/* connection closed */
174 		errno = 0;
175 		return (0);
176 	}
177 
178 	msgbuf_drain(msgbuf, n);
179 
180 	return (1);
181 }
182 
183 void
ibuf_free(struct ibuf * buf)184 ibuf_free(struct ibuf *buf)
185 {
186 	free(buf->buf);
187 	free(buf);
188 }
189 
190 void
msgbuf_init(struct msgbuf * msgbuf)191 msgbuf_init(struct msgbuf *msgbuf)
192 {
193 	msgbuf->queued = 0;
194 	msgbuf->fd = -1;
195 	TAILQ_INIT(&msgbuf->bufs);
196 }
197 
198 void
msgbuf_drain(struct msgbuf * msgbuf,size_t n)199 msgbuf_drain(struct msgbuf *msgbuf, size_t n)
200 {
201 	struct ibuf	*buf, *next;
202 
203 	for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
204 	    buf = next) {
205 		next = TAILQ_NEXT(buf, entry);
206 		if (buf->rpos + n >= buf->wpos) {
207 			n -= buf->wpos - buf->rpos;
208 			ibuf_dequeue(msgbuf, buf);
209 		} else {
210 			buf->rpos += n;
211 			n = 0;
212 		}
213 	}
214 }
215 
216 void
msgbuf_clear(struct msgbuf * msgbuf)217 msgbuf_clear(struct msgbuf *msgbuf)
218 {
219 	struct ibuf	*buf;
220 
221 	while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
222 		ibuf_dequeue(msgbuf, buf);
223 }
224 
225 int
msgbuf_write(struct msgbuf * msgbuf)226 msgbuf_write(struct msgbuf *msgbuf)
227 {
228 	struct iovec	 iov[IOV_MAX];
229 	struct ibuf	*buf;
230 	unsigned int	 i = 0;
231 	ssize_t		 n;
232 	struct msghdr	 msg;
233 	struct cmsghdr	*cmsg;
234 	union {
235 		struct cmsghdr	hdr;
236 		char		buf[CMSG_SPACE(sizeof(int))];
237 	} cmsgbuf;
238 
239 	bzero(&iov, sizeof(iov));
240 	bzero(&msg, sizeof(msg));
241 	TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
242 		if (i >= IOV_MAX)
243 			break;
244 		iov[i].iov_base = buf->buf + buf->rpos;
245 		iov[i].iov_len = buf->wpos - buf->rpos;
246 		i++;
247 		if (buf->fd != -1)
248 			break;
249 	}
250 
251 	msg.msg_iov = iov;
252 	msg.msg_iovlen = i;
253 
254 	if (buf != NULL && buf->fd != -1) {
255 		msg.msg_control = (caddr_t)&cmsgbuf.buf;
256 		msg.msg_controllen = sizeof(cmsgbuf.buf);
257 		cmsg = CMSG_FIRSTHDR(&msg);
258 		cmsg->cmsg_len = CMSG_LEN(sizeof(int));
259 		cmsg->cmsg_level = SOL_SOCKET;
260 		cmsg->cmsg_type = SCM_RIGHTS;
261 		//*(int *)CMSG_DATA(cmsg) = buf->fd;
262 		memcpy(CMSG_DATA(cmsg), &buf->fd, sizeof(int));
263 	}
264 
265 again:
266 	if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
267 		if (errno == EINTR)
268 			goto again;
269 		if (errno == ENOBUFS)
270 			errno = EAGAIN;
271 		return (-1);
272 	}
273 
274 	if (n == 0) {			/* connection closed */
275 		errno = 0;
276 		return (0);
277 	}
278 
279 	/*
280 	 * assumption: fd got sent if sendmsg sent anything
281 	 * this works because fds are passed one at a time
282 	 */
283 	if (buf != NULL && buf->fd != -1) {
284 		close(buf->fd);
285 		buf->fd = -1;
286 	}
287 
288 	msgbuf_drain(msgbuf, n);
289 
290 	return (1);
291 }
292 
293 void
ibuf_enqueue(struct msgbuf * msgbuf,struct ibuf * buf)294 ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
295 {
296 	TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
297 	msgbuf->queued++;
298 }
299 
300 void
ibuf_dequeue(struct msgbuf * msgbuf,struct ibuf * buf)301 ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
302 {
303 	TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
304 
305 	if (buf->fd != -1)
306 		close(buf->fd);
307 
308 	msgbuf->queued--;
309 	ibuf_free(buf);
310 }
311