Fri Nov 6 23:11:09 2015 UTC ()
Cleanup and simplify.
undef svc_fdset and svc_maxfd to get to the real data for the compat code.


(christos)
diff -r1.10 -r1.11 src/lib/libc/rpc/rpc_commondata.c
diff -r1.3 -r1.4 src/lib/libc/rpc/svc_fdset.c

cvs diff -r1.10 -r1.11 src/lib/libc/rpc/rpc_commondata.c (expand / switch to unified diff)

--- src/lib/libc/rpc/rpc_commondata.c 2015/11/06 23:05:09 1.10
+++ src/lib/libc/rpc/rpc_commondata.c 2015/11/06 23:11:09 1.11
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: rpc_commondata.c,v 1.10 2015/11/06 23:05:09 joerg Exp $ */ 1/* $NetBSD: rpc_commondata.c,v 1.11 2015/11/06 23:11:09 christos Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 2010, Oracle America, Inc. 4 * Copyright (c) 2010, Oracle America, Inc.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are 7 * modification, are permitted provided that the following conditions are
8 * met: 8 * met:
9 * 9 *
10 * * Redistributions of source code must retain the above copyright 10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above 12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following 13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials 14 * disclaimer in the documentation and/or other materials
@@ -28,30 +28,32 @@ @@ -28,30 +28,32 @@
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */ 32 */
33 33
34#define SVC_LEGACY 34#define SVC_LEGACY
35 35
36#include <sys/cdefs.h> 36#include <sys/cdefs.h>
37#if defined(LIBC_SCCS) && !defined(lint) 37#if defined(LIBC_SCCS) && !defined(lint)
38#if 0 38#if 0
39static char *sccsid = "@(#)rpc_commondata.c 2.1 88/07/29 4.0 RPCSRC"; 39static char *sccsid = "@(#)rpc_commondata.c 2.1 88/07/29 4.0 RPCSRC";
40#else 40#else
41__RCSID("$NetBSD: rpc_commondata.c,v 1.10 2015/11/06 23:05:09 joerg Exp $"); 41__RCSID("$NetBSD: rpc_commondata.c,v 1.11 2015/11/06 23:11:09 christos Exp $");
42#endif 42#endif
43#endif 43#endif
44 44
45#include <rpc/rpc.h> 45#include <rpc/rpc.h>
46 46
47/* 47/*
48 * This file should only contain common data (global data) that is exported 48 * This file should only contain common data (global data) that is exported
49 * by public interfaces  49 * by public interfaces
50 */ 50 */
51struct opaque_auth _null_auth; 51struct opaque_auth _null_auth;
 52#undef svc_fdset
52fd_set svc_fdset; 53fd_set svc_fdset;
 54#undef svc_maxfd
53int svc_maxfd = -1; 55int svc_maxfd = -1;
54#ifndef _REENTRANT 56#ifndef _REENTRANT
55#undef rpc_createerr 57#undef rpc_createerr
56struct rpc_createerr rpc_createerr; 58struct rpc_createerr rpc_createerr;
57#endif 59#endif

cvs diff -r1.3 -r1.4 src/lib/libc/rpc/svc_fdset.c (expand / switch to unified diff)

--- src/lib/libc/rpc/svc_fdset.c 2015/11/06 23:05:09 1.3
+++ src/lib/libc/rpc/svc_fdset.c 2015/11/06 23:11:09 1.4
@@ -1,300 +1,309 @@ @@ -1,300 +1,309 @@
1/* $NetBSD: svc_fdset.c,v 1.3 2015/11/06 23:05:09 joerg Exp $ */ 1/* $NetBSD: svc_fdset.c,v 1.4 2015/11/06 23:11:09 christos Exp $ */
 2
 3/*-
 4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
 5 * All rights resefdsed.
 6 *
 7 * This code is derived from software contributed to The NetBSD Foundation
 8 * by Christos Zoulas.
 9 *
 10 * Redistribution and use in source and binary forms, with or without
 11 * modification, are permitted provided that the following conditions
 12 * are met:
 13 * 1. Redistributions of source code must retain the above copyright
 14 * notice, this list of conditions and the following disclaimer.
 15 * 2. Redistributions in binary form must reproduce the above copyright
 16 * notice, this list of conditions and the following disclaimer in the
 17 * documentation and/or other materials provided with the distribution.
 18 *
 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 29 * POSSIBILITY OF SUCH DAMAGE.
 30 */
