Module Name:    src
Committed By:   christos
Date:           Fri Nov  6 23:11:09 UTC 2015

Modified Files:
        src/lib/libc/rpc: rpc_commondata.c svc_fdset.c

Log Message:
Cleanup and simplify.
undef svc_fdset and svc_maxfd to get to the real data for the compat code.


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/lib/libc/rpc/rpc_commondata.c
cvs rdiff -u -r1.3 -r1.4 src/lib/libc/rpc/svc_fdset.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/rpc/rpc_commondata.c
diff -u src/lib/libc/rpc/rpc_commondata.c:1.10 src/lib/libc/rpc/rpc_commondata.c:1.11
--- src/lib/libc/rpc/rpc_commondata.c:1.10	Fri Nov  6 18:05:09 2015
+++ src/lib/libc/rpc/rpc_commondata.c	Fri Nov  6 18:11:09 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: rpc_commondata.c,v 1.10 2015/11/06 23:05:09 joerg Exp $	*/
+/*	$NetBSD: rpc_commondata.c,v 1.11 2015/11/06 23:11:09 christos Exp $	*/
 
 /*
  * Copyright (c) 2010, Oracle America, Inc.
@@ -38,7 +38,7 @@
 #if 0
 static char *sccsid = "@(#)rpc_commondata.c	2.1 88/07/29 4.0 RPCSRC";
 #else
-__RCSID("$NetBSD: rpc_commondata.c,v 1.10 2015/11/06 23:05:09 joerg Exp $");
+__RCSID("$NetBSD: rpc_commondata.c,v 1.11 2015/11/06 23:11:09 christos Exp $");
 #endif
 #endif
 
@@ -49,7 +49,9 @@ __RCSID("$NetBSD: rpc_commondata.c,v 1.1
  * by public interfaces 
  */
 struct opaque_auth _null_auth;
+#undef svc_fdset
 fd_set svc_fdset;
+#undef svc_maxfd
 int svc_maxfd = -1;
 #ifndef _REENTRANT
 #undef rpc_createerr

Index: src/lib/libc/rpc/svc_fdset.c
diff -u src/lib/libc/rpc/svc_fdset.c:1.3 src/lib/libc/rpc/svc_fdset.c:1.4
--- src/lib/libc/rpc/svc_fdset.c:1.3	Fri Nov  6 18:05:09 2015
+++ src/lib/libc/rpc/svc_fdset.c	Fri Nov  6 18:11:09 2015
@@ -1,7 +1,36 @@
-/*	$NetBSD: svc_fdset.c,v 1.3 2015/11/06 23:05:09 joerg Exp $	*/
+/*	$NetBSD: svc_fdset.c,v 1.4 2015/11/06 23:11:09 christos Exp $	*/
+
+/*-
+ * Copyright (c) 2015 The NetBSD Foundation, Inc.
+ * All rights resefdsed.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: svc_fdset.c,v 1.3 2015/11/06 23:05:09 joerg Exp $");
+__RCSID("$NetBSD: svc_fdset.c,v 1.4 2015/11/06 23:11:09 christos Exp $");
 
 
 #include "reentrant.h"
@@ -13,25 +42,19 @@ __RCSID("$NetBSD: svc_fdset.c,v 1.3 2015
 #include <stdlib.h>
 #include <string.h>
 
-struct my_svc_fdset {
+#undef svc_fdset
+#undef svc_maxfd
+extern fd_set svc_fdset;
+extern int svc_maxfd;
+
+struct svc_fdset {
 	fd_set *fdset;
 	int	fdmax;
 	int	fdsize;
 };
 
-
 /* The single threaded, one global fd_set version */
-static fd_set *__svc_fdset;
-static int svc_fdsize = 0;
-
-/*
- * Update the old global svc_fdset if needed for binary compatibility
- */
-#define COMPAT_UPDATE(a)				\
-	do						\
-		if ((a) == __svc_fdset)			\
-			svc_fdset = *__svc_fdset;	\
-	while (/*CONSTCOND*/0)
+static struct svc_fdset __svc_fdset;
 
 static thread_key_t fdsetkey = -2;
 
