sys_select.c revision 1.3.2.2 1 1.3.2.2 christos /* $NetBSD: sys_select.c,v 1.3.2.2 2008/03/29 20:47:01 christos Exp $ */
2 1.3.2.2 christos
3 1.3.2.2 christos /*-
4 1.3.2.2 christos * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
5 1.3.2.2 christos * All rights reserved.
6 1.3.2.2 christos *
7 1.3.2.2 christos * This code is derived from software contributed to The NetBSD Foundation
8 1.3.2.2 christos * by Andrew Doran.
9 1.3.2.2 christos *
10 1.3.2.2 christos * Redistribution and use in source and binary forms, with or without
11 1.3.2.2 christos * modification, are permitted provided that the following conditions
12 1.3.2.2 christos * are met:
13 1.3.2.2 christos * 1. Redistributions of source code must retain the above copyright
14 1.3.2.2 christos * notice, this list of conditions and the following disclaimer.
15 1.3.2.2 christos * 2. Redistributions in binary form must reproduce the above copyright
16 1.3.2.2 christos * notice, this list of conditions and the following disclaimer in the
17 1.3.2.2 christos * documentation and/or other materials provided with the distribution.
18 1.3.2.2 christos * 3. All advertising materials mentioning features or use of this software
19 1.3.2.2 christos * must display the following acknowledgement:
20 1.3.2.2 christos * This product includes software developed by the NetBSD
21 1.3.2.2 christos * Foundation, Inc. and its contributors.
22 1.3.2.2 christos * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.3.2.2 christos * contributors may be used to endorse or promote products derived
24 1.3.2.2 christos * from this software without specific prior written permission.
25 1.3.2.2 christos *
26 1.3.2.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.3.2.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.3.2.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.3.2.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.3.2.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.3.2.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.3.2.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.3.2.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.3.2.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.3.2.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.3.2.2 christos * POSSIBILITY OF SUCH DAMAGE.
37 1.3.2.2 christos */
38 1.3.2.2 christos
39 1.3.2.2 christos /*
40 1.3.2.2 christos * Copyright (c) 1982, 1986, 1989, 1993
41 1.3.2.2 christos * The Regents of the University of California. All rights reserved.
42 1.3.2.2 christos * (c) UNIX System Laboratories, Inc.
43 1.3.2.2 christos * All or some portions of this file are derived from material licensed
44 1.3.2.2 christos * to the University of California by American Telephone and Telegraph
45 1.3.2.2 christos * Co. or Unix System Laboratories, Inc. and are reproduced herein with
46 1.3.2.2 christos * the permission of UNIX System Laboratories, Inc.
47 1.3.2.2 christos *
48 1.3.2.2 christos * Redistribution and use in source and binary forms, with or without
49 1.3.2.2 christos * modification, are permitted provided that the following conditions
50 1.3.2.2 christos * are met:
51 1.3.2.2 christos * 1. Redistributions of source code must retain the above copyright
52 1.3.2.2 christos * notice, this list of conditions and the following disclaimer.
53 1.3.2.2 christos * 2. Redistributions in binary form must reproduce the above copyright
54 1.3.2.2 christos * notice, this list of conditions and the following disclaimer in the
55 1.3.2.2 christos * documentation and/or other materials provided with the distribution.
56 1.3.2.2 christos * 3. Neither the name of the University nor the names of its contributors
57 1.3.2.2 christos * may be used to endorse or promote products derived from this software
58 1.3.2.2 christos * without specific prior written permission.
59 1.3.2.2 christos *
60 1.3.2.2 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 1.3.2.2 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 1.3.2.2 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 1.3.2.2 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 1.3.2.2 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 1.3.2.2 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 1.3.2.2 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 1.3.2.2 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 1.3.2.2 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 1.3.2.2 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 1.3.2.2 christos * SUCH DAMAGE.
71 1.3.2.2 christos *
72 1.3.2.2 christos * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
73 1.3.2.2 christos */
74 1.3.2.2 christos
75 1.3.2.2 christos /*
76 1.3.2.2 christos * System calls relating to files.
77 1.3.2.2 christos */
78 1.3.2.2 christos
79 1.3.2.2 christos #include <sys/cdefs.h>
80 1.3.2.2 christos __KERNEL_RCSID(0, "$NetBSD: sys_select.c,v 1.3.2.2 2008/03/29 20:47:01 christos Exp $");
81 1.3.2.2 christos
82 1.3.2.2 christos #include <sys/param.h>
83 1.3.2.2 christos #include <sys/systm.h>
84 1.3.2.2 christos #include <sys/filedesc.h>
85 1.3.2.2 christos #include <sys/ioctl.h>
86 1.3.2.2 christos #include <sys/file.h>
87 1.3.2.2 christos #include <sys/proc.h>
88 1.3.2.2 christos #include <sys/socketvar.h>
89 1.3.2.2 christos #include <sys/signalvar.h>
90 1.3.2.2 christos #include <sys/uio.h>
91 1.3.2.2 christos #include <sys/kernel.h>
92 1.3.2.2 christos #include <sys/stat.h>
93 1.3.2.2 christos #include <sys/poll.h>
94 1.3.2.2 christos #include <sys/vnode.h>
95 1.3.2.2 christos #include <sys/mount.h>
96 1.3.2.2 christos #include <sys/syscallargs.h>
97 1.3.2.2 christos #include <sys/cpu.h>
98 1.3.2.2 christos #include <sys/atomic.h>
99 1.3.2.2 christos #include <sys/socketvar.h>
100 1.3.2.2 christos #include <sys/sleepq.h>
101 1.3.2.2 christos
102 1.3.2.2 christos /* Flags for lwp::l_selflag. */
103 1.3.2.2 christos #define SEL_RESET 0 /* awoken, interrupted, or not yet polling */
104 1.3.2.2 christos #define SEL_SCANNING 1 /* polling descriptors */
105 1.3.2.2 christos #define SEL_BLOCKING 2 /* about to block on select_cv */
106 1.3.2.2 christos
107 1.3.2.2 christos /* Per-CPU state for select()/poll(). */
108 1.3.2.2 christos #if MAXCPUS > 32
109 1.3.2.2 christos #error adjust this code
110 1.3.2.2 christos #endif
111 1.3.2.2 christos typedef struct selcpu {
112 1.3.2.2 christos kmutex_t sc_lock;
113 1.3.2.2 christos sleepq_t sc_sleepq;
114 1.3.2.2 christos int sc_ncoll;
115 1.3.2.2 christos uint32_t sc_mask;
116 1.3.2.2 christos } selcpu_t;
117 1.3.2.2 christos
118 1.3.2.2 christos static int selscan(lwp_t *, fd_mask *, fd_mask *, int, register_t *);
119 1.3.2.2 christos static int pollscan(lwp_t *, struct pollfd *, int, register_t *);
120 1.3.2.2 christos static void selclear(void);
121 1.3.2.2 christos
122 1.3.2.2 christos static syncobj_t select_sobj = {
123 1.3.2.2 christos SOBJ_SLEEPQ_FIFO,
124 1.3.2.2 christos sleepq_unsleep,
125 1.3.2.2 christos sleepq_changepri,
126 1.3.2.2 christos sleepq_lendpri,
127 1.3.2.2 christos syncobj_noowner,
128 1.3.2.2 christos };
129 1.3.2.2 christos
130 1.3.2.2 christos /*
131 1.3.2.2 christos * Select system call.
132 1.3.2.2 christos */
133 1.3.2.2 christos int
134 1.3.2.2 christos sys___pselect50(struct lwp *l, const struct sys___pselect50_args *uap,
135 1.3.2.2 christos register_t *retval)
136 1.3.2.2 christos {
137 1.3.2.2 christos /* {
138 1.3.2.2 christos syscallarg(int) nd;
139 1.3.2.2 christos syscallarg(fd_set *) in;
140 1.3.2.2 christos syscallarg(fd_set *) ou;
141 1.3.2.2 christos syscallarg(fd_set *) ex;
142 1.3.2.2 christos syscallarg(const struct timespec *) ts;
143 1.3.2.2 christos syscallarg(sigset_t *) mask;
144 1.3.2.2 christos } */
145 1.3.2.2 christos struct timespec ats;
146 1.3.2.2 christos struct timeval atv, *tv = NULL;
147 1.3.2.2 christos sigset_t amask, *mask = NULL;
148 1.3.2.2 christos int error;
149 1.3.2.2 christos
150 1.3.2.2 christos if (SCARG(uap, ts)) {
151 1.3.2.2 christos error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
152 1.3.2.2 christos if (error)
153 1.3.2.2 christos return error;
154 1.3.2.2 christos atv.tv_sec = ats.tv_sec;
155 1.3.2.2 christos atv.tv_usec = ats.tv_nsec / 1000;
156 1.3.2.2 christos tv = &atv;
157 1.3.2.2 christos }
158 1.3.2.2 christos if (SCARG(uap, mask) != NULL) {
159 1.3.2.2 christos error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
160 1.3.2.2 christos if (error)
161 1.3.2.2 christos return error;
162 1.3.2.2 christos mask = &amask;
163 1.3.2.2 christos }
164 1.3.2.2 christos
165 1.3.2.2 christos return selcommon(l, retval, SCARG(uap, nd), SCARG(uap, in),
166 1.3.2.2 christos SCARG(uap, ou), SCARG(uap, ex), tv, mask);
167 1.3.2.2 christos }
168 1.3.2.2 christos
169 1.3.2.2 christos int
170 1.3.2.2 christos inittimeleft(struct timeval *tv, struct timeval *sleeptv)
171 1.3.2.2 christos {
172 1.3.2.2 christos if (itimerfix(tv))
173 1.3.2.2 christos return -1;
174 1.3.2.2 christos getmicrouptime(sleeptv);
175 1.3.2.2 christos return 0;
176 1.3.2.2 christos }
177 1.3.2.2 christos
178 1.3.2.2 christos int
179 1.3.2.2 christos gettimeleft(struct timeval *tv, struct timeval *sleeptv)
180 1.3.2.2 christos {
181 1.3.2.2 christos /*
182 1.3.2.2 christos * We have to recalculate the timeout on every retry.
183 1.3.2.2 christos */
184 1.3.2.2 christos struct timeval slepttv;
185 1.3.2.2 christos /*
186 1.3.2.2 christos * reduce tv by elapsed time
187 1.3.2.2 christos * based on monotonic time scale
188 1.3.2.2 christos */
189 1.3.2.2 christos getmicrouptime(&slepttv);
190 1.3.2.2 christos timeradd(tv, sleeptv, tv);
191 1.3.2.2 christos timersub(tv, &slepttv, tv);
192 1.3.2.2 christos *sleeptv = slepttv;
193 1.3.2.2 christos return tvtohz(tv);
194 1.3.2.2 christos }
195 1.3.2.2 christos
196 1.3.2.2 christos int
197 1.3.2.2 christos sys___select50(struct lwp *l, const struct sys___select50_args *uap,
198 1.3.2.2 christos register_t *retval)
199 1.3.2.2 christos {
200 1.3.2.2 christos /* {
201 1.3.2.2 christos syscallarg(int) nd;
202 1.3.2.2 christos syscallarg(fd_set *) in;
203 1.3.2.2 christos syscallarg(fd_set *) ou;
204 1.3.2.2 christos syscallarg(fd_set *) ex;
205 1.3.2.2 christos syscallarg(struct timeval *) tv;
206 1.3.2.2 christos } */
207 1.3.2.2 christos struct timeval atv, *tv = NULL;
208 1.3.2.2 christos int error;
209 1.3.2.2 christos
210 1.3.2.2 christos if (SCARG(uap, tv)) {
211 1.3.2.2 christos error = copyin(SCARG(uap, tv), (void *)&atv,
212 1.3.2.2 christos sizeof(atv));
213 1.3.2.2 christos if (error)
214 1.3.2.2 christos return error;
215 1.3.2.2 christos tv = &atv;
216 1.3.2.2 christos }
217 1.3.2.2 christos
218 1.3.2.2 christos return selcommon(l, retval, SCARG(uap, nd), SCARG(uap, in),
219 1.3.2.2 christos SCARG(uap, ou), SCARG(uap, ex), tv, NULL);
220 1.3.2.2 christos }
221 1.3.2.2 christos
222 1.3.2.2 christos int
223 1.3.2.2 christos selcommon(lwp_t *l, register_t *retval, int nd, fd_set *u_in,
224 1.3.2.2 christos fd_set *u_ou, fd_set *u_ex, struct timeval *tv, sigset_t *mask)
225 1.3.2.2 christos {
226 1.3.2.2 christos char smallbits[howmany(FD_SETSIZE, NFDBITS) *
227 1.3.2.2 christos sizeof(fd_mask) * 6];
228 1.3.2.2 christos proc_t * const p = l->l_proc;
229 1.3.2.2 christos char *bits;
230 1.3.2.2 christos int ncoll, error, timo;
231 1.3.2.2 christos size_t ni;
232 1.3.2.2 christos sigset_t oldmask;
233 1.3.2.2 christos struct timeval sleeptv;
234 1.3.2.2 christos selcpu_t *sc;
235 1.3.2.2 christos
236 1.3.2.2 christos error = 0;
237 1.3.2.2 christos if (nd < 0)
238 1.3.2.2 christos return (EINVAL);
239 1.3.2.2 christos if (nd > p->p_fd->fd_nfiles) {
240 1.3.2.2 christos /* forgiving; slightly wrong */
241 1.3.2.2 christos nd = p->p_fd->fd_nfiles;
242 1.3.2.2 christos }
243 1.3.2.2 christos ni = howmany(nd, NFDBITS) * sizeof(fd_mask);
244 1.3.2.2 christos if (ni * 6 > sizeof(smallbits))
245 1.3.2.2 christos bits = kmem_alloc(ni * 6, KM_SLEEP);
246 1.3.2.2 christos else
247 1.3.2.2 christos bits = smallbits;
248 1.3.2.2 christos
249 1.3.2.2 christos #define getbits(name, x) \
250 1.3.2.2 christos if (u_ ## name) { \
251 1.3.2.2 christos error = copyin(u_ ## name, bits + ni * x, ni); \
252 1.3.2.2 christos if (error) \
253 1.3.2.2 christos goto done; \
254 1.3.2.2 christos } else \
255 1.3.2.2 christos memset(bits + ni * x, 0, ni);
256 1.3.2.2 christos getbits(in, 0);
257 1.3.2.2 christos getbits(ou, 1);
258 1.3.2.2 christos getbits(ex, 2);
259 1.3.2.2 christos #undef getbits
260 1.3.2.2 christos
261 1.3.2.2 christos timo = 0;
262 1.3.2.2 christos if (tv && inittimeleft(tv, &sleeptv) == -1) {
263 1.3.2.2 christos error = EINVAL;
264 1.3.2.2 christos goto done;
265 1.3.2.2 christos }
266 1.3.2.2 christos
267 1.3.2.2 christos if (mask) {
268 1.3.2.2 christos sigminusset(&sigcantmask, mask);
269 1.3.2.2 christos mutex_enter(&p->p_smutex);
270 1.3.2.2 christos oldmask = l->l_sigmask;
271 1.3.2.2 christos l->l_sigmask = *mask;
272 1.3.2.2 christos mutex_exit(&p->p_smutex);
273 1.3.2.2 christos } else
274 1.3.2.2 christos oldmask = l->l_sigmask; /* XXXgcc */
275 1.3.2.2 christos
276 1.3.2.2 christos sc = curcpu()->ci_data.cpu_selcpu;
277 1.3.2.2 christos l->l_selcpu = sc;
278 1.3.2.2 christos SLIST_INIT(&l->l_selwait);
279 1.3.2.2 christos for (;;) {
280 1.3.2.2 christos /*
281 1.3.2.2 christos * No need to lock. If this is overwritten by another
282 1.3.2.2 christos * value while scanning, we will retry below. We only
283 1.3.2.2 christos * need to see exact state from the descriptors that
284 1.3.2.2 christos * we are about to poll, and lock activity resulting
285 1.3.2.2 christos * from fo_poll is enough to provide an up to date value
286 1.3.2.2 christos * for new polling activity.
287 1.3.2.2 christos */
288 1.3.2.2 christos l->l_selflag = SEL_SCANNING;
289 1.3.2.2 christos ncoll = sc->sc_ncoll;
290 1.3.2.2 christos
291 1.3.2.2 christos error = selscan(l, (fd_mask *)(bits + ni * 0),
292 1.3.2.2 christos (fd_mask *)(bits + ni * 3), nd, retval);
293 1.3.2.2 christos
294 1.3.2.2 christos if (error || *retval)
295 1.3.2.2 christos break;
296 1.3.2.2 christos if (tv && (timo = gettimeleft(tv, &sleeptv)) <= 0)
297 1.3.2.2 christos break;
298 1.3.2.2 christos mutex_spin_enter(&sc->sc_lock);
299 1.3.2.2 christos if (l->l_selflag != SEL_SCANNING || sc->sc_ncoll != ncoll) {
300 1.3.2.2 christos mutex_spin_exit(&sc->sc_lock);
301 1.3.2.2 christos continue;
302 1.3.2.2 christos }
303 1.3.2.2 christos l->l_selflag = SEL_BLOCKING;
304 1.3.2.2 christos lwp_lock(l);
305 1.3.2.2 christos lwp_unlock_to(l, &sc->sc_lock);
306 1.3.2.2 christos sleepq_enqueue(&sc->sc_sleepq, sc, "select", &select_sobj);
307 1.3.2.2 christos KERNEL_UNLOCK_ALL(NULL, &l->l_biglocks); /* XXX */
308 1.3.2.2 christos error = sleepq_block(timo, true);
309 1.3.2.2 christos if (error != 0)
310 1.3.2.2 christos break;
311 1.3.2.2 christos }
312 1.3.2.2 christos selclear();
313 1.3.2.2 christos
314 1.3.2.2 christos if (mask) {
315 1.3.2.2 christos mutex_enter(&p->p_smutex);
316 1.3.2.2 christos l->l_sigmask = oldmask;
317 1.3.2.2 christos mutex_exit(&p->p_smutex);
318 1.3.2.2 christos }
319 1.3.2.2 christos
320 1.3.2.2 christos done:
321 1.3.2.2 christos /* select is not restarted after signals... */
322 1.3.2.2 christos if (error == ERESTART)
323 1.3.2.2 christos error = EINTR;
324 1.3.2.2 christos if (error == EWOULDBLOCK)
325 1.3.2.2 christos error = 0;
326 1.3.2.2 christos if (error == 0 && u_in != NULL)
327 1.3.2.2 christos error = copyout(bits + ni * 3, u_in, ni);
328 1.3.2.2 christos if (error == 0 && u_ou != NULL)
329 1.3.2.2 christos error = copyout(bits + ni * 4, u_ou, ni);
330 1.3.2.2 christos if (error == 0 && u_ex != NULL)
331 1.3.2.2 christos error = copyout(bits + ni * 5, u_ex, ni);
332 1.3.2.2 christos if (bits != smallbits)
333 1.3.2.2 christos kmem_free(bits, ni * 6);
334 1.3.2.2 christos return (error);
335 1.3.2.2 christos }
336 1.3.2.2 christos
337 1.3.2.2 christos int
338 1.3.2.2 christos selscan(lwp_t *l, fd_mask *ibitp, fd_mask *obitp, int nfd,
339 1.3.2.2 christos register_t *retval)
340 1.3.2.2 christos {
341 1.3.2.2 christos static const int flag[3] = { POLLRDNORM | POLLHUP | POLLERR,
342 1.3.2.2 christos POLLWRNORM | POLLHUP | POLLERR,
343 1.3.2.2 christos POLLRDBAND };
344 1.3.2.2 christos int msk, i, j, fd, n;
345 1.3.2.2 christos fd_mask ibits, obits;
346 1.3.2.2 christos file_t *fp;
347 1.3.2.2 christos
348 1.3.2.2 christos n = 0;
349 1.3.2.2 christos for (msk = 0; msk < 3; msk++) {
350 1.3.2.2 christos for (i = 0; i < nfd; i += NFDBITS) {
351 1.3.2.2 christos ibits = *ibitp++;
352 1.3.2.2 christos obits = 0;
353 1.3.2.2 christos while ((j = ffs(ibits)) && (fd = i + --j) < nfd) {
354 1.3.2.2 christos ibits &= ~(1 << j);
355 1.3.2.2 christos if ((fp = fd_getfile(fd)) == NULL)
356 1.3.2.2 christos return (EBADF);
357 1.3.2.2 christos if ((*fp->f_ops->fo_poll)(fp, flag[msk])) {
358 1.3.2.2 christos obits |= (1 << j);
359 1.3.2.2 christos n++;
360 1.3.2.2 christos }
361 1.3.2.2 christos fd_putfile(fd);
362 1.3.2.2 christos }
363 1.3.2.2 christos *obitp++ = obits;
364 1.3.2.2 christos }
365 1.3.2.2 christos }
366 1.3.2.2 christos *retval = n;
367 1.3.2.2 christos return (0);
368 1.3.2.2 christos }
369 1.3.2.2 christos
370 1.3.2.2 christos /*
371 1.3.2.2 christos * Poll system call.
372 1.3.2.2 christos */
373 1.3.2.2 christos int
374 1.3.2.2 christos sys_poll(struct lwp *l, const struct sys_poll_args *uap, register_t *retval)
375 1.3.2.2 christos {
376 1.3.2.2 christos /* {
377 1.3.2.2 christos syscallarg(struct pollfd *) fds;
378 1.3.2.2 christos syscallarg(u_int) nfds;
379 1.3.2.2 christos syscallarg(int) timeout;
380 1.3.2.2 christos } */
381 1.3.2.2 christos struct timeval atv, *tv = NULL;
382 1.3.2.2 christos
383 1.3.2.2 christos if (SCARG(uap, timeout) != INFTIM) {
384 1.3.2.2 christos atv.tv_sec = SCARG(uap, timeout) / 1000;
385 1.3.2.2 christos atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
386 1.3.2.2 christos tv = &atv;
387 1.3.2.2 christos }
388 1.3.2.2 christos
389 1.3.2.2 christos return pollcommon(l, retval, SCARG(uap, fds), SCARG(uap, nfds),
390 1.3.2.2 christos tv, NULL);
391 1.3.2.2 christos }
392 1.3.2.2 christos
393 1.3.2.2 christos /*
394 1.3.2.2 christos * Poll system call.
395 1.3.2.2 christos */
396 1.3.2.2 christos int
397 1.3.2.2 christos sys___pollts50(struct lwp *l, const struct sys___pollts50_args *uap,
398 1.3.2.2 christos register_t *retval)
399 1.3.2.2 christos {
400 1.3.2.2 christos /* {
401 1.3.2.2 christos syscallarg(struct pollfd *) fds;
402 1.3.2.2 christos syscallarg(u_int) nfds;
403 1.3.2.2 christos syscallarg(const struct timespec *) ts;
404 1.3.2.2 christos syscallarg(const sigset_t *) mask;
405 1.3.2.2 christos } */
406 1.3.2.2 christos struct timespec ats;
407 1.3.2.2 christos struct timeval atv, *tv = NULL;
408 1.3.2.2 christos sigset_t amask, *mask = NULL;
409 1.3.2.2 christos int error;
410 1.3.2.2 christos
411 1.3.2.2 christos if (SCARG(uap, ts)) {
412 1.3.2.2 christos error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
413 1.3.2.2 christos if (error)
414 1.3.2.2 christos return error;
415 1.3.2.2 christos atv.tv_sec = ats.tv_sec;
416 1.3.2.2 christos atv.tv_usec = ats.tv_nsec / 1000;
417 1.3.2.2 christos tv = &atv;
418 1.3.2.2 christos }
419 1.3.2.2 christos if (SCARG(uap, mask)) {
420 1.3.2.2 christos error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
421 1.3.2.2 christos if (error)
422 1.3.2.2 christos return error;
423 1.3.2.2 christos mask = &amask;
424 1.3.2.2 christos }
425 1.3.2.2 christos
426 1.3.2.2 christos return pollcommon(l, retval, SCARG(uap, fds), SCARG(uap, nfds),
427 1.3.2.2 christos tv, mask);
428 1.3.2.2 christos }
429 1.3.2.2 christos
430 1.3.2.2 christos int
431 1.3.2.2 christos pollcommon(lwp_t *l, register_t *retval,
432 1.3.2.2 christos struct pollfd *u_fds, u_int nfds,
433 1.3.2.2 christos struct timeval *tv, sigset_t *mask)
434 1.3.2.2 christos {
435 1.3.2.2 christos char smallbits[32 * sizeof(struct pollfd)];
436 1.3.2.2 christos proc_t * const p = l->l_proc;
437 1.3.2.2 christos void * bits;
438 1.3.2.2 christos sigset_t oldmask;
439 1.3.2.2 christos int ncoll, error, timo;
440 1.3.2.2 christos size_t ni;
441 1.3.2.2 christos struct timeval sleeptv;
442 1.3.2.2 christos selcpu_t *sc;
443 1.3.2.2 christos
444 1.3.2.2 christos if (nfds > p->p_fd->fd_nfiles) {
445 1.3.2.2 christos /* forgiving; slightly wrong */
446 1.3.2.2 christos nfds = p->p_fd->fd_nfiles;
447 1.3.2.2 christos }
448 1.3.2.2 christos ni = nfds * sizeof(struct pollfd);
449 1.3.2.2 christos if (ni > sizeof(smallbits))
450 1.3.2.2 christos bits = kmem_alloc(ni, KM_SLEEP);
451 1.3.2.2 christos else
452 1.3.2.2 christos bits = smallbits;
453 1.3.2.2 christos
454 1.3.2.2 christos error = copyin(u_fds, bits, ni);
455 1.3.2.2 christos if (error)
456 1.3.2.2 christos goto done;
457 1.3.2.2 christos
458 1.3.2.2 christos timo = 0;
459 1.3.2.2 christos if (tv && inittimeleft(tv, &sleeptv) == -1) {
460 1.3.2.2 christos error = EINVAL;
461 1.3.2.2 christos goto done;
462 1.3.2.2 christos }
463 1.3.2.2 christos
464 1.3.2.2 christos if (mask) {
465 1.3.2.2 christos sigminusset(&sigcantmask, mask);
466 1.3.2.2 christos mutex_enter(&p->p_smutex);
467 1.3.2.2 christos oldmask = l->l_sigmask;
468 1.3.2.2 christos l->l_sigmask = *mask;
469 1.3.2.2 christos mutex_exit(&p->p_smutex);
470 1.3.2.2 christos } else
471 1.3.2.2 christos oldmask = l->l_sigmask; /* XXXgcc */
472 1.3.2.2 christos
473 1.3.2.2 christos sc = curcpu()->ci_data.cpu_selcpu;
474 1.3.2.2 christos l->l_selcpu = sc;
475 1.3.2.2 christos SLIST_INIT(&l->l_selwait);
476 1.3.2.2 christos for (;;) {
477 1.3.2.2 christos /*
478 1.3.2.2 christos * No need to lock. If this is overwritten by another
479 1.3.2.2 christos * value while scanning, we will retry below. We only
480 1.3.2.2 christos * need to see exact state from the descriptors that
481 1.3.2.2 christos * we are about to poll, and lock activity resulting
482 1.3.2.2 christos * from fo_poll is enough to provide an up to date value
483 1.3.2.2 christos * for new polling activity.
484 1.3.2.2 christos */
485 1.3.2.2 christos ncoll = sc->sc_ncoll;
486 1.3.2.2 christos l->l_selflag = SEL_SCANNING;
487 1.3.2.2 christos
488 1.3.2.2 christos error = pollscan(l, (struct pollfd *)bits, nfds, retval);
489 1.3.2.2 christos
490 1.3.2.2 christos if (error || *retval)
491 1.3.2.2 christos break;
492 1.3.2.2 christos if (tv && (timo = gettimeleft(tv, &sleeptv)) <= 0)
493 1.3.2.2 christos break;
494 1.3.2.2 christos mutex_spin_enter(&sc->sc_lock);
495 1.3.2.2 christos if (l->l_selflag != SEL_SCANNING || sc->sc_ncoll != ncoll) {
496 1.3.2.2 christos mutex_spin_exit(&sc->sc_lock);
497 1.3.2.2 christos continue;
498 1.3.2.2 christos }
499 1.3.2.2 christos l->l_selflag = SEL_BLOCKING;
500 1.3.2.2 christos lwp_lock(l);
501 1.3.2.2 christos lwp_unlock_to(l, &sc->sc_lock);
502 1.3.2.2 christos sleepq_enqueue(&sc->sc_sleepq, sc, "select", &select_sobj);
503 1.3.2.2 christos KERNEL_UNLOCK_ALL(NULL, &l->l_biglocks); /* XXX */
504 1.3.2.2 christos error = sleepq_block(timo, true);
505 1.3.2.2 christos if (error != 0)
506 1.3.2.2 christos break;
507 1.3.2.2 christos }
508 1.3.2.2 christos selclear();
509 1.3.2.2 christos
510 1.3.2.2 christos if (mask) {
511 1.3.2.2 christos mutex_enter(&p->p_smutex);
512 1.3.2.2 christos l->l_sigmask = oldmask;
513 1.3.2.2 christos mutex_exit(&p->p_smutex);
514 1.3.2.2 christos }
515 1.3.2.2 christos done:
516 1.3.2.2 christos /* poll is not restarted after signals... */
517 1.3.2.2 christos if (error == ERESTART)
518 1.3.2.2 christos error = EINTR;
519 1.3.2.2 christos if (error == EWOULDBLOCK)
520 1.3.2.2 christos error = 0;
521 1.3.2.2 christos if (error == 0)
522 1.3.2.2 christos error = copyout(bits, u_fds, ni);
523 1.3.2.2 christos if (bits != smallbits)
524 1.3.2.2 christos kmem_free(bits, ni);
525 1.3.2.2 christos return (error);
526 1.3.2.2 christos }
527 1.3.2.2 christos
528 1.3.2.2 christos int
529 1.3.2.2 christos pollscan(lwp_t *l, struct pollfd *fds, int nfd, register_t *retval)
530 1.3.2.2 christos {
531 1.3.2.2 christos int i, n;
532 1.3.2.2 christos file_t *fp;
533 1.3.2.2 christos
534 1.3.2.2 christos n = 0;
535 1.3.2.2 christos for (i = 0; i < nfd; i++, fds++) {
536 1.3.2.2 christos if (fds->fd < 0) {
537 1.3.2.2 christos fds->revents = 0;
538 1.3.2.2 christos } else if ((fp = fd_getfile(fds->fd)) == NULL) {
539 1.3.2.2 christos fds->revents = POLLNVAL;
540 1.3.2.2 christos n++;
541 1.3.2.2 christos } else {
542 1.3.2.2 christos fds->revents = (*fp->f_ops->fo_poll)(fp,
543 1.3.2.2 christos fds->events | POLLERR | POLLHUP);
544 1.3.2.2 christos if (fds->revents != 0)
545 1.3.2.2 christos n++;
546 1.3.2.2 christos fd_putfile(fds->fd);
547 1.3.2.2 christos }
548 1.3.2.2 christos }
549 1.3.2.2 christos *retval = n;
550 1.3.2.2 christos return (0);
551 1.3.2.2 christos }
552 1.3.2.2 christos
553 1.3.2.2 christos /*ARGSUSED*/
554 1.3.2.2 christos int
555 1.3.2.2 christos seltrue(dev_t dev, int events, lwp_t *l)
556 1.3.2.2 christos {
557 1.3.2.2 christos
558 1.3.2.2 christos return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
559 1.3.2.2 christos }
560 1.3.2.2 christos
561 1.3.2.2 christos /*
562 1.3.2.2 christos * Record a select request. Concurrency issues:
563 1.3.2.2 christos *
564 1.3.2.2 christos * The caller holds the same lock across calls to selrecord() and
565 1.3.2.2 christos * selwakeup(), so we don't need to consider a concurrent wakeup
566 1.3.2.2 christos * while in this routine.
567 1.3.2.2 christos *
568 1.3.2.2 christos * The only activity we need to guard against is selclear(), called by
569 1.3.2.2 christos * another thread that is exiting selcommon() or pollcommon().
570 1.3.2.2 christos * `sel_lwp' can only become non-NULL while the caller's lock is held,
571 1.3.2.2 christos * so it cannot become non-NULL due to a change made by another thread
572 1.3.2.2 christos * while we are in this routine. It can only become _NULL_ due to a
573 1.3.2.2 christos * call to selclear().
574 1.3.2.2 christos *
575 1.3.2.2 christos * If it is non-NULL and != selector there is the potential for
576 1.3.2.2 christos * selclear() to be called by another thread. If either of those
577 1.3.2.2 christos * conditions are true, we're not interested in touching the `named
578 1.3.2.2 christos * waiter' part of the selinfo record because we need to record a
579 1.3.2.2 christos * collision. Hence there is no need for additional locking in this
580 1.3.2.2 christos * routine.
581 1.3.2.2 christos */
582 1.3.2.2 christos void
583 1.3.2.2 christos selrecord(lwp_t *selector, struct selinfo *sip)
584 1.3.2.2 christos {
585 1.3.2.2 christos selcpu_t *sc;
586 1.3.2.2 christos lwp_t *other;
587 1.3.2.2 christos
588 1.3.2.2 christos KASSERT(selector == curlwp);
589 1.3.2.2 christos
590 1.3.2.2 christos sc = selector->l_selcpu;
591 1.3.2.2 christos other = sip->sel_lwp;
592 1.3.2.2 christos
593 1.3.2.2 christos if (other == selector) {
594 1.3.2.2 christos /* `selector' has already claimed it. */
595 1.3.2.2 christos KASSERT(sip->sel_cpu = sc);
596 1.3.2.2 christos } else if (other == NULL) {
597 1.3.2.2 christos /*
598 1.3.2.2 christos * First named waiter, although there may be unnamed
599 1.3.2.2 christos * waiters (collisions). Issue a memory barrier to
600 1.3.2.2 christos * ensure that we access sel_lwp (above) before other
601 1.3.2.2 christos * fields - this guards against a call to selclear().
602 1.3.2.2 christos */
603 1.3.2.2 christos membar_enter();
604 1.3.2.2 christos sip->sel_lwp = selector;
605 1.3.2.2 christos SLIST_INSERT_HEAD(&selector->l_selwait, sip, sel_chain);
606 1.3.2.2 christos /* Replace selinfo's lock with our chosen CPU's lock. */
607 1.3.2.2 christos sip->sel_cpu = sc;
608 1.3.2.2 christos } else {
609 1.3.2.2 christos /* Multiple waiters: record a collision. */
610 1.3.2.2 christos sip->sel_collision |= sc->sc_mask;
611 1.3.2.2 christos KASSERT(sip->sel_cpu != NULL);
612 1.3.2.2 christos }
613 1.3.2.2 christos }
614 1.3.2.2 christos
615 1.3.2.2 christos /*
616 1.3.2.2 christos * Do a wakeup when a selectable event occurs. Concurrency issues:
617 1.3.2.2 christos *
618 1.3.2.2 christos * As per selrecord(), the caller's object lock is held. If there
619 1.3.2.2 christos * is a named waiter, we must acquire the associated selcpu's lock
620 1.3.2.2 christos * in order to synchronize with selclear() and pollers going to sleep
621 1.3.2.2 christos * in selcommon() and/or pollcommon().
622 1.3.2.2 christos *
623 1.3.2.2 christos * sip->sel_cpu cannot change at this point, as it is only changed
624 1.3.2.2 christos * in selrecord(), and concurrent calls to selrecord() are locked
625 1.3.2.2 christos * out by the caller.
626 1.3.2.2 christos */
627 1.3.2.2 christos void
628 1.3.2.2 christos selnotify(struct selinfo *sip, int events, long knhint)
629 1.3.2.2 christos {
630 1.3.2.2 christos selcpu_t *sc;
631 1.3.2.2 christos uint32_t mask;
632 1.3.2.2 christos int index, oflag, swapin;
633 1.3.2.2 christos lwp_t *l;
634 1.3.2.2 christos
635 1.3.2.2 christos KNOTE(&sip->sel_klist, knhint);
636 1.3.2.2 christos
637 1.3.2.2 christos if (sip->sel_lwp != NULL) {
638 1.3.2.2 christos /* One named LWP is waiting. */
639 1.3.2.2 christos swapin = 0;
640 1.3.2.2 christos sc = sip->sel_cpu;
641 1.3.2.2 christos mutex_spin_enter(&sc->sc_lock);
642 1.3.2.2 christos /* Still there? */
643 1.3.2.2 christos if (sip->sel_lwp != NULL) {
644 1.3.2.2 christos l = sip->sel_lwp;
645 1.3.2.2 christos /*
646 1.3.2.2 christos * If thread is sleeping, wake it up. If it's not
647 1.3.2.2 christos * yet asleep, it will notice the change in state
648 1.3.2.2 christos * and will re-poll the descriptors.
649 1.3.2.2 christos */
650 1.3.2.2 christos oflag = l->l_selflag;
651 1.3.2.2 christos l->l_selflag = SEL_RESET;
652 1.3.2.2 christos if (oflag == SEL_BLOCKING &&
653 1.3.2.2 christos l->l_mutex == &sc->sc_lock) {
654 1.3.2.2 christos KASSERT(l->l_wchan == sc);
655 1.3.2.2 christos swapin = sleepq_unsleep(l, false);
656 1.3.2.2 christos }
657 1.3.2.2 christos }
658 1.3.2.2 christos mutex_spin_exit(&sc->sc_lock);
659 1.3.2.2 christos if (swapin)
660 1.3.2.2 christos uvm_kick_scheduler();
661 1.3.2.2 christos }
662 1.3.2.2 christos
663 1.3.2.2 christos if ((mask = sip->sel_collision) != 0) {
664 1.3.2.2 christos /*
665 1.3.2.2 christos * There was a collision (multiple waiters): we must
666 1.3.2.2 christos * inform all potentially interested waiters.
667 1.3.2.2 christos */
668 1.3.2.2 christos sip->sel_collision = 0;
669 1.3.2.2 christos do {
670 1.3.2.2 christos index = ffs(mask) - 1;
671 1.3.2.2 christos mask &= ~(1 << index);
672 1.3.2.2 christos sc = cpu_lookup_byindex(index)->ci_data.cpu_selcpu;
673 1.3.2.2 christos mutex_spin_enter(&sc->sc_lock);
674 1.3.2.2 christos sc->sc_ncoll++;
675 1.3.2.2 christos sleepq_wake(&sc->sc_sleepq, sc, (u_int)-1);
676 1.3.2.2 christos } while (__predict_false(mask != 0));
677 1.3.2.2 christos }
678 1.3.2.2 christos }
679 1.3.2.2 christos
680 1.3.2.2 christos /*
681 1.3.2.2 christos * Remove an LWP from all objects that it is waiting for. Concurrency
682 1.3.2.2 christos * issues:
683 1.3.2.2 christos *
684 1.3.2.2 christos * The object owner's (e.g. device driver) lock is not held here. Calls
685 1.3.2.2 christos * can be made to selrecord() and we do not synchronize against those
686 1.3.2.2 christos * directly using locks. However, we use `sel_lwp' to lock out changes.
687 1.3.2.2 christos * Before clearing it we must use memory barriers to ensure that we can
688 1.3.2.2 christos * safely traverse the list of selinfo records.
689 1.3.2.2 christos */
690 1.3.2.2 christos static void
691 1.3.2.2 christos selclear(void)
692 1.3.2.2 christos {
693 1.3.2.2 christos struct selinfo *sip, *next;
694 1.3.2.2 christos selcpu_t *sc;
695 1.3.2.2 christos lwp_t *l;
696 1.3.2.2 christos
697 1.3.2.2 christos l = curlwp;
698 1.3.2.2 christos sc = l->l_selcpu;
699 1.3.2.2 christos
700 1.3.2.2 christos mutex_spin_enter(&sc->sc_lock);
701 1.3.2.2 christos for (sip = SLIST_FIRST(&l->l_selwait); sip != NULL; sip = next) {
702 1.3.2.2 christos KASSERT(sip->sel_lwp == l);
703 1.3.2.2 christos KASSERT(sip->sel_cpu == l->l_selcpu);
704 1.3.2.2 christos /*
705 1.3.2.2 christos * Read link to next selinfo record, if any.
706 1.3.2.2 christos * It's no longer safe to touch `sip' after clearing
707 1.3.2.2 christos * `sel_lwp', so ensure that the read of `sel_chain'
708 1.3.2.2 christos * completes before the clearing of sel_lwp becomes
709 1.3.2.2 christos * globally visible.
710 1.3.2.2 christos */
711 1.3.2.2 christos next = SLIST_NEXT(sip, sel_chain);
712 1.3.2.2 christos membar_exit();
713 1.3.2.2 christos /* Release the record for another named waiter to use. */
714 1.3.2.2 christos sip->sel_lwp = NULL;
715 1.3.2.2 christos }
716 1.3.2.2 christos mutex_spin_exit(&sc->sc_lock);
717 1.3.2.2 christos }
718 1.3.2.2 christos
719 1.3.2.2 christos /*
720 1.3.2.2 christos * Initialize the select/poll system calls. Called once for each
721 1.3.2.2 christos * CPU in the system, as they are attached.
722 1.3.2.2 christos */
723 1.3.2.2 christos void
724 1.3.2.2 christos selsysinit(struct cpu_info *ci)
725 1.3.2.2 christos {
726 1.3.2.2 christos selcpu_t *sc;
727 1.3.2.2 christos
728 1.3.2.2 christos sc = kmem_alloc(roundup2(sizeof(selcpu_t), coherency_unit) +
729 1.3.2.2 christos coherency_unit, KM_SLEEP);
730 1.3.2.2 christos sc = (void *)roundup2((uintptr_t)sc, coherency_unit);
731 1.3.2.2 christos mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SCHED);
732 1.3.2.2 christos sleepq_init(&sc->sc_sleepq, &sc->sc_lock);
733 1.3.2.2 christos sc->sc_ncoll = 0;
734 1.3.2.2 christos sc->sc_mask = (1 << cpu_index(ci));
735 1.3.2.2 christos ci->ci_data.cpu_selcpu = sc;
736 1.3.2.2 christos }
737 1.3.2.2 christos
738 1.3.2.2 christos /*
739 1.3.2.2 christos * Initialize a selinfo record.
740 1.3.2.2 christos */
741 1.3.2.2 christos void
742 1.3.2.2 christos selinit(struct selinfo *sip)
743 1.3.2.2 christos {
744 1.3.2.2 christos
745 1.3.2.2 christos memset(sip, 0, sizeof(*sip));
746 1.3.2.2 christos }
747 1.3.2.2 christos
748 1.3.2.2 christos /*
749 1.3.2.2 christos * Destroy a selinfo record. The owning object must not gain new
750 1.3.2.2 christos * references while this is in progress: all activity on the record
751 1.3.2.2 christos * must be stopped.
752 1.3.2.2 christos *
753 1.3.2.2 christos * Concurrency issues: we only need guard against a call to selclear()
754 1.3.2.2 christos * by a thread exiting selcommon() and/or pollcommon(). The caller has
755 1.3.2.2 christos * prevented further references being made to the selinfo record via
756 1.3.2.2 christos * selrecord(), and it won't call selwakeup() again.
757 1.3.2.2 christos */
758 1.3.2.2 christos void
759 1.3.2.2 christos seldestroy(struct selinfo *sip)
760 1.3.2.2 christos {
761 1.3.2.2 christos selcpu_t *sc;
762 1.3.2.2 christos lwp_t *l;
763 1.3.2.2 christos
764 1.3.2.2 christos if (sip->sel_lwp == NULL)
765 1.3.2.2 christos return;
766 1.3.2.2 christos
767 1.3.2.2 christos /*
768 1.3.2.2 christos * Lock out selclear(). The selcpu pointer can't change while
769 1.3.2.2 christos * we are here since it is only ever changed in selrecord(),
770 1.3.2.2 christos * and that will not be entered again for this record because
771 1.3.2.2 christos * it is dying.
772 1.3.2.2 christos */
773 1.3.2.2 christos KASSERT(sip->sel_cpu != NULL);
774 1.3.2.2 christos sc = sip->sel_cpu;
775 1.3.2.2 christos mutex_spin_enter(&sc->sc_lock);
776 1.3.2.2 christos if ((l = sip->sel_lwp) != NULL) {
777 1.3.2.2 christos /*
778 1.3.2.2 christos * This should rarely happen, so although SLIST_REMOVE()
779 1.3.2.2 christos * is slow, using it here is not a problem.
780 1.3.2.2 christos */
781 1.3.2.2 christos KASSERT(l->l_selcpu == sc);
782 1.3.2.2 christos SLIST_REMOVE(&l->l_selwait, sip, selinfo, sel_chain);
783 1.3.2.2 christos sip->sel_lwp = NULL;
784 1.3.2.2 christos }
785 1.3.2.2 christos mutex_spin_exit(&sc->sc_lock);
786 1.3.2.2 christos }
787 1.3.2.2 christos
788 1.3.2.2 christos int
789 1.3.2.2 christos pollsock(struct socket *so, const struct timeval *tvp, int events)
790 1.3.2.2 christos {
791 1.3.2.2 christos int ncoll, error, timo;
792 1.3.2.2 christos struct timeval sleeptv, tv;
793 1.3.2.2 christos selcpu_t *sc;
794 1.3.2.2 christos lwp_t *l;
795 1.3.2.2 christos
796 1.3.2.2 christos timo = 0;
797 1.3.2.2 christos if (tvp != NULL) {
798 1.3.2.2 christos tv = *tvp;
799 1.3.2.2 christos if (inittimeleft(&tv, &sleeptv) == -1)
800 1.3.2.2 christos return EINVAL;
801 1.3.2.2 christos }
802 1.3.2.2 christos
803 1.3.2.2 christos l = curlwp;
804 1.3.2.2 christos sc = l->l_cpu->ci_data.cpu_selcpu;
805 1.3.2.2 christos l->l_selcpu = sc;
806 1.3.2.2 christos SLIST_INIT(&l->l_selwait);
807 1.3.2.2 christos error = 0;
808 1.3.2.2 christos for (;;) {
809 1.3.2.2 christos /*
810 1.3.2.2 christos * No need to lock. If this is overwritten by another
811 1.3.2.2 christos * value while scanning, we will retry below. We only
812 1.3.2.2 christos * need to see exact state from the descriptors that
813 1.3.2.2 christos * we are about to poll, and lock activity resulting
814 1.3.2.2 christos * from fo_poll is enough to provide an up to date value
815 1.3.2.2 christos * for new polling activity.
816 1.3.2.2 christos */
817 1.3.2.2 christos ncoll = sc->sc_ncoll;
818 1.3.2.2 christos l->l_selflag = SEL_SCANNING;
819 1.3.2.2 christos if (sopoll(so, events) != 0)
820 1.3.2.2 christos break;
821 1.3.2.2 christos if (tvp && (timo = gettimeleft(&tv, &sleeptv)) <= 0)
822 1.3.2.2 christos break;
823 1.3.2.2 christos mutex_spin_enter(&sc->sc_lock);
824 1.3.2.2 christos if (l->l_selflag != SEL_SCANNING || sc->sc_ncoll != ncoll) {
825 1.3.2.2 christos mutex_spin_exit(&sc->sc_lock);
826 1.3.2.2 christos continue;
827 1.3.2.2 christos }
828 1.3.2.2 christos l->l_selflag = SEL_BLOCKING;
829 1.3.2.2 christos lwp_lock(l);
830 1.3.2.2 christos lwp_unlock_to(l, &sc->sc_lock);
831 1.3.2.2 christos sleepq_enqueue(&sc->sc_sleepq, sc, "pollsock", &select_sobj);
832 1.3.2.2 christos KERNEL_UNLOCK_ALL(NULL, &l->l_biglocks); /* XXX */
833 1.3.2.2 christos error = sleepq_block(timo, true);
834 1.3.2.2 christos if (error != 0)
835 1.3.2.2 christos break;
836 1.3.2.2 christos }
837 1.3.2.2 christos selclear();
838 1.3.2.2 christos /* poll is not restarted after signals... */
839 1.3.2.2 christos if (error == ERESTART)
840 1.3.2.2 christos error = EINTR;
841 1.3.2.2 christos if (error == EWOULDBLOCK)
842 1.3.2.2 christos error = 0;
843 1.3.2.2 christos return (error);
844 1.3.2.2 christos }
845