xref: /freebsd/sys/kern/sys_generic.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
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_compat.h"
41 #include "opt_ktrace.h"
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sysproto.h>
46 #include <sys/filedesc.h>
47 #include <sys/filio.h>
48 #include <sys/fcntl.h>
49 #include <sys/file.h>
50 #include <sys/proc.h>
51 #include <sys/signalvar.h>
52 #include <sys/socketvar.h>
53 #include <sys/uio.h>
54 #include <sys/kernel.h>
55 #include <sys/ktr.h>
56 #include <sys/limits.h>
57 #include <sys/malloc.h>
58 #include <sys/poll.h>
59 #include <sys/resourcevar.h>
60 #include <sys/selinfo.h>
61 #include <sys/sleepqueue.h>
62 #include <sys/syscallsubr.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysent.h>
65 #include <sys/vnode.h>
66 #include <sys/bio.h>
67 #include <sys/buf.h>
68 #include <sys/condvar.h>
69 #ifdef KTRACE
70 #include <sys/ktrace.h>
71 #endif
72 
73 #include <security/audit/audit.h>
74 
75 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
76 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
77 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
78 
79 static int	pollscan(struct thread *, struct pollfd *, u_int);
80 static int	pollrescan(struct thread *);
81 static int	selscan(struct thread *, fd_mask **, fd_mask **, int);
82 static int	selrescan(struct thread *, fd_mask **, fd_mask **);
83 static void	selfdalloc(struct thread *, void *);
84 static void	selfdfree(struct seltd *, struct selfd *);
85 static int	dofileread(struct thread *, int, struct file *, struct uio *,
86 		    off_t, int);
87 static int	dofilewrite(struct thread *, int, struct file *, struct uio *,
88 		    off_t, int);
89 static void	doselwakeup(struct selinfo *, int);
90 static void	seltdinit(struct thread *);
91 static int	seltdwait(struct thread *, int);
92 static void	seltdclear(struct thread *);
93 
94 /*
95  * One seltd per-thread allocated on demand as needed.
96  *
97  *	t - protected by st_mtx
98  * 	k - Only accessed by curthread or read-only
99  */
100 struct seltd {
101 	STAILQ_HEAD(, selfd)	st_selq;	/* (k) List of selfds. */
102 	struct selfd		*st_free1;	/* (k) free fd for read set. */
103 	struct selfd		*st_free2;	/* (k) free fd for write set. */
104 	struct mtx		st_mtx;		/* Protects struct seltd */
105 	struct cv		st_wait;	/* (t) Wait channel. */
106 	int			st_flags;	/* (t) SELTD_ flags. */
107 };
108 
109 #define	SELTD_PENDING	0x0001			/* We have pending events. */
110 #define	SELTD_RESCAN	0x0002			/* Doing a rescan. */
111 
112 /*
113  * One selfd allocated per-thread per-file-descriptor.
114  *	f - protected by sf_mtx
115  */
116 struct selfd {
117 	STAILQ_ENTRY(selfd)	sf_link;	/* (k) fds owned by this td. */
118 	TAILQ_ENTRY(selfd)	sf_threads;	/* (f) fds on this selinfo. */
119 	struct selinfo		*sf_si;		/* (f) selinfo when linked. */
120 	struct mtx		*sf_mtx;	/* Pointer to selinfo mtx. */
121 	struct seltd		*sf_td;		/* (k) owning seltd. */
122 	void			*sf_cookie;	/* (k) fd or pollfd. */
123 };
124 
125 static uma_zone_t selfd_zone;
126 
127 #ifndef _SYS_SYSPROTO_H_
128 struct read_args {
129 	int	fd;
130 	void	*buf;
131 	size_t	nbyte;
132 };
133 #endif
134 int
135 read(td, uap)
136 	struct thread *td;
137 	struct read_args *uap;
138 {
139 	struct uio auio;
140 	struct iovec aiov;
141 	int error;
142 
143 	if (uap->nbyte > INT_MAX)
144 		return (EINVAL);
145 	aiov.iov_base = uap->buf;
146 	aiov.iov_len = uap->nbyte;
147 	auio.uio_iov = &aiov;
148 	auio.uio_iovcnt = 1;
149 	auio.uio_resid = uap->nbyte;
150 	auio.uio_segflg = UIO_USERSPACE;
151 	error = kern_readv(td, uap->fd, &auio);
152 	return(error);
153 }
154 
155 /*
156  * Positioned read system call
157  */
158 #ifndef _SYS_SYSPROTO_H_
159 struct pread_args {
160 	int	fd;
161 	void	*buf;
162 	size_t	nbyte;
163 	int	pad;
164 	off_t	offset;
165 };
166 #endif
167 int
168 pread(td, uap)
169 	struct thread *td;
170 	struct pread_args *uap;
171 {
172 	struct uio auio;
173 	struct iovec aiov;
174 	int error;
175 
176 	if (uap->nbyte > INT_MAX)
177 		return (EINVAL);
178 	aiov.iov_base = uap->buf;
179 	aiov.iov_len = uap->nbyte;
180 	auio.uio_iov = &aiov;
181 	auio.uio_iovcnt = 1;
182 	auio.uio_resid = uap->nbyte;
183 	auio.uio_segflg = UIO_USERSPACE;
184 	error = kern_preadv(td, uap->fd, &auio, uap->offset);
185 	return(error);
186 }
187 
188 int
189 freebsd6_pread(td, uap)
190 	struct thread *td;
191 	struct freebsd6_pread_args *uap;
192 {
193 	struct pread_args oargs;
194 
195 	oargs.fd = uap->fd;
196 	oargs.buf = uap->buf;
197 	oargs.nbyte = uap->nbyte;
198 	oargs.offset = uap->offset;
199 	return (pread(td, &oargs));
200 }
201 
202 /*
203  * Scatter read system call.
204  */
205 #ifndef _SYS_SYSPROTO_H_
206 struct readv_args {
207 	int	fd;
208 	struct	iovec *iovp;
209 	u_int	iovcnt;
210 };
211 #endif
212 int
213 readv(struct thread *td, struct readv_args *uap)
214 {
215 	struct uio *auio;
216 	int error;
217 
218 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
219 	if (error)
220 		return (error);
221 	error = kern_readv(td, uap->fd, auio);
222 	free(auio, M_IOV);
223 	return (error);
224 }
225 
226 int
227 kern_readv(struct thread *td, int fd, struct uio *auio)
228 {
229 	struct file *fp;
230 	int error;
231 
232 	error = fget_read(td, fd, &fp);
233 	if (error)
234 		return (error);
235 	error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
236 	fdrop(fp, td);
237 	return (error);
238 }
239 
240 /*
241  * Scatter positioned read system call.
242  */
243 #ifndef _SYS_SYSPROTO_H_
244 struct preadv_args {
245 	int	fd;
246 	struct	iovec *iovp;
247 	u_int	iovcnt;
248 	off_t	offset;
249 };
250 #endif
251 int
252 preadv(struct thread *td, struct preadv_args *uap)
253 {
254 	struct uio *auio;
255 	int error;
256 
257 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
258 	if (error)
259 		return (error);
260 	error = kern_preadv(td, uap->fd, auio, uap->offset);
261 	free(auio, M_IOV);
262 	return (error);
263 }
264 
265 int
266 kern_preadv(td, fd, auio, offset)
267 	struct thread *td;
268 	int fd;
269 	struct uio *auio;
270 	off_t offset;
271 {
272 	struct file *fp;
273 	int error;
274 
275 	error = fget_read(td, fd, &fp);
276 	if (error)
277 		return (error);
278 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
279 		error = ESPIPE;
280 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
281 		error = EINVAL;
282 	else
283 		error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
284 	fdrop(fp, td);
285 	return (error);
286 }
287 
288 /*
289  * Common code for readv and preadv that reads data in
290  * from a file using the passed in uio, offset, and flags.
291  */
292 static int
293 dofileread(td, fd, fp, auio, offset, flags)
294 	struct thread *td;
295 	int fd;
296 	struct file *fp;
297 	struct uio *auio;
298 	off_t offset;
299 	int flags;
300 {
301 	ssize_t cnt;
302 	int error;
303 #ifdef KTRACE
304 	struct uio *ktruio = NULL;
305 #endif
306 
307 	/* Finish zero length reads right here */
308 	if (auio->uio_resid == 0) {
309 		td->td_retval[0] = 0;
310 		return(0);
311 	}
312 	auio->uio_rw = UIO_READ;
313 	auio->uio_offset = offset;
314 	auio->uio_td = td;
315 #ifdef KTRACE
316 	if (KTRPOINT(td, KTR_GENIO))
317 		ktruio = cloneuio(auio);
318 #endif
319 	cnt = auio->uio_resid;
320 	if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
321 		if (auio->uio_resid != cnt && (error == ERESTART ||
322 		    error == EINTR || error == EWOULDBLOCK))
323 			error = 0;
324 	}
325 	cnt -= auio->uio_resid;
326 #ifdef KTRACE
327 	if (ktruio != NULL) {
328 		ktruio->uio_resid = cnt;
329 		ktrgenio(fd, UIO_READ, ktruio, error);
330 	}
331 #endif
332 	td->td_retval[0] = cnt;
333 	return (error);
334 }
335 
336 #ifndef _SYS_SYSPROTO_H_
337 struct write_args {
338 	int	fd;
339 	const void *buf;
340 	size_t	nbyte;
341 };
342 #endif
343 int
344 write(td, uap)
345 	struct thread *td;
346 	struct write_args *uap;
347 {
348 	struct uio auio;
349 	struct iovec aiov;
350 	int error;
351 
352 	if (uap->nbyte > INT_MAX)
353 		return (EINVAL);
354 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
355 	aiov.iov_len = uap->nbyte;
356 	auio.uio_iov = &aiov;
357 	auio.uio_iovcnt = 1;
358 	auio.uio_resid = uap->nbyte;
359 	auio.uio_segflg = UIO_USERSPACE;
360 	error = kern_writev(td, uap->fd, &auio);
361 	return(error);
362 }
363 
364 /*
365  * Positioned write system call.
366  */
367 #ifndef _SYS_SYSPROTO_H_
368 struct pwrite_args {
369 	int	fd;
370 	const void *buf;
371 	size_t	nbyte;
372 	int	pad;
373 	off_t	offset;
374 };
375 #endif
376 int
377 pwrite(td, uap)
378 	struct thread *td;
379 	struct pwrite_args *uap;
380 {
381 	struct uio auio;
382 	struct iovec aiov;
383 	int error;
384 
385 	if (uap->nbyte > INT_MAX)
386 		return (EINVAL);
387 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
388 	aiov.iov_len = uap->nbyte;
389 	auio.uio_iov = &aiov;
390 	auio.uio_iovcnt = 1;
391 	auio.uio_resid = uap->nbyte;
392 	auio.uio_segflg = UIO_USERSPACE;
393 	error = kern_pwritev(td, uap->fd, &auio, uap->offset);
394 	return(error);
395 }
396 
397 int
398 freebsd6_pwrite(td, uap)
399 	struct thread *td;
400 	struct freebsd6_pwrite_args *uap;
401 {
402 	struct pwrite_args oargs;
403 
404 	oargs.fd = uap->fd;
405 	oargs.buf = uap->buf;
406 	oargs.nbyte = uap->nbyte;
407 	oargs.offset = uap->offset;
408 	return (pwrite(td, &oargs));
409 }
410 
411 /*
412  * Gather write system call.
413  */
414 #ifndef _SYS_SYSPROTO_H_
415 struct writev_args {
416 	int	fd;
417 	struct	iovec *iovp;
418 	u_int	iovcnt;
419 };
420 #endif
421 int
422 writev(struct thread *td, struct writev_args *uap)
423 {
424 	struct uio *auio;
425 	int error;
426 
427 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
428 	if (error)
429 		return (error);
430 	error = kern_writev(td, uap->fd, auio);
431 	free(auio, M_IOV);
432 	return (error);
433 }
434 
435 int
436 kern_writev(struct thread *td, int fd, struct uio *auio)
437 {
438 	struct file *fp;
439 	int error;
440 
441 	error = fget_write(td, fd, &fp);
442 	if (error)
443 		return (error);
444 	error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
445 	fdrop(fp, td);
446 	return (error);
447 }
448 
449 /*
450  * Gather positioned write system call.
451  */
452 #ifndef _SYS_SYSPROTO_H_
453 struct pwritev_args {
454 	int	fd;
455 	struct	iovec *iovp;
456 	u_int	iovcnt;
457 	off_t	offset;
458 };
459 #endif
460 int
461 pwritev(struct thread *td, struct pwritev_args *uap)
462 {
463 	struct uio *auio;
464 	int error;
465 
466 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
467 	if (error)
468 		return (error);
469 	error = kern_pwritev(td, uap->fd, auio, uap->offset);
470 	free(auio, M_IOV);
471 	return (error);
472 }
473 
474 int
475 kern_pwritev(td, fd, auio, offset)
476 	struct thread *td;
477 	struct uio *auio;
478 	int fd;
479 	off_t offset;
480 {
481 	struct file *fp;
482 	int error;
483 
484 	error = fget_write(td, fd, &fp);
485 	if (error)
486 		return (error);
487 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
488 		error = ESPIPE;
489 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
490 		error = EINVAL;
491 	else
492 		error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
493 	fdrop(fp, td);
494 	return (error);
495 }
496 
497 /*
498  * Common code for writev and pwritev that writes data to
499  * a file using the passed in uio, offset, and flags.
500  */
501 static int
502 dofilewrite(td, fd, fp, auio, offset, flags)
503 	struct thread *td;
504 	int fd;
505 	struct file *fp;
506 	struct uio *auio;
507 	off_t offset;
508 	int flags;
509 {
510 	ssize_t cnt;
511 	int error;
512 #ifdef KTRACE
513 	struct uio *ktruio = NULL;
514 #endif
515 
516 	auio->uio_rw = UIO_WRITE;
517 	auio->uio_td = td;
518 	auio->uio_offset = offset;
519 #ifdef KTRACE
520 	if (KTRPOINT(td, KTR_GENIO))
521 		ktruio = cloneuio(auio);
522 #endif
523 	cnt = auio->uio_resid;
524 	if (fp->f_type == DTYPE_VNODE)
525 		bwillwrite();
526 	if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
527 		if (auio->uio_resid != cnt && (error == ERESTART ||
528 		    error == EINTR || error == EWOULDBLOCK))
529 			error = 0;
530 		/* Socket layer is responsible for issuing SIGPIPE. */
531 		if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
532 			PROC_LOCK(td->td_proc);
533 			psignal(td->td_proc, SIGPIPE);
534 			PROC_UNLOCK(td->td_proc);
535 		}
536 	}
537 	cnt -= auio->uio_resid;
538 #ifdef KTRACE
539 	if (ktruio != NULL) {
540 		ktruio->uio_resid = cnt;
541 		ktrgenio(fd, UIO_WRITE, ktruio, error);
542 	}
543 #endif
544 	td->td_retval[0] = cnt;
545 	return (error);
546 }
547 
548 /*
549  * Truncate a file given a file descriptor.
550  *
551  * Can't use fget_write() here, since must return EINVAL and not EBADF if the
552  * descriptor isn't writable.
553  */
554 int
555 kern_ftruncate(td, fd, length)
556 	struct thread *td;
557 	int fd;
558 	off_t length;
559 {
560 	struct file *fp;
561 	int error;
562 
563 	AUDIT_ARG(fd, fd);
564 	if (length < 0)
565 		return (EINVAL);
566 	error = fget(td, fd, &fp);
567 	if (error)
568 		return (error);
569 	AUDIT_ARG(file, td->td_proc, fp);
570 	if (!(fp->f_flag & FWRITE)) {
571 		fdrop(fp, td);
572 		return (EINVAL);
573 	}
574 	error = fo_truncate(fp, length, td->td_ucred, td);
575 	fdrop(fp, td);
576 	return (error);
577 }
578 
579 #ifndef _SYS_SYSPROTO_H_
580 struct ftruncate_args {
581 	int	fd;
582 	int	pad;
583 	off_t	length;
584 };
585 #endif
586 int
587 ftruncate(td, uap)
588 	struct thread *td;
589 	struct ftruncate_args *uap;
590 {
591 
592 	return (kern_ftruncate(td, uap->fd, uap->length));
593 }
594 
595 #if defined(COMPAT_43)
596 #ifndef _SYS_SYSPROTO_H_
597 struct oftruncate_args {
598 	int	fd;
599 	long	length;
600 };
601 #endif
602 int
603 oftruncate(td, uap)
604 	struct thread *td;
605 	struct oftruncate_args *uap;
606 {
607 
608 	return (kern_ftruncate(td, uap->fd, uap->length));
609 }
610 #endif /* COMPAT_43 */
611 
612 #ifndef _SYS_SYSPROTO_H_
613 struct ioctl_args {
614 	int	fd;
615 	u_long	com;
616 	caddr_t	data;
617 };
618 #endif
619 /* ARGSUSED */
620 int
621 ioctl(struct thread *td, struct ioctl_args *uap)
622 {
623 	u_long com;
624 	int arg, error;
625 	u_int size;
626 	caddr_t data;
627 
628 	if (uap->com > 0xffffffff) {
629 		printf(
630 		    "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
631 		    td->td_proc->p_pid, td->td_name, uap->com);
632 		uap->com &= 0xffffffff;
633 	}
634 	com = uap->com;
635 
636 	/*
637 	 * Interpret high order word to find amount of data to be
638 	 * copied to/from the user's address space.
639 	 */
640 	size = IOCPARM_LEN(com);
641 	if ((size > IOCPARM_MAX) ||
642 	    ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
643 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
644 	    ((com & IOC_OUT) && size == 0) ||
645 #else
646 	    ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
647 #endif
648 	    ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
649 		return (ENOTTY);
650 
651 	if (size > 0) {
652 		if (com & IOC_VOID) {
653 			/* Integer argument. */
654 			arg = (intptr_t)uap->data;
655 			data = (void *)&arg;
656 			size = 0;
657 		} else
658 			data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
659 	} else
660 		data = (void *)&uap->data;
661 	if (com & IOC_IN) {
662 		error = copyin(uap->data, data, (u_int)size);
663 		if (error) {
664 			if (size > 0)
665 				free(data, M_IOCTLOPS);
666 			return (error);
667 		}
668 	} else if (com & IOC_OUT) {
669 		/*
670 		 * Zero the buffer so the user always
671 		 * gets back something deterministic.
672 		 */
673 		bzero(data, size);
674 	}
675 
676 	error = kern_ioctl(td, uap->fd, com, data);
677 
678 	if (error == 0 && (com & IOC_OUT))
679 		error = copyout(data, uap->data, (u_int)size);
680 
681 	if (size > 0)
682 		free(data, M_IOCTLOPS);
683 	return (error);
684 }
685 
686 int
687 kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
688 {
689 	struct file *fp;
690 	struct filedesc *fdp;
691 	int error;
692 	int tmp;
693 
694 	if ((error = fget(td, fd, &fp)) != 0)
695 		return (error);
696 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
697 		fdrop(fp, td);
698 		return (EBADF);
699 	}
700 	fdp = td->td_proc->p_fd;
701 	switch (com) {
702 	case FIONCLEX:
703 		FILEDESC_XLOCK(fdp);
704 		fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
705 		FILEDESC_XUNLOCK(fdp);
706 		goto out;
707 	case FIOCLEX:
708 		FILEDESC_XLOCK(fdp);
709 		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
710 		FILEDESC_XUNLOCK(fdp);
711 		goto out;
712 	case FIONBIO:
713 		if ((tmp = *(int *)data))
714 			atomic_set_int(&fp->f_flag, FNONBLOCK);
715 		else
716 			atomic_clear_int(&fp->f_flag, FNONBLOCK);
717 		data = (void *)&tmp;
718 		break;
719 	case FIOASYNC:
720 		if ((tmp = *(int *)data))
721 			atomic_set_int(&fp->f_flag, FASYNC);
722 		else
723 			atomic_clear_int(&fp->f_flag, FASYNC);
724 		data = (void *)&tmp;
725 		break;
726 	}
727 
728 	error = fo_ioctl(fp, com, data, td->td_ucred, td);
729 out:
730 	fdrop(fp, td);
731 	return (error);
732 }
733 
734 #ifndef _SYS_SYSPROTO_H_
735 struct select_args {
736 	int	nd;
737 	fd_set	*in, *ou, *ex;
738 	struct	timeval *tv;
739 };
740 #endif
741 int
742 select(td, uap)
743 	register struct thread *td;
744 	register struct select_args *uap;
745 {
746 	struct timeval tv, *tvp;
747 	int error;
748 
749 	if (uap->tv != NULL) {
750 		error = copyin(uap->tv, &tv, sizeof(tv));
751 		if (error)
752 			return (error);
753 		tvp = &tv;
754 	} else
755 		tvp = NULL;
756 
757 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp));
758 }
759 
760 int
761 kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
762     fd_set *fd_ex, struct timeval *tvp)
763 {
764 	struct filedesc *fdp;
765 	/*
766 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
767 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
768 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
769 	 * of 256.
770 	 */
771 	fd_mask s_selbits[howmany(2048, NFDBITS)];
772 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
773 	struct timeval atv, rtv, ttv;
774 	int error, timo;
775 	u_int nbufbytes, ncpbytes, nfdbits;
776 
777 	if (nd < 0)
778 		return (EINVAL);
779 	fdp = td->td_proc->p_fd;
780 
781 	FILEDESC_SLOCK(fdp);
782 	if (nd > td->td_proc->p_fd->fd_nfiles)
783 		nd = td->td_proc->p_fd->fd_nfiles;   /* forgiving; slightly wrong */
784 	FILEDESC_SUNLOCK(fdp);
785 
786 	/*
787 	 * Allocate just enough bits for the non-null fd_sets.  Use the
788 	 * preallocated auto buffer if possible.
789 	 */
790 	nfdbits = roundup(nd, NFDBITS);
791 	ncpbytes = nfdbits / NBBY;
792 	nbufbytes = 0;
793 	if (fd_in != NULL)
794 		nbufbytes += 2 * ncpbytes;
795 	if (fd_ou != NULL)
796 		nbufbytes += 2 * ncpbytes;
797 	if (fd_ex != NULL)
798 		nbufbytes += 2 * ncpbytes;
799 	if (nbufbytes <= sizeof s_selbits)
800 		selbits = &s_selbits[0];
801 	else
802 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
803 
804 	/*
805 	 * Assign pointers into the bit buffers and fetch the input bits.
806 	 * Put the output buffers together so that they can be bzeroed
807 	 * together.
808 	 */
809 	sbp = selbits;
810 #define	getbits(name, x) \
811 	do {								\
812 		if (name == NULL)					\
813 			ibits[x] = NULL;				\
814 		else {							\
815 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
816 			obits[x] = sbp;					\
817 			sbp += ncpbytes / sizeof *sbp;			\
818 			error = copyin(name, ibits[x], ncpbytes);	\
819 			if (error != 0)					\
820 				goto done;				\
821 		}							\
822 	} while (0)
823 	getbits(fd_in, 0);
824 	getbits(fd_ou, 1);
825 	getbits(fd_ex, 2);
826 #undef	getbits
827 	if (nbufbytes != 0)
828 		bzero(selbits, nbufbytes / 2);
829 
830 	if (tvp != NULL) {
831 		atv = *tvp;
832 		if (itimerfix(&atv)) {
833 			error = EINVAL;
834 			goto done;
835 		}
836 		getmicrouptime(&rtv);
837 		timevaladd(&atv, &rtv);
838 	} else {
839 		atv.tv_sec = 0;
840 		atv.tv_usec = 0;
841 	}
842 	timo = 0;
843 	seltdinit(td);
844 	/* Iterate until the timeout expires or descriptors become ready. */
845 	for (;;) {
846 		error = selscan(td, ibits, obits, nd);
847 		if (error || td->td_retval[0] != 0)
848 			break;
849 		if (atv.tv_sec || atv.tv_usec) {
850 			getmicrouptime(&rtv);
851 			if (timevalcmp(&rtv, &atv, >=))
852 				break;
853 			ttv = atv;
854 			timevalsub(&ttv, &rtv);
855 			timo = ttv.tv_sec > 24 * 60 * 60 ?
856 			    24 * 60 * 60 * hz : tvtohz(&ttv);
857 		}
858 		error = seltdwait(td, timo);
859 		if (error)
860 			break;
861 		error = selrescan(td, ibits, obits);
862 		if (error || td->td_retval[0] != 0)
863 			break;
864 	}
865 	seltdclear(td);
866 
867 done:
868 	/* select is not restarted after signals... */
869 	if (error == ERESTART)
870 		error = EINTR;
871 	if (error == EWOULDBLOCK)
872 		error = 0;
873 #define	putbits(name, x) \
874 	if (name && (error2 = copyout(obits[x], name, ncpbytes))) \
875 		error = error2;
876 	if (error == 0) {
877 		int error2;
878 
879 		putbits(fd_in, 0);
880 		putbits(fd_ou, 1);
881 		putbits(fd_ex, 2);
882 #undef putbits
883 	}
884 	if (selbits != &s_selbits[0])
885 		free(selbits, M_SELECT);
886 
887 	return (error);
888 }
889 /*
890  * Convert a select bit set to poll flags.
891  *
892  * The backend always returns POLLHUP/POLLERR if appropriate and we
893  * return this as a set bit in any set.
894  */
895 static int select_flags[3] = {
896     POLLRDNORM | POLLHUP | POLLERR,
897     POLLWRNORM | POLLHUP | POLLERR,
898     POLLRDBAND | POLLHUP | POLLERR
899 };
900 
901 /*
902  * Compute the fo_poll flags required for a fd given by the index and
903  * bit position in the fd_mask array.
904  */
905 static __inline int
906 selflags(fd_mask **ibits, int idx, fd_mask bit)
907 {
908 	int flags;
909 	int msk;
910 
911 	flags = 0;
912 	for (msk = 0; msk < 3; msk++) {
913 		if (ibits[msk] == NULL)
914 			continue;
915 		if ((ibits[msk][idx] & bit) == 0)
916 			continue;
917 		flags |= select_flags[msk];
918 	}
919 	return (flags);
920 }
921 
922 /*
923  * Set the appropriate output bits given a mask of fired events and the
924  * input bits originally requested.
925  */
926 static __inline int
927 selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
928 {
929 	int msk;
930 	int n;
931 
932 	n = 0;
933 	for (msk = 0; msk < 3; msk++) {
934 		if ((events & select_flags[msk]) == 0)
935 			continue;
936 		if (ibits[msk] == NULL)
937 			continue;
938 		if ((ibits[msk][idx] & bit) == 0)
939 			continue;
940 		/*
941 		 * XXX Check for a duplicate set.  This can occur because a
942 		 * socket calls selrecord() twice for each poll() call
943 		 * resulting in two selfds per real fd.  selrescan() will
944 		 * call selsetbits twice as a result.
945 		 */
946 		if ((obits[msk][idx] & bit) != 0)
947 			continue;
948 		obits[msk][idx] |= bit;
949 		n++;
950 	}
951 
952 	return (n);
953 }
954 
955 /*
956  * Traverse the list of fds attached to this thread's seltd and check for
957  * completion.
958  */
959 static int
960 selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
961 {
962 	struct filedesc *fdp;
963 	struct selinfo *si;
964 	struct seltd *stp;
965 	struct selfd *sfp;
966 	struct selfd *sfn;
967 	struct file *fp;
968 	fd_mask bit;
969 	int fd, ev, n, idx;
970 
971 	fdp = td->td_proc->p_fd;
972 	stp = td->td_sel;
973 	n = 0;
974 	FILEDESC_SLOCK(fdp);
975 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
976 		fd = (int)(uintptr_t)sfp->sf_cookie;
977 		si = sfp->sf_si;
978 		selfdfree(stp, sfp);
979 		/* If the selinfo wasn't cleared the event didn't fire. */
980 		if (si != NULL)
981 			continue;
982 		if ((fp = fget_locked(fdp, fd)) == NULL) {
983 			FILEDESC_SUNLOCK(fdp);
984 			return (EBADF);
985 		}
986 		idx = fd / NFDBITS;
987 		bit = (fd_mask)1 << (fd % NFDBITS);
988 		ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
989 		if (ev != 0)
990 			n += selsetbits(ibits, obits, idx, bit, ev);
991 	}
992 	FILEDESC_SUNLOCK(fdp);
993 	stp->st_flags = 0;
994 	td->td_retval[0] = n;
995 	return (0);
996 }
997 
998 /*
999  * Perform the initial filedescriptor scan and register ourselves with
1000  * each selinfo.
1001  */
1002 static int
1003 selscan(td, ibits, obits, nfd)
1004 	struct thread *td;
1005 	fd_mask **ibits, **obits;
1006 	int nfd;
1007 {
1008 	struct filedesc *fdp;
1009 	struct file *fp;
1010 	fd_mask bit;
1011 	int ev, flags, end, fd;
1012 	int n, idx;
1013 
1014 	fdp = td->td_proc->p_fd;
1015 	n = 0;
1016 	FILEDESC_SLOCK(fdp);
1017 	for (idx = 0, fd = 0; fd < nfd; idx++) {
1018 		end = imin(fd + NFDBITS, nfd);
1019 		for (bit = 1; fd < end; bit <<= 1, fd++) {
1020 			/* Compute the list of events we're interested in. */
1021 			flags = selflags(ibits, idx, bit);
1022 			if (flags == 0)
1023 				continue;
1024 			if ((fp = fget_locked(fdp, fd)) == NULL) {
1025 				FILEDESC_SUNLOCK(fdp);
1026 				return (EBADF);
1027 			}
1028 			selfdalloc(td, (void *)(uintptr_t)fd);
1029 			ev = fo_poll(fp, flags, td->td_ucred, td);
1030 			if (ev != 0)
1031 				n += selsetbits(ibits, obits, idx, bit, ev);
1032 		}
1033 	}
1034 
1035 	FILEDESC_SUNLOCK(fdp);
1036 	td->td_retval[0] = n;
1037 	return (0);
1038 }
1039 
1040 #ifndef _SYS_SYSPROTO_H_
1041 struct poll_args {
1042 	struct pollfd *fds;
1043 	u_int	nfds;
1044 	int	timeout;
1045 };
1046 #endif
1047 int
1048 poll(td, uap)
1049 	struct thread *td;
1050 	struct poll_args *uap;
1051 {
1052 	struct pollfd *bits;
1053 	struct pollfd smallbits[32];
1054 	struct timeval atv, rtv, ttv;
1055 	int error = 0, timo;
1056 	u_int nfds;
1057 	size_t ni;
1058 
1059 	nfds = uap->nfds;
1060 	if (nfds > maxfilesperproc && nfds > FD_SETSIZE)
1061 		return (EINVAL);
1062 	ni = nfds * sizeof(struct pollfd);
1063 	if (ni > sizeof(smallbits))
1064 		bits = malloc(ni, M_TEMP, M_WAITOK);
1065 	else
1066 		bits = smallbits;
1067 	error = copyin(uap->fds, bits, ni);
1068 	if (error)
1069 		goto done;
1070 	if (uap->timeout != INFTIM) {
1071 		atv.tv_sec = uap->timeout / 1000;
1072 		atv.tv_usec = (uap->timeout % 1000) * 1000;
1073 		if (itimerfix(&atv)) {
1074 			error = EINVAL;
1075 			goto done;
1076 		}
1077 		getmicrouptime(&rtv);
1078 		timevaladd(&atv, &rtv);
1079 	} else {
1080 		atv.tv_sec = 0;
1081 		atv.tv_usec = 0;
1082 	}
1083 	timo = 0;
1084 	seltdinit(td);
1085 	/* Iterate until the timeout expires or descriptors become ready. */
1086 	for (;;) {
1087 		error = pollscan(td, bits, nfds);
1088 		if (error || td->td_retval[0] != 0)
1089 			break;
1090 		if (atv.tv_sec || atv.tv_usec) {
1091 			getmicrouptime(&rtv);
1092 			if (timevalcmp(&rtv, &atv, >=))
1093 				break;
1094 			ttv = atv;
1095 			timevalsub(&ttv, &rtv);
1096 			timo = ttv.tv_sec > 24 * 60 * 60 ?
1097 			    24 * 60 * 60 * hz : tvtohz(&ttv);
1098 		}
1099 		error = seltdwait(td, timo);
1100 		if (error)
1101 			break;
1102 		error = pollrescan(td);
1103 		if (error || td->td_retval[0] != 0)
1104 			break;
1105 	}
1106 	seltdclear(td);
1107 
1108 done:
1109 	/* poll is not restarted after signals... */
1110 	if (error == ERESTART)
1111 		error = EINTR;
1112 	if (error == EWOULDBLOCK)
1113 		error = 0;
1114 	if (error == 0) {
1115 		error = copyout(bits, uap->fds, ni);
1116 		if (error)
1117 			goto out;
1118 	}
1119 out:
1120 	if (ni > sizeof(smallbits))
1121 		free(bits, M_TEMP);
1122 	return (error);
1123 }
1124 
1125 static int
1126 pollrescan(struct thread *td)
1127 {
1128 	struct seltd *stp;
1129 	struct selfd *sfp;
1130 	struct selfd *sfn;
1131 	struct selinfo *si;
1132 	struct filedesc *fdp;
1133 	struct file *fp;
1134 	struct pollfd *fd;
1135 	int n;
1136 
1137 	n = 0;
1138 	fdp = td->td_proc->p_fd;
1139 	stp = td->td_sel;
1140 	FILEDESC_SLOCK(fdp);
1141 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1142 		fd = (struct pollfd *)sfp->sf_cookie;
1143 		si = sfp->sf_si;
1144 		selfdfree(stp, sfp);
1145 		/* If the selinfo wasn't cleared the event didn't fire. */
1146 		if (si != NULL)
1147 			continue;
1148 		fp = fdp->fd_ofiles[fd->fd];
1149 		if (fp == NULL) {
1150 			fd->revents = POLLNVAL;
1151 			n++;
1152 			continue;
1153 		}
1154 		/*
1155 		 * Note: backend also returns POLLHUP and
1156 		 * POLLERR if appropriate.
1157 		 */
1158 		fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1159 		if (fd->revents != 0)
1160 			n++;
1161 	}
1162 	FILEDESC_SUNLOCK(fdp);
1163 	stp->st_flags = 0;
1164 	td->td_retval[0] = n;
1165 	return (0);
1166 }
1167 
1168 
1169 static int
1170 pollscan(td, fds, nfd)
1171 	struct thread *td;
1172 	struct pollfd *fds;
1173 	u_int nfd;
1174 {
1175 	struct filedesc *fdp = td->td_proc->p_fd;
1176 	int i;
1177 	struct file *fp;
1178 	int n = 0;
1179 
1180 	FILEDESC_SLOCK(fdp);
1181 	for (i = 0; i < nfd; i++, fds++) {
1182 		if (fds->fd >= fdp->fd_nfiles) {
1183 			fds->revents = POLLNVAL;
1184 			n++;
1185 		} else if (fds->fd < 0) {
1186 			fds->revents = 0;
1187 		} else {
1188 			fp = fdp->fd_ofiles[fds->fd];
1189 			if (fp == NULL) {
1190 				fds->revents = POLLNVAL;
1191 				n++;
1192 			} else {
1193 				/*
1194 				 * Note: backend also returns POLLHUP and
1195 				 * POLLERR if appropriate.
1196 				 */
1197 				selfdalloc(td, fds);
1198 				fds->revents = fo_poll(fp, fds->events,
1199 				    td->td_ucred, td);
1200 				if (fds->revents != 0)
1201 					n++;
1202 			}
1203 		}
1204 	}
1205 	FILEDESC_SUNLOCK(fdp);
1206 	td->td_retval[0] = n;
1207 	return (0);
1208 }
1209 
1210 /*
1211  * OpenBSD poll system call.
1212  *
1213  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
1214  */
1215 #ifndef _SYS_SYSPROTO_H_
1216 struct openbsd_poll_args {
1217 	struct pollfd *fds;
1218 	u_int	nfds;
1219 	int	timeout;
1220 };
1221 #endif
1222 int
1223 openbsd_poll(td, uap)
1224 	register struct thread *td;
1225 	register struct openbsd_poll_args *uap;
1226 {
1227 	return (poll(td, (struct poll_args *)uap));
1228 }
1229 
1230 /*
1231  * XXX This was created specifically to support netncp and netsmb.  This
1232  * allows the caller to specify a socket to wait for events on.  It returns
1233  * 0 if any events matched and an error otherwise.  There is no way to
1234  * determine which events fired.
1235  */
1236 int
1237 selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td)
1238 {
1239 	struct timeval atv, rtv, ttv;
1240 	int error, timo;
1241 
1242 	if (tvp != NULL) {
1243 		atv = *tvp;
1244 		if (itimerfix(&atv))
1245 			return (EINVAL);
1246 		getmicrouptime(&rtv);
1247 		timevaladd(&atv, &rtv);
1248 	} else {
1249 		atv.tv_sec = 0;
1250 		atv.tv_usec = 0;
1251 	}
1252 
1253 	timo = 0;
1254 	seltdinit(td);
1255 	/*
1256 	 * Iterate until the timeout expires or the socket becomes ready.
1257 	 */
1258 	for (;;) {
1259 		selfdalloc(td, NULL);
1260 		error = sopoll(so, events, NULL, td);
1261 		/* error here is actually the ready events. */
1262 		if (error)
1263 			return (0);
1264 		if (atv.tv_sec || atv.tv_usec) {
1265 			getmicrouptime(&rtv);
1266 			if (timevalcmp(&rtv, &atv, >=)) {
1267 				seltdclear(td);
1268 				return (EWOULDBLOCK);
1269 			}
1270 			ttv = atv;
1271 			timevalsub(&ttv, &rtv);
1272 			timo = ttv.tv_sec > 24 * 60 * 60 ?
1273 			    24 * 60 * 60 * hz : tvtohz(&ttv);
1274 		}
1275 		error = seltdwait(td, timo);
1276 		seltdclear(td);
1277 		if (error)
1278 			break;
1279 	}
1280 	/* XXX Duplicates ncp/smb behavior. */
1281 	if (error == ERESTART)
1282 		error = 0;
1283 	return (error);
1284 }
1285 
1286 /*
1287  * Preallocate two selfds associated with 'cookie'.  Some fo_poll routines
1288  * have two select sets, one for read and another for write.
1289  */
1290 static void
1291 selfdalloc(struct thread *td, void *cookie)
1292 {
1293 	struct seltd *stp;
1294 
1295 	stp = td->td_sel;
1296 	if (stp->st_free1 == NULL)
1297 		stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1298 	stp->st_free1->sf_td = stp;
1299 	stp->st_free1->sf_cookie = cookie;
1300 	if (stp->st_free2 == NULL)
1301 		stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1302 	stp->st_free2->sf_td = stp;
1303 	stp->st_free2->sf_cookie = cookie;
1304 }
1305 
1306 static void
1307 selfdfree(struct seltd *stp, struct selfd *sfp)
1308 {
1309 	STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
1310 	mtx_lock(sfp->sf_mtx);
1311 	if (sfp->sf_si)
1312 		TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1313 	mtx_unlock(sfp->sf_mtx);
1314 	uma_zfree(selfd_zone, sfp);
1315 }
1316 
1317 /*
1318  * Record a select request.
1319  */
1320 void
1321 selrecord(selector, sip)
1322 	struct thread *selector;
1323 	struct selinfo *sip;
1324 {
1325 	struct selfd *sfp;
1326 	struct seltd *stp;
1327 	struct mtx *mtxp;
1328 
1329 	stp = selector->td_sel;
1330 	/*
1331 	 * Don't record when doing a rescan.
1332 	 */
1333 	if (stp->st_flags & SELTD_RESCAN)
1334 		return;
1335 	/*
1336 	 * Grab one of the preallocated descriptors.
1337 	 */
1338 	sfp = NULL;
1339 	if ((sfp = stp->st_free1) != NULL)
1340 		stp->st_free1 = NULL;
1341 	else if ((sfp = stp->st_free2) != NULL)
1342 		stp->st_free2 = NULL;
1343 	else
1344 		panic("selrecord: No free selfd on selq");
1345 	mtxp = mtx_pool_find(mtxpool_sleep, sip);
1346 	/*
1347 	 * Initialize the sfp and queue it in the thread.
1348 	 */
1349 	sfp->sf_si = sip;
1350 	sfp->sf_mtx = mtxp;
1351 	STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1352 	/*
1353 	 * Now that we've locked the sip, check for initialization.
1354 	 */
1355 	mtx_lock(mtxp);
1356 	if (sip->si_mtx == NULL) {
1357 		sip->si_mtx = mtxp;
1358 		TAILQ_INIT(&sip->si_tdlist);
1359 	}
1360 	/*
1361 	 * Add this thread to the list of selfds listening on this selinfo.
1362 	 */
1363 	TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1364 	mtx_unlock(sip->si_mtx);
1365 }
1366 
1367 /* Wake up a selecting thread. */
1368 void
1369 selwakeup(sip)
1370 	struct selinfo *sip;
1371 {
1372 	doselwakeup(sip, -1);
1373 }
1374 
1375 /* Wake up a selecting thread, and set its priority. */
1376 void
1377 selwakeuppri(sip, pri)
1378 	struct selinfo *sip;
1379 	int pri;
1380 {
1381 	doselwakeup(sip, pri);
1382 }
1383 
1384 /*
1385  * Do a wakeup when a selectable event occurs.
1386  */
1387 static void
1388 doselwakeup(sip, pri)
1389 	struct selinfo *sip;
1390 	int pri;
1391 {
1392 	struct selfd *sfp;
1393 	struct selfd *sfn;
1394 	struct seltd *stp;
1395 
1396 	/* If it's not initialized there can't be any waiters. */
1397 	if (sip->si_mtx == NULL)
1398 		return;
1399 	/*
1400 	 * Locking the selinfo locks all selfds associated with it.
1401 	 */
1402 	mtx_lock(sip->si_mtx);
1403 	TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1404 		/*
1405 		 * Once we remove this sfp from the list and clear the
1406 		 * sf_si seltdclear will know to ignore this si.
1407 		 */
1408 		TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1409 		sfp->sf_si = NULL;
1410 		stp = sfp->sf_td;
1411 		mtx_lock(&stp->st_mtx);
1412 		stp->st_flags |= SELTD_PENDING;
1413 		cv_broadcastpri(&stp->st_wait, pri);
1414 		mtx_unlock(&stp->st_mtx);
1415 	}
1416 	mtx_unlock(sip->si_mtx);
1417 }
1418 
1419 static void
1420 seltdinit(struct thread *td)
1421 {
1422 	struct seltd *stp;
1423 
1424 	if ((stp = td->td_sel) != NULL)
1425 		goto out;
1426 	td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1427 	mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1428 	cv_init(&stp->st_wait, "select");
1429 out:
1430 	stp->st_flags = 0;
1431 	STAILQ_INIT(&stp->st_selq);
1432 }
1433 
1434 static int
1435 seltdwait(struct thread *td, int timo)
1436 {
1437 	struct seltd *stp;
1438 	int error;
1439 
1440 	stp = td->td_sel;
1441 	/*
1442 	 * An event of interest may occur while we do not hold the seltd
1443 	 * locked so check the pending flag before we sleep.
1444 	 */
1445 	mtx_lock(&stp->st_mtx);
1446 	/*
1447 	 * Any further calls to selrecord will be a rescan.
1448 	 */
1449 	stp->st_flags |= SELTD_RESCAN;
1450 	if (stp->st_flags & SELTD_PENDING) {
1451 		mtx_unlock(&stp->st_mtx);
1452 		return (0);
1453 	}
1454 	if (timo > 0)
1455 		error = cv_timedwait_sig(&stp->st_wait, &stp->st_mtx, timo);
1456 	else
1457 		error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
1458 	mtx_unlock(&stp->st_mtx);
1459 
1460 	return (error);
1461 }
1462 
1463 void
1464 seltdfini(struct thread *td)
1465 {
1466 	struct seltd *stp;
1467 
1468 	stp = td->td_sel;
1469 	if (stp == NULL)
1470 		return;
1471 	if (stp->st_free1)
1472 		uma_zfree(selfd_zone, stp->st_free1);
1473 	if (stp->st_free2)
1474 		uma_zfree(selfd_zone, stp->st_free2);
1475 	td->td_sel = NULL;
1476 	free(stp, M_SELECT);
1477 }
1478 
1479 /*
1480  * Remove the references to the thread from all of the objects we were
1481  * polling.
1482  */
1483 static void
1484 seltdclear(struct thread *td)
1485 {
1486 	struct seltd *stp;
1487 	struct selfd *sfp;
1488 	struct selfd *sfn;
1489 
1490 	stp = td->td_sel;
1491 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
1492 		selfdfree(stp, sfp);
1493 	stp->st_flags = 0;
1494 }
1495 
1496 static void selectinit(void *);
1497 SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
1498 static void
1499 selectinit(void *dummy __unused)
1500 {
1501 	selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL,
1502 	    NULL, NULL, UMA_ALIGN_PTR, 0);
1503 }
1504