@@ -50,8 +73,8 @@ svc_header(const char *func, size_t line
 	va_end(ap);
 }
 
-static void __printflike(5, 6)
-svc_fdset_print(const char *func, size_t line, const fd_set *fds, int fdmax,
+static void __printflike(4, 5)
+svc_fdset_print(const char *func, size_t line, struct svc_fdset *fds, 
     const char *fmt, ...)
 {
 	va_list ap;
@@ -61,12 +84,9 @@ svc_fdset_print(const char *func, size_t
 	svc_header(func, line, fmt, ap);
 	va_end(ap);
 
-	if (fdmax == 0)
-		fdmax = FD_SETSIZE;
-
-	fprintf(stderr, "%p[%d] <", fds, fdmax);
-	for (int i = 0; i <= fdmax; i++) {
-		if (!FD_ISSET(i, fds))
+	fprintf(stderr, "%p[%d] <", fds->fdset, fds->fdmax);
+	for (int i = 0; i <= fds->fdmax; i++) {
+		if (!FD_ISSET(i, fds->fdset))
 			continue;
 		fprintf(stderr, "%s%d", did, i);
 		did = ", ";
@@ -85,102 +105,102 @@ svc_print(const char *func, size_t line,
 	fprintf(stderr, "\n");
 }
 
-#define DPRINTF_FDSET(...)	svc_fdset_print(__func__, __LINE__, __VA_ARGS__)
 #define DPRINTF(...)		svc_print(__func__, __LINE__, __VA_ARGS__)
+#define DPRINTF_FDSET(...)	svc_fdset_print(__func__, __LINE__, __VA_ARGS__)
+
 #else
-#define DPRINTF_FDSET(...)
+
 #define DPRINTF(...)
+#define DPRINTF_FDSET(...)
+
 #endif
 
 
+static inline void
+svc_fdset_sanitize(struct svc_fdset *fds)
+{
+	while (fds->fdmax >= 0 && !FD_ISSET(fds->fdmax, fds->fdset))
+		fds->fdmax--;
+	/* Compat update */
+	if (fds == &__svc_fdset) {
+		svc_fdset = *__svc_fdset.fdset;
+		svc_maxfd = __svc_fdset.fdmax;
+	}
+}
+
 static void
 svc_fdset_free(void *v)
 {
-	struct my_svc_fdset *rv = v;
-	DPRINTF_FDSET(rv->fdset, 0, "free");
+	struct svc_fdset *fds = v;
+	DPRINTF_FDSET(fds, "free");
 
-	free(rv->fdset);
-	free(rv);
+	free(fds->fdset);
+	free(fds);
 }
 
-static fd_set *
-svc_fdset_resize(int fd, fd_set **fdset, int *fdsize)
+static struct svc_fdset *
+svc_fdset_resize(int fd, struct svc_fdset *fds)
 {
-	if (*fdset && fd < *fdsize) {
-		DPRINTF_FDSET(*fdset, 0, "keeping %d < %d",
-		    fd, *fdsize);
-		return *fdset;
+	if (fds->fdset && fd < fds->fdsize) {
+		DPRINTF_FDSET(fds, "keeping %d < %d", fd, fds->fdsize);
+		return fds;
 	}
 
 	fd += FD_SETSIZE; 
-	if (fd == 517)
-		abort();
 
-	char *newfdset = realloc(*fdset, __NFD_BYTES(fd));
+	char *newfdset = realloc(fds->fdset, __NFD_BYTES(fd));
 	if (newfdset == NULL)
 		return NULL;
 
-	memset(newfdset + __NFD_BYTES(*fdsize), 0,
-	    __NFD_BYTES(fd) - __NFD_BYTES(*fdsize));
-
+	memset(newfdset + __NFD_BYTES(fds->fdsize), 0,
+	    __NFD_BYTES(fd) - __NFD_BYTES(fds->fdsize));
 
-	*fdset = (void *)newfdset;
-	DPRINTF_FDSET(*fdset, 0, "resize %d > %d", fd, *fdsize);
-	*fdsize = fd;
 
-	COMPAT_UPDATE(*fdset);
+	fds->fdset = (void *)newfdset;
+	DPRINTF_FDSET(fds, "resize %d > %d", fd, fds->fdsize);
+	fds->fdsize = fd;
 
-	return *fdset;
+	return fds;
 }
 
-static struct my_svc_fdset *
+static struct svc_fdset *
 svc_fdset_alloc(int fd)
 {
-	struct my_svc_fdset *rv;
+	struct svc_fdset *fds;
 
 	if (fdsetkey == -1)
 		thr_keycreate(&fdsetkey, svc_fdset_free);
 
-	if ((rv = thr_getspecific(fdsetkey)) == NULL) {
+	if ((fds = thr_getspecific(fdsetkey)) == NULL) {
 
-		rv = calloc(1, sizeof(*rv));
-		if (rv == NULL)
+		fds = calloc(1, sizeof(*fds));
+		if (fds == NULL)
 			return NULL;
 
-		(void)thr_setspecific(fdsetkey, rv);
-
-		if (svc_fdsize != 0) {
-			rv->fdset = __svc_fdset;
-			DPRINTF("switching to %p", rv->fdset);
-			rv->fdmax = svc_maxfd;
-			rv->fdsize = svc_fdsize;
+		(void)thr_setspecific(fdsetkey, fds);
 
-			svc_fdsize = 0;
+		if (__svc_fdset.fdsize != 0) {
+			*fds = __svc_fdset;
+			DPRINTF("switching to %p", fds->fdset);
 		} else {
-			DPRINTF("first thread time %p", rv->fdset);
+			DPRINTF("first thread time %p", fds->fdset);
 		}
 	} else {
-		DPRINTF("again for %p", rv->fdset);
-		if (fd < rv->fdsize)
-			return rv;
+		DPRINTF("again for %p", fds->fdset);
+		if (fd < fds->fdsize)
+			return fds;
 	}
-	if (svc_fdset_resize(fd, &rv->fdset, &rv->fdsize) == NULL)
-		return NULL;
-	return rv;
+
+	return svc_fdset_resize(fd, fds);
 }
 
-static fd_set *
+static struct svc_fdset *
 svc_fdset_get_internal(int fd)
 {
-	struct my_svc_fdset *rv;
-
 	if (!__isthreaded || fdsetkey == -2)
-		return svc_fdset_resize(fd, &__svc_fdset, &svc_fdsize);
+		return svc_fdset_resize(fd, &__svc_fdset);
 
-	rv = svc_fdset_alloc(fd);
-	if (rv == NULL)
-		return NULL;
-	return rv->fdset;
+	return svc_fdset_alloc(fd);
 }
 
 
@@ -197,104 +217,93 @@ void
 svc_fdset_zero(void)
 {
 	DPRINTF("zero");
-	fd_set *fds = svc_fdset_get_internal(0);
-	int size = svc_fdset_getsize(0);
-	memset(fds, 0, __NFD_BYTES(size));
-	*svc_fdset_getmax() = 0;
-
-	COMPAT_UPDATE(fds);
-
+	struct svc_fdset *fds = svc_fdset_get_internal(0);
+	memset(fds->fdset, 0, fds->fdsize);
+	fds->fdmax = 0;
 }
 
 void
 svc_fdset_set(int fd)
 {
-	fd_set *fds = svc_fdset_get_internal(fd);
-	int *fdmax = svc_fdset_getmax();
-	FD_SET(fd, fds);
-	if (fd > *fdmax)
-		*fdmax = fd;
-	DPRINTF_FDSET(fds, *fdmax, "%d", fd);
+	struct svc_fdset *fds = svc_fdset_get_internal(fd);
+	FD_SET(fd, fds->fdset);
+	if (fd > fds->fdmax)
+		fds->fdmax = fd;
+	DPRINTF_FDSET(fds, "%d", fd);
 
-	COMPAT_UPDATE(fds);
+	svc_fdset_sanitize(fds);
 }
 
 int
 svc_fdset_isset(int fd)
 {
-	fd_set *fds = svc_fdset_get_internal(fd);
-	DPRINTF_FDSET(fds, 0, "%d", fd);
-	return FD_ISSET(fd, fds);
+	struct svc_fdset *fds = svc_fdset_get_internal(fd);
+	svc_fdset_sanitize(fds);
+	DPRINTF_FDSET(fds, "%d", fd);
+	return FD_ISSET(fd, fds->fdset);
 }
 
 void
 svc_fdset_clr(int fd)
 {
-	fd_set *fds = svc_fdset_get_internal(fd);
-	FD_CLR(fd, fds);
-	/* XXX: update fdmax? */
-	DPRINTF_FDSET(fds, 0, "%d", fd);
-
-	COMPAT_UPDATE(fds);
+	struct svc_fdset *fds = svc_fdset_get_internal(fd);
+	FD_CLR(fd, fds->fdset);
+	svc_fdset_sanitize(fds);
+	DPRINTF_FDSET(fds, "%d", fd);
 }
 
 fd_set *
 svc_fdset_copy(const fd_set *orig)
 {
-	int len, fdmax;
-	fd_set *fds;
-	
-	len = 0;
-	fds = 0;
-	fdmax = *svc_fdset_getmax();
-
-	DPRINTF_FDSET(orig, 0, "[orig]");
-	fds = svc_fdset_resize(fdmax, &fds, &len);
-	if (fds == NULL)
+	int size = svc_fdset_getsize(0);
+	fd_set *copy = calloc(1, __NFD_BYTES(size));
+	if (copy == NULL)
 		return NULL;
-
 	if (orig)
-		memcpy(fds, orig, __NFD_BYTES(fdmax));
-	DPRINTF_FDSET(fds, 0, "[copy]");
-	return fds;
+		memcpy(copy, orig, __NFD_BYTES(size));
+	return copy;
 }
 
 fd_set *
 svc_fdset_get(void)
 {
-	fd_set *fds = svc_fdset_get_internal(0);
-	DPRINTF_FDSET(fds, 0, "get");
-	return fds;
+	struct svc_fdset *fds = svc_fdset_get_internal(0);
+	svc_fdset_sanitize(fds);
+
+	DPRINTF_FDSET(fds, "get");
+	return fds->fdset;
 }
 
 int *
 svc_fdset_getmax(void)
 {
-	struct my_svc_fdset *rv;
+	struct svc_fdset *fds;
 
-	if (!__isthreaded || fdsetkey == -2)
-		return &svc_maxfd;
+	if (!__isthreaded || fdsetkey == -2) {
+		svc_fdset_sanitize(&__svc_fdset);
+		return &__svc_fdset.fdmax;
+	}
 		
-	rv = svc_fdset_alloc(0);
-	if (rv == NULL)
+	fds = svc_fdset_alloc(0);
+	if (fds == NULL)
 		return NULL;
-	return &rv->fdmax;
+	return &fds->fdmax;
 }
 
 int
 svc_fdset_getsize(int fd)
 {
-	struct my_svc_fdset *rv;
+	struct svc_fdset *fds;
 
 	if (!__isthreaded || fdsetkey == -2) {
-		if (svc_fdset_resize(fd, &__svc_fdset, &svc_fdsize) == NULL)
+		if (svc_fdset_resize(fd, &__svc_fdset) == NULL)
 			return -1;
 		else
-			return svc_fdsize;
+			return __svc_fdset.fdsize;
 	}
 
-	rv = svc_fdset_alloc(fd);
-	if (rv == NULL)
+	fds = svc_fdset_alloc(fd);
+	if (fds == NULL)
 		return -1;
-	return rv->fdsize;
+	return fds->fdsize;
 }

Reply via email to