076892
diff --git a/src/bindresvport.c b/src/bindresvport.c
076892
index ef9b345..5c0ddcf 100644
076892
--- a/src/bindresvport.c
076892
+++ b/src/bindresvport.c
076892
@@ -164,10 +164,11 @@ bindresvport_sa(sd, sa)
076892
 	int endport = ENDPORT;
076892
 	int i;
076892
 
076892
+	mutex_lock(&port_lock);
076892
+
076892
 	if (!blacklist_read)
076892
 		load_blacklist();
076892
 
076892
-	mutex_lock(&port_lock);
076892
 	nports = ENDPORT - startport + 1;
076892
 
076892
         if (sa == NULL) {
076892
diff --git a/src/clnt_dg.c b/src/clnt_dg.c
076892
index d8890b5..166af63 100644
076892
--- a/src/clnt_dg.c
076892
+++ b/src/clnt_dg.c
076892
@@ -53,6 +53,7 @@
076892
 #include <unistd.h>
076892
 #include <err.h>
076892
 #include "rpc_com.h"
076892
+#include "clnt_fd_locks.h"
076892
 
076892
 #ifdef IP_RECVERR
076892
 #include <asm/types.h>
076892
@@ -81,24 +82,29 @@ static void clnt_dg_destroy(CLIENT *);
076892
  *	This machinery implements per-fd locks for MT-safety.  It is not
076892
  *	sufficient to do per-CLIENT handle locks for MT-safety because a
076892
  *	user may create more than one CLIENT handle with the same fd behind
076892
- *	it.  Therfore, we allocate an array of flags (dg_fd_locks), protected
076892
- *	by the clnt_fd_lock mutex, and an array (dg_cv) of condition variables
076892
- *	similarly protected.  Dg_fd_lock[fd] == 1 => a call is activte on some
076892
- *	CLIENT handle created for that fd.
076892
+ *	it.
076892
+ *
076892
+ *	We keep track of a list of per-fd locks, protected by the clnt_fd_lock
076892
+ *	mutex. Each per-fd lock consists of a predicate indicating whether is
076892
+ *	active or not: fd_lock->active == TRUE => a call is active on some
076892
+ *	CLIENT handle created for that fd. Each fd predicate is guarded by a
076892
+ *	condition variable so that the global mutex can be unlocked while
076892
+ *	waiting for the predicate to change.
076892
+ *
076892
  *	The current implementation holds locks across the entire RPC and reply,
076892
  *	including retransmissions.  Yes, this is silly, and as soon as this
076892
  *	code is proven to work, this should be the first thing fixed.  One step
076892
  *	at a time.
076892
  */
076892
-static int	*dg_fd_locks;
076892
+static fd_locks_t *dg_fd_locks;
076892
 extern mutex_t clnt_fd_lock;
076892
-static cond_t	*dg_cv;
076892
-#define	release_fd_lock(fd, mask) {		\
076892
+#define	release_fd_lock(fd_lock, mask) {	\
076892
 	mutex_lock(&clnt_fd_lock);	\
076892
-	dg_fd_locks[fd] = 0;		\
076892
-	mutex_unlock(&clnt_fd_lock);	\
076892
+	fd_lock->active = FALSE;	\
076892
+	fd_lock->pending--;		\
076892
 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL); \
076892
-	cond_signal(&dg_cv[fd]);	\
076892
+	cond_signal(&fd_lock->cv);	\
076892
+	mutex_unlock(&clnt_fd_lock);    \
076892
 }
076892
 
076892
 static const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory";
076892
@@ -110,6 +116,7 @@ static const char mem_err_clnt_dg[] = "clnt_dg_create: out of memory";
076892
  */
076892
 struct cu_data {
076892
 	int			cu_fd;		/* connections fd */
076892
+	fd_lock_t 		*cu_fd_lock;
076892
 	bool_t			cu_closeit;	/* opened by library */
076892
 	struct sockaddr_storage	cu_raddr;	/* remote address */
076892
 	int			cu_rlen;
076892
@@ -158,46 +165,22 @@ clnt_dg_create(fd, svcaddr, program, version, sendsz, recvsz)
076892
 	sigset_t newmask;
076892
 	struct __rpc_sockinfo si;
076892
 	int one = 1;
076892
+	fd_lock_t *fd_lock;
076892
 
076892
 	sigfillset(&newmask);
076892
 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
076892
 	mutex_lock(&clnt_fd_lock);
076892
-	if (dg_fd_locks == (int *) NULL) {
076892
-		size_t cv_allocsz, fd_allocsz;
076892
-		unsigned int dtbsize = __rpc_dtbsize();
076892
-
076892
-		if ( (size_t) dtbsize > SIZE_MAX/sizeof(cond_t)) {
076892
+	if (dg_fd_locks == (fd_locks_t *) NULL) {
076892
+		dg_fd_locks = fd_locks_init();
076892
+		if (dg_fd_locks == (fd_locks_t *) NULL) {
076892
 			mutex_unlock(&clnt_fd_lock);
076892
-			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
076892
-			errno = EOVERFLOW;
076892
 			goto err1;
076892
 		}
076892
-
076892
-		fd_allocsz = dtbsize * sizeof (int);
076892
-		dg_fd_locks = (int *) mem_alloc(fd_allocsz);
076892
-		if (dg_fd_locks == (int *) NULL) {
076892
-			mutex_unlock(&clnt_fd_lock);
076892
-			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
076892
-			errno = ENOMEM;
076892
-			goto err1;
076892
-		} else
076892
-			memset(dg_fd_locks, '\0', fd_allocsz);
076892
-
076892
-		cv_allocsz = dtbsize * sizeof (cond_t);
076892
-		dg_cv = (cond_t *) mem_alloc(cv_allocsz);
076892
-		if (dg_cv == (cond_t *) NULL) {
076892
-			mem_free(dg_fd_locks, fd_allocsz);
076892
-			dg_fd_locks = (int *) NULL;
076892
-			mutex_unlock(&clnt_fd_lock);
076892
-			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
076892
-			errno = ENOMEM;
076892
-			goto err1;
076892
-		} else {
076892
-			int i;
076892
-
076892
-			for (i = 0; i < dtbsize; i++)
076892
-				cond_init(&dg_cv[i], 0, (void *) 0);
076892
-		}
076892
+	}
076892
+	fd_lock = fd_lock_create(fd, dg_fd_locks);
076892
+	if (fd_lock == (fd_lock_t *) NULL) {
076892
+		mutex_unlock(&clnt_fd_lock);
076892
+		goto err1;
076892
 	}
076892
 
076892
 	mutex_unlock(&clnt_fd_lock);
076892
@@ -277,6 +260,7 @@ clnt_dg_create(fd, svcaddr, program, version, sendsz, recvsz)
076892
 	 */
076892
 	cu->cu_closeit = FALSE;
076892
 	cu->cu_fd = fd;
076892
+	cu->cu_fd_lock = fd_lock;
076892
 	cl->cl_ops = clnt_dg_ops();
076892
 	cl->cl_private = (caddr_t)(void *)cu;
076892
 	cl->cl_auth = authnone_create();
076892
@@ -322,17 +306,16 @@ clnt_dg_call(cl, proc, xargs, argsp, xresults, resultsp, utimeout)
076892
 	sigset_t newmask;
076892
 	socklen_t salen;
076892
 	ssize_t recvlen = 0;
076892
-	int rpc_lock_value;
076892
 	u_int32_t xid, inval, outval;
076892
 
076892
 	outlen = 0;
076892
 	sigfillset(&newmask);
076892
 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
076892
 	mutex_lock(&clnt_fd_lock);
076892
-	while (dg_fd_locks[cu->cu_fd])
076892
-		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
076892
-	rpc_lock_value = 1;
076892
-	dg_fd_locks[cu->cu_fd] = rpc_lock_value;
076892
+	cu->cu_fd_lock->pending++;
076892
+	while (cu->cu_fd_lock->active)
076892
+		cond_wait(&cu->cu_fd_lock->cv, &clnt_fd_lock);
076892
+	cu->cu_fd_lock->active = TRUE;
076892
 	mutex_unlock(&clnt_fd_lock);
076892
 	if (cu->cu_total.tv_usec == -1) {
076892
 		timeout = utimeout;	/* use supplied timeout */
076892
@@ -481,7 +464,7 @@ get_reply:
076892
 		  e = (struct sock_extended_err *) CMSG_DATA(cmsg);
076892
 		  cu->cu_error.re_errno = e->ee_errno;
076892
 		  mem_free(cbuf, (outlen + 256));
076892
-		  release_fd_lock(cu->cu_fd, mask);
076892
+		  release_fd_lock(cu->cu_fd_lock, mask);
076892
 		  return (cu->cu_error.re_status = RPC_CANTRECV);
076892
 		}
076892
 	  mem_free(cbuf, (outlen + 256));
076892
@@ -561,7 +544,7 @@ get_reply:
076892
 
076892
 	}
076892
 out:
076892
-	release_fd_lock(cu->cu_fd, mask);
076892
+	release_fd_lock(cu->cu_fd_lock, mask);
076892
 	return (cu->cu_error.re_status);
076892
 }
076892
 
076892
@@ -590,13 +573,15 @@ clnt_dg_freeres(cl, xdr_res, res_ptr)
076892
 	sigfillset(&newmask);
076892
 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
076892
 	mutex_lock(&clnt_fd_lock);
076892
-	while (dg_fd_locks[cu->cu_fd])
076892
-		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
076892
+	cu->cu_fd_lock->pending++;
076892
+	while (cu->cu_fd_lock->active)
076892
+		cond_wait(&cu->cu_fd_lock->cv, &clnt_fd_lock);
076892
 	xdrs->x_op = XDR_FREE;
076892
 	dummy = (*xdr_res)(xdrs, res_ptr);
076892
-	mutex_unlock(&clnt_fd_lock);
076892
+	cu->cu_fd_lock->pending--;
076892
 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
076892
-	cond_signal(&dg_cv[cu->cu_fd]);
076892
+	cond_signal(&cu->cu_fd_lock->cv);
076892
+	mutex_unlock(&clnt_fd_lock);
076892
 	return (dummy);
076892
 }
