xref: /freebsd/sys/kern/sys_generic.c (revision cbd92ce62e92bd17871c9668c2c2bebac3e2ac2e)
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
1918451d0ddSKip Macy sys_read(td, uap)
192b40ce416SJulian Elischer 	struct thread *td;
193b064d43dSMatthew Dillon 	struct read_args *uap;
194df8bae1dSRodney W. Grimes {
195bcd9e0ddSJohn Baldwin 	struct uio auio;
196bcd9e0ddSJohn Baldwin 	struct iovec aiov;
197279d7226SMatthew Dillon 	int error;
198df8bae1dSRodney W. Grimes 
199526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
200bcd9e0ddSJohn Baldwin 		return (EINVAL);
201bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
202bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
203bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
204bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
205bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
206bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
207bcd9e0ddSJohn Baldwin 	error = kern_readv(td, uap->fd, &auio);
208279d7226SMatthew Dillon 	return (error);
209df8bae1dSRodney W. Grimes }
210df8bae1dSRodney W. Grimes 
211df8bae1dSRodney W. Grimes /*
212bcd9e0ddSJohn Baldwin  * Positioned read system call
2134160ccd9SAlan Cox  */
2144160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
2154160ccd9SAlan Cox struct pread_args {
2164160ccd9SAlan Cox 	int	fd;
2174160ccd9SAlan Cox 	void	*buf;
2184160ccd9SAlan Cox 	size_t	nbyte;
2198fe387abSDmitrij Tejblum 	int	pad;
2204160ccd9SAlan Cox 	off_t	offset;
2214160ccd9SAlan Cox };
2224160ccd9SAlan Cox #endif
2234160ccd9SAlan Cox int
224b38b22b0SEdward Tomasz Napierala sys_pread(struct thread *td, struct pread_args *uap)
225b38b22b0SEdward Tomasz Napierala {
226b38b22b0SEdward Tomasz Napierala 
227b38b22b0SEdward Tomasz Napierala 	return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
228b38b22b0SEdward Tomasz Napierala }
229b38b22b0SEdward Tomasz Napierala 
230b38b22b0SEdward Tomasz Napierala int
231b38b22b0SEdward Tomasz Napierala kern_pread(struct thread *td, int fd, void *buf, size_t nbyte, off_t offset)
2324160ccd9SAlan Cox {
2334160ccd9SAlan Cox 	struct uio auio;
2344160ccd9SAlan Cox 	struct iovec aiov;
235bcd9e0ddSJohn Baldwin 	int error;
2364160ccd9SAlan Cox 
237b38b22b0SEdward Tomasz Napierala 	if (nbyte > IOSIZE_MAX)
238bcd9e0ddSJohn Baldwin 		return (EINVAL);
239b38b22b0SEdward Tomasz Napierala 	aiov.iov_base = buf;
240b38b22b0SEdward Tomasz Napierala 	aiov.iov_len = nbyte;
2414160ccd9SAlan Cox 	auio.uio_iov = &aiov;
2424160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
243b38b22b0SEdward Tomasz Napierala 	auio.uio_resid = nbyte;
2444160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
245b38b22b0SEdward Tomasz Napierala 	error = kern_preadv(td, fd, &auio, offset);
2464160ccd9SAlan Cox 	return (error);
2474160ccd9SAlan Cox }
2484160ccd9SAlan Cox 
2490538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6)
250c2815ad5SPeter Wemm int
251b38b22b0SEdward Tomasz Napierala freebsd6_pread(struct thread *td, struct freebsd6_pread_args *uap)
252c2815ad5SPeter Wemm {
253c2815ad5SPeter Wemm 
254b38b22b0SEdward Tomasz Napierala 	return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
255c2815ad5SPeter Wemm }
2560538aafcSKonstantin Belousov #endif
257c2815ad5SPeter Wemm 
2584160ccd9SAlan Cox /*
259df8bae1dSRodney W. Grimes  * Scatter read system call.
260df8bae1dSRodney W. Grimes  */
261d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
262df8bae1dSRodney W. Grimes struct readv_args {
2637147b19dSBruce Evans 	int	fd;
264df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
265df8bae1dSRodney W. Grimes 	u_int	iovcnt;
266df8bae1dSRodney W. Grimes };
267d2d3e875SBruce Evans #endif
26826f9a767SRodney W. Grimes int
2698451d0ddSKip Macy sys_readv(struct thread *td, struct readv_args *uap)
270df8bae1dSRodney W. Grimes {
271b88ec951SJohn Baldwin 	struct uio *auio;
272b88ec951SJohn Baldwin 	int error;
273b88ec951SJohn Baldwin 
274b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
275b88ec951SJohn Baldwin 	if (error)
276b88ec951SJohn Baldwin 		return (error);
277b88ec951SJohn Baldwin 	error = kern_readv(td, uap->fd, auio);
278b88ec951SJohn Baldwin 	free(auio, M_IOV);
279b88ec951SJohn Baldwin 	return (error);
280b88ec951SJohn Baldwin }
281b88ec951SJohn Baldwin 
282b88ec951SJohn Baldwin int
283b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio)
284b88ec951SJohn Baldwin {
285b064d43dSMatthew Dillon 	struct file *fp;
286bcd9e0ddSJohn Baldwin 	int error;
287bcd9e0ddSJohn Baldwin 
288*cbd92ce6SMatt Macy 	error = fget_read(td, fd, &cap_read_rights, &fp);
289bcd9e0ddSJohn Baldwin 	if (error)
290bcd9e0ddSJohn Baldwin 		return (error);
291bcd9e0ddSJohn Baldwin 	error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
292bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
293bcd9e0ddSJohn Baldwin 	return (error);
294bcd9e0ddSJohn Baldwin }
295bcd9e0ddSJohn Baldwin 
296bcd9e0ddSJohn Baldwin /*
297bcd9e0ddSJohn Baldwin  * Scatter positioned read system call.
298bcd9e0ddSJohn Baldwin  */
299bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
300bcd9e0ddSJohn Baldwin struct preadv_args {
301bcd9e0ddSJohn Baldwin 	int	fd;
302bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
303bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
304bcd9e0ddSJohn Baldwin 	off_t	offset;
305bcd9e0ddSJohn Baldwin };
306bcd9e0ddSJohn Baldwin #endif
307bcd9e0ddSJohn Baldwin int
3088451d0ddSKip Macy sys_preadv(struct thread *td, struct preadv_args *uap)
309bcd9e0ddSJohn Baldwin {
310bcd9e0ddSJohn Baldwin 	struct uio *auio;
311bcd9e0ddSJohn Baldwin 	int error;
312bcd9e0ddSJohn Baldwin 
313bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
314bcd9e0ddSJohn Baldwin 	if (error)
315bcd9e0ddSJohn Baldwin 		return (error);
316bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, auio, uap->offset);
317bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
318bcd9e0ddSJohn Baldwin 	return (error);
319bcd9e0ddSJohn Baldwin }
320bcd9e0ddSJohn Baldwin 
321bcd9e0ddSJohn Baldwin int
322bcd9e0ddSJohn Baldwin kern_preadv(td, fd, auio, offset)
323bcd9e0ddSJohn Baldwin 	struct thread *td;
324bcd9e0ddSJohn Baldwin 	int fd;
325bcd9e0ddSJohn Baldwin 	struct uio *auio;
326bcd9e0ddSJohn Baldwin 	off_t offset;
327bcd9e0ddSJohn Baldwin {
328bcd9e0ddSJohn Baldwin 	struct file *fp;
329bcd9e0ddSJohn Baldwin 	int error;
330bcd9e0ddSJohn Baldwin 
331*cbd92ce6SMatt Macy 	error = fget_read(td, fd, &cap_pread_rights, &fp);
332bcd9e0ddSJohn Baldwin 	if (error)
333bcd9e0ddSJohn Baldwin 		return (error);
334bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
335bcd9e0ddSJohn Baldwin 		error = ESPIPE;
33632a1fb0dSMahdi Mokhtari 	else if (offset < 0 &&
33732a1fb0dSMahdi Mokhtari 	    (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR))
338bcd9e0ddSJohn Baldwin 		error = EINVAL;
339bcd9e0ddSJohn Baldwin 	else
340bcd9e0ddSJohn Baldwin 		error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
341bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
342bcd9e0ddSJohn Baldwin 	return (error);
343bcd9e0ddSJohn Baldwin }
344bcd9e0ddSJohn Baldwin 
345bcd9e0ddSJohn Baldwin /*
346bcd9e0ddSJohn Baldwin  * Common code for readv and preadv that reads data in
347bcd9e0ddSJohn Baldwin  * from a file using the passed in uio, offset, and flags.
348bcd9e0ddSJohn Baldwin  */
349bcd9e0ddSJohn Baldwin static int
350bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags)
351bcd9e0ddSJohn Baldwin 	struct thread *td;
352bcd9e0ddSJohn Baldwin 	int fd;
353bcd9e0ddSJohn Baldwin 	struct file *fp;
354bcd9e0ddSJohn Baldwin 	struct uio *auio;
355bcd9e0ddSJohn Baldwin 	off_t offset;
356bcd9e0ddSJohn Baldwin 	int flags;
357bcd9e0ddSJohn Baldwin {
358bcd9e0ddSJohn Baldwin 	ssize_t cnt;
35982641acdSAlan Cox 	int error;
360df8bae1dSRodney W. Grimes #ifdef KTRACE
361552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
362df8bae1dSRodney W. Grimes #endif
363df8bae1dSRodney W. Grimes 
36451d1f690SRobert Watson 	AUDIT_ARG_FD(fd);
36551d1f690SRobert Watson 
3664f8d23d6SPoul-Henning Kamp 	/* Finish zero length reads right here */
3674f8d23d6SPoul-Henning Kamp 	if (auio->uio_resid == 0) {
3684f8d23d6SPoul-Henning Kamp 		td->td_retval[0] = 0;
3694f8d23d6SPoul-Henning Kamp 		return (0);
3704f8d23d6SPoul-Henning Kamp 	}
371552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_READ;
372bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
373552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
374df8bae1dSRodney W. Grimes #ifdef KTRACE
375552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
376552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
377df8bae1dSRodney W. Grimes #endif
378552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
379bcd9e0ddSJohn Baldwin 	if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
380552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
381df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
382df8bae1dSRodney W. Grimes 			error = 0;
383279d7226SMatthew Dillon 	}
384552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
385df8bae1dSRodney W. Grimes #ifdef KTRACE
386552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
387552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
388b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_READ, ktruio, error);
389df8bae1dSRodney W. Grimes 	}
390df8bae1dSRodney W. Grimes #endif
391b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
392df8bae1dSRodney W. Grimes 	return (error);
393df8bae1dSRodney W. Grimes }
394df8bae1dSRodney W. Grimes 
395d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
396df8bae1dSRodney W. Grimes struct write_args {
397df8bae1dSRodney W. Grimes 	int	fd;
398134e06feSBruce Evans 	const void *buf;
399134e06feSBruce Evans 	size_t	nbyte;
400df8bae1dSRodney W. Grimes };
401d2d3e875SBruce Evans #endif
40226f9a767SRodney W. Grimes int
4038451d0ddSKip Macy sys_write(td, uap)
404b40ce416SJulian Elischer 	struct thread *td;
405b064d43dSMatthew Dillon 	struct write_args *uap;
406df8bae1dSRodney W. Grimes {
407bcd9e0ddSJohn Baldwin 	struct uio auio;
408bcd9e0ddSJohn Baldwin 	struct iovec aiov;
409279d7226SMatthew Dillon 	int error;
410df8bae1dSRodney W. Grimes 
411526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
412bcd9e0ddSJohn Baldwin 		return (EINVAL);
413bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
414bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
415bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
416bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
417bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
418bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
419bcd9e0ddSJohn Baldwin 	error = kern_writev(td, uap->fd, &auio);
420279d7226SMatthew Dillon 	return (error);
421df8bae1dSRodney W. Grimes }
422df8bae1dSRodney W. Grimes 
423df8bae1dSRodney W. Grimes /*
4240c14ff0eSRobert Watson  * Positioned write system call.
4254160ccd9SAlan Cox  */
4264160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
4274160ccd9SAlan Cox struct pwrite_args {
4284160ccd9SAlan Cox 	int	fd;
4294160ccd9SAlan Cox 	const void *buf;
4304160ccd9SAlan Cox 	size_t	nbyte;
4318fe387abSDmitrij Tejblum 	int	pad;
4324160ccd9SAlan Cox 	off_t	offset;
4334160ccd9SAlan Cox };
4344160ccd9SAlan Cox #endif
4354160ccd9SAlan Cox int
436b38b22b0SEdward Tomasz Napierala sys_pwrite(struct thread *td, struct pwrite_args *uap)
437b38b22b0SEdward Tomasz Napierala {
438b38b22b0SEdward Tomasz Napierala 
439b38b22b0SEdward Tomasz Napierala 	return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
440b38b22b0SEdward Tomasz Napierala }
441b38b22b0SEdward Tomasz Napierala 
442b38b22b0SEdward Tomasz Napierala int
443b38b22b0SEdward Tomasz Napierala kern_pwrite(struct thread *td, int fd, const void *buf, size_t nbyte,
444b38b22b0SEdward Tomasz Napierala     off_t offset)
4454160ccd9SAlan Cox {
4464160ccd9SAlan Cox 	struct uio auio;
4474160ccd9SAlan Cox 	struct iovec aiov;
448bcd9e0ddSJohn Baldwin 	int error;
4494160ccd9SAlan Cox 
450b38b22b0SEdward Tomasz Napierala 	if (nbyte > IOSIZE_MAX)
451bcd9e0ddSJohn Baldwin 		return (EINVAL);
452b38b22b0SEdward Tomasz Napierala 	aiov.iov_base = (void *)(uintptr_t)buf;
453b38b22b0SEdward Tomasz Napierala 	aiov.iov_len = nbyte;
4544160ccd9SAlan Cox 	auio.uio_iov = &aiov;
4554160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
456b38b22b0SEdward Tomasz Napierala 	auio.uio_resid = nbyte;
4574160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
458b38b22b0SEdward Tomasz Napierala 	error = kern_pwritev(td, fd, &auio, offset);
4594160ccd9SAlan Cox 	return (error);
4604160ccd9SAlan Cox }
4614160ccd9SAlan Cox 
4620538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6)
463c2815ad5SPeter Wemm int
464b38b22b0SEdward Tomasz Napierala freebsd6_pwrite(struct thread *td, struct freebsd6_pwrite_args *uap)
465c2815ad5SPeter Wemm {
466c2815ad5SPeter Wemm 
467b38b22b0SEdward Tomasz Napierala 	return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
468c2815ad5SPeter Wemm }
4690538aafcSKonstantin Belousov #endif
470c2815ad5SPeter Wemm 
4714160ccd9SAlan Cox /*
4720c14ff0eSRobert Watson  * Gather write system call.
473df8bae1dSRodney W. Grimes  */
474d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
475df8bae1dSRodney W. Grimes struct writev_args {
476df8bae1dSRodney W. Grimes 	int	fd;
477df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
478df8bae1dSRodney W. Grimes 	u_int	iovcnt;
479df8bae1dSRodney W. Grimes };
480d2d3e875SBruce Evans #endif
48126f9a767SRodney W. Grimes int
4828451d0ddSKip Macy sys_writev(struct thread *td, struct writev_args *uap)
483df8bae1dSRodney W. Grimes {
484b88ec951SJohn Baldwin 	struct uio *auio;
485b88ec951SJohn Baldwin 	int error;
486b88ec951SJohn Baldwin 
487b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
488b88ec951SJohn Baldwin 	if (error)
489b88ec951SJohn Baldwin 		return (error);
490b88ec951SJohn Baldwin 	error = kern_writev(td, uap->fd, auio);
491b88ec951SJohn Baldwin 	free(auio, M_IOV);
492b88ec951SJohn Baldwin 	return (error);
493b88ec951SJohn Baldwin }
494b88ec951SJohn Baldwin 
495b88ec951SJohn Baldwin int
496b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio)
497b88ec951SJohn Baldwin {
498b064d43dSMatthew Dillon 	struct file *fp;
499bcd9e0ddSJohn Baldwin 	int error;
500bcd9e0ddSJohn Baldwin 
501*cbd92ce6SMatt Macy 	error = fget_write(td, fd, &cap_write_rights, &fp);
502bcd9e0ddSJohn Baldwin 	if (error)
503af56abaaSJohn Baldwin 		return (error);
504bcd9e0ddSJohn Baldwin 	error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
505bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
506bcd9e0ddSJohn Baldwin 	return (error);
507bcd9e0ddSJohn Baldwin }
508bcd9e0ddSJohn Baldwin 
509bcd9e0ddSJohn Baldwin /*
5100c14ff0eSRobert Watson  * Gather positioned write system call.
511bcd9e0ddSJohn Baldwin  */
512bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
513bcd9e0ddSJohn Baldwin struct pwritev_args {
514bcd9e0ddSJohn Baldwin 	int	fd;
515bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
516bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
517bcd9e0ddSJohn Baldwin 	off_t	offset;
518bcd9e0ddSJohn Baldwin };
519bcd9e0ddSJohn Baldwin #endif
520bcd9e0ddSJohn Baldwin int
5218451d0ddSKip Macy sys_pwritev(struct thread *td, struct pwritev_args *uap)
522bcd9e0ddSJohn Baldwin {
523bcd9e0ddSJohn Baldwin 	struct uio *auio;
524bcd9e0ddSJohn Baldwin 	int error;
525bcd9e0ddSJohn Baldwin 
526bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
527bcd9e0ddSJohn Baldwin 	if (error)
528bcd9e0ddSJohn Baldwin 		return (error);
529bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, auio, uap->offset);
530bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
531bcd9e0ddSJohn Baldwin 	return (error);
532bcd9e0ddSJohn Baldwin }
533bcd9e0ddSJohn Baldwin 
534bcd9e0ddSJohn Baldwin int
535bcd9e0ddSJohn Baldwin kern_pwritev(td, fd, auio, offset)
536bcd9e0ddSJohn Baldwin 	struct thread *td;
537bcd9e0ddSJohn Baldwin 	struct uio *auio;
538bcd9e0ddSJohn Baldwin 	int fd;
539bcd9e0ddSJohn Baldwin 	off_t offset;
540bcd9e0ddSJohn Baldwin {
541bcd9e0ddSJohn Baldwin 	struct file *fp;
542bcd9e0ddSJohn Baldwin 	int error;
543bcd9e0ddSJohn Baldwin 
544*cbd92ce6SMatt Macy 	error = fget_write(td, fd, &cap_pwrite_rights, &fp);
545bcd9e0ddSJohn Baldwin 	if (error)
546af56abaaSJohn Baldwin 		return (error);
547bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
548bcd9e0ddSJohn Baldwin 		error = ESPIPE;
54932a1fb0dSMahdi Mokhtari 	else if (offset < 0 &&
55032a1fb0dSMahdi Mokhtari 	    (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR))
551bcd9e0ddSJohn Baldwin 		error = EINVAL;
552bcd9e0ddSJohn Baldwin 	else
553bcd9e0ddSJohn Baldwin 		error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
554bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
555bcd9e0ddSJohn Baldwin 	return (error);
556bcd9e0ddSJohn Baldwin }
557bcd9e0ddSJohn Baldwin 
558bcd9e0ddSJohn Baldwin /*
559bcd9e0ddSJohn Baldwin  * Common code for writev and pwritev that writes data to
560bcd9e0ddSJohn Baldwin  * a file using the passed in uio, offset, and flags.
561bcd9e0ddSJohn Baldwin  */
562bcd9e0ddSJohn Baldwin static int
563bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags)
564bcd9e0ddSJohn Baldwin 	struct thread *td;
565bcd9e0ddSJohn Baldwin 	int fd;
566bcd9e0ddSJohn Baldwin 	struct file *fp;
567bcd9e0ddSJohn Baldwin 	struct uio *auio;
568bcd9e0ddSJohn Baldwin 	off_t offset;
569bcd9e0ddSJohn Baldwin 	int flags;
570bcd9e0ddSJohn Baldwin {
571bcd9e0ddSJohn Baldwin 	ssize_t cnt;
572552afd9cSPoul-Henning Kamp 	int error;
573df8bae1dSRodney W. Grimes #ifdef KTRACE
574552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
575df8bae1dSRodney W. Grimes #endif
576df8bae1dSRodney W. Grimes 
57751d1f690SRobert Watson 	AUDIT_ARG_FD(fd);
578552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_WRITE;
579552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
580bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
581df8bae1dSRodney W. Grimes #ifdef KTRACE
582552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
583552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
584df8bae1dSRodney W. Grimes #endif
585552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
586ad9789f6SKonstantin Belousov 	if (fp->f_type == DTYPE_VNODE &&
587ad9789f6SKonstantin Belousov 	    (fp->f_vnread_flags & FDEVFS_VNODE) == 0)
5889440653dSMatthew Dillon 		bwillwrite();
589bcd9e0ddSJohn Baldwin 	if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
590552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
591df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
592df8bae1dSRodney W. Grimes 			error = 0;
593bcd9e0ddSJohn Baldwin 		/* Socket layer is responsible for issuing SIGPIPE. */
5946f7ca813SBruce M Simpson 		if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
595b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
5967a6f3d78SJohn Baldwin 			tdsignal(td, SIGPIPE);
597b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
59819eb87d2SJohn Baldwin 		}
599df8bae1dSRodney W. Grimes 	}
600552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
601df8bae1dSRodney W. Grimes #ifdef KTRACE
602552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
603552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
604b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_WRITE, ktruio, error);
605df8bae1dSRodney W. Grimes 	}
606df8bae1dSRodney W. Grimes #endif
607b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
608df8bae1dSRodney W. Grimes 	return (error);
609df8bae1dSRodney W. Grimes }
610df8bae1dSRodney W. Grimes 
611e4650294SJohn Baldwin /*
612e4650294SJohn Baldwin  * Truncate a file given a file descriptor.
613e4650294SJohn Baldwin  *
614e4650294SJohn Baldwin  * Can't use fget_write() here, since must return EINVAL and not EBADF if the
615e4650294SJohn Baldwin  * descriptor isn't writable.
616e4650294SJohn Baldwin  */
617e4650294SJohn Baldwin int
618e4650294SJohn Baldwin kern_ftruncate(td, fd, length)
619e4650294SJohn Baldwin 	struct thread *td;
620e4650294SJohn Baldwin 	int fd;
621e4650294SJohn Baldwin 	off_t length;
622e4650294SJohn Baldwin {
623e4650294SJohn Baldwin 	struct file *fp;
624e4650294SJohn Baldwin 	int error;
625e4650294SJohn Baldwin 
62614961ba7SRobert Watson 	AUDIT_ARG_FD(fd);
627e4650294SJohn Baldwin 	if (length < 0)
628e4650294SJohn Baldwin 		return (EINVAL);
629*cbd92ce6SMatt Macy 	error = fget(td, fd, &cap_ftruncate_rights, &fp);
630e4650294SJohn Baldwin 	if (error)
631e4650294SJohn Baldwin 		return (error);
63214961ba7SRobert Watson 	AUDIT_ARG_FILE(td->td_proc, fp);
633e4650294SJohn Baldwin 	if (!(fp->f_flag & FWRITE)) {
634e4650294SJohn Baldwin 		fdrop(fp, td);
635e4650294SJohn Baldwin 		return (EINVAL);
636e4650294SJohn Baldwin 	}
637e4650294SJohn Baldwin 	error = fo_truncate(fp, length, td->td_ucred, td);
638e4650294SJohn Baldwin 	fdrop(fp, td);
639e4650294SJohn Baldwin 	return (error);
640e4650294SJohn Baldwin }
641e4650294SJohn Baldwin 
642e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
643e4650294SJohn Baldwin struct ftruncate_args {
644e4650294SJohn Baldwin 	int	fd;
645e4650294SJohn Baldwin 	int	pad;
646e4650294SJohn Baldwin 	off_t	length;
647e4650294SJohn Baldwin };
648e4650294SJohn Baldwin #endif
649e4650294SJohn Baldwin int
6508451d0ddSKip Macy sys_ftruncate(td, uap)
651e4650294SJohn Baldwin 	struct thread *td;
652e4650294SJohn Baldwin 	struct ftruncate_args *uap;
653e4650294SJohn Baldwin {
654e4650294SJohn Baldwin 
655e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
656e4650294SJohn Baldwin }
657e4650294SJohn Baldwin 
658e4650294SJohn Baldwin #if defined(COMPAT_43)
659e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
660e4650294SJohn Baldwin struct oftruncate_args {
661e4650294SJohn Baldwin 	int	fd;
662e4650294SJohn Baldwin 	long	length;
663e4650294SJohn Baldwin };
664e4650294SJohn Baldwin #endif
665e4650294SJohn Baldwin int
666e4650294SJohn Baldwin oftruncate(td, uap)
667e4650294SJohn Baldwin 	struct thread *td;
668e4650294SJohn Baldwin 	struct oftruncate_args *uap;
669e4650294SJohn Baldwin {
670e4650294SJohn Baldwin 
671e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
672e4650294SJohn Baldwin }
673e4650294SJohn Baldwin #endif /* COMPAT_43 */
674e4650294SJohn Baldwin 
675d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
676df8bae1dSRodney W. Grimes struct ioctl_args {
677df8bae1dSRodney W. Grimes 	int	fd;
678069e9bc1SDoug Rabson 	u_long	com;
679df8bae1dSRodney W. Grimes 	caddr_t	data;
680df8bae1dSRodney W. Grimes };
681d2d3e875SBruce Evans #endif
682df8bae1dSRodney W. Grimes /* ARGSUSED */
68326f9a767SRodney W. Grimes int
6848451d0ddSKip Macy sys_ioctl(struct thread *td, struct ioctl_args *uap)
685df8bae1dSRodney W. Grimes {
68650ae6690SHans Petter Selasky 	u_char smalldata[SYS_IOCTL_SMALL_SIZE] __aligned(SYS_IOCTL_SMALL_ALIGN);
6873e15c66fSPoul-Henning Kamp 	u_long com;
6889fddcc66SRuslan Ermilov 	int arg, error;
6893e15c66fSPoul-Henning Kamp 	u_int size;
6909fddcc66SRuslan Ermilov 	caddr_t data;
691df8bae1dSRodney W. Grimes 
6929fc6aa06SPoul-Henning Kamp 	if (uap->com > 0xffffffff) {
6939fc6aa06SPoul-Henning Kamp 		printf(
6949fc6aa06SPoul-Henning Kamp 		    "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
695431f8906SJulian Elischer 		    td->td_proc->p_pid, td->td_name, uap->com);
6969fc6aa06SPoul-Henning Kamp 		uap->com &= 0xffffffff;
6979fc6aa06SPoul-Henning Kamp 	}
698d9f46233SJohn Baldwin 	com = uap->com;
699df8bae1dSRodney W. Grimes 
700df8bae1dSRodney W. Grimes 	/*
701df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
702df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
703df8bae1dSRodney W. Grimes 	 */
704df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
705ca51b19bSPoul-Henning Kamp 	if ((size > IOCPARM_MAX) ||
706ca51b19bSPoul-Henning Kamp 	    ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
7072de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
7082de92a38SPeter Wemm 	    ((com & IOC_OUT) && size == 0) ||
7092de92a38SPeter Wemm #else
7102de92a38SPeter Wemm 	    ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
7112de92a38SPeter Wemm #endif
7129fddcc66SRuslan Ermilov 	    ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
713426da3bcSAlfred Perlstein 		return (ENOTTY);
714279d7226SMatthew Dillon 
715ca51b19bSPoul-Henning Kamp 	if (size > 0) {
716715457f6SDavid E. O'Brien 		if (com & IOC_VOID) {
7179fddcc66SRuslan Ermilov 			/* Integer argument. */
7189fddcc66SRuslan Ermilov 			arg = (intptr_t)uap->data;
7199fddcc66SRuslan Ermilov 			data = (void *)&arg;
7209fddcc66SRuslan Ermilov 			size = 0;
7215cbf44bfSMateusz Guzik 		} else {
7220ecd606bSHans Petter Selasky 			if (size > SYS_IOCTL_SMALL_SIZE)
723715457f6SDavid E. O'Brien 				data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
7240ecd606bSHans Petter Selasky 			else
7250ecd606bSHans Petter Selasky 				data = smalldata;
7265cbf44bfSMateusz Guzik 		}
7279fddcc66SRuslan Ermilov 	} else
7289fddcc66SRuslan Ermilov 		data = (void *)&uap->data;
729df8bae1dSRodney W. Grimes 	if (com & IOC_IN) {
730df8bae1dSRodney W. Grimes 		error = copyin(uap->data, data, (u_int)size);
7315cbf44bfSMateusz Guzik 		if (error != 0)
7325cbf44bfSMateusz Guzik 			goto out;
733ca51b19bSPoul-Henning Kamp 	} else if (com & IOC_OUT) {
734df8bae1dSRodney W. Grimes 		/*
735df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
736df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
737df8bae1dSRodney W. Grimes 		 */
738df8bae1dSRodney W. Grimes 		bzero(data, size);
739279d7226SMatthew Dillon 	}
740df8bae1dSRodney W. Grimes 
741d9f46233SJohn Baldwin 	error = kern_ioctl(td, uap->fd, com, data);
742d9f46233SJohn Baldwin 
743d9f46233SJohn Baldwin 	if (error == 0 && (com & IOC_OUT))
744d9f46233SJohn Baldwin 		error = copyout(data, uap->data, (u_int)size);
745d9f46233SJohn Baldwin 
7465cbf44bfSMateusz Guzik out:
7470ecd606bSHans Petter Selasky 	if (size > SYS_IOCTL_SMALL_SIZE)
7489fddcc66SRuslan Ermilov 		free(data, M_IOCTLOPS);
749d9f46233SJohn Baldwin 	return (error);
750d9f46233SJohn Baldwin }
751d9f46233SJohn Baldwin 
752d9f46233SJohn Baldwin int
753d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
754d9f46233SJohn Baldwin {
755d9f46233SJohn Baldwin 	struct file *fp;
756d9f46233SJohn Baldwin 	struct filedesc *fdp;
7572609222aSPawel Jakub Dawidek 	int error, tmp, locked;
758d9f46233SJohn Baldwin 
75964c9a4d9SRobert Watson 	AUDIT_ARG_FD(fd);
76064c9a4d9SRobert Watson 	AUDIT_ARG_CMD(com);
7612609222aSPawel Jakub Dawidek 
762d9f46233SJohn Baldwin 	fdp = td->td_proc->p_fd;
7632609222aSPawel Jakub Dawidek 
764d9f46233SJohn Baldwin 	switch (com) {
765d9f46233SJohn Baldwin 	case FIONCLEX:
766d9f46233SJohn Baldwin 	case FIOCLEX:
7675e3f7694SRobert Watson 		FILEDESC_XLOCK(fdp);
7682609222aSPawel Jakub Dawidek 		locked = LA_XLOCKED;
7692609222aSPawel Jakub Dawidek 		break;
7702609222aSPawel Jakub Dawidek 	default:
7712609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
7722609222aSPawel Jakub Dawidek 		FILEDESC_SLOCK(fdp);
7732609222aSPawel Jakub Dawidek 		locked = LA_SLOCKED;
7742609222aSPawel Jakub Dawidek #else
7752609222aSPawel Jakub Dawidek 		locked = LA_UNLOCKED;
7762609222aSPawel Jakub Dawidek #endif
7772609222aSPawel Jakub Dawidek 		break;
7782609222aSPawel Jakub Dawidek 	}
7792609222aSPawel Jakub Dawidek 
7802609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
7812609222aSPawel Jakub Dawidek 	if ((fp = fget_locked(fdp, fd)) == NULL) {
7822609222aSPawel Jakub Dawidek 		error = EBADF;
7832609222aSPawel Jakub Dawidek 		goto out;
7842609222aSPawel Jakub Dawidek 	}
7852609222aSPawel Jakub Dawidek 	if ((error = cap_ioctl_check(fdp, fd, com)) != 0) {
7862609222aSPawel Jakub Dawidek 		fp = NULL;	/* fhold() was not called yet */
7872609222aSPawel Jakub Dawidek 		goto out;
7882609222aSPawel Jakub Dawidek 	}
7892609222aSPawel Jakub Dawidek 	fhold(fp);
7902609222aSPawel Jakub Dawidek 	if (locked == LA_SLOCKED) {
7912609222aSPawel Jakub Dawidek 		FILEDESC_SUNLOCK(fdp);
7922609222aSPawel Jakub Dawidek 		locked = LA_UNLOCKED;
7932609222aSPawel Jakub Dawidek 	}
7942609222aSPawel Jakub Dawidek #else
795*cbd92ce6SMatt Macy 	error = fget(td, fd, &cap_ioctl_rights, &fp);
7967008be5bSPawel Jakub Dawidek 	if (error != 0) {
7972609222aSPawel Jakub Dawidek 		fp = NULL;
7982609222aSPawel Jakub Dawidek 		goto out;
7992609222aSPawel Jakub Dawidek 	}
8002609222aSPawel Jakub Dawidek #endif
8012609222aSPawel Jakub Dawidek 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
8022609222aSPawel Jakub Dawidek 		error = EBADF;
8032609222aSPawel Jakub Dawidek 		goto out;
8042609222aSPawel Jakub Dawidek 	}
8052609222aSPawel Jakub Dawidek 
8062609222aSPawel Jakub Dawidek 	switch (com) {
8072609222aSPawel Jakub Dawidek 	case FIONCLEX:
8082609222aSPawel Jakub Dawidek 		fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE;
8092609222aSPawel Jakub Dawidek 		goto out;
8102609222aSPawel Jakub Dawidek 	case FIOCLEX:
8112609222aSPawel Jakub Dawidek 		fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE;
812d9f46233SJohn Baldwin 		goto out;
813d9f46233SJohn Baldwin 	case FIONBIO:
814bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
815397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FNONBLOCK);
816df8bae1dSRodney W. Grimes 		else
817397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FNONBLOCK);
8188ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
819d9f46233SJohn Baldwin 		break;
820d9f46233SJohn Baldwin 	case FIOASYNC:
821bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
822397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FASYNC);
823df8bae1dSRodney W. Grimes 		else
824397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FASYNC);
8258ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
826d9f46233SJohn Baldwin 		break;
827df8bae1dSRodney W. Grimes 	}
8288ccf264fSPoul-Henning Kamp 
8298ccf264fSPoul-Henning Kamp 	error = fo_ioctl(fp, com, data, td->td_ucred, td);
830d9f46233SJohn Baldwin out:
8312609222aSPawel Jakub Dawidek 	switch (locked) {
8322609222aSPawel Jakub Dawidek 	case LA_XLOCKED:
8332609222aSPawel Jakub Dawidek 		FILEDESC_XUNLOCK(fdp);
8342609222aSPawel Jakub Dawidek 		break;
8352609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
8362609222aSPawel Jakub Dawidek 	case LA_SLOCKED:
8372609222aSPawel Jakub Dawidek 		FILEDESC_SUNLOCK(fdp);
8382609222aSPawel Jakub Dawidek 		break;
8392609222aSPawel Jakub Dawidek #endif
8402609222aSPawel Jakub Dawidek 	default:
8412609222aSPawel Jakub Dawidek 		FILEDESC_UNLOCK_ASSERT(fdp);
8422609222aSPawel Jakub Dawidek 		break;
8432609222aSPawel Jakub Dawidek 	}
8442609222aSPawel Jakub Dawidek 	if (fp != NULL)
845b40ce416SJulian Elischer 		fdrop(fp, td);
846df8bae1dSRodney W. Grimes 	return (error);
847df8bae1dSRodney W. Grimes }
848df8bae1dSRodney W. Grimes 
849125dcf8cSKonstantin Belousov int
850125dcf8cSKonstantin Belousov poll_no_poll(int events)
851125dcf8cSKonstantin Belousov {
852125dcf8cSKonstantin Belousov 	/*
853125dcf8cSKonstantin Belousov 	 * Return true for read/write.  If the user asked for something
854125dcf8cSKonstantin Belousov 	 * special, return POLLNVAL, so that clients have a way of
855125dcf8cSKonstantin Belousov 	 * determining reliably whether or not the extended
856125dcf8cSKonstantin Belousov 	 * functionality is present without hard-coding knowledge
857125dcf8cSKonstantin Belousov 	 * of specific filesystem implementations.
858125dcf8cSKonstantin Belousov 	 */
859125dcf8cSKonstantin Belousov 	if (events & ~POLLSTANDARD)
860125dcf8cSKonstantin Belousov 		return (POLLNVAL);
861125dcf8cSKonstantin Belousov 
862125dcf8cSKonstantin Belousov 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
863125dcf8cSKonstantin Belousov }
864125dcf8cSKonstantin Belousov 
865066d836bSKonstantin Belousov int
8668451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap)
867066d836bSKonstantin Belousov {
868066d836bSKonstantin Belousov 	struct timespec ts;
869066d836bSKonstantin Belousov 	struct timeval tv, *tvp;
870066d836bSKonstantin Belousov 	sigset_t set, *uset;
871066d836bSKonstantin Belousov 	int error;
872066d836bSKonstantin Belousov 
873066d836bSKonstantin Belousov 	if (uap->ts != NULL) {
874066d836bSKonstantin Belousov 		error = copyin(uap->ts, &ts, sizeof(ts));
875066d836bSKonstantin Belousov 		if (error != 0)
876066d836bSKonstantin Belousov 		    return (error);
877066d836bSKonstantin Belousov 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
878066d836bSKonstantin Belousov 		tvp = &tv;
879066d836bSKonstantin Belousov 	} else
880066d836bSKonstantin Belousov 		tvp = NULL;
881066d836bSKonstantin Belousov 	if (uap->sm != NULL) {
882066d836bSKonstantin Belousov 		error = copyin(uap->sm, &set, sizeof(set));
883066d836bSKonstantin Belousov 		if (error != 0)
884066d836bSKonstantin Belousov 			return (error);
885066d836bSKonstantin Belousov 		uset = &set;
886066d836bSKonstantin Belousov 	} else
887066d836bSKonstantin Belousov 		uset = NULL;
888066d836bSKonstantin Belousov 	return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
889066d836bSKonstantin Belousov 	    uset, NFDBITS));
890066d836bSKonstantin Belousov }
891066d836bSKonstantin Belousov 
892066d836bSKonstantin Belousov int
893066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
894066d836bSKonstantin Belousov     struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
895066d836bSKonstantin Belousov {
896066d836bSKonstantin Belousov 	int error;
897066d836bSKonstantin Belousov 
898066d836bSKonstantin Belousov 	if (uset != NULL) {
899066d836bSKonstantin Belousov 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
900066d836bSKonstantin Belousov 		    &td->td_oldsigmask, 0);
901066d836bSKonstantin Belousov 		if (error != 0)
902066d836bSKonstantin Belousov 			return (error);
903066d836bSKonstantin Belousov 		td->td_pflags |= TDP_OLDMASK;
904066d836bSKonstantin Belousov 		/*
905066d836bSKonstantin Belousov 		 * Make sure that ast() is called on return to
906066d836bSKonstantin Belousov 		 * usermode and TDP_OLDMASK is cleared, restoring old
907066d836bSKonstantin Belousov 		 * sigmask.
908066d836bSKonstantin Belousov 		 */
909066d836bSKonstantin Belousov 		thread_lock(td);
910066d836bSKonstantin Belousov 		td->td_flags |= TDF_ASTPENDING;
911066d836bSKonstantin Belousov 		thread_unlock(td);
912066d836bSKonstantin Belousov 	}
913066d836bSKonstantin Belousov 	error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
914066d836bSKonstantin Belousov 	return (error);
915066d836bSKonstantin Belousov }
916066d836bSKonstantin Belousov 
917d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
918df8bae1dSRodney W. Grimes struct select_args {
919b08f7993SSujal Patel 	int	nd;
920df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
921df8bae1dSRodney W. Grimes 	struct	timeval *tv;
922df8bae1dSRodney W. Grimes };
923d2d3e875SBruce Evans #endif
92426f9a767SRodney W. Grimes int
9258451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap)
926df8bae1dSRodney W. Grimes {
9278f19eb88SIan Dowse 	struct timeval tv, *tvp;
9288f19eb88SIan Dowse 	int error;
9298f19eb88SIan Dowse 
9308f19eb88SIan Dowse 	if (uap->tv != NULL) {
9318f19eb88SIan Dowse 		error = copyin(uap->tv, &tv, sizeof(tv));
9328f19eb88SIan Dowse 		if (error)
9338f19eb88SIan Dowse 			return (error);
9348f19eb88SIan Dowse 		tvp = &tv;
9358f19eb88SIan Dowse 	} else
9368f19eb88SIan Dowse 		tvp = NULL;
9378f19eb88SIan Dowse 
938b55ef216SKonstantin Belousov 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
939b55ef216SKonstantin Belousov 	    NFDBITS));
9408f19eb88SIan Dowse }
9418f19eb88SIan Dowse 
94256be1b9aSKonstantin Belousov /*
94356be1b9aSKonstantin Belousov  * In the unlikely case when user specified n greater then the last
94456be1b9aSKonstantin Belousov  * open file descriptor, check that no bits are set after the last
94556be1b9aSKonstantin Belousov  * valid fd.  We must return EBADF if any is set.
94656be1b9aSKonstantin Belousov  *
94756be1b9aSKonstantin Belousov  * There are applications that rely on the behaviour.
94856be1b9aSKonstantin Belousov  *
94956be1b9aSKonstantin Belousov  * nd is fd_lastfile + 1.
95056be1b9aSKonstantin Belousov  */
95156be1b9aSKonstantin Belousov static int
95256be1b9aSKonstantin Belousov select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits)
95356be1b9aSKonstantin Belousov {
95456be1b9aSKonstantin Belousov 	char *addr, *oaddr;
95556be1b9aSKonstantin Belousov 	int b, i, res;
95656be1b9aSKonstantin Belousov 	uint8_t bits;
95756be1b9aSKonstantin Belousov 
95856be1b9aSKonstantin Belousov 	if (nd >= ndu || fd_in == NULL)
95956be1b9aSKonstantin Belousov 		return (0);
96056be1b9aSKonstantin Belousov 
96156be1b9aSKonstantin Belousov 	oaddr = NULL;
96256be1b9aSKonstantin Belousov 	bits = 0; /* silence gcc */
96356be1b9aSKonstantin Belousov 	for (i = nd; i < ndu; i++) {
96456be1b9aSKonstantin Belousov 		b = i / NBBY;
96556be1b9aSKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN
96656be1b9aSKonstantin Belousov 		addr = (char *)fd_in + b;
96756be1b9aSKonstantin Belousov #else
96856be1b9aSKonstantin Belousov 		addr = (char *)fd_in;
96956be1b9aSKonstantin Belousov 		if (abi_nfdbits == NFDBITS) {
97056be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(fd_mask)) +
97156be1b9aSKonstantin Belousov 			    sizeof(fd_mask) - 1 - b % sizeof(fd_mask);
97256be1b9aSKonstantin Belousov 		} else {
97356be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(uint32_t)) +
97456be1b9aSKonstantin Belousov 			    sizeof(uint32_t) - 1 - b % sizeof(uint32_t);
97556be1b9aSKonstantin Belousov 		}
97656be1b9aSKonstantin Belousov #endif
97756be1b9aSKonstantin Belousov 		if (addr != oaddr) {
97856be1b9aSKonstantin Belousov 			res = fubyte(addr);
97956be1b9aSKonstantin Belousov 			if (res == -1)
98056be1b9aSKonstantin Belousov 				return (EFAULT);
98156be1b9aSKonstantin Belousov 			oaddr = addr;
98256be1b9aSKonstantin Belousov 			bits = res;
98356be1b9aSKonstantin Belousov 		}
98456be1b9aSKonstantin Belousov 		if ((bits & (1 << (i % NBBY))) != 0)
98556be1b9aSKonstantin Belousov 			return (EBADF);
98656be1b9aSKonstantin Belousov 	}
98756be1b9aSKonstantin Belousov 	return (0);
98856be1b9aSKonstantin Belousov }
98956be1b9aSKonstantin Belousov 
9908f19eb88SIan Dowse int
9918f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
992b55ef216SKonstantin Belousov     fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
9938f19eb88SIan Dowse {
994426da3bcSAlfred Perlstein 	struct filedesc *fdp;
995d5e4d7e1SBruce Evans 	/*
996d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
997d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
998d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
999d5e4d7e1SBruce Evans 	 * of 256.
1000d5e4d7e1SBruce Evans 	 */
1001d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
1002eb209311SAlfred Perlstein 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
1003cf5e4fe6SDavide Italiano 	struct timeval rtv;
1004cf5e4fe6SDavide Italiano 	sbintime_t asbt, precision, rsbt;
1005b55ef216SKonstantin Belousov 	u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
1006cf5e4fe6SDavide Italiano 	int error, lf, ndu;
1007df8bae1dSRodney W. Grimes 
10088f19eb88SIan Dowse 	if (nd < 0)
1009acbfbfeaSSujal Patel 		return (EINVAL);
1010426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
101156be1b9aSKonstantin Belousov 	ndu = nd;
101256be1b9aSKonstantin Belousov 	lf = fdp->fd_lastfile;
101356be1b9aSKonstantin Belousov 	if (nd > lf + 1)
101456be1b9aSKonstantin Belousov 		nd = lf + 1;
101556be1b9aSKonstantin Belousov 
101656be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits);
101756be1b9aSKonstantin Belousov 	if (error != 0)
101856be1b9aSKonstantin Belousov 		return (error);
101956be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits);
102056be1b9aSKonstantin Belousov 	if (error != 0)
102156be1b9aSKonstantin Belousov 		return (error);
102256be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits);
102356be1b9aSKonstantin Belousov 	if (error != 0)
102456be1b9aSKonstantin Belousov 		return (error);
1025b08f7993SSujal Patel 
1026d5e4d7e1SBruce Evans 	/*
1027d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
1028d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
1029d5e4d7e1SBruce Evans 	 */
10308f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
1031d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
1032b55ef216SKonstantin Belousov 	ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
1033d5e4d7e1SBruce Evans 	nbufbytes = 0;
10348f19eb88SIan Dowse 	if (fd_in != NULL)
1035d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
10368f19eb88SIan Dowse 	if (fd_ou != NULL)
1037d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
10388f19eb88SIan Dowse 	if (fd_ex != NULL)
1039d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
1040d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
1041d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
1042d5e4d7e1SBruce Evans 	else
1043a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
1044b08f7993SSujal Patel 
1045b08f7993SSujal Patel 	/*
1046d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
1047d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
1048d5e4d7e1SBruce Evans 	 * together.
1049b08f7993SSujal Patel 	 */
1050d5e4d7e1SBruce Evans 	sbp = selbits;
1051df8bae1dSRodney W. Grimes #define	getbits(name, x) \
1052d5e4d7e1SBruce Evans 	do {								\
1053841c0c7eSNathan Whitehorn 		if (name == NULL) {					\
1054d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
1055841c0c7eSNathan Whitehorn 			obits[x] = NULL;				\
1056841c0c7eSNathan Whitehorn 		} else {						\
1057d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
1058d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
1059d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
1060b55ef216SKonstantin Belousov 			error = copyin(name, ibits[x], ncpubytes);	\
1061265fc98fSSeigo Tanimura 			if (error != 0)					\
1062ace8398dSJeff Roberson 				goto done;				\
1063b55ef216SKonstantin Belousov 			bzero((char *)ibits[x] + ncpubytes,		\
1064b55ef216SKonstantin Belousov 			    ncpbytes - ncpubytes);			\
1065e04ac2feSJohn Baldwin 		}							\
1066d5e4d7e1SBruce Evans 	} while (0)
10678f19eb88SIan Dowse 	getbits(fd_in, 0);
10688f19eb88SIan Dowse 	getbits(fd_ou, 1);
10698f19eb88SIan Dowse 	getbits(fd_ex, 2);
1070df8bae1dSRodney W. Grimes #undef	getbits
1071841c0c7eSNathan Whitehorn 
1072841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
1073841c0c7eSNathan Whitehorn 	/*
1074841c0c7eSNathan Whitehorn 	 * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
1075841c0c7eSNathan Whitehorn 	 * we are running under 32-bit emulation. This should be more
1076841c0c7eSNathan Whitehorn 	 * generic.
1077841c0c7eSNathan Whitehorn 	 */
1078841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)						\
1079841c0c7eSNathan Whitehorn 	if (abi_nfdbits != NFDBITS && bits != NULL) {			\
1080841c0c7eSNathan Whitehorn 		int i;							\
1081841c0c7eSNathan Whitehorn 		for (i = 0; i < ncpbytes / sizeof *sbp; i++)		\
1082841c0c7eSNathan Whitehorn 			bits[i] = (bits[i] >> 32) | (bits[i] << 32);	\
1083841c0c7eSNathan Whitehorn 	}
1084841c0c7eSNathan Whitehorn #else
1085841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)
1086841c0c7eSNathan Whitehorn #endif
1087841c0c7eSNathan Whitehorn 
1088841c0c7eSNathan Whitehorn 	/* Make sure the bit order makes it through an ABI transition */
1089841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[0]);
1090841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[1]);
1091841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[2]);
1092841c0c7eSNathan Whitehorn 
1093d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
1094d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
1095df8bae1dSRodney W. Grimes 
1096cf5e4fe6SDavide Italiano 	precision = 0;
10978f19eb88SIan Dowse 	if (tvp != NULL) {
1098cf5e4fe6SDavide Italiano 		rtv = *tvp;
1099cf5e4fe6SDavide Italiano 		if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1100cf5e4fe6SDavide Italiano 		    rtv.tv_usec >= 1000000) {
1101df8bae1dSRodney W. Grimes 			error = EINVAL;
1102ace8398dSJeff Roberson 			goto done;
1103df8bae1dSRodney W. Grimes 		}
110421a37a71SAlexander Motin 		if (!timevalisset(&rtv))
1105980c545dSAlexander Motin 			asbt = 0;
110621a37a71SAlexander Motin 		else if (rtv.tv_sec <= INT32_MAX) {
1107cf5e4fe6SDavide Italiano 			rsbt = tvtosbt(rtv);
1108cf5e4fe6SDavide Italiano 			precision = rsbt;
1109cf5e4fe6SDavide Italiano 			precision >>= tc_precexp;
1110cf5e4fe6SDavide Italiano 			if (TIMESEL(&asbt, rsbt))
1111cf5e4fe6SDavide Italiano 				asbt += tc_tick_sbt;
11124bc38a5aSDavide Italiano 			if (asbt <= SBT_MAX - rsbt)
1113cf5e4fe6SDavide Italiano 				asbt += rsbt;
111421a37a71SAlexander Motin 			else
1115980c545dSAlexander Motin 				asbt = -1;
1116980c545dSAlexander Motin 		} else
1117980c545dSAlexander Motin 			asbt = -1;
1118cf5e4fe6SDavide Italiano 	} else
1119cf5e4fe6SDavide Italiano 		asbt = -1;
1120ace8398dSJeff Roberson 	seltdinit(td);
1121ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1122ace8398dSJeff Roberson 	for (;;) {
11238f19eb88SIan Dowse 		error = selscan(td, ibits, obits, nd);
1124ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1125ace8398dSJeff Roberson 			break;
1126cf5e4fe6SDavide Italiano 		error = seltdwait(td, asbt, precision);
1127ace8398dSJeff Roberson 		if (error)
1128ace8398dSJeff Roberson 			break;
1129ace8398dSJeff Roberson 		error = selrescan(td, ibits, obits);
1130ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1131ace8398dSJeff Roberson 			break;
113285f190e4SAlfred Perlstein 	}
1133ace8398dSJeff Roberson 	seltdclear(td);
1134265fc98fSSeigo Tanimura 
1135df8bae1dSRodney W. Grimes done:
1136df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
1137df8bae1dSRodney W. Grimes 	if (error == ERESTART)
1138df8bae1dSRodney W. Grimes 		error = EINTR;
1139df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
1140df8bae1dSRodney W. Grimes 		error = 0;
1141841c0c7eSNathan Whitehorn 
1142841c0c7eSNathan Whitehorn 	/* swizzle bit order back, if necessary */
1143841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[0]);
1144841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[1]);
1145841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[2]);
1146841c0c7eSNathan Whitehorn #undef swizzle_fdset
1147841c0c7eSNathan Whitehorn 
1148df8bae1dSRodney W. Grimes #define	putbits(name, x) \
1149b55ef216SKonstantin Belousov 	if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
1150df8bae1dSRodney W. Grimes 		error = error2;
1151df8bae1dSRodney W. Grimes 	if (error == 0) {
1152df8bae1dSRodney W. Grimes 		int error2;
1153df8bae1dSRodney W. Grimes 
11548f19eb88SIan Dowse 		putbits(fd_in, 0);
11558f19eb88SIan Dowse 		putbits(fd_ou, 1);
11568f19eb88SIan Dowse 		putbits(fd_ex, 2);
1157df8bae1dSRodney W. Grimes #undef putbits
1158df8bae1dSRodney W. Grimes 	}
1159d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
1160d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
1161ad2edad9SMatthew Dillon 
1162df8bae1dSRodney W. Grimes 	return (error);
1163df8bae1dSRodney W. Grimes }
116411b763dfSJeff Roberson /*
116511b763dfSJeff Roberson  * Convert a select bit set to poll flags.
1166748b9df6SJeff Roberson  *
116711b763dfSJeff Roberson  * The backend always returns POLLHUP/POLLERR if appropriate and we
116811b763dfSJeff Roberson  * return this as a set bit in any set.
116911b763dfSJeff Roberson  */
117011b763dfSJeff Roberson static int select_flags[3] = {
117111b763dfSJeff Roberson     POLLRDNORM | POLLHUP | POLLERR,
117211b763dfSJeff Roberson     POLLWRNORM | POLLHUP | POLLERR,
117361e53a38SKonstantin Belousov     POLLRDBAND | POLLERR
117411b763dfSJeff Roberson };
117511b763dfSJeff Roberson 
117611b763dfSJeff Roberson /*
117711b763dfSJeff Roberson  * Compute the fo_poll flags required for a fd given by the index and
117811b763dfSJeff Roberson  * bit position in the fd_mask array.
117911b763dfSJeff Roberson  */
118011b763dfSJeff Roberson static __inline int
118160b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit)
118211b763dfSJeff Roberson {
118311b763dfSJeff Roberson 	int flags;
118411b763dfSJeff Roberson 	int msk;
118511b763dfSJeff Roberson 
118611b763dfSJeff Roberson 	flags = 0;
118711b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
118811b763dfSJeff Roberson 		if (ibits[msk] == NULL)
118911b763dfSJeff Roberson 			continue;
119060b7f468SStephane E. Potvin 		if ((ibits[msk][idx] & bit) == 0)
119111b763dfSJeff Roberson 			continue;
119211b763dfSJeff Roberson 		flags |= select_flags[msk];
119311b763dfSJeff Roberson 	}
119411b763dfSJeff Roberson 	return (flags);
119511b763dfSJeff Roberson }
119611b763dfSJeff Roberson 
119711b763dfSJeff Roberson /*
119811b763dfSJeff Roberson  * Set the appropriate output bits given a mask of fired events and the
119911b763dfSJeff Roberson  * input bits originally requested.
120011b763dfSJeff Roberson  */
120111b763dfSJeff Roberson static __inline int
120211b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
120311b763dfSJeff Roberson {
120411b763dfSJeff Roberson 	int msk;
120511b763dfSJeff Roberson 	int n;
120611b763dfSJeff Roberson 
120711b763dfSJeff Roberson 	n = 0;
120811b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
120911b763dfSJeff Roberson 		if ((events & select_flags[msk]) == 0)
121011b763dfSJeff Roberson 			continue;
121111b763dfSJeff Roberson 		if (ibits[msk] == NULL)
121211b763dfSJeff Roberson 			continue;
121311b763dfSJeff Roberson 		if ((ibits[msk][idx] & bit) == 0)
121411b763dfSJeff Roberson 			continue;
121511b763dfSJeff Roberson 		/*
121611b763dfSJeff Roberson 		 * XXX Check for a duplicate set.  This can occur because a
121711b763dfSJeff Roberson 		 * socket calls selrecord() twice for each poll() call
121811b763dfSJeff Roberson 		 * resulting in two selfds per real fd.  selrescan() will
121911b763dfSJeff Roberson 		 * call selsetbits twice as a result.
122011b763dfSJeff Roberson 		 */
122111b763dfSJeff Roberson 		if ((obits[msk][idx] & bit) != 0)
122211b763dfSJeff Roberson 			continue;
122311b763dfSJeff Roberson 		obits[msk][idx] |= bit;
122411b763dfSJeff Roberson 		n++;
122511b763dfSJeff Roberson 	}
122611b763dfSJeff Roberson 
122711b763dfSJeff Roberson 	return (n);
122811b763dfSJeff Roberson }
1229df8bae1dSRodney W. Grimes 
1230a9d2f8d8SRobert Watson static __inline int
1231a9d2f8d8SRobert Watson getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp)
1232a9d2f8d8SRobert Watson {
1233a9d2f8d8SRobert Watson 
1234*cbd92ce6SMatt Macy 	return (fget_unlocked(fdp, fd, &cap_event_rights, fpp, NULL));
1235a9d2f8d8SRobert Watson }
1236a9d2f8d8SRobert Watson 
1237ace8398dSJeff Roberson /*
1238ace8398dSJeff Roberson  * Traverse the list of fds attached to this thread's seltd and check for
1239ace8398dSJeff Roberson  * completion.
1240ace8398dSJeff Roberson  */
1241ace8398dSJeff Roberson static int
1242ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
1243ace8398dSJeff Roberson {
124411b763dfSJeff Roberson 	struct filedesc *fdp;
124511b763dfSJeff Roberson 	struct selinfo *si;
1246ace8398dSJeff Roberson 	struct seltd *stp;
1247ace8398dSJeff Roberson 	struct selfd *sfp;
1248ace8398dSJeff Roberson 	struct selfd *sfn;
1249ace8398dSJeff Roberson 	struct file *fp;
12509cdacff1SJeff Roberson 	fd_mask bit;
12519cdacff1SJeff Roberson 	int fd, ev, n, idx;
1252a9d2f8d8SRobert Watson 	int error;
1253ace8398dSJeff Roberson 
125411b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
1255ace8398dSJeff Roberson 	stp = td->td_sel;
125611b763dfSJeff Roberson 	n = 0;
1257ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1258ace8398dSJeff Roberson 		fd = (int)(uintptr_t)sfp->sf_cookie;
1259ace8398dSJeff Roberson 		si = sfp->sf_si;
1260ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1261ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1262ace8398dSJeff Roberson 		if (si != NULL)
1263ace8398dSJeff Roberson 			continue;
1264a9d2f8d8SRobert Watson 		error = getselfd_cap(fdp, fd, &fp);
1265a9d2f8d8SRobert Watson 		if (error)
1266a9d2f8d8SRobert Watson 			return (error);
126711b763dfSJeff Roberson 		idx = fd / NFDBITS;
12689cdacff1SJeff Roberson 		bit = (fd_mask)1 << (fd % NFDBITS);
126911b763dfSJeff Roberson 		ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
1270bf422e5fSJeff Roberson 		fdrop(fp, td);
127111b763dfSJeff Roberson 		if (ev != 0)
127211b763dfSJeff Roberson 			n += selsetbits(ibits, obits, idx, bit, ev);
1273ace8398dSJeff Roberson 	}
1274ace8398dSJeff Roberson 	stp->st_flags = 0;
1275ace8398dSJeff Roberson 	td->td_retval[0] = n;
1276ace8398dSJeff Roberson 	return (0);
1277ace8398dSJeff Roberson }
1278ace8398dSJeff Roberson 
1279ace8398dSJeff Roberson /*
1280ace8398dSJeff Roberson  * Perform the initial filedescriptor scan and register ourselves with
1281ace8398dSJeff Roberson  * each selinfo.
1282ace8398dSJeff Roberson  */
1283265fc98fSSeigo Tanimura static int
1284b40ce416SJulian Elischer selscan(td, ibits, obits, nfd)
1285b40ce416SJulian Elischer 	struct thread *td;
1286b08f7993SSujal Patel 	fd_mask **ibits, **obits;
1287cb226aaaSPoul-Henning Kamp 	int nfd;
1288df8bae1dSRodney W. Grimes {
128911b763dfSJeff Roberson 	struct filedesc *fdp;
1290df8bae1dSRodney W. Grimes 	struct file *fp;
12919cdacff1SJeff Roberson 	fd_mask bit;
129211b763dfSJeff Roberson 	int ev, flags, end, fd;
12939cdacff1SJeff Roberson 	int n, idx;
1294a9d2f8d8SRobert Watson 	int error;
1295df8bae1dSRodney W. Grimes 
129611b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
129711b763dfSJeff Roberson 	n = 0;
12989cdacff1SJeff Roberson 	for (idx = 0, fd = 0; fd < nfd; idx++) {
129911b763dfSJeff Roberson 		end = imin(fd + NFDBITS, nfd);
130011b763dfSJeff Roberson 		for (bit = 1; fd < end; bit <<= 1, fd++) {
130111b763dfSJeff Roberson 			/* Compute the list of events we're interested in. */
130211b763dfSJeff Roberson 			flags = selflags(ibits, idx, bit);
130311b763dfSJeff Roberson 			if (flags == 0)
1304f082218cSPeter Wemm 				continue;
1305a9d2f8d8SRobert Watson 			error = getselfd_cap(fdp, fd, &fp);
1306a9d2f8d8SRobert Watson 			if (error)
1307a9d2f8d8SRobert Watson 				return (error);
1308ace8398dSJeff Roberson 			selfdalloc(td, (void *)(uintptr_t)fd);
130911b763dfSJeff Roberson 			ev = fo_poll(fp, flags, td->td_ucred, td);
1310bf422e5fSJeff Roberson 			fdrop(fp, td);
131111b763dfSJeff Roberson 			if (ev != 0)
131211b763dfSJeff Roberson 				n += selsetbits(ibits, obits, idx, bit, ev);
1313df8bae1dSRodney W. Grimes 		}
1314df8bae1dSRodney W. Grimes 	}
131511b763dfSJeff Roberson 
1316b40ce416SJulian Elischer 	td->td_retval[0] = n;
1317df8bae1dSRodney W. Grimes 	return (0);
1318df8bae1dSRodney W. Grimes }
1319df8bae1dSRodney W. Grimes 
132042d11757SPeter Wemm int
1321186d9c34SDmitry Chagin sys_poll(struct thread *td, struct poll_args *uap)
1322186d9c34SDmitry Chagin {
1323186d9c34SDmitry Chagin 	struct timespec ts, *tsp;
1324186d9c34SDmitry Chagin 
1325186d9c34SDmitry Chagin 	if (uap->timeout != INFTIM) {
1326186d9c34SDmitry Chagin 		if (uap->timeout < 0)
1327186d9c34SDmitry Chagin 			return (EINVAL);
1328186d9c34SDmitry Chagin 		ts.tv_sec = uap->timeout / 1000;
1329186d9c34SDmitry Chagin 		ts.tv_nsec = (uap->timeout % 1000) * 1000000;
1330186d9c34SDmitry Chagin 		tsp = &ts;
1331186d9c34SDmitry Chagin 	} else
1332186d9c34SDmitry Chagin 		tsp = NULL;
1333186d9c34SDmitry Chagin 
1334186d9c34SDmitry Chagin 	return (kern_poll(td, uap->fds, uap->nfds, tsp, NULL));
1335186d9c34SDmitry Chagin }
1336186d9c34SDmitry Chagin 
1337186d9c34SDmitry Chagin int
1338186d9c34SDmitry Chagin kern_poll(struct thread *td, struct pollfd *fds, u_int nfds,
1339186d9c34SDmitry Chagin     struct timespec *tsp, sigset_t *uset)
134042d11757SPeter Wemm {
13412580f4e5SAndre Oppermann 	struct pollfd *bits;
13422580f4e5SAndre Oppermann 	struct pollfd smallbits[32];
1343186d9c34SDmitry Chagin 	sbintime_t sbt, precision, tmp;
1344186d9c34SDmitry Chagin 	time_t over;
1345186d9c34SDmitry Chagin 	struct timespec ts;
1346cf5e4fe6SDavide Italiano 	int error;
134742d11757SPeter Wemm 	size_t ni;
134842d11757SPeter Wemm 
1349186d9c34SDmitry Chagin 	precision = 0;
1350186d9c34SDmitry Chagin 	if (tsp != NULL) {
1351186d9c34SDmitry Chagin 		if (tsp->tv_sec < 0)
1352186d9c34SDmitry Chagin 			return (EINVAL);
1353186d9c34SDmitry Chagin 		if (tsp->tv_nsec < 0 || tsp->tv_nsec >= 1000000000)
1354186d9c34SDmitry Chagin 			return (EINVAL);
1355186d9c34SDmitry Chagin 		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1356186d9c34SDmitry Chagin 			sbt = 0;
1357186d9c34SDmitry Chagin 		else {
1358186d9c34SDmitry Chagin 			ts = *tsp;
1359186d9c34SDmitry Chagin 			if (ts.tv_sec > INT32_MAX / 2) {
1360186d9c34SDmitry Chagin 				over = ts.tv_sec - INT32_MAX / 2;
1361186d9c34SDmitry Chagin 				ts.tv_sec -= over;
1362186d9c34SDmitry Chagin 			} else
1363186d9c34SDmitry Chagin 				over = 0;
1364186d9c34SDmitry Chagin 			tmp = tstosbt(ts);
1365186d9c34SDmitry Chagin 			precision = tmp;
1366186d9c34SDmitry Chagin 			precision >>= tc_precexp;
1367186d9c34SDmitry Chagin 			if (TIMESEL(&sbt, tmp))
1368186d9c34SDmitry Chagin 				sbt += tc_tick_sbt;
1369186d9c34SDmitry Chagin 			sbt += tmp;
1370186d9c34SDmitry Chagin 		}
1371186d9c34SDmitry Chagin 	} else
1372186d9c34SDmitry Chagin 		sbt = -1;
1373186d9c34SDmitry Chagin 
1374374ae2a3SJeff Roberson 	if (nfds > maxfilesperproc && nfds > FD_SETSIZE)
1375ace8398dSJeff Roberson 		return (EINVAL);
137689b71647SPeter Wemm 	ni = nfds * sizeof(struct pollfd);
137742d11757SPeter Wemm 	if (ni > sizeof(smallbits))
1378a163d034SWarner Losh 		bits = malloc(ni, M_TEMP, M_WAITOK);
137942d11757SPeter Wemm 	else
138042d11757SPeter Wemm 		bits = smallbits;
1381186d9c34SDmitry Chagin 	error = copyin(fds, bits, ni);
138242d11757SPeter Wemm 	if (error)
1383ace8398dSJeff Roberson 		goto done;
1384186d9c34SDmitry Chagin 
1385186d9c34SDmitry Chagin 	if (uset != NULL) {
1386186d9c34SDmitry Chagin 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
1387186d9c34SDmitry Chagin 		    &td->td_oldsigmask, 0);
1388186d9c34SDmitry Chagin 		if (error)
1389ace8398dSJeff Roberson 			goto done;
1390186d9c34SDmitry Chagin 		td->td_pflags |= TDP_OLDMASK;
1391186d9c34SDmitry Chagin 		/*
1392186d9c34SDmitry Chagin 		 * Make sure that ast() is called on return to
1393186d9c34SDmitry Chagin 		 * usermode and TDP_OLDMASK is cleared, restoring old
1394186d9c34SDmitry Chagin 		 * sigmask.
1395186d9c34SDmitry Chagin 		 */
1396186d9c34SDmitry Chagin 		thread_lock(td);
1397186d9c34SDmitry Chagin 		td->td_flags |= TDF_ASTPENDING;
1398186d9c34SDmitry Chagin 		thread_unlock(td);
139942d11757SPeter Wemm 	}
1400186d9c34SDmitry Chagin 
1401ace8398dSJeff Roberson 	seltdinit(td);
1402ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1403ace8398dSJeff Roberson 	for (;;) {
14042580f4e5SAndre Oppermann 		error = pollscan(td, bits, nfds);
1405ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1406ace8398dSJeff Roberson 			break;
1407186d9c34SDmitry Chagin 		error = seltdwait(td, sbt, precision);
1408ace8398dSJeff Roberson 		if (error)
1409ace8398dSJeff Roberson 			break;
1410ace8398dSJeff Roberson 		error = pollrescan(td);
1411ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1412ace8398dSJeff Roberson 			break;
141385f190e4SAlfred Perlstein 	}
1414ace8398dSJeff Roberson 	seltdclear(td);
1415265fc98fSSeigo Tanimura 
141642d11757SPeter Wemm done:
141742d11757SPeter Wemm 	/* poll is not restarted after signals... */
141842d11757SPeter Wemm 	if (error == ERESTART)
141942d11757SPeter Wemm 		error = EINTR;
142042d11757SPeter Wemm 	if (error == EWOULDBLOCK)
142142d11757SPeter Wemm 		error = 0;
142242d11757SPeter Wemm 	if (error == 0) {
1423186d9c34SDmitry Chagin 		error = pollout(td, bits, fds, nfds);
142442d11757SPeter Wemm 		if (error)
142542d11757SPeter Wemm 			goto out;
142642d11757SPeter Wemm 	}
142742d11757SPeter Wemm out:
142842d11757SPeter Wemm 	if (ni > sizeof(smallbits))
142942d11757SPeter Wemm 		free(bits, M_TEMP);
143042d11757SPeter Wemm 	return (error);
143142d11757SPeter Wemm }
143242d11757SPeter Wemm 
1433186d9c34SDmitry Chagin int
1434186d9c34SDmitry Chagin sys_ppoll(struct thread *td, struct ppoll_args *uap)
1435186d9c34SDmitry Chagin {
1436186d9c34SDmitry Chagin 	struct timespec ts, *tsp;
1437186d9c34SDmitry Chagin 	sigset_t set, *ssp;
1438186d9c34SDmitry Chagin 	int error;
1439186d9c34SDmitry Chagin 
1440186d9c34SDmitry Chagin 	if (uap->ts != NULL) {
1441186d9c34SDmitry Chagin 		error = copyin(uap->ts, &ts, sizeof(ts));
1442186d9c34SDmitry Chagin 		if (error)
1443186d9c34SDmitry Chagin 			return (error);
1444186d9c34SDmitry Chagin 		tsp = &ts;
1445186d9c34SDmitry Chagin 	} else
1446186d9c34SDmitry Chagin 		tsp = NULL;
1447186d9c34SDmitry Chagin 	if (uap->set != NULL) {
1448186d9c34SDmitry Chagin 		error = copyin(uap->set, &set, sizeof(set));
1449186d9c34SDmitry Chagin 		if (error)
1450186d9c34SDmitry Chagin 			return (error);
1451186d9c34SDmitry Chagin 		ssp = &set;
1452186d9c34SDmitry Chagin 	} else
1453186d9c34SDmitry Chagin 		ssp = NULL;
1454186d9c34SDmitry Chagin 	/*
1455186d9c34SDmitry Chagin 	 * fds is still a pointer to user space. kern_poll() will
1456186d9c34SDmitry Chagin 	 * take care of copyin that array to the kernel space.
1457186d9c34SDmitry Chagin 	 */
1458186d9c34SDmitry Chagin 
1459186d9c34SDmitry Chagin 	return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
1460186d9c34SDmitry Chagin }
1461186d9c34SDmitry Chagin 
146242d11757SPeter Wemm static int
1463ace8398dSJeff Roberson pollrescan(struct thread *td)
1464ace8398dSJeff Roberson {
1465ace8398dSJeff Roberson 	struct seltd *stp;
1466ace8398dSJeff Roberson 	struct selfd *sfp;
1467ace8398dSJeff Roberson 	struct selfd *sfn;
1468ace8398dSJeff Roberson 	struct selinfo *si;
1469ace8398dSJeff Roberson 	struct filedesc *fdp;
1470ace8398dSJeff Roberson 	struct file *fp;
1471ace8398dSJeff Roberson 	struct pollfd *fd;
1472ace8398dSJeff Roberson 	int n;
1473ace8398dSJeff Roberson 
1474ace8398dSJeff Roberson 	n = 0;
1475ace8398dSJeff Roberson 	fdp = td->td_proc->p_fd;
1476ace8398dSJeff Roberson 	stp = td->td_sel;
1477ace8398dSJeff Roberson 	FILEDESC_SLOCK(fdp);
1478ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1479ace8398dSJeff Roberson 		fd = (struct pollfd *)sfp->sf_cookie;
1480ace8398dSJeff Roberson 		si = sfp->sf_si;
1481ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1482ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1483ace8398dSJeff Roberson 		if (si != NULL)
1484ace8398dSJeff Roberson 			continue;
14852609222aSPawel Jakub Dawidek 		fp = fdp->fd_ofiles[fd->fd].fde_file;
1486d6f72489SJonathan Anderson #ifdef CAPABILITIES
14872609222aSPawel Jakub Dawidek 		if (fp == NULL ||
1488*cbd92ce6SMatt Macy 		    cap_check(cap_rights(fdp, fd->fd), &cap_event_rights) != 0)
1489d6f72489SJonathan Anderson #else
14902609222aSPawel Jakub Dawidek 		if (fp == NULL)
1491d6f72489SJonathan Anderson #endif
14922609222aSPawel Jakub Dawidek 		{
1493ace8398dSJeff Roberson 			fd->revents = POLLNVAL;
1494ace8398dSJeff Roberson 			n++;
1495ace8398dSJeff Roberson 			continue;
1496ace8398dSJeff Roberson 		}
1497d6f72489SJonathan Anderson 
1498ace8398dSJeff Roberson 		/*
1499ace8398dSJeff Roberson 		 * Note: backend also returns POLLHUP and
1500ace8398dSJeff Roberson 		 * POLLERR if appropriate.
1501ace8398dSJeff Roberson 		 */
1502ace8398dSJeff Roberson 		fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1503ace8398dSJeff Roberson 		if (fd->revents != 0)
1504ace8398dSJeff Roberson 			n++;
1505ace8398dSJeff Roberson 	}
1506ace8398dSJeff Roberson 	FILEDESC_SUNLOCK(fdp);
1507ace8398dSJeff Roberson 	stp->st_flags = 0;
1508ace8398dSJeff Roberson 	td->td_retval[0] = n;
1509ace8398dSJeff Roberson 	return (0);
1510ace8398dSJeff Roberson }
1511ace8398dSJeff Roberson 
1512ace8398dSJeff Roberson 
1513ace8398dSJeff Roberson static int
15146d8feddaSKonstantin Belousov pollout(td, fds, ufds, nfd)
15156d8feddaSKonstantin Belousov 	struct thread *td;
1516ae81968fSRobert Watson 	struct pollfd *fds;
1517ae81968fSRobert Watson 	struct pollfd *ufds;
1518ae81968fSRobert Watson 	u_int nfd;
1519ae81968fSRobert Watson {
1520ae81968fSRobert Watson 	int error = 0;
1521ae81968fSRobert Watson 	u_int i = 0;
15226d8feddaSKonstantin Belousov 	u_int n = 0;
1523ae81968fSRobert Watson 
1524ae81968fSRobert Watson 	for (i = 0; i < nfd; i++) {
1525ae81968fSRobert Watson 		error = copyout(&fds->revents, &ufds->revents,
1526ae81968fSRobert Watson 		    sizeof(ufds->revents));
1527ae81968fSRobert Watson 		if (error)
1528ae81968fSRobert Watson 			return (error);
15296d8feddaSKonstantin Belousov 		if (fds->revents != 0)
15306d8feddaSKonstantin Belousov 			n++;
1531ae81968fSRobert Watson 		fds++;
1532ae81968fSRobert Watson 		ufds++;
1533ae81968fSRobert Watson 	}
15346d8feddaSKonstantin Belousov 	td->td_retval[0] = n;
1535ae81968fSRobert Watson 	return (0);
1536ae81968fSRobert Watson }
1537ae81968fSRobert Watson 
1538ae81968fSRobert Watson static int
1539b40ce416SJulian Elischer pollscan(td, fds, nfd)
1540b40ce416SJulian Elischer 	struct thread *td;
154142d11757SPeter Wemm 	struct pollfd *fds;
1542ea0237edSJonathan Lemon 	u_int nfd;
154342d11757SPeter Wemm {
1544ace8398dSJeff Roberson 	struct filedesc *fdp = td->td_proc->p_fd;
154542d11757SPeter Wemm 	struct file *fp;
15462609222aSPawel Jakub Dawidek 	int i, n = 0;
154742d11757SPeter Wemm 
15485e3f7694SRobert Watson 	FILEDESC_SLOCK(fdp);
1549eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
1550adf87ab0SMateusz Guzik 		if (fds->fd > fdp->fd_lastfile) {
155142d11757SPeter Wemm 			fds->revents = POLLNVAL;
155242d11757SPeter Wemm 			n++;
1553337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
1554337c9691SJordan K. Hubbard 			fds->revents = 0;
155542d11757SPeter Wemm 		} else {
15562609222aSPawel Jakub Dawidek 			fp = fdp->fd_ofiles[fds->fd].fde_file;
1557d6f72489SJonathan Anderson #ifdef CAPABILITIES
15582609222aSPawel Jakub Dawidek 			if (fp == NULL ||
1559*cbd92ce6SMatt Macy 			    cap_check(cap_rights(fdp, fds->fd), &cap_event_rights) != 0)
1560d6f72489SJonathan Anderson #else
15612609222aSPawel Jakub Dawidek 			if (fp == NULL)
1562d6f72489SJonathan Anderson #endif
15632609222aSPawel Jakub Dawidek 			{
156442d11757SPeter Wemm 				fds->revents = POLLNVAL;
156542d11757SPeter Wemm 				n++;
156642d11757SPeter Wemm 			} else {
15672087c896SBruce Evans 				/*
15682087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
15692087c896SBruce Evans 				 * POLLERR if appropriate.
15702087c896SBruce Evans 				 */
1571ace8398dSJeff Roberson 				selfdalloc(td, fds);
157213ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
1573ea6027a8SRobert Watson 				    td->td_ucred, td);
1574f2159cc7SKonstantin Belousov 				/*
1575f2159cc7SKonstantin Belousov 				 * POSIX requires POLLOUT to be never
1576f2159cc7SKonstantin Belousov 				 * set simultaneously with POLLHUP.
1577f2159cc7SKonstantin Belousov 				 */
1578f2159cc7SKonstantin Belousov 				if ((fds->revents & POLLHUP) != 0)
1579f2159cc7SKonstantin Belousov 					fds->revents &= ~POLLOUT;
1580f2159cc7SKonstantin Belousov 
158142d11757SPeter Wemm 				if (fds->revents != 0)
158242d11757SPeter Wemm 					n++;
158342d11757SPeter Wemm 			}
158442d11757SPeter Wemm 		}
158542d11757SPeter Wemm 	}
15865e3f7694SRobert Watson 	FILEDESC_SUNLOCK(fdp);
1587b40ce416SJulian Elischer 	td->td_retval[0] = n;
158842d11757SPeter Wemm 	return (0);
158942d11757SPeter Wemm }
159042d11757SPeter Wemm 
159142d11757SPeter Wemm /*
1592237abf0cSDavide Italiano  * XXX This was created specifically to support netncp and netsmb.  This
1593237abf0cSDavide Italiano  * allows the caller to specify a socket to wait for events on.  It returns
1594237abf0cSDavide Italiano  * 0 if any events matched and an error otherwise.  There is no way to
1595237abf0cSDavide Italiano  * determine which events fired.
1596237abf0cSDavide Italiano  */
1597237abf0cSDavide Italiano int
1598237abf0cSDavide Italiano selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td)
1599237abf0cSDavide Italiano {
1600237abf0cSDavide Italiano 	struct timeval rtv;
1601237abf0cSDavide Italiano 	sbintime_t asbt, precision, rsbt;
1602237abf0cSDavide Italiano 	int error;
1603237abf0cSDavide Italiano 
160476665df9SPeter Wemm 	precision = 0;	/* stupid gcc! */
1605237abf0cSDavide Italiano 	if (tvp != NULL) {
1606237abf0cSDavide Italiano 		rtv = *tvp;
1607237abf0cSDavide Italiano 		if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1608237abf0cSDavide Italiano 		    rtv.tv_usec >= 1000000)
1609237abf0cSDavide Italiano 			return (EINVAL);
1610237abf0cSDavide Italiano 		if (!timevalisset(&rtv))
1611237abf0cSDavide Italiano 			asbt = 0;
1612237abf0cSDavide Italiano 		else if (rtv.tv_sec <= INT32_MAX) {
1613237abf0cSDavide Italiano 			rsbt = tvtosbt(rtv);
1614237abf0cSDavide Italiano 			precision = rsbt;
1615237abf0cSDavide Italiano 			precision >>= tc_precexp;
1616237abf0cSDavide Italiano 			if (TIMESEL(&asbt, rsbt))
1617237abf0cSDavide Italiano 				asbt += tc_tick_sbt;
16184bc38a5aSDavide Italiano 			if (asbt <= SBT_MAX - rsbt)
1619237abf0cSDavide Italiano 				asbt += rsbt;
1620237abf0cSDavide Italiano 			else
1621237abf0cSDavide Italiano 				asbt = -1;
1622237abf0cSDavide Italiano 		} else
1623237abf0cSDavide Italiano 			asbt = -1;
1624237abf0cSDavide Italiano 	} else
1625237abf0cSDavide Italiano 		asbt = -1;
1626237abf0cSDavide Italiano 	seltdinit(td);
1627237abf0cSDavide Italiano 	/*
1628237abf0cSDavide Italiano 	 * Iterate until the timeout expires or the socket becomes ready.
1629237abf0cSDavide Italiano 	 */
1630237abf0cSDavide Italiano 	for (;;) {
1631237abf0cSDavide Italiano 		selfdalloc(td, NULL);
1632237abf0cSDavide Italiano 		error = sopoll(so, events, NULL, td);
1633237abf0cSDavide Italiano 		/* error here is actually the ready events. */
1634237abf0cSDavide Italiano 		if (error)
1635237abf0cSDavide Italiano 			return (0);
1636237abf0cSDavide Italiano 		error = seltdwait(td, asbt, precision);
1637237abf0cSDavide Italiano 		if (error)
1638237abf0cSDavide Italiano 			break;
1639237abf0cSDavide Italiano 	}
1640237abf0cSDavide Italiano 	seltdclear(td);
1641237abf0cSDavide Italiano 	/* XXX Duplicates ncp/smb behavior. */
1642237abf0cSDavide Italiano 	if (error == ERESTART)
1643237abf0cSDavide Italiano 		error = 0;
1644237abf0cSDavide Italiano 	return (error);
1645237abf0cSDavide Italiano }
1646237abf0cSDavide Italiano 
1647237abf0cSDavide Italiano /*
1648ace8398dSJeff Roberson  * Preallocate two selfds associated with 'cookie'.  Some fo_poll routines
1649ace8398dSJeff Roberson  * have two select sets, one for read and another for write.
1650ace8398dSJeff Roberson  */
1651ace8398dSJeff Roberson static void
1652ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie)
1653ace8398dSJeff Roberson {
1654ace8398dSJeff Roberson 	struct seltd *stp;
1655ace8398dSJeff Roberson 
1656ace8398dSJeff Roberson 	stp = td->td_sel;
1657ace8398dSJeff Roberson 	if (stp->st_free1 == NULL)
1658ace8398dSJeff Roberson 		stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1659ace8398dSJeff Roberson 	stp->st_free1->sf_td = stp;
1660ace8398dSJeff Roberson 	stp->st_free1->sf_cookie = cookie;
1661ace8398dSJeff Roberson 	if (stp->st_free2 == NULL)
1662ace8398dSJeff Roberson 		stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1663ace8398dSJeff Roberson 	stp->st_free2->sf_td = stp;
1664ace8398dSJeff Roberson 	stp->st_free2->sf_cookie = cookie;
1665ace8398dSJeff Roberson }
1666ace8398dSJeff Roberson 
1667ace8398dSJeff Roberson static void
1668ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp)
1669ace8398dSJeff Roberson {
1670ace8398dSJeff Roberson 	STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
167173f2e5f7SMateusz Guzik 	if (sfp->sf_si != NULL) {
1672ace8398dSJeff Roberson 		mtx_lock(sfp->sf_mtx);
1673fcb5b3a4SKonstantin Belousov 		if (sfp->sf_si != NULL) {
1674ace8398dSJeff Roberson 			TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1675fcb5b3a4SKonstantin Belousov 			refcount_release(&sfp->sf_refs);
1676fcb5b3a4SKonstantin Belousov 		}
1677ace8398dSJeff Roberson 		mtx_unlock(sfp->sf_mtx);
167873f2e5f7SMateusz Guzik 	}
1679fcb5b3a4SKonstantin Belousov 	if (refcount_release(&sfp->sf_refs))
1680ace8398dSJeff Roberson 		uma_zfree(selfd_zone, sfp);
168185f190e4SAlfred Perlstein }
168285f190e4SAlfred Perlstein 
16836aba400aSAttilio Rao /* Drain the waiters tied to all the selfd belonging the specified selinfo. */
16846aba400aSAttilio Rao void
16856aba400aSAttilio Rao seldrain(sip)
16866aba400aSAttilio Rao         struct selinfo *sip;
16876aba400aSAttilio Rao {
16886aba400aSAttilio Rao 
16896aba400aSAttilio Rao 	/*
16906aba400aSAttilio Rao 	 * This feature is already provided by doselwakeup(), thus it is
16916aba400aSAttilio Rao 	 * enough to go for it.
16926aba400aSAttilio Rao 	 * Eventually, the context, should take care to avoid races
16936aba400aSAttilio Rao 	 * between thread calling select()/poll() and file descriptor
16946aba400aSAttilio Rao 	 * detaching, but, again, the races are just the same as
16956aba400aSAttilio Rao 	 * selwakeup().
16966aba400aSAttilio Rao 	 */
16976aba400aSAttilio Rao         doselwakeup(sip, -1);
16986aba400aSAttilio Rao }
16996aba400aSAttilio Rao 
1700df8bae1dSRodney W. Grimes /*
1701df8bae1dSRodney W. Grimes  * Record a select request.
1702df8bae1dSRodney W. Grimes  */
1703df8bae1dSRodney W. Grimes void
1704df8bae1dSRodney W. Grimes selrecord(selector, sip)
1705b40ce416SJulian Elischer 	struct thread *selector;
1706df8bae1dSRodney W. Grimes 	struct selinfo *sip;
1707df8bae1dSRodney W. Grimes {
1708ace8398dSJeff Roberson 	struct selfd *sfp;
1709ace8398dSJeff Roberson 	struct seltd *stp;
1710ace8398dSJeff Roberson 	struct mtx *mtxp;
1711df8bae1dSRodney W. Grimes 
1712ace8398dSJeff Roberson 	stp = selector->td_sel;
171385f190e4SAlfred Perlstein 	/*
1714ace8398dSJeff Roberson 	 * Don't record when doing a rescan.
171585f190e4SAlfred Perlstein 	 */
1716ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_RESCAN)
1717ace8398dSJeff Roberson 		return;
1718ace8398dSJeff Roberson 	/*
1719ace8398dSJeff Roberson 	 * Grab one of the preallocated descriptors.
1720ace8398dSJeff Roberson 	 */
1721ace8398dSJeff Roberson 	sfp = NULL;
1722ace8398dSJeff Roberson 	if ((sfp = stp->st_free1) != NULL)
1723ace8398dSJeff Roberson 		stp->st_free1 = NULL;
1724ace8398dSJeff Roberson 	else if ((sfp = stp->st_free2) != NULL)
1725ace8398dSJeff Roberson 		stp->st_free2 = NULL;
1726ace8398dSJeff Roberson 	else
1727ace8398dSJeff Roberson 		panic("selrecord: No free selfd on selq");
17282141453eSJeff Roberson 	mtxp = sip->si_mtx;
17292141453eSJeff Roberson 	if (mtxp == NULL)
17302141453eSJeff Roberson 		mtxp = mtx_pool_find(mtxpool_select, sip);
1731ace8398dSJeff Roberson 	/*
1732ace8398dSJeff Roberson 	 * Initialize the sfp and queue it in the thread.
1733ace8398dSJeff Roberson 	 */
1734ace8398dSJeff Roberson 	sfp->sf_si = sip;
1735ace8398dSJeff Roberson 	sfp->sf_mtx = mtxp;
1736fcb5b3a4SKonstantin Belousov 	refcount_init(&sfp->sf_refs, 2);
1737ace8398dSJeff Roberson 	STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1738ace8398dSJeff Roberson 	/*
1739ace8398dSJeff Roberson 	 * Now that we've locked the sip, check for initialization.
1740ace8398dSJeff Roberson 	 */
1741ace8398dSJeff Roberson 	mtx_lock(mtxp);
1742ace8398dSJeff Roberson 	if (sip->si_mtx == NULL) {
1743ace8398dSJeff Roberson 		sip->si_mtx = mtxp;
1744ace8398dSJeff Roberson 		TAILQ_INIT(&sip->si_tdlist);
174585f190e4SAlfred Perlstein 	}
1746ace8398dSJeff Roberson 	/*
1747ace8398dSJeff Roberson 	 * Add this thread to the list of selfds listening on this selinfo.
1748ace8398dSJeff Roberson 	 */
1749ace8398dSJeff Roberson 	TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1750ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1751df8bae1dSRodney W. Grimes }
1752df8bae1dSRodney W. Grimes 
1753512824f8SSeigo Tanimura /* Wake up a selecting thread. */
1754df8bae1dSRodney W. Grimes void
1755df8bae1dSRodney W. Grimes selwakeup(sip)
175685f190e4SAlfred Perlstein 	struct selinfo *sip;
1757df8bae1dSRodney W. Grimes {
1758512824f8SSeigo Tanimura 	doselwakeup(sip, -1);
1759512824f8SSeigo Tanimura }
1760512824f8SSeigo Tanimura 
1761512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */
1762512824f8SSeigo Tanimura void
1763512824f8SSeigo Tanimura selwakeuppri(sip, pri)
1764512824f8SSeigo Tanimura 	struct selinfo *sip;
1765512824f8SSeigo Tanimura 	int pri;
1766512824f8SSeigo Tanimura {
1767512824f8SSeigo Tanimura 	doselwakeup(sip, pri);
1768512824f8SSeigo Tanimura }
1769512824f8SSeigo Tanimura 
1770512824f8SSeigo Tanimura /*
1771512824f8SSeigo Tanimura  * Do a wakeup when a selectable event occurs.
1772512824f8SSeigo Tanimura  */
1773512824f8SSeigo Tanimura static void
1774512824f8SSeigo Tanimura doselwakeup(sip, pri)
1775512824f8SSeigo Tanimura 	struct selinfo *sip;
1776512824f8SSeigo Tanimura 	int pri;
1777512824f8SSeigo Tanimura {
1778ace8398dSJeff Roberson 	struct selfd *sfp;
1779ace8398dSJeff Roberson 	struct selfd *sfn;
1780ace8398dSJeff Roberson 	struct seltd *stp;
1781df8bae1dSRodney W. Grimes 
1782ace8398dSJeff Roberson 	/* If it's not initialized there can't be any waiters. */
1783ace8398dSJeff Roberson 	if (sip->si_mtx == NULL)
1784b40ce416SJulian Elischer 		return;
1785ace8398dSJeff Roberson 	/*
1786ace8398dSJeff Roberson 	 * Locking the selinfo locks all selfds associated with it.
1787ace8398dSJeff Roberson 	 */
1788ace8398dSJeff Roberson 	mtx_lock(sip->si_mtx);
1789ace8398dSJeff Roberson 	TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1790ace8398dSJeff Roberson 		/*
1791ace8398dSJeff Roberson 		 * Once we remove this sfp from the list and clear the
1792ace8398dSJeff Roberson 		 * sf_si seltdclear will know to ignore this si.
1793ace8398dSJeff Roberson 		 */
1794ace8398dSJeff Roberson 		TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1795ace8398dSJeff Roberson 		sfp->sf_si = NULL;
1796ace8398dSJeff Roberson 		stp = sfp->sf_td;
1797ace8398dSJeff Roberson 		mtx_lock(&stp->st_mtx);
1798ace8398dSJeff Roberson 		stp->st_flags |= SELTD_PENDING;
1799ace8398dSJeff Roberson 		cv_broadcastpri(&stp->st_wait, pri);
1800ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1801fcb5b3a4SKonstantin Belousov 		if (refcount_release(&sfp->sf_refs))
1802fcb5b3a4SKonstantin Belousov 			uma_zfree(selfd_zone, sfp);
1803b40ce416SJulian Elischer 	}
1804ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1805ace8398dSJeff Roberson }
1806ace8398dSJeff Roberson 
1807ace8398dSJeff Roberson static void
1808ace8398dSJeff Roberson seltdinit(struct thread *td)
1809ace8398dSJeff Roberson {
1810ace8398dSJeff Roberson 	struct seltd *stp;
1811ace8398dSJeff Roberson 
1812ace8398dSJeff Roberson 	if ((stp = td->td_sel) != NULL)
1813ace8398dSJeff Roberson 		goto out;
1814ace8398dSJeff Roberson 	td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1815ace8398dSJeff Roberson 	mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1816ace8398dSJeff Roberson 	cv_init(&stp->st_wait, "select");
1817ace8398dSJeff Roberson out:
1818ace8398dSJeff Roberson 	stp->st_flags = 0;
1819ace8398dSJeff Roberson 	STAILQ_INIT(&stp->st_selq);
1820ace8398dSJeff Roberson }
1821ace8398dSJeff Roberson 
1822ace8398dSJeff Roberson static int
1823cf5e4fe6SDavide Italiano seltdwait(struct thread *td, sbintime_t sbt, sbintime_t precision)
1824ace8398dSJeff Roberson {
1825ace8398dSJeff Roberson 	struct seltd *stp;
1826ace8398dSJeff Roberson 	int error;
1827ace8398dSJeff Roberson 
1828ace8398dSJeff Roberson 	stp = td->td_sel;
1829ace8398dSJeff Roberson 	/*
1830ace8398dSJeff Roberson 	 * An event of interest may occur while we do not hold the seltd
1831ace8398dSJeff Roberson 	 * locked so check the pending flag before we sleep.
1832ace8398dSJeff Roberson 	 */
1833ace8398dSJeff Roberson 	mtx_lock(&stp->st_mtx);
1834ace8398dSJeff Roberson 	/*
1835ace8398dSJeff Roberson 	 * Any further calls to selrecord will be a rescan.
1836ace8398dSJeff Roberson 	 */
1837ace8398dSJeff Roberson 	stp->st_flags |= SELTD_RESCAN;
1838ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_PENDING) {
1839ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1840ace8398dSJeff Roberson 		return (0);
1841ace8398dSJeff Roberson 	}
1842cf5e4fe6SDavide Italiano 	if (sbt == 0)
1843cf5e4fe6SDavide Italiano 		error = EWOULDBLOCK;
1844cf5e4fe6SDavide Italiano 	else if (sbt != -1)
1845cf5e4fe6SDavide Italiano 		error = cv_timedwait_sig_sbt(&stp->st_wait, &stp->st_mtx,
1846cf5e4fe6SDavide Italiano 		    sbt, precision, C_ABSOLUTE);
1847ace8398dSJeff Roberson 	else
1848ace8398dSJeff Roberson 		error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
1849ace8398dSJeff Roberson 	mtx_unlock(&stp->st_mtx);
1850ace8398dSJeff Roberson 
1851ace8398dSJeff Roberson 	return (error);
1852ace8398dSJeff Roberson }
1853ace8398dSJeff Roberson 
1854ace8398dSJeff Roberson void
1855ace8398dSJeff Roberson seltdfini(struct thread *td)
1856ace8398dSJeff Roberson {
1857ace8398dSJeff Roberson 	struct seltd *stp;
1858ace8398dSJeff Roberson 
1859ace8398dSJeff Roberson 	stp = td->td_sel;
1860ace8398dSJeff Roberson 	if (stp == NULL)
1861ace8398dSJeff Roberson 		return;
1862ace8398dSJeff Roberson 	if (stp->st_free1)
1863ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free1);
1864ace8398dSJeff Roberson 	if (stp->st_free2)
1865ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free2);
1866ace8398dSJeff Roberson 	td->td_sel = NULL;
186736bce27bSKonstantin Belousov 	cv_destroy(&stp->st_wait);
186836bce27bSKonstantin Belousov 	mtx_destroy(&stp->st_mtx);
1869ace8398dSJeff Roberson 	free(stp, M_SELECT);
1870ace8398dSJeff Roberson }
1871ace8398dSJeff Roberson 
1872ace8398dSJeff Roberson /*
1873ace8398dSJeff Roberson  * Remove the references to the thread from all of the objects we were
1874ace8398dSJeff Roberson  * polling.
1875ace8398dSJeff Roberson  */
1876ace8398dSJeff Roberson static void
1877ace8398dSJeff Roberson seltdclear(struct thread *td)
1878ace8398dSJeff Roberson {
1879ace8398dSJeff Roberson 	struct seltd *stp;
1880ace8398dSJeff Roberson 	struct selfd *sfp;
1881ace8398dSJeff Roberson 	struct selfd *sfn;
1882ace8398dSJeff Roberson 
1883ace8398dSJeff Roberson 	stp = td->td_sel;
1884ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
1885ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1886ace8398dSJeff Roberson 	stp->st_flags = 0;
1887df8bae1dSRodney W. Grimes }
1888265fc98fSSeigo Tanimura 
18894d77a549SAlfred Perlstein static void selectinit(void *);
1890ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
1891265fc98fSSeigo Tanimura static void
1892ace8398dSJeff Roberson selectinit(void *dummy __unused)
1893265fc98fSSeigo Tanimura {
18942141453eSJeff Roberson 
1895ace8398dSJeff Roberson 	selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL,
1896ace8398dSJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, 0);
18972141453eSJeff Roberson 	mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);
1898265fc98fSSeigo Tanimura }
18990acf5d0bSMark Johnston 
19000acf5d0bSMark Johnston /*
19010acf5d0bSMark Johnston  * Set up a syscall return value that follows the convention specified for
19020acf5d0bSMark Johnston  * posix_* functions.
19030acf5d0bSMark Johnston  */
19040acf5d0bSMark Johnston int
19050acf5d0bSMark Johnston kern_posix_error(struct thread *td, int error)
19060acf5d0bSMark Johnston {
19070acf5d0bSMark Johnston 
19080acf5d0bSMark Johnston 	if (error <= 0)
19090acf5d0bSMark Johnston 		return (error);
19100acf5d0bSMark Johnston 	td->td_errno = error;
19110acf5d0bSMark Johnston 	td->td_pflags |= TDP_NERRNO;
19120acf5d0bSMark Johnston 	td->td_retval[0] = error;
19130acf5d0bSMark Johnston 	return (0);
19140acf5d0bSMark Johnston }
1915