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