076892
 
076892
@@ -617,36 +602,35 @@ clnt_dg_control(cl, request, info)
076892
 	struct netbuf *addr;
076892
 	sigset_t mask;
076892
 	sigset_t newmask;
076892
-	int rpc_lock_value;
076892
 
076892
 	sigfillset(&newmask);
076892
 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
076892
 	mutex_lock(&clnt_fd_lock);
076892
-	while (dg_fd_locks[cu->cu_fd])
076892
-		cond_wait(&dg_cv[cu->cu_fd], &clnt_fd_lock);
076892
-        rpc_lock_value = 1;
076892
-	dg_fd_locks[cu->cu_fd] = rpc_lock_value;
076892
+	cu->cu_fd_lock->pending++;
076892
+	while (cu->cu_fd_lock->active)
076892
+		cond_wait(&cu->cu_fd_lock->cv, &clnt_fd_lock);
076892
+	cu->cu_fd_lock->active = TRUE;
076892
 	mutex_unlock(&clnt_fd_lock);
076892
 	switch (request) {
076892
 	case CLSET_FD_CLOSE:
076892
 		cu->cu_closeit = TRUE;
076892
-		release_fd_lock(cu->cu_fd, mask);
076892
+		release_fd_lock(cu->cu_fd_lock, mask);
076892
 		return (TRUE);
076892
 	case CLSET_FD_NCLOSE:
076892
 		cu->cu_closeit = FALSE;
076892
-		release_fd_lock(cu->cu_fd, mask);
076892
+		release_fd_lock(cu->cu_fd_lock, mask);
076892
 		return (TRUE);
076892
 	}
076892
 
076892
 	/* for other requests which use info */
076892
 	if (info == NULL) {
076892
-		release_fd_lock(cu->cu_fd, mask);
076892
+		release_fd_lock(cu->cu_fd_lock, mask);
076892
 		return (FALSE);
076892
 	}
076892
 	switch (request) {
076892
 	case CLSET_TIMEOUT:
076892
 		if (time_not_ok((struct timeval *)info)) {
076892
-			release_fd_lock(cu->cu_fd, mask);
076892
+			release_fd_lock(cu->cu_fd_lock, mask);
076892
 			return (FALSE);
076892
 		}
076892
 		cu->cu_total = *(struct timeval *)info;
076892
@@ -660,7 +644,7 @@ clnt_dg_control(cl, request, info)
076892
 		break;
076892
 	case CLSET_RETRY_TIMEOUT:
076892
 		if (time_not_ok((struct timeval *)info)) {
076892
-			release_fd_lock(cu->cu_fd, mask);
076892
+			release_fd_lock(cu->cu_fd_lock, mask);
076892
 			return (FALSE);
076892
 		}
076892
 		cu->cu_wait = *(struct timeval *)info;
076892
@@ -680,7 +664,7 @@ clnt_dg_control(cl, request, info)
076892
 	case CLSET_SVC_ADDR:		/* set to new address */
076892
 		addr = (struct netbuf *)info;
076892
 		if (addr->len < sizeof cu->cu_raddr) {
076892
-			release_fd_lock(cu->cu_fd, mask);
076892
+			release_fd_lock(cu->cu_fd_lock, mask);
076892
 			return (FALSE);
076892
 		}
076892
 		(void) memcpy(&cu->cu_raddr, addr->buf, addr->len);
076892
@@ -743,10 +727,10 @@ clnt_dg_control(cl, request, info)
076892
 		cu->cu_connect = *(int *)info;
076892
 		break;
076892
 	default:
076892
-		release_fd_lock(cu->cu_fd, mask);
076892
+		release_fd_lock(cu->cu_fd_lock, mask);
076892
 		return (FALSE);
076892
 	}
076892
-	release_fd_lock(cu->cu_fd, mask);
076892
+	release_fd_lock(cu->cu_fd_lock, mask);
076892
 	return (TRUE);
076892
 }
076892
 
076892
@@ -756,14 +740,21 @@ clnt_dg_destroy(cl)
076892
 {
076892
 	struct cu_data *cu = (struct cu_data *)cl->cl_private;
076892
 	int cu_fd = cu->cu_fd;
076892
+	fd_lock_t *cu_fd_lock = cu->cu_fd_lock;
076892
 	sigset_t mask;
076892
 	sigset_t newmask;
076892
 
076892
 	sigfillset(&newmask);
076892
 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
076892
 	mutex_lock(&clnt_fd_lock);
076892
-	while (dg_fd_locks[cu_fd])
076892
-		cond_wait(&dg_cv[cu_fd], &clnt_fd_lock);
076892
+	/* wait until all pending operations on client are completed. */
076892
+	while (cu_fd_lock->pending > 0) {
076892
+		/* If a blocked operation can be awakened, then do it. */
076892
+		if (cu_fd_lock->active == FALSE)
076892
+			cond_signal(&cu_fd_lock->cv);
076892
+		/* keep waiting... */
076892
+		cond_wait(&cu_fd_lock->cv, &clnt_fd_lock);
076892
+	}
076892
 	if (cu->cu_closeit)
076892
 		(void)close(cu_fd);
076892
 	XDR_DESTROY(&(cu->cu_outxdrs));
076892
@@ -773,9 +764,10 @@ clnt_dg_destroy(cl)
076892
 	if (cl->cl_tp && cl->cl_tp[0])
076892
 		mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
076892
 	mem_free(cl, sizeof (CLIENT));
076892
+	cond_signal(&cu_fd_lock->cv);
076892
+	fd_lock_destroy(cu_fd, cu_fd_lock, dg_fd_locks);
076892
 	mutex_unlock(&clnt_fd_lock);
076892
 	thr_sigsetmask(SIG_SETMASK, &mask, NULL);
076892
-	cond_signal(&dg_cv[cu_fd]);
076892
 }
