| @@ -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 |
56 | extern __fd_set_256 svc_fdset; | | 57 | extern __fd_set_256 svc_fdset; |
| | | 58 | #endif |
57 | extern int svc_maxfd; | | 59 | extern int svc_maxfd; |
58 | int __svc_flags; | | 60 | int __svc_flags; |
59 | | | 61 | |
60 | struct svc_fdset { | | 62 | struct 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 */ |
72 | static struct svc_fdset __svc_fdset; | | 74 | static struct svc_fdset __svc_fdset; |
73 | | | 75 | |
74 | static thread_key_t fdsetkey = -2; | | 76 | static thread_key_t fdsetkey = -2; |
75 | | | 77 | |
76 | #ifdef FDSET_DEBUG | | 78 | #ifdef FDSET_DEBUG |
77 | | | 79 | |
78 | static void __printflike(3, 0) | | 80 | static void __printflike(3, 0) |
79 | svc_header(const char *func, size_t line, const char *fmt, va_list ap) | | 81 | svc_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 | |
87 | static void __printflike(4, 5) | | 89 | static void __printflike(4, 5) |
88 | svc_fdset_print(const char *func, size_t line, struct svc_fdset *fds, | | 90 | svc_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 | |
108 | static void __printflike(3, 4) | | 110 | static void __printflike(3, 4) |
109 | svc_print(const char *func, size_t line, const char *fmt, ...) | | 111 | svc_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 | |
130 | static inline void | | 132 | static inline void |
131 | svc_fdset_sanitize(struct svc_fdset *fds) | | 133 | svc_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 | |
142 | static void | | 146 | static void |
143 | svc_fdset_free(void *v) | | 147 | svc_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 | |
153 | static void | | 157 | static void |
154 | svc_pollfd_init(struct pollfd *pfd, int nfd) | | 158 | svc_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 | |
163 | static struct pollfd * | | 167 | static struct pollfd * |
164 | svc_pollfd_alloc(struct svc_fdset *fds) | | 168 | svc_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 | |
175 | static struct svc_fdset * | | 179 | static struct svc_fdset * |
176 | svc_pollfd_add(int fd, struct svc_fdset *fds) | | 180 | svc_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 | |
202 | static struct svc_fdset * | | 206 | static struct svc_fdset * |
203 | svc_pollfd_del(int fd, struct svc_fdset *fds) | | 207 | svc_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 | |
228 | static struct svc_fdset * | | 232 | static struct svc_fdset * |
229 | svc_fdset_resize(int fd, struct svc_fdset *fds) | | 233 | svc_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 | |
253 | static struct svc_fdset * | | 257 | static struct svc_fdset * |
254 | svc_fdset_alloc(int fd) | | 258 | svc_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 */ |
288 | void | | 292 | void |
289 | svc_fdset_init(int flags) | | 293 | svc_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 | |
297 | void | | 301 | void |
298 | svc_fdset_zero(void) | | 302 | svc_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 | |
311 | int | | 315 | int |
312 | svc_fdset_set(int fd) | | 316 | svc_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 | |
329 | int | | 333 | int |
330 | svc_fdset_isset(int fd) | | 334 | svc_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 | |
342 | int | | 346 | int |
343 | svc_fdset_clr(int fd) | | 347 | svc_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 | |
357 | fd_set * | | 361 | fd_set * |
358 | svc_fdset_copy(const fd_set *orig) | | 362 | svc_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 | |
369 | fd_set * | | 373 | fd_set * |
370 | svc_fdset_get(void) | | 374 | svc_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 | |
382 | int * | | 386 | int * |
383 | svc_fdset_getmax(void) | | 387 | svc_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 | |
395 | int | | 399 | int |
396 | svc_fdset_getsize(int fd) | | 400 | svc_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 | |
407 | struct pollfd * | | 411 | struct pollfd * |
408 | svc_pollfd_copy(const struct pollfd *orig) | | 412 | svc_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 | |
419 | struct pollfd * | | 423 | struct pollfd * |
420 | svc_pollfd_get(void) | | 424 | svc_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 | |
431 | int * | | 435 | int * |
432 | svc_pollfd_getmax(void) | | 436 | svc_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 | |
441 | int | | 445 | int |
442 | svc_pollfd_getsize(int fd) | | 446 | svc_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 | } |