Sun Nov 8 02:46:53 2015 UTC ()
Only do the compat cruft if we are compiling libc.


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

cvs diff -r1.13 -r1.14 src/lib/libc/rpc/rpc_commondata.c (switch to unified diff)

--- src/lib/libc/rpc/rpc_commondata.c 2015/11/07 14:21:32 1.13
+++ src/lib/libc/rpc/rpc_commondata.c 2015/11/08 02:46:53 1.14
@@ -1,58 +1,60 @@ @@ -1,58 +1,60 @@
1/* $NetBSD: rpc_commondata.c,v 1.13 2015/11/07 14:21:32 christos Exp $ */ 1/* $NetBSD: rpc_commondata.c,v 1.14 2015/11/08 02:46:53 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
15 * provided with the distribution. 15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its 16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived 17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission. 18 * from this software without specific prior written permission.
19 * 19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
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#include <sys/cdefs.h> 34#include <sys/cdefs.h>
35#if defined(LIBC_SCCS) && !defined(lint) 35#if defined(LIBC_SCCS) && !defined(lint)
36#if 0 36#if 0
37static char *sccsid = "@(#)rpc_commondata.c 2.1 88/07/29 4.0 RPCSRC"; 37static char *sccsid = "@(#)rpc_commondata.c 2.1 88/07/29 4.0 RPCSRC";
38#else 38#else
39__RCSID("$NetBSD: rpc_commondata.c,v 1.13 2015/11/07 14:21:32 christos Exp $"); 39__RCSID("$NetBSD: rpc_commondata.c,v 1.14 2015/11/08 02:46:53 christos Exp $");
40#endif 40#endif
41#endif 41#endif
42 42
43#include <rpc/rpc.h> 43#include <rpc/rpc.h>
44#include "svc_fdset.h" 44#include "svc_fdset.h"
45 45
46/* 46/*
47 * This file should only contain common data (global data) that is exported 47 * This file should only contain common data (global data) that is exported
48 * by public interfaces  48 * by public interfaces
49 */ 49 */
50struct opaque_auth _null_auth; 50struct opaque_auth _null_auth;
 51#ifdef _LIBC
51#undef svc_fdset 52#undef svc_fdset
52__fd_set_256 svc_fdset; 53__fd_set_256 svc_fdset;
53#undef svc_maxfd 54#undef svc_maxfd
54int svc_maxfd = -1; 55int svc_maxfd = -1;
 56#endif
55#ifndef _REENTRANT 57#ifndef _REENTRANT
56#undef rpc_createerr 58#undef rpc_createerr
57struct rpc_createerr rpc_createerr; 59struct rpc_createerr rpc_createerr;
58#endif 60#endif

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

