Xr gotweb(8) from got(1)
[got-portable.git] / compat / imsg-buffer.c
blobce3ca9baf57a8856dcbf04e41c7a3ff1e07dda10
1 /* $OpenBSD: imsg-buffer.c,v 1.11 2017/12/14 09:27:44 kettenis Exp $ */
3 /*
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.
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <sys/uio.h>
23 #include <limits.h>
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "got_compat.h"
31 static int ibuf_realloc(struct ibuf *, size_t);
32 static void ibuf_enqueue(struct msgbuf *, struct ibuf *);
33 static void ibuf_dequeue(struct msgbuf *, struct ibuf *);
35 struct ibuf *
36 ibuf_open(size_t len)
38 struct ibuf *buf;
40 if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
41 return (NULL);
42 if ((buf->buf = malloc(len)) == NULL) {
43 free(buf);
44 return (NULL);
46 buf->size = buf->max = len;
47 buf->fd = -1;
49 return (buf);
52 struct ibuf *
53 ibuf_dynamic(size_t len, size_t max)
55 struct ibuf *buf;
57 if (max < len)
58 return (NULL);
60 if ((buf = ibuf_open(len)) == NULL)
61 return (NULL);
63 if (max > 0)
64 buf->max = max;
66 return (buf);
69 static int
70 ibuf_realloc(struct ibuf *buf, size_t len)
72 u_char *b;
74 /* on static buffers max is eq size and so the following fails */
75 if (buf->wpos + len > buf->max) {
76 errno = ERANGE;
77 return (-1);
80 b = recallocarray(buf->buf, buf->size, buf->wpos + len, 1);
81 if (b == NULL)
82 return (-1);
83 buf->buf = b;
84 buf->size = buf->wpos + len;
86 return (0);
89 int
90 ibuf_add(struct ibuf *buf, const void *data, size_t len)
92 if (buf->wpos + len > buf->size)
93 if (ibuf_realloc(buf, len) == -1)
94 return (-1);
96 memcpy(buf->buf + buf->wpos, data, len);
97 buf->wpos += len;
98 return (0);
101 void *
102 ibuf_reserve(struct ibuf *buf, size_t len)
104 void *b;
106 if (buf->wpos + len > buf->size)
107 if (ibuf_realloc(buf, len) == -1)
108 return (NULL);
110 b = buf->buf + buf->wpos;
111 buf->wpos += len;
112 return (b);
115 void *
116 ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
118 /* only allowed to seek in already written parts */
119 if (pos + len > buf->wpos)
120 return (NULL);
122 return (buf->buf + pos);
125 size_t
126 ibuf_size(struct ibuf *buf)
128 return (buf->wpos);
131 size_t
132 ibuf_left(struct ibuf *buf)
134 return (buf->max - buf->wpos);
137 void
138 ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
140 ibuf_enqueue(msgbuf, buf);
144 ibuf_write(struct msgbuf *msgbuf)
146 struct iovec iov[IOV_MAX];
147 struct ibuf *buf;
148 unsigned int i = 0;
149 ssize_t n;
151 memset(&iov, 0, sizeof(iov));
152 TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
153 if (i >= IOV_MAX)
154 break;
155 iov[i].iov_base = buf->buf + buf->rpos;
156 iov[i].iov_len = buf->wpos - buf->rpos;
157 i++;
160 again:
161 if ((n = writev(msgbuf->fd, iov, i)) == -1) {
162 if (errno == EINTR)
163 goto again;
164 if (errno == ENOBUFS)
165 errno = EAGAIN;
166 return (-1);
169 if (n == 0) { /* connection closed */
170 errno = 0;
171 return (0);
174 msgbuf_drain(msgbuf, n);
176 return (1);
179 void
180 ibuf_free(struct ibuf *buf)
182 if (buf == NULL)
183 return;
184 freezero(buf->buf, buf->size);
185 free(buf);
188 void
189 msgbuf_init(struct msgbuf *msgbuf)
191 msgbuf->queued = 0;
192 msgbuf->fd = -1;
193 TAILQ_INIT(&msgbuf->bufs);
196 void
197 msgbuf_drain(struct msgbuf *msgbuf, size_t n)
199 struct ibuf *buf, *next;
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;
214 void
215 msgbuf_clear(struct msgbuf *msgbuf)
217 struct ibuf *buf;
219 while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
220 ibuf_dequeue(msgbuf, buf);
224 msgbuf_write(struct msgbuf *msgbuf)
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;
237 memset(&iov, 0, sizeof(iov));
238 memset(&msg, 0, sizeof(msg));
239 memset(&cmsgbuf, 0, sizeof(cmsgbuf));
240 TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
241 if (i >= IOV_MAX)
242 break;
243 iov[i].iov_base = buf->buf + buf->rpos;
244 iov[i].iov_len = buf->wpos - buf->rpos;
245 i++;
246 if (buf->fd != -1)
247 break;
250 msg.msg_iov = iov;
251 msg.msg_iovlen = i;
253 if (buf != NULL && buf->fd != -1) {
254 msg.msg_control = (caddr_t)&cmsgbuf.buf;
255 msg.msg_controllen = sizeof(cmsgbuf.buf);
256 cmsg = CMSG_FIRSTHDR(&msg);
257 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
258 cmsg->cmsg_level = SOL_SOCKET;
259 cmsg->cmsg_type = SCM_RIGHTS;
260 *(int *)CMSG_DATA(cmsg) = buf->fd;
263 again:
264 if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
265 if (errno == EINTR)
266 goto again;
267 if (errno == ENOBUFS)
268 errno = EAGAIN;
269 return (-1);
272 if (n == 0) { /* connection closed */
273 errno = 0;
274 return (0);
278 * assumption: fd got sent if sendmsg sent anything
279 * this works because fds are passed one at a time
281 if (buf != NULL && buf->fd != -1) {
282 close(buf->fd);
283 buf->fd = -1;
286 msgbuf_drain(msgbuf, n);
288 return (1);
291 static void
292 ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
294 TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
295 msgbuf->queued++;
298 static void
299 ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
301 TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
303 if (buf->fd != -1)
304 close(buf->fd);
306 msgbuf->queued--;
307 ibuf_free(buf);