xref: /openbsd-src/sys/kern/sys_generic.c (revision fdd2406a066a41ff8a28f7c2229907702e86d1f0)
1 /*	$OpenBSD: sys_generic.c,v 1.138 2021/10/24 11:23:22 mpi Exp $	*/
2 /*	$NetBSD: sys_generic.c,v 1.24 1996/03/29 00:25:32 cgd Exp $	*/
3 
4 /*
5  * Copyright (c) 1996 Theo de Raadt
6  * Copyright (c) 1982, 1986, 1989, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  * (c) UNIX System Laboratories, Inc.
9  * All or some portions of this file are derived from material licensed
10  * to the University of California by American Telephone and Telegraph
11  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
12  * the permission of UNIX System Laboratories, Inc.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)sys_generic.c	8.5 (Berkeley) 1/21/94
39  */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/filedesc.h>
44 #include <sys/ioctl.h>
45 #include <sys/fcntl.h>
46 #include <sys/vnode.h>
47 #include <sys/file.h>
48 #include <sys/proc.h>
49 #include <sys/resourcevar.h>
50 #include <sys/socketvar.h>
51 #include <sys/signalvar.h>
52 #include <sys/uio.h>
53 #include <sys/kernel.h>
54 #include <sys/stat.h>
55 #include <sys/time.h>
56 #include <sys/malloc.h>
57 #include <sys/poll.h>
58 #include <sys/eventvar.h>
59 #ifdef KTRACE
60 #include <sys/ktrace.h>
61 #endif
62 #include <sys/sched.h>
63 #include <sys/pledge.h>
64 
65 #include <sys/mount.h>
66 #include <sys/syscallargs.h>
67 
68 #include <uvm/uvm_extern.h>
69 
70 /*
71  * Debug values:
72  *  1 - print implementation errors, things that should not happen.
73  *  2 - print ppoll(2) information, somewhat verbose
74  *  3 - print pselect(2) and ppoll(2) information, very verbose
75  */
76 int kqpoll_debug = 0;
77 #define DPRINTFN(v, x...) if (kqpoll_debug > v) {			\
78 	printf("%s(%d): ", curproc->p_p->ps_comm, curproc->p_tid);	\
79 	printf(x);							\
80 }
81 
82 int pselregister(struct proc *, fd_set *[], fd_set *[], int, int *, int *);
83 int pselcollect(struct proc *, struct kevent *, fd_set *[], int *);
84 int ppollregister(struct proc *, struct pollfd *, int, int *);
85 int ppollcollect(struct proc *, struct kevent *, struct pollfd *, u_int);
86 
87 int pollout(struct pollfd *, struct pollfd *, u_int);
88 int dopselect(struct proc *, int, fd_set *, fd_set *, fd_set *,
89     struct timespec *, const sigset_t *, register_t *);
90 int doppoll(struct proc *, struct pollfd *, u_int, struct timespec *,
91     const sigset_t *, register_t *);
92 void doselwakeup(struct selinfo *);
93 
94 int
95 iovec_copyin(const struct iovec *uiov, struct iovec **iovp, struct iovec *aiov,
96     unsigned int iovcnt, size_t *residp)
97 {
98 #ifdef KTRACE
99 	struct proc *p = curproc;
100 #endif
101 	struct iovec *iov;
102 	int error, i;
103 	size_t resid = 0;
104 
105 	if (iovcnt > UIO_SMALLIOV) {
106 		if (iovcnt > IOV_MAX)
107 			return (EINVAL);
108 		iov = mallocarray(iovcnt, sizeof(*iov), M_IOV, M_WAITOK);
109 	} else if (iovcnt > 0) {
110 		iov = aiov;
111 	} else {
112 		return (EINVAL);
113 	}
114 	*iovp = iov;
115 
116 	if ((error = copyin(uiov, iov, iovcnt * sizeof(*iov))))
117 		return (error);
118 
119 #ifdef KTRACE
120 	if (KTRPOINT(p, KTR_STRUCT))
121 		ktriovec(p, iov, iovcnt);
122 #endif
123 
124 	for (i = 0; i < iovcnt; i++) {
125 		resid += iov->iov_len;
126 		/*
127 		 * Writes return ssize_t because -1 is returned on error.
128 		 * Therefore we must restrict the length to SSIZE_MAX to
129 		 * avoid garbage return values.  Note that the addition is
130 		 * guaranteed to not wrap because SSIZE_MAX * 2 < SIZE_MAX.
131 		 */
132 		if (iov->iov_len > SSIZE_MAX || resid > SSIZE_MAX)
133 			return (EINVAL);
134 		iov++;
135 	}
136 
137 	if (residp != NULL)
138 		*residp = resid;
139 
140 	return (0);
141 }
142 
143 void
144 iovec_free(struct iovec *iov, unsigned int iovcnt)
145 {
146 	if (iovcnt > UIO_SMALLIOV)
147 		free(iov, M_IOV, iovcnt * sizeof(*iov));
148 }
149 
150 /*
151  * Read system call.
152  */
153 int
154 sys_read(struct proc *p, void *v, register_t *retval)
155 {
156 	struct sys_read_args /* {
157 		syscallarg(int) fd;
158 		syscallarg(void *) buf;
159 		syscallarg(size_t) nbyte;
160 	} */ *uap = v;
161 	struct iovec iov;
162 	struct uio auio;
163 
164 	iov.iov_base = SCARG(uap, buf);
165 	iov.iov_len = SCARG(uap, nbyte);
166 	if (iov.iov_len > SSIZE_MAX)
167 		return (EINVAL);
168 
169 	auio.uio_iov = &iov;
170 	auio.uio_iovcnt = 1;
171 	auio.uio_resid = iov.iov_len;
172 
173 	return (dofilereadv(p, SCARG(uap, fd), &auio, 0, retval));
174 }
175 
176 /*
177  * Scatter read system call.
178  */
179 int
180 sys_readv(struct proc *p, void *v, register_t *retval)
181 {
182 	struct sys_readv_args /* {
183 		syscallarg(int) fd;
184 		syscallarg(const struct iovec *) iovp;
185 		syscallarg(int) iovcnt;
186 	} */ *uap = v;
187 	struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
188 	int error, iovcnt = SCARG(uap, iovcnt);
189 	struct uio auio;
190 	size_t resid;
191 
192 	error = iovec_copyin(SCARG(uap, iovp), &iov, aiov, iovcnt, &resid);
193 	if (error)
194 		goto done;
195 
196 	auio.uio_iov = iov;
197 	auio.uio_iovcnt = iovcnt;
198 	auio.uio_resid = resid;
199 
200 	error = dofilereadv(p, SCARG(uap, fd), &auio, 0, retval);
201  done:
202 	iovec_free(iov, iovcnt);
203 	return (error);
204 }
205 
206 int
207 dofilereadv(struct proc *p, int fd, struct uio *uio, int flags,
208     register_t *retval)
209 {
210 	struct filedesc *fdp = p->p_fd;
211 	struct file *fp;
212 	long cnt, error = 0;
213 	u_int iovlen;
214 #ifdef KTRACE
215 	struct iovec *ktriov = NULL;
216 #endif
217 
218 	KASSERT(uio->uio_iov != NULL && uio->uio_iovcnt > 0);
219 	iovlen = uio->uio_iovcnt * sizeof(struct iovec);
220 
221 	if ((fp = fd_getfile_mode(fdp, fd, FREAD)) == NULL)
222 		return (EBADF);
223 
224 	/* Checks for positioned read. */
225 	if (flags & FO_POSITION) {
226 		struct vnode *vp = fp->f_data;
227 
228 		if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO ||
229 		    (vp->v_flag & VISTTY)) {
230 			error = ESPIPE;
231 			goto done;
232 		}
233 
234 		if (uio->uio_offset < 0 && vp->v_type != VCHR) {
235 			error = EINVAL;
236 			goto done;
237 		}
238 	}
239 
240 	uio->uio_rw = UIO_READ;
241 	uio->uio_segflg = UIO_USERSPACE;
242 	uio->uio_procp = p;
243 #ifdef KTRACE
244 	/*
245 	 * if tracing, save a copy of iovec
246 	 */
247 	if (KTRPOINT(p, KTR_GENIO)) {
248 		ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
249 		memcpy(ktriov, uio->uio_iov, iovlen);
250 	}
251 #endif
252 	cnt = uio->uio_resid;
253 	error = (*fp->f_ops->fo_read)(fp, uio, flags);
254 	if (error) {
255 		if (uio->uio_resid != cnt && (error == ERESTART ||
256 		    error == EINTR || error == EWOULDBLOCK))
257 			error = 0;
258 	}
259 	cnt -= uio->uio_resid;
260 
261 	mtx_enter(&fp->f_mtx);
262 	fp->f_rxfer++;
263 	fp->f_rbytes += cnt;
264 	mtx_leave(&fp->f_mtx);
265 #ifdef KTRACE
266 	if (ktriov != NULL) {
267 		if (error == 0)
268 			ktrgenio(p, fd, UIO_READ, ktriov, cnt);
269 		free(ktriov, M_TEMP, iovlen);
270 	}
271 #endif
272 	*retval = cnt;
273  done:
274 	FRELE(fp, p);
275 	return (error);
276 }
277 
278 /*
279  * Write system call
280  */
281 int
282 sys_write(struct proc *p, void *v, register_t *retval)
283 {
284 	struct sys_write_args /* {
285 		syscallarg(int) fd;
286 		syscallarg(const void *) buf;
287 		syscallarg(size_t) nbyte;
288 	} */ *uap = v;
289 	struct iovec iov;
290 	struct uio auio;
291 
292 	iov.iov_base = (void *)SCARG(uap, buf);
293 	iov.iov_len = SCARG(uap, nbyte);
294 	if (iov.iov_len > SSIZE_MAX)
295 		return (EINVAL);
296 
297 	auio.uio_iov = &iov;
298 	auio.uio_iovcnt = 1;
299 	auio.uio_resid = iov.iov_len;
300 
301 	return (dofilewritev(p, SCARG(uap, fd), &auio, 0, retval));
302 }
303 
304 /*
305  * Gather write system call
306  */
307 int
308 sys_writev(struct proc *p, void *v, register_t *retval)
309 {
310 	struct sys_writev_args /* {
311 		syscallarg(int) fd;
312 		syscallarg(const struct iovec *) iovp;
313 		syscallarg(int) iovcnt;
314 	} */ *uap = v;
315 	struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
316 	int error, iovcnt = SCARG(uap, iovcnt);
317 	struct uio auio;
318 	size_t resid;
319 
320 	error = iovec_copyin(SCARG(uap, iovp), &iov, aiov, iovcnt, &resid);
321 	if (error)
322 		goto done;
323 
324 	auio.uio_iov = iov;
325 	auio.uio_iovcnt = iovcnt;
326 	auio.uio_resid = resid;
327 
328 	error = dofilewritev(p, SCARG(uap, fd), &auio, 0, retval);
329  done:
330 	iovec_free(iov, iovcnt);
331  	return (error);
332 }
333 
334 int
335 dofilewritev(struct proc *p, int fd, struct uio *uio, int flags,
336     register_t *retval)
337 {
338 	struct filedesc *fdp = p->p_fd;
339 	struct file *fp;
340 	long cnt, error = 0;
341 	u_int iovlen;
342 #ifdef KTRACE
343 	struct iovec *ktriov = NULL;
344 #endif
345 
346 	KASSERT(uio->uio_iov != NULL && uio->uio_iovcnt > 0);
347 	iovlen = uio->uio_iovcnt * sizeof(struct iovec);
348 
349 	if ((fp = fd_getfile_mode(fdp, fd, FWRITE)) == NULL)
350 		return (EBADF);
351 
352 	/* Checks for positioned write. */
353 	if (flags & FO_POSITION) {
354 		struct vnode *vp = fp->f_data;
355 
356 		if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO ||
357 		    (vp->v_flag & VISTTY)) {
358 			error = ESPIPE;
359 			goto done;
360 		}
361 
362 		if (uio->uio_offset < 0 && vp->v_type != VCHR) {
363 			error = EINVAL;
364 			goto done;
365 		}
366 	}
367 
368 	uio->uio_rw = UIO_WRITE;
369 	uio->uio_segflg = UIO_USERSPACE;
370 	uio->uio_procp = p;
371 #ifdef KTRACE
372 	/*
373 	 * if tracing, save a copy of iovec
374 	 */
375 	if (KTRPOINT(p, KTR_GENIO)) {
376 		ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
377 		memcpy(ktriov, uio->uio_iov, iovlen);
378 	}
379 #endif
380 	cnt = uio->uio_resid;
381 	error = (*fp->f_ops->fo_write)(fp, uio, flags);
382 	if (error) {
383 		if (uio->uio_resid != cnt && (error == ERESTART ||
384 		    error == EINTR || error == EWOULDBLOCK))
385 			error = 0;
386 		if (error == EPIPE) {
387 			KERNEL_LOCK();
388 			ptsignal(p, SIGPIPE, STHREAD);
389 			KERNEL_UNLOCK();
390 		}
391 	}
392 	cnt -= uio->uio_resid;
393 
394 	mtx_enter(&fp->f_mtx);
395 	fp->f_wxfer++;
396 	fp->f_wbytes += cnt;
397 	mtx_leave(&fp->f_mtx);
398 #ifdef KTRACE
399 	if (ktriov != NULL) {
400 		if (error == 0)
401 			ktrgenio(p, fd, UIO_WRITE, ktriov, cnt);
402 		free(ktriov, M_TEMP, iovlen);
403 	}
404 #endif
405 	*retval = cnt;
406  done:
407 	FRELE(fp, p);
408 	return (error);
409 }
410 
411 /*
412  * Ioctl system call
413  */
414 int
415 sys_ioctl(struct proc *p, void *v, register_t *retval)
416 {
417 	struct sys_ioctl_args /* {
418 		syscallarg(int) fd;
419 		syscallarg(u_long) com;
420 		syscallarg(void *) data;
421 	} */ *uap = v;
422 	struct file *fp;
423 	struct filedesc *fdp = p->p_fd;
424 	u_long com = SCARG(uap, com);
425 	int error = 0;
426 	u_int size = 0;
427 	caddr_t data, memp = NULL;
428 	int tmp;
429 #define STK_PARAMS	128
430 	long long stkbuf[STK_PARAMS / sizeof(long long)];
431 
432 	if ((fp = fd_getfile_mode(fdp, SCARG(uap, fd), FREAD|FWRITE)) == NULL)
433 		return (EBADF);
434 
435 	if (fp->f_type == DTYPE_SOCKET) {
436 		struct socket *so = fp->f_data;
437 
438 		if (so->so_state & SS_DNS) {
439 			error = EINVAL;
440 			goto out;
441 		}
442 	}
443 
444 	error = pledge_ioctl(p, com, fp);
445 	if (error)
446 		goto out;
447 
448 	switch (com) {
449 	case FIONCLEX:
450 	case FIOCLEX:
451 		fdplock(fdp);
452 		if (com == FIONCLEX)
453 			fdp->fd_ofileflags[SCARG(uap, fd)] &= ~UF_EXCLOSE;
454 		else
455 			fdp->fd_ofileflags[SCARG(uap, fd)] |= UF_EXCLOSE;
456 		fdpunlock(fdp);
457 		goto out;
458 	}
459 
460 	/*
461 	 * Interpret high order word to find amount of data to be
462 	 * copied to/from the user's address space.
463 	 */
464 	size = IOCPARM_LEN(com);
465 	if (size > IOCPARM_MAX) {
466 		error = ENOTTY;
467 		goto out;
468 	}
469 	if (size > sizeof (stkbuf)) {
470 		memp = malloc(size, M_IOCTLOPS, M_WAITOK);
471 		data = memp;
472 	} else
473 		data = (caddr_t)stkbuf;
474 	if (com&IOC_IN) {
475 		if (size) {
476 			error = copyin(SCARG(uap, data), data, size);
477 			if (error) {
478 				goto out;
479 			}
480 		} else
481 			*(caddr_t *)data = SCARG(uap, data);
482 	} else if ((com&IOC_OUT) && size)
483 		/*
484 		 * Zero the buffer so the user always
485 		 * gets back something deterministic.
486 		 */
487 		memset(data, 0, size);
488 	else if (com&IOC_VOID)
489 		*(caddr_t *)data = SCARG(uap, data);
490 
491 	switch (com) {
492 
493 	case FIONBIO:
494 		if ((tmp = *(int *)data) != 0)
495 			atomic_setbits_int(&fp->f_flag, FNONBLOCK);
496 		else
497 			atomic_clearbits_int(&fp->f_flag, FNONBLOCK);
498 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
499 		break;
500 
501 	case FIOASYNC:
502 		if ((tmp = *(int *)data) != 0)
503 			atomic_setbits_int(&fp->f_flag, FASYNC);
504 		else
505 			atomic_clearbits_int(&fp->f_flag, FASYNC);
506 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
507 		break;
508 
509 	default:
510 		error = (*fp->f_ops->fo_ioctl)(fp, com, data, p);
511 		break;
512 	}
513 	/*
514 	 * Copy any data to user, size was
515 	 * already set and checked above.
516 	 */
517 	if (error == 0 && (com&IOC_OUT) && size)
518 		error = copyout(data, SCARG(uap, data), size);
519 out:
520 	FRELE(fp, p);
521 	free(memp, M_IOCTLOPS, size);
522 	return (error);
523 }
524 
525 int	selwait, nselcoll;
526 
527 /*
528  * Select system call.
529  */
530 int
531 sys_select(struct proc *p, void *v, register_t *retval)
532 {
533 	struct sys_select_args /* {
534 		syscallarg(int) nd;
535 		syscallarg(fd_set *) in;
536 		syscallarg(fd_set *) ou;
537 		syscallarg(fd_set *) ex;
538 		syscallarg(struct timeval *) tv;
539 	} */ *uap = v;
540 
541 	struct timespec ts, *tsp = NULL;
542 	int error;
543 
544 	if (SCARG(uap, tv) != NULL) {
545 		struct timeval tv;
546 		if ((error = copyin(SCARG(uap, tv), &tv, sizeof tv)) != 0)
547 			return (error);
548 #ifdef KTRACE
549 		if (KTRPOINT(p, KTR_STRUCT))
550 			ktrreltimeval(p, &tv);
551 #endif
552 		if (tv.tv_sec < 0 || !timerisvalid(&tv))
553 			return (EINVAL);
554 		TIMEVAL_TO_TIMESPEC(&tv, &ts);
555 		tsp = &ts;
556 	}
557 
558 	return (dopselect(p, SCARG(uap, nd), SCARG(uap, in), SCARG(uap, ou),
559 	    SCARG(uap, ex), tsp, NULL, retval));
560 }
561 
562 int
563 sys_pselect(struct proc *p, void *v, register_t *retval)
564 {
565 	struct sys_pselect_args /* {
566 		syscallarg(int) nd;
567 		syscallarg(fd_set *) in;
568 		syscallarg(fd_set *) ou;
569 		syscallarg(fd_set *) ex;
570 		syscallarg(const struct timespec *) ts;
571 		syscallarg(const sigset_t *) mask;
572 	} */ *uap = v;
573 
574 	struct timespec ts, *tsp = NULL;
575 	sigset_t ss, *ssp = NULL;
576 	int error;
577 
578 	if (SCARG(uap, ts) != NULL) {
579 		if ((error = copyin(SCARG(uap, ts), &ts, sizeof ts)) != 0)
580 			return (error);
581 #ifdef KTRACE
582 		if (KTRPOINT(p, KTR_STRUCT))
583 			ktrreltimespec(p, &ts);
584 #endif
585 		if (ts.tv_sec < 0 || !timespecisvalid(&ts))
586 			return (EINVAL);
587 		tsp = &ts;
588 	}
589 	if (SCARG(uap, mask) != NULL) {
590 		if ((error = copyin(SCARG(uap, mask), &ss, sizeof ss)) != 0)
591 			return (error);
592 		ssp = &ss;
593 	}
594 
595 	return (dopselect(p, SCARG(uap, nd), SCARG(uap, in), SCARG(uap, ou),
596 	    SCARG(uap, ex), tsp, ssp, retval));
597 }
598 
599 int
600 dopselect(struct proc *p, int nd, fd_set *in, fd_set *ou, fd_set *ex,
601     struct timespec *timeout, const sigset_t *sigmask, register_t *retval)
602 {
603 	struct kqueue_scan_state scan;
604 	fd_mask bits[6];
605 	fd_set *pibits[3], *pobits[3];
606 	int error, ncollected = 0, nevents = 0;
607 	u_int ni;
608 
609 	if (nd < 0)
610 		return (EINVAL);
611 	if (nd > p->p_fd->fd_nfiles) {
612 		/* forgiving; slightly wrong */
613 		nd = p->p_fd->fd_nfiles;
614 	}
615 	ni = howmany(nd, NFDBITS) * sizeof(fd_mask);
616 	if (ni > sizeof(bits[0])) {
617 		caddr_t mbits;
618 
619 		mbits = mallocarray(6, ni, M_TEMP, M_WAITOK|M_ZERO);
620 		pibits[0] = (fd_set *)&mbits[ni * 0];
621 		pibits[1] = (fd_set *)&mbits[ni * 1];
622 		pibits[2] = (fd_set *)&mbits[ni * 2];
623 		pobits[0] = (fd_set *)&mbits[ni * 3];
624 		pobits[1] = (fd_set *)&mbits[ni * 4];
625 		pobits[2] = (fd_set *)&mbits[ni * 5];
626 	} else {
627 		memset(bits, 0, sizeof(bits));
628 		pibits[0] = (fd_set *)&bits[0];
629 		pibits[1] = (fd_set *)&bits[1];
630 		pibits[2] = (fd_set *)&bits[2];
631 		pobits[0] = (fd_set *)&bits[3];
632 		pobits[1] = (fd_set *)&bits[4];
633 		pobits[2] = (fd_set *)&bits[5];
634 	}
635 
636 	kqpoll_init();
637 
638 #define	getbits(name, x) \
639 	if (name && (error = copyin(name, pibits[x], ni))) \
640 		goto done;
641 	getbits(in, 0);
642 	getbits(ou, 1);
643 	getbits(ex, 2);
644 #undef	getbits
645 #ifdef KTRACE
646 	if (ni > 0 && KTRPOINT(p, KTR_STRUCT)) {
647 		if (in) ktrfdset(p, pibits[0], ni);
648 		if (ou) ktrfdset(p, pibits[1], ni);
649 		if (ex) ktrfdset(p, pibits[2], ni);
650 	}
651 #endif
652 
653 	if (sigmask)
654 		dosigsuspend(p, *sigmask &~ sigcantmask);
655 
656 	/* Register kqueue events */
657 	error = pselregister(p, pibits, pobits, nd, &nevents, &ncollected);
658 	if (error != 0)
659 		goto done;
660 
661 	/*
662 	 * The poll/select family of syscalls has been designed to
663 	 * block when file descriptors are not available, even if
664 	 * there's nothing to wait for.
665 	 */
666 	if (nevents == 0 && ncollected == 0) {
667 		uint64_t nsecs = INFSLP;
668 
669 		if (timeout != NULL) {
670 			if (!timespecisset(timeout))
671 				goto done;
672 			nsecs = MAX(1, MIN(TIMESPEC_TO_NSEC(timeout), MAXTSLP));
673 		}
674 		error = tsleep_nsec(&p->p_kq, PSOCK | PCATCH, "kqsel", nsecs);
675 		/* select is not restarted after signals... */
676 		if (error == ERESTART)
677 			error = EINTR;
678 		if (error == EWOULDBLOCK)
679 			error = 0;
680 		goto done;
681 	}
682 
683 	/* Collect at most `nevents' possibly waiting in kqueue_scan() */
684 	kqueue_scan_setup(&scan, p->p_kq);
685 	while (nevents > 0) {
686 		struct kevent kev[KQ_NEVENTS];
687 		int i, ready, count;
688 
689 		/* Maximum number of events per iteration */
690 		count = MIN(nitems(kev), nevents);
691 		ready = kqueue_scan(&scan, count, kev, timeout, p, &error);
692 #ifdef KTRACE
693 		if (KTRPOINT(p, KTR_STRUCT))
694 			ktrevent(p, kev, ready);
695 #endif
696 		/* Convert back events that are ready. */
697 		for (i = 0; i < ready && error == 0; i++)
698 			error = pselcollect(p, &kev[i], pobits, &ncollected);
699 		/*
700 		 * Stop if there was an error or if we had enough
701 		 * space to collect all events that were ready.
702 		 */
703 		if (error || ready < count)
704 			break;
705 
706 		nevents -= ready;
707 	}
708 	kqueue_scan_finish(&scan);
709 	*retval = ncollected;
710 done:
711 #define	putbits(name, x) \
712 	if (name && (error2 = copyout(pobits[x], name, ni))) \
713 		error = error2;
714 	if (error == 0) {
715 		int error2;
716 
717 		putbits(in, 0);
718 		putbits(ou, 1);
719 		putbits(ex, 2);
720 #undef putbits
721 #ifdef KTRACE
722 		if (ni > 0 && KTRPOINT(p, KTR_STRUCT)) {
723 			if (in) ktrfdset(p, pobits[0], ni);
724 			if (ou) ktrfdset(p, pobits[1], ni);
725 			if (ex) ktrfdset(p, pobits[2], ni);
726 		}
727 #endif
728 	}
729 
730 	if (pibits[0] != (fd_set *)&bits[0])
731 		free(pibits[0], M_TEMP, 6 * ni);
732 
733 	kqueue_purge(p, p->p_kq);
734 	p->p_kq_serial += nd;
735 
736 	return (error);
737 }
738 
739 /*
740  * Convert fd_set into kqueue events and register them on the
741  * per-thread queue.
742  */
743 int
744 pselregister(struct proc *p, fd_set *pibits[3], fd_set *pobits[3], int nfd,
745     int *nregistered, int *ncollected)
746 {
747 	static const int evf[] = { EVFILT_READ, EVFILT_WRITE, EVFILT_EXCEPT };
748 	static const int evff[] = { 0, 0, NOTE_OOB };
749 	int msk, i, j, fd, nevents = 0, error = 0;
750 	struct kevent kev;
751 	fd_mask bits;
752 
753 	for (msk = 0; msk < 3; msk++) {
754 		for (i = 0; i < nfd; i += NFDBITS) {
755 			bits = pibits[msk]->fds_bits[i / NFDBITS];
756 			while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
757 				bits &= ~(1 << j);
758 
759 				DPRINTFN(2, "select fd %d mask %d serial %lu\n",
760 				    fd, msk, p->p_kq_serial);
761 				EV_SET(&kev, fd, evf[msk],
762 				    EV_ADD|EV_ENABLE|EV_ONESHOT|__EV_POLL,
763 				    evff[msk], 0, (void *)(p->p_kq_serial));
764 #ifdef KTRACE
765 				if (KTRPOINT(p, KTR_STRUCT))
766 					ktrevent(p, &kev, 1);
767 #endif
768 				error = kqueue_register(p->p_kq, &kev, p);
769 				switch (error) {
770 				case 0:
771 					nevents++;
772 				/* FALLTHROUGH */
773 				case EOPNOTSUPP:/* No underlying kqfilter */
774 				case EINVAL:	/* Unimplemented filter */
775 				case EPERM:	/* Specific to FIFO */
776 					error = 0;
777 					break;
778 				case EPIPE:	/* Specific to pipes */
779 					KASSERT(kev.filter == EVFILT_WRITE);
780 					FD_SET(kev.ident, pobits[1]);
781 					(*ncollected)++;
782 					error = 0;
783 					break;
784 				case ENXIO:	/* Device has been detached */
785 				default:
786 					goto bad;
787 				}
788 			}
789 		}
790 	}
791 
792 	*nregistered = nevents;
793 	return (0);
794 bad:
795 	DPRINTFN(0, "select fd %u filt %d error %d\n", (int)kev.ident,
796 	    kev.filter, error);
797 	return (error);
798 }
799 
800 /*
801  * Convert given kqueue event into corresponding select(2) bit.
802  */
803 int
804 pselcollect(struct proc *p, struct kevent *kevp, fd_set *pobits[3],
805     int *ncollected)
806 {
807 	/* Filter out and lazily delete spurious events */
808 	if ((unsigned long)kevp->udata != p->p_kq_serial) {
809 		DPRINTFN(0, "select fd %u mismatched serial %lu\n",
810 		    (int)kevp->ident, p->p_kq_serial);
811 		kevp->flags = EV_DISABLE|EV_DELETE;
812 		kqueue_register(p->p_kq, kevp, p);
813 		return (0);
814 	}
815 
816 	if (kevp->flags & EV_ERROR) {
817 		DPRINTFN(2, "select fd %d filt %d error %d\n",
818 		    (int)kevp->ident, kevp->filter, (int)kevp->data);
819 		return (kevp->data);
820 	}
821 
822 	switch (kevp->filter) {
823 	case EVFILT_READ:
824 		FD_SET(kevp->ident, pobits[0]);
825 		break;
826 	case EVFILT_WRITE:
827 		FD_SET(kevp->ident, pobits[1]);
828 		break;
829 	case EVFILT_EXCEPT:
830 		FD_SET(kevp->ident, pobits[2]);
831 		break;
832 	default:
833 		KASSERT(0);
834 	}
835 	(*ncollected)++;
836 
837 	DPRINTFN(2, "select fd %d filt %d\n", (int)kevp->ident, kevp->filter);
838 	return (0);
839 }
840 
841 int
842 seltrue(dev_t dev, int events, struct proc *p)
843 {
844 
845 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
846 }
847 
848 int
849 selfalse(dev_t dev, int events, struct proc *p)
850 {
851 
852 	return (0);
853 }
854 
855 /*
856  * Record a select request.
857  */
858 void
859 selrecord(struct proc *selector, struct selinfo *sip)
860 {
861 	struct proc *p;
862 	pid_t mytid;
863 
864 	KERNEL_ASSERT_LOCKED();
865 
866 	mytid = selector->p_tid;
867 	if (sip->si_seltid == mytid)
868 		return;
869 	if (sip->si_seltid && (p = tfind(sip->si_seltid)) &&
870 	    p->p_wchan == (caddr_t)&selwait)
871 		sip->si_flags |= SI_COLL;
872 	else
873 		sip->si_seltid = mytid;
874 }
875 
876 /*
877  * Do a wakeup when a selectable event occurs.
878  */
879 void
880 selwakeup(struct selinfo *sip)
881 {
882 	KERNEL_LOCK();
883 	KNOTE(&sip->si_note, NOTE_SUBMIT);
884 	doselwakeup(sip);
885 	KERNEL_UNLOCK();
886 }
887 
888 void
889 doselwakeup(struct selinfo *sip)
890 {
891 	struct proc *p;
892 
893 	KERNEL_ASSERT_LOCKED();
894 
895 	if (sip->si_seltid == 0)
896 		return;
897 	if (sip->si_flags & SI_COLL) {
898 		nselcoll++;
899 		sip->si_flags &= ~SI_COLL;
900 		wakeup(&selwait);
901 	}
902 	p = tfind(sip->si_seltid);
903 	sip->si_seltid = 0;
904 	if (p != NULL) {
905 		if (wakeup_proc(p, &selwait)) {
906 			/* nothing else to do */
907 		} else if (p->p_flag & P_SELECT)
908 			atomic_clearbits_int(&p->p_flag, P_SELECT);
909 	}
910 }
911 
912 int
913 ppollregister_evts(struct proc *p, struct kevent *kevp, int nkev,
914     struct pollfd *pl)
915 {
916 	int i, error, nevents = 0;
917 
918 	KASSERT(pl->revents == 0);
919 
920 #ifdef KTRACE
921 	if (KTRPOINT(p, KTR_STRUCT))
922 		ktrevent(p, kevp, nkev);
923 #endif
924 	for (i = 0; i < nkev; i++, kevp++) {
925 again:
926 		error = kqueue_register(p->p_kq, kevp, p);
927 		switch (error) {
928 		case 0:
929 			nevents++;
930 			break;
931 		case EOPNOTSUPP:/* No underlying kqfilter */
932 		case EINVAL:	/* Unimplemented filter */
933 			break;
934 		case EBADF:	/* Bad file descriptor */
935 			pl->revents |= POLLNVAL;
936 			break;
937 		case EPERM:	/* Specific to FIFO */
938 			KASSERT(kevp->filter == EVFILT_WRITE);
939 			if (nkev == 1) {
940 				/*
941 				 * If this is the only filter make sure
942 				 * POLLHUP is passed to userland.
943 				 */
944 				kevp->filter = EVFILT_EXCEPT;
945 				goto again;
946 			}
947 			break;
948 		case EPIPE:	/* Specific to pipes */
949 			KASSERT(kevp->filter == EVFILT_WRITE);
950 			pl->revents |= POLLHUP;
951 			break;
952 		default:
953 #ifdef DIAGNOSTIC
954 			DPRINTFN(0, "poll err %lu fd %d revents %02x serial"
955 			    " %lu filt %d ERROR=%d\n",
956 			    ((unsigned long)kevp->udata - p->p_kq_serial),
957 			    pl->fd, pl->revents, p->p_kq_serial, kevp->filter,
958 			    error);
959 #endif
960 			/* FALLTHROUGH */
961 		case ENXIO:	/* Device has been detached */
962 			pl->revents |= POLLERR;
963 			break;
964 		}
965 	}
966 
967 	return (nevents);
968 }
969 
970 /*
971  * Convert pollfd into kqueue events and register them on the
972  * per-thread queue.
973  *
974  * Return the number of pollfd that triggered at least one error and aren't
975  * completly monitored.  These pollfd should have the correponding error bit
976  * set in `revents'.
977  *
978  * At most 3 events can correspond to a single pollfd.
979  */
980 int
981 ppollregister(struct proc *p, struct pollfd *pl, int nfds, int *nregistered)
982 {
983 	int i, nkev, nevt, errcount = 0, forcehup = 0;
984 	struct kevent kev[3], *kevp;
985 
986 	for (i = 0; i < nfds; i++) {
987 		pl[i].events &= ~POLL_NOHUP;
988 		pl[i].revents = 0;
989 
990 		if (pl[i].fd < 0)
991 			continue;
992 
993 		if (pl[i].events == 0)
994 			forcehup = 1;
995 
996 		DPRINTFN(1, "poll set %d/%d fd %d events %02x serial %lu\n",
997 		    i+1, nfds, pl[i].fd, pl[i].events, p->p_kq_serial);
998 
999 		nevt = 0;
1000 		nkev = 0;
1001 		kevp = kev;
1002 		if (pl[i].events & (POLLIN | POLLRDNORM)) {
1003 			EV_SET(kevp, pl[i].fd, EVFILT_READ,
1004 			    EV_ADD|EV_ENABLE|EV_ONESHOT|__EV_POLL, 0, 0,
1005 			    (void *)(p->p_kq_serial + i));
1006 			nkev++;
1007 			kevp++;
1008 		}
1009 		if (pl[i].events & (POLLOUT | POLLWRNORM)) {
1010 			EV_SET(kevp, pl[i].fd, EVFILT_WRITE,
1011 			    EV_ADD|EV_ENABLE|EV_ONESHOT|__EV_POLL, 0, 0,
1012 			    (void *)(p->p_kq_serial + i));
1013 			nkev++;
1014 			kevp++;
1015 		}
1016 		if ((pl[i].events & (POLLPRI | POLLRDBAND)) || forcehup) {
1017 			int evff = forcehup ? 0 : NOTE_OOB;
1018 
1019 			EV_SET(kevp, pl[i].fd, EVFILT_EXCEPT,
1020 			    EV_ADD|EV_ENABLE|EV_ONESHOT|__EV_POLL, evff, 0,
1021 			    (void *)(p->p_kq_serial + i));
1022 			nkev++;
1023 			kevp++;
1024 		}
1025 
1026 		if (nkev == 0)
1027 			continue;
1028 
1029 		nevt = ppollregister_evts(p, kev, nkev, &pl[i]);
1030 		if (nevt == 0 && !forcehup)
1031 			errcount++;
1032 		*nregistered += nevt;
1033 	}
1034 
1035 #ifdef DIAGNOSTIC
1036 	DPRINTFN(1, "poll registered = %d, errors = %d\n", *nregistered,
1037 	    errcount);
1038 #endif
1039 	return (errcount);
1040 }
1041 
1042 /*
1043  * Only copyout the revents field.
1044  */
1045 int
1046 pollout(struct pollfd *pl, struct pollfd *upl, u_int nfds)
1047 {
1048 	int error = 0;
1049 	u_int i = 0;
1050 
1051 	while (!error && i++ < nfds) {
1052 		error = copyout(&pl->revents, &upl->revents,
1053 		    sizeof(upl->revents));
1054 		pl++;
1055 		upl++;
1056 	}
1057 
1058 	return (error);
1059 }
1060 
1061 /*
1062  * We are using the same mechanism as select only we encode/decode args
1063  * differently.
1064  */
1065 int
1066 sys_poll(struct proc *p, void *v, register_t *retval)
1067 {
1068 	struct sys_poll_args /* {
1069 		syscallarg(struct pollfd *) fds;
1070 		syscallarg(u_int) nfds;
1071 		syscallarg(int) timeout;
1072 	} */ *uap = v;
1073 
1074 	struct timespec ts, *tsp = NULL;
1075 	int msec = SCARG(uap, timeout);
1076 
1077 	if (msec != INFTIM) {
1078 		if (msec < 0)
1079 			return (EINVAL);
1080 		ts.tv_sec = msec / 1000;
1081 		ts.tv_nsec = (msec - (ts.tv_sec * 1000)) * 1000000;
1082 		tsp = &ts;
1083 	}
1084 
1085 	return (doppoll(p, SCARG(uap, fds), SCARG(uap, nfds), tsp, NULL,
1086 	    retval));
1087 }
1088 
1089 int
1090 sys_ppoll(struct proc *p, void *v, register_t *retval)
1091 {
1092 	struct sys_ppoll_args /* {
1093 		syscallarg(struct pollfd *) fds;
1094 		syscallarg(u_int) nfds;
1095 		syscallarg(const struct timespec *) ts;
1096 		syscallarg(const sigset_t *) mask;
1097 	} */ *uap = v;
1098 
1099 	int error;
1100 	struct timespec ts, *tsp = NULL;
1101 	sigset_t ss, *ssp = NULL;
1102 
1103 	if (SCARG(uap, ts) != NULL) {
1104 		if ((error = copyin(SCARG(uap, ts), &ts, sizeof ts)) != 0)
1105 			return (error);
1106 #ifdef KTRACE
1107 		if (KTRPOINT(p, KTR_STRUCT))
1108 			ktrreltimespec(p, &ts);
1109 #endif
1110 		if (ts.tv_sec < 0 || !timespecisvalid(&ts))
1111 			return (EINVAL);
1112 		tsp = &ts;
1113 	}
1114 
1115 	if (SCARG(uap, mask) != NULL) {
1116 		if ((error = copyin(SCARG(uap, mask), &ss, sizeof ss)) != 0)
1117 			return (error);
1118 		ssp = &ss;
1119 	}
1120 
1121 	return (doppoll(p, SCARG(uap, fds), SCARG(uap, nfds), tsp, ssp,
1122 	    retval));
1123 }
1124 
1125 int
1126 doppoll(struct proc *p, struct pollfd *fds, u_int nfds,
1127     struct timespec *timeout, const sigset_t *sigmask, register_t *retval)
1128 {
1129 	struct kqueue_scan_state scan;
1130 	struct pollfd pfds[4], *pl = pfds;
1131 	int error, nevents = 0;
1132 	size_t sz;
1133 
1134 	/* Standards say no more than MAX_OPEN; this is possibly better. */
1135 	if (nfds > min((int)lim_cur(RLIMIT_NOFILE), maxfiles))
1136 		return (EINVAL);
1137 
1138 	/* optimize for the default case, of a small nfds value */
1139 	if (nfds > nitems(pfds)) {
1140 		pl = mallocarray(nfds, sizeof(*pl), M_TEMP,
1141 		    M_WAITOK | M_CANFAIL);
1142 		if (pl == NULL)
1143 			return (EINVAL);
1144 	}
1145 
1146 	kqpoll_init();
1147 
1148 	sz = nfds * sizeof(*pl);
1149 
1150 	if ((error = copyin(fds, pl, sz)) != 0)
1151 		goto bad;
1152 
1153 	if (sigmask)
1154 		dosigsuspend(p, *sigmask &~ sigcantmask);
1155 
1156 	/* Register kqueue events */
1157 	*retval = ppollregister(p, pl, nfds, &nevents);
1158 
1159 	/*
1160 	 * The poll/select family of syscalls has been designed to
1161 	 * block when file descriptors are not available, even if
1162 	 * there's nothing to wait for.
1163 	 */
1164 	if (nevents == 0) {
1165 		uint64_t nsecs = INFSLP;
1166 
1167 		if (timeout != NULL) {
1168 			if (!timespecisset(timeout))
1169 				goto done;
1170 			nsecs = MAX(1, MIN(TIMESPEC_TO_NSEC(timeout), MAXTSLP));
1171 		}
1172 
1173 		error = tsleep_nsec(&p->p_kq, PSOCK | PCATCH, "kqpoll", nsecs);
1174 		if (error == ERESTART)
1175 			error = EINTR;
1176 		if (error == EWOULDBLOCK)
1177 			error = 0;
1178 		goto done;
1179 	}
1180 
1181 	/* Collect at most `nevents' possibly waiting in kqueue_scan() */
1182 	kqueue_scan_setup(&scan, p->p_kq);
1183 	while (nevents > 0) {
1184 		struct kevent kev[KQ_NEVENTS];
1185 		int i, ready, count;
1186 
1187 		/* Maxium number of events per iteration */
1188 		count = MIN(nitems(kev), nevents);
1189 		ready = kqueue_scan(&scan, count, kev, timeout, p, &error);
1190 #ifdef KTRACE
1191 		if (KTRPOINT(p, KTR_STRUCT))
1192 			ktrevent(p, kev, ready);
1193 #endif
1194 		/* Convert back events that are ready. */
1195 		for (i = 0; i < ready; i++)
1196 			*retval += ppollcollect(p, &kev[i], pl, nfds);
1197 
1198 		/*
1199 		 * Stop if there was an error or if we had enough
1200 		 * place to collect all events that were ready.
1201 		 */
1202 		if (error || ready < count)
1203 			break;
1204 
1205 		nevents -= ready;
1206 	}
1207 	kqueue_scan_finish(&scan);
1208 done:
1209 	/*
1210 	 * NOTE: poll(2) is not restarted after a signal and EWOULDBLOCK is
1211 	 *       ignored (since the whole point is to see what would block).
1212 	 */
1213 	switch (error) {
1214 	case EINTR:
1215 		error = pollout(pl, fds, nfds);
1216 		if (error == 0)
1217 			error = EINTR;
1218 		break;
1219 	case EWOULDBLOCK:
1220 	case 0:
1221 		error = pollout(pl, fds, nfds);
1222 		break;
1223 	}
1224 #ifdef KTRACE
1225 	if (KTRPOINT(p, KTR_STRUCT))
1226 		ktrpollfd(p, pl, nfds);
1227 #endif /* KTRACE */
1228 bad:
1229 	if (pl != pfds)
1230 		free(pl, M_TEMP, sz);
1231 
1232 	kqueue_purge(p, p->p_kq);
1233 	p->p_kq_serial += nfds;
1234 
1235 	return (error);
1236 }
1237 
1238 /*
1239  * Convert given kqueue event into corresponding poll(2) revents bit.
1240  */
1241 int
1242 ppollcollect(struct proc *p, struct kevent *kevp, struct pollfd *pl, u_int nfds)
1243 {
1244 	int already_seen;
1245 	unsigned long i;
1246 
1247 	/*  Extract poll array index */
1248 	i = (unsigned long)kevp->udata - p->p_kq_serial;
1249 
1250 	/*
1251 	 * Lazily delete spurious events.
1252 	 *
1253 	 * This should not happen as long as kqueue_purge() is called
1254 	 * at the end of every syscall.  It migh be interesting to do
1255 	 * like DragonFlyBSD and not always allocated a new knote in
1256 	 * kqueue_register() with that lazy removal makes sense.
1257 	 */
1258 	if (i >= nfds) {
1259 		DPRINTFN(0, "poll get out of range udata %lu vs serial %lu\n",
1260 		    (unsigned long)kevp->udata, p->p_kq_serial);
1261 		kevp->flags = EV_DISABLE|EV_DELETE;
1262 		kqueue_register(p->p_kq, kevp, p);
1263 		return (0);
1264 	}
1265 	if ((int)kevp->ident != pl[i].fd) {
1266 		DPRINTFN(0, "poll get %lu/%d mismatch fd %u!=%d serial %lu\n",
1267 		    i+1, nfds, (int)kevp->ident, pl[i].fd, p->p_kq_serial);
1268 		return (0);
1269 	}
1270 
1271 	/*
1272 	 * A given descriptor may already have generated an error
1273 	 * against another filter during kqueue_register().
1274 	 *
1275 	 * Make sure to set the appropriate flags but do not
1276 	 * increment `*retval' more than once.
1277 	 */
1278 	already_seen = (pl[i].revents != 0);
1279 
1280 	switch (kevp->filter) {
1281 	case EVFILT_READ:
1282 		if (kevp->flags & __EV_HUP)
1283 			pl[i].revents |= POLLHUP;
1284 		if (pl[i].events & (POLLIN | POLLRDNORM))
1285 			pl[i].revents |= pl[i].events & (POLLIN | POLLRDNORM);
1286 		break;
1287 	case EVFILT_WRITE:
1288 		/* POLLHUP and POLLOUT/POLLWRNORM are mutually exclusive */
1289 		if (kevp->flags & __EV_HUP) {
1290 			pl[i].revents |= POLLHUP;
1291 		} else if (pl[i].events & (POLLOUT | POLLWRNORM)) {
1292 			pl[i].revents |= pl[i].events & (POLLOUT | POLLWRNORM);
1293 		}
1294 		break;
1295 	case EVFILT_EXCEPT:
1296 		if (kevp->flags & __EV_HUP) {
1297 #ifdef DIAGNOSTIC
1298 			if (pl[i].events != 0 && pl[i].events != POLLOUT)
1299 				DPRINTFN(0, "weird events %x\n", pl[i].events);
1300 #endif
1301 			pl[i].revents |= POLLHUP;
1302 			break;
1303 		}
1304 		if (pl[i].events & (POLLPRI | POLLRDBAND))
1305 			pl[i].revents |= pl[i].events & (POLLPRI | POLLRDBAND);
1306 		break;
1307 	default:
1308 		KASSERT(0);
1309 	}
1310 
1311 	DPRINTFN(1, "poll get %lu/%d fd %d revents %02x serial %lu filt %d\n",
1312 	    i+1, nfds, pl[i].fd, pl[i].revents, (unsigned long)kevp->udata,
1313 	    kevp->filter);
1314 	if (!already_seen && (pl[i].revents != 0))
1315 		return (1);
1316 
1317 	return (0);
1318 }
1319 
1320 /*
1321  * utrace system call
1322  */
1323 int
1324 sys_utrace(struct proc *curp, void *v, register_t *retval)
1325 {
1326 #ifdef KTRACE
1327 	struct sys_utrace_args /* {
1328 		syscallarg(const char *) label;
1329 		syscallarg(const void *) addr;
1330 		syscallarg(size_t) len;
1331 	} */ *uap = v;
1332 
1333 	return (ktruser(curp, SCARG(uap, label), SCARG(uap, addr),
1334 	    SCARG(uap, len)));
1335 #else
1336 	return (0);
1337 #endif
1338 }
1339