1 /* $OpenBSD: imsg-buffer.c,v 1.7 2015/07/12 18:40:49 nicm Exp $ */
4 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
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.
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.
21 #include <sys/types.h>
22 #include <sys/queue.h>
23 #include <sys/socket.h>
34 int ibuf_realloc(struct ibuf *, size_t);
35 void ibuf_enqueue(struct msgbuf *, struct ibuf *);
36 void ibuf_dequeue(struct msgbuf *, struct ibuf *);
43 if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
45 if ((buf->buf = malloc(len)) == NULL) {
49 buf->size = buf->max = len;
56 ibuf_dynamic(size_t len, size_t max)
63 if ((buf = ibuf_open(len)) == NULL)
73 ibuf_realloc(struct ibuf *buf, size_t len)
77 /* on static buffers max is eq size and so the following fails */
78 if (buf->wpos + len > buf->max) {
83 b = realloc(buf->buf, buf->wpos + len);
87 buf->size = buf->wpos + len;
93 ibuf_add(struct ibuf *buf, const void *data, size_t len)
95 if (buf->wpos + len > buf->size)
96 if (ibuf_realloc(buf, len) == -1)
99 memcpy(buf->buf + buf->wpos, data, len);
105 ibuf_reserve(struct ibuf *buf, size_t len)
109 if (buf->wpos + len > buf->size)
110 if (ibuf_realloc(buf, len) == -1)
113 b = buf->buf + buf->wpos;
119 ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
121 /* only allowed to seek in already written parts */
122 if (pos + len > buf->wpos)
125 return (buf->buf + pos);
129 ibuf_size(struct ibuf *buf)
135 ibuf_left(struct ibuf *buf)
137 return (buf->max - buf->wpos);
141 ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
143 ibuf_enqueue(msgbuf, buf);
147 ibuf_write(struct msgbuf *msgbuf)
149 struct iovec iov[IOV_MAX];
154 memset(&iov, 0, sizeof(iov));
155 TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
158 iov[i].iov_base = buf->buf + buf->rpos;
159 iov[i].iov_len = buf->wpos - buf->rpos;
164 if ((n = writev(msgbuf->fd, iov, i)) == -1) {
167 if (errno == ENOBUFS)
172 if (n == 0) { /* connection closed */
177 msgbuf_drain(msgbuf, n);
183 ibuf_free(struct ibuf *buf)
190 msgbuf_init(struct msgbuf *msgbuf)
194 TAILQ_INIT(&msgbuf->bufs);
198 msgbuf_drain(struct msgbuf *msgbuf, size_t n)
200 struct ibuf *buf, *next;
202 for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
204 next = TAILQ_NEXT(buf, entry);
205 if (buf->rpos + n >= buf->wpos) {
206 n -= buf->wpos - buf->rpos;
207 ibuf_dequeue(msgbuf, buf);
216 msgbuf_clear(struct msgbuf *msgbuf)
220 while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
221 ibuf_dequeue(msgbuf, buf);
225 msgbuf_write(struct msgbuf *msgbuf)
227 struct iovec iov[IOV_MAX];
232 struct cmsghdr *cmsg;
235 char buf[CMSG_SPACE(sizeof(int))];
238 memset(&iov, 0, sizeof(iov));
239 memset(&msg, 0, sizeof(msg));
240 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
241 TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
244 iov[i].iov_base = buf->buf + buf->rpos;
245 iov[i].iov_len = buf->wpos - buf->rpos;
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;
265 if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
268 if (errno == ENOBUFS)
273 if (n == 0) { /* connection closed */
279 * assumption: fd got sent if sendmsg sent anything
280 * this works because fds are passed one at a time
282 if (buf != NULL && buf->fd != -1) {
287 msgbuf_drain(msgbuf, n);
293 ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
295 TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
300 ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
302 TAILQ_REMOVE(&msgbuf->bufs, buf, entry);