xref: /freebsd/sys/kern/sys_generic.c (revision 724b4bfdf1306e4f2c451b6d146fe0fe0353b2c8)
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)sys_generic.c	8.5 (Berkeley) 1/21/94
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "opt_capsicum.h"
41 #include "opt_compat.h"
42 #include "opt_ktrace.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/sysproto.h>
47 #include <sys/capability.h>
48 #include <sys/filedesc.h>
49 #include <sys/filio.h>
50 #include <sys/fcntl.h>
51 #include <sys/file.h>
52 #include <sys/proc.h>
53 #include <sys/signalvar.h>
54 #include <sys/socketvar.h>
55 #include <sys/uio.h>
56 #include <sys/kernel.h>
57 #include <sys/ktr.h>
58 #include <sys/limits.h>
59 #include <sys/malloc.h>
60 #include <sys/poll.h>
61 #include <sys/resourcevar.h>
62 #include <sys/selinfo.h>
63 #include <sys/sleepqueue.h>
64 #include <sys/syscallsubr.h>
65 #include <sys/sysctl.h>
66 #include <sys/sysent.h>
67 #include <sys/vnode.h>
68 #include <sys/bio.h>
69 #include <sys/buf.h>
70 #include <sys/condvar.h>
71 #ifdef KTRACE
72 #include <sys/ktrace.h>
73 #endif
74 
75 #include <security/audit/audit.h>
76 
77 int iosize_max_clamp = 1;
78 SYSCTL_INT(_debug, OID_AUTO, iosize_max_clamp, CTLFLAG_RW,
79     &iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX");
80 /*
81  * Assert that the return value of read(2) and write(2) syscalls fits
82  * into a register.  If not, an architecture will need to provide the
83  * usermode wrappers to reconstruct the result.
84  */
85 CTASSERT(sizeof(register_t) >= sizeof(size_t));
86 
87 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
88 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
89 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
90 
91 static int	pollout(struct thread *, struct pollfd *, struct pollfd *,
92 		    u_int);
93 static int	pollscan(struct thread *, struct pollfd *, u_int);
94 static int	pollrescan(struct thread *);
95 static int	selscan(struct thread *, fd_mask **, fd_mask **, int);
96 static int	selrescan(struct thread *, fd_mask **, fd_mask **);
97 static void	selfdalloc(struct thread *, void *);
98 static void	selfdfree(struct seltd *, struct selfd *);
99 static int	dofileread(struct thread *, int, struct file *, struct uio *,
100 		    off_t, int);
101 static int	dofilewrite(struct thread *, int, struct file *, struct uio *,
102 		    off_t, int);
103 static void	doselwakeup(struct selinfo *, int);
104 static void	seltdinit(struct thread *);
105 static int	seltdwait(struct thread *, int);
106 static void	seltdclear(struct thread *);
107 
108 /*
109  * One seltd per-thread allocated on demand as needed.
110  *
111  *	t - protected by st_mtx
112  * 	k - Only accessed by curthread or read-only
113  */
114 struct seltd {
115 	STAILQ_HEAD(, selfd)	st_selq;	/* (k) List of selfds. */
116 	struct selfd		*st_free1;	/* (k) free fd for read set. */
117 	struct selfd		*st_free2;	/* (k) free fd for write set. */
118 	struct mtx		st_mtx;		/* Protects struct seltd */
119 	struct cv		st_wait;	/* (t) Wait channel. */
120 	int			st_flags;	/* (t) SELTD_ flags. */
121 };
122 
123 #define	SELTD_PENDING	0x0001			/* We have pending events. */
124 #define	SELTD_RESCAN	0x0002			/* Doing a rescan. */
125 
126 /*
127  * One selfd allocated per-thread per-file-descriptor.
128  *	f - protected by sf_mtx
129  */
130 struct selfd {
131 	STAILQ_ENTRY(selfd)	sf_link;	/* (k) fds owned by this td. */
132 	TAILQ_ENTRY(selfd)	sf_threads;	/* (f) fds on this selinfo. */
133 	struct selinfo		*sf_si;		/* (f) selinfo when linked. */
134 	struct mtx		*sf_mtx;	/* Pointer to selinfo mtx. */
135 	struct seltd		*sf_td;		/* (k) owning seltd. */
136 	void			*sf_cookie;	/* (k) fd or pollfd. */
137 };
138 
139 static uma_zone_t selfd_zone;
140 static struct mtx_pool *mtxpool_select;
141 
142 #ifndef _SYS_SYSPROTO_H_
143 struct read_args {
144 	int	fd;
145 	void	*buf;
146 	size_t	nbyte;
147 };
148 #endif
149 int
150 sys_read(td, uap)
151 	struct thread *td;
152 	struct read_args *uap;
153 {
154 	struct uio auio;
155 	struct iovec aiov;
156 	int error;
157 
158 	if (uap->nbyte > IOSIZE_MAX)
159 		return (EINVAL);
160 	aiov.iov_base = uap->buf;
161 	aiov.iov_len = uap->nbyte;
162 	auio.uio_iov = &aiov;
163 	auio.uio_iovcnt = 1;
164 	auio.uio_resid = uap->nbyte;
165 	auio.uio_segflg = UIO_USERSPACE;
166 	error = kern_readv(td, uap->fd, &auio);
167 	return(error);
168 }
169 
170 /*
171  * Positioned read system call
172  */
173 #ifndef _SYS_SYSPROTO_H_
174 struct pread_args {
175 	int	fd;
176 	void	*buf;
177 	size_t	nbyte;
178 	int	pad;
179 	off_t	offset;
180 };
181 #endif
182 int
183 sys_pread(td, uap)
184 	struct thread *td;
185 	struct pread_args *uap;
186 {
187 	struct uio auio;
188 	struct iovec aiov;
189 	int error;
190 
191 	if (uap->nbyte > IOSIZE_MAX)
192 		return (EINVAL);
193 	aiov.iov_base = uap->buf;
194 	aiov.iov_len = uap->nbyte;
195 	auio.uio_iov = &aiov;
196 	auio.uio_iovcnt = 1;
197 	auio.uio_resid = uap->nbyte;
198 	auio.uio_segflg = UIO_USERSPACE;
199 	error = kern_preadv(td, uap->fd, &auio, uap->offset);
200 	return(error);
201 }
202 
203 int
204 freebsd6_pread(td, uap)
205 	struct thread *td;
206 	struct freebsd6_pread_args *uap;
207 {
208 	struct pread_args oargs;
209 
210 	oargs.fd = uap->fd;
211 	oargs.buf = uap->buf;
212 	oargs.nbyte = uap->nbyte;
213 	oargs.offset = uap->offset;
214 	return (sys_pread(td, &oargs));
215 }
216 
217 /*
218  * Scatter read system call.
219  */
220 #ifndef _SYS_SYSPROTO_H_
221 struct readv_args {
222 	int	fd;
223 	struct	iovec *iovp;
224 	u_int	iovcnt;
225 };
226 #endif
227 int
228 sys_readv(struct thread *td, struct readv_args *uap)
229 {
230 	struct uio *auio;
231 	int error;
232 
233 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
234 	if (error)
235 		return (error);
236 	error = kern_readv(td, uap->fd, auio);
237 	free(auio, M_IOV);
238 	return (error);
239 }
240 
241 int
242 kern_readv(struct thread *td, int fd, struct uio *auio)
243 {
244 	struct file *fp;
245 	int error;
246 
247 	error = fget_read(td, fd, CAP_READ | CAP_SEEK, &fp);
248 	if (error)
249 		return (error);
250 	error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
251 	fdrop(fp, td);
252 	return (error);
253 }
254 
255 /*
256  * Scatter positioned read system call.
257  */
258 #ifndef _SYS_SYSPROTO_H_
259 struct preadv_args {
260 	int	fd;
261 	struct	iovec *iovp;
262 	u_int	iovcnt;
263 	off_t	offset;
264 };
265 #endif
266 int
267 sys_preadv(struct thread *td, struct preadv_args *uap)
268 {
269 	struct uio *auio;
270 	int error;
271 
272 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
273 	if (error)
274 		return (error);
275 	error = kern_preadv(td, uap->fd, auio, uap->offset);
276 	free(auio, M_IOV);
277 	return (error);
278 }
279 
280 int
281 kern_preadv(td, fd, auio, offset)
282 	struct thread *td;
283 	int fd;
284 	struct uio *auio;
285 	off_t offset;
286 {
287 	struct file *fp;
288 	int error;
289 
290 	error = fget_read(td, fd, CAP_READ, &fp);
291 	if (error)
292 		return (error);
293 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
294 		error = ESPIPE;
295 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
296 		error = EINVAL;
297 	else
298 		error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
299 	fdrop(fp, td);
300 	return (error);
301 }
302 
303 /*
304  * Common code for readv and preadv that reads data in
305  * from a file using the passed in uio, offset, and flags.
306  */
307 static int
308 dofileread(td, fd, fp, auio, offset, flags)
309 	struct thread *td;
310 	int fd;
311 	struct file *fp;
312 	struct uio *auio;
313 	off_t offset;
314 	int flags;
315 {
316 	ssize_t cnt;
317 	int error;
318 #ifdef KTRACE
319 	struct uio *ktruio = NULL;
320 #endif
321 
322 	/* Finish zero length reads right here */
323 	if (auio->uio_resid == 0) {
324 		td->td_retval[0] = 0;
325 		return(0);
326 	}
327 	auio->uio_rw = UIO_READ;
328 	auio->uio_offset = offset;
329 	auio->uio_td = td;
330 #ifdef KTRACE
331 	if (KTRPOINT(td, KTR_GENIO))
332 		ktruio = cloneuio(auio);
333 #endif
334 	cnt = auio->uio_resid;
335 	if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
336 		if (auio->uio_resid != cnt && (error == ERESTART ||
337 		    error == EINTR || error == EWOULDBLOCK))
338 			error = 0;
339 	}
340 	cnt -= auio->uio_resid;
341 #ifdef KTRACE
342 	if (ktruio != NULL) {
343 		ktruio->uio_resid = cnt;
344 		ktrgenio(fd, UIO_READ, ktruio, error);
345 	}
346 #endif
347 	td->td_retval[0] = cnt;
348 	return (error);
349 }
350 
351 #ifndef _SYS_SYSPROTO_H_
352 struct write_args {
353 	int	fd;
354 	const void *buf;
355 	size_t	nbyte;
356 };
357 #endif
358 int
359 sys_write(td, uap)
360 	struct thread *td;
361 	struct write_args *uap;
362 {
363 	struct uio auio;
364 	struct iovec aiov;
365 	int error;
366 
367 	if (uap->nbyte > IOSIZE_MAX)
368 		return (EINVAL);
369 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
370 	aiov.iov_len = uap->nbyte;
371 	auio.uio_iov = &aiov;
372 	auio.uio_iovcnt = 1;
373 	auio.uio_resid = uap->nbyte;
374 	auio.uio_segflg = UIO_USERSPACE;
375 	error = kern_writev(td, uap->fd, &auio);
376 	return(error);
377 }
378 
379 /*
380  * Positioned write system call.
381  */
382 #ifndef _SYS_SYSPROTO_H_
383 struct pwrite_args {
384 	int	fd;
385 	const void *buf;
386 	size_t	nbyte;
387 	int	pad;
388 	off_t	offset;
389 };
390 #endif
391 int
392 sys_pwrite(td, uap)
393 	struct thread *td;
394 	struct pwrite_args *uap;
395 {
396 	struct uio auio;
397 	struct iovec aiov;
398 	int error;
399 
400 	if (uap->nbyte > IOSIZE_MAX)
401 		return (EINVAL);
402 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
403 	aiov.iov_len = uap->nbyte;
404 	auio.uio_iov = &aiov;
405 	auio.uio_iovcnt = 1;
406 	auio.uio_resid = uap->nbyte;
407 	auio.uio_segflg = UIO_USERSPACE;
408 	error = kern_pwritev(td, uap->fd, &auio, uap->offset);
409 	return(error);
410 }
411 
412 int
413 freebsd6_pwrite(td, uap)
414 	struct thread *td;
415 	struct freebsd6_pwrite_args *uap;
416 {
417 	struct pwrite_args oargs;
418 
419 	oargs.fd = uap->fd;
420 	oargs.buf = uap->buf;
421 	oargs.nbyte = uap->nbyte;
422 	oargs.offset = uap->offset;
423 	return (sys_pwrite(td, &oargs));
424 }
425 
426 /*
427  * Gather write system call.
428  */
429 #ifndef _SYS_SYSPROTO_H_
430 struct writev_args {
431 	int	fd;
432 	struct	iovec *iovp;
433 	u_int	iovcnt;
434 };
435 #endif
436 int
437 sys_writev(struct thread *td, struct writev_args *uap)
438 {
439 	struct uio *auio;
440 	int error;
441 
442 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
443 	if (error)
444 		return (error);
445 	error = kern_writev(td, uap->fd, auio);
446 	free(auio, M_IOV);
447 	return (error);
448 }
449 
450 int
451 kern_writev(struct thread *td, int fd, struct uio *auio)
452 {
453 	struct file *fp;
454 	int error;
455 
456 	error = fget_write(td, fd, CAP_WRITE | CAP_SEEK, &fp);
457 	if (error)
458 		return (error);
459 	error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
460 	fdrop(fp, td);
461 	return (error);
462 }
463 
464 /*
465  * Gather positioned write system call.
466  */
467 #ifndef _SYS_SYSPROTO_H_
468 struct pwritev_args {
469 	int	fd;
470 	struct	iovec *iovp;
471 	u_int	iovcnt;
472 	off_t	offset;
473 };
474 #endif
475 int
476 sys_pwritev(struct thread *td, struct pwritev_args *uap)
477 {
478 	struct uio *auio;
479 	int error;
480 
481 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
482 	if (error)
483 		return (error);
484 	error = kern_pwritev(td, uap->fd, auio, uap->offset);
485 	free(auio, M_IOV);
486 	return (error);
487 }
488 
489 int
490 kern_pwritev(td, fd, auio, offset)
491 	struct thread *td;
492 	struct uio *auio;
493 	int fd;
494 	off_t offset;
495 {
496 	struct file *fp;
497 	int error;
498 
499 	error = fget_write(td, fd, CAP_WRITE, &fp);
500 	if (error)
501 		return (error);
502 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
503 		error = ESPIPE;
504 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
505 		error = EINVAL;
506 	else
507 		error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
508 	fdrop(fp, td);
509 	return (error);
510 }
511 
512 /*
513  * Common code for writev and pwritev that writes data to
514  * a file using the passed in uio, offset, and flags.
515  */
516 static int
517 dofilewrite(td, fd, fp, auio, offset, flags)
518 	struct thread *td;
519 	int fd;
520 	struct file *fp;
521 	struct uio *auio;
522 	off_t offset;
523 	int flags;
524 {
525 	ssize_t cnt;
526 	int error;
527 #ifdef KTRACE
528 	struct uio *ktruio = NULL;
529 #endif
530 
531 	auio->uio_rw = UIO_WRITE;
532 	auio->uio_td = td;
533 	auio->uio_offset = offset;
534 #ifdef KTRACE
535 	if (KTRPOINT(td, KTR_GENIO))
536 		ktruio = cloneuio(auio);
537 #endif
538 	cnt = auio->uio_resid;
539 	if (fp->f_type == DTYPE_VNODE)
540 		bwillwrite();
541 	if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
542 		if (auio->uio_resid != cnt && (error == ERESTART ||
543 		    error == EINTR || error == EWOULDBLOCK))
544 			error = 0;
545 		/* Socket layer is responsible for issuing SIGPIPE. */
546 		if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
547 			PROC_LOCK(td->td_proc);
548 			tdsignal(td, SIGPIPE);
549 			PROC_UNLOCK(td->td_proc);
550 		}
551 	}
552 	cnt -= auio->uio_resid;
553 #ifdef KTRACE
554 	if (ktruio != NULL) {
555 		ktruio->uio_resid = cnt;
556 		ktrgenio(fd, UIO_WRITE, ktruio, error);
557 	}
558 #endif
559 	td->td_retval[0] = cnt;
560 	return (error);
561 }
562 
563 /*
564  * Truncate a file given a file descriptor.
565  *
566  * Can't use fget_write() here, since must return EINVAL and not EBADF if the
567  * descriptor isn't writable.
568  */
569 int
570 kern_ftruncate(td, fd, length)
571 	struct thread *td;
572 	int fd;
573 	off_t length;
574 {
575 	struct file *fp;
576 	int error;
577 
578 	AUDIT_ARG_FD(fd);
579 	if (length < 0)
580 		return (EINVAL);
581 	error = fget(td, fd, CAP_FTRUNCATE, &fp);
582 	if (error)
583 		return (error);
584 	AUDIT_ARG_FILE(td->td_proc, fp);
585 	if (!(fp->f_flag & FWRITE)) {
586 		fdrop(fp, td);
587 		return (EINVAL);
588 	}
589 	error = fo_truncate(fp, length, td->td_ucred, td);
590 	fdrop(fp, td);
591 	return (error);
592 }
593 
594 #ifndef _SYS_SYSPROTO_H_
595 struct ftruncate_args {
596 	int	fd;
597 	int	pad;
598 	off_t	length;
599 };
600 #endif
601 int
602 sys_ftruncate(td, uap)
603 	struct thread *td;
604 	struct ftruncate_args *uap;
605 {
606 
607 	return (kern_ftruncate(td, uap->fd, uap->length));
608 }
609 
610 #if defined(COMPAT_43)
611 #ifndef _SYS_SYSPROTO_H_
612 struct oftruncate_args {
613 	int	fd;
614 	long	length;
615 };
616 #endif
617 int
618 oftruncate(td, uap)
619 	struct thread *td;
620 	struct oftruncate_args *uap;
621 {
622 
623 	return (kern_ftruncate(td, uap->fd, uap->length));
624 }
625 #endif /* COMPAT_43 */
626 
627 #ifndef _SYS_SYSPROTO_H_
628 struct ioctl_args {
629 	int	fd;
630 	u_long	com;
631 	caddr_t	data;
632 };
633 #endif
634 /* ARGSUSED */
635 int
636 sys_ioctl(struct thread *td, struct ioctl_args *uap)
637 {
638 	u_long com;
639 	int arg, error;
640 	u_int size;
641 	caddr_t data;
642 
643 	if (uap->com > 0xffffffff) {
644 		printf(
645 		    "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
646 		    td->td_proc->p_pid, td->td_name, uap->com);
647 		uap->com &= 0xffffffff;
648 	}
649 	com = uap->com;
650 
651 	/*
652 	 * Interpret high order word to find amount of data to be
653 	 * copied to/from the user's address space.
654 	 */
655 	size = IOCPARM_LEN(com);
656 	if ((size > IOCPARM_MAX) ||
657 	    ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
658 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
659 	    ((com & IOC_OUT) && size == 0) ||
660 #else
661 	    ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
662 #endif
663 	    ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
664 		return (ENOTTY);
665 
666 	if (size > 0) {
667 		if (com & IOC_VOID) {
668 			/* Integer argument. */
669 			arg = (intptr_t)uap->data;
670 			data = (void *)&arg;
671 			size = 0;
672 		} else
673 			data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
674 	} else
675 		data = (void *)&uap->data;
676 	if (com & IOC_IN) {
677 		error = copyin(uap->data, data, (u_int)size);
678 		if (error) {
679 			if (size > 0)
680 				free(data, M_IOCTLOPS);
681 			return (error);
682 		}
683 	} else if (com & IOC_OUT) {
684 		/*
685 		 * Zero the buffer so the user always
686 		 * gets back something deterministic.
687 		 */
688 		bzero(data, size);
689 	}
690 
691 	error = kern_ioctl(td, uap->fd, com, data);
692 
693 	if (error == 0 && (com & IOC_OUT))
694 		error = copyout(data, uap->data, (u_int)size);
695 
696 	if (size > 0)
697 		free(data, M_IOCTLOPS);
698 	return (error);
699 }
700 
701 int
702 kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
703 {
704 	struct file *fp;
705 	struct filedesc *fdp;
706 	int error;
707 	int tmp;
708 
709 	AUDIT_ARG_FD(fd);
710 	AUDIT_ARG_CMD(com);
711 	if ((error = fget(td, fd, CAP_IOCTL, &fp)) != 0)
712 		return (error);
713 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
714 		fdrop(fp, td);
715 		return (EBADF);
716 	}
717 	fdp = td->td_proc->p_fd;
718 	switch (com) {
719 	case FIONCLEX:
720 		FILEDESC_XLOCK(fdp);
721 		fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
722 		FILEDESC_XUNLOCK(fdp);
723 		goto out;
724 	case FIOCLEX:
725 		FILEDESC_XLOCK(fdp);
726 		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
727 		FILEDESC_XUNLOCK(fdp);
728 		goto out;
729 	case FIONBIO:
730 		if ((tmp = *(int *)data))
731 			atomic_set_int(&fp->f_flag, FNONBLOCK);
732 		else
733 			atomic_clear_int(&fp->f_flag, FNONBLOCK);
734 		data = (void *)&tmp;
735 		break;
736 	case FIOASYNC:
737 		if ((tmp = *(int *)data))
738 			atomic_set_int(&fp->f_flag, FASYNC);
739 		else
740 			atomic_clear_int(&fp->f_flag, FASYNC);
741 		data = (void *)&tmp;
742 		break;
743 	}
744 
745 	error = fo_ioctl(fp, com, data, td->td_ucred, td);
746 out:
747 	fdrop(fp, td);
748 	return (error);
749 }
750 
751 int
752 poll_no_poll(int events)
753 {
754 	/*
755 	 * Return true for read/write.  If the user asked for something
756 	 * special, return POLLNVAL, so that clients have a way of
757 	 * determining reliably whether or not the extended
758 	 * functionality is present without hard-coding knowledge
759 	 * of specific filesystem implementations.
760 	 */
761 	if (events & ~POLLSTANDARD)
762 		return (POLLNVAL);
763 
764 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
765 }
766 
767 int
768 sys_pselect(struct thread *td, struct pselect_args *uap)
769 {
770 	struct timespec ts;
771 	struct timeval tv, *tvp;
772 	sigset_t set, *uset;
773 	int error;
774 
775 	if (uap->ts != NULL) {
776 		error = copyin(uap->ts, &ts, sizeof(ts));
777 		if (error != 0)
778 		    return (error);
779 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
780 		tvp = &tv;
781 	} else
782 		tvp = NULL;
783 	if (uap->sm != NULL) {
784 		error = copyin(uap->sm, &set, sizeof(set));
785 		if (error != 0)
786 			return (error);
787 		uset = &set;
788 	} else
789 		uset = NULL;
790 	return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
791 	    uset, NFDBITS));
792 }
793 
794 int
795 kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
796     struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
797 {
798 	int error;
799 
800 	if (uset != NULL) {
801 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
802 		    &td->td_oldsigmask, 0);
803 		if (error != 0)
804 			return (error);
805 		td->td_pflags |= TDP_OLDMASK;
806 		/*
807 		 * Make sure that ast() is called on return to
808 		 * usermode and TDP_OLDMASK is cleared, restoring old
809 		 * sigmask.
810 		 */
811 		thread_lock(td);
812 		td->td_flags |= TDF_ASTPENDING;
813 		thread_unlock(td);
814 	}
815 	error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
816 	return (error);
817 }
818 
819 #ifndef _SYS_SYSPROTO_H_
820 struct select_args {
821 	int	nd;
822 	fd_set	*in, *ou, *ex;
823 	struct	timeval *tv;
824 };
825 #endif
826 int
827 sys_select(struct thread *td, struct select_args *uap)
828 {
829 	struct timeval tv, *tvp;
830 	int error;
831 
832 	if (uap->tv != NULL) {
833 		error = copyin(uap->tv, &tv, sizeof(tv));
834 		if (error)
835 			return (error);
836 		tvp = &tv;
837 	} else
838 		tvp = NULL;
839 
840 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
841 	    NFDBITS));
842 }
843 
844 /*
845  * In the unlikely case when user specified n greater then the last
846  * open file descriptor, check that no bits are set after the last
847  * valid fd.  We must return EBADF if any is set.
848  *
849  * There are applications that rely on the behaviour.
850  *
851  * nd is fd_lastfile + 1.
852  */
853 static int
854 select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits)
855 {
856 	char *addr, *oaddr;
857 	int b, i, res;
858 	uint8_t bits;
859 
860 	if (nd >= ndu || fd_in == NULL)
861 		return (0);
862 
863 	oaddr = NULL;
864 	bits = 0; /* silence gcc */
865 	for (i = nd; i < ndu; i++) {
866 		b = i / NBBY;
867 #if BYTE_ORDER == LITTLE_ENDIAN
868 		addr = (char *)fd_in + b;
869 #else
870 		addr = (char *)fd_in;
871 		if (abi_nfdbits == NFDBITS) {
872 			addr += rounddown(b, sizeof(fd_mask)) +
873 			    sizeof(fd_mask) - 1 - b % sizeof(fd_mask);
874 		} else {
875 			addr += rounddown(b, sizeof(uint32_t)) +
876 			    sizeof(uint32_t) - 1 - b % sizeof(uint32_t);
877 		}
878 #endif
879 		if (addr != oaddr) {
880 			res = fubyte(addr);
881 			if (res == -1)
882 				return (EFAULT);
883 			oaddr = addr;
884 			bits = res;
885 		}
886 		if ((bits & (1 << (i % NBBY))) != 0)
887 			return (EBADF);
888 	}
889 	return (0);
890 }
891 
892 int
893 kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
894     fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
895 {
896 	struct filedesc *fdp;
897 	/*
898 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
899 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
900 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
901 	 * of 256.
902 	 */
903 	fd_mask s_selbits[howmany(2048, NFDBITS)];
904 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
905 	struct timeval atv, rtv, ttv;
906 	int error, lf, ndu, timo;
907 	u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
908 
909 	if (nd < 0)
910 		return (EINVAL);
911 	fdp = td->td_proc->p_fd;
912 	ndu = nd;
913 	lf = fdp->fd_lastfile;
914 	if (nd > lf + 1)
915 		nd = lf + 1;
916 
917 	error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits);
918 	if (error != 0)
919 		return (error);
920 	error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits);
921 	if (error != 0)
922 		return (error);
923 	error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits);
924 	if (error != 0)
925 		return (error);
926 
927 	/*
928 	 * Allocate just enough bits for the non-null fd_sets.  Use the
929 	 * preallocated auto buffer if possible.
930 	 */
931 	nfdbits = roundup(nd, NFDBITS);
932 	ncpbytes = nfdbits / NBBY;
933 	ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
934 	nbufbytes = 0;
935 	if (fd_in != NULL)
936 		nbufbytes += 2 * ncpbytes;
937 	if (fd_ou != NULL)
938 		nbufbytes += 2 * ncpbytes;
939 	if (fd_ex != NULL)
940 		nbufbytes += 2 * ncpbytes;
941 	if (nbufbytes <= sizeof s_selbits)
942 		selbits = &s_selbits[0];
943 	else
944 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
945 
946 	/*
947 	 * Assign pointers into the bit buffers and fetch the input bits.
948 	 * Put the output buffers together so that they can be bzeroed
949 	 * together.
950 	 */
951 	sbp = selbits;
952 #define	getbits(name, x) \
953 	do {								\
954 		if (name == NULL) {					\
955 			ibits[x] = NULL;				\
956 			obits[x] = NULL;				\
957 		} else {						\
958 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
959 			obits[x] = sbp;					\
960 			sbp += ncpbytes / sizeof *sbp;			\
961 			error = copyin(name, ibits[x], ncpubytes);	\
962 			if (error != 0)					\
963 				goto done;				\
964 			bzero((char *)ibits[x] + ncpubytes,		\
965 			    ncpbytes - ncpubytes);			\
966 		}							\
967 	} while (0)
968 	getbits(fd_in, 0);
969 	getbits(fd_ou, 1);
970 	getbits(fd_ex, 2);
971 #undef	getbits
972 
973 #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
974 	/*
975 	 * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
976 	 * we are running under 32-bit emulation. This should be more
977 	 * generic.
978 	 */
979 #define swizzle_fdset(bits)						\
980 	if (abi_nfdbits != NFDBITS && bits != NULL) {			\
981 		int i;							\
982 		for (i = 0; i < ncpbytes / sizeof *sbp; i++)		\
983 			bits[i] = (bits[i] >> 32) | (bits[i] << 32);	\
984 	}
985 #else
986 #define swizzle_fdset(bits)
987 #endif
988 
989 	/* Make sure the bit order makes it through an ABI transition */
990 	swizzle_fdset(ibits[0]);
991 	swizzle_fdset(ibits[1]);
992 	swizzle_fdset(ibits[2]);
993 
994 	if (nbufbytes != 0)
995 		bzero(selbits, nbufbytes / 2);
996 
997 	if (tvp != NULL) {
998 		atv = *tvp;
999 		if (itimerfix(&atv)) {
1000 			error = EINVAL;
1001 			goto done;
1002 		}
1003 		getmicrouptime(&rtv);
1004 		timevaladd(&atv, &rtv);
1005 	} else {
1006 		atv.tv_sec = 0;
1007 		atv.tv_usec = 0;
1008 	}
1009 	timo = 0;
1010 	seltdinit(td);
1011 	/* Iterate until the timeout expires or descriptors become ready. */
1012 	for (;;) {
1013 		error = selscan(td, ibits, obits, nd);
1014 		if (error || td->td_retval[0] != 0)
1015 			break;
1016 		if (atv.tv_sec || atv.tv_usec) {
1017 			getmicrouptime(&rtv);
1018 			if (timevalcmp(&rtv, &atv, >=))
1019 				break;
1020 			ttv = atv;
1021 			timevalsub(&ttv, &rtv);
1022 			timo = ttv.tv_sec > 24 * 60 * 60 ?
1023 			    24 * 60 * 60 * hz : tvtohz(&ttv);
1024 		}
1025 		error = seltdwait(td, timo);
1026 		if (error)
1027 			break;
1028 		error = selrescan(td, ibits, obits);
1029 		if (error || td->td_retval[0] != 0)
1030 			break;
1031 	}
1032 	seltdclear(td);
1033 
1034 done:
1035 	/* select is not restarted after signals... */
1036 	if (error == ERESTART)
1037 		error = EINTR;
1038 	if (error == EWOULDBLOCK)
1039 		error = 0;
1040 
1041 	/* swizzle bit order back, if necessary */
1042 	swizzle_fdset(obits[0]);
1043 	swizzle_fdset(obits[1]);
1044 	swizzle_fdset(obits[2]);
1045 #undef swizzle_fdset
1046 
1047 #define	putbits(name, x) \
1048 	if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
1049 		error = error2;
1050 	if (error == 0) {
1051 		int error2;
1052 
1053 		putbits(fd_in, 0);
1054 		putbits(fd_ou, 1);
1055 		putbits(fd_ex, 2);
1056 #undef putbits
1057 	}
1058 	if (selbits != &s_selbits[0])
1059 		free(selbits, M_SELECT);
1060 
1061 	return (error);
1062 }
1063 /*
1064  * Convert a select bit set to poll flags.
1065  *
1066  * The backend always returns POLLHUP/POLLERR if appropriate and we
1067  * return this as a set bit in any set.
1068  */
1069 static int select_flags[3] = {
1070     POLLRDNORM | POLLHUP | POLLERR,
1071     POLLWRNORM | POLLHUP | POLLERR,
1072     POLLRDBAND | POLLERR
1073 };
1074 
1075 /*
1076  * Compute the fo_poll flags required for a fd given by the index and
1077  * bit position in the fd_mask array.
1078  */
1079 static __inline int
1080 selflags(fd_mask **ibits, int idx, fd_mask bit)
1081 {
1082 	int flags;
1083 	int msk;
1084 
1085 	flags = 0;
1086 	for (msk = 0; msk < 3; msk++) {
1087 		if (ibits[msk] == NULL)
1088 			continue;
1089 		if ((ibits[msk][idx] & bit) == 0)
1090 			continue;
1091 		flags |= select_flags[msk];
1092 	}
1093 	return (flags);
1094 }
1095 
1096 /*
1097  * Set the appropriate output bits given a mask of fired events and the
1098  * input bits originally requested.
1099  */
1100 static __inline int
1101 selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
1102 {
1103 	int msk;
1104 	int n;
1105 
1106 	n = 0;
1107 	for (msk = 0; msk < 3; msk++) {
1108 		if ((events & select_flags[msk]) == 0)
1109 			continue;
1110 		if (ibits[msk] == NULL)
1111 			continue;
1112 		if ((ibits[msk][idx] & bit) == 0)
1113 			continue;
1114 		/*
1115 		 * XXX Check for a duplicate set.  This can occur because a
1116 		 * socket calls selrecord() twice for each poll() call
1117 		 * resulting in two selfds per real fd.  selrescan() will
1118 		 * call selsetbits twice as a result.
1119 		 */
1120 		if ((obits[msk][idx] & bit) != 0)
1121 			continue;
1122 		obits[msk][idx] |= bit;
1123 		n++;
1124 	}
1125 
1126 	return (n);
1127 }
1128 
1129 static __inline int
1130 getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp)
1131 {
1132 	struct file *fp;
1133 #ifdef CAPABILITIES
1134 	struct file *fp_fromcap;
1135 	int error;
1136 #endif
1137 
1138 	if ((fp = fget_unlocked(fdp, fd)) == NULL)
1139 		return (EBADF);
1140 #ifdef CAPABILITIES
1141 	/*
1142 	 * If the file descriptor is for a capability, test rights and use
1143 	 * the file descriptor references by the capability.
1144 	 */
1145 	error = cap_funwrap(fp, CAP_POLL_EVENT, &fp_fromcap);
1146 	if (error) {
1147 		fdrop(fp, curthread);
1148 		return (error);
1149 	}
1150 	if (fp != fp_fromcap) {
1151 		fhold(fp_fromcap);
1152 		fdrop(fp, curthread);
1153 		fp = fp_fromcap;
1154 	}
1155 #endif /* CAPABILITIES */
1156 	*fpp = fp;
1157 	return (0);
1158 }
1159 
1160 /*
1161  * Traverse the list of fds attached to this thread's seltd and check for
1162  * completion.
1163  */
1164 static int
1165 selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
1166 {
1167 	struct filedesc *fdp;
1168 	struct selinfo *si;
1169 	struct seltd *stp;
1170 	struct selfd *sfp;
1171 	struct selfd *sfn;
1172 	struct file *fp;
1173 	fd_mask bit;
1174 	int fd, ev, n, idx;
1175 	int error;
1176 
1177 	fdp = td->td_proc->p_fd;
1178 	stp = td->td_sel;
1179 	n = 0;
1180 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1181 		fd = (int)(uintptr_t)sfp->sf_cookie;
1182 		si = sfp->sf_si;
1183 		selfdfree(stp, sfp);
1184 		/* If the selinfo wasn't cleared the event didn't fire. */
1185 		if (si != NULL)
1186 			continue;
1187 		error = getselfd_cap(fdp, fd, &fp);
1188 		if (error)
1189 			return (error);
1190 		idx = fd / NFDBITS;
1191 		bit = (fd_mask)1 << (fd % NFDBITS);
1192 		ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
1193 		fdrop(fp, td);
1194 		if (ev != 0)
1195 			n += selsetbits(ibits, obits, idx, bit, ev);
1196 	}
1197 	stp->st_flags = 0;
1198 	td->td_retval[0] = n;
1199 	return (0);
1200 }
1201 
1202 /*
1203  * Perform the initial filedescriptor scan and register ourselves with
1204  * each selinfo.
1205  */
1206 static int
1207 selscan(td, ibits, obits, nfd)
1208 	struct thread *td;
1209 	fd_mask **ibits, **obits;
1210 	int nfd;
1211 {
1212 	struct filedesc *fdp;
1213 	struct file *fp;
1214 	fd_mask bit;
1215 	int ev, flags, end, fd;
1216 	int n, idx;
1217 	int error;
1218 
1219 	fdp = td->td_proc->p_fd;
1220 	n = 0;
1221 	for (idx = 0, fd = 0; fd < nfd; idx++) {
1222 		end = imin(fd + NFDBITS, nfd);
1223 		for (bit = 1; fd < end; bit <<= 1, fd++) {
1224 			/* Compute the list of events we're interested in. */
1225 			flags = selflags(ibits, idx, bit);
1226 			if (flags == 0)
1227 				continue;
1228 			error = getselfd_cap(fdp, fd, &fp);
1229 			if (error)
1230 				return (error);
1231 			selfdalloc(td, (void *)(uintptr_t)fd);
1232 			ev = fo_poll(fp, flags, td->td_ucred, td);
1233 			fdrop(fp, td);
1234 			if (ev != 0)
1235 				n += selsetbits(ibits, obits, idx, bit, ev);
1236 		}
1237 	}
1238 
1239 	td->td_retval[0] = n;
1240 	return (0);
1241 }
1242 
1243 #ifndef _SYS_SYSPROTO_H_
1244 struct poll_args {
1245 	struct pollfd *fds;
1246 	u_int	nfds;
1247 	int	timeout;
1248 };
1249 #endif
1250 int
1251 sys_poll(td, uap)
1252 	struct thread *td;
1253 	struct poll_args *uap;
1254 {
1255 	struct pollfd *bits;
1256 	struct pollfd smallbits[32];
1257 	struct timeval atv, rtv, ttv;
1258 	int error, timo;
1259 	u_int nfds;
1260 	size_t ni;
1261 
1262 	nfds = uap->nfds;
1263 	if (nfds > maxfilesperproc && nfds > FD_SETSIZE)
1264 		return (EINVAL);
1265 	ni = nfds * sizeof(struct pollfd);
1266 	if (ni > sizeof(smallbits))
1267 		bits = malloc(ni, M_TEMP, M_WAITOK);
1268 	else
1269 		bits = smallbits;
1270 	error = copyin(uap->fds, bits, ni);
1271 	if (error)
1272 		goto done;
1273 	if (uap->timeout != INFTIM) {
1274 		atv.tv_sec = uap->timeout / 1000;
1275 		atv.tv_usec = (uap->timeout % 1000) * 1000;
1276 		if (itimerfix(&atv)) {
1277 			error = EINVAL;
1278 			goto done;
1279 		}
1280 		getmicrouptime(&rtv);
1281 		timevaladd(&atv, &rtv);
1282 	} else {
1283 		atv.tv_sec = 0;
1284 		atv.tv_usec = 0;
1285 	}
1286 	timo = 0;
1287 	seltdinit(td);
1288 	/* Iterate until the timeout expires or descriptors become ready. */
1289 	for (;;) {
1290 		error = pollscan(td, bits, nfds);
1291 		if (error || td->td_retval[0] != 0)
1292 			break;
1293 		if (atv.tv_sec || atv.tv_usec) {
1294 			getmicrouptime(&rtv);
1295 			if (timevalcmp(&rtv, &atv, >=))
1296 				break;
1297 			ttv = atv;
1298 			timevalsub(&ttv, &rtv);
1299 			timo = ttv.tv_sec > 24 * 60 * 60 ?
1300 			    24 * 60 * 60 * hz : tvtohz(&ttv);
1301 		}
1302 		error = seltdwait(td, timo);
1303 		if (error)
1304 			break;
1305 		error = pollrescan(td);
1306 		if (error || td->td_retval[0] != 0)
1307 			break;
1308 	}
1309 	seltdclear(td);
1310 
1311 done:
1312 	/* poll is not restarted after signals... */
1313 	if (error == ERESTART)
1314 		error = EINTR;
1315 	if (error == EWOULDBLOCK)
1316 		error = 0;
1317 	if (error == 0) {
1318 		error = pollout(td, bits, uap->fds, nfds);
1319 		if (error)
1320 			goto out;
1321 	}
1322 out:
1323 	if (ni > sizeof(smallbits))
1324 		free(bits, M_TEMP);
1325 	return (error);
1326 }
1327 
1328 static int
1329 pollrescan(struct thread *td)
1330 {
1331 	struct seltd *stp;
1332 	struct selfd *sfp;
1333 	struct selfd *sfn;
1334 	struct selinfo *si;
1335 	struct filedesc *fdp;
1336 	struct file *fp;
1337 	struct pollfd *fd;
1338 	int n;
1339 
1340 	n = 0;
1341 	fdp = td->td_proc->p_fd;
1342 	stp = td->td_sel;
1343 	FILEDESC_SLOCK(fdp);
1344 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1345 		fd = (struct pollfd *)sfp->sf_cookie;
1346 		si = sfp->sf_si;
1347 		selfdfree(stp, sfp);
1348 		/* If the selinfo wasn't cleared the event didn't fire. */
1349 		if (si != NULL)
1350 			continue;
1351 		fp = fdp->fd_ofiles[fd->fd];
1352 #ifdef CAPABILITIES
1353 		if ((fp == NULL)
1354 		    || (cap_funwrap(fp, CAP_POLL_EVENT, &fp) != 0)) {
1355 #else
1356 		if (fp == NULL) {
1357 #endif
1358 			fd->revents = POLLNVAL;
1359 			n++;
1360 			continue;
1361 		}
1362 
1363 		/*
1364 		 * Note: backend also returns POLLHUP and
1365 		 * POLLERR if appropriate.
1366 		 */
1367 		fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1368 		if (fd->revents != 0)
1369 			n++;
1370 	}
1371 	FILEDESC_SUNLOCK(fdp);
1372 	stp->st_flags = 0;
1373 	td->td_retval[0] = n;
1374 	return (0);
1375 }
1376 
1377 
1378 static int
1379 pollout(td, fds, ufds, nfd)
1380 	struct thread *td;
1381 	struct pollfd *fds;
1382 	struct pollfd *ufds;
1383 	u_int nfd;
1384 {
1385 	int error = 0;
1386 	u_int i = 0;
1387 	u_int n = 0;
1388 
1389 	for (i = 0; i < nfd; i++) {
1390 		error = copyout(&fds->revents, &ufds->revents,
1391 		    sizeof(ufds->revents));
1392 		if (error)
1393 			return (error);
1394 		if (fds->revents != 0)
1395 			n++;
1396 		fds++;
1397 		ufds++;
1398 	}
1399 	td->td_retval[0] = n;
1400 	return (0);
1401 }
1402 
1403 static int
1404 pollscan(td, fds, nfd)
1405 	struct thread *td;
1406 	struct pollfd *fds;
1407 	u_int nfd;
1408 {
1409 	struct filedesc *fdp = td->td_proc->p_fd;
1410 	int i;
1411 	struct file *fp;
1412 	int n = 0;
1413 
1414 	FILEDESC_SLOCK(fdp);
1415 	for (i = 0; i < nfd; i++, fds++) {
1416 		if (fds->fd >= fdp->fd_nfiles) {
1417 			fds->revents = POLLNVAL;
1418 			n++;
1419 		} else if (fds->fd < 0) {
1420 			fds->revents = 0;
1421 		} else {
1422 			fp = fdp->fd_ofiles[fds->fd];
1423 #ifdef CAPABILITIES
1424 			if ((fp == NULL)
1425 			    || (cap_funwrap(fp, CAP_POLL_EVENT, &fp) != 0)) {
1426 #else
1427 			if (fp == NULL) {
1428 #endif
1429 				fds->revents = POLLNVAL;
1430 				n++;
1431 			} else {
1432 				/*
1433 				 * Note: backend also returns POLLHUP and
1434 				 * POLLERR if appropriate.
1435 				 */
1436 				selfdalloc(td, fds);
1437 				fds->revents = fo_poll(fp, fds->events,
1438 				    td->td_ucred, td);
1439 				/*
1440 				 * POSIX requires POLLOUT to be never
1441 				 * set simultaneously with POLLHUP.
1442 				 */
1443 				if ((fds->revents & POLLHUP) != 0)
1444 					fds->revents &= ~POLLOUT;
1445 
1446 				if (fds->revents != 0)
1447 					n++;
1448 			}
1449 		}
1450 	}
1451 	FILEDESC_SUNLOCK(fdp);
1452 	td->td_retval[0] = n;
1453 	return (0);
1454 }
1455 
1456 /*
1457  * OpenBSD poll system call.
1458  *
1459  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
1460  */
1461 #ifndef _SYS_SYSPROTO_H_
1462 struct openbsd_poll_args {
1463 	struct pollfd *fds;
1464 	u_int	nfds;
1465 	int	timeout;
1466 };
1467 #endif
1468 int
1469 sys_openbsd_poll(td, uap)
1470 	register struct thread *td;
1471 	register struct openbsd_poll_args *uap;
1472 {
1473 	return (sys_poll(td, (struct poll_args *)uap));
1474 }
1475 
1476 /*
1477  * Preallocate two selfds associated with 'cookie'.  Some fo_poll routines
1478  * have two select sets, one for read and another for write.
1479  */
1480 static void
1481 selfdalloc(struct thread *td, void *cookie)
1482 {
1483 	struct seltd *stp;
1484 
1485 	stp = td->td_sel;
1486 	if (stp->st_free1 == NULL)
1487 		stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1488 	stp->st_free1->sf_td = stp;
1489 	stp->st_free1->sf_cookie = cookie;
1490 	if (stp->st_free2 == NULL)
1491 		stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1492 	stp->st_free2->sf_td = stp;
1493 	stp->st_free2->sf_cookie = cookie;
1494 }
1495 
1496 static void
1497 selfdfree(struct seltd *stp, struct selfd *sfp)
1498 {
1499 	STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
1500 	mtx_lock(sfp->sf_mtx);
1501 	if (sfp->sf_si)
1502 		TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1503 	mtx_unlock(sfp->sf_mtx);
1504 	uma_zfree(selfd_zone, sfp);
1505 }
1506 
1507 /* Drain the waiters tied to all the selfd belonging the specified selinfo. */
1508 void
1509 seldrain(sip)
1510         struct selinfo *sip;
1511 {
1512 
1513 	/*
1514 	 * This feature is already provided by doselwakeup(), thus it is
1515 	 * enough to go for it.
1516 	 * Eventually, the context, should take care to avoid races
1517 	 * between thread calling select()/poll() and file descriptor
1518 	 * detaching, but, again, the races are just the same as
1519 	 * selwakeup().
1520 	 */
1521         doselwakeup(sip, -1);
1522 }
1523 
1524 /*
1525  * Record a select request.
1526  */
1527 void
1528 selrecord(selector, sip)
1529 	struct thread *selector;
1530 	struct selinfo *sip;
1531 {
1532 	struct selfd *sfp;
1533 	struct seltd *stp;
1534 	struct mtx *mtxp;
1535 
1536 	stp = selector->td_sel;
1537 	/*
1538 	 * Don't record when doing a rescan.
1539 	 */
1540 	if (stp->st_flags & SELTD_RESCAN)
1541 		return;
1542 	/*
1543 	 * Grab one of the preallocated descriptors.
1544 	 */
1545 	sfp = NULL;
1546 	if ((sfp = stp->st_free1) != NULL)
1547 		stp->st_free1 = NULL;
1548 	else if ((sfp = stp->st_free2) != NULL)
1549 		stp->st_free2 = NULL;
1550 	else
1551 		panic("selrecord: No free selfd on selq");
1552 	mtxp = sip->si_mtx;
1553 	if (mtxp == NULL)
1554 		mtxp = mtx_pool_find(mtxpool_select, sip);
1555 	/*
1556 	 * Initialize the sfp and queue it in the thread.
1557 	 */
1558 	sfp->sf_si = sip;
1559 	sfp->sf_mtx = mtxp;
1560 	STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1561 	/*
1562 	 * Now that we've locked the sip, check for initialization.
1563 	 */
1564 	mtx_lock(mtxp);
1565 	if (sip->si_mtx == NULL) {
1566 		sip->si_mtx = mtxp;
1567 		TAILQ_INIT(&sip->si_tdlist);
1568 	}
1569 	/*
1570 	 * Add this thread to the list of selfds listening on this selinfo.
1571 	 */
1572 	TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1573 	mtx_unlock(sip->si_mtx);
1574 }
1575 
1576 /* Wake up a selecting thread. */
1577 void
1578 selwakeup(sip)
1579 	struct selinfo *sip;
1580 {
1581 	doselwakeup(sip, -1);
1582 }
1583 
1584 /* Wake up a selecting thread, and set its priority. */
1585 void
1586 selwakeuppri(sip, pri)
1587 	struct selinfo *sip;
1588 	int pri;
1589 {
1590 	doselwakeup(sip, pri);
1591 }
1592 
1593 /*
1594  * Do a wakeup when a selectable event occurs.
1595  */
1596 static void
1597 doselwakeup(sip, pri)
1598 	struct selinfo *sip;
1599 	int pri;
1600 {
1601 	struct selfd *sfp;
1602 	struct selfd *sfn;
1603 	struct seltd *stp;
1604 
1605 	/* If it's not initialized there can't be any waiters. */
1606 	if (sip->si_mtx == NULL)
1607 		return;
1608 	/*
1609 	 * Locking the selinfo locks all selfds associated with it.
1610 	 */
1611 	mtx_lock(sip->si_mtx);
1612 	TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1613 		/*
1614 		 * Once we remove this sfp from the list and clear the
1615 		 * sf_si seltdclear will know to ignore this si.
1616 		 */
1617 		TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1618 		sfp->sf_si = NULL;
1619 		stp = sfp->sf_td;
1620 		mtx_lock(&stp->st_mtx);
1621 		stp->st_flags |= SELTD_PENDING;
1622 		cv_broadcastpri(&stp->st_wait, pri);
1623 		mtx_unlock(&stp->st_mtx);
1624 	}
1625 	mtx_unlock(sip->si_mtx);
1626 }
1627 
1628 static void
1629 seltdinit(struct thread *td)
1630 {
1631 	struct seltd *stp;
1632 
1633 	if ((stp = td->td_sel) != NULL)
1634 		goto out;
1635 	td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1636 	mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1637 	cv_init(&stp->st_wait, "select");
1638 out:
1639 	stp->st_flags = 0;
1640 	STAILQ_INIT(&stp->st_selq);
1641 }
1642 
1643 static int
1644 seltdwait(struct thread *td, int timo)
1645 {
1646 	struct seltd *stp;
1647 	int error;
1648 
1649 	stp = td->td_sel;
1650 	/*
1651 	 * An event of interest may occur while we do not hold the seltd
1652 	 * locked so check the pending flag before we sleep.
1653 	 */
1654 	mtx_lock(&stp->st_mtx);
1655 	/*
1656 	 * Any further calls to selrecord will be a rescan.
1657 	 */
1658 	stp->st_flags |= SELTD_RESCAN;
1659 	if (stp->st_flags & SELTD_PENDING) {
1660 		mtx_unlock(&stp->st_mtx);
1661 		return (0);
1662 	}
1663 	if (timo > 0)
1664 		error = cv_timedwait_sig(&stp->st_wait, &stp->st_mtx, timo);
1665 	else
1666 		error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
1667 	mtx_unlock(&stp->st_mtx);
1668 
1669 	return (error);
1670 }
1671 
1672 void
1673 seltdfini(struct thread *td)
1674 {
1675 	struct seltd *stp;
1676 
1677 	stp = td->td_sel;
1678 	if (stp == NULL)
1679 		return;
1680 	if (stp->st_free1)
1681 		uma_zfree(selfd_zone, stp->st_free1);
1682 	if (stp->st_free2)
1683 		uma_zfree(selfd_zone, stp->st_free2);
1684 	td->td_sel = NULL;
1685 	free(stp, M_SELECT);
1686 }
1687 
1688 /*
1689  * Remove the references to the thread from all of the objects we were
1690  * polling.
1691  */
1692 static void
1693 seltdclear(struct thread *td)
1694 {
1695 	struct seltd *stp;
1696 	struct selfd *sfp;
1697 	struct selfd *sfn;
1698 
1699 	stp = td->td_sel;
1700 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
1701 		selfdfree(stp, sfp);
1702 	stp->st_flags = 0;
1703 }
1704 
1705 static void selectinit(void *);
1706 SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
1707 static void
1708 selectinit(void *dummy __unused)
1709 {
1710 
1711 	selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL,
1712 	    NULL, NULL, UMA_ALIGN_PTR, 0);
1713 	mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);
1714 }
1715