Remove _THREAD_SAFE depenendancies. Create weakly associated stubs for
[games.git] / lib / libc / rpc / svc_unix.c
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  *
29  * @(#)svc_unix.c 1.21 87/08/11 Copyr 1984 Sun Micro
30  * @(#)svc_unix.c       2.2 88/08/01 4.0 RPCSRC
31  * $FreeBSD: src/lib/libc/rpc/svc_unix.c,v 1.7.2.2 2001/09/05 22:29:23 dec Exp $
32  * $DragonFly: src/lib/libc/rpc/svc_unix.c,v 1.4 2005/01/31 22:29:38 dillon Exp $
33  */
34
35 /*
36  * svc_unix.c, Server side for TCP/IP based RPC.
37  *
38  * Copyright (C) 1984, Sun Microsystems, Inc.
39  *
40  * Actually implements two flavors of transporter -
41  * a unix rendezvouser (a listner and connection establisher)
42  * and a record/unix stream.
43  */
44
45 #include "namespace.h"
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <string.h>
50 #include <rpc/rpc.h>
51 #include <sys/socket.h>
52 #include <sys/un.h>
53 #include <sys/uio.h>
54 #include <errno.h>
55 #include "un-namespace.h"
56
57 /*
58  * Ops vector for AF_UNIX based rpc service handle
59  */
60 static bool_t           svcunix_recv();
61 static enum xprt_stat   svcunix_stat();
62 static bool_t           svcunix_getargs();
63 static bool_t           svcunix_reply();
64 static bool_t           svcunix_freeargs();
65 static void             svcunix_destroy();
66
67 static struct xp_ops svcunix_op = {
68         svcunix_recv,
69         svcunix_stat,
70         svcunix_getargs,
71         svcunix_reply,
72         svcunix_freeargs,
73         svcunix_destroy
74 };
75
76 /*
77  * Ops vector for TCP/IP rendezvous handler
78  */
79 static bool_t           rendezvous_request();
80 static enum xprt_stat   rendezvous_stat();
81
82 static struct xp_ops svcunix_rendezvous_op = {
83         rendezvous_request,
84         rendezvous_stat,
85         (bool_t (*)())abort,
86         (bool_t (*)())abort,
87         (bool_t (*)())abort,
88         svcunix_destroy
89 };
90
91 static int readunix(), writeunix();
92 static SVCXPRT *makefd_xprt();
93
94 struct unix_rendezvous { /* kept in xprt->xp_p1 */
95         u_int sendsize;
96         u_int recvsize;
97 };
98
99 struct unix_conn {  /* kept in xprt->xp_p1 */
100         enum xprt_stat strm_stat;
101         u_long x_id;
102         XDR xdrs;
103         char verf_body[MAX_AUTH_BYTES];
104 };
105
106
107 struct cmessage {
108         struct cmsghdr cmsg;
109         struct cmsgcred cmcred;
110 };
111
112 static struct cmessage cm;
113
114 static int __msgread(sock, buf, cnt)
115         int sock;
116         void *buf;
117         size_t cnt;
118 {
119         struct iovec iov[1];
120         struct msghdr msg;
121
122         bzero((char *)&cm, sizeof(cm));
123         iov[0].iov_base = buf;
124         iov[0].iov_len = cnt;
125
126         msg.msg_iov = iov;
127         msg.msg_iovlen = 1;
128         msg.msg_name = NULL;
129         msg.msg_namelen = 0;
130         msg.msg_control = (caddr_t)&cm;
131         msg.msg_controllen = sizeof(struct cmessage);
132         msg.msg_flags = 0;
133
134         return(_recvmsg(sock, &msg, 0));
135 }
136
137 static int __msgwrite(sock, buf, cnt)
138         int sock;
139         void *buf;
140         size_t cnt;
141 {
142         struct iovec iov[1];
143         struct msghdr msg;
144
145         bzero((char *)&cm, sizeof(cm));
146         iov[0].iov_base = buf;
147         iov[0].iov_len = cnt;
148
149         cm.cmsg.cmsg_type = SCM_CREDS;
150         cm.cmsg.cmsg_level = SOL_SOCKET;
151         cm.cmsg.cmsg_len = sizeof(struct cmessage);
152
153         msg.msg_iov = iov;
154         msg.msg_iovlen = 1;
155         msg.msg_name = NULL;
156         msg.msg_namelen = 0;
157         msg.msg_control = (caddr_t)&cm;
158         msg.msg_controllen = sizeof(struct cmessage);
159         msg.msg_flags = 0;
160
161         return(_sendmsg(sock, &msg, 0));
162 }
163
164 /*
165  * Usage:
166  *      xprt = svcunix_create(sock, send_buf_size, recv_buf_size);
167  *
168  * Creates, registers, and returns a (rpc) unix based transporter.
169  * Once *xprt is initialized, it is registered as a transporter
170  * see (svc.h, xprt_register).  This routine returns
171  * a NULL if a problem occurred.
172  *
173  * If sock<0 then a socket is created, else sock is used.
174  * If the socket, sock is not bound to a port then svcunix_create
175  * binds it to an arbitrary port.  The routine then starts a unix
176  * listener on the socket's associated port.  In any (successful) case,
177  * xprt->xp_sock is the registered socket number and xprt->xp_port is the
178  * associated port number.
179  *
180  * Since unix streams do buffered io similar to stdio, the caller can specify
181  * how big the send and receive buffers are via the second and third parms;
182  * 0 => use the system default.
183  */
184 SVCXPRT *
185 svcunix_create(sock, sendsize, recvsize, path)
186         int sock;
187         u_int sendsize;
188         u_int recvsize;
189         char *path;
190 {
191         bool_t madesock = FALSE;
192         SVCXPRT *xprt;
193         struct unix_rendezvous *r;
194         struct sockaddr_un addr;
195         int len = sizeof(struct sockaddr_un);
196
197         if (sock == RPC_ANYSOCK) {
198                 if ((sock = _socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
199                         perror("svc_unix.c - AF_UNIX socket creation problem");
200                         return ((SVCXPRT *)NULL);
201                 }
202                 madesock = TRUE;
203         }
204         memset(&addr, 0, sizeof (addr));
205         addr.sun_family = AF_UNIX;
206         strcpy(addr.sun_path, path);
207         len = strlen(addr.sun_path) + sizeof(addr.sun_family) +
208                 sizeof(addr.sun_len) + 1;
209         addr.sun_len = len;
210
211         _bind(sock, (struct sockaddr *)&addr, len);
212
213         if ((_getsockname(sock, (struct sockaddr *)&addr, &len) != 0)  ||
214             (_listen(sock, 2) != 0)) {
215                 perror("svc_unix.c - cannot getsockname or listen");
216                 if (madesock)
217                        (void)_close(sock);
218                 return ((SVCXPRT *)NULL);
219         }
220         r = (struct unix_rendezvous *)mem_alloc(sizeof(*r));
221         if (r == NULL) {
222                 (void) fprintf(stderr, "svcunix_create: out of memory\n");
223                 return (NULL);
224         }
225         r->sendsize = sendsize;
226         r->recvsize = recvsize;
227         xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
228         if (xprt == NULL) {
229                 (void) fprintf(stderr, "svcunix_create: out of memory\n");
230                 return (NULL);
231         }
232         xprt->xp_p2 = NULL;
233         xprt->xp_p1 = (caddr_t)r;
234         xprt->xp_verf = _null_auth;
235         xprt->xp_ops = &svcunix_rendezvous_op;
236         xprt->xp_port = -1 /*ntohs(addr.sin_port)*/;
237         xprt->xp_sock = sock;
238         xprt_register(xprt);
239         return (xprt);
240 }
241
242 /*
243  * Like svunix_create(), except the routine takes any *open* UNIX file
244  * descriptor as its first input.
245  */
246 SVCXPRT *
247 svcunixfd_create(fd, sendsize, recvsize)
248         int fd;
249         u_int sendsize;
250         u_int recvsize;
251 {
252
253         return (makefd_xprt(fd, sendsize, recvsize));
254 }
255
256 static SVCXPRT *
257 makefd_xprt(fd, sendsize, recvsize)
258         int fd;
259         u_int sendsize;
260         u_int recvsize;
261 {
262         SVCXPRT *xprt;
263         struct unix_conn *cd;
264
265         xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
266         if (xprt == (SVCXPRT *)NULL) {
267                 (void) fprintf(stderr, "svc_unix: makefd_xprt: out of memory\n");
268                 goto done;
269         }
270         cd = (struct unix_conn *)mem_alloc(sizeof(struct unix_conn));
271         if (cd == (struct unix_conn *)NULL) {
272                 (void) fprintf(stderr, "svc_unix: makefd_xprt: out of memory\n");
273                 mem_free((char *) xprt, sizeof(SVCXPRT));
274                 xprt = (SVCXPRT *)NULL;
275                 goto done;
276         }
277         cd->strm_stat = XPRT_IDLE;
278         xdrrec_create(&(cd->xdrs), sendsize, recvsize,
279             (caddr_t)xprt, readunix, writeunix);
280         xprt->xp_p2 = NULL;
281         xprt->xp_p1 = (caddr_t)cd;
282         xprt->xp_verf.oa_base = cd->verf_body;
283         xprt->xp_addrlen = 0;
284         xprt->xp_ops = &svcunix_op;  /* truely deals with calls */
285         xprt->xp_port = 0;  /* this is a connection, not a rendezvouser */
286         xprt->xp_sock = fd;
287         xprt_register(xprt);
288     done:
289         return (xprt);
290 }
291
292 static bool_t
293 rendezvous_request(xprt)
294         SVCXPRT *xprt;
295 {
296         int sock;
297         struct unix_rendezvous *r;
298         struct sockaddr_un addr;
299         struct sockaddr_in in_addr;
300         int len;
301
302         r = (struct unix_rendezvous *)xprt->xp_p1;
303     again:
304         len = sizeof(struct sockaddr_in);
305         if ((sock = _accept(xprt->xp_sock, (struct sockaddr *)&addr,
306             &len)) < 0) {
307                 if (errno == EINTR)
308                         goto again;
309                return (FALSE);
310         }
311
312         /*
313          * make a new transporter (re-uses xprt)
314          */
315         bzero((char *)&in_addr, sizeof(in_addr));
316         in_addr.sin_family = AF_UNIX;
317         xprt = makefd_xprt(sock, r->sendsize, r->recvsize);
318         xprt->xp_raddr = in_addr;
319         xprt->xp_addrlen = len;
320         return (FALSE); /* there is never an rpc msg to be processed */
321 }
322
323 static enum xprt_stat
324 rendezvous_stat()
325 {
326
327         return (XPRT_IDLE);
328 }
329
330 static void
331 svcunix_destroy(xprt)
332         SVCXPRT *xprt;
333 {
334         struct unix_conn *cd = (struct unix_conn *)xprt->xp_p1;
335
336         xprt_unregister(xprt);
337         (void)_close(xprt->xp_sock);
338         if (xprt->xp_port != 0) {
339                 /* a rendezvouser socket */
340                 xprt->xp_port = 0;
341         } else {
342                 /* an actual connection socket */
343                 XDR_DESTROY(&(cd->xdrs));
344         }
345         mem_free((caddr_t)cd, sizeof(struct unix_conn));
346         mem_free((caddr_t)xprt, sizeof(SVCXPRT));
347 }
348
349 /*
350  * All read operations timeout after 35 seconds.
351  * A timeout is fatal for the connection.
352  */
353 static struct timeval wait_per_try = { 35, 0 };
354
355 /*
356  * reads data from the unix conection.
357  * any error is fatal and the connection is closed.
358  * (And a read of zero bytes is a half closed stream => error.)
359  *
360  * Note: we have to be careful here not to allow ourselves to become
361  * blocked too long in this routine. While we're waiting for data from one
362  * client, another client may be trying to connect. To avoid this situation,
363  * some code from svc_run() is transplanted here: the _select() loop checks
364  * all RPC descriptors including the one we want and calls svc_getreqset2()
365  * to handle new requests if any are detected.
366  */
367 static int
368 readunix(xprt, buf, len)
369         SVCXPRT *xprt;
370         caddr_t buf;
371         int len;
372 {
373         int sock = xprt->xp_sock;
374         struct timeval start, delta, tv;
375         struct timeval tmp1, tmp2;
376         fd_set *fds;
377         extern fd_set           *__svc_fdset;
378         extern int              __svc_fdsetsize;
379
380         delta = wait_per_try;
381         fds = NULL;
382         gettimeofday(&start, NULL);
383         do {
384                 int bytes = howmany(__svc_fdsetsize, NFDBITS) *
385                                 sizeof(fd_mask);
386                 if (fds != NULL)
387                         free(fds);
388                 fds = (fd_set *)malloc(bytes);
389                 if (fds == NULL)
390                         goto fatal_err;
391                 memcpy(fds, __svc_fdset, bytes);
392
393                 /* XXX we know the other bits are still clear */
394                 FD_SET(sock, fds);
395                 tv = delta;     /* in case _select() implements writeback */
396                 switch (_select(svc_maxfd + 1, fds, NULL, NULL, &tv)) {
397                 case -1:
398                         memset(fds, 0, bytes);
399                         if (errno != EINTR)
400                                 goto fatal_err;
401                         gettimeofday(&tmp1, NULL);
402                         timersub(&tmp1, &start, &tmp2);
403                         timersub(&wait_per_try, &tmp2, &tmp1);
404                         if (tmp1.tv_sec < 0 || !timerisset(&tmp1))
405                                 goto fatal_err;
406                         delta = tmp1;
407                         continue;
408                 case 0:
409                         goto fatal_err;
410                 default:
411                         if (!FD_ISSET(sock, fds)) {
412                                 svc_getreqset2(fds, svc_maxfd + 1);
413                                 gettimeofday(&tmp1, NULL);
414                                 timersub(&tmp1, &start, &tmp2);
415                                 timersub(&wait_per_try, &tmp2, &tmp1);
416                                 if (tmp1.tv_sec < 0 || !timerisset(&tmp1))
417                                         goto fatal_err;
418                                 delta = tmp1;
419                                 continue;
420                         }
421                 }
422         } while (!FD_ISSET(sock, fds));
423         if ((len = __msgread(sock, buf, len)) > 0) {
424                 if (fds != NULL)
425                         free(fds);
426                 return (len);
427         }
428 fatal_err:
429         ((struct unix_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
430         if (fds != NULL)
431                 free(fds);
432         return (-1);
433 }
434
435 /*
436  * writes data to the unix connection.
437  * Any error is fatal and the connection is closed.
438  */
439 static int
440 writeunix(xprt, buf, len)
441         SVCXPRT *xprt;
442         caddr_t buf;
443         int len;
444 {
445         int i, cnt;
446
447         for (cnt = len; cnt > 0; cnt -= i, buf += i) {
448                 if ((i = __msgwrite(xprt->xp_sock, buf, cnt)) < 0) {
449                         ((struct unix_conn *)(xprt->xp_p1))->strm_stat =
450                             XPRT_DIED;
451                         return (-1);
452                 }
453         }
454         return (len);
455 }
456
457 static enum xprt_stat
458 svcunix_stat(xprt)
459         SVCXPRT *xprt;
460 {
461         struct unix_conn *cd =
462             (struct unix_conn *)(xprt->xp_p1);
463
464         if (cd->strm_stat == XPRT_DIED)
465                 return (XPRT_DIED);
466         if (! xdrrec_eof(&(cd->xdrs)))
467                 return (XPRT_MOREREQS);
468         return (XPRT_IDLE);
469 }
470
471 static bool_t
472 svcunix_recv(xprt, msg)
473         SVCXPRT *xprt;
474         struct rpc_msg *msg;
475 {
476         struct unix_conn *cd =
477             (struct unix_conn *)(xprt->xp_p1);
478         XDR *xdrs = &(cd->xdrs);
479
480         xdrs->x_op = XDR_DECODE;
481         (void)xdrrec_skiprecord(xdrs);
482         if (xdr_callmsg(xdrs, msg)) {
483                 cd->x_id = msg->rm_xid;
484                 /* set up verifiers */
485                 msg->rm_call.cb_verf.oa_flavor = AUTH_UNIX;
486                 msg->rm_call.cb_verf.oa_base = (caddr_t)&cm;
487                 msg->rm_call.cb_verf.oa_length = sizeof(cm);
488                 return (TRUE);
489         }
490         cd->strm_stat = XPRT_DIED;      /* XXXX */
491         return (FALSE);
492 }
493
494 static bool_t
495 svcunix_getargs(xprt, xdr_args, args_ptr)
496         SVCXPRT *xprt;
497         xdrproc_t xdr_args;
498         caddr_t args_ptr;
499 {
500
501         return ((*xdr_args)(&(((struct unix_conn *)(xprt->xp_p1))->xdrs), args_ptr));
502 }
503
504 static bool_t
505 svcunix_freeargs(xprt, xdr_args, args_ptr)
506         SVCXPRT *xprt;
507         xdrproc_t xdr_args;
508         caddr_t args_ptr;
509 {
510         XDR *xdrs =
511             &(((struct unix_conn *)(xprt->xp_p1))->xdrs);
512
513         xdrs->x_op = XDR_FREE;
514         return ((*xdr_args)(xdrs, args_ptr));
515 }
516
517 static bool_t
518 svcunix_reply(xprt, msg)
519         SVCXPRT *xprt;
520         struct rpc_msg *msg;
521 {
522         struct unix_conn *cd =
523             (struct unix_conn *)(xprt->xp_p1);
524         XDR *xdrs = &(cd->xdrs);
525         bool_t stat;
526
527         xdrs->x_op = XDR_ENCODE;
528         msg->rm_xid = cd->x_id;
529         stat = xdr_replymsg(xdrs, msg);
530         (void)xdrrec_endofrecord(xdrs, TRUE);
531         return (stat);
532 }