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