Blame


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