xref: /freebsd/sys/kern/sys_generic.c (revision 50ae6690fcef28e806f580fe7bb0b213e0e54df3)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *	@(#)sys_generic.c	8.5 (Berkeley) 1/21/94
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37677b542eSDavid E. O'Brien #include <sys/cdefs.h>
38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
39677b542eSDavid E. O'Brien 
40a9d2f8d8SRobert Watson #include "opt_capsicum.h"
412de92a38SPeter Wemm #include "opt_compat.h"
42db6a20e2SGarrett Wollman #include "opt_ktrace.h"
43db6a20e2SGarrett Wollman 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
46d2d3e875SBruce Evans #include <sys/sysproto.h>
474a144410SRobert Watson #include <sys/capsicum.h>
48df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
4920982410SBruce Evans #include <sys/filio.h>
503ac4d1efSBruce Evans #include <sys/fcntl.h>
51df8bae1dSRodney W. Grimes #include <sys/file.h>
522609222aSPawel Jakub Dawidek #include <sys/lock.h>
53df8bae1dSRodney W. Grimes #include <sys/proc.h>
54797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
55df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
56df8bae1dSRodney W. Grimes #include <sys/uio.h>
57df8bae1dSRodney W. Grimes #include <sys/kernel.h>
58e4650294SJohn Baldwin #include <sys/ktr.h>
59104a9b7eSAlexander Kabaev #include <sys/limits.h>
60df8bae1dSRodney W. Grimes #include <sys/malloc.h>
6142d11757SPeter Wemm #include <sys/poll.h>
6289b71647SPeter Wemm #include <sys/resourcevar.h>
630a2c3d48SGarrett Wollman #include <sys/selinfo.h>
6444f3b092SJohn Baldwin #include <sys/sleepqueue.h>
658f19eb88SIan Dowse #include <sys/syscallsubr.h>
668cb96f20SPeter Wemm #include <sys/sysctl.h>
6742d11757SPeter Wemm #include <sys/sysent.h>
689bbee259SAndrey A. Chernov #include <sys/vnode.h>
69279d7226SMatthew Dillon #include <sys/bio.h>
70279d7226SMatthew Dillon #include <sys/buf.h>
71265fc98fSSeigo Tanimura #include <sys/condvar.h>
72df8bae1dSRodney W. Grimes #ifdef KTRACE
73df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
74df8bae1dSRodney W. Grimes #endif
75df8bae1dSRodney W. Grimes 
76e4650294SJohn Baldwin #include <security/audit/audit.h>
77ace8398dSJeff Roberson 
78*50ae6690SHans Petter Selasky /*
79*50ae6690SHans Petter Selasky  * The following macro defines how many bytes will be allocated from
80*50ae6690SHans Petter Selasky  * the stack instead of memory allocated when passing the IOCTL data
81*50ae6690SHans Petter Selasky  * structures from userspace and to the kernel. Some IOCTLs having
82*50ae6690SHans Petter Selasky  * small data structures are used very frequently and this small
83*50ae6690SHans Petter Selasky  * buffer on the stack gives a significant speedup improvement for
84*50ae6690SHans Petter Selasky  * those requests. The value of this define should be greater or equal
85*50ae6690SHans Petter Selasky  * to 64 bytes and should also be power of two. The data structure is
86*50ae6690SHans Petter Selasky  * currently hard-aligned to a 8-byte boundary on the stack. This
87*50ae6690SHans Petter Selasky  * should currently be sufficient for all supported platforms.
88*50ae6690SHans Petter Selasky  */
89*50ae6690SHans Petter Selasky #define	SYS_IOCTL_SMALL_SIZE	128	/* bytes */
90*50ae6690SHans Petter Selasky #define	SYS_IOCTL_SMALL_ALIGN	8	/* bytes */
91*50ae6690SHans Petter Selasky 
92cd4dd444SKonstantin Belousov int iosize_max_clamp = 0;
938bb9a904SKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, iosize_max_clamp, CTLFLAG_RW,
948bb9a904SKonstantin Belousov     &iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX");
95bf3e483bSKonstantin Belousov int devfs_iosize_max_clamp = 1;
96bf3e483bSKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, devfs_iosize_max_clamp, CTLFLAG_RW,
97bf3e483bSKonstantin Belousov     &devfs_iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX for devices");
98bf3e483bSKonstantin Belousov 
998bb9a904SKonstantin Belousov /*
1008bb9a904SKonstantin Belousov  * Assert that the return value of read(2) and write(2) syscalls fits
1018bb9a904SKonstantin Belousov  * into a register.  If not, an architecture will need to provide the
1028bb9a904SKonstantin Belousov  * usermode wrappers to reconstruct the result.
1038bb9a904SKonstantin Belousov  */
1048bb9a904SKonstantin Belousov CTASSERT(sizeof(register_t) >= sizeof(size_t));
105526d0bd5SKonstantin Belousov 
106a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
107a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
108a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's");
10955166637SPoul-Henning Kamp 
1106d8feddaSKonstantin Belousov static int	pollout(struct thread *, struct pollfd *, struct pollfd *,
1116d8feddaSKonstantin Belousov 		    u_int);
112bbbb04ceSAlfred Perlstein static int	pollscan(struct thread *, struct pollfd *, u_int);
113ace8398dSJeff Roberson static int	pollrescan(struct thread *);
114bbbb04ceSAlfred Perlstein static int	selscan(struct thread *, fd_mask **, fd_mask **, int);
115ace8398dSJeff Roberson static int	selrescan(struct thread *, fd_mask **, fd_mask **);
116ace8398dSJeff Roberson static void	selfdalloc(struct thread *, void *);
117ace8398dSJeff Roberson static void	selfdfree(struct seltd *, struct selfd *);
118bcd9e0ddSJohn Baldwin static int	dofileread(struct thread *, int, struct file *, struct uio *,
119bcd9e0ddSJohn Baldwin 		    off_t, int);
120bcd9e0ddSJohn Baldwin static int	dofilewrite(struct thread *, int, struct file *, struct uio *,
121bcd9e0ddSJohn Baldwin 		    off_t, int);
122512824f8SSeigo Tanimura static void	doselwakeup(struct selinfo *, int);
123ace8398dSJeff Roberson static void	seltdinit(struct thread *);
124cf5e4fe6SDavide Italiano static int	seltdwait(struct thread *, sbintime_t, sbintime_t);
125ace8398dSJeff Roberson static void	seltdclear(struct thread *);
126ace8398dSJeff Roberson 
127ace8398dSJeff Roberson /*
128ace8398dSJeff Roberson  * One seltd per-thread allocated on demand as needed.
129ace8398dSJeff Roberson  *
130ace8398dSJeff Roberson  *	t - protected by st_mtx
131ace8398dSJeff Roberson  * 	k - Only accessed by curthread or read-only
132ace8398dSJeff Roberson  */
133ace8398dSJeff Roberson struct seltd {
134ace8398dSJeff Roberson 	STAILQ_HEAD(, selfd)	st_selq;	/* (k) List of selfds. */
135ace8398dSJeff Roberson 	struct selfd		*st_free1;	/* (k) free fd for read set. */
136ace8398dSJeff Roberson 	struct selfd		*st_free2;	/* (k) free fd for write set. */
137ace8398dSJeff Roberson 	struct mtx		st_mtx;		/* Protects struct seltd */
138ace8398dSJeff Roberson 	struct cv		st_wait;	/* (t) Wait channel. */
139ace8398dSJeff Roberson 	int			st_flags;	/* (t) SELTD_ flags. */
140ace8398dSJeff Roberson };
141ace8398dSJeff Roberson 
142ace8398dSJeff Roberson #define	SELTD_PENDING	0x0001			/* We have pending events. */
143ace8398dSJeff Roberson #define	SELTD_RESCAN	0x0002			/* Doing a rescan. */
144ace8398dSJeff Roberson 
145ace8398dSJeff Roberson /*
146ace8398dSJeff Roberson  * One selfd allocated per-thread per-file-descriptor.
147ace8398dSJeff Roberson  *	f - protected by sf_mtx
148ace8398dSJeff Roberson  */
149ace8398dSJeff Roberson struct selfd {
150ace8398dSJeff Roberson 	STAILQ_ENTRY(selfd)	sf_link;	/* (k) fds owned by this td. */
151ace8398dSJeff Roberson 	TAILQ_ENTRY(selfd)	sf_threads;	/* (f) fds on this selinfo. */
152ace8398dSJeff Roberson 	struct selinfo		*sf_si;		/* (f) selinfo when linked. */
153ace8398dSJeff Roberson 	struct mtx		*sf_mtx;	/* Pointer to selinfo mtx. */
154ace8398dSJeff Roberson 	struct seltd		*sf_td;		/* (k) owning seltd. */
155ace8398dSJeff Roberson 	void			*sf_cookie;	/* (k) fd or pollfd. */
156ace8398dSJeff Roberson };
157ace8398dSJeff Roberson 
158ace8398dSJeff Roberson static uma_zone_t selfd_zone;
1592141453eSJeff Roberson static struct mtx_pool *mtxpool_select;
1608fe387abSDmitrij Tejblum 
161d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
162df8bae1dSRodney W. Grimes struct read_args {
163df8bae1dSRodney W. Grimes 	int	fd;
164134e06feSBruce Evans 	void	*buf;
165134e06feSBruce Evans 	size_t	nbyte;
166df8bae1dSRodney W. Grimes };
167d2d3e875SBruce Evans #endif
16826f9a767SRodney W. Grimes int
1698451d0ddSKip Macy sys_read(td, uap)
170b40ce416SJulian Elischer 	struct thread *td;
171b064d43dSMatthew Dillon 	struct read_args *uap;
172df8bae1dSRodney W. Grimes {
173bcd9e0ddSJohn Baldwin 	struct uio auio;
174bcd9e0ddSJohn Baldwin 	struct iovec aiov;
175279d7226SMatthew Dillon 	int error;
176df8bae1dSRodney W. Grimes 
177526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
178bcd9e0ddSJohn Baldwin 		return (EINVAL);
179bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
180bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
181bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
182bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
183bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
184bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
185bcd9e0ddSJohn Baldwin 	error = kern_readv(td, uap->fd, &auio);
186279d7226SMatthew Dillon 	return(error);
187df8bae1dSRodney W. Grimes }
188df8bae1dSRodney W. Grimes 
189df8bae1dSRodney W. Grimes /*
190bcd9e0ddSJohn Baldwin  * Positioned read system call
1914160ccd9SAlan Cox  */
1924160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
1934160ccd9SAlan Cox struct pread_args {
1944160ccd9SAlan Cox 	int	fd;
1954160ccd9SAlan Cox 	void	*buf;
1964160ccd9SAlan Cox 	size_t	nbyte;
1978fe387abSDmitrij Tejblum 	int	pad;
1984160ccd9SAlan Cox 	off_t	offset;
1994160ccd9SAlan Cox };
2004160ccd9SAlan Cox #endif
2014160ccd9SAlan Cox int
2028451d0ddSKip Macy sys_pread(td, uap)
203b40ce416SJulian Elischer 	struct thread *td;
204b064d43dSMatthew Dillon 	struct pread_args *uap;
2054160ccd9SAlan Cox {
2064160ccd9SAlan Cox 	struct uio auio;
2074160ccd9SAlan Cox 	struct iovec aiov;
208bcd9e0ddSJohn Baldwin 	int error;
2094160ccd9SAlan Cox 
210526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
211bcd9e0ddSJohn Baldwin 		return (EINVAL);
212bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
213bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
2144160ccd9SAlan Cox 	auio.uio_iov = &aiov;
2154160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
216bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
2174160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
218bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, &auio, uap->offset);
2194160ccd9SAlan Cox 	return(error);
2204160ccd9SAlan Cox }
2214160ccd9SAlan Cox 
222c2815ad5SPeter Wemm int
223c2815ad5SPeter Wemm freebsd6_pread(td, uap)
224c2815ad5SPeter Wemm 	struct thread *td;
225c2815ad5SPeter Wemm 	struct freebsd6_pread_args *uap;
226c2815ad5SPeter Wemm {
227c2815ad5SPeter Wemm 	struct pread_args oargs;
228c2815ad5SPeter Wemm 
229c2815ad5SPeter Wemm 	oargs.fd = uap->fd;
230c2815ad5SPeter Wemm 	oargs.buf = uap->buf;
231c2815ad5SPeter Wemm 	oargs.nbyte = uap->nbyte;
232c2815ad5SPeter Wemm 	oargs.offset = uap->offset;
2338451d0ddSKip Macy 	return (sys_pread(td, &oargs));
234c2815ad5SPeter Wemm }
235c2815ad5SPeter Wemm 
2364160ccd9SAlan Cox /*
237df8bae1dSRodney W. Grimes  * Scatter read system call.
238df8bae1dSRodney W. Grimes  */
239d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
240df8bae1dSRodney W. Grimes struct readv_args {
2417147b19dSBruce Evans 	int	fd;
242df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
243df8bae1dSRodney W. Grimes 	u_int	iovcnt;
244df8bae1dSRodney W. Grimes };
245d2d3e875SBruce Evans #endif
24626f9a767SRodney W. Grimes int
2478451d0ddSKip Macy sys_readv(struct thread *td, struct readv_args *uap)
248df8bae1dSRodney W. Grimes {
249b88ec951SJohn Baldwin 	struct uio *auio;
250b88ec951SJohn Baldwin 	int error;
251b88ec951SJohn Baldwin 
252b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
253b88ec951SJohn Baldwin 	if (error)
254b88ec951SJohn Baldwin 		return (error);
255b88ec951SJohn Baldwin 	error = kern_readv(td, uap->fd, auio);
256b88ec951SJohn Baldwin 	free(auio, M_IOV);
257b88ec951SJohn Baldwin 	return (error);
258b88ec951SJohn Baldwin }
259b88ec951SJohn Baldwin 
260b88ec951SJohn Baldwin int
261b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio)
262b88ec951SJohn Baldwin {
263b064d43dSMatthew Dillon 	struct file *fp;
2647008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
265bcd9e0ddSJohn Baldwin 	int error;
266bcd9e0ddSJohn Baldwin 
2677008be5bSPawel Jakub Dawidek 	error = fget_read(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
268bcd9e0ddSJohn Baldwin 	if (error)
269bcd9e0ddSJohn Baldwin 		return (error);
270bcd9e0ddSJohn Baldwin 	error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
271bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
272bcd9e0ddSJohn Baldwin 	return (error);
273bcd9e0ddSJohn Baldwin }
274bcd9e0ddSJohn Baldwin 
275bcd9e0ddSJohn Baldwin /*
276bcd9e0ddSJohn Baldwin  * Scatter positioned read system call.
277bcd9e0ddSJohn Baldwin  */
278bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
279bcd9e0ddSJohn Baldwin struct preadv_args {
280bcd9e0ddSJohn Baldwin 	int	fd;
281bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
282bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
283bcd9e0ddSJohn Baldwin 	off_t	offset;
284bcd9e0ddSJohn Baldwin };
285bcd9e0ddSJohn Baldwin #endif
286bcd9e0ddSJohn Baldwin int
2878451d0ddSKip Macy sys_preadv(struct thread *td, struct preadv_args *uap)
288bcd9e0ddSJohn Baldwin {
289bcd9e0ddSJohn Baldwin 	struct uio *auio;
290bcd9e0ddSJohn Baldwin 	int error;
291bcd9e0ddSJohn Baldwin 
292bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
293bcd9e0ddSJohn Baldwin 	if (error)
294bcd9e0ddSJohn Baldwin 		return (error);
295bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, auio, uap->offset);
296bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
297bcd9e0ddSJohn Baldwin 	return (error);
298bcd9e0ddSJohn Baldwin }
299bcd9e0ddSJohn Baldwin 
300bcd9e0ddSJohn Baldwin int
301bcd9e0ddSJohn Baldwin kern_preadv(td, fd, auio, offset)
302bcd9e0ddSJohn Baldwin 	struct thread *td;
303bcd9e0ddSJohn Baldwin 	int fd;
304bcd9e0ddSJohn Baldwin 	struct uio *auio;
305bcd9e0ddSJohn Baldwin 	off_t offset;
306bcd9e0ddSJohn Baldwin {
307bcd9e0ddSJohn Baldwin 	struct file *fp;
3087008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
309bcd9e0ddSJohn Baldwin 	int error;
310bcd9e0ddSJohn Baldwin 
3117008be5bSPawel Jakub Dawidek 	error = fget_read(td, fd, cap_rights_init(&rights, CAP_PREAD), &fp);
312bcd9e0ddSJohn Baldwin 	if (error)
313bcd9e0ddSJohn Baldwin 		return (error);
314bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
315bcd9e0ddSJohn Baldwin 		error = ESPIPE;
316bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
317bcd9e0ddSJohn Baldwin 		error = EINVAL;
318bcd9e0ddSJohn Baldwin 	else
319bcd9e0ddSJohn Baldwin 		error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
320bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
321bcd9e0ddSJohn Baldwin 	return (error);
322bcd9e0ddSJohn Baldwin }
323bcd9e0ddSJohn Baldwin 
324bcd9e0ddSJohn Baldwin /*
325bcd9e0ddSJohn Baldwin  * Common code for readv and preadv that reads data in
326bcd9e0ddSJohn Baldwin  * from a file using the passed in uio, offset, and flags.
327bcd9e0ddSJohn Baldwin  */
328bcd9e0ddSJohn Baldwin static int
329bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags)
330bcd9e0ddSJohn Baldwin 	struct thread *td;
331bcd9e0ddSJohn Baldwin 	int fd;
332bcd9e0ddSJohn Baldwin 	struct file *fp;
333bcd9e0ddSJohn Baldwin 	struct uio *auio;
334bcd9e0ddSJohn Baldwin 	off_t offset;
335bcd9e0ddSJohn Baldwin 	int flags;
336bcd9e0ddSJohn Baldwin {
337bcd9e0ddSJohn Baldwin 	ssize_t cnt;
33882641acdSAlan Cox 	int error;
339df8bae1dSRodney W. Grimes #ifdef KTRACE
340552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
341df8bae1dSRodney W. Grimes #endif
342df8bae1dSRodney W. Grimes 
3434f8d23d6SPoul-Henning Kamp 	/* Finish zero length reads right here */
3444f8d23d6SPoul-Henning Kamp 	if (auio->uio_resid == 0) {
3454f8d23d6SPoul-Henning Kamp 		td->td_retval[0] = 0;
3464f8d23d6SPoul-Henning Kamp 		return(0);
3474f8d23d6SPoul-Henning Kamp 	}
348552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_READ;
349bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
350552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
351df8bae1dSRodney W. Grimes #ifdef KTRACE
352552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
353552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
354df8bae1dSRodney W. Grimes #endif
355552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
356bcd9e0ddSJohn Baldwin 	if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
357552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
358df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
359df8bae1dSRodney W. Grimes 			error = 0;
360279d7226SMatthew Dillon 	}
361552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
362df8bae1dSRodney W. Grimes #ifdef KTRACE
363552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
364552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
365b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_READ, ktruio, error);
366df8bae1dSRodney W. Grimes 	}
367df8bae1dSRodney W. Grimes #endif
368b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
369df8bae1dSRodney W. Grimes 	return (error);
370df8bae1dSRodney W. Grimes }
371df8bae1dSRodney W. Grimes 
372d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
373df8bae1dSRodney W. Grimes struct write_args {
374df8bae1dSRodney W. Grimes 	int	fd;
375134e06feSBruce Evans 	const void *buf;
376134e06feSBruce Evans 	size_t	nbyte;
377df8bae1dSRodney W. Grimes };
378d2d3e875SBruce Evans #endif
37926f9a767SRodney W. Grimes int
3808451d0ddSKip Macy sys_write(td, uap)
381b40ce416SJulian Elischer 	struct thread *td;
382b064d43dSMatthew Dillon 	struct write_args *uap;
383df8bae1dSRodney W. Grimes {
384bcd9e0ddSJohn Baldwin 	struct uio auio;
385bcd9e0ddSJohn Baldwin 	struct iovec aiov;
386279d7226SMatthew Dillon 	int error;
387df8bae1dSRodney W. Grimes 
388526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
389bcd9e0ddSJohn Baldwin 		return (EINVAL);
390bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
391bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
392bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
393bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
394bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
395bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
396bcd9e0ddSJohn Baldwin 	error = kern_writev(td, uap->fd, &auio);
397279d7226SMatthew Dillon 	return(error);
398df8bae1dSRodney W. Grimes }
399df8bae1dSRodney W. Grimes 
400df8bae1dSRodney W. Grimes /*
4010c14ff0eSRobert Watson  * Positioned write system call.
4024160ccd9SAlan Cox  */
4034160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
4044160ccd9SAlan Cox struct pwrite_args {
4054160ccd9SAlan Cox 	int	fd;
4064160ccd9SAlan Cox 	const void *buf;
4074160ccd9SAlan Cox 	size_t	nbyte;
4088fe387abSDmitrij Tejblum 	int	pad;
4094160ccd9SAlan Cox 	off_t	offset;
4104160ccd9SAlan Cox };
4114160ccd9SAlan Cox #endif
4124160ccd9SAlan Cox int
4138451d0ddSKip Macy sys_pwrite(td, uap)
414b40ce416SJulian Elischer 	struct thread *td;
415b064d43dSMatthew Dillon 	struct pwrite_args *uap;
4164160ccd9SAlan Cox {
4174160ccd9SAlan Cox 	struct uio auio;
4184160ccd9SAlan Cox 	struct iovec aiov;
419bcd9e0ddSJohn Baldwin 	int error;
4204160ccd9SAlan Cox 
421526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
422bcd9e0ddSJohn Baldwin 		return (EINVAL);
423bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
424bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
4254160ccd9SAlan Cox 	auio.uio_iov = &aiov;
4264160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
427bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
4284160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
429bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, &auio, uap->offset);
4304160ccd9SAlan Cox 	return(error);
4314160ccd9SAlan Cox }
4324160ccd9SAlan Cox 
433c2815ad5SPeter Wemm int
434c2815ad5SPeter Wemm freebsd6_pwrite(td, uap)
435c2815ad5SPeter Wemm 	struct thread *td;
436c2815ad5SPeter Wemm 	struct freebsd6_pwrite_args *uap;
437c2815ad5SPeter Wemm {
438c2815ad5SPeter Wemm 	struct pwrite_args oargs;
439c2815ad5SPeter Wemm 
440c2815ad5SPeter Wemm 	oargs.fd = uap->fd;
441c2815ad5SPeter Wemm 	oargs.buf = uap->buf;
442c2815ad5SPeter Wemm 	oargs.nbyte = uap->nbyte;
443c2815ad5SPeter Wemm 	oargs.offset = uap->offset;
4448451d0ddSKip Macy 	return (sys_pwrite(td, &oargs));
445c2815ad5SPeter Wemm }
446c2815ad5SPeter Wemm 
4474160ccd9SAlan Cox /*
4480c14ff0eSRobert Watson  * Gather write system call.
449df8bae1dSRodney W. Grimes  */
450d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
451df8bae1dSRodney W. Grimes struct writev_args {
452df8bae1dSRodney W. Grimes 	int	fd;
453df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
454df8bae1dSRodney W. Grimes 	u_int	iovcnt;
455df8bae1dSRodney W. Grimes };
456d2d3e875SBruce Evans #endif
45726f9a767SRodney W. Grimes int
4588451d0ddSKip Macy sys_writev(struct thread *td, struct writev_args *uap)
459df8bae1dSRodney W. Grimes {
460b88ec951SJohn Baldwin 	struct uio *auio;
461b88ec951SJohn Baldwin 	int error;
462b88ec951SJohn Baldwin 
463b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
464b88ec951SJohn Baldwin 	if (error)
465b88ec951SJohn Baldwin 		return (error);
466b88ec951SJohn Baldwin 	error = kern_writev(td, uap->fd, auio);
467b88ec951SJohn Baldwin 	free(auio, M_IOV);
468b88ec951SJohn Baldwin 	return (error);
469b88ec951SJohn Baldwin }
470b88ec951SJohn Baldwin 
471b88ec951SJohn Baldwin int
472b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio)
473b88ec951SJohn Baldwin {
474b064d43dSMatthew Dillon 	struct file *fp;
4757008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
476bcd9e0ddSJohn Baldwin 	int error;
477bcd9e0ddSJohn Baldwin 
4787008be5bSPawel Jakub Dawidek 	error = fget_write(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
479bcd9e0ddSJohn Baldwin 	if (error)
480af56abaaSJohn Baldwin 		return (error);
481bcd9e0ddSJohn Baldwin 	error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
482bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
483bcd9e0ddSJohn Baldwin 	return (error);
484bcd9e0ddSJohn Baldwin }
485bcd9e0ddSJohn Baldwin 
486bcd9e0ddSJohn Baldwin /*
4870c14ff0eSRobert Watson  * Gather positioned write system call.
488bcd9e0ddSJohn Baldwin  */
489bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
490bcd9e0ddSJohn Baldwin struct pwritev_args {
491bcd9e0ddSJohn Baldwin 	int	fd;
492bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
493bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
494bcd9e0ddSJohn Baldwin 	off_t	offset;
495bcd9e0ddSJohn Baldwin };
496bcd9e0ddSJohn Baldwin #endif
497bcd9e0ddSJohn Baldwin int
4988451d0ddSKip Macy sys_pwritev(struct thread *td, struct pwritev_args *uap)
499bcd9e0ddSJohn Baldwin {
500bcd9e0ddSJohn Baldwin 	struct uio *auio;
501bcd9e0ddSJohn Baldwin 	int error;
502bcd9e0ddSJohn Baldwin 
503bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
504bcd9e0ddSJohn Baldwin 	if (error)
505bcd9e0ddSJohn Baldwin 		return (error);
506bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, auio, uap->offset);
507bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
508bcd9e0ddSJohn Baldwin 	return (error);
509bcd9e0ddSJohn Baldwin }
510bcd9e0ddSJohn Baldwin 
511bcd9e0ddSJohn Baldwin int
512bcd9e0ddSJohn Baldwin kern_pwritev(td, fd, auio, offset)
513bcd9e0ddSJohn Baldwin 	struct thread *td;
514bcd9e0ddSJohn Baldwin 	struct uio *auio;
515bcd9e0ddSJohn Baldwin 	int fd;
516bcd9e0ddSJohn Baldwin 	off_t offset;
517bcd9e0ddSJohn Baldwin {
518bcd9e0ddSJohn Baldwin 	struct file *fp;
5197008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
520bcd9e0ddSJohn Baldwin 	int error;
521bcd9e0ddSJohn Baldwin 
5227008be5bSPawel Jakub Dawidek 	error = fget_write(td, fd, cap_rights_init(&rights, CAP_PWRITE), &fp);
523bcd9e0ddSJohn Baldwin 	if (error)
524af56abaaSJohn Baldwin 		return (error);
525bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
526bcd9e0ddSJohn Baldwin 		error = ESPIPE;
527bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
528bcd9e0ddSJohn Baldwin 		error = EINVAL;
529bcd9e0ddSJohn Baldwin 	else
530bcd9e0ddSJohn Baldwin 		error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
531bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
532bcd9e0ddSJohn Baldwin 	return (error);
533bcd9e0ddSJohn Baldwin }
534bcd9e0ddSJohn Baldwin 
535bcd9e0ddSJohn Baldwin /*
536bcd9e0ddSJohn Baldwin  * Common code for writev and pwritev that writes data to
537bcd9e0ddSJohn Baldwin  * a file using the passed in uio, offset, and flags.
538bcd9e0ddSJohn Baldwin  */
539bcd9e0ddSJohn Baldwin static int
540bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags)
541bcd9e0ddSJohn Baldwin 	struct thread *td;
542bcd9e0ddSJohn Baldwin 	int fd;
543bcd9e0ddSJohn Baldwin 	struct file *fp;
544bcd9e0ddSJohn Baldwin 	struct uio *auio;
545bcd9e0ddSJohn Baldwin 	off_t offset;
546bcd9e0ddSJohn Baldwin 	int flags;
547bcd9e0ddSJohn Baldwin {
548bcd9e0ddSJohn Baldwin 	ssize_t cnt;
549552afd9cSPoul-Henning Kamp 	int error;
550df8bae1dSRodney W. Grimes #ifdef KTRACE
551552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
552df8bae1dSRodney W. Grimes #endif
553df8bae1dSRodney W. Grimes 
554552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_WRITE;
555552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
556bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
557df8bae1dSRodney W. Grimes #ifdef KTRACE
558552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
559552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
560df8bae1dSRodney W. Grimes #endif
561552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
562ad9789f6SKonstantin Belousov 	if (fp->f_type == DTYPE_VNODE &&
563ad9789f6SKonstantin Belousov 	    (fp->f_vnread_flags & FDEVFS_VNODE) == 0)
5649440653dSMatthew Dillon 		bwillwrite();
565bcd9e0ddSJohn Baldwin 	if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
566552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
567df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
568df8bae1dSRodney W. Grimes 			error = 0;
569bcd9e0ddSJohn Baldwin 		/* Socket layer is responsible for issuing SIGPIPE. */
5706f7ca813SBruce M Simpson 		if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
571b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
5727a6f3d78SJohn Baldwin 			tdsignal(td, SIGPIPE);
573b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
57419eb87d2SJohn Baldwin 		}
575df8bae1dSRodney W. Grimes 	}
576552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
577df8bae1dSRodney W. Grimes #ifdef KTRACE
578552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
579552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
580b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_WRITE, ktruio, error);
581df8bae1dSRodney W. Grimes 	}
582df8bae1dSRodney W. Grimes #endif
583b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
584df8bae1dSRodney W. Grimes 	return (error);
585df8bae1dSRodney W. Grimes }
586df8bae1dSRodney W. Grimes 
587e4650294SJohn Baldwin /*
588e4650294SJohn Baldwin  * Truncate a file given a file descriptor.
589e4650294SJohn Baldwin  *
590e4650294SJohn Baldwin  * Can't use fget_write() here, since must return EINVAL and not EBADF if the
591e4650294SJohn Baldwin  * descriptor isn't writable.
592e4650294SJohn Baldwin  */
593e4650294SJohn Baldwin int
594e4650294SJohn Baldwin kern_ftruncate(td, fd, length)
595e4650294SJohn Baldwin 	struct thread *td;
596e4650294SJohn Baldwin 	int fd;
597e4650294SJohn Baldwin 	off_t length;
598e4650294SJohn Baldwin {
599e4650294SJohn Baldwin 	struct file *fp;
6007008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
601e4650294SJohn Baldwin 	int error;
602e4650294SJohn Baldwin 
60314961ba7SRobert Watson 	AUDIT_ARG_FD(fd);
604e4650294SJohn Baldwin 	if (length < 0)
605e4650294SJohn Baldwin 		return (EINVAL);
6067008be5bSPawel Jakub Dawidek 	error = fget(td, fd, cap_rights_init(&rights, CAP_FTRUNCATE), &fp);
607e4650294SJohn Baldwin 	if (error)
608e4650294SJohn Baldwin 		return (error);
60914961ba7SRobert Watson 	AUDIT_ARG_FILE(td->td_proc, fp);
610e4650294SJohn Baldwin 	if (!(fp->f_flag & FWRITE)) {
611e4650294SJohn Baldwin 		fdrop(fp, td);
612e4650294SJohn Baldwin 		return (EINVAL);
613e4650294SJohn Baldwin 	}
614e4650294SJohn Baldwin 	error = fo_truncate(fp, length, td->td_ucred, td);
615e4650294SJohn Baldwin 	fdrop(fp, td);
616e4650294SJohn Baldwin 	return (error);
617e4650294SJohn Baldwin }
618e4650294SJohn Baldwin 
619e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
620e4650294SJohn Baldwin struct ftruncate_args {
621e4650294SJohn Baldwin 	int	fd;
622e4650294SJohn Baldwin 	int	pad;
623e4650294SJohn Baldwin 	off_t	length;
624e4650294SJohn Baldwin };
625e4650294SJohn Baldwin #endif
626e4650294SJohn Baldwin int
6278451d0ddSKip Macy sys_ftruncate(td, uap)
628e4650294SJohn Baldwin 	struct thread *td;
629e4650294SJohn Baldwin 	struct ftruncate_args *uap;
630e4650294SJohn Baldwin {
631e4650294SJohn Baldwin 
632e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
633e4650294SJohn Baldwin }
634e4650294SJohn Baldwin 
635e4650294SJohn Baldwin #if defined(COMPAT_43)
636e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
637e4650294SJohn Baldwin struct oftruncate_args {
638e4650294SJohn Baldwin 	int	fd;
639e4650294SJohn Baldwin 	long	length;
640e4650294SJohn Baldwin };
641e4650294SJohn Baldwin #endif
642e4650294SJohn Baldwin int
643e4650294SJohn Baldwin oftruncate(td, uap)
644e4650294SJohn Baldwin 	struct thread *td;
645e4650294SJohn Baldwin 	struct oftruncate_args *uap;
646e4650294SJohn Baldwin {
647e4650294SJohn Baldwin 
648e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
649e4650294SJohn Baldwin }
650e4650294SJohn Baldwin #endif /* COMPAT_43 */
651e4650294SJohn Baldwin 
652d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
653df8bae1dSRodney W. Grimes struct ioctl_args {
654df8bae1dSRodney W. Grimes 	int	fd;
655069e9bc1SDoug Rabson 	u_long	com;
656df8bae1dSRodney W. Grimes 	caddr_t	data;
657df8bae1dSRodney W. Grimes };
658d2d3e875SBruce Evans #endif
659df8bae1dSRodney W. Grimes /* ARGSUSED */
66026f9a767SRodney W. Grimes int
6618451d0ddSKip Macy sys_ioctl(struct thread *td, struct ioctl_args *uap)
662df8bae1dSRodney W. Grimes {
663*50ae6690SHans Petter Selasky 	u_char smalldata[SYS_IOCTL_SMALL_SIZE] __aligned(SYS_IOCTL_SMALL_ALIGN);
6643e15c66fSPoul-Henning Kamp 	u_long com;
6659fddcc66SRuslan Ermilov 	int arg, error;
6663e15c66fSPoul-Henning Kamp 	u_int size;
6679fddcc66SRuslan Ermilov 	caddr_t data;
668df8bae1dSRodney W. Grimes 
6699fc6aa06SPoul-Henning Kamp 	if (uap->com > 0xffffffff) {
6709fc6aa06SPoul-Henning Kamp 		printf(
6719fc6aa06SPoul-Henning Kamp 		    "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
672431f8906SJulian Elischer 		    td->td_proc->p_pid, td->td_name, uap->com);
6739fc6aa06SPoul-Henning Kamp 		uap->com &= 0xffffffff;
6749fc6aa06SPoul-Henning Kamp 	}
675d9f46233SJohn Baldwin 	com = uap->com;
676df8bae1dSRodney W. Grimes 
677df8bae1dSRodney W. Grimes 	/*
678df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
679df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
680df8bae1dSRodney W. Grimes 	 */
681df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
682ca51b19bSPoul-Henning Kamp 	if ((size > IOCPARM_MAX) ||
683ca51b19bSPoul-Henning Kamp 	    ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
6842de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
6852de92a38SPeter Wemm 	    ((com & IOC_OUT) && size == 0) ||
6862de92a38SPeter Wemm #else
6872de92a38SPeter Wemm 	    ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
6882de92a38SPeter Wemm #endif
6899fddcc66SRuslan Ermilov 	    ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
690426da3bcSAlfred Perlstein 		return (ENOTTY);
691279d7226SMatthew Dillon 
692ca51b19bSPoul-Henning Kamp 	if (size > 0) {
693715457f6SDavid E. O'Brien 		if (com & IOC_VOID) {
6949fddcc66SRuslan Ermilov 			/* Integer argument. */
6959fddcc66SRuslan Ermilov 			arg = (intptr_t)uap->data;
6969fddcc66SRuslan Ermilov 			data = (void *)&arg;
6979fddcc66SRuslan Ermilov 			size = 0;
6985cbf44bfSMateusz Guzik 		} else {
6990ecd606bSHans Petter Selasky 			if (size > SYS_IOCTL_SMALL_SIZE)
700715457f6SDavid E. O'Brien 				data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
7010ecd606bSHans Petter Selasky 			else
7020ecd606bSHans Petter Selasky 				data = smalldata;
7035cbf44bfSMateusz Guzik 		}
7049fddcc66SRuslan Ermilov 	} else
7059fddcc66SRuslan Ermilov 		data = (void *)&uap->data;
706df8bae1dSRodney W. Grimes 	if (com & IOC_IN) {
707df8bae1dSRodney W. Grimes 		error = copyin(uap->data, data, (u_int)size);
7085cbf44bfSMateusz Guzik 		if (error != 0)
7095cbf44bfSMateusz Guzik 			goto out;
710ca51b19bSPoul-Henning Kamp 	} else if (com & IOC_OUT) {
711df8bae1dSRodney W. Grimes 		/*
712df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
713df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
714df8bae1dSRodney W. Grimes 		 */
715df8bae1dSRodney W. Grimes 		bzero(data, size);
716279d7226SMatthew Dillon 	}
717df8bae1dSRodney W. Grimes 
718d9f46233SJohn Baldwin 	error = kern_ioctl(td, uap->fd, com, data);
719d9f46233SJohn Baldwin 
720d9f46233SJohn Baldwin 	if (error == 0 && (com & IOC_OUT))
721d9f46233SJohn Baldwin 		error = copyout(data, uap->data, (u_int)size);
722d9f46233SJohn Baldwin 
7235cbf44bfSMateusz Guzik out:
7240ecd606bSHans Petter Selasky 	if (size > SYS_IOCTL_SMALL_SIZE)
7259fddcc66SRuslan Ermilov 		free(data, M_IOCTLOPS);
726d9f46233SJohn Baldwin 	return (error);
727d9f46233SJohn Baldwin }
728d9f46233SJohn Baldwin 
729d9f46233SJohn Baldwin int
730d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
731d9f46233SJohn Baldwin {
732d9f46233SJohn Baldwin 	struct file *fp;
733d9f46233SJohn Baldwin 	struct filedesc *fdp;
7347008be5bSPawel Jakub Dawidek #ifndef CAPABILITIES
7357008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
7367008be5bSPawel Jakub Dawidek #endif
7372609222aSPawel Jakub Dawidek 	int error, tmp, locked;
738d9f46233SJohn Baldwin 
73964c9a4d9SRobert Watson 	AUDIT_ARG_FD(fd);
74064c9a4d9SRobert Watson 	AUDIT_ARG_CMD(com);
7412609222aSPawel Jakub Dawidek 
742d9f46233SJohn Baldwin 	fdp = td->td_proc->p_fd;
7432609222aSPawel Jakub Dawidek 
744d9f46233SJohn Baldwin 	switch (com) {
745d9f46233SJohn Baldwin 	case FIONCLEX:
746d9f46233SJohn Baldwin 	case FIOCLEX:
7475e3f7694SRobert Watson 		FILEDESC_XLOCK(fdp);
7482609222aSPawel Jakub Dawidek 		locked = LA_XLOCKED;
7492609222aSPawel Jakub Dawidek 		break;
7502609222aSPawel Jakub Dawidek 	default:
7512609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
7522609222aSPawel Jakub Dawidek 		FILEDESC_SLOCK(fdp);
7532609222aSPawel Jakub Dawidek 		locked = LA_SLOCKED;
7542609222aSPawel Jakub Dawidek #else
7552609222aSPawel Jakub Dawidek 		locked = LA_UNLOCKED;
7562609222aSPawel Jakub Dawidek #endif
7572609222aSPawel Jakub Dawidek 		break;
7582609222aSPawel Jakub Dawidek 	}
7592609222aSPawel Jakub Dawidek 
7602609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
7612609222aSPawel Jakub Dawidek 	if ((fp = fget_locked(fdp, fd)) == NULL) {
7622609222aSPawel Jakub Dawidek 		error = EBADF;
7632609222aSPawel Jakub Dawidek 		goto out;
7642609222aSPawel Jakub Dawidek 	}
7652609222aSPawel Jakub Dawidek 	if ((error = cap_ioctl_check(fdp, fd, com)) != 0) {
7662609222aSPawel Jakub Dawidek 		fp = NULL;	/* fhold() was not called yet */
7672609222aSPawel Jakub Dawidek 		goto out;
7682609222aSPawel Jakub Dawidek 	}
7692609222aSPawel Jakub Dawidek 	fhold(fp);
7702609222aSPawel Jakub Dawidek 	if (locked == LA_SLOCKED) {
7712609222aSPawel Jakub Dawidek 		FILEDESC_SUNLOCK(fdp);
7722609222aSPawel Jakub Dawidek 		locked = LA_UNLOCKED;
7732609222aSPawel Jakub Dawidek 	}
7742609222aSPawel Jakub Dawidek #else
7757008be5bSPawel Jakub Dawidek 	error = fget(td, fd, cap_rights_init(&rights, CAP_IOCTL), &fp);
7767008be5bSPawel Jakub Dawidek 	if (error != 0) {
7772609222aSPawel Jakub Dawidek 		fp = NULL;
7782609222aSPawel Jakub Dawidek 		goto out;
7792609222aSPawel Jakub Dawidek 	}
7802609222aSPawel Jakub Dawidek #endif
7812609222aSPawel Jakub Dawidek 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
7822609222aSPawel Jakub Dawidek 		error = EBADF;
7832609222aSPawel Jakub Dawidek 		goto out;
7842609222aSPawel Jakub Dawidek 	}
7852609222aSPawel Jakub Dawidek 
7862609222aSPawel Jakub Dawidek 	switch (com) {
7872609222aSPawel Jakub Dawidek 	case FIONCLEX:
7882609222aSPawel Jakub Dawidek 		fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE;
7892609222aSPawel Jakub Dawidek 		goto out;
7902609222aSPawel Jakub Dawidek 	case FIOCLEX:
7912609222aSPawel Jakub Dawidek 		fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE;
792d9f46233SJohn Baldwin 		goto out;
793d9f46233SJohn Baldwin 	case FIONBIO:
794bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
795397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FNONBLOCK);
796df8bae1dSRodney W. Grimes 		else
797397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FNONBLOCK);
7988ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
799d9f46233SJohn Baldwin 		break;
800d9f46233SJohn Baldwin 	case FIOASYNC:
801bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
802397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FASYNC);
803df8bae1dSRodney W. Grimes 		else
804397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FASYNC);
8058ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
806d9f46233SJohn Baldwin 		break;
807df8bae1dSRodney W. Grimes 	}
8088ccf264fSPoul-Henning Kamp 
8098ccf264fSPoul-Henning Kamp 	error = fo_ioctl(fp, com, data, td->td_ucred, td);
810d9f46233SJohn Baldwin out:
8112609222aSPawel Jakub Dawidek 	switch (locked) {
8122609222aSPawel Jakub Dawidek 	case LA_XLOCKED:
8132609222aSPawel Jakub Dawidek 		FILEDESC_XUNLOCK(fdp);
8142609222aSPawel Jakub Dawidek 		break;
8152609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
8162609222aSPawel Jakub Dawidek 	case LA_SLOCKED:
8172609222aSPawel Jakub Dawidek 		FILEDESC_SUNLOCK(fdp);
8182609222aSPawel Jakub Dawidek 		break;
8192609222aSPawel Jakub Dawidek #endif
8202609222aSPawel Jakub Dawidek 	default:
8212609222aSPawel Jakub Dawidek 		FILEDESC_UNLOCK_ASSERT(fdp);
8222609222aSPawel Jakub Dawidek 		break;
8232609222aSPawel Jakub Dawidek 	}
8242609222aSPawel Jakub Dawidek 	if (fp != NULL)
825b40ce416SJulian Elischer 		fdrop(fp, td);
826df8bae1dSRodney W. Grimes 	return (error);
827df8bae1dSRodney W. Grimes }
828df8bae1dSRodney W. Grimes 
829125dcf8cSKonstantin Belousov int
830125dcf8cSKonstantin Belousov poll_no_poll(int events)
831125dcf8cSKonstantin Belousov {
832125dcf8cSKonstantin Belousov 	/*
833125dcf8cSKonstantin Belousov 	 * Return true for read/write.  If the user asked for something
834125dcf8cSKonstantin Belousov 	 * special, return POLLNVAL, so that clients have a way of
835125dcf8cSKonstantin Belousov 	 * determining reliably whether or not the extended
836125dcf8cSKonstantin Belousov 	 * functionality is present without hard-coding knowledge
837125dcf8cSKonstantin Belousov 	 * of specific filesystem implementations.
838125dcf8cSKonstantin Belousov 	 */
839125dcf8cSKonstantin Belousov 	if (events & ~POLLSTANDARD)
840125dcf8cSKonstantin Belousov 		return (POLLNVAL);
841125dcf8cSKonstantin Belousov 
842125dcf8cSKonstantin Belousov 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
843125dcf8cSKonstantin Belousov }
844125dcf8cSKonstantin Belousov 
845066d836bSKonstantin Belousov int
8468451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap)
847066d836bSKonstantin Belousov {
848066d836bSKonstantin Belousov 	struct timespec ts;
849066d836bSKonstantin Belousov 	struct timeval tv, *tvp;
850066d836bSKonstantin Belousov 	sigset_t set, *uset;
851066d836bSKonstantin Belousov 	int error;
852066d836bSKonstantin Belousov 
853066d836bSKonstantin Belousov 	if (uap->ts != NULL) {
854066d836bSKonstantin Belousov 		error = copyin(uap->ts, &ts, sizeof(ts));
855066d836bSKonstantin Belousov 		if (error != 0)
856066d836bSKonstantin Belousov 		    return (error);
857066d836bSKonstantin Belousov 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
858066d836bSKonstantin Belousov 		tvp = &tv;
859066d836bSKonstantin Belousov 	} else
860066d836bSKonstantin Belousov 		tvp = NULL;
861066d836bSKonstantin Belousov 	if (uap->sm != NULL) {
862066d836bSKonstantin Belousov 		error = copyin(uap->sm, &set, sizeof(set));
863066d836bSKonstantin Belousov 		if (error != 0)
864066d836bSKonstantin Belousov 			return (error);
865066d836bSKonstantin Belousov 		uset = &set;
866066d836bSKonstantin Belousov 	} else
867066d836bSKonstantin Belousov 		uset = NULL;
868066d836bSKonstantin Belousov 	return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
869066d836bSKonstantin Belousov 	    uset, NFDBITS));
870066d836bSKonstantin Belousov }
871066d836bSKonstantin Belousov 
872066d836bSKonstantin Belousov int
873066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
874066d836bSKonstantin Belousov     struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
875066d836bSKonstantin Belousov {
876066d836bSKonstantin Belousov 	int error;
877066d836bSKonstantin Belousov 
878066d836bSKonstantin Belousov 	if (uset != NULL) {
879066d836bSKonstantin Belousov 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
880066d836bSKonstantin Belousov 		    &td->td_oldsigmask, 0);
881066d836bSKonstantin Belousov 		if (error != 0)
882066d836bSKonstantin Belousov 			return (error);
883066d836bSKonstantin Belousov 		td->td_pflags |= TDP_OLDMASK;
884066d836bSKonstantin Belousov 		/*
885066d836bSKonstantin Belousov 		 * Make sure that ast() is called on return to
886066d836bSKonstantin Belousov 		 * usermode and TDP_OLDMASK is cleared, restoring old
887066d836bSKonstantin Belousov 		 * sigmask.
888066d836bSKonstantin Belousov 		 */
889066d836bSKonstantin Belousov 		thread_lock(td);
890066d836bSKonstantin Belousov 		td->td_flags |= TDF_ASTPENDING;
891066d836bSKonstantin Belousov 		thread_unlock(td);
892066d836bSKonstantin Belousov 	}
893066d836bSKonstantin Belousov 	error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
894066d836bSKonstantin Belousov 	return (error);
895066d836bSKonstantin Belousov }
896066d836bSKonstantin Belousov 
897d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
898df8bae1dSRodney W. Grimes struct select_args {
899b08f7993SSujal Patel 	int	nd;
900df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
901df8bae1dSRodney W. Grimes 	struct	timeval *tv;
902df8bae1dSRodney W. Grimes };
903d2d3e875SBruce Evans #endif
90426f9a767SRodney W. Grimes int
9058451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap)
906df8bae1dSRodney W. Grimes {
9078f19eb88SIan Dowse 	struct timeval tv, *tvp;
9088f19eb88SIan Dowse 	int error;
9098f19eb88SIan Dowse 
9108f19eb88SIan Dowse 	if (uap->tv != NULL) {
9118f19eb88SIan Dowse 		error = copyin(uap->tv, &tv, sizeof(tv));
9128f19eb88SIan Dowse 		if (error)
9138f19eb88SIan Dowse 			return (error);
9148f19eb88SIan Dowse 		tvp = &tv;
9158f19eb88SIan Dowse 	} else
9168f19eb88SIan Dowse 		tvp = NULL;
9178f19eb88SIan Dowse 
918b55ef216SKonstantin Belousov 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
919b55ef216SKonstantin Belousov 	    NFDBITS));
9208f19eb88SIan Dowse }
9218f19eb88SIan Dowse 
92256be1b9aSKonstantin Belousov /*
92356be1b9aSKonstantin Belousov  * In the unlikely case when user specified n greater then the last
92456be1b9aSKonstantin Belousov  * open file descriptor, check that no bits are set after the last
92556be1b9aSKonstantin Belousov  * valid fd.  We must return EBADF if any is set.
92656be1b9aSKonstantin Belousov  *
92756be1b9aSKonstantin Belousov  * There are applications that rely on the behaviour.
92856be1b9aSKonstantin Belousov  *
92956be1b9aSKonstantin Belousov  * nd is fd_lastfile + 1.
93056be1b9aSKonstantin Belousov  */
93156be1b9aSKonstantin Belousov static int
93256be1b9aSKonstantin Belousov select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits)
93356be1b9aSKonstantin Belousov {
93456be1b9aSKonstantin Belousov 	char *addr, *oaddr;
93556be1b9aSKonstantin Belousov 	int b, i, res;
93656be1b9aSKonstantin Belousov 	uint8_t bits;
93756be1b9aSKonstantin Belousov 
93856be1b9aSKonstantin Belousov 	if (nd >= ndu || fd_in == NULL)
93956be1b9aSKonstantin Belousov 		return (0);
94056be1b9aSKonstantin Belousov 
94156be1b9aSKonstantin Belousov 	oaddr = NULL;
94256be1b9aSKonstantin Belousov 	bits = 0; /* silence gcc */
94356be1b9aSKonstantin Belousov 	for (i = nd; i < ndu; i++) {
94456be1b9aSKonstantin Belousov 		b = i / NBBY;
94556be1b9aSKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN
94656be1b9aSKonstantin Belousov 		addr = (char *)fd_in + b;
94756be1b9aSKonstantin Belousov #else
94856be1b9aSKonstantin Belousov 		addr = (char *)fd_in;
94956be1b9aSKonstantin Belousov 		if (abi_nfdbits == NFDBITS) {
95056be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(fd_mask)) +
95156be1b9aSKonstantin Belousov 			    sizeof(fd_mask) - 1 - b % sizeof(fd_mask);
95256be1b9aSKonstantin Belousov 		} else {
95356be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(uint32_t)) +
95456be1b9aSKonstantin Belousov 			    sizeof(uint32_t) - 1 - b % sizeof(uint32_t);
95556be1b9aSKonstantin Belousov 		}
95656be1b9aSKonstantin Belousov #endif
95756be1b9aSKonstantin Belousov 		if (addr != oaddr) {
95856be1b9aSKonstantin Belousov 			res = fubyte(addr);
95956be1b9aSKonstantin Belousov 			if (res == -1)
96056be1b9aSKonstantin Belousov 				return (EFAULT);
96156be1b9aSKonstantin Belousov 			oaddr = addr;
96256be1b9aSKonstantin Belousov 			bits = res;
96356be1b9aSKonstantin Belousov 		}
96456be1b9aSKonstantin Belousov 		if ((bits & (1 << (i % NBBY))) != 0)
96556be1b9aSKonstantin Belousov 			return (EBADF);
96656be1b9aSKonstantin Belousov 	}
96756be1b9aSKonstantin Belousov 	return (0);
96856be1b9aSKonstantin Belousov }
96956be1b9aSKonstantin Belousov 
9708f19eb88SIan Dowse int
9718f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
972b55ef216SKonstantin Belousov     fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
9738f19eb88SIan Dowse {
974426da3bcSAlfred Perlstein 	struct filedesc *fdp;
975d5e4d7e1SBruce Evans 	/*
976d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
977d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
978d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
979d5e4d7e1SBruce Evans 	 * of 256.
980d5e4d7e1SBruce Evans 	 */
981d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
982eb209311SAlfred Perlstein 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
983cf5e4fe6SDavide Italiano 	struct timeval rtv;
984cf5e4fe6SDavide Italiano 	sbintime_t asbt, precision, rsbt;
985b55ef216SKonstantin Belousov 	u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
986cf5e4fe6SDavide Italiano 	int error, lf, ndu;
987df8bae1dSRodney W. Grimes 
9888f19eb88SIan Dowse 	if (nd < 0)
989acbfbfeaSSujal Patel 		return (EINVAL);
990426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
99156be1b9aSKonstantin Belousov 	ndu = nd;
99256be1b9aSKonstantin Belousov 	lf = fdp->fd_lastfile;
99356be1b9aSKonstantin Belousov 	if (nd > lf + 1)
99456be1b9aSKonstantin Belousov 		nd = lf + 1;
99556be1b9aSKonstantin Belousov 
99656be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits);
99756be1b9aSKonstantin Belousov 	if (error != 0)
99856be1b9aSKonstantin Belousov 		return (error);
99956be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits);
100056be1b9aSKonstantin Belousov 	if (error != 0)
100156be1b9aSKonstantin Belousov 		return (error);
100256be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits);
100356be1b9aSKonstantin Belousov 	if (error != 0)
100456be1b9aSKonstantin Belousov 		return (error);
1005b08f7993SSujal Patel 
1006d5e4d7e1SBruce Evans 	/*
1007d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
1008d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
1009d5e4d7e1SBruce Evans 	 */
10108f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
1011d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
1012b55ef216SKonstantin Belousov 	ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
1013d5e4d7e1SBruce Evans 	nbufbytes = 0;
10148f19eb88SIan Dowse 	if (fd_in != NULL)
1015d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
10168f19eb88SIan Dowse 	if (fd_ou != NULL)
1017d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
10188f19eb88SIan Dowse 	if (fd_ex != NULL)
1019d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
1020d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
1021d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
1022d5e4d7e1SBruce Evans 	else
1023a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
1024b08f7993SSujal Patel 
1025b08f7993SSujal Patel 	/*
1026d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
1027d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
1028d5e4d7e1SBruce Evans 	 * together.
1029b08f7993SSujal Patel 	 */
1030d5e4d7e1SBruce Evans 	sbp = selbits;
1031df8bae1dSRodney W. Grimes #define	getbits(name, x) \
1032d5e4d7e1SBruce Evans 	do {								\
1033841c0c7eSNathan Whitehorn 		if (name == NULL) {					\
1034d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
1035841c0c7eSNathan Whitehorn 			obits[x] = NULL;				\
1036841c0c7eSNathan Whitehorn 		} else {						\
1037d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
1038d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
1039d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
1040b55ef216SKonstantin Belousov 			error = copyin(name, ibits[x], ncpubytes);	\
1041265fc98fSSeigo Tanimura 			if (error != 0)					\
1042ace8398dSJeff Roberson 				goto done;				\
1043b55ef216SKonstantin Belousov 			bzero((char *)ibits[x] + ncpubytes,		\
1044b55ef216SKonstantin Belousov 			    ncpbytes - ncpubytes);			\
1045e04ac2feSJohn Baldwin 		}							\
1046d5e4d7e1SBruce Evans 	} while (0)
10478f19eb88SIan Dowse 	getbits(fd_in, 0);
10488f19eb88SIan Dowse 	getbits(fd_ou, 1);
10498f19eb88SIan Dowse 	getbits(fd_ex, 2);
1050df8bae1dSRodney W. Grimes #undef	getbits
1051841c0c7eSNathan Whitehorn 
1052841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
1053841c0c7eSNathan Whitehorn 	/*
1054841c0c7eSNathan Whitehorn 	 * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
1055841c0c7eSNathan Whitehorn 	 * we are running under 32-bit emulation. This should be more
1056841c0c7eSNathan Whitehorn 	 * generic.
1057841c0c7eSNathan Whitehorn 	 */
1058841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)						\
1059841c0c7eSNathan Whitehorn 	if (abi_nfdbits != NFDBITS && bits != NULL) {			\
1060841c0c7eSNathan Whitehorn 		int i;							\
1061841c0c7eSNathan Whitehorn 		for (i = 0; i < ncpbytes / sizeof *sbp; i++)		\
1062841c0c7eSNathan Whitehorn 			bits[i] = (bits[i] >> 32) | (bits[i] << 32);	\
1063841c0c7eSNathan Whitehorn 	}
1064841c0c7eSNathan Whitehorn #else
1065841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)
1066841c0c7eSNathan Whitehorn #endif
1067841c0c7eSNathan Whitehorn 
1068841c0c7eSNathan Whitehorn 	/* Make sure the bit order makes it through an ABI transition */
1069841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[0]);
1070841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[1]);
1071841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[2]);
1072841c0c7eSNathan Whitehorn 
1073d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
1074d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
1075df8bae1dSRodney W. Grimes 
1076cf5e4fe6SDavide Italiano 	precision = 0;
10778f19eb88SIan Dowse 	if (tvp != NULL) {
1078cf5e4fe6SDavide Italiano 		rtv = *tvp;
1079cf5e4fe6SDavide Italiano 		if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1080cf5e4fe6SDavide Italiano 		    rtv.tv_usec >= 1000000) {
1081df8bae1dSRodney W. Grimes 			error = EINVAL;
1082ace8398dSJeff Roberson 			goto done;
1083df8bae1dSRodney W. Grimes 		}
108421a37a71SAlexander Motin 		if (!timevalisset(&rtv))
1085980c545dSAlexander Motin 			asbt = 0;
108621a37a71SAlexander Motin 		else if (rtv.tv_sec <= INT32_MAX) {
1087cf5e4fe6SDavide Italiano 			rsbt = tvtosbt(rtv);
1088cf5e4fe6SDavide Italiano 			precision = rsbt;
1089cf5e4fe6SDavide Italiano 			precision >>= tc_precexp;
1090cf5e4fe6SDavide Italiano 			if (TIMESEL(&asbt, rsbt))
1091cf5e4fe6SDavide Italiano 				asbt += tc_tick_sbt;
10924bc38a5aSDavide Italiano 			if (asbt <= SBT_MAX - rsbt)
1093cf5e4fe6SDavide Italiano 				asbt += rsbt;
109421a37a71SAlexander Motin 			else
1095980c545dSAlexander Motin 				asbt = -1;
1096980c545dSAlexander Motin 		} else
1097980c545dSAlexander Motin 			asbt = -1;
1098cf5e4fe6SDavide Italiano 	} else
1099cf5e4fe6SDavide Italiano 		asbt = -1;
1100ace8398dSJeff Roberson 	seltdinit(td);
1101ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1102ace8398dSJeff Roberson 	for (;;) {
11038f19eb88SIan Dowse 		error = selscan(td, ibits, obits, nd);
1104ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1105ace8398dSJeff Roberson 			break;
1106cf5e4fe6SDavide Italiano 		error = seltdwait(td, asbt, precision);
1107ace8398dSJeff Roberson 		if (error)
1108ace8398dSJeff Roberson 			break;
1109ace8398dSJeff Roberson 		error = selrescan(td, ibits, obits);
1110ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1111ace8398dSJeff Roberson 			break;
111285f190e4SAlfred Perlstein 	}
1113ace8398dSJeff Roberson 	seltdclear(td);
1114265fc98fSSeigo Tanimura 
1115df8bae1dSRodney W. Grimes done:
1116df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
1117df8bae1dSRodney W. Grimes 	if (error == ERESTART)
1118df8bae1dSRodney W. Grimes 		error = EINTR;
1119df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
1120df8bae1dSRodney W. Grimes 		error = 0;
1121841c0c7eSNathan Whitehorn 
1122841c0c7eSNathan Whitehorn 	/* swizzle bit order back, if necessary */
1123841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[0]);
1124841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[1]);
1125841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[2]);
1126841c0c7eSNathan Whitehorn #undef swizzle_fdset
1127841c0c7eSNathan Whitehorn 
1128df8bae1dSRodney W. Grimes #define	putbits(name, x) \
1129b55ef216SKonstantin Belousov 	if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
1130df8bae1dSRodney W. Grimes 		error = error2;
1131df8bae1dSRodney W. Grimes 	if (error == 0) {
1132df8bae1dSRodney W. Grimes 		int error2;
1133df8bae1dSRodney W. Grimes 
11348f19eb88SIan Dowse 		putbits(fd_in, 0);
11358f19eb88SIan Dowse 		putbits(fd_ou, 1);
11368f19eb88SIan Dowse 		putbits(fd_ex, 2);
1137df8bae1dSRodney W. Grimes #undef putbits
1138df8bae1dSRodney W. Grimes 	}
1139d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
1140d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
1141ad2edad9SMatthew Dillon 
1142df8bae1dSRodney W. Grimes 	return (error);
1143df8bae1dSRodney W. Grimes }
114411b763dfSJeff Roberson /*
114511b763dfSJeff Roberson  * Convert a select bit set to poll flags.
1146748b9df6SJeff Roberson  *
114711b763dfSJeff Roberson  * The backend always returns POLLHUP/POLLERR if appropriate and we
114811b763dfSJeff Roberson  * return this as a set bit in any set.
114911b763dfSJeff Roberson  */
115011b763dfSJeff Roberson static int select_flags[3] = {
115111b763dfSJeff Roberson     POLLRDNORM | POLLHUP | POLLERR,
115211b763dfSJeff Roberson     POLLWRNORM | POLLHUP | POLLERR,
115361e53a38SKonstantin Belousov     POLLRDBAND | POLLERR
115411b763dfSJeff Roberson };
115511b763dfSJeff Roberson 
115611b763dfSJeff Roberson /*
115711b763dfSJeff Roberson  * Compute the fo_poll flags required for a fd given by the index and
115811b763dfSJeff Roberson  * bit position in the fd_mask array.
115911b763dfSJeff Roberson  */
116011b763dfSJeff Roberson static __inline int
116160b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit)
116211b763dfSJeff Roberson {
116311b763dfSJeff Roberson 	int flags;
116411b763dfSJeff Roberson 	int msk;
116511b763dfSJeff Roberson 
116611b763dfSJeff Roberson 	flags = 0;
116711b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
116811b763dfSJeff Roberson 		if (ibits[msk] == NULL)
116911b763dfSJeff Roberson 			continue;
117060b7f468SStephane E. Potvin 		if ((ibits[msk][idx] & bit) == 0)
117111b763dfSJeff Roberson 			continue;
117211b763dfSJeff Roberson 		flags |= select_flags[msk];
117311b763dfSJeff Roberson 	}
117411b763dfSJeff Roberson 	return (flags);
117511b763dfSJeff Roberson }
117611b763dfSJeff Roberson 
117711b763dfSJeff Roberson /*
117811b763dfSJeff Roberson  * Set the appropriate output bits given a mask of fired events and the
117911b763dfSJeff Roberson  * input bits originally requested.
118011b763dfSJeff Roberson  */
118111b763dfSJeff Roberson static __inline int
118211b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
118311b763dfSJeff Roberson {
118411b763dfSJeff Roberson 	int msk;
118511b763dfSJeff Roberson 	int n;
118611b763dfSJeff Roberson 
118711b763dfSJeff Roberson 	n = 0;
118811b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
118911b763dfSJeff Roberson 		if ((events & select_flags[msk]) == 0)
119011b763dfSJeff Roberson 			continue;
119111b763dfSJeff Roberson 		if (ibits[msk] == NULL)
119211b763dfSJeff Roberson 			continue;
119311b763dfSJeff Roberson 		if ((ibits[msk][idx] & bit) == 0)
119411b763dfSJeff Roberson 			continue;
119511b763dfSJeff Roberson 		/*
119611b763dfSJeff Roberson 		 * XXX Check for a duplicate set.  This can occur because a
119711b763dfSJeff Roberson 		 * socket calls selrecord() twice for each poll() call
119811b763dfSJeff Roberson 		 * resulting in two selfds per real fd.  selrescan() will
119911b763dfSJeff Roberson 		 * call selsetbits twice as a result.
120011b763dfSJeff Roberson 		 */
120111b763dfSJeff Roberson 		if ((obits[msk][idx] & bit) != 0)
120211b763dfSJeff Roberson 			continue;
120311b763dfSJeff Roberson 		obits[msk][idx] |= bit;
120411b763dfSJeff Roberson 		n++;
120511b763dfSJeff Roberson 	}
120611b763dfSJeff Roberson 
120711b763dfSJeff Roberson 	return (n);
120811b763dfSJeff Roberson }
1209df8bae1dSRodney W. Grimes 
1210a9d2f8d8SRobert Watson static __inline int
1211a9d2f8d8SRobert Watson getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp)
1212a9d2f8d8SRobert Watson {
12137008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
1214a9d2f8d8SRobert Watson 
1215ed5848c8SPawel Jakub Dawidek 	cap_rights_init(&rights, CAP_EVENT);
1216ed5848c8SPawel Jakub Dawidek 
1217ed5848c8SPawel Jakub Dawidek 	return (fget_unlocked(fdp, fd, &rights, 0, fpp, NULL));
1218a9d2f8d8SRobert Watson }
1219a9d2f8d8SRobert Watson 
1220ace8398dSJeff Roberson /*
1221ace8398dSJeff Roberson  * Traverse the list of fds attached to this thread's seltd and check for
1222ace8398dSJeff Roberson  * completion.
1223ace8398dSJeff Roberson  */
1224ace8398dSJeff Roberson static int
1225ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
1226ace8398dSJeff Roberson {
122711b763dfSJeff Roberson 	struct filedesc *fdp;
122811b763dfSJeff Roberson 	struct selinfo *si;
1229ace8398dSJeff Roberson 	struct seltd *stp;
1230ace8398dSJeff Roberson 	struct selfd *sfp;
1231ace8398dSJeff Roberson 	struct selfd *sfn;
1232ace8398dSJeff Roberson 	struct file *fp;
12339cdacff1SJeff Roberson 	fd_mask bit;
12349cdacff1SJeff Roberson 	int fd, ev, n, idx;
1235a9d2f8d8SRobert Watson 	int error;
1236ace8398dSJeff Roberson 
123711b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
1238ace8398dSJeff Roberson 	stp = td->td_sel;
123911b763dfSJeff Roberson 	n = 0;
1240ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1241ace8398dSJeff Roberson 		fd = (int)(uintptr_t)sfp->sf_cookie;
1242ace8398dSJeff Roberson 		si = sfp->sf_si;
1243ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1244ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1245ace8398dSJeff Roberson 		if (si != NULL)
1246ace8398dSJeff Roberson 			continue;
1247a9d2f8d8SRobert Watson 		error = getselfd_cap(fdp, fd, &fp);
1248a9d2f8d8SRobert Watson 		if (error)
1249a9d2f8d8SRobert Watson 			return (error);
125011b763dfSJeff Roberson 		idx = fd / NFDBITS;
12519cdacff1SJeff Roberson 		bit = (fd_mask)1 << (fd % NFDBITS);
125211b763dfSJeff Roberson 		ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
1253bf422e5fSJeff Roberson 		fdrop(fp, td);
125411b763dfSJeff Roberson 		if (ev != 0)
125511b763dfSJeff Roberson 			n += selsetbits(ibits, obits, idx, bit, ev);
1256ace8398dSJeff Roberson 	}
1257ace8398dSJeff Roberson 	stp->st_flags = 0;
1258ace8398dSJeff Roberson 	td->td_retval[0] = n;
1259ace8398dSJeff Roberson 	return (0);
1260ace8398dSJeff Roberson }
1261ace8398dSJeff Roberson 
1262ace8398dSJeff Roberson /*
1263ace8398dSJeff Roberson  * Perform the initial filedescriptor scan and register ourselves with
1264ace8398dSJeff Roberson  * each selinfo.
1265ace8398dSJeff Roberson  */
1266265fc98fSSeigo Tanimura static int
1267b40ce416SJulian Elischer selscan(td, ibits, obits, nfd)
1268b40ce416SJulian Elischer 	struct thread *td;
1269b08f7993SSujal Patel 	fd_mask **ibits, **obits;
1270cb226aaaSPoul-Henning Kamp 	int nfd;
1271df8bae1dSRodney W. Grimes {
127211b763dfSJeff Roberson 	struct filedesc *fdp;
1273df8bae1dSRodney W. Grimes 	struct file *fp;
12749cdacff1SJeff Roberson 	fd_mask bit;
127511b763dfSJeff Roberson 	int ev, flags, end, fd;
12769cdacff1SJeff Roberson 	int n, idx;
1277a9d2f8d8SRobert Watson 	int error;
1278df8bae1dSRodney W. Grimes 
127911b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
128011b763dfSJeff Roberson 	n = 0;
12819cdacff1SJeff Roberson 	for (idx = 0, fd = 0; fd < nfd; idx++) {
128211b763dfSJeff Roberson 		end = imin(fd + NFDBITS, nfd);
128311b763dfSJeff Roberson 		for (bit = 1; fd < end; bit <<= 1, fd++) {
128411b763dfSJeff Roberson 			/* Compute the list of events we're interested in. */
128511b763dfSJeff Roberson 			flags = selflags(ibits, idx, bit);
128611b763dfSJeff Roberson 			if (flags == 0)
1287f082218cSPeter Wemm 				continue;
1288a9d2f8d8SRobert Watson 			error = getselfd_cap(fdp, fd, &fp);
1289a9d2f8d8SRobert Watson 			if (error)
1290a9d2f8d8SRobert Watson 				return (error);
1291ace8398dSJeff Roberson 			selfdalloc(td, (void *)(uintptr_t)fd);
129211b763dfSJeff Roberson 			ev = fo_poll(fp, flags, td->td_ucred, td);
1293bf422e5fSJeff Roberson 			fdrop(fp, td);
129411b763dfSJeff Roberson 			if (ev != 0)
129511b763dfSJeff Roberson 				n += selsetbits(ibits, obits, idx, bit, ev);
1296df8bae1dSRodney W. Grimes 		}
1297df8bae1dSRodney W. Grimes 	}
129811b763dfSJeff Roberson 
1299b40ce416SJulian Elischer 	td->td_retval[0] = n;
1300df8bae1dSRodney W. Grimes 	return (0);
1301df8bae1dSRodney W. Grimes }
1302df8bae1dSRodney W. Grimes 
130342d11757SPeter Wemm int
1304186d9c34SDmitry Chagin sys_poll(struct thread *td, struct poll_args *uap)
1305186d9c34SDmitry Chagin {
1306186d9c34SDmitry Chagin 	struct timespec ts, *tsp;
1307186d9c34SDmitry Chagin 
1308186d9c34SDmitry Chagin 	if (uap->timeout != INFTIM) {
1309186d9c34SDmitry Chagin 		if (uap->timeout < 0)
1310186d9c34SDmitry Chagin 			return (EINVAL);
1311186d9c34SDmitry Chagin 		ts.tv_sec = uap->timeout / 1000;
1312186d9c34SDmitry Chagin 		ts.tv_nsec = (uap->timeout % 1000) * 1000000;
1313186d9c34SDmitry Chagin 		tsp = &ts;
1314186d9c34SDmitry Chagin 	} else
1315186d9c34SDmitry Chagin 		tsp = NULL;
1316186d9c34SDmitry Chagin 
1317186d9c34SDmitry Chagin 	return (kern_poll(td, uap->fds, uap->nfds, tsp, NULL));
1318186d9c34SDmitry Chagin }
1319186d9c34SDmitry Chagin 
1320186d9c34SDmitry Chagin int
1321186d9c34SDmitry Chagin kern_poll(struct thread *td, struct pollfd *fds, u_int nfds,
1322186d9c34SDmitry Chagin     struct timespec *tsp, sigset_t *uset)
132342d11757SPeter Wemm {
13242580f4e5SAndre Oppermann 	struct pollfd *bits;
13252580f4e5SAndre Oppermann 	struct pollfd smallbits[32];
1326186d9c34SDmitry Chagin 	sbintime_t sbt, precision, tmp;
1327186d9c34SDmitry Chagin 	time_t over;
1328186d9c34SDmitry Chagin 	struct timespec ts;
1329cf5e4fe6SDavide Italiano 	int error;
133042d11757SPeter Wemm 	size_t ni;
133142d11757SPeter Wemm 
1332186d9c34SDmitry Chagin 	precision = 0;
1333186d9c34SDmitry Chagin 	if (tsp != NULL) {
1334186d9c34SDmitry Chagin 		if (tsp->tv_sec < 0)
1335186d9c34SDmitry Chagin 			return (EINVAL);
1336186d9c34SDmitry Chagin 		if (tsp->tv_nsec < 0 || tsp->tv_nsec >= 1000000000)
1337186d9c34SDmitry Chagin 			return (EINVAL);
1338186d9c34SDmitry Chagin 		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1339186d9c34SDmitry Chagin 			sbt = 0;
1340186d9c34SDmitry Chagin 		else {
1341186d9c34SDmitry Chagin 			ts = *tsp;
1342186d9c34SDmitry Chagin 			if (ts.tv_sec > INT32_MAX / 2) {
1343186d9c34SDmitry Chagin 				over = ts.tv_sec - INT32_MAX / 2;
1344186d9c34SDmitry Chagin 				ts.tv_sec -= over;
1345186d9c34SDmitry Chagin 			} else
1346186d9c34SDmitry Chagin 				over = 0;
1347186d9c34SDmitry Chagin 			tmp = tstosbt(ts);
1348186d9c34SDmitry Chagin 			precision = tmp;
1349186d9c34SDmitry Chagin 			precision >>= tc_precexp;
1350186d9c34SDmitry Chagin 			if (TIMESEL(&sbt, tmp))
1351186d9c34SDmitry Chagin 				sbt += tc_tick_sbt;
1352186d9c34SDmitry Chagin 			sbt += tmp;
1353186d9c34SDmitry Chagin 		}
1354186d9c34SDmitry Chagin 	} else
1355186d9c34SDmitry Chagin 		sbt = -1;
1356186d9c34SDmitry Chagin 
1357374ae2a3SJeff Roberson 	if (nfds > maxfilesperproc && nfds > FD_SETSIZE)
1358ace8398dSJeff Roberson 		return (EINVAL);
135989b71647SPeter Wemm 	ni = nfds * sizeof(struct pollfd);
136042d11757SPeter Wemm 	if (ni > sizeof(smallbits))
1361a163d034SWarner Losh 		bits = malloc(ni, M_TEMP, M_WAITOK);
136242d11757SPeter Wemm 	else
136342d11757SPeter Wemm 		bits = smallbits;
1364186d9c34SDmitry Chagin 	error = copyin(fds, bits, ni);
136542d11757SPeter Wemm 	if (error)
1366ace8398dSJeff Roberson 		goto done;
1367186d9c34SDmitry Chagin 
1368186d9c34SDmitry Chagin 	if (uset != NULL) {
1369186d9c34SDmitry Chagin 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
1370186d9c34SDmitry Chagin 		    &td->td_oldsigmask, 0);
1371186d9c34SDmitry Chagin 		if (error)
1372ace8398dSJeff Roberson 			goto done;
1373186d9c34SDmitry Chagin 		td->td_pflags |= TDP_OLDMASK;
1374186d9c34SDmitry Chagin 		/*
1375186d9c34SDmitry Chagin 		 * Make sure that ast() is called on return to
1376186d9c34SDmitry Chagin 		 * usermode and TDP_OLDMASK is cleared, restoring old
1377186d9c34SDmitry Chagin 		 * sigmask.
1378186d9c34SDmitry Chagin 		 */
1379186d9c34SDmitry Chagin 		thread_lock(td);
1380186d9c34SDmitry Chagin 		td->td_flags |= TDF_ASTPENDING;
1381186d9c34SDmitry Chagin 		thread_unlock(td);
138242d11757SPeter Wemm 	}
1383186d9c34SDmitry Chagin 
1384ace8398dSJeff Roberson 	seltdinit(td);
1385ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1386ace8398dSJeff Roberson 	for (;;) {
13872580f4e5SAndre Oppermann 		error = pollscan(td, bits, nfds);
1388ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1389ace8398dSJeff Roberson 			break;
1390186d9c34SDmitry Chagin 		error = seltdwait(td, sbt, precision);
1391ace8398dSJeff Roberson 		if (error)
1392ace8398dSJeff Roberson 			break;
1393ace8398dSJeff Roberson 		error = pollrescan(td);
1394ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1395ace8398dSJeff Roberson 			break;
139685f190e4SAlfred Perlstein 	}
1397ace8398dSJeff Roberson 	seltdclear(td);
1398265fc98fSSeigo Tanimura 
139942d11757SPeter Wemm done:
140042d11757SPeter Wemm 	/* poll is not restarted after signals... */
140142d11757SPeter Wemm 	if (error == ERESTART)
140242d11757SPeter Wemm 		error = EINTR;
140342d11757SPeter Wemm 	if (error == EWOULDBLOCK)
140442d11757SPeter Wemm 		error = 0;
140542d11757SPeter Wemm 	if (error == 0) {
1406186d9c34SDmitry Chagin 		error = pollout(td, bits, fds, nfds);
140742d11757SPeter Wemm 		if (error)
140842d11757SPeter Wemm 			goto out;
140942d11757SPeter Wemm 	}
141042d11757SPeter Wemm out:
141142d11757SPeter Wemm 	if (ni > sizeof(smallbits))
141242d11757SPeter Wemm 		free(bits, M_TEMP);
141342d11757SPeter Wemm 	return (error);
141442d11757SPeter Wemm }
141542d11757SPeter Wemm 
1416186d9c34SDmitry Chagin int
1417186d9c34SDmitry Chagin sys_ppoll(struct thread *td, struct ppoll_args *uap)
1418186d9c34SDmitry Chagin {
1419186d9c34SDmitry Chagin 	struct timespec ts, *tsp;
1420186d9c34SDmitry Chagin 	sigset_t set, *ssp;
1421186d9c34SDmitry Chagin 	int error;
1422186d9c34SDmitry Chagin 
1423186d9c34SDmitry Chagin 	if (uap->ts != NULL) {
1424186d9c34SDmitry Chagin 		error = copyin(uap->ts, &ts, sizeof(ts));
1425186d9c34SDmitry Chagin 		if (error)
1426186d9c34SDmitry Chagin 			return (error);
1427186d9c34SDmitry Chagin 		tsp = &ts;
1428186d9c34SDmitry Chagin 	} else
1429186d9c34SDmitry Chagin 		tsp = NULL;
1430186d9c34SDmitry Chagin 	if (uap->set != NULL) {
1431186d9c34SDmitry Chagin 		error = copyin(uap->set, &set, sizeof(set));
1432186d9c34SDmitry Chagin 		if (error)
1433186d9c34SDmitry Chagin 			return (error);
1434186d9c34SDmitry Chagin 		ssp = &set;
1435186d9c34SDmitry Chagin 	} else
1436186d9c34SDmitry Chagin 		ssp = NULL;
1437186d9c34SDmitry Chagin 	/*
1438186d9c34SDmitry Chagin 	 * fds is still a pointer to user space. kern_poll() will
1439186d9c34SDmitry Chagin 	 * take care of copyin that array to the kernel space.
1440186d9c34SDmitry Chagin 	 */
1441186d9c34SDmitry Chagin 
1442186d9c34SDmitry Chagin 	return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
1443186d9c34SDmitry Chagin }
1444186d9c34SDmitry Chagin 
144542d11757SPeter Wemm static int
1446ace8398dSJeff Roberson pollrescan(struct thread *td)
1447ace8398dSJeff Roberson {
1448ace8398dSJeff Roberson 	struct seltd *stp;
1449ace8398dSJeff Roberson 	struct selfd *sfp;
1450ace8398dSJeff Roberson 	struct selfd *sfn;
1451ace8398dSJeff Roberson 	struct selinfo *si;
1452ace8398dSJeff Roberson 	struct filedesc *fdp;
1453ace8398dSJeff Roberson 	struct file *fp;
1454ace8398dSJeff Roberson 	struct pollfd *fd;
14558e076effSSean Bruno #ifdef CAPABILITIES
14567008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
14578e076effSSean Bruno #endif
1458ace8398dSJeff Roberson 	int n;
1459ace8398dSJeff Roberson 
1460ace8398dSJeff Roberson 	n = 0;
1461ace8398dSJeff Roberson 	fdp = td->td_proc->p_fd;
1462ace8398dSJeff Roberson 	stp = td->td_sel;
1463ace8398dSJeff Roberson 	FILEDESC_SLOCK(fdp);
1464ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1465ace8398dSJeff Roberson 		fd = (struct pollfd *)sfp->sf_cookie;
1466ace8398dSJeff Roberson 		si = sfp->sf_si;
1467ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1468ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1469ace8398dSJeff Roberson 		if (si != NULL)
1470ace8398dSJeff Roberson 			continue;
14712609222aSPawel Jakub Dawidek 		fp = fdp->fd_ofiles[fd->fd].fde_file;
1472d6f72489SJonathan Anderson #ifdef CAPABILITIES
14732609222aSPawel Jakub Dawidek 		if (fp == NULL ||
14747008be5bSPawel Jakub Dawidek 		    cap_check(cap_rights(fdp, fd->fd),
1475ed5848c8SPawel Jakub Dawidek 		    cap_rights_init(&rights, CAP_EVENT)) != 0)
1476d6f72489SJonathan Anderson #else
14772609222aSPawel Jakub Dawidek 		if (fp == NULL)
1478d6f72489SJonathan Anderson #endif
14792609222aSPawel Jakub Dawidek 		{
1480ace8398dSJeff Roberson 			fd->revents = POLLNVAL;
1481ace8398dSJeff Roberson 			n++;
1482ace8398dSJeff Roberson 			continue;
1483ace8398dSJeff Roberson 		}
1484d6f72489SJonathan Anderson 
1485ace8398dSJeff Roberson 		/*
1486ace8398dSJeff Roberson 		 * Note: backend also returns POLLHUP and
1487ace8398dSJeff Roberson 		 * POLLERR if appropriate.
1488ace8398dSJeff Roberson 		 */
1489ace8398dSJeff Roberson 		fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1490ace8398dSJeff Roberson 		if (fd->revents != 0)
1491ace8398dSJeff Roberson 			n++;
1492ace8398dSJeff Roberson 	}
1493ace8398dSJeff Roberson 	FILEDESC_SUNLOCK(fdp);
1494ace8398dSJeff Roberson 	stp->st_flags = 0;
1495ace8398dSJeff Roberson 	td->td_retval[0] = n;
1496ace8398dSJeff Roberson 	return (0);
1497ace8398dSJeff Roberson }
1498ace8398dSJeff Roberson 
1499ace8398dSJeff Roberson 
1500ace8398dSJeff Roberson static int
15016d8feddaSKonstantin Belousov pollout(td, fds, ufds, nfd)
15026d8feddaSKonstantin Belousov 	struct thread *td;
1503ae81968fSRobert Watson 	struct pollfd *fds;
1504ae81968fSRobert Watson 	struct pollfd *ufds;
1505ae81968fSRobert Watson 	u_int nfd;
1506ae81968fSRobert Watson {
1507ae81968fSRobert Watson 	int error = 0;
1508ae81968fSRobert Watson 	u_int i = 0;
15096d8feddaSKonstantin Belousov 	u_int n = 0;
1510ae81968fSRobert Watson 
1511ae81968fSRobert Watson 	for (i = 0; i < nfd; i++) {
1512ae81968fSRobert Watson 		error = copyout(&fds->revents, &ufds->revents,
1513ae81968fSRobert Watson 		    sizeof(ufds->revents));
1514ae81968fSRobert Watson 		if (error)
1515ae81968fSRobert Watson 			return (error);
15166d8feddaSKonstantin Belousov 		if (fds->revents != 0)
15176d8feddaSKonstantin Belousov 			n++;
1518ae81968fSRobert Watson 		fds++;
1519ae81968fSRobert Watson 		ufds++;
1520ae81968fSRobert Watson 	}
15216d8feddaSKonstantin Belousov 	td->td_retval[0] = n;
1522ae81968fSRobert Watson 	return (0);
1523ae81968fSRobert Watson }
1524ae81968fSRobert Watson 
1525ae81968fSRobert Watson static int
1526b40ce416SJulian Elischer pollscan(td, fds, nfd)
1527b40ce416SJulian Elischer 	struct thread *td;
152842d11757SPeter Wemm 	struct pollfd *fds;
1529ea0237edSJonathan Lemon 	u_int nfd;
153042d11757SPeter Wemm {
1531ace8398dSJeff Roberson 	struct filedesc *fdp = td->td_proc->p_fd;
153242d11757SPeter Wemm 	struct file *fp;
15338e076effSSean Bruno #ifdef CAPABILITIES
15347008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
15358e076effSSean Bruno #endif
15362609222aSPawel Jakub Dawidek 	int i, n = 0;
153742d11757SPeter Wemm 
15385e3f7694SRobert Watson 	FILEDESC_SLOCK(fdp);
1539eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
1540adf87ab0SMateusz Guzik 		if (fds->fd > fdp->fd_lastfile) {
154142d11757SPeter Wemm 			fds->revents = POLLNVAL;
154242d11757SPeter Wemm 			n++;
1543337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
1544337c9691SJordan K. Hubbard 			fds->revents = 0;
154542d11757SPeter Wemm 		} else {
15462609222aSPawel Jakub Dawidek 			fp = fdp->fd_ofiles[fds->fd].fde_file;
1547d6f72489SJonathan Anderson #ifdef CAPABILITIES
15482609222aSPawel Jakub Dawidek 			if (fp == NULL ||
15492609222aSPawel Jakub Dawidek 			    cap_check(cap_rights(fdp, fds->fd),
1550ed5848c8SPawel Jakub Dawidek 			    cap_rights_init(&rights, CAP_EVENT)) != 0)
1551d6f72489SJonathan Anderson #else
15522609222aSPawel Jakub Dawidek 			if (fp == NULL)
1553d6f72489SJonathan Anderson #endif
15542609222aSPawel Jakub Dawidek 			{
155542d11757SPeter Wemm 				fds->revents = POLLNVAL;
155642d11757SPeter Wemm 				n++;
155742d11757SPeter Wemm 			} else {
15582087c896SBruce Evans 				/*
15592087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
15602087c896SBruce Evans 				 * POLLERR if appropriate.
15612087c896SBruce Evans 				 */
1562ace8398dSJeff Roberson 				selfdalloc(td, fds);
156313ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
1564ea6027a8SRobert Watson 				    td->td_ucred, td);
1565f2159cc7SKonstantin Belousov 				/*
1566f2159cc7SKonstantin Belousov 				 * POSIX requires POLLOUT to be never
1567f2159cc7SKonstantin Belousov 				 * set simultaneously with POLLHUP.
1568f2159cc7SKonstantin Belousov 				 */
1569f2159cc7SKonstantin Belousov 				if ((fds->revents & POLLHUP) != 0)
1570f2159cc7SKonstantin Belousov 					fds->revents &= ~POLLOUT;
1571f2159cc7SKonstantin Belousov 
157242d11757SPeter Wemm 				if (fds->revents != 0)
157342d11757SPeter Wemm 					n++;
157442d11757SPeter Wemm 			}
157542d11757SPeter Wemm 		}
157642d11757SPeter Wemm 	}
15775e3f7694SRobert Watson 	FILEDESC_SUNLOCK(fdp);
1578b40ce416SJulian Elischer 	td->td_retval[0] = n;
157942d11757SPeter Wemm 	return (0);
158042d11757SPeter Wemm }
158142d11757SPeter Wemm 
158242d11757SPeter Wemm /*
158342d11757SPeter Wemm  * OpenBSD poll system call.
15840c14ff0eSRobert Watson  *
158542d11757SPeter Wemm  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
158642d11757SPeter Wemm  */
158742d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
158842d11757SPeter Wemm struct openbsd_poll_args {
158942d11757SPeter Wemm 	struct pollfd *fds;
159042d11757SPeter Wemm 	u_int	nfds;
159142d11757SPeter Wemm 	int	timeout;
159242d11757SPeter Wemm };
159342d11757SPeter Wemm #endif
159442d11757SPeter Wemm int
15958451d0ddSKip Macy sys_openbsd_poll(td, uap)
1596b40ce416SJulian Elischer 	register struct thread *td;
159742d11757SPeter Wemm 	register struct openbsd_poll_args *uap;
159842d11757SPeter Wemm {
15998451d0ddSKip Macy 	return (sys_poll(td, (struct poll_args *)uap));
160042d11757SPeter Wemm }
160142d11757SPeter Wemm 
160285f190e4SAlfred Perlstein /*
1603237abf0cSDavide Italiano  * XXX This was created specifically to support netncp and netsmb.  This
1604237abf0cSDavide Italiano  * allows the caller to specify a socket to wait for events on.  It returns
1605237abf0cSDavide Italiano  * 0 if any events matched and an error otherwise.  There is no way to
1606237abf0cSDavide Italiano  * determine which events fired.
1607237abf0cSDavide Italiano  */
1608237abf0cSDavide Italiano int
1609237abf0cSDavide Italiano selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td)
1610237abf0cSDavide Italiano {
1611237abf0cSDavide Italiano 	struct timeval rtv;
1612237abf0cSDavide Italiano 	sbintime_t asbt, precision, rsbt;
1613237abf0cSDavide Italiano 	int error;
1614237abf0cSDavide Italiano 
161576665df9SPeter Wemm 	precision = 0;	/* stupid gcc! */
1616237abf0cSDavide Italiano 	if (tvp != NULL) {
1617237abf0cSDavide Italiano 		rtv = *tvp;
1618237abf0cSDavide Italiano 		if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1619237abf0cSDavide Italiano 		    rtv.tv_usec >= 1000000)
1620237abf0cSDavide Italiano 			return (EINVAL);
1621237abf0cSDavide Italiano 		if (!timevalisset(&rtv))
1622237abf0cSDavide Italiano 			asbt = 0;
1623237abf0cSDavide Italiano 		else if (rtv.tv_sec <= INT32_MAX) {
1624237abf0cSDavide Italiano 			rsbt = tvtosbt(rtv);
1625237abf0cSDavide Italiano 			precision = rsbt;
1626237abf0cSDavide Italiano 			precision >>= tc_precexp;
1627237abf0cSDavide Italiano 			if (TIMESEL(&asbt, rsbt))
1628237abf0cSDavide Italiano 				asbt += tc_tick_sbt;
16294bc38a5aSDavide Italiano 			if (asbt <= SBT_MAX - rsbt)
1630237abf0cSDavide Italiano 				asbt += rsbt;
1631237abf0cSDavide Italiano 			else
1632237abf0cSDavide Italiano 				asbt = -1;
1633237abf0cSDavide Italiano 		} else
1634237abf0cSDavide Italiano 			asbt = -1;
1635237abf0cSDavide Italiano 	} else
1636237abf0cSDavide Italiano 		asbt = -1;
1637237abf0cSDavide Italiano 	seltdinit(td);
1638237abf0cSDavide Italiano 	/*
1639237abf0cSDavide Italiano 	 * Iterate until the timeout expires or the socket becomes ready.
1640237abf0cSDavide Italiano 	 */
1641237abf0cSDavide Italiano 	for (;;) {
1642237abf0cSDavide Italiano 		selfdalloc(td, NULL);
1643237abf0cSDavide Italiano 		error = sopoll(so, events, NULL, td);
1644237abf0cSDavide Italiano 		/* error here is actually the ready events. */
1645237abf0cSDavide Italiano 		if (error)
1646237abf0cSDavide Italiano 			return (0);
1647237abf0cSDavide Italiano 		error = seltdwait(td, asbt, precision);
1648237abf0cSDavide Italiano 		if (error)
1649237abf0cSDavide Italiano 			break;
1650237abf0cSDavide Italiano 	}
1651237abf0cSDavide Italiano 	seltdclear(td);
1652237abf0cSDavide Italiano 	/* XXX Duplicates ncp/smb behavior. */
1653237abf0cSDavide Italiano 	if (error == ERESTART)
1654237abf0cSDavide Italiano 		error = 0;
1655237abf0cSDavide Italiano 	return (error);
1656237abf0cSDavide Italiano }
1657237abf0cSDavide Italiano 
1658237abf0cSDavide Italiano /*
1659ace8398dSJeff Roberson  * Preallocate two selfds associated with 'cookie'.  Some fo_poll routines
1660ace8398dSJeff Roberson  * have two select sets, one for read and another for write.
1661ace8398dSJeff Roberson  */
1662ace8398dSJeff Roberson static void
1663ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie)
1664ace8398dSJeff Roberson {
1665ace8398dSJeff Roberson 	struct seltd *stp;
1666ace8398dSJeff Roberson 
1667ace8398dSJeff Roberson 	stp = td->td_sel;
1668ace8398dSJeff Roberson 	if (stp->st_free1 == NULL)
1669ace8398dSJeff Roberson 		stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1670ace8398dSJeff Roberson 	stp->st_free1->sf_td = stp;
1671ace8398dSJeff Roberson 	stp->st_free1->sf_cookie = cookie;
1672ace8398dSJeff Roberson 	if (stp->st_free2 == NULL)
1673ace8398dSJeff Roberson 		stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1674ace8398dSJeff Roberson 	stp->st_free2->sf_td = stp;
1675ace8398dSJeff Roberson 	stp->st_free2->sf_cookie = cookie;
1676ace8398dSJeff Roberson }
1677ace8398dSJeff Roberson 
1678ace8398dSJeff Roberson static void
1679ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp)
1680ace8398dSJeff Roberson {
1681ace8398dSJeff Roberson 	STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
168273f2e5f7SMateusz Guzik 	if (sfp->sf_si != NULL) {
1683ace8398dSJeff Roberson 		mtx_lock(sfp->sf_mtx);
1684ffc5ce7bSMateusz Guzik 		if (sfp->sf_si != NULL)
1685ace8398dSJeff Roberson 			TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1686ace8398dSJeff Roberson 		mtx_unlock(sfp->sf_mtx);
168773f2e5f7SMateusz Guzik 	}
1688ace8398dSJeff Roberson 	uma_zfree(selfd_zone, sfp);
168985f190e4SAlfred Perlstein }
169085f190e4SAlfred Perlstein 
16916aba400aSAttilio Rao /* Drain the waiters tied to all the selfd belonging the specified selinfo. */
16926aba400aSAttilio Rao void
16936aba400aSAttilio Rao seldrain(sip)
16946aba400aSAttilio Rao         struct selinfo *sip;
16956aba400aSAttilio Rao {
16966aba400aSAttilio Rao 
16976aba400aSAttilio Rao 	/*
16986aba400aSAttilio Rao 	 * This feature is already provided by doselwakeup(), thus it is
16996aba400aSAttilio Rao 	 * enough to go for it.
17006aba400aSAttilio Rao 	 * Eventually, the context, should take care to avoid races
17016aba400aSAttilio Rao 	 * between thread calling select()/poll() and file descriptor
17026aba400aSAttilio Rao 	 * detaching, but, again, the races are just the same as
17036aba400aSAttilio Rao 	 * selwakeup().
17046aba400aSAttilio Rao 	 */
17056aba400aSAttilio Rao         doselwakeup(sip, -1);
17066aba400aSAttilio Rao }
17076aba400aSAttilio Rao 
1708df8bae1dSRodney W. Grimes /*
1709df8bae1dSRodney W. Grimes  * Record a select request.
1710df8bae1dSRodney W. Grimes  */
1711df8bae1dSRodney W. Grimes void
1712df8bae1dSRodney W. Grimes selrecord(selector, sip)
1713b40ce416SJulian Elischer 	struct thread *selector;
1714df8bae1dSRodney W. Grimes 	struct selinfo *sip;
1715df8bae1dSRodney W. Grimes {
1716ace8398dSJeff Roberson 	struct selfd *sfp;
1717ace8398dSJeff Roberson 	struct seltd *stp;
1718ace8398dSJeff Roberson 	struct mtx *mtxp;
1719df8bae1dSRodney W. Grimes 
1720ace8398dSJeff Roberson 	stp = selector->td_sel;
172185f190e4SAlfred Perlstein 	/*
1722ace8398dSJeff Roberson 	 * Don't record when doing a rescan.
172385f190e4SAlfred Perlstein 	 */
1724ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_RESCAN)
1725ace8398dSJeff Roberson 		return;
1726ace8398dSJeff Roberson 	/*
1727ace8398dSJeff Roberson 	 * Grab one of the preallocated descriptors.
1728ace8398dSJeff Roberson 	 */
1729ace8398dSJeff Roberson 	sfp = NULL;
1730ace8398dSJeff Roberson 	if ((sfp = stp->st_free1) != NULL)
1731ace8398dSJeff Roberson 		stp->st_free1 = NULL;
1732ace8398dSJeff Roberson 	else if ((sfp = stp->st_free2) != NULL)
1733ace8398dSJeff Roberson 		stp->st_free2 = NULL;
1734ace8398dSJeff Roberson 	else
1735ace8398dSJeff Roberson 		panic("selrecord: No free selfd on selq");
17362141453eSJeff Roberson 	mtxp = sip->si_mtx;
17372141453eSJeff Roberson 	if (mtxp == NULL)
17382141453eSJeff Roberson 		mtxp = mtx_pool_find(mtxpool_select, sip);
1739ace8398dSJeff Roberson 	/*
1740ace8398dSJeff Roberson 	 * Initialize the sfp and queue it in the thread.
1741ace8398dSJeff Roberson 	 */
1742ace8398dSJeff Roberson 	sfp->sf_si = sip;
1743ace8398dSJeff Roberson 	sfp->sf_mtx = mtxp;
1744ace8398dSJeff Roberson 	STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1745ace8398dSJeff Roberson 	/*
1746ace8398dSJeff Roberson 	 * Now that we've locked the sip, check for initialization.
1747ace8398dSJeff Roberson 	 */
1748ace8398dSJeff Roberson 	mtx_lock(mtxp);
1749ace8398dSJeff Roberson 	if (sip->si_mtx == NULL) {
1750ace8398dSJeff Roberson 		sip->si_mtx = mtxp;
1751ace8398dSJeff Roberson 		TAILQ_INIT(&sip->si_tdlist);
175285f190e4SAlfred Perlstein 	}
1753ace8398dSJeff Roberson 	/*
1754ace8398dSJeff Roberson 	 * Add this thread to the list of selfds listening on this selinfo.
1755ace8398dSJeff Roberson 	 */
1756ace8398dSJeff Roberson 	TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1757ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1758df8bae1dSRodney W. Grimes }
1759df8bae1dSRodney W. Grimes 
1760512824f8SSeigo Tanimura /* Wake up a selecting thread. */
1761df8bae1dSRodney W. Grimes void
1762df8bae1dSRodney W. Grimes selwakeup(sip)
176385f190e4SAlfred Perlstein 	struct selinfo *sip;
1764df8bae1dSRodney W. Grimes {
1765512824f8SSeigo Tanimura 	doselwakeup(sip, -1);
1766512824f8SSeigo Tanimura }
1767512824f8SSeigo Tanimura 
1768512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */
1769512824f8SSeigo Tanimura void
1770512824f8SSeigo Tanimura selwakeuppri(sip, pri)
1771512824f8SSeigo Tanimura 	struct selinfo *sip;
1772512824f8SSeigo Tanimura 	int pri;
1773512824f8SSeigo Tanimura {
1774512824f8SSeigo Tanimura 	doselwakeup(sip, pri);
1775512824f8SSeigo Tanimura }
1776512824f8SSeigo Tanimura 
1777512824f8SSeigo Tanimura /*
1778512824f8SSeigo Tanimura  * Do a wakeup when a selectable event occurs.
1779512824f8SSeigo Tanimura  */
1780512824f8SSeigo Tanimura static void
1781512824f8SSeigo Tanimura doselwakeup(sip, pri)
1782512824f8SSeigo Tanimura 	struct selinfo *sip;
1783512824f8SSeigo Tanimura 	int pri;
1784512824f8SSeigo Tanimura {
1785ace8398dSJeff Roberson 	struct selfd *sfp;
1786ace8398dSJeff Roberson 	struct selfd *sfn;
1787ace8398dSJeff Roberson 	struct seltd *stp;
1788df8bae1dSRodney W. Grimes 
1789ace8398dSJeff Roberson 	/* If it's not initialized there can't be any waiters. */
1790ace8398dSJeff Roberson 	if (sip->si_mtx == NULL)
1791b40ce416SJulian Elischer 		return;
1792ace8398dSJeff Roberson 	/*
1793ace8398dSJeff Roberson 	 * Locking the selinfo locks all selfds associated with it.
1794ace8398dSJeff Roberson 	 */
1795ace8398dSJeff Roberson 	mtx_lock(sip->si_mtx);
1796ace8398dSJeff Roberson 	TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1797ace8398dSJeff Roberson 		/*
1798ace8398dSJeff Roberson 		 * Once we remove this sfp from the list and clear the
1799ace8398dSJeff Roberson 		 * sf_si seltdclear will know to ignore this si.
1800ace8398dSJeff Roberson 		 */
1801ace8398dSJeff Roberson 		TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1802ace8398dSJeff Roberson 		sfp->sf_si = NULL;
1803ace8398dSJeff Roberson 		stp = sfp->sf_td;
1804ace8398dSJeff Roberson 		mtx_lock(&stp->st_mtx);
1805ace8398dSJeff Roberson 		stp->st_flags |= SELTD_PENDING;
1806ace8398dSJeff Roberson 		cv_broadcastpri(&stp->st_wait, pri);
1807ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1808b40ce416SJulian Elischer 	}
1809ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1810ace8398dSJeff Roberson }
1811ace8398dSJeff Roberson 
1812ace8398dSJeff Roberson static void
1813ace8398dSJeff Roberson seltdinit(struct thread *td)
1814ace8398dSJeff Roberson {
1815ace8398dSJeff Roberson 	struct seltd *stp;
1816ace8398dSJeff Roberson 
1817ace8398dSJeff Roberson 	if ((stp = td->td_sel) != NULL)
1818ace8398dSJeff Roberson 		goto out;
1819ace8398dSJeff Roberson 	td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1820ace8398dSJeff Roberson 	mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1821ace8398dSJeff Roberson 	cv_init(&stp->st_wait, "select");
1822ace8398dSJeff Roberson out:
1823ace8398dSJeff Roberson 	stp->st_flags = 0;
1824ace8398dSJeff Roberson 	STAILQ_INIT(&stp->st_selq);
1825ace8398dSJeff Roberson }
1826ace8398dSJeff Roberson 
1827ace8398dSJeff Roberson static int
1828cf5e4fe6SDavide Italiano seltdwait(struct thread *td, sbintime_t sbt, sbintime_t precision)
1829ace8398dSJeff Roberson {
1830ace8398dSJeff Roberson 	struct seltd *stp;
1831ace8398dSJeff Roberson 	int error;
1832ace8398dSJeff Roberson 
1833ace8398dSJeff Roberson 	stp = td->td_sel;
1834ace8398dSJeff Roberson 	/*
1835ace8398dSJeff Roberson 	 * An event of interest may occur while we do not hold the seltd
1836ace8398dSJeff Roberson 	 * locked so check the pending flag before we sleep.
1837ace8398dSJeff Roberson 	 */
1838ace8398dSJeff Roberson 	mtx_lock(&stp->st_mtx);
1839ace8398dSJeff Roberson 	/*
1840ace8398dSJeff Roberson 	 * Any further calls to selrecord will be a rescan.
1841ace8398dSJeff Roberson 	 */
1842ace8398dSJeff Roberson 	stp->st_flags |= SELTD_RESCAN;
1843ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_PENDING) {
1844ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1845ace8398dSJeff Roberson 		return (0);
1846ace8398dSJeff Roberson 	}
1847cf5e4fe6SDavide Italiano 	if (sbt == 0)
1848cf5e4fe6SDavide Italiano 		error = EWOULDBLOCK;
1849cf5e4fe6SDavide Italiano 	else if (sbt != -1)
1850cf5e4fe6SDavide Italiano 		error = cv_timedwait_sig_sbt(&stp->st_wait, &stp->st_mtx,
1851cf5e4fe6SDavide Italiano 		    sbt, precision, C_ABSOLUTE);
1852ace8398dSJeff Roberson 	else
1853ace8398dSJeff Roberson 		error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
1854ace8398dSJeff Roberson 	mtx_unlock(&stp->st_mtx);
1855ace8398dSJeff Roberson 
1856ace8398dSJeff Roberson 	return (error);
1857ace8398dSJeff Roberson }
1858ace8398dSJeff Roberson 
1859ace8398dSJeff Roberson void
1860ace8398dSJeff Roberson seltdfini(struct thread *td)
1861ace8398dSJeff Roberson {
1862ace8398dSJeff Roberson 	struct seltd *stp;
1863ace8398dSJeff Roberson 
1864ace8398dSJeff Roberson 	stp = td->td_sel;
1865ace8398dSJeff Roberson 	if (stp == NULL)
1866ace8398dSJeff Roberson 		return;
1867ace8398dSJeff Roberson 	if (stp->st_free1)
1868ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free1);
1869ace8398dSJeff Roberson 	if (stp->st_free2)
1870ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free2);
1871ace8398dSJeff Roberson 	td->td_sel = NULL;
1872ace8398dSJeff Roberson 	free(stp, M_SELECT);
1873ace8398dSJeff Roberson }
1874ace8398dSJeff Roberson 
1875ace8398dSJeff Roberson /*
1876ace8398dSJeff Roberson  * Remove the references to the thread from all of the objects we were
1877ace8398dSJeff Roberson  * polling.
1878ace8398dSJeff Roberson  */
1879ace8398dSJeff Roberson static void
1880ace8398dSJeff Roberson seltdclear(struct thread *td)
1881ace8398dSJeff Roberson {
1882ace8398dSJeff Roberson 	struct seltd *stp;
1883ace8398dSJeff Roberson 	struct selfd *sfp;
1884ace8398dSJeff Roberson 	struct selfd *sfn;
1885ace8398dSJeff Roberson 
1886ace8398dSJeff Roberson 	stp = td->td_sel;
1887ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
1888ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1889ace8398dSJeff Roberson 	stp->st_flags = 0;
1890df8bae1dSRodney W. Grimes }
1891265fc98fSSeigo Tanimura 
18924d77a549SAlfred Perlstein static void selectinit(void *);
1893ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
1894265fc98fSSeigo Tanimura static void
1895ace8398dSJeff Roberson selectinit(void *dummy __unused)
1896265fc98fSSeigo Tanimura {
18972141453eSJeff Roberson 
1898ace8398dSJeff Roberson 	selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL,
1899ace8398dSJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, 0);
19002141453eSJeff Roberson 	mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);
1901265fc98fSSeigo Tanimura }
1902