2 31
3#include <sys/cdefs.h> 32#include <sys/cdefs.h>
4__RCSID("$NetBSD: svc_fdset.c,v 1.3 2015/11/06 23:05:09 joerg Exp $"); 33__RCSID("$NetBSD: svc_fdset.c,v 1.4 2015/11/06 23:11:09 christos Exp $");
5 34
6 35
7#include "reentrant.h" 36#include "reentrant.h"
8 37
9#include <sys/fd_set.h> 38#include <sys/fd_set.h>
10 39
11#include <rpc/rpc.h> 40#include <rpc/rpc.h>
12 41
13#include <stdlib.h> 42#include <stdlib.h>
14#include <string.h> 43#include <string.h>
15 44
16struct my_svc_fdset { 45#undef svc_fdset
 46#undef svc_maxfd
 47extern fd_set svc_fdset;
 48extern int svc_maxfd;
 49
 50struct svc_fdset {
17 fd_set *fdset; 51 fd_set *fdset;
18 int fdmax; 52 int fdmax;
19 int fdsize; 53 int fdsize;
20}; 54};
21 55
22 
23/* The single threaded, one global fd_set version */ 56/* The single threaded, one global fd_set version */
24static fd_set *__svc_fdset; 57static struct svc_fdset __svc_fdset;
25static int svc_fdsize = 0; 
26 
27/* 
28 * Update the old global svc_fdset if needed for binary compatibility 
29 */ 
30#define COMPAT_UPDATE(a) \ 
31 do \ 
32 if ((a) == __svc_fdset) \ 
33 svc_fdset = *__svc_fdset; \ 
34 while (/*CONSTCOND*/0) 
35 58
36static thread_key_t fdsetkey = -2; 59static thread_key_t fdsetkey = -2;
37 60
38#ifdef FDSET_DEBUG 61#ifdef FDSET_DEBUG
39#include <stdio.h> 62#include <stdio.h>
40#include <stdarg.h> 63#include <stdarg.h>
41#include <unistd.h> 64#include <unistd.h>
42#include <lwp.h> 65#include <lwp.h>
43 66
44static void __printflike(3, 0) 67static void __printflike(3, 0)
45svc_header(const char *func, size_t line, const char *fmt, va_list ap) 68svc_header(const char *func, size_t line, const char *fmt, va_list ap)
46{ 69{
47 fprintf(stderr, "%s[%d.%d]: %s, %zu: ", getprogname(), (int)getpid(), 70 fprintf(stderr, "%s[%d.%d]: %s, %zu: ", getprogname(), (int)getpid(),
48 (int)_lwp_self(), func, line); 71 (int)_lwp_self(), func, line);
49 vfprintf(stderr, fmt, ap); 72 vfprintf(stderr, fmt, ap);
50 va_end(ap); 73 va_end(ap);
51} 74}
52 75
53static void __printflike(5, 6) 76static void __printflike(4, 5)
54svc_fdset_print(const char *func, size_t line, const fd_set *fds, int fdmax, 77svc_fdset_print(const char *func, size_t line, struct svc_fdset *fds,
55 const char *fmt, ...) 78 const char *fmt, ...)
56{ 79{
57 va_list ap; 80 va_list ap;
58 const char *did = ""; 81 const char *did = "";
59 82
60 va_start(ap, fmt); 83 va_start(ap, fmt);
61 svc_header(func, line, fmt, ap); 84 svc_header(func, line, fmt, ap);
62 va_end(ap); 85 va_end(ap);
63 86
64 if (fdmax == 0) 87 fprintf(stderr, "%p[%d] <", fds->fdset, fds->fdmax);
65 fdmax = FD_SETSIZE; 88 for (int i = 0; i <= fds->fdmax; i++) {
66 89 if (!FD_ISSET(i, fds->fdset))
67 fprintf(stderr, "%p[%d] <", fds, fdmax); 
68 for (int i = 0; i <= fdmax; i++) { 
69 if (!FD_ISSET(i, fds)) 
70 continue; 90 continue;
71 fprintf(stderr, "%s%d", did, i); 91 fprintf(stderr, "%s%d", did, i);
72 did = ", "; 92 did = ", ";
73 } 93 }
74 fprintf(stderr, ">\n"); 94 fprintf(stderr, ">\n");
75} 95}
76 96
77static void __printflike(3, 4) 97static void __printflike(3, 4)
78svc_print(const char *func, size_t line, const char *fmt, ...) 98svc_print(const char *func, size_t line, const char *fmt, ...)
79{ 99{
80 va_list ap; 100 va_list ap;
81 101
82 va_start(ap, fmt); 102 va_start(ap, fmt);
83 svc_header(func, line, fmt, ap); 103 svc_header(func, line, fmt, ap);
84 va_end(ap); 104 va_end(ap);
85 fprintf(stderr, "\n"); 105 fprintf(stderr, "\n");
86} 106}
87 107
88#define DPRINTF_FDSET(...) svc_fdset_print(__func__, __LINE__, __VA_ARGS__) 
89#define DPRINTF(...) svc_print(__func__, __LINE__, __VA_ARGS__) 108#define DPRINTF(...) svc_print(__func__, __LINE__, __VA_ARGS__)
 109#define DPRINTF_FDSET(...) svc_fdset_print(__func__, __LINE__, __VA_ARGS__)
 110
