/* $NetBSD: svc_fdset.c,v 1.4 2015/11/06 23:11:09 christos Exp $ */ /*- * Copyright (c) 2015 The NetBSD Foundation, Inc. * All rights resefdsed. * * This code is derived from software contributed to The NetBSD Foundation * by Christos Zoulas. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include __RCSID("$NetBSD: svc_fdset.c,v 1.4 2015/11/06 23:11:09 christos Exp $"); #include "reentrant.h" #include #include #include #include #undef svc_fdset #undef svc_maxfd extern fd_set svc_fdset; extern int svc_maxfd; struct svc_fdset { fd_set *fdset; int fdmax; int fdsize; }; /* The single threaded, one global fd_set version */ static struct svc_fdset __svc_fdset; static thread_key_t fdsetkey = -2; #ifdef FDSET_DEBUG #include #include #include #include static void __printflike(3, 0) svc_header(const char *func, size_t line, const char *fmt, va_list ap) { fprintf(stderr, "%s[%d.%d]: %s, %zu: ", getprogname(), (int)getpid(), (int)_lwp_self(), func, line); vfprintf(stderr, fmt, ap); va_end(ap); } static void __printflike(4, 5) svc_fdset_print(const char *func, size_t line, struct svc_fdset *fds, const char *fmt, ...) { va_list ap; const char *did = ""; va_start(ap, fmt); svc_header(func, line, fmt, ap); va_end(ap); fprintf(stderr, "%p[%d] <", fds->fdset, fds->fdmax); for (int i = 0; i <= fds->fdmax; i++) { if (!FD_ISSET(i, fds->fdset)) continue; fprintf(stderr, "%s%d", did, i); did = ", "; } fprintf(stderr, ">\n"); } static void __printflike(3, 4) svc_print(const char *func, size_t line, const char *fmt, ...) { va_list ap; va_start(ap, fmt); svc_header(func, line, fmt, ap); va_end(ap); fprintf(stderr, "\n"); } #define DPRINTF(...) svc_print(__func__, __LINE__, __VA_ARGS__) #define DPRINTF_FDSET(...) svc_fdset_print(__func__, __LINE__, __VA_ARGS__) #else #define DPRINTF(...) #define DPRINTF_FDSET(...) #endif static inline void svc_fdset_sanitize(struct svc_fdset *fds) { while (fds->fdmax >= 0 && !FD_ISSET(fds->fdmax, fds->fdset)) fds->fdmax--; /* Compat update */ if (fds == &__svc_fdset) { svc_fdset = *__svc_fdset.fdset; svc_maxfd = __svc_fdset.fdmax; } } static void svc_fdset_free(void *v) { struct svc_fdset *fds = v; DPRINTF_FDSET(fds, "free"); free(fds->fdset); free(fds); } static struct svc_fdset * svc_fdset_resize(int fd, struct svc_fdset *fds) { if (fds->fdset && fd < fds->fdsize) { DPRINTF_FDSET(fds, "keeping %d < %d", fd, fds->fdsize); return fds; } fd += FD_SETSIZE; char *newfdset = realloc(fds->fdset, __NFD_BYTES(fd)); if (newfdset == NULL) return NULL; memset(newfdset + __NFD_BYTES(fds->fdsize), 0, __NFD_BYTES(fd) - __NFD_BYTES(fds->fdsize)); fds->fdset = (void *)newfdset; DPRINTF_FDSET(fds, "resize %d > %d", fd, fds->fdsize); fds->fdsize = fd; return fds; } static struct svc_fdset * svc_fdset_alloc(int fd) { struct svc_fdset *fds; if (fdsetkey == -1) thr_keycreate(&fdsetkey, svc_fdset_free); if ((fds = thr_getspecific(fdsetkey)) == NULL) { fds = calloc(1, sizeof(*fds)); if (fds == NULL) return NULL; (void)thr_setspecific(fdsetkey, fds); if (__svc_fdset.fdsize != 0) { *fds = __svc_fdset; DPRINTF("switching to %p", fds->fdset); } else { DPRINTF("first thread time %p", fds->fdset); } } else { DPRINTF("again for %p", fds->fdset); if (fd < fds->fdsize) return fds; } return svc_fdset_resize(fd, fds); } static struct svc_fdset * svc_fdset_get_internal(int fd) { if (!__isthreaded || fdsetkey == -2) return svc_fdset_resize(fd, &__svc_fdset); return svc_fdset_alloc(fd); } /* allow each thread to have their own copy */ void svc_fdset_init(int flags) { DPRINTF("%x", flags); if ((flags & SVC_FDSET_MT) && fdsetkey == -2) fdsetkey = -1; } void svc_fdset_zero(void) { DPRINTF("zero"); struct svc_fdset *fds = svc_fdset_get_internal(0); memset(fds->fdset, 0, fds->fdsize); fds->fdmax = 0; } void svc_fdset_set(int fd) { struct svc_fdset *fds = svc_fdset_get_internal(fd); FD_SET(fd, fds->fdset); if (fd > fds->fdmax) fds->fdmax = fd; DPRINTF_FDSET(fds, "%d", fd); svc_fdset_sanitize(fds); } int svc_fdset_isset(int fd) { struct svc_fdset *fds = svc_fdset_get_internal(fd); svc_fdset_sanitize(fds); DPRINTF_FDSET(fds, "%d", fd); return FD_ISSET(fd, fds->fdset); } void svc_fdset_clr(int fd) { struct svc_fdset *fds = svc_fdset_get_internal(fd); FD_CLR(fd, fds->fdset); svc_fdset_sanitize(fds); DPRINTF_FDSET(fds, "%d", fd); } fd_set * svc_fdset_copy(const fd_set *orig) { int size = svc_fdset_getsize(0); fd_set *copy = calloc(1, __NFD_BYTES(size)); if (copy == NULL) return NULL; if (orig) memcpy(copy, orig, __NFD_BYTES(size)); return copy; } fd_set * svc_fdset_get(void) { struct svc_fdset *fds = svc_fdset_get_internal(0); svc_fdset_sanitize(fds); DPRINTF_FDSET(fds, "get"); return fds->fdset; } int * svc_fdset_getmax(void) { struct svc_fdset *fds; if (!__isthreaded || fdsetkey == -2) { svc_fdset_sanitize(&__svc_fdset); return &__svc_fdset.fdmax; } fds = svc_fdset_alloc(0); if (fds == NULL) return NULL; return &fds->fdmax; } int svc_fdset_getsize(int fd) { struct svc_fdset *fds; if (!__isthreaded || fdsetkey == -2) { if (svc_fdset_resize(fd, &__svc_fdset) == NULL) return -1; else return __svc_fdset.fdsize; } fds = svc_fdset_alloc(fd); if (fds == NULL) return -1; return fds->fdsize; }