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