Blame


1 dd038bc6 2021-09-21 thomas.ad /* $OpenBSD: imsg-buffer.c,v 1.11 2017/12/14 09:27:44 kettenis Exp $ */
2 dd038bc6 2021-09-21 thomas.ad
3 dd038bc6 2021-09-21 thomas.ad /*
4 dd038bc6 2021-09-21 thomas.ad * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5 dd038bc6 2021-09-21 thomas.ad *
6 dd038bc6 2021-09-21 thomas.ad * Permission to use, copy, modify, and distribute this software for any
7 dd038bc6 2021-09-21 thomas.ad * purpose with or without fee is hereby granted, provided that the above
8 dd038bc6 2021-09-21 thomas.ad * copyright notice and this permission notice appear in all copies.
9 dd038bc6 2021-09-21 thomas.ad *
10 dd038bc6 2021-09-21 thomas.ad * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 dd038bc6 2021-09-21 thomas.ad * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 dd038bc6 2021-09-21 thomas.ad * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 dd038bc6 2021-09-21 thomas.ad * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 dd038bc6 2021-09-21 thomas.ad * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 dd038bc6 2021-09-21 thomas.ad * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 dd038bc6 2021-09-21 thomas.ad * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 dd038bc6 2021-09-21 thomas.ad */
18 dd038bc6 2021-09-21 thomas.ad
19 dd038bc6 2021-09-21 thomas.ad #include <sys/types.h>
20 dd038bc6 2021-09-21 thomas.ad #include <sys/socket.h>
21 dd038bc6 2021-09-21 thomas.ad #include <sys/uio.h>
22 dd038bc6 2021-09-21 thomas.ad
23 dd038bc6 2021-09-21 thomas.ad #include <limits.h>
24 dd038bc6 2021-09-21 thomas.ad #include <errno.h>
25 dd038bc6 2021-09-21 thomas.ad #include <stdlib.h>
26 dd038bc6 2021-09-21 thomas.ad #include <string.h>
27 dd038bc6 2021-09-21 thomas.ad #include <unistd.h>
28 dd038bc6 2021-09-21 thomas.ad
29 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
30 dd038bc6 2021-09-21 thomas.ad
31 dd038bc6 2021-09-21 thomas.ad static int ibuf_realloc(struct ibuf *, size_t);
32 dd038bc6 2021-09-21 thomas.ad static void ibuf_enqueue(struct msgbuf *, struct ibuf *);
33 dd038bc6 2021-09-21 thomas.ad static void ibuf_dequeue(struct msgbuf *, struct ibuf *);
34 dd038bc6 2021-09-21 thomas.ad
35 dd038bc6 2021-09-21 thomas.ad struct ibuf *
36 dd038bc6 2021-09-21 thomas.ad ibuf_open(size_t len)
37 dd038bc6 2021-09-21 thomas.ad {
38 dd038bc6 2021-09-21 thomas.ad struct ibuf *buf;
39 dd038bc6 2021-09-21 thomas.ad
40 dd038bc6 2021-09-21 thomas.ad if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
41 dd038bc6 2021-09-21 thomas.ad return (NULL);
42 dd038bc6 2021-09-21 thomas.ad if ((buf->buf = malloc(len)) == NULL) {
43 dd038bc6 2021-09-21 thomas.ad free(buf);
44 dd038bc6 2021-09-21 thomas.ad return (NULL);
45 dd038bc6 2021-09-21 thomas.ad }
46 dd038bc6 2021-09-21 thomas.ad buf->size = buf->max = len;
47 dd038bc6 2021-09-21 thomas.ad buf->fd = -1;
48 dd038bc6 2021-09-21 thomas.ad
49 dd038bc6 2021-09-21 thomas.ad return (buf);
50 dd038bc6 2021-09-21 thomas.ad }
51 dd038bc6 2021-09-21 thomas.ad
52 dd038bc6 2021-09-21 thomas.ad struct ibuf *
53 dd038bc6 2021-09-21 thomas.ad ibuf_dynamic(size_t len, size_t max)
54 dd038bc6 2021-09-21 thomas.ad {
55 dd038bc6 2021-09-21 thomas.ad struct ibuf *buf;
56 dd038bc6 2021-09-21 thomas.ad
57 dd038bc6 2021-09-21 thomas.ad if (max < len)
58 dd038bc6 2021-09-21 thomas.ad return (NULL);
59 dd038bc6 2021-09-21 thomas.ad
60 dd038bc6 2021-09-21 thomas.ad if ((buf = ibuf_open(len)) == NULL)
61 dd038bc6 2021-09-21 thomas.ad return (NULL);
62 dd038bc6 2021-09-21 thomas.ad
63 dd038bc6 2021-09-21 thomas.ad if (max > 0)
64 dd038bc6 2021-09-21 thomas.ad buf->max = max;
65 dd038bc6 2021-09-21 thomas.ad
66 dd038bc6 2021-09-21 thomas.ad return (buf);
67 dd038bc6 2021-09-21 thomas.ad }
68 dd038bc6 2021-09-21 thomas.ad
69 dd038bc6 2021-09-21 thomas.ad static int
70 dd038bc6 2021-09-21 thomas.ad ibuf_realloc(struct ibuf *buf, size_t len)
71 dd038bc6 2021-09-21 thomas.ad {
72 dd038bc6 2021-09-21 thomas.ad u_char *b;
73 dd038bc6 2021-09-21 thomas.ad
74 dd038bc6 2021-09-21 thomas.ad /* on static buffers max is eq size and so the following fails */
75 dd038bc6 2021-09-21 thomas.ad if (buf->wpos + len > buf->max) {
76 dd038bc6 2021-09-21 thomas.ad errno = ERANGE;
77 dd038bc6 2021-09-21 thomas.ad return (-1);
78 dd038bc6 2021-09-21 thomas.ad }
79 dd038bc6 2021-09-21 thomas.ad
80 c0faa645 2021-09-21 thomas.ad b = recallocarray(buf->buf, buf->size, buf->wpos + len, 1);
81 dd038bc6 2021-09-21 thomas.ad if (b == NULL)
82 dd038bc6 2021-09-21 thomas.ad return (-1);
83 dd038bc6 2021-09-21 thomas.ad buf->buf = b;
84 dd038bc6 2021-09-21 thomas.ad buf->size = buf->wpos + len;
85 dd038bc6 2021-09-21 thomas.ad
86 dd038bc6 2021-09-21 thomas.ad return (0);
87 dd038bc6 2021-09-21 thomas.ad }
88 dd038bc6 2021-09-21 thomas.ad
89 dd038bc6 2021-09-21 thomas.ad int
90 dd038bc6 2021-09-21 thomas.ad ibuf_add(struct ibuf *buf, const void *data, size_t len)
91 dd038bc6 2021-09-21 thomas.ad {
92 dd038bc6 2021-09-21 thomas.ad if (buf->wpos + len > buf->size)
93 dd038bc6 2021-09-21 thomas.ad if (ibuf_realloc(buf, len) == -1)
94 dd038bc6 2021-09-21 thomas.ad return (-1);
95 dd038bc6 2021-09-21 thomas.ad
96 dd038bc6 2021-09-21 thomas.ad memcpy(buf->buf + buf->wpos, data, len);
97 dd038bc6 2021-09-21 thomas.ad buf->wpos += len;
98 dd038bc6 2021-09-21 thomas.ad return (0);
99 dd038bc6 2021-09-21 thomas.ad }
100 dd038bc6 2021-09-21 thomas.ad
101 dd038bc6 2021-09-21 thomas.ad void *
102 dd038bc6 2021-09-21 thomas.ad ibuf_reserve(struct ibuf *buf, size_t len)
103 dd038bc6 2021-09-21 thomas.ad {
104 dd038bc6 2021-09-21 thomas.ad void *b;
105 dd038bc6 2021-09-21 thomas.ad
106 dd038bc6 2021-09-21 thomas.ad if (buf->wpos + len > buf->size)
107 dd038bc6 2021-09-21 thomas.ad if (ibuf_realloc(buf, len) == -1)
108 dd038bc6 2021-09-21 thomas.ad return (NULL);
109 dd038bc6 2021-09-21 thomas.ad
110 dd038bc6 2021-09-21 thomas.ad b = buf->buf + buf->wpos;
111 dd038bc6 2021-09-21 thomas.ad buf->wpos += len;
112 dd038bc6 2021-09-21 thomas.ad return (b);
113 dd038bc6 2021-09-21 thomas.ad }
114 dd038bc6 2021-09-21 thomas.ad
115 dd038bc6 2021-09-21 thomas.ad void *
116 dd038bc6 2021-09-21 thomas.ad ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
117 dd038bc6 2021-09-21 thomas.ad {
118 dd038bc6 2021-09-21 thomas.ad /* only allowed to seek in already written parts */
119 dd038bc6 2021-09-21 thomas.ad if (pos + len > buf->wpos)
120 dd038bc6 2021-09-21 thomas.ad return (NULL);
121 dd038bc6 2021-09-21 thomas.ad
122 dd038bc6 2021-09-21 thomas.ad return (buf->buf + pos);
123 dd038bc6 2021-09-21 thomas.ad }
124 dd038bc6 2021-09-21 thomas.ad
125 dd038bc6 2021-09-21 thomas.ad size_t
126 dd038bc6 2021-09-21 thomas.ad ibuf_size(struct ibuf *buf)
127 dd038bc6 2021-09-21 thomas.ad {
128 dd038bc6 2021-09-21 thomas.ad return (buf->wpos);
129 dd038bc6 2021-09-21 thomas.ad }
130 dd038bc6 2021-09-21 thomas.ad
131 dd038bc6 2021-09-21 thomas.ad size_t
132 dd038bc6 2021-09-21 thomas.ad ibuf_left(struct ibuf *buf)
133 dd038bc6 2021-09-21 thomas.ad {
134 dd038bc6 2021-09-21 thomas.ad return (buf->max - buf->wpos);
135 dd038bc6 2021-09-21 thomas.ad }
136 dd038bc6 2021-09-21 thomas.ad
137 dd038bc6 2021-09-21 thomas.ad void
138 dd038bc6 2021-09-21 thomas.ad ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
139 dd038bc6 2021-09-21 thomas.ad {
140 dd038bc6 2021-09-21 thomas.ad ibuf_enqueue(msgbuf, buf);
141 dd038bc6 2021-09-21 thomas.ad }
142 dd038bc6 2021-09-21 thomas.ad
143 dd038bc6 2021-09-21 thomas.ad int
144 dd038bc6 2021-09-21 thomas.ad ibuf_write(struct msgbuf *msgbuf)
145 dd038bc6 2021-09-21 thomas.ad {
146 dd038bc6 2021-09-21 thomas.ad struct iovec iov[IOV_MAX];
147 dd038bc6 2021-09-21 thomas.ad struct ibuf *buf;
148 dd038bc6 2021-09-21 thomas.ad unsigned int i = 0;
149 dd038bc6 2021-09-21 thomas.ad ssize_t n;
150 dd038bc6 2021-09-21 thomas.ad
151 dd038bc6 2021-09-21 thomas.ad memset(&iov, 0, sizeof(iov));
152 dd038bc6 2021-09-21 thomas.ad TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
153 dd038bc6 2021-09-21 thomas.ad if (i >= IOV_MAX)
154 dd038bc6 2021-09-21 thomas.ad break;
155 dd038bc6 2021-09-21 thomas.ad iov[i].iov_base = buf->buf + buf->rpos;
156 dd038bc6 2021-09-21 thomas.ad iov[i].iov_len = buf->wpos - buf->rpos;
157 dd038bc6 2021-09-21 thomas.ad i++;
158 dd038bc6 2021-09-21 thomas.ad }
159 dd038bc6 2021-09-21 thomas.ad
160 dd038bc6 2021-09-21 thomas.ad again:
161 dd038bc6 2021-09-21 thomas.ad if ((n = writev(msgbuf->fd, iov, i)) == -1) {
162 dd038bc6 2021-09-21 thomas.ad if (errno == EINTR)
163 dd038bc6 2021-09-21 thomas.ad goto again;
164 dd038bc6 2021-09-21 thomas.ad if (errno == ENOBUFS)
165 dd038bc6 2021-09-21 thomas.ad errno = EAGAIN;
166 dd038bc6 2021-09-21 thomas.ad return (-1);
167 dd038bc6 2021-09-21 thomas.ad }
168 dd038bc6 2021-09-21 thomas.ad
169 dd038bc6 2021-09-21 thomas.ad if (n == 0) { /* connection closed */
170 dd038bc6 2021-09-21 thomas.ad errno = 0;
171 dd038bc6 2021-09-21 thomas.ad return (0);
172 dd038bc6 2021-09-21 thomas.ad }
173 dd038bc6 2021-09-21 thomas.ad
174 dd038bc6 2021-09-21 thomas.ad msgbuf_drain(msgbuf, n);
175 dd038bc6 2021-09-21 thomas.ad
176 dd038bc6 2021-09-21 thomas.ad return (1);
177 dd038bc6 2021-09-21 thomas.ad }
178 dd038bc6 2021-09-21 thomas.ad
179 dd038bc6 2021-09-21 thomas.ad void
180 dd038bc6 2021-09-21 thomas.ad ibuf_free(struct ibuf *buf)
181 dd038bc6 2021-09-21 thomas.ad {
182 dd038bc6 2021-09-21 thomas.ad if (buf == NULL)
183 dd038bc6 2021-09-21 thomas.ad return;
184 dd038bc6 2021-09-21 thomas.ad freezero(buf->buf, buf->size);
185 dd038bc6 2021-09-21 thomas.ad free(buf);
186 dd038bc6 2021-09-21 thomas.ad }
187 dd038bc6 2021-09-21 thomas.ad
188 dd038bc6 2021-09-21 thomas.ad void
189 dd038bc6 2021-09-21 thomas.ad msgbuf_init(struct msgbuf *msgbuf)
190 dd038bc6 2021-09-21 thomas.ad {
191 dd038bc6 2021-09-21 thomas.ad msgbuf->queued = 0;
192 dd038bc6 2021-09-21 thomas.ad msgbuf->fd = -1;
193 dd038bc6 2021-09-21 thomas.ad TAILQ_INIT(&msgbuf->bufs);
194 dd038bc6 2021-09-21 thomas.ad }
195 dd038bc6 2021-09-21 thomas.ad
196 dd038bc6 2021-09-21 thomas.ad void
197 dd038bc6 2021-09-21 thomas.ad msgbuf_drain(struct msgbuf *msgbuf, size_t n)
198 dd038bc6 2021-09-21 thomas.ad {
199 dd038bc6 2021-09-21 thomas.ad struct ibuf *buf, *next;
200 dd038bc6 2021-09-21 thomas.ad
201 dd038bc6 2021-09-21 thomas.ad for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
202 dd038bc6 2021-09-21 thomas.ad buf = next) {
203 dd038bc6 2021-09-21 thomas.ad next = TAILQ_NEXT(buf, entry);
204 dd038bc6 2021-09-21 thomas.ad if (buf->rpos + n >= buf->wpos) {
205 dd038bc6 2021-09-21 thomas.ad n -= buf->wpos - buf->rpos;
206 dd038bc6 2021-09-21 thomas.ad ibuf_dequeue(msgbuf, buf);
207 dd038bc6 2021-09-21 thomas.ad } else {
208 dd038bc6 2021-09-21 thomas.ad buf->rpos += n;
209 dd038bc6 2021-09-21 thomas.ad n = 0;
210 dd038bc6 2021-09-21 thomas.ad }
211 dd038bc6 2021-09-21 thomas.ad }
212 dd038bc6 2021-09-21 thomas.ad }
213 dd038bc6 2021-09-21 thomas.ad
214 dd038bc6 2021-09-21 thomas.ad void
215 dd038bc6 2021-09-21 thomas.ad msgbuf_clear(struct msgbuf *msgbuf)
216 dd038bc6 2021-09-21 thomas.ad {
217 dd038bc6 2021-09-21 thomas.ad struct ibuf *buf;
218 dd038bc6 2021-09-21 thomas.ad
219 dd038bc6 2021-09-21 thomas.ad while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
220 dd038bc6 2021-09-21 thomas.ad ibuf_dequeue(msgbuf, buf);
221 dd038bc6 2021-09-21 thomas.ad }
222 dd038bc6 2021-09-21 thomas.ad
223 dd038bc6 2021-09-21 thomas.ad int
224 dd038bc6 2021-09-21 thomas.ad msgbuf_write(struct msgbuf *msgbuf)
225 dd038bc6 2021-09-21 thomas.ad {
226 dd038bc6 2021-09-21 thomas.ad struct iovec iov[IOV_MAX];
227 dd038bc6 2021-09-21 thomas.ad struct ibuf *buf;
228 dd038bc6 2021-09-21 thomas.ad unsigned int i = 0;
229 dd038bc6 2021-09-21 thomas.ad ssize_t n;
230 dd038bc6 2021-09-21 thomas.ad struct msghdr msg;
231 dd038bc6 2021-09-21 thomas.ad struct cmsghdr *cmsg;
232 dd038bc6 2021-09-21 thomas.ad union {
233 dd038bc6 2021-09-21 thomas.ad struct cmsghdr hdr;
234 dd038bc6 2021-09-21 thomas.ad char buf[CMSG_SPACE(sizeof(int))];
235 dd038bc6 2021-09-21 thomas.ad } cmsgbuf;
236 dd038bc6 2021-09-21 thomas.ad
237 dd038bc6 2021-09-21 thomas.ad memset(&iov, 0, sizeof(iov));
238 dd038bc6 2021-09-21 thomas.ad memset(&msg, 0, sizeof(msg));
239 dd038bc6 2021-09-21 thomas.ad memset(&cmsgbuf, 0, sizeof(cmsgbuf));
240 dd038bc6 2021-09-21 thomas.ad TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
241 dd038bc6 2021-09-21 thomas.ad if (i >= IOV_MAX)
242 dd038bc6 2021-09-21 thomas.ad break;
243 dd038bc6 2021-09-21 thomas.ad iov[i].iov_base = buf->buf + buf->rpos;
244 dd038bc6 2021-09-21 thomas.ad iov[i].iov_len = buf->wpos - buf->rpos;
245 dd038bc6 2021-09-21 thomas.ad i++;
246 dd038bc6 2021-09-21 thomas.ad if (buf->fd != -1)
247 dd038bc6 2021-09-21 thomas.ad break;
248 dd038bc6 2021-09-21 thomas.ad }
249 dd038bc6 2021-09-21 thomas.ad
250 dd038bc6 2021-09-21 thomas.ad msg.msg_iov = iov;
251 dd038bc6 2021-09-21 thomas.ad msg.msg_iovlen = i;
252 dd038bc6 2021-09-21 thomas.ad
253 dd038bc6 2021-09-21 thomas.ad if (buf != NULL && buf->fd != -1) {
254 dd038bc6 2021-09-21 thomas.ad msg.msg_control = (caddr_t)&cmsgbuf.buf;
255 dd038bc6 2021-09-21 thomas.ad msg.msg_controllen = sizeof(cmsgbuf.buf);
256 dd038bc6 2021-09-21 thomas.ad cmsg = CMSG_FIRSTHDR(&msg);
257 dd038bc6 2021-09-21 thomas.ad cmsg->cmsg_len = CMSG_LEN(sizeof(int));
258 dd038bc6 2021-09-21 thomas.ad cmsg->cmsg_level = SOL_SOCKET;
259 dd038bc6 2021-09-21 thomas.ad cmsg->cmsg_type = SCM_RIGHTS;
260 dd038bc6 2021-09-21 thomas.ad *(int *)CMSG_DATA(cmsg) = buf->fd;
261 dd038bc6 2021-09-21 thomas.ad }
262 dd038bc6 2021-09-21 thomas.ad
263 dd038bc6 2021-09-21 thomas.ad again:
264 dd038bc6 2021-09-21 thomas.ad if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
265 dd038bc6 2021-09-21 thomas.ad if (errno == EINTR)
266 dd038bc6 2021-09-21 thomas.ad goto again;
267 dd038bc6 2021-09-21 thomas.ad if (errno == ENOBUFS)
268 dd038bc6 2021-09-21 thomas.ad errno = EAGAIN;
269 dd038bc6 2021-09-21 thomas.ad return (-1);
270 dd038bc6 2021-09-21 thomas.ad }
271 dd038bc6 2021-09-21 thomas.ad
272 dd038bc6 2021-09-21 thomas.ad if (n == 0) { /* connection closed */
273 dd038bc6 2021-09-21 thomas.ad errno = 0;
274 dd038bc6 2021-09-21 thomas.ad return (0);
275 dd038bc6 2021-09-21 thomas.ad }
276 dd038bc6 2021-09-21 thomas.ad
277 dd038bc6 2021-09-21 thomas.ad /*
278 dd038bc6 2021-09-21 thomas.ad * assumption: fd got sent if sendmsg sent anything
279 dd038bc6 2021-09-21 thomas.ad * this works because fds are passed one at a time
280 dd038bc6 2021-09-21 thomas.ad */
281 dd038bc6 2021-09-21 thomas.ad if (buf != NULL && buf->fd != -1) {
282 dd038bc6 2021-09-21 thomas.ad close(buf->fd);
283 dd038bc6 2021-09-21 thomas.ad buf->fd = -1;
284 dd038bc6 2021-09-21 thomas.ad }
285 dd038bc6 2021-09-21 thomas.ad
286 dd038bc6 2021-09-21 thomas.ad msgbuf_drain(msgbuf, n);
287 dd038bc6 2021-09-21 thomas.ad
288 dd038bc6 2021-09-21 thomas.ad return (1);
289 dd038bc6 2021-09-21 thomas.ad }
290 dd038bc6 2021-09-21 thomas.ad
291 dd038bc6 2021-09-21 thomas.ad static void
292 dd038bc6 2021-09-21 thomas.ad ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
293 dd038bc6 2021-09-21 thomas.ad {
294 dd038bc6 2021-09-21 thomas.ad TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
295 dd038bc6 2021-09-21 thomas.ad msgbuf->queued++;
296 dd038bc6 2021-09-21 thomas.ad }
297 dd038bc6 2021-09-21 thomas.ad
298 dd038bc6 2021-09-21 thomas.ad static void
299 dd038bc6 2021-09-21 thomas.ad ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
300 dd038bc6 2021-09-21 thomas.ad {
301 dd038bc6 2021-09-21 thomas.ad TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
302 dd038bc6 2021-09-21 thomas.ad
303 dd038bc6 2021-09-21 thomas.ad if (buf->fd != -1)
304 dd038bc6 2021-09-21 thomas.ad close(buf->fd);
305 dd038bc6 2021-09-21 thomas.ad
306 dd038bc6 2021-09-21 thomas.ad msgbuf->queued--;
307 dd038bc6 2021-09-21 thomas.ad ibuf_free(buf);
308 dd038bc6 2021-09-21 thomas.ad }