076892
 
076892
 static struct clnt_ops *
076892
diff --git a/src/clnt_fd_locks.h b/src/clnt_fd_locks.h
076892
new file mode 100644
076892
index 0000000..6ba62cb
076892
--- /dev/null
076892
+++ b/src/clnt_fd_locks.h
076892
@@ -0,0 +1,209 @@
076892
+/*
076892
+ * debug.h -- debugging routines for libtirpc
076892
+ *
076892
+ * Copyright (c) 2020 SUSE LINUX GmbH, Nuernberg, Germany.
076892
+ *
076892
+ * Redistribution and use in source and binary forms, with or without
076892
+ * modification, are permitted provided that the following conditions are met:
076892
+ * - Redistributions of source code must retain the above copyright notice,
076892
+ *   this list of conditions and the following disclaimer.
076892
+ * - Redistributions in binary form must reproduce the above copyright notice,
076892
+ *   this list of conditions and the following disclaimer in the documentation
076892
+ *   and/or other materials provided with the distribution.
076892
+ * - Neither the name of Sun Microsystems, Inc. nor the names of its
076892
+ *   contributors may be used to endorse or promote products derived
076892
+ *   from this software without specific prior written permission.
076892
+ *
076892
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
076892
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
076892
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
076892
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
076892
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
076892
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
076892
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
076892
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
076892
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
076892
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
076892
+ * POSSIBILITY OF SUCH DAMAGE.
076892
+ */
076892
+
076892
+#ifndef _CLNT_FD_LOCKS_H
076892
+#define _CLNT_FD_LOCKS_H
076892
+
076892
+#include <sys/queue.h>
076892
+#include <errno.h>
076892
+#include <reentrant.h>
076892
+#include <rpc/xdr.h>
076892
+
076892
+
076892
+/*
076892
+ * This utility manages a list of per-fd locks for the clients.
076892
+ *
076892
+ * If MAX_FDLOCKS_PREALLOC is defined, a number of pre-fd locks will be
076892
+ * pre-allocated. This number is the minimum of MAX_FDLOCKS_PREALLOC or
076892
+ * the process soft limit of allowed fds.
076892
+ */
076892
+#ifdef MAX_FDLOCKS_PREALLOC
076892
+static unsigned int fd_locks_prealloc = 0;
076892
+#endif
076892
+
076892
+/* per-fd lock */
076892
+struct fd_lock_t {
076892
+	bool_t active;
076892
+	int pending;        /* Number of pending operations on fd */
076892
+	cond_t cv;
076892
+};
076892
+typedef struct fd_lock_t fd_lock_t;
076892
+
076892
+
076892
+/* internal type to store per-fd locks in a list */
076892
+struct fd_lock_item_t {
076892
+	/* fd_lock_t first so we can cast to fd_lock_item_t */
076892
+	fd_lock_t fd_lock;
076892
+	int fd;
076892
+	unsigned int refs;
076892
+	TAILQ_ENTRY(fd_lock_item_t) link;
076892
+};
076892
+typedef struct fd_lock_item_t fd_lock_item_t;
076892
+#define to_fd_lock_item(fdlock_t_ptr) ((fd_lock_item_t*) fdlock_t_ptr)
076892
+
076892
+
076892
+/* internal list of per-fd locks */
076892
+typedef TAILQ_HEAD(,fd_lock_item_t) fd_lock_list_t;
076892
+
076892
+
076892
+#ifdef MAX_FDLOCKS_PREALLOC
076892
+
076892
+/* With pre-allocation, keep track of both an array and a list */
076892
+struct fd_locks_t {
076892
+	fd_lock_list_t fd_lock_list;
076892
+	fd_lock_t *fd_lock_array;
076892
+};
076892
+typedef struct fd_locks_t fd_locks_t;
076892
+#define to_fd_lock_list(fd_locks_t_ptr) (&fd_locks_t_ptr->fd_lock_list)
076892
+
076892
+#else
076892
+
076892
+/* With no pre-allocation, just keep track of a list */
076892
+typedef fd_lock_list_t fd_locks_t;
076892
+#define to_fd_lock_list(fd_locks_t_ptr) ((fd_lock_list_t *) fd_locks_t_ptr)
076892
+
076892
+#endif
076892
+
076892
+
076892
+/* allocate fd locks */
076892
+static inline
076892
+fd_locks_t* fd_locks_init() {
076892
+	fd_locks_t *fd_locks;
076892
+
076892
+	fd_locks = (fd_locks_t *) mem_alloc(sizeof(fd_locks_t));
076892
+	if (fd_locks == (fd_locks_t *) NULL) {
076892
+		errno = ENOMEM;
076892
+		return (NULL);
076892
+	}
076892
+	TAILQ_INIT(to_fd_lock_list(fd_locks));
076892
+
076892
+#ifdef MAX_FDLOCKS_PREALLOC
076892
+	size_t fd_lock_arraysz;
076892
+
076892
+	if (fd_locks_prealloc == 0) {
076892
+		unsigned int dtbsize = __rpc_dtbsize();
076892
+		if (0 < dtbsize && dtbsize < MAX_FDLOCKS_PREALLOC)
076892
+			fd_locks_prealloc = dtbsize;
076892
+		else
076892
+			fd_locks_prealloc = MAX_FDLOCKS_PREALLOC;
076892
+	}
076892
+
076892
+	if ( (size_t) fd_locks_prealloc > SIZE_MAX/sizeof(fd_lock_t)) {
076892
+		mem_free(fd_locks, sizeof (*fd_locks));
076892
+		errno = EOVERFLOW;
076892
+		return (NULL);
076892
+	}
076892
+
076892
+	fd_lock_arraysz = fd_locks_prealloc * sizeof (fd_lock_t);
076892
+	fd_locks->fd_lock_array = (fd_lock_t *) mem_alloc(fd_lock_arraysz);
076892
+	if (fd_locks->fd_lock_array == (fd_lock_t *) NULL) {
076892
+		mem_free(fd_locks, sizeof (*fd_locks));
076892
+		errno = ENOMEM;
076892
+		return (NULL);
076892
+	}
076892
+	else {
076892
+		int i;
076892
+
076892
+		for (i = 0; i < fd_locks_prealloc; i++) {
076892
+			fd_locks->fd_lock_array[i].active = FALSE;
076892
+			cond_init(&fd_locks->fd_lock_array[i].cv, 0, (void *) 0);
076892
+		}
076892
+	}
076892
+#endif
076892
+
076892
+	return fd_locks;
076892
+}
076892
+
076892
+/* de-allocate fd locks */
076892
+static inline
076892
+void fd_locks_destroy(fd_locks_t *fd_locks) {
076892
+#ifdef MAX_FDLOCKS_PREALLOC
076892
+	fd_lock_t *array = fd_locks->fd_lock_array;
076892
+	mem_free(array, fd_locks_prealloc * sizeof (fd_lock_t));
076892
+#endif
076892
+	fd_lock_item_t *item;
076892
+	fd_lock_list_t *list = to_fd_lock_list(fd_locks);
076892
+
076892
+	TAILQ_FOREACH(item, list, link) {
076892
+		cond_destroy(&item->fd_lock.cv);
076892
+		mem_free(item, sizeof (*item));
076892
+	}
076892
+	mem_free(fd_locks, sizeof (*fd_locks));
076892
+}
076892
+
076892
+/* allocate per-fd lock */
076892
+static inline
076892
+fd_lock_t* fd_lock_create(int fd, fd_locks_t *fd_locks) {
076892
+#ifdef MAX_FDLOCKS_PREALLOC
076892
+	if (fd < fd_locks_prealloc) {
076892
+		return &fd_locks->fd_lock_array[fd];
076892
+	}
076892
+#endif
076892
+	fd_lock_item_t *item;
076892
+	fd_lock_list_t *list = to_fd_lock_list(fd_locks);
076892
+
076892
+	for (item = TAILQ_FIRST(list);
076892
+	     item != (fd_lock_item_t *) NULL && item->fd != fd;
076892
+	     item = TAILQ_NEXT(item, link));
076892
+
076892
+	if (item == (fd_lock_item_t *) NULL) {
076892
+		item = (fd_lock_item_t *) mem_alloc(sizeof(fd_lock_item_t));
076892
+		if (item == (fd_lock_item_t *) NULL) {
076892
+			errno = ENOMEM;
076892
+			return (NULL);
076892
+		}
076892
+		item->fd = fd;
076892
+		item->refs = 1;
076892
+		item->fd_lock.active = FALSE;
076892
+		item->fd_lock.pending = 0;
076892
+		cond_init(&item->fd_lock.cv, 0, (void *) 0);
076892
+		TAILQ_INSERT_HEAD(list, item, link);
076892
+	} else {
076892
+		item->refs++;
076892
+	}
076892
+	return &item->fd_lock;
076892
+}
076892
+
076892
+/* de-allocate per-fd lock */
076892
+static inline
076892
+void fd_lock_destroy(int fd, fd_lock_t *fd_lock, fd_locks_t *fd_locks) {
076892
+#ifdef MAX_FDLOCKS_PREALLOC
076892
+	if (fd < fd_locks_prealloc)
076892
+		return;
076892
+#endif
076892
+	fd_lock_item_t* item = to_fd_lock_item(fd_lock);
076892
+	item->refs--;
076892
+	if (item->refs <= 0) {
076892
+		TAILQ_REMOVE(to_fd_lock_list(fd_locks), item, link);
076892
+		cond_destroy(&item->fd_lock.cv);
076892
+		mem_free(item, sizeof (*item));
076892
+	}
076892
+}
076892
+
076892
+#endif /* _CLNT_FD_LOCKS_H */
076892
diff --git a/src/clnt_vc.c b/src/clnt_vc.c
076892
index 3ba55de..7fe3016 100644
076892
--- a/src/clnt_vc.c
076892
+++ b/src/clnt_vc.c
076892
@@ -67,6 +67,7 @@
076892
 
