309840
diff --git a/src/rpc_com.h b/src/rpc_com.h
309840
index 10bec79..76badef 100644
309840
--- a/src/rpc_com.h
309840
+++ b/src/rpc_com.h
309840
@@ -61,8 +61,7 @@ void __xprt_unregister_unlocked(SVCXPRT *);
309840
 void __xprt_set_raddr(SVCXPRT *, const struct sockaddr_storage *);
309840
 
309840
 
309840
-SVCXPRT **__svc_xports;
309840
-int __svc_maxrec;
309840
+extern int __svc_maxrec;
309840
 
309840
 #ifdef __cplusplus
309840
 }
309840
diff --git a/src/svc.c b/src/svc.c
309840
index b59467b..3a8709f 100644
309840
--- a/src/svc.c
309840
+++ b/src/svc.c
309840
@@ -57,6 +57,9 @@
309840
 
309840
 #define max(a, b) (a > b ? a : b)
309840
 
309840
+SVCXPRT **__svc_xports;
309840
+int __svc_maxrec;
309840
+
309840
 /*
309840
  * The services list
309840
  * Each entry represents a set of procedures (an rpc program).
309840
@@ -191,6 +194,21 @@ __xprt_do_unregister (xprt, dolock)
309840
     rwlock_unlock (&svc_fd_lock);
309840
 }
309840
 
309840
+int
309840
+svc_open_fds()
309840
+{
309840
+	int ix;
309840
+	int nfds = 0;
309840
+
309840
+	rwlock_rdlock (&svc_fd_lock);
309840
+	for (ix = 0; ix < svc_max_pollfd; ++ix) {
309840
+		if (svc_pollfd[ix].fd != -1)
309840
+			nfds++;
309840
+	}
309840
+	rwlock_unlock (&svc_fd_lock);
309840
+	return (nfds);
309840
+}
309840
+
309840
 /*
309840
  * Add a service program to the callout list.
309840
  * The dispatch routine will be called when a rpc request for this
309840
diff --git a/src/svc_vc.c b/src/svc_vc.c
309840
index c23cd36..1729963 100644
309840
--- a/src/svc_vc.c
309840
+++ b/src/svc_vc.c
309840
@@ -64,6 +64,8 @@
309840
 
309840
 
309840
 extern rwlock_t svc_fd_lock;
309840
+extern SVCXPRT **__svc_xports;
309840
+extern int svc_open_fds();
309840
 
309840
 static SVCXPRT *makefd_xprt(int, u_int, u_int);
309840
 static bool_t rendezvous_request(SVCXPRT *, struct rpc_msg *);
309840
@@ -82,6 +84,7 @@ static void svc_vc_ops(SVCXPRT *);
309840
 static bool_t svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in);
309840
 static bool_t svc_vc_rendezvous_control (SVCXPRT *xprt, const u_int rq,
309840
 				   	     void *in);
309840
+static int __svc_destroy_idle(int timeout);
309840
 
309840
 struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */
309840
 	u_int sendsize;
309840
@@ -312,13 +315,14 @@ done:
309840
 	return (xprt);
309840
 }
309840
 
309840
+
309840
 /*ARGSUSED*/
309840
 static bool_t
309840
 rendezvous_request(xprt, msg)
309840
 	SVCXPRT *xprt;
309840
 	struct rpc_msg *msg;
309840
 {
309840
-	int sock, flags;
309840
+	int sock, flags, nfds, cnt;
309840
 	struct cf_rendezvous *r;
309840
 	struct cf_conn *cd;
309840
 	struct sockaddr_storage addr;
309840
@@ -378,6 +382,16 @@ again:
309840
 
309840
 	gettimeofday(&cd->last_recv_time, NULL);
309840
 
309840
+	nfds = svc_open_fds();
309840
+	if (nfds >= (_rpc_dtablesize() / 5) * 4) {
309840
+		/* destroy idle connections */
309840
+		cnt = __svc_destroy_idle(15);
309840
+		if (cnt == 0) {
309840
+			/* destroy least active */
309840
+			__svc_destroy_idle(0);
309840
+		}
309840
+	}
309840
+
309840
 	return (FALSE); /* there is never an rpc msg to be processed */
309840
 }
309840
 
309840
@@ -819,3 +833,49 @@ __svc_clean_idle(fd_set *fds, int timeout, bool_t cleanblock)
309840
 {
309840
 	return FALSE;
309840
 }
309840
+
309840
+static int
309840
+__svc_destroy_idle(int timeout)
309840
+{
309840
+	int i, ncleaned = 0;
309840
+	SVCXPRT *xprt, *least_active;
309840
+	struct timeval tv, tdiff, tmax;
309840
+	struct cf_conn *cd;
309840
+
309840
+	gettimeofday(&tv, NULL);
309840
+	tmax.tv_sec = tmax.tv_usec = 0;
309840
+	least_active = NULL;
309840
+	rwlock_wrlock(&svc_fd_lock);
309840
+
309840
+	for (i = 0; i <= svc_max_pollfd; i++) {
309840
+		if (svc_pollfd[i].fd == -1)
309840
+			continue;
309840
+		xprt = __svc_xports[i];
309840
+		if (xprt == NULL || xprt->xp_ops == NULL ||
309840
+			xprt->xp_ops->xp_recv != svc_vc_recv)
309840
+			continue;
309840
+		cd = (struct cf_conn *)xprt->xp_p1;
309840
+		if (!cd->nonblock)
309840
+			continue;
309840
+		if (timeout == 0) {
309840
+			timersub(&tv, &cd->last_recv_time, &tdiff);
309840
+			if (timercmp(&tdiff, &tmax, >)) {
309840
+				tmax = tdiff;
309840
+				least_active = xprt;
309840
+			}
309840
+			continue;
309840
+		}
309840
+		if (tv.tv_sec - cd->last_recv_time.tv_sec > timeout) {
309840
+			__xprt_unregister_unlocked(xprt);
309840
+			__svc_vc_dodestroy(xprt);
309840
+			ncleaned++;
309840
+		}
309840
+	}
309840
+	if (timeout == 0 && least_active != NULL) {
309840
+		__xprt_unregister_unlocked(least_active);
309840
+		__svc_vc_dodestroy(least_active);
309840
+		ncleaned++;
309840
+	}
309840
+	rwlock_unlock(&svc_fd_lock);
309840
+	return (ncleaned);
309840
+}