--- src/lib/libc/rpc/svc_fdset.c 2015/11/07 23:17:09 1.10
+++ src/lib/libc/rpc/svc_fdset.c 2015/11/08 02:46:53 1.11
@@ -1,451 +1,455 @@ @@ -1,451 +1,455 @@
1/* $NetBSD: svc_fdset.c,v 1.10 2015/11/07 23:17:09 christos Exp $ */ 1/* $NetBSD: svc_fdset.c,v 1.11 2015/11/08 02:46:53 christos Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2015 The NetBSD Foundation, Inc. 4 * Copyright (c) 2015 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christos Zoulas. 8 * by Christos Zoulas.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the 16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution. 17 * documentation and/or other materials provided with the distribution.
18 * 18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 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 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 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 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 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 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 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33__RCSID("$NetBSD: svc_fdset.c,v 1.10 2015/11/07 23:17:09 christos Exp $"); 33__RCSID("$NetBSD: svc_fdset.c,v 1.11 2015/11/08 02:46:53 christos Exp $");
34 34
35 35
36#include "reentrant.h" 36#include "reentrant.h"
37 37
38#include <sys/fd_set.h> 38#include <sys/fd_set.h>
39 39
40#include <rpc/rpc.h> 40#include <rpc/rpc.h>
41 41
42#ifdef FDSET_DEBUG 42#ifdef FDSET_DEBUG
43#include <stdio.h> 43#include <stdio.h>
44#include <stdarg.h> 44#include <stdarg.h>
45#include <unistd.h> 45#include <unistd.h>
46#include <lwp.h> 46#include <lwp.h>
47#endif 47#endif
48#include <stdlib.h> 48#include <stdlib.h>
49#include <string.h> 49#include <string.h>
50#include <poll.h> 50#include <poll.h>
51 51
52#include "svc_fdset.h" 52#include "svc_fdset.h"
53 53
54#undef svc_fdset 54#undef svc_fdset
55#undef svc_maxfd 55#undef svc_maxfd
 56#ifdef _LIBC
56extern __fd_set_256 svc_fdset; 57extern __fd_set_256 svc_fdset;
 58#endif
57extern int svc_maxfd; 59extern int svc_maxfd;
58int __svc_flags; 60int __svc_flags;
59 61
60struct svc_fdset { 62struct svc_fdset {
61 /* select */ 63 /* select */
62 fd_set *fdset; 64 fd_set *fdset;
63 int fdmax; 65 int fdmax;
64 int fdsize; 66 int fdsize;
65 /* poll */ 67 /* poll */
66 struct pollfd *fdp; 68 struct pollfd *fdp;
67 int fdnum; 69 int fdnum;
68 int fdused; 70 int fdused;
69}; 71};
70 72
71/* The single threaded, one global fd_set version */ 73/* The single threaded, one global fd_set version */
72static struct svc_fdset __svc_fdset; 74static struct svc_fdset __svc_fdset;
73 75
74static thread_key_t fdsetkey = -2; 76static thread_key_t fdsetkey = -2;
75 77
76#ifdef FDSET_DEBUG 78#ifdef FDSET_DEBUG
77 79
78static void __printflike(3, 0) 80static void __printflike(3, 0)
79svc_header(const char *func, size_t line, const char *fmt, va_list ap) 81svc_header(const char *func, size_t line, const char *fmt, va_list ap)
80{ 82{
81 fprintf(stderr, "%s[%d.%d]: %s, %zu: ", getprogname(), (int)getpid(), 83 fprintf(stderr, "%s[%d.%d]: %s, %zu: ", getprogname(), (int)getpid(),
82 (int)_lwp_self(), func, line); 84 (int)_lwp_self(), func, line);
83 vfprintf(stderr, fmt, ap); 85 vfprintf(stderr, fmt, ap);
84 va_end(ap); 86 va_end(ap);
85} 87}
86 88
87static void __printflike(4, 5) 89static void __printflike(4, 5)
88svc_fdset_print(const char *func, size_t line, struct svc_fdset *fds,  90svc_fdset_print(const char *func, size_t line, struct svc_fdset *fds,
89 const char *fmt, ...) 91 const char *fmt, ...)
90{ 92{
91 va_list ap; 93 va_list ap;
92 const char *did = ""; 94 const char *did = "";
93 95
94 va_start(ap, fmt); 96 va_start(ap, fmt);
95 svc_header(func, line, fmt, ap); 97 svc_header(func, line, fmt, ap);
96 va_end(ap); 98 va_end(ap);
97 99
98 fprintf(stderr, "%p[%d] <", fds->fdset, fds->fdmax); 100 fprintf(stderr, "%p[%d] <", fds->fdset, fds->fdmax);
99 for (int i = 0; i <= fds->fdmax; i++) { 101 for (int i = 0; i <= fds->fdmax; i++) {
100 if (!FD_ISSET(i, fds->fdset)) 102 if (!FD_ISSET(i, fds->fdset))
101 continue; 103 continue;
102 fprintf(stderr, "%s%d", did, i); 104 fprintf(stderr, "%s%d", did, i);
103 did = ", "; 105 did = ", ";
104 } 106 }
105 fprintf(stderr, ">\n"); 107 fprintf(stderr, ">\n");
106} 108}
107 109
108static void __printflike(3, 4) 110static void __printflike(3, 4)
109svc_print(const char *func, size_t line, const char *fmt, ...) 111svc_print(const char *func, size_t line, const char *fmt, ...)
110{ 112{
111 va_list ap; 113 va_list ap;
112 114
113 va_start(ap, fmt); 115 va_start(ap, fmt);
114 svc_header(func, line, fmt, ap); 116 svc_header(func, line, fmt, ap);
115 va_end(ap); 117 va_end(ap);
116 fprintf(stderr, "\n"); 118 fprintf(stderr, "\n");
117} 119}
118 120
119#define DPRINTF(...) svc_print(__func__, __LINE__, __VA_ARGS__) 121#define DPRINTF(...) svc_print(__func__, __LINE__, __VA_ARGS__)
120#define DPRINTF_FDSET(...) svc_fdset_print(__func__, __LINE__, __VA_ARGS__) 122#define DPRINTF_FDSET(...) svc_fdset_print(__func__, __LINE__, __VA_ARGS__)
121 123
122#else 124#else
123 125
124#define DPRINTF(...) 126#define DPRINTF(...)
125#define DPRINTF_FDSET(...) 127#define DPRINTF_FDSET(...)
126 128
127#endif 129#endif
128 130
129 131
130static inline void 132static inline void
131svc_fdset_sanitize(struct svc_fdset *fds) 133svc_fdset_sanitize(struct svc_fdset *fds)
132{ 134{
133 while (fds->fdmax >= 0 && !FD_ISSET(fds->fdmax, fds->fdset)) 135 while (fds->fdmax >= 0 && !FD_ISSET(fds->fdmax, fds->fdset))
134 fds->fdmax--; 136 fds->fdmax--;
 137#ifdef _LIBC
135 /* Compat update */ 138 /* Compat update */
136 if (fds == &__svc_fdset) { 139 if (fds == &__svc_fdset) {
137 svc_fdset = *(__fd_set_256 *)__svc_fdset.fdset; 140 svc_fdset = *(__fd_set_256 *)__svc_fdset.fdset;
138 svc_maxfd = __svc_fdset.fdmax; 141 svc_maxfd = __svc_fdset.fdmax;
139 } 142 }
 143#endif
140} 144}
141 145
142static void 146static void
143svc_fdset_free(void *v) 147svc_fdset_free(void *v)
144{ 148{
145 struct svc_fdset *fds = v; 149 struct svc_fdset *fds = v;
146 DPRINTF_FDSET(fds, "free"); 150 DPRINTF_FDSET(fds, "free");
147 151
148 free(fds->fdp); 152 free(fds->fdp);
149 free(fds->fdset); 153 free(fds->fdset);
150 free(fds); 154 free(fds);
151} 155}
152 156
153static void 157static void
154svc_pollfd_init(struct pollfd *pfd, int nfd) 158svc_pollfd_init(struct pollfd *pfd, int nfd)
155{ 159{
156 for (int i = 0; i < nfd; i++) { 160 for (int i = 0; i < nfd; i++) {
157 pfd[i].fd = -1; 161 pfd[i].fd = -1;
158 pfd[i].events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND; 162 pfd[i].events = POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND;
159 pfd[i].revents = 0; 163 pfd[i].revents = 0;
160 } 164 }
161} 165}
162 166
163static struct pollfd * 167static struct pollfd *
164svc_pollfd_alloc(struct svc_fdset *fds) 168svc_pollfd_alloc(struct svc_fdset *fds)
165{ 169{
166 fds->fdnum = FD_SETSIZE; 170 fds->fdnum = FD_SETSIZE;
167 fds->fdp = calloc(fds->fdnum, sizeof(*fds->fdp)); 171 fds->fdp = calloc(fds->fdnum, sizeof(*fds->fdp));
168 if (fds->fdp == NULL) 172 if (fds->fdp == NULL)
169 return NULL; 173 return NULL;
170 svc_pollfd_init(fds->fdp, fds->fdnum); 174 svc_pollfd_init(fds->fdp, fds->fdnum);
171 return fds->fdp; 175 return fds->fdp;
172} 176}
173 177
174 178
175static struct svc_fdset * 179static struct svc_fdset *
176svc_pollfd_add(int fd, struct svc_fdset *fds) 180svc_pollfd_add(int fd, struct svc_fdset *fds)
177{ 181{
178 struct pollfd *pfd; 182 struct pollfd *pfd;
179 183
180 if ((pfd = svc_pollfd_alloc(fds)) == NULL) 184 if ((pfd = svc_pollfd_alloc(fds)) == NULL)
181 return NULL; 185 return NULL;
182 186
183 for (int i = 0; i < fds->fdnum; i++) 187 for (int i = 0; i < fds->fdnum; i++)
184 if (pfd[i].fd == -1) { 188 if (pfd[i].fd == -1) {
185 if (i > fds->fdused) 189 if (i > fds->fdused)
186 fds->fdused = i + 1; 190 fds->fdused = i + 1;
187 pfd[i].fd = fd; 191 pfd[i].fd = fd;
188 return fds; 192 return fds;
189 } 193 }
190 194
191 pfd = realloc(fds->fdp, (fds->fdnum + FD_SETSIZE) * sizeof(*fds->fdp)); 195 pfd = realloc(fds->fdp, (fds->fdnum + FD_SETSIZE) * sizeof(*fds->fdp));
192 if (pfd == NULL) 196 if (pfd == NULL)
193 return NULL; 197 return NULL;
194 198
195 svc_pollfd_init(pfd + fds->fdnum, FD_SETSIZE); 199 svc_pollfd_init(pfd + fds->fdnum, FD_SETSIZE);
196 pfd[fds->fdnum].fd = fd; 200 pfd[fds->fdnum].fd = fd;
197 fds->fdused = fds->fdnum + 1; 201 fds->fdused = fds->fdnum + 1;
198 fds->fdnum += FD_SETSIZE; 202 fds->fdnum += FD_SETSIZE;
199 return fds; 203 return fds;
200} 204}
201 205
202static struct svc_fdset * 206static struct svc_fdset *
203svc_pollfd_del(int fd, struct svc_fdset *fds) 207svc_pollfd_del(int fd, struct svc_fdset *fds)
204{ 208{
205 struct pollfd *pfd; 209 struct pollfd *pfd;
206 210
207 if ((pfd = svc_pollfd_alloc(fds)) == NULL) 211 if ((pfd = svc_pollfd_alloc(fds)) == NULL)
208 return NULL; 212 return NULL;
209 213
210 for (int i = 0; i < fds->fdnum; i++) { 214 for (int i = 0; i < fds->fdnum; i++) {
211 if (pfd[i].fd != fd) 215 if (pfd[i].fd != fd)
212 continue; 216 continue;
213 217
214 pfd[i].fd = -1; 218 pfd[i].fd = -1;
215 if (i != fds->fdused - 1) 219 if (i != fds->fdused - 1)
216 return fds; 220 return fds;
217 221
218 do 222 do
219 if (pfd[i].fd != -1)  223 if (pfd[i].fd != -1)
220 break; 224 break;
221 while (--i >= 0); 225 while (--i >= 0);
222 fds->fdused = i + 1; 226 fds->fdused = i + 1;
223 return fds; 227 return fds;
224 } 228 }
225 return NULL; 229 return NULL;
226} 230}
227 231
228static struct svc_fdset * 232static struct svc_fdset *
229svc_fdset_resize(int fd, struct svc_fdset *fds) 233svc_fdset_resize(int fd, struct svc_fdset *fds)
230{ 234{
231 if (fds->fdset && fd < fds->fdsize) { 235 if (fds->fdset && fd < fds->fdsize) {
232 DPRINTF_FDSET(fds, "keeping %d < %d", fd, fds->fdsize); 236 DPRINTF_FDSET(fds, "keeping %d < %d", fd, fds->fdsize);
233 return fds; 237 return fds;
234 } 238 }
235 239
236 fd += FD_SETSIZE;  240 fd += FD_SETSIZE;
237 241
238 char *newfdset = realloc(fds->fdset, __NFD_BYTES(fd)); 242 char *newfdset = realloc(fds->fdset, __NFD_BYTES(fd));
239 if (newfdset == NULL) 243 if (newfdset == NULL)
240 return NULL; 244 return NULL;
241 245
242 memset(newfdset + __NFD_BYTES(fds->fdsize), 0, 246 memset(newfdset + __NFD_BYTES(fds->fdsize), 0,
243 __NFD_BYTES(fd) - __NFD_BYTES(fds->fdsize)); 247 __NFD_BYTES(fd) - __NFD_BYTES(fds->fdsize));
244 248
245 249
246 fds->fdset = (void *)newfdset; 250 fds->fdset = (void *)newfdset;
247 DPRINTF_FDSET(fds, "resize %d > %d", fd, fds->fdsize); 251 DPRINTF_FDSET(fds, "resize %d > %d", fd, fds->fdsize);
248 fds->fdsize = fd; 252 fds->fdsize = fd;
249 253
250 return fds; 254 return fds;
251} 255}
252 256
253static struct svc_fdset * 257static struct svc_fdset *
254svc_fdset_alloc(int fd) 258svc_fdset_alloc(int fd)
255{ 259{
256 struct svc_fdset *fds; 260 struct svc_fdset *fds;
257 261
258 if (!__isthreaded || fdsetkey == -2) 262 if (!__isthreaded || fdsetkey == -2)
259 return svc_fdset_resize(fd, &__svc_fdset); 263 return svc_fdset_resize(fd, &__svc_fdset);
260 264
261 if (fdsetkey == -1) 265 if (fdsetkey == -1)
262 thr_keycreate(&fdsetkey, svc_fdset_free); 266 thr_keycreate(&fdsetkey, svc_fdset_free);
263 267
264 if ((fds = thr_getspecific(fdsetkey)) == NULL) { 268 if ((fds = thr_getspecific(fdsetkey)) == NULL) {
265 269
266 fds = calloc(1, sizeof(*fds)); 270 fds = calloc(1, sizeof(*fds));
267 if (fds == NULL) 271 if (fds == NULL)
268 return NULL; 272 return NULL;
269 273
270 (void)thr_setspecific(fdsetkey, fds); 274 (void)thr_setspecific(fdsetkey, fds);
271 275
272 if (__svc_fdset.fdsize != 0) { 276 if (__svc_fdset.fdsize != 0) {
273 *fds = __svc_fdset; 277 *fds = __svc_fdset;
274 DPRINTF("switching to %p", fds->fdset); 278 DPRINTF("switching to %p", fds->fdset);
275 } else { 279 } else {
276 DPRINTF("first thread time %p", fds->fdset); 280 DPRINTF("first thread time %p", fds->fdset);
277 } 281 }
278 } else { 282 } else {
279 DPRINTF("again for %p", fds->fdset); 283 DPRINTF("again for %p", fds->fdset);
280 if (fd < fds->fdsize) 284 if (fd < fds->fdsize)
281 return fds; 285 return fds;
282 } 286 }
283 287
284 return svc_fdset_resize(fd, fds); 288 return svc_fdset_resize(fd, fds);
285} 289}
286 290
287/* allow each thread to have their own copy */ 291/* allow each thread to have their own copy */
288void 292void
289svc_fdset_init(int flags) 293svc_fdset_init(int flags)
290{ 294{
291 DPRINTF("%x", flags); 295 DPRINTF("%x", flags);
292 __svc_flags = flags; 296 __svc_flags = flags;
293 if ((flags & SVC_FDSET_MT) && fdsetkey == -2) 297 if ((flags & SVC_FDSET_MT) && fdsetkey == -2)
294 fdsetkey = -1; 298 fdsetkey = -1;
295} 299}
296 300
297void 301void
298svc_fdset_zero(void) 302svc_fdset_zero(void)
299{ 303{
300 DPRINTF("zero"); 304 DPRINTF("zero");
301 305
302 struct svc_fdset *fds = svc_fdset_alloc(0); 306 struct svc_fdset *fds = svc_fdset_alloc(0);
303 memset(fds->fdset, 0, fds->fdsize); 307 memset(fds->fdset, 0, fds->fdsize);
304 fds->fdmax = -1; 308 fds->fdmax = -1;
305 309
306 free(fds->fdp); 310 free(fds->fdp);
307 fds->fdp = NULL; 311 fds->fdp = NULL;
308 fds->fdnum = fds->fdused = 0; 312 fds->fdnum = fds->fdused = 0;
309} 313}
310 314
311int 315int
312svc_fdset_set(int fd) 316svc_fdset_set(int fd)
313{ 317{
314 struct svc_fdset *fds = svc_fdset_alloc(fd); 318 struct svc_fdset *fds = svc_fdset_alloc(fd);
315 319
316 if (fds == NULL) 320 if (fds == NULL)
317 return -1; 321 return -1;
318 322
319 FD_SET(fd, fds->fdset); 323 FD_SET(fd, fds->fdset);
320 if (fd > fds->fdmax) 324 if (fd > fds->fdmax)
321 fds->fdmax = fd; 325 fds->fdmax = fd;
322 326
323 DPRINTF_FDSET(fds, "%d", fd); 327 DPRINTF_FDSET(fds, "%d", fd);
324 328
325 svc_fdset_sanitize(fds); 329 svc_fdset_sanitize(fds);
326 return svc_pollfd_add(fd, fds) ? 0 : -1; 330 return svc_pollfd_add(fd, fds) ? 0 : -1;
327} 331}
328 332
329int 333int
330svc_fdset_isset(int fd) 334svc_fdset_isset(int fd)
331{ 335{
332 struct svc_fdset *fds = svc_fdset_alloc(fd); 336 struct svc_fdset *fds = svc_fdset_alloc(fd);
333 337
334 if (fds == NULL) 338 if (fds == NULL)
335 return -1; 339 return -1;
336 340
337 DPRINTF_FDSET(fds, "%d", fd); 341 DPRINTF_FDSET(fds, "%d", fd);
338 342
339 return FD_ISSET(fd, fds->fdset) != 0; 343 return FD_ISSET(fd, fds->fdset) != 0;
340} 344}
341 345
342int 346int
343svc_fdset_clr(int fd) 347svc_fdset_clr(int fd)
344{ 348{
345 struct svc_fdset *fds = svc_fdset_alloc(fd); 349 struct svc_fdset *fds = svc_fdset_alloc(fd);
346 350
347 if (fds == NULL) 351 if (fds == NULL)
348 return -1; 352 return -1;
349 353
350 FD_CLR(fd, fds->fdset); 354 FD_CLR(fd, fds->fdset);
351 DPRINTF_FDSET(fds, "%d", fd); 355 DPRINTF_FDSET(fds, "%d", fd);
352 356
353 svc_fdset_sanitize(fds); 357 svc_fdset_sanitize(fds);
354 return svc_pollfd_del(fd, fds) ? 0 : -1; 358 return svc_pollfd_del(fd, fds) ? 0 : -1;
355} 359}
356 360
357fd_set * 361fd_set *
358svc_fdset_copy(const fd_set *orig) 362svc_fdset_copy(const fd_set *orig)
359{ 363{
360 int size = svc_fdset_getsize(0); 364 int size = svc_fdset_getsize(0);
361 fd_set *copy = calloc(1, __NFD_BYTES(size)); 365 fd_set *copy = calloc(1, __NFD_BYTES(size));
362 if (copy == NULL) 366 if (copy == NULL)
363 return NULL; 367 return NULL;
364 if (orig) 368 if (orig)
365 memcpy(copy, orig, __NFD_BYTES(size)); 369 memcpy(copy, orig, __NFD_BYTES(size));
366 return copy; 370 return copy;
367} 371}
368 372
369fd_set * 373fd_set *
370svc_fdset_get(void) 374svc_fdset_get(void)
371{ 375{
372 struct svc_fdset *fds = svc_fdset_alloc(0); 376 struct svc_fdset *fds = svc_fdset_alloc(0);
373 377
374 if (fds == NULL) 378 if (fds == NULL)
375 return NULL; 379 return NULL;
376 380
377 DPRINTF_FDSET(fds, "get"); 381 DPRINTF_FDSET(fds, "get");
378 svc_fdset_sanitize(fds); 382 svc_fdset_sanitize(fds);
379 return fds->fdset; 383 return fds->fdset;
380} 384}
381 385
382int * 386int *
383svc_fdset_getmax(void) 387svc_fdset_getmax(void)
384{ 388{
385 struct svc_fdset *fds = svc_fdset_alloc(0); 389 struct svc_fdset *fds = svc_fdset_alloc(0);
386 390
387 if (fds == NULL) 391 if (fds == NULL)
388 return NULL; 392 return NULL;
389 393
390 DPRINTF_FDSET(fds, "getmax"); 394 DPRINTF_FDSET(fds, "getmax");
391 svc_fdset_sanitize(fds); 395 svc_fdset_sanitize(fds);
392 return &fds->fdmax; 396 return &fds->fdmax;
393} 397}
394 398
395int 399int
396svc_fdset_getsize(int fd) 400svc_fdset_getsize(int fd)
397{ 401{
398 struct svc_fdset *fds = svc_fdset_alloc(fd); 402 struct svc_fdset *fds = svc_fdset_alloc(fd);
399 403
400 if (fds == NULL) 404 if (fds == NULL)
401 return -1; 405 return -1;
402 406
403 DPRINTF_FDSET(fds, "getsize"); 407 DPRINTF_FDSET(fds, "getsize");
404 return fds->fdsize; 408 return fds->fdsize;
405} 409}
406 410
407struct pollfd * 411struct pollfd *
408svc_pollfd_copy(const struct pollfd *orig) 412svc_pollfd_copy(const struct pollfd *orig)
409{ 413{
410 int size = svc_fdset_getsize(0); 414 int size = svc_fdset_getsize(0);
411 struct pollfd *copy = calloc(size, sizeof(*orig)); 415 struct pollfd *copy = calloc(size, sizeof(*orig));
412 if (copy == NULL) 416 if (copy == NULL)
413 return NULL; 417 return NULL;
414 if (orig) 418 if (orig)
415 memcpy(copy, orig, size * sizeof(*orig)); 419 memcpy(copy, orig, size * sizeof(*orig));
416 return copy; 420 return copy;
417} 421}
418 422
419struct pollfd * 423struct pollfd *
420svc_pollfd_get(void) 424svc_pollfd_get(void)
421{ 425{
422 struct svc_fdset *fds = svc_fdset_alloc(0); 426 struct svc_fdset *fds = svc_fdset_alloc(0);
423 427
424 if (fds == NULL) 428 if (fds == NULL)
425 return NULL; 429 return NULL;
426 430
427 DPRINTF_FDSET(fds, "getpoll"); 431 DPRINTF_FDSET(fds, "getpoll");
428 return fds->fdp; 432 return fds->fdp;
429} 433}
430 434
431int * 435int *
432svc_pollfd_getmax(void) 436svc_pollfd_getmax(void)
433{ 437{
434 struct svc_fdset *fds = svc_fdset_alloc(0); 438 struct svc_fdset *fds = svc_fdset_alloc(0);
435 439
436 if (fds == NULL) 440 if (fds == NULL)
437 return NULL; 441 return NULL;
438 return &fds->fdused; 442 return &fds->fdused;
439} 443}
440 444
441int 445int
442svc_pollfd_getsize(int fd) 446svc_pollfd_getsize(int fd)
443{ 447{
444 struct svc_fdset *fds = svc_fdset_alloc(fd); 448 struct svc_fdset *fds = svc_fdset_alloc(fd);
445 449
446 if (fds == NULL) 450 if (fds == NULL)
447 return -1; 451 return -1;
448 452
449 DPRINTF_FDSET(fds, "getsize"); 453 DPRINTF_FDSET(fds, "getsize");
450 return fds->fdnum; 454 return fds->fdnum;
451} 455}

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

