xref: /freebsd/sbin/hastd/proto_common.c (revision 499fe48de8938d4c7b0a91e20eb6c16db9d55633)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009-2010 The FreeBSD Foundation
5  * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
6  * All rights reserved.
7  *
8  * This software was developed by Pawel Jakub Dawidek under sponsorship from
9  * the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
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.
19  *
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
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdbool.h>
42 #include <stdlib.h>
43 #include <strings.h>
44 #include <unistd.h>
45 
46 #include "pjdlog.h"
47 #include "proto_impl.h"
48 
49 /* Maximum size of packet we want to use when sending data. */
50 #ifndef MAX_SEND_SIZE
51 #define	MAX_SEND_SIZE	32768
52 #endif
53 
54 static bool
55 blocking_socket(int sock)
56 {
57 	int flags;
58 
59 	flags = fcntl(sock, F_GETFL);
60 	PJDLOG_ASSERT(flags >= 0);
61 	return ((flags & O_NONBLOCK) == 0);
62 }
63 
64 static int
65 proto_descriptor_send(int sock, int fd)
66 {
67 	unsigned char ctrl[CMSG_SPACE(sizeof(fd))];
68 	struct msghdr msg;
69 	struct cmsghdr *cmsg;
70 
71 	PJDLOG_ASSERT(sock >= 0);
72 	PJDLOG_ASSERT(fd >= 0);
73 
74 	bzero(&msg, sizeof(msg));
75 	bzero(&ctrl, sizeof(ctrl));
76 
77 	msg.msg_iov = NULL;
78 	msg.msg_iovlen = 0;
79 	msg.msg_control = ctrl;
80 	msg.msg_controllen = sizeof(ctrl);
81 
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));
87 
88 	if (sendmsg(sock, &msg, 0) == -1)
89 		return (errno);
90 
91 	return (0);
92 }
93 
94 int
95 proto_common_send(int sock, const unsigned char *data, size_t size, int fd)
96 {
97 	ssize_t done;
98 	size_t sendsize;
99 	int errcount = 0;
100 
101 	PJDLOG_ASSERT(sock >= 0);
102 
103 	if (data == NULL) {
104 		/* The caller is just trying to decide about direction. */
105 
106 		PJDLOG_ASSERT(size == 0);
107 
108 		if (shutdown(sock, SHUT_RD) == -1)
109 			return (errno);
110 		return (0);
111 	}
112 
113 	PJDLOG_ASSERT(data != NULL);
114 	PJDLOG_ASSERT(size > 0);
115 
116 	do {
117 		sendsize = size < MAX_SEND_SIZE ? size : MAX_SEND_SIZE;
118 		done = send(sock, data, sendsize, MSG_NOSIGNAL);
119 		if (done == 0) {
120 			return (ENOTCONN);
121 		} else if (done == -1) {
122 			if (errno == EINTR)
123 				continue;
124 			if (errno == ENOBUFS) {
125 				/*
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.
130 				 */
131 				if (errcount == 15) {
132 					pjdlog_warning("Getting ENOBUFS errors for 11s on send(), giving up.");
133 				} else {
134 					if (errcount == 0)
135 						pjdlog_warning("Got ENOBUFS error on send(), retrying for a bit.");
136 					errcount++;
137 					usleep(100000 * errcount);
138 					continue;
139 				}
140 			}
141 			/*
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.
146 			 */
147 			if (errno == EAGAIN && blocking_socket(sock))
148 				errno = ETIMEDOUT;
149 			return (errno);
150 		}
151 		data += done;
152 		size -= done;
153 	} while (size > 0);
154 	if (errcount > 0) {
155 		pjdlog_info("Data sent successfully after %d ENOBUFS error%s.",
156 		    errcount, errcount == 1 ? "" : "s");
157 	}
158 
159 	if (fd == -1)
160 		return (0);
161 	return (proto_descriptor_send(sock, fd));
162 }
163 
164 static int
165 proto_descriptor_recv(int sock, int *fdp)
166 {
167 	unsigned char ctrl[CMSG_SPACE(sizeof(*fdp))];
168 	struct msghdr msg;
169 	struct cmsghdr *cmsg;
170 
171 	PJDLOG_ASSERT(sock >= 0);
172 	PJDLOG_ASSERT(fdp != NULL);
173 
174 	bzero(&msg, sizeof(msg));
175 	bzero(&ctrl, sizeof(ctrl));
176 
177 	msg.msg_iov = NULL;
178 	msg.msg_iovlen = 0;
179 	msg.msg_control = ctrl;
180 	msg.msg_controllen = sizeof(ctrl);
181 
182 	if (recvmsg(sock, &msg, 0) == -1)
183 		return (errno);
184 
185 	cmsg = CMSG_FIRSTHDR(&msg);
186 	if (cmsg == NULL || cmsg->cmsg_level != SOL_SOCKET ||
187 	    cmsg->cmsg_type != SCM_RIGHTS) {
188 		return (EINVAL);
189 	}
190 	bcopy(CMSG_DATA(cmsg), fdp, sizeof(*fdp));
191 
192 	return (0);
193 }
194 
195 int
196 proto_common_recv(int sock, unsigned char *data, size_t size, int *fdp)
197 {
198 	ssize_t done;
199 
200 	PJDLOG_ASSERT(sock >= 0);
201 
202 	if (data == NULL) {
203 		/* The caller is just trying to decide about direction. */
204 
205 		PJDLOG_ASSERT(size == 0);
206 
207 		if (shutdown(sock, SHUT_WR) == -1)
208 			return (errno);
209 		return (0);
210 	}
211 
212 	PJDLOG_ASSERT(data != NULL);
213 	PJDLOG_ASSERT(size > 0);
214 
215 	do {
216 		done = recv(sock, data, size, MSG_WAITALL);
217 	} while (done == -1 && errno == EINTR);
218 	if (done == 0) {
219 		return (ENOTCONN);
220 	} else if (done == -1) {
221 		/*
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.
226 		 */
227 		if (errno == EAGAIN && blocking_socket(sock))
228 			errno = ETIMEDOUT;
229 		return (errno);
230 	}
231 	if (fdp == NULL)
232 		return (0);
233 	return (proto_descriptor_recv(sock, fdp));
234 }
235