076892
 #include <rpc/rpc.h>
076892
 #include "rpc_com.h"
076892
+#include "clnt_fd_locks.h"
076892
 
076892
 #ifdef HAVE_RPCSEC_GSS
076892
 #include <rpc/auth_gss.h>
076892
@@ -114,6 +115,7 @@ static int write_vc(void *, void *, int);
076892
 
076892
 struct ct_data {
076892
 	int		ct_fd;		/* connection's fd */
076892
+	fd_lock_t	*ct_fd_lock;
076892
 	bool_t		ct_closeit;	/* close it on destroy */
076892
 	struct timeval	ct_wait;	/* wait interval in milliseconds */
076892
 	bool_t          ct_waitset;	/* wait set by clnt_control? */
076892
@@ -128,27 +130,33 @@ struct ct_data {
076892
 };
076892
 
076892
 /*
076892
- *      This machinery implements per-fd locks for MT-safety.  It is not
076892
- *      sufficient to do per-CLIENT handle locks for MT-safety because a
076892
- *      user may create more than one CLIENT handle with the same fd behind
076892
- *      it.  Therfore, we allocate an array of flags (vc_fd_locks), protected
076892
- *      by the clnt_fd_lock mutex, and an array (vc_cv) of condition variables
076892
- *      similarly protected.  Vc_fd_lock[fd] == 1 => a call is active on some
076892
- *      CLIENT handle created for that fd.
076892
- *      The current implementation holds locks across the entire RPC and reply.
076892
- *      Yes, this is silly, and as soon as this code is proven to work, this
076892
- *      should be the first thing fixed.  One step at a time.
076892
+ *	This machinery implements per-fd locks for MT-safety.  It is not
076892
+ *	sufficient to do per-CLIENT handle locks for MT-safety because a
076892
+ *	user may create more than one CLIENT handle with the same fd behind
076892
+ *	it.
076892
+ *
076892
+ *	We keep track of a list of per-fd locks, protected by the clnt_fd_lock
076892
+ *	mutex. Each per-fd lock consists of a predicate indicating whether is
076892
+ *	active or not: fd_lock->active == TRUE => a call is active on some
076892
+ *	CLIENT handle created for that fd. Each fd predicate is guarded by a
076892
+ *	condition variable so that the global mutex can be unlocked while
076892
+ *	waiting for the predicate to change.
076892
+ *
076892
+ *	The current implementation holds locks across the entire RPC and reply,
076892
+ *	including retransmissions.  Yes, this is silly, and as soon as this
076892
+ *	code is proven to work, this should be the first thing fixed.  One step
076892
+ *	at a time.
076892
  */
076892
-static int      *vc_fd_locks;
076892
+static fd_locks_t *vc_fd_locks;
076892
 extern pthread_mutex_t disrupt_lock;
076892
 extern mutex_t  clnt_fd_lock;
076892
-static cond_t   *vc_cv;
076892
-#define release_fd_lock(fd, mask) {	\
076892
+#define release_fd_lock(fd_lock, mask) {	\
076892
 	mutex_lock(&clnt_fd_lock);	\
076892
-	vc_fd_locks[fd] = 0;		\
076892
-	mutex_unlock(&clnt_fd_lock);	\
076892
+	fd_lock->active = FALSE;	\
076892
+	fd_lock->pending--;		\
076892
 	thr_sigsetmask(SIG_SETMASK, &(mask), (sigset_t *) NULL);	\
076892
-	cond_signal(&vc_cv[fd]);	\
076892
+	cond_signal(&fd_lock->cv);	\
076892
+	mutex_unlock(&clnt_fd_lock);    \
076892
 }
076892
 
076892
 static const char clnt_vc_errstr[] = "%s : %s";
076892
@@ -185,6 +193,7 @@ clnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz)
076892
 	struct sockaddr_storage ss;
076892
 	socklen_t slen;
076892
 	struct __rpc_sockinfo si;
076892
+	fd_lock_t *fd_lock;
076892
 
076892
 	mutex_lock(&disrupt_lock);
076892
 	if (disrupt == 0)
076892
@@ -205,49 +214,26 @@ clnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz)
076892
 	sigfillset(&newmask);
076892
 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
076892
 	mutex_lock(&clnt_fd_lock);
