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