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 */ 31 32 #include <sys/cdefs.h> 33 __RCSID("$NetBSD: svc_fdset.c,v 1.4 2015/11/06 23:11:09 christos Exp $"); 34 35 36 #include "reentrant.h" 37 38 #include <sys/fd_set.h> 39 40 #include <rpc/rpc.h> 41 42 #include <stdlib.h> 43 #include <string.h> 44 45 #undef svc_fdset 46 #undef svc_maxfd 47 extern fd_set svc_fdset; 48 extern int svc_maxfd; 49 50 struct svc_fdset { 51 fd_set *fdset; 52 int fdmax; 53 int fdsize; 54 }; 55 56 /* The single threaded, one global fd_set version */ 57 static struct svc_fdset __svc_fdset; 58 59 static thread_key_t fdsetkey = -2; 60 61 #ifdef FDSET_DEBUG 62 #include <stdio.h> 63 #include <stdarg.h> 64 #include <unistd.h> 65 #include <lwp.h> 66 67 static void __printflike(3, 0) 68 svc_header(const char *func, size_t line, const char *fmt, va_list ap) 69 { 70 fprintf(stderr, "%s[%d.%d]: %s, %zu: ", getprogname(), (int)getpid(), 71 (int)_lwp_self(), func, line); 72 vfprintf(stderr, fmt, ap); 73 va_end(ap); 74 } 75 76 static void __printflike(4, 5) 77 svc_fdset_print(const char *func, size_t line, struct svc_fdset *fds, 78 const char *fmt, ...) 79 { 80 va_list ap; 81 const char *did = ""; 82 83 va_start(ap, fmt); 84 svc_header(func, line, fmt, ap); 85 va_end(ap); 86 87 fprintf(stderr, "%p[%d] <", fds->fdset, fds->fdmax); 88 for (int i = 0; i <= fds->fdmax; i++) { 89 if (!FD_ISSET(i, fds->fdset)) 90 continue; 91 fprintf(stderr, "%s%d", did, i); 92 did = ", "; 93 } 94 fprintf(stderr, ">\n"); 95 } 96 97 static void __printflike(3, 4) 98 svc_print(const char *func, size_t line, const char *fmt, ...) 99 { 100 va_list ap; 101 102 va_start(ap, fmt); 103 svc_header(func, line, fmt, ap); 104 va_end(ap); 105 fprintf(stderr, "\n"); 106 } 107 108 #define DPRINTF(...) svc_print(__func__, __LINE__, __VA_ARGS__) 109 #define DPRINTF_FDSET(...) svc_fdset_print(__func__, __LINE__, __VA_ARGS__) 110 111 #else 112 113 #define DPRINTF(...) 114 #define DPRINTF_FDSET(...) 115 116 #endif 117 118 119 static inline void 120 svc_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 131 static void 132 svc_fdset_free(void *v) 133 { 134 struct svc_fdset *fds = v; 135 DPRINTF_FDSET(fds, "free"); 136 137 free(fds->fdset); 138 free(fds); 139 } 140 141 static struct svc_fdset * 142 svc_fdset_resize(int fd, struct svc_fdset *fds) 143 { 144 if (fds->fdset && fd < fds->fdsize) { 145 DPRINTF_FDSET(fds, "keeping %d < %d", fd, fds->fdsize); 146 return fds; 147 } 148 149 fd += FD_SETSIZE; 150 151 char *newfdset = realloc(fds->fdset, __NFD_BYTES(fd)); 152 if (newfdset == NULL) 153 return NULL; 154 155 memset(newfdset + __NFD_BYTES(fds->fdsize), 0, 156 __NFD_BYTES(fd) - __NFD_BYTES(fds->fdsize)); 157 158 159 fds->fdset = (void *)newfdset; 160 DPRINTF_FDSET(fds, "resize %d > %d", fd, fds->fdsize); 161 fds->fdsize = fd; 162 163 return fds; 164 } 165 166 static struct svc_fdset * 167 svc_fdset_alloc(int fd) 168 { 169 struct svc_fdset *fds; 170 171 if (fdsetkey == -1) 172 thr_keycreate(&fdsetkey, svc_fdset_free); 173 174 if ((fds = thr_getspecific(fdsetkey)) == NULL) { 175 176 fds = calloc(1, sizeof(*fds)); 177 if (fds == NULL) 178 return NULL; 179 180 (void)thr_setspecific(fdsetkey, fds); 181 182 if (__svc_fdset.fdsize != 0) { 183 *fds = __svc_fdset; 184 DPRINTF("switching to %p", fds->fdset); 185 } else { 186 DPRINTF("first thread time %p", fds->fdset); 187 } 188 } else { 189 DPRINTF("again for %p", fds->fdset); 190 if (fd < fds->fdsize) 191 return fds; 192 } 193 194 return svc_fdset_resize(fd, fds); 195 } 196 197 static struct svc_fdset * 198 svc_fdset_get_internal(int fd) 199 { 200 if (!__isthreaded || fdsetkey == -2) 201 return svc_fdset_resize(fd, &__svc_fdset); 202 203 return svc_fdset_alloc(fd); 204 } 205 206 207 /* allow each thread to have their own copy */ 208 void 209 svc_fdset_init(int flags) 210 { 211 DPRINTF("%x", flags); 212 if ((flags & SVC_FDSET_MT) && fdsetkey == -2) 213 fdsetkey = -1; 214 } 215 216 void 217 svc_fdset_zero(void) 218 { 219 DPRINTF("zero"); 220 struct svc_fdset *fds = svc_fdset_get_internal(0); 221 memset(fds->fdset, 0, fds->fdsize); 222 fds->fdmax = 0; 223 } 224 225 void 226 svc_fdset_set(int fd) 227 { 228 struct svc_fdset *fds = svc_fdset_get_internal(fd); 229 FD_SET(fd, fds->fdset); 230 if (fd > fds->fdmax) 231 fds->fdmax = fd; 232 DPRINTF_FDSET(fds, "%d", fd); 233 234 svc_fdset_sanitize(fds); 235 } 236 237 int 238 svc_fdset_isset(int fd) 239 { 240 struct svc_fdset *fds = svc_fdset_get_internal(fd); 241 svc_fdset_sanitize(fds); 242 DPRINTF_FDSET(fds, "%d", fd); 243 return FD_ISSET(fd, fds->fdset); 244 } 245 246 void 247 svc_fdset_clr(int fd) 248 { 249 struct svc_fdset *fds = svc_fdset_get_internal(fd); 250 FD_CLR(fd, fds->fdset); 251 svc_fdset_sanitize(fds); 252 DPRINTF_FDSET(fds, "%d", fd); 253 } 254 255 fd_set * 256 svc_fdset_copy(const fd_set *orig) 257 { 258 int size = svc_fdset_getsize(0); 259 fd_set *copy = calloc(1, __NFD_BYTES(size)); 260 if (copy == NULL) 261 return NULL; 262 if (orig) 263 memcpy(copy, orig, __NFD_BYTES(size)); 264 return copy; 265 } 266 267 fd_set * 268 svc_fdset_get(void) 269 { 270 struct svc_fdset *fds = svc_fdset_get_internal(0); 271 svc_fdset_sanitize(fds); 272 273 DPRINTF_FDSET(fds, "get"); 274 return fds->fdset; 275 } 276 277 int * 278 svc_fdset_getmax(void) 279 { 280 struct svc_fdset *fds; 281 282 if (!__isthreaded || fdsetkey == -2) { 283 svc_fdset_sanitize(&__svc_fdset); 284 return &__svc_fdset.fdmax; 285 } 286 287 fds = svc_fdset_alloc(0); 288 if (fds == NULL) 289 return NULL; 290 return &fds->fdmax; 291 } 292 293 int 294 svc_fdset_getsize(int fd) 295 { 296 struct svc_fdset *fds; 297 298 if (!__isthreaded || fdsetkey == -2) { 299 if (svc_fdset_resize(fd, &__svc_fdset) == NULL) 300 return -1; 301 else 302 return __svc_fdset.fdsize; 303 } 304 305 fds = svc_fdset_alloc(fd); 306 if (fds == NULL) 307 return -1; 308 return fds->fdsize; 309 } 310