netbsd32_select.c revision 1.1.4.2 1 /* $NetBSD: netbsd32_select.c,v 1.1.4.2 2002/08/23 02:37:11 petrov 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.1.4.2 2002/08/23 02:37:11 petrov 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)(u_long)SCARG(uap, name), 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)(u_long)SCARG(uap, tv), (caddr_t)&tv32,
102 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 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 if (timo <= 0)
128 goto done;
129 }
130 s = splhigh();
131 if ((l->l_flag & L_SELECT) == 0 || nselcoll != ncoll) {
132 splx(s);
133 goto retry;
134 }
135 l->l_flag &= ~L_SELECT;
136 error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
137 splx(s);
138 if (error == 0)
139 goto retry;
140 done:
141 l->l_flag &= ~L_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, (caddr_t)(u_long)SCARG(uap, name), ni); \
151 if (error) \
152 goto out; \
153 }
154 putbits(in, 3);
155 putbits(ou, 4);
156 putbits(ex, 5);
157 #undef putbits
158 }
159 out:
160 if (ni * 6 > sizeof(smallbits))
161 free(bits, M_TEMP);
162 return (error);
163 }
164