2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009-2010 The FreeBSD Foundation
5 * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
8 * This software was developed by Pawel Jakub Dawidek under sponsorship from
9 * the FreeBSD Foundation.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/types.h>
37 #include <sys/socket.h>
47 #include "proto_impl.h"
49 /* Maximum size of packet we want to use when sending data. */
51 #define MAX_SEND_SIZE 32768
55 blocking_socket(int sock)
59 flags = fcntl(sock, F_GETFL);
60 PJDLOG_ASSERT(flags >= 0);
61 return ((flags & O_NONBLOCK) == 0);
65 proto_descriptor_send(int sock, int fd)
67 unsigned char ctrl[CMSG_SPACE(sizeof(fd))];
71 PJDLOG_ASSERT(sock >= 0);
72 PJDLOG_ASSERT(fd >= 0);
74 bzero(&msg, sizeof(msg));
75 bzero(&ctrl, sizeof(ctrl));
79 msg.msg_control = ctrl;
80 msg.msg_controllen = sizeof(ctrl);
82 cmsg = CMSG_FIRSTHDR(&msg);
83 cmsg->cmsg_level = SOL_SOCKET;
84 cmsg->cmsg_type = SCM_RIGHTS;
85 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
86 bcopy(&fd, CMSG_DATA(cmsg), sizeof(fd));
88 if (sendmsg(sock, &msg, 0) == -1)
95 proto_common_send(int sock, const unsigned char *data, size_t size, int fd)
101 PJDLOG_ASSERT(sock >= 0);
104 /* The caller is just trying to decide about direction. */
106 PJDLOG_ASSERT(size == 0);
108 if (shutdown(sock, SHUT_RD) == -1)
113 PJDLOG_ASSERT(data != NULL);
114 PJDLOG_ASSERT(size > 0);
117 sendsize = size < MAX_SEND_SIZE ? size : MAX_SEND_SIZE;
118 done = send(sock, data, sendsize, MSG_NOSIGNAL);
121 } else if (done == -1) {
124 if (errno == ENOBUFS) {
126 * If there are no buffers we retry.
127 * After each try we increase delay before the
128 * next one and we give up after fifteen times.
129 * This gives 11s of total wait time.
131 if (errcount == 15) {
132 pjdlog_warning("Getting ENOBUFS errors for 11s on send(), giving up.");
135 pjdlog_warning("Got ENOBUFS error on send(), retrying for a bit.");
137 usleep(100000 * errcount);
142 * If this is blocking socket and we got EAGAIN, this
143 * means the request timed out. Translate errno to
144 * ETIMEDOUT, to give administrator a hint to
145 * eventually increase timeout.
147 if (errno == EAGAIN && blocking_socket(sock))
155 pjdlog_info("Data sent successfully after %d ENOBUFS error%s.",
156 errcount, errcount == 1 ? "" : "s");
161 return (proto_descriptor_send(sock, fd));
165 proto_descriptor_recv(int sock, int *fdp)
167 unsigned char ctrl[CMSG_SPACE(sizeof(*fdp))];
169 struct cmsghdr *cmsg;
171 PJDLOG_ASSERT(sock >= 0);
172 PJDLOG_ASSERT(fdp != NULL);
174 bzero(&msg, sizeof(msg));
175 bzero(&ctrl, sizeof(ctrl));
179 msg.msg_control = ctrl;
180 msg.msg_controllen = sizeof(ctrl);
182 if (recvmsg(sock, &msg, 0) == -1)
185 cmsg = CMSG_FIRSTHDR(&msg);
186 if (cmsg == NULL || cmsg->cmsg_level != SOL_SOCKET ||
187 cmsg->cmsg_type != SCM_RIGHTS) {
190 bcopy(CMSG_DATA(cmsg), fdp, sizeof(*fdp));
196 proto_common_recv(int sock, unsigned char *data, size_t size, int *fdp)
200 PJDLOG_ASSERT(sock >= 0);
203 /* The caller is just trying to decide about direction. */
205 PJDLOG_ASSERT(size == 0);
207 if (shutdown(sock, SHUT_WR) == -1)
212 PJDLOG_ASSERT(data != NULL);
213 PJDLOG_ASSERT(size > 0);
216 done = recv(sock, data, size, MSG_WAITALL);
217 } while (done == -1 && errno == EINTR);
220 } else if (done == -1) {
222 * If this is blocking socket and we got EAGAIN, this
223 * means the request timed out. Translate errno to
224 * ETIMEDOUT, to give administrator a hint to
225 * eventually increase timeout.
227 if (errno == EAGAIN && blocking_socket(sock))
233 return (proto_descriptor_recv(sock, fdp));