msgio.c revision 1.2.6.2 1 1.2.6.2 christos /* $NetBSD: msgio.c,v 1.2.6.2 2019/06/10 22:08:38 christos Exp $ */
2 1.2.6.2 christos
3 1.2.6.2 christos /*-
4 1.2.6.2 christos * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 1.2.6.2 christos *
6 1.2.6.2 christos * Copyright (c) 2013 The FreeBSD Foundation
7 1.2.6.2 christos * Copyright (c) 2013 Mariusz Zaborski <oshogbo (at) FreeBSD.org>
8 1.2.6.2 christos * All rights reserved.
9 1.2.6.2 christos *
10 1.2.6.2 christos * This software was developed by Pawel Jakub Dawidek under sponsorship from
11 1.2.6.2 christos * the FreeBSD Foundation.
12 1.2.6.2 christos *
13 1.2.6.2 christos * Redistribution and use in source and binary forms, with or without
14 1.2.6.2 christos * modification, are permitted provided that the following conditions
15 1.2.6.2 christos * are met:
16 1.2.6.2 christos * 1. Redistributions of source code must retain the above copyright
17 1.2.6.2 christos * notice, this list of conditions and the following disclaimer.
18 1.2.6.2 christos * 2. Redistributions in binary form must reproduce the above copyright
19 1.2.6.2 christos * notice, this list of conditions and the following disclaimer in the
20 1.2.6.2 christos * documentation and/or other materials provided with the distribution.
21 1.2.6.2 christos *
22 1.2.6.2 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
23 1.2.6.2 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 1.2.6.2 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 1.2.6.2 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
26 1.2.6.2 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 1.2.6.2 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 1.2.6.2 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 1.2.6.2 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 1.2.6.2 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.2.6.2 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 1.2.6.2 christos * SUCH DAMAGE.
33 1.2.6.2 christos */
34 1.2.6.2 christos
35 1.2.6.2 christos #include <sys/cdefs.h>
36 1.2.6.2 christos #ifdef __FreeBSD__
37 1.2.6.2 christos __FBSDID("$FreeBSD: head/lib/libnv/msgio.c 326219 2017-11-26 02:00:33Z pfg $");
38 1.2.6.2 christos #else
39 1.2.6.2 christos __RCSID("$NetBSD: msgio.c,v 1.2.6.2 2019/06/10 22:08:38 christos Exp $");
40 1.2.6.2 christos #endif
41 1.2.6.2 christos
42 1.2.6.2 christos #include <sys/param.h>
43 1.2.6.2 christos #include <sys/socket.h>
44 1.2.6.2 christos #include <sys/select.h>
45 1.2.6.2 christos
46 1.2.6.2 christos #include <errno.h>
47 1.2.6.2 christos #include <fcntl.h>
48 1.2.6.2 christos #include <stdbool.h>
49 1.2.6.2 christos #include <stdint.h>
50 1.2.6.2 christos #include <stdlib.h>
51 1.2.6.2 christos #include <string.h>
52 1.2.6.2 christos #include <unistd.h>
53 1.2.6.2 christos
54 1.2.6.2 christos #ifdef HAVE_PJDLOG
55 1.2.6.2 christos #include <pjdlog.h>
56 1.2.6.2 christos #endif
57 1.2.6.2 christos
58 1.2.6.2 christos #include "common_impl.h"
59 1.2.6.2 christos #include "msgio.h"
60 1.2.6.2 christos
61 1.2.6.2 christos #ifndef HAVE_PJDLOG
62 1.2.6.2 christos #include <assert.h>
63 1.2.6.2 christos #define PJDLOG_ASSERT(...) assert(__VA_ARGS__)
64 1.2.6.2 christos #define PJDLOG_RASSERT(expr, ...) assert(expr)
65 1.2.6.2 christos #define PJDLOG_ABORT(...) abort()
66 1.2.6.2 christos #endif
67 1.2.6.2 christos
68 1.2.6.2 christos #ifdef __linux__
69 1.2.6.2 christos /* Linux: arbitrary size, but must be lower than SCM_MAX_FD. */
70 1.2.6.2 christos #define PKG_MAX_SIZE ((64U - 1) * CMSG_SPACE(sizeof(int)))
71 1.2.6.2 christos #else
72 1.2.6.2 christos #define PKG_MAX_SIZE (MCLBYTES / CMSG_SPACE(sizeof(int)) - 1)
73 1.2.6.2 christos #endif
74 1.2.6.2 christos
75 1.2.6.2 christos static int
76 1.2.6.2 christos msghdr_add_fd(struct cmsghdr *cmsg, int fd)
77 1.2.6.2 christos {
78 1.2.6.2 christos
79 1.2.6.2 christos PJDLOG_ASSERT(fd >= 0);
80 1.2.6.2 christos
81 1.2.6.2 christos if (!fd_is_valid(fd)) {
82 1.2.6.2 christos errno = EBADF;
83 1.2.6.2 christos return (-1);
84 1.2.6.2 christos }
85 1.2.6.2 christos
86 1.2.6.2 christos cmsg->cmsg_level = SOL_SOCKET;
87 1.2.6.2 christos cmsg->cmsg_type = SCM_RIGHTS;
88 1.2.6.2 christos cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
89 1.2.6.2 christos bcopy(&fd, CMSG_DATA(cmsg), sizeof(fd));
90 1.2.6.2 christos
91 1.2.6.2 christos return (0);
92 1.2.6.2 christos }
93 1.2.6.2 christos
94 1.2.6.2 christos static int
95 1.2.6.2 christos msghdr_get_fd(struct cmsghdr *cmsg)
96 1.2.6.2 christos {
97 1.2.6.2 christos int fd;
98 1.2.6.2 christos
99 1.2.6.2 christos if (cmsg == NULL || cmsg->cmsg_level != SOL_SOCKET ||
100 1.2.6.2 christos cmsg->cmsg_type != SCM_RIGHTS ||
101 1.2.6.2 christos cmsg->cmsg_len != CMSG_LEN(sizeof(fd))) {
102 1.2.6.2 christos errno = EINVAL;
103 1.2.6.2 christos return (-1);
104 1.2.6.2 christos }
105 1.2.6.2 christos
106 1.2.6.2 christos bcopy(CMSG_DATA(cmsg), &fd, sizeof(fd));
107 1.2.6.2 christos #ifndef MSG_CMSG_CLOEXEC
108 1.2.6.2 christos /*
109 1.2.6.2 christos * If the MSG_CMSG_CLOEXEC flag is not available we cannot set the
110 1.2.6.2 christos * close-on-exec flag atomically, but we still want to set it for
111 1.2.6.2 christos * consistency.
112 1.2.6.2 christos */
113 1.2.6.2 christos (void) fcntl(fd, F_SETFD, FD_CLOEXEC);
114 1.2.6.2 christos #endif
115 1.2.6.2 christos
116 1.2.6.2 christos return (fd);
117 1.2.6.2 christos }
118 1.2.6.2 christos
119 1.2.6.2 christos static void
120 1.2.6.2 christos fd_wait(int fd, bool doread)
121 1.2.6.2 christos {
122 1.2.6.2 christos fd_set fds;
123 1.2.6.2 christos
124 1.2.6.2 christos PJDLOG_ASSERT(fd >= 0);
125 1.2.6.2 christos
126 1.2.6.2 christos FD_ZERO(&fds);
127 1.2.6.2 christos FD_SET(fd, &fds);
128 1.2.6.2 christos (void)select(fd + 1, doread ? &fds : NULL, doread ? NULL : &fds,
129 1.2.6.2 christos NULL, NULL);
130 1.2.6.2 christos }
131 1.2.6.2 christos
132 1.2.6.2 christos static int
133 1.2.6.2 christos msg_recv(int sock, struct msghdr *msg)
134 1.2.6.2 christos {
135 1.2.6.2 christos int flags;
136 1.2.6.2 christos
137 1.2.6.2 christos PJDLOG_ASSERT(sock >= 0);
138 1.2.6.2 christos
139 1.2.6.2 christos #ifdef MSG_CMSG_CLOEXEC
140 1.2.6.2 christos flags = MSG_CMSG_CLOEXEC;
141 1.2.6.2 christos #else
142 1.2.6.2 christos flags = 0;
143 1.2.6.2 christos #endif
144 1.2.6.2 christos
145 1.2.6.2 christos for (;;) {
146 1.2.6.2 christos fd_wait(sock, true);
147 1.2.6.2 christos if (recvmsg(sock, msg, flags) == -1) {
148 1.2.6.2 christos if (errno == EINTR)
149 1.2.6.2 christos continue;
150 1.2.6.2 christos return (-1);
151 1.2.6.2 christos }
152 1.2.6.2 christos break;
153 1.2.6.2 christos }
154 1.2.6.2 christos
155 1.2.6.2 christos return (0);
156 1.2.6.2 christos }
157 1.2.6.2 christos
158 1.2.6.2 christos static int
159 1.2.6.2 christos msg_send(int sock, const struct msghdr *msg)
160 1.2.6.2 christos {
161 1.2.6.2 christos
162 1.2.6.2 christos PJDLOG_ASSERT(sock >= 0);
163 1.2.6.2 christos
164 1.2.6.2 christos for (;;) {
165 1.2.6.2 christos fd_wait(sock, false);
166 1.2.6.2 christos if (sendmsg(sock, msg, 0) == -1) {
167 1.2.6.2 christos if (errno == EINTR)
168 1.2.6.2 christos continue;
169 1.2.6.2 christos return (-1);
170 1.2.6.2 christos }
171 1.2.6.2 christos break;
172 1.2.6.2 christos }
173 1.2.6.2 christos
174 1.2.6.2 christos return (0);
175 1.2.6.2 christos }
176 1.2.6.2 christos
177 1.2.6.2 christos #ifdef __FreeBSD__
178 1.2.6.2 christos int
179 1.2.6.2 christos cred_send(int sock)
180 1.2.6.2 christos {
181 1.2.6.2 christos unsigned char credbuf[CMSG_SPACE(sizeof(struct cmsgcred))];
182 1.2.6.2 christos struct msghdr msg;
183 1.2.6.2 christos struct cmsghdr *cmsg;
184 1.2.6.2 christos struct iovec iov;
185 1.2.6.2 christos uint8_t dummy;
186 1.2.6.2 christos
187 1.2.6.2 christos bzero(credbuf, sizeof(credbuf));
188 1.2.6.2 christos bzero(&msg, sizeof(msg));
189 1.2.6.2 christos bzero(&iov, sizeof(iov));
190 1.2.6.2 christos
191 1.2.6.2 christos /*
192 1.2.6.2 christos * XXX: We send one byte along with the control message, because
193 1.2.6.2 christos * setting msg_iov to NULL only works if this is the first
194 1.2.6.2 christos * packet send over the socket. Once we send some data we
195 1.2.6.2 christos * won't be able to send credentials anymore. This is most
196 1.2.6.2 christos * likely a kernel bug.
197 1.2.6.2 christos */
198 1.2.6.2 christos dummy = 0;
199 1.2.6.2 christos iov.iov_base = &dummy;
200 1.2.6.2 christos iov.iov_len = sizeof(dummy);
201 1.2.6.2 christos
202 1.2.6.2 christos msg.msg_iov = &iov;
203 1.2.6.2 christos msg.msg_iovlen = 1;
204 1.2.6.2 christos msg.msg_control = credbuf;
205 1.2.6.2 christos msg.msg_controllen = sizeof(credbuf);
206 1.2.6.2 christos
207 1.2.6.2 christos cmsg = CMSG_FIRSTHDR(&msg);
208 1.2.6.2 christos cmsg->cmsg_len = CMSG_LEN(sizeof(struct cmsgcred));
209 1.2.6.2 christos cmsg->cmsg_level = SOL_SOCKET;
210 1.2.6.2 christos cmsg->cmsg_type = SCM_CREDS;
211 1.2.6.2 christos
212 1.2.6.2 christos if (msg_send(sock, &msg) == -1)
213 1.2.6.2 christos return (-1);
214 1.2.6.2 christos
215 1.2.6.2 christos return (0);
216 1.2.6.2 christos }
217 1.2.6.2 christos
218 1.2.6.2 christos int
219 1.2.6.2 christos cred_recv(int sock, struct cmsgcred *cred)
220 1.2.6.2 christos {
221 1.2.6.2 christos unsigned char credbuf[CMSG_SPACE(sizeof(struct cmsgcred))];
222 1.2.6.2 christos struct msghdr msg;
223 1.2.6.2 christos struct cmsghdr *cmsg;
224 1.2.6.2 christos struct iovec iov;
225 1.2.6.2 christos uint8_t dummy;
226 1.2.6.2 christos
227 1.2.6.2 christos bzero(credbuf, sizeof(credbuf));
228 1.2.6.2 christos bzero(&msg, sizeof(msg));
229 1.2.6.2 christos bzero(&iov, sizeof(iov));
230 1.2.6.2 christos
231 1.2.6.2 christos iov.iov_base = &dummy;
232 1.2.6.2 christos iov.iov_len = sizeof(dummy);
233 1.2.6.2 christos
234 1.2.6.2 christos msg.msg_iov = &iov;
235 1.2.6.2 christos msg.msg_iovlen = 1;
236 1.2.6.2 christos msg.msg_control = credbuf;
237 1.2.6.2 christos msg.msg_controllen = sizeof(credbuf);
238 1.2.6.2 christos
239 1.2.6.2 christos if (msg_recv(sock, &msg) == -1)
240 1.2.6.2 christos return (-1);
241 1.2.6.2 christos
242 1.2.6.2 christos cmsg = CMSG_FIRSTHDR(&msg);
243 1.2.6.2 christos if (cmsg == NULL ||
244 1.2.6.2 christos cmsg->cmsg_len != CMSG_LEN(sizeof(struct cmsgcred)) ||
245 1.2.6.2 christos cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_CREDS) {
246 1.2.6.2 christos errno = EINVAL;
247 1.2.6.2 christos return (-1);
248 1.2.6.2 christos }
249 1.2.6.2 christos bcopy(CMSG_DATA(cmsg), cred, sizeof(*cred));
250 1.2.6.2 christos
251 1.2.6.2 christos return (0);
252 1.2.6.2 christos }
253 1.2.6.2 christos #endif
254 1.2.6.2 christos
255 1.2.6.2 christos static int
256 1.2.6.2 christos fd_package_send(int sock, const int *fds, size_t nfds)
257 1.2.6.2 christos {
258 1.2.6.2 christos struct msghdr msg;
259 1.2.6.2 christos struct cmsghdr *cmsg;
260 1.2.6.2 christos struct iovec iov;
261 1.2.6.2 christos unsigned int i;
262 1.2.6.2 christos int serrno, ret;
263 1.2.6.2 christos uint8_t dummy;
264 1.2.6.2 christos
265 1.2.6.2 christos PJDLOG_ASSERT(sock >= 0);
266 1.2.6.2 christos PJDLOG_ASSERT(fds != NULL);
267 1.2.6.2 christos PJDLOG_ASSERT(nfds > 0);
268 1.2.6.2 christos
269 1.2.6.2 christos bzero(&msg, sizeof(msg));
270 1.2.6.2 christos
271 1.2.6.2 christos /*
272 1.2.6.2 christos * XXX: Look into cred_send function for more details.
273 1.2.6.2 christos */
274 1.2.6.2 christos dummy = 0;
275 1.2.6.2 christos iov.iov_base = &dummy;
276 1.2.6.2 christos iov.iov_len = sizeof(dummy);
277 1.2.6.2 christos
278 1.2.6.2 christos msg.msg_iov = &iov;
279 1.2.6.2 christos msg.msg_iovlen = 1;
280 1.2.6.2 christos msg.msg_controllen = nfds * CMSG_SPACE(sizeof(int));
281 1.2.6.2 christos msg.msg_control = calloc(1, msg.msg_controllen);
282 1.2.6.2 christos if (msg.msg_control == NULL)
283 1.2.6.2 christos return (-1);
284 1.2.6.2 christos
285 1.2.6.2 christos ret = -1;
286 1.2.6.2 christos
287 1.2.6.2 christos for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL;
288 1.2.6.2 christos i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
289 1.2.6.2 christos if (msghdr_add_fd(cmsg, fds[i]) == -1)
290 1.2.6.2 christos goto end;
291 1.2.6.2 christos }
292 1.2.6.2 christos
293 1.2.6.2 christos if (msg_send(sock, &msg) == -1)
294 1.2.6.2 christos goto end;
295 1.2.6.2 christos
296 1.2.6.2 christos ret = 0;
297 1.2.6.2 christos end:
298 1.2.6.2 christos serrno = errno;
299 1.2.6.2 christos free(msg.msg_control);
300 1.2.6.2 christos errno = serrno;
301 1.2.6.2 christos return (ret);
302 1.2.6.2 christos }
303 1.2.6.2 christos
304 1.2.6.2 christos static int
305 1.2.6.2 christos fd_package_recv(int sock, int *fds, size_t nfds)
306 1.2.6.2 christos {
307 1.2.6.2 christos struct msghdr msg;
308 1.2.6.2 christos struct cmsghdr *cmsg;
309 1.2.6.2 christos unsigned int i;
310 1.2.6.2 christos int serrno, ret;
311 1.2.6.2 christos struct iovec iov;
312 1.2.6.2 christos uint8_t dummy;
313 1.2.6.2 christos
314 1.2.6.2 christos PJDLOG_ASSERT(sock >= 0);
315 1.2.6.2 christos PJDLOG_ASSERT(nfds > 0);
316 1.2.6.2 christos PJDLOG_ASSERT(fds != NULL);
317 1.2.6.2 christos
318 1.2.6.2 christos bzero(&msg, sizeof(msg));
319 1.2.6.2 christos bzero(&iov, sizeof(iov));
320 1.2.6.2 christos
321 1.2.6.2 christos /*
322 1.2.6.2 christos * XXX: Look into cred_send function for more details.
323 1.2.6.2 christos */
324 1.2.6.2 christos iov.iov_base = &dummy;
325 1.2.6.2 christos iov.iov_len = sizeof(dummy);
326 1.2.6.2 christos
327 1.2.6.2 christos msg.msg_iov = &iov;
328 1.2.6.2 christos msg.msg_iovlen = 1;
329 1.2.6.2 christos msg.msg_controllen = nfds * CMSG_SPACE(sizeof(int));
330 1.2.6.2 christos msg.msg_control = calloc(1, msg.msg_controllen);
331 1.2.6.2 christos if (msg.msg_control == NULL)
332 1.2.6.2 christos return (-1);
333 1.2.6.2 christos
334 1.2.6.2 christos ret = -1;
335 1.2.6.2 christos
336 1.2.6.2 christos if (msg_recv(sock, &msg) == -1)
337 1.2.6.2 christos goto end;
338 1.2.6.2 christos
339 1.2.6.2 christos for (i = 0, cmsg = CMSG_FIRSTHDR(&msg); i < nfds && cmsg != NULL;
340 1.2.6.2 christos i++, cmsg = CMSG_NXTHDR(&msg, cmsg)) {
341 1.2.6.2 christos fds[i] = msghdr_get_fd(cmsg);
342 1.2.6.2 christos if (fds[i] < 0)
343 1.2.6.2 christos break;
344 1.2.6.2 christos }
345 1.2.6.2 christos
346 1.2.6.2 christos if (cmsg != NULL || i < nfds) {
347 1.2.6.2 christos int fd;
348 1.2.6.2 christos
349 1.2.6.2 christos /*
350 1.2.6.2 christos * We need to close all received descriptors, even if we have
351 1.2.6.2 christos * different control message (eg. SCM_CREDS) in between.
352 1.2.6.2 christos */
353 1.2.6.2 christos for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
354 1.2.6.2 christos cmsg = CMSG_NXTHDR(&msg, cmsg)) {
355 1.2.6.2 christos fd = msghdr_get_fd(cmsg);
356 1.2.6.2 christos if (fd >= 0)
357 1.2.6.2 christos close(fd);
358 1.2.6.2 christos }
359 1.2.6.2 christos errno = EINVAL;
360 1.2.6.2 christos goto end;
361 1.2.6.2 christos }
362 1.2.6.2 christos
363 1.2.6.2 christos ret = 0;
364 1.2.6.2 christos end:
365 1.2.6.2 christos serrno = errno;
366 1.2.6.2 christos free(msg.msg_control);
367 1.2.6.2 christos errno = serrno;
368 1.2.6.2 christos return (ret);
369 1.2.6.2 christos }
370 1.2.6.2 christos
371 1.2.6.2 christos int
372 1.2.6.2 christos fd_recv(int sock, int *fds, size_t nfds)
373 1.2.6.2 christos {
374 1.2.6.2 christos unsigned int i, step, j;
375 1.2.6.2 christos int ret, serrno;
376 1.2.6.2 christos
377 1.2.6.2 christos if (nfds == 0 || fds == NULL) {
378 1.2.6.2 christos errno = EINVAL;
379 1.2.6.2 christos return (-1);
380 1.2.6.2 christos }
381 1.2.6.2 christos
382 1.2.6.2 christos ret = i = step = 0;
383 1.2.6.2 christos while (i < nfds) {
384 1.2.6.2 christos if (PKG_MAX_SIZE < nfds - i)
385 1.2.6.2 christos step = PKG_MAX_SIZE;
386 1.2.6.2 christos else
387 1.2.6.2 christos step = nfds - i;
388 1.2.6.2 christos ret = fd_package_recv(sock, fds + i, step);
389 1.2.6.2 christos if (ret != 0) {
390 1.2.6.2 christos /* Close all received descriptors. */
391 1.2.6.2 christos serrno = errno;
392 1.2.6.2 christos for (j = 0; j < i; j++)
393 1.2.6.2 christos close(fds[j]);
394 1.2.6.2 christos errno = serrno;
395 1.2.6.2 christos break;
396 1.2.6.2 christos }
397 1.2.6.2 christos i += step;
398 1.2.6.2 christos }
399 1.2.6.2 christos
400 1.2.6.2 christos return (ret);
401 1.2.6.2 christos }
402 1.2.6.2 christos
403 1.2.6.2 christos int
404 1.2.6.2 christos fd_send(int sock, const int *fds, size_t nfds)
405 1.2.6.2 christos {
406 1.2.6.2 christos unsigned int i, step;
407 1.2.6.2 christos int ret;
408 1.2.6.2 christos
409 1.2.6.2 christos if (nfds == 0 || fds == NULL) {
410 1.2.6.2 christos errno = EINVAL;
411 1.2.6.2 christos return (-1);
412 1.2.6.2 christos }
413 1.2.6.2 christos
414 1.2.6.2 christos ret = i = step = 0;
415 1.2.6.2 christos while (i < nfds) {
416 1.2.6.2 christos if (PKG_MAX_SIZE < nfds - i)
417 1.2.6.2 christos step = PKG_MAX_SIZE;
418 1.2.6.2 christos else
419 1.2.6.2 christos step = nfds - i;
420 1.2.6.2 christos ret = fd_package_send(sock, fds + i, step);
421 1.2.6.2 christos if (ret != 0)
422 1.2.6.2 christos break;
423 1.2.6.2 christos i += step;
424 1.2.6.2 christos }
425 1.2.6.2 christos
426 1.2.6.2 christos return (ret);
427 1.2.6.2 christos }
428 1.2.6.2 christos
429 1.2.6.2 christos int
430 1.2.6.2 christos buf_send(int sock, void *buf, size_t size)
431 1.2.6.2 christos {
432 1.2.6.2 christos ssize_t done;
433 1.2.6.2 christos unsigned char *ptr;
434 1.2.6.2 christos
435 1.2.6.2 christos PJDLOG_ASSERT(sock >= 0);
436 1.2.6.2 christos PJDLOG_ASSERT(size > 0);
437 1.2.6.2 christos PJDLOG_ASSERT(buf != NULL);
438 1.2.6.2 christos
439 1.2.6.2 christos ptr = buf;
440 1.2.6.2 christos do {
441 1.2.6.2 christos fd_wait(sock, false);
442 1.2.6.2 christos done = send(sock, ptr, size, 0);
443 1.2.6.2 christos if (done == -1) {
444 1.2.6.2 christos if (errno == EINTR)
445 1.2.6.2 christos continue;
446 1.2.6.2 christos return (-1);
447 1.2.6.2 christos } else if (done == 0) {
448 1.2.6.2 christos errno = ENOTCONN;
449 1.2.6.2 christos return (-1);
450 1.2.6.2 christos }
451 1.2.6.2 christos size -= done;
452 1.2.6.2 christos ptr += done;
453 1.2.6.2 christos } while (size > 0);
454 1.2.6.2 christos
455 1.2.6.2 christos return (0);
456 1.2.6.2 christos }
457 1.2.6.2 christos
458 1.2.6.2 christos int
459 1.2.6.2 christos buf_recv(int sock, void *buf, size_t size)
460 1.2.6.2 christos {
461 1.2.6.2 christos ssize_t done;
462 1.2.6.2 christos unsigned char *ptr;
463 1.2.6.2 christos
464 1.2.6.2 christos PJDLOG_ASSERT(sock >= 0);
465 1.2.6.2 christos PJDLOG_ASSERT(buf != NULL);
466 1.2.6.2 christos
467 1.2.6.2 christos ptr = buf;
468 1.2.6.2 christos while (size > 0) {
469 1.2.6.2 christos fd_wait(sock, true);
470 1.2.6.2 christos done = recv(sock, ptr, size, 0);
471 1.2.6.2 christos if (done == -1) {
472 1.2.6.2 christos if (errno == EINTR)
473 1.2.6.2 christos continue;
474 1.2.6.2 christos return (-1);
475 1.2.6.2 christos } else if (done == 0) {
476 1.2.6.2 christos errno = ENOTCONN;
477 1.2.6.2 christos return (-1);
478 1.2.6.2 christos }
479 1.2.6.2 christos size -= done;
480 1.2.6.2 christos ptr += done;
481 1.2.6.2 christos }
482 1.2.6.2 christos
483 1.2.6.2 christos return (0);
484 1.2.6.2 christos }
485