1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 23 /* 24 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 25 * Use is subject to license terms. 26 */ 27 28 #pragma ident "%Z%%M% %I% %E% SMI" 29 30 /* 31 * This file contains functions that enables the rpc library to synchronize 32 * between various threads while they compete for a particular file descriptor. 33 */ 34 35 #include "mt.h" 36 #include "rpc_mt.h" 37 #include <rpc/rpc.h> 38 #include <errno.h> 39 #include <sys/poll.h> 40 #include <syslog.h> 41 #include <sys/types.h> 42 #include <stdlib.h> 43 #include <unistd.h> 44 45 /* 46 * A block holds an array of maxBlockSize cell and associated recursive locks 47 */ 48 49 #define CELLTBLSZ 1024 50 51 typedef struct rpcfd_block { 52 struct rpcfd_block *next; /* Next Block */ 53 struct rpcfd_block *prev; /* prev Block */ 54 int end; /* fd of last lock in the list */ 55 mutex_t lock[CELLTBLSZ]; /* recursive locks */ 56 } rpcfd_block_t; 57 58 mutex_t rpc_fd_list_lock = DEFAULTMUTEX; /* protects list manipulation */ 59 60 /* Following functions create and manipulates the dgfd lock object */ 61 62 static mutex_t *search(const void *handle, int fd); 63 static rpcfd_block_t *create_block(const void *handle, int fd); 64 65 void * 66 rpc_fd_init(void) 67 { 68 /* 69 * Create first chunk of CELLTBLSZ 70 * (No lock is required for initial creation.) 71 */ 72 return (create_block(NULL, 0)); 73 } 74 75 /* 76 * If rpc_fd_lock() fails to acquire a lock, it returns non-zero (ENOMEM). 77 * (The operation can only fail due to a malloc() failure.) 78 * The caller of rpc_fd_lock() must call rpc_fd_unlock() even if 79 * rpc_fd_lock() failed. This keeps _sigoff() and _sigon() balanced. 80 * 81 * If search() and create_block() fail for rpc_fd_lock(), then search() 82 * will fail for rpc_fd_unlock(), so mutex_lock() and mutex_unlock() 83 * calls will be balanced. In any case, since the mutex is marked 84 * LOCK_ERRORCHECK, an additional mutex_unlock() does nothing. 85 */ 86 int 87 rpc_fd_lock(const void *handle, int fd) 88 { 89 mutex_t *mp; 90 rpcfd_block_t *p; 91 92 _sigoff(); 93 (void) mutex_lock(&rpc_fd_list_lock); 94 mp = search(handle, fd); 95 if (mp == NULL) { 96 p = create_block(handle, fd); 97 if (p != NULL) 98 mp = &p->lock[fd % CELLTBLSZ]; 99 } 100 (void) mutex_unlock(&rpc_fd_list_lock); 101 if (mp == NULL) 102 return (ENOMEM); 103 (void) mutex_lock(mp); 104 return (0); 105 } 106 107 void 108 rpc_fd_unlock(const void *handle, int fd) 109 { 110 mutex_t *mp; 111 112 (void) mutex_lock(&rpc_fd_list_lock); 113 mp = search(handle, fd); 114 (void) mutex_unlock(&rpc_fd_list_lock); 115 if (mp != NULL) 116 (void) mutex_unlock(mp); 117 _sigon(); 118 } 119 120 static rpcfd_block_t * 121 create_block(const void *handle, int fd) 122 { 123 rpcfd_block_t *l, *lprev; 124 rpcfd_block_t *p; 125 int i; 126 127 p = malloc(sizeof (rpcfd_block_t)); 128 if (p == NULL) 129 return (NULL); 130 131 for (i = 0; i < CELLTBLSZ; i++) { 132 (void) mutex_init(&p->lock[i], 133 USYNC_THREAD | LOCK_RECURSIVE | LOCK_ERRORCHECK, NULL); 134 } 135 p->end = (((fd + CELLTBLSZ) / CELLTBLSZ) * CELLTBLSZ) - 1; 136 lprev = NULL; 137 for (l = (rpcfd_block_t *)handle; l; l = l->next) { 138 lprev = l; 139 if (fd < l->end) 140 break; 141 } 142 143 p->next = l; 144 p->prev = lprev; 145 if (lprev) 146 lprev->next = p; 147 if (l) 148 l->prev = p; 149 150 return (p); 151 } 152 153 /* 154 * Called with rpc_fd_list_lock held. 155 */ 156 static mutex_t * 157 search(const void *handle, int fd) 158 { 159 rpcfd_block_t *p; 160 161 for (p = (rpcfd_block_t *)handle; p; p = p->next) { 162 if (fd <= p->end) 163 return (&p->lock[fd % CELLTBLSZ]); 164 } 165 166 return (NULL); 167 } 168