xref: /freebsd/sys/kern/sys_generic.c (revision b38b22b0b2e515b9155ef5a730b309f9684e2a8c)
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.
1869a28758SEd Maste  * 3. 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 
92611fcff9SJohn Baldwin #ifdef __LP64__
93611fcff9SJohn Baldwin static int iosize_max_clamp = 0;
948bb9a904SKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, iosize_max_clamp, CTLFLAG_RW,
958bb9a904SKonstantin Belousov     &iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX");
96611fcff9SJohn Baldwin static int devfs_iosize_max_clamp = 1;
97bf3e483bSKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, devfs_iosize_max_clamp, CTLFLAG_RW,
98bf3e483bSKonstantin Belousov     &devfs_iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX for devices");
99611fcff9SJohn Baldwin #endif
100bf3e483bSKonstantin Belousov 
1018bb9a904SKonstantin Belousov /*
1028bb9a904SKonstantin Belousov  * Assert that the return value of read(2) and write(2) syscalls fits
1038bb9a904SKonstantin Belousov  * into a register.  If not, an architecture will need to provide the
1048bb9a904SKonstantin Belousov  * usermode wrappers to reconstruct the result.
1058bb9a904SKonstantin Belousov  */
1068bb9a904SKonstantin Belousov CTASSERT(sizeof(register_t) >= sizeof(size_t));
107526d0bd5SKonstantin Belousov 
108a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
109a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
110a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's");
11155166637SPoul-Henning Kamp 
1126d8feddaSKonstantin Belousov static int	pollout(struct thread *, struct pollfd *, struct pollfd *,
1136d8feddaSKonstantin Belousov 		    u_int);
114bbbb04ceSAlfred Perlstein static int	pollscan(struct thread *, struct pollfd *, u_int);
115ace8398dSJeff Roberson static int	pollrescan(struct thread *);
116bbbb04ceSAlfred Perlstein static int	selscan(struct thread *, fd_mask **, fd_mask **, int);
117ace8398dSJeff Roberson static int	selrescan(struct thread *, fd_mask **, fd_mask **);
118ace8398dSJeff Roberson static void	selfdalloc(struct thread *, void *);
119ace8398dSJeff Roberson static void	selfdfree(struct seltd *, struct selfd *);
120bcd9e0ddSJohn Baldwin static int	dofileread(struct thread *, int, struct file *, struct uio *,
121bcd9e0ddSJohn Baldwin 		    off_t, int);
122bcd9e0ddSJohn Baldwin static int	dofilewrite(struct thread *, int, struct file *, struct uio *,
123bcd9e0ddSJohn Baldwin 		    off_t, int);
124512824f8SSeigo Tanimura static void	doselwakeup(struct selinfo *, int);
125ace8398dSJeff Roberson static void	seltdinit(struct thread *);
126cf5e4fe6SDavide Italiano static int	seltdwait(struct thread *, sbintime_t, sbintime_t);
127ace8398dSJeff Roberson static void	seltdclear(struct thread *);
128ace8398dSJeff Roberson 
129ace8398dSJeff Roberson /*
130ace8398dSJeff Roberson  * One seltd per-thread allocated on demand as needed.
131ace8398dSJeff Roberson  *
132ace8398dSJeff Roberson  *	t - protected by st_mtx
133ace8398dSJeff Roberson  * 	k - Only accessed by curthread or read-only
134ace8398dSJeff Roberson  */
135ace8398dSJeff Roberson struct seltd {
136ace8398dSJeff Roberson 	STAILQ_HEAD(, selfd)	st_selq;	/* (k) List of selfds. */
137ace8398dSJeff Roberson 	struct selfd		*st_free1;	/* (k) free fd for read set. */
138ace8398dSJeff Roberson 	struct selfd		*st_free2;	/* (k) free fd for write set. */
139ace8398dSJeff Roberson 	struct mtx		st_mtx;		/* Protects struct seltd */
140ace8398dSJeff Roberson 	struct cv		st_wait;	/* (t) Wait channel. */
141ace8398dSJeff Roberson 	int			st_flags;	/* (t) SELTD_ flags. */
142ace8398dSJeff Roberson };
143ace8398dSJeff Roberson 
144ace8398dSJeff Roberson #define	SELTD_PENDING	0x0001			/* We have pending events. */
145ace8398dSJeff Roberson #define	SELTD_RESCAN	0x0002			/* Doing a rescan. */
146ace8398dSJeff Roberson 
147ace8398dSJeff Roberson /*
148ace8398dSJeff Roberson  * One selfd allocated per-thread per-file-descriptor.
149ace8398dSJeff Roberson  *	f - protected by sf_mtx
150ace8398dSJeff Roberson  */
151ace8398dSJeff Roberson struct selfd {
152ace8398dSJeff Roberson 	STAILQ_ENTRY(selfd)	sf_link;	/* (k) fds owned by this td. */
153ace8398dSJeff Roberson 	TAILQ_ENTRY(selfd)	sf_threads;	/* (f) fds on this selinfo. */
154ace8398dSJeff Roberson 	struct selinfo		*sf_si;		/* (f) selinfo when linked. */
155ace8398dSJeff Roberson 	struct mtx		*sf_mtx;	/* Pointer to selinfo mtx. */
156ace8398dSJeff Roberson 	struct seltd		*sf_td;		/* (k) owning seltd. */
157ace8398dSJeff Roberson 	void			*sf_cookie;	/* (k) fd or pollfd. */
158fcb5b3a4SKonstantin Belousov 	u_int			sf_refs;
159ace8398dSJeff Roberson };
160ace8398dSJeff Roberson 
161ace8398dSJeff Roberson static uma_zone_t selfd_zone;
1622141453eSJeff Roberson static struct mtx_pool *mtxpool_select;
1638fe387abSDmitrij Tejblum 
164611fcff9SJohn Baldwin #ifdef __LP64__
165611fcff9SJohn Baldwin size_t
166611fcff9SJohn Baldwin devfs_iosize_max(void)
167611fcff9SJohn Baldwin {
168611fcff9SJohn Baldwin 
169611fcff9SJohn Baldwin 	return (devfs_iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ?
170611fcff9SJohn Baldwin 	    INT_MAX : SSIZE_MAX);
171611fcff9SJohn Baldwin }
172611fcff9SJohn Baldwin 
173611fcff9SJohn Baldwin size_t
174611fcff9SJohn Baldwin iosize_max(void)
175611fcff9SJohn Baldwin {
176611fcff9SJohn Baldwin 
177611fcff9SJohn Baldwin 	return (iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ?
178611fcff9SJohn Baldwin 	    INT_MAX : SSIZE_MAX);
179611fcff9SJohn Baldwin }
180611fcff9SJohn Baldwin #endif
181611fcff9SJohn Baldwin 
182d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
183df8bae1dSRodney W. Grimes struct read_args {
184df8bae1dSRodney W. Grimes 	int	fd;
185134e06feSBruce Evans 	void	*buf;
186134e06feSBruce Evans 	size_t	nbyte;
187df8bae1dSRodney W. Grimes };
188d2d3e875SBruce Evans #endif
18926f9a767SRodney W. Grimes int
1908451d0ddSKip Macy sys_read(td, uap)
191b40ce416SJulian Elischer 	struct thread *td;
192b064d43dSMatthew Dillon 	struct read_args *uap;
193df8bae1dSRodney W. Grimes {
194bcd9e0ddSJohn Baldwin 	struct uio auio;
195bcd9e0ddSJohn Baldwin 	struct iovec aiov;
196279d7226SMatthew Dillon 	int error;
197df8bae1dSRodney W. Grimes 
198526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
199bcd9e0ddSJohn Baldwin 		return (EINVAL);
200bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
201bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
202bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
203bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
204bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
205bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
206bcd9e0ddSJohn Baldwin 	error = kern_readv(td, uap->fd, &auio);
207279d7226SMatthew Dillon 	return(error);
208df8bae1dSRodney W. Grimes }
209df8bae1dSRodney W. Grimes 
210df8bae1dSRodney W. Grimes /*
211bcd9e0ddSJohn Baldwin  * Positioned read system call
2124160ccd9SAlan Cox  */
2134160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
2144160ccd9SAlan Cox struct pread_args {
2154160ccd9SAlan Cox 	int	fd;
2164160ccd9SAlan Cox 	void	*buf;
2174160ccd9SAlan Cox 	size_t	nbyte;
2188fe387abSDmitrij Tejblum 	int	pad;
2194160ccd9SAlan Cox 	off_t	offset;
2204160ccd9SAlan Cox };
2214160ccd9SAlan Cox #endif
2224160ccd9SAlan Cox int
223*b38b22b0SEdward Tomasz Napierala sys_pread(struct thread *td, struct pread_args *uap)
224*b38b22b0SEdward Tomasz Napierala {
225*b38b22b0SEdward Tomasz Napierala 
226*b38b22b0SEdward Tomasz Napierala 	return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
227*b38b22b0SEdward Tomasz Napierala }
228*b38b22b0SEdward Tomasz Napierala 
229*b38b22b0SEdward Tomasz Napierala int
230*b38b22b0SEdward Tomasz Napierala kern_pread(struct thread *td, int fd, void *buf, size_t nbyte, off_t offset)
2314160ccd9SAlan Cox {
2324160ccd9SAlan Cox 	struct uio auio;
2334160ccd9SAlan Cox 	struct iovec aiov;
234bcd9e0ddSJohn Baldwin 	int error;
2354160ccd9SAlan Cox 
236*b38b22b0SEdward Tomasz Napierala 	if (nbyte > IOSIZE_MAX)
237bcd9e0ddSJohn Baldwin 		return (EINVAL);
238*b38b22b0SEdward Tomasz Napierala 	aiov.iov_base = buf;
239*b38b22b0SEdward Tomasz Napierala 	aiov.iov_len = nbyte;
2404160ccd9SAlan Cox 	auio.uio_iov = &aiov;
2414160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
242*b38b22b0SEdward Tomasz Napierala 	auio.uio_resid = nbyte;
2434160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
244*b38b22b0SEdward Tomasz Napierala 	error = kern_preadv(td, fd, &auio, offset);
2454160ccd9SAlan Cox 	return (error);
2464160ccd9SAlan Cox }
2474160ccd9SAlan Cox 
2480538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6)
249c2815ad5SPeter Wemm int
250*b38b22b0SEdward Tomasz Napierala freebsd6_pread(struct thread *td, struct freebsd6_pread_args *uap)
251c2815ad5SPeter Wemm {
252c2815ad5SPeter Wemm 
253*b38b22b0SEdward Tomasz Napierala 	return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
254c2815ad5SPeter Wemm }
2550538aafcSKonstantin Belousov #endif
256c2815ad5SPeter Wemm 
2574160ccd9SAlan Cox /*
258df8bae1dSRodney W. Grimes  * Scatter read system call.
259df8bae1dSRodney W. Grimes  */
260d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
261df8bae1dSRodney W. Grimes struct readv_args {
2627147b19dSBruce Evans 	int	fd;
263df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
264df8bae1dSRodney W. Grimes 	u_int	iovcnt;
265df8bae1dSRodney W. Grimes };
266d2d3e875SBruce Evans #endif
26726f9a767SRodney W. Grimes int
2688451d0ddSKip Macy sys_readv(struct thread *td, struct readv_args *uap)
269df8bae1dSRodney W. Grimes {
270b88ec951SJohn Baldwin 	struct uio *auio;
271b88ec951SJohn Baldwin 	int error;
272b88ec951SJohn Baldwin 
273b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
274b88ec951SJohn Baldwin 	if (error)
275b88ec951SJohn Baldwin 		return (error);
276b88ec951SJohn Baldwin 	error = kern_readv(td, uap->fd, auio);
277b88ec951SJohn Baldwin 	free(auio, M_IOV);
278b88ec951SJohn Baldwin 	return (error);
279b88ec951SJohn Baldwin }
280b88ec951SJohn Baldwin 
281b88ec951SJohn Baldwin int
282b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio)
283b88ec951SJohn Baldwin {
284b064d43dSMatthew Dillon 	struct file *fp;
2857008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
286bcd9e0ddSJohn Baldwin 	int error;
287bcd9e0ddSJohn Baldwin 
2887008be5bSPawel Jakub Dawidek 	error = fget_read(td, fd, cap_rights_init(&rights, CAP_READ), &fp);
289bcd9e0ddSJohn Baldwin 	if (error)
290bcd9e0ddSJohn Baldwin 		return (error);
291bcd9e0ddSJohn Baldwin 	error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
292bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
293bcd9e0ddSJohn Baldwin 	return (error);
294bcd9e0ddSJohn Baldwin }
295bcd9e0ddSJohn Baldwin 
296bcd9e0ddSJohn Baldwin /*
297bcd9e0ddSJohn Baldwin  * Scatter positioned read system call.
298bcd9e0ddSJohn Baldwin  */
299bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
300bcd9e0ddSJohn Baldwin struct preadv_args {
301bcd9e0ddSJohn Baldwin 	int	fd;
302bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
303bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
304bcd9e0ddSJohn Baldwin 	off_t	offset;
305bcd9e0ddSJohn Baldwin };
306bcd9e0ddSJohn Baldwin #endif
307bcd9e0ddSJohn Baldwin int
3088451d0ddSKip Macy sys_preadv(struct thread *td, struct preadv_args *uap)
309bcd9e0ddSJohn Baldwin {
310bcd9e0ddSJohn Baldwin 	struct uio *auio;
311bcd9e0ddSJohn Baldwin 	int error;
312bcd9e0ddSJohn Baldwin 
313bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
314bcd9e0ddSJohn Baldwin 	if (error)
315bcd9e0ddSJohn Baldwin 		return (error);
316bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, auio, uap->offset);
317bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
318bcd9e0ddSJohn Baldwin 	return (error);
319bcd9e0ddSJohn Baldwin }
320bcd9e0ddSJohn Baldwin 
321bcd9e0ddSJohn Baldwin int
322bcd9e0ddSJohn Baldwin kern_preadv(td, fd, auio, offset)
323bcd9e0ddSJohn Baldwin 	struct thread *td;
324bcd9e0ddSJohn Baldwin 	int fd;
325bcd9e0ddSJohn Baldwin 	struct uio *auio;
326bcd9e0ddSJohn Baldwin 	off_t offset;
327bcd9e0ddSJohn Baldwin {
328bcd9e0ddSJohn Baldwin 	struct file *fp;
3297008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
330bcd9e0ddSJohn Baldwin 	int error;
331bcd9e0ddSJohn Baldwin 
3327008be5bSPawel Jakub Dawidek 	error = fget_read(td, fd, cap_rights_init(&rights, CAP_PREAD), &fp);
333bcd9e0ddSJohn Baldwin 	if (error)
334bcd9e0ddSJohn Baldwin 		return (error);
335bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
336bcd9e0ddSJohn Baldwin 		error = ESPIPE;
337bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
338bcd9e0ddSJohn Baldwin 		error = EINVAL;
339bcd9e0ddSJohn Baldwin 	else
340bcd9e0ddSJohn Baldwin 		error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
341bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
342bcd9e0ddSJohn Baldwin 	return (error);
343bcd9e0ddSJohn Baldwin }
344bcd9e0ddSJohn Baldwin 
345bcd9e0ddSJohn Baldwin /*
346bcd9e0ddSJohn Baldwin  * Common code for readv and preadv that reads data in
347bcd9e0ddSJohn Baldwin  * from a file using the passed in uio, offset, and flags.
348bcd9e0ddSJohn Baldwin  */
349bcd9e0ddSJohn Baldwin static int
350bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags)
351bcd9e0ddSJohn Baldwin 	struct thread *td;
352bcd9e0ddSJohn Baldwin 	int fd;
353bcd9e0ddSJohn Baldwin 	struct file *fp;
354bcd9e0ddSJohn Baldwin 	struct uio *auio;
355bcd9e0ddSJohn Baldwin 	off_t offset;
356bcd9e0ddSJohn Baldwin 	int flags;
357bcd9e0ddSJohn Baldwin {
358bcd9e0ddSJohn Baldwin 	ssize_t cnt;
35982641acdSAlan Cox 	int error;
360df8bae1dSRodney W. Grimes #ifdef KTRACE
361552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
362df8bae1dSRodney W. Grimes #endif
363df8bae1dSRodney W. Grimes 
36451d1f690SRobert Watson 	AUDIT_ARG_FD(fd);
36551d1f690SRobert Watson 
3664f8d23d6SPoul-Henning Kamp 	/* Finish zero length reads right here */
3674f8d23d6SPoul-Henning Kamp 	if (auio->uio_resid == 0) {
3684f8d23d6SPoul-Henning Kamp 		td->td_retval[0] = 0;
3694f8d23d6SPoul-Henning Kamp 		return(0);
3704f8d23d6SPoul-Henning Kamp 	}
371552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_READ;
372bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
373552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
374df8bae1dSRodney W. Grimes #ifdef KTRACE
375552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
376552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
377df8bae1dSRodney W. Grimes #endif
378552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
379bcd9e0ddSJohn Baldwin 	if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
380552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
381df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
382df8bae1dSRodney W. Grimes 			error = 0;
383279d7226SMatthew Dillon 	}
384552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
385df8bae1dSRodney W. Grimes #ifdef KTRACE
386552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
387552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
388b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_READ, ktruio, error);
389df8bae1dSRodney W. Grimes 	}
390df8bae1dSRodney W. Grimes #endif
391b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
392df8bae1dSRodney W. Grimes 	return (error);
393df8bae1dSRodney W. Grimes }
394df8bae1dSRodney W. Grimes 
395d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
396df8bae1dSRodney W. Grimes struct write_args {
397df8bae1dSRodney W. Grimes 	int	fd;
398134e06feSBruce Evans 	const void *buf;
399134e06feSBruce Evans 	size_t	nbyte;
400df8bae1dSRodney W. Grimes };
401d2d3e875SBruce Evans #endif
40226f9a767SRodney W. Grimes int
4038451d0ddSKip Macy sys_write(td, uap)
404b40ce416SJulian Elischer 	struct thread *td;
405b064d43dSMatthew Dillon 	struct write_args *uap;
406df8bae1dSRodney W. Grimes {
407bcd9e0ddSJohn Baldwin 	struct uio auio;
408bcd9e0ddSJohn Baldwin 	struct iovec aiov;
409279d7226SMatthew Dillon 	int error;
410df8bae1dSRodney W. Grimes 
411526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
412bcd9e0ddSJohn Baldwin 		return (EINVAL);
413bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
414bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
415bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
416bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
417bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
418bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
419bcd9e0ddSJohn Baldwin 	error = kern_writev(td, uap->fd, &auio);
420279d7226SMatthew Dillon 	return(error);
421df8bae1dSRodney W. Grimes }
422df8bae1dSRodney W. Grimes 
423df8bae1dSRodney W. Grimes /*
4240c14ff0eSRobert Watson  * Positioned write system call.
4254160ccd9SAlan Cox  */
4264160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
4274160ccd9SAlan Cox struct pwrite_args {
4284160ccd9SAlan Cox 	int	fd;
4294160ccd9SAlan Cox 	const void *buf;
4304160ccd9SAlan Cox 	size_t	nbyte;
4318fe387abSDmitrij Tejblum 	int	pad;
4324160ccd9SAlan Cox 	off_t	offset;
4334160ccd9SAlan Cox };
4344160ccd9SAlan Cox #endif
4354160ccd9SAlan Cox int
436*b38b22b0SEdward Tomasz Napierala sys_pwrite(struct thread *td, struct pwrite_args *uap)
437*b38b22b0SEdward Tomasz Napierala {
438*b38b22b0SEdward Tomasz Napierala 
439*b38b22b0SEdward Tomasz Napierala 	return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
440*b38b22b0SEdward Tomasz Napierala }
441*b38b22b0SEdward Tomasz Napierala 
442*b38b22b0SEdward Tomasz Napierala int
443*b38b22b0SEdward Tomasz Napierala kern_pwrite(struct thread *td, int fd, const void *buf, size_t nbyte,
444*b38b22b0SEdward Tomasz Napierala     off_t offset)
4454160ccd9SAlan Cox {
4464160ccd9SAlan Cox 	struct uio auio;
4474160ccd9SAlan Cox 	struct iovec aiov;
448bcd9e0ddSJohn Baldwin 	int error;
4494160ccd9SAlan Cox 
450*b38b22b0SEdward Tomasz Napierala 	if (nbyte > IOSIZE_MAX)
451bcd9e0ddSJohn Baldwin 		return (EINVAL);
452*b38b22b0SEdward Tomasz Napierala 	aiov.iov_base = (void *)(uintptr_t)buf;
453*b38b22b0SEdward Tomasz Napierala 	aiov.iov_len = nbyte;
4544160ccd9SAlan Cox 	auio.uio_iov = &aiov;
4554160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
456*b38b22b0SEdward Tomasz Napierala 	auio.uio_resid = nbyte;
4574160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
458*b38b22b0SEdward Tomasz Napierala 	error = kern_pwritev(td, fd, &auio, offset);
4594160ccd9SAlan Cox 	return(error);
4604160ccd9SAlan Cox }
4614160ccd9SAlan Cox 
4620538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6)
463c2815ad5SPeter Wemm int
464*b38b22b0SEdward Tomasz Napierala freebsd6_pwrite(struct thread *td, struct freebsd6_pwrite_args *uap)
465c2815ad5SPeter Wemm {
466c2815ad5SPeter Wemm 
467*b38b22b0SEdward Tomasz Napierala 	return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
468c2815ad5SPeter Wemm }
4690538aafcSKonstantin Belousov #endif
470c2815ad5SPeter Wemm 
4714160ccd9SAlan Cox /*
4720c14ff0eSRobert Watson  * Gather write system call.
473df8bae1dSRodney W. Grimes  */
474d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
475df8bae1dSRodney W. Grimes struct writev_args {
476df8bae1dSRodney W. Grimes 	int	fd;
477df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
478df8bae1dSRodney W. Grimes 	u_int	iovcnt;
479df8bae1dSRodney W. Grimes };
480d2d3e875SBruce Evans #endif
48126f9a767SRodney W. Grimes int
4828451d0ddSKip Macy sys_writev(struct thread *td, struct writev_args *uap)
483df8bae1dSRodney W. Grimes {
484b88ec951SJohn Baldwin 	struct uio *auio;
485b88ec951SJohn Baldwin 	int error;
486b88ec951SJohn Baldwin 
487b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
488b88ec951SJohn Baldwin 	if (error)
489b88ec951SJohn Baldwin 		return (error);
490b88ec951SJohn Baldwin 	error = kern_writev(td, uap->fd, auio);
491b88ec951SJohn Baldwin 	free(auio, M_IOV);
492b88ec951SJohn Baldwin 	return (error);
493b88ec951SJohn Baldwin }
494b88ec951SJohn Baldwin 
495b88ec951SJohn Baldwin int
496b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio)
497b88ec951SJohn Baldwin {
498b064d43dSMatthew Dillon 	struct file *fp;
4997008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
500bcd9e0ddSJohn Baldwin 	int error;
501bcd9e0ddSJohn Baldwin 
5027008be5bSPawel Jakub Dawidek 	error = fget_write(td, fd, cap_rights_init(&rights, CAP_WRITE), &fp);
503bcd9e0ddSJohn Baldwin 	if (error)
504af56abaaSJohn Baldwin 		return (error);
505bcd9e0ddSJohn Baldwin 	error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
506bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
507bcd9e0ddSJohn Baldwin 	return (error);
508bcd9e0ddSJohn Baldwin }
509bcd9e0ddSJohn Baldwin 
510bcd9e0ddSJohn Baldwin /*
5110c14ff0eSRobert Watson  * Gather positioned write system call.
512bcd9e0ddSJohn Baldwin  */
513bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
514bcd9e0ddSJohn Baldwin struct pwritev_args {
515bcd9e0ddSJohn Baldwin 	int	fd;
516bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
517bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
518bcd9e0ddSJohn Baldwin 	off_t	offset;
519bcd9e0ddSJohn Baldwin };
520bcd9e0ddSJohn Baldwin #endif
521bcd9e0ddSJohn Baldwin int
5228451d0ddSKip Macy sys_pwritev(struct thread *td, struct pwritev_args *uap)
523bcd9e0ddSJohn Baldwin {
524bcd9e0ddSJohn Baldwin 	struct uio *auio;
525bcd9e0ddSJohn Baldwin 	int error;
526bcd9e0ddSJohn Baldwin 
527bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
528bcd9e0ddSJohn Baldwin 	if (error)
529bcd9e0ddSJohn Baldwin 		return (error);
530bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, auio, uap->offset);
531bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
532bcd9e0ddSJohn Baldwin 	return (error);
533bcd9e0ddSJohn Baldwin }
534bcd9e0ddSJohn Baldwin 
535bcd9e0ddSJohn Baldwin int
536bcd9e0ddSJohn Baldwin kern_pwritev(td, fd, auio, offset)
537bcd9e0ddSJohn Baldwin 	struct thread *td;
538bcd9e0ddSJohn Baldwin 	struct uio *auio;
539bcd9e0ddSJohn Baldwin 	int fd;
540bcd9e0ddSJohn Baldwin 	off_t offset;
541bcd9e0ddSJohn Baldwin {
542bcd9e0ddSJohn Baldwin 	struct file *fp;
5437008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
544bcd9e0ddSJohn Baldwin 	int error;
545bcd9e0ddSJohn Baldwin 
5467008be5bSPawel Jakub Dawidek 	error = fget_write(td, fd, cap_rights_init(&rights, CAP_PWRITE), &fp);
547bcd9e0ddSJohn Baldwin 	if (error)
548af56abaaSJohn Baldwin 		return (error);
549bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
550bcd9e0ddSJohn Baldwin 		error = ESPIPE;
551bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
552bcd9e0ddSJohn Baldwin 		error = EINVAL;
553bcd9e0ddSJohn Baldwin 	else
554bcd9e0ddSJohn Baldwin 		error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
555bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
556bcd9e0ddSJohn Baldwin 	return (error);
557bcd9e0ddSJohn Baldwin }
558bcd9e0ddSJohn Baldwin 
559bcd9e0ddSJohn Baldwin /*
560bcd9e0ddSJohn Baldwin  * Common code for writev and pwritev that writes data to
561bcd9e0ddSJohn Baldwin  * a file using the passed in uio, offset, and flags.
562bcd9e0ddSJohn Baldwin  */
563bcd9e0ddSJohn Baldwin static int
564bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags)
565bcd9e0ddSJohn Baldwin 	struct thread *td;
566bcd9e0ddSJohn Baldwin 	int fd;
567bcd9e0ddSJohn Baldwin 	struct file *fp;
568bcd9e0ddSJohn Baldwin 	struct uio *auio;
569bcd9e0ddSJohn Baldwin 	off_t offset;
570bcd9e0ddSJohn Baldwin 	int flags;
571bcd9e0ddSJohn Baldwin {
572bcd9e0ddSJohn Baldwin 	ssize_t cnt;
573552afd9cSPoul-Henning Kamp 	int error;
574df8bae1dSRodney W. Grimes #ifdef KTRACE
575552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
576df8bae1dSRodney W. Grimes #endif
577df8bae1dSRodney W. Grimes 
57851d1f690SRobert Watson 	AUDIT_ARG_FD(fd);
579552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_WRITE;
580552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
581bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
582df8bae1dSRodney W. Grimes #ifdef KTRACE
583552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
584552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
585df8bae1dSRodney W. Grimes #endif
586552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
587ad9789f6SKonstantin Belousov 	if (fp->f_type == DTYPE_VNODE &&
588ad9789f6SKonstantin Belousov 	    (fp->f_vnread_flags & FDEVFS_VNODE) == 0)
5899440653dSMatthew Dillon 		bwillwrite();
590bcd9e0ddSJohn Baldwin 	if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
591552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
592df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
593df8bae1dSRodney W. Grimes 			error = 0;
594bcd9e0ddSJohn Baldwin 		/* Socket layer is responsible for issuing SIGPIPE. */
5956f7ca813SBruce M Simpson 		if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
596b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
5977a6f3d78SJohn Baldwin 			tdsignal(td, SIGPIPE);
598b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
59919eb87d2SJohn Baldwin 		}
600df8bae1dSRodney W. Grimes 	}
601552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
602df8bae1dSRodney W. Grimes #ifdef KTRACE
603552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
604552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
605b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_WRITE, ktruio, error);
606df8bae1dSRodney W. Grimes 	}
607df8bae1dSRodney W. Grimes #endif
608b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
609df8bae1dSRodney W. Grimes 	return (error);
610df8bae1dSRodney W. Grimes }
611df8bae1dSRodney W. Grimes 
612e4650294SJohn Baldwin /*
613e4650294SJohn Baldwin  * Truncate a file given a file descriptor.
614e4650294SJohn Baldwin  *
615e4650294SJohn Baldwin  * Can't use fget_write() here, since must return EINVAL and not EBADF if the
616e4650294SJohn Baldwin  * descriptor isn't writable.
617e4650294SJohn Baldwin  */
618e4650294SJohn Baldwin int
619e4650294SJohn Baldwin kern_ftruncate(td, fd, length)
620e4650294SJohn Baldwin 	struct thread *td;
621e4650294SJohn Baldwin 	int fd;
622e4650294SJohn Baldwin 	off_t length;
623e4650294SJohn Baldwin {
624e4650294SJohn Baldwin 	struct file *fp;
6257008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
626e4650294SJohn Baldwin 	int error;
627e4650294SJohn Baldwin 
62814961ba7SRobert Watson 	AUDIT_ARG_FD(fd);
629e4650294SJohn Baldwin 	if (length < 0)
630e4650294SJohn Baldwin 		return (EINVAL);
6317008be5bSPawel Jakub Dawidek 	error = fget(td, fd, cap_rights_init(&rights, CAP_FTRUNCATE), &fp);
632e4650294SJohn Baldwin 	if (error)
633e4650294SJohn Baldwin 		return (error);
63414961ba7SRobert Watson 	AUDIT_ARG_FILE(td->td_proc, fp);
635e4650294SJohn Baldwin 	if (!(fp->f_flag & FWRITE)) {
636e4650294SJohn Baldwin 		fdrop(fp, td);
637e4650294SJohn Baldwin 		return (EINVAL);
638e4650294SJohn Baldwin 	}
639e4650294SJohn Baldwin 	error = fo_truncate(fp, length, td->td_ucred, td);
640e4650294SJohn Baldwin 	fdrop(fp, td);
641e4650294SJohn Baldwin 	return (error);
642e4650294SJohn Baldwin }
643e4650294SJohn Baldwin 
644e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
645e4650294SJohn Baldwin struct ftruncate_args {
646e4650294SJohn Baldwin 	int	fd;
647e4650294SJohn Baldwin 	int	pad;
648e4650294SJohn Baldwin 	off_t	length;
649e4650294SJohn Baldwin };
650e4650294SJohn Baldwin #endif
651e4650294SJohn Baldwin int
6528451d0ddSKip Macy sys_ftruncate(td, uap)
653e4650294SJohn Baldwin 	struct thread *td;
654e4650294SJohn Baldwin 	struct ftruncate_args *uap;
655e4650294SJohn Baldwin {
656e4650294SJohn Baldwin 
657e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
658e4650294SJohn Baldwin }
659e4650294SJohn Baldwin 
660e4650294SJohn Baldwin #if defined(COMPAT_43)
661e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
662e4650294SJohn Baldwin struct oftruncate_args {
663e4650294SJohn Baldwin 	int	fd;
664e4650294SJohn Baldwin 	long	length;
665e4650294SJohn Baldwin };
666e4650294SJohn Baldwin #endif
667e4650294SJohn Baldwin int
668e4650294SJohn Baldwin oftruncate(td, uap)
669e4650294SJohn Baldwin 	struct thread *td;
670e4650294SJohn Baldwin 	struct oftruncate_args *uap;
671e4650294SJohn Baldwin {
672e4650294SJohn Baldwin 
673e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
674e4650294SJohn Baldwin }
675e4650294SJohn Baldwin #endif /* COMPAT_43 */
676e4650294SJohn Baldwin 
677d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
678df8bae1dSRodney W. Grimes struct ioctl_args {
679df8bae1dSRodney W. Grimes 	int	fd;
680069e9bc1SDoug Rabson 	u_long	com;
681df8bae1dSRodney W. Grimes 	caddr_t	data;
682df8bae1dSRodney W. Grimes };
683d2d3e875SBruce Evans #endif
684df8bae1dSRodney W. Grimes /* ARGSUSED */
68526f9a767SRodney W. Grimes int
6868451d0ddSKip Macy sys_ioctl(struct thread *td, struct ioctl_args *uap)
687df8bae1dSRodney W. Grimes {
68850ae6690SHans Petter Selasky 	u_char smalldata[SYS_IOCTL_SMALL_SIZE] __aligned(SYS_IOCTL_SMALL_ALIGN);
6893e15c66fSPoul-Henning Kamp 	u_long com;
6909fddcc66SRuslan Ermilov 	int arg, error;
6913e15c66fSPoul-Henning Kamp 	u_int size;
6929fddcc66SRuslan Ermilov 	caddr_t data;
693df8bae1dSRodney W. Grimes 
6949fc6aa06SPoul-Henning Kamp 	if (uap->com > 0xffffffff) {
6959fc6aa06SPoul-Henning Kamp 		printf(
6969fc6aa06SPoul-Henning Kamp 		    "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
697431f8906SJulian Elischer 		    td->td_proc->p_pid, td->td_name, uap->com);
6989fc6aa06SPoul-Henning Kamp 		uap->com &= 0xffffffff;
6999fc6aa06SPoul-Henning Kamp 	}
700d9f46233SJohn Baldwin 	com = uap->com;
701df8bae1dSRodney W. Grimes 
702df8bae1dSRodney W. Grimes 	/*
703df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
704df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
705df8bae1dSRodney W. Grimes 	 */
706df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
707ca51b19bSPoul-Henning Kamp 	if ((size > IOCPARM_MAX) ||
708ca51b19bSPoul-Henning Kamp 	    ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
7092de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
7102de92a38SPeter Wemm 	    ((com & IOC_OUT) && size == 0) ||
7112de92a38SPeter Wemm #else
7122de92a38SPeter Wemm 	    ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
7132de92a38SPeter Wemm #endif
7149fddcc66SRuslan Ermilov 	    ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
715426da3bcSAlfred Perlstein 		return (ENOTTY);
716279d7226SMatthew Dillon 
717ca51b19bSPoul-Henning Kamp 	if (size > 0) {
718715457f6SDavid E. O'Brien 		if (com & IOC_VOID) {
7199fddcc66SRuslan Ermilov 			/* Integer argument. */
7209fddcc66SRuslan Ermilov 			arg = (intptr_t)uap->data;
7219fddcc66SRuslan Ermilov 			data = (void *)&arg;
7229fddcc66SRuslan Ermilov 			size = 0;
7235cbf44bfSMateusz Guzik 		} else {
7240ecd606bSHans Petter Selasky 			if (size > SYS_IOCTL_SMALL_SIZE)
725715457f6SDavid E. O'Brien 				data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
7260ecd606bSHans Petter Selasky 			else
7270ecd606bSHans Petter Selasky 				data = smalldata;
7285cbf44bfSMateusz Guzik 		}
7299fddcc66SRuslan Ermilov 	} else
7309fddcc66SRuslan Ermilov 		data = (void *)&uap->data;
731df8bae1dSRodney W. Grimes 	if (com & IOC_IN) {
732df8bae1dSRodney W. Grimes 		error = copyin(uap->data, data, (u_int)size);
7335cbf44bfSMateusz Guzik 		if (error != 0)
7345cbf44bfSMateusz Guzik 			goto out;
735ca51b19bSPoul-Henning Kamp 	} else if (com & IOC_OUT) {
736df8bae1dSRodney W. Grimes 		/*
737df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
738df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
739df8bae1dSRodney W. Grimes 		 */
740df8bae1dSRodney W. Grimes 		bzero(data, size);
741279d7226SMatthew Dillon 	}
742df8bae1dSRodney W. Grimes 
743d9f46233SJohn Baldwin 	error = kern_ioctl(td, uap->fd, com, data);
744d9f46233SJohn Baldwin 
745d9f46233SJohn Baldwin 	if (error == 0 && (com & IOC_OUT))
746d9f46233SJohn Baldwin 		error = copyout(data, uap->data, (u_int)size);
747d9f46233SJohn Baldwin 
7485cbf44bfSMateusz Guzik out:
7490ecd606bSHans Petter Selasky 	if (size > SYS_IOCTL_SMALL_SIZE)
7509fddcc66SRuslan Ermilov 		free(data, M_IOCTLOPS);
751d9f46233SJohn Baldwin 	return (error);
752d9f46233SJohn Baldwin }
753d9f46233SJohn Baldwin 
754d9f46233SJohn Baldwin int
755d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
756d9f46233SJohn Baldwin {
757d9f46233SJohn Baldwin 	struct file *fp;
758d9f46233SJohn Baldwin 	struct filedesc *fdp;
7597008be5bSPawel Jakub Dawidek #ifndef CAPABILITIES
7607008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
7617008be5bSPawel Jakub Dawidek #endif
7622609222aSPawel Jakub Dawidek 	int error, tmp, locked;
763d9f46233SJohn Baldwin 
76464c9a4d9SRobert Watson 	AUDIT_ARG_FD(fd);
76564c9a4d9SRobert Watson 	AUDIT_ARG_CMD(com);
7662609222aSPawel Jakub Dawidek 
767d9f46233SJohn Baldwin 	fdp = td->td_proc->p_fd;
7682609222aSPawel Jakub Dawidek 
769d9f46233SJohn Baldwin 	switch (com) {
770d9f46233SJohn Baldwin 	case FIONCLEX:
771d9f46233SJohn Baldwin 	case FIOCLEX:
7725e3f7694SRobert Watson 		FILEDESC_XLOCK(fdp);
7732609222aSPawel Jakub Dawidek 		locked = LA_XLOCKED;
7742609222aSPawel Jakub Dawidek 		break;
7752609222aSPawel Jakub Dawidek 	default:
7762609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
7772609222aSPawel Jakub Dawidek 		FILEDESC_SLOCK(fdp);
7782609222aSPawel Jakub Dawidek 		locked = LA_SLOCKED;
7792609222aSPawel Jakub Dawidek #else
7802609222aSPawel Jakub Dawidek 		locked = LA_UNLOCKED;
7812609222aSPawel Jakub Dawidek #endif
7822609222aSPawel Jakub Dawidek 		break;
7832609222aSPawel Jakub Dawidek 	}
7842609222aSPawel Jakub Dawidek 
7852609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
7862609222aSPawel Jakub Dawidek 	if ((fp = fget_locked(fdp, fd)) == NULL) {
7872609222aSPawel Jakub Dawidek 		error = EBADF;
7882609222aSPawel Jakub Dawidek 		goto out;
7892609222aSPawel Jakub Dawidek 	}
7902609222aSPawel Jakub Dawidek 	if ((error = cap_ioctl_check(fdp, fd, com)) != 0) {
7912609222aSPawel Jakub Dawidek 		fp = NULL;	/* fhold() was not called yet */
7922609222aSPawel Jakub Dawidek 		goto out;
7932609222aSPawel Jakub Dawidek 	}
7942609222aSPawel Jakub Dawidek 	fhold(fp);
7952609222aSPawel Jakub Dawidek 	if (locked == LA_SLOCKED) {
7962609222aSPawel Jakub Dawidek 		FILEDESC_SUNLOCK(fdp);
7972609222aSPawel Jakub Dawidek 		locked = LA_UNLOCKED;
7982609222aSPawel Jakub Dawidek 	}
7992609222aSPawel Jakub Dawidek #else
8007008be5bSPawel Jakub Dawidek 	error = fget(td, fd, cap_rights_init(&rights, CAP_IOCTL), &fp);
8017008be5bSPawel Jakub Dawidek 	if (error != 0) {
8022609222aSPawel Jakub Dawidek 		fp = NULL;
8032609222aSPawel Jakub Dawidek 		goto out;
8042609222aSPawel Jakub Dawidek 	}
8052609222aSPawel Jakub Dawidek #endif
8062609222aSPawel Jakub Dawidek 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
8072609222aSPawel Jakub Dawidek 		error = EBADF;
8082609222aSPawel Jakub Dawidek 		goto out;
8092609222aSPawel Jakub Dawidek 	}
8102609222aSPawel Jakub Dawidek 
8112609222aSPawel Jakub Dawidek 	switch (com) {
8122609222aSPawel Jakub Dawidek 	case FIONCLEX:
8132609222aSPawel Jakub Dawidek 		fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE;
8142609222aSPawel Jakub Dawidek 		goto out;
8152609222aSPawel Jakub Dawidek 	case FIOCLEX:
8162609222aSPawel Jakub Dawidek 		fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE;
817d9f46233SJohn Baldwin 		goto out;
818d9f46233SJohn Baldwin 	case FIONBIO:
819bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
820397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FNONBLOCK);
821df8bae1dSRodney W. Grimes 		else
822397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FNONBLOCK);
8238ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
824d9f46233SJohn Baldwin 		break;
825d9f46233SJohn Baldwin 	case FIOASYNC:
826bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
827397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FASYNC);
828df8bae1dSRodney W. Grimes 		else
829397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FASYNC);
8308ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
831d9f46233SJohn Baldwin 		break;
832df8bae1dSRodney W. Grimes 	}
8338ccf264fSPoul-Henning Kamp 
8348ccf264fSPoul-Henning Kamp 	error = fo_ioctl(fp, com, data, td->td_ucred, td);
835d9f46233SJohn Baldwin out:
8362609222aSPawel Jakub Dawidek 	switch (locked) {
8372609222aSPawel Jakub Dawidek 	case LA_XLOCKED:
8382609222aSPawel Jakub Dawidek 		FILEDESC_XUNLOCK(fdp);
8392609222aSPawel Jakub Dawidek 		break;
8402609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
8412609222aSPawel Jakub Dawidek 	case LA_SLOCKED:
8422609222aSPawel Jakub Dawidek 		FILEDESC_SUNLOCK(fdp);
8432609222aSPawel Jakub Dawidek 		break;
8442609222aSPawel Jakub Dawidek #endif
8452609222aSPawel Jakub Dawidek 	default:
8462609222aSPawel Jakub Dawidek 		FILEDESC_UNLOCK_ASSERT(fdp);
8472609222aSPawel Jakub Dawidek 		break;
8482609222aSPawel Jakub Dawidek 	}
8492609222aSPawel Jakub Dawidek 	if (fp != NULL)
850b40ce416SJulian Elischer 		fdrop(fp, td);
851df8bae1dSRodney W. Grimes 	return (error);
852df8bae1dSRodney W. Grimes }
853df8bae1dSRodney W. Grimes 
854125dcf8cSKonstantin Belousov int
855125dcf8cSKonstantin Belousov poll_no_poll(int events)
856125dcf8cSKonstantin Belousov {
857125dcf8cSKonstantin Belousov 	/*
858125dcf8cSKonstantin Belousov 	 * Return true for read/write.  If the user asked for something
859125dcf8cSKonstantin Belousov 	 * special, return POLLNVAL, so that clients have a way of
860125dcf8cSKonstantin Belousov 	 * determining reliably whether or not the extended
861125dcf8cSKonstantin Belousov 	 * functionality is present without hard-coding knowledge
862125dcf8cSKonstantin Belousov 	 * of specific filesystem implementations.
863125dcf8cSKonstantin Belousov 	 */
864125dcf8cSKonstantin Belousov 	if (events & ~POLLSTANDARD)
865125dcf8cSKonstantin Belousov 		return (POLLNVAL);
866125dcf8cSKonstantin Belousov 
867125dcf8cSKonstantin Belousov 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
868125dcf8cSKonstantin Belousov }
869125dcf8cSKonstantin Belousov 
870066d836bSKonstantin Belousov int
8718451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap)
872066d836bSKonstantin Belousov {
873066d836bSKonstantin Belousov 	struct timespec ts;
874066d836bSKonstantin Belousov 	struct timeval tv, *tvp;
875066d836bSKonstantin Belousov 	sigset_t set, *uset;
876066d836bSKonstantin Belousov 	int error;
877066d836bSKonstantin Belousov 
878066d836bSKonstantin Belousov 	if (uap->ts != NULL) {
879066d836bSKonstantin Belousov 		error = copyin(uap->ts, &ts, sizeof(ts));
880066d836bSKonstantin Belousov 		if (error != 0)
881066d836bSKonstantin Belousov 		    return (error);
882066d836bSKonstantin Belousov 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
883066d836bSKonstantin Belousov 		tvp = &tv;
884066d836bSKonstantin Belousov 	} else
885066d836bSKonstantin Belousov 		tvp = NULL;
886066d836bSKonstantin Belousov 	if (uap->sm != NULL) {
887066d836bSKonstantin Belousov 		error = copyin(uap->sm, &set, sizeof(set));
888066d836bSKonstantin Belousov 		if (error != 0)
889066d836bSKonstantin Belousov 			return (error);
890066d836bSKonstantin Belousov 		uset = &set;
891066d836bSKonstantin Belousov 	} else
892066d836bSKonstantin Belousov 		uset = NULL;
893066d836bSKonstantin Belousov 	return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
894066d836bSKonstantin Belousov 	    uset, NFDBITS));
895066d836bSKonstantin Belousov }
896066d836bSKonstantin Belousov 
897066d836bSKonstantin Belousov int
898066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
899066d836bSKonstantin Belousov     struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
900066d836bSKonstantin Belousov {
901066d836bSKonstantin Belousov 	int error;
902066d836bSKonstantin Belousov 
903066d836bSKonstantin Belousov 	if (uset != NULL) {
904066d836bSKonstantin Belousov 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
905066d836bSKonstantin Belousov 		    &td->td_oldsigmask, 0);
906066d836bSKonstantin Belousov 		if (error != 0)
907066d836bSKonstantin Belousov 			return (error);
908066d836bSKonstantin Belousov 		td->td_pflags |= TDP_OLDMASK;
909066d836bSKonstantin Belousov 		/*
910066d836bSKonstantin Belousov 		 * Make sure that ast() is called on return to
911066d836bSKonstantin Belousov 		 * usermode and TDP_OLDMASK is cleared, restoring old
912066d836bSKonstantin Belousov 		 * sigmask.
913066d836bSKonstantin Belousov 		 */
914066d836bSKonstantin Belousov 		thread_lock(td);
915066d836bSKonstantin Belousov 		td->td_flags |= TDF_ASTPENDING;
916066d836bSKonstantin Belousov 		thread_unlock(td);
917066d836bSKonstantin Belousov 	}
918066d836bSKonstantin Belousov 	error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
919066d836bSKonstantin Belousov 	return (error);
920066d836bSKonstantin Belousov }
921066d836bSKonstantin Belousov 
922d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
923df8bae1dSRodney W. Grimes struct select_args {
924b08f7993SSujal Patel 	int	nd;
925df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
926df8bae1dSRodney W. Grimes 	struct	timeval *tv;
927df8bae1dSRodney W. Grimes };
928d2d3e875SBruce Evans #endif
92926f9a767SRodney W. Grimes int
9308451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap)
931df8bae1dSRodney W. Grimes {
9328f19eb88SIan Dowse 	struct timeval tv, *tvp;
9338f19eb88SIan Dowse 	int error;
9348f19eb88SIan Dowse 
9358f19eb88SIan Dowse 	if (uap->tv != NULL) {
9368f19eb88SIan Dowse 		error = copyin(uap->tv, &tv, sizeof(tv));
9378f19eb88SIan Dowse 		if (error)
9388f19eb88SIan Dowse 			return (error);
9398f19eb88SIan Dowse 		tvp = &tv;
9408f19eb88SIan Dowse 	} else
9418f19eb88SIan Dowse 		tvp = NULL;
9428f19eb88SIan Dowse 
943b55ef216SKonstantin Belousov 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
944b55ef216SKonstantin Belousov 	    NFDBITS));
9458f19eb88SIan Dowse }
9468f19eb88SIan Dowse 
94756be1b9aSKonstantin Belousov /*
94856be1b9aSKonstantin Belousov  * In the unlikely case when user specified n greater then the last
94956be1b9aSKonstantin Belousov  * open file descriptor, check that no bits are set after the last
95056be1b9aSKonstantin Belousov  * valid fd.  We must return EBADF if any is set.
95156be1b9aSKonstantin Belousov  *
95256be1b9aSKonstantin Belousov  * There are applications that rely on the behaviour.
95356be1b9aSKonstantin Belousov  *
95456be1b9aSKonstantin Belousov  * nd is fd_lastfile + 1.
95556be1b9aSKonstantin Belousov  */
95656be1b9aSKonstantin Belousov static int
95756be1b9aSKonstantin Belousov select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits)
95856be1b9aSKonstantin Belousov {
95956be1b9aSKonstantin Belousov 	char *addr, *oaddr;
96056be1b9aSKonstantin Belousov 	int b, i, res;
96156be1b9aSKonstantin Belousov 	uint8_t bits;
96256be1b9aSKonstantin Belousov 
96356be1b9aSKonstantin Belousov 	if (nd >= ndu || fd_in == NULL)
96456be1b9aSKonstantin Belousov 		return (0);
96556be1b9aSKonstantin Belousov 
96656be1b9aSKonstantin Belousov 	oaddr = NULL;
96756be1b9aSKonstantin Belousov 	bits = 0; /* silence gcc */
96856be1b9aSKonstantin Belousov 	for (i = nd; i < ndu; i++) {
96956be1b9aSKonstantin Belousov 		b = i / NBBY;
97056be1b9aSKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN
97156be1b9aSKonstantin Belousov 		addr = (char *)fd_in + b;
97256be1b9aSKonstantin Belousov #else
97356be1b9aSKonstantin Belousov 		addr = (char *)fd_in;
97456be1b9aSKonstantin Belousov 		if (abi_nfdbits == NFDBITS) {
97556be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(fd_mask)) +
97656be1b9aSKonstantin Belousov 			    sizeof(fd_mask) - 1 - b % sizeof(fd_mask);
97756be1b9aSKonstantin Belousov 		} else {
97856be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(uint32_t)) +
97956be1b9aSKonstantin Belousov 			    sizeof(uint32_t) - 1 - b % sizeof(uint32_t);
98056be1b9aSKonstantin Belousov 		}
98156be1b9aSKonstantin Belousov #endif
98256be1b9aSKonstantin Belousov 		if (addr != oaddr) {
98356be1b9aSKonstantin Belousov 			res = fubyte(addr);
98456be1b9aSKonstantin Belousov 			if (res == -1)
98556be1b9aSKonstantin Belousov 				return (EFAULT);
98656be1b9aSKonstantin Belousov 			oaddr = addr;
98756be1b9aSKonstantin Belousov 			bits = res;
98856be1b9aSKonstantin Belousov 		}
98956be1b9aSKonstantin Belousov 		if ((bits & (1 << (i % NBBY))) != 0)
99056be1b9aSKonstantin Belousov 			return (EBADF);
99156be1b9aSKonstantin Belousov 	}
99256be1b9aSKonstantin Belousov 	return (0);
99356be1b9aSKonstantin Belousov }
99456be1b9aSKonstantin Belousov 
9958f19eb88SIan Dowse int
9968f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
997b55ef216SKonstantin Belousov     fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
9988f19eb88SIan Dowse {
999426da3bcSAlfred Perlstein 	struct filedesc *fdp;
1000d5e4d7e1SBruce Evans 	/*
1001d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
1002d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
1003d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
1004d5e4d7e1SBruce Evans 	 * of 256.
1005d5e4d7e1SBruce Evans 	 */
1006d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
1007eb209311SAlfred Perlstein 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
1008cf5e4fe6SDavide Italiano 	struct timeval rtv;
1009cf5e4fe6SDavide Italiano 	sbintime_t asbt, precision, rsbt;
1010b55ef216SKonstantin Belousov 	u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
1011cf5e4fe6SDavide Italiano 	int error, lf, ndu;
1012df8bae1dSRodney W. Grimes 
10138f19eb88SIan Dowse 	if (nd < 0)
1014acbfbfeaSSujal Patel 		return (EINVAL);
1015426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
101656be1b9aSKonstantin Belousov 	ndu = nd;
101756be1b9aSKonstantin Belousov 	lf = fdp->fd_lastfile;
101856be1b9aSKonstantin Belousov 	if (nd > lf + 1)
101956be1b9aSKonstantin Belousov 		nd = lf + 1;
102056be1b9aSKonstantin Belousov 
102156be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits);
102256be1b9aSKonstantin Belousov 	if (error != 0)
102356be1b9aSKonstantin Belousov 		return (error);
102456be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits);
102556be1b9aSKonstantin Belousov 	if (error != 0)
102656be1b9aSKonstantin Belousov 		return (error);
102756be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits);
102856be1b9aSKonstantin Belousov 	if (error != 0)
102956be1b9aSKonstantin Belousov 		return (error);
1030b08f7993SSujal Patel 
1031d5e4d7e1SBruce Evans 	/*
1032d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
1033d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
1034d5e4d7e1SBruce Evans 	 */
10358f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
1036d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
1037b55ef216SKonstantin Belousov 	ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
1038d5e4d7e1SBruce Evans 	nbufbytes = 0;
10398f19eb88SIan Dowse 	if (fd_in != NULL)
1040d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
10418f19eb88SIan Dowse 	if (fd_ou != NULL)
1042d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
10438f19eb88SIan Dowse 	if (fd_ex != NULL)
1044d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
1045d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
1046d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
1047d5e4d7e1SBruce Evans 	else
1048a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
1049b08f7993SSujal Patel 
1050b08f7993SSujal Patel 	/*
1051d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
1052d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
1053d5e4d7e1SBruce Evans 	 * together.
1054b08f7993SSujal Patel 	 */
1055d5e4d7e1SBruce Evans 	sbp = selbits;
1056df8bae1dSRodney W. Grimes #define	getbits(name, x) \
1057d5e4d7e1SBruce Evans 	do {								\
1058841c0c7eSNathan Whitehorn 		if (name == NULL) {					\
1059d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
1060841c0c7eSNathan Whitehorn 			obits[x] = NULL;				\
1061841c0c7eSNathan Whitehorn 		} else {						\
1062d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
1063d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
1064d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
1065b55ef216SKonstantin Belousov 			error = copyin(name, ibits[x], ncpubytes);	\
1066265fc98fSSeigo Tanimura 			if (error != 0)					\
1067ace8398dSJeff Roberson 				goto done;				\
1068b55ef216SKonstantin Belousov 			bzero((char *)ibits[x] + ncpubytes,		\
1069b55ef216SKonstantin Belousov 			    ncpbytes - ncpubytes);			\
1070e04ac2feSJohn Baldwin 		}							\
1071d5e4d7e1SBruce Evans 	} while (0)
10728f19eb88SIan Dowse 	getbits(fd_in, 0);
10738f19eb88SIan Dowse 	getbits(fd_ou, 1);
10748f19eb88SIan Dowse 	getbits(fd_ex, 2);
1075df8bae1dSRodney W. Grimes #undef	getbits
1076841c0c7eSNathan Whitehorn 
1077841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
1078841c0c7eSNathan Whitehorn 	/*
1079841c0c7eSNathan Whitehorn 	 * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
1080841c0c7eSNathan Whitehorn 	 * we are running under 32-bit emulation. This should be more
1081841c0c7eSNathan Whitehorn 	 * generic.
1082841c0c7eSNathan Whitehorn 	 */
1083841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)						\
1084841c0c7eSNathan Whitehorn 	if (abi_nfdbits != NFDBITS && bits != NULL) {			\
1085841c0c7eSNathan Whitehorn 		int i;							\
1086841c0c7eSNathan Whitehorn 		for (i = 0; i < ncpbytes / sizeof *sbp; i++)		\
1087841c0c7eSNathan Whitehorn 			bits[i] = (bits[i] >> 32) | (bits[i] << 32);	\
1088841c0c7eSNathan Whitehorn 	}
1089841c0c7eSNathan Whitehorn #else
1090841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)
1091841c0c7eSNathan Whitehorn #endif
1092841c0c7eSNathan Whitehorn 
1093841c0c7eSNathan Whitehorn 	/* Make sure the bit order makes it through an ABI transition */
1094841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[0]);
1095841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[1]);
1096841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[2]);
1097841c0c7eSNathan Whitehorn 
1098d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
1099d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
1100df8bae1dSRodney W. Grimes 
1101cf5e4fe6SDavide Italiano 	precision = 0;
11028f19eb88SIan Dowse 	if (tvp != NULL) {
1103cf5e4fe6SDavide Italiano 		rtv = *tvp;
1104cf5e4fe6SDavide Italiano 		if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1105cf5e4fe6SDavide Italiano 		    rtv.tv_usec >= 1000000) {
1106df8bae1dSRodney W. Grimes 			error = EINVAL;
1107ace8398dSJeff Roberson 			goto done;
1108df8bae1dSRodney W. Grimes 		}
110921a37a71SAlexander Motin 		if (!timevalisset(&rtv))
1110980c545dSAlexander Motin 			asbt = 0;
111121a37a71SAlexander Motin 		else if (rtv.tv_sec <= INT32_MAX) {
1112cf5e4fe6SDavide Italiano 			rsbt = tvtosbt(rtv);
1113cf5e4fe6SDavide Italiano 			precision = rsbt;
1114cf5e4fe6SDavide Italiano 			precision >>= tc_precexp;
1115cf5e4fe6SDavide Italiano 			if (TIMESEL(&asbt, rsbt))
1116cf5e4fe6SDavide Italiano 				asbt += tc_tick_sbt;
11174bc38a5aSDavide Italiano 			if (asbt <= SBT_MAX - rsbt)
1118cf5e4fe6SDavide Italiano 				asbt += rsbt;
111921a37a71SAlexander Motin 			else
1120980c545dSAlexander Motin 				asbt = -1;
1121980c545dSAlexander Motin 		} else
1122980c545dSAlexander Motin 			asbt = -1;
1123cf5e4fe6SDavide Italiano 	} else
1124cf5e4fe6SDavide Italiano 		asbt = -1;
1125ace8398dSJeff Roberson 	seltdinit(td);
1126ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1127ace8398dSJeff Roberson 	for (;;) {
11288f19eb88SIan Dowse 		error = selscan(td, ibits, obits, nd);
1129ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1130ace8398dSJeff Roberson 			break;
1131cf5e4fe6SDavide Italiano 		error = seltdwait(td, asbt, precision);
1132ace8398dSJeff Roberson 		if (error)
1133ace8398dSJeff Roberson 			break;
1134ace8398dSJeff Roberson 		error = selrescan(td, ibits, obits);
1135ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1136ace8398dSJeff Roberson 			break;
113785f190e4SAlfred Perlstein 	}
1138ace8398dSJeff Roberson 	seltdclear(td);
1139265fc98fSSeigo Tanimura 
1140df8bae1dSRodney W. Grimes done:
1141df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
1142df8bae1dSRodney W. Grimes 	if (error == ERESTART)
1143df8bae1dSRodney W. Grimes 		error = EINTR;
1144df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
1145df8bae1dSRodney W. Grimes 		error = 0;
1146841c0c7eSNathan Whitehorn 
1147841c0c7eSNathan Whitehorn 	/* swizzle bit order back, if necessary */
1148841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[0]);
1149841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[1]);
1150841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[2]);
1151841c0c7eSNathan Whitehorn #undef swizzle_fdset
1152841c0c7eSNathan Whitehorn 
1153df8bae1dSRodney W. Grimes #define	putbits(name, x) \
1154b55ef216SKonstantin Belousov 	if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
1155df8bae1dSRodney W. Grimes 		error = error2;
1156df8bae1dSRodney W. Grimes 	if (error == 0) {
1157df8bae1dSRodney W. Grimes 		int error2;
1158df8bae1dSRodney W. Grimes 
11598f19eb88SIan Dowse 		putbits(fd_in, 0);
11608f19eb88SIan Dowse 		putbits(fd_ou, 1);
11618f19eb88SIan Dowse 		putbits(fd_ex, 2);
1162df8bae1dSRodney W. Grimes #undef putbits
1163df8bae1dSRodney W. Grimes 	}
1164d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
1165d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
1166ad2edad9SMatthew Dillon 
1167df8bae1dSRodney W. Grimes 	return (error);
1168df8bae1dSRodney W. Grimes }
116911b763dfSJeff Roberson /*
117011b763dfSJeff Roberson  * Convert a select bit set to poll flags.
1171748b9df6SJeff Roberson  *
117211b763dfSJeff Roberson  * The backend always returns POLLHUP/POLLERR if appropriate and we
117311b763dfSJeff Roberson  * return this as a set bit in any set.
117411b763dfSJeff Roberson  */
117511b763dfSJeff Roberson static int select_flags[3] = {
117611b763dfSJeff Roberson     POLLRDNORM | POLLHUP | POLLERR,
117711b763dfSJeff Roberson     POLLWRNORM | POLLHUP | POLLERR,
117861e53a38SKonstantin Belousov     POLLRDBAND | POLLERR
117911b763dfSJeff Roberson };
118011b763dfSJeff Roberson 
118111b763dfSJeff Roberson /*
118211b763dfSJeff Roberson  * Compute the fo_poll flags required for a fd given by the index and
118311b763dfSJeff Roberson  * bit position in the fd_mask array.
118411b763dfSJeff Roberson  */
118511b763dfSJeff Roberson static __inline int
118660b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit)
118711b763dfSJeff Roberson {
118811b763dfSJeff Roberson 	int flags;
118911b763dfSJeff Roberson 	int msk;
119011b763dfSJeff Roberson 
119111b763dfSJeff Roberson 	flags = 0;
119211b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
119311b763dfSJeff Roberson 		if (ibits[msk] == NULL)
119411b763dfSJeff Roberson 			continue;
119560b7f468SStephane E. Potvin 		if ((ibits[msk][idx] & bit) == 0)
119611b763dfSJeff Roberson 			continue;
119711b763dfSJeff Roberson 		flags |= select_flags[msk];
119811b763dfSJeff Roberson 	}
119911b763dfSJeff Roberson 	return (flags);
120011b763dfSJeff Roberson }
120111b763dfSJeff Roberson 
120211b763dfSJeff Roberson /*
120311b763dfSJeff Roberson  * Set the appropriate output bits given a mask of fired events and the
120411b763dfSJeff Roberson  * input bits originally requested.
120511b763dfSJeff Roberson  */
120611b763dfSJeff Roberson static __inline int
120711b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
120811b763dfSJeff Roberson {
120911b763dfSJeff Roberson 	int msk;
121011b763dfSJeff Roberson 	int n;
121111b763dfSJeff Roberson 
121211b763dfSJeff Roberson 	n = 0;
121311b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
121411b763dfSJeff Roberson 		if ((events & select_flags[msk]) == 0)
121511b763dfSJeff Roberson 			continue;
121611b763dfSJeff Roberson 		if (ibits[msk] == NULL)
121711b763dfSJeff Roberson 			continue;
121811b763dfSJeff Roberson 		if ((ibits[msk][idx] & bit) == 0)
121911b763dfSJeff Roberson 			continue;
122011b763dfSJeff Roberson 		/*
122111b763dfSJeff Roberson 		 * XXX Check for a duplicate set.  This can occur because a
122211b763dfSJeff Roberson 		 * socket calls selrecord() twice for each poll() call
122311b763dfSJeff Roberson 		 * resulting in two selfds per real fd.  selrescan() will
122411b763dfSJeff Roberson 		 * call selsetbits twice as a result.
122511b763dfSJeff Roberson 		 */
122611b763dfSJeff Roberson 		if ((obits[msk][idx] & bit) != 0)
122711b763dfSJeff Roberson 			continue;
122811b763dfSJeff Roberson 		obits[msk][idx] |= bit;
122911b763dfSJeff Roberson 		n++;
123011b763dfSJeff Roberson 	}
123111b763dfSJeff Roberson 
123211b763dfSJeff Roberson 	return (n);
123311b763dfSJeff Roberson }
1234df8bae1dSRodney W. Grimes 
1235a9d2f8d8SRobert Watson static __inline int
1236a9d2f8d8SRobert Watson getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp)
1237a9d2f8d8SRobert Watson {
12387008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
1239a9d2f8d8SRobert Watson 
1240ed5848c8SPawel Jakub Dawidek 	cap_rights_init(&rights, CAP_EVENT);
1241ed5848c8SPawel Jakub Dawidek 
1242b7a39e9eSMateusz Guzik 	return (fget_unlocked(fdp, fd, &rights, fpp, NULL));
1243a9d2f8d8SRobert Watson }
1244a9d2f8d8SRobert Watson 
1245ace8398dSJeff Roberson /*
1246ace8398dSJeff Roberson  * Traverse the list of fds attached to this thread's seltd and check for
1247ace8398dSJeff Roberson  * completion.
1248ace8398dSJeff Roberson  */
1249ace8398dSJeff Roberson static int
1250ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
1251ace8398dSJeff Roberson {
125211b763dfSJeff Roberson 	struct filedesc *fdp;
125311b763dfSJeff Roberson 	struct selinfo *si;
1254ace8398dSJeff Roberson 	struct seltd *stp;
1255ace8398dSJeff Roberson 	struct selfd *sfp;
1256ace8398dSJeff Roberson 	struct selfd *sfn;
1257ace8398dSJeff Roberson 	struct file *fp;
12589cdacff1SJeff Roberson 	fd_mask bit;
12599cdacff1SJeff Roberson 	int fd, ev, n, idx;
1260a9d2f8d8SRobert Watson 	int error;
1261ace8398dSJeff Roberson 
126211b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
1263ace8398dSJeff Roberson 	stp = td->td_sel;
126411b763dfSJeff Roberson 	n = 0;
1265ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1266ace8398dSJeff Roberson 		fd = (int)(uintptr_t)sfp->sf_cookie;
1267ace8398dSJeff Roberson 		si = sfp->sf_si;
1268ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1269ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1270ace8398dSJeff Roberson 		if (si != NULL)
1271ace8398dSJeff Roberson 			continue;
1272a9d2f8d8SRobert Watson 		error = getselfd_cap(fdp, fd, &fp);
1273a9d2f8d8SRobert Watson 		if (error)
1274a9d2f8d8SRobert Watson 			return (error);
127511b763dfSJeff Roberson 		idx = fd / NFDBITS;
12769cdacff1SJeff Roberson 		bit = (fd_mask)1 << (fd % NFDBITS);
127711b763dfSJeff Roberson 		ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
1278bf422e5fSJeff Roberson 		fdrop(fp, td);
127911b763dfSJeff Roberson 		if (ev != 0)
128011b763dfSJeff Roberson 			n += selsetbits(ibits, obits, idx, bit, ev);
1281ace8398dSJeff Roberson 	}
1282ace8398dSJeff Roberson 	stp->st_flags = 0;
1283ace8398dSJeff Roberson 	td->td_retval[0] = n;
1284ace8398dSJeff Roberson 	return (0);
1285ace8398dSJeff Roberson }
1286ace8398dSJeff Roberson 
1287ace8398dSJeff Roberson /*
1288ace8398dSJeff Roberson  * Perform the initial filedescriptor scan and register ourselves with
1289ace8398dSJeff Roberson  * each selinfo.
1290ace8398dSJeff Roberson  */
1291265fc98fSSeigo Tanimura static int
1292b40ce416SJulian Elischer selscan(td, ibits, obits, nfd)
1293b40ce416SJulian Elischer 	struct thread *td;
1294b08f7993SSujal Patel 	fd_mask **ibits, **obits;
1295cb226aaaSPoul-Henning Kamp 	int nfd;
1296df8bae1dSRodney W. Grimes {
129711b763dfSJeff Roberson 	struct filedesc *fdp;
1298df8bae1dSRodney W. Grimes 	struct file *fp;
12999cdacff1SJeff Roberson 	fd_mask bit;
130011b763dfSJeff Roberson 	int ev, flags, end, fd;
13019cdacff1SJeff Roberson 	int n, idx;
1302a9d2f8d8SRobert Watson 	int error;
1303df8bae1dSRodney W. Grimes 
130411b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
130511b763dfSJeff Roberson 	n = 0;
13069cdacff1SJeff Roberson 	for (idx = 0, fd = 0; fd < nfd; idx++) {
130711b763dfSJeff Roberson 		end = imin(fd + NFDBITS, nfd);
130811b763dfSJeff Roberson 		for (bit = 1; fd < end; bit <<= 1, fd++) {
130911b763dfSJeff Roberson 			/* Compute the list of events we're interested in. */
131011b763dfSJeff Roberson 			flags = selflags(ibits, idx, bit);
131111b763dfSJeff Roberson 			if (flags == 0)
1312f082218cSPeter Wemm 				continue;
1313a9d2f8d8SRobert Watson 			error = getselfd_cap(fdp, fd, &fp);
1314a9d2f8d8SRobert Watson 			if (error)
1315a9d2f8d8SRobert Watson 				return (error);
1316ace8398dSJeff Roberson 			selfdalloc(td, (void *)(uintptr_t)fd);
131711b763dfSJeff Roberson 			ev = fo_poll(fp, flags, td->td_ucred, td);
1318bf422e5fSJeff Roberson 			fdrop(fp, td);
131911b763dfSJeff Roberson 			if (ev != 0)
132011b763dfSJeff Roberson 				n += selsetbits(ibits, obits, idx, bit, ev);
1321df8bae1dSRodney W. Grimes 		}
1322df8bae1dSRodney W. Grimes 	}
132311b763dfSJeff Roberson 
1324b40ce416SJulian Elischer 	td->td_retval[0] = n;
1325df8bae1dSRodney W. Grimes 	return (0);
1326df8bae1dSRodney W. Grimes }
1327df8bae1dSRodney W. Grimes 
132842d11757SPeter Wemm int
1329186d9c34SDmitry Chagin sys_poll(struct thread *td, struct poll_args *uap)
1330186d9c34SDmitry Chagin {
1331186d9c34SDmitry Chagin 	struct timespec ts, *tsp;
1332186d9c34SDmitry Chagin 
1333186d9c34SDmitry Chagin 	if (uap->timeout != INFTIM) {
1334186d9c34SDmitry Chagin 		if (uap->timeout < 0)
1335186d9c34SDmitry Chagin 			return (EINVAL);
1336186d9c34SDmitry Chagin 		ts.tv_sec = uap->timeout / 1000;
1337186d9c34SDmitry Chagin 		ts.tv_nsec = (uap->timeout % 1000) * 1000000;
1338186d9c34SDmitry Chagin 		tsp = &ts;
1339186d9c34SDmitry Chagin 	} else
1340186d9c34SDmitry Chagin 		tsp = NULL;
1341186d9c34SDmitry Chagin 
1342186d9c34SDmitry Chagin 	return (kern_poll(td, uap->fds, uap->nfds, tsp, NULL));
1343186d9c34SDmitry Chagin }
1344186d9c34SDmitry Chagin 
1345186d9c34SDmitry Chagin int
1346186d9c34SDmitry Chagin kern_poll(struct thread *td, struct pollfd *fds, u_int nfds,
1347186d9c34SDmitry Chagin     struct timespec *tsp, sigset_t *uset)
134842d11757SPeter Wemm {
13492580f4e5SAndre Oppermann 	struct pollfd *bits;
13502580f4e5SAndre Oppermann 	struct pollfd smallbits[32];
1351186d9c34SDmitry Chagin 	sbintime_t sbt, precision, tmp;
1352186d9c34SDmitry Chagin 	time_t over;
1353186d9c34SDmitry Chagin 	struct timespec ts;
1354cf5e4fe6SDavide Italiano 	int error;
135542d11757SPeter Wemm 	size_t ni;
135642d11757SPeter Wemm 
1357186d9c34SDmitry Chagin 	precision = 0;
1358186d9c34SDmitry Chagin 	if (tsp != NULL) {
1359186d9c34SDmitry Chagin 		if (tsp->tv_sec < 0)
1360186d9c34SDmitry Chagin 			return (EINVAL);
1361186d9c34SDmitry Chagin 		if (tsp->tv_nsec < 0 || tsp->tv_nsec >= 1000000000)
1362186d9c34SDmitry Chagin 			return (EINVAL);
1363186d9c34SDmitry Chagin 		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1364186d9c34SDmitry Chagin 			sbt = 0;
1365186d9c34SDmitry Chagin 		else {
1366186d9c34SDmitry Chagin 			ts = *tsp;
1367186d9c34SDmitry Chagin 			if (ts.tv_sec > INT32_MAX / 2) {
1368186d9c34SDmitry Chagin 				over = ts.tv_sec - INT32_MAX / 2;
1369186d9c34SDmitry Chagin 				ts.tv_sec -= over;
1370186d9c34SDmitry Chagin 			} else
1371186d9c34SDmitry Chagin 				over = 0;
1372186d9c34SDmitry Chagin 			tmp = tstosbt(ts);
1373186d9c34SDmitry Chagin 			precision = tmp;
1374186d9c34SDmitry Chagin 			precision >>= tc_precexp;
1375186d9c34SDmitry Chagin 			if (TIMESEL(&sbt, tmp))
1376186d9c34SDmitry Chagin 				sbt += tc_tick_sbt;
1377186d9c34SDmitry Chagin 			sbt += tmp;
1378186d9c34SDmitry Chagin 		}
1379186d9c34SDmitry Chagin 	} else
1380186d9c34SDmitry Chagin 		sbt = -1;
1381186d9c34SDmitry Chagin 
1382374ae2a3SJeff Roberson 	if (nfds > maxfilesperproc && nfds > FD_SETSIZE)
1383ace8398dSJeff Roberson 		return (EINVAL);
138489b71647SPeter Wemm 	ni = nfds * sizeof(struct pollfd);
138542d11757SPeter Wemm 	if (ni > sizeof(smallbits))
1386a163d034SWarner Losh 		bits = malloc(ni, M_TEMP, M_WAITOK);
138742d11757SPeter Wemm 	else
138842d11757SPeter Wemm 		bits = smallbits;
1389186d9c34SDmitry Chagin 	error = copyin(fds, bits, ni);
139042d11757SPeter Wemm 	if (error)
1391ace8398dSJeff Roberson 		goto done;
1392186d9c34SDmitry Chagin 
1393186d9c34SDmitry Chagin 	if (uset != NULL) {
1394186d9c34SDmitry Chagin 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
1395186d9c34SDmitry Chagin 		    &td->td_oldsigmask, 0);
1396186d9c34SDmitry Chagin 		if (error)
1397ace8398dSJeff Roberson 			goto done;
1398186d9c34SDmitry Chagin 		td->td_pflags |= TDP_OLDMASK;
1399186d9c34SDmitry Chagin 		/*
1400186d9c34SDmitry Chagin 		 * Make sure that ast() is called on return to
1401186d9c34SDmitry Chagin 		 * usermode and TDP_OLDMASK is cleared, restoring old
1402186d9c34SDmitry Chagin 		 * sigmask.
1403186d9c34SDmitry Chagin 		 */
1404186d9c34SDmitry Chagin 		thread_lock(td);
1405186d9c34SDmitry Chagin 		td->td_flags |= TDF_ASTPENDING;
1406186d9c34SDmitry Chagin 		thread_unlock(td);
140742d11757SPeter Wemm 	}
1408186d9c34SDmitry Chagin 
1409ace8398dSJeff Roberson 	seltdinit(td);
1410ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1411ace8398dSJeff Roberson 	for (;;) {
14122580f4e5SAndre Oppermann 		error = pollscan(td, bits, nfds);
1413ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1414ace8398dSJeff Roberson 			break;
1415186d9c34SDmitry Chagin 		error = seltdwait(td, sbt, precision);
1416ace8398dSJeff Roberson 		if (error)
1417ace8398dSJeff Roberson 			break;
1418ace8398dSJeff Roberson 		error = pollrescan(td);
1419ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1420ace8398dSJeff Roberson 			break;
142185f190e4SAlfred Perlstein 	}
1422ace8398dSJeff Roberson 	seltdclear(td);
1423265fc98fSSeigo Tanimura 
142442d11757SPeter Wemm done:
142542d11757SPeter Wemm 	/* poll is not restarted after signals... */
142642d11757SPeter Wemm 	if (error == ERESTART)
142742d11757SPeter Wemm 		error = EINTR;
142842d11757SPeter Wemm 	if (error == EWOULDBLOCK)
142942d11757SPeter Wemm 		error = 0;
143042d11757SPeter Wemm 	if (error == 0) {
1431186d9c34SDmitry Chagin 		error = pollout(td, bits, fds, nfds);
143242d11757SPeter Wemm 		if (error)
143342d11757SPeter Wemm 			goto out;
143442d11757SPeter Wemm 	}
143542d11757SPeter Wemm out:
143642d11757SPeter Wemm 	if (ni > sizeof(smallbits))
143742d11757SPeter Wemm 		free(bits, M_TEMP);
143842d11757SPeter Wemm 	return (error);
143942d11757SPeter Wemm }
144042d11757SPeter Wemm 
1441186d9c34SDmitry Chagin int
1442186d9c34SDmitry Chagin sys_ppoll(struct thread *td, struct ppoll_args *uap)
1443186d9c34SDmitry Chagin {
1444186d9c34SDmitry Chagin 	struct timespec ts, *tsp;
1445186d9c34SDmitry Chagin 	sigset_t set, *ssp;
1446186d9c34SDmitry Chagin 	int error;
1447186d9c34SDmitry Chagin 
1448186d9c34SDmitry Chagin 	if (uap->ts != NULL) {
1449186d9c34SDmitry Chagin 		error = copyin(uap->ts, &ts, sizeof(ts));
1450186d9c34SDmitry Chagin 		if (error)
1451186d9c34SDmitry Chagin 			return (error);
1452186d9c34SDmitry Chagin 		tsp = &ts;
1453186d9c34SDmitry Chagin 	} else
1454186d9c34SDmitry Chagin 		tsp = NULL;
1455186d9c34SDmitry Chagin 	if (uap->set != NULL) {
1456186d9c34SDmitry Chagin 		error = copyin(uap->set, &set, sizeof(set));
1457186d9c34SDmitry Chagin 		if (error)
1458186d9c34SDmitry Chagin 			return (error);
1459186d9c34SDmitry Chagin 		ssp = &set;
1460186d9c34SDmitry Chagin 	} else
1461186d9c34SDmitry Chagin 		ssp = NULL;
1462186d9c34SDmitry Chagin 	/*
1463186d9c34SDmitry Chagin 	 * fds is still a pointer to user space. kern_poll() will
1464186d9c34SDmitry Chagin 	 * take care of copyin that array to the kernel space.
1465186d9c34SDmitry Chagin 	 */
1466186d9c34SDmitry Chagin 
1467186d9c34SDmitry Chagin 	return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
1468186d9c34SDmitry Chagin }
1469186d9c34SDmitry Chagin 
147042d11757SPeter Wemm static int
1471ace8398dSJeff Roberson pollrescan(struct thread *td)
1472ace8398dSJeff Roberson {
1473ace8398dSJeff Roberson 	struct seltd *stp;
1474ace8398dSJeff Roberson 	struct selfd *sfp;
1475ace8398dSJeff Roberson 	struct selfd *sfn;
1476ace8398dSJeff Roberson 	struct selinfo *si;
1477ace8398dSJeff Roberson 	struct filedesc *fdp;
1478ace8398dSJeff Roberson 	struct file *fp;
1479ace8398dSJeff Roberson 	struct pollfd *fd;
14808e076effSSean Bruno #ifdef CAPABILITIES
14817008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
14828e076effSSean Bruno #endif
1483ace8398dSJeff Roberson 	int n;
1484ace8398dSJeff Roberson 
1485ace8398dSJeff Roberson 	n = 0;
1486ace8398dSJeff Roberson 	fdp = td->td_proc->p_fd;
1487ace8398dSJeff Roberson 	stp = td->td_sel;
1488ace8398dSJeff Roberson 	FILEDESC_SLOCK(fdp);
1489ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1490ace8398dSJeff Roberson 		fd = (struct pollfd *)sfp->sf_cookie;
1491ace8398dSJeff Roberson 		si = sfp->sf_si;
1492ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1493ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1494ace8398dSJeff Roberson 		if (si != NULL)
1495ace8398dSJeff Roberson 			continue;
14962609222aSPawel Jakub Dawidek 		fp = fdp->fd_ofiles[fd->fd].fde_file;
1497d6f72489SJonathan Anderson #ifdef CAPABILITIES
14982609222aSPawel Jakub Dawidek 		if (fp == NULL ||
14997008be5bSPawel Jakub Dawidek 		    cap_check(cap_rights(fdp, fd->fd),
1500ed5848c8SPawel Jakub Dawidek 		    cap_rights_init(&rights, CAP_EVENT)) != 0)
1501d6f72489SJonathan Anderson #else
15022609222aSPawel Jakub Dawidek 		if (fp == NULL)
1503d6f72489SJonathan Anderson #endif
15042609222aSPawel Jakub Dawidek 		{
1505ace8398dSJeff Roberson 			fd->revents = POLLNVAL;
1506ace8398dSJeff Roberson 			n++;
1507ace8398dSJeff Roberson 			continue;
1508ace8398dSJeff Roberson 		}
1509d6f72489SJonathan Anderson 
1510ace8398dSJeff Roberson 		/*
1511ace8398dSJeff Roberson 		 * Note: backend also returns POLLHUP and
1512ace8398dSJeff Roberson 		 * POLLERR if appropriate.
1513ace8398dSJeff Roberson 		 */
1514ace8398dSJeff Roberson 		fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1515ace8398dSJeff Roberson 		if (fd->revents != 0)
1516ace8398dSJeff Roberson 			n++;
1517ace8398dSJeff Roberson 	}
1518ace8398dSJeff Roberson 	FILEDESC_SUNLOCK(fdp);
1519ace8398dSJeff Roberson 	stp->st_flags = 0;
1520ace8398dSJeff Roberson 	td->td_retval[0] = n;
1521ace8398dSJeff Roberson 	return (0);
1522ace8398dSJeff Roberson }
1523ace8398dSJeff Roberson 
1524ace8398dSJeff Roberson 
1525ace8398dSJeff Roberson static int
15266d8feddaSKonstantin Belousov pollout(td, fds, ufds, nfd)
15276d8feddaSKonstantin Belousov 	struct thread *td;
1528ae81968fSRobert Watson 	struct pollfd *fds;
1529ae81968fSRobert Watson 	struct pollfd *ufds;
1530ae81968fSRobert Watson 	u_int nfd;
1531ae81968fSRobert Watson {
1532ae81968fSRobert Watson 	int error = 0;
1533ae81968fSRobert Watson 	u_int i = 0;
15346d8feddaSKonstantin Belousov 	u_int n = 0;
1535ae81968fSRobert Watson 
1536ae81968fSRobert Watson 	for (i = 0; i < nfd; i++) {
1537ae81968fSRobert Watson 		error = copyout(&fds->revents, &ufds->revents,
1538ae81968fSRobert Watson 		    sizeof(ufds->revents));
1539ae81968fSRobert Watson 		if (error)
1540ae81968fSRobert Watson 			return (error);
15416d8feddaSKonstantin Belousov 		if (fds->revents != 0)
15426d8feddaSKonstantin Belousov 			n++;
1543ae81968fSRobert Watson 		fds++;
1544ae81968fSRobert Watson 		ufds++;
1545ae81968fSRobert Watson 	}
15466d8feddaSKonstantin Belousov 	td->td_retval[0] = n;
1547ae81968fSRobert Watson 	return (0);
1548ae81968fSRobert Watson }
1549ae81968fSRobert Watson 
1550ae81968fSRobert Watson static int
1551b40ce416SJulian Elischer pollscan(td, fds, nfd)
1552b40ce416SJulian Elischer 	struct thread *td;
155342d11757SPeter Wemm 	struct pollfd *fds;
1554ea0237edSJonathan Lemon 	u_int nfd;
155542d11757SPeter Wemm {
1556ace8398dSJeff Roberson 	struct filedesc *fdp = td->td_proc->p_fd;
155742d11757SPeter Wemm 	struct file *fp;
15588e076effSSean Bruno #ifdef CAPABILITIES
15597008be5bSPawel Jakub Dawidek 	cap_rights_t rights;
15608e076effSSean Bruno #endif
15612609222aSPawel Jakub Dawidek 	int i, n = 0;
156242d11757SPeter Wemm 
15635e3f7694SRobert Watson 	FILEDESC_SLOCK(fdp);
1564eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
1565adf87ab0SMateusz Guzik 		if (fds->fd > fdp->fd_lastfile) {
156642d11757SPeter Wemm 			fds->revents = POLLNVAL;
156742d11757SPeter Wemm 			n++;
1568337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
1569337c9691SJordan K. Hubbard 			fds->revents = 0;
157042d11757SPeter Wemm 		} else {
15712609222aSPawel Jakub Dawidek 			fp = fdp->fd_ofiles[fds->fd].fde_file;
1572d6f72489SJonathan Anderson #ifdef CAPABILITIES
15732609222aSPawel Jakub Dawidek 			if (fp == NULL ||
15742609222aSPawel Jakub Dawidek 			    cap_check(cap_rights(fdp, fds->fd),
1575ed5848c8SPawel Jakub Dawidek 			    cap_rights_init(&rights, CAP_EVENT)) != 0)
1576d6f72489SJonathan Anderson #else
15772609222aSPawel Jakub Dawidek 			if (fp == NULL)
1578d6f72489SJonathan Anderson #endif
15792609222aSPawel Jakub Dawidek 			{
158042d11757SPeter Wemm 				fds->revents = POLLNVAL;
158142d11757SPeter Wemm 				n++;
158242d11757SPeter Wemm 			} else {
15832087c896SBruce Evans 				/*
15842087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
15852087c896SBruce Evans 				 * POLLERR if appropriate.
15862087c896SBruce Evans 				 */
1587ace8398dSJeff Roberson 				selfdalloc(td, fds);
158813ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
1589ea6027a8SRobert Watson 				    td->td_ucred, td);
1590f2159cc7SKonstantin Belousov 				/*
1591f2159cc7SKonstantin Belousov 				 * POSIX requires POLLOUT to be never
1592f2159cc7SKonstantin Belousov 				 * set simultaneously with POLLHUP.
1593f2159cc7SKonstantin Belousov 				 */
1594f2159cc7SKonstantin Belousov 				if ((fds->revents & POLLHUP) != 0)
1595f2159cc7SKonstantin Belousov 					fds->revents &= ~POLLOUT;
1596f2159cc7SKonstantin Belousov 
159742d11757SPeter Wemm 				if (fds->revents != 0)
159842d11757SPeter Wemm 					n++;
159942d11757SPeter Wemm 			}
160042d11757SPeter Wemm 		}
160142d11757SPeter Wemm 	}
16025e3f7694SRobert Watson 	FILEDESC_SUNLOCK(fdp);
1603b40ce416SJulian Elischer 	td->td_retval[0] = n;
160442d11757SPeter Wemm 	return (0);
160542d11757SPeter Wemm }
160642d11757SPeter Wemm 
160742d11757SPeter Wemm /*
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);
1689fcb5b3a4SKonstantin Belousov 		if (sfp->sf_si != NULL) {
1690ace8398dSJeff Roberson 			TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1691fcb5b3a4SKonstantin Belousov 			refcount_release(&sfp->sf_refs);
1692fcb5b3a4SKonstantin Belousov 		}
1693ace8398dSJeff Roberson 		mtx_unlock(sfp->sf_mtx);
169473f2e5f7SMateusz Guzik 	}
1695fcb5b3a4SKonstantin 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;
1752fcb5b3a4SKonstantin 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);
1817fcb5b3a4SKonstantin Belousov 		if (refcount_release(&sfp->sf_refs))
1818fcb5b3a4SKonstantin 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 }
19130acf5d0bSMark Johnston 
19140acf5d0bSMark Johnston /*
19150acf5d0bSMark Johnston  * Set up a syscall return value that follows the convention specified for
19160acf5d0bSMark Johnston  * posix_* functions.
19170acf5d0bSMark Johnston  */
19180acf5d0bSMark Johnston int
19190acf5d0bSMark Johnston kern_posix_error(struct thread *td, int error)
19200acf5d0bSMark Johnston {
19210acf5d0bSMark Johnston 
19220acf5d0bSMark Johnston 	if (error <= 0)
19230acf5d0bSMark Johnston 		return (error);
19240acf5d0bSMark Johnston 	td->td_errno = error;
19250acf5d0bSMark Johnston 	td->td_pflags |= TDP_NERRNO;
19260acf5d0bSMark Johnston 	td->td_retval[0] = error;
19270acf5d0bSMark Johnston 	return (0);
19280acf5d0bSMark Johnston }
1929