| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: svc_fdset.c,v 1.6 2015/11/07 03:06:32 christos Exp $ */ | | 1 | /* $NetBSD: svc_fdset.c,v 1.7 2015/11/07 16:58:24 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 resefdsed. | | 5 | * All rights resefdsed. |
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. |
| @@ -20,27 +20,27 @@ | | | @@ -20,27 +20,27 @@ |
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.6 2015/11/07 03:06:32 christos Exp $"); | | 33 | __RCSID("$NetBSD: svc_fdset.c,v 1.7 2015/11/07 16:58:24 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> |
| @@ -209,88 +209,108 @@ svc_fdset_init(int flags) | | | @@ -209,88 +209,108 @@ svc_fdset_init(int flags) |
209 | if ((flags & SVC_FDSET_MT) && fdsetkey == -2) | | 209 | if ((flags & SVC_FDSET_MT) && fdsetkey == -2) |
210 | fdsetkey = -1; | | 210 | fdsetkey = -1; |
211 | } | | 211 | } |
212 | | | 212 | |
213 | void | | 213 | void |
214 | svc_fdset_zero(void) | | 214 | svc_fdset_zero(void) |
215 | { | | 215 | { |
216 | DPRINTF("zero"); | | 216 | DPRINTF("zero"); |
217 | struct svc_fdset *fds = svc_fdset_alloc(0); | | 217 | struct svc_fdset *fds = svc_fdset_alloc(0); |
218 | memset(fds->fdset, 0, fds->fdsize); | | 218 | memset(fds->fdset, 0, fds->fdsize); |
219 | fds->fdmax = -1; | | 219 | fds->fdmax = -1; |
220 | } | | 220 | } |
221 | | | 221 | |
222 | void | | 222 | int |
223 | svc_fdset_set(int fd) | | 223 | svc_fdset_set(int fd) |
224 | { | | 224 | { |
225 | struct svc_fdset *fds = svc_fdset_alloc(fd); | | 225 | struct svc_fdset *fds = svc_fdset_alloc(fd); |
226 | | | 226 | |
| | | 227 | if (fds == NULL) |
| | | 228 | return -1; |
| | | 229 | |
227 | FD_SET(fd, fds->fdset); | | 230 | FD_SET(fd, fds->fdset); |
228 | if (fd > fds->fdmax) | | 231 | if (fd > fds->fdmax) |
229 | fds->fdmax = fd; | | 232 | fds->fdmax = fd; |
230 | | | 233 | |
231 | DPRINTF_FDSET(fds, "%d", fd); | | 234 | DPRINTF_FDSET(fds, "%d", fd); |
232 | | | 235 | |
233 | svc_fdset_sanitize(fds); | | 236 | svc_fdset_sanitize(fds); |
| | | 237 | return 0; |
234 | } | | 238 | } |
235 | | | 239 | |
236 | int | | 240 | int |
237 | svc_fdset_isset(int fd) | | 241 | svc_fdset_isset(int fd) |
238 | { | | 242 | { |
239 | struct svc_fdset *fds = svc_fdset_alloc(fd); | | 243 | struct svc_fdset *fds = svc_fdset_alloc(fd); |
240 | | | 244 | |
| | | 245 | if (fds == NULL) |
| | | 246 | return -1; |
| | | 247 | |
241 | DPRINTF_FDSET(fds, "%d", fd); | | 248 | DPRINTF_FDSET(fds, "%d", fd); |
242 | | | 249 | |
243 | return FD_ISSET(fd, fds->fdset); | | 250 | return FD_ISSET(fd, fds->fdset) != 0; |
244 | } | | 251 | } |
245 | | | 252 | |
246 | void | | 253 | int |
247 | svc_fdset_clr(int fd) | | 254 | svc_fdset_clr(int fd) |
248 | { | | 255 | { |
249 | struct svc_fdset *fds = svc_fdset_alloc(fd); | | 256 | struct svc_fdset *fds = svc_fdset_alloc(fd); |
250 | | | 257 | |
| | | 258 | if (fds == NULL) |
| | | 259 | return -1; |
| | | 260 | |
251 | FD_CLR(fd, fds->fdset); | | 261 | FD_CLR(fd, fds->fdset); |
252 | DPRINTF_FDSET(fds, "%d", fd); | | 262 | DPRINTF_FDSET(fds, "%d", fd); |
253 | | | 263 | |
254 | svc_fdset_sanitize(fds); | | 264 | svc_fdset_sanitize(fds); |
| | | 265 | return 0; |
255 | } | | 266 | } |
256 | | | 267 | |
257 | fd_set * | | 268 | fd_set * |
258 | svc_fdset_copy(const fd_set *orig) | | 269 | svc_fdset_copy(const fd_set *orig) |
259 | { | | 270 | { |
260 | int size = svc_fdset_getsize(0); | | 271 | int size = svc_fdset_getsize(0); |
261 | fd_set *copy = calloc(1, __NFD_BYTES(size)); | | 272 | fd_set *copy = calloc(1, __NFD_BYTES(size)); |
262 | if (copy == NULL) | | 273 | if (copy == NULL) |
263 | return NULL; | | 274 | return NULL; |
264 | if (orig) | | 275 | if (orig) |
265 | memcpy(copy, orig, __NFD_BYTES(size)); | | 276 | memcpy(copy, orig, __NFD_BYTES(size)); |
266 | return copy; | | 277 | return copy; |
267 | } | | 278 | } |
268 | | | 279 | |
269 | fd_set * | | 280 | fd_set * |
270 | svc_fdset_get(void) | | 281 | svc_fdset_get(void) |
271 | { | | 282 | { |
272 | struct svc_fdset *fds = svc_fdset_alloc(0); | | 283 | struct svc_fdset *fds = svc_fdset_alloc(0); |
273 | | | 284 | |
| | | 285 | if (fds == NULL) |
| | | 286 | return NULL; |
| | | 287 | |
274 | DPRINTF_FDSET(fds, "get"); | | 288 | DPRINTF_FDSET(fds, "get"); |
275 | svc_fdset_sanitize(fds); | | 289 | svc_fdset_sanitize(fds); |
276 | return fds->fdset; | | 290 | return fds->fdset; |
277 | } | | 291 | } |
278 | | | 292 | |
279 | int * | | 293 | int * |
280 | svc_fdset_getmax(void) | | 294 | svc_fdset_getmax(void) |
281 | { | | 295 | { |
282 | struct svc_fdset *fds = svc_fdset_alloc(0); | | 296 | struct svc_fdset *fds = svc_fdset_alloc(0); |
283 | | | 297 | |
| | | 298 | if (fds == NULL) |
| | | 299 | return NULL; |
| | | 300 | |
284 | DPRINTF_FDSET(fds, "getmax"); | | 301 | DPRINTF_FDSET(fds, "getmax"); |
285 | svc_fdset_sanitize(fds); | | 302 | svc_fdset_sanitize(fds); |
286 | return &fds->fdmax; | | 303 | return &fds->fdmax; |
287 | } | | 304 | } |
288 | | | 305 | |
289 | int | | 306 | int |
290 | svc_fdset_getsize(int fd) | | 307 | svc_fdset_getsize(int fd) |
291 | { | | 308 | { |
292 | struct svc_fdset *fds = svc_fdset_alloc(fd); | | 309 | struct svc_fdset *fds = svc_fdset_alloc(fd); |
293 | | | 310 | |
| | | 311 | if (fds == NULL) |
| | | 312 | return -1; |
| | | 313 | |
294 | DPRINTF_FDSET(fds, "getsize"); | | 314 | DPRINTF_FDSET(fds, "getsize"); |
295 | return fds->fdsize; | | 315 | return fds->fdsize; |
296 | } | | 316 | } |