xref: /onnv-gate/usr/src/lib/libnsl/rpc/fdsync.c (revision 132:e3f7eaf7dde4)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*132Srobinson 
230Sstevel@tonic-gate /*
24*132Srobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * This file contains functions that enables the rpc library to synchronize
320Sstevel@tonic-gate  * between various threads while they compete for a particular file descriptor.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include "mt.h"
360Sstevel@tonic-gate #include "rpc_mt.h"
370Sstevel@tonic-gate #include <rpc/rpc.h>
380Sstevel@tonic-gate #include <errno.h>
390Sstevel@tonic-gate #include <sys/poll.h>
400Sstevel@tonic-gate #include <syslog.h>
410Sstevel@tonic-gate #include <sys/types.h>
420Sstevel@tonic-gate #include <stdlib.h>
430Sstevel@tonic-gate #include <unistd.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * A block holds an array of maxBlockSize cell and associated recursive locks
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate 
490Sstevel@tonic-gate #define	CELLTBLSZ	1024
500Sstevel@tonic-gate 
510Sstevel@tonic-gate typedef struct rpcfd_block {
520Sstevel@tonic-gate 	struct rpcfd_block *next;	/* Next Block */
530Sstevel@tonic-gate 	struct rpcfd_block *prev;	/* prev Block */
540Sstevel@tonic-gate 	int 	end;			/* fd of last lock in the list */
550Sstevel@tonic-gate 	mutex_t	lock[CELLTBLSZ];	/* recursive locks */
560Sstevel@tonic-gate } rpcfd_block_t;
570Sstevel@tonic-gate 
580Sstevel@tonic-gate mutex_t	rpc_fd_list_lock = DEFAULTMUTEX;	/* protects list manipulation */
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /* Following functions create and manipulates the dgfd lock object */
610Sstevel@tonic-gate 
620Sstevel@tonic-gate static mutex_t *search(const void *handle, int fd);
630Sstevel@tonic-gate static rpcfd_block_t *create_block(const void *handle, int fd);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate void *
rpc_fd_init(void)660Sstevel@tonic-gate rpc_fd_init(void)
670Sstevel@tonic-gate {
680Sstevel@tonic-gate 	/*
690Sstevel@tonic-gate 	 * Create first chunk of CELLTBLSZ
700Sstevel@tonic-gate 	 * (No lock is required for initial creation.)
710Sstevel@tonic-gate 	 */
720Sstevel@tonic-gate 	return (create_block(NULL, 0));
730Sstevel@tonic-gate }
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate  * If rpc_fd_lock() fails to acquire a lock, it returns non-zero (ENOMEM).
770Sstevel@tonic-gate  * (The operation can only fail due to a malloc() failure.)
780Sstevel@tonic-gate  * The caller of rpc_fd_lock() must call rpc_fd_unlock() even if
790Sstevel@tonic-gate  * rpc_fd_lock() failed.  This keeps _sigoff() and _sigon() balanced.
800Sstevel@tonic-gate  *
810Sstevel@tonic-gate  * If search() and create_block() fail for rpc_fd_lock(), then search()
820Sstevel@tonic-gate  * will fail for rpc_fd_unlock(), so mutex_lock() and mutex_unlock()
830Sstevel@tonic-gate  * calls will be balanced.  In any case, since the mutex is marked
840Sstevel@tonic-gate  * LOCK_ERRORCHECK, an additional mutex_unlock() does nothing.
850Sstevel@tonic-gate  */
860Sstevel@tonic-gate int
rpc_fd_lock(const void * handle,int fd)870Sstevel@tonic-gate rpc_fd_lock(const void *handle, int fd)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate 	mutex_t *mp;
900Sstevel@tonic-gate 	rpcfd_block_t *p;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	_sigoff();
930Sstevel@tonic-gate 	(void) mutex_lock(&rpc_fd_list_lock);
940Sstevel@tonic-gate 	mp = search(handle, fd);
950Sstevel@tonic-gate 	if (mp == NULL) {
960Sstevel@tonic-gate 		p = create_block(handle, fd);
970Sstevel@tonic-gate 		if (p != NULL)
980Sstevel@tonic-gate 			mp = &p->lock[fd % CELLTBLSZ];
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 	(void) mutex_unlock(&rpc_fd_list_lock);
1010Sstevel@tonic-gate 	if (mp == NULL)
1020Sstevel@tonic-gate 		return (ENOMEM);
1030Sstevel@tonic-gate 	(void) mutex_lock(mp);
1040Sstevel@tonic-gate 	return (0);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate void
rpc_fd_unlock(const void * handle,int fd)1080Sstevel@tonic-gate rpc_fd_unlock(const void *handle, int fd)
1090Sstevel@tonic-gate {
1100Sstevel@tonic-gate 	mutex_t *mp;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	(void) mutex_lock(&rpc_fd_list_lock);
1130Sstevel@tonic-gate 	mp = search(handle, fd);
1140Sstevel@tonic-gate 	(void) mutex_unlock(&rpc_fd_list_lock);
1150Sstevel@tonic-gate 	if (mp != NULL)
1160Sstevel@tonic-gate 		(void) mutex_unlock(mp);
1170Sstevel@tonic-gate 	_sigon();
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate static rpcfd_block_t *
create_block(const void * handle,int fd)1210Sstevel@tonic-gate create_block(const void *handle, int fd)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate 	rpcfd_block_t *l, *lprev;
1240Sstevel@tonic-gate 	rpcfd_block_t *p;
1250Sstevel@tonic-gate 	int i;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	p = malloc(sizeof (rpcfd_block_t));
1280Sstevel@tonic-gate 	if (p == NULL)
1290Sstevel@tonic-gate 		return (NULL);
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	for (i = 0; i < CELLTBLSZ; i++) {
1320Sstevel@tonic-gate 		(void) mutex_init(&p->lock[i],
1330Sstevel@tonic-gate 			USYNC_THREAD | LOCK_RECURSIVE | LOCK_ERRORCHECK, NULL);
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 	p->end = (((fd + CELLTBLSZ) / CELLTBLSZ) * CELLTBLSZ) - 1;
1360Sstevel@tonic-gate 	lprev = NULL;
1370Sstevel@tonic-gate 	for (l = (rpcfd_block_t *)handle; l; l = l->next) {
1380Sstevel@tonic-gate 		lprev = l;
1390Sstevel@tonic-gate 		if (fd < l->end)
1400Sstevel@tonic-gate 			break;
1410Sstevel@tonic-gate 	}
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	p->next = l;
1440Sstevel@tonic-gate 	p->prev = lprev;
1450Sstevel@tonic-gate 	if (lprev)
1460Sstevel@tonic-gate 		lprev->next = p;
1470Sstevel@tonic-gate 	if (l)
1480Sstevel@tonic-gate 		l->prev = p;
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	return (p);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate  * Called with rpc_fd_list_lock held.
1550Sstevel@tonic-gate  */
1560Sstevel@tonic-gate static mutex_t *
search(const void * handle,int fd)1570Sstevel@tonic-gate search(const void *handle, int fd)
1580Sstevel@tonic-gate {
1590Sstevel@tonic-gate 	rpcfd_block_t *p;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	for (p = (rpcfd_block_t *)handle; p; p = p->next) {
1620Sstevel@tonic-gate 		if (fd <= p->end)
1630Sstevel@tonic-gate 			return (&p->lock[fd % CELLTBLSZ]);
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	return (NULL);
1670Sstevel@tonic-gate }
168