Fix "make xcheck" in sunrpc.
[platform/upstream/glibc.git] / sunrpc / svc_tcp.c
1 /*
2  * svc_tcp.c, Server side for TCP/IP based RPC.
3  *
4  * Copyright (c) 2010, Oracle America, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above
13  *       copyright notice, this list of conditions and the following
14  *       disclaimer in the documentation and/or other materials
15  *       provided with the distribution.
16  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Actually implements two flavors of transporter -
34  * a tcp rendezvouser (a listener and connection establisher)
35  * and a record/tcp stream.
36  */
37
38 #include <stdio.h>
39 #include <unistd.h>
40 #include <string.h>
41 #include <libintl.h>
42 #include <rpc/rpc.h>
43 #include <sys/socket.h>
44 #include <sys/poll.h>
45 #include <errno.h>
46 #include <stdlib.h>
47
48 #ifdef USE_IN_LIBIO
49 # include <wchar.h>
50 # include <libio/iolibio.h>
51 #endif
52
53 /*
54  * Ops vector for TCP/IP based rpc service handle
55  */
56 static bool_t svctcp_recv (SVCXPRT *, struct rpc_msg *);
57 static enum xprt_stat svctcp_stat (SVCXPRT *);
58 static bool_t svctcp_getargs (SVCXPRT *, xdrproc_t, caddr_t);
59 static bool_t svctcp_reply (SVCXPRT *, struct rpc_msg *);
60 static bool_t svctcp_freeargs (SVCXPRT *, xdrproc_t, caddr_t);
61 static void svctcp_destroy (SVCXPRT *);
62
63 static const struct xp_ops svctcp_op =
64 {
65   svctcp_recv,
66   svctcp_stat,
67   svctcp_getargs,
68   svctcp_reply,
69   svctcp_freeargs,
70   svctcp_destroy
71 };
72
73 /*
74  * Ops vector for TCP/IP rendezvous handler
75  */
76 static bool_t rendezvous_request (SVCXPRT *, struct rpc_msg *);
77 static enum xprt_stat rendezvous_stat (SVCXPRT *);
78 static void svctcp_rendezvous_abort (void) __attribute__ ((__noreturn__));
79
80 /* This function makes sure abort() relocation goes through PLT
81    and thus can be lazy bound.  */
82 static void
83 svctcp_rendezvous_abort (void)
84 {
85   abort ();
86 };
87
88 static const struct xp_ops svctcp_rendezvous_op =
89 {
90   rendezvous_request,
91   rendezvous_stat,
92   (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
93   (bool_t (*) (SVCXPRT *, struct rpc_msg *)) svctcp_rendezvous_abort,
94   (bool_t (*) (SVCXPRT *, xdrproc_t, caddr_t)) svctcp_rendezvous_abort,
95   svctcp_destroy
96 };
97
98 static int readtcp (char*, char *, int);
99 static int writetcp (char *, char *, int);
100 static SVCXPRT *makefd_xprt (int, u_int, u_int) internal_function;
101
102 struct tcp_rendezvous
103   {                             /* kept in xprt->xp_p1 */
104     u_int sendsize;
105     u_int recvsize;
106   };
107
108 struct tcp_conn
109   {                             /* kept in xprt->xp_p1 */
110     enum xprt_stat strm_stat;
111     u_long x_id;
112     XDR xdrs;
113     char verf_body[MAX_AUTH_BYTES];
114   };
115
116 /*
117  * Usage:
118  *      xprt = svctcp_create(sock, send_buf_size, recv_buf_size);
119  *
120  * Creates, registers, and returns a (rpc) tcp based transporter.
121  * Once *xprt is initialized, it is registered as a transporter
122  * see (svc.h, xprt_register).  This routine returns
123  * a NULL if a problem occurred.
124  *
125  * If sock<0 then a socket is created, else sock is used.
126  * If the socket, sock is not bound to a port then svctcp_create
127  * binds it to an arbitrary port.  The routine then starts a tcp
128  * listener on the socket's associated port.  In any (successful) case,
129  * xprt->xp_sock is the registered socket number and xprt->xp_port is the
130  * associated port number.
131  *
132  * Since tcp streams do buffered io similar to stdio, the caller can specify
133  * how big the send and receive buffers are via the second and third parms;
134  * 0 => use the system default.
135  */
136 SVCXPRT *
137 svctcp_create (int sock, u_int sendsize, u_int recvsize)
138 {
139   bool_t madesock = FALSE;
140   SVCXPRT *xprt;
141   struct tcp_rendezvous *r;
142   struct sockaddr_in addr;
143   socklen_t len = sizeof (struct sockaddr_in);
144
145   if (sock == RPC_ANYSOCK)
146     {
147       if ((sock = __socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
148         {
149           perror (_("svc_tcp.c - tcp socket creation problem"));
150           return (SVCXPRT *) NULL;
151         }
152       madesock = TRUE;
153     }
154   __bzero ((char *) &addr, sizeof (addr));
155   addr.sin_family = AF_INET;
156   if (bindresvport (sock, &addr))
157     {
158       addr.sin_port = 0;
159       (void) __bind (sock, (struct sockaddr *) &addr, len);
160     }
161   if ((__getsockname (sock, (struct sockaddr *) &addr, &len) != 0) ||
162       (__listen (sock, SOMAXCONN) != 0))
163     {
164       perror (_("svc_tcp.c - cannot getsockname or listen"));
165       if (madesock)
166         (void) __close (sock);
167       return (SVCXPRT *) NULL;
168     }
169   r = (struct tcp_rendezvous *) mem_alloc (sizeof (*r));
170   xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
171   if (r == NULL || xprt == NULL)
172     {
173       (void) __fxprintf (NULL, "%s: %s", __func__, _("out of memory\n"));
174       mem_free (r, sizeof (*r));
175       mem_free (xprt, sizeof (SVCXPRT));
176       return NULL;
177     }
178   r->sendsize = sendsize;
179   r->recvsize = recvsize;
180   xprt->xp_p2 = NULL;
181   xprt->xp_p1 = (caddr_t) r;
182   xprt->xp_verf = _null_auth;
183   xprt->xp_ops = &svctcp_rendezvous_op;
184   xprt->xp_port = ntohs (addr.sin_port);
185   xprt->xp_sock = sock;
186   xprt_register (xprt);
187   return xprt;
188 }
189 #ifdef EXPORT_RPC_SYMBOLS
190 libc_hidden_def (svctcp_create)
191 #else
192 libc_hidden_nolink (svctcp_create, GLIBC_2_0)
193 #endif
194
195 /*
196  * Like svtcp_create(), except the routine takes any *open* UNIX file
197  * descriptor as its first input.
198  */
199 SVCXPRT *
200 svcfd_create (int fd, u_int sendsize, u_int recvsize)
201 {
202   return makefd_xprt (fd, sendsize, recvsize);
203 }
204 libc_hidden_nolink (svcfd_create, GLIBC_2_0)
205
206 static SVCXPRT *
207 internal_function
208 makefd_xprt (int fd, u_int sendsize, u_int recvsize)
209 {
210   SVCXPRT *xprt;
211   struct tcp_conn *cd;
212
213   xprt = (SVCXPRT *) mem_alloc (sizeof (SVCXPRT));
214   cd = (struct tcp_conn *) mem_alloc (sizeof (struct tcp_conn));
215   if (xprt == (SVCXPRT *) NULL || cd == NULL)
216     {
217       (void) __fxprintf (NULL, "%s: %s", "svc_tcp: makefd_xprt",
218                          _("out of memory\n"));
219       mem_free (xprt, sizeof (SVCXPRT));
220       mem_free (cd, sizeof (struct tcp_conn));
221       return NULL;
222     }
223   cd->strm_stat = XPRT_IDLE;
224   xdrrec_create (&(cd->xdrs), sendsize, recvsize,
225                  (caddr_t) xprt, readtcp, writetcp);
226   xprt->xp_p2 = NULL;
227   xprt->xp_p1 = (caddr_t) cd;
228   xprt->xp_verf.oa_base = cd->verf_body;
229   xprt->xp_addrlen = 0;
230   xprt->xp_ops = &svctcp_op;    /* truly deals with calls */
231   xprt->xp_port = 0;            /* this is a connection, not a rendezvouser */
232   xprt->xp_sock = fd;
233   xprt_register (xprt);
234   return xprt;
235 }
236
237 static bool_t
238 rendezvous_request (SVCXPRT *xprt, struct rpc_msg *errmsg)
239 {
240   int sock;
241   struct tcp_rendezvous *r;
242   struct sockaddr_in addr;
243   socklen_t len;
244
245   r = (struct tcp_rendezvous *) xprt->xp_p1;
246 again:
247   len = sizeof (struct sockaddr_in);
248   if ((sock = accept (xprt->xp_sock, (struct sockaddr *) &addr, &len)) < 0)
249     {
250       if (errno == EINTR)
251         goto again;
252       return FALSE;
253     }
254   /*
255    * make a new transporter (re-uses xprt)
256    */
257   xprt = makefd_xprt (sock, r->sendsize, r->recvsize);
258   memcpy (&xprt->xp_raddr, &addr, sizeof (addr));
259   xprt->xp_addrlen = len;
260   return FALSE;         /* there is never an rpc msg to be processed */
261 }
262
263 static enum xprt_stat
264 rendezvous_stat (SVCXPRT *xprt)
265 {
266   return XPRT_IDLE;
267 }
268
269 static void
270 svctcp_destroy (SVCXPRT *xprt)
271 {
272   struct tcp_conn *cd = (struct tcp_conn *) xprt->xp_p1;
273
274   xprt_unregister (xprt);
275   (void) __close (xprt->xp_sock);
276   if (xprt->xp_port != 0)
277     {
278       /* a rendezvouser socket */
279       xprt->xp_port = 0;
280     }
281   else
282     {
283       /* an actual connection socket */
284       XDR_DESTROY (&(cd->xdrs));
285     }
286   mem_free ((caddr_t) cd, sizeof (struct tcp_conn));
287   mem_free ((caddr_t) xprt, sizeof (SVCXPRT));
288 }
289
290
291 /*
292  * reads data from the tcp connection.
293  * any error is fatal and the connection is closed.
294  * (And a read of zero bytes is a half closed stream => error.)
295  */
296 static int
297 readtcp (char *xprtptr, char *buf, int len)
298 {
299   SVCXPRT *xprt = (SVCXPRT *)xprtptr;
300   int sock = xprt->xp_sock;
301   int milliseconds = 35 * 1000;
302   struct pollfd pollfd;
303
304   do
305     {
306       pollfd.fd = sock;
307       pollfd.events = POLLIN;
308       switch (__poll (&pollfd, 1, milliseconds))
309         {
310         case -1:
311           if (errno == EINTR)
312             continue;
313           /*FALLTHROUGH*/
314         case 0:
315           goto fatal_err;
316         default:
317           if ((pollfd.revents & POLLERR) || (pollfd.revents & POLLHUP)
318               || (pollfd.revents & POLLNVAL))
319             goto fatal_err;
320           break;
321         }
322     }
323   while ((pollfd.revents & POLLIN) == 0);
324
325   if ((len = __read (sock, buf, len)) > 0)
326     return len;
327
328  fatal_err:
329   ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
330   return -1;
331 }
332
333 /*
334  * writes data to the tcp connection.
335  * Any error is fatal and the connection is closed.
336  */
337 static int
338 writetcp (char *xprtptr, char * buf, int len)
339 {
340   SVCXPRT *xprt = (SVCXPRT *)xprtptr;
341   int i, cnt;
342
343   for (cnt = len; cnt > 0; cnt -= i, buf += i)
344     {
345       if ((i = __write (xprt->xp_sock, buf, cnt)) < 0)
346         {
347           ((struct tcp_conn *) (xprt->xp_p1))->strm_stat = XPRT_DIED;
348           return -1;
349         }
350     }
351   return len;
352 }
353
354 static enum xprt_stat
355 svctcp_stat (SVCXPRT *xprt)
356 {
357   struct tcp_conn *cd =
358   (struct tcp_conn *) (xprt->xp_p1);
359
360   if (cd->strm_stat == XPRT_DIED)
361     return XPRT_DIED;
362   if (!xdrrec_eof (&(cd->xdrs)))
363     return XPRT_MOREREQS;
364   return XPRT_IDLE;
365 }
366
367 static bool_t
368 svctcp_recv (SVCXPRT *xprt, struct rpc_msg *msg)
369 {
370   struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
371   XDR *xdrs = &(cd->xdrs);
372
373   xdrs->x_op = XDR_DECODE;
374   (void) xdrrec_skiprecord (xdrs);
375   if (xdr_callmsg (xdrs, msg))
376     {
377       cd->x_id = msg->rm_xid;
378       return TRUE;
379     }
380   cd->strm_stat = XPRT_DIED;    /* XXXX */
381   return FALSE;
382 }
383
384 static bool_t
385 svctcp_getargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
386 {
387   return ((*xdr_args) (&(((struct tcp_conn *)
388                           (xprt->xp_p1))->xdrs), args_ptr));
389 }
390
391 static bool_t
392 svctcp_freeargs (SVCXPRT *xprt, xdrproc_t xdr_args, caddr_t args_ptr)
393 {
394   XDR *xdrs = &(((struct tcp_conn *) (xprt->xp_p1))->xdrs);
395
396   xdrs->x_op = XDR_FREE;
397   return ((*xdr_args) (xdrs, args_ptr));
398 }
399
400 static bool_t
401 svctcp_reply (SVCXPRT *xprt, struct rpc_msg *msg)
402 {
403   struct tcp_conn *cd = (struct tcp_conn *) (xprt->xp_p1);
404   XDR *xdrs = &(cd->xdrs);
405   bool_t stat;
406
407   xdrs->x_op = XDR_ENCODE;
408   msg->rm_xid = cd->x_id;
409   stat = xdr_replymsg (xdrs, msg);
410   (void) xdrrec_endofrecord (xdrs, TRUE);
411   return stat;
412 }