076892
-	if (vc_fd_locks == (int *) NULL) {
076892
-		size_t cv_allocsz, fd_allocsz;
076892
-		unsigned int dtbsize = __rpc_dtbsize();
076892
-		struct rpc_createerr *ce = &get_rpc_createerr();
076892
-
076892
-		if ( (size_t) dtbsize > SIZE_MAX/sizeof(cond_t)) {
076892
+	if (vc_fd_locks == (fd_locks_t *) NULL) {
076892
+		vc_fd_locks = fd_locks_init();
076892
+		if (vc_fd_locks == (fd_locks_t *) NULL) {
076892
+			struct rpc_createerr *ce;
076892
 			mutex_unlock(&clnt_fd_lock);
076892
-			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
076892
-			ce->cf_stat = RPC_SYSTEMERROR;
076892
-			ce->cf_error.re_errno = EOVERFLOW;
076892
-			goto err;
076892
-		}
076892
-
076892
-		fd_allocsz = dtbsize * sizeof (int);
076892
-		vc_fd_locks = (int *) mem_alloc(fd_allocsz);
076892
-		if (vc_fd_locks == (int *) NULL) {
076892
-			mutex_unlock(&clnt_fd_lock);
076892
-			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
076892
-			ce->cf_stat = RPC_SYSTEMERROR;
076892
-			ce->cf_error.re_errno = ENOMEM;
076892
-			goto err;
076892
-		} else
076892
-			memset(vc_fd_locks, '\0', fd_allocsz);
076892
-
076892
-		assert(vc_cv == (cond_t *) NULL);
076892
-		cv_allocsz = dtbsize * sizeof (cond_t);
076892
-		vc_cv = (cond_t *) mem_alloc(cv_allocsz);
076892
-		if (vc_cv == (cond_t *) NULL) {
076892
-			mem_free(vc_fd_locks, fd_allocsz);
076892
-			vc_fd_locks = (int *) NULL;
076892
-			mutex_unlock(&clnt_fd_lock);
076892
-			thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
076892
+			ce = &get_rpc_createerr();
076892
 			ce->cf_stat = RPC_SYSTEMERROR;
076892
-			ce->cf_error.re_errno = ENOMEM;
076892
+			ce->cf_error.re_errno = errno;
076892
 			goto err;
076892
-		} else {
076892
-			int i;
076892
-
076892
-			for (i = 0; i < dtbsize; i++)
076892
-				cond_init(&vc_cv[i], 0, (void *) 0);
076892
 		}
076892
-	} else
076892
-		assert(vc_cv != (cond_t *) NULL);
076892
+	}
076892
+	fd_lock = fd_lock_create(fd, vc_fd_locks);
076892
+	if (fd_lock == (fd_lock_t *) NULL) {
076892
+		struct rpc_createerr *ce;
076892
+		mutex_unlock(&clnt_fd_lock);
076892
+		ce = &get_rpc_createerr();
076892
+		ce->cf_stat = RPC_SYSTEMERROR;
076892
+		ce->cf_error.re_errno = errno;
076892
+		goto err;
076892
+	}
076892
 
076892
 	/*
076892
 	 * Do not hold mutex during connect
076892
@@ -283,6 +269,7 @@ clnt_vc_create(fd, raddr, prog, vers, sendsz, recvsz)
076892
 	 * Set up private data struct
076892
 	 */
076892
 	ct->ct_fd = fd;
076892
+	ct->ct_fd_lock = fd_lock;
076892
 	ct->ct_wait.tv_usec = 0;
076892
 	ct->ct_waitset = FALSE;
076892
 	ct->ct_addr.buf = malloc(raddr->maxlen);
076892
@@ -362,17 +349,16 @@ clnt_vc_call(cl, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
076892
 	bool_t shipnow;
076892
 	int refreshes = 2;
076892
 	sigset_t mask, newmask;
076892
-	int rpc_lock_value;
076892
 
076892
 	assert(cl != NULL);
076892
 
076892
 	sigfillset(&newmask);
076892
 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
076892
 	mutex_lock(&clnt_fd_lock);
076892
-	while (vc_fd_locks[ct->ct_fd])
076892
-		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
076892
-        rpc_lock_value = 1;
076892
-	vc_fd_locks[ct->ct_fd] = rpc_lock_value;
076892
+	ct->ct_fd_lock->pending++;
076892
+	while (ct->ct_fd_lock->active)
076892
+		cond_wait(&ct->ct_fd_lock->cv, &clnt_fd_lock);
076892
+	ct->ct_fd_lock->active = TRUE;
076892
 	mutex_unlock(&clnt_fd_lock);
076892
 	if (!ct->ct_waitset) {
076892
 		/* If time is not within limits, we ignore it. */
076892
@@ -401,22 +387,22 @@ call_again:
076892
 		if (ct->ct_error.re_status == RPC_SUCCESS)
076892
 			ct->ct_error.re_status = RPC_CANTENCODEARGS;
076892
 		(void)xdrrec_endofrecord(xdrs, TRUE);
076892
-		release_fd_lock(ct->ct_fd, mask);
076892
+		release_fd_lock(ct->ct_fd_lock, mask);
076892
 		return (ct->ct_error.re_status);
076892
 	}
076892
 	if (! xdrrec_endofrecord(xdrs, shipnow)) {
076892
-		release_fd_lock(ct->ct_fd, mask);
076892
+		release_fd_lock(ct->ct_fd_lock, mask);
076892
 		return (ct->ct_error.re_status = RPC_CANTSEND);
076892
 	}
076892
 	if (! shipnow) {
076892
-		release_fd_lock(ct->ct_fd, mask);
076892
+		release_fd_lock(ct->ct_fd_lock, mask);
076892
 		return (RPC_SUCCESS);
076892
 	}
076892
 	/*
076892
 	 * Hack to provide rpc-based message passing
076892
 	 */
076892
 	if (timeout.tv_sec == 0 && timeout.tv_usec == 0) {
076892
-		release_fd_lock(ct->ct_fd, mask);
076892
+		release_fd_lock(ct->ct_fd_lock, mask);
076892
 		return(ct->ct_error.re_status = RPC_TIMEDOUT);
076892
 	}
076892
 
076892
@@ -430,14 +416,14 @@ call_again:
076892
 		reply_msg.acpted_rply.ar_results.where = NULL;
076892
 		reply_msg.acpted_rply.ar_results.proc = (xdrproc_t)xdr_void;
076892
 		if (! xdrrec_skiprecord(xdrs)) {
076892
-			release_fd_lock(ct->ct_fd, mask);
076892
+			release_fd_lock(ct->ct_fd_lock, mask);
076892
 			return (ct->ct_error.re_status);
076892
 		}
076892
 		/* now decode and validate the response header */
076892
 		if (! xdr_replymsg(xdrs, &reply_msg)) {
076892
 			if (ct->ct_error.re_status == RPC_SUCCESS)
076892
 				continue;
076892
-			release_fd_lock(ct->ct_fd, mask);
076892
+			release_fd_lock(ct->ct_fd_lock, mask);
076892
 			return (ct->ct_error.re_status);
076892
 		}
076892
 		if (reply_msg.rm_xid == x_id)
076892
@@ -470,7 +456,7 @@ call_again:
076892
 		if (refreshes-- && AUTH_REFRESH(cl->cl_auth, &reply_msg))
076892
 			goto call_again;
076892
 	}  /* end of unsuccessful completion */
076892
-	release_fd_lock(ct->ct_fd, mask);
076892
+	release_fd_lock(ct->ct_fd_lock, mask);
076892
 	return (ct->ct_error.re_status);
076892
 }
