xref: /illumos-gate/usr/src/lib/libnsl/rpc/svc.c (revision b1cdc720)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
59acbbeafSnn  * Common Development and Distribution License (the "License").
69acbbeafSnn  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2161961e0fSrobinson 
227c478bd9Sstevel@tonic-gate /*
239ff75adeSSurya Prakki  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
2429d91154SMarcel Telka  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
2548bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
26*b1cdc720SAlex Wilson  * Copyright 2017 Joyent Inc
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
327c478bd9Sstevel@tonic-gate /* All Rights Reserved */
337c478bd9Sstevel@tonic-gate /*
347c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
357c478bd9Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
367c478bd9Sstevel@tonic-gate  * California.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * svc.c, Server-side remote procedure call interface.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * There are two sets of procedures here.  The xprt routines are
437c478bd9Sstevel@tonic-gate  * for handling transport handles.  The svc routines handle the
447c478bd9Sstevel@tonic-gate  * list of service routines.
457c478bd9Sstevel@tonic-gate  *
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include "mt.h"
497c478bd9Sstevel@tonic-gate #include "rpc_mt.h"
507c478bd9Sstevel@tonic-gate #include <assert.h>
517c478bd9Sstevel@tonic-gate #include <errno.h>
527c478bd9Sstevel@tonic-gate #include <sys/types.h>
537c478bd9Sstevel@tonic-gate #include <stropts.h>
547c478bd9Sstevel@tonic-gate #include <sys/conf.h>
557c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
56*b1cdc720SAlex Wilson #include <rpc/auth.h>
577c478bd9Sstevel@tonic-gate #ifdef PORTMAP
587c478bd9Sstevel@tonic-gate #include <rpc/pmap_clnt.h>
597c478bd9Sstevel@tonic-gate #endif
607c478bd9Sstevel@tonic-gate #include <sys/poll.h>
617c478bd9Sstevel@tonic-gate #include <netconfig.h>
627c478bd9Sstevel@tonic-gate #include <syslog.h>
637c478bd9Sstevel@tonic-gate #include <stdlib.h>
647c478bd9Sstevel@tonic-gate #include <unistd.h>
657c478bd9Sstevel@tonic-gate #include <string.h>
667c478bd9Sstevel@tonic-gate #include <limits.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate extern bool_t __svc_get_door_cred();
697c478bd9Sstevel@tonic-gate extern bool_t __rpc_get_local_cred();
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate SVCXPRT **svc_xports;
727c478bd9Sstevel@tonic-gate static int nsvc_xports; 	/* total number of svc_xports allocated */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate XDR **svc_xdrs;		/* common XDR receive area */
757c478bd9Sstevel@tonic-gate int nsvc_xdrs;		/* total number of svc_xdrs allocated */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate int __rpc_use_pollfd_done;	/* to unlimit the number of connections */
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #define	NULL_SVC ((struct svc_callout *)0)
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * The services list
837c478bd9Sstevel@tonic-gate  * Each entry represents a set of procedures (an rpc program).
847c478bd9Sstevel@tonic-gate  * The dispatch routine takes request structs and runs the
857c478bd9Sstevel@tonic-gate  * appropriate procedure.
867c478bd9Sstevel@tonic-gate  */
877c478bd9Sstevel@tonic-gate static struct svc_callout {
887c478bd9Sstevel@tonic-gate 	struct svc_callout *sc_next;
897c478bd9Sstevel@tonic-gate 	rpcprog_t	    sc_prog;
907c478bd9Sstevel@tonic-gate 	rpcvers_t	    sc_vers;
917c478bd9Sstevel@tonic-gate 	char		   *sc_netid;
927c478bd9Sstevel@tonic-gate 	void		    (*sc_dispatch)();
937c478bd9Sstevel@tonic-gate } *svc_head;
947c478bd9Sstevel@tonic-gate extern rwlock_t	svc_lock;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static struct svc_callout *svc_find();
977c478bd9Sstevel@tonic-gate int _svc_prog_dispatch();
987c478bd9Sstevel@tonic-gate void svc_getreq_common();
997c478bd9Sstevel@tonic-gate char *strdup();
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate extern mutex_t	svc_door_mutex;
1027c478bd9Sstevel@tonic-gate extern cond_t	svc_door_waitcv;
1037c478bd9Sstevel@tonic-gate extern int	svc_ndoorfds;
1047c478bd9Sstevel@tonic-gate extern SVCXPRT_LIST *_svc_xprtlist;
1057c478bd9Sstevel@tonic-gate extern mutex_t xprtlist_lock;
1067c478bd9Sstevel@tonic-gate extern void __svc_rm_from_xlist();
1077c478bd9Sstevel@tonic-gate 
1082209d3c8SRichard Lowe #if !defined(_LP64)
1097c478bd9Sstevel@tonic-gate extern fd_set _new_svc_fdset;
1102209d3c8SRichard Lowe #endif
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /*
1137c478bd9Sstevel@tonic-gate  * If the allocated array of reactor is too small, this value is used as a
1147c478bd9Sstevel@tonic-gate  * margin. This reduces the number of allocations.
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate #define	USER_FD_INCREMENT 5
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate static void add_pollfd(int fd, short events);
1197c478bd9Sstevel@tonic-gate static void remove_pollfd(int fd);
1207c478bd9Sstevel@tonic-gate static void __svc_remove_input_of_fd(int fd);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * Data used to handle reactor:
1247c478bd9Sstevel@tonic-gate  * 	- one file descriptor we listen to,
1257c478bd9Sstevel@tonic-gate  *	- one callback we call if the fd pops,
1267c478bd9Sstevel@tonic-gate  *	- and a cookie passed as a parameter to the callback.
1277c478bd9Sstevel@tonic-gate  *
1287c478bd9Sstevel@tonic-gate  * The structure is an array indexed on the file descriptor. Each entry is
1297c478bd9Sstevel@tonic-gate  * pointing to the first element of a double-linked list of callback.
1307c478bd9Sstevel@tonic-gate  * only one callback may be associated to a couple (fd, event).
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate struct _svc_user_fd_head;
1347c478bd9Sstevel@tonic-gate 
13561961e0fSrobinson typedef struct {
1367c478bd9Sstevel@tonic-gate 	struct _svc_user_fd_node *next;
1377c478bd9Sstevel@tonic-gate 	struct _svc_user_fd_node *previous;
1387c478bd9Sstevel@tonic-gate } _svc_user_link;
1397c478bd9Sstevel@tonic-gate 
14061961e0fSrobinson typedef struct _svc_user_fd_node {
1417c478bd9Sstevel@tonic-gate 	_svc_user_link lnk;
1427c478bd9Sstevel@tonic-gate 	svc_input_id_t id;
1437c478bd9Sstevel@tonic-gate 	int	    fd;
1447c478bd9Sstevel@tonic-gate 	unsigned int   events;
1457c478bd9Sstevel@tonic-gate 	svc_callback_t callback;
1467c478bd9Sstevel@tonic-gate 	void*	  cookie;
1477c478bd9Sstevel@tonic-gate } _svc_user_fd_node;
1487c478bd9Sstevel@tonic-gate 
14961961e0fSrobinson typedef struct _svc_user_fd_head {
15029d91154SMarcel Telka 	struct _svc_user_fd_node *list;
1517c478bd9Sstevel@tonic-gate 	unsigned int mask;    /* logical OR of all sub-masks */
1527c478bd9Sstevel@tonic-gate } _svc_user_fd_head;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /* Array of defined reactor - indexed on file descriptor */
15629d91154SMarcel Telka static _svc_user_fd_head *svc_userfds = NULL;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /* current size of file descriptor */
1597c478bd9Sstevel@tonic-gate static int svc_nuserfds = 0;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /* Mutex to ensure MT safe operations for user fds callbacks. */
1627c478bd9Sstevel@tonic-gate static mutex_t svc_userfds_lock = DEFAULTMUTEX;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * This structure is used to have constant time alogrithms. There is an array
1677c478bd9Sstevel@tonic-gate  * of this structure as large as svc_nuserfds. When the user is registering a
1687c478bd9Sstevel@tonic-gate  * new callback, the address of the created structure is stored in a cell of
1697c478bd9Sstevel@tonic-gate  * this array. The address of this cell is the returned unique identifier.
1707c478bd9Sstevel@tonic-gate  *
1717c478bd9Sstevel@tonic-gate  * On removing, the id is given by the user, then we know if this cell is
1727c478bd9Sstevel@tonic-gate  * filled or not (with free). If it is free, we return an error. Otherwise,
1737c478bd9Sstevel@tonic-gate  * we can free the structure pointed by fd_node.
1747c478bd9Sstevel@tonic-gate  *
1757c478bd9Sstevel@tonic-gate  * On insertion, we use the linked list created by (first_free,
1767c478bd9Sstevel@tonic-gate  * next_free). In this way with a constant time computation, we can give a
1777c478bd9Sstevel@tonic-gate  * correct index to the user.
1787c478bd9Sstevel@tonic-gate  */
1797c478bd9Sstevel@tonic-gate 
18061961e0fSrobinson typedef struct _svc_management_user_fd {
1817c478bd9Sstevel@tonic-gate 	bool_t free;
1827c478bd9Sstevel@tonic-gate 	union {
1837c478bd9Sstevel@tonic-gate 		svc_input_id_t next_free;
1847c478bd9Sstevel@tonic-gate 		_svc_user_fd_node *fd_node;
1857c478bd9Sstevel@tonic-gate 	} data;
1867c478bd9Sstevel@tonic-gate } _svc_management_user_fd;
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate /* index to the first free elem */
1897c478bd9Sstevel@tonic-gate static svc_input_id_t first_free = (svc_input_id_t)-1;
1907c478bd9Sstevel@tonic-gate /* the size of this array is the same as svc_nuserfds */
1917c478bd9Sstevel@tonic-gate static _svc_management_user_fd* user_fd_mgt_array = NULL;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate /* current size of user_fd_mgt_array */
1947c478bd9Sstevel@tonic-gate static int svc_nmgtuserfds = 0;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /* Define some macros to access data associated to registration ids. */
1987c478bd9Sstevel@tonic-gate #define	node_from_id(id) (user_fd_mgt_array[(int)id].data.fd_node)
1997c478bd9Sstevel@tonic-gate #define	is_free_id(id) (user_fd_mgt_array[(int)id].free)
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate #ifndef POLLSTANDARD
2027c478bd9Sstevel@tonic-gate #define	POLLSTANDARD \
2037c478bd9Sstevel@tonic-gate 	(POLLIN|POLLPRI|POLLOUT|POLLRDNORM|POLLRDBAND| \
2047c478bd9Sstevel@tonic-gate 	POLLWRBAND|POLLERR|POLLHUP|POLLNVAL)
2057c478bd9Sstevel@tonic-gate #endif
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * To free an Id, we set the cell as free and insert its address in the list
2097c478bd9Sstevel@tonic-gate  * of free cell.
2107c478bd9Sstevel@tonic-gate  */
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate static void
_svc_free_id(const svc_input_id_t id)2137c478bd9Sstevel@tonic-gate _svc_free_id(const svc_input_id_t id)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	assert(((int)id >= 0) && ((int)id < svc_nmgtuserfds));
2167c478bd9Sstevel@tonic-gate 	user_fd_mgt_array[(int)id].free = TRUE;
2177c478bd9Sstevel@tonic-gate 	user_fd_mgt_array[(int)id].data.next_free = first_free;
2187c478bd9Sstevel@tonic-gate 	first_free = id;
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate /*
2227c478bd9Sstevel@tonic-gate  * To get a free cell, we just have to take it from the free linked list and
2237c478bd9Sstevel@tonic-gate  * set the flag to "not free". This function also allocates new memory if
2247c478bd9Sstevel@tonic-gate  * necessary
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate static svc_input_id_t
_svc_attribute_new_id(_svc_user_fd_node * node)2277c478bd9Sstevel@tonic-gate _svc_attribute_new_id(_svc_user_fd_node *node)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	int selected_index = (int)first_free;
2307c478bd9Sstevel@tonic-gate 	assert(node != NULL);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	if (selected_index == -1) {
2337c478bd9Sstevel@tonic-gate 		/* Allocate new entries */
2347c478bd9Sstevel@tonic-gate 		int L_inOldSize = svc_nmgtuserfds;
2357c478bd9Sstevel@tonic-gate 		int i;
23629d91154SMarcel Telka 		_svc_management_user_fd *tmp;
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		svc_nmgtuserfds += USER_FD_INCREMENT;
2397c478bd9Sstevel@tonic-gate 
24029d91154SMarcel Telka 		tmp = realloc(user_fd_mgt_array,
24129d91154SMarcel Telka 		    svc_nmgtuserfds * sizeof (_svc_management_user_fd));
2427c478bd9Sstevel@tonic-gate 
24329d91154SMarcel Telka 		if (tmp == NULL) {
2447c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "_svc_attribute_new_id: out of memory");
24529d91154SMarcel Telka 			svc_nmgtuserfds = L_inOldSize;
2467c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
2477c478bd9Sstevel@tonic-gate 			return ((svc_input_id_t)-1);
2487c478bd9Sstevel@tonic-gate 		}
2497c478bd9Sstevel@tonic-gate 
25029d91154SMarcel Telka 		user_fd_mgt_array = tmp;
25129d91154SMarcel Telka 
2527c478bd9Sstevel@tonic-gate 		for (i = svc_nmgtuserfds - 1; i >= L_inOldSize; i--)
2537c478bd9Sstevel@tonic-gate 			_svc_free_id((svc_input_id_t)i);
2547c478bd9Sstevel@tonic-gate 		selected_index = (int)first_free;
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	node->id = (svc_input_id_t)selected_index;
2587c478bd9Sstevel@tonic-gate 	first_free = user_fd_mgt_array[selected_index].data.next_free;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	user_fd_mgt_array[selected_index].data.fd_node = node;
2617c478bd9Sstevel@tonic-gate 	user_fd_mgt_array[selected_index].free = FALSE;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	return ((svc_input_id_t)selected_index);
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate /*
2677c478bd9Sstevel@tonic-gate  * Access to a pollfd treatment. Scan all the associated callbacks that have
2687c478bd9Sstevel@tonic-gate  * at least one bit in their mask that masks a received event.
2697c478bd9Sstevel@tonic-gate  *
2707c478bd9Sstevel@tonic-gate  * If event POLLNVAL is received, we check that one callback processes it, if
2717c478bd9Sstevel@tonic-gate  * not, then remove the file descriptor from the poll. If there is one, let
2727c478bd9Sstevel@tonic-gate  * the user do the work.
2737c478bd9Sstevel@tonic-gate  */
2747c478bd9Sstevel@tonic-gate void
__svc_getreq_user(struct pollfd * pfd)2757c478bd9Sstevel@tonic-gate __svc_getreq_user(struct pollfd *pfd)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate 	int fd = pfd->fd;
2787c478bd9Sstevel@tonic-gate 	short revents = pfd->revents;
2797c478bd9Sstevel@tonic-gate 	bool_t invalHandled = FALSE;
2807c478bd9Sstevel@tonic-gate 	_svc_user_fd_node *node;
2817c478bd9Sstevel@tonic-gate 
28261961e0fSrobinson 	(void) mutex_lock(&svc_userfds_lock);
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	if ((fd < 0) || (fd >= svc_nuserfds)) {
28561961e0fSrobinson 		(void) mutex_unlock(&svc_userfds_lock);
2867c478bd9Sstevel@tonic-gate 		return;
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 
28929d91154SMarcel Telka 	node = svc_userfds[fd].list;
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	/* check if at least one mask fits */
2927c478bd9Sstevel@tonic-gate 	if (0 == (revents & svc_userfds[fd].mask)) {
29361961e0fSrobinson 		(void) mutex_unlock(&svc_userfds_lock);
2947c478bd9Sstevel@tonic-gate 		return;
2957c478bd9Sstevel@tonic-gate 	}
2967c478bd9Sstevel@tonic-gate 
29729d91154SMarcel Telka 	while ((svc_userfds[fd].mask != 0) && (node != NULL)) {
2987c478bd9Sstevel@tonic-gate 		/*
2997c478bd9Sstevel@tonic-gate 		 * If one of the received events maps the ones the node listens
3007c478bd9Sstevel@tonic-gate 		 * to
3017c478bd9Sstevel@tonic-gate 		 */
3027c478bd9Sstevel@tonic-gate 		_svc_user_fd_node *next = node->lnk.next;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 		if (node->callback != NULL) {
3057c478bd9Sstevel@tonic-gate 			if (node->events & revents) {
3067c478bd9Sstevel@tonic-gate 				if (revents & POLLNVAL) {
3077c478bd9Sstevel@tonic-gate 					invalHandled = TRUE;
3087c478bd9Sstevel@tonic-gate 				}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 				/*
3117c478bd9Sstevel@tonic-gate 				 * The lock must be released before calling the
3127c478bd9Sstevel@tonic-gate 				 * user function, as this function can call
3137c478bd9Sstevel@tonic-gate 				 * svc_remove_input() for example.
3147c478bd9Sstevel@tonic-gate 				 */
31561961e0fSrobinson 				(void) mutex_unlock(&svc_userfds_lock);
3167c478bd9Sstevel@tonic-gate 				node->callback(node->id, node->fd,
3177c478bd9Sstevel@tonic-gate 				    node->events & revents, node->cookie);
3187c478bd9Sstevel@tonic-gate 				/*
3197c478bd9Sstevel@tonic-gate 				 * Do not use the node structure anymore, as it
3207c478bd9Sstevel@tonic-gate 				 * could have been deallocated by the previous
3217c478bd9Sstevel@tonic-gate 				 * callback.
3227c478bd9Sstevel@tonic-gate 				 */
32361961e0fSrobinson 				(void) mutex_lock(&svc_userfds_lock);
3247c478bd9Sstevel@tonic-gate 			}
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 		node = next;
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	if ((revents & POLLNVAL) && !invalHandled)
3307c478bd9Sstevel@tonic-gate 		__svc_remove_input_of_fd(fd);
33161961e0fSrobinson 	(void) mutex_unlock(&svc_userfds_lock);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate /*
3367c478bd9Sstevel@tonic-gate  * Check if a file descriptor is associated with a user reactor.
3377c478bd9Sstevel@tonic-gate  * To do this, just check that the array indexed on fd has a non-void linked
3387c478bd9Sstevel@tonic-gate  * list (ie. first element is not NULL)
3397c478bd9Sstevel@tonic-gate  */
3407c478bd9Sstevel@tonic-gate bool_t
__is_a_userfd(int fd)3417c478bd9Sstevel@tonic-gate __is_a_userfd(int fd)
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate 	/* Checks argument */
3447c478bd9Sstevel@tonic-gate 	if ((fd < 0) || (fd >= svc_nuserfds))
3457c478bd9Sstevel@tonic-gate 		return (FALSE);
3467c478bd9Sstevel@tonic-gate 	return ((svc_userfds[fd].mask == 0x0000)? FALSE:TRUE);
3477c478bd9Sstevel@tonic-gate }
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate /* free everything concerning user fd */
3507c478bd9Sstevel@tonic-gate /* used in svc_run.c => no static */
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate void
__destroy_userfd(void)35361961e0fSrobinson __destroy_userfd(void)
3547c478bd9Sstevel@tonic-gate {
3557c478bd9Sstevel@tonic-gate 	int one_fd;
3567c478bd9Sstevel@tonic-gate 	/* Clean user fd */
3577c478bd9Sstevel@tonic-gate 	if (svc_userfds != NULL) {
3587c478bd9Sstevel@tonic-gate 		for (one_fd = 0; one_fd < svc_nuserfds; one_fd++) {
3597c478bd9Sstevel@tonic-gate 			_svc_user_fd_node *node;
3607c478bd9Sstevel@tonic-gate 
36129d91154SMarcel Telka 			node = svc_userfds[one_fd].list;
36229d91154SMarcel Telka 			while (node != NULL) {
36329d91154SMarcel Telka 				_svc_user_fd_node *tmp = node;
3647c478bd9Sstevel@tonic-gate 				_svc_free_id(node->id);
3657c478bd9Sstevel@tonic-gate 				node = node->lnk.next;
36629d91154SMarcel Telka 				free(tmp);
3677c478bd9Sstevel@tonic-gate 			}
3687c478bd9Sstevel@tonic-gate 		}
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 		free(user_fd_mgt_array);
3717c478bd9Sstevel@tonic-gate 		user_fd_mgt_array = NULL;
3727c478bd9Sstevel@tonic-gate 		first_free = (svc_input_id_t)-1;
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 		free(svc_userfds);
3757c478bd9Sstevel@tonic-gate 		svc_userfds = NULL;
3767c478bd9Sstevel@tonic-gate 		svc_nuserfds = 0;
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate /*
3817c478bd9Sstevel@tonic-gate  * Remove all the callback associated with a fd => useful when the fd is
3827c478bd9Sstevel@tonic-gate  * closed for instance
3837c478bd9Sstevel@tonic-gate  */
3847c478bd9Sstevel@tonic-gate static void
__svc_remove_input_of_fd(int fd)3857c478bd9Sstevel@tonic-gate __svc_remove_input_of_fd(int fd)
3867c478bd9Sstevel@tonic-gate {
38729d91154SMarcel Telka 	_svc_user_fd_node **pnode;
38829d91154SMarcel Telka 	_svc_user_fd_node *tmp;
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	if ((fd < 0) || (fd >= svc_nuserfds))
3917c478bd9Sstevel@tonic-gate 		return;
3927c478bd9Sstevel@tonic-gate 
39329d91154SMarcel Telka 	pnode = &svc_userfds[fd].list;
39429d91154SMarcel Telka 	while ((tmp = *pnode) != NULL) {
39529d91154SMarcel Telka 		*pnode = tmp->lnk.next;
39629d91154SMarcel Telka 
39729d91154SMarcel Telka 		_svc_free_id(tmp->id);
39829d91154SMarcel Telka 		free(tmp);
3997c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	svc_userfds[fd].mask = 0;
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate /*
4057c478bd9Sstevel@tonic-gate  * Allow user to add an fd in the poll list. If it does not succeed, return
4067c478bd9Sstevel@tonic-gate  * -1. Otherwise, return a svc_id
4077c478bd9Sstevel@tonic-gate  */
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate svc_input_id_t
svc_add_input(int user_fd,unsigned int events,svc_callback_t user_callback,void * cookie)4107c478bd9Sstevel@tonic-gate svc_add_input(int user_fd, unsigned int events,
4117c478bd9Sstevel@tonic-gate     svc_callback_t user_callback, void *cookie)
4127c478bd9Sstevel@tonic-gate {
4137c478bd9Sstevel@tonic-gate 	_svc_user_fd_node *new_node;
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	if (user_fd < 0) {
4167c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4177c478bd9Sstevel@tonic-gate 		return ((svc_input_id_t)-1);
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	if ((events == 0x0000) ||
4217c478bd9Sstevel@tonic-gate 	    (events & ~(POLLIN|POLLPRI|POLLOUT|POLLRDNORM|POLLRDBAND|\
4227c478bd9Sstevel@tonic-gate 	    POLLWRBAND|POLLERR|POLLHUP|POLLNVAL))) {
4237c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4247c478bd9Sstevel@tonic-gate 		return ((svc_input_id_t)-1);
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 
42761961e0fSrobinson 	(void) mutex_lock(&svc_userfds_lock);
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	if ((user_fd < svc_nuserfds) &&
4307c478bd9Sstevel@tonic-gate 	    (svc_userfds[user_fd].mask & events) != 0) {
4317c478bd9Sstevel@tonic-gate 		/* Already registrated call-back */
4327c478bd9Sstevel@tonic-gate 		errno = EEXIST;
43361961e0fSrobinson 		(void) mutex_unlock(&svc_userfds_lock);
4347c478bd9Sstevel@tonic-gate 		return ((svc_input_id_t)-1);
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	/* Handle memory allocation. */
4387c478bd9Sstevel@tonic-gate 	if (user_fd >= svc_nuserfds) {
4397c478bd9Sstevel@tonic-gate 		int oldSize = svc_nuserfds;
4407c478bd9Sstevel@tonic-gate 		int i;
44129d91154SMarcel Telka 		_svc_user_fd_head *tmp;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 		svc_nuserfds = (user_fd + 1) + USER_FD_INCREMENT;
4447c478bd9Sstevel@tonic-gate 
44529d91154SMarcel Telka 		tmp = realloc(svc_userfds,
44629d91154SMarcel Telka 		    svc_nuserfds * sizeof (_svc_user_fd_head));
4477c478bd9Sstevel@tonic-gate 
44829d91154SMarcel Telka 		if (tmp == NULL) {
4497c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "svc_add_input: out of memory");
45029d91154SMarcel Telka 			svc_nuserfds = oldSize;
4517c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
45261961e0fSrobinson 			(void) mutex_unlock(&svc_userfds_lock);
4537c478bd9Sstevel@tonic-gate 			return ((svc_input_id_t)-1);
4547c478bd9Sstevel@tonic-gate 		}
4557c478bd9Sstevel@tonic-gate 
45629d91154SMarcel Telka 		svc_userfds = tmp;
45729d91154SMarcel Telka 
4587c478bd9Sstevel@tonic-gate 		for (i = oldSize; i < svc_nuserfds; i++) {
45929d91154SMarcel Telka 			svc_userfds[i].list = NULL;
4607c478bd9Sstevel@tonic-gate 			svc_userfds[i].mask = 0;
4617c478bd9Sstevel@tonic-gate 		}
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate 
46461961e0fSrobinson 	new_node = malloc(sizeof (_svc_user_fd_node));
4657c478bd9Sstevel@tonic-gate 	if (new_node == NULL) {
4667c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "svc_add_input: out of memory");
4677c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
46861961e0fSrobinson 		(void) mutex_unlock(&svc_userfds_lock);
4697c478bd9Sstevel@tonic-gate 		return ((svc_input_id_t)-1);
4707c478bd9Sstevel@tonic-gate 	}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	/* create a new node */
4737c478bd9Sstevel@tonic-gate 	new_node->fd		= user_fd;
4747c478bd9Sstevel@tonic-gate 	new_node->events	= events;
4757c478bd9Sstevel@tonic-gate 	new_node->callback	= user_callback;
4767c478bd9Sstevel@tonic-gate 	new_node->cookie	= cookie;
4777c478bd9Sstevel@tonic-gate 
47829d91154SMarcel Telka 	if (_svc_attribute_new_id(new_node) == -1) {
47929d91154SMarcel Telka 		(void) mutex_unlock(&svc_userfds_lock);
48029d91154SMarcel Telka 		free(new_node);
48129d91154SMarcel Telka 		return ((svc_input_id_t)-1);
48229d91154SMarcel Telka 	}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/* Add the new element at the beginning of the list. */
48529d91154SMarcel Telka 	if (svc_userfds[user_fd].list != NULL)
48629d91154SMarcel Telka 		svc_userfds[user_fd].list->lnk.previous = new_node;
48729d91154SMarcel Telka 	new_node->lnk.next = svc_userfds[user_fd].list;
48829d91154SMarcel Telka 	new_node->lnk.previous = NULL;
4897c478bd9Sstevel@tonic-gate 
49029d91154SMarcel Telka 	svc_userfds[user_fd].list = new_node;
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	/* refresh global mask for this file desciptor */
4937c478bd9Sstevel@tonic-gate 	svc_userfds[user_fd].mask |= events;
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	/* refresh mask for the poll */
4967c478bd9Sstevel@tonic-gate 	add_pollfd(user_fd, (svc_userfds[user_fd].mask));
4977c478bd9Sstevel@tonic-gate 
49861961e0fSrobinson 	(void) mutex_unlock(&svc_userfds_lock);
4997c478bd9Sstevel@tonic-gate 	return (new_node->id);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate int
svc_remove_input(svc_input_id_t id)5037c478bd9Sstevel@tonic-gate svc_remove_input(svc_input_id_t id)
5047c478bd9Sstevel@tonic-gate {
5057c478bd9Sstevel@tonic-gate 	_svc_user_fd_node* node;
5067c478bd9Sstevel@tonic-gate 	_svc_user_fd_node* next;
5077c478bd9Sstevel@tonic-gate 	_svc_user_fd_node* previous;
5087c478bd9Sstevel@tonic-gate 	int fd;		/* caching optim */
5097c478bd9Sstevel@tonic-gate 
51061961e0fSrobinson 	(void) mutex_lock(&svc_userfds_lock);
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	/* Immediately update data for id management */
5137c478bd9Sstevel@tonic-gate 	if (user_fd_mgt_array == NULL || id >= svc_nmgtuserfds ||
5147c478bd9Sstevel@tonic-gate 	    is_free_id(id)) {
5157c478bd9Sstevel@tonic-gate 		errno = EINVAL;
51661961e0fSrobinson 		(void) mutex_unlock(&svc_userfds_lock);
5177c478bd9Sstevel@tonic-gate 		return (-1);
5187c478bd9Sstevel@tonic-gate 	}
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	node = node_from_id(id);
5217c478bd9Sstevel@tonic-gate 	assert(node != NULL);
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	_svc_free_id(id);
5247c478bd9Sstevel@tonic-gate 	next		= node->lnk.next;
5257c478bd9Sstevel@tonic-gate 	previous	= node->lnk.previous;
5267c478bd9Sstevel@tonic-gate 	fd		= node->fd; /* caching optim */
5277c478bd9Sstevel@tonic-gate 
52829d91154SMarcel Telka 	/* Remove this node from the list. */
52929d91154SMarcel Telka 	if (previous != NULL) {
53029d91154SMarcel Telka 		previous->lnk.next = next;
53129d91154SMarcel Telka 	} else {
53229d91154SMarcel Telka 		assert(svc_userfds[fd].list == node);
53329d91154SMarcel Telka 		svc_userfds[fd].list = next;
53429d91154SMarcel Telka 	}
53529d91154SMarcel Telka 	if (next != NULL)
53629d91154SMarcel Telka 		next->lnk.previous = previous;
5377c478bd9Sstevel@tonic-gate 
53829d91154SMarcel Telka 	/* Remove the node flags from the global mask */
5397c478bd9Sstevel@tonic-gate 	svc_userfds[fd].mask ^= node->events;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	free(node);
5427c478bd9Sstevel@tonic-gate 	if (svc_userfds[fd].mask == 0) {
54329d91154SMarcel Telka 		assert(svc_userfds[fd].list == NULL);
5447c478bd9Sstevel@tonic-gate 		remove_pollfd(fd);
54529d91154SMarcel Telka 	} else {
54629d91154SMarcel Telka 		assert(svc_userfds[fd].list != NULL);
5477c478bd9Sstevel@tonic-gate 	}
5487c478bd9Sstevel@tonic-gate 	/* <=> CLEAN NEEDED TO SHRINK MEMORY USAGE */
5497c478bd9Sstevel@tonic-gate 
55061961e0fSrobinson 	(void) mutex_unlock(&svc_userfds_lock);
5517c478bd9Sstevel@tonic-gate 	return (0);
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate /*
5557c478bd9Sstevel@tonic-gate  * Provides default service-side functions for authentication flavors
5567c478bd9Sstevel@tonic-gate  * that do not use all the fields in struct svc_auth_ops.
5577c478bd9Sstevel@tonic-gate  */
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5607c478bd9Sstevel@tonic-gate static int
authany_wrap(AUTH * auth,XDR * xdrs,xdrproc_t xfunc,caddr_t xwhere)56161961e0fSrobinson authany_wrap(AUTH *auth, XDR *xdrs, xdrproc_t xfunc, caddr_t xwhere)
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate 	return (*xfunc)(xdrs, xwhere);
5647c478bd9Sstevel@tonic-gate }
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate struct svc_auth_ops svc_auth_any_ops = {
5677c478bd9Sstevel@tonic-gate 	authany_wrap,
5687c478bd9Sstevel@tonic-gate 	authany_wrap,
5697c478bd9Sstevel@tonic-gate };
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate /*
5727c478bd9Sstevel@tonic-gate  * Return pointer to server authentication structure.
5737c478bd9Sstevel@tonic-gate  */
5747c478bd9Sstevel@tonic-gate SVCAUTH *
__svc_get_svcauth(SVCXPRT * xprt)57561961e0fSrobinson __svc_get_svcauth(SVCXPRT *xprt)
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate /* LINTED pointer alignment */
5787c478bd9Sstevel@tonic-gate 	return (&SVC_XP_AUTH(xprt));
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate /*
5827c478bd9Sstevel@tonic-gate  * A callback routine to cleanup after a procedure is executed.
5837c478bd9Sstevel@tonic-gate  */
5847c478bd9Sstevel@tonic-gate void (*__proc_cleanup_cb)() = NULL;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate void *
__svc_set_proc_cleanup_cb(void * cb)58761961e0fSrobinson __svc_set_proc_cleanup_cb(void *cb)
5887c478bd9Sstevel@tonic-gate {
5897c478bd9Sstevel@tonic-gate 	void	*tmp = (void *)__proc_cleanup_cb;
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	__proc_cleanup_cb = (void (*)())cb;
5927c478bd9Sstevel@tonic-gate 	return (tmp);
5937c478bd9Sstevel@tonic-gate }
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate /* ***************  SVCXPRT related stuff **************** */
5967c478bd9Sstevel@tonic-gate 
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate static int pollfd_shrinking = 1;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate /*
6027c478bd9Sstevel@tonic-gate  * Add fd to svc_pollfd
6037c478bd9Sstevel@tonic-gate  */
6047c478bd9Sstevel@tonic-gate static void
add_pollfd(int fd,short events)6057c478bd9Sstevel@tonic-gate add_pollfd(int fd, short events)
6067c478bd9Sstevel@tonic-gate {
6077c478bd9Sstevel@tonic-gate 	if (fd < FD_SETSIZE) {
6087c478bd9Sstevel@tonic-gate 		FD_SET(fd, &svc_fdset);
6097c478bd9Sstevel@tonic-gate #if !defined(_LP64)
6107c478bd9Sstevel@tonic-gate 		FD_SET(fd, &_new_svc_fdset);
6117c478bd9Sstevel@tonic-gate #endif
6127c478bd9Sstevel@tonic-gate 		svc_nfds++;
6137c478bd9Sstevel@tonic-gate 		svc_nfds_set++;
6147c478bd9Sstevel@tonic-gate 		if (fd >= svc_max_fd)
6157c478bd9Sstevel@tonic-gate 			svc_max_fd = fd + 1;
6167c478bd9Sstevel@tonic-gate 	}
6177c478bd9Sstevel@tonic-gate 	if (fd >= svc_max_pollfd)
6187c478bd9Sstevel@tonic-gate 		svc_max_pollfd = fd + 1;
6197c478bd9Sstevel@tonic-gate 	if (svc_max_pollfd > svc_pollfd_allocd) {
6207c478bd9Sstevel@tonic-gate 		int i = svc_pollfd_allocd;
6217c478bd9Sstevel@tonic-gate 		pollfd_t *tmp;
6227c478bd9Sstevel@tonic-gate 		do {
6237c478bd9Sstevel@tonic-gate 			svc_pollfd_allocd += POLLFD_EXTEND;
6247c478bd9Sstevel@tonic-gate 		} while (svc_max_pollfd > svc_pollfd_allocd);
6257c478bd9Sstevel@tonic-gate 		tmp = realloc(svc_pollfd,
62629d91154SMarcel Telka 		    sizeof (pollfd_t) * svc_pollfd_allocd);
6277c478bd9Sstevel@tonic-gate 		if (tmp != NULL) {
6287c478bd9Sstevel@tonic-gate 			svc_pollfd = tmp;
6297c478bd9Sstevel@tonic-gate 			for (; i < svc_pollfd_allocd; i++)
6307c478bd9Sstevel@tonic-gate 				POLLFD_CLR(i, tmp);
6317c478bd9Sstevel@tonic-gate 		} else {
6327c478bd9Sstevel@tonic-gate 			/*
6337c478bd9Sstevel@tonic-gate 			 * give an error message; undo fdset setting
6347c478bd9Sstevel@tonic-gate 			 * above;  reset the pollfd_shrinking flag.
6357c478bd9Sstevel@tonic-gate 			 * because of this poll will not be done
6367c478bd9Sstevel@tonic-gate 			 * on these fds.
6377c478bd9Sstevel@tonic-gate 			 */
6387c478bd9Sstevel@tonic-gate 			if (fd < FD_SETSIZE) {
6397c478bd9Sstevel@tonic-gate 				FD_CLR(fd, &svc_fdset);
6407c478bd9Sstevel@tonic-gate #if !defined(_LP64)
6417c478bd9Sstevel@tonic-gate 				FD_CLR(fd, &_new_svc_fdset);
6427c478bd9Sstevel@tonic-gate #endif
6437c478bd9Sstevel@tonic-gate 				svc_nfds--;
6447c478bd9Sstevel@tonic-gate 				svc_nfds_set--;
6457c478bd9Sstevel@tonic-gate 				if (fd == (svc_max_fd - 1))
6467c478bd9Sstevel@tonic-gate 					svc_max_fd--;
6477c478bd9Sstevel@tonic-gate 			}
6487c478bd9Sstevel@tonic-gate 			if (fd == (svc_max_pollfd - 1))
6497c478bd9Sstevel@tonic-gate 				svc_max_pollfd--;
6507c478bd9Sstevel@tonic-gate 			pollfd_shrinking = 0;
6517c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "add_pollfd: out of memory");
6527c478bd9Sstevel@tonic-gate 			_exit(1);
6537c478bd9Sstevel@tonic-gate 		}
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 	svc_pollfd[fd].fd	= fd;
6567c478bd9Sstevel@tonic-gate 	svc_pollfd[fd].events	= events;
6577c478bd9Sstevel@tonic-gate 	svc_npollfds++;
6587c478bd9Sstevel@tonic-gate 	svc_npollfds_set++;
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate  * the fd is still active but only the bit in fdset is cleared.
6637c478bd9Sstevel@tonic-gate  * do not subtract svc_nfds or svc_npollfds
6647c478bd9Sstevel@tonic-gate  */
6657c478bd9Sstevel@tonic-gate void
clear_pollfd(int fd)6667c478bd9Sstevel@tonic-gate clear_pollfd(int fd)
6677c478bd9Sstevel@tonic-gate {
6687c478bd9Sstevel@tonic-gate 	if (fd < FD_SETSIZE && FD_ISSET(fd, &svc_fdset)) {
6697c478bd9Sstevel@tonic-gate 		FD_CLR(fd, &svc_fdset);
6707c478bd9Sstevel@tonic-gate #if !defined(_LP64)
6717c478bd9Sstevel@tonic-gate 		FD_CLR(fd, &_new_svc_fdset);
6727c478bd9Sstevel@tonic-gate #endif
6737c478bd9Sstevel@tonic-gate 		svc_nfds_set--;
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate 	if (fd < svc_pollfd_allocd && POLLFD_ISSET(fd, svc_pollfd)) {
6767c478bd9Sstevel@tonic-gate 		POLLFD_CLR(fd, svc_pollfd);
6777c478bd9Sstevel@tonic-gate 		svc_npollfds_set--;
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate /*
6827c478bd9Sstevel@tonic-gate  * sets the bit in fdset for an active fd so that poll() is done for that
6837c478bd9Sstevel@tonic-gate  */
6847c478bd9Sstevel@tonic-gate void
set_pollfd(int fd,short events)6857c478bd9Sstevel@tonic-gate set_pollfd(int fd, short events)
6867c478bd9Sstevel@tonic-gate {
6877c478bd9Sstevel@tonic-gate 	if (fd < FD_SETSIZE) {
6887c478bd9Sstevel@tonic-gate 		FD_SET(fd, &svc_fdset);
6897c478bd9Sstevel@tonic-gate #if !defined(_LP64)
6907c478bd9Sstevel@tonic-gate 		FD_SET(fd, &_new_svc_fdset);
6917c478bd9Sstevel@tonic-gate #endif
6927c478bd9Sstevel@tonic-gate 		svc_nfds_set++;
6937c478bd9Sstevel@tonic-gate 	}
6947c478bd9Sstevel@tonic-gate 	if (fd < svc_pollfd_allocd) {
6957c478bd9Sstevel@tonic-gate 		svc_pollfd[fd].fd	= fd;
6967c478bd9Sstevel@tonic-gate 		svc_pollfd[fd].events	= events;
6977c478bd9Sstevel@tonic-gate 		svc_npollfds_set++;
6987c478bd9Sstevel@tonic-gate 	}
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate /*
7027c478bd9Sstevel@tonic-gate  * remove a svc_pollfd entry; it does not shrink the memory
7037c478bd9Sstevel@tonic-gate  */
7047c478bd9Sstevel@tonic-gate static void
remove_pollfd(int fd)70561961e0fSrobinson remove_pollfd(int fd)
7067c478bd9Sstevel@tonic-gate {
7077c478bd9Sstevel@tonic-gate 	clear_pollfd(fd);
7087c478bd9Sstevel@tonic-gate 	if (fd == (svc_max_fd - 1))
7097c478bd9Sstevel@tonic-gate 		svc_max_fd--;
7107c478bd9Sstevel@tonic-gate 	svc_nfds--;
7117c478bd9Sstevel@tonic-gate 	if (fd == (svc_max_pollfd - 1))
7127c478bd9Sstevel@tonic-gate 		svc_max_pollfd--;
7137c478bd9Sstevel@tonic-gate 	svc_npollfds--;
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate /*
7177c478bd9Sstevel@tonic-gate  * delete a svc_pollfd entry; it shrinks the memory
7187c478bd9Sstevel@tonic-gate  * use remove_pollfd if you do not want to shrink
7197c478bd9Sstevel@tonic-gate  */
7207c478bd9Sstevel@tonic-gate static void
delete_pollfd(int fd)7217c478bd9Sstevel@tonic-gate delete_pollfd(int fd)
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate 	remove_pollfd(fd);
7247c478bd9Sstevel@tonic-gate 	if (pollfd_shrinking && svc_max_pollfd <
72529d91154SMarcel Telka 	    (svc_pollfd_allocd - POLLFD_SHRINK)) {
7267c478bd9Sstevel@tonic-gate 		do {
7277c478bd9Sstevel@tonic-gate 			svc_pollfd_allocd -= POLLFD_SHRINK;
7287c478bd9Sstevel@tonic-gate 		} while (svc_max_pollfd < (svc_pollfd_allocd - POLLFD_SHRINK));
7297c478bd9Sstevel@tonic-gate 		svc_pollfd = realloc(svc_pollfd,
73029d91154SMarcel Telka 		    sizeof (pollfd_t) * svc_pollfd_allocd);
7317c478bd9Sstevel@tonic-gate 		if (svc_pollfd == NULL) {
7327c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "delete_pollfd: out of memory");
7337c478bd9Sstevel@tonic-gate 			_exit(1);
7347c478bd9Sstevel@tonic-gate 		}
7357c478bd9Sstevel@tonic-gate 	}
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate /*
7407c478bd9Sstevel@tonic-gate  * Activate a transport handle.
7417c478bd9Sstevel@tonic-gate  */
7427c478bd9Sstevel@tonic-gate void
xprt_register(const SVCXPRT * xprt)74361961e0fSrobinson xprt_register(const SVCXPRT *xprt)
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate 	int fd = xprt->xp_fd;
7467c478bd9Sstevel@tonic-gate #ifdef CALLBACK
7477c478bd9Sstevel@tonic-gate 	extern void (*_svc_getreqset_proc)();
7487c478bd9Sstevel@tonic-gate #endif
7497c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY svc_fd_lock: svc_xports, svc_fdset */
7507c478bd9Sstevel@tonic-gate 
75161961e0fSrobinson 	(void) rw_wrlock(&svc_fd_lock);
7527c478bd9Sstevel@tonic-gate 	if (svc_xports == NULL) {
7537c478bd9Sstevel@tonic-gate 		/* allocate some small amount first */
7547c478bd9Sstevel@tonic-gate 		svc_xports = calloc(FD_INCREMENT,  sizeof (SVCXPRT *));
7557c478bd9Sstevel@tonic-gate 		if (svc_xports == NULL) {
7567c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "xprt_register: out of memory");
7577c478bd9Sstevel@tonic-gate 			_exit(1);
7587c478bd9Sstevel@tonic-gate 		}
7597c478bd9Sstevel@tonic-gate 		nsvc_xports = FD_INCREMENT;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate #ifdef CALLBACK
7627c478bd9Sstevel@tonic-gate 		/*
7637c478bd9Sstevel@tonic-gate 		 * XXX: This code does not keep track of the server state.
7647c478bd9Sstevel@tonic-gate 		 *
7657c478bd9Sstevel@tonic-gate 		 * This provides for callback support.	When a client
7667c478bd9Sstevel@tonic-gate 		 * recv's a call from another client on the server fd's,
7677c478bd9Sstevel@tonic-gate 		 * it calls _svc_getreqset_proc() which would return
7687c478bd9Sstevel@tonic-gate 		 * after serving all the server requests.  Also look under
7697c478bd9Sstevel@tonic-gate 		 * clnt_dg.c and clnt_vc.c  (clnt_call part of it)
7707c478bd9Sstevel@tonic-gate 		 */
7717c478bd9Sstevel@tonic-gate 		_svc_getreqset_proc = svc_getreq_poll;
7727c478bd9Sstevel@tonic-gate #endif
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	while (fd >= nsvc_xports) {
7767c478bd9Sstevel@tonic-gate 		SVCXPRT **tmp_xprts = svc_xports;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 		/* time to expand svc_xprts */
7797c478bd9Sstevel@tonic-gate 		tmp_xprts = realloc(svc_xports,
78029d91154SMarcel Telka 		    sizeof (SVCXPRT *) * (nsvc_xports + FD_INCREMENT));
7817c478bd9Sstevel@tonic-gate 		if (tmp_xprts == NULL) {
7827c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "xprt_register : out of memory.");
7837c478bd9Sstevel@tonic-gate 			_exit(1);
7847c478bd9Sstevel@tonic-gate 		}
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 		svc_xports = tmp_xprts;
7877c478bd9Sstevel@tonic-gate 		(void) memset(&svc_xports[nsvc_xports], 0,
78829d91154SMarcel Telka 		    sizeof (SVCXPRT *) * FD_INCREMENT);
7897c478bd9Sstevel@tonic-gate 		nsvc_xports += FD_INCREMENT;
7907c478bd9Sstevel@tonic-gate 	}
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 	svc_xports[fd] = (SVCXPRT *)xprt;
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 	add_pollfd(fd, MASKVAL);
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	if (svc_polling) {
7977c478bd9Sstevel@tonic-gate 		char dummy;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 		/*
8007c478bd9Sstevel@tonic-gate 		 * This happens only in one of the MT modes.
8017c478bd9Sstevel@tonic-gate 		 * Wake up poller.
8027c478bd9Sstevel@tonic-gate 		 */
80361961e0fSrobinson 		(void) write(svc_pipe[1], &dummy, sizeof (dummy));
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 	/*
8067c478bd9Sstevel@tonic-gate 	 * If already dispatching door based services, start
8077c478bd9Sstevel@tonic-gate 	 * dispatching TLI based services now.
8087c478bd9Sstevel@tonic-gate 	 */
80961961e0fSrobinson 	(void) mutex_lock(&svc_door_mutex);
8107c478bd9Sstevel@tonic-gate 	if (svc_ndoorfds > 0)
81161961e0fSrobinson 		(void) cond_signal(&svc_door_waitcv);
81261961e0fSrobinson 	(void) mutex_unlock(&svc_door_mutex);
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	if (svc_xdrs == NULL) {
8157c478bd9Sstevel@tonic-gate 		/* allocate initial chunk */
8167c478bd9Sstevel@tonic-gate 		svc_xdrs = calloc(FD_INCREMENT, sizeof (XDR *));
8177c478bd9Sstevel@tonic-gate 		if (svc_xdrs != NULL)
8187c478bd9Sstevel@tonic-gate 			nsvc_xdrs = FD_INCREMENT;
8197c478bd9Sstevel@tonic-gate 		else {
8207c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "xprt_register : out of memory.");
8217c478bd9Sstevel@tonic-gate 			_exit(1);
8227c478bd9Sstevel@tonic-gate 		}
8237c478bd9Sstevel@tonic-gate 	}
82461961e0fSrobinson 	(void) rw_unlock(&svc_fd_lock);
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate /*
8287c478bd9Sstevel@tonic-gate  * De-activate a transport handle.
8297c478bd9Sstevel@tonic-gate  */
8307c478bd9Sstevel@tonic-gate void
__xprt_unregister_private(const SVCXPRT * xprt,bool_t lock_not_held)8317c478bd9Sstevel@tonic-gate __xprt_unregister_private(const SVCXPRT *xprt, bool_t lock_not_held)
8327c478bd9Sstevel@tonic-gate {
8337c478bd9Sstevel@tonic-gate 	int fd = xprt->xp_fd;
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 	if (lock_not_held)
83661961e0fSrobinson 		(void) rw_wrlock(&svc_fd_lock);
8377c478bd9Sstevel@tonic-gate 	if ((fd < nsvc_xports) && (svc_xports[fd] == xprt)) {
83861961e0fSrobinson 		svc_xports[fd] = NULL;
8397c478bd9Sstevel@tonic-gate 		delete_pollfd(fd);
8407c478bd9Sstevel@tonic-gate 	}
8417c478bd9Sstevel@tonic-gate 	if (lock_not_held)
84261961e0fSrobinson 		(void) rw_unlock(&svc_fd_lock);
8437c478bd9Sstevel@tonic-gate 	__svc_rm_from_xlist(&_svc_xprtlist, xprt, &xprtlist_lock);
8447c478bd9Sstevel@tonic-gate }
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate void
xprt_unregister(const SVCXPRT * xprt)84761961e0fSrobinson xprt_unregister(const SVCXPRT *xprt)
8487c478bd9Sstevel@tonic-gate {
8497c478bd9Sstevel@tonic-gate 	__xprt_unregister_private(xprt, TRUE);
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate /* ********************** CALLOUT list related stuff ************* */
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate /*
8557c478bd9Sstevel@tonic-gate  * Add a service program to the callout list.
8567c478bd9Sstevel@tonic-gate  * The dispatch routine will be called when a rpc request for this
8577c478bd9Sstevel@tonic-gate  * program number comes in.
8587c478bd9Sstevel@tonic-gate  */
8597c478bd9Sstevel@tonic-gate bool_t
svc_reg(const SVCXPRT * xprt,const rpcprog_t prog,const rpcvers_t vers,void (* dispatch)(),const struct netconfig * nconf)86061961e0fSrobinson svc_reg(const SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
8612209d3c8SRichard Lowe     void (*dispatch)(), const struct netconfig *nconf)
8627c478bd9Sstevel@tonic-gate {
8637c478bd9Sstevel@tonic-gate 	struct svc_callout *prev;
8647c478bd9Sstevel@tonic-gate 	struct svc_callout *s, **s2;
8657c478bd9Sstevel@tonic-gate 	struct netconfig *tnconf;
8667c478bd9Sstevel@tonic-gate 	char *netid = NULL;
8677c478bd9Sstevel@tonic-gate 	int flag = 0;
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	if (xprt->xp_netid) {
8727c478bd9Sstevel@tonic-gate 		netid = strdup(xprt->xp_netid);
8737c478bd9Sstevel@tonic-gate 		flag = 1;
8747c478bd9Sstevel@tonic-gate 	} else if (nconf && nconf->nc_netid) {
8757c478bd9Sstevel@tonic-gate 		netid = strdup(nconf->nc_netid);
8767c478bd9Sstevel@tonic-gate 		flag = 1;
8777c478bd9Sstevel@tonic-gate 	} else if ((tnconf = __rpcfd_to_nconf(xprt->xp_fd, xprt->xp_type))
87829d91154SMarcel Telka 	    != NULL) {
8797c478bd9Sstevel@tonic-gate 		netid = strdup(tnconf->nc_netid);
8807c478bd9Sstevel@tonic-gate 		flag = 1;
8817c478bd9Sstevel@tonic-gate 		freenetconfigent(tnconf);
8827c478bd9Sstevel@tonic-gate 	} /* must have been created with svc_raw_create */
88361961e0fSrobinson 	if ((netid == NULL) && (flag == 1))
8847c478bd9Sstevel@tonic-gate 		return (FALSE);
8857c478bd9Sstevel@tonic-gate 
88661961e0fSrobinson 	(void) rw_wrlock(&svc_lock);
8877c478bd9Sstevel@tonic-gate 	if ((s = svc_find(prog, vers, &prev, netid)) != NULL_SVC) {
8887c478bd9Sstevel@tonic-gate 		if (netid)
8897c478bd9Sstevel@tonic-gate 			free(netid);
8907c478bd9Sstevel@tonic-gate 		if (s->sc_dispatch == dispatch)
89148bbca81SDaniel Hoffman 			goto rpcb_it; /* it is registering another xptr */
89261961e0fSrobinson 		(void) rw_unlock(&svc_lock);
8937c478bd9Sstevel@tonic-gate 		return (FALSE);
8947c478bd9Sstevel@tonic-gate 	}
89561961e0fSrobinson 	s = malloc(sizeof (struct svc_callout));
89661961e0fSrobinson 	if (s == NULL) {
8977c478bd9Sstevel@tonic-gate 		if (netid)
8987c478bd9Sstevel@tonic-gate 			free(netid);
89961961e0fSrobinson 		(void) rw_unlock(&svc_lock);
9007c478bd9Sstevel@tonic-gate 		return (FALSE);
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 	s->sc_prog = prog;
9047c478bd9Sstevel@tonic-gate 	s->sc_vers = vers;
9057c478bd9Sstevel@tonic-gate 	s->sc_dispatch = dispatch;
9067c478bd9Sstevel@tonic-gate 	s->sc_netid = netid;
9077c478bd9Sstevel@tonic-gate 	s->sc_next = NULL;
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	/*
9107c478bd9Sstevel@tonic-gate 	 * The ordering of transports is such that the most frequently used
9117c478bd9Sstevel@tonic-gate 	 * one appears first.  So add the new entry to the end of the list.
9127c478bd9Sstevel@tonic-gate 	 */
9137c478bd9Sstevel@tonic-gate 	for (s2 = &svc_head; *s2 != NULL; s2 = &(*s2)->sc_next)
9147c478bd9Sstevel@tonic-gate 		;
9157c478bd9Sstevel@tonic-gate 	*s2 = s;
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
9187c478bd9Sstevel@tonic-gate 		if ((((SVCXPRT *)xprt)->xp_netid = strdup(netid)) == NULL) {
9197c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "svc_reg : strdup failed.");
9207c478bd9Sstevel@tonic-gate 			free(netid);
92161961e0fSrobinson 			free(s);
9227c478bd9Sstevel@tonic-gate 			*s2 = NULL;
92361961e0fSrobinson 			(void) rw_unlock(&svc_lock);
9247c478bd9Sstevel@tonic-gate 			return (FALSE);
9257c478bd9Sstevel@tonic-gate 		}
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate rpcb_it:
92861961e0fSrobinson 	(void) rw_unlock(&svc_lock);
9299acbbeafSnn 
9307c478bd9Sstevel@tonic-gate 	/* now register the information with the local binder service */
9319ff75adeSSurya Prakki 	if (nconf)
93261961e0fSrobinson 		return (rpcb_set(prog, vers, nconf, &xprt->xp_ltaddr));
9339ff75adeSSurya Prakki 	return (TRUE);
9349acbbeafSnn 	/*NOTREACHED*/
9357c478bd9Sstevel@tonic-gate }
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate /*
9387c478bd9Sstevel@tonic-gate  * Remove a service program from the callout list.
9397c478bd9Sstevel@tonic-gate  */
9407c478bd9Sstevel@tonic-gate void
svc_unreg(const rpcprog_t prog,const rpcvers_t vers)94161961e0fSrobinson svc_unreg(const rpcprog_t prog, const rpcvers_t vers)
9427c478bd9Sstevel@tonic-gate {
9437c478bd9Sstevel@tonic-gate 	struct svc_callout *prev;
9447c478bd9Sstevel@tonic-gate 	struct svc_callout *s;
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate 	/* unregister the information anyway */
9479ff75adeSSurya Prakki 	(void) rpcb_unset(prog, vers, NULL);
9489ff75adeSSurya Prakki 
94961961e0fSrobinson 	(void) rw_wrlock(&svc_lock);
9507c478bd9Sstevel@tonic-gate 	while ((s = svc_find(prog, vers, &prev, NULL)) != NULL_SVC) {
9517c478bd9Sstevel@tonic-gate 		if (prev == NULL_SVC) {
9527c478bd9Sstevel@tonic-gate 			svc_head = s->sc_next;
9537c478bd9Sstevel@tonic-gate 		} else {
9547c478bd9Sstevel@tonic-gate 			prev->sc_next = s->sc_next;
9557c478bd9Sstevel@tonic-gate 		}
9567c478bd9Sstevel@tonic-gate 		s->sc_next = NULL_SVC;
9577c478bd9Sstevel@tonic-gate 		if (s->sc_netid)
95861961e0fSrobinson 			free(s->sc_netid);
95961961e0fSrobinson 		free(s);
9607c478bd9Sstevel@tonic-gate 	}
96161961e0fSrobinson 	(void) rw_unlock(&svc_lock);
9627c478bd9Sstevel@tonic-gate }
9637c478bd9Sstevel@tonic-gate 
9647c478bd9Sstevel@tonic-gate #ifdef PORTMAP
9657c478bd9Sstevel@tonic-gate /*
9667c478bd9Sstevel@tonic-gate  * Add a service program to the callout list.
9677c478bd9Sstevel@tonic-gate  * The dispatch routine will be called when a rpc request for this
9687c478bd9Sstevel@tonic-gate  * program number comes in.
9697c478bd9Sstevel@tonic-gate  * For version 2 portmappers.
9707c478bd9Sstevel@tonic-gate  */
9717c478bd9Sstevel@tonic-gate bool_t
svc_register(SVCXPRT * xprt,rpcprog_t prog,rpcvers_t vers,void (* dispatch)(),int protocol)97261961e0fSrobinson svc_register(SVCXPRT *xprt, rpcprog_t prog, rpcvers_t vers,
9732209d3c8SRichard Lowe     void (*dispatch)(), int protocol)
9747c478bd9Sstevel@tonic-gate {
9757c478bd9Sstevel@tonic-gate 	struct svc_callout *prev;
9767c478bd9Sstevel@tonic-gate 	struct svc_callout *s;
9777c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
9787c478bd9Sstevel@tonic-gate 	char *netid = NULL;
9797c478bd9Sstevel@tonic-gate 	int flag = 0;
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	if (xprt->xp_netid) {
9827c478bd9Sstevel@tonic-gate 		netid = strdup(xprt->xp_netid);
9837c478bd9Sstevel@tonic-gate 		flag = 1;
9847c478bd9Sstevel@tonic-gate 	} else if ((ioctl(xprt->xp_fd, I_FIND, "timod") > 0) && ((nconf =
98529d91154SMarcel Telka 	    __rpcfd_to_nconf(xprt->xp_fd, xprt->xp_type)) != NULL)) {
9867c478bd9Sstevel@tonic-gate 		/* fill in missing netid field in SVCXPRT */
9877c478bd9Sstevel@tonic-gate 		netid = strdup(nconf->nc_netid);
9887c478bd9Sstevel@tonic-gate 		flag = 1;
9897c478bd9Sstevel@tonic-gate 		freenetconfigent(nconf);
9907c478bd9Sstevel@tonic-gate 	} /* must be svc_raw_create */
9917c478bd9Sstevel@tonic-gate 
99261961e0fSrobinson 	if ((netid == NULL) && (flag == 1))
9937c478bd9Sstevel@tonic-gate 		return (FALSE);
9947c478bd9Sstevel@tonic-gate 
99561961e0fSrobinson 	(void) rw_wrlock(&svc_lock);
9967c478bd9Sstevel@tonic-gate 	if ((s = svc_find(prog, vers, &prev, netid)) != NULL_SVC) {
9977c478bd9Sstevel@tonic-gate 		if (netid)
9987c478bd9Sstevel@tonic-gate 			free(netid);
9997c478bd9Sstevel@tonic-gate 		if (s->sc_dispatch == dispatch)
100048bbca81SDaniel Hoffman 			goto pmap_it;  /* it is registering another xptr */
100161961e0fSrobinson 		(void) rw_unlock(&svc_lock);
10027c478bd9Sstevel@tonic-gate 		return (FALSE);
10037c478bd9Sstevel@tonic-gate 	}
100461961e0fSrobinson 	s = malloc(sizeof (struct svc_callout));
10057c478bd9Sstevel@tonic-gate 	if (s == (struct svc_callout *)0) {
10067c478bd9Sstevel@tonic-gate 		if (netid)
10077c478bd9Sstevel@tonic-gate 			free(netid);
100861961e0fSrobinson 		(void) rw_unlock(&svc_lock);
10097c478bd9Sstevel@tonic-gate 		return (FALSE);
10107c478bd9Sstevel@tonic-gate 	}
10117c478bd9Sstevel@tonic-gate 	s->sc_prog = prog;
10127c478bd9Sstevel@tonic-gate 	s->sc_vers = vers;
10137c478bd9Sstevel@tonic-gate 	s->sc_dispatch = dispatch;
10147c478bd9Sstevel@tonic-gate 	s->sc_netid = netid;
10157c478bd9Sstevel@tonic-gate 	s->sc_next = svc_head;
10167c478bd9Sstevel@tonic-gate 	svc_head = s;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
10197c478bd9Sstevel@tonic-gate 		if ((xprt->xp_netid = strdup(netid)) == NULL) {
10207c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "svc_register : strdup failed.");
10217c478bd9Sstevel@tonic-gate 			free(netid);
10227c478bd9Sstevel@tonic-gate 			svc_head = s->sc_next;
102361961e0fSrobinson 			free(s);
102461961e0fSrobinson 			(void) rw_unlock(&svc_lock);
10257c478bd9Sstevel@tonic-gate 			return (FALSE);
10267c478bd9Sstevel@tonic-gate 		}
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate pmap_it:
102961961e0fSrobinson 	(void) rw_unlock(&svc_lock);
10307c478bd9Sstevel@tonic-gate 	/* now register the information with the local binder service */
103161961e0fSrobinson 	if (protocol)
103261961e0fSrobinson 		return (pmap_set(prog, vers, protocol, xprt->xp_port));
10337c478bd9Sstevel@tonic-gate 	return (TRUE);
1034