Blame


1 291ecc47 2023-07-02 op /* $OpenBSD: imsg-buffer.c,v 1.16 2023/06/19 17:19:50 claudio Exp $ */
2 83f0f95a 2022-09-29 op
3 83f0f95a 2022-09-29 op /*
4 83f0f95a 2022-09-29 op * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5 83f0f95a 2022-09-29 op *
6 83f0f95a 2022-09-29 op * Permission to use, copy, modify, and distribute this software for any
7 83f0f95a 2022-09-29 op * purpose with or without fee is hereby granted, provided that the above
8 83f0f95a 2022-09-29 op * copyright notice and this permission notice appear in all copies.
9 83f0f95a 2022-09-29 op *
10 83f0f95a 2022-09-29 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 83f0f95a 2022-09-29 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 83f0f95a 2022-09-29 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 83f0f95a 2022-09-29 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 83f0f95a 2022-09-29 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 83f0f95a 2022-09-29 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 83f0f95a 2022-09-29 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 83f0f95a 2022-09-29 op */
18 83f0f95a 2022-09-29 op
19 83f0f95a 2022-09-29 op #include <sys/types.h>
20 83f0f95a 2022-09-29 op #include <sys/queue.h>
21 83f0f95a 2022-09-29 op #include <sys/socket.h>
22 83f0f95a 2022-09-29 op #include <sys/uio.h>
23 83f0f95a 2022-09-29 op
24 83f0f95a 2022-09-29 op #include <limits.h>
25 83f0f95a 2022-09-29 op #include <errno.h>
26 291ecc47 2023-07-02 op #include <endian.h>
27 83f0f95a 2022-09-29 op #include <stdlib.h>
28 83f0f95a 2022-09-29 op #include <string.h>
29 83f0f95a 2022-09-29 op #include <unistd.h>
30 83f0f95a 2022-09-29 op
31 83f0f95a 2022-09-29 op #include "imsg.h"
32 83f0f95a 2022-09-29 op
33 83f0f95a 2022-09-29 op static int ibuf_realloc(struct ibuf *, size_t);
34 83f0f95a 2022-09-29 op static void ibuf_enqueue(struct msgbuf *, struct ibuf *);
35 83f0f95a 2022-09-29 op static void ibuf_dequeue(struct msgbuf *, struct ibuf *);
36 291ecc47 2023-07-02 op static void msgbuf_drain(struct msgbuf *, size_t);
37 83f0f95a 2022-09-29 op
38 83f0f95a 2022-09-29 op struct ibuf *
39 83f0f95a 2022-09-29 op ibuf_open(size_t len)
40 83f0f95a 2022-09-29 op {
41 83f0f95a 2022-09-29 op struct ibuf *buf;
42 83f0f95a 2022-09-29 op
43 291ecc47 2023-07-02 op if (len == 0) {
44 291ecc47 2023-07-02 op errno = EINVAL;
45 291ecc47 2023-07-02 op return (NULL);
46 291ecc47 2023-07-02 op }
47 83f0f95a 2022-09-29 op if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
48 83f0f95a 2022-09-29 op return (NULL);
49 291ecc47 2023-07-02 op if ((buf->buf = calloc(len, 1)) == NULL) {
50 83f0f95a 2022-09-29 op free(buf);
51 83f0f95a 2022-09-29 op return (NULL);
52 83f0f95a 2022-09-29 op }
53 83f0f95a 2022-09-29 op buf->size = buf->max = len;
54 83f0f95a 2022-09-29 op buf->fd = -1;
55 83f0f95a 2022-09-29 op
56 83f0f95a 2022-09-29 op return (buf);
57 83f0f95a 2022-09-29 op }
58 83f0f95a 2022-09-29 op
59 83f0f95a 2022-09-29 op struct ibuf *
60 83f0f95a 2022-09-29 op ibuf_dynamic(size_t len, size_t max)
61 83f0f95a 2022-09-29 op {
62 83f0f95a 2022-09-29 op struct ibuf *buf;
63 83f0f95a 2022-09-29 op
64 291ecc47 2023-07-02 op if (max < len) {
65 291ecc47 2023-07-02 op errno = EINVAL;
66 83f0f95a 2022-09-29 op return (NULL);
67 291ecc47 2023-07-02 op }
68 83f0f95a 2022-09-29 op
69 291ecc47 2023-07-02 op if ((buf = calloc(1, sizeof(struct ibuf))) == NULL)
70 83f0f95a 2022-09-29 op return (NULL);
71 291ecc47 2023-07-02 op if (len > 0) {
72 291ecc47 2023-07-02 op if ((buf->buf = calloc(len, 1)) == NULL) {
73 291ecc47 2023-07-02 op free(buf);
74 291ecc47 2023-07-02 op return (NULL);
75 291ecc47 2023-07-02 op }
76 291ecc47 2023-07-02 op }
77 291ecc47 2023-07-02 op buf->size = len;
78 291ecc47 2023-07-02 op buf->max = max;
79 291ecc47 2023-07-02 op buf->fd = -1;
80 83f0f95a 2022-09-29 op
81 83f0f95a 2022-09-29 op return (buf);
82 83f0f95a 2022-09-29 op }
83 83f0f95a 2022-09-29 op
84 83f0f95a 2022-09-29 op static int
85 83f0f95a 2022-09-29 op ibuf_realloc(struct ibuf *buf, size_t len)
86 83f0f95a 2022-09-29 op {
87 83f0f95a 2022-09-29 op unsigned char *b;
88 83f0f95a 2022-09-29 op
89 83f0f95a 2022-09-29 op /* on static buffers max is eq size and so the following fails */
90 83f0f95a 2022-09-29 op if (len > SIZE_MAX - buf->wpos || buf->wpos + len > buf->max) {
91 83f0f95a 2022-09-29 op errno = ERANGE;
92 83f0f95a 2022-09-29 op return (-1);
93 83f0f95a 2022-09-29 op }
94 83f0f95a 2022-09-29 op
95 83f0f95a 2022-09-29 op b = recallocarray(buf->buf, buf->size, buf->wpos + len, 1);
96 83f0f95a 2022-09-29 op if (b == NULL)
97 83f0f95a 2022-09-29 op return (-1);
98 83f0f95a 2022-09-29 op buf->buf = b;
99 83f0f95a 2022-09-29 op buf->size = buf->wpos + len;
100 83f0f95a 2022-09-29 op
101 83f0f95a 2022-09-29 op return (0);
102 83f0f95a 2022-09-29 op }
103 83f0f95a 2022-09-29 op
104 83f0f95a 2022-09-29 op void *
105 83f0f95a 2022-09-29 op ibuf_reserve(struct ibuf *buf, size_t len)
106 83f0f95a 2022-09-29 op {
107 83f0f95a 2022-09-29 op void *b;
108 83f0f95a 2022-09-29 op
109 83f0f95a 2022-09-29 op if (len > SIZE_MAX - buf->wpos) {
110 83f0f95a 2022-09-29 op errno = ERANGE;
111 83f0f95a 2022-09-29 op return (NULL);
112 83f0f95a 2022-09-29 op }
113 83f0f95a 2022-09-29 op
114 83f0f95a 2022-09-29 op if (buf->wpos + len > buf->size)
115 83f0f95a 2022-09-29 op if (ibuf_realloc(buf, len) == -1)
116 83f0f95a 2022-09-29 op return (NULL);
117 83f0f95a 2022-09-29 op
118 83f0f95a 2022-09-29 op b = buf->buf + buf->wpos;
119 83f0f95a 2022-09-29 op buf->wpos += len;
120 291ecc47 2023-07-02 op memset(b, 0, len);
121 83f0f95a 2022-09-29 op return (b);
122 83f0f95a 2022-09-29 op }
123 83f0f95a 2022-09-29 op
124 291ecc47 2023-07-02 op int
125 291ecc47 2023-07-02 op ibuf_add(struct ibuf *buf, const void *data, size_t len)
126 291ecc47 2023-07-02 op {
127 291ecc47 2023-07-02 op void *b;
128 291ecc47 2023-07-02 op
129 291ecc47 2023-07-02 op if ((b = ibuf_reserve(buf, len)) == NULL)
130 291ecc47 2023-07-02 op return (-1);
131 291ecc47 2023-07-02 op
132 291ecc47 2023-07-02 op memcpy(b, data, len);
133 291ecc47 2023-07-02 op return (0);
134 291ecc47 2023-07-02 op }
135 291ecc47 2023-07-02 op
136 291ecc47 2023-07-02 op int
137 291ecc47 2023-07-02 op ibuf_add_buf(struct ibuf *buf, const struct ibuf *from)
138 291ecc47 2023-07-02 op {
139 291ecc47 2023-07-02 op return ibuf_add(buf, from->buf, from->wpos);
140 291ecc47 2023-07-02 op }
141 291ecc47 2023-07-02 op
142 291ecc47 2023-07-02 op int
143 291ecc47 2023-07-02 op ibuf_add_n8(struct ibuf *buf, uint64_t value)
144 291ecc47 2023-07-02 op {
145 291ecc47 2023-07-02 op uint8_t v;
146 291ecc47 2023-07-02 op
147 291ecc47 2023-07-02 op if (value > UINT8_MAX) {
148 291ecc47 2023-07-02 op errno = EINVAL;
149 291ecc47 2023-07-02 op return (-1);
150 291ecc47 2023-07-02 op }
151 291ecc47 2023-07-02 op v = value;
152 291ecc47 2023-07-02 op return ibuf_add(buf, &v, sizeof(v));
153 291ecc47 2023-07-02 op }
154 291ecc47 2023-07-02 op
155 291ecc47 2023-07-02 op int
156 291ecc47 2023-07-02 op ibuf_add_n16(struct ibuf *buf, uint64_t value)
157 291ecc47 2023-07-02 op {
158 291ecc47 2023-07-02 op uint16_t v;
159 291ecc47 2023-07-02 op
160 291ecc47 2023-07-02 op if (value > UINT16_MAX) {
161 291ecc47 2023-07-02 op errno = EINVAL;
162 291ecc47 2023-07-02 op return (-1);
163 291ecc47 2023-07-02 op }
164 291ecc47 2023-07-02 op v = htobe16(value);
165 291ecc47 2023-07-02 op return ibuf_add(buf, &v, sizeof(v));
166 291ecc47 2023-07-02 op }
167 291ecc47 2023-07-02 op
168 291ecc47 2023-07-02 op int
169 291ecc47 2023-07-02 op ibuf_add_n32(struct ibuf *buf, uint64_t value)
170 291ecc47 2023-07-02 op {
171 291ecc47 2023-07-02 op uint32_t v;
172 291ecc47 2023-07-02 op
173 291ecc47 2023-07-02 op if (value > UINT32_MAX) {
174 291ecc47 2023-07-02 op errno = EINVAL;
175 291ecc47 2023-07-02 op return (-1);
176 291ecc47 2023-07-02 op }
177 291ecc47 2023-07-02 op v = htobe32(value);
178 291ecc47 2023-07-02 op return ibuf_add(buf, &v, sizeof(v));
179 291ecc47 2023-07-02 op }
180 291ecc47 2023-07-02 op
181 291ecc47 2023-07-02 op int
182 291ecc47 2023-07-02 op ibuf_add_n64(struct ibuf *buf, uint64_t value)
183 291ecc47 2023-07-02 op {
184 291ecc47 2023-07-02 op value = htobe64(value);
185 291ecc47 2023-07-02 op return ibuf_add(buf, &value, sizeof(value));
186 291ecc47 2023-07-02 op }
187 291ecc47 2023-07-02 op
188 291ecc47 2023-07-02 op int
189 291ecc47 2023-07-02 op ibuf_add_zero(struct ibuf *buf, size_t len)
190 291ecc47 2023-07-02 op {
191 291ecc47 2023-07-02 op void *b;
192 291ecc47 2023-07-02 op
193 291ecc47 2023-07-02 op if ((b = ibuf_reserve(buf, len)) == NULL)
194 291ecc47 2023-07-02 op return (-1);
195 291ecc47 2023-07-02 op return (0);
196 291ecc47 2023-07-02 op }
197 291ecc47 2023-07-02 op
198 83f0f95a 2022-09-29 op void *
199 83f0f95a 2022-09-29 op ibuf_seek(struct ibuf *buf, size_t pos, size_t len)
200 83f0f95a 2022-09-29 op {
201 83f0f95a 2022-09-29 op /* only allowed to seek in already written parts */
202 291ecc47 2023-07-02 op if (len > SIZE_MAX - pos || pos + len > buf->wpos) {
203 291ecc47 2023-07-02 op errno = ERANGE;
204 83f0f95a 2022-09-29 op return (NULL);
205 291ecc47 2023-07-02 op }
206 83f0f95a 2022-09-29 op
207 83f0f95a 2022-09-29 op return (buf->buf + pos);
208 83f0f95a 2022-09-29 op }
209 83f0f95a 2022-09-29 op
210 291ecc47 2023-07-02 op int
211 291ecc47 2023-07-02 op ibuf_set(struct ibuf *buf, size_t pos, const void *data, size_t len)
212 291ecc47 2023-07-02 op {
213 291ecc47 2023-07-02 op void *b;
214 291ecc47 2023-07-02 op
215 291ecc47 2023-07-02 op if ((b = ibuf_seek(buf, pos, len)) == NULL)
216 291ecc47 2023-07-02 op return (-1);
217 291ecc47 2023-07-02 op
218 291ecc47 2023-07-02 op memcpy(b, data, len);
219 291ecc47 2023-07-02 op return (0);
220 291ecc47 2023-07-02 op }
221 291ecc47 2023-07-02 op
222 291ecc47 2023-07-02 op int
223 291ecc47 2023-07-02 op ibuf_set_n8(struct ibuf *buf, size_t pos, uint64_t value)
224 291ecc47 2023-07-02 op {
225 291ecc47 2023-07-02 op uint8_t v;
226 291ecc47 2023-07-02 op
227 291ecc47 2023-07-02 op if (value > UINT8_MAX) {
228 291ecc47 2023-07-02 op errno = EINVAL;
229 291ecc47 2023-07-02 op return (-1);
230 291ecc47 2023-07-02 op }
231 291ecc47 2023-07-02 op v = value;
232 291ecc47 2023-07-02 op return (ibuf_set(buf, pos, &v, sizeof(v)));
233 291ecc47 2023-07-02 op }
234 291ecc47 2023-07-02 op
235 291ecc47 2023-07-02 op int
236 291ecc47 2023-07-02 op ibuf_set_n16(struct ibuf *buf, size_t pos, uint64_t value)
237 291ecc47 2023-07-02 op {
238 291ecc47 2023-07-02 op uint16_t v;
239 291ecc47 2023-07-02 op
240 291ecc47 2023-07-02 op if (value > UINT16_MAX) {
241 291ecc47 2023-07-02 op errno = EINVAL;
242 291ecc47 2023-07-02 op return (-1);
243 291ecc47 2023-07-02 op }
244 291ecc47 2023-07-02 op v = htobe16(value);
245 291ecc47 2023-07-02 op return (ibuf_set(buf, pos, &v, sizeof(v)));
246 291ecc47 2023-07-02 op }
247 291ecc47 2023-07-02 op
248 291ecc47 2023-07-02 op int
249 291ecc47 2023-07-02 op ibuf_set_n32(struct ibuf *buf, size_t pos, uint64_t value)
250 291ecc47 2023-07-02 op {
251 291ecc47 2023-07-02 op uint32_t v;
252 291ecc47 2023-07-02 op
253 291ecc47 2023-07-02 op if (value > UINT32_MAX) {
254 291ecc47 2023-07-02 op errno = EINVAL;
255 291ecc47 2023-07-02 op return (-1);
256 291ecc47 2023-07-02 op }
257 291ecc47 2023-07-02 op v = htobe32(value);
258 291ecc47 2023-07-02 op return (ibuf_set(buf, pos, &v, sizeof(v)));
259 291ecc47 2023-07-02 op }
260 291ecc47 2023-07-02 op
261 291ecc47 2023-07-02 op int
262 291ecc47 2023-07-02 op ibuf_set_n64(struct ibuf *buf, size_t pos, uint64_t value)
263 291ecc47 2023-07-02 op {
264 291ecc47 2023-07-02 op value = htobe64(value);
265 291ecc47 2023-07-02 op return (ibuf_set(buf, pos, &value, sizeof(value)));
266 291ecc47 2023-07-02 op }
267 291ecc47 2023-07-02 op
268 291ecc47 2023-07-02 op void *
269 291ecc47 2023-07-02 op ibuf_data(struct ibuf *buf)
270 291ecc47 2023-07-02 op {
271 291ecc47 2023-07-02 op return (buf->buf);
272 291ecc47 2023-07-02 op }
273 291ecc47 2023-07-02 op
274 83f0f95a 2022-09-29 op size_t
275 83f0f95a 2022-09-29 op ibuf_size(struct ibuf *buf)
276 83f0f95a 2022-09-29 op {
277 83f0f95a 2022-09-29 op return (buf->wpos);
278 83f0f95a 2022-09-29 op }
279 83f0f95a 2022-09-29 op
280 83f0f95a 2022-09-29 op size_t
281 83f0f95a 2022-09-29 op ibuf_left(struct ibuf *buf)
282 83f0f95a 2022-09-29 op {
283 83f0f95a 2022-09-29 op return (buf->max - buf->wpos);
284 83f0f95a 2022-09-29 op }
285 83f0f95a 2022-09-29 op
286 83f0f95a 2022-09-29 op void
287 83f0f95a 2022-09-29 op ibuf_close(struct msgbuf *msgbuf, struct ibuf *buf)
288 83f0f95a 2022-09-29 op {
289 83f0f95a 2022-09-29 op ibuf_enqueue(msgbuf, buf);
290 83f0f95a 2022-09-29 op }
291 83f0f95a 2022-09-29 op
292 291ecc47 2023-07-02 op void
293 291ecc47 2023-07-02 op ibuf_free(struct ibuf *buf)
294 291ecc47 2023-07-02 op {
295 291ecc47 2023-07-02 op if (buf == NULL)
296 291ecc47 2023-07-02 op return;
297 291ecc47 2023-07-02 op #ifdef NOTYET
298 291ecc47 2023-07-02 op if (buf->fd != -1)
299 291ecc47 2023-07-02 op close(buf->fd);
300 291ecc47 2023-07-02 op #endif
301 291ecc47 2023-07-02 op freezero(buf->buf, buf->size);
302 291ecc47 2023-07-02 op free(buf);
303 291ecc47 2023-07-02 op }
304 291ecc47 2023-07-02 op
305 83f0f95a 2022-09-29 op int
306 291ecc47 2023-07-02 op ibuf_fd_avail(struct ibuf *buf)
307 291ecc47 2023-07-02 op {
308 291ecc47 2023-07-02 op return (buf->fd != -1);
309 291ecc47 2023-07-02 op }
310 291ecc47 2023-07-02 op
311 291ecc47 2023-07-02 op int
312 291ecc47 2023-07-02 op ibuf_fd_get(struct ibuf *buf)
313 291ecc47 2023-07-02 op {
314 291ecc47 2023-07-02 op int fd;
315 291ecc47 2023-07-02 op
316 291ecc47 2023-07-02 op fd = buf->fd;
317 291ecc47 2023-07-02 op #ifdef NOTYET
318 291ecc47 2023-07-02 op buf->fd = -1;
319 291ecc47 2023-07-02 op #endif
320 291ecc47 2023-07-02 op return (fd);
321 291ecc47 2023-07-02 op }
322 291ecc47 2023-07-02 op
323 291ecc47 2023-07-02 op void
324 291ecc47 2023-07-02 op ibuf_fd_set(struct ibuf *buf, int fd)
325 291ecc47 2023-07-02 op {
326 291ecc47 2023-07-02 op if (buf->fd != -1)
327 291ecc47 2023-07-02 op close(buf->fd);
328 291ecc47 2023-07-02 op buf->fd = fd;
329 291ecc47 2023-07-02 op }
330 291ecc47 2023-07-02 op
331 291ecc47 2023-07-02 op int
332 83f0f95a 2022-09-29 op ibuf_write(struct msgbuf *msgbuf)
333 83f0f95a 2022-09-29 op {
334 83f0f95a 2022-09-29 op struct iovec iov[IOV_MAX];
335 83f0f95a 2022-09-29 op struct ibuf *buf;
336 83f0f95a 2022-09-29 op unsigned int i = 0;
337 83f0f95a 2022-09-29 op ssize_t n;
338 83f0f95a 2022-09-29 op
339 83f0f95a 2022-09-29 op memset(&iov, 0, sizeof(iov));
340 83f0f95a 2022-09-29 op TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
341 83f0f95a 2022-09-29 op if (i >= IOV_MAX)
342 83f0f95a 2022-09-29 op break;
343 83f0f95a 2022-09-29 op iov[i].iov_base = buf->buf + buf->rpos;
344 83f0f95a 2022-09-29 op iov[i].iov_len = buf->wpos - buf->rpos;
345 83f0f95a 2022-09-29 op i++;
346 83f0f95a 2022-09-29 op }
347 83f0f95a 2022-09-29 op
348 83f0f95a 2022-09-29 op again:
349 83f0f95a 2022-09-29 op if ((n = writev(msgbuf->fd, iov, i)) == -1) {
350 83f0f95a 2022-09-29 op if (errno == EINTR)
351 83f0f95a 2022-09-29 op goto again;
352 83f0f95a 2022-09-29 op if (errno == ENOBUFS)
353 83f0f95a 2022-09-29 op errno = EAGAIN;
354 83f0f95a 2022-09-29 op return (-1);
355 83f0f95a 2022-09-29 op }
356 83f0f95a 2022-09-29 op
357 83f0f95a 2022-09-29 op if (n == 0) { /* connection closed */
358 83f0f95a 2022-09-29 op errno = 0;
359 83f0f95a 2022-09-29 op return (0);
360 83f0f95a 2022-09-29 op }
361 83f0f95a 2022-09-29 op
362 83f0f95a 2022-09-29 op msgbuf_drain(msgbuf, n);
363 83f0f95a 2022-09-29 op
364 83f0f95a 2022-09-29 op return (1);
365 83f0f95a 2022-09-29 op }
366 83f0f95a 2022-09-29 op
367 83f0f95a 2022-09-29 op void
368 83f0f95a 2022-09-29 op msgbuf_init(struct msgbuf *msgbuf)
369 83f0f95a 2022-09-29 op {
370 83f0f95a 2022-09-29 op msgbuf->queued = 0;
371 83f0f95a 2022-09-29 op msgbuf->fd = -1;
372 83f0f95a 2022-09-29 op TAILQ_INIT(&msgbuf->bufs);
373 83f0f95a 2022-09-29 op }
374 83f0f95a 2022-09-29 op
375 291ecc47 2023-07-02 op static void
376 83f0f95a 2022-09-29 op msgbuf_drain(struct msgbuf *msgbuf, size_t n)
377 83f0f95a 2022-09-29 op {
378 83f0f95a 2022-09-29 op struct ibuf *buf, *next;
379 83f0f95a 2022-09-29 op
380 83f0f95a 2022-09-29 op for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0;
381 83f0f95a 2022-09-29 op buf = next) {
382 83f0f95a 2022-09-29 op next = TAILQ_NEXT(buf, entry);
383 83f0f95a 2022-09-29 op if (n >= buf->wpos - buf->rpos) {
384 83f0f95a 2022-09-29 op n -= buf->wpos - buf->rpos;
385 83f0f95a 2022-09-29 op ibuf_dequeue(msgbuf, buf);
386 83f0f95a 2022-09-29 op } else {
387 83f0f95a 2022-09-29 op buf->rpos += n;
388 83f0f95a 2022-09-29 op n = 0;
389 83f0f95a 2022-09-29 op }
390 83f0f95a 2022-09-29 op }
391 83f0f95a 2022-09-29 op }
392 83f0f95a 2022-09-29 op
393 83f0f95a 2022-09-29 op void
394 83f0f95a 2022-09-29 op msgbuf_clear(struct msgbuf *msgbuf)
395 83f0f95a 2022-09-29 op {
396 83f0f95a 2022-09-29 op struct ibuf *buf;
397 83f0f95a 2022-09-29 op
398 83f0f95a 2022-09-29 op while ((buf = TAILQ_FIRST(&msgbuf->bufs)) != NULL)
399 83f0f95a 2022-09-29 op ibuf_dequeue(msgbuf, buf);
400 83f0f95a 2022-09-29 op }
401 83f0f95a 2022-09-29 op
402 83f0f95a 2022-09-29 op int
403 83f0f95a 2022-09-29 op msgbuf_write(struct msgbuf *msgbuf)
404 83f0f95a 2022-09-29 op {
405 83f0f95a 2022-09-29 op struct iovec iov[IOV_MAX];
406 83f0f95a 2022-09-29 op struct ibuf *buf, *buf0 = NULL;
407 83f0f95a 2022-09-29 op unsigned int i = 0;
408 83f0f95a 2022-09-29 op ssize_t n;
409 83f0f95a 2022-09-29 op struct msghdr msg;
410 83f0f95a 2022-09-29 op struct cmsghdr *cmsg;
411 83f0f95a 2022-09-29 op union {
412 83f0f95a 2022-09-29 op struct cmsghdr hdr;
413 83f0f95a 2022-09-29 op char buf[CMSG_SPACE(sizeof(int))];
414 83f0f95a 2022-09-29 op } cmsgbuf;
415 83f0f95a 2022-09-29 op
416 83f0f95a 2022-09-29 op memset(&iov, 0, sizeof(iov));
417 83f0f95a 2022-09-29 op memset(&msg, 0, sizeof(msg));
418 83f0f95a 2022-09-29 op memset(&cmsgbuf, 0, sizeof(cmsgbuf));
419 83f0f95a 2022-09-29 op TAILQ_FOREACH(buf, &msgbuf->bufs, entry) {
420 83f0f95a 2022-09-29 op if (i >= IOV_MAX)
421 83f0f95a 2022-09-29 op break;
422 83f0f95a 2022-09-29 op if (i > 0 && buf->fd != -1)
423 83f0f95a 2022-09-29 op break;
424 83f0f95a 2022-09-29 op iov[i].iov_base = buf->buf + buf->rpos;
425 83f0f95a 2022-09-29 op iov[i].iov_len = buf->wpos - buf->rpos;
426 83f0f95a 2022-09-29 op i++;
427 83f0f95a 2022-09-29 op if (buf->fd != -1)
428 83f0f95a 2022-09-29 op buf0 = buf;
429 83f0f95a 2022-09-29 op }
430 83f0f95a 2022-09-29 op
431 83f0f95a 2022-09-29 op msg.msg_iov = iov;
432 83f0f95a 2022-09-29 op msg.msg_iovlen = i;
433 83f0f95a 2022-09-29 op
434 83f0f95a 2022-09-29 op if (buf0 != NULL) {
435 83f0f95a 2022-09-29 op msg.msg_control = (caddr_t)&cmsgbuf.buf;
436 83f0f95a 2022-09-29 op msg.msg_controllen = sizeof(cmsgbuf.buf);
437 83f0f95a 2022-09-29 op cmsg = CMSG_FIRSTHDR(&msg);
438 83f0f95a 2022-09-29 op cmsg->cmsg_len = CMSG_LEN(sizeof(int));
439 83f0f95a 2022-09-29 op cmsg->cmsg_level = SOL_SOCKET;
440 83f0f95a 2022-09-29 op cmsg->cmsg_type = SCM_RIGHTS;
441 83f0f95a 2022-09-29 op *(int *)CMSG_DATA(cmsg) = buf0->fd;
442 83f0f95a 2022-09-29 op }
443 83f0f95a 2022-09-29 op
444 83f0f95a 2022-09-29 op again:
445 83f0f95a 2022-09-29 op if ((n = sendmsg(msgbuf->fd, &msg, 0)) == -1) {
446 83f0f95a 2022-09-29 op if (errno == EINTR)
447 83f0f95a 2022-09-29 op goto again;
448 83f0f95a 2022-09-29 op if (errno == ENOBUFS)
449 83f0f95a 2022-09-29 op errno = EAGAIN;
450 83f0f95a 2022-09-29 op return (-1);
451 83f0f95a 2022-09-29 op }
452 83f0f95a 2022-09-29 op
453 83f0f95a 2022-09-29 op if (n == 0) { /* connection closed */
454 83f0f95a 2022-09-29 op errno = 0;
455 83f0f95a 2022-09-29 op return (0);
456 83f0f95a 2022-09-29 op }
457 83f0f95a 2022-09-29 op
458 83f0f95a 2022-09-29 op /*
459 83f0f95a 2022-09-29 op * assumption: fd got sent if sendmsg sent anything
460 83f0f95a 2022-09-29 op * this works because fds are passed one at a time
461 83f0f95a 2022-09-29 op */
462 83f0f95a 2022-09-29 op if (buf0 != NULL) {
463 83f0f95a 2022-09-29 op close(buf0->fd);
464 83f0f95a 2022-09-29 op buf0->fd = -1;
465 83f0f95a 2022-09-29 op }
466 83f0f95a 2022-09-29 op
467 83f0f95a 2022-09-29 op msgbuf_drain(msgbuf, n);
468 83f0f95a 2022-09-29 op
469 83f0f95a 2022-09-29 op return (1);
470 83f0f95a 2022-09-29 op }
471 83f0f95a 2022-09-29 op
472 83f0f95a 2022-09-29 op static void
473 83f0f95a 2022-09-29 op ibuf_enqueue(struct msgbuf *msgbuf, struct ibuf *buf)
474 83f0f95a 2022-09-29 op {
475 83f0f95a 2022-09-29 op TAILQ_INSERT_TAIL(&msgbuf->bufs, buf, entry);
476 83f0f95a 2022-09-29 op msgbuf->queued++;
477 83f0f95a 2022-09-29 op }
478 83f0f95a 2022-09-29 op
479 83f0f95a 2022-09-29 op static void
480 83f0f95a 2022-09-29 op ibuf_dequeue(struct msgbuf *msgbuf, struct ibuf *buf)
481 83f0f95a 2022-09-29 op {
482 83f0f95a 2022-09-29 op TAILQ_REMOVE(&msgbuf->bufs, buf, entry);
483 83f0f95a 2022-09-29 op
484 291ecc47 2023-07-02 op if (buf->fd != -1) {
485 83f0f95a 2022-09-29 op close(buf->fd);
486 291ecc47 2023-07-02 op buf->fd = -1;
487 291ecc47 2023-07-02 op }
488 83f0f95a 2022-09-29 op
489 83f0f95a 2022-09-29 op msgbuf->queued--;
490 83f0f95a 2022-09-29 op ibuf_free(buf);
491 83f0f95a 2022-09-29 op }