1 /* $NetBSD: netbsd32_select.c,v 1.3 2002/10/23 13:16:45 scw 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.3 2002/10/23 13:16:45 scw 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)NETBSD32PTR64(SCARG(uap, name)), \ 90 bits + ni * x, ni); \ 91 if (error) \ 92 goto done; \ 93 } else \ 94 memset(bits + ni * x, 0, ni); 95 getbits(in, 0); 96 getbits(ou, 1); 97 getbits(ex, 2); 98 #undef getbits 99 100 if (SCARG(uap, tv)) { 101 error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, tv)), 102 (caddr_t)&tv32, sizeof(tv32)); 103 if (error) 104 goto done; 105 netbsd32_to_timeval(&tv32, &atv); 106 if (itimerfix(&atv)) { 107 error = EINVAL; 108 goto done; 109 } 110 s = splclock(); 111 timeradd(&atv, &time, &atv); 112 splx(s); 113 } else 114 timo = 0; 115 retry: 116 ncoll = nselcoll; 117 p->p_flag |= P_SELECT; 118 error = selscan(p, (fd_mask *)(bits + ni * 0), 119 (fd_mask *)(bits + ni * 3), SCARG(uap, nd), retval); 120 if (error || *retval) 121 goto done; 122 if (SCARG(uap, tv)) { 123 /* 124 * We have to recalculate the timeout on every retry. 125 */ 126 timo = hzto(&atv); 127 if (timo <= 0) 128 goto done; 129 } 130 s = splhigh(); 131 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) { 132 splx(s); 133 goto retry; 134 } 135 p->p_flag &= ~P_SELECT; 136 error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo); 137 splx(s); 138 if (error == 0) 139 goto retry; 140 done: 141 p->p_flag &= ~P_SELECT; 142 /* select is not restarted after signals... */ 143 if (error == ERESTART) 144 error = EINTR; 145 if (error == EWOULDBLOCK) 146 error = 0; 147 if (error == 0) { 148 #define putbits(name, x) \ 149 if (SCARG(uap, name)) { \ 150 error = copyout(bits + ni * x, \ 151 (caddr_t)NETBSD32PTR64(SCARG(uap, name)), ni); \ 152 if (error) \ 153 goto out; \ 154 } 155 putbits(in, 3); 156 putbits(ou, 4); 157 putbits(ex, 5); 158 #undef putbits 159 } 160 out: 161 if (ni * 6 > sizeof(smallbits)) 162 free(bits, M_TEMP); 163 return (error); 164 } 165