076892
 
076892
@@ -508,13 +494,15 @@ clnt_vc_freeres(cl, xdr_res, res_ptr)
076892
 	sigfillset(&newmask);
076892
 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
076892
 	mutex_lock(&clnt_fd_lock);
076892
-	while (vc_fd_locks[ct->ct_fd])
076892
-		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
076892
+	ct->ct_fd_lock->pending++;
076892
+	while (ct->ct_fd_lock->active)
076892
+		cond_wait(&ct->ct_fd_lock->cv, &clnt_fd_lock);
076892
 	xdrs->x_op = XDR_FREE;
076892
 	dummy = (*xdr_res)(xdrs, res_ptr);
076892
-	mutex_unlock(&clnt_fd_lock);
076892
+	ct->ct_fd_lock->pending--;
076892
 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
076892
-	cond_signal(&vc_cv[ct->ct_fd]);
076892
+	cond_signal(&ct->ct_fd_lock->cv);
076892
+	mutex_unlock(&clnt_fd_lock);
076892
 
076892
 	return dummy;
076892
 }
076892
@@ -536,7 +524,6 @@ clnt_vc_control(cl, request, info)
076892
 	void *infop = info;
076892
 	sigset_t mask;
076892
 	sigset_t newmask;
076892
-	int rpc_lock_value;
076892
 	u_int32_t tmp;
076892
 	u_int32_t ltmp;
076892
 
076892
@@ -547,20 +534,20 @@ clnt_vc_control(cl, request, info)
076892
 	sigfillset(&newmask);
076892
 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
076892
 	mutex_lock(&clnt_fd_lock);
076892
-	while (vc_fd_locks[ct->ct_fd])
076892
-		cond_wait(&vc_cv[ct->ct_fd], &clnt_fd_lock);
076892
-        rpc_lock_value = 1;
076892
-	vc_fd_locks[ct->ct_fd] = rpc_lock_value;
076892
+	ct->ct_fd_lock->pending++;
076892
+	while (ct->ct_fd_lock->active)
076892
+		cond_wait(&ct->ct_fd_lock->cv, &clnt_fd_lock);
076892
+	ct->ct_fd_lock->active = TRUE;
076892
 	mutex_unlock(&clnt_fd_lock);
076892
 
