xref: /freebsd/sbin/hastd/proto_common.c (revision aa24f48b361effe51163877d84f1b70d32b77e04)
1 /*-
2  * Copyright (c) 2009-2010 The FreeBSD Foundation
3  * Copyright (c) 2011 Pawel Jakub Dawidek <pawel@dawidek.net>
4  * All rights reserved.
5  *
6  * This software was developed by Pawel Jakub Dawidek under sponsorship from
7  * the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdbool.h>
40 #include <stdlib.h>
41 #include <strings.h>
42 #include <unistd.h>
43 
44 #include "pjdlog.h"
45 #include "proto_impl.h"
46 
47 /* Maximum size of packet we want to use when sending data. */
48 #ifndef MAX_SEND_SIZE
49 #define	MAX_SEND_SIZE	32768
50 #endif
51 
52 static bool
53 blocking_socket(int sock)
54 {
55 	int flags;
56 
57 	flags = fcntl(sock, F_GETFL);
58 	PJDLOG_ASSERT(flags >= 0);
59 	return ((flags & O_NONBLOCK) == 0);
60 }
61 
62 static int
63 proto_descriptor_send(int sock, int fd)
64 {
65 	unsigned char ctrl[CMSG_SPACE(sizeof(fd))];
66 	struct msghdr msg;
67 	struct cmsghdr *cmsg;
68 
69 	PJDLOG_ASSERT(sock >= 0);
70 	PJDLOG_ASSERT(fd >= 0);
71 
72 	bzero(&msg, sizeof(msg));
73 	bzero(&ctrl, sizeof(ctrl));
74 
75 	msg.msg_iov = NULL;
76 	msg.msg_iovlen = 0;
77 	msg.msg_control = ctrl;
78 	msg.msg_controllen = sizeof(ctrl);
79 
80 	cmsg = CMSG_FIRSTHDR(&msg);
81 	cmsg->cmsg_level = SOL_SOCKET;
82 	cmsg->cmsg_type = SCM_RIGHTS;
83 	cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
84 	bcopy(&fd, CMSG_DATA(cmsg), sizeof(fd));
85 
86 	if (sendmsg(sock, &msg, 0) == -1)
87 		return (errno);
88 
89 	return (0);
90 }
91 
92 int
93 proto_common_send(int sock, const unsigned char *data, size_t size, int fd)
94 {
95 	ssize_t done;
96 	size_t sendsize;
97 	int errcount = 0;
98 
99 	PJDLOG_ASSERT(sock >= 0);
100 
101 	if (data == NULL) {
102 		/* The caller is just trying to decide about direction. */
103 
104 		PJDLOG_ASSERT(size == 0);
105 
106 		if (shutdown(sock, SHUT_RD) == -1)
107 			return (errno);
108 		return (0);
109 	}
110 
111 	PJDLOG_ASSERT(data != NULL);
112 	PJDLOG_ASSERT(size > 0);
113 
114 	do {
115 		sendsize = size < MAX_SEND_SIZE ? size : MAX_SEND_SIZE;
116 		done = send(sock, data, sendsize, MSG_NOSIGNAL);
117 		if (done == 0) {
118 			return (ENOTCONN);
119 		} else if (done == -1) {
120 			if (errno == EINTR)
121 				continue;
122 			if (errno == ENOBUFS) {
123 				/*
124 				 * If there are no buffers we retry.
125 				 * After each try we increase delay before the
126 				 * next one and we give up after fifteen times.
127 				 * This gives 11s of total wait time.
128 				 */
129 				if (errcount == 15) {
130 					pjdlog_warning("Getting ENOBUFS errors for 11s on send(), giving up.");
131 				} else {
132 					if (errcount == 0)
133 						pjdlog_warning("Got ENOBUFS error on send(), retrying for a bit.");
134 					errcount++;
135 					usleep(100000 * errcount);
136 					continue;
137 				}
138 			}
139 			/*
140 			 * If this is blocking socket and we got EAGAIN, this
141 			 * means the request timed out. Translate errno to
142 			 * ETIMEDOUT, to give administrator a hint to
143 			 * eventually increase timeout.
144 			 */
145 			if (errno == EAGAIN && blocking_socket(sock))
146 				errno = ETIMEDOUT;
147 			return (errno);
148 		}
149 		data += done;
150 		size -= done;
151 	} while (size > 0);
152 	if (errcount > 0) {
153 		pjdlog_info("Data sent successfully after %d ENOBUFS error%s.",
154 		    errcount, errcount == 1 ? "" : "s");
155 	}
156 
157 	if (fd == -1)
158 		return (0);
159 	return (proto_descriptor_send(sock, fd));
160 }
161 
162 static int
163 proto_descriptor_recv(int sock, int *fdp)
164 {
165 	unsigned char ctrl[CMSG_SPACE(sizeof(*fdp))];
166 	struct msghdr msg;
167 	struct cmsghdr *cmsg;
168 
169 	PJDLOG_ASSERT(sock >= 0);
170 	PJDLOG_ASSERT(fdp != NULL);
171 
172 	bzero(&msg, sizeof(msg));
173 	bzero(&ctrl, sizeof(ctrl));
174 
175 	msg.msg_iov = NULL;
176 	msg.msg_iovlen = 0;
177 	msg.msg_control = ctrl;
178 	msg.msg_controllen = sizeof(ctrl);
179 
180 	if (recvmsg(sock, &msg, 0) == -1)
181 		return (errno);
182 
183 	cmsg = CMSG_FIRSTHDR(&msg);
184 	if (cmsg == NULL || cmsg->cmsg_level != SOL_SOCKET ||
185 	    cmsg->cmsg_type != SCM_RIGHTS) {
186 		return (EINVAL);
187 	}
188 	bcopy(CMSG_DATA(cmsg), fdp, sizeof(*fdp));
189 
190 	return (0);
191 }
192 
193 int
194 proto_common_recv(int sock, unsigned char *data, size_t size, int *fdp)
195 {
196 	ssize_t done;
197 
198 	PJDLOG_ASSERT(sock >= 0);
199 
200 	if (data == NULL) {
201 		/* The caller is just trying to decide about direction. */
202 
203 		PJDLOG_ASSERT(size == 0);
204 
205 		if (shutdown(sock, SHUT_WR) == -1)
206 			return (errno);
207 		return (0);
208 	}
209 
210 	PJDLOG_ASSERT(data != NULL);
211 	PJDLOG_ASSERT(size > 0);
212 
213 	do {
214 		done = recv(sock, data, size, MSG_WAITALL);
215 	} while (done == -1 && errno == EINTR);
216 	if (done == 0) {
217 		return (ENOTCONN);
218 	} else if (done == -1) {
219 		/*
220 		 * If this is blocking socket and we got EAGAIN, this
221 		 * means the request timed out. Translate errno to
222 		 * ETIMEDOUT, to give administrator a hint to
223 		 * eventually increase timeout.
224 		 */
225 		if (errno == EAGAIN && blocking_socket(sock))
226 			errno = ETIMEDOUT;
227 		return (errno);
228 	}
229 	if (fdp == NULL)
230 		return (0);
231 	return (proto_descriptor_recv(sock, fdp));
232 }
233