1 /* $NetBSD: netbsd32_select.c,v 1.2 2001/11/13 02:09:07 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2001 Matthew R. Green 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_select.c,v 1.2 2001/11/13 02:09:07 lukem Exp $"); 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/malloc.h> 37 #include <sys/mount.h> 38 #include <sys/time.h> 39 #include <sys/vnode.h> 40 #include <sys/file.h> 41 #include <sys/filedesc.h> 42 43 #include <sys/proc.h> 44 45 #include <net/if.h> 46 47 #include <compat/netbsd32/netbsd32.h> 48 #include <compat/netbsd32/netbsd32_syscall.h> 49 #include <compat/netbsd32/netbsd32_syscallargs.h> 50 #include <compat/netbsd32/netbsd32_conv.h> 51 52 int 53 netbsd32_select(p, v, retval) 54 struct proc *p; 55 void *v; 56 register_t *retval; 57 { 58 struct netbsd32_select_args /* { 59 syscallarg(int) nd; 60 syscallarg(netbsd32_fd_setp_t) in; 61 syscallarg(netbsd32_fd_setp_t) ou; 62 syscallarg(netbsd32_fd_setp_t) ex; 63 syscallarg(netbsd32_timevalp_t) tv; 64 } */ *uap = v; 65 /* This one must be done in-line 'cause of the timeval */ 66 struct netbsd32_timeval tv32; 67 caddr_t bits; 68 char smallbits[howmany(FD_SETSIZE, NFDBITS) * sizeof(fd_mask) * 6]; 69 struct timeval atv; 70 int s, ncoll, error = 0, timo; 71 size_t ni; 72 extern int selwait, nselcoll; 73 extern int selscan __P((struct proc *, fd_mask *, fd_mask *, int, register_t *)); 74 75 if (SCARG(uap, nd) < 0) 76 return (EINVAL); 77 if (SCARG(uap, nd) > p->p_fd->fd_nfiles) { 78 /* forgiving; slightly wrong */ 79 SCARG(uap, nd) = p->p_fd->fd_nfiles; 80 } 81 ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask); 82 if (ni * 6 > sizeof(smallbits)) 83 bits = malloc(ni * 6, M_TEMP, M_WAITOK); 84 else 85 bits = smallbits; 86 87 #define getbits(name, x) \ 88 if (SCARG(uap, name)) { \ 89 error = copyin((caddr_t)(u_long)SCARG(uap, name), bits + ni * x, ni); \ 90 if (error) \ 91 goto done; \ 92 } else \ 93 memset(bits + ni * x, 0, ni); 94 getbits(in, 0); 95 getbits(ou, 1); 96 getbits(ex, 2); 97 #undef getbits 98 99 if (SCARG(uap, tv)) { 100 error = copyin((caddr_t)(u_long)SCARG(uap, tv), (caddr_t)&tv32, 101 sizeof(tv32)); 102 if (error) 103 goto done; 104 netbsd32_to_timeval(&tv32, &atv); 105 if (itimerfix(&atv)) { 106 error = EINVAL; 107 goto done; 108 } 109 s = splclock(); 110 timeradd(&atv, &time, &atv); 111 splx(s); 112 } else 113 timo = 0; 114 retry: 115 ncoll = nselcoll; 116 p->p_flag |= P_SELECT; 117 error = selscan(p, (fd_mask *)(bits + ni * 0), 118 (fd_mask *)(bits + ni * 3), SCARG(uap, nd), retval); 119 if (error || *retval) 120 goto done; 121 if (SCARG(uap, tv)) { 122 /* 123 * We have to recalculate the timeout on every retry. 124 */ 125 timo = hzto(&atv); 126 if (timo <= 0) 127 goto done; 128 } 129 s = splhigh(); 130 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) { 131 splx(s); 132 goto retry; 133 } 134 p->p_flag &= ~P_SELECT; 135 error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo); 136 splx(s); 137 if (error == 0) 138 goto retry; 139 done: 140 p->p_flag &= ~P_SELECT; 141 /* select is not restarted after signals... */ 142 if (error == ERESTART) 143 error = EINTR; 144 if (error == EWOULDBLOCK) 145 error = 0; 146 if (error == 0) { 147 #define putbits(name, x) \ 148 if (SCARG(uap, name)) { \ 149 error = copyout(bits + ni * x, (caddr_t)(u_long)SCARG(uap, name), ni); \ 150 if (error) \ 151 goto out; \ 152 } 153 putbits(in, 3); 154 putbits(ou, 4); 155 putbits(ex, 5); 156 #undef putbits 157 } 158 out: 159 if (ni * 6 > sizeof(smallbits)) 160 free(bits, M_TEMP); 161 return (error); 162 } 163