076892
 	switch (request) {
076892
 	case CLSET_FD_CLOSE:
076892
 		ct->ct_closeit = TRUE;
076892
-		release_fd_lock(ct->ct_fd, mask);
076892
+		release_fd_lock(ct->ct_fd_lock, mask);
076892
 		return (TRUE);
076892
 	case CLSET_FD_NCLOSE:
076892
 		ct->ct_closeit = FALSE;
076892
-		release_fd_lock(ct->ct_fd, mask);
076892
+		release_fd_lock(ct->ct_fd_lock, mask);
076892
 		return (TRUE);
076892
 	default:
076892
 		break;
076892
@@ -568,13 +555,13 @@ clnt_vc_control(cl, request, info)
076892
 
076892
 	/* for other requests which use info */
076892
 	if (info == NULL) {
076892
-		release_fd_lock(ct->ct_fd, mask);
076892
+		release_fd_lock(ct->ct_fd_lock, mask);
076892
 		return (FALSE);
076892
 	}
076892
 	switch (request) {
076892
 	case CLSET_TIMEOUT:
076892
 		if (time_not_ok((struct timeval *)info)) {
076892
-			release_fd_lock(ct->ct_fd, mask);
076892
+			release_fd_lock(ct->ct_fd_lock, mask);
076892
 			return (FALSE);
076892
 		}
076892
 		ct->ct_wait = *(struct timeval *)infop;
076892
@@ -594,7 +581,7 @@ clnt_vc_control(cl, request, info)
076892
 		*(struct netbuf *)info = ct->ct_addr;
076892
 		break;
076892
 	case CLSET_SVC_ADDR:		/* set to new address */
076892
-		release_fd_lock(ct->ct_fd, mask);
076892
+		release_fd_lock(ct->ct_fd_lock, mask);
076892
 		return (FALSE);
076892
 	case CLGET_XID:
076892
 		/*
076892
@@ -648,10 +635,10 @@ clnt_vc_control(cl, request, info)
076892
 		break;
076892
 
076892
 	default:
076892
-		release_fd_lock(ct->ct_fd, mask);
076892
+		release_fd_lock(ct->ct_fd_lock, mask);
076892
 		return (FALSE);
076892
 	}
076892
-	release_fd_lock(ct->ct_fd, mask);
076892
+	release_fd_lock(ct->ct_fd_lock, mask);
076892
 	return (TRUE);
076892
 }
076892
 
076892
@@ -660,20 +647,24 @@ static void
076892
 clnt_vc_destroy(cl)
076892
 	CLIENT *cl;
076892
 {
076892
+	assert(cl != NULL);
076892
 	struct ct_data *ct = (struct ct_data *) cl->cl_private;
076892
 	int ct_fd = ct->ct_fd;
076892
+	fd_lock_t *ct_fd_lock = ct->ct_fd_lock;
076892
 	sigset_t mask;
076892
 	sigset_t newmask;
076892
 
076892
-	assert(cl != NULL);
076892
-
076892
-	ct = (struct ct_data *) cl->cl_private;
076892
-
076892
 	sigfillset(&newmask);
076892
 	thr_sigsetmask(SIG_SETMASK, &newmask, &mask);
076892
 	mutex_lock(&clnt_fd_lock);
076892
-	while (vc_fd_locks[ct_fd])
076892
-		cond_wait(&vc_cv[ct_fd], &clnt_fd_lock);
076892
+	/* wait until all pending operations on client are completed. */
076892
+	while (ct_fd_lock->pending > 0) {
076892
+		/* If a blocked operation can be awakened, then do it. */
076892
+		if (ct_fd_lock->active == FALSE)
076892
+			cond_signal(&ct_fd_lock->cv);
076892
+		/* keep waiting... */
076892
+		cond_wait(&ct_fd_lock->cv, &clnt_fd_lock);
076892
+	}
076892
 	if (ct->ct_closeit && ct->ct_fd != -1) {
076892
 		(void)close(ct->ct_fd);
076892
 	}
076892
@@ -686,9 +677,10 @@ clnt_vc_destroy(cl)
076892
 	if (cl->cl_tp && cl->cl_tp[0])
076892
 		mem_free(cl->cl_tp, strlen(cl->cl_tp) +1);
076892
 	mem_free(cl, sizeof(CLIENT));
076892
+	cond_signal(&ct_fd_lock->cv);
076892
+	fd_lock_destroy(ct_fd, ct_fd_lock, vc_fd_locks);
076892
 	mutex_unlock(&clnt_fd_lock);
076892
 	thr_sigsetmask(SIG_SETMASK, &(mask), NULL);
076892
-	cond_signal(&vc_cv[ct_fd]);
076892
 }
076892
 
076892
 /*
076892
diff --git a/src/getpublickey.c b/src/getpublickey.c
076892
index 8cf4dc2..be37a24 100644
076892
--- a/src/getpublickey.c
076892
+++ b/src/getpublickey.c
076892
@@ -74,7 +74,7 @@ __getpublickey_real(netname, publickey)
076892
 		return (0);
076892
 	}
076892
 	*p = '\0';
076892
-	(void) strncpy(publickey, lookup, HEXKEYBYTES);
076892
+	memcpy(publickey, lookup, HEXKEYBYTES);
076892
 	publickey[HEXKEYBYTES] = '\0';
076892
 	return (1);
076892
 }
076892
diff --git a/src/mt_misc.c b/src/mt_misc.c
076892
index 5a49b78..3a2bc51 100644
076892
--- a/src/mt_misc.c
076892
+++ b/src/mt_misc.c
076892
@@ -13,7 +13,7 @@ pthread_rwlock_t	svc_lock = PTHREAD_RWLOCK_INITIALIZER;
076892
 pthread_rwlock_t	svc_fd_lock = PTHREAD_RWLOCK_INITIALIZER;
076892
 
076892
 /* protects the RPCBIND address cache */
076892
-pthread_rwlock_t	rpcbaddr_cache_lock = PTHREAD_RWLOCK_INITIALIZER;
076892
+pthread_mutex_t	rpcbaddr_cache_lock = PTHREAD_MUTEX_INITIALIZER;
076892
 
076892
 /* protects authdes cache (svcauth_des.c) */
076892
 pthread_mutex_t	authdes_lock = PTHREAD_MUTEX_INITIALIZER;
076892
diff --git a/src/rpcb_clnt.c b/src/rpcb_clnt.c
076892
index a5923cb..65ff43e 100644
076892
--- a/src/rpcb_clnt.c
076892
+++ b/src/rpcb_clnt.c
076892
@@ -85,7 +85,7 @@ static int cachesize;
076892
 
076892
 extern int __rpc_lowvers;
076892
 
076892
-static struct address_cache *check_cache(const char *, const char *);
076892
+static struct address_cache *copy_of_cached(const char *, char *);
076892
 static void delete_cache(struct netbuf *);
076892
 static void add_cache(const char *, const char *, struct netbuf *, char *);
076892
 static CLIENT *getclnthandle(const char *, const struct netconfig *, char **);
076892
@@ -94,6 +94,82 @@ static CLIENT *local_rpcb(void);
076892
 static struct netbuf *got_entry(rpcb_entry_list_ptr, const struct netconfig *);
076892
 #endif
076892
 
076892
+/*
076892
+ * Destroys a cached address entry structure.
076892
+ *
076892
+ */
076892
+static void
076892
+destroy_addr(addr)
076892
+	struct address_cache *addr;
076892
+{
076892
+	if (addr == NULL)
076892
+		return;
076892
+	if(addr->ac_host != NULL)
076892
+		free(addr->ac_host);
076892
+	if(addr->ac_netid != NULL)
076892
+		free(addr->ac_netid);
076892
+	if(addr->ac_uaddr != NULL)
076892
+		free(addr->ac_uaddr);
076892
+	if(addr->ac_taddr != NULL) {
076892
+		if(addr->ac_taddr->buf != NULL)
076892
+			free(addr->ac_taddr->buf);
076892
+	}
076892
+	free(addr);
076892
+}
076892
+
076892
+/*
076892
+ * Creates an unlinked copy of an address cache entry. If the argument is NULL
076892
+ * or the new entry cannot be allocated then NULL is returned.
076892
+ */
076892
+static struct address_cache *
076892
+copy_addr(addr)
076892
+	const struct address_cache *addr;
076892
+{
076892
+	struct address_cache *copy;
076892
+
076892
+	if (addr == NULL)
076892
+		return (NULL);
076892
+
076892
+	copy = calloc(1, sizeof(*addr));
076892
+	if (copy == NULL)
076892
+		return (NULL);
076892
+
076892
+	if (addr->ac_host != NULL) {
076892
+		copy->ac_host = strdup(addr->ac_host);
076892
+		if (copy->ac_host == NULL)
076892
+			goto err;
076892
+	}
076892
+	if (addr->ac_netid != NULL) {
076892
+		copy->ac_netid = strdup(addr->ac_netid);
076892
+		if (copy->ac_netid == NULL)
076892
+			goto err;
076892
+	}
076892
+	if (addr->ac_uaddr != NULL) {
076892
+		copy->ac_uaddr = strdup(addr->ac_uaddr);
076892
+		if (copy->ac_uaddr == NULL)
076892
+			goto err;
076892
+	}
076892
+
076892
+	if (addr->ac_taddr == NULL)
076892
+		return (copy);
076892
+
076892
+	copy->ac_taddr = calloc(1, sizeof(*addr->ac_taddr));
076892
+	if (copy->ac_taddr == NULL)
076892
+		goto err;
076892
+
076892
+	memcpy(copy->ac_taddr, addr->ac_taddr, sizeof(*addr->ac_taddr));
076892
+	copy->ac_taddr->buf = malloc(addr->ac_taddr->len);
076892
+	if (copy->ac_taddr->buf == NULL)
076892
+		goto err;
076892
+
076892
+	memcpy(copy->ac_taddr->buf, addr->ac_taddr->buf, addr->ac_taddr->len);
076892
+	return (copy);
076892
+
076892
+err:
076892
+	destroy_addr(copy);
076892
+	return (NULL);
076892
+}
076892
+
076892
 /*
076892
  * This routine adjusts the timeout used for calls to the remote rpcbind.
076892
  * Also, this routine can be used to set the use of portmapper version 2
076892
@@ -125,17 +201,18 @@ __rpc_control(request, info)
076892
 }
076892
 
076892
 /*
076892
- *	It might seem that a reader/writer lock would be more reasonable here.
076892
- *	However because getclnthandle(), the only user of the cache functions,
076892
- *	may do a delete_cache() operation if a check_cache() fails to return an
076892
- *	address useful to clnt_tli_create(), we may as well use a mutex.
076892
- */
076892
-/*
076892
- * As it turns out, if the cache lock is *not* a reader/writer lock, we will
076892
- * block all clnt_create's if we are trying to connect to a host that's down,
076892
- * since the lock will be held all during that time.
076892
+ * Protect against concurrent access to the address cache and modifications
076892
+ * (esp. deletions) of cache entries.
076892
+ *
076892
+ * Previously a bidirectional R/W lock was used. However, R/W locking is
076892
+ * dangerous as it allows concurrent modification (e.g. deletion with write
076892
+ * lock) at the same time as the deleted element is accessed via check_cache()
076892
+ * and a read lock). We absolutely need a single mutex for all access to
076892
+ * prevent cache corruption. If the mutexing is restricted to only the
076892
+ * relevant code sections, deadlocking should be avoided even with recursed
076892
+ * client creation.
076892
  */
076892
-extern rwlock_t	rpcbaddr_cache_lock;
076892
+extern pthread_mutex_t	rpcbaddr_cache_lock;
076892
 
076892
 /*
076892
  * The routines check_cache(), add_cache(), delete_cache() manage the
076892
@@ -143,49 +220,52 @@ extern rwlock_t	rpcbaddr_cache_lock;
076892
  */
076892
 
076892
 static struct address_cache *
076892
-check_cache(host, netid)
076892
-	const char *host, *netid;
076892
+copy_of_cached(host, netid)
076892
+	const char *host; 
076892
+	char *netid;
076892
 {
076892
-	struct address_cache *cptr;
076892
-
076892
-	/* READ LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
076892
+	struct address_cache *cptr, *copy = NULL;
076892
 
076892
+	mutex_lock(&rpcbaddr_cache_lock);
076892
 	for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
076892
 		if (!strcmp(cptr->ac_host, host) &&
076892
 		    !strcmp(cptr->ac_netid, netid)) {
076892
 			LIBTIRPC_DEBUG(3, ("check_cache: Found cache entry for %s: %s\n", 
076892
 				host, netid));
076892
-			return (cptr);
076892
+			copy = copy_addr(cptr);
076892
+			break;
076892
 		}
076892
 	}
076892
-	return ((struct address_cache *) NULL);
076892
+	mutex_unlock(&rpcbaddr_cache_lock);
076892
+	return copy;
076892
 }
076892
 
076892
 static void
076892
 delete_cache(addr)
076892
 	struct netbuf *addr;
076892
 {
076892
-	struct address_cache *cptr, *prevptr = NULL;
076892
+	struct address_cache *cptr = NULL, *prevptr = NULL;
076892
+
076892
+	/* LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
076892
+	mutex_lock(&rpcbaddr_cache_lock);
076892
 
076892
-	/* WRITE LOCK HELD ON ENTRY: rpcbaddr_cache_lock */
076892
 	for (cptr = front; cptr != NULL; cptr = cptr->ac_next) {
076892
 		if (!memcmp(cptr->ac_taddr->buf, addr->buf, addr->len)) {
076892
-			free(cptr->ac_host);
076892
-			free(cptr->ac_netid);
076892
-			free(cptr->ac_taddr->buf);
076892
-			free(cptr->ac_taddr);
076892
+			/* Unlink from cache. We'll destroy it after releasing the mutex. */
076892
 			if (cptr->ac_uaddr)
076892
 				free(cptr->ac_uaddr);
076892
 			if (prevptr)
076892
 				prevptr->ac_next = cptr->ac_next;
076892
 			else
076892
 				front = cptr->ac_next;
076892
-			free(cptr);
076892
 			cachesize--;
076892
 			break;
076892
 		}
076892
 		prevptr = cptr;
076892
 	}
076892
+
076892
+	mutex_unlock(&rpcbaddr_cache_lock);
076892
+	destroy_addr(cptr);
076892
 }
076892
 
076892
 static void
076892
@@ -217,7 +297,7 @@ add_cache(host, netid, taddr, uaddr)
076892
 
076892
 /* VARIABLES PROTECTED BY rpcbaddr_cache_lock:  cptr */
076892
 
076892
-	rwlock_wrlock(&rpcbaddr_cache_lock);
076892
+	mutex_lock(&rpcbaddr_cache_lock);
076892
 	if (cachesize < CACHESIZE) {
076892
 		ad_cache->ac_next = front;
076892
 		front = ad_cache;
076892
@@ -250,7 +330,7 @@ add_cache(host, netid, taddr, uaddr)
076892
 		}
076892
 		free(cptr);
076892
 	}
