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