Blame


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