xref: /freebsd/lib/libc/rpc/svc.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*	$NetBSD: svc.c,v 1.21 2000/07/06 03:10:35 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2009, Sun Microsystems, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * - Redistributions of source code must retain the above copyright notice,
12  *   this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright notice,
14  *   this list of conditions and the following disclaimer in the documentation
15  *   and/or other materials provided with the distribution.
16  * - Neither the name of Sun Microsystems, 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 "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char *sccsid2 = "@(#)svc.c 1.44 88/02/08 Copyr 1984 Sun Micro";
35 static char *sccsid = "@(#)svc.c	2.4 88/08/11 4.0 RPCSRC";
36 #endif
37 /*
38  * svc.c, Server-side remote procedure call interface.
39  *
40  * There are two sets of procedures here.  The xprt routines are
41  * for handling transport handles.  The svc routines handle the
42  * list of service routines.
43  *
44  * Copyright (C) 1984, Sun Microsystems, Inc.
45  */
46 
47 #include "namespace.h"
48 #include "reentrant.h"
49 #include <sys/types.h>
50 #include <sys/poll.h>
51 #include <assert.h>
52 #include <errno.h>
53 #include <stdlib.h>
54 #include <string.h>
55 
56 #include <rpc/rpc.h>
57 #ifdef PORTMAP
58 #include <rpc/pmap_clnt.h>
59 #endif				/* PORTMAP */
60 #include "un-namespace.h"
61 
62 #include "rpc_com.h"
63 #include "mt_misc.h"
64 
65 #define	RQCRED_SIZE	400		/* this size is excessive */
66 
67 #define SVC_VERSQUIET 0x0001		/* keep quiet about vers mismatch */
68 #define version_keepquiet(xp) (SVC_EXT(xp)->xp_flags & SVC_VERSQUIET)
69 
70 #define max(a, b) (a > b ? a : b)
71 
72 /*
73  * The services list
74  * Each entry represents a set of procedures (an rpc program).
75  * The dispatch routine takes request structs and runs the
76  * appropriate procedure.
77  */
78 static struct svc_callout {
79 	struct svc_callout *sc_next;
80 	rpcprog_t	    sc_prog;
81 	rpcvers_t	    sc_vers;
82 	char		   *sc_netid;
83 	void		    (*sc_dispatch)(struct svc_req *, SVCXPRT *);
84 } *svc_head;
85 
86 SVCXPRT **__svc_xports;
87 int __svc_maxrec;
88 
89 static struct svc_callout *svc_find(rpcprog_t, rpcvers_t,
90     struct svc_callout **, char *);
91 static void __xprt_do_unregister (SVCXPRT *xprt, bool_t dolock);
92 
93 /* ***************  SVCXPRT related stuff **************** */
94 
95 /*
96  * Activate a transport handle.
97  */
98 void
99 xprt_register(SVCXPRT *xprt)
100 {
101 	int sock;
102 
103 	assert(xprt != NULL);
104 
105 	sock = xprt->xp_fd;
106 
107 	rwlock_wrlock(&svc_fd_lock);
108 	if (__svc_xports == NULL) {
109 		__svc_xports = (SVCXPRT **)
110 			mem_alloc((FD_SETSIZE + 1) * sizeof(SVCXPRT *));
111 		if (__svc_xports == NULL) {
112 			rwlock_unlock(&svc_fd_lock);
113 			return;
114 		}
115 		memset(__svc_xports, '\0', (FD_SETSIZE + 1) * sizeof(SVCXPRT *));
116 	}
117 	if (sock < FD_SETSIZE) {
118 		__svc_xports[sock] = xprt;
119 		FD_SET(sock, &svc_fdset);
120 		svc_maxfd = max(svc_maxfd, sock);
121 	} else if (sock == FD_SETSIZE)
122 		__svc_xports[sock] = xprt;
123 	rwlock_unlock(&svc_fd_lock);
124 }
125 
126 void
127 xprt_unregister(SVCXPRT *xprt)
128 {
129 	__xprt_do_unregister(xprt, TRUE);
130 }
131 
132 void
133 __xprt_unregister_unlocked(SVCXPRT *xprt)
134 {
135 	__xprt_do_unregister(xprt, FALSE);
136 }
137 
138 /*
139  * De-activate a transport handle.
140  */
141 static void
142 __xprt_do_unregister(SVCXPRT *xprt, bool_t dolock)
143 {
144 	int sock;
145 
146 	assert(xprt != NULL);
147 
148 	sock = xprt->xp_fd;
149 
150 	if (dolock)
151 		rwlock_wrlock(&svc_fd_lock);
152 	if ((sock < FD_SETSIZE) && (__svc_xports[sock] == xprt)) {
153 		__svc_xports[sock] = NULL;
154 		FD_CLR(sock, &svc_fdset);
155 		if (sock >= svc_maxfd) {
156 			for (svc_maxfd--; svc_maxfd>=0; svc_maxfd--)
157 				if (__svc_xports[svc_maxfd])
158 					break;
159 		}
160 	} else if ((sock == FD_SETSIZE) && (__svc_xports[sock] == xprt))
161 		__svc_xports[sock] = NULL;
162 	if (dolock)
163 		rwlock_unlock(&svc_fd_lock);
164 }
165 
166 /*
167  * Add a service program to the callout list.
168  * The dispatch routine will be called when a rpc request for this
169  * program number comes in.
170  */
171 bool_t
172 svc_reg(SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
173     void (*dispatch)(struct svc_req *, SVCXPRT *),
174     const struct netconfig *nconf)
175 {
176 	bool_t dummy;
177 	struct svc_callout *prev;
178 	struct svc_callout *s;
179 	struct netconfig *tnconf;
180 	char *netid = NULL;
181 	int flag = 0;
182 
183 /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */
184 
185 	if (xprt->xp_netid) {
186 		netid = strdup(xprt->xp_netid);
187 		flag = 1;
188 	} else if (nconf && nconf->nc_netid) {
189 		netid = strdup(nconf->nc_netid);
190 		flag = 1;
191 	} else if ((tnconf = __rpcgettp(xprt->xp_fd)) != NULL) {
192 		netid = strdup(tnconf->nc_netid);
193 		flag = 1;
194 		freenetconfigent(tnconf);
195 	} /* must have been created with svc_raw_create */
196 	if ((netid == NULL) && (flag == 1)) {
197 		return (FALSE);
198 	}
199 
200 	rwlock_wrlock(&svc_lock);
201 	if ((s = svc_find(prog, vers, &prev, netid)) != NULL) {
202 		free(netid);
203 		if (s->sc_dispatch == dispatch)
204 			goto rpcb_it; /* he is registering another xptr */
205 		rwlock_unlock(&svc_lock);
206 		return (FALSE);
207 	}
208 	s = mem_alloc(sizeof (struct svc_callout));
209 	if (s == NULL) {
210 		free(netid);
211 		rwlock_unlock(&svc_lock);
212 		return (FALSE);
213 	}
214 
215 	s->sc_prog = prog;
216 	s->sc_vers = vers;
217 	s->sc_dispatch = dispatch;
218 	s->sc_netid = netid;
219 	s->sc_next = svc_head;
220 	svc_head = s;
221 
222 	if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
223 		((SVCXPRT *) xprt)->xp_netid = strdup(netid);
224 
225 rpcb_it:
226 	rwlock_unlock(&svc_lock);
227 	/* now register the information with the local binder service */
228 	if (nconf) {
229 		/*LINTED const castaway*/
230 		dummy = rpcb_set(prog, vers, (struct netconfig *) nconf,
231 		&((SVCXPRT *) xprt)->xp_ltaddr);
232 		return (dummy);
233 	}
234 	return (TRUE);
235 }
236 
237 /*
238  * Remove a service program from the callout list.
239  */
240 void
241 svc_unreg(const rpcprog_t prog, const rpcvers_t vers)
242 {
243 	struct svc_callout *prev;
244 	struct svc_callout *s;
245 
246 	/* unregister the information anyway */
247 	(void) rpcb_unset(prog, vers, NULL);
248 	rwlock_wrlock(&svc_lock);
249 	while ((s = svc_find(prog, vers, &prev, NULL)) != NULL) {
250 		if (prev == NULL) {
251 			svc_head = s->sc_next;
252 		} else {
253 			prev->sc_next = s->sc_next;
254 		}
255 		s->sc_next = NULL;
256 		if (s->sc_netid)
257 			mem_free(s->sc_netid, sizeof (s->sc_netid) + 1);
258 		mem_free(s, sizeof (struct svc_callout));
259 	}
260 	rwlock_unlock(&svc_lock);
261 }
262 
263 /* ********************** CALLOUT list related stuff ************* */
264 
265 #ifdef PORTMAP
266 /*
267  * Add a service program to the callout list.
268  * The dispatch routine will be called when a rpc request for this
269  * program number comes in.
270  */
271 bool_t
272 svc_register(SVCXPRT *xprt, u_long prog, u_long vers,
273     void (*dispatch)(struct svc_req *, SVCXPRT *),
274     int protocol)
275 {
276 	struct svc_callout *prev;
277 	struct svc_callout *s;
278 
279 	assert(xprt != NULL);
280 	assert(dispatch != NULL);
281 
282 	if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) !=
283 	    NULL) {
284 		if (s->sc_dispatch == dispatch)
285 			goto pmap_it;  /* he is registering another xptr */
286 		return (FALSE);
287 	}
288 	s = mem_alloc(sizeof(struct svc_callout));
289 	if (s == NULL) {
290 		return (FALSE);
291 	}
292 	s->sc_prog = (rpcprog_t)prog;
293 	s->sc_vers = (rpcvers_t)vers;
294 	s->sc_dispatch = dispatch;
295 	s->sc_next = svc_head;
296 	svc_head = s;
297 pmap_it:
298 	/* now register the information with the local binder service */
299 	if (protocol) {
300 		return (pmap_set(prog, vers, protocol, xprt->xp_port));
301 	}
302 	return (TRUE);
303 }
304 
305 /*
306  * Remove a service program from the callout list.
307  */
308 void
309 svc_unregister(u_long prog, u_long vers)
310 {
311 	struct svc_callout *prev;
312 	struct svc_callout *s;
313 
314 	if ((s = svc_find((rpcprog_t)prog, (rpcvers_t)vers, &prev, NULL)) ==
315 	    NULL)
316 		return;
317 	if (prev == NULL) {
318 		svc_head = s->sc_next;
319 	} else {
320 		prev->sc_next = s->sc_next;
321 	}
322 	s->sc_next = NULL;
323 	mem_free(s, sizeof(struct svc_callout));
324 	/* now unregister the information with the local binder service */
325 	(void)pmap_unset(prog, vers);
326 }
327 #endif				/* PORTMAP */
328 
329 /*
330  * Search the callout list for a program number, return the callout
331  * struct.
332  */
333 static struct svc_callout *
334 svc_find(rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev,
335     char *netid)
336 {
337 	struct svc_callout *s, *p;
338 
339 	assert(prev != NULL);
340 
341 	p = NULL;
342 	for (s = svc_head; s != NULL; s = s->sc_next) {
343 		if (((s->sc_prog == prog) && (s->sc_vers == vers)) &&
344 		    ((netid == NULL) || (s->sc_netid == NULL) ||
345 		    (strcmp(netid, s->sc_netid) == 0)))
346 			break;
347 		p = s;
348 	}
349 	*prev = p;
350 	return (s);
351 }
352 
353 /* ******************* REPLY GENERATION ROUTINES  ************ */
354 
355 /*
356  * Send a reply to an rpc request
357  */
358 bool_t
359 svc_sendreply(SVCXPRT *xprt, xdrproc_t xdr_results,
360     void * xdr_location)
361 {
362 	struct rpc_msg rply;
363 
364 	assert(xprt != NULL);
365 
366 	rply.rm_direction = REPLY;
367 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
368 	rply.acpted_rply.ar_verf = xprt->xp_verf;
369 	rply.acpted_rply.ar_stat = SUCCESS;
370 	rply.acpted_rply.ar_results.where = xdr_location;
371 	rply.acpted_rply.ar_results.proc = xdr_results;
372 	return (SVC_REPLY(xprt, &rply));
373 }
374 
375 /*
376  * No procedure error reply
377  */
378 void
379 svcerr_noproc(SVCXPRT *xprt)
380 {
381 	struct rpc_msg rply;
382 
383 	assert(xprt != NULL);
384 
385 	rply.rm_direction = REPLY;
386 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
387 	rply.acpted_rply.ar_verf = xprt->xp_verf;
388 	rply.acpted_rply.ar_stat = PROC_UNAVAIL;
389 	SVC_REPLY(xprt, &rply);
390 }
391 
392 /*
393  * Can't decode args error reply
394  */
395 void
396 svcerr_decode(SVCXPRT *xprt)
397 {
398 	struct rpc_msg rply;
399 
400 	assert(xprt != NULL);
401 
402 	rply.rm_direction = REPLY;
403 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
404 	rply.acpted_rply.ar_verf = xprt->xp_verf;
405 	rply.acpted_rply.ar_stat = GARBAGE_ARGS;
406 	SVC_REPLY(xprt, &rply);
407 }
408 
409 /*
410  * Some system error
411  */
412 void
413 svcerr_systemerr(SVCXPRT *xprt)
414 {
415 	struct rpc_msg rply;
416 
417 	assert(xprt != NULL);
418 
419 	rply.rm_direction = REPLY;
420 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
421 	rply.acpted_rply.ar_verf = xprt->xp_verf;
422 	rply.acpted_rply.ar_stat = SYSTEM_ERR;
423 	SVC_REPLY(xprt, &rply);
424 }
425 
426 #if 0
427 /*
428  * Tell RPC package to not complain about version errors to the client.	 This
429  * is useful when revving broadcast protocols that sit on a fixed address.
430  * There is really one (or should be only one) example of this kind of
431  * protocol: the portmapper (or rpc binder).
432  */
433 void
434 __svc_versquiet_on(SVCXPRT *xprt)
435 {
436 
437 	SVC_EXT(xprt)->xp_flags |= SVC_VERSQUIET;
438 }
439 
440 void
441 __svc_versquiet_off(SVCXPRT *xprt)
442 {
443 
444 	SVC_EXT(xprt)->xp_flags &= ~SVC_VERSQUIET;
445 }
446 
447 void
448 svc_versquiet(SVCXPRT *xprt)
449 {
450 	__svc_versquiet_on(xprt);
451 }
452 
453 int
454 __svc_versquiet_get(SVCXPRT *xprt)
455 {
456 
457 	return (SVC_EXT(xprt)->xp_flags & SVC_VERSQUIET);
458 }
459 #endif
460 
461 /*
462  * Authentication error reply
463  */
464 void
465 svcerr_auth(SVCXPRT *xprt, enum auth_stat why)
466 {
467 	struct rpc_msg rply;
468 
469 	assert(xprt != NULL);
470 
471 	rply.rm_direction = REPLY;
472 	rply.rm_reply.rp_stat = MSG_DENIED;
473 	rply.rjcted_rply.rj_stat = AUTH_ERROR;
474 	rply.rjcted_rply.rj_why = why;
475 	SVC_REPLY(xprt, &rply);
476 }
477 
478 /*
479  * Auth too weak error reply
480  */
481 void
482 svcerr_weakauth(SVCXPRT *xprt)
483 {
484 
485 	assert(xprt != NULL);
486 
487 	svcerr_auth(xprt, AUTH_TOOWEAK);
488 }
489 
490 /*
491  * Program unavailable error reply
492  */
493 void
494 svcerr_noprog(SVCXPRT *xprt)
495 {
496 	struct rpc_msg rply;
497 
498 	assert(xprt != NULL);
499 
500 	rply.rm_direction = REPLY;
501 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
502 	rply.acpted_rply.ar_verf = xprt->xp_verf;
503 	rply.acpted_rply.ar_stat = PROG_UNAVAIL;
504 	SVC_REPLY(xprt, &rply);
505 }
506 
507 /*
508  * Program version mismatch error reply
509  */
510 void
511 svcerr_progvers(SVCXPRT *xprt, rpcvers_t low_vers, rpcvers_t high_vers)
512 {
513 	struct rpc_msg rply;
514 
515 	assert(xprt != NULL);
516 
517 	rply.rm_direction = REPLY;
518 	rply.rm_reply.rp_stat = MSG_ACCEPTED;
519 	rply.acpted_rply.ar_verf = xprt->xp_verf;
520 	rply.acpted_rply.ar_stat = PROG_MISMATCH;
521 	rply.acpted_rply.ar_vers.low = (u_int32_t)low_vers;
522 	rply.acpted_rply.ar_vers.high = (u_int32_t)high_vers;
523 	SVC_REPLY(xprt, &rply);
524 }
525 
526 /*
527  * Allocate a new server transport structure. All fields are
528  * initialized to zero and xp_p3 is initialized to point at an
529  * extension structure to hold various flags and authentication
530  * parameters.
531  */
532 SVCXPRT *
533 svc_xprt_alloc(void)
534 {
535 	SVCXPRT *xprt;
536 	SVCXPRT_EXT *ext;
537 
538 	xprt = mem_alloc(sizeof(SVCXPRT));
539 	if (xprt == NULL)
540 		return (NULL);
541 	memset(xprt, 0, sizeof(SVCXPRT));
542 	ext = mem_alloc(sizeof(SVCXPRT_EXT));
543 	if (ext == NULL) {
544 		mem_free(xprt, sizeof(SVCXPRT));
545 		return (NULL);
546 	}
547 	memset(ext, 0, sizeof(SVCXPRT_EXT));
548 	xprt->xp_p3 = ext;
549 	ext->xp_auth.svc_ah_ops = &svc_auth_null_ops;
550 
551 	return (xprt);
552 }
553 
554 /*
555  * Free a server transport structure.
556  */
557 void
558 svc_xprt_free(SVCXPRT *xprt)
559 {
560 
561 	mem_free(xprt->xp_p3, sizeof(SVCXPRT_EXT));
562 	mem_free(xprt, sizeof(SVCXPRT));
563 }
564 
565 /* ******************* SERVER INPUT STUFF ******************* */
566 
567 /*
568  * Get server side input from some transport.
569  *
570  * Statement of authentication parameters management:
571  * This function owns and manages all authentication parameters, specifically
572  * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
573  * the "cooked" credentials (rqst->rq_clntcred).
574  * However, this function does not know the structure of the cooked
575  * credentials, so it make the following assumptions:
576  *   a) the structure is contiguous (no pointers), and
577  *   b) the cred structure size does not exceed RQCRED_SIZE bytes.
578  * In all events, all three parameters are freed upon exit from this routine.
579  * The storage is trivially management on the call stack in user land, but
580  * is mallocated in kernel land.
581  */
582 
583 void
584 svc_getreq(int rdfds)
585 {
586 	fd_set readfds;
587 
588 	FD_ZERO(&readfds);
589 	readfds.fds_bits[0] = rdfds;
590 	svc_getreqset(&readfds);
591 }
592 
593 void
594 svc_getreqset(fd_set *readfds)
595 {
596 	int bit, fd;
597 	fd_mask mask, *maskp;
598 	int sock;
599 
600 	assert(readfds != NULL);
601 
602 	maskp = readfds->fds_bits;
603 	for (sock = 0; sock < FD_SETSIZE; sock += NFDBITS) {
604 	    for (mask = *maskp++; (bit = ffsl(mask)) != 0;
605 		mask ^= (1ul << (bit - 1))) {
606 		/* sock has input waiting */
607 		fd = sock + bit - 1;
608 		svc_getreq_common(fd);
609 	    }
610 	}
611 }
612 
613 void
614 svc_getreq_common(int fd)
615 {
616 	SVCXPRT *xprt;
617 	struct svc_req r;
618 	struct rpc_msg msg;
619 	int prog_found;
620 	rpcvers_t low_vers;
621 	rpcvers_t high_vers;
622 	enum xprt_stat stat;
623 	char cred_area[2*MAX_AUTH_BYTES + RQCRED_SIZE];
624 
625 	msg.rm_call.cb_cred.oa_base = cred_area;
626 	msg.rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
627 	r.rq_clntcred = &(cred_area[2*MAX_AUTH_BYTES]);
628 
629 	rwlock_rdlock(&svc_fd_lock);
630 	xprt = __svc_xports[fd];
631 	rwlock_unlock(&svc_fd_lock);
632 	if (xprt == NULL)
633 		/* But do we control sock? */
634 		return;
635 	/* now receive msgs from xprtprt (support batch calls) */
636 	do {
637 		if (SVC_RECV(xprt, &msg)) {
638 
639 			/* now find the exported program and call it */
640 			struct svc_callout *s;
641 			enum auth_stat why;
642 
643 			r.rq_xprt = xprt;
644 			r.rq_prog = msg.rm_call.cb_prog;
645 			r.rq_vers = msg.rm_call.cb_vers;
646 			r.rq_proc = msg.rm_call.cb_proc;
647 			r.rq_cred = msg.rm_call.cb_cred;
648 			/* first authenticate the message */
649 			if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
650 				/*
651 				 * RPCSEC_GSS uses this return code
652 				 * for requests that form part of its
653 				 * context establishment protocol and
654 				 * should not be dispatched to the
655 				 * application.
656 				 */
657 				if (why != RPCSEC_GSS_NODISPATCH)
658 					svcerr_auth(xprt, why);
659 				goto call_done;
660 			}
661 			/* now match message with a registered service*/
662 			prog_found = FALSE;
663 			low_vers = (rpcvers_t) -1L;
664 			high_vers = (rpcvers_t) 0L;
665 			for (s = svc_head; s != NULL; s = s->sc_next) {
666 				if (s->sc_prog == r.rq_prog) {
667 					if (s->sc_vers == r.rq_vers) {
668 						(*s->sc_dispatch)(&r, xprt);
669 						goto call_done;
670 					}  /* found correct version */
671 					prog_found = TRUE;
672 					if (s->sc_vers < low_vers)
673 						low_vers = s->sc_vers;
674 					if (s->sc_vers > high_vers)
675 						high_vers = s->sc_vers;
676 				}   /* found correct program */
677 			}
678 			/*
679 			 * if we got here, the program or version
680 			 * is not served ...
681 			 */
682 			if (prog_found)
683 				svcerr_progvers(xprt, low_vers, high_vers);
684 			else
685 				svcerr_noprog(xprt);
686 			/* Fall through to ... */
687 		}
688 		/*
689 		 * Check if the xprt has been disconnected in a
690 		 * recursive call in the service dispatch routine.
691 		 * If so, then break.
692 		 */
693 		rwlock_rdlock(&svc_fd_lock);
694 		if (xprt != __svc_xports[fd]) {
695 			rwlock_unlock(&svc_fd_lock);
696 			break;
697 		}
698 		rwlock_unlock(&svc_fd_lock);
699 call_done:
700 		if ((stat = SVC_STAT(xprt)) == XPRT_DIED){
701 			SVC_DESTROY(xprt);
702 			break;
703 		}
704 	} while (stat == XPRT_MOREREQS);
705 }
706 
707 
708 void
709 svc_getreq_poll(struct pollfd *pfdp, int pollretval)
710 {
711 	int i;
712 	int fds_found;
713 
714 	for (i = fds_found = 0; fds_found < pollretval; i++) {
715 		struct pollfd *p = &pfdp[i];
716 
717 		if (p->revents) {
718 			/* fd has input waiting */
719 			fds_found++;
720 			/*
721 			 *	We assume that this function is only called
722 			 *	via someone _select()ing from svc_fdset or
723 			 *	_poll()ing from svc_pollset[].  Thus it's safe
724 			 *	to handle the POLLNVAL event by simply turning
725 			 *	the corresponding bit off in svc_fdset.  The
726 			 *	svc_pollset[] array is derived from svc_fdset
727 			 *	and so will also be updated eventually.
728 			 *
729 			 *	XXX Should we do an xprt_unregister() instead?
730 			 */
731 			if (p->revents & POLLNVAL) {
732 				rwlock_wrlock(&svc_fd_lock);
733 				FD_CLR(p->fd, &svc_fdset);
734 				rwlock_unlock(&svc_fd_lock);
735 			} else
736 				svc_getreq_common(p->fd);
737 		}
738 	}
739 }
740 
741 bool_t
742 rpc_control(int what, void *arg)
743 {
744 	int val;
745 
746 	switch (what) {
747 	case RPC_SVC_CONNMAXREC_SET:
748 		val = *(int *)arg;
749 		if (val <= 0)
750 			return FALSE;
751 		__svc_maxrec = val;
752 		return TRUE;
753 	case RPC_SVC_CONNMAXREC_GET:
754 		*(int *)arg = __svc_maxrec;
755 		return TRUE;
756 	default:
757 		break;
758 	}
759 	return FALSE;
760 }
761