sys_select.c revision 1.3.4.4 1 1.3.4.2 mjf /* $NetBSD: sys_select.c,v 1.3.4.4 2008/06/05 19:14:36 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.4 2008/06/05 19:14:36 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.2 mjf sys_pselect(struct lwp *l, const struct sys_pselect_args *uap, register_t *retval)
128 1.3.4.2 mjf {
129 1.3.4.2 mjf /* {
130 1.3.4.2 mjf syscallarg(int) nd;
131 1.3.4.2 mjf syscallarg(fd_set *) in;
132 1.3.4.2 mjf syscallarg(fd_set *) ou;
133 1.3.4.2 mjf syscallarg(fd_set *) ex;
134 1.3.4.2 mjf syscallarg(const struct timespec *) ts;
135 1.3.4.2 mjf syscallarg(sigset_t *) mask;
136 1.3.4.2 mjf } */
137 1.3.4.2 mjf struct timespec ats;
138 1.3.4.2 mjf struct timeval atv, *tv = NULL;
139 1.3.4.2 mjf sigset_t amask, *mask = NULL;
140 1.3.4.2 mjf int error;
141 1.3.4.2 mjf
142 1.3.4.2 mjf if (SCARG(uap, ts)) {
143 1.3.4.2 mjf error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
144 1.3.4.2 mjf if (error)
145 1.3.4.2 mjf return error;
146 1.3.4.2 mjf atv.tv_sec = ats.tv_sec;
147 1.3.4.2 mjf atv.tv_usec = ats.tv_nsec / 1000;
148 1.3.4.2 mjf tv = &atv;
149 1.3.4.2 mjf }
150 1.3.4.2 mjf if (SCARG(uap, mask) != NULL) {
151 1.3.4.2 mjf error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
152 1.3.4.2 mjf if (error)
153 1.3.4.2 mjf return error;
154 1.3.4.2 mjf mask = &amask;
155 1.3.4.2 mjf }
156 1.3.4.2 mjf
157 1.3.4.2 mjf return selcommon(l, retval, SCARG(uap, nd), SCARG(uap, in),
158 1.3.4.2 mjf SCARG(uap, ou), SCARG(uap, ex), tv, mask);
159 1.3.4.2 mjf }
160 1.3.4.2 mjf
161 1.3.4.2 mjf int
162 1.3.4.2 mjf inittimeleft(struct timeval *tv, struct timeval *sleeptv)
163 1.3.4.2 mjf {
164 1.3.4.2 mjf if (itimerfix(tv))
165 1.3.4.2 mjf return -1;
166 1.3.4.2 mjf getmicrouptime(sleeptv);
167 1.3.4.2 mjf return 0;
168 1.3.4.2 mjf }
169 1.3.4.2 mjf
170 1.3.4.2 mjf int
171 1.3.4.2 mjf gettimeleft(struct timeval *tv, struct timeval *sleeptv)
172 1.3.4.2 mjf {
173 1.3.4.2 mjf /*
174 1.3.4.2 mjf * We have to recalculate the timeout on every retry.
175 1.3.4.2 mjf */
176 1.3.4.2 mjf struct timeval slepttv;
177 1.3.4.2 mjf /*
178 1.3.4.2 mjf * reduce tv by elapsed time
179 1.3.4.2 mjf * based on monotonic time scale
180 1.3.4.2 mjf */
181 1.3.4.2 mjf getmicrouptime(&slepttv);
182 1.3.4.2 mjf timeradd(tv, sleeptv, tv);
183 1.3.4.2 mjf timersub(tv, &slepttv, tv);
184 1.3.4.2 mjf *sleeptv = slepttv;
185 1.3.4.2 mjf return tvtohz(tv);
186 1.3.4.2 mjf }
187 1.3.4.2 mjf
188 1.3.4.2 mjf int
189 1.3.4.2 mjf sys_select(struct lwp *l, const struct sys_select_args *uap, register_t *retval)
190 1.3.4.2 mjf {
191 1.3.4.2 mjf /* {
192 1.3.4.2 mjf syscallarg(int) nd;
193 1.3.4.2 mjf syscallarg(fd_set *) in;
194 1.3.4.2 mjf syscallarg(fd_set *) ou;
195 1.3.4.2 mjf syscallarg(fd_set *) ex;
196 1.3.4.2 mjf syscallarg(struct timeval *) tv;
197 1.3.4.2 mjf } */
198 1.3.4.2 mjf struct timeval atv, *tv = NULL;
199 1.3.4.2 mjf int error;
200 1.3.4.2 mjf
201 1.3.4.2 mjf if (SCARG(uap, tv)) {
202 1.3.4.2 mjf error = copyin(SCARG(uap, tv), (void *)&atv,
203 1.3.4.2 mjf sizeof(atv));
204 1.3.4.2 mjf if (error)
205 1.3.4.2 mjf return error;
206 1.3.4.2 mjf tv = &atv;
207 1.3.4.2 mjf }
208 1.3.4.2 mjf
209 1.3.4.2 mjf return selcommon(l, retval, SCARG(uap, nd), SCARG(uap, in),
210 1.3.4.2 mjf SCARG(uap, ou), SCARG(uap, ex), tv, NULL);
211 1.3.4.2 mjf }
212 1.3.4.2 mjf
213 1.3.4.2 mjf int
214 1.3.4.2 mjf selcommon(lwp_t *l, register_t *retval, int nd, fd_set *u_in,
215 1.3.4.2 mjf fd_set *u_ou, fd_set *u_ex, struct timeval *tv, sigset_t *mask)
216 1.3.4.2 mjf {
217 1.3.4.2 mjf char smallbits[howmany(FD_SETSIZE, NFDBITS) *
218 1.3.4.2 mjf sizeof(fd_mask) * 6];
219 1.3.4.2 mjf proc_t * const p = l->l_proc;
220 1.3.4.2 mjf char *bits;
221 1.3.4.2 mjf int ncoll, error, timo;
222 1.3.4.2 mjf size_t ni;
223 1.3.4.2 mjf sigset_t oldmask;
224 1.3.4.2 mjf struct timeval sleeptv;
225 1.3.4.2 mjf selcpu_t *sc;
226 1.3.4.2 mjf
227 1.3.4.2 mjf error = 0;
228 1.3.4.2 mjf if (nd < 0)
229 1.3.4.2 mjf return (EINVAL);
230 1.3.4.2 mjf if (nd > p->p_fd->fd_nfiles) {
231 1.3.4.2 mjf /* forgiving; slightly wrong */
232 1.3.4.2 mjf nd = p->p_fd->fd_nfiles;
233 1.3.4.2 mjf }
234 1.3.4.2 mjf ni = howmany(nd, NFDBITS) * sizeof(fd_mask);
235 1.3.4.4 mjf if (ni * 6 > sizeof(smallbits)) {
236 1.3.4.2 mjf bits = kmem_alloc(ni * 6, KM_SLEEP);
237 1.3.4.4 mjf if (bits == NULL)
238 1.3.4.4 mjf return ENOMEM;
239 1.3.4.4 mjf } else
240 1.3.4.2 mjf bits = smallbits;
241 1.3.4.2 mjf
242 1.3.4.2 mjf #define getbits(name, x) \
243 1.3.4.2 mjf if (u_ ## name) { \
244 1.3.4.2 mjf error = copyin(u_ ## name, bits + ni * x, ni); \
245 1.3.4.2 mjf if (error) \
246 1.3.4.2 mjf goto done; \
247 1.3.4.2 mjf } else \
248 1.3.4.2 mjf memset(bits + ni * x, 0, ni);
249 1.3.4.2 mjf getbits(in, 0);
250 1.3.4.2 mjf getbits(ou, 1);
251 1.3.4.2 mjf getbits(ex, 2);
252 1.3.4.2 mjf #undef getbits
253 1.3.4.2 mjf
254 1.3.4.2 mjf timo = 0;
255 1.3.4.2 mjf if (tv && inittimeleft(tv, &sleeptv) == -1) {
256 1.3.4.2 mjf error = EINVAL;
257 1.3.4.2 mjf goto done;
258 1.3.4.2 mjf }
259 1.3.4.2 mjf
260 1.3.4.2 mjf if (mask) {
261 1.3.4.2 mjf sigminusset(&sigcantmask, mask);
262 1.3.4.3 mjf mutex_enter(p->p_lock);
263 1.3.4.2 mjf oldmask = l->l_sigmask;
264 1.3.4.2 mjf l->l_sigmask = *mask;
265 1.3.4.3 mjf mutex_exit(p->p_lock);
266 1.3.4.2 mjf } else
267 1.3.4.2 mjf oldmask = l->l_sigmask; /* XXXgcc */
268 1.3.4.2 mjf
269 1.3.4.2 mjf sc = curcpu()->ci_data.cpu_selcpu;
270 1.3.4.2 mjf l->l_selcpu = sc;
271 1.3.4.2 mjf SLIST_INIT(&l->l_selwait);
272 1.3.4.2 mjf for (;;) {
273 1.3.4.2 mjf /*
274 1.3.4.2 mjf * No need to lock. If this is overwritten by another
275 1.3.4.2 mjf * value while scanning, we will retry below. We only
276 1.3.4.2 mjf * need to see exact state from the descriptors that
277 1.3.4.2 mjf * we are about to poll, and lock activity resulting
278 1.3.4.2 mjf * from fo_poll is enough to provide an up to date value
279 1.3.4.2 mjf * for new polling activity.
280 1.3.4.2 mjf */
281 1.3.4.2 mjf l->l_selflag = SEL_SCANNING;
282 1.3.4.2 mjf ncoll = sc->sc_ncoll;
283 1.3.4.2 mjf
284 1.3.4.2 mjf error = selscan(l, (fd_mask *)(bits + ni * 0),
285 1.3.4.2 mjf (fd_mask *)(bits + ni * 3), nd, retval);
286 1.3.4.2 mjf
287 1.3.4.2 mjf if (error || *retval)
288 1.3.4.2 mjf break;
289 1.3.4.2 mjf if (tv && (timo = gettimeleft(tv, &sleeptv)) <= 0)
290 1.3.4.2 mjf break;
291 1.3.4.2 mjf mutex_spin_enter(&sc->sc_lock);
292 1.3.4.2 mjf if (l->l_selflag != SEL_SCANNING || sc->sc_ncoll != ncoll) {
293 1.3.4.2 mjf mutex_spin_exit(&sc->sc_lock);
294 1.3.4.2 mjf continue;
295 1.3.4.2 mjf }
296 1.3.4.2 mjf l->l_selflag = SEL_BLOCKING;
297 1.3.4.3 mjf l->l_kpriority = true;
298 1.3.4.3 mjf sleepq_enter(&sc->sc_sleepq, l, &sc->sc_lock);
299 1.3.4.2 mjf sleepq_enqueue(&sc->sc_sleepq, sc, "select", &select_sobj);
300 1.3.4.2 mjf error = sleepq_block(timo, true);
301 1.3.4.2 mjf if (error != 0)
302 1.3.4.2 mjf break;
303 1.3.4.2 mjf }
304 1.3.4.2 mjf selclear();
305 1.3.4.2 mjf
306 1.3.4.2 mjf if (mask) {
307 1.3.4.3 mjf mutex_enter(p->p_lock);
308 1.3.4.2 mjf l->l_sigmask = oldmask;
309 1.3.4.3 mjf mutex_exit(p->p_lock);
310 1.3.4.2 mjf }
311 1.3.4.2 mjf
312 1.3.4.2 mjf done:
313 1.3.4.2 mjf /* select is not restarted after signals... */
314 1.3.4.2 mjf if (error == ERESTART)
315 1.3.4.2 mjf error = EINTR;
316 1.3.4.2 mjf if (error == EWOULDBLOCK)
317 1.3.4.2 mjf error = 0;
318 1.3.4.2 mjf if (error == 0 && u_in != NULL)
319 1.3.4.2 mjf error = copyout(bits + ni * 3, u_in, ni);
320 1.3.4.2 mjf if (error == 0 && u_ou != NULL)
321 1.3.4.2 mjf error = copyout(bits + ni * 4, u_ou, ni);
322 1.3.4.2 mjf if (error == 0 && u_ex != NULL)
323 1.3.4.2 mjf error = copyout(bits + ni * 5, u_ex, ni);
324 1.3.4.2 mjf if (bits != smallbits)
325 1.3.4.2 mjf kmem_free(bits, ni * 6);
326 1.3.4.2 mjf return (error);
327 1.3.4.2 mjf }
328 1.3.4.2 mjf
329 1.3.4.2 mjf int
330 1.3.4.2 mjf selscan(lwp_t *l, fd_mask *ibitp, fd_mask *obitp, int nfd,
331 1.3.4.2 mjf register_t *retval)
332 1.3.4.2 mjf {
333 1.3.4.2 mjf static const int flag[3] = { POLLRDNORM | POLLHUP | POLLERR,
334 1.3.4.2 mjf POLLWRNORM | POLLHUP | POLLERR,
335 1.3.4.2 mjf POLLRDBAND };
336 1.3.4.2 mjf int msk, i, j, fd, n;
337 1.3.4.2 mjf fd_mask ibits, obits;
338 1.3.4.2 mjf file_t *fp;
339 1.3.4.2 mjf
340 1.3.4.2 mjf n = 0;
341 1.3.4.2 mjf for (msk = 0; msk < 3; msk++) {
342 1.3.4.2 mjf for (i = 0; i < nfd; i += NFDBITS) {
343 1.3.4.2 mjf ibits = *ibitp++;
344 1.3.4.2 mjf obits = 0;
345 1.3.4.2 mjf while ((j = ffs(ibits)) && (fd = i + --j) < nfd) {
346 1.3.4.2 mjf ibits &= ~(1 << j);
347 1.3.4.2 mjf if ((fp = fd_getfile(fd)) == NULL)
348 1.3.4.2 mjf return (EBADF);
349 1.3.4.2 mjf if ((*fp->f_ops->fo_poll)(fp, flag[msk])) {
350 1.3.4.2 mjf obits |= (1 << j);
351 1.3.4.2 mjf n++;
352 1.3.4.2 mjf }
353 1.3.4.2 mjf fd_putfile(fd);
354 1.3.4.2 mjf }
355 1.3.4.2 mjf *obitp++ = obits;
356 1.3.4.2 mjf }
357 1.3.4.2 mjf }
358 1.3.4.2 mjf *retval = n;
359 1.3.4.2 mjf return (0);
360 1.3.4.2 mjf }
361 1.3.4.2 mjf
362 1.3.4.2 mjf /*
363 1.3.4.2 mjf * Poll system call.
364 1.3.4.2 mjf */
365 1.3.4.2 mjf int
366 1.3.4.2 mjf sys_poll(struct lwp *l, const struct sys_poll_args *uap, register_t *retval)
367 1.3.4.2 mjf {
368 1.3.4.2 mjf /* {
369 1.3.4.2 mjf syscallarg(struct pollfd *) fds;
370 1.3.4.2 mjf syscallarg(u_int) nfds;
371 1.3.4.2 mjf syscallarg(int) timeout;
372 1.3.4.2 mjf } */
373 1.3.4.2 mjf struct timeval atv, *tv = NULL;
374 1.3.4.2 mjf
375 1.3.4.2 mjf if (SCARG(uap, timeout) != INFTIM) {
376 1.3.4.2 mjf atv.tv_sec = SCARG(uap, timeout) / 1000;
377 1.3.4.2 mjf atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
378 1.3.4.2 mjf tv = &atv;
379 1.3.4.2 mjf }
380 1.3.4.2 mjf
381 1.3.4.2 mjf return pollcommon(l, retval, SCARG(uap, fds), SCARG(uap, nfds),
382 1.3.4.2 mjf tv, NULL);
383 1.3.4.2 mjf }
384 1.3.4.2 mjf
385 1.3.4.2 mjf /*
386 1.3.4.2 mjf * Poll system call.
387 1.3.4.2 mjf */
388 1.3.4.2 mjf int
389 1.3.4.2 mjf sys_pollts(struct lwp *l, const struct sys_pollts_args *uap, register_t *retval)
390 1.3.4.2 mjf {
391 1.3.4.2 mjf /* {
392 1.3.4.2 mjf syscallarg(struct pollfd *) fds;
393 1.3.4.2 mjf syscallarg(u_int) nfds;
394 1.3.4.2 mjf syscallarg(const struct timespec *) ts;
395 1.3.4.2 mjf syscallarg(const sigset_t *) mask;
396 1.3.4.2 mjf } */
397 1.3.4.2 mjf struct timespec ats;
398 1.3.4.2 mjf struct timeval atv, *tv = NULL;
399 1.3.4.2 mjf sigset_t amask, *mask = NULL;
400 1.3.4.2 mjf int error;
401 1.3.4.2 mjf
402 1.3.4.2 mjf if (SCARG(uap, ts)) {
403 1.3.4.2 mjf error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
404 1.3.4.2 mjf if (error)
405 1.3.4.2 mjf return error;
406 1.3.4.2 mjf atv.tv_sec = ats.tv_sec;
407 1.3.4.2 mjf atv.tv_usec = ats.tv_nsec / 1000;
408 1.3.4.2 mjf tv = &atv;
409 1.3.4.2 mjf }
410 1.3.4.2 mjf if (SCARG(uap, mask)) {
411 1.3.4.2 mjf error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
412 1.3.4.2 mjf if (error)
413 1.3.4.2 mjf return error;
414 1.3.4.2 mjf mask = &amask;
415 1.3.4.2 mjf }
416 1.3.4.2 mjf
417 1.3.4.2 mjf return pollcommon(l, retval, SCARG(uap, fds), SCARG(uap, nfds),
418 1.3.4.2 mjf tv, mask);
419 1.3.4.2 mjf }
420 1.3.4.2 mjf
421 1.3.4.2 mjf int
422 1.3.4.2 mjf pollcommon(lwp_t *l, register_t *retval,
423 1.3.4.2 mjf struct pollfd *u_fds, u_int nfds,
424 1.3.4.2 mjf struct timeval *tv, sigset_t *mask)
425 1.3.4.2 mjf {
426 1.3.4.2 mjf char smallbits[32 * sizeof(struct pollfd)];
427 1.3.4.2 mjf proc_t * const p = l->l_proc;
428 1.3.4.2 mjf void * bits;
429 1.3.4.2 mjf sigset_t oldmask;
430 1.3.4.2 mjf int ncoll, error, timo;
431 1.3.4.2 mjf size_t ni;
432 1.3.4.2 mjf struct timeval sleeptv;
433 1.3.4.2 mjf selcpu_t *sc;
434 1.3.4.2 mjf
435 1.3.4.2 mjf if (nfds > p->p_fd->fd_nfiles) {
436 1.3.4.2 mjf /* forgiving; slightly wrong */
437 1.3.4.2 mjf nfds = p->p_fd->fd_nfiles;
438 1.3.4.2 mjf }
439 1.3.4.2 mjf ni = nfds * sizeof(struct pollfd);
440 1.3.4.4 mjf if (ni > sizeof(smallbits)) {
441 1.3.4.2 mjf bits = kmem_alloc(ni, KM_SLEEP);
442 1.3.4.4 mjf if (bits == NULL)
443 1.3.4.4 mjf return ENOMEM;
444 1.3.4.4 mjf } else
445 1.3.4.2 mjf bits = smallbits;
446 1.3.4.2 mjf
447 1.3.4.2 mjf error = copyin(u_fds, bits, ni);
448 1.3.4.2 mjf if (error)
449 1.3.4.2 mjf goto done;
450 1.3.4.2 mjf
451 1.3.4.2 mjf timo = 0;
452 1.3.4.2 mjf if (tv && inittimeleft(tv, &sleeptv) == -1) {
453 1.3.4.2 mjf error = EINVAL;
454 1.3.4.2 mjf goto done;
455 1.3.4.2 mjf }
456 1.3.4.2 mjf
457 1.3.4.2 mjf if (mask) {
458 1.3.4.2 mjf sigminusset(&sigcantmask, mask);
459 1.3.4.3 mjf mutex_enter(p->p_lock);
460 1.3.4.2 mjf oldmask = l->l_sigmask;
461 1.3.4.2 mjf l->l_sigmask = *mask;
462 1.3.4.3 mjf mutex_exit(p->p_lock);
463 1.3.4.2 mjf } else
464 1.3.4.2 mjf oldmask = l->l_sigmask; /* XXXgcc */
465 1.3.4.2 mjf
466 1.3.4.2 mjf sc = curcpu()->ci_data.cpu_selcpu;
467 1.3.4.2 mjf l->l_selcpu = sc;
468 1.3.4.2 mjf SLIST_INIT(&l->l_selwait);
469 1.3.4.2 mjf for (;;) {
470 1.3.4.2 mjf /*
471 1.3.4.2 mjf * No need to lock. If this is overwritten by another
472 1.3.4.2 mjf * value while scanning, we will retry below. We only
473 1.3.4.2 mjf * need to see exact state from the descriptors that
474 1.3.4.2 mjf * we are about to poll, and lock activity resulting
475 1.3.4.2 mjf * from fo_poll is enough to provide an up to date value
476 1.3.4.2 mjf * for new polling activity.
477 1.3.4.2 mjf */
478 1.3.4.2 mjf ncoll = sc->sc_ncoll;
479 1.3.4.2 mjf l->l_selflag = SEL_SCANNING;
480 1.3.4.2 mjf
481 1.3.4.2 mjf error = pollscan(l, (struct pollfd *)bits, nfds, retval);
482 1.3.4.2 mjf
483 1.3.4.2 mjf if (error || *retval)
484 1.3.4.2 mjf break;
485 1.3.4.2 mjf if (tv && (timo = gettimeleft(tv, &sleeptv)) <= 0)
486 1.3.4.2 mjf break;
487 1.3.4.2 mjf mutex_spin_enter(&sc->sc_lock);
488 1.3.4.2 mjf if (l->l_selflag != SEL_SCANNING || sc->sc_ncoll != ncoll) {
489 1.3.4.2 mjf mutex_spin_exit(&sc->sc_lock);
490 1.3.4.2 mjf continue;
491 1.3.4.2 mjf }
492 1.3.4.2 mjf l->l_selflag = SEL_BLOCKING;
493 1.3.4.3 mjf l->l_kpriority = true;
494 1.3.4.3 mjf sleepq_enter(&sc->sc_sleepq, l, &sc->sc_lock);
495 1.3.4.2 mjf sleepq_enqueue(&sc->sc_sleepq, sc, "select", &select_sobj);
496 1.3.4.2 mjf error = sleepq_block(timo, true);
497 1.3.4.2 mjf if (error != 0)
498 1.3.4.2 mjf break;
499 1.3.4.2 mjf }
500 1.3.4.2 mjf selclear();
501 1.3.4.2 mjf
502 1.3.4.2 mjf if (mask) {
503 1.3.4.3 mjf mutex_enter(p->p_lock);
504 1.3.4.2 mjf l->l_sigmask = oldmask;
505 1.3.4.3 mjf mutex_exit(p->p_lock);
506 1.3.4.2 mjf }
507 1.3.4.2 mjf done:
508 1.3.4.2 mjf /* poll is not restarted after signals... */
509 1.3.4.2 mjf if (error == ERESTART)
510 1.3.4.2 mjf error = EINTR;
511 1.3.4.2 mjf if (error == EWOULDBLOCK)
512 1.3.4.2 mjf error = 0;
513 1.3.4.2 mjf if (error == 0)
514 1.3.4.2 mjf error = copyout(bits, u_fds, ni);
515 1.3.4.2 mjf if (bits != smallbits)
516 1.3.4.2 mjf kmem_free(bits, ni);
517 1.3.4.2 mjf return (error);
518 1.3.4.2 mjf }
519 1.3.4.2 mjf
520 1.3.4.2 mjf int
521 1.3.4.2 mjf pollscan(lwp_t *l, struct pollfd *fds, int nfd, register_t *retval)
522 1.3.4.2 mjf {
523 1.3.4.2 mjf int i, n;
524 1.3.4.2 mjf file_t *fp;
525 1.3.4.2 mjf
526 1.3.4.2 mjf n = 0;
527 1.3.4.2 mjf for (i = 0; i < nfd; i++, fds++) {
528 1.3.4.2 mjf if (fds->fd < 0) {
529 1.3.4.2 mjf fds->revents = 0;
530 1.3.4.2 mjf } else if ((fp = fd_getfile(fds->fd)) == NULL) {
531 1.3.4.2 mjf fds->revents = POLLNVAL;
532 1.3.4.2 mjf n++;
533 1.3.4.2 mjf } else {
534 1.3.4.2 mjf fds->revents = (*fp->f_ops->fo_poll)(fp,
535 1.3.4.2 mjf fds->events | POLLERR | POLLHUP);
536 1.3.4.2 mjf if (fds->revents != 0)
537 1.3.4.2 mjf n++;
538 1.3.4.2 mjf fd_putfile(fds->fd);
539 1.3.4.2 mjf }
540 1.3.4.2 mjf }
541 1.3.4.2 mjf *retval = n;
542 1.3.4.2 mjf return (0);
543 1.3.4.2 mjf }
544 1.3.4.2 mjf
545 1.3.4.2 mjf /*ARGSUSED*/
546 1.3.4.2 mjf int
547 1.3.4.2 mjf seltrue(dev_t dev, int events, lwp_t *l)
548 1.3.4.2 mjf {
549 1.3.4.2 mjf
550 1.3.4.2 mjf return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
551 1.3.4.2 mjf }
552 1.3.4.2 mjf
553 1.3.4.2 mjf /*
554 1.3.4.2 mjf * Record a select request. Concurrency issues:
555 1.3.4.2 mjf *
556 1.3.4.2 mjf * The caller holds the same lock across calls to selrecord() and
557 1.3.4.3 mjf * selnotify(), so we don't need to consider a concurrent wakeup
558 1.3.4.2 mjf * while in this routine.
559 1.3.4.2 mjf *
560 1.3.4.2 mjf * The only activity we need to guard against is selclear(), called by
561 1.3.4.2 mjf * another thread that is exiting selcommon() or pollcommon().
562 1.3.4.2 mjf * `sel_lwp' can only become non-NULL while the caller's lock is held,
563 1.3.4.2 mjf * so it cannot become non-NULL due to a change made by another thread
564 1.3.4.2 mjf * while we are in this routine. It can only become _NULL_ due to a
565 1.3.4.2 mjf * call to selclear().
566 1.3.4.2 mjf *
567 1.3.4.2 mjf * If it is non-NULL and != selector there is the potential for
568 1.3.4.2 mjf * selclear() to be called by another thread. If either of those
569 1.3.4.2 mjf * conditions are true, we're not interested in touching the `named
570 1.3.4.2 mjf * waiter' part of the selinfo record because we need to record a
571 1.3.4.2 mjf * collision. Hence there is no need for additional locking in this
572 1.3.4.2 mjf * routine.
573 1.3.4.2 mjf */
574 1.3.4.2 mjf void
575 1.3.4.2 mjf selrecord(lwp_t *selector, struct selinfo *sip)
576 1.3.4.2 mjf {
577 1.3.4.2 mjf selcpu_t *sc;
578 1.3.4.2 mjf lwp_t *other;
579 1.3.4.2 mjf
580 1.3.4.2 mjf KASSERT(selector == curlwp);
581 1.3.4.2 mjf
582 1.3.4.2 mjf sc = selector->l_selcpu;
583 1.3.4.2 mjf other = sip->sel_lwp;
584 1.3.4.2 mjf
585 1.3.4.2 mjf if (other == selector) {
586 1.3.4.2 mjf /* `selector' has already claimed it. */
587 1.3.4.2 mjf KASSERT(sip->sel_cpu = sc);
588 1.3.4.2 mjf } else if (other == NULL) {
589 1.3.4.2 mjf /*
590 1.3.4.2 mjf * First named waiter, although there may be unnamed
591 1.3.4.2 mjf * waiters (collisions). Issue a memory barrier to
592 1.3.4.2 mjf * ensure that we access sel_lwp (above) before other
593 1.3.4.2 mjf * fields - this guards against a call to selclear().
594 1.3.4.2 mjf */
595 1.3.4.2 mjf membar_enter();
596 1.3.4.2 mjf sip->sel_lwp = selector;
597 1.3.4.2 mjf SLIST_INSERT_HEAD(&selector->l_selwait, sip, sel_chain);
598 1.3.4.2 mjf /* Replace selinfo's lock with our chosen CPU's lock. */
599 1.3.4.2 mjf sip->sel_cpu = sc;
600 1.3.4.2 mjf } else {
601 1.3.4.2 mjf /* Multiple waiters: record a collision. */
602 1.3.4.2 mjf sip->sel_collision |= sc->sc_mask;
603 1.3.4.2 mjf KASSERT(sip->sel_cpu != NULL);
604 1.3.4.2 mjf }
605 1.3.4.2 mjf }
606 1.3.4.2 mjf
607 1.3.4.2 mjf /*
608 1.3.4.2 mjf * Do a wakeup when a selectable event occurs. Concurrency issues:
609 1.3.4.2 mjf *
610 1.3.4.2 mjf * As per selrecord(), the caller's object lock is held. If there
611 1.3.4.2 mjf * is a named waiter, we must acquire the associated selcpu's lock
612 1.3.4.2 mjf * in order to synchronize with selclear() and pollers going to sleep
613 1.3.4.2 mjf * in selcommon() and/or pollcommon().
614 1.3.4.2 mjf *
615 1.3.4.2 mjf * sip->sel_cpu cannot change at this point, as it is only changed
616 1.3.4.2 mjf * in selrecord(), and concurrent calls to selrecord() are locked
617 1.3.4.2 mjf * out by the caller.
618 1.3.4.2 mjf */
619 1.3.4.2 mjf void
620 1.3.4.2 mjf selnotify(struct selinfo *sip, int events, long knhint)
621 1.3.4.2 mjf {
622 1.3.4.2 mjf selcpu_t *sc;
623 1.3.4.2 mjf uint32_t mask;
624 1.3.4.2 mjf int index, oflag, swapin;
625 1.3.4.2 mjf lwp_t *l;
626 1.3.4.2 mjf
627 1.3.4.2 mjf KNOTE(&sip->sel_klist, knhint);
628 1.3.4.2 mjf
629 1.3.4.2 mjf if (sip->sel_lwp != NULL) {
630 1.3.4.2 mjf /* One named LWP is waiting. */
631 1.3.4.2 mjf swapin = 0;
632 1.3.4.2 mjf sc = sip->sel_cpu;
633 1.3.4.2 mjf mutex_spin_enter(&sc->sc_lock);
634 1.3.4.2 mjf /* Still there? */
635 1.3.4.2 mjf if (sip->sel_lwp != NULL) {
636 1.3.4.2 mjf l = sip->sel_lwp;
637 1.3.4.2 mjf /*
638 1.3.4.2 mjf * If thread is sleeping, wake it up. If it's not
639 1.3.4.2 mjf * yet asleep, it will notice the change in state
640 1.3.4.2 mjf * and will re-poll the descriptors.
641 1.3.4.2 mjf */
642 1.3.4.2 mjf oflag = l->l_selflag;
643 1.3.4.2 mjf l->l_selflag = SEL_RESET;
644 1.3.4.2 mjf if (oflag == SEL_BLOCKING &&
645 1.3.4.2 mjf l->l_mutex == &sc->sc_lock) {
646 1.3.4.2 mjf KASSERT(l->l_wchan == sc);
647 1.3.4.2 mjf swapin = sleepq_unsleep(l, false);
648 1.3.4.2 mjf }
649 1.3.4.2 mjf }
650 1.3.4.2 mjf mutex_spin_exit(&sc->sc_lock);
651 1.3.4.2 mjf if (swapin)
652 1.3.4.2 mjf uvm_kick_scheduler();
653 1.3.4.2 mjf }
654 1.3.4.2 mjf
655 1.3.4.2 mjf if ((mask = sip->sel_collision) != 0) {
656 1.3.4.2 mjf /*
657 1.3.4.2 mjf * There was a collision (multiple waiters): we must
658 1.3.4.2 mjf * inform all potentially interested waiters.
659 1.3.4.2 mjf */
660 1.3.4.2 mjf sip->sel_collision = 0;
661 1.3.4.2 mjf do {
662 1.3.4.2 mjf index = ffs(mask) - 1;
663 1.3.4.2 mjf mask &= ~(1 << index);
664 1.3.4.2 mjf sc = cpu_lookup_byindex(index)->ci_data.cpu_selcpu;
665 1.3.4.2 mjf mutex_spin_enter(&sc->sc_lock);
666 1.3.4.2 mjf sc->sc_ncoll++;
667 1.3.4.3 mjf sleepq_wake(&sc->sc_sleepq, sc, (u_int)-1,
668 1.3.4.3 mjf &sc->sc_lock);
669 1.3.4.2 mjf } while (__predict_false(mask != 0));
670 1.3.4.2 mjf }
671 1.3.4.2 mjf }
672 1.3.4.2 mjf
673 1.3.4.2 mjf /*
674 1.3.4.2 mjf * Remove an LWP from all objects that it is waiting for. Concurrency
675 1.3.4.2 mjf * issues:
676 1.3.4.2 mjf *
677 1.3.4.2 mjf * The object owner's (e.g. device driver) lock is not held here. Calls
678 1.3.4.2 mjf * can be made to selrecord() and we do not synchronize against those
679 1.3.4.2 mjf * directly using locks. However, we use `sel_lwp' to lock out changes.
680 1.3.4.2 mjf * Before clearing it we must use memory barriers to ensure that we can
681 1.3.4.2 mjf * safely traverse the list of selinfo records.
682 1.3.4.2 mjf */
683 1.3.4.2 mjf static void
684 1.3.4.2 mjf selclear(void)
685 1.3.4.2 mjf {
686 1.3.4.2 mjf struct selinfo *sip, *next;
687 1.3.4.2 mjf selcpu_t *sc;
688 1.3.4.2 mjf lwp_t *l;
689 1.3.4.2 mjf
690 1.3.4.2 mjf l = curlwp;
691 1.3.4.2 mjf sc = l->l_selcpu;
692 1.3.4.2 mjf
693 1.3.4.2 mjf mutex_spin_enter(&sc->sc_lock);
694 1.3.4.2 mjf for (sip = SLIST_FIRST(&l->l_selwait); sip != NULL; sip = next) {
695 1.3.4.2 mjf KASSERT(sip->sel_lwp == l);
696 1.3.4.2 mjf KASSERT(sip->sel_cpu == l->l_selcpu);
697 1.3.4.2 mjf /*
698 1.3.4.2 mjf * Read link to next selinfo record, if any.
699 1.3.4.2 mjf * It's no longer safe to touch `sip' after clearing
700 1.3.4.2 mjf * `sel_lwp', so ensure that the read of `sel_chain'
701 1.3.4.2 mjf * completes before the clearing of sel_lwp becomes
702 1.3.4.2 mjf * globally visible.
703 1.3.4.2 mjf */
704 1.3.4.2 mjf next = SLIST_NEXT(sip, sel_chain);
705 1.3.4.2 mjf membar_exit();
706 1.3.4.2 mjf /* Release the record for another named waiter to use. */
707 1.3.4.2 mjf sip->sel_lwp = NULL;
708 1.3.4.2 mjf }
709 1.3.4.2 mjf mutex_spin_exit(&sc->sc_lock);
710 1.3.4.2 mjf }
711 1.3.4.2 mjf
712 1.3.4.2 mjf /*
713 1.3.4.2 mjf * Initialize the select/poll system calls. Called once for each
714 1.3.4.2 mjf * CPU in the system, as they are attached.
715 1.3.4.2 mjf */
716 1.3.4.2 mjf void
717 1.3.4.2 mjf selsysinit(struct cpu_info *ci)
718 1.3.4.2 mjf {
719 1.3.4.2 mjf selcpu_t *sc;
720 1.3.4.2 mjf
721 1.3.4.2 mjf sc = kmem_alloc(roundup2(sizeof(selcpu_t), coherency_unit) +
722 1.3.4.2 mjf coherency_unit, KM_SLEEP);
723 1.3.4.2 mjf sc = (void *)roundup2((uintptr_t)sc, coherency_unit);
724 1.3.4.2 mjf mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SCHED);
725 1.3.4.3 mjf sleepq_init(&sc->sc_sleepq);
726 1.3.4.2 mjf sc->sc_ncoll = 0;
727 1.3.4.2 mjf sc->sc_mask = (1 << cpu_index(ci));
728 1.3.4.2 mjf ci->ci_data.cpu_selcpu = sc;
729 1.3.4.2 mjf }
730 1.3.4.2 mjf
731 1.3.4.2 mjf /*
732 1.3.4.2 mjf * Initialize a selinfo record.
733 1.3.4.2 mjf */
734 1.3.4.2 mjf void
735 1.3.4.2 mjf selinit(struct selinfo *sip)
736 1.3.4.2 mjf {
737 1.3.4.2 mjf
738 1.3.4.2 mjf memset(sip, 0, sizeof(*sip));
739 1.3.4.2 mjf }
740 1.3.4.2 mjf
741 1.3.4.2 mjf /*
742 1.3.4.2 mjf * Destroy a selinfo record. The owning object must not gain new
743 1.3.4.2 mjf * references while this is in progress: all activity on the record
744 1.3.4.2 mjf * must be stopped.
745 1.3.4.2 mjf *
746 1.3.4.2 mjf * Concurrency issues: we only need guard against a call to selclear()
747 1.3.4.2 mjf * by a thread exiting selcommon() and/or pollcommon(). The caller has
748 1.3.4.2 mjf * prevented further references being made to the selinfo record via
749 1.3.4.2 mjf * selrecord(), and it won't call selwakeup() again.
750 1.3.4.2 mjf */
751 1.3.4.2 mjf void
752 1.3.4.2 mjf seldestroy(struct selinfo *sip)
753 1.3.4.2 mjf {
754 1.3.4.2 mjf selcpu_t *sc;
755 1.3.4.2 mjf lwp_t *l;
756 1.3.4.2 mjf
757 1.3.4.2 mjf if (sip->sel_lwp == NULL)
758 1.3.4.2 mjf return;
759 1.3.4.2 mjf
760 1.3.4.2 mjf /*
761 1.3.4.2 mjf * Lock out selclear(). The selcpu pointer can't change while
762 1.3.4.2 mjf * we are here since it is only ever changed in selrecord(),
763 1.3.4.2 mjf * and that will not be entered again for this record because
764 1.3.4.2 mjf * it is dying.
765 1.3.4.2 mjf */
766 1.3.4.2 mjf KASSERT(sip->sel_cpu != NULL);
767 1.3.4.2 mjf sc = sip->sel_cpu;
768 1.3.4.2 mjf mutex_spin_enter(&sc->sc_lock);
769 1.3.4.2 mjf if ((l = sip->sel_lwp) != NULL) {
770 1.3.4.2 mjf /*
771 1.3.4.2 mjf * This should rarely happen, so although SLIST_REMOVE()
772 1.3.4.2 mjf * is slow, using it here is not a problem.
773 1.3.4.2 mjf */
774 1.3.4.2 mjf KASSERT(l->l_selcpu == sc);
775 1.3.4.2 mjf SLIST_REMOVE(&l->l_selwait, sip, selinfo, sel_chain);
776 1.3.4.2 mjf sip->sel_lwp = NULL;
777 1.3.4.2 mjf }
778 1.3.4.2 mjf mutex_spin_exit(&sc->sc_lock);
779 1.3.4.2 mjf }
780 1.3.4.2 mjf
781 1.3.4.2 mjf int
782 1.3.4.2 mjf pollsock(struct socket *so, const struct timeval *tvp, int events)
783 1.3.4.2 mjf {
784 1.3.4.2 mjf int ncoll, error, timo;
785 1.3.4.2 mjf struct timeval sleeptv, tv;
786 1.3.4.2 mjf selcpu_t *sc;
787 1.3.4.2 mjf lwp_t *l;
788 1.3.4.2 mjf
789 1.3.4.2 mjf timo = 0;
790 1.3.4.2 mjf if (tvp != NULL) {
791 1.3.4.2 mjf tv = *tvp;
792 1.3.4.2 mjf if (inittimeleft(&tv, &sleeptv) == -1)
793 1.3.4.2 mjf return EINVAL;
794 1.3.4.2 mjf }
795 1.3.4.2 mjf
796 1.3.4.2 mjf l = curlwp;
797 1.3.4.2 mjf sc = l->l_cpu->ci_data.cpu_selcpu;
798 1.3.4.2 mjf l->l_selcpu = sc;
799 1.3.4.2 mjf SLIST_INIT(&l->l_selwait);
800 1.3.4.2 mjf error = 0;
801 1.3.4.2 mjf for (;;) {
802 1.3.4.2 mjf /*
803 1.3.4.2 mjf * No need to lock. If this is overwritten by another
804 1.3.4.2 mjf * value while scanning, we will retry below. We only
805 1.3.4.2 mjf * need to see exact state from the descriptors that
806 1.3.4.2 mjf * we are about to poll, and lock activity resulting
807 1.3.4.2 mjf * from fo_poll is enough to provide an up to date value
808 1.3.4.2 mjf * for new polling activity.
809 1.3.4.2 mjf */
810 1.3.4.2 mjf ncoll = sc->sc_ncoll;
811 1.3.4.2 mjf l->l_selflag = SEL_SCANNING;
812 1.3.4.2 mjf if (sopoll(so, events) != 0)
813 1.3.4.2 mjf break;
814 1.3.4.2 mjf if (tvp && (timo = gettimeleft(&tv, &sleeptv)) <= 0)
815 1.3.4.2 mjf break;
816 1.3.4.2 mjf mutex_spin_enter(&sc->sc_lock);
817 1.3.4.2 mjf if (l->l_selflag != SEL_SCANNING || sc->sc_ncoll != ncoll) {
818 1.3.4.2 mjf mutex_spin_exit(&sc->sc_lock);
819 1.3.4.2 mjf continue;
820 1.3.4.2 mjf }
821 1.3.4.2 mjf l->l_selflag = SEL_BLOCKING;
822 1.3.4.3 mjf sleepq_enter(&sc->sc_sleepq, l, &sc->sc_lock);
823 1.3.4.2 mjf sleepq_enqueue(&sc->sc_sleepq, sc, "pollsock", &select_sobj);
824 1.3.4.2 mjf error = sleepq_block(timo, true);
825 1.3.4.2 mjf if (error != 0)
826 1.3.4.2 mjf break;
827 1.3.4.2 mjf }
828 1.3.4.2 mjf selclear();
829 1.3.4.2 mjf /* poll is not restarted after signals... */
830 1.3.4.2 mjf if (error == ERESTART)
831 1.3.4.2 mjf error = EINTR;
832 1.3.4.2 mjf if (error == EWOULDBLOCK)
833 1.3.4.2 mjf error = 0;
834 1.3.4.2 mjf return (error);
835 1.3.4.2 mjf }
836