10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
52712Snn35248 * Common Development and Distribution License (the "License").
62712Snn35248 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21132Srobinson
220Sstevel@tonic-gate /*
23*12648SSurya.Prakki@Sun.COM * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate * Copyright 1993 OpenVision Technologies, Inc., All Rights Reserved.
270Sstevel@tonic-gate */
280Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
290Sstevel@tonic-gate /* All Rights Reserved */
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * Portions of this source code were derived from Berkeley
320Sstevel@tonic-gate * 4.3 BSD under license from the Regents of the University of
330Sstevel@tonic-gate * California.
340Sstevel@tonic-gate */
350Sstevel@tonic-gate
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate * svc.c, Server-side remote procedure call interface.
380Sstevel@tonic-gate *
390Sstevel@tonic-gate * There are two sets of procedures here. The xprt routines are
400Sstevel@tonic-gate * for handling transport handles. The svc routines handle the
410Sstevel@tonic-gate * list of service routines.
420Sstevel@tonic-gate *
430Sstevel@tonic-gate */
440Sstevel@tonic-gate
450Sstevel@tonic-gate
460Sstevel@tonic-gate #include "mt.h"
470Sstevel@tonic-gate #include "rpc_mt.h"
480Sstevel@tonic-gate #include <assert.h>
490Sstevel@tonic-gate #include <errno.h>
500Sstevel@tonic-gate #include <sys/types.h>
510Sstevel@tonic-gate #include <stropts.h>
520Sstevel@tonic-gate #include <sys/conf.h>
530Sstevel@tonic-gate #include <rpc/rpc.h>
540Sstevel@tonic-gate #ifdef PORTMAP
550Sstevel@tonic-gate #include <rpc/pmap_clnt.h>
560Sstevel@tonic-gate #endif
570Sstevel@tonic-gate #include <sys/poll.h>
580Sstevel@tonic-gate #include <netconfig.h>
590Sstevel@tonic-gate #include <syslog.h>
600Sstevel@tonic-gate #include <stdlib.h>
610Sstevel@tonic-gate #include <unistd.h>
620Sstevel@tonic-gate #include <string.h>
630Sstevel@tonic-gate #include <limits.h>
640Sstevel@tonic-gate
650Sstevel@tonic-gate extern bool_t __svc_get_door_cred();
660Sstevel@tonic-gate extern bool_t __rpc_get_local_cred();
670Sstevel@tonic-gate
680Sstevel@tonic-gate SVCXPRT **svc_xports;
690Sstevel@tonic-gate static int nsvc_xports; /* total number of svc_xports allocated */
700Sstevel@tonic-gate
710Sstevel@tonic-gate XDR **svc_xdrs; /* common XDR receive area */
720Sstevel@tonic-gate int nsvc_xdrs; /* total number of svc_xdrs allocated */
730Sstevel@tonic-gate
740Sstevel@tonic-gate int __rpc_use_pollfd_done; /* to unlimit the number of connections */
750Sstevel@tonic-gate
760Sstevel@tonic-gate #define NULL_SVC ((struct svc_callout *)0)
770Sstevel@tonic-gate #define RQCRED_SIZE 400 /* this size is excessive */
780Sstevel@tonic-gate
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate * The services list
810Sstevel@tonic-gate * Each entry represents a set of procedures (an rpc program).
820Sstevel@tonic-gate * The dispatch routine takes request structs and runs the
830Sstevel@tonic-gate * appropriate procedure.
840Sstevel@tonic-gate */
850Sstevel@tonic-gate static struct svc_callout {
860Sstevel@tonic-gate struct svc_callout *sc_next;
870Sstevel@tonic-gate rpcprog_t sc_prog;
880Sstevel@tonic-gate rpcvers_t sc_vers;
890Sstevel@tonic-gate char *sc_netid;
900Sstevel@tonic-gate void (*sc_dispatch)();
910Sstevel@tonic-gate } *svc_head;
920Sstevel@tonic-gate extern rwlock_t svc_lock;
930Sstevel@tonic-gate
940Sstevel@tonic-gate static struct svc_callout *svc_find();
950Sstevel@tonic-gate int _svc_prog_dispatch();
960Sstevel@tonic-gate void svc_getreq_common();
970Sstevel@tonic-gate char *strdup();
980Sstevel@tonic-gate
990Sstevel@tonic-gate extern mutex_t svc_door_mutex;
1000Sstevel@tonic-gate extern cond_t svc_door_waitcv;
1010Sstevel@tonic-gate extern int svc_ndoorfds;
1020Sstevel@tonic-gate extern SVCXPRT_LIST *_svc_xprtlist;
1030Sstevel@tonic-gate extern mutex_t xprtlist_lock;
1040Sstevel@tonic-gate extern void __svc_rm_from_xlist();
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate extern fd_set _new_svc_fdset;
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate * If the allocated array of reactor is too small, this value is used as a
1100Sstevel@tonic-gate * margin. This reduces the number of allocations.
1110Sstevel@tonic-gate */
1120Sstevel@tonic-gate #define USER_FD_INCREMENT 5
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate static void add_pollfd(int fd, short events);
1150Sstevel@tonic-gate static void remove_pollfd(int fd);
1160Sstevel@tonic-gate static void __svc_remove_input_of_fd(int fd);
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate * Data used to handle reactor:
1210Sstevel@tonic-gate * - one file descriptor we listen to,
1220Sstevel@tonic-gate * - one callback we call if the fd pops,
1230Sstevel@tonic-gate * - and a cookie passed as a parameter to the callback.
1240Sstevel@tonic-gate *
1250Sstevel@tonic-gate * The structure is an array indexed on the file descriptor. Each entry is
1260Sstevel@tonic-gate * pointing to the first element of a double-linked list of callback.
1270Sstevel@tonic-gate * only one callback may be associated to a couple (fd, event).
1280Sstevel@tonic-gate */
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate struct _svc_user_fd_head;
1310Sstevel@tonic-gate
132132Srobinson typedef struct {
1330Sstevel@tonic-gate struct _svc_user_fd_node *next;
1340Sstevel@tonic-gate struct _svc_user_fd_node *previous;
1350Sstevel@tonic-gate } _svc_user_link;
1360Sstevel@tonic-gate
137132Srobinson typedef struct _svc_user_fd_node {
1380Sstevel@tonic-gate /* The lnk field must be the first field. */
1390Sstevel@tonic-gate _svc_user_link lnk;
1400Sstevel@tonic-gate svc_input_id_t id;
1410Sstevel@tonic-gate int fd;
1420Sstevel@tonic-gate unsigned int events;
1430Sstevel@tonic-gate svc_callback_t callback;
1440Sstevel@tonic-gate void* cookie;
1450Sstevel@tonic-gate } _svc_user_fd_node;
1460Sstevel@tonic-gate
147132Srobinson typedef struct _svc_user_fd_head {
1480Sstevel@tonic-gate /* The lnk field must be the first field. */
1490Sstevel@tonic-gate _svc_user_link lnk;
1500Sstevel@tonic-gate unsigned int mask; /* logical OR of all sub-masks */
1510Sstevel@tonic-gate } _svc_user_fd_head;
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate /* Define some macros to manage the linked list. */
1550Sstevel@tonic-gate #define LIST_ISEMPTY(l) ((_svc_user_fd_node *) &(l.lnk) == l.lnk.next)
1560Sstevel@tonic-gate #define LIST_CLR(l) \
1570Sstevel@tonic-gate (l.lnk.previous = l.lnk.next = (_svc_user_fd_node *) &(l.lnk))
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate /* Array of defined reactor - indexed on file descriptor */
1600Sstevel@tonic-gate static _svc_user_fd_head *svc_userfds = NULL;
1610Sstevel@tonic-gate
1620Sstevel@tonic-gate /* current size of file descriptor */
1630Sstevel@tonic-gate static int svc_nuserfds = 0;
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate /* Mutex to ensure MT safe operations for user fds callbacks. */
1660Sstevel@tonic-gate static mutex_t svc_userfds_lock = DEFAULTMUTEX;
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate * This structure is used to have constant time alogrithms. There is an array
1710Sstevel@tonic-gate * of this structure as large as svc_nuserfds. When the user is registering a
1720Sstevel@tonic-gate * new callback, the address of the created structure is stored in a cell of
1730Sstevel@tonic-gate * this array. The address of this cell is the returned unique identifier.
1740Sstevel@tonic-gate *
1750Sstevel@tonic-gate * On removing, the id is given by the user, then we know if this cell is
1760Sstevel@tonic-gate * filled or not (with free). If it is free, we return an error. Otherwise,
1770Sstevel@tonic-gate * we can free the structure pointed by fd_node.
1780Sstevel@tonic-gate *
1790Sstevel@tonic-gate * On insertion, we use the linked list created by (first_free,
1800Sstevel@tonic-gate * next_free). In this way with a constant time computation, we can give a
1810Sstevel@tonic-gate * correct index to the user.
1820Sstevel@tonic-gate */
1830Sstevel@tonic-gate
184132Srobinson typedef struct _svc_management_user_fd {
1850Sstevel@tonic-gate bool_t free;
1860Sstevel@tonic-gate union {
1870Sstevel@tonic-gate svc_input_id_t next_free;
1880Sstevel@tonic-gate _svc_user_fd_node *fd_node;
1890Sstevel@tonic-gate } data;
1900Sstevel@tonic-gate } _svc_management_user_fd;
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate /* index to the first free elem */
1930Sstevel@tonic-gate static svc_input_id_t first_free = (svc_input_id_t)-1;
1940Sstevel@tonic-gate /* the size of this array is the same as svc_nuserfds */
1950Sstevel@tonic-gate static _svc_management_user_fd* user_fd_mgt_array = NULL;
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate /* current size of user_fd_mgt_array */
1980Sstevel@tonic-gate static int svc_nmgtuserfds = 0;
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate /* Define some macros to access data associated to registration ids. */
2020Sstevel@tonic-gate #define node_from_id(id) (user_fd_mgt_array[(int)id].data.fd_node)
2030Sstevel@tonic-gate #define is_free_id(id) (user_fd_mgt_array[(int)id].free)
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate #ifndef POLLSTANDARD
2060Sstevel@tonic-gate #define POLLSTANDARD \
2070Sstevel@tonic-gate (POLLIN|POLLPRI|POLLOUT|POLLRDNORM|POLLRDBAND| \
2080Sstevel@tonic-gate POLLWRBAND|POLLERR|POLLHUP|POLLNVAL)
2090Sstevel@tonic-gate #endif
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate /*
2120Sstevel@tonic-gate * To free an Id, we set the cell as free and insert its address in the list
2130Sstevel@tonic-gate * of free cell.
2140Sstevel@tonic-gate */
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate static void
_svc_free_id(const svc_input_id_t id)2170Sstevel@tonic-gate _svc_free_id(const svc_input_id_t id)
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate assert(((int)id >= 0) && ((int)id < svc_nmgtuserfds));
2200Sstevel@tonic-gate user_fd_mgt_array[(int)id].free = TRUE;
2210Sstevel@tonic-gate user_fd_mgt_array[(int)id].data.next_free = first_free;
2220Sstevel@tonic-gate first_free = id;
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate /*
2260Sstevel@tonic-gate * To get a free cell, we just have to take it from the free linked list and
2270Sstevel@tonic-gate * set the flag to "not free". This function also allocates new memory if
2280Sstevel@tonic-gate * necessary
2290Sstevel@tonic-gate */
2300Sstevel@tonic-gate static svc_input_id_t
_svc_attribute_new_id(_svc_user_fd_node * node)2310Sstevel@tonic-gate _svc_attribute_new_id(_svc_user_fd_node *node)
2320Sstevel@tonic-gate {
2330Sstevel@tonic-gate int selected_index = (int)first_free;
2340Sstevel@tonic-gate assert(node != NULL);
2350Sstevel@tonic-gate
2360Sstevel@tonic-gate if (selected_index == -1) {
2370Sstevel@tonic-gate /* Allocate new entries */
2380Sstevel@tonic-gate int L_inOldSize = svc_nmgtuserfds;
2390Sstevel@tonic-gate int i;
2400Sstevel@tonic-gate
2410Sstevel@tonic-gate svc_nmgtuserfds += USER_FD_INCREMENT;
2420Sstevel@tonic-gate
2430Sstevel@tonic-gate user_fd_mgt_array = (_svc_management_user_fd *)
2440Sstevel@tonic-gate realloc(user_fd_mgt_array, svc_nmgtuserfds
2450Sstevel@tonic-gate * sizeof (_svc_management_user_fd));
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate if (user_fd_mgt_array == NULL) {
2480Sstevel@tonic-gate syslog(LOG_ERR, "_svc_attribute_new_id: out of memory");
2490Sstevel@tonic-gate errno = ENOMEM;
2500Sstevel@tonic-gate return ((svc_input_id_t)-1);
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate for (i = svc_nmgtuserfds - 1; i >= L_inOldSize; i--)
2540Sstevel@tonic-gate _svc_free_id((svc_input_id_t)i);
2550Sstevel@tonic-gate selected_index = (int)first_free;
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate node->id = (svc_input_id_t)selected_index;
2590Sstevel@tonic-gate first_free = user_fd_mgt_array[selected_index].data.next_free;
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate user_fd_mgt_array[selected_index].data.fd_node = node;
2620Sstevel@tonic-gate user_fd_mgt_array[selected_index].free = FALSE;
2630Sstevel@tonic-gate
2640Sstevel@tonic-gate return ((svc_input_id_t)selected_index);
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate * Access to a pollfd treatment. Scan all the associated callbacks that have
2690Sstevel@tonic-gate * at least one bit in their mask that masks a received event.
2700Sstevel@tonic-gate *
2710Sstevel@tonic-gate * If event POLLNVAL is received, we check that one callback processes it, if
2720Sstevel@tonic-gate * not, then remove the file descriptor from the poll. If there is one, let
2730Sstevel@tonic-gate * the user do the work.
2740Sstevel@tonic-gate */
2750Sstevel@tonic-gate void
__svc_getreq_user(struct pollfd * pfd)2760Sstevel@tonic-gate __svc_getreq_user(struct pollfd *pfd)
2770Sstevel@tonic-gate {
2780Sstevel@tonic-gate int fd = pfd->fd;
2790Sstevel@tonic-gate short revents = pfd->revents;
2800Sstevel@tonic-gate bool_t invalHandled = FALSE;
2810Sstevel@tonic-gate _svc_user_fd_node *node;
2820Sstevel@tonic-gate
283132Srobinson (void) mutex_lock(&svc_userfds_lock);
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate if ((fd < 0) || (fd >= svc_nuserfds)) {
286132Srobinson (void) mutex_unlock(&svc_userfds_lock);
2870Sstevel@tonic-gate return;
2880Sstevel@tonic-gate }
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate node = svc_userfds[fd].lnk.next;
2910Sstevel@tonic-gate
2920Sstevel@tonic-gate /* check if at least one mask fits */
2930Sstevel@tonic-gate if (0 == (revents & svc_userfds[fd].mask)) {
294132Srobinson (void) mutex_unlock(&svc_userfds_lock);
2950Sstevel@tonic-gate return;
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate while ((svc_userfds[fd].mask != 0) &&
2990Sstevel@tonic-gate ((_svc_user_link *)node != &(svc_userfds[fd].lnk))) {
3000Sstevel@tonic-gate /*
3010Sstevel@tonic-gate * If one of the received events maps the ones the node listens
3020Sstevel@tonic-gate * to
3030Sstevel@tonic-gate */
3040Sstevel@tonic-gate _svc_user_fd_node *next = node->lnk.next;
3050Sstevel@tonic-gate
3060Sstevel@tonic-gate if (node->callback != NULL) {
3070Sstevel@tonic-gate if (node->events & revents) {
3080Sstevel@tonic-gate if (revents & POLLNVAL) {
3090Sstevel@tonic-gate invalHandled = TRUE;
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate * The lock must be released before calling the
3140Sstevel@tonic-gate * user function, as this function can call
3150Sstevel@tonic-gate * svc_remove_input() for example.
3160Sstevel@tonic-gate */
317132Srobinson (void) mutex_unlock(&svc_userfds_lock);
3180Sstevel@tonic-gate node->callback(node->id, node->fd,
3190Sstevel@tonic-gate node->events & revents, node->cookie);
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate * Do not use the node structure anymore, as it
3220Sstevel@tonic-gate * could have been deallocated by the previous
3230Sstevel@tonic-gate * callback.
3240Sstevel@tonic-gate */
325132Srobinson (void) mutex_lock(&svc_userfds_lock);
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate node = next;
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate
3310Sstevel@tonic-gate if ((revents & POLLNVAL) && !invalHandled)
3320Sstevel@tonic-gate __svc_remove_input_of_fd(fd);
333132Srobinson (void) mutex_unlock(&svc_userfds_lock);
3340Sstevel@tonic-gate }
3350Sstevel@tonic-gate
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate /*
3380Sstevel@tonic-gate * Check if a file descriptor is associated with a user reactor.
3390Sstevel@tonic-gate * To do this, just check that the array indexed on fd has a non-void linked
3400Sstevel@tonic-gate * list (ie. first element is not NULL)
3410Sstevel@tonic-gate */
3420Sstevel@tonic-gate bool_t
__is_a_userfd(int fd)3430Sstevel@tonic-gate __is_a_userfd(int fd)
3440Sstevel@tonic-gate {
3450Sstevel@tonic-gate /* Checks argument */
3460Sstevel@tonic-gate if ((fd < 0) || (fd >= svc_nuserfds))
3470Sstevel@tonic-gate return (FALSE);
3480Sstevel@tonic-gate return ((svc_userfds[fd].mask == 0x0000)? FALSE:TRUE);
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate /* free everything concerning user fd */
3520Sstevel@tonic-gate /* used in svc_run.c => no static */
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate void
__destroy_userfd(void)355132Srobinson __destroy_userfd(void)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate int one_fd;
3580Sstevel@tonic-gate /* Clean user fd */
3590Sstevel@tonic-gate if (svc_userfds != NULL) {
3600Sstevel@tonic-gate for (one_fd = 0; one_fd < svc_nuserfds; one_fd++) {
3610Sstevel@tonic-gate _svc_user_fd_node *node;
3620Sstevel@tonic-gate
3630Sstevel@tonic-gate node = svc_userfds[one_fd].lnk.next;
3640Sstevel@tonic-gate while ((_svc_user_link *) node
3650Sstevel@tonic-gate != (_svc_user_link *) &(svc_userfds[one_fd])) {
3660Sstevel@tonic-gate _svc_free_id(node->id);
3670Sstevel@tonic-gate node = node->lnk.next;
3680Sstevel@tonic-gate free(node->lnk.previous);
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate }
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate free(user_fd_mgt_array);
3730Sstevel@tonic-gate user_fd_mgt_array = NULL;
3740Sstevel@tonic-gate first_free = (svc_input_id_t)-1;
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate free(svc_userfds);
3770Sstevel@tonic-gate svc_userfds = NULL;
3780Sstevel@tonic-gate svc_nuserfds = 0;
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate /*
3830Sstevel@tonic-gate * Remove all the callback associated with a fd => useful when the fd is
3840Sstevel@tonic-gate * closed for instance
3850Sstevel@tonic-gate */
3860Sstevel@tonic-gate static void
__svc_remove_input_of_fd(int fd)3870Sstevel@tonic-gate __svc_remove_input_of_fd(int fd)
3880Sstevel@tonic-gate {
3890Sstevel@tonic-gate _svc_user_fd_node *one_node;
3900Sstevel@tonic-gate
3910Sstevel@tonic-gate if ((fd < 0) || (fd >= svc_nuserfds))
3920Sstevel@tonic-gate return;
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate one_node = svc_userfds[fd].lnk.next;
3950Sstevel@tonic-gate while ((_svc_user_link *) one_node
3960Sstevel@tonic-gate != (_svc_user_link *) &(svc_userfds[fd].lnk)) {
3970Sstevel@tonic-gate _svc_free_id(one_node->id);
3980Sstevel@tonic-gate one_node = one_node->lnk.next;
3990Sstevel@tonic-gate free(one_node->lnk.previous);
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate
4020Sstevel@tonic-gate LIST_CLR(svc_userfds[fd]);
4030Sstevel@tonic-gate svc_userfds[fd].mask = 0;
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate
4060Sstevel@tonic-gate /*
4070Sstevel@tonic-gate * Allow user to add an fd in the poll list. If it does not succeed, return
4080Sstevel@tonic-gate * -1. Otherwise, return a svc_id
4090Sstevel@tonic-gate */
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate svc_input_id_t
svc_add_input(int user_fd,unsigned int events,svc_callback_t user_callback,void * cookie)4120Sstevel@tonic-gate svc_add_input(int user_fd, unsigned int events,
4130Sstevel@tonic-gate svc_callback_t user_callback, void *cookie)
4140Sstevel@tonic-gate {
4150Sstevel@tonic-gate _svc_user_fd_node *new_node;
4160Sstevel@tonic-gate
4170Sstevel@tonic-gate if (user_fd < 0) {
4180Sstevel@tonic-gate errno = EINVAL;
4190Sstevel@tonic-gate return ((svc_input_id_t)-1);
4200Sstevel@tonic-gate }
4210Sstevel@tonic-gate
4220Sstevel@tonic-gate if ((events == 0x0000) ||
4230Sstevel@tonic-gate (events & ~(POLLIN|POLLPRI|POLLOUT|POLLRDNORM|POLLRDBAND|\
4240Sstevel@tonic-gate POLLWRBAND|POLLERR|POLLHUP|POLLNVAL))) {
4250Sstevel@tonic-gate errno = EINVAL;
4260Sstevel@tonic-gate return ((svc_input_id_t)-1);
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate
429132Srobinson (void) mutex_lock(&svc_userfds_lock);
4300Sstevel@tonic-gate
4310Sstevel@tonic-gate if ((user_fd < svc_nuserfds) &&
4320Sstevel@tonic-gate (svc_userfds[user_fd].mask & events) != 0) {
4330Sstevel@tonic-gate /* Already registrated call-back */
4340Sstevel@tonic-gate errno = EEXIST;
435132Srobinson (void) mutex_unlock(&svc_userfds_lock);
4360Sstevel@tonic-gate return ((svc_input_id_t)-1);
4370Sstevel@tonic-gate }
4380Sstevel@tonic-gate
4390Sstevel@tonic-gate /* Handle memory allocation. */
4400Sstevel@tonic-gate if (user_fd >= svc_nuserfds) {
4410Sstevel@tonic-gate int oldSize = svc_nuserfds;
4420Sstevel@tonic-gate int i;
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate svc_nuserfds = (user_fd + 1) + USER_FD_INCREMENT;
4450Sstevel@tonic-gate
4460Sstevel@tonic-gate svc_userfds = (_svc_user_fd_head *)
4470Sstevel@tonic-gate realloc(svc_userfds,
4480Sstevel@tonic-gate svc_nuserfds * sizeof (_svc_user_fd_head));
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate if (svc_userfds == NULL) {
4510Sstevel@tonic-gate syslog(LOG_ERR, "svc_add_input: out of memory");
4520Sstevel@tonic-gate errno = ENOMEM;
453132Srobinson (void) mutex_unlock(&svc_userfds_lock);
4540Sstevel@tonic-gate return ((svc_input_id_t)-1);
4550Sstevel@tonic-gate }
4560Sstevel@tonic-gate
4570Sstevel@tonic-gate for (i = oldSize; i < svc_nuserfds; i++) {
4580Sstevel@tonic-gate LIST_CLR(svc_userfds[i]);
4590Sstevel@tonic-gate svc_userfds[i].mask = 0;
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate }
4620Sstevel@tonic-gate
463132Srobinson new_node = malloc(sizeof (_svc_user_fd_node));
4640Sstevel@tonic-gate if (new_node == NULL) {
4650Sstevel@tonic-gate syslog(LOG_ERR, "svc_add_input: out of memory");
4660Sstevel@tonic-gate errno = ENOMEM;
467132Srobinson (void) mutex_unlock(&svc_userfds_lock);
4680Sstevel@tonic-gate return ((svc_input_id_t)-1);
4690Sstevel@tonic-gate }
4700Sstevel@tonic-gate
4710Sstevel@tonic-gate /* create a new node */
4720Sstevel@tonic-gate new_node->fd = user_fd;
4730Sstevel@tonic-gate new_node->events = events;
4740Sstevel@tonic-gate new_node->callback = user_callback;
4750Sstevel@tonic-gate new_node->cookie = cookie;
4760Sstevel@tonic-gate
4770Sstevel@tonic-gate (void) _svc_attribute_new_id(new_node);
4780Sstevel@tonic-gate
4790Sstevel@tonic-gate /* Add the new element at the beginning of the list. */
4800Sstevel@tonic-gate if (LIST_ISEMPTY(svc_userfds[user_fd])) {
4810Sstevel@tonic-gate svc_userfds[user_fd].lnk.previous = new_node;
4820Sstevel@tonic-gate }
4830Sstevel@tonic-gate new_node->lnk.next = svc_userfds[user_fd].lnk.next;
4840Sstevel@tonic-gate new_node->lnk.previous = (_svc_user_fd_node *)&(svc_userfds[user_fd]);
4850Sstevel@tonic-gate
4860Sstevel@tonic-gate svc_userfds[user_fd].lnk.next = new_node;
4870Sstevel@tonic-gate
4880Sstevel@tonic-gate /* refresh global mask for this file desciptor */
4890Sstevel@tonic-gate svc_userfds[user_fd].mask |= events;
4900Sstevel@tonic-gate
4910Sstevel@tonic-gate /* refresh mask for the poll */
4920Sstevel@tonic-gate add_pollfd(user_fd, (svc_userfds[user_fd].mask));
4930Sstevel@tonic-gate
494132Srobinson (void) mutex_unlock(&svc_userfds_lock);
4950Sstevel@tonic-gate return (new_node->id);
4960Sstevel@tonic-gate }
4970Sstevel@tonic-gate
4980Sstevel@tonic-gate
4990Sstevel@tonic-gate int
svc_remove_input(svc_input_id_t id)5000Sstevel@tonic-gate svc_remove_input(svc_input_id_t id)
5010Sstevel@tonic-gate {
5020Sstevel@tonic-gate _svc_user_fd_node* node;
5030Sstevel@tonic-gate _svc_user_fd_node* next;
5040Sstevel@tonic-gate _svc_user_fd_node* previous;
5050Sstevel@tonic-gate int fd; /* caching optim */
5060Sstevel@tonic-gate
507132Srobinson (void) mutex_lock(&svc_userfds_lock);
5080Sstevel@tonic-gate
5090Sstevel@tonic-gate /* Immediately update data for id management */
5100Sstevel@tonic-gate if (user_fd_mgt_array == NULL || id >= svc_nmgtuserfds ||
5110Sstevel@tonic-gate is_free_id(id)) {
5120Sstevel@tonic-gate errno = EINVAL;
513132Srobinson (void) mutex_unlock(&svc_userfds_lock);
5140Sstevel@tonic-gate return (-1);
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate
5170Sstevel@tonic-gate node = node_from_id(id);
5180Sstevel@tonic-gate assert(node != NULL);
5190Sstevel@tonic-gate
5200Sstevel@tonic-gate _svc_free_id(id);
5210Sstevel@tonic-gate next = node->lnk.next;
5220Sstevel@tonic-gate previous = node->lnk.previous;
5230Sstevel@tonic-gate fd = node->fd; /* caching optim */
5240Sstevel@tonic-gate
5250Sstevel@tonic-gate /* Remove this node from the list. */
5260Sstevel@tonic-gate previous->lnk.next = next;
5270Sstevel@tonic-gate next->lnk.previous = previous;
5280Sstevel@tonic-gate
5290Sstevel@tonic-gate /* Remove the node flags from the global mask */
5300Sstevel@tonic-gate svc_userfds[fd].mask ^= node->events;
5310Sstevel@tonic-gate
5320Sstevel@tonic-gate free(node);
5330Sstevel@tonic-gate if (svc_userfds[fd].mask == 0) {
5340Sstevel@tonic-gate LIST_CLR(svc_userfds[fd]);
5350Sstevel@tonic-gate assert(LIST_ISEMPTY(svc_userfds[fd]));
5360Sstevel@tonic-gate remove_pollfd(fd);
5370Sstevel@tonic-gate }
5380Sstevel@tonic-gate /* <=> CLEAN NEEDED TO SHRINK MEMORY USAGE */
5390Sstevel@tonic-gate
540132Srobinson (void) mutex_unlock(&svc_userfds_lock);
5410Sstevel@tonic-gate return (0);
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate
5440Sstevel@tonic-gate
5450Sstevel@tonic-gate /*
5460Sstevel@tonic-gate * Provides default service-side functions for authentication flavors
5470Sstevel@tonic-gate * that do not use all the fields in struct svc_auth_ops.
5480Sstevel@tonic-gate */
5490Sstevel@tonic-gate
5500Sstevel@tonic-gate /*ARGSUSED*/
5510Sstevel@tonic-gate static int
authany_wrap(AUTH * auth,XDR * xdrs,xdrproc_t xfunc,caddr_t xwhere)552132Srobinson authany_wrap(AUTH *auth, XDR *xdrs, xdrproc_t xfunc, caddr_t xwhere)
5530Sstevel@tonic-gate {
5540Sstevel@tonic-gate return (*xfunc)(xdrs, xwhere);
5550Sstevel@tonic-gate }
5560Sstevel@tonic-gate
5570Sstevel@tonic-gate struct svc_auth_ops svc_auth_any_ops = {
5580Sstevel@tonic-gate authany_wrap,
5590Sstevel@tonic-gate authany_wrap,
5600Sstevel@tonic-gate };
5610Sstevel@tonic-gate
5620Sstevel@tonic-gate /*
5630Sstevel@tonic-gate * Return pointer to server authentication structure.
5640Sstevel@tonic-gate */
5650Sstevel@tonic-gate SVCAUTH *
__svc_get_svcauth(SVCXPRT * xprt)566132Srobinson __svc_get_svcauth(SVCXPRT *xprt)
5670Sstevel@tonic-gate {
5680Sstevel@tonic-gate /* LINTED pointer alignment */
5690Sstevel@tonic-gate return (&SVC_XP_AUTH(xprt));
5700Sstevel@tonic-gate }
5710Sstevel@tonic-gate
5720Sstevel@tonic-gate /*
5730Sstevel@tonic-gate * A callback routine to cleanup after a procedure is executed.
5740Sstevel@tonic-gate */
5750Sstevel@tonic-gate void (*__proc_cleanup_cb)() = NULL;
5760Sstevel@tonic-gate
5770Sstevel@tonic-gate void *
__svc_set_proc_cleanup_cb(void * cb)578132Srobinson __svc_set_proc_cleanup_cb(void *cb)
5790Sstevel@tonic-gate {
5800Sstevel@tonic-gate void *tmp = (void *)__proc_cleanup_cb;
5810Sstevel@tonic-gate
5820Sstevel@tonic-gate __proc_cleanup_cb = (void (*)())cb;
5830Sstevel@tonic-gate return (tmp);
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate
5860Sstevel@tonic-gate /* *************** SVCXPRT related stuff **************** */
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate
5890Sstevel@tonic-gate static int pollfd_shrinking = 1;
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate
5920Sstevel@tonic-gate /*
5930Sstevel@tonic-gate * Add fd to svc_pollfd
5940Sstevel@tonic-gate */
5950Sstevel@tonic-gate static void
add_pollfd(int fd,short events)5960Sstevel@tonic-gate add_pollfd(int fd, short events)
5970Sstevel@tonic-gate {
5980Sstevel@tonic-gate if (fd < FD_SETSIZE) {
5990Sstevel@tonic-gate FD_SET(fd, &svc_fdset);
6000Sstevel@tonic-gate #if !defined(_LP64)
6010Sstevel@tonic-gate FD_SET(fd, &_new_svc_fdset);
6020Sstevel@tonic-gate #endif
6030Sstevel@tonic-gate svc_nfds++;
6040Sstevel@tonic-gate svc_nfds_set++;
6050Sstevel@tonic-gate if (fd >= svc_max_fd)
6060Sstevel@tonic-gate svc_max_fd = fd + 1;
6070Sstevel@tonic-gate }
6080Sstevel@tonic-gate if (fd >= svc_max_pollfd)
6090Sstevel@tonic-gate svc_max_pollfd = fd + 1;
6100Sstevel@tonic-gate if (svc_max_pollfd > svc_pollfd_allocd) {
6110Sstevel@tonic-gate int i = svc_pollfd_allocd;
6120Sstevel@tonic-gate pollfd_t *tmp;
6130Sstevel@tonic-gate do {
6140Sstevel@tonic-gate svc_pollfd_allocd += POLLFD_EXTEND;
6150Sstevel@tonic-gate } while (svc_max_pollfd > svc_pollfd_allocd);
6160Sstevel@tonic-gate tmp = realloc(svc_pollfd,
6170Sstevel@tonic-gate sizeof (pollfd_t) * svc_pollfd_allocd);
6180Sstevel@tonic-gate if (tmp != NULL) {
6190Sstevel@tonic-gate svc_pollfd = tmp;
6200Sstevel@tonic-gate for (; i < svc_pollfd_allocd; i++)
6210Sstevel@tonic-gate POLLFD_CLR(i, tmp);
6220Sstevel@tonic-gate } else {
6230Sstevel@tonic-gate /*
6240Sstevel@tonic-gate * give an error message; undo fdset setting
6250Sstevel@tonic-gate * above; reset the pollfd_shrinking flag.
6260Sstevel@tonic-gate * because of this poll will not be done
6270Sstevel@tonic-gate * on these fds.
6280Sstevel@tonic-gate */
6290Sstevel@tonic-gate if (fd < FD_SETSIZE) {
6300Sstevel@tonic-gate FD_CLR(fd, &svc_fdset);
6310Sstevel@tonic-gate #if !defined(_LP64)
6320Sstevel@tonic-gate FD_CLR(fd, &_new_svc_fdset);
6330Sstevel@tonic-gate #endif
6340Sstevel@tonic-gate svc_nfds--;
6350Sstevel@tonic-gate svc_nfds_set--;
6360Sstevel@tonic-gate if (fd == (svc_max_fd - 1))
6370Sstevel@tonic-gate svc_max_fd--;
6380Sstevel@tonic-gate }
6390Sstevel@tonic-gate if (fd == (svc_max_pollfd - 1))
6400Sstevel@tonic-gate svc_max_pollfd--;
6410Sstevel@tonic-gate pollfd_shrinking = 0;
6420Sstevel@tonic-gate syslog(LOG_ERR, "add_pollfd: out of memory");
6430Sstevel@tonic-gate _exit(1);
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate svc_pollfd[fd].fd = fd;
6470Sstevel@tonic-gate svc_pollfd[fd].events = events;
6480Sstevel@tonic-gate svc_npollfds++;
6490Sstevel@tonic-gate svc_npollfds_set++;
6500Sstevel@tonic-gate }
6510Sstevel@tonic-gate
6520Sstevel@tonic-gate /*
6530Sstevel@tonic-gate * the fd is still active but only the bit in fdset is cleared.
6540Sstevel@tonic-gate * do not subtract svc_nfds or svc_npollfds
6550Sstevel@tonic-gate */
6560Sstevel@tonic-gate void
clear_pollfd(int fd)6570Sstevel@tonic-gate clear_pollfd(int fd)
6580Sstevel@tonic-gate {
6590Sstevel@tonic-gate if (fd < FD_SETSIZE && FD_ISSET(fd, &svc_fdset)) {
6600Sstevel@tonic-gate FD_CLR(fd, &svc_fdset);
6610Sstevel@tonic-gate #if !defined(_LP64)
6620Sstevel@tonic-gate FD_CLR(fd, &_new_svc_fdset);
6630Sstevel@tonic-gate #endif
6640Sstevel@tonic-gate svc_nfds_set--;
6650Sstevel@tonic-gate }
6660Sstevel@tonic-gate if (fd < svc_pollfd_allocd && POLLFD_ISSET(fd, svc_pollfd)) {
6670Sstevel@tonic-gate POLLFD_CLR(fd, svc_pollfd);
6680Sstevel@tonic-gate svc_npollfds_set--;
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate }
6710Sstevel@tonic-gate
6720Sstevel@tonic-gate /*
6730Sstevel@tonic-gate * sets the bit in fdset for an active fd so that poll() is done for that
6740Sstevel@tonic-gate */
6750Sstevel@tonic-gate void
set_pollfd(int fd,short events)6760Sstevel@tonic-gate set_pollfd(int fd, short events)
6770Sstevel@tonic-gate {
6780Sstevel@tonic-gate if (fd < FD_SETSIZE) {
6790Sstevel@tonic-gate FD_SET(fd, &svc_fdset);
6800Sstevel@tonic-gate #if !defined(_LP64)
6810Sstevel@tonic-gate FD_SET(fd, &_new_svc_fdset);
6820Sstevel@tonic-gate #endif
6830Sstevel@tonic-gate svc_nfds_set++;
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate if (fd < svc_pollfd_allocd) {
6860Sstevel@tonic-gate svc_pollfd[fd].fd = fd;
6870Sstevel@tonic-gate svc_pollfd[fd].events = events;
6880Sstevel@tonic-gate svc_npollfds_set++;
6890Sstevel@tonic-gate }
6900Sstevel@tonic-gate }
6910Sstevel@tonic-gate
6920Sstevel@tonic-gate /*
6930Sstevel@tonic-gate * remove a svc_pollfd entry; it does not shrink the memory
6940Sstevel@tonic-gate */
6950Sstevel@tonic-gate static void
remove_pollfd(int fd)696132Srobinson remove_pollfd(int fd)
6970Sstevel@tonic-gate {
6980Sstevel@tonic-gate clear_pollfd(fd);
6990Sstevel@tonic-gate if (fd == (svc_max_fd - 1))
7000Sstevel@tonic-gate svc_max_fd--;
7010Sstevel@tonic-gate svc_nfds--;
7020Sstevel@tonic-gate if (fd == (svc_max_pollfd - 1))
7030Sstevel@tonic-gate svc_max_pollfd--;
7040Sstevel@tonic-gate svc_npollfds--;
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate
7070Sstevel@tonic-gate /*
7080Sstevel@tonic-gate * delete a svc_pollfd entry; it shrinks the memory
7090Sstevel@tonic-gate * use remove_pollfd if you do not want to shrink
7100Sstevel@tonic-gate */
7110Sstevel@tonic-gate static void
delete_pollfd(int fd)7120Sstevel@tonic-gate delete_pollfd(int fd)
7130Sstevel@tonic-gate {
7140Sstevel@tonic-gate remove_pollfd(fd);
7150Sstevel@tonic-gate if (pollfd_shrinking && svc_max_pollfd <
7160Sstevel@tonic-gate (svc_pollfd_allocd - POLLFD_SHRINK)) {
7170Sstevel@tonic-gate do {
7180Sstevel@tonic-gate svc_pollfd_allocd -= POLLFD_SHRINK;
7190Sstevel@tonic-gate } while (svc_max_pollfd < (svc_pollfd_allocd - POLLFD_SHRINK));
7200Sstevel@tonic-gate svc_pollfd = realloc(svc_pollfd,
7210Sstevel@tonic-gate sizeof (pollfd_t) * svc_pollfd_allocd);
7220Sstevel@tonic-gate if (svc_pollfd == NULL) {
7230Sstevel@tonic-gate syslog(LOG_ERR, "delete_pollfd: out of memory");
7240Sstevel@tonic-gate _exit(1);
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate }
7280Sstevel@tonic-gate
7290Sstevel@tonic-gate
7300Sstevel@tonic-gate /*
7310Sstevel@tonic-gate * Activate a transport handle.
7320Sstevel@tonic-gate */
7330Sstevel@tonic-gate void
xprt_register(const SVCXPRT * xprt)734132Srobinson xprt_register(const SVCXPRT *xprt)
7350Sstevel@tonic-gate {
7360Sstevel@tonic-gate int fd = xprt->xp_fd;
7370Sstevel@tonic-gate #ifdef CALLBACK
7380Sstevel@tonic-gate extern void (*_svc_getreqset_proc)();
7390Sstevel@tonic-gate #endif
7400Sstevel@tonic-gate /* VARIABLES PROTECTED BY svc_fd_lock: svc_xports, svc_fdset */
7410Sstevel@tonic-gate
742132Srobinson (void) rw_wrlock(&svc_fd_lock);
7430Sstevel@tonic-gate if (svc_xports == NULL) {
7440Sstevel@tonic-gate /* allocate some small amount first */
7450Sstevel@tonic-gate svc_xports = calloc(FD_INCREMENT, sizeof (SVCXPRT *));
7460Sstevel@tonic-gate if (svc_xports == NULL) {
7470Sstevel@tonic-gate syslog(LOG_ERR, "xprt_register: out of memory");
7480Sstevel@tonic-gate _exit(1);
7490Sstevel@tonic-gate }
7500Sstevel@tonic-gate nsvc_xports = FD_INCREMENT;
7510Sstevel@tonic-gate
7520Sstevel@tonic-gate #ifdef CALLBACK
7530Sstevel@tonic-gate /*
7540Sstevel@tonic-gate * XXX: This code does not keep track of the server state.
7550Sstevel@tonic-gate *
7560Sstevel@tonic-gate * This provides for callback support. When a client
7570Sstevel@tonic-gate * recv's a call from another client on the server fd's,
7580Sstevel@tonic-gate * it calls _svc_getreqset_proc() which would return
7590Sstevel@tonic-gate * after serving all the server requests. Also look under
7600Sstevel@tonic-gate * clnt_dg.c and clnt_vc.c (clnt_call part of it)
7610Sstevel@tonic-gate */
7620Sstevel@tonic-gate _svc_getreqset_proc = svc_getreq_poll;
7630Sstevel@tonic-gate #endif
7640Sstevel@tonic-gate }
7650Sstevel@tonic-gate
7660Sstevel@tonic-gate while (fd >= nsvc_xports) {
7670Sstevel@tonic-gate SVCXPRT **tmp_xprts = svc_xports;
7680Sstevel@tonic-gate
7690Sstevel@tonic-gate /* time to expand svc_xprts */
7700Sstevel@tonic-gate tmp_xprts = realloc(svc_xports,
7710Sstevel@tonic-gate sizeof (SVCXPRT *) * (nsvc_xports + FD_INCREMENT));
7720Sstevel@tonic-gate if (tmp_xprts == NULL) {
7730Sstevel@tonic-gate syslog(LOG_ERR, "xprt_register : out of memory.");
7740Sstevel@tonic-gate _exit(1);
7750Sstevel@tonic-gate }
7760Sstevel@tonic-gate
7770Sstevel@tonic-gate svc_xports = tmp_xprts;
7780Sstevel@tonic-gate (void) memset(&svc_xports[nsvc_xports], 0,
7790Sstevel@tonic-gate sizeof (SVCXPRT *) * FD_INCREMENT);
7800Sstevel@tonic-gate nsvc_xports += FD_INCREMENT;
7810Sstevel@tonic-gate }
7820Sstevel@tonic-gate
7830Sstevel@tonic-gate svc_xports[fd] = (SVCXPRT *)xprt;
7840Sstevel@tonic-gate
7850Sstevel@tonic-gate add_pollfd(fd, MASKVAL);
7860Sstevel@tonic-gate
7870Sstevel@tonic-gate if (svc_polling) {
7880Sstevel@tonic-gate char dummy;
7890Sstevel@tonic-gate
7900Sstevel@tonic-gate /*
7910Sstevel@tonic-gate * This happens only in one of the MT modes.
7920Sstevel@tonic-gate * Wake up poller.
7930Sstevel@tonic-gate */
794132Srobinson (void) write(svc_pipe[1], &dummy, sizeof (dummy));
7950Sstevel@tonic-gate }
7960Sstevel@tonic-gate /*
7970Sstevel@tonic-gate * If already dispatching door based services, start
7980Sstevel@tonic-gate * dispatching TLI based services now.
7990Sstevel@tonic-gate */
800132Srobinson (void) mutex_lock(&svc_door_mutex);
8010Sstevel@tonic-gate if (svc_ndoorfds > 0)
802132Srobinson (void) cond_signal(&svc_door_waitcv);
803132Srobinson (void) mutex_unlock(&svc_door_mutex);
8040Sstevel@tonic-gate
8050Sstevel@tonic-gate if (svc_xdrs == NULL) {
8060Sstevel@tonic-gate /* allocate initial chunk */
8070Sstevel@tonic-gate svc_xdrs = calloc(FD_INCREMENT, sizeof (XDR *));
8080Sstevel@tonic-gate if (svc_xdrs != NULL)
8090Sstevel@tonic-gate nsvc_xdrs = FD_INCREMENT;
8100Sstevel@tonic-gate else {
8110Sstevel@tonic-gate syslog(LOG_ERR, "xprt_register : out of memory.");
8120Sstevel@tonic-gate _exit(1);
8130Sstevel@tonic-gate }
8140Sstevel@tonic-gate }
815132Srobinson (void) rw_unlock(&svc_fd_lock);
8160Sstevel@tonic-gate }
8170Sstevel@tonic-gate
8180Sstevel@tonic-gate /*
8190Sstevel@tonic-gate * De-activate a transport handle.
8200Sstevel@tonic-gate */
8210Sstevel@tonic-gate void
__xprt_unregister_private(const SVCXPRT * xprt,bool_t lock_not_held)8220Sstevel@tonic-gate __xprt_unregister_private(const SVCXPRT *xprt, bool_t lock_not_held)
8230Sstevel@tonic-gate {
8240Sstevel@tonic-gate int fd = xprt->xp_fd;
8250Sstevel@tonic-gate
8260Sstevel@tonic-gate if (lock_not_held)
827132Srobinson (void) rw_wrlock(&svc_fd_lock);
8280Sstevel@tonic-gate if ((fd < nsvc_xports) && (svc_xports[fd] == xprt)) {
829132Srobinson svc_xports[fd] = NULL;
8300Sstevel@tonic-gate delete_pollfd(fd);
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate if (lock_not_held)
833132Srobinson (void) rw_unlock(&svc_fd_lock);
8340Sstevel@tonic-gate __svc_rm_from_xlist(&_svc_xprtlist, xprt, &xprtlist_lock);
8350Sstevel@tonic-gate }
8360Sstevel@tonic-gate
8370Sstevel@tonic-gate void
xprt_unregister(const SVCXPRT * xprt)838132Srobinson xprt_unregister(const SVCXPRT *xprt)
8390Sstevel@tonic-gate {
8400Sstevel@tonic-gate __xprt_unregister_private(xprt, TRUE);
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate
8430Sstevel@tonic-gate /* ********************** CALLOUT list related stuff ************* */
8440Sstevel@tonic-gate
8450Sstevel@tonic-gate /*
8460Sstevel@tonic-gate * Add a service program to the callout list.
8470Sstevel@tonic-gate * The dispatch routine will be called when a rpc request for this
8480Sstevel@tonic-gate * program number comes in.
8490Sstevel@tonic-gate */
8500Sstevel@tonic-gate bool_t
svc_reg(const SVCXPRT * xprt,const rpcprog_t prog,const rpcvers_t vers,void (* dispatch)(),const struct netconfig * nconf)851132Srobinson svc_reg(const SVCXPRT *xprt, const rpcprog_t prog, const rpcvers_t vers,
852132Srobinson void (*dispatch)(), const struct netconfig *nconf)
8530Sstevel@tonic-gate {
8540Sstevel@tonic-gate struct svc_callout *prev;
8550Sstevel@tonic-gate struct svc_callout *s, **s2;
8560Sstevel@tonic-gate struct netconfig *tnconf;
8570Sstevel@tonic-gate char *netid = NULL;
8580Sstevel@tonic-gate int flag = 0;
8590Sstevel@tonic-gate
8600Sstevel@tonic-gate /* VARIABLES PROTECTED BY svc_lock: s, prev, svc_head */
8610Sstevel@tonic-gate
8620Sstevel@tonic-gate if (xprt->xp_netid) {
8630Sstevel@tonic-gate netid = strdup(xprt->xp_netid);
8640Sstevel@tonic-gate flag = 1;
8650Sstevel@tonic-gate } else if (nconf && nconf->nc_netid) {
8660Sstevel@tonic-gate netid = strdup(nconf->nc_netid);
8670Sstevel@tonic-gate flag = 1;
8680Sstevel@tonic-gate } else if ((tnconf = __rpcfd_to_nconf(xprt->xp_fd, xprt->xp_type))
8690Sstevel@tonic-gate != NULL) {
8700Sstevel@tonic-gate netid = strdup(tnconf->nc_netid);
8710Sstevel@tonic-gate flag = 1;
8720Sstevel@tonic-gate freenetconfigent(tnconf);
8730Sstevel@tonic-gate } /* must have been created with svc_raw_create */
874132Srobinson if ((netid == NULL) && (flag == 1))
8750Sstevel@tonic-gate return (FALSE);
8760Sstevel@tonic-gate
877132Srobinson (void) rw_wrlock(&svc_lock);
8780Sstevel@tonic-gate if ((s = svc_find(prog, vers, &prev, netid)) != NULL_SVC) {
8790Sstevel@tonic-gate if (netid)
8800Sstevel@tonic-gate free(netid);
8810Sstevel@tonic-gate if (s->sc_dispatch == dispatch)
8820Sstevel@tonic-gate goto rpcb_it; /* he is registering another xptr */
883132Srobinson (void) rw_unlock(&svc_lock);
8840Sstevel@tonic-gate return (FALSE);
8850Sstevel@tonic-gate }
886132Srobinson s = malloc(sizeof (struct svc_callout));
887132Srobinson if (s == NULL) {
8880Sstevel@tonic-gate if (netid)
8890Sstevel@tonic-gate free(netid);
890132Srobinson (void) rw_unlock(&svc_lock);
8910Sstevel@tonic-gate return (FALSE);
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate
8940Sstevel@tonic-gate s->sc_prog = prog;
8950Sstevel@tonic-gate s->sc_vers = vers;
8960Sstevel@tonic-gate s->sc_dispatch = dispatch;
8970Sstevel@tonic-gate s->sc_netid = netid;
8980Sstevel@tonic-gate s->sc_next = NULL;
8990Sstevel@tonic-gate
9000Sstevel@tonic-gate /*
9010Sstevel@tonic-gate * The ordering of transports is such that the most frequently used
9020Sstevel@tonic-gate * one appears first. So add the new entry to the end of the list.
9030Sstevel@tonic-gate */
9040Sstevel@tonic-gate for (s2 = &svc_head; *s2 != NULL; s2 = &(*s2)->sc_next)
9050Sstevel@tonic-gate ;
9060Sstevel@tonic-gate *s2 = s;
9070Sstevel@tonic-gate
9080Sstevel@tonic-gate if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
9090Sstevel@tonic-gate if ((((SVCXPRT *)xprt)->xp_netid = strdup(netid)) == NULL) {
9100Sstevel@tonic-gate syslog(LOG_ERR, "svc_reg : strdup failed.");
9110Sstevel@tonic-gate free(netid);
912132Srobinson free(s);
9130Sstevel@tonic-gate *s2 = NULL;
914132Srobinson (void) rw_unlock(&svc_lock);
9150Sstevel@tonic-gate return (FALSE);
9160Sstevel@tonic-gate }
9170Sstevel@tonic-gate
9180Sstevel@tonic-gate rpcb_it:
919132Srobinson (void) rw_unlock(&svc_lock);
9202712Snn35248
9210Sstevel@tonic-gate /* now register the information with the local binder service */
922*12648SSurya.Prakki@Sun.COM if (nconf)
923132Srobinson return (rpcb_set(prog, vers, nconf, &xprt->xp_ltaddr));
924*12648SSurya.Prakki@Sun.COM return (TRUE);
9252712Snn35248 /*NOTREACHED*/
9260Sstevel@tonic-gate }
9270Sstevel@tonic-gate
9280Sstevel@tonic-gate /*
9290Sstevel@tonic-gate * Remove a service program from the callout list.
9300Sstevel@tonic-gate */
9310Sstevel@tonic-gate void
svc_unreg(const rpcprog_t prog,const rpcvers_t vers)932132Srobinson svc_unreg(const rpcprog_t prog, const rpcvers_t vers)
9330Sstevel@tonic-gate {
9340Sstevel@tonic-gate struct svc_callout *prev;
9350Sstevel@tonic-gate struct svc_callout *s;
9360Sstevel@tonic-gate
9370Sstevel@tonic-gate /* unregister the information anyway */
938*12648SSurya.Prakki@Sun.COM (void) rpcb_unset(prog, vers, NULL);
939*12648SSurya.Prakki@Sun.COM
940132Srobinson (void) rw_wrlock(&svc_lock);
9410Sstevel@tonic-gate while ((s = svc_find(prog, vers, &prev, NULL)) != NULL_SVC) {
9420Sstevel@tonic-gate if (prev == NULL_SVC) {
9430Sstevel@tonic-gate svc_head = s->sc_next;
9440Sstevel@tonic-gate } else {
9450Sstevel@tonic-gate prev->sc_next = s->sc_next;
9460Sstevel@tonic-gate }
9470Sstevel@tonic-gate s->sc_next = NULL_SVC;
9480Sstevel@tonic-gate if (s->sc_netid)
949132Srobinson free(s->sc_netid);
950132Srobinson free(s);
9510Sstevel@tonic-gate }
952132Srobinson (void) rw_unlock(&svc_lock);
9530Sstevel@tonic-gate }
9540Sstevel@tonic-gate
9550Sstevel@tonic-gate #ifdef PORTMAP
9560Sstevel@tonic-gate /*
9570Sstevel@tonic-gate * Add a service program to the callout list.
9580Sstevel@tonic-gate * The dispatch routine will be called when a rpc request for this
9590Sstevel@tonic-gate * program number comes in.
9600Sstevel@tonic-gate * For version 2 portmappers.
9610Sstevel@tonic-gate */
9620Sstevel@tonic-gate bool_t
svc_register(SVCXPRT * xprt,rpcprog_t prog,rpcvers_t vers,void (* dispatch)(),int protocol)963132Srobinson svc_register(SVCXPRT *xprt, rpcprog_t prog, rpcvers_t vers,
964132Srobinson void (*dispatch)(), int protocol)
9650Sstevel@tonic-gate {
9660Sstevel@tonic-gate struct svc_callout *prev;
9670Sstevel@tonic-gate struct svc_callout *s;
9680Sstevel@tonic-gate struct netconfig *nconf;
9690Sstevel@tonic-gate char *netid = NULL;
9700Sstevel@tonic-gate int flag = 0;
9710Sstevel@tonic-gate
9720Sstevel@tonic-gate if (xprt->xp_netid) {
9730Sstevel@tonic-gate netid = strdup(xprt->xp_netid);
9740Sstevel@tonic-gate flag = 1;
9750Sstevel@tonic-gate } else if ((ioctl(xprt->xp_fd, I_FIND, "timod") > 0) && ((nconf =
9760Sstevel@tonic-gate __rpcfd_to_nconf(xprt->xp_fd, xprt->xp_type)) != NULL)) {
9770Sstevel@tonic-gate /* fill in missing netid field in SVCXPRT */
9780Sstevel@tonic-gate netid = strdup(nconf->nc_netid);
9790Sstevel@tonic-gate flag = 1;
9800Sstevel@tonic-gate freenetconfigent(nconf);
9810Sstevel@tonic-gate } /* must be svc_raw_create */
9820Sstevel@tonic-gate
983132Srobinson if ((netid == NULL) && (flag == 1))
9840Sstevel@tonic-gate return (FALSE);
9850Sstevel@tonic-gate
986132Srobinson (void) rw_wrlock(&svc_lock);
9870Sstevel@tonic-gate if ((s = svc_find(prog, vers, &prev, netid)) != NULL_SVC) {
9880Sstevel@tonic-gate if (netid)
9890Sstevel@tonic-gate free(netid);
9900Sstevel@tonic-gate if (s->sc_dispatch == dispatch)
9910Sstevel@tonic-gate goto pmap_it; /* he is registering another xptr */
992132Srobinson (void) rw_unlock(&svc_lock);
9930Sstevel@tonic-gate return (FALSE);
9940Sstevel@tonic-gate }
995132Srobinson s = malloc(sizeof (struct svc_callout));
9960Sstevel@tonic-gate if (s == (struct svc_callout *)0) {
9970Sstevel@tonic-gate if (netid)
9980Sstevel@tonic-gate free(netid);
999132Srobinson (void) rw_unlock(&svc_lock);
10000Sstevel@tonic-gate return (FALSE);
10010Sstevel@tonic-gate }
10020Sstevel@tonic-gate s->sc_prog = prog;
10030Sstevel@tonic-gate s->sc_vers = vers;
10040Sstevel@tonic-gate s->sc_dispatch = dispatch;
10050Sstevel@tonic-gate s->sc_netid = netid;
10060Sstevel@tonic-gate s->sc_next = svc_head;
10070Sstevel@tonic-gate svc_head = s;
10080Sstevel@tonic-gate
10090Sstevel@tonic-gate if ((xprt->xp_netid == NULL) && (flag == 1) && netid)
10100Sstevel@tonic-gate if ((xprt->xp_netid = strdup(netid)) == NULL) {
10110Sstevel@tonic-gate syslog(LOG_ERR, "svc_register : strdup failed.");
10120Sstevel@tonic-gate free(netid);
10130Sstevel@tonic-gate svc_head = s->sc_next;
1014132Srobinson free(s);
1015132Srobinson (void) rw_unlock(&svc_lock);
10160Sstevel@tonic-gate return (FALSE);
10170Sstevel@tonic-gate }
10180Sstevel@tonic-gate
10190Sstevel@tonic-gate pmap_it:
1020132Srobinson (void) rw_unlock(&svc_lock);
10210Sstevel@tonic-gate /* now register the information with the local binder service */
1022132Srobinson if (protocol)
1023132Srobinson return (pmap_set(prog, vers, protocol, xprt->xp_port));
10240Sstevel@tonic-gate return (TRUE);
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate
10270Sstevel@tonic-gate /*
10280Sstevel@tonic-gate * Remove a service program from the callout list.
10290Sstevel@tonic-gate * For version 2 portmappers.
10300Sstevel@tonic-gate */
10310Sstevel@tonic-gate void
svc_unregister(rpcprog_t prog,rpcvers_t vers)1032132Srobinson svc_unregister(rpcprog_t prog, rpcvers_t vers)
10330Sstevel@tonic-gate {
10340Sstevel@tonic-gate struct svc_callout *prev;
10350Sstevel@tonic-gate struct svc_callout *s;
10360Sstevel@tonic-gate
1037132Srobinson (void) rw_wrlock(&svc_lock);
10380Sstevel@tonic-gate while ((s = svc_find(prog, vers, &prev, NULL)) != NULL_SVC) {
10390Sstevel@tonic-gate if (prev == NULL_SVC) {
10400Sstevel@tonic-gate svc_head = s->sc_next;
10410Sstevel@tonic-gate } else {
10420Sstevel@tonic-gate prev->sc_next = s->sc_next;
10430Sstevel@tonic-gate }
10440Sstevel@tonic-gate s->sc_next = NULL_SVC;
10450Sstevel@tonic-gate if (s->sc_netid)
1046132Srobinson free(s->sc_netid);
1047132Srobinson free(s);
10480Sstevel@tonic-gate /* unregister the information with the local binder service */
10490Sstevel@tonic-gate (void) pmap_unset(prog, vers);
10500Sstevel@tonic-gate }
1051132Srobinson (void) rw_unlock(&svc_lock);
10520Sstevel@tonic-gate }
1053132Srobinson #endif /* PORTMAP */
10540Sstevel@tonic-gate
10550Sstevel@tonic-gate /*
10560Sstevel@tonic-gate * Search the callout list for a program number, return the callout
10570Sstevel@tonic-gate * struct.
10580Sstevel@tonic-gate * Also check for transport as well. Many routines such as svc_unreg
10590Sstevel@tonic-gate * dont give any corresponding transport, so dont check for transport if
10600Sstevel@tonic-gate * netid == NULL
10610Sstevel@tonic-gate */
10620Sstevel@tonic-gate static struct svc_callout *
svc_find(rpcprog_t prog,rpcvers_t vers,struct svc_callout ** prev,char * netid)1063132Srobinson svc_find(rpcprog_t prog, rpcvers_t vers, struct svc_callout **prev, char *netid)
10640Sstevel@tonic-gate {
10650Sstevel@tonic-gate struct svc_callout *s, *p;
10660Sstevel@tonic-gate
10670Sstevel@tonic-gate /* WRITE LOCK HELD ON ENTRY: svc_lock */
10680Sstevel@tonic-gate
10690Sstevel@tonic-gate /* assert(RW_WRITE_HELD(&svc_lock)); */
10700Sstevel@tonic-gate p = NULL_SVC;
10710Sstevel@tonic-gate for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
10720Sstevel@tonic-gate if (((s->sc_prog == prog) && (s->sc_vers == vers)) &&
10730Sstevel@tonic-gate ((netid == NULL) || (s->sc_netid == NULL) ||
10740Sstevel@tonic-gate (strcmp(netid, s->sc_netid) == 0)))
10750Sstevel@tonic-gate break;
10760Sstevel@tonic-gate p = s;
10770Sstevel@tonic-gate }
10780Sstevel@tonic-gate *prev = p;
10790Sstevel@tonic-gate return (s);
10800Sstevel@tonic-gate }
10810Sstevel@tonic-gate
10820Sstevel@tonic-gate
10830Sstevel@tonic-gate /* ******************* REPLY GENERATION ROUTINES ************ */
10840Sstevel@tonic-gate
10850Sstevel@tonic-gate /*
10860Sstevel@tonic-gate * Send a reply to an rpc request
10870Sstevel@tonic-gate */
10880Sstevel@tonic-gate bool_t
svc_sendreply(const SVCXPRT * xprt,const xdrproc_t xdr_results,const caddr_t xdr_location)1089132Srobinson svc_sendreply(const SVCXPRT *xprt, const xdrproc_t xdr_results,
1090132Srobinson const caddr_t xdr_location)
10910Sstevel@tonic-gate {
10920Sstevel@tonic-gate struct rpc_msg rply;
10930Sstevel@tonic-gate
10940Sstevel@tonic-gate rply.rm_direction = REPLY;
10950Sstevel@tonic-gate rply.rm_reply.rp_stat = MSG_ACCEPTED;
10960Sstevel@tonic-gate rply.acpted_rply.ar_verf = xprt->xp_verf;
10970Sstevel@tonic-gate rply.acpted_rply.ar_stat = SUCCESS;
10980Sstevel@tonic-gate rply.acpted_rply.ar_results.where = xdr_location;
10990Sstevel@tonic-gate rply.acpted_rply.ar_results.proc = xdr_results;
1100132Srobinson return (SVC_REPLY((SVCXPRT *)xprt, &rply));
11010Sstevel@tonic-gate }
11020Sstevel@tonic-gate
11030Sstevel@tonic-gate /*
11040Sstevel@tonic-gate * No procedure error reply
11050Sstevel@tonic-gate */
11060Sstevel@tonic-gate void
svcerr_noproc(const SVCXPRT * xprt)1107132Srobinson svcerr_noproc(const SVCXPRT *xprt)
11080Sstevel@tonic-gate {
11090Sstevel@tonic-gate struct rpc_msg rply;
11100Sstevel@tonic-gate
11110Sstevel@tonic-gate rply.rm_direction = REPLY;
11120Sstevel@tonic-gate rply.rm_reply.rp_stat = MSG_ACCEPTED;
11130Sstevel@tonic-gate rply.acpted_rply.ar_verf = xprt->xp_verf;
11140Sstevel@tonic-gate rply.acpted_rply.ar_stat = PROC_UNAVAIL;
11150Sstevel@tonic-gate SVC_REPLY((SVCXPRT *)xprt, &rply);
11160Sstevel@tonic-gate }
11170Sstevel@tonic-gate
11180Sstevel@tonic-gate /*
11190Sstevel@tonic-gate * Can't decode args error reply
11200Sstevel@tonic-gate */
11210Sstevel@tonic-gate void
svcerr_decode(const SVCXPRT * xprt)1122132Srobinson svcerr_decode(const SVCXPRT *xprt)
11230Sstevel@tonic-gate {
11240Sstevel@tonic-gate struct rpc_msg rply;
11250Sstevel@tonic-gate
11260Sstevel@tonic-gate rply.rm_direction = REPLY;
11270Sstevel@tonic-gate rply.rm_reply.rp_stat = MSG_ACCEPTED;
11280Sstevel@tonic-gate rply.acpted_rply.ar_verf = xprt->xp_verf;
11290Sstevel@tonic-gate rply.acpted_rply.ar_stat = GARBAGE_ARGS;
11300Sstevel@tonic-gate SVC_REPLY((SVCXPRT *)xprt, &rply);
11310Sstevel@tonic-gate }
11320Sstevel@tonic-gate
11330Sstevel@tonic-gate /*
11340Sstevel@tonic-gate * Some system error
11350Sstevel@tonic-gate */
11360Sstevel@tonic-gate void
svcerr_systemerr(const SVCXPRT * xprt)1137132Srobinson svcerr_systemerr(const SVCXPRT *xprt)
11380Sstevel@tonic-gate {
11390Sstevel@tonic-gate struct rpc_msg rply;
11400Sstevel@tonic-gate
11410Sstevel@tonic-gate rply.rm_direction = REPLY;
11420Sstevel@tonic-gate rply.rm_reply.rp_stat = MSG_ACCEPTED;
11430Sstevel@tonic-gate rply.acpted_rply.ar_verf = xprt->xp_verf;
11440Sstevel@tonic-gate rply.acpted_rply.ar_stat = SYSTEM_ERR;
11450Sstevel@tonic-gate SVC_REPLY((SVCXPRT *)xprt, &rply);
11460Sstevel@tonic-gate }
11470Sstevel@tonic-gate
11480Sstevel@tonic-gate /*
11490Sstevel@tonic-gate * Tell RPC package to not complain about version errors to the client. This
11500Sstevel@tonic-gate * is useful when revving broadcast protocols that sit on a fixed address.
11510Sstevel@tonic-gate * There is really one (or should be only one) example of this kind of
11520Sstevel@tonic-gate * protocol: the portmapper (or rpc binder).
11530Sstevel@tonic-gate */
11540Sstevel@tonic-gate void
__svc_versquiet_on(const SVCXPRT * xprt)1155132Srobinson __svc_versquiet_on(const SVCXPRT *xprt)
11560Sstevel@tonic-gate {
11570Sstevel@tonic-gate /* LINTED pointer alignment */
11580Sstevel@tonic-gate svc_flags(xprt) |= SVC_VERSQUIET;
11590Sstevel@tonic-gate }
11600Sstevel@tonic-gate
11610Sstevel@tonic-gate void
__svc_versquiet_off(const SVCXPRT * xprt)1162132Srobinson __svc_versquiet_off(const SVCXPRT *xprt)
11630Sstevel@tonic-gate {
11640Sstevel@tonic-gate /* LINTED pointer alignment */
11650Sstevel@tonic-gate svc_flags(xprt) &= ~SVC_VERSQUIET;
11660Sstevel@tonic-gate }
11670Sstevel@tonic-gate
11680Sstevel@tonic-gate void
svc_versquiet(const SVCXPRT * xprt)1169132Srobinson svc_versquiet(const SVCXPRT *xprt)
11700Sstevel@tonic-gate {
11710Sstevel@tonic-gate __svc_versquiet_on(xprt);
11720Sstevel@tonic-gate }
11730Sstevel@tonic-gate
11740Sstevel@tonic-gate int
__svc_versquiet_get(const SVCXPRT * xprt)1175132Srobinson __svc_versquiet_get(const SVCXPRT *xprt)
11760Sstevel@tonic-gate {
11770Sstevel@tonic-gate /* LINTED pointer alignment */
11780Sstevel@tonic-gate return (svc_flags(xprt) & SVC_VERSQUIET);
11790Sstevel@tonic-gate }
11800Sstevel@tonic-gate
11810Sstevel@tonic-gate /*
11820Sstevel@tonic-gate * Authentication error reply
11830Sstevel@tonic-gate */
11840Sstevel@tonic-gate void
svcerr_auth(const SVCXPRT * xprt,const enum auth_stat why)1185132Srobinson svcerr_auth(const SVCXPRT *xprt, const enum auth_stat why)
11860Sstevel@tonic-gate {
11870Sstevel@tonic-gate struct rpc_msg rply;
11880Sstevel@tonic-gate
11890Sstevel@tonic-gate rply.rm_direction = REPLY;
11900Sstevel@tonic-gate rply.rm_reply.rp_stat = MSG_DENIED;
11910Sstevel@tonic-gate rply.rjcted_rply.rj_stat = AUTH_ERROR;
11920Sstevel@tonic-gate rply.rjcted_rply.rj_why = why;
11930Sstevel@tonic-gate SVC_REPLY((SVCXPRT *)xprt, &rply);
11940Sstevel@tonic-gate }
11950Sstevel@tonic-gate
11960Sstevel@tonic-gate /*
11970Sstevel@tonic-gate * Auth too weak error reply
11980Sstevel@tonic-gate */
11990Sstevel@tonic-gate void
svcerr_weakauth(const SVCXPRT * xprt)1200132Srobinson svcerr_weakauth(const SVCXPRT *xprt)
12010Sstevel@tonic-gate {
12020Sstevel@tonic-gate svcerr_auth(xprt, AUTH_TOOWEAK);
12030Sstevel@tonic-gate }
12040Sstevel@tonic-gate
12050Sstevel@tonic-gate /*
12060Sstevel@tonic-gate * Program unavailable error reply
12070Sstevel@tonic-gate */
12080Sstevel@tonic-gate void
svcerr_noprog(const SVCXPRT * xprt)1209132Srobinson svcerr_noprog(const SVCXPRT *xprt)
12100Sstevel@tonic-gate {
12110Sstevel@tonic-gate struct rpc_msg rply;
12120Sstevel@tonic-gate
12130Sstevel@tonic-gate rply.rm_direction = REPLY;
12140Sstevel@tonic-gate rply.rm_reply.rp_stat = MSG_ACCEPTED;
12150Sstevel@tonic-gate rply.acpted_rply.ar_verf = xprt->xp_verf;
12160Sstevel@tonic-gate rply.acpted_rply.ar_stat = PROG_UNAVAIL;
12170Sstevel@tonic-gate SVC_REPLY((SVCXPRT *)xprt, &rply);
12180Sstevel@tonic-gate }
12190Sstevel@tonic-gate
12200Sstevel@tonic-gate /*
12210Sstevel@tonic-gate * Program version mismatch error reply
12220Sstevel@tonic-gate */
12230Sstevel@tonic-gate void
svcerr_progvers(const SVCXPRT * xprt,const rpcvers_t low_vers,const rpcvers_t high_vers)1224132Srobinson svcerr_progvers(const SVCXPRT *xprt, const rpcvers_t low_vers,
1225132Srobinson const rpcvers_t high_vers)
12260Sstevel@tonic-gate {
12270Sstevel@tonic-gate struct rpc_msg rply;
12280Sstevel@tonic-gate
12290Sstevel@tonic-gate rply.rm_direction = REPLY;
12300Sstevel@tonic-gate rply.rm_reply.rp_stat = MSG_ACCEPTED;
12310Sstevel@tonic-gate rply.acpted_rply.ar_verf = xprt->xp_verf;
12320Sstevel@tonic-gate rply.acpted_rply.ar_stat = PROG_MISMATCH;
12330Sstevel@tonic-gate rply.acpted_rply.ar_vers.low = low_vers;
12340Sstevel@tonic-gate rply.acpted_rply.ar_vers.high = high_vers;
12350Sstevel@tonic-gate SVC_REPLY((SVCXPRT *)xprt, &rply);
12360Sstevel@tonic-gate }
12370Sstevel@tonic-gate
12380Sstevel@tonic-gate /* ******************* SERVER INPUT STUFF ******************* */
12390Sstevel@tonic-gate
12400Sstevel@tonic-gate /*
12410Sstevel@tonic-gate * Get server side input from some transport.
12420Sstevel@tonic-gate *
12430Sstevel@tonic-gate * Statement of authentication parameters management:
12440Sstevel@tonic-gate * This function owns and manages all authentication parameters, specifically
12450Sstevel@tonic-gate * the "raw" parameters (msg.rm_call.cb_cred and msg.rm_call.cb_verf) and
12460Sstevel@tonic-gate * the "cooked" credentials (rqst->rq_clntcred).
12470Sstevel@tonic-gate * However, this function does not know the structure of the cooked
12480Sstevel@tonic-gate * credentials, so it make the following assumptions:
12490Sstevel@tonic-gate * a) the structure is contiguous (no pointers), and
12500Sstevel@tonic-gate * b) the cred structure size does not exceed RQCRED_SIZE bytes.
12510Sstevel@tonic-gate * In all events, all three parameters are freed upon exit from this routine.
12520Sstevel@tonic-gate * The storage is trivially management on the call stack in user land, but
12530Sstevel@tonic-gate * is mallocated in kernel land.
12540Sstevel@tonic-gate */
12550Sstevel@tonic-gate
12560Sstevel@tonic-gate void
svc_getreq(int rdfds)1257132Srobinson svc_getreq(int rdfds)
12580Sstevel@tonic-gate {
12590Sstevel@tonic-gate fd_set readfds;
12600Sstevel@tonic-gate
12610Sstevel@tonic-gate FD_ZERO(&readfds);
12620Sstevel@tonic-gate readfds.fds_bits[0] = rdfds;
12630Sstevel@tonic-gate svc_getreqset(&readfds);
12640Sstevel@tonic-gate }
12650Sstevel@tonic-gate
12660Sstevel@tonic-gate void
svc_getreqset(fd_set * readfds)1267132Srobinson svc_getreqset(fd_set *readfds)
12680Sstevel@tonic-gate {
12690Sstevel@tonic-gate int i;
12700Sstevel@tonic-gate
12710Sstevel@tonic-gate for (i = 0; i < svc_max_fd; i++) {
12720Sstevel@tonic-gate /* fd has input waiting */
12730Sstevel@tonic-gate if (FD_ISSET(i, readfds))
12740Sstevel@tonic-gate svc_getreq_common(i);
12750Sstevel@tonic-gate }
12760Sstevel@tonic-gate }
12770Sstevel@tonic-gate
12780Sstevel@tonic-gate void
svc_getreq_poll(struct pollfd * pfdp,const int pollretval)1279132Srobinson svc_getreq_poll(struct pollfd *pfdp, const int pollretval)
12800Sstevel@tonic-gate {
12810Sstevel@tonic-gate int i;
12820Sstevel@tonic-gate int fds_found;
12830Sstevel@tonic-gate
12840Sstevel@tonic-gate for (i = fds_found = 0; fds_found < pollretval; i++) {
12850Sstevel@tonic-gate struct pollfd *p = &pfdp[i];
12860Sstevel@tonic-gate
12870Sstevel@tonic-gate if (p->revents) {
12880Sstevel@tonic-gate /* fd has input waiting */
12890Sstevel@tonic-gate fds_found++;
12900Sstevel@tonic-gate /*
12910Sstevel@tonic-gate * We assume that this function is only called
12920Sstevel@tonic-gate * via someone select()ing from svc_fdset or
12930Sstevel@tonic-gate * poll()ing from svc_pollset[]. Thus it's safe
12940Sstevel@tonic-gate * to handle the POLLNVAL event by simply turning
12950Sstevel@tonic-gate * the corresponding bit off in svc_fdset. The
12960Sstevel@tonic-gate * svc_pollset[] array is derived from svc_fdset
12970Sstevel@tonic-gate * and so will also be updated eventually.
12980Sstevel@tonic-gate *
12990Sstevel@tonic-gate * XXX Should we do an xprt_unregister() instead?
13000Sstevel@tonic-gate */
13010Sstevel@tonic-gate /* Handle user callback */
13020Sstevel@tonic-gate if (__is_a_userfd(p->fd) == TRUE) {
1303132Srobinson (void) rw_rdlock(&svc_fd_lock);
13040Sstevel@tonic-gate __svc_getreq_user(p);
1305132Srobinson (void) rw_unlock(&svc_fd_lock);
13060Sstevel@tonic-gate } else {
13070Sstevel@tonic-gate if (p->revents & POLLNVAL) {
1308132Srobinson (void) rw_wrlock(&svc_fd_lock);
13090Sstevel@tonic-gate remove_pollfd(p->fd); /* XXX */
1310132Srobinson (void) rw_unlock(&svc_fd_lock);
1311132Srobinson } else {
13120Sstevel@tonic-gate svc_getreq_common(p->fd);
1313132Srobinson }
13140Sstevel@tonic-gate }
13150Sstevel@tonic-gate }
13160Sstevel@tonic-gate }
13170Sstevel@tonic-gate }
13180Sstevel@tonic-gate
13190Sstevel@tonic-gate void
svc_getreq_common(const int fd)1320132Srobinson svc_getreq_common(const int fd)
13210Sstevel@tonic-gate {
13220Sstevel@tonic-gate SVCXPRT *xprt;
13230Sstevel@tonic-gate enum xprt_stat stat;
13240Sstevel@tonic-gate struct rpc_msg *msg;
13250Sstevel@tonic-gate struct svc_req *r;
13260Sstevel@tonic-gate char *cred_area;
13270Sstevel@tonic-gate
1328132Srobinson (void) rw_rdlock(&svc_fd_lock);
13290Sstevel@tonic-gate
13300Sstevel@tonic-gate /* HANDLE USER CALLBACK */
13310Sstevel@tonic-gate if (__is_a_userfd(fd) == TRUE) {
13320Sstevel@tonic-gate struct pollfd virtual_fd;
13330Sstevel@tonic-gate
13340Sstevel@tonic-gate virtual_fd.events = virtual_fd.revents = (short)0xFFFF;
13350Sstevel@tonic-gate virtual_fd.fd = fd;
13360Sstevel@tonic-gate __svc_getreq_user(&virtual_fd);
1337132Srobinson (void) rw_unlock(&svc_fd_lock);
13380Sstevel@tonic-gate return;
13390Sstevel@tonic-gate }
13400Sstevel@tonic-gate
13410Sstevel@tonic-gate /*
13420Sstevel@tonic-gate * The transport associated with this fd could have been
13430Sstevel@tonic-gate * removed from svc_timeout_nonblock_xprt_and_LRU, for instance.
13440Sstevel@tonic-gate * This can happen if two or more fds get read events and are
13450Sstevel@tonic-gate * passed to svc_getreq_poll/set, the first fd is seviced by
13460Sstevel@tonic-gate * the dispatch routine and cleans up any dead transports. If
13470Sstevel@tonic-gate * one of the dead transports removed is the other fd that
13480Sstevel@tonic-gate * had a read event then svc_getreq_common() will be called with no
13490Sstevel@tonic-gate * xprt associated with the fd that had the original read event.
13500Sstevel@tonic-gate */
13510Sstevel@tonic-gate if ((fd >= nsvc_xports) || (xprt = svc_xports[fd]) == NULL) {
1352132Srobinson (void) rw_unlock(&svc_fd_lock);
13530Sstevel@tonic-gate return;
13540Sstevel@tonic-gate }
1355132Srobinson (void) rw_unlock(&svc_fd_lock);
13560Sstevel@tonic-gate /* LINTED pointer alignment */
13570Sstevel@tonic-gate msg = SVCEXT(xprt)->msg;
13580Sstevel@tonic-gate /* LINTED pointer alignment */
13590Sstevel@tonic-gate r = SVCEXT(xprt)->req;
13600Sstevel@tonic-gate /* LINTED pointer alignment */
13610Sstevel@tonic-gate cred_area = SVCEXT(xprt)->cred_area;
13620Sstevel@tonic-gate msg->rm_call.cb_cred.oa_base = cred_area;
13630Sstevel@tonic-gate msg->rm_call.cb_verf.oa_base = &(cred_area[MAX_AUTH_BYTES]);
13640Sstevel@tonic-gate r->rq_clntcred = &(cred_area[2 * MAX_AUTH_BYTES]);
13650Sstevel@tonic-gate
13660Sstevel@tonic-gate /* receive msgs from xprtprt (support batch calls) */
13670Sstevel@tonic-gate do {
13680Sstevel@tonic-gate bool_t dispatch;
13690Sstevel@tonic-gate
13700Sstevel@tonic-gate if (dispatch = SVC_RECV(xprt, msg))
13710Sstevel@tonic-gate (void) _svc_prog_dispatch(xprt, msg, r);
13720Sstevel@tonic-gate /*
13730Sstevel@tonic-gate * Check if the xprt has been disconnected in a recursive call
13740Sstevel@tonic-gate * in the service dispatch routine. If so, then break
13750Sstevel@tonic-gate */
1376132Srobinson (void) rw_rdlock(&svc_fd_lock);
13770Sstevel@tonic-gate if (xprt != svc_xports[fd]) {
1378132Srobinson (void) rw_unlock(&svc_fd_lock);
13790Sstevel@tonic-gate break;
13800Sstevel@tonic-gate }
1381132Srobinson (void) rw_unlock(&svc_fd_lock);
13820Sstevel@tonic-gate
13830Sstevel@tonic-gate /*
13840Sstevel@tonic-gate * Call cleanup procedure if set.
13850Sstevel@tonic-gate */
13860Sstevel@tonic-gate if (__proc_cleanup_cb != NULL && dispatch)
13870Sstevel@tonic-gate (*__proc_cleanup_cb)(xprt);
13880Sstevel@tonic-gate
13890Sstevel@tonic-gate if ((stat = SVC_STAT(xprt)) == XPRT_DIED) {
13900Sstevel@tonic-gate SVC_DESTROY(xprt);
13910Sstevel@tonic-gate break;
13920Sstevel@tonic-gate }
13930Sstevel@tonic-gate } while (stat == XPRT_MOREREQS);
13940Sstevel@tonic-gate }
13950Sstevel@tonic-gate
13960Sstevel@tonic-gate int
_svc_prog_dispatch(SVCXPRT * xprt,struct rpc_msg * msg,struct svc_req * r)1397132Srobinson _svc_prog_dispatch(SVCXPRT *xprt, struct rpc_msg *msg, struct svc_req *r)
13980Sstevel@tonic-gate {
13990Sstevel@tonic-gate struct svc_callout *s;
14000Sstevel@tonic-gate enum auth_stat why;
14010Sstevel@tonic-gate int prog_found;
14020Sstevel@tonic-gate rpcvers_t low_vers;
14030Sstevel@tonic-gate rpcvers_t high_vers;
14040Sstevel@tonic-gate void (*disp_fn)();
14050Sstevel@tonic-gate
14060Sstevel@tonic-gate r->rq_xprt = xprt;
14070Sstevel@tonic-gate r->rq_prog = msg->rm_call.cb_prog;
14080Sstevel@tonic-gate r->rq_vers = msg->rm_call.cb_vers;
14090Sstevel@tonic-gate r->rq_proc = msg->rm_call.cb_proc;
14100Sstevel@tonic-gate r->rq_cred = msg->rm_call.cb_cred;
14110Sstevel@tonic-gate /* LINTED pointer alignment */
14120Sstevel@tonic-gate SVC_XP_AUTH(r->rq_xprt).svc_ah_ops = svc_auth_any_ops;
14130Sstevel@tonic-gate /* LINTED pointer alignment */
14140Sstevel@tonic-gate SVC_XP_AUTH(r->rq_xprt).svc_ah_private = NULL;
14150Sstevel@tonic-gate
14160Sstevel@tonic-gate /* first authenticate the message */
14170Sstevel@tonic-gate /* Check for null flavor and bypass these calls if possible */
14180Sstevel@tonic-gate
14190Sstevel@tonic-gate if (msg->rm_call.cb_cred.oa_flavor == AUTH_NULL) {
14200Sstevel@tonic-gate r->rq_xprt->xp_verf.oa_flavor = _null_auth.oa_flavor;
14210Sstevel@tonic-gate r->rq_xprt->xp_verf.oa_length = 0;
14220Sstevel@tonic-gate } else {
14230Sstevel@tonic-gate bool_t no_dispatch;
14240Sstevel@tonic-gate
14250Sstevel@tonic-gate if ((why = __gss_authenticate(r, msg,
14260Sstevel@tonic-gate &no_dispatch)) != AUTH_OK) {
14270Sstevel@tonic-gate svcerr_auth(xprt, why);
14280Sstevel@tonic-gate return (0);
14290Sstevel@tonic-gate }
14300Sstevel@tonic-gate if (no_dispatch)
14310Sstevel@tonic-gate return (0);
14320Sstevel@tonic-gate }
14330Sstevel@tonic-gate /* match message with a registered service */
14340Sstevel@tonic-gate prog_found = FALSE;
14350Sstevel@tonic-gate low_vers = (rpcvers_t)(0 - 1);
14360Sstevel@tonic-gate high_vers = 0;
1437132Srobinson (void) rw_rdlock(&svc_lock);
14380Sstevel@tonic-gate for (s = svc_head; s != NULL_SVC; s = s->sc_next) {
14390Sstevel@tonic-gate if (s->sc_prog == r->rq_prog) {
14400Sstevel@tonic-gate prog_found = TRUE;
14410Sstevel@tonic-gate if (s->sc_vers == r->rq_vers) {
14420Sstevel@tonic-gate if ((xprt->xp_netid == NULL) ||
14430Sstevel@tonic-gate (s->sc_netid == NULL) ||
14440Sstevel@tonic-gate (strcmp(xprt->xp_netid,
14450Sstevel@tonic-gate s->sc_netid) == 0)) {
14460Sstevel@tonic-gate disp_fn = (*s->sc_dispatch);
1447132Srobinson (void) rw_unlock(&svc_lock);
14480Sstevel@tonic-gate disp_fn(r, xprt);
14490Sstevel@tonic-gate return (1);
14500Sstevel@tonic-gate }
1451132Srobinson prog_found = FALSE;
14520Sstevel@tonic-gate }
14530Sstevel@tonic-gate if (s->sc_vers < low_vers)
14540Sstevel@tonic-gate low_vers = s->sc_vers;
14550Sstevel@tonic-gate if (s->sc_vers > high_vers)
14560Sstevel@tonic-gate high_vers = s->sc_vers;
14570Sstevel@tonic-gate } /* found correct program */
14580Sstevel@tonic-gate }
1459132Srobinson (void) rw_unlock(&svc_lock);
14600Sstevel@tonic-gate
14610Sstevel@tonic-gate /*
14620Sstevel@tonic-gate * if we got here, the program or version
14630Sstevel@tonic-gate * is not served ...
14640Sstevel@tonic-gate */
14650Sstevel@tonic-gate if (prog_found) {
14660Sstevel@tonic-gate /* LINTED pointer alignment */
14670Sstevel@tonic-gate if (!version_keepquiet(xprt))
14680Sstevel@tonic-gate svcerr_progvers(xprt, low_vers, high_vers);
14690Sstevel@tonic-gate } else {
14700Sstevel@tonic-gate svcerr_noprog(xprt);
14710Sstevel@tonic-gate }
14720Sstevel@tonic-gate return (0);
14730Sstevel@tonic-gate }
14740Sstevel@tonic-gate
14750Sstevel@tonic-gate /* ******************* SVCXPRT allocation and deallocation ***************** */
14760Sstevel@tonic-gate
14770Sstevel@tonic-gate /*
14780Sstevel@tonic-gate * svc_xprt_alloc() - allocate a service transport handle
14790Sstevel@tonic-gate */
14800Sstevel@tonic-gate SVCXPRT *
svc_xprt_alloc(void)1481132Srobinson svc_xprt_alloc(void)
14820Sstevel@tonic-gate {
14830Sstevel@tonic-gate SVCXPRT *xprt = NULL;
14840Sstevel@tonic-gate SVCXPRT_EXT *xt = NULL;
14850Sstevel@tonic-gate SVCXPRT_LIST *xlist = NULL;
14860Sstevel@tonic-gate struct rpc_msg *msg = NULL;
14870Sstevel@tonic-gate struct svc_req *req = NULL;
14880Sstevel@tonic-gate char *cred_area = NULL;
14890Sstevel@tonic-gate
1490132Srobinson if ((xprt = calloc(1, sizeof (SVCXPRT))) == NULL)
14910Sstevel@tonic-gate goto err_exit;
14920Sstevel@tonic-gate
1493132Srobinson if ((xt = calloc(1, sizeof (SVCXPRT_EXT))) == NULL)
14940Sstevel@tonic-gate goto err_exit;
14950Sstevel@tonic-gate xprt->xp_p3 = (caddr_t)xt; /* SVCEXT(xprt) = xt */
14960Sstevel@tonic-gate
1497132Srobinson if ((xlist = calloc(1, sizeof (SVCXPRT_LIST))) == NULL)
14980Sstevel@tonic-gate goto err_exit;
14990Sstevel@tonic-gate xt->my_xlist = xlist;
15000Sstevel@tonic-gate xlist->xprt = xprt;
15010Sstevel@tonic-gate
1502132Srobinson if ((msg = malloc(sizeof (struct rpc_msg))) == NULL)
15030Sstevel@tonic-gate goto err_exit;
15040Sstevel@tonic-gate xt->msg = msg;
15050Sstevel@tonic-gate
1506132Srobinson if ((req = malloc(sizeof (struct svc_req))) == NULL)
15070Sstevel@tonic-gate goto err_exit;
15080Sstevel@tonic-gate xt->req = req;
15090Sstevel@tonic-gate
1510132Srobinson if ((cred_area = malloc(2*MAX_AUTH_BYTES + RQCRED_SIZE)) == NULL)
15110Sstevel@tonic-gate goto err_exit;
15120Sstevel@tonic-gate xt->cred_area = cred_area;
15130Sstevel@tonic-gate
15140Sstevel@tonic-gate /* LINTED pointer alignment */
1515132Srobinson (void) mutex_init(&svc_send_mutex(xprt), USYNC_THREAD, (void *)0);
15160Sstevel@tonic-gate return (xprt);
15170Sstevel@tonic-gate
15180Sstevel@tonic-gate err_exit:
15190Sstevel@tonic-gate svc_xprt_free(xprt);
15200Sstevel@tonic-gate return (NULL);
15210Sstevel@tonic-gate }
15220Sstevel@tonic-gate
15230Sstevel@tonic-gate
15240Sstevel@tonic-gate /*
15250Sstevel@tonic-gate * svc_xprt_free() - free a service handle
15260Sstevel@tonic-gate */
15270Sstevel@tonic-gate void
svc_xprt_free(SVCXPRT * xprt)1528132Srobinson svc_xprt_free(SVCXPRT *xprt)
15290Sstevel@tonic-gate {
15300Sstevel@tonic-gate /* LINTED pointer alignment */
15310Sstevel@tonic-gate SVCXPRT_EXT *xt = xprt ? SVCEXT(xprt) : NULL;
15320Sstevel@tonic-gate SVCXPRT_LIST *my_xlist = xt ? xt->my_xlist: NULL;
15330Sstevel@tonic-gate struct rpc_msg *msg = xt ? xt->msg : NULL;
15340Sstevel@tonic-gate struct svc_req *req = xt ? xt->req : NULL;
15350Sstevel@tonic-gate char *cred_area = xt ? xt->cred_area : NULL;
15360Sstevel@tonic-gate
15370Sstevel@tonic-gate if (xprt)
1538132Srobinson free(xprt);
15390Sstevel@tonic-gate if (xt)
1540132Srobinson free(xt);
15410Sstevel@tonic-gate if (my_xlist)
1542132Srobinson free(my_xlist);
15430Sstevel@tonic-gate if (msg)
1544132Srobinson free(msg);
15450Sstevel@tonic-gate if (req)
1546132Srobinson free(req);
15470Sstevel@tonic-gate if (cred_area)
1548132Srobinson free(cred_area);
15490Sstevel@tonic-gate }
15500Sstevel@tonic-gate
15510Sstevel@tonic-gate
15520Sstevel@tonic-gate /*
15530Sstevel@tonic-gate * svc_xprt_destroy() - free parent and child xprt list
15540Sstevel@tonic-gate */
15550Sstevel@tonic-gate void
svc_xprt_destroy(SVCXPRT * xprt)1556132Srobinson svc_xprt_destroy(SVCXPRT *xprt)
15570Sstevel@tonic-gate {
15580Sstevel@tonic-gate SVCXPRT_LIST *xlist, *xnext = NULL;
15590Sstevel@tonic-gate int type;
15600Sstevel@tonic-gate
15610Sstevel@tonic-gate /* LINTED pointer alignment */
15620Sstevel@tonic-gate if (SVCEXT(xprt)->parent)
15630Sstevel@tonic-gate /* LINTED pointer alignment */
15640Sstevel@tonic-gate xprt = SVCEXT(xprt)->parent;
15650Sstevel@tonic-gate /* LINTED pointer alignment */
15660Sstevel@tonic-gate type = svc_type(xprt);
15670Sstevel@tonic-gate /* LINTED pointer alignment */
15680Sstevel@tonic-gate for (xlist = SVCEXT(xprt)->my_xlist; xlist != NULL; xlist = xnext) {
15690Sstevel@tonic-gate xnext = xlist->next;
15700Sstevel@tonic-gate xprt = xlist->xprt;
15710Sstevel@tonic-gate switch (type) {
15720Sstevel@tonic-gate case SVC_DGRAM:
15730Sstevel@tonic-gate svc_dg_xprtfree(xprt);
15740Sstevel@tonic-gate break;
15750Sstevel@tonic-gate case SVC_RENDEZVOUS:
15760Sstevel@tonic-gate svc_vc_xprtfree(xprt);
15770Sstevel@tonic-gate break;
15780Sstevel@tonic-gate case SVC_CONNECTION:
15790Sstevel@tonic-gate svc_fd_xprtfree(xprt);
15800Sstevel@tonic-gate break;
15810Sstevel@tonic-gate case SVC_DOOR:
15820Sstevel@tonic-gate svc_door_xprtfree(xprt);
15830Sstevel@tonic-gate break;
15840Sstevel@tonic-gate }
15850Sstevel@tonic-gate }
15860Sstevel@tonic-gate }
15870Sstevel@tonic-gate
15880Sstevel@tonic-gate
15890Sstevel@tonic-gate /*
15900Sstevel@tonic-gate * svc_copy() - make a copy of parent
15910Sstevel@tonic-gate */
15920Sstevel@tonic-gate SVCXPRT *
svc_copy(SVCXPRT * xprt)1593132Srobinson svc_copy(SVCXPRT *xprt)
15940Sstevel@tonic-gate {
15950Sstevel@tonic-gate /* LINTED pointer alignment */
15960Sstevel@tonic-gate switch (svc_type(xprt)) {
15970Sstevel@tonic-gate case SVC_DGRAM:
15980Sstevel@tonic-gate return (svc_dg_xprtcopy(xprt));
15990Sstevel@tonic-gate case SVC_RENDEZVOUS:
16000Sstevel@tonic-gate return (svc_vc_xprtcopy(xprt));
16010Sstevel@tonic-gate case SVC_CONNECTION:
16020Sstevel@tonic-gate return (svc_fd_xprtcopy(xprt));
16030Sstevel@tonic-gate }
1604132Srobinson return (NULL);
16050Sstevel@tonic-gate }
16060Sstevel@tonic-gate
16070Sstevel@tonic-gate
16080Sstevel@tonic-gate /*
16090Sstevel@tonic-gate * _svc_destroy_private() - private SVC_DESTROY interface
16100Sstevel@tonic-gate */
16110Sstevel@tonic-gate void
_svc_destroy_private(SVCXPRT * xprt)1612132Srobinson _svc_destroy_private(SVCXPRT *xprt)
16130Sstevel@tonic-gate {
16140Sstevel@tonic-gate /* LINTED pointer alignment */
16150Sstevel@tonic-gate switch (svc_type(xprt)) {
16160Sstevel@tonic-gate case SVC_DGRAM:
16170Sstevel@tonic-gate _svc_dg_destroy_private(xprt);
16180Sstevel@tonic-gate break;
16190Sstevel@tonic-gate case SVC_RENDEZVOUS:
16200Sstevel@tonic-gate case SVC_CONNECTION:
16210Sstevel@tonic-gate _svc_vc_destroy_private(xprt, TRUE);
16220Sstevel@tonic-gate break;
16230Sstevel@tonic-gate }
16240Sstevel@tonic-gate }
16250Sstevel@tonic-gate
16260Sstevel@tonic-gate /*
16270Sstevel@tonic-gate * svc_get_local_cred() - fetch local user credentials. This always
16280Sstevel@tonic-gate * works over doors based transports. For local transports, this
16290Sstevel@tonic-gate * does not yield correct results unless the __rpc_negotiate_uid()
16300Sstevel@tonic-gate * call has been invoked to enable this feature.
16310Sstevel@tonic-gate */
16320Sstevel@tonic-gate bool_t
svc_get_local_cred(SVCXPRT * xprt,svc_local_cred_t * lcred)1633132Srobinson svc_get_local_cred(SVCXPRT *xprt, svc_local_cred_t *lcred)
16340Sstevel@tonic-gate {
16350Sstevel@tonic-gate /* LINTED pointer alignment */
16360Sstevel@tonic-gate if (svc_type(xprt) == SVC_DOOR)
16370Sstevel@tonic-gate return (__svc_get_door_cred(xprt, lcred));
16380Sstevel@tonic-gate return (__rpc_get_local_cred(xprt, lcred));
16390Sstevel@tonic-gate }
16400Sstevel@tonic-gate
16410Sstevel@tonic-gate
16420Sstevel@tonic-gate /* ******************* DUPLICATE ENTRY HANDLING ROUTINES ************** */
16430Sstevel@tonic-gate
16440Sstevel@tonic-gate /*
16450Sstevel@tonic-gate * the dup cacheing routines below provide a cache of received
16460Sstevel@tonic-gate * transactions. rpc service routines can use this to detect
16470Sstevel@tonic-gate * retransmissions and re-send a non-failure response. Uses a
16480Sstevel@tonic-gate * lru scheme to find entries to get rid of entries in the cache,
16490Sstevel@tonic-gate * though only DUP_DONE entries are placed on the lru list.
16500Sstevel@tonic-gate * the routines were written towards development of a generic
16510Sstevel@tonic-gate * SVC_DUP() interface, which can be expanded to encompass the
16520Sstevel@tonic-gate * svc_dg_enablecache() routines as well. the cache is currently
16530Sstevel@tonic-gate * private to the automounter.
16540Sstevel@tonic-gate */
16550Sstevel@tonic-gate
16560Sstevel@tonic-gate
16570Sstevel@tonic-gate /* dupcache header contains xprt specific information */
1658132Srobinson struct dupcache {
16590Sstevel@tonic-gate rwlock_t dc_lock;
16600Sstevel@tonic-gate time_t dc_time;
16610Sstevel@tonic-gate int dc_buckets;
16620Sstevel@tonic-gate int dc_maxsz;
16630Sstevel@tonic-gate int dc_basis;
16640Sstevel@tonic-gate struct dupreq *dc_mru;
16650Sstevel@tonic-gate struct dupreq **dc_hashtbl;
16660Sstevel@tonic-gate };
16670Sstevel@tonic-gate
16680Sstevel@tonic-gate /*
16690Sstevel@tonic-gate * private duplicate cache request routines
16700Sstevel@tonic-gate */
16710Sstevel@tonic-gate static int __svc_dupcache_check(struct svc_req *, caddr_t *, uint_t *,
16720Sstevel@tonic-gate struct dupcache *, uint32_t, uint32_t);
16730Sstevel@tonic-gate static struct dupreq *__svc_dupcache_victim(struct dupcache *, time_t);
16740Sstevel@tonic-gate static int __svc_dupcache_enter(struct svc_req *, struct dupreq *,
16750Sstevel@tonic-gate struct dupcache *, uint32_t, uint32_t, time_t);
16760Sstevel@tonic-gate static int __svc_dupcache_update(struct svc_req *, caddr_t, uint_t, int,
16770Sstevel@tonic-gate struct dupcache *, uint32_t, uint32_t);
16780Sstevel@tonic-gate #ifdef DUP_DEBUG
16790Sstevel@tonic-gate static void __svc_dupcache_debug(struct dupcache *);
16800Sstevel@tonic-gate #endif /* DUP_DEBUG */
16810Sstevel@tonic-gate
16820Sstevel@tonic-gate /* default parameters for the dupcache */
16830Sstevel@tonic-gate #define DUPCACHE_BUCKETS 257
16840Sstevel@tonic-gate #define DUPCACHE_TIME 900
16850Sstevel@tonic-gate #define DUPCACHE_MAXSZ INT_MAX
16860Sstevel@tonic-gate
16870Sstevel@tonic-gate /*
16880Sstevel@tonic-gate * __svc_dupcache_init(void *condition, int basis, char *xprt_cache)
16890Sstevel@tonic-gate * initialize the duprequest cache and assign it to the xprt_cache
16900Sstevel@tonic-gate * Use default values depending on the cache condition and basis.
16910Sstevel@tonic-gate * return TRUE on success and FALSE on failure
16920Sstevel@tonic-gate */
16930Sstevel@tonic-gate bool_t
__svc_dupcache_init(void * condition,int basis,char ** xprt_cache)16940Sstevel@tonic-gate __svc_dupcache_init(void *condition, int basis, char **xprt_cache)
16950Sstevel@tonic-gate {
16960Sstevel@tonic-gate static mutex_t initdc_lock = DEFAULTMUTEX;
16970Sstevel@tonic-gate int i;
16980Sstevel@tonic-gate struct dupcache *dc;
16990Sstevel@tonic-gate
1700132Srobinson (void) mutex_lock(&initdc_lock);
17010Sstevel@tonic-gate if (*xprt_cache != NULL) { /* do only once per xprt */
1702132Srobinson (void) mutex_unlock(&initdc_lock);
17030Sstevel@tonic-gate syslog(LOG_ERR,
1704132Srobinson "__svc_dupcache_init: multiply defined dup cache");
17050Sstevel@tonic-gate return (FALSE);
17060Sstevel@tonic-gate }
17070Sstevel@tonic-gate
17080Sstevel@tonic-gate switch (basis) {
17090Sstevel@tonic-gate case DUPCACHE_FIXEDTIME:
1710132Srobinson dc = malloc(sizeof (struct dupcache));
17110Sstevel@tonic-gate if (dc == NULL) {
1712132Srobinson (void) mutex_unlock(&initdc_lock);
17130Sstevel@tonic-gate syslog(LOG_ERR,
17140Sstevel@tonic-gate "__svc_dupcache_init: memory alloc failed");
17150Sstevel@tonic-gate return (FALSE);
17160Sstevel@tonic-gate }
1717132Srobinson (void) rwlock_init(&(dc->dc_lock), USYNC_THREAD, NULL);
17180Sstevel@tonic-gate if (condition != NULL)
17190Sstevel@tonic-gate dc->dc_time = *((time_t *)condition);
17200Sstevel@tonic-gate else
17210Sstevel@tonic-gate dc->dc_time = DUPCACHE_TIME;
17220Sstevel@tonic-gate dc->dc_buckets = DUPCACHE_BUCKETS;
17230Sstevel@tonic-gate dc->dc_maxsz = DUPCACHE_MAXSZ;
17240Sstevel@tonic-gate dc->dc_basis = basis;
17250Sstevel@tonic-gate dc->dc_mru = NULL;
1726132Srobinson dc->dc_hashtbl = malloc(dc->dc_buckets *
17270Sstevel@tonic-gate sizeof (struct dupreq *));
17280Sstevel@tonic-gate if (dc->dc_hashtbl == NULL) {
1729132Srobinson free(dc);
1730132Srobinson (void) mutex_unlock(&initdc_lock);
17310Sstevel@tonic-gate syslog(LOG_ERR,
17320Sstevel@tonic-gate "__svc_dupcache_init: memory alloc failed");
17330Sstevel@tonic-gate return (FALSE);
17340Sstevel@tonic-gate }
17350Sstevel@tonic-gate for (i = 0; i < DUPCACHE_BUCKETS; i++)
17360Sstevel@tonic-gate dc->dc_hashtbl[i] = NULL;
17370Sstevel@tonic-gate *xprt_cache = (char *)dc;
17380Sstevel@tonic-gate break;
17390Sstevel@tonic-gate default:
1740132Srobinson (void) mutex_unlock(&initdc_lock);
17410Sstevel@tonic-gate syslog(LOG_ERR,
1742132Srobinson "__svc_dupcache_init: undefined dup cache basis");
17430Sstevel@tonic-gate return (FALSE);
17440Sstevel@tonic-gate }
17450Sstevel@tonic-gate
1746132Srobinson (void) mutex_unlock(&initdc_lock);
17470Sstevel@tonic-gate
17480Sstevel@tonic-gate return (TRUE);
17490Sstevel@tonic-gate }
17500Sstevel@tonic-gate
17510Sstevel@tonic-gate /*
17520Sstevel@tonic-gate * __svc_dup(struct svc_req *req, caddr_t *resp_buf, uint_t *resp_bufsz,
17530Sstevel@tonic-gate * char *xprt_cache)
17540Sstevel@tonic-gate * searches the request cache. Creates an entry and returns DUP_NEW if
17550Sstevel@tonic-gate * the request is not found in the cache. If it is found, then it
17560Sstevel@tonic-gate * returns the state of the request (in progress, drop, or done) and
17570Sstevel@tonic-gate * also allocates, and passes back results to the user (if any) in
17580Sstevel@tonic-gate * resp_buf, and its length in resp_bufsz. DUP_ERROR is returned on error.
17590Sstevel@tonic-gate */
17600Sstevel@tonic-gate int
__svc_dup(struct svc_req * req,caddr_t * resp_buf,uint_t * resp_bufsz,char * xprt_cache)17610Sstevel@tonic-gate __svc_dup(struct svc_req *req, caddr_t *resp_buf, uint_t *resp_bufsz,
17620Sstevel@tonic-gate char *xprt_cache)
17630Sstevel@tonic-gate {
17640Sstevel@tonic-gate uint32_t drxid, drhash;
17650Sstevel@tonic-gate int rc;
17660Sstevel@tonic-gate struct dupreq *dr = NULL;
17670Sstevel@tonic-gate time_t timenow = time(NULL);
17680Sstevel@tonic-gate
17690Sstevel@tonic-gate /* LINTED pointer alignment */
17700Sstevel@tonic-gate struct dupcache *dc = (struct dupcache *)xprt_cache;
17710Sstevel@tonic-gate
17720Sstevel@tonic-gate if (dc == NULL) {
17730Sstevel@tonic-gate syslog(LOG_ERR, "__svc_dup: undefined cache");
17740Sstevel@tonic-gate return (DUP_ERROR);
17750Sstevel@tonic-gate }
17760Sstevel@tonic-gate
17770Sstevel@tonic-gate /* get the xid of the request */
17780Sstevel@tonic-gate if (SVC_CONTROL(req->rq_xprt, SVCGET_XID, (void*)&drxid) == FALSE) {
17790Sstevel@tonic-gate syslog(LOG_ERR, "__svc_dup: xid error");
17800Sstevel@tonic-gate return (DUP_ERROR);
17810Sstevel@tonic-gate }
17820Sstevel@tonic-gate drhash = drxid % dc->dc_buckets;
17830Sstevel@tonic-gate
17840Sstevel@tonic-gate if ((rc = __svc_dupcache_check(req, resp_buf, resp_bufsz, dc, drxid,
17850Sstevel@tonic-gate drhash)) != DUP_NEW)
17860Sstevel@tonic-gate return (rc);
17870Sstevel@tonic-gate
17880Sstevel@tonic-gate if ((dr = __svc_dupcache_victim(dc, timenow)) == NULL)
17890Sstevel@tonic-gate return (DUP_ERROR);
17900Sstevel@tonic-gate
17910Sstevel@tonic-gate if ((rc = __svc_dupcache_enter(req, dr, dc, drxid, drhash, timenow))
17920Sstevel@tonic-gate == DUP_ERROR)
17930Sstevel@tonic-gate return (rc);
17940Sstevel@tonic-gate
17950Sstevel@tonic-gate return (DUP_NEW);
17960Sstevel@tonic-gate }
17970Sstevel@tonic-gate
17980Sstevel@tonic-gate
17990Sstevel@tonic-gate
18000Sstevel@tonic-gate /*
18010Sstevel@tonic-gate * __svc_dupcache_check(struct svc_req *req, caddr_t *resp_buf,
18020Sstevel@tonic-gate * uint_t *resp_bufsz,truct dupcache *dc, uint32_t drxid,
18030Sstevel@tonic-gate * uint32_t drhash)
18040Sstevel@tonic-gate * Checks to see whether an entry already exists in the cache. If it does
18050Sstevel@tonic-gate * copy back into the resp_buf, if appropriate. Return the status of
18060Sstevel@tonic-gate * the request, or DUP_NEW if the entry is not in the cache
18070Sstevel@tonic-gate */
18080Sstevel@tonic-gate static int
__svc_dupcache_check(struct svc_req * req,caddr_t * resp_buf,uint_t * resp_bufsz,struct dupcache * dc,uint32_t drxid,uint32_t drhash)18090Sstevel@tonic-gate __svc_dupcache_check(struct svc_req *req, caddr_t *resp_buf, uint_t *resp_bufsz,
18100Sstevel@tonic-gate struct dupcache *dc, uint32_t drxid, uint32_t drhash)
18110Sstevel@tonic-gate {
18120Sstevel@tonic-gate struct dupreq *dr = NULL;
18130Sstevel@tonic-gate
1814132Srobinson (void) rw_rdlock(&(dc->dc_lock));
18150Sstevel@tonic-gate dr = dc->dc_hashtbl[drhash];
18160Sstevel@tonic-gate while (dr != NULL) {
18170Sstevel@tonic-gate if (dr->dr_xid == drxid &&
18180Sstevel@tonic-gate dr->dr_proc == req->rq_proc &&
18190Sstevel@tonic-gate dr->dr_prog == req->rq_prog &&
18200Sstevel@tonic-gate dr->dr_vers == req->rq_vers &&
18210Sstevel@tonic-gate dr->dr_addr.len == req->rq_xprt->xp_rtaddr.len &&
1822132Srobinson memcmp(dr->dr_addr.buf,
1823132Srobinson req->rq_xprt->xp_rtaddr.buf,
18240Sstevel@tonic-gate dr->dr_addr.len) == 0) { /* entry found */
18250Sstevel@tonic-gate if (dr->dr_hash != drhash) {
18260Sstevel@tonic-gate /* sanity check */
1827132Srobinson (void) rw_unlock((&dc->dc_lock));
18280Sstevel@tonic-gate syslog(LOG_ERR,
18290Sstevel@tonic-gate "\n__svc_dupdone: hashing error");
18300Sstevel@tonic-gate return (DUP_ERROR);
18310Sstevel@tonic-gate }
18320Sstevel@tonic-gate
18330Sstevel@tonic-gate /*
18340Sstevel@tonic-gate * return results for requests on lru list, if
18350Sstevel@tonic-gate * appropriate requests must be DUP_DROP or DUP_DONE
18360Sstevel@tonic-gate * to have a result. A NULL buffer in the cache
18370Sstevel@tonic-gate * implies no results were sent during dupdone.
18380Sstevel@tonic-gate * A NULL buffer in the call implies not interested
18390Sstevel@tonic-gate * in results.
18400Sstevel@tonic-gate */
18410Sstevel@tonic-gate if (((dr->dr_status == DUP_DONE) ||
18420Sstevel@tonic-gate (dr->dr_status == DUP_DROP)) &&
18430Sstevel@tonic-gate resp_buf != NULL &&
18440Sstevel@tonic-gate dr->dr_resp.buf != NULL) {
1845132Srobinson *resp_buf = malloc(dr->dr_resp.len);
18460Sstevel@tonic-gate if (*resp_buf == NULL) {
18470Sstevel@tonic-gate syslog(LOG_ERR,
18480Sstevel@tonic-gate "__svc_dupcache_check: malloc failed");
1849132Srobinson (void) rw_unlock(&(dc->dc_lock));
18500Sstevel@tonic-gate return (DUP_ERROR);
18510Sstevel@tonic-gate }
1852132Srobinson (void) memset(*resp_buf, 0, dr->dr_resp.len);
1853132Srobinson (void) memcpy(*resp_buf, dr->dr_resp.buf,
18540Sstevel@tonic-gate dr->dr_resp.len);
18550Sstevel@tonic-gate *resp_bufsz = dr->dr_resp.len;
18560Sstevel@tonic-gate } else {
18570Sstevel@tonic-gate /* no result */
18580Sstevel@tonic-gate if (resp_buf)
18590Sstevel@tonic-gate *resp_buf = NULL;
18600Sstevel@tonic-gate if (resp_bufsz)
18610Sstevel@tonic-gate *resp_bufsz = 0;
18620Sstevel@tonic-gate }
1863132Srobinson (void) rw_unlock(&(dc->dc_lock));
18640Sstevel@tonic-gate return (dr->dr_status);
18650Sstevel@tonic-gate }
18660Sstevel@tonic-gate dr = dr->dr_chain;
18670Sstevel@tonic-gate }
1868132Srobinson (void) rw_unlock(&(dc->dc_lock));
18690Sstevel@tonic-gate return (DUP_NEW);
18700Sstevel@tonic-gate }
18710Sstevel@tonic-gate
18720Sstevel@tonic-gate /*
18730Sstevel@tonic-gate * __svc_dupcache_victim(struct dupcache *dc, time_t timenow)
18740Sstevel@tonic-gate * Return a victim dupreq entry to the caller, depending on cache policy.
18750Sstevel@tonic-gate */
18760Sstevel@tonic-gate static struct dupreq *
__svc_dupcache_victim(struct dupcache * dc,time_t timenow)18770Sstevel@tonic-gate __svc_dupcache_victim(struct dupcache *dc, time_t timenow)
18780Sstevel@tonic-gate {
18790Sstevel@tonic-gate struct dupreq *dr = NULL;
18800Sstevel@tonic-gate
18810Sstevel@tonic-gate switch (dc->dc_basis) {
18820Sstevel@tonic-gate case DUPCACHE_FIXEDTIME:
18830Sstevel@tonic-gate /*
18840Sstevel@tonic-gate * The hash policy is to free up a bit of the hash
18850Sstevel@tonic-gate * table before allocating a new entry as the victim.
18860Sstevel@tonic-gate * Freeing up the hash table each time should split
18870Sstevel@tonic-gate * the cost of keeping the hash table clean among threads.
18880Sstevel@tonic-gate * Note that only DONE or DROPPED entries are on the lru
18890Sstevel@tonic-gate * list but we do a sanity check anyway.
18900Sstevel@tonic-gate */
1891132Srobinson (void) rw_wrlock(&(dc->dc_lock));
18920Sstevel@tonic-gate while ((dc->dc_mru) && (dr = dc->dc_mru->dr_next) &&
18930Sstevel@tonic-gate ((timenow - dr->dr_time) > dc->dc_time)) {
18940Sstevel@tonic-gate /* clean and then free the entry */
18950Sstevel@tonic-gate if (dr->dr_status != DUP_DONE &&
18960Sstevel@tonic-gate dr->dr_status != DUP_DROP) {
18970Sstevel@tonic-gate /*
18980Sstevel@tonic-gate * The LRU list can't contain an
18990Sstevel@tonic-gate * entry where the status is other than
19000Sstevel@tonic-gate * DUP_DONE or DUP_DROP.
19010Sstevel@tonic-gate */
19020Sstevel@tonic-gate syslog(LOG_ERR,
1903132Srobinson "__svc_dupcache_victim: bad victim");
19040Sstevel@tonic-gate #ifdef DUP_DEBUG
19050Sstevel@tonic-gate /*
19060Sstevel@tonic-gate * Need to hold the reader/writers lock to
19070Sstevel@tonic-gate * print the cache info, since we already
19080Sstevel@tonic-gate * hold the writers lock, we shall continue
19090Sstevel@tonic-gate * calling __svc_dupcache_debug()
19100Sstevel@tonic-gate */
19110Sstevel@tonic-gate __svc_dupcache_debug(dc);
19120Sstevel@tonic-gate #endif /* DUP_DEBUG */
1913132Srobinson (void) rw_unlock(&(dc->dc_lock));
19140Sstevel@tonic-gate return (NULL);
19150Sstevel@tonic-gate }
19160Sstevel@tonic-gate /* free buffers */
19170Sstevel@tonic-gate if (dr->dr_resp.buf) {
1918132Srobinson free(dr->dr_resp.buf);
19190Sstevel@tonic-gate dr->dr_resp.buf = NULL;
19200Sstevel@tonic-gate }
19210Sstevel@tonic-gate if (dr->dr_addr.buf) {
1922132Srobinson free(dr->dr_addr.buf);
19230Sstevel@tonic-gate dr->dr_addr.buf = NULL;
19240Sstevel@tonic-gate }
19250Sstevel@tonic-gate
19260Sstevel@tonic-gate /* unhash the entry */
19270Sstevel@tonic-gate if (dr->dr_chain)
19280Sstevel@tonic-gate dr->dr_chain->dr_prevchain = dr->dr_prevchain;
19290Sstevel@tonic-gate if (dr->dr_prevchain)
19300Sstevel@tonic-gate dr->dr_prevchain->dr_chain = dr->dr_chain;
19310Sstevel@tonic-gate if (dc->dc_hashtbl[dr->dr_hash] == dr)
19320Sstevel@tonic-gate dc->dc_hashtbl[dr->dr_hash] = dr->dr_chain;
19330Sstevel@tonic-gate
19340Sstevel@tonic-gate /* modify the lru pointers */
1935132Srobinson if (dc->dc_mru == dr) {
19360Sstevel@tonic-gate dc->dc_mru = NULL;
1937132Srobinson } else {
19380Sstevel@tonic-gate dc->dc_mru->dr_next = dr->dr_next;
19390Sstevel@tonic-gate dr->dr_next->dr_prev = dc->dc_mru;
19400Sstevel@tonic-gate }
1941132Srobinson free(dr);
19420Sstevel@tonic-gate dr = NULL;
19430Sstevel@tonic-gate }
1944132Srobinson (void) rw_unlock(&(dc->dc_lock));
19450Sstevel@tonic-gate
19460Sstevel@tonic-gate /*
19470Sstevel@tonic-gate * Allocate and return new clean entry as victim
19480Sstevel@tonic-gate */
1949132Srobinson if ((dr = malloc(sizeof (*dr))) == NULL) {
19500Sstevel@tonic-gate syslog(LOG_ERR,
1951132Srobinson "__svc_dupcache_victim: malloc failed");
19520Sstevel@tonic-gate return (NULL);
19530Sstevel@tonic-gate }
1954132Srobinson (void) memset(dr, 0, sizeof (*dr));
19550Sstevel@tonic-gate return (dr);
19560Sstevel@tonic-gate default:
19570Sstevel@tonic-gate syslog(LOG_ERR,
1958132Srobinson "__svc_dupcache_victim: undefined dup cache_basis");
19590Sstevel@tonic-gate return (NULL);
19600Sstevel@tonic-gate }
19610Sstevel@tonic-gate }
19620Sstevel@tonic-gate
19630Sstevel@tonic-gate /*
19640Sstevel@tonic-gate * __svc_dupcache_enter(struct svc_req *req, struct dupreq *dr,
19650Sstevel@tonic-gate * struct dupcache *dc, uint32_t drxid, uint32_t drhash, time_t timenow)
19660Sstevel@tonic-gate * build new duprequest entry and then insert into the cache
19670Sstevel@tonic-gate */
19680Sstevel@tonic-gate static int
__svc_dupcache_enter(struct svc_req * req,struct dupreq * dr,struct dupcache * dc,uint32_t drxid,uint32_t drhash,time_t timenow)19690Sstevel@tonic-gate __svc_dupcache_enter(struct svc_req *req, struct dupreq *dr,
19700Sstevel@tonic-gate struct dupcache *dc, uint32_t drxid, uint32_t drhash, time_t timenow)
19710Sstevel@tonic-gate {
19720Sstevel@tonic-gate dr->dr_xid = drxid;
19730Sstevel@tonic-gate dr->dr_prog = req->rq_prog;
19740Sstevel@tonic-gate dr->dr_vers = req->rq_vers;
19750Sstevel@tonic-gate dr->dr_proc = req->rq_proc;
19760Sstevel@tonic-gate dr->dr_addr.maxlen = req->rq_xprt->xp_rtaddr.len;
19770Sstevel@tonic-gate dr->dr_addr.len = dr->dr_addr.maxlen;
1978132Srobinson if ((dr->dr_addr.buf = malloc(dr->dr_addr.maxlen)) == NULL) {
1979132Srobinson syslog(LOG_ERR, "__svc_dupcache_enter: malloc failed");
1980132Srobinson free(dr);
19810Sstevel@tonic-gate return (DUP_ERROR);
19820Sstevel@tonic-gate }
1983132Srobinson (void) memset(dr->dr_addr.buf, 0, dr->dr_addr.len);
1984132Srobinson (void) memcpy(dr->dr_addr.buf, req->rq_xprt->xp_rtaddr.buf,
1985132Srobinson dr->dr_addr.len);
19860Sstevel@tonic-gate dr->dr_resp.buf = NULL;
19870Sstevel@tonic-gate dr->dr_resp.maxlen = 0;
19880Sstevel@tonic-gate dr->dr_resp.len = 0;
19890Sstevel@tonic-gate dr->dr_status = DUP_INPROGRESS;
19900Sstevel@tonic-gate dr->dr_time = timenow;
19910Sstevel@tonic-gate dr->dr_hash = drhash; /* needed for efficient victim cleanup */
19920Sstevel@tonic-gate
19930Sstevel@tonic-gate /* place entry at head of hash table */
1994132Srobinson (void) rw_wrlock(&(dc->dc_lock));
19950Sstevel@tonic-gate dr->dr_chain = dc->dc_hashtbl[drhash];
19960Sstevel@tonic-gate dr->dr_prevchain = NULL;
19970Sstevel@tonic-gate if (dc->dc_hashtbl[drhash] != NULL)
19980Sstevel@tonic-gate dc->dc_hashtbl[drhash]->dr_prevchain = dr;
19990Sstevel@tonic-gate dc->dc_hashtbl[drhash] = dr;
2000132Srobinson (void) rw_unlock(&(dc->dc_lock));
20010Sstevel@tonic-gate return (DUP_NEW);
20020Sstevel@tonic-gate }
20030Sstevel@tonic-gate
20040Sstevel@tonic-gate /*
20050Sstevel@tonic-gate * __svc_dupdone(struct svc_req *req, caddr_t resp_buf, uint_t resp_bufsz,
20060Sstevel@tonic-gate * int status, char *xprt_cache)
20070Sstevel@tonic-gate * Marks the request done (DUP_DONE or DUP_DROP) and stores the response.
20080Sstevel@tonic-gate * Only DONE and DROP requests can be marked as done. Sets the lru pointers
20090Sstevel@tonic-gate * to make the entry the most recently used. Returns DUP_ERROR or status.
20100Sstevel@tonic-gate */
20110Sstevel@tonic-gate int
__svc_dupdone(struct svc_req * req,caddr_t resp_buf,uint_t resp_bufsz,int status,char * xprt_cache)20120Sstevel@tonic-gate __svc_dupdone(struct svc_req *req, caddr_t resp_buf, uint_t resp_bufsz,
20130Sstevel@tonic-gate int status, char *xprt_cache)
20140Sstevel@tonic-gate {
20150Sstevel@tonic-gate uint32_t drxid, drhash;
20160Sstevel@tonic-gate int rc;
20170Sstevel@tonic-gate
20180Sstevel@tonic-gate /* LINTED pointer alignment */
20190Sstevel@tonic-gate struct dupcache *dc = (struct dupcache *)xprt_cache;
20200Sstevel@tonic-gate
20210Sstevel@tonic-gate if (dc == NULL) {
20220Sstevel@tonic-gate syslog(LOG_ERR, "__svc_dupdone: undefined cache");
20230Sstevel@tonic-gate return (DUP_ERROR);
20240Sstevel@tonic-gate }
20250Sstevel@tonic-gate
20260Sstevel@tonic-gate if (status != DUP_DONE && status != DUP_DROP) {
20270Sstevel@tonic-gate syslog(LOG_ERR, "__svc_dupdone: invalid dupdone status");
20280Sstevel@tonic-gate syslog(LOG_ERR, " must be DUP_DONE or DUP_DROP");
20290Sstevel@tonic-gate return (DUP_ERROR);
20300Sstevel@tonic-gate }
20310Sstevel@tonic-gate
20320Sstevel@tonic-gate /* find the xid of the entry in the cache */
20330Sstevel@tonic-gate if (SVC_CONTROL(req->rq_xprt, SVCGET_XID, (void*)&drxid) == FALSE) {
20340Sstevel@tonic-gate syslog(LOG_ERR, "__svc_dup: xid error");
20350Sstevel@tonic-gate return (DUP_ERROR);
20360Sstevel@tonic-gate }
20370Sstevel@tonic-gate drhash = drxid % dc->dc_buckets;
20380Sstevel@tonic-gate
20390Sstevel@tonic-gate /* update the status of the entry and result buffers, if required */
20400Sstevel@tonic-gate if ((rc = __svc_dupcache_update(req, resp_buf, resp_bufsz, status,
20410Sstevel@tonic-gate dc, drxid, drhash)) == DUP_ERROR) {
20420Sstevel@tonic-gate syslog(LOG_ERR, "__svc_dupdone: cache entry error");
20430Sstevel@tonic-gate return (DUP_ERROR);
20440Sstevel@tonic-gate }
20450Sstevel@tonic-gate
20460Sstevel@tonic-gate return (rc);
20470Sstevel@tonic-gate }
20480Sstevel@tonic-gate
20490Sstevel@tonic-gate /*
20500Sstevel@tonic-gate * __svc_dupcache_update(struct svc_req *req, caddr_t resp_buf,
20510Sstevel@tonic-gate * uint_t resp_bufsz, int status, struct dupcache *dc, uint32_t drxid,
20520Sstevel@tonic-gate * uint32_t drhash)
20530Sstevel@tonic-gate * Check if entry exists in the dupcacache. If it does, update its status
20540Sstevel@tonic-gate * and time and also its buffer, if appropriate. Its possible, but unlikely
20550Sstevel@tonic-gate * for DONE requests to not exist in the cache. Return DUP_ERROR or status.
20560Sstevel@tonic-gate */
20570Sstevel@tonic-gate static int
__svc_dupcache_update(struct svc_req * req,caddr_t resp_buf,uint_t resp_bufsz,int status,struct dupcache * dc,uint32_t drxid,uint32_t drhash)20580Sstevel@tonic-gate __svc_dupcache_update(struct svc_req *req, caddr_t resp_buf, uint_t resp_bufsz,
20590Sstevel@tonic-gate int status, struct dupcache *dc, uint32_t drxid, uint32_t drhash)
20600Sstevel@tonic-gate {
20610Sstevel@tonic-gate struct dupreq *dr = NULL;
20620Sstevel@tonic-gate time_t timenow = time(NULL);
20630Sstevel@tonic-gate
2064132Srobinson (void) rw_wrlock(&(dc->dc_lock));
20650Sstevel@tonic-gate dr = dc->dc_hashtbl[drhash];
20660Sstevel@tonic-gate while (dr != NULL) {
20670Sstevel@tonic-gate if (dr->dr_xid == drxid &&
20680Sstevel@tonic-gate dr->dr_proc == req->rq_proc &&
20690Sstevel@tonic-gate dr->dr_prog == req->rq_prog &&
20700Sstevel@tonic-gate dr->dr_vers == req->rq_vers &&
20710Sstevel@tonic-gate dr->dr_addr.len == req->rq_xprt->xp_rtaddr.len &&
2072132Srobinson memcmp(dr->dr_addr.buf,
2073132Srobinson req->rq_xprt->xp_rtaddr.buf,
20740Sstevel@tonic-gate dr->dr_addr.len) == 0) { /* entry found */
20750Sstevel@tonic-gate if (dr->dr_hash != drhash) {
20760Sstevel@tonic-gate /* sanity check */
2077132Srobinson (void) rw_unlock(&(dc->dc_lock));
20780Sstevel@tonic-gate syslog(LOG_ERR,
20790Sstevel@tonic-gate "\n__svc_dupdone: hashing error");
20800Sstevel@tonic-gate return (DUP_ERROR);
20810Sstevel@tonic-gate }
20820Sstevel@tonic-gate
20830Sstevel@tonic-gate /* store the results if bufer is not NULL */
20840Sstevel@tonic-gate if (resp_buf != NULL) {
2085132Srobinson if ((dr->dr_resp.buf =
2086132Srobinson malloc(resp_bufsz)) == NULL) {
2087132Srobinson (void) rw_unlock(&(dc->dc_lock));
20880Sstevel@tonic-gate syslog(LOG_ERR,
2089132Srobinson "__svc_dupdone: malloc failed");
20900Sstevel@tonic-gate return (DUP_ERROR);
20910Sstevel@tonic-gate }
2092132Srobinson (void) memset(dr->dr_resp.buf, 0, resp_bufsz);
2093132Srobinson (void) memcpy(dr->dr_resp.buf, resp_buf,
20940Sstevel@tonic-gate (uint_t)resp_bufsz);
20950Sstevel@tonic-gate dr->dr_resp.len = resp_bufsz;
20960Sstevel@tonic-gate }
20970Sstevel@tonic-gate
20980Sstevel@tonic-gate /* update status and done time */
20990Sstevel@tonic-gate dr->dr_status = status;
21000Sstevel@tonic-gate dr->dr_time = timenow;
21010Sstevel@tonic-gate
21020Sstevel@tonic-gate /* move the entry to the mru position */
21030Sstevel@tonic-gate if (dc->dc_mru == NULL) {
21040Sstevel@tonic-gate dr->dr_next = dr;
21050Sstevel@tonic-gate dr->dr_prev = dr;
21060Sstevel@tonic-gate } else {
21070Sstevel@tonic-gate dr->dr_next = dc->dc_mru->dr_next;
21080Sstevel@tonic-gate dc->dc_mru->dr_next->dr_prev = dr;
21090Sstevel@tonic-gate dr->dr_prev = dc->dc_mru;
21100Sstevel@tonic-gate dc->dc_mru->dr_next = dr;
21110Sstevel@tonic-gate }
21120Sstevel@tonic-gate dc->dc_mru = dr;
21130Sstevel@tonic-gate
2114132Srobinson (void) rw_unlock(&(dc->dc_lock));
21150Sstevel@tonic-gate return (status);
21160Sstevel@tonic-gate }
21170Sstevel@tonic-gate dr = dr->dr_chain;
21180Sstevel@tonic-gate }
2119132Srobinson (void) rw_unlock(&(dc->dc_lock));
21200Sstevel@tonic-gate syslog(LOG_ERR, "__svc_dupdone: entry not in dup cache");
21210Sstevel@tonic-gate return (DUP_ERROR);
21220Sstevel@tonic-gate }
21230Sstevel@tonic-gate
21240Sstevel@tonic-gate #ifdef DUP_DEBUG
21250Sstevel@tonic-gate /*
21260Sstevel@tonic-gate * __svc_dupcache_debug(struct dupcache *dc)
21270Sstevel@tonic-gate * print out the hash table stuff
21280Sstevel@tonic-gate *
21290Sstevel@tonic-gate * This function requires the caller to hold the reader
21300Sstevel@tonic-gate * or writer version of the duplicate request cache lock (dc_lock).
21310Sstevel@tonic-gate */
21320Sstevel@tonic-gate static void
__svc_dupcache_debug(struct dupcache * dc)21330Sstevel@tonic-gate __svc_dupcache_debug(struct dupcache *dc)
21340Sstevel@tonic-gate {
21350Sstevel@tonic-gate struct dupreq *dr = NULL;
21360Sstevel@tonic-gate int i;
21370Sstevel@tonic-gate bool_t bval;
21380Sstevel@tonic-gate
21390Sstevel@tonic-gate fprintf(stderr, " HASHTABLE\n");
21400Sstevel@tonic-gate for (i = 0; i < dc->dc_buckets; i++) {
21410Sstevel@tonic-gate bval = FALSE;
21420Sstevel@tonic-gate dr = dc->dc_hashtbl[i];
21430Sstevel@tonic-gate while (dr != NULL) {
21440Sstevel@tonic-gate if (!bval) { /* ensures bucket printed only once */
21450Sstevel@tonic-gate fprintf(stderr, " bucket : %d\n", i);
21460Sstevel@tonic-gate bval = TRUE;
21470Sstevel@tonic-gate }
21480Sstevel@tonic-gate fprintf(stderr, "\txid: %u status: %d time: %ld",
21490Sstevel@tonic-gate dr->dr_xid, dr->dr_status, dr->dr_time);
21500Sstevel@tonic-gate fprintf(stderr, " dr: %x chain: %x prevchain: %x\n",
21510Sstevel@tonic-gate dr, dr->dr_chain, dr->dr_prevchain);
21520Sstevel@tonic-gate dr = dr->dr_chain;
21530Sstevel@tonic-gate }
21540Sstevel@tonic-gate }
21550Sstevel@tonic-gate
21560Sstevel@tonic-gate fprintf(stderr, " LRU\n");
21570Sstevel@tonic-gate if (dc->dc_mru) {
21580Sstevel@tonic-gate dr = dc->dc_mru->dr_next; /* lru */
21590Sstevel@tonic-gate while (dr != dc->dc_mru) {
21600Sstevel@tonic-gate fprintf(stderr, "\txid: %u status : %d time : %ld",
21610Sstevel@tonic-gate dr->dr_xid, dr->dr_status, dr->dr_time);
21620Sstevel@tonic-gate fprintf(stderr, " dr: %x next: %x prev: %x\n",
21630Sstevel@tonic-gate dr, dr->dr_next, dr->dr_prev);
21640Sstevel@tonic-gate dr = dr->dr_next;
21650Sstevel@tonic-gate }
21660Sstevel@tonic-gate fprintf(stderr, "\txid: %u status: %d time: %ld",
21670Sstevel@tonic-gate dr->dr_xid, dr->dr_status, dr->dr_time);
21680Sstevel@tonic-gate fprintf(stderr, " dr: %x next: %x prev: %x\n", dr,
21690Sstevel@tonic-gate dr->dr_next, dr->dr_prev);
21700Sstevel@tonic-gate }
21710Sstevel@tonic-gate }
21720Sstevel@tonic-gate #endif /* DUP_DEBUG */
2173