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