1 /* $NetBSD: netbsd32_select.c,v 1.5 2003/10/26 19:12:50 christos 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.5 2003/10/26 19:12:50 christos 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(l, v, retval) 54 struct lwp *l; 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 proc *p = l->l_proc; 67 struct netbsd32_timeval tv32; 68 caddr_t bits; 69 char smallbits[howmany(FD_SETSIZE, NFDBITS) * sizeof(fd_mask) * 6]; 70 struct timeval atv; 71 int s, ncoll, error = 0, timo; 72 size_t ni; 73 extern int selwait, nselcoll; 74 extern int selscan __P((struct proc *, fd_mask *, fd_mask *, int, register_t *)); 75 76 if (SCARG(uap, nd) < 0) 77 return (EINVAL); 78 if (SCARG(uap, nd) > p->p_fd->fd_nfiles) { 79 /* forgiving; slightly wrong */ 80 SCARG(uap, nd) = p->p_fd->fd_nfiles; 81 } 82 ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask); 83 if (ni * 6 > sizeof(smallbits)) 84 bits = malloc(ni * 6, M_TEMP, M_WAITOK); 85 else 86 bits = smallbits; 87 88 #define getbits(name, x) \ 89 if (SCARG(uap, name)) { \ 90 error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, name)), \ 91 bits + ni * x, ni); \ 92 if (error) \ 93 goto done; \ 94 } else \ 95 memset(bits + ni * x, 0, ni); 96 getbits(in, 0); 97 getbits(ou, 1); 98 getbits(ex, 2); 99 #undef getbits 100 101 if (SCARG(uap, tv)) { 102 error = copyin((caddr_t)NETBSD32PTR64(SCARG(uap, tv)), 103 (caddr_t)&tv32, sizeof(tv32)); 104 if (error) 105 goto done; 106 netbsd32_to_timeval(&tv32, &atv); 107 if (itimerfix(&atv)) { 108 error = EINVAL; 109 goto done; 110 } 111 s = splclock(); 112 timeradd(&atv, &time, &atv); 113 splx(s); 114 } 115 retry: 116 ncoll = nselcoll; 117 l->l_flag |= L_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 } else 128 timo = 0; 129 if (timo <= 0) 130 goto done; 131 s = splhigh(); 132 if ((l->l_flag & L_SELECT) == 0 || nselcoll != ncoll) { 133 splx(s); 134 goto retry; 135 } 136 l->l_flag &= ~L_SELECT; 137 error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo); 138 splx(s); 139 if (error == 0) 140 goto retry; 141 done: 142 l->l_flag &= ~L_SELECT; 143 /* select is not restarted after signals... */ 144 if (error == ERESTART) 145 error = EINTR; 146 if (error == EWOULDBLOCK) 147 error = 0; 148 if (error == 0) { 149 #define putbits(name, x) \ 150 if (SCARG(uap, name)) { \ 151 error = copyout(bits + ni * x, \ 152 (caddr_t)NETBSD32PTR64(SCARG(uap, name)), ni); \ 153 if (error) \ 154 goto out; \ 155 } 156 putbits(in, 3); 157 putbits(ou, 4); 158 putbits(ex, 5); 159 #undef putbits 160 } 161 out: 162 if (ni * 6 > sizeof(smallbits)) 163 free(bits, M_TEMP); 164 return (error); 165 } 166