xref: /titanic_41/usr/src/lib/libnsl/rpc/svc_run.c (revision 8ad60789b9913d5b2118fe76d8cd7a661d8ae8b6)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 /*
30  * Portions of this source code were derived from Berkeley
31  * 4.3 BSD under license from the Regents of the University of
32  * California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 /*
38  * This is the rpc server side idle loop
39  * Wait for input, call server program.
40  */
41 #include "mt.h"
42 #include "rpc_mt.h"
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <signal.h>
46 #include <rpc/rpc.h>
47 #include <errno.h>
48 #include <sys/poll.h>
49 #include <sys/types.h>
50 #include <syslog.h>
51 #include <thread.h>
52 #include <assert.h>
53 #include <libintl.h>
54 #include <values.h>
55 
56 extern const char __nsl_dom[];
57 
58 extern int __rpc_compress_pollfd(int, pollfd_t *, pollfd_t *);
59 extern bool_t __is_a_userfd(int P_fd);
60 extern void   __destroy_userfd();
61 extern void clear_pollfd(int);
62 extern void set_pollfd(int /* fd */, short /* events */);
63 extern void svc_getreq_poll();
64 extern void (*__proc_cleanup_cb)();
65 
66 static void start_threads();
67 static void create_pipe();
68 static void clear_pipe();
69 static int select_next_pollfd();
70 static SVCXPRT *make_xprt_copy();
71 static void _svc_run_mt();
72 static void _svc_run();
73 
74 int _svc_prog_dispatch();
75 static void _svc_done_private();
76 
77 extern rwlock_t svc_fd_lock;
78 extern mutex_t	svc_door_mutex;
79 extern cond_t	svc_door_waitcv;
80 extern int	svc_ndoorfds;
81 extern void	__svc_cleanup_door_xprts();
82 extern void	__svc_free_xprtlist();
83 extern void	__svc_getreq_user(struct pollfd *);
84 
85 /*
86  * Maximum fragment size allowed for connection oriented connections.
87  * Zero means that no maximum size limit has been requested.
88  */
89 int __rpc_connmaxrec = 0;
90 
91 /* Inter-Record Timeout in secs for non-blocked connection RPC */
92 int __rpc_irtimeout = 35;
93 
94 /*
95  * Request exclusive access to tcp and udp non-priv ports bound with a
96  * wildcard addr.
97  */
98 bool_t __rpc_tp_exclbind = FALSE;
99 
100 /*
101  * XXX - eventually, all mutexes and their initializations static
102  */
103 
104 /*
105  * Variables used for MT
106  */
107 int svc_mt_mode;		/* multi-threading mode */
108 
109 int svc_pipe[2];	/* pipe for breaking out of poll: read(0), write(1) */
110 
111 /* BEGIN PROTECTED BY svc_mutex */
112 
113 static int svc_thr_max = 16;	/* default maximum number of threads allowed */
114 
115 static int svc_thr_total;	/* current number of threads */
116 
117 static int svc_thr_active;	/* current number of threads active */
118 
119 /* circular array of file descriptors with pending data */
120 
121 #define	CIRCULAR_BUFSIZE	1024
122 
123 static int svc_pending_fds[CIRCULAR_BUFSIZE+1];	/* fds with pending data */
124 
125 static int svc_next_pending;			/* next one to be processed */
126 
127 static int svc_last_pending;			/* last one in list */
128 
129 static int svc_total_pending;			/* total in list */
130 
131 static int svc_thr_total_creates;	/* total created - stats */
132 
133 static int svc_thr_total_create_errors;	/* total create errors - stats */
134 
135 static int svc_waiters;		/* number of waiting threads */
136 
137 /* END PROTECTED BY svc_mutex */
138 
139 /* BEGIN PROTECTED BY svc_fd_lock: */
140 
141 int svc_nfds;		/* total number of active file descriptors */
142 
143 int svc_nfds_set;	/* total number of fd bits set in svc_fdset */
144 
145 int svc_max_fd = 0;	/* largest active file descriptor */
146 
147 int svc_npollfds;	/* total number of active pollfds */
148 
149 int svc_npollfds_set;	/* total number of pollfd set in svc_pollfd */
150 
151 int svc_max_pollfd;	/* largest active pollfd so far */
152 
153 int svc_pollfd_allocd;  /* number of pollfd structures allocated */
154 
155 /* END PROTECTED BY svc_fd_lock: */
156 
157 /* BEGIN PROTECTED BY svc_thr_mutex */
158 
159 #define	POLLSET_EXTEND	256
160 static int svc_pollset_allocd;
161 static struct pollfd *svc_pollset;
162 				/*
163 				 * array of file descriptors currently active
164 				 */
165 static int svc_polled;		/* no of fds polled in last poll() - input */
166 
167 static int svc_pollfds;		/* no of active fds in last poll() - output */
168 
169 static int svc_next_pollfd;	/* next fd  to processin svc_pollset */
170 
171 bool_t svc_polling;		/* true if a thread is polling */
172 
173 /* END PROTECTED BY svc_thr_mutex */
174 
175 /* BEGIN PROTECTED BY svc_exit_mutex */
176 
177 static bool_t svc_exit_done = TRUE;
178 
179 /* END PROTECTED BY svc_exit_mutex */
180 
181 /*
182  * Warlock section
183  */
184 
185 /* VARIABLES PROTECTED BY svc_mutex:
186 	svc_thr_total, svc_thr_active, svc_pending_fds, svc_next_pending,
187 	svc_last_pending, svc_total_pending, svc_thr_total_creates,
188 	svc_thr_total_create_errors,
189 	svcxprt_list_t::next, svcxprt_ext_t::my_xlist,
190 	svc_thr_max, svc_waiters
191  */
192 
193 /* VARIABLES PROTECTED BY svc_fd_lock:
194 	svc_xports, svc_fdset, svc_nfds, svc_nfds_set, svc_max_fd,
195 	svc_pollfd, svc_npollfds, svc_npollfds_set, svc_max_pollfd
196  */
197 
198 /* VARIABLES PROTECTED BY svc_thr_mutex:
199 	svc_pollset, svc_pollfds, svc_next_pollfd, svc_polling
200 	svc_pollset_allocd, svc_polled
201  */
202 
203 /* VARIABLES PROTECTED BY svc_exit_mutex:
204 	svc_exit_done
205  */
206 
207 /* VARIABLES READABLE WITHOUT LOCK:
208 	svc_thr_total, svc_thr_active, svc_thr_total_creates,
209 	svc_thr_total_create_errors,
210 	svc_xports, svc_nfds, svc_nfds_set, svc_max_fd,
211 	svc_npollfds, svc_npollfds_set, svc_max_pollfd,
212 	svc_pollfds, svc_next_pollfd, svc_exit_done, svc_polling,
213 	svc_thr_max, svc_waiters
214  */
215 
216 /* VARIABLES PROTECTED BY "program_logic":
217 	rpc_msg::, svc_req::, svcxprt_ext_t::flags, svc_mt_mode,
218 	svcxprt_ext_t::parent
219  */
220 
221 /* LOCK ORDER:
222 	svc_exit_mutex, svc_thr_mutex, svc_mutex, svc_fd_lock
223  */
224 
225 
226 void
227 svc_run(void)
228 {
229 	/* NO OTHER THREADS ARE RUNNING */
230 
231 	svc_exit_done = FALSE;
232 
233 	while ((svc_npollfds > 0 || svc_ndoorfds > 0) && !svc_exit_done) {
234 		if (svc_npollfds > 0) {
235 			switch (svc_mt_mode) {
236 			case RPC_SVC_MT_NONE:
237 				_svc_run();
238 				break;
239 			default:
240 				_svc_run_mt();
241 				break;
242 			}
243 			continue;
244 		}
245 
246 		(void) mutex_lock(&svc_door_mutex);
247 		if (svc_ndoorfds > 0)
248 			(void) cond_wait(&svc_door_waitcv, &svc_door_mutex);
249 		(void) mutex_unlock(&svc_door_mutex);
250 	}
251 }
252 
253 
254 /*
255  *	This function causes svc_run() to exit by destroying all
256  *	service handles.
257  */
258 void
259 svc_exit(void)
260 {
261 	SVCXPRT	*xprt;
262 	int fd;
263 	char dummy;
264 
265 	/* NO LOCKS HELD */
266 
267 	(void) mutex_lock(&svc_exit_mutex);
268 	if (svc_exit_done) {
269 		(void) mutex_unlock(&svc_exit_mutex);
270 		return;
271 	}
272 	svc_exit_done = TRUE;
273 	for (fd = 0; fd < svc_max_pollfd; fd++) {
274 		xprt = svc_xports[fd];
275 		if (xprt) {
276 			SVC_DESTROY(xprt);
277 		}
278 	}
279 	__svc_free_xprtlist();
280 	__svc_cleanup_door_xprts();
281 	(void) mutex_unlock(&svc_exit_mutex);
282 
283 	if (svc_mt_mode != RPC_SVC_MT_NONE) {
284 		(void) mutex_lock(&svc_mutex);
285 		(void) cond_broadcast(&svc_thr_fdwait);
286 		(void) mutex_unlock(&svc_mutex);
287 
288 		(void) write(svc_pipe[1], &dummy, sizeof (dummy));
289 	}
290 
291 	(void) mutex_lock(&svc_door_mutex);
292 	(void) cond_signal(&svc_door_waitcv);	/* wake up door dispatching */
293 	(void) mutex_unlock(&svc_door_mutex);
294 
295 	/* destroy reactor information if any */
296 	__destroy_userfd();
297 }
298 
299 
300 /*
301  * this funtion is called with svc_fd_lock and svc_thr_mutex
302  */
303 
304 static int
305 alloc_pollset(int npollfds)
306 {
307 	if (npollfds > svc_pollset_allocd) {
308 		pollfd_t *tmp;
309 		do {
310 			svc_pollset_allocd += POLLSET_EXTEND;
311 		} while (npollfds > svc_pollset_allocd);
312 		tmp = realloc(svc_pollset,
313 				sizeof (pollfd_t) * svc_pollset_allocd);
314 		if (tmp == NULL) {
315 			syslog(LOG_ERR, "alloc_pollset: out of memory");
316 			return (-1);
317 		}
318 		svc_pollset = tmp;
319 	}
320 	return (0);
321 }
322 
323 extern int _sigemptyset(sigset_t *);
324 extern int _sigaddset(sigset_t *, int);
325 extern int _sigprocmask(int, const sigset_t *, sigset_t *);
326 
327 static void
328 _svc_run(void)
329 {
330 	sigset_t set, oldset;
331 	int npollfds;
332 	int i;
333 
334 	/*
335 	 * Block SIGALRM while doing work.  Unblock it while doing poll().
336 	 * This is so that services like rpc.rstatd can cause the poll()
337 	 * to be interrupted due to alarm() but that we don't end up in
338 	 * an MT-unsafe signal handler at an inopportune time.
339 	 */
340 	(void) _sigemptyset(&set);
341 	(void) _sigaddset(&set, SIGALRM);
342 	(void) _sigprocmask(SIG_BLOCK, &set, &oldset);
343 	while (!svc_exit_done) {
344 		/*
345 		 * Check whether there is any server fd on which we may want
346 		 * to wait.
347 		 */
348 		(void) rw_rdlock(&svc_fd_lock);
349 		if (alloc_pollset(svc_npollfds) == -1)
350 			break;
351 		npollfds = __rpc_compress_pollfd(svc_max_pollfd,
352 			svc_pollfd, svc_pollset);
353 		(void) rw_unlock(&svc_fd_lock);
354 		if (npollfds == 0)
355 			break;	/* None waiting, hence return */
356 
357 		(void) _sigprocmask(SIG_SETMASK, &oldset, NULL);
358 		i = poll(svc_pollset, npollfds, -1);
359 		(void) _sigprocmask(SIG_BLOCK, &set, &oldset);
360 		switch (i) {
361 		case -1:
362 			/*
363 			 * We ignore all errors, continuing with the assumption
364 			 * that it was set by the signal handlers (or any
365 			 * other outside event) and not caused by poll().
366 			 */
367 		case 0:
368 			continue;
369 		default:
370 			svc_getreq_poll(svc_pollset, i);
371 		}
372 	}
373 	(void) _sigprocmask(SIG_SETMASK, &oldset, NULL);
374 }
375 
376 /*
377  * In _svc_run_mt, myfd is linked with mypollfd
378  * svc_pollset[mypollfd].fd == myfd
379  * However, in some cases, the link can not be made, thus we define the
380  * following values for these special cases
381  */
382 enum {
383 	INVALID_POLLFD	= -200,
384 	FD_FROM_PENDING
385 };
386 
387 static void
388 _svc_run_mt(void)
389 {
390 	int npollfds;
391 	int n_polled, dispatch;
392 
393 	static bool_t first_time = TRUE;
394 	bool_t main_thread = FALSE;
395 	int n_new;
396 	int myfd, mypollfd;
397 	SVCXPRT *parent_xprt, *xprt;
398 
399 	/*
400 	 * Server is multi-threaded.  Do "first time" initializations.
401 	 * Since only one thread exists in the beginning, there's no
402 	 * need for mutex protection for first time initializations.
403 	 */
404 	if (first_time) {
405 		first_time = FALSE;
406 		main_thread = TRUE;
407 		svc_thr_total = 1;	/* this thread */
408 		svc_next_pending = svc_last_pending = 0;
409 
410 		/*
411 		 * Create a pipe for waking up the poll, if new
412 		 * descriptors have been added to svc_fdset.
413 		 */
414 		create_pipe();
415 	}
416 
417 	/* OTHER THREADS ARE RUNNING */
418 
419 	if (svc_exit_done)
420 		return;
421 
422 	for (;;) {
423 		/*
424 		 * svc_thr_mutex prevents more than one thread from
425 		 * trying to select a descriptor to process further.
426 		 * svc_thr_mutex is unlocked after a thread selects
427 		 * a descriptor on which to receive data.  If there are
428 		 * no such descriptors, the thread will poll with
429 		 * svc_thr_mutex locked, after unlocking all other
430 		 * locks.  This prevents more than one thread from
431 		 * trying to poll at the same time.
432 		 */
433 		(void) mutex_lock(&svc_thr_mutex);
434 		(void) mutex_lock(&svc_mutex);
435 continue_with_locks:
436 		myfd = -1;
437 		mypollfd = INVALID_POLLFD;
438 
439 		/*
440 		 * Check if there are any descriptors with data pending.
441 		 */
442 		if (svc_total_pending > 0) {
443 			myfd = svc_pending_fds[svc_next_pending++];
444 			mypollfd = FD_FROM_PENDING;
445 			if (svc_next_pending > CIRCULAR_BUFSIZE)
446 				svc_next_pending = 0;
447 			svc_total_pending--;
448 		}
449 
450 		/*
451 		 * Get the next active file descriptor to process.
452 		 */
453 		if (myfd == -1 && svc_pollfds == 0) {
454 			/*
455 			 * svc_pollset is empty; do polling
456 			 */
457 			svc_polling = TRUE;
458 
459 			/*
460 			 * if there are no file descriptors, return
461 			 */
462 			(void) rw_rdlock(&svc_fd_lock);
463 			if (svc_npollfds == 0 ||
464 					alloc_pollset(svc_npollfds + 1) == -1) {
465 				(void) rw_unlock(&svc_fd_lock);
466 				svc_polling = FALSE;
467 				svc_thr_total--;
468 				(void) mutex_unlock(&svc_mutex);
469 				(void) mutex_unlock(&svc_thr_mutex);
470 				if (!main_thread) {
471 					thr_exit(NULL);
472 					/* NOTREACHED */
473 				}
474 				break;
475 			}
476 
477 			npollfds = __rpc_compress_pollfd(svc_max_pollfd,
478 					svc_pollfd, svc_pollset);
479 			(void) rw_unlock(&svc_fd_lock);
480 
481 			if (npollfds == 0) {
482 				/*
483 				 * There are file descriptors, but none of them
484 				 * are available for polling.  If this is the
485 				 * main thread, or if no thread is waiting,
486 				 * wait on condition variable, otherwise exit.
487 				 */
488 				svc_polling = FALSE;
489 				(void) mutex_unlock(&svc_thr_mutex);
490 				if ((!main_thread) && svc_waiters > 0) {
491 					svc_thr_total--;
492 					(void) mutex_unlock(&svc_mutex);
493 					thr_exit(NULL);
494 					/* NOTREACHED */
495 				}
496 
497 				while (svc_npollfds_set == 0 &&
498 					svc_pollfds == 0 &&
499 					svc_total_pending == 0 &&
500 							!svc_exit_done) {
501 					svc_waiters++;
502 					(void) cond_wait(&svc_thr_fdwait,
503 								&svc_mutex);
504 					svc_waiters--;
505 				}
506 
507 				/*
508 				 * Check exit flag.  If this is not the main
509 				 * thread, exit.
510 				 */
511 				if (svc_exit_done) {
512 					svc_thr_total--;
513 					(void) mutex_unlock(&svc_mutex);
514 					if (!main_thread)
515 						thr_exit(NULL);
516 					break;
517 				}
518 
519 				(void) mutex_unlock(&svc_mutex);
520 				continue;
521 			}
522 
523 			/*
524 			 * We're ready to poll.  Always set svc_pipe[0]
525 			 * as the last one, since the poll will occasionally
526 			 * need to be interrupted.  Release svc_mutex for
527 			 * the duration of the poll, but hold on to
528 			 * svc_thr_mutex, as we don't want any other thread
529 			 * to do the same.
530 			 */
531 			svc_pollset[npollfds].fd = svc_pipe[0];
532 			svc_pollset[npollfds].events = MASKVAL;
533 
534 			do {
535 				int i, j;
536 
537 				(void) mutex_unlock(&svc_mutex);
538 				n_polled = poll(svc_pollset, npollfds + 1, -1);
539 				(void) mutex_lock(&svc_mutex);
540 				if (n_polled <= 0)
541 					continue;
542 
543 				/*
544 				 * Check if information returned indicates one
545 				 * or more closed fd's; find and remove any such
546 				 * information
547 				 */
548 				for (i = 0; i <= npollfds; i++) {
549 					if (svc_pollset[i].revents & POLLNVAL) {
550 						/* Overwrite svc_pollset[i] */
551 						for (j = i; j < npollfds; j++)
552 							svc_pollset[j] =
553 							    svc_pollset[j + 1];
554 						(void) memset(&svc_pollset[j],
555 						    0, sizeof (struct pollfd));
556 						npollfds--;
557 						n_polled--;
558 						i--;
559 					}
560 				}
561 			} while (n_polled <= 0);
562 			svc_polling = FALSE;
563 
564 			/*
565 			 * If there's data in the pipe, clear it.
566 			 */
567 			if (svc_pollset[npollfds].revents) {
568 				clear_pipe();
569 				n_polled--;
570 				svc_pollset[npollfds].revents = 0;
571 			}
572 			svc_polled = npollfds;
573 			svc_pollfds = n_polled;
574 			svc_next_pollfd = 0;
575 
576 			/*
577 			 * Check exit flag.
578 			 */
579 			if (svc_exit_done) {
580 				svc_thr_total--;
581 				(void) mutex_unlock(&svc_mutex);
582 				(void) mutex_unlock(&svc_thr_mutex);
583 				if (!main_thread) {
584 					thr_exit(NULL);
585 					/* NOTREACHED */
586 				}
587 				break;
588 			}
589 
590 			/*
591 			 * If no descriptor is active, continue.
592 			 */
593 			if (svc_pollfds == 0)
594 				goto continue_with_locks;
595 		}
596 
597 		/*
598 		 * If a file descriptor has already not been selected,
599 		 * choose a file descriptor.
600 		 * svc_pollfds and svc_next_pollfd are updated.
601 		 */
602 		if (myfd == -1) {
603 			if (select_next_pollfd(&myfd, &mypollfd) == -1)
604 				goto continue_with_locks;
605 		}
606 
607 		/*
608 		 * Check to see if new threads need to be started.
609 		 * Count of threads that could be gainfully employed is
610 		 * obtained as follows:
611 		 *	- count 1 for poller
612 		 *	- count 1 for this request
613 		 *	- count active file descriptors (svc_pollfds)
614 		 *	- count pending file descriptors
615 		 *
616 		 * (svc_thr_total - svc_thr_active) are already available.
617 		 * This thread is one of the available threads.
618 		 *
619 		 * Number of new threads should not exceed
620 		 *	(svc_thr_max - svc_thr_total).
621 		 */
622 		if (svc_thr_total < svc_thr_max &&
623 			    svc_mt_mode == RPC_SVC_MT_AUTO && !svc_exit_done) {
624 			n_new = 1 + 1 + svc_pollfds + svc_total_pending -
625 					(svc_thr_total - svc_thr_active);
626 			if (n_new > (svc_thr_max - svc_thr_total))
627 				n_new = svc_thr_max - svc_thr_total;
628 			if (n_new > 0)
629 				start_threads(n_new);
630 		}
631 
632 		/*
633 		 * Get parent xprt.  It is possible for the parent service
634 		 * handle to be destroyed by now, due to a race condition.
635 		 * Check for this, and if so, log a warning and go on.
636 		 */
637 		parent_xprt = svc_xports[myfd];
638 		if (parent_xprt == NULL) {
639 			/* Check if it is not a user FD */
640 			if (__is_a_userfd(myfd) == TRUE)
641 				__svc_getreq_user(&(svc_pollset[mypollfd]));
642 			goto continue_with_locks;
643 		}
644 /* LINTED pointer alignment */
645 		if (svc_defunct(parent_xprt) || svc_failed(parent_xprt))
646 			goto continue_with_locks;
647 
648 		/*
649 		 * Make a copy of parent xprt, update svc_fdset.
650 		 */
651 		if ((xprt = make_xprt_copy(parent_xprt)) == NULL)
652 			goto continue_with_locks;
653 
654 		/*
655 		 * Keep track of active threads in automatic mode.
656 		 */
657 		if (svc_mt_mode == RPC_SVC_MT_AUTO)
658 			svc_thr_active++;
659 
660 		/*
661 		 * Release mutexes so other threads can get going.
662 		 */
663 		(void) mutex_unlock(&svc_mutex);
664 		(void) mutex_unlock(&svc_thr_mutex);
665 
666 		/*
667 		 * Process request.
668 		 */
669 		{
670 			struct rpc_msg *msg;
671 			struct svc_req *r;
672 			char *cred_area;
673 
674 /* LINTED pointer alignment */
675 			msg = SVCEXT(xprt)->msg;
676 /* LINTED pointer alignment */
677 			r = SVCEXT(xprt)->req;
678 /* LINTED pointer alignment */
679 			cred_area = SVCEXT(xprt)->cred_area;
680 
681 
682 			msg->rm_call.cb_cred.oa_base = cred_area;
683 			msg->rm_call.cb_verf.oa_base =
684 						&(cred_area[MAX_AUTH_BYTES]);
685 			r->rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
686 
687 			/*
688 			 * receive RPC message
689 			 */
690 			if ((dispatch = SVC_RECV(xprt, msg))) {
691 				if (svc_mt_mode != RPC_SVC_MT_NONE)
692 /* LINTED pointer alignment */
693 					svc_flags(xprt) |= SVC_ARGS_CHECK;
694 				dispatch = _svc_prog_dispatch(xprt, msg, r);
695 
696 				/*
697 				 * Call cleanup procedure if set.
698 				 */
699 				if (__proc_cleanup_cb != NULL)
700 					(*__proc_cleanup_cb)(xprt);
701 			} else
702 				svc_args_done(xprt);
703 
704 			/*
705 			 * Finish up, if automatic mode, or not dispatched.
706 			 */
707 			if (svc_mt_mode == RPC_SVC_MT_AUTO || !dispatch) {
708 /* LINTED pointer alignment */
709 				if (svc_flags(xprt) & SVC_ARGS_CHECK)
710 					svc_args_done(xprt);
711 				(void) mutex_lock(&svc_mutex);
712 				_svc_done_private(xprt);
713 				if (svc_mt_mode == RPC_SVC_MT_AUTO) {
714 					/*
715 					 * not active any more
716 					 */
717 					svc_thr_active--;
718 
719 					/*
720 					 * If not main thread, exit unless
721 					 * there's some immediate work.
722 					 */
723 					if (!main_thread &&
724 						    svc_pollfds <= 0 &&
725 						    svc_total_pending <= 0 &&
726 						    (svc_polling ||
727 							svc_waiters > 0)) {
728 						svc_thr_total--;
729 						if (svc_thr_total ==
730 						    svc_waiters) {
731 							(void) cond_broadcast(
732 							    &svc_thr_fdwait);
733 						}
734 						(void) mutex_unlock(&svc_mutex);
735 						thr_exit(NULL);
736 						/* NOTREACHED */
737 					}
738 				}
739 				(void) mutex_unlock(&svc_mutex);
740 			}
741 		}
742 
743 	}
744 }
745 
746 
747 /*
748  * start_threads() - Start specified number of threads.
749  */
750 static void
751 start_threads(int num_threads)
752 {
753 	int		i;
754 
755 	assert(MUTEX_HELD(&svc_mutex));
756 
757 	for (i = 0; i < num_threads; i++) {
758 		if (thr_create(NULL, 0, (void *(*)(void *))_svc_run_mt, NULL,
759 		    THR_DETACHED, NULL) == 0) {
760 			svc_thr_total++;
761 			svc_thr_total_creates++;
762 		} else {
763 			svc_thr_total_create_errors++;
764 		}
765 	}
766 }
767 
768 
769 /*
770  * create_pipe() - create pipe for breaking out of poll.
771  */
772 static void
773 create_pipe(void)
774 {
775 	if (pipe(svc_pipe) == -1) {
776 		syslog(LOG_ERR, dgettext(__nsl_dom,
777 				"RPC: svc could not create pipe - exiting"));
778 		exit(1);
779 	}
780 	if (_fcntl(svc_pipe[0], F_SETFL, O_NONBLOCK) == -1) {
781 		syslog(LOG_ERR, dgettext(__nsl_dom,
782 					"RPC: svc pipe error - exiting"));
783 		exit(1);
784 	}
785 	if (_fcntl(svc_pipe[1], F_SETFL, O_NONBLOCK) == -1) {
786 		syslog(LOG_ERR, dgettext(__nsl_dom,
787 					"RPC: svc pipe error - exiting"));
788 		exit(1);
789 	}
790 }
791 
792 
793 /*
794  * clear_pipe() - Empty data in pipe.
795  */
796 static void
797 clear_pipe(void)
798 {
799 	char	buf[16];
800 	int	i;
801 
802 	do {
803 		i = read(svc_pipe[0], buf, sizeof (buf));
804 	} while (i == sizeof (buf));
805 }
806 
807 
808 /*
809  * select_next_pollfd() - Select the next active fd in svc_pollset.
810  */
811 static int
812 select_next_pollfd(int *fd, int *pollfdIndex)
813 {
814 	int i;
815 
816 	assert(MUTEX_HELD(&svc_thr_mutex));
817 	assert(MUTEX_HELD(&svc_mutex));
818 
819 	for (i = svc_next_pollfd; svc_pollfds > 0 && i < svc_polled;
820 							i++) {
821 		if (svc_pollset[i].revents) {
822 			svc_pollfds--;
823 			/*
824 			 * No more special case for POLLNVAL, because it may
825 			 * be linked with a user file descriptot callback
826 			 */
827 			svc_next_pollfd = i + 1;
828 
829 			*fd = svc_pollset[i].fd;
830 			*pollfdIndex = i;
831 
832 			return (0);
833 		}
834 	}
835 	svc_next_pollfd = svc_pollfds = 0;
836 	*fd = -1;
837 	*pollfdIndex = INVALID_POLLFD;
838 	return (-1);
839 }
840 
841 
842 /*
843  * make_xprt_copy() - make a copy of the parent xprt.
844  * Clear fd bit in svc_fdset.
845  */
846 static SVCXPRT *
847 make_xprt_copy(SVCXPRT *parent)
848 {
849 /* LINTED pointer alignment */
850 	SVCXPRT_LIST	*xlist = SVCEXT(parent)->my_xlist;
851 	SVCXPRT_LIST	*xret;
852 	SVCXPRT		*xprt;
853 	int		fd = parent->xp_fd;
854 
855 	assert(MUTEX_HELD(&svc_mutex));
856 
857 	xret = xlist->next;
858 	if (xret) {
859 		xlist->next = xret->next;
860 		xret->next = NULL;
861 		xprt = xret->xprt;
862 /* LINTED pointer alignment */
863 		svc_flags(xprt) = svc_flags(parent);
864 	} else
865 		xprt = svc_copy(parent);
866 
867 	if (xprt) {
868 /* LINTED pointer alignment */
869 		SVCEXT(parent)->refcnt++;
870 		(void) rw_wrlock(&svc_fd_lock);
871 		clear_pollfd(fd);
872 		(void) rw_unlock(&svc_fd_lock);
873 	}
874 	return (xprt);
875 }
876 
877 /*
878  * _svc_done_private() - return copies to library.
879  */
880 static void
881 _svc_done_private(SVCXPRT *xprt)
882 {
883 	SVCXPRT		*parent;
884 	SVCXPRT_LIST	*xhead, *xlist;
885 
886 	assert(MUTEX_HELD(&svc_mutex));
887 
888 /* LINTED pointer alignment */
889 	if ((parent = SVCEXT(xprt)->parent) == NULL)
890 		return;
891 
892 /* LINTED pointer alignment */
893 	xhead = SVCEXT(parent)->my_xlist;
894 /* LINTED pointer alignment */
895 	xlist = SVCEXT(xprt)->my_xlist;
896 	xlist->next = xhead->next;
897 	xhead->next = xlist;
898 
899 /* LINTED pointer alignment */
900 	SVCEXT(parent)->refcnt--;
901 
902 	/*
903 	 * Propagate any error flags.  This is done in both directions to
904 	 * ensure that if one child gets an error, everyone will see it
905 	 * (even if there are multiple outstanding children) and the
906 	 * transport will get closed.
907 	 */
908 /* LINTED pointer alignment */
909 	svc_flags(xprt) |= svc_flags(parent);
910 /* LINTED pointer alignment */
911 	if (svc_failed(xprt) || svc_defunct(xprt)) {
912 /* LINTED pointer alignment */
913 		svc_flags(parent) |= (svc_flags(xprt) &
914 				(SVC_FAILED | SVC_DEFUNCT));
915 /* LINTED pointer alignment */
916 		if (SVCEXT(parent)->refcnt == 0)
917 			_svc_destroy_private(xprt);
918 	}
919 }
920 
921 void
922 svc_done(SVCXPRT *xprt)
923 {
924 	if (svc_mt_mode != RPC_SVC_MT_USER)
925 		return;
926 
927 	/*
928 	 * Make sure file descriptor is released in user mode.
929 	 * If the xprt is a door, do nothing: this work is performed by
930 	 * svc_door.c's return_xprt_copy() routine, which is basically a
931 	 * door-specific copy of _svc_done_private().
932 	 */
933 /* LINTED pointer alignment */
934 	if (svc_type(xprt) == SVC_DOOR)
935 		return;
936 
937 /* LINTED pointer alignment */
938 	if (svc_flags(xprt) & SVC_ARGS_CHECK)
939 		svc_args_done(xprt);
940 
941 	(void) mutex_lock(&svc_mutex);
942 	_svc_done_private(xprt);
943 	(void) mutex_unlock(&svc_mutex);
944 }
945 
946 
947 /*
948  * Mark argument completion.  Release file descriptor.
949  */
950 void
951 svc_args_done(SVCXPRT *xprt)
952 {
953 	char	dummy;
954 /* LINTED pointer alignment */
955 	SVCXPRT	*parent = SVCEXT(xprt)->parent;
956 	bool_t	wake_up_poller;
957 	enum	xprt_stat stat;
958 
959 /* LINTED pointer alignment */
960 	svc_flags(xprt) |= svc_flags(parent);
961 /* LINTED pointer alignment */
962 	svc_flags(xprt) &= ~SVC_ARGS_CHECK;
963 /* LINTED pointer alignment */
964 	if (svc_failed(xprt) || svc_defunct(parent))
965 		return;
966 
967 /* LINTED pointer alignment */
968 	if (svc_type(xprt) == SVC_CONNECTION &&
969 				(stat = SVC_STAT(xprt)) != XPRT_IDLE) {
970 		if (stat == XPRT_MOREREQS) {
971 			(void) mutex_lock(&svc_mutex);
972 			svc_pending_fds[svc_last_pending++] = xprt->xp_fd;
973 			if (svc_last_pending > CIRCULAR_BUFSIZE)
974 				svc_last_pending = 0;
975 			svc_total_pending++;
976 			(void) mutex_unlock(&svc_mutex);
977 			wake_up_poller = FALSE;
978 		} else {
979 			/*
980 			 * connection failed
981 			 */
982 			return;
983 		}
984 	} else {
985 		(void) rw_wrlock(&svc_fd_lock);
986 		set_pollfd(xprt->xp_fd, MASKVAL);
987 		(void) rw_unlock(&svc_fd_lock);
988 		wake_up_poller = TRUE;
989 	}
990 
991 	if (!wake_up_poller || !svc_polling) {
992 		/*
993 		 * Wake up any waiting threads.
994 		 */
995 		(void) mutex_lock(&svc_mutex);
996 		if (svc_waiters > 0) {
997 			(void) cond_broadcast(&svc_thr_fdwait);
998 			(void) mutex_unlock(&svc_mutex);
999 			return;
1000 		}
1001 		(void) mutex_unlock(&svc_mutex);
1002 	}
1003 
1004 	/*
1005 	 * Wake up any polling thread.
1006 	 */
1007 	if (svc_polling)
1008 		(void) write(svc_pipe[1], &dummy, sizeof (dummy));
1009 }
1010 
1011 
1012 int
1013 __rpc_legal_connmaxrec(int suggested) {
1014 	if (suggested == -1) {
1015 		/* Supply default */
1016 		return (RPC_MAXDATASIZE + 2*sizeof (uint32_t));
1017 	} else if (suggested < 0) {
1018 		return (-1);
1019 	} else if (suggested > 0) {
1020 		/* Round down to multiple of BYTES_PER_XDR_UNIT */
1021 		suggested -= suggested % BYTES_PER_XDR_UNIT;
1022 		/* If possible, allow for two fragment headers */
1023 		if (suggested < MAXINT-(2*sizeof (uint32_t))) {
1024 			/* Allow for two fragment headers */
1025 			suggested += 2 * sizeof (uint32_t);
1026 		} else {
1027 			suggested = MAXINT;
1028 		}
1029 		if (suggested < sizeof (struct rpc_msg)) {
1030 			return (-1);
1031 		}
1032 	}
1033 	return (suggested);
1034 }
1035 
1036 
1037 bool_t
1038 rpc_control(int op, void *info)
1039 {
1040 	int		tmp;
1041 	extern int	__rpc_minfd;
1042 
1043 	switch (op) {
1044 	case RPC_SVC_MTMODE_SET:
1045 		tmp = *((int *)info);
1046 		if (tmp != RPC_SVC_MT_NONE && tmp != RPC_SVC_MT_AUTO &&
1047 						tmp != RPC_SVC_MT_USER)
1048 			return (FALSE);
1049 		if (svc_mt_mode != RPC_SVC_MT_NONE && svc_mt_mode != tmp)
1050 			return (FALSE);
1051 		svc_mt_mode = tmp;
1052 		return (TRUE);
1053 	case RPC_SVC_MTMODE_GET:
1054 		*((int *)info) = svc_mt_mode;
1055 		return (TRUE);
1056 	case RPC_SVC_THRMAX_SET:
1057 		if ((tmp = *((int *)info)) < 1)
1058 			return (FALSE);
1059 		(void) mutex_lock(&svc_mutex);
1060 		svc_thr_max = tmp;
1061 		(void) mutex_unlock(&svc_mutex);
1062 		return (TRUE);
1063 	case RPC_SVC_THRMAX_GET:
1064 		*((int *)info) = svc_thr_max;
1065 		return (TRUE);
1066 	case RPC_SVC_THRTOTAL_GET:
1067 		*((int *)info) = svc_thr_total;
1068 		return (TRUE);
1069 	case RPC_SVC_THRCREATES_GET:
1070 		*((int *)info) = svc_thr_total_creates;
1071 		return (TRUE);
1072 	case RPC_SVC_THRERRORS_GET:
1073 		*((int *)info) = svc_thr_total_create_errors;
1074 		return (TRUE);
1075 	case RPC_SVC_USE_POLLFD:
1076 		if (*((int *)info) && !__rpc_use_pollfd_done) {
1077 			__rpc_use_pollfd_done = 1;
1078 			return (TRUE);
1079 		}
1080 		return (FALSE);
1081 	case __RPC_CLNT_MINFD_SET:
1082 		tmp = *((int *)info);
1083 		if (tmp < 0)
1084 			return (FALSE);
1085 		__rpc_minfd = tmp;
1086 		return (TRUE);
1087 	case __RPC_CLNT_MINFD_GET:
1088 		*((int *)info) = __rpc_minfd;
1089 		return (TRUE);
1090 	case RPC_SVC_CONNMAXREC_SET:
1091 		tmp = __rpc_legal_connmaxrec(*(int *)info);
1092 		if (tmp >= 0) {
1093 			__rpc_connmaxrec = tmp;
1094 			return (TRUE);
1095 		} else {
1096 			return (FALSE);
1097 		}
1098 	case RPC_SVC_CONNMAXREC_GET:
1099 		*((int *)info) = __rpc_connmaxrec;
1100 		return (TRUE);
1101 	case RPC_SVC_IRTIMEOUT_SET:
1102 		tmp = *((int *)info);
1103 		if (tmp >= 0) {
1104 			__rpc_irtimeout = tmp;
1105 			return (TRUE);
1106 		} else {
1107 			return (FALSE);
1108 		}
1109 	/*
1110 	 * No mutex necessary as _EXCLBIND_SET will/should only
1111 	 * be used before an RPC daemon goes mt-hot.
1112 	 */
1113 	case __RPC_SVC_EXCLBIND_SET:
1114 		if (info) {
1115 			__rpc_tp_exclbind = *((bool_t *)info);
1116 			return (TRUE);
1117 		}
1118 		return (FALSE);
1119 	case __RPC_SVC_EXCLBIND_GET:
1120 		if (info) {
1121 			*((bool_t *)info) = __rpc_tp_exclbind;
1122 			return (TRUE);
1123 		}
1124 		return (FALSE);
1125 
1126 	default:
1127 		return (FALSE);
1128 	}
1129 }
1130