--- src/lib/libc/rpc/svc_fdset.h 2015/11/07 00:42:04 1.3
+++ src/lib/libc/rpc/svc_fdset.h 2015/11/08 02:46:53 1.4
@@ -1,22 +1,24 @@ @@ -1,22 +1,24 @@
1/* $NetBSD: svc_fdset.h,v 1.3 2015/11/07 00:42:04 christos Exp $ */ 1/* $NetBSD: svc_fdset.h,v 1.4 2015/11/08 02:46:53 christos Exp $ */
2 2
3# ifdef RUMP_RPC 3# ifdef RUMP_RPC
4# include <rump/rump.h> 4# include <rump/rump.h>
5# include <rump/rump_syscalls.h> 5# include <rump/rump_syscalls.h>
6# undef close 6# undef close
7# define close(a) rump_sys_close(a) 7# define close(a) rump_sys_close(a)
8# undef fcntl 8# undef fcntl
9# define fcntl(a, b, c) rump_sys_fcntl(a, b, c) 9# define fcntl(a, b, c) rump_sys_fcntl(a, b, c)
10# undef read 10# undef read
11# define read(a, b, c) rump_sys_read(a, b, c) 11# define read(a, b, c) rump_sys_read(a, b, c)
12# undef write 12# undef write
13# define write(a, b, c) rump_sys_write(a, b, c) 13# define write(a, b, c) rump_sys_write(a, b, c)
14# undef pollts 14# undef pollts
15# define pollts(a, b, c, d) rump_sys_pollts(a, b, c, d) 15# define pollts(a, b, c, d) rump_sys_pollts(a, b, c, d)
16# undef select 16# undef select
17# define select(a, b, c, d, e) rump_sys_select(a, b, c, d, e) 17# define select(a, b, c, d, e) rump_sys_select(a, b, c, d, e)
18# endif 18# endif
19 19
 20#ifdef _LIBC
20typedef struct __fd_set_256 { 21typedef struct __fd_set_256 {
21 __fd_mask fds_bits[__NFD_LEN(256)]; 22 __fd_mask fds_bits[__NFD_LEN(256)];
22} __fd_set_256; 23} __fd_set_256;
 24#endif