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/types.h>
32 #include <sys/socket.h>
33
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <stdbool.h>
37 #include <stdlib.h>
38 #include <strings.h>
39 #include <unistd.h>
40
41 #include <compat/compat.h>
42
43 #include "pjdlog.h"
44 #include "proto_impl.h"
45
46 /* Maximum size of packet we want to use when sending data. */
47 #ifndef MAX_SEND_SIZE
48 #define MAX_SEND_SIZE 32768
49 #endif
50
51 static bool
blocking_socket(int sock)52 blocking_socket(int sock)
53 {
54 int flags;
55
56 flags = fcntl(sock, F_GETFL);
57 PJDLOG_ASSERT(flags >= 0);
58 return ((flags & O_NONBLOCK) == 0);
59 }
60
61 static int
proto_descriptor_send(int sock,int fd)62 proto_descriptor_send(int sock, int fd)
63 {
64 unsigned char ctrl[CMSG_SPACE(sizeof(fd))];
65 struct msghdr msg;
66 struct cmsghdr *cmsg;
67
68 PJDLOG_ASSERT(sock >= 0);
69 PJDLOG_ASSERT(fd >= 0);
70
71 bzero(&msg, sizeof(msg));
72 bzero(&ctrl, sizeof(ctrl));
73
74 msg.msg_iov = NULL;
75 msg.msg_iovlen = 0;
76 msg.msg_control = ctrl;
77 msg.msg_controllen = sizeof(ctrl);
78
79 cmsg = CMSG_FIRSTHDR(&msg);
80 cmsg->cmsg_level = SOL_SOCKET;
81 cmsg->cmsg_type = SCM_RIGHTS;
82 cmsg->cmsg_len = CMSG_LEN(sizeof(fd));
83 bcopy(&fd, CMSG_DATA(cmsg), sizeof(fd));
84
85 if (sendmsg(sock, &msg, 0) == -1)
86 return (errno);
87
88 return (0);
89 }
90
91 int
proto_common_send(int sock,const unsigned char * data,size_t size,int fd)92 proto_common_send(int sock, const unsigned char *data, size_t size, int fd)
93 {
94 ssize_t done;
95 size_t sendsize;
96 int errcount = 0;
97
98 PJDLOG_ASSERT(sock >= 0);
99
100 if (data == NULL) {
101 /* The caller is just trying to decide about direction. */
102
103 PJDLOG_ASSERT(size == 0);
104
105 if (shutdown(sock, SHUT_RD) == -1)
106 return (errno);
107 return (0);
108 }
109
110 PJDLOG_ASSERT(data != NULL);
111 PJDLOG_ASSERT(size > 0);
112
113 do {
114 sendsize = size < MAX_SEND_SIZE ? size : MAX_SEND_SIZE;
115 done = send(sock, data, sendsize, MSG_NOSIGNAL);
116 if (done == 0) {
117 return (ENOTCONN);
118 } else if (done < 0) {
119 if (errno == EINTR)
120 continue;
121 if (errno == ENOBUFS) {
122 /*
123 * If there are no buffers we retry.
124 * After each try we increase delay before the
125 * next one and we give up after fifteen times.
126 * This gives 11s of total wait time.
127 */
128 if (errcount == 15) {
129 pjdlog_warning("Getting ENOBUFS errors for 11s on send(), giving up.");
130 } else {
131 if (errcount == 0)
132 pjdlog_warning("Got ENOBUFS error on send(), retrying for a bit.");
133 errcount++;
134 usleep(100000 * errcount);
135 continue;
136 }
137 }
138 /*
139 * If this is blocking socket and we got EAGAIN, this
140 * means the request timed out. Translate errno to
141 * ETIMEDOUT, to give administrator a hint to
142 * eventually increase timeout.
143 */
144 if (errno == EAGAIN && blocking_socket(sock))
145 errno = ETIMEDOUT;
146 return (errno);
147 }
148 data += done;
149 size -= done;
150 } while (size > 0);
151 if (errcount > 0) {
152 pjdlog_info("Data sent successfully after %d ENOBUFS error%s.",
153 errcount, errcount == 1 ? "" : "s");
154 }
155
156 if (fd == -1)
157 return (0);
158 return (proto_descriptor_send(sock, fd));
159 }
160
161 static int
proto_descriptor_recv(int sock,int * fdp)162 proto_descriptor_recv(int sock, int *fdp)
163 {
164 unsigned char ctrl[CMSG_SPACE(sizeof(*fdp))];
165 struct msghdr msg;
166 struct cmsghdr *cmsg;
167
168 PJDLOG_ASSERT(sock >= 0);
169 PJDLOG_ASSERT(fdp != NULL);
170
171 bzero(&msg, sizeof(msg));
172 bzero(&ctrl, sizeof(ctrl));
173
174 msg.msg_iov = NULL;
175 msg.msg_iovlen = 0;
176 msg.msg_control = ctrl;
177 msg.msg_controllen = sizeof(ctrl);
178
179 if (recvmsg(sock, &msg, 0) == -1)
180 return (errno);
181
182 cmsg = CMSG_FIRSTHDR(&msg);
183 if (cmsg->cmsg_level != SOL_SOCKET ||
184 cmsg->cmsg_type != SCM_RIGHTS) {
185 return (EINVAL);
186 }
187 bcopy(CMSG_DATA(cmsg), fdp, sizeof(*fdp));
188
189 return (0);
190 }
191
192 int
proto_common_recv(int sock,unsigned char * data,size_t size,int * fdp)193 proto_common_recv(int sock, unsigned char *data, size_t size, int *fdp)
194 {
195 ssize_t done;
196
197 PJDLOG_ASSERT(sock >= 0);
198
199 if (data == NULL) {
200 /* The caller is just trying to decide about direction. */
201
202 PJDLOG_ASSERT(size == 0);
203
204 if (shutdown(sock, SHUT_WR) == -1)
205 return (errno);
206 return (0);
207 }
208
209 PJDLOG_ASSERT(data != NULL);
210 PJDLOG_ASSERT(size > 0);
211
212 do {
213 done = recv(sock, data, size, MSG_WAITALL);
214 } while (done == -1 && errno == EINTR);
215 if (done == 0) {
216 return (ENOTCONN);
217 } else if (done < 0) {
218 /*
219 * If this is blocking socket and we got EAGAIN, this
220 * means the request timed out. Translate errno to
221 * ETIMEDOUT, to give administrator a hint to
222 * eventually increase timeout.
223 */
224 if (errno == EAGAIN && blocking_socket(sock))
225 errno = ETIMEDOUT;
226 return (errno);
227 }
228 if (fdp == NULL)
229 return (0);
230 return (proto_descriptor_recv(sock, fdp));
231 }
232