076892
-	rwlock_unlock(&rpcbaddr_cache_lock);
076892
+	mutex_unlock(&rpcbaddr_cache_lock);
076892
 	return;
076892
 
076892
 out_free:
076892
@@ -261,6 +341,7 @@ out_free:
076892
 	free(ad_cache);
076892
 }
076892
 
076892
+
076892
 /*
076892
  * This routine will return a client handle that is connected to the
076892
  * rpcbind. If targaddr is non-NULL, the "universal address" of the
076892
@@ -275,11 +356,9 @@ getclnthandle(host, nconf, targaddr)
076892
 	char **targaddr;
076892
 {
076892
 	CLIENT *client;
076892
-	struct netbuf *addr, taddr;
076892
-	struct netbuf addr_to_delete;
076892
+	struct netbuf taddr;
076892
 	struct __rpc_sockinfo si;
076892
 	struct addrinfo hints, *res, *tres;
076892
-	struct address_cache *ad_cache;
076892
 	char *tmpaddr;
076892
 
076892
 	if (nconf == NULL) {
076892
@@ -294,47 +373,35 @@ getclnthandle(host, nconf, targaddr)
076892
 		return NULL;
076892
 	}
076892
 
076892
-/* VARIABLES PROTECTED BY rpcbaddr_cache_lock:  ad_cache */
076892
+
076892
 
076892
 	/* Get the address of the rpcbind.  Check cache first */
076892
 	client = NULL;
076892
 	if (targaddr)
076892
 		*targaddr = NULL;
076892
-	addr_to_delete.len = 0;
076892
-	rwlock_rdlock(&rpcbaddr_cache_lock);
076892
-	ad_cache = NULL;
076892
-
076892
-	if (host != NULL)
076892
-		ad_cache = check_cache(host, nconf->nc_netid);
076892
-	if (ad_cache != NULL) {
076892
-		addr = ad_cache->ac_taddr;
076892
-		client = clnt_tli_create(RPC_ANYFD, nconf, addr,
076892
-		    (rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
076892
-		if (client != NULL) {
076892
-			if (targaddr && ad_cache->ac_uaddr)
076892
-				*targaddr = strdup(ad_cache->ac_uaddr);
076892
-			rwlock_unlock(&rpcbaddr_cache_lock);
076892
-			return (client);
076892
-		}
076892
-		addr_to_delete.len = addr->len;
076892
-		addr_to_delete.buf = (char *)malloc(addr->len);
076892
-		if (addr_to_delete.buf == NULL) {
076892
-			addr_to_delete.len = 0;
076892
-		} else {
076892
-			memcpy(addr_to_delete.buf, addr->buf, addr->len);
076892
+
076892
+	if (host != NULL)  {
076892
+		struct address_cache *ad_cache;
076892
+
076892
+		/* Get an MT-safe copy of the cached address (if any) */
076892
+		ad_cache = copy_of_cached(host, nconf->nc_netid);
076892
+		if (ad_cache != NULL) {
076892
+			client = clnt_tli_create(RPC_ANYFD, nconf, ad_cache->ac_taddr,
076892
+							(rpcprog_t)RPCBPROG, (rpcvers_t)RPCBVERS4, 0, 0);
076892
+			if (client != NULL) {
076892
+				if (targaddr && ad_cache->ac_uaddr) {
076892
+					*targaddr = ad_cache->ac_uaddr;
076892
+					ad_cache->ac_uaddr = NULL; /* De-reference before destruction */
076892
+				}
076892
+				destroy_addr(ad_cache);
076892
+				return (client);
076892
+			}
076892
+
076892
+			delete_cache(ad_cache->ac_taddr);
076892
+			destroy_addr(ad_cache);
076892
 		}
076892
 	}
076892
-	rwlock_unlock(&rpcbaddr_cache_lock);
076892
-	if (addr_to_delete.len != 0) {
076892
-		/*
076892
-		 * Assume this may be due to cache data being
076892
-		 *  outdated
076892
-		 */
076892
-		rwlock_wrlock(&rpcbaddr_cache_lock);
076892
-		delete_cache(&addr_to_delete);
076892
-		rwlock_unlock(&rpcbaddr_cache_lock);
076892
-		free(addr_to_delete.buf);
076892
-	}
076892
+
076892
 	if (!__rpc_nconf2sockinfo(nconf, &si)) {
076892
 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
076892
 		assert(client == NULL);
076892
diff --git a/tirpc/reentrant.h b/tirpc/reentrant.h
076892
index 5f5c96e..5bb581a 100644
076892
--- a/tirpc/reentrant.h
076892
+++ b/tirpc/reentrant.h
076892
@@ -57,6 +57,7 @@
076892
 #define mutex_unlock(m)		pthread_mutex_unlock(m)
076892
 
076892
 #define cond_init(c, a, p)	pthread_cond_init(c, a)
076892
+#define cond_destroy(c)		pthread_cond_destroy(c)
076892
 #define cond_signal(m)		pthread_cond_signal(m)
076892
 #define cond_broadcast(m)	pthread_cond_broadcast(m)
076892
 #define cond_wait(c, m)		pthread_cond_wait(c, m)