| @@ -1,29 +1,29 @@ | | | @@ -1,29 +1,29 @@ |
1 | /* $NetBSD: svc_fdset.c,v 1.2 2015/11/06 19:34:13 christos Exp $ */ | | 1 | /* $NetBSD: svc_fdset.c,v 1.3 2015/11/06 23:05:09 joerg Exp $ */ |
2 | | | 2 | |
3 | #include <sys/cdefs.h> | | 3 | #include <sys/cdefs.h> |
4 | __RCSID("$NetBSD: svc_fdset.c,v 1.2 2015/11/06 19:34:13 christos Exp $"); | | 4 | __RCSID("$NetBSD: svc_fdset.c,v 1.3 2015/11/06 23:05:09 joerg Exp $"); |
5 | | | 5 | |
6 | | | 6 | |
7 | #include "reentrant.h" | | 7 | #include "reentrant.h" |
8 | | | 8 | |
9 | #include <sys/fd_set.h> | | 9 | #include <sys/fd_set.h> |
10 | | | 10 | |
11 | #include <rpc/rpc.h> | | 11 | #include <rpc/rpc.h> |
12 | | | 12 | |
13 | #include <stdlib.h> | | 13 | #include <stdlib.h> |
14 | #include <string.h> | | 14 | #include <string.h> |
15 | | | 15 | |
16 | struct svc_fdset { | | 16 | struct my_svc_fdset { |
17 | fd_set *fdset; | | 17 | fd_set *fdset; |
18 | int fdmax; | | 18 | int fdmax; |
19 | int fdsize; | | 19 | int fdsize; |
20 | }; | | 20 | }; |
21 | | | 21 | |
22 | | | 22 | |
23 | /* The single threaded, one global fd_set version */ | | 23 | /* The single threaded, one global fd_set version */ |
24 | static fd_set *__svc_fdset; | | 24 | static fd_set *__svc_fdset; |
25 | static int svc_fdsize = 0; | | 25 | static int svc_fdsize = 0; |
26 | | | 26 | |
27 | /* | | 27 | /* |
28 | * Update the old global svc_fdset if needed for binary compatibility | | 28 | * Update the old global svc_fdset if needed for binary compatibility |
29 | */ | | 29 | */ |
| @@ -86,27 +86,27 @@ svc_print(const char *func, size_t line, | | | @@ -86,27 +86,27 @@ svc_print(const char *func, size_t line, |
86 | } | | 86 | } |
87 | | | 87 | |
88 | #define DPRINTF_FDSET(...) svc_fdset_print(__func__, __LINE__, __VA_ARGS__) | | 88 | #define DPRINTF_FDSET(...) svc_fdset_print(__func__, __LINE__, __VA_ARGS__) |
89 | #define DPRINTF(...) svc_print(__func__, __LINE__, __VA_ARGS__) | | 89 | #define DPRINTF(...) svc_print(__func__, __LINE__, __VA_ARGS__) |
90 | #else | | 90 | #else |
91 | #define DPRINTF_FDSET(...) | | 91 | #define DPRINTF_FDSET(...) |
92 | #define DPRINTF(...) | | 92 | #define DPRINTF(...) |
93 | #endif | | 93 | #endif |
94 | | | 94 | |
95 | | | 95 | |
96 | static void | | 96 | static void |
97 | svc_fdset_free(void *v) | | 97 | svc_fdset_free(void *v) |
98 | { | | 98 | { |
99 | struct svc_fdset *rv = v; | | 99 | struct my_svc_fdset *rv = v; |
100 | DPRINTF_FDSET(rv->fdset, 0, "free"); | | 100 | DPRINTF_FDSET(rv->fdset, 0, "free"); |
101 | | | 101 | |
102 | free(rv->fdset); | | 102 | free(rv->fdset); |
103 | free(rv); | | 103 | free(rv); |
104 | } | | 104 | } |
105 | | | 105 | |
106 | static fd_set * | | 106 | static fd_set * |
107 | svc_fdset_resize(int fd, fd_set **fdset, int *fdsize) | | 107 | svc_fdset_resize(int fd, fd_set **fdset, int *fdsize) |
108 | { | | 108 | { |
109 | if (*fdset && fd < *fdsize) { | | 109 | if (*fdset && fd < *fdsize) { |
110 | DPRINTF_FDSET(*fdset, 0, "keeping %d < %d", | | 110 | DPRINTF_FDSET(*fdset, 0, "keeping %d < %d", |
111 | fd, *fdsize); | | 111 | fd, *fdsize); |
112 | return *fdset; | | 112 | return *fdset; |
| @@ -123,30 +123,30 @@ svc_fdset_resize(int fd, fd_set **fdset, | | | @@ -123,30 +123,30 @@ svc_fdset_resize(int fd, fd_set **fdset, |
123 | memset(newfdset + __NFD_BYTES(*fdsize), 0, | | 123 | memset(newfdset + __NFD_BYTES(*fdsize), 0, |
124 | __NFD_BYTES(fd) - __NFD_BYTES(*fdsize)); | | 124 | __NFD_BYTES(fd) - __NFD_BYTES(*fdsize)); |
125 | | | 125 | |
126 | | | 126 | |
127 | *fdset = (void *)newfdset; | | 127 | *fdset = (void *)newfdset; |
128 | DPRINTF_FDSET(*fdset, 0, "resize %d > %d", fd, *fdsize); | | 128 | DPRINTF_FDSET(*fdset, 0, "resize %d > %d", fd, *fdsize); |
129 | *fdsize = fd; | | 129 | *fdsize = fd; |
130 | | | 130 | |
131 | COMPAT_UPDATE(*fdset); | | 131 | COMPAT_UPDATE(*fdset); |
132 | | | 132 | |
133 | return *fdset; | | 133 | return *fdset; |
134 | } | | 134 | } |
135 | | | 135 | |
136 | static struct svc_fdset * | | 136 | static struct my_svc_fdset * |
137 | svc_fdset_alloc(int fd) | | 137 | svc_fdset_alloc(int fd) |
138 | { | | 138 | { |
139 | struct svc_fdset *rv; | | 139 | struct my_svc_fdset *rv; |
140 | | | 140 | |
141 | if (fdsetkey == -1) | | 141 | if (fdsetkey == -1) |
142 | thr_keycreate(&fdsetkey, svc_fdset_free); | | 142 | thr_keycreate(&fdsetkey, svc_fdset_free); |
143 | | | 143 | |
144 | if ((rv = thr_getspecific(fdsetkey)) == NULL) { | | 144 | if ((rv = thr_getspecific(fdsetkey)) == NULL) { |
145 | | | 145 | |
146 | rv = calloc(1, sizeof(*rv)); | | 146 | rv = calloc(1, sizeof(*rv)); |
147 | if (rv == NULL) | | 147 | if (rv == NULL) |
148 | return NULL; | | 148 | return NULL; |
149 | | | 149 | |
150 | (void)thr_setspecific(fdsetkey, rv); | | 150 | (void)thr_setspecific(fdsetkey, rv); |
151 | | | 151 | |
152 | if (svc_fdsize != 0) { | | 152 | if (svc_fdsize != 0) { |
| @@ -162,27 +162,27 @@ svc_fdset_alloc(int fd) | | | @@ -162,27 +162,27 @@ svc_fdset_alloc(int fd) |
162 | } else { | | 162 | } else { |
163 | DPRINTF("again for %p", rv->fdset); | | 163 | DPRINTF("again for %p", rv->fdset); |
164 | if (fd < rv->fdsize) | | 164 | if (fd < rv->fdsize) |
165 | return rv; | | 165 | return rv; |
166 | } | | 166 | } |
167 | if (svc_fdset_resize(fd, &rv->fdset, &rv->fdsize) == NULL) | | 167 | if (svc_fdset_resize(fd, &rv->fdset, &rv->fdsize) == NULL) |
168 | return NULL; | | 168 | return NULL; |
169 | return rv; | | 169 | return rv; |
170 | } | | 170 | } |
171 | | | 171 | |
172 | static fd_set * | | 172 | static fd_set * |
173 | svc_fdset_get_internal(int fd) | | 173 | svc_fdset_get_internal(int fd) |
174 | { | | 174 | { |
175 | struct svc_fdset *rv; | | 175 | struct my_svc_fdset *rv; |
176 | | | 176 | |
177 | if (!__isthreaded || fdsetkey == -2) | | 177 | if (!__isthreaded || fdsetkey == -2) |
178 | return svc_fdset_resize(fd, &__svc_fdset, &svc_fdsize); | | 178 | return svc_fdset_resize(fd, &__svc_fdset, &svc_fdsize); |
179 | | | 179 | |
180 | rv = svc_fdset_alloc(fd); | | 180 | rv = svc_fdset_alloc(fd); |
181 | if (rv == NULL) | | 181 | if (rv == NULL) |
182 | return NULL; | | 182 | return NULL; |
183 | return rv->fdset; | | 183 | return rv->fdset; |
184 | } | | 184 | } |
185 | | | 185 | |
186 | | | 186 | |
187 | /* allow each thread to have their own copy */ | | 187 | /* allow each thread to have their own copy */ |
188 | void | | 188 | void |
| @@ -260,41 +260,41 @@ svc_fdset_copy(const fd_set *orig) | | | @@ -260,41 +260,41 @@ svc_fdset_copy(const fd_set *orig) |
260 | } | | 260 | } |
261 | | | 261 | |
262 | fd_set * | | 262 | fd_set * |
263 | svc_fdset_get(void) | | 263 | svc_fdset_get(void) |
264 | { | | 264 | { |
265 | fd_set *fds = svc_fdset_get_internal(0); | | 265 | fd_set *fds = svc_fdset_get_internal(0); |
266 | DPRINTF_FDSET(fds, 0, "get"); | | 266 | DPRINTF_FDSET(fds, 0, "get"); |
267 | return fds; | | 267 | return fds; |
268 | } | | 268 | } |
269 | | | 269 | |
270 | int * | | 270 | int * |
271 | svc_fdset_getmax(void) | | 271 | svc_fdset_getmax(void) |
272 | { | | 272 | { |
273 | struct svc_fdset *rv; | | 273 | struct my_svc_fdset *rv; |
274 | | | 274 | |
275 | if (!__isthreaded || fdsetkey == -2) | | 275 | if (!__isthreaded || fdsetkey == -2) |
276 | return &svc_maxfd; | | 276 | return &svc_maxfd; |
277 | | | 277 | |
278 | rv = svc_fdset_alloc(0); | | 278 | rv = svc_fdset_alloc(0); |
279 | if (rv == NULL) | | 279 | if (rv == NULL) |
280 | return NULL; | | 280 | return NULL; |
281 | return &rv->fdmax; | | 281 | return &rv->fdmax; |
282 | } | | 282 | } |
283 | | | 283 | |
284 | int | | 284 | int |
285 | svc_fdset_getsize(int fd) | | 285 | svc_fdset_getsize(int fd) |
286 | { | | 286 | { |
287 | struct svc_fdset *rv; | | 287 | struct my_svc_fdset *rv; |
288 | | | 288 | |
289 | if (!__isthreaded || fdsetkey == -2) { | | 289 | if (!__isthreaded || fdsetkey == -2) { |
290 | if (svc_fdset_resize(fd, &__svc_fdset, &svc_fdsize) == NULL) | | 290 | if (svc_fdset_resize(fd, &__svc_fdset, &svc_fdsize) == NULL) |
291 | return -1; | | 291 | return -1; |
292 | else | | 292 | else |
293 | return svc_fdsize; | | 293 | return svc_fdsize; |
294 | } | | 294 | } |
295 | | | 295 | |
296 | rv = svc_fdset_alloc(fd); | | 296 | rv = svc_fdset_alloc(fd); |
297 | if (rv == NULL) | | 297 | if (rv == NULL) |
298 | return -1; | | 298 | return -1; |
299 | return rv->fdsize; | | 299 | return rv->fdsize; |
300 | } | | 300 | } |