90#else 111#else
91#define DPRINTF_FDSET(...) 112
92#define DPRINTF(...) 113#define DPRINTF(...)
 114#define DPRINTF_FDSET(...)
 115
93#endif 116#endif
94 117
95 118
 119static inline void
 120svc_fdset_sanitize(struct svc_fdset *fds)
 121{
 122 while (fds->fdmax >= 0 && !FD_ISSET(fds->fdmax, fds->fdset))
 123 fds->fdmax--;
 124 /* Compat update */
 125 if (fds == &__svc_fdset) {
 126 svc_fdset = *__svc_fdset.fdset;
 127 svc_maxfd = __svc_fdset.fdmax;
 128 }
 129}
 130
96static void 131static void
97svc_fdset_free(void *v) 132svc_fdset_free(void *v)
98{ 133{
99 struct my_svc_fdset *rv = v; 134 struct svc_fdset *fds = v;
100 DPRINTF_FDSET(rv->fdset, 0, "free"); 135 DPRINTF_FDSET(fds, "free");
101 136
102 free(rv->fdset); 137 free(fds->fdset);
103 free(rv); 138 free(fds);
104} 139}
105 140
106static fd_set * 141static struct svc_fdset *
107svc_fdset_resize(int fd, fd_set **fdset, int *fdsize) 142svc_fdset_resize(int fd, struct svc_fdset *fds)
108{ 143{
109 if (*fdset && fd < *fdsize) { 144 if (fds->fdset && fd < fds->fdsize) {
110 DPRINTF_FDSET(*fdset, 0, "keeping %d < %d", 145 DPRINTF_FDSET(fds, "keeping %d < %d", fd, fds->fdsize);
111 fd, *fdsize); 146 return fds;
112 return *fdset; 
113 } 147 }
114 148
115 fd += FD_SETSIZE;  149 fd += FD_SETSIZE;
116 if (fd == 517) 
117 abort(); 
118 150
119 char *newfdset = realloc(*fdset, __NFD_BYTES(fd)); 151 char *newfdset = realloc(fds->fdset, __NFD_BYTES(fd));
120 if (newfdset == NULL) 152 if (newfdset == NULL)
121 return NULL; 153 return NULL;
122 154
123 memset(newfdset + __NFD_BYTES(*fdsize), 0, 155 memset(newfdset + __NFD_BYTES(fds->fdsize), 0,
124 __NFD_BYTES(fd) - __NFD_BYTES(*fdsize)); 156 __NFD_BYTES(fd) - __NFD_BYTES(fds->fdsize));
125 
126 157
127 *fdset = (void *)newfdset; 
128 DPRINTF_FDSET(*fdset, 0, "resize %d > %d", fd, *fdsize); 
129 *fdsize = fd; 
130 158
131 COMPAT_UPDATE(*fdset); 159 fds->fdset = (void *)newfdset;
 160 DPRINTF_FDSET(fds, "resize %d > %d", fd, fds->fdsize);
 161 fds->fdsize = fd;
132 162
133 return *fdset; 163 return fds;
134} 164}
135 165
136static struct my_svc_fdset * 166static struct svc_fdset *
137svc_fdset_alloc(int fd) 167svc_fdset_alloc(int fd)
138{ 168{
139 struct my_svc_fdset *rv; 169 struct svc_fdset *fds;
140 170
141 if (fdsetkey == -1) 171 if (fdsetkey == -1)
142 thr_keycreate(&fdsetkey, svc_fdset_free); 172 thr_keycreate(&fdsetkey, svc_fdset_free);
143 173
144 if ((rv = thr_getspecific(fdsetkey)) == NULL) { 174 if ((fds = thr_getspecific(fdsetkey)) == NULL) {
145 175
146 rv = calloc(1, sizeof(*rv)); 176 fds = calloc(1, sizeof(*fds));
147 if (rv == NULL) 177 if (fds == NULL)
148 return NULL; 178 return NULL;
149 179
150 (void)thr_setspecific(fdsetkey, rv); 180 (void)thr_setspecific(fdsetkey, fds);
151 
152 if (svc_fdsize != 0) { 
153 rv->fdset = __svc_fdset; 
154 DPRINTF("switching to %p", rv->fdset); 
155 rv->fdmax = svc_maxfd; 
156 rv->fdsize = svc_fdsize; 
157 181
158 svc_fdsize = 0; 182 if (__svc_fdset.fdsize != 0) {
 183 *fds = __svc_fdset;
 184 DPRINTF("switching to %p", fds->fdset);
159 } else { 185 } else {
160 DPRINTF("first thread time %p", rv->fdset); 186 DPRINTF("first thread time %p", fds->fdset);
161 } 187 }
162 } else { 188 } else {
163 DPRINTF("again for %p", rv->fdset); 189 DPRINTF("again for %p", fds->fdset);
164 if (fd < rv->fdsize) 190 if (fd < fds->fdsize)
165 return rv; 191 return fds;
166 } 192 }
167 if (svc_fdset_resize(fd, &rv->fdset, &rv->fdsize) == NULL) 193
168 return NULL; 194 return svc_fdset_resize(fd, fds);
169 return rv; 
170} 195}
171 196
172static fd_set * 197static struct svc_fdset *
173svc_fdset_get_internal(int fd) 198svc_fdset_get_internal(int fd)
174{ 199{
175 struct my_svc_fdset *rv; 
176 
177 if (!__isthreaded || fdsetkey == -2) 200 if (!__isthreaded || fdsetkey == -2)
178 return svc_fdset_resize(fd, &__svc_fdset, &svc_fdsize); 201 return svc_fdset_resize(fd, &__svc_fdset);
179 202
180 rv = svc_fdset_alloc(fd); 203 return svc_fdset_alloc(fd);
181 if (rv == NULL) 
182 return NULL; 
183 return rv->fdset; 
184} 204}
185 205
186 206
187/* allow each thread to have their own copy */ 207/* allow each thread to have their own copy */
188void 208void
189svc_fdset_init(int flags) 209svc_fdset_init(int flags)
190{ 210{
191 DPRINTF("%x", flags); 211 DPRINTF("%x", flags);
192 if ((flags & SVC_FDSET_MT) && fdsetkey == -2) 212 if ((flags & SVC_FDSET_MT) && fdsetkey == -2)
193 fdsetkey = -1; 213 fdsetkey = -1;
194} 214}
195 215
196void 216void
197svc_fdset_zero(void) 217svc_fdset_zero(void)
198{ 218{
199 DPRINTF("zero"); 219 DPRINTF("zero");
200 fd_set *fds = svc_fdset_get_internal(0); 220 struct svc_fdset *fds = svc_fdset_get_internal(0);
201 int size = svc_fdset_getsize(0); 221 memset(fds->fdset, 0, fds->fdsize);
202 memset(fds, 0, __NFD_BYTES(size)); 222 fds->fdmax = 0;
203 *svc_fdset_getmax() = 0; 
204 
205 COMPAT_UPDATE(fds); 
206 
207} 223}
208 224
209void 225void
210svc_fdset_set(int fd) 226svc_fdset_set(int fd)
211{ 227{
212 fd_set *fds = svc_fdset_get_internal(fd); 228 struct svc_fdset *fds = svc_fdset_get_internal(fd);
213 int *fdmax = svc_fdset_getmax(); 229 FD_SET(fd, fds->fdset);
214 FD_SET(fd, fds); 230 if (fd > fds->fdmax)
215 if (fd > *fdmax) 231 fds->fdmax = fd;
216 *fdmax = fd; 232 DPRINTF_FDSET(fds, "%d", fd);
217 DPRINTF_FDSET(fds, *fdmax, "%d", fd); 
218 233
219 COMPAT_UPDATE(fds); 234 svc_fdset_sanitize(fds);
220} 235}
221 236
222int 237int
223svc_fdset_isset(int fd) 238svc_fdset_isset(int fd)
224{ 239{
225 fd_set *fds = svc_fdset_get_internal(fd); 240 struct svc_fdset *fds = svc_fdset_get_internal(fd);
226 DPRINTF_FDSET(fds, 0, "%d", fd); 241 svc_fdset_sanitize(fds);
227 return FD_ISSET(fd, fds); 242 DPRINTF_FDSET(fds, "%d", fd);
 243 return FD_ISSET(fd, fds->fdset);
228} 244}
229 245
230void 246void
231svc_fdset_clr(int fd) 247svc_fdset_clr(int fd)
232{ 248{
233 fd_set *fds = svc_fdset_get_internal(fd); 249 struct svc_fdset *fds = svc_fdset_get_internal(fd);
234 FD_CLR(fd, fds); 250 FD_CLR(fd, fds->fdset);
235 /* XXX: update fdmax? */ 251 svc_fdset_sanitize(fds);
236 DPRINTF_FDSET(fds, 0, "%d", fd); 252 DPRINTF_FDSET(fds, "%d", fd);
237 
238 COMPAT_UPDATE(fds); 
239} 253}
240 254
241fd_set * 255fd_set *
242svc_fdset_copy(const fd_set *orig) 256svc_fdset_copy(const fd_set *orig)
243{ 257{
244 int len, fdmax; 258 int size = svc_fdset_getsize(0);
245 fd_set *fds; 259 fd_set *copy = calloc(1, __NFD_BYTES(size));
246  260 if (copy == NULL)
247 len = 0; 
248 fds = 0; 
249 fdmax = *svc_fdset_getmax(); 
250 
251 DPRINTF_FDSET(orig, 0, "[orig]"); 
252 fds = svc_fdset_resize(fdmax, &fds, &len); 
253 if (fds == NULL) 
254 return NULL; 261 return NULL;
255 
256 if (orig) 262 if (orig)
257 memcpy(fds, orig, __NFD_BYTES(fdmax)); 263 memcpy(copy, orig, __NFD_BYTES(size));
258 DPRINTF_FDSET(fds, 0, "[copy]"); 264 return copy;
259 return fds; 
260} 265}
261 266
262fd_set * 267fd_set *
263svc_fdset_get(void) 268svc_fdset_get(void)
264{ 269{
265 fd_set *fds = svc_fdset_get_internal(0); 270 struct svc_fdset *fds = svc_fdset_get_internal(0);
266 DPRINTF_FDSET(fds, 0, "get"); 271 svc_fdset_sanitize(fds);
267 return fds; 272
 273 DPRINTF_FDSET(fds, "get");
 274 return fds->fdset;
268} 275}
269 276
270int * 277int *
271svc_fdset_getmax(void) 278svc_fdset_getmax(void)
272{ 279{
273 struct my_svc_fdset *rv; 280 struct svc_fdset *fds;
274 281
275 if (!__isthreaded || fdsetkey == -2) 282 if (!__isthreaded || fdsetkey == -2) {
276 return &svc_maxfd; 283 svc_fdset_sanitize(&__svc_fdset);
 284 return &__svc_fdset.fdmax;
 285 }
277  286
278 rv = svc_fdset_alloc(0); 287 fds = svc_fdset_alloc(0);
279 if (rv == NULL) 288 if (fds == NULL)
280 return NULL; 289 return NULL;
281 return &rv->fdmax; 290 return &fds->fdmax;
282} 291}
283 292
284int 293int
285svc_fdset_getsize(int fd) 294svc_fdset_getsize(int fd)
286{ 295{
287 struct my_svc_fdset *rv; 296 struct svc_fdset *fds;
288 297
289 if (!__isthreaded || fdsetkey == -2) { 298 if (!__isthreaded || fdsetkey == -2) {
290 if (svc_fdset_resize(fd, &__svc_fdset, &svc_fdsize) == NULL) 299 if (svc_fdset_resize(fd, &__svc_fdset) == NULL)
291 return -1; 300 return -1;
292 else 301 else
293 return svc_fdsize; 302 return __svc_fdset.fdsize;
294 } 303 }
295 304
296 rv = svc_fdset_alloc(fd); 305 fds = svc_fdset_alloc(fd);
297 if (rv == NULL) 306 if (fds == NULL)
298 return -1; 307 return -1;
299 return rv->fdsize; 308 return fds->fdsize;
300} 309}