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.
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.
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.
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.
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.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
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 $
36 * svc_unix.c, Server side for TCP/IP based RPC.
38 * Copyright (C) 1984, Sun Microsystems, Inc.
40 * Actually implements two flavors of transporter -
41 * a unix rendezvouser (a listner and connection establisher)
42 * and a record/unix stream.
45 #include "namespace.h"
51 #include <sys/socket.h>
55 #include "un-namespace.h"
58 * Ops vector for AF_UNIX based rpc service handle
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();
67 static struct xp_ops svcunix_op = {
77 * Ops vector for TCP/IP rendezvous handler
79 static bool_t rendezvous_request();
80 static enum xprt_stat rendezvous_stat();
82 static struct xp_ops svcunix_rendezvous_op = {
91 static int readunix(), writeunix();
92 static SVCXPRT *makefd_xprt();
94 struct unix_rendezvous { /* kept in xprt->xp_p1 */
99 struct unix_conn { /* kept in xprt->xp_p1 */
100 enum xprt_stat strm_stat;
103 char verf_body[MAX_AUTH_BYTES];
109 struct cmsgcred cmcred;
112 static struct cmessage cm;
114 static int __msgread(sock, buf, cnt)
122 bzero((char *)&cm, sizeof(cm));
123 iov[0].iov_base = buf;
124 iov[0].iov_len = cnt;
130 msg.msg_control = (caddr_t)&cm;
131 msg.msg_controllen = sizeof(struct cmessage);
134 return(_recvmsg(sock, &msg, 0));
137 static int __msgwrite(sock, buf, cnt)
145 bzero((char *)&cm, sizeof(cm));
146 iov[0].iov_base = buf;
147 iov[0].iov_len = cnt;
149 cm.cmsg.cmsg_type = SCM_CREDS;
150 cm.cmsg.cmsg_level = SOL_SOCKET;
151 cm.cmsg.cmsg_len = sizeof(struct cmessage);
157 msg.msg_control = (caddr_t)&cm;
158 msg.msg_controllen = sizeof(struct cmessage);
161 return(_sendmsg(sock, &msg, 0));
166 * xprt = svcunix_create(sock, send_buf_size, recv_buf_size);
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.
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.
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.
185 svcunix_create(sock, sendsize, recvsize, path)
191 bool_t madesock = FALSE;
193 struct unix_rendezvous *r;
194 struct sockaddr_un addr;
195 int len = sizeof(struct sockaddr_un);
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);
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;
211 _bind(sock, (struct sockaddr *)&addr, len);
213 if ((_getsockname(sock, (struct sockaddr *)&addr, &len) != 0) ||
214 (_listen(sock, 2) != 0)) {
215 perror("svc_unix.c - cannot getsockname or listen");
218 return ((SVCXPRT *)NULL);
220 r = (struct unix_rendezvous *)mem_alloc(sizeof(*r));
222 (void) fprintf(stderr, "svcunix_create: out of memory\n");
225 r->sendsize = sendsize;
226 r->recvsize = recvsize;
227 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
229 (void) fprintf(stderr, "svcunix_create: out of memory\n");
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;
243 * Like svunix_create(), except the routine takes any *open* UNIX file
244 * descriptor as its first input.
247 svcunixfd_create(fd, sendsize, recvsize)
253 return (makefd_xprt(fd, sendsize, recvsize));
257 makefd_xprt(fd, sendsize, recvsize)
263 struct unix_conn *cd;
265 xprt = (SVCXPRT *)mem_alloc(sizeof(SVCXPRT));
266 if (xprt == (SVCXPRT *)NULL) {
267 (void) fprintf(stderr, "svc_unix: makefd_xprt: out of memory\n");
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;
277 cd->strm_stat = XPRT_IDLE;
278 xdrrec_create(&(cd->xdrs), sendsize, recvsize,
279 (caddr_t)xprt, readunix, writeunix);
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 */
293 rendezvous_request(xprt)
297 struct unix_rendezvous *r;
298 struct sockaddr_un addr;
299 struct sockaddr_in in_addr;
302 r = (struct unix_rendezvous *)xprt->xp_p1;
304 len = sizeof(struct sockaddr_in);
305 if ((sock = _accept(xprt->xp_sock, (struct sockaddr *)&addr,
313 * make a new transporter (re-uses xprt)
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 */
323 static enum xprt_stat
331 svcunix_destroy(xprt)
334 struct unix_conn *cd = (struct unix_conn *)xprt->xp_p1;
336 xprt_unregister(xprt);
337 (void)_close(xprt->xp_sock);
338 if (xprt->xp_port != 0) {
339 /* a rendezvouser socket */
342 /* an actual connection socket */
343 XDR_DESTROY(&(cd->xdrs));
345 mem_free((caddr_t)cd, sizeof(struct unix_conn));
346 mem_free((caddr_t)xprt, sizeof(SVCXPRT));
350 * All read operations timeout after 35 seconds.
351 * A timeout is fatal for the connection.
353 static struct timeval wait_per_try = { 35, 0 };
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.)
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.
368 readunix(xprt, buf, len)
373 int sock = xprt->xp_sock;
374 struct timeval start, delta, tv;
375 struct timeval tmp1, tmp2;
377 extern fd_set *__svc_fdset;
378 extern int __svc_fdsetsize;
380 delta = wait_per_try;
382 gettimeofday(&start, NULL);
384 int bytes = howmany(__svc_fdsetsize, NFDBITS) *
388 fds = (fd_set *)malloc(bytes);
391 memcpy(fds, __svc_fdset, bytes);
393 /* XXX we know the other bits are still clear */
395 tv = delta; /* in case _select() implements writeback */
396 switch (_select(svc_maxfd + 1, fds, NULL, NULL, &tv)) {
398 memset(fds, 0, bytes);
401 gettimeofday(&tmp1, NULL);
402 timersub(&tmp1, &start, &tmp2);
403 timersub(&wait_per_try, &tmp2, &tmp1);
404 if (tmp1.tv_sec < 0 || !timerisset(&tmp1))
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))
422 } while (!FD_ISSET(sock, fds));
423 if ((len = __msgread(sock, buf, len)) > 0) {
429 ((struct unix_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
436 * writes data to the unix connection.
437 * Any error is fatal and the connection is closed.
440 writeunix(xprt, buf, len)
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 =
457 static enum xprt_stat
461 struct unix_conn *cd =
462 (struct unix_conn *)(xprt->xp_p1);
464 if (cd->strm_stat == XPRT_DIED)
466 if (! xdrrec_eof(&(cd->xdrs)))
467 return (XPRT_MOREREQS);
472 svcunix_recv(xprt, msg)
476 struct unix_conn *cd =
477 (struct unix_conn *)(xprt->xp_p1);
478 XDR *xdrs = &(cd->xdrs);
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);
490 cd->strm_stat = XPRT_DIED; /* XXXX */
495 svcunix_getargs(xprt, xdr_args, args_ptr)
501 return ((*xdr_args)(&(((struct unix_conn *)(xprt->xp_p1))->xdrs), args_ptr));
505 svcunix_freeargs(xprt, xdr_args, args_ptr)
511 &(((struct unix_conn *)(xprt->xp_p1))->xdrs);
513 xdrs->x_op = XDR_FREE;
514 return ((*xdr_args)(xdrs, args_ptr));
518 svcunix_reply(xprt, msg)
522 struct unix_conn *cd =
523 (struct unix_conn *)(xprt->xp_p1);
524 XDR *xdrs = &(cd->xdrs);
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);