xref: /freebsd/sys/kern/sys_generic.c (revision fcb5b3a41928b95eb24f618b58e865c3405d1562)
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 
7850ae6690SHans Petter Selasky /*
7950ae6690SHans Petter Selasky  * The following macro defines how many bytes will be allocated from
8050ae6690SHans Petter Selasky  * the stack instead of memory allocated when passing the IOCTL data
8150ae6690SHans Petter Selasky  * structures from userspace and to the kernel. Some IOCTLs having
8250ae6690SHans Petter Selasky  * small data structures are used very frequently and this small
8350ae6690SHans Petter Selasky  * buffer on the stack gives a significant speedup improvement for
8450ae6690SHans Petter Selasky  * those requests. The value of this define should be greater or equal
8550ae6690SHans Petter Selasky  * to 64 bytes and should also be power of two. The data structure is
8650ae6690SHans Petter Selasky  * currently hard-aligned to a 8-byte boundary on the stack. This
8750ae6690SHans Petter Selasky  * should currently be sufficient for all supported platforms.
8850ae6690SHans Petter Selasky  */
8950ae6690SHans Petter Selasky #define	SYS_IOCTL_SMALL_SIZE	128	/* bytes */
9050ae6690SHans Petter Selasky #define	SYS_IOCTL_SMALL_ALIGN	8	/* bytes */
9150ae6690SHans 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. */
156*fcb5b3a4SKonstantin Belousov 	u_int			sf_refs;
157ace8398dSJeff Roberson };
158ace8398dSJeff Roberson 
159ace8398dSJeff Roberson static uma_zone_t selfd_zone;
1602141453eSJeff Roberson static struct mtx_pool *mtxpool_select;
1618fe387abSDmitrij Tejblum 
162d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
163df8bae1dSRodney W. Grimes struct read_args {
164df8bae1dSRodney W. Grimes 	int	fd;
165134e06feSBruce Evans 	void	*buf;
166134e06feSBruce Evans 	size_t	nbyte;
167df8bae1dSRodney W. Grimes };
168d2d3e875SBruce Evans #endif
16926f9a767SRodney W. Grimes int
1708451d0ddSKip Macy sys_read(td, uap)
171b40ce416SJulian Elischer 	struct thread *td;
172b064d43dSMatthew Dillon 	struct read_args *uap;
173df8bae1dSRodney W. Grimes {
174bcd9e0ddSJohn Baldwin 	struct uio auio;
175bcd9e0ddSJohn Baldwin 	struct iovec aiov;
176279d7226SMatthew Dillon 	int error;
177df8bae1dSRodney W. Grimes 
178526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
179bcd9e0ddSJohn Baldwin 		return (EINVAL);
180bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
181bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
182bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
183bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
184bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
185bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
186bcd9e0ddSJohn Baldwin 	error = kern_readv(td, uap->fd, &auio);
187279d7226SMatthew Dillon 	return(error);
188df8bae1dSRodney W. Grimes }
189df8bae1dSRodney W. Grimes 
190df8bae1dSRodney W. Grimes /*
191bcd9e0ddSJohn Baldwin  * Positioned read system call
1924160ccd9SAlan Cox  */
1934160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
1944160ccd9SAlan Cox struct pread_args {
1954160ccd9SAlan Cox 	int	fd;
1964160ccd9SAlan Cox 	void	*buf;
1974160ccd9SAlan Cox 	size_t	nbyte;
1988fe387abSDmitrij Tejblum 	int	pad;
1994160ccd9SAlan Cox 	off_t	offset;
2004160ccd9SAlan Cox };
2014160ccd9SAlan Cox #endif
2024160ccd9SAlan Cox int
2038451d0ddSKip Macy sys_pread(td, uap)
204b40ce416SJulian Elischer 	struct thread *td;
205b064d43dSMatthew Dillon 	struct pread_args *uap;
2064160ccd9SAlan Cox {
2074160ccd9SAlan Cox 	struct uio auio;
2084160ccd9SAlan Cox 	struct iovec aiov;
209bcd9e0ddSJohn Baldwin 	int error;
2104160ccd9SAlan Cox 
211526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
212bcd9e0ddSJohn Baldwin 		return (EINVAL);
213bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
214bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
2154160ccd9SAlan Cox 	auio.uio_iov = &aiov;
2164160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
217bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
2184160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
219bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, &auio, uap->offset);
2204160ccd9SAlan Cox 	return(error);
2214160ccd9SAlan Cox }
2224160ccd9SAlan Cox 
2230538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6)
224c2815ad5SPeter Wemm int
225c2815ad5SPeter Wemm freebsd6_pread(td, uap)
226c2815ad5SPeter Wemm 	struct thread *td;
227c2815ad5SPeter Wemm 	struct freebsd6_pread_args *uap;
228c2815ad5SPeter Wemm {
229c2815ad5SPeter Wemm 	struct pread_args oargs;
230c2815ad5SPeter Wemm 
231c2815ad5SPeter Wemm 	oargs.fd = uap->fd;
232c2815ad5SPeter Wemm 	oargs.buf = uap->buf;
233c2815ad5SPeter Wemm 	oargs.nbyte = uap->nbyte;
234c2815ad5SPeter Wemm 	oargs.offset = uap->offset;
2358451d0ddSKip Macy 	return (sys_pread(td, &oargs));
236c2815ad5SPeter Wemm }
2370538aafcSKonstantin Belousov #endif
238c2815ad5SPeter Wemm 
2394160ccd9SAlan Cox /*
240df8bae1dSRodney W. Grimes  * Scatter read system call.
241df8bae1dSRodney W. Grimes  */
242d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
243df8bae1dSRodney W. Grimes struct readv_args {
2447147b19dSBruce Evans 	int	fd;
245df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
246df8bae1dSRodney W. Grimes 	u_int	iovcnt;
247df8bae1dSRodney W. Grimes };
248d2d3e875SBruce Evans #endif
24926f9a767SRodney W. Grimes int
2508451d0ddSKip Macy sys_readv(struct thread *td, struct readv_args *uap)
251df8bae1dSRodney W. Grimes {
252b88ec951SJohn Baldwin 	struct uio *auio;
253b88ec951SJohn Baldwin 	int error;
254b88ec951SJohn Baldwin 
255b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
256b88ec951SJohn Baldwin 	if (error)
257b88ec951SJohn Baldwin 		return (error);
258b88ec951SJohn Baldwin 	error = kern_readv(td, uap->fd, auio);
259b88ec951SJohn Baldwin 	free(auio, M_IOV);
260b88ec951SJohn Baldwin 	return (error);
261b88ec951SJohn Baldwin }
262b88ec951SJohn Baldwin 
263b88ec951SJohn Baldwin int
264b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio)
265b88ec951SJohn Baldwin {
266b064d43dSMatthew Dillon 	struct file *fp;
2677008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
268bcd9e0ddSJohn Baldwin 	int error;
269bcd9e0ddSJohn Baldwin 
2707008be5bSPawel Jakub Dawidek 	error = fget_read(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
271bcd9e0ddSJohn Baldwin 	if (error)
272bcd9e0ddSJohn Baldwin 		return (error);
273bcd9e0ddSJohn Baldwin 	error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
274bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
275bcd9e0ddSJohn Baldwin 	return (error);
276bcd9e0ddSJohn Baldwin }
277bcd9e0ddSJohn Baldwin 
278bcd9e0ddSJohn Baldwin /*
279bcd9e0ddSJohn Baldwin  * Scatter positioned read system call.
280bcd9e0ddSJohn Baldwin  */
281bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
282bcd9e0ddSJohn Baldwin struct preadv_args {
283bcd9e0ddSJohn Baldwin 	int	fd;
284bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
285bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
286bcd9e0ddSJohn Baldwin 	off_t	offset;
287bcd9e0ddSJohn Baldwin };
288bcd9e0ddSJohn Baldwin #endif
289bcd9e0ddSJohn Baldwin int
2908451d0ddSKip Macy sys_preadv(struct thread *td, struct preadv_args *uap)
291bcd9e0ddSJohn Baldwin {
292bcd9e0ddSJohn Baldwin 	struct uio *auio;
293bcd9e0ddSJohn Baldwin 	int error;
294bcd9e0ddSJohn Baldwin 
295bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
296bcd9e0ddSJohn Baldwin 	if (error)
297bcd9e0ddSJohn Baldwin 		return (error);
298bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, auio, uap->offset);
299bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
300bcd9e0ddSJohn Baldwin 	return (error);
301bcd9e0ddSJohn Baldwin }
302bcd9e0ddSJohn Baldwin 
303bcd9e0ddSJohn Baldwin int
304bcd9e0ddSJohn Baldwin kern_preadv(td, fd, auio, offset)
305bcd9e0ddSJohn Baldwin 	struct thread *td;
306bcd9e0ddSJohn Baldwin 	int fd;
307bcd9e0ddSJohn Baldwin 	struct uio *auio;
308bcd9e0ddSJohn Baldwin 	off_t offset;
309bcd9e0ddSJohn Baldwin {
310bcd9e0ddSJohn Baldwin 	struct file *fp;
3117008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
312bcd9e0ddSJohn Baldwin 	int error;
313bcd9e0ddSJohn Baldwin 
3147008be5bSPawel Jakub Dawidek 	error = fget_read(td, fd, cap_rights_init(&rights, CAP_PREAD), &fp);
315bcd9e0ddSJohn Baldwin 	if (error)
316bcd9e0ddSJohn Baldwin 		return (error);
317bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
318bcd9e0ddSJohn Baldwin 		error = ESPIPE;
319bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
320bcd9e0ddSJohn Baldwin 		error = EINVAL;
321bcd9e0ddSJohn Baldwin 	else
322bcd9e0ddSJohn Baldwin 		error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
323bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
324bcd9e0ddSJohn Baldwin 	return (error);
325bcd9e0ddSJohn Baldwin }
326bcd9e0ddSJohn Baldwin 
327bcd9e0ddSJohn Baldwin /*
328bcd9e0ddSJohn Baldwin  * Common code for readv and preadv that reads data in
329bcd9e0ddSJohn Baldwin  * from a file using the passed in uio, offset, and flags.
330bcd9e0ddSJohn Baldwin  */
331bcd9e0ddSJohn Baldwin static int
332bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags)
333bcd9e0ddSJohn Baldwin 	struct thread *td;
334bcd9e0ddSJohn Baldwin 	int fd;
335bcd9e0ddSJohn Baldwin 	struct file *fp;
336bcd9e0ddSJohn Baldwin 	struct uio *auio;
337bcd9e0ddSJohn Baldwin 	off_t offset;
338bcd9e0ddSJohn Baldwin 	int flags;
339bcd9e0ddSJohn Baldwin {
340bcd9e0ddSJohn Baldwin 	ssize_t cnt;
34182641acdSAlan Cox 	int error;
342df8bae1dSRodney W. Grimes #ifdef KTRACE
343552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
344df8bae1dSRodney W. Grimes #endif
345df8bae1dSRodney W. Grimes 
3464f8d23d6SPoul-Henning Kamp 	/* Finish zero length reads right here */
3474f8d23d6SPoul-Henning Kamp 	if (auio->uio_resid == 0) {
3484f8d23d6SPoul-Henning Kamp 		td->td_retval[0] = 0;
3494f8d23d6SPoul-Henning Kamp 		return(0);
3504f8d23d6SPoul-Henning Kamp 	}
351552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_READ;
352bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
353552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
354df8bae1dSRodney W. Grimes #ifdef KTRACE
355552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
356552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
357df8bae1dSRodney W. Grimes #endif
358552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
359bcd9e0ddSJohn Baldwin 	if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
360552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
361df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
362df8bae1dSRodney W. Grimes 			error = 0;
363279d7226SMatthew Dillon 	}
364552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
365df8bae1dSRodney W. Grimes #ifdef KTRACE
366552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
367552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
368b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_READ, ktruio, error);
369df8bae1dSRodney W. Grimes 	}
370df8bae1dSRodney W. Grimes #endif
371b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
372df8bae1dSRodney W. Grimes 	return (error);
373df8bae1dSRodney W. Grimes }
374df8bae1dSRodney W. Grimes 
375d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
376df8bae1dSRodney W. Grimes struct write_args {
377df8bae1dSRodney W. Grimes 	int	fd;
378134e06feSBruce Evans 	const void *buf;
379134e06feSBruce Evans 	size_t	nbyte;
380df8bae1dSRodney W. Grimes };
381d2d3e875SBruce Evans #endif
38226f9a767SRodney W. Grimes int
3838451d0ddSKip Macy sys_write(td, uap)
384b40ce416SJulian Elischer 	struct thread *td;
385b064d43dSMatthew Dillon 	struct write_args *uap;
386df8bae1dSRodney W. Grimes {
387bcd9e0ddSJohn Baldwin 	struct uio auio;
388bcd9e0ddSJohn Baldwin 	struct iovec aiov;
389279d7226SMatthew Dillon 	int error;
390df8bae1dSRodney W. Grimes 
391526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
392bcd9e0ddSJohn Baldwin 		return (EINVAL);
393bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
394bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
395bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
396bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
397bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
398bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
399bcd9e0ddSJohn Baldwin 	error = kern_writev(td, uap->fd, &auio);
400279d7226SMatthew Dillon 	return(error);
401df8bae1dSRodney W. Grimes }
402df8bae1dSRodney W. Grimes 
403df8bae1dSRodney W. Grimes /*
4040c14ff0eSRobert Watson  * Positioned write system call.
4054160ccd9SAlan Cox  */
4064160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
4074160ccd9SAlan Cox struct pwrite_args {
4084160ccd9SAlan Cox 	int	fd;
4094160ccd9SAlan Cox 	const void *buf;
4104160ccd9SAlan Cox 	size_t	nbyte;
4118fe387abSDmitrij Tejblum 	int	pad;
4124160ccd9SAlan Cox 	off_t	offset;
4134160ccd9SAlan Cox };
4144160ccd9SAlan Cox #endif
4154160ccd9SAlan Cox int
4168451d0ddSKip Macy sys_pwrite(td, uap)
417b40ce416SJulian Elischer 	struct thread *td;
418b064d43dSMatthew Dillon 	struct pwrite_args *uap;
4194160ccd9SAlan Cox {
4204160ccd9SAlan Cox 	struct uio auio;
4214160ccd9SAlan Cox 	struct iovec aiov;
422bcd9e0ddSJohn Baldwin 	int error;
4234160ccd9SAlan Cox 
424526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
425bcd9e0ddSJohn Baldwin 		return (EINVAL);
426bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
427bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
4284160ccd9SAlan Cox 	auio.uio_iov = &aiov;
4294160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
430bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
4314160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
432bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, &auio, uap->offset);
4334160ccd9SAlan Cox 	return(error);
4344160ccd9SAlan Cox }
4354160ccd9SAlan Cox 
4360538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6)
437c2815ad5SPeter Wemm int
438c2815ad5SPeter Wemm freebsd6_pwrite(td, uap)
439c2815ad5SPeter Wemm 	struct thread *td;
440c2815ad5SPeter Wemm 	struct freebsd6_pwrite_args *uap;
441c2815ad5SPeter Wemm {
442c2815ad5SPeter Wemm 	struct pwrite_args oargs;
443c2815ad5SPeter Wemm 
444c2815ad5SPeter Wemm 	oargs.fd = uap->fd;
445c2815ad5SPeter Wemm 	oargs.buf = uap->buf;
446c2815ad5SPeter Wemm 	oargs.nbyte = uap->nbyte;
447c2815ad5SPeter Wemm 	oargs.offset = uap->offset;
4488451d0ddSKip Macy 	return (sys_pwrite(td, &oargs));
449c2815ad5SPeter Wemm }
4500538aafcSKonstantin Belousov #endif
451c2815ad5SPeter Wemm 
4524160ccd9SAlan Cox /*
4530c14ff0eSRobert Watson  * Gather write system call.
454df8bae1dSRodney W. Grimes  */
455d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
456df8bae1dSRodney W. Grimes struct writev_args {
457df8bae1dSRodney W. Grimes 	int	fd;
458df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
459df8bae1dSRodney W. Grimes 	u_int	iovcnt;
460df8bae1dSRodney W. Grimes };
461d2d3e875SBruce Evans #endif
46226f9a767SRodney W. Grimes int
4638451d0ddSKip Macy sys_writev(struct thread *td, struct writev_args *uap)
464df8bae1dSRodney W. Grimes {
465b88ec951SJohn Baldwin 	struct uio *auio;
466b88ec951SJohn Baldwin 	int error;
467b88ec951SJohn Baldwin 
468b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
469b88ec951SJohn Baldwin 	if (error)
470b88ec951SJohn Baldwin 		return (error);
471b88ec951SJohn Baldwin 	error = kern_writev(td, uap->fd, auio);
472b88ec951SJohn Baldwin 	free(auio, M_IOV);
473b88ec951SJohn Baldwin 	return (error);
474b88ec951SJohn Baldwin }
475b88ec951SJohn Baldwin 
476b88ec951SJohn Baldwin int
477b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio)
478b88ec951SJohn Baldwin {
479b064d43dSMatthew Dillon 	struct file *fp;
4807008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
481bcd9e0ddSJohn Baldwin 	int error;
482bcd9e0ddSJohn Baldwin 
4837008be5bSPawel Jakub Dawidek 	error = fget_write(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
484bcd9e0ddSJohn Baldwin 	if (error)
485af56abaaSJohn Baldwin 		return (error);
486bcd9e0ddSJohn Baldwin 	error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
487bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
488bcd9e0ddSJohn Baldwin 	return (error);
489bcd9e0ddSJohn Baldwin }
490bcd9e0ddSJohn Baldwin 
491bcd9e0ddSJohn Baldwin /*
4920c14ff0eSRobert Watson  * Gather positioned write system call.
493bcd9e0ddSJohn Baldwin  */
494bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
495bcd9e0ddSJohn Baldwin struct pwritev_args {
496bcd9e0ddSJohn Baldwin 	int	fd;
497bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
498bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
499bcd9e0ddSJohn Baldwin 	off_t	offset;
500bcd9e0ddSJohn Baldwin };
501bcd9e0ddSJohn Baldwin #endif
502bcd9e0ddSJohn Baldwin int
5038451d0ddSKip Macy sys_pwritev(struct thread *td, struct pwritev_args *uap)
504bcd9e0ddSJohn Baldwin {
505bcd9e0ddSJohn Baldwin 	struct uio *auio;
506bcd9e0ddSJohn Baldwin 	int error;
507bcd9e0ddSJohn Baldwin 
508bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
509bcd9e0ddSJohn Baldwin 	if (error)
510bcd9e0ddSJohn Baldwin 		return (error);
511bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, auio, uap->offset);
512bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
513bcd9e0ddSJohn Baldwin 	return (error);
514bcd9e0ddSJohn Baldwin }
515bcd9e0ddSJohn Baldwin 
516bcd9e0ddSJohn Baldwin int
517bcd9e0ddSJohn Baldwin kern_pwritev(td, fd, auio, offset)
518bcd9e0ddSJohn Baldwin 	struct thread *td;
519bcd9e0ddSJohn Baldwin 	struct uio *auio;
520bcd9e0ddSJohn Baldwin 	int fd;
521bcd9e0ddSJohn Baldwin 	off_t offset;
522bcd9e0ddSJohn Baldwin {
523bcd9e0ddSJohn Baldwin 	struct file *fp;
5247008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
525bcd9e0ddSJohn Baldwin 	int error;
526bcd9e0ddSJohn Baldwin 
5277008be5bSPawel Jakub Dawidek 	error = fget_write(td, fd, cap_rights_init(&rights, CAP_PWRITE), &fp);
528bcd9e0ddSJohn Baldwin 	if (error)
529af56abaaSJohn Baldwin 		return (error);
530bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
531bcd9e0ddSJohn Baldwin 		error = ESPIPE;
532bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
533bcd9e0ddSJohn Baldwin 		error = EINVAL;
534bcd9e0ddSJohn Baldwin 	else
535bcd9e0ddSJohn Baldwin 		error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
536bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
537bcd9e0ddSJohn Baldwin 	return (error);
538bcd9e0ddSJohn Baldwin }
539bcd9e0ddSJohn Baldwin 
540bcd9e0ddSJohn Baldwin /*
541bcd9e0ddSJohn Baldwin  * Common code for writev and pwritev that writes data to
542bcd9e0ddSJohn Baldwin  * a file using the passed in uio, offset, and flags.
543bcd9e0ddSJohn Baldwin  */
544bcd9e0ddSJohn Baldwin static int
545bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags)
546bcd9e0ddSJohn Baldwin 	struct thread *td;
547bcd9e0ddSJohn Baldwin 	int fd;
548bcd9e0ddSJohn Baldwin 	struct file *fp;
549bcd9e0ddSJohn Baldwin 	struct uio *auio;
550bcd9e0ddSJohn Baldwin 	off_t offset;
551bcd9e0ddSJohn Baldwin 	int flags;
552bcd9e0ddSJohn Baldwin {
553bcd9e0ddSJohn Baldwin 	ssize_t cnt;
554552afd9cSPoul-Henning Kamp 	int error;
555df8bae1dSRodney W. Grimes #ifdef KTRACE
556552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
557df8bae1dSRodney W. Grimes #endif
558df8bae1dSRodney W. Grimes 
559552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_WRITE;
560552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
561bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
562df8bae1dSRodney W. Grimes #ifdef KTRACE
563552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
564552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
565df8bae1dSRodney W. Grimes #endif
566552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
567ad9789f6SKonstantin Belousov 	if (fp->f_type == DTYPE_VNODE &&
568ad9789f6SKonstantin Belousov 	    (fp->f_vnread_flags & FDEVFS_VNODE) == 0)
5699440653dSMatthew Dillon 		bwillwrite();
570bcd9e0ddSJohn Baldwin 	if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
571552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
572df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
573df8bae1dSRodney W. Grimes 			error = 0;
574bcd9e0ddSJohn Baldwin 		/* Socket layer is responsible for issuing SIGPIPE. */
5756f7ca813SBruce M Simpson 		if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
576b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
5777a6f3d78SJohn Baldwin 			tdsignal(td, SIGPIPE);
578b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
57919eb87d2SJohn Baldwin 		}
580df8bae1dSRodney W. Grimes 	}
581552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
582df8bae1dSRodney W. Grimes #ifdef KTRACE
583552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
584552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
585b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_WRITE, ktruio, error);
586df8bae1dSRodney W. Grimes 	}
587df8bae1dSRodney W. Grimes #endif
588b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
589df8bae1dSRodney W. Grimes 	return (error);
590df8bae1dSRodney W. Grimes }
591df8bae1dSRodney W. Grimes 
592e4650294SJohn Baldwin /*
593e4650294SJohn Baldwin  * Truncate a file given a file descriptor.
594e4650294SJohn Baldwin  *
595e4650294SJohn Baldwin  * Can't use fget_write() here, since must return EINVAL and not EBADF if the
596e4650294SJohn Baldwin  * descriptor isn't writable.
597e4650294SJohn Baldwin  */
598e4650294SJohn Baldwin int
599e4650294SJohn Baldwin kern_ftruncate(td, fd, length)
600e4650294SJohn Baldwin 	struct thread *td;
601e4650294SJohn Baldwin 	int fd;
602e4650294SJohn Baldwin 	off_t length;
603e4650294SJohn Baldwin {
604e4650294SJohn Baldwin 	struct file *fp;
6057008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
606e4650294SJohn Baldwin 	int error;
607e4650294SJohn Baldwin 
60814961ba7SRobert Watson 	AUDIT_ARG_FD(fd);
609e4650294SJohn Baldwin 	if (length < 0)
610e4650294SJohn Baldwin 		return (EINVAL);
6117008be5bSPawel Jakub Dawidek 	error = fget(td, fd, cap_rights_init(&rights, CAP_FTRUNCATE), &fp);
612e4650294SJohn Baldwin 	if (error)
613e4650294SJohn Baldwin 		return (error);
61414961ba7SRobert Watson 	AUDIT_ARG_FILE(td->td_proc, fp);
615e4650294SJohn Baldwin 	if (!(fp->f_flag & FWRITE)) {
616e4650294SJohn Baldwin 		fdrop(fp, td);
617e4650294SJohn Baldwin 		return (EINVAL);
618e4650294SJohn Baldwin 	}
619e4650294SJohn Baldwin 	error = fo_truncate(fp, length, td->td_ucred, td);
620e4650294SJohn Baldwin 	fdrop(fp, td);
621e4650294SJohn Baldwin 	return (error);
622e4650294SJohn Baldwin }
623e4650294SJohn Baldwin 
624e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
625e4650294SJohn Baldwin struct ftruncate_args {
626e4650294SJohn Baldwin 	int	fd;
627e4650294SJohn Baldwin 	int	pad;
628e4650294SJohn Baldwin 	off_t	length;
629e4650294SJohn Baldwin };
630e4650294SJohn Baldwin #endif
631e4650294SJohn Baldwin int
6328451d0ddSKip Macy sys_ftruncate(td, uap)
633e4650294SJohn Baldwin 	struct thread *td;
634e4650294SJohn Baldwin 	struct ftruncate_args *uap;
635e4650294SJohn Baldwin {
636e4650294SJohn Baldwin 
637e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
638e4650294SJohn Baldwin }
639e4650294SJohn Baldwin 
640e4650294SJohn Baldwin #if defined(COMPAT_43)
641e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
642e4650294SJohn Baldwin struct oftruncate_args {
643e4650294SJohn Baldwin 	int	fd;
644e4650294SJohn Baldwin 	long	length;
645e4650294SJohn Baldwin };
646e4650294SJohn Baldwin #endif
647e4650294SJohn Baldwin int
648e4650294SJohn Baldwin oftruncate(td, uap)
649e4650294SJohn Baldwin 	struct thread *td;
650e4650294SJohn Baldwin 	struct oftruncate_args *uap;
651e4650294SJohn Baldwin {
652e4650294SJohn Baldwin 
653e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
654e4650294SJohn Baldwin }
655e4650294SJohn Baldwin #endif /* COMPAT_43 */
656e4650294SJohn Baldwin 
657d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
658df8bae1dSRodney W. Grimes struct ioctl_args {
659df8bae1dSRodney W. Grimes 	int	fd;
660069e9bc1SDoug Rabson 	u_long	com;
661df8bae1dSRodney W. Grimes 	caddr_t	data;
662df8bae1dSRodney W. Grimes };
663d2d3e875SBruce Evans #endif
664df8bae1dSRodney W. Grimes /* ARGSUSED */
66526f9a767SRodney W. Grimes int
6668451d0ddSKip Macy sys_ioctl(struct thread *td, struct ioctl_args *uap)
667df8bae1dSRodney W. Grimes {
66850ae6690SHans Petter Selasky 	u_char smalldata[SYS_IOCTL_SMALL_SIZE] __aligned(SYS_IOCTL_SMALL_ALIGN);
6693e15c66fSPoul-Henning Kamp 	u_long com;
6709fddcc66SRuslan Ermilov 	int arg, error;
6713e15c66fSPoul-Henning Kamp 	u_int size;
6729fddcc66SRuslan Ermilov 	caddr_t data;
673df8bae1dSRodney W. Grimes 
6749fc6aa06SPoul-Henning Kamp 	if (uap->com > 0xffffffff) {
6759fc6aa06SPoul-Henning Kamp 		printf(
6769fc6aa06SPoul-Henning Kamp 		    "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
677431f8906SJulian Elischer 		    td->td_proc->p_pid, td->td_name, uap->com);
6789fc6aa06SPoul-Henning Kamp 		uap->com &= 0xffffffff;
6799fc6aa06SPoul-Henning Kamp 	}
680d9f46233SJohn Baldwin 	com = uap->com;
681df8bae1dSRodney W. Grimes 
682df8bae1dSRodney W. Grimes 	/*
683df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
684df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
685df8bae1dSRodney W. Grimes 	 */
686df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
687ca51b19bSPoul-Henning Kamp 	if ((size > IOCPARM_MAX) ||
688ca51b19bSPoul-Henning Kamp 	    ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
6892de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
6902de92a38SPeter Wemm 	    ((com & IOC_OUT) && size == 0) ||
6912de92a38SPeter Wemm #else
6922de92a38SPeter Wemm 	    ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
6932de92a38SPeter Wemm #endif
6949fddcc66SRuslan Ermilov 	    ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
695426da3bcSAlfred Perlstein 		return (ENOTTY);
696279d7226SMatthew Dillon 
697ca51b19bSPoul-Henning Kamp 	if (size > 0) {
698715457f6SDavid E. O'Brien 		if (com & IOC_VOID) {
6999fddcc66SRuslan Ermilov 			/* Integer argument. */
7009fddcc66SRuslan Ermilov 			arg = (intptr_t)uap->data;
7019fddcc66SRuslan Ermilov 			data = (void *)&arg;
7029fddcc66SRuslan Ermilov 			size = 0;
7035cbf44bfSMateusz Guzik 		} else {
7040ecd606bSHans Petter Selasky 			if (size > SYS_IOCTL_SMALL_SIZE)
705715457f6SDavid E. O'Brien 				data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
7060ecd606bSHans Petter Selasky 			else
7070ecd606bSHans Petter Selasky 				data = smalldata;
7085cbf44bfSMateusz Guzik 		}
7099fddcc66SRuslan Ermilov 	} else
7109fddcc66SRuslan Ermilov 		data = (void *)&uap->data;
711df8bae1dSRodney W. Grimes 	if (com & IOC_IN) {
712df8bae1dSRodney W. Grimes 		error = copyin(uap->data, data, (u_int)size);
7135cbf44bfSMateusz Guzik 		if (error != 0)
7145cbf44bfSMateusz Guzik 			goto out;
715ca51b19bSPoul-Henning Kamp 	} else if (com & IOC_OUT) {
716df8bae1dSRodney W. Grimes 		/*
717df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
718df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
719df8bae1dSRodney W. Grimes 		 */
720df8bae1dSRodney W. Grimes 		bzero(data, size);
721279d7226SMatthew Dillon 	}
722df8bae1dSRodney W. Grimes 
723d9f46233SJohn Baldwin 	error = kern_ioctl(td, uap->fd, com, data);
724d9f46233SJohn Baldwin 
725d9f46233SJohn Baldwin 	if (error == 0 && (com & IOC_OUT))
726d9f46233SJohn Baldwin 		error = copyout(data, uap->data, (u_int)size);
727d9f46233SJohn Baldwin 
7285cbf44bfSMateusz Guzik out:
7290ecd606bSHans Petter Selasky 	if (size > SYS_IOCTL_SMALL_SIZE)
7309fddcc66SRuslan Ermilov 		free(data, M_IOCTLOPS);
731d9f46233SJohn Baldwin 	return (error);
732d9f46233SJohn Baldwin }
733d9f46233SJohn Baldwin 
734d9f46233SJohn Baldwin int
735d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
736d9f46233SJohn Baldwin {
737d9f46233SJohn Baldwin 	struct file *fp;
738d9f46233SJohn Baldwin 	struct filedesc *fdp;
7397008be5bSPawel Jakub Dawidek #ifndef CAPABILITIES
7407008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
7417008be5bSPawel Jakub Dawidek #endif
7422609222aSPawel Jakub Dawidek 	int error, tmp, locked;
743d9f46233SJohn Baldwin 
74464c9a4d9SRobert Watson 	AUDIT_ARG_FD(fd);
74564c9a4d9SRobert Watson 	AUDIT_ARG_CMD(com);
7462609222aSPawel Jakub Dawidek 
747d9f46233SJohn Baldwin 	fdp = td->td_proc->p_fd;
7482609222aSPawel Jakub Dawidek 
749d9f46233SJohn Baldwin 	switch (com) {
750d9f46233SJohn Baldwin 	case FIONCLEX:
751d9f46233SJohn Baldwin 	case FIOCLEX:
7525e3f7694SRobert Watson 		FILEDESC_XLOCK(fdp);
7532609222aSPawel Jakub Dawidek 		locked = LA_XLOCKED;
7542609222aSPawel Jakub Dawidek 		break;
7552609222aSPawel Jakub Dawidek 	default:
7562609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
7572609222aSPawel Jakub Dawidek 		FILEDESC_SLOCK(fdp);
7582609222aSPawel Jakub Dawidek 		locked = LA_SLOCKED;
7592609222aSPawel Jakub Dawidek #else
7602609222aSPawel Jakub Dawidek 		locked = LA_UNLOCKED;
7612609222aSPawel Jakub Dawidek #endif
7622609222aSPawel Jakub Dawidek 		break;
7632609222aSPawel Jakub Dawidek 	}
7642609222aSPawel Jakub Dawidek 
7652609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
7662609222aSPawel Jakub Dawidek 	if ((fp = fget_locked(fdp, fd)) == NULL) {
7672609222aSPawel Jakub Dawidek 		error = EBADF;
7682609222aSPawel Jakub Dawidek 		goto out;
7692609222aSPawel Jakub Dawidek 	}
7702609222aSPawel Jakub Dawidek 	if ((error = cap_ioctl_check(fdp, fd, com)) != 0) {
7712609222aSPawel Jakub Dawidek 		fp = NULL;	/* fhold() was not called yet */
7722609222aSPawel Jakub Dawidek 		goto out;
7732609222aSPawel Jakub Dawidek 	}
7742609222aSPawel Jakub Dawidek 	fhold(fp);
7752609222aSPawel Jakub Dawidek 	if (locked == LA_SLOCKED) {
7762609222aSPawel Jakub Dawidek 		FILEDESC_SUNLOCK(fdp);
7772609222aSPawel Jakub Dawidek 		locked = LA_UNLOCKED;
7782609222aSPawel Jakub Dawidek 	}
7792609222aSPawel Jakub Dawidek #else
7807008be5bSPawel Jakub Dawidek 	error = fget(td, fd, cap_rights_init(&rights, CAP_IOCTL), &fp);
7817008be5bSPawel Jakub Dawidek 	if (error != 0) {
7822609222aSPawel Jakub Dawidek 		fp = NULL;
7832609222aSPawel Jakub Dawidek 		goto out;
7842609222aSPawel Jakub Dawidek 	}
7852609222aSPawel Jakub Dawidek #endif
7862609222aSPawel Jakub Dawidek 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
7872609222aSPawel Jakub Dawidek 		error = EBADF;
7882609222aSPawel Jakub Dawidek 		goto out;
7892609222aSPawel Jakub Dawidek 	}
7902609222aSPawel Jakub Dawidek 
7912609222aSPawel Jakub Dawidek 	switch (com) {
7922609222aSPawel Jakub Dawidek 	case FIONCLEX:
7932609222aSPawel Jakub Dawidek 		fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE;
7942609222aSPawel Jakub Dawidek 		goto out;
7952609222aSPawel Jakub Dawidek 	case FIOCLEX:
7962609222aSPawel Jakub Dawidek 		fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE;
797d9f46233SJohn Baldwin 		goto out;
798d9f46233SJohn Baldwin 	case FIONBIO:
799bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
800397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FNONBLOCK);
801df8bae1dSRodney W. Grimes 		else
802397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FNONBLOCK);
8038ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
804d9f46233SJohn Baldwin 		break;
805d9f46233SJohn Baldwin 	case FIOASYNC:
806bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
807397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FASYNC);
808df8bae1dSRodney W. Grimes 		else
809397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FASYNC);
8108ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
811d9f46233SJohn Baldwin 		break;
812df8bae1dSRodney W. Grimes 	}
8138ccf264fSPoul-Henning Kamp 
8148ccf264fSPoul-Henning Kamp 	error = fo_ioctl(fp, com, data, td->td_ucred, td);
815d9f46233SJohn Baldwin out:
8162609222aSPawel Jakub Dawidek 	switch (locked) {
8172609222aSPawel Jakub Dawidek 	case LA_XLOCKED:
8182609222aSPawel Jakub Dawidek 		FILEDESC_XUNLOCK(fdp);
8192609222aSPawel Jakub Dawidek 		break;
8202609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
8212609222aSPawel Jakub Dawidek 	case LA_SLOCKED:
8222609222aSPawel Jakub Dawidek 		FILEDESC_SUNLOCK(fdp);
8232609222aSPawel Jakub Dawidek 		break;
8242609222aSPawel Jakub Dawidek #endif
8252609222aSPawel Jakub Dawidek 	default:
8262609222aSPawel Jakub Dawidek 		FILEDESC_UNLOCK_ASSERT(fdp);
8272609222aSPawel Jakub Dawidek 		break;
8282609222aSPawel Jakub Dawidek 	}
8292609222aSPawel Jakub Dawidek 	if (fp != NULL)
830b40ce416SJulian Elischer 		fdrop(fp, td);
831df8bae1dSRodney W. Grimes 	return (error);
832df8bae1dSRodney W. Grimes }
833df8bae1dSRodney W. Grimes 
834125dcf8cSKonstantin Belousov int
835125dcf8cSKonstantin Belousov poll_no_poll(int events)
836125dcf8cSKonstantin Belousov {
837125dcf8cSKonstantin Belousov 	/*
838125dcf8cSKonstantin Belousov 	 * Return true for read/write.  If the user asked for something
839125dcf8cSKonstantin Belousov 	 * special, return POLLNVAL, so that clients have a way of
840125dcf8cSKonstantin Belousov 	 * determining reliably whether or not the extended
841125dcf8cSKonstantin Belousov 	 * functionality is present without hard-coding knowledge
842125dcf8cSKonstantin Belousov 	 * of specific filesystem implementations.
843125dcf8cSKonstantin Belousov 	 */
844125dcf8cSKonstantin Belousov 	if (events & ~POLLSTANDARD)
845125dcf8cSKonstantin Belousov 		return (POLLNVAL);
846125dcf8cSKonstantin Belousov 
847125dcf8cSKonstantin Belousov 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
848125dcf8cSKonstantin Belousov }
849125dcf8cSKonstantin Belousov 
850066d836bSKonstantin Belousov int
8518451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap)
852066d836bSKonstantin Belousov {
853066d836bSKonstantin Belousov 	struct timespec ts;
854066d836bSKonstantin Belousov 	struct timeval tv, *tvp;
855066d836bSKonstantin Belousov 	sigset_t set, *uset;
856066d836bSKonstantin Belousov 	int error;
857066d836bSKonstantin Belousov 
858066d836bSKonstantin Belousov 	if (uap->ts != NULL) {
859066d836bSKonstantin Belousov 		error = copyin(uap->ts, &ts, sizeof(ts));
860066d836bSKonstantin Belousov 		if (error != 0)
861066d836bSKonstantin Belousov 		    return (error);
862066d836bSKonstantin Belousov 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
863066d836bSKonstantin Belousov 		tvp = &tv;
864066d836bSKonstantin Belousov 	} else
865066d836bSKonstantin Belousov 		tvp = NULL;
866066d836bSKonstantin Belousov 	if (uap->sm != NULL) {
867066d836bSKonstantin Belousov 		error = copyin(uap->sm, &set, sizeof(set));
868066d836bSKonstantin Belousov 		if (error != 0)
869066d836bSKonstantin Belousov 			return (error);
870066d836bSKonstantin Belousov 		uset = &set;
871066d836bSKonstantin Belousov 	} else
872066d836bSKonstantin Belousov 		uset = NULL;
873066d836bSKonstantin Belousov 	return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
874066d836bSKonstantin Belousov 	    uset, NFDBITS));
875066d836bSKonstantin Belousov }
876066d836bSKonstantin Belousov 
877066d836bSKonstantin Belousov int
878066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
879066d836bSKonstantin Belousov     struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
880066d836bSKonstantin Belousov {
881066d836bSKonstantin Belousov 	int error;
882066d836bSKonstantin Belousov 
883066d836bSKonstantin Belousov 	if (uset != NULL) {
884066d836bSKonstantin Belousov 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
885066d836bSKonstantin Belousov 		    &td->td_oldsigmask, 0);
886066d836bSKonstantin Belousov 		if (error != 0)
887066d836bSKonstantin Belousov 			return (error);
888066d836bSKonstantin Belousov 		td->td_pflags |= TDP_OLDMASK;
889066d836bSKonstantin Belousov 		/*
890066d836bSKonstantin Belousov 		 * Make sure that ast() is called on return to
891066d836bSKonstantin Belousov 		 * usermode and TDP_OLDMASK is cleared, restoring old
892066d836bSKonstantin Belousov 		 * sigmask.
893066d836bSKonstantin Belousov 		 */
894066d836bSKonstantin Belousov 		thread_lock(td);
895066d836bSKonstantin Belousov 		td->td_flags |= TDF_ASTPENDING;
896066d836bSKonstantin Belousov 		thread_unlock(td);
897066d836bSKonstantin Belousov 	}
898066d836bSKonstantin Belousov 	error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
899066d836bSKonstantin Belousov 	return (error);
900066d836bSKonstantin Belousov }
901066d836bSKonstantin Belousov 
902d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
903df8bae1dSRodney W. Grimes struct select_args {
904b08f7993SSujal Patel 	int	nd;
905df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
906df8bae1dSRodney W. Grimes 	struct	timeval *tv;
907df8bae1dSRodney W. Grimes };
908d2d3e875SBruce Evans #endif
90926f9a767SRodney W. Grimes int
9108451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap)
911df8bae1dSRodney W. Grimes {
9128f19eb88SIan Dowse 	struct timeval tv, *tvp;
9138f19eb88SIan Dowse 	int error;
9148f19eb88SIan Dowse 
9158f19eb88SIan Dowse 	if (uap->tv != NULL) {
9168f19eb88SIan Dowse 		error = copyin(uap->tv, &tv, sizeof(tv));
9178f19eb88SIan Dowse 		if (error)
9188f19eb88SIan Dowse 			return (error);
9198f19eb88SIan Dowse 		tvp = &tv;
9208f19eb88SIan Dowse 	} else
9218f19eb88SIan Dowse 		tvp = NULL;
9228f19eb88SIan Dowse 
923b55ef216SKonstantin Belousov 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
924b55ef216SKonstantin Belousov 	    NFDBITS));
9258f19eb88SIan Dowse }
9268f19eb88SIan Dowse 
92756be1b9aSKonstantin Belousov /*
92856be1b9aSKonstantin Belousov  * In the unlikely case when user specified n greater then the last
92956be1b9aSKonstantin Belousov  * open file descriptor, check that no bits are set after the last
93056be1b9aSKonstantin Belousov  * valid fd.  We must return EBADF if any is set.
93156be1b9aSKonstantin Belousov  *
93256be1b9aSKonstantin Belousov  * There are applications that rely on the behaviour.
93356be1b9aSKonstantin Belousov  *
93456be1b9aSKonstantin Belousov  * nd is fd_lastfile + 1.
93556be1b9aSKonstantin Belousov  */
93656be1b9aSKonstantin Belousov static int
93756be1b9aSKonstantin Belousov select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits)
93856be1b9aSKonstantin Belousov {
93956be1b9aSKonstantin Belousov 	char *addr, *oaddr;
94056be1b9aSKonstantin Belousov 	int b, i, res;
94156be1b9aSKonstantin Belousov 	uint8_t bits;
94256be1b9aSKonstantin Belousov 
94356be1b9aSKonstantin Belousov 	if (nd >= ndu || fd_in == NULL)
94456be1b9aSKonstantin Belousov 		return (0);
94556be1b9aSKonstantin Belousov 
94656be1b9aSKonstantin Belousov 	oaddr = NULL;
94756be1b9aSKonstantin Belousov 	bits = 0; /* silence gcc */
94856be1b9aSKonstantin Belousov 	for (i = nd; i < ndu; i++) {
94956be1b9aSKonstantin Belousov 		b = i / NBBY;
95056be1b9aSKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN
95156be1b9aSKonstantin Belousov 		addr = (char *)fd_in + b;
95256be1b9aSKonstantin Belousov #else
95356be1b9aSKonstantin Belousov 		addr = (char *)fd_in;
95456be1b9aSKonstantin Belousov 		if (abi_nfdbits == NFDBITS) {
95556be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(fd_mask)) +
95656be1b9aSKonstantin Belousov 			    sizeof(fd_mask) - 1 - b % sizeof(fd_mask);
95756be1b9aSKonstantin Belousov 		} else {
95856be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(uint32_t)) +
95956be1b9aSKonstantin Belousov 			    sizeof(uint32_t) - 1 - b % sizeof(uint32_t);
96056be1b9aSKonstantin Belousov 		}
96156be1b9aSKonstantin Belousov #endif
96256be1b9aSKonstantin Belousov 		if (addr != oaddr) {
96356be1b9aSKonstantin Belousov 			res = fubyte(addr);
96456be1b9aSKonstantin Belousov 			if (res == -1)
96556be1b9aSKonstantin Belousov 				return (EFAULT);
96656be1b9aSKonstantin Belousov 			oaddr = addr;
96756be1b9aSKonstantin Belousov 			bits = res;
96856be1b9aSKonstantin Belousov 		}
96956be1b9aSKonstantin Belousov 		if ((bits & (1 << (i % NBBY))) != 0)
97056be1b9aSKonstantin Belousov 			return (EBADF);
97156be1b9aSKonstantin Belousov 	}
97256be1b9aSKonstantin Belousov 	return (0);
97356be1b9aSKonstantin Belousov }
97456be1b9aSKonstantin Belousov 
9758f19eb88SIan Dowse int
9768f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
977b55ef216SKonstantin Belousov     fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
9788f19eb88SIan Dowse {
979426da3bcSAlfred Perlstein 	struct filedesc *fdp;
980d5e4d7e1SBruce Evans 	/*
981d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
982d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
983d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
984d5e4d7e1SBruce Evans 	 * of 256.
985d5e4d7e1SBruce Evans 	 */
986d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
987eb209311SAlfred Perlstein 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
988cf5e4fe6SDavide Italiano 	struct timeval rtv;
989cf5e4fe6SDavide Italiano 	sbintime_t asbt, precision, rsbt;
990b55ef216SKonstantin Belousov 	u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
991cf5e4fe6SDavide Italiano 	int error, lf, ndu;
992df8bae1dSRodney W. Grimes 
9938f19eb88SIan Dowse 	if (nd < 0)
994acbfbfeaSSujal Patel 		return (EINVAL);
995426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
99656be1b9aSKonstantin Belousov 	ndu = nd;
99756be1b9aSKonstantin Belousov 	lf = fdp->fd_lastfile;
99856be1b9aSKonstantin Belousov 	if (nd > lf + 1)
99956be1b9aSKonstantin Belousov 		nd = lf + 1;
100056be1b9aSKonstantin Belousov 
100156be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits);
100256be1b9aSKonstantin Belousov 	if (error != 0)
100356be1b9aSKonstantin Belousov 		return (error);
100456be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits);
100556be1b9aSKonstantin Belousov 	if (error != 0)
100656be1b9aSKonstantin Belousov 		return (error);
100756be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits);
100856be1b9aSKonstantin Belousov 	if (error != 0)
100956be1b9aSKonstantin Belousov 		return (error);
1010b08f7993SSujal Patel 
1011d5e4d7e1SBruce Evans 	/*
1012d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
1013d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
1014d5e4d7e1SBruce Evans 	 */
10158f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
1016d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
1017b55ef216SKonstantin Belousov 	ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
1018d5e4d7e1SBruce Evans 	nbufbytes = 0;
10198f19eb88SIan Dowse 	if (fd_in != NULL)
1020d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
10218f19eb88SIan Dowse 	if (fd_ou != NULL)
1022d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
10238f19eb88SIan Dowse 	if (fd_ex != NULL)
1024d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
1025d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
1026d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
1027d5e4d7e1SBruce Evans 	else
1028a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
1029b08f7993SSujal Patel 
1030b08f7993SSujal Patel 	/*
1031d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
1032d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
1033d5e4d7e1SBruce Evans 	 * together.
1034b08f7993SSujal Patel 	 */
1035d5e4d7e1SBruce Evans 	sbp = selbits;
1036df8bae1dSRodney W. Grimes #define	getbits(name, x) \
1037d5e4d7e1SBruce Evans 	do {								\
1038841c0c7eSNathan Whitehorn 		if (name == NULL) {					\
1039d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
1040841c0c7eSNathan Whitehorn 			obits[x] = NULL;				\
1041841c0c7eSNathan Whitehorn 		} else {						\
1042d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
1043d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
1044d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
1045b55ef216SKonstantin Belousov 			error = copyin(name, ibits[x], ncpubytes);	\
1046265fc98fSSeigo Tanimura 			if (error != 0)					\
1047ace8398dSJeff Roberson 				goto done;				\
1048b55ef216SKonstantin Belousov 			bzero((char *)ibits[x] + ncpubytes,		\
1049b55ef216SKonstantin Belousov 			    ncpbytes - ncpubytes);			\
1050e04ac2feSJohn Baldwin 		}							\
1051d5e4d7e1SBruce Evans 	} while (0)
10528f19eb88SIan Dowse 	getbits(fd_in, 0);
10538f19eb88SIan Dowse 	getbits(fd_ou, 1);
10548f19eb88SIan Dowse 	getbits(fd_ex, 2);
1055df8bae1dSRodney W. Grimes #undef	getbits
1056841c0c7eSNathan Whitehorn 
1057841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
1058841c0c7eSNathan Whitehorn 	/*
1059841c0c7eSNathan Whitehorn 	 * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
1060841c0c7eSNathan Whitehorn 	 * we are running under 32-bit emulation. This should be more
1061841c0c7eSNathan Whitehorn 	 * generic.
1062841c0c7eSNathan Whitehorn 	 */
1063841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)						\
1064841c0c7eSNathan Whitehorn 	if (abi_nfdbits != NFDBITS && bits != NULL) {			\
1065841c0c7eSNathan Whitehorn 		int i;							\
1066841c0c7eSNathan Whitehorn 		for (i = 0; i < ncpbytes / sizeof *sbp; i++)		\
1067841c0c7eSNathan Whitehorn 			bits[i] = (bits[i] >> 32) | (bits[i] << 32);	\
1068841c0c7eSNathan Whitehorn 	}
1069841c0c7eSNathan Whitehorn #else
1070841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)
1071841c0c7eSNathan Whitehorn #endif
1072841c0c7eSNathan Whitehorn 
1073841c0c7eSNathan Whitehorn 	/* Make sure the bit order makes it through an ABI transition */
1074841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[0]);
1075841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[1]);
1076841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[2]);
1077841c0c7eSNathan Whitehorn 
1078d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
1079d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
1080df8bae1dSRodney W. Grimes 
1081cf5e4fe6SDavide Italiano 	precision = 0;
10828f19eb88SIan Dowse 	if (tvp != NULL) {
1083cf5e4fe6SDavide Italiano 		rtv = *tvp;
1084cf5e4fe6SDavide Italiano 		if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1085cf5e4fe6SDavide Italiano 		    rtv.tv_usec >= 1000000) {
1086df8bae1dSRodney W. Grimes 			error = EINVAL;
1087ace8398dSJeff Roberson 			goto done;
1088df8bae1dSRodney W. Grimes 		}
108921a37a71SAlexander Motin 		if (!timevalisset(&rtv))
1090980c545dSAlexander Motin 			asbt = 0;
109121a37a71SAlexander Motin 		else if (rtv.tv_sec <= INT32_MAX) {
1092cf5e4fe6SDavide Italiano 			rsbt = tvtosbt(rtv);
1093cf5e4fe6SDavide Italiano 			precision = rsbt;
1094cf5e4fe6SDavide Italiano 			precision >>= tc_precexp;
1095cf5e4fe6SDavide Italiano 			if (TIMESEL(&asbt, rsbt))
1096cf5e4fe6SDavide Italiano 				asbt += tc_tick_sbt;
10974bc38a5aSDavide Italiano 			if (asbt <= SBT_MAX - rsbt)
1098cf5e4fe6SDavide Italiano 				asbt += rsbt;
109921a37a71SAlexander Motin 			else
1100980c545dSAlexander Motin 				asbt = -1;
1101980c545dSAlexander Motin 		} else
1102980c545dSAlexander Motin 			asbt = -1;
1103cf5e4fe6SDavide Italiano 	} else
1104cf5e4fe6SDavide Italiano 		asbt = -1;
1105ace8398dSJeff Roberson 	seltdinit(td);
1106ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1107ace8398dSJeff Roberson 	for (;;) {
11088f19eb88SIan Dowse 		error = selscan(td, ibits, obits, nd);
1109ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1110ace8398dSJeff Roberson 			break;
1111cf5e4fe6SDavide Italiano 		error = seltdwait(td, asbt, precision);
1112ace8398dSJeff Roberson 		if (error)
1113ace8398dSJeff Roberson 			break;
1114ace8398dSJeff Roberson 		error = selrescan(td, ibits, obits);
1115ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1116ace8398dSJeff Roberson 			break;
111785f190e4SAlfred Perlstein 	}
1118ace8398dSJeff Roberson 	seltdclear(td);
1119265fc98fSSeigo Tanimura 
1120df8bae1dSRodney W. Grimes done:
1121df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
1122df8bae1dSRodney W. Grimes 	if (error == ERESTART)
1123df8bae1dSRodney W. Grimes 		error = EINTR;
1124df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
1125df8bae1dSRodney W. Grimes 		error = 0;
1126841c0c7eSNathan Whitehorn 
1127841c0c7eSNathan Whitehorn 	/* swizzle bit order back, if necessary */
1128841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[0]);
1129841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[1]);
1130841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[2]);
1131841c0c7eSNathan Whitehorn #undef swizzle_fdset
1132841c0c7eSNathan Whitehorn 
1133df8bae1dSRodney W. Grimes #define	putbits(name, x) \
1134b55ef216SKonstantin Belousov 	if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
1135df8bae1dSRodney W. Grimes 		error = error2;
1136df8bae1dSRodney W. Grimes 	if (error == 0) {
1137df8bae1dSRodney W. Grimes 		int error2;
1138df8bae1dSRodney W. Grimes 
11398f19eb88SIan Dowse 		putbits(fd_in, 0);
11408f19eb88SIan Dowse 		putbits(fd_ou, 1);
11418f19eb88SIan Dowse 		putbits(fd_ex, 2);
1142df8bae1dSRodney W. Grimes #undef putbits
1143df8bae1dSRodney W. Grimes 	}
1144d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
1145d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
1146ad2edad9SMatthew Dillon 
1147df8bae1dSRodney W. Grimes 	return (error);
1148df8bae1dSRodney W. Grimes }
114911b763dfSJeff Roberson /*
115011b763dfSJeff Roberson  * Convert a select bit set to poll flags.
1151748b9df6SJeff Roberson  *
115211b763dfSJeff Roberson  * The backend always returns POLLHUP/POLLERR if appropriate and we
115311b763dfSJeff Roberson  * return this as a set bit in any set.
115411b763dfSJeff Roberson  */
115511b763dfSJeff Roberson static int select_flags[3] = {
115611b763dfSJeff Roberson     POLLRDNORM | POLLHUP | POLLERR,
115711b763dfSJeff Roberson     POLLWRNORM | POLLHUP | POLLERR,
115861e53a38SKonstantin Belousov     POLLRDBAND | POLLERR
115911b763dfSJeff Roberson };
116011b763dfSJeff Roberson 
116111b763dfSJeff Roberson /*
116211b763dfSJeff Roberson  * Compute the fo_poll flags required for a fd given by the index and
116311b763dfSJeff Roberson  * bit position in the fd_mask array.
116411b763dfSJeff Roberson  */
116511b763dfSJeff Roberson static __inline int
116660b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit)
116711b763dfSJeff Roberson {
116811b763dfSJeff Roberson 	int flags;
116911b763dfSJeff Roberson 	int msk;
117011b763dfSJeff Roberson 
117111b763dfSJeff Roberson 	flags = 0;
117211b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
117311b763dfSJeff Roberson 		if (ibits[msk] == NULL)
117411b763dfSJeff Roberson 			continue;
117560b7f468SStephane E. Potvin 		if ((ibits[msk][idx] & bit) == 0)
117611b763dfSJeff Roberson 			continue;
117711b763dfSJeff Roberson 		flags |= select_flags[msk];
117811b763dfSJeff Roberson 	}
117911b763dfSJeff Roberson 	return (flags);
118011b763dfSJeff Roberson }
118111b763dfSJeff Roberson 
118211b763dfSJeff Roberson /*
118311b763dfSJeff Roberson  * Set the appropriate output bits given a mask of fired events and the
118411b763dfSJeff Roberson  * input bits originally requested.
118511b763dfSJeff Roberson  */
118611b763dfSJeff Roberson static __inline int
118711b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
118811b763dfSJeff Roberson {
118911b763dfSJeff Roberson 	int msk;
119011b763dfSJeff Roberson 	int n;
119111b763dfSJeff Roberson 
119211b763dfSJeff Roberson 	n = 0;
119311b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
119411b763dfSJeff Roberson 		if ((events & select_flags[msk]) == 0)
119511b763dfSJeff Roberson 			continue;
119611b763dfSJeff Roberson 		if (ibits[msk] == NULL)
119711b763dfSJeff Roberson 			continue;
119811b763dfSJeff Roberson 		if ((ibits[msk][idx] & bit) == 0)
119911b763dfSJeff Roberson 			continue;
120011b763dfSJeff Roberson 		/*
120111b763dfSJeff Roberson 		 * XXX Check for a duplicate set.  This can occur because a
120211b763dfSJeff Roberson 		 * socket calls selrecord() twice for each poll() call
120311b763dfSJeff Roberson 		 * resulting in two selfds per real fd.  selrescan() will
120411b763dfSJeff Roberson 		 * call selsetbits twice as a result.
120511b763dfSJeff Roberson 		 */
120611b763dfSJeff Roberson 		if ((obits[msk][idx] & bit) != 0)
120711b763dfSJeff Roberson 			continue;
120811b763dfSJeff Roberson 		obits[msk][idx] |= bit;
120911b763dfSJeff Roberson 		n++;
121011b763dfSJeff Roberson 	}
121111b763dfSJeff Roberson 
121211b763dfSJeff Roberson 	return (n);
121311b763dfSJeff Roberson }
1214df8bae1dSRodney W. Grimes 
1215a9d2f8d8SRobert Watson static __inline int
1216a9d2f8d8SRobert Watson getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp)
1217a9d2f8d8SRobert Watson {
12187008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
1219a9d2f8d8SRobert Watson 
1220ed5848c8SPawel Jakub Dawidek 	cap_rights_init(&rights, CAP_EVENT);
1221ed5848c8SPawel Jakub Dawidek 
1222b7a39e9eSMateusz Guzik 	return (fget_unlocked(fdp, fd, &rights, fpp, NULL));
1223a9d2f8d8SRobert Watson }
1224a9d2f8d8SRobert Watson 
1225ace8398dSJeff Roberson /*
1226ace8398dSJeff Roberson  * Traverse the list of fds attached to this thread's seltd and check for
1227ace8398dSJeff Roberson  * completion.
1228ace8398dSJeff Roberson  */
1229ace8398dSJeff Roberson static int
1230ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
1231ace8398dSJeff Roberson {
123211b763dfSJeff Roberson 	struct filedesc *fdp;
123311b763dfSJeff Roberson 	struct selinfo *si;
1234ace8398dSJeff Roberson 	struct seltd *stp;
1235ace8398dSJeff Roberson 	struct selfd *sfp;
1236ace8398dSJeff Roberson 	struct selfd *sfn;
1237ace8398dSJeff Roberson 	struct file *fp;
12389cdacff1SJeff Roberson 	fd_mask bit;
12399cdacff1SJeff Roberson 	int fd, ev, n, idx;
1240a9d2f8d8SRobert Watson 	int error;
1241ace8398dSJeff Roberson 
124211b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
1243ace8398dSJeff Roberson 	stp = td->td_sel;
124411b763dfSJeff Roberson 	n = 0;
1245ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1246ace8398dSJeff Roberson 		fd = (int)(uintptr_t)sfp->sf_cookie;
1247ace8398dSJeff Roberson 		si = sfp->sf_si;
1248ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1249ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1250ace8398dSJeff Roberson 		if (si != NULL)
1251ace8398dSJeff Roberson 			continue;
1252a9d2f8d8SRobert Watson 		error = getselfd_cap(fdp, fd, &fp);
1253a9d2f8d8SRobert Watson 		if (error)
1254a9d2f8d8SRobert Watson 			return (error);
125511b763dfSJeff Roberson 		idx = fd / NFDBITS;
12569cdacff1SJeff Roberson 		bit = (fd_mask)1 << (fd % NFDBITS);
125711b763dfSJeff Roberson 		ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
1258bf422e5fSJeff Roberson 		fdrop(fp, td);
125911b763dfSJeff Roberson 		if (ev != 0)
126011b763dfSJeff Roberson 			n += selsetbits(ibits, obits, idx, bit, ev);
1261ace8398dSJeff Roberson 	}
1262ace8398dSJeff Roberson 	stp->st_flags = 0;
1263ace8398dSJeff Roberson 	td->td_retval[0] = n;
1264ace8398dSJeff Roberson 	return (0);
1265ace8398dSJeff Roberson }
1266ace8398dSJeff Roberson 
1267ace8398dSJeff Roberson /*
1268ace8398dSJeff Roberson  * Perform the initial filedescriptor scan and register ourselves with
1269ace8398dSJeff Roberson  * each selinfo.
1270ace8398dSJeff Roberson  */
1271265fc98fSSeigo Tanimura static int
1272b40ce416SJulian Elischer selscan(td, ibits, obits, nfd)
1273b40ce416SJulian Elischer 	struct thread *td;
1274b08f7993SSujal Patel 	fd_mask **ibits, **obits;
1275cb226aaaSPoul-Henning Kamp 	int nfd;
1276df8bae1dSRodney W. Grimes {
127711b763dfSJeff Roberson 	struct filedesc *fdp;
1278df8bae1dSRodney W. Grimes 	struct file *fp;
12799cdacff1SJeff Roberson 	fd_mask bit;
128011b763dfSJeff Roberson 	int ev, flags, end, fd;
12819cdacff1SJeff Roberson 	int n, idx;
1282a9d2f8d8SRobert Watson 	int error;
1283df8bae1dSRodney W. Grimes 
128411b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
128511b763dfSJeff Roberson 	n = 0;
12869cdacff1SJeff Roberson 	for (idx = 0, fd = 0; fd < nfd; idx++) {
128711b763dfSJeff Roberson 		end = imin(fd + NFDBITS, nfd);
128811b763dfSJeff Roberson 		for (bit = 1; fd < end; bit <<= 1, fd++) {
128911b763dfSJeff Roberson 			/* Compute the list of events we're interested in. */
129011b763dfSJeff Roberson 			flags = selflags(ibits, idx, bit);
129111b763dfSJeff Roberson 			if (flags == 0)
1292f082218cSPeter Wemm 				continue;
1293a9d2f8d8SRobert Watson 			error = getselfd_cap(fdp, fd, &fp);
1294a9d2f8d8SRobert Watson 			if (error)
1295a9d2f8d8SRobert Watson 				return (error);
1296ace8398dSJeff Roberson 			selfdalloc(td, (void *)(uintptr_t)fd);
129711b763dfSJeff Roberson 			ev = fo_poll(fp, flags, td->td_ucred, td);
1298bf422e5fSJeff Roberson 			fdrop(fp, td);
129911b763dfSJeff Roberson 			if (ev != 0)
130011b763dfSJeff Roberson 				n += selsetbits(ibits, obits, idx, bit, ev);
1301df8bae1dSRodney W. Grimes 		}
1302df8bae1dSRodney W. Grimes 	}
130311b763dfSJeff Roberson 
1304b40ce416SJulian Elischer 	td->td_retval[0] = n;
1305df8bae1dSRodney W. Grimes 	return (0);
1306df8bae1dSRodney W. Grimes }
1307df8bae1dSRodney W. Grimes 
130842d11757SPeter Wemm int
1309186d9c34SDmitry Chagin sys_poll(struct thread *td, struct poll_args *uap)
1310186d9c34SDmitry Chagin {
1311186d9c34SDmitry Chagin 	struct timespec ts, *tsp;
1312186d9c34SDmitry Chagin 
1313186d9c34SDmitry Chagin 	if (uap->timeout != INFTIM) {
1314186d9c34SDmitry Chagin 		if (uap->timeout < 0)
1315186d9c34SDmitry Chagin 			return (EINVAL);
1316186d9c34SDmitry Chagin 		ts.tv_sec = uap->timeout / 1000;
1317186d9c34SDmitry Chagin 		ts.tv_nsec = (uap->timeout % 1000) * 1000000;
1318186d9c34SDmitry Chagin 		tsp = &ts;
1319186d9c34SDmitry Chagin 	} else
1320186d9c34SDmitry Chagin 		tsp = NULL;
1321186d9c34SDmitry Chagin 
1322186d9c34SDmitry Chagin 	return (kern_poll(td, uap->fds, uap->nfds, tsp, NULL));
1323186d9c34SDmitry Chagin }
1324186d9c34SDmitry Chagin 
1325186d9c34SDmitry Chagin int
1326186d9c34SDmitry Chagin kern_poll(struct thread *td, struct pollfd *fds, u_int nfds,
1327186d9c34SDmitry Chagin     struct timespec *tsp, sigset_t *uset)
132842d11757SPeter Wemm {
13292580f4e5SAndre Oppermann 	struct pollfd *bits;
13302580f4e5SAndre Oppermann 	struct pollfd smallbits[32];
1331186d9c34SDmitry Chagin 	sbintime_t sbt, precision, tmp;
1332186d9c34SDmitry Chagin 	time_t over;
1333186d9c34SDmitry Chagin 	struct timespec ts;
1334cf5e4fe6SDavide Italiano 	int error;
133542d11757SPeter Wemm 	size_t ni;
133642d11757SPeter Wemm 
1337186d9c34SDmitry Chagin 	precision = 0;
1338186d9c34SDmitry Chagin 	if (tsp != NULL) {
1339186d9c34SDmitry Chagin 		if (tsp->tv_sec < 0)
1340186d9c34SDmitry Chagin 			return (EINVAL);
1341186d9c34SDmitry Chagin 		if (tsp->tv_nsec < 0 || tsp->tv_nsec >= 1000000000)
1342186d9c34SDmitry Chagin 			return (EINVAL);
1343186d9c34SDmitry Chagin 		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1344186d9c34SDmitry Chagin 			sbt = 0;
1345186d9c34SDmitry Chagin 		else {
1346186d9c34SDmitry Chagin 			ts = *tsp;
1347186d9c34SDmitry Chagin 			if (ts.tv_sec > INT32_MAX / 2) {
1348186d9c34SDmitry Chagin 				over = ts.tv_sec - INT32_MAX / 2;
1349186d9c34SDmitry Chagin 				ts.tv_sec -= over;
1350186d9c34SDmitry Chagin 			} else
1351186d9c34SDmitry Chagin 				over = 0;
1352186d9c34SDmitry Chagin 			tmp = tstosbt(ts);
1353186d9c34SDmitry Chagin 			precision = tmp;
1354186d9c34SDmitry Chagin 			precision >>= tc_precexp;
1355186d9c34SDmitry Chagin 			if (TIMESEL(&sbt, tmp))
1356186d9c34SDmitry Chagin 				sbt += tc_tick_sbt;
1357186d9c34SDmitry Chagin 			sbt += tmp;
1358186d9c34SDmitry Chagin 		}
1359186d9c34SDmitry Chagin 	} else
1360186d9c34SDmitry Chagin 		sbt = -1;
1361186d9c34SDmitry Chagin 
1362374ae2a3SJeff Roberson 	if (nfds > maxfilesperproc && nfds > FD_SETSIZE)
1363ace8398dSJeff Roberson 		return (EINVAL);
136489b71647SPeter Wemm 	ni = nfds * sizeof(struct pollfd);
136542d11757SPeter Wemm 	if (ni > sizeof(smallbits))
1366a163d034SWarner Losh 		bits = malloc(ni, M_TEMP, M_WAITOK);
136742d11757SPeter Wemm 	else
136842d11757SPeter Wemm 		bits = smallbits;
1369186d9c34SDmitry Chagin 	error = copyin(fds, bits, ni);
137042d11757SPeter Wemm 	if (error)
1371ace8398dSJeff Roberson 		goto done;
1372186d9c34SDmitry Chagin 
1373186d9c34SDmitry Chagin 	if (uset != NULL) {
1374186d9c34SDmitry Chagin 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
1375186d9c34SDmitry Chagin 		    &td->td_oldsigmask, 0);
1376186d9c34SDmitry Chagin 		if (error)
1377ace8398dSJeff Roberson 			goto done;
1378186d9c34SDmitry Chagin 		td->td_pflags |= TDP_OLDMASK;
1379186d9c34SDmitry Chagin 		/*
1380186d9c34SDmitry Chagin 		 * Make sure that ast() is called on return to
1381186d9c34SDmitry Chagin 		 * usermode and TDP_OLDMASK is cleared, restoring old
1382186d9c34SDmitry Chagin 		 * sigmask.
1383186d9c34SDmitry Chagin 		 */
1384186d9c34SDmitry Chagin 		thread_lock(td);
1385186d9c34SDmitry Chagin 		td->td_flags |= TDF_ASTPENDING;
1386186d9c34SDmitry Chagin 		thread_unlock(td);
138742d11757SPeter Wemm 	}
1388186d9c34SDmitry Chagin 
1389ace8398dSJeff Roberson 	seltdinit(td);
1390ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1391ace8398dSJeff Roberson 	for (;;) {
13922580f4e5SAndre Oppermann 		error = pollscan(td, bits, nfds);
1393ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1394ace8398dSJeff Roberson 			break;
1395186d9c34SDmitry Chagin 		error = seltdwait(td, sbt, precision);
1396ace8398dSJeff Roberson 		if (error)
1397ace8398dSJeff Roberson 			break;
1398ace8398dSJeff Roberson 		error = pollrescan(td);
1399ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1400ace8398dSJeff Roberson 			break;
140185f190e4SAlfred Perlstein 	}
1402ace8398dSJeff Roberson 	seltdclear(td);
1403265fc98fSSeigo Tanimura 
140442d11757SPeter Wemm done:
140542d11757SPeter Wemm 	/* poll is not restarted after signals... */
140642d11757SPeter Wemm 	if (error == ERESTART)
140742d11757SPeter Wemm 		error = EINTR;
140842d11757SPeter Wemm 	if (error == EWOULDBLOCK)
140942d11757SPeter Wemm 		error = 0;
141042d11757SPeter Wemm 	if (error == 0) {
1411186d9c34SDmitry Chagin 		error = pollout(td, bits, fds, nfds);
141242d11757SPeter Wemm 		if (error)
141342d11757SPeter Wemm 			goto out;
141442d11757SPeter Wemm 	}
141542d11757SPeter Wemm out:
141642d11757SPeter Wemm 	if (ni > sizeof(smallbits))
141742d11757SPeter Wemm 		free(bits, M_TEMP);
141842d11757SPeter Wemm 	return (error);
141942d11757SPeter Wemm }
142042d11757SPeter Wemm 
1421186d9c34SDmitry Chagin int
1422186d9c34SDmitry Chagin sys_ppoll(struct thread *td, struct ppoll_args *uap)
1423186d9c34SDmitry Chagin {
1424186d9c34SDmitry Chagin 	struct timespec ts, *tsp;
1425186d9c34SDmitry Chagin 	sigset_t set, *ssp;
1426186d9c34SDmitry Chagin 	int error;
1427186d9c34SDmitry Chagin 
1428186d9c34SDmitry Chagin 	if (uap->ts != NULL) {
1429186d9c34SDmitry Chagin 		error = copyin(uap->ts, &ts, sizeof(ts));
1430186d9c34SDmitry Chagin 		if (error)
1431186d9c34SDmitry Chagin 			return (error);
1432186d9c34SDmitry Chagin 		tsp = &ts;
1433186d9c34SDmitry Chagin 	} else
1434186d9c34SDmitry Chagin 		tsp = NULL;
1435186d9c34SDmitry Chagin 	if (uap->set != NULL) {
1436186d9c34SDmitry Chagin 		error = copyin(uap->set, &set, sizeof(set));
1437186d9c34SDmitry Chagin 		if (error)
1438186d9c34SDmitry Chagin 			return (error);
1439186d9c34SDmitry Chagin 		ssp = &set;
1440186d9c34SDmitry Chagin 	} else
1441186d9c34SDmitry Chagin 		ssp = NULL;
1442186d9c34SDmitry Chagin 	/*
1443186d9c34SDmitry Chagin 	 * fds is still a pointer to user space. kern_poll() will
1444186d9c34SDmitry Chagin 	 * take care of copyin that array to the kernel space.
1445186d9c34SDmitry Chagin 	 */
1446186d9c34SDmitry Chagin 
1447186d9c34SDmitry Chagin 	return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
1448186d9c34SDmitry Chagin }
1449186d9c34SDmitry Chagin 
145042d11757SPeter Wemm static int
1451ace8398dSJeff Roberson pollrescan(struct thread *td)
1452ace8398dSJeff Roberson {
1453ace8398dSJeff Roberson 	struct seltd *stp;
1454ace8398dSJeff Roberson 	struct selfd *sfp;
1455ace8398dSJeff Roberson 	struct selfd *sfn;
1456ace8398dSJeff Roberson 	struct selinfo *si;
1457ace8398dSJeff Roberson 	struct filedesc *fdp;
1458ace8398dSJeff Roberson 	struct file *fp;
1459ace8398dSJeff Roberson 	struct pollfd *fd;
14608e076effSSean Bruno #ifdef CAPABILITIES
14617008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
14628e076effSSean Bruno #endif
1463ace8398dSJeff Roberson 	int n;
1464ace8398dSJeff Roberson 
1465ace8398dSJeff Roberson 	n = 0;
1466ace8398dSJeff Roberson 	fdp = td->td_proc->p_fd;
1467ace8398dSJeff Roberson 	stp = td->td_sel;
1468ace8398dSJeff Roberson 	FILEDESC_SLOCK(fdp);
1469ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1470ace8398dSJeff Roberson 		fd = (struct pollfd *)sfp->sf_cookie;
1471ace8398dSJeff Roberson 		si = sfp->sf_si;
1472ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1473ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1474ace8398dSJeff Roberson 		if (si != NULL)
1475ace8398dSJeff Roberson 			continue;
14762609222aSPawel Jakub Dawidek 		fp = fdp->fd_ofiles[fd->fd].fde_file;
1477d6f72489SJonathan Anderson #ifdef CAPABILITIES
14782609222aSPawel Jakub Dawidek 		if (fp == NULL ||
14797008be5bSPawel Jakub Dawidek 		    cap_check(cap_rights(fdp, fd->fd),
1480ed5848c8SPawel Jakub Dawidek 		    cap_rights_init(&rights, CAP_EVENT)) != 0)
1481d6f72489SJonathan Anderson #else
14822609222aSPawel Jakub Dawidek 		if (fp == NULL)
1483d6f72489SJonathan Anderson #endif
14842609222aSPawel Jakub Dawidek 		{
1485ace8398dSJeff Roberson 			fd->revents = POLLNVAL;
1486ace8398dSJeff Roberson 			n++;
1487ace8398dSJeff Roberson 			continue;
1488ace8398dSJeff Roberson 		}
1489d6f72489SJonathan Anderson 
1490ace8398dSJeff Roberson 		/*
1491ace8398dSJeff Roberson 		 * Note: backend also returns POLLHUP and
1492ace8398dSJeff Roberson 		 * POLLERR if appropriate.
1493ace8398dSJeff Roberson 		 */
1494ace8398dSJeff Roberson 		fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1495ace8398dSJeff Roberson 		if (fd->revents != 0)
1496ace8398dSJeff Roberson 			n++;
1497ace8398dSJeff Roberson 	}
1498ace8398dSJeff Roberson 	FILEDESC_SUNLOCK(fdp);
1499ace8398dSJeff Roberson 	stp->st_flags = 0;
1500ace8398dSJeff Roberson 	td->td_retval[0] = n;
1501ace8398dSJeff Roberson 	return (0);
1502ace8398dSJeff Roberson }
1503ace8398dSJeff Roberson 
1504ace8398dSJeff Roberson 
1505ace8398dSJeff Roberson static int
15066d8feddaSKonstantin Belousov pollout(td, fds, ufds, nfd)
15076d8feddaSKonstantin Belousov 	struct thread *td;
1508ae81968fSRobert Watson 	struct pollfd *fds;
1509ae81968fSRobert Watson 	struct pollfd *ufds;
1510ae81968fSRobert Watson 	u_int nfd;
1511ae81968fSRobert Watson {
1512ae81968fSRobert Watson 	int error = 0;
1513ae81968fSRobert Watson 	u_int i = 0;
15146d8feddaSKonstantin Belousov 	u_int n = 0;
1515ae81968fSRobert Watson 
1516ae81968fSRobert Watson 	for (i = 0; i < nfd; i++) {
1517ae81968fSRobert Watson 		error = copyout(&fds->revents, &ufds->revents,
1518ae81968fSRobert Watson 		    sizeof(ufds->revents));
1519ae81968fSRobert Watson 		if (error)
1520ae81968fSRobert Watson 			return (error);
15216d8feddaSKonstantin Belousov 		if (fds->revents != 0)
15226d8feddaSKonstantin Belousov 			n++;
1523ae81968fSRobert Watson 		fds++;
1524ae81968fSRobert Watson 		ufds++;
1525ae81968fSRobert Watson 	}
15266d8feddaSKonstantin Belousov 	td->td_retval[0] = n;
1527ae81968fSRobert Watson 	return (0);
1528ae81968fSRobert Watson }
1529ae81968fSRobert Watson 
1530ae81968fSRobert Watson static int
1531b40ce416SJulian Elischer pollscan(td, fds, nfd)
1532b40ce416SJulian Elischer 	struct thread *td;
153342d11757SPeter Wemm 	struct pollfd *fds;
1534ea0237edSJonathan Lemon 	u_int nfd;
153542d11757SPeter Wemm {
1536ace8398dSJeff Roberson 	struct filedesc *fdp = td->td_proc->p_fd;
153742d11757SPeter Wemm 	struct file *fp;
15388e076effSSean Bruno #ifdef CAPABILITIES
15397008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
15408e076effSSean Bruno #endif
15412609222aSPawel Jakub Dawidek 	int i, n = 0;
154242d11757SPeter Wemm 
15435e3f7694SRobert Watson 	FILEDESC_SLOCK(fdp);
1544eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
1545adf87ab0SMateusz Guzik 		if (fds->fd > fdp->fd_lastfile) {
154642d11757SPeter Wemm 			fds->revents = POLLNVAL;
154742d11757SPeter Wemm 			n++;
1548337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
1549337c9691SJordan K. Hubbard 			fds->revents = 0;
155042d11757SPeter Wemm 		} else {
15512609222aSPawel Jakub Dawidek 			fp = fdp->fd_ofiles[fds->fd].fde_file;
1552d6f72489SJonathan Anderson #ifdef CAPABILITIES
15532609222aSPawel Jakub Dawidek 			if (fp == NULL ||
15542609222aSPawel Jakub Dawidek 			    cap_check(cap_rights(fdp, fds->fd),
1555ed5848c8SPawel Jakub Dawidek 			    cap_rights_init(&rights, CAP_EVENT)) != 0)
1556d6f72489SJonathan Anderson #else
15572609222aSPawel Jakub Dawidek 			if (fp == NULL)
1558d6f72489SJonathan Anderson #endif
15592609222aSPawel Jakub Dawidek 			{
156042d11757SPeter Wemm 				fds->revents = POLLNVAL;
156142d11757SPeter Wemm 				n++;
156242d11757SPeter Wemm 			} else {
15632087c896SBruce Evans 				/*
15642087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
15652087c896SBruce Evans 				 * POLLERR if appropriate.
15662087c896SBruce Evans 				 */
1567ace8398dSJeff Roberson 				selfdalloc(td, fds);
156813ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
1569ea6027a8SRobert Watson 				    td->td_ucred, td);
1570f2159cc7SKonstantin Belousov 				/*
1571f2159cc7SKonstantin Belousov 				 * POSIX requires POLLOUT to be never
1572f2159cc7SKonstantin Belousov 				 * set simultaneously with POLLHUP.
1573f2159cc7SKonstantin Belousov 				 */
1574f2159cc7SKonstantin Belousov 				if ((fds->revents & POLLHUP) != 0)
1575f2159cc7SKonstantin Belousov 					fds->revents &= ~POLLOUT;
1576f2159cc7SKonstantin Belousov 
157742d11757SPeter Wemm 				if (fds->revents != 0)
157842d11757SPeter Wemm 					n++;
157942d11757SPeter Wemm 			}
158042d11757SPeter Wemm 		}
158142d11757SPeter Wemm 	}
15825e3f7694SRobert Watson 	FILEDESC_SUNLOCK(fdp);
1583b40ce416SJulian Elischer 	td->td_retval[0] = n;
158442d11757SPeter Wemm 	return (0);
158542d11757SPeter Wemm }
158642d11757SPeter Wemm 
158742d11757SPeter Wemm /*
158842d11757SPeter Wemm  * OpenBSD poll system call.
15890c14ff0eSRobert Watson  *
159042d11757SPeter Wemm  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
159142d11757SPeter Wemm  */
159242d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
159342d11757SPeter Wemm struct openbsd_poll_args {
159442d11757SPeter Wemm 	struct pollfd *fds;
159542d11757SPeter Wemm 	u_int	nfds;
159642d11757SPeter Wemm 	int	timeout;
159742d11757SPeter Wemm };
159842d11757SPeter Wemm #endif
159942d11757SPeter Wemm int
16008451d0ddSKip Macy sys_openbsd_poll(td, uap)
1601b40ce416SJulian Elischer 	register struct thread *td;
160242d11757SPeter Wemm 	register struct openbsd_poll_args *uap;
160342d11757SPeter Wemm {
16048451d0ddSKip Macy 	return (sys_poll(td, (struct poll_args *)uap));
160542d11757SPeter Wemm }
160642d11757SPeter Wemm 
160785f190e4SAlfred Perlstein /*
1608237abf0cSDavide Italiano  * XXX This was created specifically to support netncp and netsmb.  This
1609237abf0cSDavide Italiano  * allows the caller to specify a socket to wait for events on.  It returns
1610237abf0cSDavide Italiano  * 0 if any events matched and an error otherwise.  There is no way to
1611237abf0cSDavide Italiano  * determine which events fired.
1612237abf0cSDavide Italiano  */
1613237abf0cSDavide Italiano int
1614237abf0cSDavide Italiano selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td)
1615237abf0cSDavide Italiano {
1616237abf0cSDavide Italiano 	struct timeval rtv;
1617237abf0cSDavide Italiano 	sbintime_t asbt, precision, rsbt;
1618237abf0cSDavide Italiano 	int error;
1619237abf0cSDavide Italiano 
162076665df9SPeter Wemm 	precision = 0;	/* stupid gcc! */
1621237abf0cSDavide Italiano 	if (tvp != NULL) {
1622237abf0cSDavide Italiano 		rtv = *tvp;
1623237abf0cSDavide Italiano 		if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1624237abf0cSDavide Italiano 		    rtv.tv_usec >= 1000000)
1625237abf0cSDavide Italiano 			return (EINVAL);
1626237abf0cSDavide Italiano 		if (!timevalisset(&rtv))
1627237abf0cSDavide Italiano 			asbt = 0;
1628237abf0cSDavide Italiano 		else if (rtv.tv_sec <= INT32_MAX) {
1629237abf0cSDavide Italiano 			rsbt = tvtosbt(rtv);
1630237abf0cSDavide Italiano 			precision = rsbt;
1631237abf0cSDavide Italiano 			precision >>= tc_precexp;
1632237abf0cSDavide Italiano 			if (TIMESEL(&asbt, rsbt))
1633237abf0cSDavide Italiano 				asbt += tc_tick_sbt;
16344bc38a5aSDavide Italiano 			if (asbt <= SBT_MAX - rsbt)
1635237abf0cSDavide Italiano 				asbt += rsbt;
1636237abf0cSDavide Italiano 			else
1637237abf0cSDavide Italiano 				asbt = -1;
1638237abf0cSDavide Italiano 		} else
1639237abf0cSDavide Italiano 			asbt = -1;
1640237abf0cSDavide Italiano 	} else
1641237abf0cSDavide Italiano 		asbt = -1;
1642237abf0cSDavide Italiano 	seltdinit(td);
1643237abf0cSDavide Italiano 	/*
1644237abf0cSDavide Italiano 	 * Iterate until the timeout expires or the socket becomes ready.
1645237abf0cSDavide Italiano 	 */
1646237abf0cSDavide Italiano 	for (;;) {
1647237abf0cSDavide Italiano 		selfdalloc(td, NULL);
1648237abf0cSDavide Italiano 		error = sopoll(so, events, NULL, td);
1649237abf0cSDavide Italiano 		/* error here is actually the ready events. */
1650237abf0cSDavide Italiano 		if (error)
1651237abf0cSDavide Italiano 			return (0);
1652237abf0cSDavide Italiano 		error = seltdwait(td, asbt, precision);
1653237abf0cSDavide Italiano 		if (error)
1654237abf0cSDavide Italiano 			break;
1655237abf0cSDavide Italiano 	}
1656237abf0cSDavide Italiano 	seltdclear(td);
1657237abf0cSDavide Italiano 	/* XXX Duplicates ncp/smb behavior. */
1658237abf0cSDavide Italiano 	if (error == ERESTART)
1659237abf0cSDavide Italiano 		error = 0;
1660237abf0cSDavide Italiano 	return (error);
1661237abf0cSDavide Italiano }
1662237abf0cSDavide Italiano 
1663237abf0cSDavide Italiano /*
1664ace8398dSJeff Roberson  * Preallocate two selfds associated with 'cookie'.  Some fo_poll routines
1665ace8398dSJeff Roberson  * have two select sets, one for read and another for write.
1666ace8398dSJeff Roberson  */
1667ace8398dSJeff Roberson static void
1668ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie)
1669ace8398dSJeff Roberson {
1670ace8398dSJeff Roberson 	struct seltd *stp;
1671ace8398dSJeff Roberson 
1672ace8398dSJeff Roberson 	stp = td->td_sel;
1673ace8398dSJeff Roberson 	if (stp->st_free1 == NULL)
1674ace8398dSJeff Roberson 		stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1675ace8398dSJeff Roberson 	stp->st_free1->sf_td = stp;
1676ace8398dSJeff Roberson 	stp->st_free1->sf_cookie = cookie;
1677ace8398dSJeff Roberson 	if (stp->st_free2 == NULL)
1678ace8398dSJeff Roberson 		stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1679ace8398dSJeff Roberson 	stp->st_free2->sf_td = stp;
1680ace8398dSJeff Roberson 	stp->st_free2->sf_cookie = cookie;
1681ace8398dSJeff Roberson }
1682ace8398dSJeff Roberson 
1683ace8398dSJeff Roberson static void
1684ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp)
1685ace8398dSJeff Roberson {
1686ace8398dSJeff Roberson 	STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
168773f2e5f7SMateusz Guzik 	if (sfp->sf_si != NULL) {
1688ace8398dSJeff Roberson 		mtx_lock(sfp->sf_mtx);
1689*fcb5b3a4SKonstantin Belousov 		if (sfp->sf_si != NULL) {
1690ace8398dSJeff Roberson 			TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1691*fcb5b3a4SKonstantin Belousov 			refcount_release(&sfp->sf_refs);
1692*fcb5b3a4SKonstantin Belousov 		}
1693ace8398dSJeff Roberson 		mtx_unlock(sfp->sf_mtx);
169473f2e5f7SMateusz Guzik 	}
1695*fcb5b3a4SKonstantin Belousov 	if (refcount_release(&sfp->sf_refs))
1696ace8398dSJeff Roberson 		uma_zfree(selfd_zone, sfp);
169785f190e4SAlfred Perlstein }
169885f190e4SAlfred Perlstein 
16996aba400aSAttilio Rao /* Drain the waiters tied to all the selfd belonging the specified selinfo. */
17006aba400aSAttilio Rao void
17016aba400aSAttilio Rao seldrain(sip)
17026aba400aSAttilio Rao         struct selinfo *sip;
17036aba400aSAttilio Rao {
17046aba400aSAttilio Rao 
17056aba400aSAttilio Rao 	/*
17066aba400aSAttilio Rao 	 * This feature is already provided by doselwakeup(), thus it is
17076aba400aSAttilio Rao 	 * enough to go for it.
17086aba400aSAttilio Rao 	 * Eventually, the context, should take care to avoid races
17096aba400aSAttilio Rao 	 * between thread calling select()/poll() and file descriptor
17106aba400aSAttilio Rao 	 * detaching, but, again, the races are just the same as
17116aba400aSAttilio Rao 	 * selwakeup().
17126aba400aSAttilio Rao 	 */
17136aba400aSAttilio Rao         doselwakeup(sip, -1);
17146aba400aSAttilio Rao }
17156aba400aSAttilio Rao 
1716df8bae1dSRodney W. Grimes /*
1717df8bae1dSRodney W. Grimes  * Record a select request.
1718df8bae1dSRodney W. Grimes  */
1719df8bae1dSRodney W. Grimes void
1720df8bae1dSRodney W. Grimes selrecord(selector, sip)
1721b40ce416SJulian Elischer 	struct thread *selector;
1722df8bae1dSRodney W. Grimes 	struct selinfo *sip;
1723df8bae1dSRodney W. Grimes {
1724ace8398dSJeff Roberson 	struct selfd *sfp;
1725ace8398dSJeff Roberson 	struct seltd *stp;
1726ace8398dSJeff Roberson 	struct mtx *mtxp;
1727df8bae1dSRodney W. Grimes 
1728ace8398dSJeff Roberson 	stp = selector->td_sel;
172985f190e4SAlfred Perlstein 	/*
1730ace8398dSJeff Roberson 	 * Don't record when doing a rescan.
173185f190e4SAlfred Perlstein 	 */
1732ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_RESCAN)
1733ace8398dSJeff Roberson 		return;
1734ace8398dSJeff Roberson 	/*
1735ace8398dSJeff Roberson 	 * Grab one of the preallocated descriptors.
1736ace8398dSJeff Roberson 	 */
1737ace8398dSJeff Roberson 	sfp = NULL;
1738ace8398dSJeff Roberson 	if ((sfp = stp->st_free1) != NULL)
1739ace8398dSJeff Roberson 		stp->st_free1 = NULL;
1740ace8398dSJeff Roberson 	else if ((sfp = stp->st_free2) != NULL)
1741ace8398dSJeff Roberson 		stp->st_free2 = NULL;
1742ace8398dSJeff Roberson 	else
1743ace8398dSJeff Roberson 		panic("selrecord: No free selfd on selq");
17442141453eSJeff Roberson 	mtxp = sip->si_mtx;
17452141453eSJeff Roberson 	if (mtxp == NULL)
17462141453eSJeff Roberson 		mtxp = mtx_pool_find(mtxpool_select, sip);
1747ace8398dSJeff Roberson 	/*
1748ace8398dSJeff Roberson 	 * Initialize the sfp and queue it in the thread.
1749ace8398dSJeff Roberson 	 */
1750ace8398dSJeff Roberson 	sfp->sf_si = sip;
1751ace8398dSJeff Roberson 	sfp->sf_mtx = mtxp;
1752*fcb5b3a4SKonstantin Belousov 	refcount_init(&sfp->sf_refs, 2);
1753ace8398dSJeff Roberson 	STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1754ace8398dSJeff Roberson 	/*
1755ace8398dSJeff Roberson 	 * Now that we've locked the sip, check for initialization.
1756ace8398dSJeff Roberson 	 */
1757ace8398dSJeff Roberson 	mtx_lock(mtxp);
1758ace8398dSJeff Roberson 	if (sip->si_mtx == NULL) {
1759ace8398dSJeff Roberson 		sip->si_mtx = mtxp;
1760ace8398dSJeff Roberson 		TAILQ_INIT(&sip->si_tdlist);
176185f190e4SAlfred Perlstein 	}
1762ace8398dSJeff Roberson 	/*
1763ace8398dSJeff Roberson 	 * Add this thread to the list of selfds listening on this selinfo.
1764ace8398dSJeff Roberson 	 */
1765ace8398dSJeff Roberson 	TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1766ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1767df8bae1dSRodney W. Grimes }
1768df8bae1dSRodney W. Grimes 
1769512824f8SSeigo Tanimura /* Wake up a selecting thread. */
1770df8bae1dSRodney W. Grimes void
1771df8bae1dSRodney W. Grimes selwakeup(sip)
177285f190e4SAlfred Perlstein 	struct selinfo *sip;
1773df8bae1dSRodney W. Grimes {
1774512824f8SSeigo Tanimura 	doselwakeup(sip, -1);
1775512824f8SSeigo Tanimura }
1776512824f8SSeigo Tanimura 
1777512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */
1778512824f8SSeigo Tanimura void
1779512824f8SSeigo Tanimura selwakeuppri(sip, pri)
1780512824f8SSeigo Tanimura 	struct selinfo *sip;
1781512824f8SSeigo Tanimura 	int pri;
1782512824f8SSeigo Tanimura {
1783512824f8SSeigo Tanimura 	doselwakeup(sip, pri);
1784512824f8SSeigo Tanimura }
1785512824f8SSeigo Tanimura 
1786512824f8SSeigo Tanimura /*
1787512824f8SSeigo Tanimura  * Do a wakeup when a selectable event occurs.
1788512824f8SSeigo Tanimura  */
1789512824f8SSeigo Tanimura static void
1790512824f8SSeigo Tanimura doselwakeup(sip, pri)
1791512824f8SSeigo Tanimura 	struct selinfo *sip;
1792512824f8SSeigo Tanimura 	int pri;
1793512824f8SSeigo Tanimura {
1794ace8398dSJeff Roberson 	struct selfd *sfp;
1795ace8398dSJeff Roberson 	struct selfd *sfn;
1796ace8398dSJeff Roberson 	struct seltd *stp;
1797df8bae1dSRodney W. Grimes 
1798ace8398dSJeff Roberson 	/* If it's not initialized there can't be any waiters. */
1799ace8398dSJeff Roberson 	if (sip->si_mtx == NULL)
1800b40ce416SJulian Elischer 		return;
1801ace8398dSJeff Roberson 	/*
1802ace8398dSJeff Roberson 	 * Locking the selinfo locks all selfds associated with it.
1803ace8398dSJeff Roberson 	 */
1804ace8398dSJeff Roberson 	mtx_lock(sip->si_mtx);
1805ace8398dSJeff Roberson 	TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1806ace8398dSJeff Roberson 		/*
1807ace8398dSJeff Roberson 		 * Once we remove this sfp from the list and clear the
1808ace8398dSJeff Roberson 		 * sf_si seltdclear will know to ignore this si.
1809ace8398dSJeff Roberson 		 */
1810ace8398dSJeff Roberson 		TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1811ace8398dSJeff Roberson 		sfp->sf_si = NULL;
1812ace8398dSJeff Roberson 		stp = sfp->sf_td;
1813ace8398dSJeff Roberson 		mtx_lock(&stp->st_mtx);
1814ace8398dSJeff Roberson 		stp->st_flags |= SELTD_PENDING;
1815ace8398dSJeff Roberson 		cv_broadcastpri(&stp->st_wait, pri);
1816ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1817*fcb5b3a4SKonstantin Belousov 		if (refcount_release(&sfp->sf_refs))
1818*fcb5b3a4SKonstantin Belousov 			uma_zfree(selfd_zone, sfp);
1819b40ce416SJulian Elischer 	}
1820ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1821ace8398dSJeff Roberson }
1822ace8398dSJeff Roberson 
1823ace8398dSJeff Roberson static void
1824ace8398dSJeff Roberson seltdinit(struct thread *td)
1825ace8398dSJeff Roberson {
1826ace8398dSJeff Roberson 	struct seltd *stp;
1827ace8398dSJeff Roberson 
1828ace8398dSJeff Roberson 	if ((stp = td->td_sel) != NULL)
1829ace8398dSJeff Roberson 		goto out;
1830ace8398dSJeff Roberson 	td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1831ace8398dSJeff Roberson 	mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1832ace8398dSJeff Roberson 	cv_init(&stp->st_wait, "select");
1833ace8398dSJeff Roberson out:
1834ace8398dSJeff Roberson 	stp->st_flags = 0;
1835ace8398dSJeff Roberson 	STAILQ_INIT(&stp->st_selq);
1836ace8398dSJeff Roberson }
1837ace8398dSJeff Roberson 
1838ace8398dSJeff Roberson static int
1839cf5e4fe6SDavide Italiano seltdwait(struct thread *td, sbintime_t sbt, sbintime_t precision)
1840ace8398dSJeff Roberson {
1841ace8398dSJeff Roberson 	struct seltd *stp;
1842ace8398dSJeff Roberson 	int error;
1843ace8398dSJeff Roberson 
1844ace8398dSJeff Roberson 	stp = td->td_sel;
1845ace8398dSJeff Roberson 	/*
1846ace8398dSJeff Roberson 	 * An event of interest may occur while we do not hold the seltd
1847ace8398dSJeff Roberson 	 * locked so check the pending flag before we sleep.
1848ace8398dSJeff Roberson 	 */
1849ace8398dSJeff Roberson 	mtx_lock(&stp->st_mtx);
1850ace8398dSJeff Roberson 	/*
1851ace8398dSJeff Roberson 	 * Any further calls to selrecord will be a rescan.
1852ace8398dSJeff Roberson 	 */
1853ace8398dSJeff Roberson 	stp->st_flags |= SELTD_RESCAN;
1854ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_PENDING) {
1855ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1856ace8398dSJeff Roberson 		return (0);
1857ace8398dSJeff Roberson 	}
1858cf5e4fe6SDavide Italiano 	if (sbt == 0)
1859cf5e4fe6SDavide Italiano 		error = EWOULDBLOCK;
1860cf5e4fe6SDavide Italiano 	else if (sbt != -1)
1861cf5e4fe6SDavide Italiano 		error = cv_timedwait_sig_sbt(&stp->st_wait, &stp->st_mtx,
1862cf5e4fe6SDavide Italiano 		    sbt, precision, C_ABSOLUTE);
1863ace8398dSJeff Roberson 	else
1864ace8398dSJeff Roberson 		error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
1865ace8398dSJeff Roberson 	mtx_unlock(&stp->st_mtx);
1866ace8398dSJeff Roberson 
1867ace8398dSJeff Roberson 	return (error);
1868ace8398dSJeff Roberson }
1869ace8398dSJeff Roberson 
1870ace8398dSJeff Roberson void
1871ace8398dSJeff Roberson seltdfini(struct thread *td)
1872ace8398dSJeff Roberson {
1873ace8398dSJeff Roberson 	struct seltd *stp;
1874ace8398dSJeff Roberson 
1875ace8398dSJeff Roberson 	stp = td->td_sel;
1876ace8398dSJeff Roberson 	if (stp == NULL)
1877ace8398dSJeff Roberson 		return;
1878ace8398dSJeff Roberson 	if (stp->st_free1)
1879ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free1);
1880ace8398dSJeff Roberson 	if (stp->st_free2)
1881ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free2);
1882ace8398dSJeff Roberson 	td->td_sel = NULL;
1883ace8398dSJeff Roberson 	free(stp, M_SELECT);
1884ace8398dSJeff Roberson }
1885ace8398dSJeff Roberson 
1886ace8398dSJeff Roberson /*
1887ace8398dSJeff Roberson  * Remove the references to the thread from all of the objects we were
1888ace8398dSJeff Roberson  * polling.
1889ace8398dSJeff Roberson  */
1890ace8398dSJeff Roberson static void
1891ace8398dSJeff Roberson seltdclear(struct thread *td)
1892ace8398dSJeff Roberson {
1893ace8398dSJeff Roberson 	struct seltd *stp;
1894ace8398dSJeff Roberson 	struct selfd *sfp;
1895ace8398dSJeff Roberson 	struct selfd *sfn;
1896ace8398dSJeff Roberson 
1897ace8398dSJeff Roberson 	stp = td->td_sel;
1898ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
1899ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1900ace8398dSJeff Roberson 	stp->st_flags = 0;
1901df8bae1dSRodney W. Grimes }
1902265fc98fSSeigo Tanimura 
19034d77a549SAlfred Perlstein static void selectinit(void *);
1904ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
1905265fc98fSSeigo Tanimura static void
1906ace8398dSJeff Roberson selectinit(void *dummy __unused)
1907265fc98fSSeigo Tanimura {
19082141453eSJeff Roberson 
1909ace8398dSJeff Roberson 	selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL,
1910ace8398dSJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, 0);
19112141453eSJeff Roberson 	mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);
1912265fc98fSSeigo Tanimura }
1913