1*62df73d3Sjca /* $OpenBSD: select.h,v 1.16 2016/03/07 18:33:10 jca Exp $ */ 2df930be7Sderaadt 3df930be7Sderaadt /*- 4df930be7Sderaadt * Copyright (c) 1992, 1993 5df930be7Sderaadt * The Regents of the University of California. All rights reserved. 6df930be7Sderaadt * 7df930be7Sderaadt * Redistribution and use in source and binary forms, with or without 8df930be7Sderaadt * modification, are permitted provided that the following conditions 9df930be7Sderaadt * are met: 10df930be7Sderaadt * 1. Redistributions of source code must retain the above copyright 11df930be7Sderaadt * notice, this list of conditions and the following disclaimer. 12df930be7Sderaadt * 2. Redistributions in binary form must reproduce the above copyright 13df930be7Sderaadt * notice, this list of conditions and the following disclaimer in the 14df930be7Sderaadt * documentation and/or other materials provided with the distribution. 1529295d1cSmillert * 3. Neither the name of the University nor the names of its contributors 16df930be7Sderaadt * may be used to endorse or promote products derived from this software 17df930be7Sderaadt * without specific prior written permission. 18df930be7Sderaadt * 19df930be7Sderaadt * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df930be7Sderaadt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df930be7Sderaadt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df930be7Sderaadt * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df930be7Sderaadt * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df930be7Sderaadt * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df930be7Sderaadt * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df930be7Sderaadt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df930be7Sderaadt * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df930be7Sderaadt * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df930be7Sderaadt * SUCH DAMAGE. 30df930be7Sderaadt * 31df930be7Sderaadt * @(#)select.h 8.2 (Berkeley) 1/4/94 32df930be7Sderaadt */ 33df930be7Sderaadt 34df930be7Sderaadt #ifndef _SYS_SELECT_H_ 35df930be7Sderaadt #define _SYS_SELECT_H_ 36df930be7Sderaadt 3752a0e603Smillert #include <sys/time.h> /* for types and struct timeval */ 381a12e8a7Sprovos 39df930be7Sderaadt /* 4017132ff2Smatthew * Currently, <sys/time.h> includes <sys/types.h> before defining timeval and 4117132ff2Smatthew * timespec, and <sys/types.h> in turn includes <sys/select.h>. So even though 4217132ff2Smatthew * we include <sys/time.h> above, the compiler might not see the timeval and 4317132ff2Smatthew * timespec definitions until after this header's contents have been processed. 4417132ff2Smatthew * 4517132ff2Smatthew * As a workaround, we forward declare timeval and timespec as structs here. 4617132ff2Smatthew */ 4717132ff2Smatthew struct timeval; 4817132ff2Smatthew struct timespec; 4917132ff2Smatthew 5017132ff2Smatthew /* 5139539e65Smillert * Select uses bit masks of file descriptors in longs. These macros 5239539e65Smillert * manipulate such bit fields (the filesystem macros use chars). 5339539e65Smillert * FD_SETSIZE may be defined by the user, but the default here should 5439539e65Smillert * be enough for most uses. 55df930be7Sderaadt */ 5639539e65Smillert #ifndef FD_SETSIZE 5739539e65Smillert #define FD_SETSIZE 1024 58df930be7Sderaadt #endif 59df930be7Sderaadt 6039539e65Smillert /* 6139539e65Smillert * We don't want to pollute the namespace with select(2) internals. 6252a0e603Smillert * Non-underscore versions are exposed later #if __BSD_VISIBLE 6339539e65Smillert */ 6439539e65Smillert #define __NBBY 8 /* number of bits in a byte */ 65a18aab4aSderaadt typedef uint32_t __fd_mask; 66ea9307b8Sotto #define __NFDBITS ((unsigned)(sizeof(__fd_mask) * __NBBY)) /* bits per mask */ 6739539e65Smillert #define __howmany(x, y) (((x) + ((y) - 1)) / (y)) 6839539e65Smillert 6939539e65Smillert typedef struct fd_set { 7039539e65Smillert __fd_mask fds_bits[__howmany(FD_SETSIZE, __NFDBITS)]; 7139539e65Smillert } fd_set; 7239539e65Smillert 73b8c90d45Sderaadt static __inline void 74b8c90d45Sderaadt __fd_set(int fd, fd_set *p) 75b8c90d45Sderaadt { 76b8c90d45Sderaadt p->fds_bits[fd / __NFDBITS] |= (1U << (fd % __NFDBITS)); 77b8c90d45Sderaadt } 78b8c90d45Sderaadt #define FD_SET(n, p) __fd_set((n), (p)) 79b8c90d45Sderaadt 80b8c90d45Sderaadt static __inline void 81b8c90d45Sderaadt __fd_clr(int fd, fd_set *p) 82b8c90d45Sderaadt { 83b8c90d45Sderaadt p->fds_bits[fd / __NFDBITS] &= ~(1U << (fd % __NFDBITS)); 84b8c90d45Sderaadt } 85b8c90d45Sderaadt #define FD_CLR(n, p) __fd_clr((n), (p)) 86b8c90d45Sderaadt 87b8c90d45Sderaadt static __inline int 88*62df73d3Sjca __fd_isset(int fd, const fd_set *p) 89b8c90d45Sderaadt { 90b8c90d45Sderaadt return (p->fds_bits[fd / __NFDBITS] & (1U << (fd % __NFDBITS))); 91b8c90d45Sderaadt } 92b8c90d45Sderaadt #define FD_ISSET(n, p) __fd_isset((n), (p)) 93d98b0f83Snaddy 94d98b0f83Snaddy #if __BSD_VISIBLE 95d98b0f83Snaddy #define FD_COPY(f, t) (void)(*(t) = *(f)) 96d98b0f83Snaddy #endif 97d98b0f83Snaddy #define FD_ZERO(p) do { \ 98d98b0f83Snaddy fd_set *_p = (p); \ 99d98b0f83Snaddy __size_t _n = __howmany(FD_SETSIZE, __NFDBITS); \ 100d98b0f83Snaddy \ 101d98b0f83Snaddy while (_n > 0) \ 102d98b0f83Snaddy _p->fds_bits[--_n] = 0; \ 103d98b0f83Snaddy } while (0) 10439539e65Smillert 10552a0e603Smillert #if __BSD_VISIBLE 10639539e65Smillert #define NBBY __NBBY 10739539e65Smillert #define fd_mask __fd_mask 10839539e65Smillert #define NFDBITS __NFDBITS 10939539e65Smillert #ifndef howmany 11039539e65Smillert #define howmany(x, y) __howmany(x, y) 11139539e65Smillert #endif 11252a0e603Smillert #endif /* __BSD_VISIBLE */ 11339539e65Smillert 11439539e65Smillert #ifndef _KERNEL 11517132ff2Smatthew #ifndef _SIGSET_T_DEFINED_ 11617132ff2Smatthew #define _SIGSET_T_DEFINED_ 11717132ff2Smatthew typedef unsigned int sigset_t; 11817132ff2Smatthew #endif 11917132ff2Smatthew 12039539e65Smillert #ifndef _SELECT_DEFINED_ 12139539e65Smillert #define _SELECT_DEFINED_ 12239539e65Smillert __BEGIN_DECLS 12317132ff2Smatthew int select(int, fd_set * __restrict, fd_set * __restrict, 12417132ff2Smatthew fd_set * __restrict, struct timeval * __restrict); 12517132ff2Smatthew int pselect(int, fd_set * __restrict, fd_set * __restrict, 12617132ff2Smatthew fd_set * __restrict, const struct timespec * __restrict, 12717132ff2Smatthew const sigset_t * __restrict); 12839539e65Smillert __END_DECLS 12939539e65Smillert #endif 13039539e65Smillert #endif /* !_KERNEL */ 13139539e65Smillert 132df930be7Sderaadt #endif /* !_SYS_SELECT_H_ */ 133