Blame


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