xref: /freebsd/sys/kern/sys_generic.c (revision f1cf2b9dcb9a4e8cf8295e97eeaa4add96e80412)
19454b2d8SWarner Losh /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
7df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
8df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
9df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
2069a28758SEd Maste  * 3. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
36df8bae1dSRodney W. Grimes  *	@(#)sys_generic.c	8.5 (Berkeley) 1/21/94
37df8bae1dSRodney W. Grimes  */
38df8bae1dSRodney W. Grimes 
39677b542eSDavid E. O'Brien #include <sys/cdefs.h>
40677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
41677b542eSDavid E. O'Brien 
42a9d2f8d8SRobert Watson #include "opt_capsicum.h"
43db6a20e2SGarrett Wollman #include "opt_ktrace.h"
44db6a20e2SGarrett Wollman 
45df8bae1dSRodney W. Grimes #include <sys/param.h>
46df8bae1dSRodney W. Grimes #include <sys/systm.h>
47d2d3e875SBruce Evans #include <sys/sysproto.h>
484a144410SRobert Watson #include <sys/capsicum.h>
49df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
5020982410SBruce Evans #include <sys/filio.h>
513ac4d1efSBruce Evans #include <sys/fcntl.h>
52df8bae1dSRodney W. Grimes #include <sys/file.h>
532609222aSPawel Jakub Dawidek #include <sys/lock.h>
54df8bae1dSRodney W. Grimes #include <sys/proc.h>
55797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
56df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
57df8bae1dSRodney W. Grimes #include <sys/uio.h>
58df8bae1dSRodney W. Grimes #include <sys/kernel.h>
59e4650294SJohn Baldwin #include <sys/ktr.h>
60104a9b7eSAlexander Kabaev #include <sys/limits.h>
61df8bae1dSRodney W. Grimes #include <sys/malloc.h>
6242d11757SPeter Wemm #include <sys/poll.h>
6389b71647SPeter Wemm #include <sys/resourcevar.h>
640a2c3d48SGarrett Wollman #include <sys/selinfo.h>
6544f3b092SJohn Baldwin #include <sys/sleepqueue.h>
668f19eb88SIan Dowse #include <sys/syscallsubr.h>
678cb96f20SPeter Wemm #include <sys/sysctl.h>
6842d11757SPeter Wemm #include <sys/sysent.h>
699bbee259SAndrey A. Chernov #include <sys/vnode.h>
70279d7226SMatthew Dillon #include <sys/bio.h>
71279d7226SMatthew Dillon #include <sys/buf.h>
72265fc98fSSeigo Tanimura #include <sys/condvar.h>
73df8bae1dSRodney W. Grimes #ifdef KTRACE
74df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
75df8bae1dSRodney W. Grimes #endif
76df8bae1dSRodney W. Grimes 
77e4650294SJohn Baldwin #include <security/audit/audit.h>
78ace8398dSJeff Roberson 
7950ae6690SHans Petter Selasky /*
8050ae6690SHans Petter Selasky  * The following macro defines how many bytes will be allocated from
8150ae6690SHans Petter Selasky  * the stack instead of memory allocated when passing the IOCTL data
8250ae6690SHans Petter Selasky  * structures from userspace and to the kernel. Some IOCTLs having
8350ae6690SHans Petter Selasky  * small data structures are used very frequently and this small
8450ae6690SHans Petter Selasky  * buffer on the stack gives a significant speedup improvement for
8550ae6690SHans Petter Selasky  * those requests. The value of this define should be greater or equal
8650ae6690SHans Petter Selasky  * to 64 bytes and should also be power of two. The data structure is
8750ae6690SHans Petter Selasky  * currently hard-aligned to a 8-byte boundary on the stack. This
8850ae6690SHans Petter Selasky  * should currently be sufficient for all supported platforms.
8950ae6690SHans Petter Selasky  */
9050ae6690SHans Petter Selasky #define	SYS_IOCTL_SMALL_SIZE	128	/* bytes */
9150ae6690SHans Petter Selasky #define	SYS_IOCTL_SMALL_ALIGN	8	/* bytes */
9250ae6690SHans Petter Selasky 
93611fcff9SJohn Baldwin #ifdef __LP64__
94611fcff9SJohn Baldwin static int iosize_max_clamp = 0;
958bb9a904SKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, iosize_max_clamp, CTLFLAG_RW,
968bb9a904SKonstantin Belousov     &iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX");
97611fcff9SJohn Baldwin static int devfs_iosize_max_clamp = 1;
98bf3e483bSKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, devfs_iosize_max_clamp, CTLFLAG_RW,
99bf3e483bSKonstantin Belousov     &devfs_iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX for devices");
100611fcff9SJohn Baldwin #endif
101bf3e483bSKonstantin Belousov 
1028bb9a904SKonstantin Belousov /*
1038bb9a904SKonstantin Belousov  * Assert that the return value of read(2) and write(2) syscalls fits
1048bb9a904SKonstantin Belousov  * into a register.  If not, an architecture will need to provide the
1058bb9a904SKonstantin Belousov  * usermode wrappers to reconstruct the result.
1068bb9a904SKonstantin Belousov  */
1078bb9a904SKonstantin Belousov CTASSERT(sizeof(register_t) >= sizeof(size_t));
108526d0bd5SKonstantin Belousov 
109a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
110a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
111a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's");
11255166637SPoul-Henning Kamp 
1136d8feddaSKonstantin Belousov static int	pollout(struct thread *, struct pollfd *, struct pollfd *,
1146d8feddaSKonstantin Belousov 		    u_int);
115bbbb04ceSAlfred Perlstein static int	pollscan(struct thread *, struct pollfd *, u_int);
116ace8398dSJeff Roberson static int	pollrescan(struct thread *);
117bbbb04ceSAlfred Perlstein static int	selscan(struct thread *, fd_mask **, fd_mask **, int);
118ace8398dSJeff Roberson static int	selrescan(struct thread *, fd_mask **, fd_mask **);
119ace8398dSJeff Roberson static void	selfdalloc(struct thread *, void *);
120ace8398dSJeff Roberson static void	selfdfree(struct seltd *, struct selfd *);
121bcd9e0ddSJohn Baldwin static int	dofileread(struct thread *, int, struct file *, struct uio *,
122bcd9e0ddSJohn Baldwin 		    off_t, int);
123bcd9e0ddSJohn Baldwin static int	dofilewrite(struct thread *, int, struct file *, struct uio *,
124bcd9e0ddSJohn Baldwin 		    off_t, int);
125512824f8SSeigo Tanimura static void	doselwakeup(struct selinfo *, int);
126ace8398dSJeff Roberson static void	seltdinit(struct thread *);
127cf5e4fe6SDavide Italiano static int	seltdwait(struct thread *, sbintime_t, sbintime_t);
128ace8398dSJeff Roberson static void	seltdclear(struct thread *);
129ace8398dSJeff Roberson 
130ace8398dSJeff Roberson /*
131ace8398dSJeff Roberson  * One seltd per-thread allocated on demand as needed.
132ace8398dSJeff Roberson  *
133ace8398dSJeff Roberson  *	t - protected by st_mtx
134ace8398dSJeff Roberson  * 	k - Only accessed by curthread or read-only
135ace8398dSJeff Roberson  */
136ace8398dSJeff Roberson struct seltd {
137ace8398dSJeff Roberson 	STAILQ_HEAD(, selfd)	st_selq;	/* (k) List of selfds. */
138ace8398dSJeff Roberson 	struct selfd		*st_free1;	/* (k) free fd for read set. */
139ace8398dSJeff Roberson 	struct selfd		*st_free2;	/* (k) free fd for write set. */
140ace8398dSJeff Roberson 	struct mtx		st_mtx;		/* Protects struct seltd */
141ace8398dSJeff Roberson 	struct cv		st_wait;	/* (t) Wait channel. */
142ace8398dSJeff Roberson 	int			st_flags;	/* (t) SELTD_ flags. */
143ace8398dSJeff Roberson };
144ace8398dSJeff Roberson 
145ace8398dSJeff Roberson #define	SELTD_PENDING	0x0001			/* We have pending events. */
146ace8398dSJeff Roberson #define	SELTD_RESCAN	0x0002			/* Doing a rescan. */
147ace8398dSJeff Roberson 
148ace8398dSJeff Roberson /*
149ace8398dSJeff Roberson  * One selfd allocated per-thread per-file-descriptor.
150ace8398dSJeff Roberson  *	f - protected by sf_mtx
151ace8398dSJeff Roberson  */
152ace8398dSJeff Roberson struct selfd {
153ace8398dSJeff Roberson 	STAILQ_ENTRY(selfd)	sf_link;	/* (k) fds owned by this td. */
154ace8398dSJeff Roberson 	TAILQ_ENTRY(selfd)	sf_threads;	/* (f) fds on this selinfo. */
155ace8398dSJeff Roberson 	struct selinfo		*sf_si;		/* (f) selinfo when linked. */
156ace8398dSJeff Roberson 	struct mtx		*sf_mtx;	/* Pointer to selinfo mtx. */
157ace8398dSJeff Roberson 	struct seltd		*sf_td;		/* (k) owning seltd. */
158ace8398dSJeff Roberson 	void			*sf_cookie;	/* (k) fd or pollfd. */
159fcb5b3a4SKonstantin Belousov 	u_int			sf_refs;
160ace8398dSJeff Roberson };
161ace8398dSJeff Roberson 
162ace8398dSJeff Roberson static uma_zone_t selfd_zone;
1632141453eSJeff Roberson static struct mtx_pool *mtxpool_select;
1648fe387abSDmitrij Tejblum 
165611fcff9SJohn Baldwin #ifdef __LP64__
166611fcff9SJohn Baldwin size_t
167611fcff9SJohn Baldwin devfs_iosize_max(void)
168611fcff9SJohn Baldwin {
169611fcff9SJohn Baldwin 
170611fcff9SJohn Baldwin 	return (devfs_iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ?
171611fcff9SJohn Baldwin 	    INT_MAX : SSIZE_MAX);
172611fcff9SJohn Baldwin }
173611fcff9SJohn Baldwin 
174611fcff9SJohn Baldwin size_t
175611fcff9SJohn Baldwin iosize_max(void)
176611fcff9SJohn Baldwin {
177611fcff9SJohn Baldwin 
178611fcff9SJohn Baldwin 	return (iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ?
179611fcff9SJohn Baldwin 	    INT_MAX : SSIZE_MAX);
180611fcff9SJohn Baldwin }
181611fcff9SJohn Baldwin #endif
182611fcff9SJohn Baldwin 
183d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
184df8bae1dSRodney W. Grimes struct read_args {
185df8bae1dSRodney W. Grimes 	int	fd;
186134e06feSBruce Evans 	void	*buf;
187134e06feSBruce Evans 	size_t	nbyte;
188df8bae1dSRodney W. Grimes };
189d2d3e875SBruce Evans #endif
19026f9a767SRodney W. Grimes int
191cc3c9df8SEd Maste sys_read(struct thread *td, struct read_args *uap)
192df8bae1dSRodney W. Grimes {
193bcd9e0ddSJohn Baldwin 	struct uio auio;
194bcd9e0ddSJohn Baldwin 	struct iovec aiov;
195279d7226SMatthew Dillon 	int error;
196df8bae1dSRodney W. Grimes 
197526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
198bcd9e0ddSJohn Baldwin 		return (EINVAL);
199bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
200bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
201bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
202bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
203bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
204bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
205bcd9e0ddSJohn Baldwin 	error = kern_readv(td, uap->fd, &auio);
206279d7226SMatthew Dillon 	return (error);
207df8bae1dSRodney W. Grimes }
208df8bae1dSRodney W. Grimes 
209df8bae1dSRodney W. Grimes /*
210bcd9e0ddSJohn Baldwin  * Positioned read system call
2114160ccd9SAlan Cox  */
2124160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
2134160ccd9SAlan Cox struct pread_args {
2144160ccd9SAlan Cox 	int	fd;
2154160ccd9SAlan Cox 	void	*buf;
2164160ccd9SAlan Cox 	size_t	nbyte;
2178fe387abSDmitrij Tejblum 	int	pad;
2184160ccd9SAlan Cox 	off_t	offset;
2194160ccd9SAlan Cox };
2204160ccd9SAlan Cox #endif
2214160ccd9SAlan Cox int
222b38b22b0SEdward Tomasz Napierala sys_pread(struct thread *td, struct pread_args *uap)
223b38b22b0SEdward Tomasz Napierala {
224b38b22b0SEdward Tomasz Napierala 
225b38b22b0SEdward Tomasz Napierala 	return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
226b38b22b0SEdward Tomasz Napierala }
227b38b22b0SEdward Tomasz Napierala 
228b38b22b0SEdward Tomasz Napierala int
229b38b22b0SEdward Tomasz Napierala kern_pread(struct thread *td, int fd, void *buf, size_t nbyte, off_t offset)
2304160ccd9SAlan Cox {
2314160ccd9SAlan Cox 	struct uio auio;
2324160ccd9SAlan Cox 	struct iovec aiov;
233bcd9e0ddSJohn Baldwin 	int error;
2344160ccd9SAlan Cox 
235b38b22b0SEdward Tomasz Napierala 	if (nbyte > IOSIZE_MAX)
236bcd9e0ddSJohn Baldwin 		return (EINVAL);
237b38b22b0SEdward Tomasz Napierala 	aiov.iov_base = buf;
238b38b22b0SEdward Tomasz Napierala 	aiov.iov_len = nbyte;
2394160ccd9SAlan Cox 	auio.uio_iov = &aiov;
2404160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
241b38b22b0SEdward Tomasz Napierala 	auio.uio_resid = nbyte;
2424160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
243b38b22b0SEdward Tomasz Napierala 	error = kern_preadv(td, fd, &auio, offset);
2444160ccd9SAlan Cox 	return (error);
2454160ccd9SAlan Cox }
2464160ccd9SAlan Cox 
2470538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6)
248c2815ad5SPeter Wemm int
249b38b22b0SEdward Tomasz Napierala freebsd6_pread(struct thread *td, struct freebsd6_pread_args *uap)
250c2815ad5SPeter Wemm {
251c2815ad5SPeter Wemm 
252b38b22b0SEdward Tomasz Napierala 	return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
253c2815ad5SPeter Wemm }
2540538aafcSKonstantin Belousov #endif
255c2815ad5SPeter Wemm 
2564160ccd9SAlan Cox /*
257df8bae1dSRodney W. Grimes  * Scatter read system call.
258df8bae1dSRodney W. Grimes  */
259d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
260df8bae1dSRodney W. Grimes struct readv_args {
2617147b19dSBruce Evans 	int	fd;
262df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
263df8bae1dSRodney W. Grimes 	u_int	iovcnt;
264df8bae1dSRodney W. Grimes };
265d2d3e875SBruce Evans #endif
26626f9a767SRodney W. Grimes int
2678451d0ddSKip Macy sys_readv(struct thread *td, struct readv_args *uap)
268df8bae1dSRodney W. Grimes {
269b88ec951SJohn Baldwin 	struct uio *auio;
270b88ec951SJohn Baldwin 	int error;
271b88ec951SJohn Baldwin 
272b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
273b88ec951SJohn Baldwin 	if (error)
274b88ec951SJohn Baldwin 		return (error);
275b88ec951SJohn Baldwin 	error = kern_readv(td, uap->fd, auio);
276b88ec951SJohn Baldwin 	free(auio, M_IOV);
277b88ec951SJohn Baldwin 	return (error);
278b88ec951SJohn Baldwin }
279b88ec951SJohn Baldwin 
280b88ec951SJohn Baldwin int
281b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio)
282b88ec951SJohn Baldwin {
283b064d43dSMatthew Dillon 	struct file *fp;
284bcd9e0ddSJohn Baldwin 	int error;
285bcd9e0ddSJohn Baldwin 
286cbd92ce6SMatt Macy 	error = fget_read(td, fd, &cap_read_rights, &fp);
287bcd9e0ddSJohn Baldwin 	if (error)
288bcd9e0ddSJohn Baldwin 		return (error);
289bcd9e0ddSJohn Baldwin 	error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
290bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
291bcd9e0ddSJohn Baldwin 	return (error);
292bcd9e0ddSJohn Baldwin }
293bcd9e0ddSJohn Baldwin 
294bcd9e0ddSJohn Baldwin /*
295bcd9e0ddSJohn Baldwin  * Scatter positioned read system call.
296bcd9e0ddSJohn Baldwin  */
297bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
298bcd9e0ddSJohn Baldwin struct preadv_args {
299bcd9e0ddSJohn Baldwin 	int	fd;
300bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
301bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
302bcd9e0ddSJohn Baldwin 	off_t	offset;
303bcd9e0ddSJohn Baldwin };
304bcd9e0ddSJohn Baldwin #endif
305bcd9e0ddSJohn Baldwin int
3068451d0ddSKip Macy sys_preadv(struct thread *td, struct preadv_args *uap)
307bcd9e0ddSJohn Baldwin {
308bcd9e0ddSJohn Baldwin 	struct uio *auio;
309bcd9e0ddSJohn Baldwin 	int error;
310bcd9e0ddSJohn Baldwin 
311bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
312bcd9e0ddSJohn Baldwin 	if (error)
313bcd9e0ddSJohn Baldwin 		return (error);
314bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, auio, uap->offset);
315bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
316bcd9e0ddSJohn Baldwin 	return (error);
317bcd9e0ddSJohn Baldwin }
318bcd9e0ddSJohn Baldwin 
319bcd9e0ddSJohn Baldwin int
320cc3c9df8SEd Maste kern_preadv(struct thread *td, int fd, struct uio *auio, off_t offset)
321bcd9e0ddSJohn Baldwin {
322bcd9e0ddSJohn Baldwin 	struct file *fp;
323bcd9e0ddSJohn Baldwin 	int error;
324bcd9e0ddSJohn Baldwin 
325cbd92ce6SMatt Macy 	error = fget_read(td, fd, &cap_pread_rights, &fp);
326bcd9e0ddSJohn Baldwin 	if (error)
327bcd9e0ddSJohn Baldwin 		return (error);
328bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
329bcd9e0ddSJohn Baldwin 		error = ESPIPE;
33032a1fb0dSMahdi Mokhtari 	else if (offset < 0 &&
33132a1fb0dSMahdi Mokhtari 	    (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR))
332bcd9e0ddSJohn Baldwin 		error = EINVAL;
333bcd9e0ddSJohn Baldwin 	else
334bcd9e0ddSJohn Baldwin 		error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
335bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
336bcd9e0ddSJohn Baldwin 	return (error);
337bcd9e0ddSJohn Baldwin }
338bcd9e0ddSJohn Baldwin 
339bcd9e0ddSJohn Baldwin /*
340bcd9e0ddSJohn Baldwin  * Common code for readv and preadv that reads data in
341bcd9e0ddSJohn Baldwin  * from a file using the passed in uio, offset, and flags.
342bcd9e0ddSJohn Baldwin  */
343bcd9e0ddSJohn Baldwin static int
344cc3c9df8SEd Maste dofileread(struct thread *td, int fd, struct file *fp, struct uio *auio,
345cc3c9df8SEd Maste     off_t offset, int flags)
346bcd9e0ddSJohn Baldwin {
347bcd9e0ddSJohn Baldwin 	ssize_t cnt;
34882641acdSAlan Cox 	int error;
349df8bae1dSRodney W. Grimes #ifdef KTRACE
350552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
351df8bae1dSRodney W. Grimes #endif
352df8bae1dSRodney W. Grimes 
35351d1f690SRobert Watson 	AUDIT_ARG_FD(fd);
35451d1f690SRobert Watson 
3554f8d23d6SPoul-Henning Kamp 	/* Finish zero length reads right here */
3564f8d23d6SPoul-Henning Kamp 	if (auio->uio_resid == 0) {
3574f8d23d6SPoul-Henning Kamp 		td->td_retval[0] = 0;
3584f8d23d6SPoul-Henning Kamp 		return (0);
3594f8d23d6SPoul-Henning Kamp 	}
360552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_READ;
361bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
362552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
363df8bae1dSRodney W. Grimes #ifdef KTRACE
364552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
365552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
366df8bae1dSRodney W. Grimes #endif
367552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
368bcd9e0ddSJohn Baldwin 	if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
369552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
370df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
371df8bae1dSRodney W. Grimes 			error = 0;
372279d7226SMatthew Dillon 	}
373552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
374df8bae1dSRodney W. Grimes #ifdef KTRACE
375552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
376552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
377b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_READ, ktruio, error);
378df8bae1dSRodney W. Grimes 	}
379df8bae1dSRodney W. Grimes #endif
380b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
381df8bae1dSRodney W. Grimes 	return (error);
382df8bae1dSRodney W. Grimes }
383df8bae1dSRodney W. Grimes 
384d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
385df8bae1dSRodney W. Grimes struct write_args {
386df8bae1dSRodney W. Grimes 	int	fd;
387134e06feSBruce Evans 	const void *buf;
388134e06feSBruce Evans 	size_t	nbyte;
389df8bae1dSRodney W. Grimes };
390d2d3e875SBruce Evans #endif
39126f9a767SRodney W. Grimes int
392cc3c9df8SEd Maste sys_write(struct thread *td, struct write_args *uap)
393df8bae1dSRodney W. Grimes {
394bcd9e0ddSJohn Baldwin 	struct uio auio;
395bcd9e0ddSJohn Baldwin 	struct iovec aiov;
396279d7226SMatthew Dillon 	int error;
397df8bae1dSRodney W. Grimes 
398526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
399bcd9e0ddSJohn Baldwin 		return (EINVAL);
400bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
401bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
402bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
403bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
404bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
405bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
406bcd9e0ddSJohn Baldwin 	error = kern_writev(td, uap->fd, &auio);
407279d7226SMatthew Dillon 	return (error);
408df8bae1dSRodney W. Grimes }
409df8bae1dSRodney W. Grimes 
410df8bae1dSRodney W. Grimes /*
4110c14ff0eSRobert Watson  * Positioned write system call.
4124160ccd9SAlan Cox  */
4134160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
4144160ccd9SAlan Cox struct pwrite_args {
4154160ccd9SAlan Cox 	int	fd;
4164160ccd9SAlan Cox 	const void *buf;
4174160ccd9SAlan Cox 	size_t	nbyte;
4188fe387abSDmitrij Tejblum 	int	pad;
4194160ccd9SAlan Cox 	off_t	offset;
4204160ccd9SAlan Cox };
4214160ccd9SAlan Cox #endif
4224160ccd9SAlan Cox int
423b38b22b0SEdward Tomasz Napierala sys_pwrite(struct thread *td, struct pwrite_args *uap)
424b38b22b0SEdward Tomasz Napierala {
425b38b22b0SEdward Tomasz Napierala 
426b38b22b0SEdward Tomasz Napierala 	return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
427b38b22b0SEdward Tomasz Napierala }
428b38b22b0SEdward Tomasz Napierala 
429b38b22b0SEdward Tomasz Napierala int
430b38b22b0SEdward Tomasz Napierala kern_pwrite(struct thread *td, int fd, const void *buf, size_t nbyte,
431b38b22b0SEdward Tomasz Napierala     off_t offset)
4324160ccd9SAlan Cox {
4334160ccd9SAlan Cox 	struct uio auio;
4344160ccd9SAlan Cox 	struct iovec aiov;
435bcd9e0ddSJohn Baldwin 	int error;
4364160ccd9SAlan Cox 
437b38b22b0SEdward Tomasz Napierala 	if (nbyte > IOSIZE_MAX)
438bcd9e0ddSJohn Baldwin 		return (EINVAL);
439b38b22b0SEdward Tomasz Napierala 	aiov.iov_base = (void *)(uintptr_t)buf;
440b38b22b0SEdward Tomasz Napierala 	aiov.iov_len = nbyte;
4414160ccd9SAlan Cox 	auio.uio_iov = &aiov;
4424160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
443b38b22b0SEdward Tomasz Napierala 	auio.uio_resid = nbyte;
4444160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
445b38b22b0SEdward Tomasz Napierala 	error = kern_pwritev(td, fd, &auio, offset);
4464160ccd9SAlan Cox 	return (error);
4474160ccd9SAlan Cox }
4484160ccd9SAlan Cox 
4490538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6)
450c2815ad5SPeter Wemm int
451b38b22b0SEdward Tomasz Napierala freebsd6_pwrite(struct thread *td, struct freebsd6_pwrite_args *uap)
452c2815ad5SPeter Wemm {
453c2815ad5SPeter Wemm 
454b38b22b0SEdward Tomasz Napierala 	return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
455c2815ad5SPeter Wemm }
4560538aafcSKonstantin Belousov #endif
457c2815ad5SPeter Wemm 
4584160ccd9SAlan Cox /*
4590c14ff0eSRobert Watson  * Gather write system call.
460df8bae1dSRodney W. Grimes  */
461d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
462df8bae1dSRodney W. Grimes struct writev_args {
463df8bae1dSRodney W. Grimes 	int	fd;
464df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
465df8bae1dSRodney W. Grimes 	u_int	iovcnt;
466df8bae1dSRodney W. Grimes };
467d2d3e875SBruce Evans #endif
46826f9a767SRodney W. Grimes int
4698451d0ddSKip Macy sys_writev(struct thread *td, struct writev_args *uap)
470df8bae1dSRodney W. Grimes {
471b88ec951SJohn Baldwin 	struct uio *auio;
472b88ec951SJohn Baldwin 	int error;
473b88ec951SJohn Baldwin 
474b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
475b88ec951SJohn Baldwin 	if (error)
476b88ec951SJohn Baldwin 		return (error);
477b88ec951SJohn Baldwin 	error = kern_writev(td, uap->fd, auio);
478b88ec951SJohn Baldwin 	free(auio, M_IOV);
479b88ec951SJohn Baldwin 	return (error);
480b88ec951SJohn Baldwin }
481b88ec951SJohn Baldwin 
482b88ec951SJohn Baldwin int
483b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio)
484b88ec951SJohn Baldwin {
485b064d43dSMatthew Dillon 	struct file *fp;
486bcd9e0ddSJohn Baldwin 	int error;
487bcd9e0ddSJohn Baldwin 
488cbd92ce6SMatt Macy 	error = fget_write(td, fd, &cap_write_rights, &fp);
489bcd9e0ddSJohn Baldwin 	if (error)
490af56abaaSJohn Baldwin 		return (error);
491bcd9e0ddSJohn Baldwin 	error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
492bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
493bcd9e0ddSJohn Baldwin 	return (error);
494bcd9e0ddSJohn Baldwin }
495bcd9e0ddSJohn Baldwin 
496bcd9e0ddSJohn Baldwin /*
4970c14ff0eSRobert Watson  * Gather positioned write system call.
498bcd9e0ddSJohn Baldwin  */
499bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
500bcd9e0ddSJohn Baldwin struct pwritev_args {
501bcd9e0ddSJohn Baldwin 	int	fd;
502bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
503bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
504bcd9e0ddSJohn Baldwin 	off_t	offset;
505bcd9e0ddSJohn Baldwin };
506bcd9e0ddSJohn Baldwin #endif
507bcd9e0ddSJohn Baldwin int
5088451d0ddSKip Macy sys_pwritev(struct thread *td, struct pwritev_args *uap)
509bcd9e0ddSJohn Baldwin {
510bcd9e0ddSJohn Baldwin 	struct uio *auio;
511bcd9e0ddSJohn Baldwin 	int error;
512bcd9e0ddSJohn Baldwin 
513bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
514bcd9e0ddSJohn Baldwin 	if (error)
515bcd9e0ddSJohn Baldwin 		return (error);
516bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, auio, uap->offset);
517bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
518bcd9e0ddSJohn Baldwin 	return (error);
519bcd9e0ddSJohn Baldwin }
520bcd9e0ddSJohn Baldwin 
521bcd9e0ddSJohn Baldwin int
522d5cdcc3aSAndrew Gallatin kern_pwritev(struct thread *td, int fd, struct uio *auio, off_t offset)
523bcd9e0ddSJohn Baldwin {
524bcd9e0ddSJohn Baldwin 	struct file *fp;
525bcd9e0ddSJohn Baldwin 	int error;
526bcd9e0ddSJohn Baldwin 
527cbd92ce6SMatt Macy 	error = fget_write(td, fd, &cap_pwrite_rights, &fp);
528bcd9e0ddSJohn Baldwin 	if (error)
529af56abaaSJohn Baldwin 		return (error);
530bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
531bcd9e0ddSJohn Baldwin 		error = ESPIPE;
53232a1fb0dSMahdi Mokhtari 	else if (offset < 0 &&
53332a1fb0dSMahdi Mokhtari 	    (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR))
534bcd9e0ddSJohn Baldwin 		error = EINVAL;
535bcd9e0ddSJohn Baldwin 	else
536bcd9e0ddSJohn Baldwin 		error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
537bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
538bcd9e0ddSJohn Baldwin 	return (error);
539bcd9e0ddSJohn Baldwin }
540bcd9e0ddSJohn Baldwin 
541bcd9e0ddSJohn Baldwin /*
542bcd9e0ddSJohn Baldwin  * Common code for writev and pwritev that writes data to
543bcd9e0ddSJohn Baldwin  * a file using the passed in uio, offset, and flags.
544bcd9e0ddSJohn Baldwin  */
545bcd9e0ddSJohn Baldwin static int
546cc3c9df8SEd Maste dofilewrite(struct thread *td, int fd, struct file *fp, struct uio *auio,
547cc3c9df8SEd Maste     off_t offset, int flags)
548bcd9e0ddSJohn Baldwin {
549bcd9e0ddSJohn Baldwin 	ssize_t cnt;
550552afd9cSPoul-Henning Kamp 	int error;
551df8bae1dSRodney W. Grimes #ifdef KTRACE
552552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
553df8bae1dSRodney W. Grimes #endif
554df8bae1dSRodney W. Grimes 
55551d1f690SRobert Watson 	AUDIT_ARG_FD(fd);
556552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_WRITE;
557552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
558bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
559df8bae1dSRodney W. Grimes #ifdef KTRACE
560552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
561552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
562df8bae1dSRodney W. Grimes #endif
563552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
564ad9789f6SKonstantin Belousov 	if (fp->f_type == DTYPE_VNODE &&
565ad9789f6SKonstantin Belousov 	    (fp->f_vnread_flags & FDEVFS_VNODE) == 0)
5669440653dSMatthew Dillon 		bwillwrite();
567bcd9e0ddSJohn Baldwin 	if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
568552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
569df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
570df8bae1dSRodney W. Grimes 			error = 0;
571bcd9e0ddSJohn Baldwin 		/* Socket layer is responsible for issuing SIGPIPE. */
5726f7ca813SBruce M Simpson 		if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
573b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
5747a6f3d78SJohn Baldwin 			tdsignal(td, SIGPIPE);
575b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
57619eb87d2SJohn Baldwin 		}
577df8bae1dSRodney W. Grimes 	}
578552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
579df8bae1dSRodney W. Grimes #ifdef KTRACE
580552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
581552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
582b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_WRITE, ktruio, error);
583df8bae1dSRodney W. Grimes 	}
584df8bae1dSRodney W. Grimes #endif
585b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
586df8bae1dSRodney W. Grimes 	return (error);
587df8bae1dSRodney W. Grimes }
588df8bae1dSRodney W. Grimes 
589e4650294SJohn Baldwin /*
590e4650294SJohn Baldwin  * Truncate a file given a file descriptor.
591e4650294SJohn Baldwin  *
592e4650294SJohn Baldwin  * Can't use fget_write() here, since must return EINVAL and not EBADF if the
593e4650294SJohn Baldwin  * descriptor isn't writable.
594e4650294SJohn Baldwin  */
595e4650294SJohn Baldwin int
596cc3c9df8SEd Maste kern_ftruncate(struct thread *td, int fd, off_t length)
597e4650294SJohn Baldwin {
598e4650294SJohn Baldwin 	struct file *fp;
599e4650294SJohn Baldwin 	int error;
600e4650294SJohn Baldwin 
60114961ba7SRobert Watson 	AUDIT_ARG_FD(fd);
602e4650294SJohn Baldwin 	if (length < 0)
603e4650294SJohn Baldwin 		return (EINVAL);
604cbd92ce6SMatt Macy 	error = fget(td, fd, &cap_ftruncate_rights, &fp);
605e4650294SJohn Baldwin 	if (error)
606e4650294SJohn Baldwin 		return (error);
60714961ba7SRobert Watson 	AUDIT_ARG_FILE(td->td_proc, fp);
608e4650294SJohn Baldwin 	if (!(fp->f_flag & FWRITE)) {
609e4650294SJohn Baldwin 		fdrop(fp, td);
610e4650294SJohn Baldwin 		return (EINVAL);
611e4650294SJohn Baldwin 	}
612e4650294SJohn Baldwin 	error = fo_truncate(fp, length, td->td_ucred, td);
613e4650294SJohn Baldwin 	fdrop(fp, td);
614e4650294SJohn Baldwin 	return (error);
615e4650294SJohn Baldwin }
616e4650294SJohn Baldwin 
617e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
618e4650294SJohn Baldwin struct ftruncate_args {
619e4650294SJohn Baldwin 	int	fd;
620e4650294SJohn Baldwin 	int	pad;
621e4650294SJohn Baldwin 	off_t	length;
622e4650294SJohn Baldwin };
623e4650294SJohn Baldwin #endif
624e4650294SJohn Baldwin int
625cc3c9df8SEd Maste sys_ftruncate(struct thread *td, struct ftruncate_args *uap)
626e4650294SJohn Baldwin {
627e4650294SJohn Baldwin 
628e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
629e4650294SJohn Baldwin }
630e4650294SJohn Baldwin 
631e4650294SJohn Baldwin #if defined(COMPAT_43)
632e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
633e4650294SJohn Baldwin struct oftruncate_args {
634e4650294SJohn Baldwin 	int	fd;
635e4650294SJohn Baldwin 	long	length;
636e4650294SJohn Baldwin };
637e4650294SJohn Baldwin #endif
638e4650294SJohn Baldwin int
639cc3c9df8SEd Maste oftruncate(struct thread *td, struct oftruncate_args *uap)
640e4650294SJohn Baldwin {
641e4650294SJohn Baldwin 
642e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
643e4650294SJohn Baldwin }
644e4650294SJohn Baldwin #endif /* COMPAT_43 */
645e4650294SJohn Baldwin 
646d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
647df8bae1dSRodney W. Grimes struct ioctl_args {
648df8bae1dSRodney W. Grimes 	int	fd;
649069e9bc1SDoug Rabson 	u_long	com;
650df8bae1dSRodney W. Grimes 	caddr_t	data;
651df8bae1dSRodney W. Grimes };
652d2d3e875SBruce Evans #endif
653df8bae1dSRodney W. Grimes /* ARGSUSED */
65426f9a767SRodney W. Grimes int
6558451d0ddSKip Macy sys_ioctl(struct thread *td, struct ioctl_args *uap)
656df8bae1dSRodney W. Grimes {
65750ae6690SHans Petter Selasky 	u_char smalldata[SYS_IOCTL_SMALL_SIZE] __aligned(SYS_IOCTL_SMALL_ALIGN);
6583e15c66fSPoul-Henning Kamp 	u_long com;
6599fddcc66SRuslan Ermilov 	int arg, error;
6603e15c66fSPoul-Henning Kamp 	u_int size;
6619fddcc66SRuslan Ermilov 	caddr_t data;
662df8bae1dSRodney W. Grimes 
6639fc6aa06SPoul-Henning Kamp 	if (uap->com > 0xffffffff) {
6649fc6aa06SPoul-Henning Kamp 		printf(
6659fc6aa06SPoul-Henning Kamp 		    "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
666431f8906SJulian Elischer 		    td->td_proc->p_pid, td->td_name, uap->com);
6679fc6aa06SPoul-Henning Kamp 		uap->com &= 0xffffffff;
6689fc6aa06SPoul-Henning Kamp 	}
669d9f46233SJohn Baldwin 	com = uap->com;
670df8bae1dSRodney W. Grimes 
671df8bae1dSRodney W. Grimes 	/*
672df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
673df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
674df8bae1dSRodney W. Grimes 	 */
675df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
676ca51b19bSPoul-Henning Kamp 	if ((size > IOCPARM_MAX) ||
677ca51b19bSPoul-Henning Kamp 	    ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
6782de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
6792de92a38SPeter Wemm 	    ((com & IOC_OUT) && size == 0) ||
6802de92a38SPeter Wemm #else
6812de92a38SPeter Wemm 	    ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
6822de92a38SPeter Wemm #endif
6839fddcc66SRuslan Ermilov 	    ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
684426da3bcSAlfred Perlstein 		return (ENOTTY);
685279d7226SMatthew Dillon 
686ca51b19bSPoul-Henning Kamp 	if (size > 0) {
687715457f6SDavid E. O'Brien 		if (com & IOC_VOID) {
6889fddcc66SRuslan Ermilov 			/* Integer argument. */
6899fddcc66SRuslan Ermilov 			arg = (intptr_t)uap->data;
6909fddcc66SRuslan Ermilov 			data = (void *)&arg;
6919fddcc66SRuslan Ermilov 			size = 0;
6925cbf44bfSMateusz Guzik 		} else {
6930ecd606bSHans Petter Selasky 			if (size > SYS_IOCTL_SMALL_SIZE)
694715457f6SDavid E. O'Brien 				data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
6950ecd606bSHans Petter Selasky 			else
6960ecd606bSHans Petter Selasky 				data = smalldata;
6975cbf44bfSMateusz Guzik 		}
6989fddcc66SRuslan Ermilov 	} else
6999fddcc66SRuslan Ermilov 		data = (void *)&uap->data;
700df8bae1dSRodney W. Grimes 	if (com & IOC_IN) {
701df8bae1dSRodney W. Grimes 		error = copyin(uap->data, data, (u_int)size);
7025cbf44bfSMateusz Guzik 		if (error != 0)
7035cbf44bfSMateusz Guzik 			goto out;
704ca51b19bSPoul-Henning Kamp 	} else if (com & IOC_OUT) {
705df8bae1dSRodney W. Grimes 		/*
706df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
707df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
708df8bae1dSRodney W. Grimes 		 */
709df8bae1dSRodney W. Grimes 		bzero(data, size);
710279d7226SMatthew Dillon 	}
711df8bae1dSRodney W. Grimes 
712d9f46233SJohn Baldwin 	error = kern_ioctl(td, uap->fd, com, data);
713d9f46233SJohn Baldwin 
714d9f46233SJohn Baldwin 	if (error == 0 && (com & IOC_OUT))
715d9f46233SJohn Baldwin 		error = copyout(data, uap->data, (u_int)size);
716d9f46233SJohn Baldwin 
7175cbf44bfSMateusz Guzik out:
7180ecd606bSHans Petter Selasky 	if (size > SYS_IOCTL_SMALL_SIZE)
7199fddcc66SRuslan Ermilov 		free(data, M_IOCTLOPS);
720d9f46233SJohn Baldwin 	return (error);
721d9f46233SJohn Baldwin }
722d9f46233SJohn Baldwin 
723d9f46233SJohn Baldwin int
724d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
725d9f46233SJohn Baldwin {
726d9f46233SJohn Baldwin 	struct file *fp;
727d9f46233SJohn Baldwin 	struct filedesc *fdp;
7282609222aSPawel Jakub Dawidek 	int error, tmp, locked;
729d9f46233SJohn Baldwin 
73064c9a4d9SRobert Watson 	AUDIT_ARG_FD(fd);
73164c9a4d9SRobert Watson 	AUDIT_ARG_CMD(com);
7322609222aSPawel Jakub Dawidek 
733d9f46233SJohn Baldwin 	fdp = td->td_proc->p_fd;
7342609222aSPawel Jakub Dawidek 
735d9f46233SJohn Baldwin 	switch (com) {
736d9f46233SJohn Baldwin 	case FIONCLEX:
737d9f46233SJohn Baldwin 	case FIOCLEX:
7385e3f7694SRobert Watson 		FILEDESC_XLOCK(fdp);
7392609222aSPawel Jakub Dawidek 		locked = LA_XLOCKED;
7402609222aSPawel Jakub Dawidek 		break;
7412609222aSPawel Jakub Dawidek 	default:
7422609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
7432609222aSPawel Jakub Dawidek 		FILEDESC_SLOCK(fdp);
7442609222aSPawel Jakub Dawidek 		locked = LA_SLOCKED;
7452609222aSPawel Jakub Dawidek #else
7462609222aSPawel Jakub Dawidek 		locked = LA_UNLOCKED;
7472609222aSPawel Jakub Dawidek #endif
7482609222aSPawel Jakub Dawidek 		break;
7492609222aSPawel Jakub Dawidek 	}
7502609222aSPawel Jakub Dawidek 
7512609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
7522609222aSPawel Jakub Dawidek 	if ((fp = fget_locked(fdp, fd)) == NULL) {
7532609222aSPawel Jakub Dawidek 		error = EBADF;
7542609222aSPawel Jakub Dawidek 		goto out;
7552609222aSPawel Jakub Dawidek 	}
7562609222aSPawel Jakub Dawidek 	if ((error = cap_ioctl_check(fdp, fd, com)) != 0) {
7572609222aSPawel Jakub Dawidek 		fp = NULL;	/* fhold() was not called yet */
7582609222aSPawel Jakub Dawidek 		goto out;
7592609222aSPawel Jakub Dawidek 	}
760*f1cf2b9dSKonstantin Belousov 	if (!fhold(fp)) {
761*f1cf2b9dSKonstantin Belousov 		error = EBADF;
762*f1cf2b9dSKonstantin Belousov 		fp = NULL;
763*f1cf2b9dSKonstantin Belousov 		goto out;
764*f1cf2b9dSKonstantin Belousov 	}
7652609222aSPawel Jakub Dawidek 	if (locked == LA_SLOCKED) {
7662609222aSPawel Jakub Dawidek 		FILEDESC_SUNLOCK(fdp);
7672609222aSPawel Jakub Dawidek 		locked = LA_UNLOCKED;
7682609222aSPawel Jakub Dawidek 	}
7692609222aSPawel Jakub Dawidek #else
770cbd92ce6SMatt Macy 	error = fget(td, fd, &cap_ioctl_rights, &fp);
7717008be5bSPawel Jakub Dawidek 	if (error != 0) {
7722609222aSPawel Jakub Dawidek 		fp = NULL;
7732609222aSPawel Jakub Dawidek 		goto out;
7742609222aSPawel Jakub Dawidek 	}
7752609222aSPawel Jakub Dawidek #endif
7762609222aSPawel Jakub Dawidek 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
7772609222aSPawel Jakub Dawidek 		error = EBADF;
7782609222aSPawel Jakub Dawidek 		goto out;
7792609222aSPawel Jakub Dawidek 	}
7802609222aSPawel Jakub Dawidek 
7812609222aSPawel Jakub Dawidek 	switch (com) {
7822609222aSPawel Jakub Dawidek 	case FIONCLEX:
7832609222aSPawel Jakub Dawidek 		fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE;
7842609222aSPawel Jakub Dawidek 		goto out;
7852609222aSPawel Jakub Dawidek 	case FIOCLEX:
7862609222aSPawel Jakub Dawidek 		fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE;
787d9f46233SJohn Baldwin 		goto out;
788d9f46233SJohn Baldwin 	case FIONBIO:
789bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
790397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FNONBLOCK);
791df8bae1dSRodney W. Grimes 		else
792397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FNONBLOCK);
7938ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
794d9f46233SJohn Baldwin 		break;
795d9f46233SJohn Baldwin 	case FIOASYNC:
796bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
797397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FASYNC);
798df8bae1dSRodney W. Grimes 		else
799397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FASYNC);
8008ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
801d9f46233SJohn Baldwin 		break;
802df8bae1dSRodney W. Grimes 	}
8038ccf264fSPoul-Henning Kamp 
8048ccf264fSPoul-Henning Kamp 	error = fo_ioctl(fp, com, data, td->td_ucred, td);
805d9f46233SJohn Baldwin out:
8062609222aSPawel Jakub Dawidek 	switch (locked) {
8072609222aSPawel Jakub Dawidek 	case LA_XLOCKED:
8082609222aSPawel Jakub Dawidek 		FILEDESC_XUNLOCK(fdp);
8092609222aSPawel Jakub Dawidek 		break;
8102609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
8112609222aSPawel Jakub Dawidek 	case LA_SLOCKED:
8122609222aSPawel Jakub Dawidek 		FILEDESC_SUNLOCK(fdp);
8132609222aSPawel Jakub Dawidek 		break;
8142609222aSPawel Jakub Dawidek #endif
8152609222aSPawel Jakub Dawidek 	default:
8162609222aSPawel Jakub Dawidek 		FILEDESC_UNLOCK_ASSERT(fdp);
8172609222aSPawel Jakub Dawidek 		break;
8182609222aSPawel Jakub Dawidek 	}
8192609222aSPawel Jakub Dawidek 	if (fp != NULL)
820b40ce416SJulian Elischer 		fdrop(fp, td);
821df8bae1dSRodney W. Grimes 	return (error);
822df8bae1dSRodney W. Grimes }
823df8bae1dSRodney W. Grimes 
824125dcf8cSKonstantin Belousov int
825125dcf8cSKonstantin Belousov poll_no_poll(int events)
826125dcf8cSKonstantin Belousov {
827125dcf8cSKonstantin Belousov 	/*
828125dcf8cSKonstantin Belousov 	 * Return true for read/write.  If the user asked for something
829125dcf8cSKonstantin Belousov 	 * special, return POLLNVAL, so that clients have a way of
830125dcf8cSKonstantin Belousov 	 * determining reliably whether or not the extended
831125dcf8cSKonstantin Belousov 	 * functionality is present without hard-coding knowledge
832125dcf8cSKonstantin Belousov 	 * of specific filesystem implementations.
833125dcf8cSKonstantin Belousov 	 */
834125dcf8cSKonstantin Belousov 	if (events & ~POLLSTANDARD)
835125dcf8cSKonstantin Belousov 		return (POLLNVAL);
836125dcf8cSKonstantin Belousov 
837125dcf8cSKonstantin Belousov 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
838125dcf8cSKonstantin Belousov }
839125dcf8cSKonstantin Belousov 
840066d836bSKonstantin Belousov int
8418451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap)
842066d836bSKonstantin Belousov {
843066d836bSKonstantin Belousov 	struct timespec ts;
844066d836bSKonstantin Belousov 	struct timeval tv, *tvp;
845066d836bSKonstantin Belousov 	sigset_t set, *uset;
846066d836bSKonstantin Belousov 	int error;
847066d836bSKonstantin Belousov 
848066d836bSKonstantin Belousov 	if (uap->ts != NULL) {
849066d836bSKonstantin Belousov 		error = copyin(uap->ts, &ts, sizeof(ts));
850066d836bSKonstantin Belousov 		if (error != 0)
851066d836bSKonstantin Belousov 		    return (error);
852066d836bSKonstantin Belousov 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
853066d836bSKonstantin Belousov 		tvp = &tv;
854066d836bSKonstantin Belousov 	} else
855066d836bSKonstantin Belousov 		tvp = NULL;
856066d836bSKonstantin Belousov 	if (uap->sm != NULL) {
857066d836bSKonstantin Belousov 		error = copyin(uap->sm, &set, sizeof(set));
858066d836bSKonstantin Belousov 		if (error != 0)
859066d836bSKonstantin Belousov 			return (error);
860066d836bSKonstantin Belousov 		uset = &set;
861066d836bSKonstantin Belousov 	} else
862066d836bSKonstantin Belousov 		uset = NULL;
863066d836bSKonstantin Belousov 	return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
864066d836bSKonstantin Belousov 	    uset, NFDBITS));
865066d836bSKonstantin Belousov }
866066d836bSKonstantin Belousov 
867066d836bSKonstantin Belousov int
868066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
869066d836bSKonstantin Belousov     struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
870066d836bSKonstantin Belousov {
871066d836bSKonstantin Belousov 	int error;
872066d836bSKonstantin Belousov 
873066d836bSKonstantin Belousov 	if (uset != NULL) {
874066d836bSKonstantin Belousov 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
875066d836bSKonstantin Belousov 		    &td->td_oldsigmask, 0);
876066d836bSKonstantin Belousov 		if (error != 0)
877066d836bSKonstantin Belousov 			return (error);
878066d836bSKonstantin Belousov 		td->td_pflags |= TDP_OLDMASK;
879066d836bSKonstantin Belousov 		/*
880066d836bSKonstantin Belousov 		 * Make sure that ast() is called on return to
881066d836bSKonstantin Belousov 		 * usermode and TDP_OLDMASK is cleared, restoring old
882066d836bSKonstantin Belousov 		 * sigmask.
883066d836bSKonstantin Belousov 		 */
884066d836bSKonstantin Belousov 		thread_lock(td);
885066d836bSKonstantin Belousov 		td->td_flags |= TDF_ASTPENDING;
886066d836bSKonstantin Belousov 		thread_unlock(td);
887066d836bSKonstantin Belousov 	}
888066d836bSKonstantin Belousov 	error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
889066d836bSKonstantin Belousov 	return (error);
890066d836bSKonstantin Belousov }
891066d836bSKonstantin Belousov 
892d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
893df8bae1dSRodney W. Grimes struct select_args {
894b08f7993SSujal Patel 	int	nd;
895df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
896df8bae1dSRodney W. Grimes 	struct	timeval *tv;
897df8bae1dSRodney W. Grimes };
898d2d3e875SBruce Evans #endif
89926f9a767SRodney W. Grimes int
9008451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap)
901df8bae1dSRodney W. Grimes {
9028f19eb88SIan Dowse 	struct timeval tv, *tvp;
9038f19eb88SIan Dowse 	int error;
9048f19eb88SIan Dowse 
9058f19eb88SIan Dowse 	if (uap->tv != NULL) {
9068f19eb88SIan Dowse 		error = copyin(uap->tv, &tv, sizeof(tv));
9078f19eb88SIan Dowse 		if (error)
9088f19eb88SIan Dowse 			return (error);
9098f19eb88SIan Dowse 		tvp = &tv;
9108f19eb88SIan Dowse 	} else
9118f19eb88SIan Dowse 		tvp = NULL;
9128f19eb88SIan Dowse 
913b55ef216SKonstantin Belousov 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
914b55ef216SKonstantin Belousov 	    NFDBITS));
9158f19eb88SIan Dowse }
9168f19eb88SIan Dowse 
91756be1b9aSKonstantin Belousov /*
91856be1b9aSKonstantin Belousov  * In the unlikely case when user specified n greater then the last
91956be1b9aSKonstantin Belousov  * open file descriptor, check that no bits are set after the last
92056be1b9aSKonstantin Belousov  * valid fd.  We must return EBADF if any is set.
92156be1b9aSKonstantin Belousov  *
92256be1b9aSKonstantin Belousov  * There are applications that rely on the behaviour.
92356be1b9aSKonstantin Belousov  *
92456be1b9aSKonstantin Belousov  * nd is fd_lastfile + 1.
92556be1b9aSKonstantin Belousov  */
92656be1b9aSKonstantin Belousov static int
92756be1b9aSKonstantin Belousov select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits)
92856be1b9aSKonstantin Belousov {
92956be1b9aSKonstantin Belousov 	char *addr, *oaddr;
93056be1b9aSKonstantin Belousov 	int b, i, res;
93156be1b9aSKonstantin Belousov 	uint8_t bits;
93256be1b9aSKonstantin Belousov 
93356be1b9aSKonstantin Belousov 	if (nd >= ndu || fd_in == NULL)
93456be1b9aSKonstantin Belousov 		return (0);
93556be1b9aSKonstantin Belousov 
93656be1b9aSKonstantin Belousov 	oaddr = NULL;
93756be1b9aSKonstantin Belousov 	bits = 0; /* silence gcc */
93856be1b9aSKonstantin Belousov 	for (i = nd; i < ndu; i++) {
93956be1b9aSKonstantin Belousov 		b = i / NBBY;
94056be1b9aSKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN
94156be1b9aSKonstantin Belousov 		addr = (char *)fd_in + b;
94256be1b9aSKonstantin Belousov #else
94356be1b9aSKonstantin Belousov 		addr = (char *)fd_in;
94456be1b9aSKonstantin Belousov 		if (abi_nfdbits == NFDBITS) {
94556be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(fd_mask)) +
94656be1b9aSKonstantin Belousov 			    sizeof(fd_mask) - 1 - b % sizeof(fd_mask);
94756be1b9aSKonstantin Belousov 		} else {
94856be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(uint32_t)) +
94956be1b9aSKonstantin Belousov 			    sizeof(uint32_t) - 1 - b % sizeof(uint32_t);
95056be1b9aSKonstantin Belousov 		}
95156be1b9aSKonstantin Belousov #endif
95256be1b9aSKonstantin Belousov 		if (addr != oaddr) {
95356be1b9aSKonstantin Belousov 			res = fubyte(addr);
95456be1b9aSKonstantin Belousov 			if (res == -1)
95556be1b9aSKonstantin Belousov 				return (EFAULT);
95656be1b9aSKonstantin Belousov 			oaddr = addr;
95756be1b9aSKonstantin Belousov 			bits = res;
95856be1b9aSKonstantin Belousov 		}
95956be1b9aSKonstantin Belousov 		if ((bits & (1 << (i % NBBY))) != 0)
96056be1b9aSKonstantin Belousov 			return (EBADF);
96156be1b9aSKonstantin Belousov 	}
96256be1b9aSKonstantin Belousov 	return (0);
96356be1b9aSKonstantin Belousov }
96456be1b9aSKonstantin Belousov 
9658f19eb88SIan Dowse int
9668f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
967b55ef216SKonstantin Belousov     fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
9688f19eb88SIan Dowse {
969426da3bcSAlfred Perlstein 	struct filedesc *fdp;
970d5e4d7e1SBruce Evans 	/*
971d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
972d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
973d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
974d5e4d7e1SBruce Evans 	 * of 256.
975d5e4d7e1SBruce Evans 	 */
976d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
977eb209311SAlfred Perlstein 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
978cf5e4fe6SDavide Italiano 	struct timeval rtv;
979cf5e4fe6SDavide Italiano 	sbintime_t asbt, precision, rsbt;
980b55ef216SKonstantin Belousov 	u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
981cf5e4fe6SDavide Italiano 	int error, lf, ndu;
982df8bae1dSRodney W. Grimes 
9838f19eb88SIan Dowse 	if (nd < 0)
984acbfbfeaSSujal Patel 		return (EINVAL);
985426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
98656be1b9aSKonstantin Belousov 	ndu = nd;
98756be1b9aSKonstantin Belousov 	lf = fdp->fd_lastfile;
98856be1b9aSKonstantin Belousov 	if (nd > lf + 1)
98956be1b9aSKonstantin Belousov 		nd = lf + 1;
99056be1b9aSKonstantin Belousov 
99156be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits);
99256be1b9aSKonstantin Belousov 	if (error != 0)
99356be1b9aSKonstantin Belousov 		return (error);
99456be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits);
99556be1b9aSKonstantin Belousov 	if (error != 0)
99656be1b9aSKonstantin Belousov 		return (error);
99756be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits);
99856be1b9aSKonstantin Belousov 	if (error != 0)
99956be1b9aSKonstantin Belousov 		return (error);
1000b08f7993SSujal Patel 
1001d5e4d7e1SBruce Evans 	/*
1002d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
1003d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
1004d5e4d7e1SBruce Evans 	 */
10058f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
1006d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
1007b55ef216SKonstantin Belousov 	ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
1008d5e4d7e1SBruce Evans 	nbufbytes = 0;
10098f19eb88SIan Dowse 	if (fd_in != NULL)
1010d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
10118f19eb88SIan Dowse 	if (fd_ou != NULL)
1012d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
10138f19eb88SIan Dowse 	if (fd_ex != NULL)
1014d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
1015d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
1016d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
1017d5e4d7e1SBruce Evans 	else
1018a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
1019b08f7993SSujal Patel 
1020b08f7993SSujal Patel 	/*
1021d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
1022d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
1023d5e4d7e1SBruce Evans 	 * together.
1024b08f7993SSujal Patel 	 */
1025d5e4d7e1SBruce Evans 	sbp = selbits;
1026df8bae1dSRodney W. Grimes #define	getbits(name, x) \
1027d5e4d7e1SBruce Evans 	do {								\
1028841c0c7eSNathan Whitehorn 		if (name == NULL) {					\
1029d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
1030841c0c7eSNathan Whitehorn 			obits[x] = NULL;				\
1031841c0c7eSNathan Whitehorn 		} else {						\
1032d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
1033d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
1034d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
1035b55ef216SKonstantin Belousov 			error = copyin(name, ibits[x], ncpubytes);	\
1036265fc98fSSeigo Tanimura 			if (error != 0)					\
1037ace8398dSJeff Roberson 				goto done;				\
1038d6fda03aSMateusz Guzik 			if (ncpbytes != ncpubytes)			\
1039b55ef216SKonstantin Belousov 				bzero((char *)ibits[x] + ncpubytes,	\
1040b55ef216SKonstantin Belousov 				    ncpbytes - ncpubytes);		\
1041e04ac2feSJohn Baldwin 		}							\
1042d5e4d7e1SBruce Evans 	} while (0)
10438f19eb88SIan Dowse 	getbits(fd_in, 0);
10448f19eb88SIan Dowse 	getbits(fd_ou, 1);
10458f19eb88SIan Dowse 	getbits(fd_ex, 2);
1046df8bae1dSRodney W. Grimes #undef	getbits
1047841c0c7eSNathan Whitehorn 
1048841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
1049841c0c7eSNathan Whitehorn 	/*
1050841c0c7eSNathan Whitehorn 	 * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
1051841c0c7eSNathan Whitehorn 	 * we are running under 32-bit emulation. This should be more
1052841c0c7eSNathan Whitehorn 	 * generic.
1053841c0c7eSNathan Whitehorn 	 */
1054841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)						\
1055841c0c7eSNathan Whitehorn 	if (abi_nfdbits != NFDBITS && bits != NULL) {			\
1056841c0c7eSNathan Whitehorn 		int i;							\
1057841c0c7eSNathan Whitehorn 		for (i = 0; i < ncpbytes / sizeof *sbp; i++)		\
1058841c0c7eSNathan Whitehorn 			bits[i] = (bits[i] >> 32) | (bits[i] << 32);	\
1059841c0c7eSNathan Whitehorn 	}
1060841c0c7eSNathan Whitehorn #else
1061841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)
1062841c0c7eSNathan Whitehorn #endif
1063841c0c7eSNathan Whitehorn 
1064841c0c7eSNathan Whitehorn 	/* Make sure the bit order makes it through an ABI transition */
1065841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[0]);
1066841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[1]);
1067841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[2]);
1068841c0c7eSNathan Whitehorn 
1069d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
1070d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
1071df8bae1dSRodney W. Grimes 
1072cf5e4fe6SDavide Italiano 	precision = 0;
10738f19eb88SIan Dowse 	if (tvp != NULL) {
1074cf5e4fe6SDavide Italiano 		rtv = *tvp;
1075cf5e4fe6SDavide Italiano 		if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1076cf5e4fe6SDavide Italiano 		    rtv.tv_usec >= 1000000) {
1077df8bae1dSRodney W. Grimes 			error = EINVAL;
1078ace8398dSJeff Roberson 			goto done;
1079df8bae1dSRodney W. Grimes 		}
108021a37a71SAlexander Motin 		if (!timevalisset(&rtv))
1081980c545dSAlexander Motin 			asbt = 0;
108221a37a71SAlexander Motin 		else if (rtv.tv_sec <= INT32_MAX) {
1083cf5e4fe6SDavide Italiano 			rsbt = tvtosbt(rtv);
1084cf5e4fe6SDavide Italiano 			precision = rsbt;
1085cf5e4fe6SDavide Italiano 			precision >>= tc_precexp;
1086cf5e4fe6SDavide Italiano 			if (TIMESEL(&asbt, rsbt))
1087cf5e4fe6SDavide Italiano 				asbt += tc_tick_sbt;
10884bc38a5aSDavide Italiano 			if (asbt <= SBT_MAX - rsbt)
1089cf5e4fe6SDavide Italiano 				asbt += rsbt;
109021a37a71SAlexander Motin 			else
1091980c545dSAlexander Motin 				asbt = -1;
1092980c545dSAlexander Motin 		} else
1093980c545dSAlexander Motin 			asbt = -1;
1094cf5e4fe6SDavide Italiano 	} else
1095cf5e4fe6SDavide Italiano 		asbt = -1;
1096ace8398dSJeff Roberson 	seltdinit(td);
1097ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1098ace8398dSJeff Roberson 	for (;;) {
10998f19eb88SIan Dowse 		error = selscan(td, ibits, obits, nd);
1100ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1101ace8398dSJeff Roberson 			break;
1102cf5e4fe6SDavide Italiano 		error = seltdwait(td, asbt, precision);
1103ace8398dSJeff Roberson 		if (error)
1104ace8398dSJeff Roberson 			break;
1105ace8398dSJeff Roberson 		error = selrescan(td, ibits, obits);
1106ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1107ace8398dSJeff Roberson 			break;
110885f190e4SAlfred Perlstein 	}
1109ace8398dSJeff Roberson 	seltdclear(td);
1110265fc98fSSeigo Tanimura 
1111df8bae1dSRodney W. Grimes done:
1112df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
1113df8bae1dSRodney W. Grimes 	if (error == ERESTART)
1114df8bae1dSRodney W. Grimes 		error = EINTR;
1115df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
1116df8bae1dSRodney W. Grimes 		error = 0;
1117841c0c7eSNathan Whitehorn 
1118841c0c7eSNathan Whitehorn 	/* swizzle bit order back, if necessary */
1119841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[0]);
1120841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[1]);
1121841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[2]);
1122841c0c7eSNathan Whitehorn #undef swizzle_fdset
1123841c0c7eSNathan Whitehorn 
1124df8bae1dSRodney W. Grimes #define	putbits(name, x) \
1125b55ef216SKonstantin Belousov 	if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
1126df8bae1dSRodney W. Grimes 		error = error2;
1127df8bae1dSRodney W. Grimes 	if (error == 0) {
1128df8bae1dSRodney W. Grimes 		int error2;
1129df8bae1dSRodney W. Grimes 
11308f19eb88SIan Dowse 		putbits(fd_in, 0);
11318f19eb88SIan Dowse 		putbits(fd_ou, 1);
11328f19eb88SIan Dowse 		putbits(fd_ex, 2);
1133df8bae1dSRodney W. Grimes #undef putbits
1134df8bae1dSRodney W. Grimes 	}
1135d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
1136d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
1137ad2edad9SMatthew Dillon 
1138df8bae1dSRodney W. Grimes 	return (error);
1139df8bae1dSRodney W. Grimes }
114011b763dfSJeff Roberson /*
114111b763dfSJeff Roberson  * Convert a select bit set to poll flags.
1142748b9df6SJeff Roberson  *
114311b763dfSJeff Roberson  * The backend always returns POLLHUP/POLLERR if appropriate and we
114411b763dfSJeff Roberson  * return this as a set bit in any set.
114511b763dfSJeff Roberson  */
114611b763dfSJeff Roberson static int select_flags[3] = {
114711b763dfSJeff Roberson     POLLRDNORM | POLLHUP | POLLERR,
114811b763dfSJeff Roberson     POLLWRNORM | POLLHUP | POLLERR,
114961e53a38SKonstantin Belousov     POLLRDBAND | POLLERR
115011b763dfSJeff Roberson };
115111b763dfSJeff Roberson 
115211b763dfSJeff Roberson /*
115311b763dfSJeff Roberson  * Compute the fo_poll flags required for a fd given by the index and
115411b763dfSJeff Roberson  * bit position in the fd_mask array.
115511b763dfSJeff Roberson  */
115611b763dfSJeff Roberson static __inline int
115760b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit)
115811b763dfSJeff Roberson {
115911b763dfSJeff Roberson 	int flags;
116011b763dfSJeff Roberson 	int msk;
116111b763dfSJeff Roberson 
116211b763dfSJeff Roberson 	flags = 0;
116311b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
116411b763dfSJeff Roberson 		if (ibits[msk] == NULL)
116511b763dfSJeff Roberson 			continue;
116660b7f468SStephane E. Potvin 		if ((ibits[msk][idx] & bit) == 0)
116711b763dfSJeff Roberson 			continue;
116811b763dfSJeff Roberson 		flags |= select_flags[msk];
116911b763dfSJeff Roberson 	}
117011b763dfSJeff Roberson 	return (flags);
117111b763dfSJeff Roberson }
117211b763dfSJeff Roberson 
117311b763dfSJeff Roberson /*
117411b763dfSJeff Roberson  * Set the appropriate output bits given a mask of fired events and the
117511b763dfSJeff Roberson  * input bits originally requested.
117611b763dfSJeff Roberson  */
117711b763dfSJeff Roberson static __inline int
117811b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
117911b763dfSJeff Roberson {
118011b763dfSJeff Roberson 	int msk;
118111b763dfSJeff Roberson 	int n;
118211b763dfSJeff Roberson 
118311b763dfSJeff Roberson 	n = 0;
118411b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
118511b763dfSJeff Roberson 		if ((events & select_flags[msk]) == 0)
118611b763dfSJeff Roberson 			continue;
118711b763dfSJeff Roberson 		if (ibits[msk] == NULL)
118811b763dfSJeff Roberson 			continue;
118911b763dfSJeff Roberson 		if ((ibits[msk][idx] & bit) == 0)
119011b763dfSJeff Roberson 			continue;
119111b763dfSJeff Roberson 		/*
119211b763dfSJeff Roberson 		 * XXX Check for a duplicate set.  This can occur because a
119311b763dfSJeff Roberson 		 * socket calls selrecord() twice for each poll() call
119411b763dfSJeff Roberson 		 * resulting in two selfds per real fd.  selrescan() will
119511b763dfSJeff Roberson 		 * call selsetbits twice as a result.
119611b763dfSJeff Roberson 		 */
119711b763dfSJeff Roberson 		if ((obits[msk][idx] & bit) != 0)
119811b763dfSJeff Roberson 			continue;
119911b763dfSJeff Roberson 		obits[msk][idx] |= bit;
120011b763dfSJeff Roberson 		n++;
120111b763dfSJeff Roberson 	}
120211b763dfSJeff Roberson 
120311b763dfSJeff Roberson 	return (n);
120411b763dfSJeff Roberson }
1205df8bae1dSRodney W. Grimes 
1206a9d2f8d8SRobert Watson static __inline int
1207a9d2f8d8SRobert Watson getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp)
1208a9d2f8d8SRobert Watson {
1209a9d2f8d8SRobert Watson 
1210cbd92ce6SMatt Macy 	return (fget_unlocked(fdp, fd, &cap_event_rights, fpp, NULL));
1211a9d2f8d8SRobert Watson }
1212a9d2f8d8SRobert Watson 
1213ace8398dSJeff Roberson /*
1214ace8398dSJeff Roberson  * Traverse the list of fds attached to this thread's seltd and check for
1215ace8398dSJeff Roberson  * completion.
1216ace8398dSJeff Roberson  */
1217ace8398dSJeff Roberson static int
1218ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
1219ace8398dSJeff Roberson {
122011b763dfSJeff Roberson 	struct filedesc *fdp;
122111b763dfSJeff Roberson 	struct selinfo *si;
1222ace8398dSJeff Roberson 	struct seltd *stp;
1223ace8398dSJeff Roberson 	struct selfd *sfp;
1224ace8398dSJeff Roberson 	struct selfd *sfn;
1225ace8398dSJeff Roberson 	struct file *fp;
12269cdacff1SJeff Roberson 	fd_mask bit;
12279cdacff1SJeff Roberson 	int fd, ev, n, idx;
1228a9d2f8d8SRobert Watson 	int error;
1229ace8398dSJeff Roberson 
123011b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
1231ace8398dSJeff Roberson 	stp = td->td_sel;
123211b763dfSJeff Roberson 	n = 0;
1233ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1234ace8398dSJeff Roberson 		fd = (int)(uintptr_t)sfp->sf_cookie;
1235ace8398dSJeff Roberson 		si = sfp->sf_si;
1236ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1237ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1238ace8398dSJeff Roberson 		if (si != NULL)
1239ace8398dSJeff Roberson 			continue;
1240a9d2f8d8SRobert Watson 		error = getselfd_cap(fdp, fd, &fp);
1241a9d2f8d8SRobert Watson 		if (error)
1242a9d2f8d8SRobert Watson 			return (error);
124311b763dfSJeff Roberson 		idx = fd / NFDBITS;
12449cdacff1SJeff Roberson 		bit = (fd_mask)1 << (fd % NFDBITS);
124511b763dfSJeff Roberson 		ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
1246bf422e5fSJeff Roberson 		fdrop(fp, td);
124711b763dfSJeff Roberson 		if (ev != 0)
124811b763dfSJeff Roberson 			n += selsetbits(ibits, obits, idx, bit, ev);
1249ace8398dSJeff Roberson 	}
1250ace8398dSJeff Roberson 	stp->st_flags = 0;
1251ace8398dSJeff Roberson 	td->td_retval[0] = n;
1252ace8398dSJeff Roberson 	return (0);
1253ace8398dSJeff Roberson }
1254ace8398dSJeff Roberson 
1255ace8398dSJeff Roberson /*
1256ace8398dSJeff Roberson  * Perform the initial filedescriptor scan and register ourselves with
1257ace8398dSJeff Roberson  * each selinfo.
1258ace8398dSJeff Roberson  */
1259265fc98fSSeigo Tanimura static int
1260cc3c9df8SEd Maste selscan(struct thread *td, fd_mask **ibits, fd_mask **obits, int nfd)
1261df8bae1dSRodney W. Grimes {
126211b763dfSJeff Roberson 	struct filedesc *fdp;
1263df8bae1dSRodney W. Grimes 	struct file *fp;
12649cdacff1SJeff Roberson 	fd_mask bit;
126511b763dfSJeff Roberson 	int ev, flags, end, fd;
12669cdacff1SJeff Roberson 	int n, idx;
1267a9d2f8d8SRobert Watson 	int error;
1268df8bae1dSRodney W. Grimes 
126911b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
127011b763dfSJeff Roberson 	n = 0;
12719cdacff1SJeff Roberson 	for (idx = 0, fd = 0; fd < nfd; idx++) {
127211b763dfSJeff Roberson 		end = imin(fd + NFDBITS, nfd);
127311b763dfSJeff Roberson 		for (bit = 1; fd < end; bit <<= 1, fd++) {
127411b763dfSJeff Roberson 			/* Compute the list of events we're interested in. */
127511b763dfSJeff Roberson 			flags = selflags(ibits, idx, bit);
127611b763dfSJeff Roberson 			if (flags == 0)
1277f082218cSPeter Wemm 				continue;
1278a9d2f8d8SRobert Watson 			error = getselfd_cap(fdp, fd, &fp);
1279a9d2f8d8SRobert Watson 			if (error)
1280a9d2f8d8SRobert Watson 				return (error);
1281ace8398dSJeff Roberson 			selfdalloc(td, (void *)(uintptr_t)fd);
128211b763dfSJeff Roberson 			ev = fo_poll(fp, flags, td->td_ucred, td);
1283bf422e5fSJeff Roberson 			fdrop(fp, td);
128411b763dfSJeff Roberson 			if (ev != 0)
128511b763dfSJeff Roberson 				n += selsetbits(ibits, obits, idx, bit, ev);
1286df8bae1dSRodney W. Grimes 		}
1287df8bae1dSRodney W. Grimes 	}
128811b763dfSJeff Roberson 
1289b40ce416SJulian Elischer 	td->td_retval[0] = n;
1290df8bae1dSRodney W. Grimes 	return (0);
1291df8bae1dSRodney W. Grimes }
1292df8bae1dSRodney W. Grimes 
129342d11757SPeter Wemm int
1294186d9c34SDmitry Chagin sys_poll(struct thread *td, struct poll_args *uap)
1295186d9c34SDmitry Chagin {
1296186d9c34SDmitry Chagin 	struct timespec ts, *tsp;
1297186d9c34SDmitry Chagin 
1298186d9c34SDmitry Chagin 	if (uap->timeout != INFTIM) {
1299186d9c34SDmitry Chagin 		if (uap->timeout < 0)
1300186d9c34SDmitry Chagin 			return (EINVAL);
1301186d9c34SDmitry Chagin 		ts.tv_sec = uap->timeout / 1000;
1302186d9c34SDmitry Chagin 		ts.tv_nsec = (uap->timeout % 1000) * 1000000;
1303186d9c34SDmitry Chagin 		tsp = &ts;
1304186d9c34SDmitry Chagin 	} else
1305186d9c34SDmitry Chagin 		tsp = NULL;
1306186d9c34SDmitry Chagin 
1307186d9c34SDmitry Chagin 	return (kern_poll(td, uap->fds, uap->nfds, tsp, NULL));
1308186d9c34SDmitry Chagin }
1309186d9c34SDmitry Chagin 
1310186d9c34SDmitry Chagin int
13112384981bSConrad Meyer kern_poll(struct thread *td, struct pollfd *ufds, u_int nfds,
1312186d9c34SDmitry Chagin     struct timespec *tsp, sigset_t *uset)
131342d11757SPeter Wemm {
13142384981bSConrad Meyer 	struct pollfd *kfds;
13152384981bSConrad Meyer 	struct pollfd stackfds[32];
1316186d9c34SDmitry Chagin 	sbintime_t sbt, precision, tmp;
1317186d9c34SDmitry Chagin 	time_t over;
1318186d9c34SDmitry Chagin 	struct timespec ts;
1319cf5e4fe6SDavide Italiano 	int error;
132042d11757SPeter Wemm 
1321186d9c34SDmitry Chagin 	precision = 0;
1322186d9c34SDmitry Chagin 	if (tsp != NULL) {
1323186d9c34SDmitry Chagin 		if (tsp->tv_sec < 0)
1324186d9c34SDmitry Chagin 			return (EINVAL);
1325186d9c34SDmitry Chagin 		if (tsp->tv_nsec < 0 || tsp->tv_nsec >= 1000000000)
1326186d9c34SDmitry Chagin 			return (EINVAL);
1327186d9c34SDmitry Chagin 		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1328186d9c34SDmitry Chagin 			sbt = 0;
1329186d9c34SDmitry Chagin 		else {
1330186d9c34SDmitry Chagin 			ts = *tsp;
1331186d9c34SDmitry Chagin 			if (ts.tv_sec > INT32_MAX / 2) {
1332186d9c34SDmitry Chagin 				over = ts.tv_sec - INT32_MAX / 2;
1333186d9c34SDmitry Chagin 				ts.tv_sec -= over;
1334186d9c34SDmitry Chagin 			} else
1335186d9c34SDmitry Chagin 				over = 0;
1336186d9c34SDmitry Chagin 			tmp = tstosbt(ts);
1337186d9c34SDmitry Chagin 			precision = tmp;
1338186d9c34SDmitry Chagin 			precision >>= tc_precexp;
1339186d9c34SDmitry Chagin 			if (TIMESEL(&sbt, tmp))
1340186d9c34SDmitry Chagin 				sbt += tc_tick_sbt;
1341186d9c34SDmitry Chagin 			sbt += tmp;
1342186d9c34SDmitry Chagin 		}
1343186d9c34SDmitry Chagin 	} else
1344186d9c34SDmitry Chagin 		sbt = -1;
1345186d9c34SDmitry Chagin 
134678c2a980SConrad Meyer 	/*
134778c2a980SConrad Meyer 	 * This is kinda bogus.  We have fd limits, but that is not
134878c2a980SConrad Meyer 	 * really related to the size of the pollfd array.  Make sure
134978c2a980SConrad Meyer 	 * we let the process use at least FD_SETSIZE entries and at
135078c2a980SConrad Meyer 	 * least enough for the system-wide limits.  We want to be reasonably
135178c2a980SConrad Meyer 	 * safe, but not overly restrictive.
135278c2a980SConrad Meyer 	 */
1353374ae2a3SJeff Roberson 	if (nfds > maxfilesperproc && nfds > FD_SETSIZE)
1354ace8398dSJeff Roberson 		return (EINVAL);
13552384981bSConrad Meyer 	if (nfds > nitems(stackfds))
13562384981bSConrad Meyer 		kfds = mallocarray(nfds, sizeof(*kfds), M_TEMP, M_WAITOK);
135742d11757SPeter Wemm 	else
13582384981bSConrad Meyer 		kfds = stackfds;
13592384981bSConrad Meyer 	error = copyin(ufds, kfds, nfds * sizeof(*kfds));
136042d11757SPeter Wemm 	if (error)
1361ace8398dSJeff Roberson 		goto done;
1362186d9c34SDmitry Chagin 
1363186d9c34SDmitry Chagin 	if (uset != NULL) {
1364186d9c34SDmitry Chagin 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
1365186d9c34SDmitry Chagin 		    &td->td_oldsigmask, 0);
1366186d9c34SDmitry Chagin 		if (error)
1367ace8398dSJeff Roberson 			goto done;
1368186d9c34SDmitry Chagin 		td->td_pflags |= TDP_OLDMASK;
1369186d9c34SDmitry Chagin 		/*
1370186d9c34SDmitry Chagin 		 * Make sure that ast() is called on return to
1371186d9c34SDmitry Chagin 		 * usermode and TDP_OLDMASK is cleared, restoring old
1372186d9c34SDmitry Chagin 		 * sigmask.
1373186d9c34SDmitry Chagin 		 */
1374186d9c34SDmitry Chagin 		thread_lock(td);
1375186d9c34SDmitry Chagin 		td->td_flags |= TDF_ASTPENDING;
1376186d9c34SDmitry Chagin 		thread_unlock(td);
137742d11757SPeter Wemm 	}
1378186d9c34SDmitry Chagin 
1379ace8398dSJeff Roberson 	seltdinit(td);
1380ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1381ace8398dSJeff Roberson 	for (;;) {
13822384981bSConrad Meyer 		error = pollscan(td, kfds, nfds);
1383ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1384ace8398dSJeff Roberson 			break;
1385186d9c34SDmitry Chagin 		error = seltdwait(td, sbt, precision);
1386ace8398dSJeff Roberson 		if (error)
1387ace8398dSJeff Roberson 			break;
1388ace8398dSJeff Roberson 		error = pollrescan(td);
1389ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1390ace8398dSJeff Roberson 			break;
139185f190e4SAlfred Perlstein 	}
1392ace8398dSJeff Roberson 	seltdclear(td);
1393265fc98fSSeigo Tanimura 
139442d11757SPeter Wemm done:
139542d11757SPeter Wemm 	/* poll is not restarted after signals... */
139642d11757SPeter Wemm 	if (error == ERESTART)
139742d11757SPeter Wemm 		error = EINTR;
139842d11757SPeter Wemm 	if (error == EWOULDBLOCK)
139942d11757SPeter Wemm 		error = 0;
140042d11757SPeter Wemm 	if (error == 0) {
14012384981bSConrad Meyer 		error = pollout(td, kfds, ufds, nfds);
140242d11757SPeter Wemm 		if (error)
140342d11757SPeter Wemm 			goto out;
140442d11757SPeter Wemm 	}
140542d11757SPeter Wemm out:
14062384981bSConrad Meyer 	if (nfds > nitems(stackfds))
14072384981bSConrad Meyer 		free(kfds, M_TEMP);
140842d11757SPeter Wemm 	return (error);
140942d11757SPeter Wemm }
141042d11757SPeter Wemm 
1411186d9c34SDmitry Chagin int
1412186d9c34SDmitry Chagin sys_ppoll(struct thread *td, struct ppoll_args *uap)
1413186d9c34SDmitry Chagin {
1414186d9c34SDmitry Chagin 	struct timespec ts, *tsp;
1415186d9c34SDmitry Chagin 	sigset_t set, *ssp;
1416186d9c34SDmitry Chagin 	int error;
1417186d9c34SDmitry Chagin 
1418186d9c34SDmitry Chagin 	if (uap->ts != NULL) {
1419186d9c34SDmitry Chagin 		error = copyin(uap->ts, &ts, sizeof(ts));
1420186d9c34SDmitry Chagin 		if (error)
1421186d9c34SDmitry Chagin 			return (error);
1422186d9c34SDmitry Chagin 		tsp = &ts;
1423186d9c34SDmitry Chagin 	} else
1424186d9c34SDmitry Chagin 		tsp = NULL;
1425186d9c34SDmitry Chagin 	if (uap->set != NULL) {
1426186d9c34SDmitry Chagin 		error = copyin(uap->set, &set, sizeof(set));
1427186d9c34SDmitry Chagin 		if (error)
1428186d9c34SDmitry Chagin 			return (error);
1429186d9c34SDmitry Chagin 		ssp = &set;
1430186d9c34SDmitry Chagin 	} else
1431186d9c34SDmitry Chagin 		ssp = NULL;
1432186d9c34SDmitry Chagin 	/*
1433186d9c34SDmitry Chagin 	 * fds is still a pointer to user space. kern_poll() will
1434186d9c34SDmitry Chagin 	 * take care of copyin that array to the kernel space.
1435186d9c34SDmitry Chagin 	 */
1436186d9c34SDmitry Chagin 
1437186d9c34SDmitry Chagin 	return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
1438186d9c34SDmitry Chagin }
1439186d9c34SDmitry Chagin 
144042d11757SPeter Wemm static int
1441ace8398dSJeff Roberson pollrescan(struct thread *td)
1442ace8398dSJeff Roberson {
1443ace8398dSJeff Roberson 	struct seltd *stp;
1444ace8398dSJeff Roberson 	struct selfd *sfp;
1445ace8398dSJeff Roberson 	struct selfd *sfn;
1446ace8398dSJeff Roberson 	struct selinfo *si;
1447ace8398dSJeff Roberson 	struct filedesc *fdp;
1448ace8398dSJeff Roberson 	struct file *fp;
1449ace8398dSJeff Roberson 	struct pollfd *fd;
1450ace8398dSJeff Roberson 	int n;
1451ace8398dSJeff Roberson 
1452ace8398dSJeff Roberson 	n = 0;
1453ace8398dSJeff Roberson 	fdp = td->td_proc->p_fd;
1454ace8398dSJeff Roberson 	stp = td->td_sel;
1455ace8398dSJeff Roberson 	FILEDESC_SLOCK(fdp);
1456ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1457ace8398dSJeff Roberson 		fd = (struct pollfd *)sfp->sf_cookie;
1458ace8398dSJeff Roberson 		si = sfp->sf_si;
1459ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1460ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1461ace8398dSJeff Roberson 		if (si != NULL)
1462ace8398dSJeff Roberson 			continue;
14632609222aSPawel Jakub Dawidek 		fp = fdp->fd_ofiles[fd->fd].fde_file;
1464d6f72489SJonathan Anderson #ifdef CAPABILITIES
14652609222aSPawel Jakub Dawidek 		if (fp == NULL ||
1466cbd92ce6SMatt Macy 		    cap_check(cap_rights(fdp, fd->fd), &cap_event_rights) != 0)
1467d6f72489SJonathan Anderson #else
14682609222aSPawel Jakub Dawidek 		if (fp == NULL)
1469d6f72489SJonathan Anderson #endif
14702609222aSPawel Jakub Dawidek 		{
1471ace8398dSJeff Roberson 			fd->revents = POLLNVAL;
1472ace8398dSJeff Roberson 			n++;
1473ace8398dSJeff Roberson 			continue;
1474ace8398dSJeff Roberson 		}
1475d6f72489SJonathan Anderson 
1476ace8398dSJeff Roberson 		/*
1477ace8398dSJeff Roberson 		 * Note: backend also returns POLLHUP and
1478ace8398dSJeff Roberson 		 * POLLERR if appropriate.
1479ace8398dSJeff Roberson 		 */
1480ace8398dSJeff Roberson 		fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1481ace8398dSJeff Roberson 		if (fd->revents != 0)
1482ace8398dSJeff Roberson 			n++;
1483ace8398dSJeff Roberson 	}
1484ace8398dSJeff Roberson 	FILEDESC_SUNLOCK(fdp);
1485ace8398dSJeff Roberson 	stp->st_flags = 0;
1486ace8398dSJeff Roberson 	td->td_retval[0] = n;
1487ace8398dSJeff Roberson 	return (0);
1488ace8398dSJeff Roberson }
1489ace8398dSJeff Roberson 
1490ace8398dSJeff Roberson 
1491ace8398dSJeff Roberson static int
1492cc3c9df8SEd Maste pollout(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd)
1493ae81968fSRobert Watson {
1494ae81968fSRobert Watson 	int error = 0;
1495ae81968fSRobert Watson 	u_int i = 0;
14966d8feddaSKonstantin Belousov 	u_int n = 0;
1497ae81968fSRobert Watson 
1498ae81968fSRobert Watson 	for (i = 0; i < nfd; i++) {
1499ae81968fSRobert Watson 		error = copyout(&fds->revents, &ufds->revents,
1500ae81968fSRobert Watson 		    sizeof(ufds->revents));
1501ae81968fSRobert Watson 		if (error)
1502ae81968fSRobert Watson 			return (error);
15036d8feddaSKonstantin Belousov 		if (fds->revents != 0)
15046d8feddaSKonstantin Belousov 			n++;
1505ae81968fSRobert Watson 		fds++;
1506ae81968fSRobert Watson 		ufds++;
1507ae81968fSRobert Watson 	}
15086d8feddaSKonstantin Belousov 	td->td_retval[0] = n;
1509ae81968fSRobert Watson 	return (0);
1510ae81968fSRobert Watson }
1511ae81968fSRobert Watson 
1512ae81968fSRobert Watson static int
1513cc3c9df8SEd Maste pollscan(struct thread *td, struct pollfd *fds, u_int nfd)
151442d11757SPeter Wemm {
1515ace8398dSJeff Roberson 	struct filedesc *fdp = td->td_proc->p_fd;
151642d11757SPeter Wemm 	struct file *fp;
15172609222aSPawel Jakub Dawidek 	int i, n = 0;
151842d11757SPeter Wemm 
15195e3f7694SRobert Watson 	FILEDESC_SLOCK(fdp);
1520eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
1521adf87ab0SMateusz Guzik 		if (fds->fd > fdp->fd_lastfile) {
152242d11757SPeter Wemm 			fds->revents = POLLNVAL;
152342d11757SPeter Wemm 			n++;
1524337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
1525337c9691SJordan K. Hubbard 			fds->revents = 0;
152642d11757SPeter Wemm 		} else {
15272609222aSPawel Jakub Dawidek 			fp = fdp->fd_ofiles[fds->fd].fde_file;
1528d6f72489SJonathan Anderson #ifdef CAPABILITIES
15292609222aSPawel Jakub Dawidek 			if (fp == NULL ||
1530cbd92ce6SMatt Macy 			    cap_check(cap_rights(fdp, fds->fd), &cap_event_rights) != 0)
1531d6f72489SJonathan Anderson #else
15322609222aSPawel Jakub Dawidek 			if (fp == NULL)
1533d6f72489SJonathan Anderson #endif
15342609222aSPawel Jakub Dawidek 			{
153542d11757SPeter Wemm 				fds->revents = POLLNVAL;
153642d11757SPeter Wemm 				n++;
153742d11757SPeter Wemm 			} else {
15382087c896SBruce Evans 				/*
15392087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
15402087c896SBruce Evans 				 * POLLERR if appropriate.
15412087c896SBruce Evans 				 */
1542ace8398dSJeff Roberson 				selfdalloc(td, fds);
154313ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
1544ea6027a8SRobert Watson 				    td->td_ucred, td);
1545f2159cc7SKonstantin Belousov 				/*
1546f2159cc7SKonstantin Belousov 				 * POSIX requires POLLOUT to be never
1547f2159cc7SKonstantin Belousov 				 * set simultaneously with POLLHUP.
1548f2159cc7SKonstantin Belousov 				 */
1549f2159cc7SKonstantin Belousov 				if ((fds->revents & POLLHUP) != 0)
1550f2159cc7SKonstantin Belousov 					fds->revents &= ~POLLOUT;
1551f2159cc7SKonstantin Belousov 
155242d11757SPeter Wemm 				if (fds->revents != 0)
155342d11757SPeter Wemm 					n++;
155442d11757SPeter Wemm 			}
155542d11757SPeter Wemm 		}
155642d11757SPeter Wemm 	}
15575e3f7694SRobert Watson 	FILEDESC_SUNLOCK(fdp);
1558b40ce416SJulian Elischer 	td->td_retval[0] = n;
155942d11757SPeter Wemm 	return (0);
156042d11757SPeter Wemm }
156142d11757SPeter Wemm 
156242d11757SPeter Wemm /*
1563237abf0cSDavide Italiano  * XXX This was created specifically to support netncp and netsmb.  This
1564237abf0cSDavide Italiano  * allows the caller to specify a socket to wait for events on.  It returns
1565237abf0cSDavide Italiano  * 0 if any events matched and an error otherwise.  There is no way to
1566237abf0cSDavide Italiano  * determine which events fired.
1567237abf0cSDavide Italiano  */
1568237abf0cSDavide Italiano int
1569237abf0cSDavide Italiano selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td)
1570237abf0cSDavide Italiano {
1571237abf0cSDavide Italiano 	struct timeval rtv;
1572237abf0cSDavide Italiano 	sbintime_t asbt, precision, rsbt;
1573237abf0cSDavide Italiano 	int error;
1574237abf0cSDavide Italiano 
157576665df9SPeter Wemm 	precision = 0;	/* stupid gcc! */
1576237abf0cSDavide Italiano 	if (tvp != NULL) {
1577237abf0cSDavide Italiano 		rtv = *tvp;
1578237abf0cSDavide Italiano 		if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1579237abf0cSDavide Italiano 		    rtv.tv_usec >= 1000000)
1580237abf0cSDavide Italiano 			return (EINVAL);
1581237abf0cSDavide Italiano 		if (!timevalisset(&rtv))
1582237abf0cSDavide Italiano 			asbt = 0;
1583237abf0cSDavide Italiano 		else if (rtv.tv_sec <= INT32_MAX) {
1584237abf0cSDavide Italiano 			rsbt = tvtosbt(rtv);
1585237abf0cSDavide Italiano 			precision = rsbt;
1586237abf0cSDavide Italiano 			precision >>= tc_precexp;
1587237abf0cSDavide Italiano 			if (TIMESEL(&asbt, rsbt))
1588237abf0cSDavide Italiano 				asbt += tc_tick_sbt;
15894bc38a5aSDavide Italiano 			if (asbt <= SBT_MAX - rsbt)
1590237abf0cSDavide Italiano 				asbt += rsbt;
1591237abf0cSDavide Italiano 			else
1592237abf0cSDavide Italiano 				asbt = -1;
1593237abf0cSDavide Italiano 		} else
1594237abf0cSDavide Italiano 			asbt = -1;
1595237abf0cSDavide Italiano 	} else
1596237abf0cSDavide Italiano 		asbt = -1;
1597237abf0cSDavide Italiano 	seltdinit(td);
1598237abf0cSDavide Italiano 	/*
1599237abf0cSDavide Italiano 	 * Iterate until the timeout expires or the socket becomes ready.
1600237abf0cSDavide Italiano 	 */
1601237abf0cSDavide Italiano 	for (;;) {
1602237abf0cSDavide Italiano 		selfdalloc(td, NULL);
1603237abf0cSDavide Italiano 		error = sopoll(so, events, NULL, td);
1604237abf0cSDavide Italiano 		/* error here is actually the ready events. */
1605237abf0cSDavide Italiano 		if (error)
1606237abf0cSDavide Italiano 			return (0);
1607237abf0cSDavide Italiano 		error = seltdwait(td, asbt, precision);
1608237abf0cSDavide Italiano 		if (error)
1609237abf0cSDavide Italiano 			break;
1610237abf0cSDavide Italiano 	}
1611237abf0cSDavide Italiano 	seltdclear(td);
1612237abf0cSDavide Italiano 	/* XXX Duplicates ncp/smb behavior. */
1613237abf0cSDavide Italiano 	if (error == ERESTART)
1614237abf0cSDavide Italiano 		error = 0;
1615237abf0cSDavide Italiano 	return (error);
1616237abf0cSDavide Italiano }
1617237abf0cSDavide Italiano 
1618237abf0cSDavide Italiano /*
1619ace8398dSJeff Roberson  * Preallocate two selfds associated with 'cookie'.  Some fo_poll routines
1620ace8398dSJeff Roberson  * have two select sets, one for read and another for write.
1621ace8398dSJeff Roberson  */
1622ace8398dSJeff Roberson static void
1623ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie)
1624ace8398dSJeff Roberson {
1625ace8398dSJeff Roberson 	struct seltd *stp;
1626ace8398dSJeff Roberson 
1627ace8398dSJeff Roberson 	stp = td->td_sel;
1628ace8398dSJeff Roberson 	if (stp->st_free1 == NULL)
1629ace8398dSJeff Roberson 		stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1630ace8398dSJeff Roberson 	stp->st_free1->sf_td = stp;
1631ace8398dSJeff Roberson 	stp->st_free1->sf_cookie = cookie;
1632ace8398dSJeff Roberson 	if (stp->st_free2 == NULL)
1633ace8398dSJeff Roberson 		stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1634ace8398dSJeff Roberson 	stp->st_free2->sf_td = stp;
1635ace8398dSJeff Roberson 	stp->st_free2->sf_cookie = cookie;
1636ace8398dSJeff Roberson }
1637ace8398dSJeff Roberson 
1638ace8398dSJeff Roberson static void
1639ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp)
1640ace8398dSJeff Roberson {
1641ace8398dSJeff Roberson 	STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
164273f2e5f7SMateusz Guzik 	if (sfp->sf_si != NULL) {
1643ace8398dSJeff Roberson 		mtx_lock(sfp->sf_mtx);
1644fcb5b3a4SKonstantin Belousov 		if (sfp->sf_si != NULL) {
1645ace8398dSJeff Roberson 			TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1646fcb5b3a4SKonstantin Belousov 			refcount_release(&sfp->sf_refs);
1647fcb5b3a4SKonstantin Belousov 		}
1648ace8398dSJeff Roberson 		mtx_unlock(sfp->sf_mtx);
164973f2e5f7SMateusz Guzik 	}
1650fcb5b3a4SKonstantin Belousov 	if (refcount_release(&sfp->sf_refs))
1651ace8398dSJeff Roberson 		uma_zfree(selfd_zone, sfp);
165285f190e4SAlfred Perlstein }
165385f190e4SAlfred Perlstein 
16546aba400aSAttilio Rao /* Drain the waiters tied to all the selfd belonging the specified selinfo. */
16556aba400aSAttilio Rao void
1656cc3c9df8SEd Maste seldrain(struct selinfo *sip)
16576aba400aSAttilio Rao {
16586aba400aSAttilio Rao 
16596aba400aSAttilio Rao 	/*
16606aba400aSAttilio Rao 	 * This feature is already provided by doselwakeup(), thus it is
16616aba400aSAttilio Rao 	 * enough to go for it.
16626aba400aSAttilio Rao 	 * Eventually, the context, should take care to avoid races
16636aba400aSAttilio Rao 	 * between thread calling select()/poll() and file descriptor
16646aba400aSAttilio Rao 	 * detaching, but, again, the races are just the same as
16656aba400aSAttilio Rao 	 * selwakeup().
16666aba400aSAttilio Rao 	 */
16676aba400aSAttilio Rao         doselwakeup(sip, -1);
16686aba400aSAttilio Rao }
16696aba400aSAttilio Rao 
1670df8bae1dSRodney W. Grimes /*
1671df8bae1dSRodney W. Grimes  * Record a select request.
1672df8bae1dSRodney W. Grimes  */
1673df8bae1dSRodney W. Grimes void
1674cc3c9df8SEd Maste selrecord(struct thread *selector, struct selinfo *sip)
1675df8bae1dSRodney W. Grimes {
1676ace8398dSJeff Roberson 	struct selfd *sfp;
1677ace8398dSJeff Roberson 	struct seltd *stp;
1678ace8398dSJeff Roberson 	struct mtx *mtxp;
1679df8bae1dSRodney W. Grimes 
1680ace8398dSJeff Roberson 	stp = selector->td_sel;
168185f190e4SAlfred Perlstein 	/*
1682ace8398dSJeff Roberson 	 * Don't record when doing a rescan.
168385f190e4SAlfred Perlstein 	 */
1684ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_RESCAN)
1685ace8398dSJeff Roberson 		return;
1686ace8398dSJeff Roberson 	/*
1687ace8398dSJeff Roberson 	 * Grab one of the preallocated descriptors.
1688ace8398dSJeff Roberson 	 */
1689ace8398dSJeff Roberson 	sfp = NULL;
1690ace8398dSJeff Roberson 	if ((sfp = stp->st_free1) != NULL)
1691ace8398dSJeff Roberson 		stp->st_free1 = NULL;
1692ace8398dSJeff Roberson 	else if ((sfp = stp->st_free2) != NULL)
1693ace8398dSJeff Roberson 		stp->st_free2 = NULL;
1694ace8398dSJeff Roberson 	else
1695ace8398dSJeff Roberson 		panic("selrecord: No free selfd on selq");
16962141453eSJeff Roberson 	mtxp = sip->si_mtx;
16972141453eSJeff Roberson 	if (mtxp == NULL)
16982141453eSJeff Roberson 		mtxp = mtx_pool_find(mtxpool_select, sip);
1699ace8398dSJeff Roberson 	/*
1700ace8398dSJeff Roberson 	 * Initialize the sfp and queue it in the thread.
1701ace8398dSJeff Roberson 	 */
1702ace8398dSJeff Roberson 	sfp->sf_si = sip;
1703ace8398dSJeff Roberson 	sfp->sf_mtx = mtxp;
1704fcb5b3a4SKonstantin Belousov 	refcount_init(&sfp->sf_refs, 2);
1705ace8398dSJeff Roberson 	STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1706ace8398dSJeff Roberson 	/*
1707ace8398dSJeff Roberson 	 * Now that we've locked the sip, check for initialization.
1708ace8398dSJeff Roberson 	 */
1709ace8398dSJeff Roberson 	mtx_lock(mtxp);
1710ace8398dSJeff Roberson 	if (sip->si_mtx == NULL) {
1711ace8398dSJeff Roberson 		sip->si_mtx = mtxp;
1712ace8398dSJeff Roberson 		TAILQ_INIT(&sip->si_tdlist);
171385f190e4SAlfred Perlstein 	}
1714ace8398dSJeff Roberson 	/*
1715ace8398dSJeff Roberson 	 * Add this thread to the list of selfds listening on this selinfo.
1716ace8398dSJeff Roberson 	 */
1717ace8398dSJeff Roberson 	TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1718ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1719df8bae1dSRodney W. Grimes }
1720df8bae1dSRodney W. Grimes 
1721512824f8SSeigo Tanimura /* Wake up a selecting thread. */
1722df8bae1dSRodney W. Grimes void
1723cc3c9df8SEd Maste selwakeup(struct selinfo *sip)
1724df8bae1dSRodney W. Grimes {
1725512824f8SSeigo Tanimura 	doselwakeup(sip, -1);
1726512824f8SSeigo Tanimura }
1727512824f8SSeigo Tanimura 
1728512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */
1729512824f8SSeigo Tanimura void
1730cc3c9df8SEd Maste selwakeuppri(struct selinfo *sip, int pri)
1731512824f8SSeigo Tanimura {
1732512824f8SSeigo Tanimura 	doselwakeup(sip, pri);
1733512824f8SSeigo Tanimura }
1734512824f8SSeigo Tanimura 
1735512824f8SSeigo Tanimura /*
1736512824f8SSeigo Tanimura  * Do a wakeup when a selectable event occurs.
1737512824f8SSeigo Tanimura  */
1738512824f8SSeigo Tanimura static void
1739cc3c9df8SEd Maste doselwakeup(struct selinfo *sip, int pri)
1740512824f8SSeigo Tanimura {
1741ace8398dSJeff Roberson 	struct selfd *sfp;
1742ace8398dSJeff Roberson 	struct selfd *sfn;
1743ace8398dSJeff Roberson 	struct seltd *stp;
1744df8bae1dSRodney W. Grimes 
1745ace8398dSJeff Roberson 	/* If it's not initialized there can't be any waiters. */
1746ace8398dSJeff Roberson 	if (sip->si_mtx == NULL)
1747b40ce416SJulian Elischer 		return;
1748ace8398dSJeff Roberson 	/*
1749ace8398dSJeff Roberson 	 * Locking the selinfo locks all selfds associated with it.
1750ace8398dSJeff Roberson 	 */
1751ace8398dSJeff Roberson 	mtx_lock(sip->si_mtx);
1752ace8398dSJeff Roberson 	TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1753ace8398dSJeff Roberson 		/*
1754ace8398dSJeff Roberson 		 * Once we remove this sfp from the list and clear the
1755ace8398dSJeff Roberson 		 * sf_si seltdclear will know to ignore this si.
1756ace8398dSJeff Roberson 		 */
1757ace8398dSJeff Roberson 		TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1758ace8398dSJeff Roberson 		sfp->sf_si = NULL;
1759ace8398dSJeff Roberson 		stp = sfp->sf_td;
1760ace8398dSJeff Roberson 		mtx_lock(&stp->st_mtx);
1761ace8398dSJeff Roberson 		stp->st_flags |= SELTD_PENDING;
1762ace8398dSJeff Roberson 		cv_broadcastpri(&stp->st_wait, pri);
1763ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1764fcb5b3a4SKonstantin Belousov 		if (refcount_release(&sfp->sf_refs))
1765fcb5b3a4SKonstantin Belousov 			uma_zfree(selfd_zone, sfp);
1766b40ce416SJulian Elischer 	}
1767ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1768ace8398dSJeff Roberson }
1769ace8398dSJeff Roberson 
1770ace8398dSJeff Roberson static void
1771ace8398dSJeff Roberson seltdinit(struct thread *td)
1772ace8398dSJeff Roberson {
1773ace8398dSJeff Roberson 	struct seltd *stp;
1774ace8398dSJeff Roberson 
1775ace8398dSJeff Roberson 	if ((stp = td->td_sel) != NULL)
1776ace8398dSJeff Roberson 		goto out;
1777ace8398dSJeff Roberson 	td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1778ace8398dSJeff Roberson 	mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1779ace8398dSJeff Roberson 	cv_init(&stp->st_wait, "select");
1780ace8398dSJeff Roberson out:
1781ace8398dSJeff Roberson 	stp->st_flags = 0;
1782ace8398dSJeff Roberson 	STAILQ_INIT(&stp->st_selq);
1783ace8398dSJeff Roberson }
1784ace8398dSJeff Roberson 
1785ace8398dSJeff Roberson static int
1786cf5e4fe6SDavide Italiano seltdwait(struct thread *td, sbintime_t sbt, sbintime_t precision)
1787ace8398dSJeff Roberson {
1788ace8398dSJeff Roberson 	struct seltd *stp;
1789ace8398dSJeff Roberson 	int error;
1790ace8398dSJeff Roberson 
1791ace8398dSJeff Roberson 	stp = td->td_sel;
1792ace8398dSJeff Roberson 	/*
1793ace8398dSJeff Roberson 	 * An event of interest may occur while we do not hold the seltd
1794ace8398dSJeff Roberson 	 * locked so check the pending flag before we sleep.
1795ace8398dSJeff Roberson 	 */
1796ace8398dSJeff Roberson 	mtx_lock(&stp->st_mtx);
1797ace8398dSJeff Roberson 	/*
1798ace8398dSJeff Roberson 	 * Any further calls to selrecord will be a rescan.
1799ace8398dSJeff Roberson 	 */
1800ace8398dSJeff Roberson 	stp->st_flags |= SELTD_RESCAN;
1801ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_PENDING) {
1802ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1803ace8398dSJeff Roberson 		return (0);
1804ace8398dSJeff Roberson 	}
1805cf5e4fe6SDavide Italiano 	if (sbt == 0)
1806cf5e4fe6SDavide Italiano 		error = EWOULDBLOCK;
1807cf5e4fe6SDavide Italiano 	else if (sbt != -1)
1808cf5e4fe6SDavide Italiano 		error = cv_timedwait_sig_sbt(&stp->st_wait, &stp->st_mtx,
1809cf5e4fe6SDavide Italiano 		    sbt, precision, C_ABSOLUTE);
1810ace8398dSJeff Roberson 	else
1811ace8398dSJeff Roberson 		error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
1812ace8398dSJeff Roberson 	mtx_unlock(&stp->st_mtx);
1813ace8398dSJeff Roberson 
1814ace8398dSJeff Roberson 	return (error);
1815ace8398dSJeff Roberson }
1816ace8398dSJeff Roberson 
1817ace8398dSJeff Roberson void
1818ace8398dSJeff Roberson seltdfini(struct thread *td)
1819ace8398dSJeff Roberson {
1820ace8398dSJeff Roberson 	struct seltd *stp;
1821ace8398dSJeff Roberson 
1822ace8398dSJeff Roberson 	stp = td->td_sel;
1823ace8398dSJeff Roberson 	if (stp == NULL)
1824ace8398dSJeff Roberson 		return;
1825ace8398dSJeff Roberson 	if (stp->st_free1)
1826ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free1);
1827ace8398dSJeff Roberson 	if (stp->st_free2)
1828ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free2);
1829ace8398dSJeff Roberson 	td->td_sel = NULL;
183036bce27bSKonstantin Belousov 	cv_destroy(&stp->st_wait);
183136bce27bSKonstantin Belousov 	mtx_destroy(&stp->st_mtx);
1832ace8398dSJeff Roberson 	free(stp, M_SELECT);
1833ace8398dSJeff Roberson }
1834ace8398dSJeff Roberson 
1835ace8398dSJeff Roberson /*
1836ace8398dSJeff Roberson  * Remove the references to the thread from all of the objects we were
1837ace8398dSJeff Roberson  * polling.
1838ace8398dSJeff Roberson  */
1839ace8398dSJeff Roberson static void
1840ace8398dSJeff Roberson seltdclear(struct thread *td)
1841ace8398dSJeff Roberson {
1842ace8398dSJeff Roberson 	struct seltd *stp;
1843ace8398dSJeff Roberson 	struct selfd *sfp;
1844ace8398dSJeff Roberson 	struct selfd *sfn;
1845ace8398dSJeff Roberson 
1846ace8398dSJeff Roberson 	stp = td->td_sel;
1847ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
1848ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1849ace8398dSJeff Roberson 	stp->st_flags = 0;
1850df8bae1dSRodney W. Grimes }
1851265fc98fSSeigo Tanimura 
18524d77a549SAlfred Perlstein static void selectinit(void *);
1853ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
1854265fc98fSSeigo Tanimura static void
1855ace8398dSJeff Roberson selectinit(void *dummy __unused)
1856265fc98fSSeigo Tanimura {
18572141453eSJeff Roberson 
1858ace8398dSJeff Roberson 	selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL,
1859ace8398dSJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, 0);
18602141453eSJeff Roberson 	mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);
1861265fc98fSSeigo Tanimura }
18620acf5d0bSMark Johnston 
18630acf5d0bSMark Johnston /*
18640acf5d0bSMark Johnston  * Set up a syscall return value that follows the convention specified for
18650acf5d0bSMark Johnston  * posix_* functions.
18660acf5d0bSMark Johnston  */
18670acf5d0bSMark Johnston int
18680acf5d0bSMark Johnston kern_posix_error(struct thread *td, int error)
18690acf5d0bSMark Johnston {
18700acf5d0bSMark Johnston 
18710acf5d0bSMark Johnston 	if (error <= 0)
18720acf5d0bSMark Johnston 		return (error);
18730acf5d0bSMark Johnston 	td->td_errno = error;
18740acf5d0bSMark Johnston 	td->td_pflags |= TDP_NERRNO;
18750acf5d0bSMark Johnston 	td->td_retval[0] = error;
18760acf5d0bSMark Johnston 	return (0);
18770acf5d0bSMark Johnston }
1878