xref: /freebsd/sys/kern/sys_generic.c (revision 04c91ac48ad13ce0d1392cedbd69c2c0223d206f)
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>
587a202823SKonstantin Belousov #include <sys/eventfd.h>
59df8bae1dSRodney W. Grimes #include <sys/kernel.h>
60e4650294SJohn Baldwin #include <sys/ktr.h>
61104a9b7eSAlexander Kabaev #include <sys/limits.h>
62df8bae1dSRodney W. Grimes #include <sys/malloc.h>
6342d11757SPeter Wemm #include <sys/poll.h>
6489b71647SPeter Wemm #include <sys/resourcevar.h>
650a2c3d48SGarrett Wollman #include <sys/selinfo.h>
6644f3b092SJohn Baldwin #include <sys/sleepqueue.h>
677a202823SKonstantin Belousov #include <sys/specialfd.h>
688f19eb88SIan Dowse #include <sys/syscallsubr.h>
698cb96f20SPeter Wemm #include <sys/sysctl.h>
7042d11757SPeter Wemm #include <sys/sysent.h>
719bbee259SAndrey A. Chernov #include <sys/vnode.h>
72279d7226SMatthew Dillon #include <sys/bio.h>
73279d7226SMatthew Dillon #include <sys/buf.h>
74265fc98fSSeigo Tanimura #include <sys/condvar.h>
75df8bae1dSRodney W. Grimes #ifdef KTRACE
76df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
77df8bae1dSRodney W. Grimes #endif
78df8bae1dSRodney W. Grimes 
79e4650294SJohn Baldwin #include <security/audit/audit.h>
80ace8398dSJeff Roberson 
8150ae6690SHans Petter Selasky /*
8250ae6690SHans Petter Selasky  * The following macro defines how many bytes will be allocated from
8350ae6690SHans Petter Selasky  * the stack instead of memory allocated when passing the IOCTL data
8450ae6690SHans Petter Selasky  * structures from userspace and to the kernel. Some IOCTLs having
8550ae6690SHans Petter Selasky  * small data structures are used very frequently and this small
8650ae6690SHans Petter Selasky  * buffer on the stack gives a significant speedup improvement for
8750ae6690SHans Petter Selasky  * those requests. The value of this define should be greater or equal
8850ae6690SHans Petter Selasky  * to 64 bytes and should also be power of two. The data structure is
8950ae6690SHans Petter Selasky  * currently hard-aligned to a 8-byte boundary on the stack. This
9050ae6690SHans Petter Selasky  * should currently be sufficient for all supported platforms.
9150ae6690SHans Petter Selasky  */
9250ae6690SHans Petter Selasky #define	SYS_IOCTL_SMALL_SIZE	128	/* bytes */
9350ae6690SHans Petter Selasky #define	SYS_IOCTL_SMALL_ALIGN	8	/* bytes */
9450ae6690SHans Petter Selasky 
95611fcff9SJohn Baldwin #ifdef __LP64__
96611fcff9SJohn Baldwin static int iosize_max_clamp = 0;
978bb9a904SKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, iosize_max_clamp, CTLFLAG_RW,
988bb9a904SKonstantin Belousov     &iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX");
99611fcff9SJohn Baldwin static int devfs_iosize_max_clamp = 1;
100bf3e483bSKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, devfs_iosize_max_clamp, CTLFLAG_RW,
101bf3e483bSKonstantin Belousov     &devfs_iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX for devices");
102611fcff9SJohn Baldwin #endif
103bf3e483bSKonstantin Belousov 
1048bb9a904SKonstantin Belousov /*
1058bb9a904SKonstantin Belousov  * Assert that the return value of read(2) and write(2) syscalls fits
1068bb9a904SKonstantin Belousov  * into a register.  If not, an architecture will need to provide the
1078bb9a904SKonstantin Belousov  * usermode wrappers to reconstruct the result.
1088bb9a904SKonstantin Belousov  */
1098bb9a904SKonstantin Belousov CTASSERT(sizeof(register_t) >= sizeof(size_t));
110526d0bd5SKonstantin Belousov 
111a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
112a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
113a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's");
11455166637SPoul-Henning Kamp 
1156d8feddaSKonstantin Belousov static int	pollout(struct thread *, struct pollfd *, struct pollfd *,
1166d8feddaSKonstantin Belousov 		    u_int);
117bbbb04ceSAlfred Perlstein static int	pollscan(struct thread *, struct pollfd *, u_int);
118ace8398dSJeff Roberson static int	pollrescan(struct thread *);
119bbbb04ceSAlfred Perlstein static int	selscan(struct thread *, fd_mask **, fd_mask **, int);
120ace8398dSJeff Roberson static int	selrescan(struct thread *, fd_mask **, fd_mask **);
121ace8398dSJeff Roberson static void	selfdalloc(struct thread *, void *);
122ace8398dSJeff Roberson static void	selfdfree(struct seltd *, struct selfd *);
123bcd9e0ddSJohn Baldwin static int	dofileread(struct thread *, int, struct file *, struct uio *,
124bcd9e0ddSJohn Baldwin 		    off_t, int);
125bcd9e0ddSJohn Baldwin static int	dofilewrite(struct thread *, int, struct file *, struct uio *,
126bcd9e0ddSJohn Baldwin 		    off_t, int);
127512824f8SSeigo Tanimura static void	doselwakeup(struct selinfo *, int);
128ace8398dSJeff Roberson static void	seltdinit(struct thread *);
129cf5e4fe6SDavide Italiano static int	seltdwait(struct thread *, sbintime_t, sbintime_t);
130ace8398dSJeff Roberson static void	seltdclear(struct thread *);
131ace8398dSJeff Roberson 
132ace8398dSJeff Roberson /*
133ace8398dSJeff Roberson  * One seltd per-thread allocated on demand as needed.
134ace8398dSJeff Roberson  *
135ace8398dSJeff Roberson  *	t - protected by st_mtx
136ace8398dSJeff Roberson  * 	k - Only accessed by curthread or read-only
137ace8398dSJeff Roberson  */
138ace8398dSJeff Roberson struct seltd {
139ace8398dSJeff Roberson 	STAILQ_HEAD(, selfd)	st_selq;	/* (k) List of selfds. */
140ace8398dSJeff Roberson 	struct selfd		*st_free1;	/* (k) free fd for read set. */
141ace8398dSJeff Roberson 	struct selfd		*st_free2;	/* (k) free fd for write set. */
142ace8398dSJeff Roberson 	struct mtx		st_mtx;		/* Protects struct seltd */
143ace8398dSJeff Roberson 	struct cv		st_wait;	/* (t) Wait channel. */
144ace8398dSJeff Roberson 	int			st_flags;	/* (t) SELTD_ flags. */
145ace8398dSJeff Roberson };
146ace8398dSJeff Roberson 
147ace8398dSJeff Roberson #define	SELTD_PENDING	0x0001			/* We have pending events. */
148ace8398dSJeff Roberson #define	SELTD_RESCAN	0x0002			/* Doing a rescan. */
149ace8398dSJeff Roberson 
150ace8398dSJeff Roberson /*
151ace8398dSJeff Roberson  * One selfd allocated per-thread per-file-descriptor.
152ace8398dSJeff Roberson  *	f - protected by sf_mtx
153ace8398dSJeff Roberson  */
154ace8398dSJeff Roberson struct selfd {
155ace8398dSJeff Roberson 	STAILQ_ENTRY(selfd)	sf_link;	/* (k) fds owned by this td. */
156ace8398dSJeff Roberson 	TAILQ_ENTRY(selfd)	sf_threads;	/* (f) fds on this selinfo. */
157ace8398dSJeff Roberson 	struct selinfo		*sf_si;		/* (f) selinfo when linked. */
158ace8398dSJeff Roberson 	struct mtx		*sf_mtx;	/* Pointer to selinfo mtx. */
159ace8398dSJeff Roberson 	struct seltd		*sf_td;		/* (k) owning seltd. */
160ace8398dSJeff Roberson 	void			*sf_cookie;	/* (k) fd or pollfd. */
161ace8398dSJeff Roberson };
162ace8398dSJeff Roberson 
163ea33cca9SMateusz Guzik MALLOC_DEFINE(M_SELFD, "selfd", "selfd");
1642141453eSJeff Roberson static struct mtx_pool *mtxpool_select;
1658fe387abSDmitrij Tejblum 
166611fcff9SJohn Baldwin #ifdef __LP64__
167611fcff9SJohn Baldwin size_t
168611fcff9SJohn Baldwin devfs_iosize_max(void)
169611fcff9SJohn Baldwin {
170611fcff9SJohn Baldwin 
171611fcff9SJohn Baldwin 	return (devfs_iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ?
172611fcff9SJohn Baldwin 	    INT_MAX : SSIZE_MAX);
173611fcff9SJohn Baldwin }
174611fcff9SJohn Baldwin 
175611fcff9SJohn Baldwin size_t
176611fcff9SJohn Baldwin iosize_max(void)
177611fcff9SJohn Baldwin {
178611fcff9SJohn Baldwin 
179611fcff9SJohn Baldwin 	return (iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ?
180611fcff9SJohn Baldwin 	    INT_MAX : SSIZE_MAX);
181611fcff9SJohn Baldwin }
182611fcff9SJohn Baldwin #endif
183611fcff9SJohn Baldwin 
184d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
185df8bae1dSRodney W. Grimes struct read_args {
186df8bae1dSRodney W. Grimes 	int	fd;
187134e06feSBruce Evans 	void	*buf;
188134e06feSBruce Evans 	size_t	nbyte;
189df8bae1dSRodney W. Grimes };
190d2d3e875SBruce Evans #endif
19126f9a767SRodney W. Grimes int
192cc3c9df8SEd Maste sys_read(struct thread *td, struct read_args *uap)
193df8bae1dSRodney W. Grimes {
194bcd9e0ddSJohn Baldwin 	struct uio auio;
195bcd9e0ddSJohn Baldwin 	struct iovec aiov;
196279d7226SMatthew Dillon 	int error;
197df8bae1dSRodney W. Grimes 
198526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
199bcd9e0ddSJohn Baldwin 		return (EINVAL);
200bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
201bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
202bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
203bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
204bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
205bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
206bcd9e0ddSJohn Baldwin 	error = kern_readv(td, uap->fd, &auio);
207279d7226SMatthew Dillon 	return (error);
208df8bae1dSRodney W. Grimes }
209df8bae1dSRodney W. Grimes 
210df8bae1dSRodney W. Grimes /*
211bcd9e0ddSJohn Baldwin  * Positioned read system call
2124160ccd9SAlan Cox  */
2134160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
2144160ccd9SAlan Cox struct pread_args {
2154160ccd9SAlan Cox 	int	fd;
2164160ccd9SAlan Cox 	void	*buf;
2174160ccd9SAlan Cox 	size_t	nbyte;
2188fe387abSDmitrij Tejblum 	int	pad;
2194160ccd9SAlan Cox 	off_t	offset;
2204160ccd9SAlan Cox };
2214160ccd9SAlan Cox #endif
2224160ccd9SAlan Cox int
223b38b22b0SEdward Tomasz Napierala sys_pread(struct thread *td, struct pread_args *uap)
224b38b22b0SEdward Tomasz Napierala {
225b38b22b0SEdward Tomasz Napierala 
226b38b22b0SEdward Tomasz Napierala 	return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
227b38b22b0SEdward Tomasz Napierala }
228b38b22b0SEdward Tomasz Napierala 
229b38b22b0SEdward Tomasz Napierala int
230b38b22b0SEdward Tomasz Napierala kern_pread(struct thread *td, int fd, void *buf, size_t nbyte, off_t offset)
2314160ccd9SAlan Cox {
2324160ccd9SAlan Cox 	struct uio auio;
2334160ccd9SAlan Cox 	struct iovec aiov;
234bcd9e0ddSJohn Baldwin 	int error;
2354160ccd9SAlan Cox 
236b38b22b0SEdward Tomasz Napierala 	if (nbyte > IOSIZE_MAX)
237bcd9e0ddSJohn Baldwin 		return (EINVAL);
238b38b22b0SEdward Tomasz Napierala 	aiov.iov_base = buf;
239b38b22b0SEdward Tomasz Napierala 	aiov.iov_len = nbyte;
2404160ccd9SAlan Cox 	auio.uio_iov = &aiov;
2414160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
242b38b22b0SEdward Tomasz Napierala 	auio.uio_resid = nbyte;
2434160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
244b38b22b0SEdward Tomasz Napierala 	error = kern_preadv(td, fd, &auio, offset);
2454160ccd9SAlan Cox 	return (error);
2464160ccd9SAlan Cox }
2474160ccd9SAlan Cox 
2480538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6)
249c2815ad5SPeter Wemm int
250b38b22b0SEdward Tomasz Napierala freebsd6_pread(struct thread *td, struct freebsd6_pread_args *uap)
251c2815ad5SPeter Wemm {
252c2815ad5SPeter Wemm 
253b38b22b0SEdward Tomasz Napierala 	return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
254c2815ad5SPeter Wemm }
2550538aafcSKonstantin Belousov #endif
256c2815ad5SPeter Wemm 
2574160ccd9SAlan Cox /*
258df8bae1dSRodney W. Grimes  * Scatter read system call.
259df8bae1dSRodney W. Grimes  */
260d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
261df8bae1dSRodney W. Grimes struct readv_args {
2627147b19dSBruce Evans 	int	fd;
263df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
264df8bae1dSRodney W. Grimes 	u_int	iovcnt;
265df8bae1dSRodney W. Grimes };
266d2d3e875SBruce Evans #endif
26726f9a767SRodney W. Grimes int
2688451d0ddSKip Macy sys_readv(struct thread *td, struct readv_args *uap)
269df8bae1dSRodney W. Grimes {
270b88ec951SJohn Baldwin 	struct uio *auio;
271b88ec951SJohn Baldwin 	int error;
272b88ec951SJohn Baldwin 
273b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
274b88ec951SJohn Baldwin 	if (error)
275b88ec951SJohn Baldwin 		return (error);
276b88ec951SJohn Baldwin 	error = kern_readv(td, uap->fd, auio);
277b88ec951SJohn Baldwin 	free(auio, M_IOV);
278b88ec951SJohn Baldwin 	return (error);
279b88ec951SJohn Baldwin }
280b88ec951SJohn Baldwin 
281b88ec951SJohn Baldwin int
282b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio)
283b88ec951SJohn Baldwin {
284b064d43dSMatthew Dillon 	struct file *fp;
285bcd9e0ddSJohn Baldwin 	int error;
286bcd9e0ddSJohn Baldwin 
287cbd92ce6SMatt Macy 	error = fget_read(td, fd, &cap_read_rights, &fp);
288bcd9e0ddSJohn Baldwin 	if (error)
289bcd9e0ddSJohn Baldwin 		return (error);
290bcd9e0ddSJohn Baldwin 	error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
291bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
292bcd9e0ddSJohn Baldwin 	return (error);
293bcd9e0ddSJohn Baldwin }
294bcd9e0ddSJohn Baldwin 
295bcd9e0ddSJohn Baldwin /*
296bcd9e0ddSJohn Baldwin  * Scatter positioned read system call.
297bcd9e0ddSJohn Baldwin  */
298bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
299bcd9e0ddSJohn Baldwin struct preadv_args {
300bcd9e0ddSJohn Baldwin 	int	fd;
301bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
302bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
303bcd9e0ddSJohn Baldwin 	off_t	offset;
304bcd9e0ddSJohn Baldwin };
305bcd9e0ddSJohn Baldwin #endif
306bcd9e0ddSJohn Baldwin int
3078451d0ddSKip Macy sys_preadv(struct thread *td, struct preadv_args *uap)
308bcd9e0ddSJohn Baldwin {
309bcd9e0ddSJohn Baldwin 	struct uio *auio;
310bcd9e0ddSJohn Baldwin 	int error;
311bcd9e0ddSJohn Baldwin 
312bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
313bcd9e0ddSJohn Baldwin 	if (error)
314bcd9e0ddSJohn Baldwin 		return (error);
315bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, auio, uap->offset);
316bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
317bcd9e0ddSJohn Baldwin 	return (error);
318bcd9e0ddSJohn Baldwin }
319bcd9e0ddSJohn Baldwin 
320bcd9e0ddSJohn Baldwin int
321cc3c9df8SEd Maste kern_preadv(struct thread *td, int fd, struct uio *auio, off_t offset)
322bcd9e0ddSJohn Baldwin {
323bcd9e0ddSJohn Baldwin 	struct file *fp;
324bcd9e0ddSJohn Baldwin 	int error;
325bcd9e0ddSJohn Baldwin 
326cbd92ce6SMatt Macy 	error = fget_read(td, fd, &cap_pread_rights, &fp);
327bcd9e0ddSJohn Baldwin 	if (error)
328bcd9e0ddSJohn Baldwin 		return (error);
329bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
330bcd9e0ddSJohn Baldwin 		error = ESPIPE;
33132a1fb0dSMahdi Mokhtari 	else if (offset < 0 &&
33232a1fb0dSMahdi Mokhtari 	    (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR))
333bcd9e0ddSJohn Baldwin 		error = EINVAL;
334bcd9e0ddSJohn Baldwin 	else
335bcd9e0ddSJohn Baldwin 		error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
336bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
337bcd9e0ddSJohn Baldwin 	return (error);
338bcd9e0ddSJohn Baldwin }
339bcd9e0ddSJohn Baldwin 
340bcd9e0ddSJohn Baldwin /*
341bcd9e0ddSJohn Baldwin  * Common code for readv and preadv that reads data in
342bcd9e0ddSJohn Baldwin  * from a file using the passed in uio, offset, and flags.
343bcd9e0ddSJohn Baldwin  */
344bcd9e0ddSJohn Baldwin static int
345cc3c9df8SEd Maste dofileread(struct thread *td, int fd, struct file *fp, struct uio *auio,
346cc3c9df8SEd Maste     off_t offset, int flags)
347bcd9e0ddSJohn Baldwin {
348bcd9e0ddSJohn Baldwin 	ssize_t cnt;
34982641acdSAlan Cox 	int error;
350df8bae1dSRodney W. Grimes #ifdef KTRACE
351552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
352df8bae1dSRodney W. Grimes #endif
353df8bae1dSRodney W. Grimes 
35451d1f690SRobert Watson 	AUDIT_ARG_FD(fd);
35551d1f690SRobert Watson 
3564f8d23d6SPoul-Henning Kamp 	/* Finish zero length reads right here */
3574f8d23d6SPoul-Henning Kamp 	if (auio->uio_resid == 0) {
3584f8d23d6SPoul-Henning Kamp 		td->td_retval[0] = 0;
3594f8d23d6SPoul-Henning Kamp 		return (0);
3604f8d23d6SPoul-Henning Kamp 	}
361552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_READ;
362bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
363552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
364df8bae1dSRodney W. Grimes #ifdef KTRACE
365552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
366552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
367df8bae1dSRodney W. Grimes #endif
368552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
369bcd9e0ddSJohn Baldwin 	if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
370552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
371df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
372df8bae1dSRodney W. Grimes 			error = 0;
373279d7226SMatthew Dillon 	}
374552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
375df8bae1dSRodney W. Grimes #ifdef KTRACE
376552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
377552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
378b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_READ, ktruio, error);
379df8bae1dSRodney W. Grimes 	}
380df8bae1dSRodney W. Grimes #endif
381b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
382df8bae1dSRodney W. Grimes 	return (error);
383df8bae1dSRodney W. Grimes }
384df8bae1dSRodney W. Grimes 
385d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
386df8bae1dSRodney W. Grimes struct write_args {
387df8bae1dSRodney W. Grimes 	int	fd;
388134e06feSBruce Evans 	const void *buf;
389134e06feSBruce Evans 	size_t	nbyte;
390df8bae1dSRodney W. Grimes };
391d2d3e875SBruce Evans #endif
39226f9a767SRodney W. Grimes int
393cc3c9df8SEd Maste sys_write(struct thread *td, struct write_args *uap)
394df8bae1dSRodney W. Grimes {
395bcd9e0ddSJohn Baldwin 	struct uio auio;
396bcd9e0ddSJohn Baldwin 	struct iovec aiov;
397279d7226SMatthew Dillon 	int error;
398df8bae1dSRodney W. Grimes 
399526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
400bcd9e0ddSJohn Baldwin 		return (EINVAL);
401bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
402bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
403bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
404bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
405bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
406bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
407bcd9e0ddSJohn Baldwin 	error = kern_writev(td, uap->fd, &auio);
408279d7226SMatthew Dillon 	return (error);
409df8bae1dSRodney W. Grimes }
410df8bae1dSRodney W. Grimes 
411df8bae1dSRodney W. Grimes /*
4120c14ff0eSRobert Watson  * Positioned write system call.
4134160ccd9SAlan Cox  */
4144160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
4154160ccd9SAlan Cox struct pwrite_args {
4164160ccd9SAlan Cox 	int	fd;
4174160ccd9SAlan Cox 	const void *buf;
4184160ccd9SAlan Cox 	size_t	nbyte;
4198fe387abSDmitrij Tejblum 	int	pad;
4204160ccd9SAlan Cox 	off_t	offset;
4214160ccd9SAlan Cox };
4224160ccd9SAlan Cox #endif
4234160ccd9SAlan Cox int
424b38b22b0SEdward Tomasz Napierala sys_pwrite(struct thread *td, struct pwrite_args *uap)
425b38b22b0SEdward Tomasz Napierala {
426b38b22b0SEdward Tomasz Napierala 
427b38b22b0SEdward Tomasz Napierala 	return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
428b38b22b0SEdward Tomasz Napierala }
429b38b22b0SEdward Tomasz Napierala 
430b38b22b0SEdward Tomasz Napierala int
431b38b22b0SEdward Tomasz Napierala kern_pwrite(struct thread *td, int fd, const void *buf, size_t nbyte,
432b38b22b0SEdward Tomasz Napierala     off_t offset)
4334160ccd9SAlan Cox {
4344160ccd9SAlan Cox 	struct uio auio;
4354160ccd9SAlan Cox 	struct iovec aiov;
436bcd9e0ddSJohn Baldwin 	int error;
4374160ccd9SAlan Cox 
438b38b22b0SEdward Tomasz Napierala 	if (nbyte > IOSIZE_MAX)
439bcd9e0ddSJohn Baldwin 		return (EINVAL);
440b38b22b0SEdward Tomasz Napierala 	aiov.iov_base = (void *)(uintptr_t)buf;
441b38b22b0SEdward Tomasz Napierala 	aiov.iov_len = nbyte;
4424160ccd9SAlan Cox 	auio.uio_iov = &aiov;
4434160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
444b38b22b0SEdward Tomasz Napierala 	auio.uio_resid = nbyte;
4454160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
446b38b22b0SEdward Tomasz Napierala 	error = kern_pwritev(td, fd, &auio, offset);
4474160ccd9SAlan Cox 	return (error);
4484160ccd9SAlan Cox }
4494160ccd9SAlan Cox 
4500538aafcSKonstantin Belousov #if defined(COMPAT_FREEBSD6)
451c2815ad5SPeter Wemm int
452b38b22b0SEdward Tomasz Napierala freebsd6_pwrite(struct thread *td, struct freebsd6_pwrite_args *uap)
453c2815ad5SPeter Wemm {
454c2815ad5SPeter Wemm 
455b38b22b0SEdward Tomasz Napierala 	return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
456c2815ad5SPeter Wemm }
4570538aafcSKonstantin Belousov #endif
458c2815ad5SPeter Wemm 
4594160ccd9SAlan Cox /*
4600c14ff0eSRobert Watson  * Gather write system call.
461df8bae1dSRodney W. Grimes  */
462d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
463df8bae1dSRodney W. Grimes struct writev_args {
464df8bae1dSRodney W. Grimes 	int	fd;
465df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
466df8bae1dSRodney W. Grimes 	u_int	iovcnt;
467df8bae1dSRodney W. Grimes };
468d2d3e875SBruce Evans #endif
46926f9a767SRodney W. Grimes int
4708451d0ddSKip Macy sys_writev(struct thread *td, struct writev_args *uap)
471df8bae1dSRodney W. Grimes {
472b88ec951SJohn Baldwin 	struct uio *auio;
473b88ec951SJohn Baldwin 	int error;
474b88ec951SJohn Baldwin 
475b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
476b88ec951SJohn Baldwin 	if (error)
477b88ec951SJohn Baldwin 		return (error);
478b88ec951SJohn Baldwin 	error = kern_writev(td, uap->fd, auio);
479b88ec951SJohn Baldwin 	free(auio, M_IOV);
480b88ec951SJohn Baldwin 	return (error);
481b88ec951SJohn Baldwin }
482b88ec951SJohn Baldwin 
483b88ec951SJohn Baldwin int
484b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio)
485b88ec951SJohn Baldwin {
486b064d43dSMatthew Dillon 	struct file *fp;
487bcd9e0ddSJohn Baldwin 	int error;
488bcd9e0ddSJohn Baldwin 
489cbd92ce6SMatt Macy 	error = fget_write(td, fd, &cap_write_rights, &fp);
490bcd9e0ddSJohn Baldwin 	if (error)
491af56abaaSJohn Baldwin 		return (error);
492bcd9e0ddSJohn Baldwin 	error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
493bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
494bcd9e0ddSJohn Baldwin 	return (error);
495bcd9e0ddSJohn Baldwin }
496bcd9e0ddSJohn Baldwin 
497bcd9e0ddSJohn Baldwin /*
4980c14ff0eSRobert Watson  * Gather positioned write system call.
499bcd9e0ddSJohn Baldwin  */
500bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
501bcd9e0ddSJohn Baldwin struct pwritev_args {
502bcd9e0ddSJohn Baldwin 	int	fd;
503bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
504bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
505bcd9e0ddSJohn Baldwin 	off_t	offset;
506bcd9e0ddSJohn Baldwin };
507bcd9e0ddSJohn Baldwin #endif
508bcd9e0ddSJohn Baldwin int
5098451d0ddSKip Macy sys_pwritev(struct thread *td, struct pwritev_args *uap)
510bcd9e0ddSJohn Baldwin {
511bcd9e0ddSJohn Baldwin 	struct uio *auio;
512bcd9e0ddSJohn Baldwin 	int error;
513bcd9e0ddSJohn Baldwin 
514bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
515bcd9e0ddSJohn Baldwin 	if (error)
516bcd9e0ddSJohn Baldwin 		return (error);
517bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, auio, uap->offset);
518bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
519bcd9e0ddSJohn Baldwin 	return (error);
520bcd9e0ddSJohn Baldwin }
521bcd9e0ddSJohn Baldwin 
522bcd9e0ddSJohn Baldwin int
523d5cdcc3aSAndrew Gallatin kern_pwritev(struct thread *td, int fd, struct uio *auio, off_t offset)
524bcd9e0ddSJohn Baldwin {
525bcd9e0ddSJohn Baldwin 	struct file *fp;
526bcd9e0ddSJohn Baldwin 	int error;
527bcd9e0ddSJohn Baldwin 
528cbd92ce6SMatt Macy 	error = fget_write(td, fd, &cap_pwrite_rights, &fp);
529bcd9e0ddSJohn Baldwin 	if (error)
530af56abaaSJohn Baldwin 		return (error);
531bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
532bcd9e0ddSJohn Baldwin 		error = ESPIPE;
53332a1fb0dSMahdi Mokhtari 	else if (offset < 0 &&
53432a1fb0dSMahdi Mokhtari 	    (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR))
535bcd9e0ddSJohn Baldwin 		error = EINVAL;
536bcd9e0ddSJohn Baldwin 	else
537bcd9e0ddSJohn Baldwin 		error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
538bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
539bcd9e0ddSJohn Baldwin 	return (error);
540bcd9e0ddSJohn Baldwin }
541bcd9e0ddSJohn Baldwin 
542bcd9e0ddSJohn Baldwin /*
543bcd9e0ddSJohn Baldwin  * Common code for writev and pwritev that writes data to
544bcd9e0ddSJohn Baldwin  * a file using the passed in uio, offset, and flags.
545bcd9e0ddSJohn Baldwin  */
546bcd9e0ddSJohn Baldwin static int
547cc3c9df8SEd Maste dofilewrite(struct thread *td, int fd, struct file *fp, struct uio *auio,
548cc3c9df8SEd Maste     off_t offset, int flags)
549bcd9e0ddSJohn Baldwin {
550bcd9e0ddSJohn Baldwin 	ssize_t cnt;
551552afd9cSPoul-Henning Kamp 	int error;
552df8bae1dSRodney W. Grimes #ifdef KTRACE
553552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
554df8bae1dSRodney W. Grimes #endif
555df8bae1dSRodney W. Grimes 
55651d1f690SRobert Watson 	AUDIT_ARG_FD(fd);
557552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_WRITE;
558552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
559bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
560df8bae1dSRodney W. Grimes #ifdef KTRACE
561552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
562552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
563df8bae1dSRodney W. Grimes #endif
564552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
565bcd9e0ddSJohn Baldwin 	if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
566552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
567df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
568df8bae1dSRodney W. Grimes 			error = 0;
569bcd9e0ddSJohn Baldwin 		/* Socket layer is responsible for issuing SIGPIPE. */
5706f7ca813SBruce M Simpson 		if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
571b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
5727a6f3d78SJohn Baldwin 			tdsignal(td, SIGPIPE);
573b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
57419eb87d2SJohn Baldwin 		}
575df8bae1dSRodney W. Grimes 	}
576552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
577df8bae1dSRodney W. Grimes #ifdef KTRACE
578552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
579552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
580b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_WRITE, ktruio, error);
581df8bae1dSRodney W. Grimes 	}
582df8bae1dSRodney W. Grimes #endif
583b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
584df8bae1dSRodney W. Grimes 	return (error);
585df8bae1dSRodney W. Grimes }
586df8bae1dSRodney W. Grimes 
587e4650294SJohn Baldwin /*
588e4650294SJohn Baldwin  * Truncate a file given a file descriptor.
589e4650294SJohn Baldwin  *
590e4650294SJohn Baldwin  * Can't use fget_write() here, since must return EINVAL and not EBADF if the
591e4650294SJohn Baldwin  * descriptor isn't writable.
592e4650294SJohn Baldwin  */
593e4650294SJohn Baldwin int
594cc3c9df8SEd Maste kern_ftruncate(struct thread *td, int fd, off_t length)
595e4650294SJohn Baldwin {
596e4650294SJohn Baldwin 	struct file *fp;
597e4650294SJohn Baldwin 	int error;
598e4650294SJohn Baldwin 
59914961ba7SRobert Watson 	AUDIT_ARG_FD(fd);
600e4650294SJohn Baldwin 	if (length < 0)
601e4650294SJohn Baldwin 		return (EINVAL);
602cbd92ce6SMatt Macy 	error = fget(td, fd, &cap_ftruncate_rights, &fp);
603e4650294SJohn Baldwin 	if (error)
604e4650294SJohn Baldwin 		return (error);
60514961ba7SRobert Watson 	AUDIT_ARG_FILE(td->td_proc, fp);
606e4650294SJohn Baldwin 	if (!(fp->f_flag & FWRITE)) {
607e4650294SJohn Baldwin 		fdrop(fp, td);
608e4650294SJohn Baldwin 		return (EINVAL);
609e4650294SJohn Baldwin 	}
610e4650294SJohn Baldwin 	error = fo_truncate(fp, length, td->td_ucred, td);
611e4650294SJohn Baldwin 	fdrop(fp, td);
612e4650294SJohn Baldwin 	return (error);
613e4650294SJohn Baldwin }
614e4650294SJohn Baldwin 
615e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
616e4650294SJohn Baldwin struct ftruncate_args {
617e4650294SJohn Baldwin 	int	fd;
618e4650294SJohn Baldwin 	int	pad;
619e4650294SJohn Baldwin 	off_t	length;
620e4650294SJohn Baldwin };
621e4650294SJohn Baldwin #endif
622e4650294SJohn Baldwin int
623cc3c9df8SEd Maste sys_ftruncate(struct thread *td, struct ftruncate_args *uap)
624e4650294SJohn Baldwin {
625e4650294SJohn Baldwin 
626e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
627e4650294SJohn Baldwin }
628e4650294SJohn Baldwin 
629e4650294SJohn Baldwin #if defined(COMPAT_43)
630e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
631e4650294SJohn Baldwin struct oftruncate_args {
632e4650294SJohn Baldwin 	int	fd;
633e4650294SJohn Baldwin 	long	length;
634e4650294SJohn Baldwin };
635e4650294SJohn Baldwin #endif
636e4650294SJohn Baldwin int
637cc3c9df8SEd Maste oftruncate(struct thread *td, struct oftruncate_args *uap)
638e4650294SJohn Baldwin {
639e4650294SJohn Baldwin 
640e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
641e4650294SJohn Baldwin }
642e4650294SJohn Baldwin #endif /* COMPAT_43 */
643e4650294SJohn Baldwin 
644d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
645df8bae1dSRodney W. Grimes struct ioctl_args {
646df8bae1dSRodney W. Grimes 	int	fd;
647069e9bc1SDoug Rabson 	u_long	com;
648df8bae1dSRodney W. Grimes 	caddr_t	data;
649df8bae1dSRodney W. Grimes };
650d2d3e875SBruce Evans #endif
651df8bae1dSRodney W. Grimes /* ARGSUSED */
65226f9a767SRodney W. Grimes int
6538451d0ddSKip Macy sys_ioctl(struct thread *td, struct ioctl_args *uap)
654df8bae1dSRodney W. Grimes {
65550ae6690SHans Petter Selasky 	u_char smalldata[SYS_IOCTL_SMALL_SIZE] __aligned(SYS_IOCTL_SMALL_ALIGN);
656a90fb6cfSHans Petter Selasky 	uint32_t com;
6579fddcc66SRuslan Ermilov 	int arg, error;
6583e15c66fSPoul-Henning Kamp 	u_int size;
6599fddcc66SRuslan Ermilov 	caddr_t data;
660df8bae1dSRodney W. Grimes 
661a90fb6cfSHans Petter Selasky #ifdef INVARIANTS
6629fc6aa06SPoul-Henning Kamp 	if (uap->com > 0xffffffff) {
6639fc6aa06SPoul-Henning Kamp 		printf(
6649fc6aa06SPoul-Henning Kamp 		    "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
665431f8906SJulian Elischer 		    td->td_proc->p_pid, td->td_name, uap->com);
6669fc6aa06SPoul-Henning Kamp 	}
667a90fb6cfSHans Petter Selasky #endif
668a90fb6cfSHans Petter Selasky 	com = (uint32_t)uap->com;
669df8bae1dSRodney W. Grimes 
670df8bae1dSRodney W. Grimes 	/*
671df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
672df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
673df8bae1dSRodney W. Grimes 	 */
674df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
675ca51b19bSPoul-Henning Kamp 	if ((size > IOCPARM_MAX) ||
676ca51b19bSPoul-Henning Kamp 	    ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
6772de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
6782de92a38SPeter Wemm 	    ((com & IOC_OUT) && size == 0) ||
6792de92a38SPeter Wemm #else
6802de92a38SPeter Wemm 	    ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
6812de92a38SPeter Wemm #endif
6829fddcc66SRuslan Ermilov 	    ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
683426da3bcSAlfred Perlstein 		return (ENOTTY);
684279d7226SMatthew Dillon 
685ca51b19bSPoul-Henning Kamp 	if (size > 0) {
686715457f6SDavid E. O'Brien 		if (com & IOC_VOID) {
6879fddcc66SRuslan Ermilov 			/* Integer argument. */
6889fddcc66SRuslan Ermilov 			arg = (intptr_t)uap->data;
6899fddcc66SRuslan Ermilov 			data = (void *)&arg;
6909fddcc66SRuslan Ermilov 			size = 0;
6915cbf44bfSMateusz Guzik 		} else {
6920ecd606bSHans Petter Selasky 			if (size > SYS_IOCTL_SMALL_SIZE)
693715457f6SDavid E. O'Brien 				data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
6940ecd606bSHans Petter Selasky 			else
6950ecd606bSHans Petter Selasky 				data = smalldata;
6965cbf44bfSMateusz Guzik 		}
6979fddcc66SRuslan Ermilov 	} else
6989fddcc66SRuslan Ermilov 		data = (void *)&uap->data;
699df8bae1dSRodney W. Grimes 	if (com & IOC_IN) {
700df8bae1dSRodney W. Grimes 		error = copyin(uap->data, data, (u_int)size);
7015cbf44bfSMateusz Guzik 		if (error != 0)
7025cbf44bfSMateusz Guzik 			goto out;
703ca51b19bSPoul-Henning Kamp 	} else if (com & IOC_OUT) {
704df8bae1dSRodney W. Grimes 		/*
705df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
706df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
707df8bae1dSRodney W. Grimes 		 */
708df8bae1dSRodney W. Grimes 		bzero(data, size);
709279d7226SMatthew Dillon 	}
710df8bae1dSRodney W. Grimes 
711d9f46233SJohn Baldwin 	error = kern_ioctl(td, uap->fd, com, data);
712d9f46233SJohn Baldwin 
713d9f46233SJohn Baldwin 	if (error == 0 && (com & IOC_OUT))
714d9f46233SJohn Baldwin 		error = copyout(data, uap->data, (u_int)size);
715d9f46233SJohn Baldwin 
7165cbf44bfSMateusz Guzik out:
7170ecd606bSHans Petter Selasky 	if (size > SYS_IOCTL_SMALL_SIZE)
7189fddcc66SRuslan Ermilov 		free(data, M_IOCTLOPS);
719d9f46233SJohn Baldwin 	return (error);
720d9f46233SJohn Baldwin }
721d9f46233SJohn Baldwin 
722d9f46233SJohn Baldwin int
723d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
724d9f46233SJohn Baldwin {
725d9f46233SJohn Baldwin 	struct file *fp;
726d9f46233SJohn Baldwin 	struct filedesc *fdp;
7272609222aSPawel Jakub Dawidek 	int error, tmp, locked;
728d9f46233SJohn Baldwin 
72964c9a4d9SRobert Watson 	AUDIT_ARG_FD(fd);
73064c9a4d9SRobert Watson 	AUDIT_ARG_CMD(com);
7312609222aSPawel Jakub Dawidek 
732d9f46233SJohn Baldwin 	fdp = td->td_proc->p_fd;
7332609222aSPawel Jakub Dawidek 
734d9f46233SJohn Baldwin 	switch (com) {
735d9f46233SJohn Baldwin 	case FIONCLEX:
736d9f46233SJohn Baldwin 	case FIOCLEX:
7375e3f7694SRobert Watson 		FILEDESC_XLOCK(fdp);
7382609222aSPawel Jakub Dawidek 		locked = LA_XLOCKED;
7392609222aSPawel Jakub Dawidek 		break;
7402609222aSPawel Jakub Dawidek 	default:
7412609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
7422609222aSPawel Jakub Dawidek 		FILEDESC_SLOCK(fdp);
7432609222aSPawel Jakub Dawidek 		locked = LA_SLOCKED;
7442609222aSPawel Jakub Dawidek #else
7452609222aSPawel Jakub Dawidek 		locked = LA_UNLOCKED;
7462609222aSPawel Jakub Dawidek #endif
7472609222aSPawel Jakub Dawidek 		break;
7482609222aSPawel Jakub Dawidek 	}
7492609222aSPawel Jakub Dawidek 
7502609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
7512609222aSPawel Jakub Dawidek 	if ((fp = fget_locked(fdp, fd)) == NULL) {
7522609222aSPawel Jakub Dawidek 		error = EBADF;
7532609222aSPawel Jakub Dawidek 		goto out;
7542609222aSPawel Jakub Dawidek 	}
7552609222aSPawel Jakub Dawidek 	if ((error = cap_ioctl_check(fdp, fd, com)) != 0) {
7562609222aSPawel Jakub Dawidek 		fp = NULL;	/* fhold() was not called yet */
7572609222aSPawel Jakub Dawidek 		goto out;
7582609222aSPawel Jakub Dawidek 	}
759f1cf2b9dSKonstantin Belousov 	if (!fhold(fp)) {
760f1cf2b9dSKonstantin Belousov 		error = EBADF;
761f1cf2b9dSKonstantin Belousov 		fp = NULL;
762f1cf2b9dSKonstantin Belousov 		goto out;
763f1cf2b9dSKonstantin Belousov 	}
7642609222aSPawel Jakub Dawidek 	if (locked == LA_SLOCKED) {
7652609222aSPawel Jakub Dawidek 		FILEDESC_SUNLOCK(fdp);
7662609222aSPawel Jakub Dawidek 		locked = LA_UNLOCKED;
7672609222aSPawel Jakub Dawidek 	}
7682609222aSPawel Jakub Dawidek #else
769cbd92ce6SMatt Macy 	error = fget(td, fd, &cap_ioctl_rights, &fp);
7707008be5bSPawel Jakub Dawidek 	if (error != 0) {
7712609222aSPawel Jakub Dawidek 		fp = NULL;
7722609222aSPawel Jakub Dawidek 		goto out;
7732609222aSPawel Jakub Dawidek 	}
7742609222aSPawel Jakub Dawidek #endif
7752609222aSPawel Jakub Dawidek 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
7762609222aSPawel Jakub Dawidek 		error = EBADF;
7772609222aSPawel Jakub Dawidek 		goto out;
7782609222aSPawel Jakub Dawidek 	}
7792609222aSPawel Jakub Dawidek 
7802609222aSPawel Jakub Dawidek 	switch (com) {
7812609222aSPawel Jakub Dawidek 	case FIONCLEX:
7822609222aSPawel Jakub Dawidek 		fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE;
7832609222aSPawel Jakub Dawidek 		goto out;
7842609222aSPawel Jakub Dawidek 	case FIOCLEX:
7852609222aSPawel Jakub Dawidek 		fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE;
786d9f46233SJohn Baldwin 		goto out;
787d9f46233SJohn Baldwin 	case FIONBIO:
788bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
789397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FNONBLOCK);
790df8bae1dSRodney W. Grimes 		else
791397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FNONBLOCK);
7928ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
793d9f46233SJohn Baldwin 		break;
794d9f46233SJohn Baldwin 	case FIOASYNC:
795bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
796397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FASYNC);
797df8bae1dSRodney W. Grimes 		else
798397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FASYNC);
7998ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
800d9f46233SJohn Baldwin 		break;
801df8bae1dSRodney W. Grimes 	}
8028ccf264fSPoul-Henning Kamp 
8038ccf264fSPoul-Henning Kamp 	error = fo_ioctl(fp, com, data, td->td_ucred, td);
804d9f46233SJohn Baldwin out:
8052609222aSPawel Jakub Dawidek 	switch (locked) {
8062609222aSPawel Jakub Dawidek 	case LA_XLOCKED:
8072609222aSPawel Jakub Dawidek 		FILEDESC_XUNLOCK(fdp);
8082609222aSPawel Jakub Dawidek 		break;
8092609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
8102609222aSPawel Jakub Dawidek 	case LA_SLOCKED:
8112609222aSPawel Jakub Dawidek 		FILEDESC_SUNLOCK(fdp);
8122609222aSPawel Jakub Dawidek 		break;
8132609222aSPawel Jakub Dawidek #endif
8142609222aSPawel Jakub Dawidek 	default:
8152609222aSPawel Jakub Dawidek 		FILEDESC_UNLOCK_ASSERT(fdp);
8162609222aSPawel Jakub Dawidek 		break;
8172609222aSPawel Jakub Dawidek 	}
8182609222aSPawel Jakub Dawidek 	if (fp != NULL)
819b40ce416SJulian Elischer 		fdrop(fp, td);
820df8bae1dSRodney W. Grimes 	return (error);
821df8bae1dSRodney W. Grimes }
822df8bae1dSRodney W. Grimes 
823125dcf8cSKonstantin Belousov int
8242856d85eSKyle Evans sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
8252856d85eSKyle Evans {
8262856d85eSKyle Evans 	int error;
8272856d85eSKyle Evans 
8282856d85eSKyle Evans 	error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len);
8292856d85eSKyle Evans 	return (kern_posix_error(td, error));
8302856d85eSKyle Evans }
8312856d85eSKyle Evans 
8322856d85eSKyle Evans int
8332856d85eSKyle Evans kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
8342856d85eSKyle Evans {
8352856d85eSKyle Evans 	struct file *fp;
8362856d85eSKyle Evans 	int error;
8372856d85eSKyle Evans 
8382856d85eSKyle Evans 	AUDIT_ARG_FD(fd);
8392856d85eSKyle Evans 	if (offset < 0 || len <= 0)
8402856d85eSKyle Evans 		return (EINVAL);
8412856d85eSKyle Evans 	/* Check for wrap. */
8422856d85eSKyle Evans 	if (offset > OFF_MAX - len)
8432856d85eSKyle Evans 		return (EFBIG);
8442856d85eSKyle Evans 	AUDIT_ARG_FD(fd);
8452856d85eSKyle Evans 	error = fget(td, fd, &cap_pwrite_rights, &fp);
8462856d85eSKyle Evans 	if (error != 0)
8472856d85eSKyle Evans 		return (error);
8482856d85eSKyle Evans 	AUDIT_ARG_FILE(td->td_proc, fp);
8492856d85eSKyle Evans 	if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
8502856d85eSKyle Evans 		error = ESPIPE;
8512856d85eSKyle Evans 		goto out;
8522856d85eSKyle Evans 	}
8532856d85eSKyle Evans 	if ((fp->f_flag & FWRITE) == 0) {
8542856d85eSKyle Evans 		error = EBADF;
8552856d85eSKyle Evans 		goto out;
8562856d85eSKyle Evans 	}
8572856d85eSKyle Evans 
8582856d85eSKyle Evans 	error = fo_fallocate(fp, offset, len, td);
8592856d85eSKyle Evans  out:
8602856d85eSKyle Evans 	fdrop(fp, td);
8612856d85eSKyle Evans 	return (error);
8622856d85eSKyle Evans }
8632856d85eSKyle Evans 
8642856d85eSKyle Evans int
8650dc332bfSKa Ho Ng sys_fspacectl(struct thread *td, struct fspacectl_args *uap)
8660dc332bfSKa Ho Ng {
8670dc332bfSKa Ho Ng 	struct spacectl_range rqsr, rmsr;
8680dc332bfSKa Ho Ng 	int error, cerror;
8690dc332bfSKa Ho Ng 
8700dc332bfSKa Ho Ng 	error = copyin(uap->rqsr, &rqsr, sizeof(rqsr));
8710dc332bfSKa Ho Ng 	if (error != 0)
8720dc332bfSKa Ho Ng 		return (error);
8730dc332bfSKa Ho Ng 
8740dc332bfSKa Ho Ng 	error = kern_fspacectl(td, uap->fd, uap->cmd, &rqsr, uap->flags,
8750dc332bfSKa Ho Ng 	    &rmsr);
8760dc332bfSKa Ho Ng 	if (uap->rmsr != NULL) {
8770dc332bfSKa Ho Ng 		cerror = copyout(&rmsr, uap->rmsr, sizeof(rmsr));
8780dc332bfSKa Ho Ng 		if (error == 0)
8790dc332bfSKa Ho Ng 			error = cerror;
8800dc332bfSKa Ho Ng 	}
8810dc332bfSKa Ho Ng 	return (error);
8820dc332bfSKa Ho Ng }
8830dc332bfSKa Ho Ng 
8840dc332bfSKa Ho Ng int
8850dc332bfSKa Ho Ng kern_fspacectl(struct thread *td, int fd, int cmd,
8860dc332bfSKa Ho Ng     const struct spacectl_range *rqsr, int flags, struct spacectl_range *rmsrp)
8870dc332bfSKa Ho Ng {
8880dc332bfSKa Ho Ng 	struct file *fp;
8890dc332bfSKa Ho Ng 	struct spacectl_range rmsr;
8900dc332bfSKa Ho Ng 	int error;
8910dc332bfSKa Ho Ng 
8920dc332bfSKa Ho Ng 	AUDIT_ARG_FD(fd);
8930dc332bfSKa Ho Ng 	AUDIT_ARG_CMD(cmd);
8940dc332bfSKa Ho Ng 	AUDIT_ARG_FFLAGS(flags);
8950dc332bfSKa Ho Ng 
8960dc332bfSKa Ho Ng 	if (rqsr == NULL)
8970dc332bfSKa Ho Ng 		return (EINVAL);
8980dc332bfSKa Ho Ng 	rmsr = *rqsr;
8990dc332bfSKa Ho Ng 	if (rmsrp != NULL)
9000dc332bfSKa Ho Ng 		*rmsrp = rmsr;
9010dc332bfSKa Ho Ng 
9020dc332bfSKa Ho Ng 	if (cmd != SPACECTL_DEALLOC ||
9030dc332bfSKa Ho Ng 	    rqsr->r_offset < 0 || rqsr->r_len <= 0 ||
9040dc332bfSKa Ho Ng 	    rqsr->r_offset > OFF_MAX - rqsr->r_len ||
9050dc332bfSKa Ho Ng 	    (flags & ~SPACECTL_F_SUPPORTED) != 0)
9060dc332bfSKa Ho Ng 		return (EINVAL);
9070dc332bfSKa Ho Ng 
9080dc332bfSKa Ho Ng 	error = fget_write(td, fd, &cap_pwrite_rights, &fp);
9090dc332bfSKa Ho Ng 	if (error != 0)
9100dc332bfSKa Ho Ng 		return (error);
9110dc332bfSKa Ho Ng 	AUDIT_ARG_FILE(td->td_proc, fp);
9120dc332bfSKa Ho Ng 	if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
9130dc332bfSKa Ho Ng 		error = ESPIPE;
9140dc332bfSKa Ho Ng 		goto out;
9150dc332bfSKa Ho Ng 	}
9160dc332bfSKa Ho Ng 	if ((fp->f_flag & FWRITE) == 0) {
9170dc332bfSKa Ho Ng 		error = EBADF;
9180dc332bfSKa Ho Ng 		goto out;
9190dc332bfSKa Ho Ng 	}
9200dc332bfSKa Ho Ng 
9210dc332bfSKa Ho Ng 	error = fo_fspacectl(fp, cmd, &rmsr.r_offset, &rmsr.r_len, flags,
9220dc332bfSKa Ho Ng 	    td->td_ucred, td);
9230dc332bfSKa Ho Ng 	/* fspacectl is not restarted after signals if the file is modified. */
9240dc332bfSKa Ho Ng 	if (rmsr.r_len != rqsr->r_len && (error == ERESTART ||
9250dc332bfSKa Ho Ng 	    error == EINTR || error == EWOULDBLOCK))
9260dc332bfSKa Ho Ng 		error = 0;
9270dc332bfSKa Ho Ng 	if (rmsrp != NULL)
9280dc332bfSKa Ho Ng 		*rmsrp = rmsr;
9290dc332bfSKa Ho Ng out:
9300dc332bfSKa Ho Ng 	fdrop(fp, td);
9310dc332bfSKa Ho Ng 	return (error);
9320dc332bfSKa Ho Ng }
9330dc332bfSKa Ho Ng 
9340dc332bfSKa Ho Ng int
9357a202823SKonstantin Belousov kern_specialfd(struct thread *td, int type, void *arg)
9367a202823SKonstantin Belousov {
9377a202823SKonstantin Belousov 	struct file *fp;
9387a202823SKonstantin Belousov 	struct specialfd_eventfd *ae;
9397a202823SKonstantin Belousov 	int error, fd, fflags;
9407a202823SKonstantin Belousov 
9417a202823SKonstantin Belousov 	fflags = 0;
9427a202823SKonstantin Belousov 	error = falloc_noinstall(td, &fp);
9437a202823SKonstantin Belousov 	if (error != 0)
9447a202823SKonstantin Belousov 		return (error);
9457a202823SKonstantin Belousov 
9467a202823SKonstantin Belousov 	switch (type) {
9477a202823SKonstantin Belousov 	case SPECIALFD_EVENTFD:
9487a202823SKonstantin Belousov 		ae = arg;
9497a202823SKonstantin Belousov 		if ((ae->flags & EFD_CLOEXEC) != 0)
9507a202823SKonstantin Belousov 			fflags |= O_CLOEXEC;
9517a202823SKonstantin Belousov 		error = eventfd_create_file(td, fp, ae->initval, ae->flags);
9527a202823SKonstantin Belousov 		break;
9537a202823SKonstantin Belousov 	default:
9547a202823SKonstantin Belousov 		error = EINVAL;
9557a202823SKonstantin Belousov 		break;
9567a202823SKonstantin Belousov 	}
9577a202823SKonstantin Belousov 
9587a202823SKonstantin Belousov 	if (error == 0)
9597a202823SKonstantin Belousov 		error = finstall(td, fp, &fd, fflags, NULL);
9607a202823SKonstantin Belousov 	fdrop(fp, td);
9617a202823SKonstantin Belousov 	if (error == 0)
9627a202823SKonstantin Belousov 		td->td_retval[0] = fd;
9637a202823SKonstantin Belousov 	return (error);
9647a202823SKonstantin Belousov }
9657a202823SKonstantin Belousov 
9667a202823SKonstantin Belousov int
9677a202823SKonstantin Belousov sys___specialfd(struct thread *td, struct __specialfd_args *args)
9687a202823SKonstantin Belousov {
9697a202823SKonstantin Belousov 	struct specialfd_eventfd ae;
9707a202823SKonstantin Belousov 	int error;
9717a202823SKonstantin Belousov 
9727a202823SKonstantin Belousov 	switch (args->type) {
9737a202823SKonstantin Belousov 	case SPECIALFD_EVENTFD:
9747a202823SKonstantin Belousov 		if (args->len != sizeof(struct specialfd_eventfd)) {
9757a202823SKonstantin Belousov 			error = EINVAL;
9767a202823SKonstantin Belousov 			break;
9777a202823SKonstantin Belousov 		}
9787a202823SKonstantin Belousov 		error = copyin(args->req, &ae, sizeof(ae));
9797a202823SKonstantin Belousov 		if (error != 0)
9807a202823SKonstantin Belousov 			break;
9817a202823SKonstantin Belousov 		if ((ae.flags & ~(EFD_CLOEXEC | EFD_NONBLOCK |
9827a202823SKonstantin Belousov 		    EFD_SEMAPHORE)) != 0) {
9837a202823SKonstantin Belousov 			error = EINVAL;
9847a202823SKonstantin Belousov 			break;
9857a202823SKonstantin Belousov 		}
9867a202823SKonstantin Belousov 		error = kern_specialfd(td, args->type, &ae);
9877a202823SKonstantin Belousov 		break;
9887a202823SKonstantin Belousov 	default:
9897a202823SKonstantin Belousov 		error = EINVAL;
9907a202823SKonstantin Belousov 		break;
9917a202823SKonstantin Belousov 	}
9927a202823SKonstantin Belousov 	return (error);
9937a202823SKonstantin Belousov }
9947a202823SKonstantin Belousov 
9957a202823SKonstantin Belousov int
996125dcf8cSKonstantin Belousov poll_no_poll(int events)
997125dcf8cSKonstantin Belousov {
998125dcf8cSKonstantin Belousov 	/*
999125dcf8cSKonstantin Belousov 	 * Return true for read/write.  If the user asked for something
1000125dcf8cSKonstantin Belousov 	 * special, return POLLNVAL, so that clients have a way of
1001125dcf8cSKonstantin Belousov 	 * determining reliably whether or not the extended
1002125dcf8cSKonstantin Belousov 	 * functionality is present without hard-coding knowledge
1003125dcf8cSKonstantin Belousov 	 * of specific filesystem implementations.
1004125dcf8cSKonstantin Belousov 	 */
1005125dcf8cSKonstantin Belousov 	if (events & ~POLLSTANDARD)
1006125dcf8cSKonstantin Belousov 		return (POLLNVAL);
1007125dcf8cSKonstantin Belousov 
1008125dcf8cSKonstantin Belousov 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1009125dcf8cSKonstantin Belousov }
1010125dcf8cSKonstantin Belousov 
1011066d836bSKonstantin Belousov int
10128451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap)
1013066d836bSKonstantin Belousov {
1014066d836bSKonstantin Belousov 	struct timespec ts;
1015066d836bSKonstantin Belousov 	struct timeval tv, *tvp;
1016066d836bSKonstantin Belousov 	sigset_t set, *uset;
1017066d836bSKonstantin Belousov 	int error;
1018066d836bSKonstantin Belousov 
1019066d836bSKonstantin Belousov 	if (uap->ts != NULL) {
1020066d836bSKonstantin Belousov 		error = copyin(uap->ts, &ts, sizeof(ts));
1021066d836bSKonstantin Belousov 		if (error != 0)
1022066d836bSKonstantin Belousov 		    return (error);
1023066d836bSKonstantin Belousov 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
1024066d836bSKonstantin Belousov 		tvp = &tv;
1025066d836bSKonstantin Belousov 	} else
1026066d836bSKonstantin Belousov 		tvp = NULL;
1027066d836bSKonstantin Belousov 	if (uap->sm != NULL) {
1028066d836bSKonstantin Belousov 		error = copyin(uap->sm, &set, sizeof(set));
1029066d836bSKonstantin Belousov 		if (error != 0)
1030066d836bSKonstantin Belousov 			return (error);
1031066d836bSKonstantin Belousov 		uset = &set;
1032066d836bSKonstantin Belousov 	} else
1033066d836bSKonstantin Belousov 		uset = NULL;
1034066d836bSKonstantin Belousov 	return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
1035066d836bSKonstantin Belousov 	    uset, NFDBITS));
1036066d836bSKonstantin Belousov }
1037066d836bSKonstantin Belousov 
1038066d836bSKonstantin Belousov int
1039066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
1040066d836bSKonstantin Belousov     struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
1041066d836bSKonstantin Belousov {
1042066d836bSKonstantin Belousov 	int error;
1043066d836bSKonstantin Belousov 
1044066d836bSKonstantin Belousov 	if (uset != NULL) {
1045066d836bSKonstantin Belousov 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
1046066d836bSKonstantin Belousov 		    &td->td_oldsigmask, 0);
1047066d836bSKonstantin Belousov 		if (error != 0)
1048066d836bSKonstantin Belousov 			return (error);
1049066d836bSKonstantin Belousov 		td->td_pflags |= TDP_OLDMASK;
1050066d836bSKonstantin Belousov 		/*
1051066d836bSKonstantin Belousov 		 * Make sure that ast() is called on return to
1052066d836bSKonstantin Belousov 		 * usermode and TDP_OLDMASK is cleared, restoring old
1053066d836bSKonstantin Belousov 		 * sigmask.
1054066d836bSKonstantin Belousov 		 */
1055066d836bSKonstantin Belousov 		thread_lock(td);
1056066d836bSKonstantin Belousov 		td->td_flags |= TDF_ASTPENDING;
1057066d836bSKonstantin Belousov 		thread_unlock(td);
1058066d836bSKonstantin Belousov 	}
1059066d836bSKonstantin Belousov 	error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
1060066d836bSKonstantin Belousov 	return (error);
1061066d836bSKonstantin Belousov }
1062066d836bSKonstantin Belousov 
1063d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
1064df8bae1dSRodney W. Grimes struct select_args {
1065b08f7993SSujal Patel 	int	nd;
1066df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
1067df8bae1dSRodney W. Grimes 	struct	timeval *tv;
1068df8bae1dSRodney W. Grimes };
1069d2d3e875SBruce Evans #endif
107026f9a767SRodney W. Grimes int
10718451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap)
1072df8bae1dSRodney W. Grimes {
10738f19eb88SIan Dowse 	struct timeval tv, *tvp;
10748f19eb88SIan Dowse 	int error;
10758f19eb88SIan Dowse 
10768f19eb88SIan Dowse 	if (uap->tv != NULL) {
10778f19eb88SIan Dowse 		error = copyin(uap->tv, &tv, sizeof(tv));
10788f19eb88SIan Dowse 		if (error)
10798f19eb88SIan Dowse 			return (error);
10808f19eb88SIan Dowse 		tvp = &tv;
10818f19eb88SIan Dowse 	} else
10828f19eb88SIan Dowse 		tvp = NULL;
10838f19eb88SIan Dowse 
1084b55ef216SKonstantin Belousov 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
1085b55ef216SKonstantin Belousov 	    NFDBITS));
10868f19eb88SIan Dowse }
10878f19eb88SIan Dowse 
108856be1b9aSKonstantin Belousov /*
108956be1b9aSKonstantin Belousov  * In the unlikely case when user specified n greater then the last
109056be1b9aSKonstantin Belousov  * open file descriptor, check that no bits are set after the last
109156be1b9aSKonstantin Belousov  * valid fd.  We must return EBADF if any is set.
109256be1b9aSKonstantin Belousov  *
109356be1b9aSKonstantin Belousov  * There are applications that rely on the behaviour.
109456be1b9aSKonstantin Belousov  *
1095d8bc2a17SMateusz Guzik  * nd is fd_nfiles.
109656be1b9aSKonstantin Belousov  */
109756be1b9aSKonstantin Belousov static int
109856be1b9aSKonstantin Belousov select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits)
109956be1b9aSKonstantin Belousov {
110056be1b9aSKonstantin Belousov 	char *addr, *oaddr;
110156be1b9aSKonstantin Belousov 	int b, i, res;
110256be1b9aSKonstantin Belousov 	uint8_t bits;
110356be1b9aSKonstantin Belousov 
110456be1b9aSKonstantin Belousov 	if (nd >= ndu || fd_in == NULL)
110556be1b9aSKonstantin Belousov 		return (0);
110656be1b9aSKonstantin Belousov 
110756be1b9aSKonstantin Belousov 	oaddr = NULL;
110856be1b9aSKonstantin Belousov 	bits = 0; /* silence gcc */
110956be1b9aSKonstantin Belousov 	for (i = nd; i < ndu; i++) {
111056be1b9aSKonstantin Belousov 		b = i / NBBY;
111156be1b9aSKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN
111256be1b9aSKonstantin Belousov 		addr = (char *)fd_in + b;
111356be1b9aSKonstantin Belousov #else
111456be1b9aSKonstantin Belousov 		addr = (char *)fd_in;
111556be1b9aSKonstantin Belousov 		if (abi_nfdbits == NFDBITS) {
111656be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(fd_mask)) +
111756be1b9aSKonstantin Belousov 			    sizeof(fd_mask) - 1 - b % sizeof(fd_mask);
111856be1b9aSKonstantin Belousov 		} else {
111956be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(uint32_t)) +
112056be1b9aSKonstantin Belousov 			    sizeof(uint32_t) - 1 - b % sizeof(uint32_t);
112156be1b9aSKonstantin Belousov 		}
112256be1b9aSKonstantin Belousov #endif
112356be1b9aSKonstantin Belousov 		if (addr != oaddr) {
112456be1b9aSKonstantin Belousov 			res = fubyte(addr);
112556be1b9aSKonstantin Belousov 			if (res == -1)
112656be1b9aSKonstantin Belousov 				return (EFAULT);
112756be1b9aSKonstantin Belousov 			oaddr = addr;
112856be1b9aSKonstantin Belousov 			bits = res;
112956be1b9aSKonstantin Belousov 		}
113056be1b9aSKonstantin Belousov 		if ((bits & (1 << (i % NBBY))) != 0)
113156be1b9aSKonstantin Belousov 			return (EBADF);
113256be1b9aSKonstantin Belousov 	}
113356be1b9aSKonstantin Belousov 	return (0);
113456be1b9aSKonstantin Belousov }
113556be1b9aSKonstantin Belousov 
11368f19eb88SIan Dowse int
11378f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
1138b55ef216SKonstantin Belousov     fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
11398f19eb88SIan Dowse {
1140426da3bcSAlfred Perlstein 	struct filedesc *fdp;
1141d5e4d7e1SBruce Evans 	/*
1142d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
1143d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
1144d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
1145d5e4d7e1SBruce Evans 	 * of 256.
1146d5e4d7e1SBruce Evans 	 */
1147d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
1148eb209311SAlfred Perlstein 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
1149cf5e4fe6SDavide Italiano 	struct timeval rtv;
1150cf5e4fe6SDavide Italiano 	sbintime_t asbt, precision, rsbt;
1151b55ef216SKonstantin Belousov 	u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
1152cf5e4fe6SDavide Italiano 	int error, lf, ndu;
1153df8bae1dSRodney W. Grimes 
11548f19eb88SIan Dowse 	if (nd < 0)
1155acbfbfeaSSujal Patel 		return (EINVAL);
1156426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
115756be1b9aSKonstantin Belousov 	ndu = nd;
1158d8bc2a17SMateusz Guzik 	lf = fdp->fd_nfiles;
1159d8bc2a17SMateusz Guzik 	if (nd > lf)
1160d8bc2a17SMateusz Guzik 		nd = lf;
116156be1b9aSKonstantin Belousov 
116256be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits);
116356be1b9aSKonstantin Belousov 	if (error != 0)
116456be1b9aSKonstantin Belousov 		return (error);
116556be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits);
116656be1b9aSKonstantin Belousov 	if (error != 0)
116756be1b9aSKonstantin Belousov 		return (error);
116856be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits);
116956be1b9aSKonstantin Belousov 	if (error != 0)
117056be1b9aSKonstantin Belousov 		return (error);
1171b08f7993SSujal Patel 
1172d5e4d7e1SBruce Evans 	/*
1173d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
1174d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
1175d5e4d7e1SBruce Evans 	 */
11768f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
1177d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
1178b55ef216SKonstantin Belousov 	ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
1179d5e4d7e1SBruce Evans 	nbufbytes = 0;
11808f19eb88SIan Dowse 	if (fd_in != NULL)
1181d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
11828f19eb88SIan Dowse 	if (fd_ou != NULL)
1183d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
11848f19eb88SIan Dowse 	if (fd_ex != NULL)
1185d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
1186d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
1187d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
1188d5e4d7e1SBruce Evans 	else
1189a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
1190b08f7993SSujal Patel 
1191b08f7993SSujal Patel 	/*
1192d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
1193d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
1194d5e4d7e1SBruce Evans 	 * together.
1195b08f7993SSujal Patel 	 */
1196d5e4d7e1SBruce Evans 	sbp = selbits;
1197df8bae1dSRodney W. Grimes #define	getbits(name, x) \
1198d5e4d7e1SBruce Evans 	do {								\
1199841c0c7eSNathan Whitehorn 		if (name == NULL) {					\
1200d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
1201841c0c7eSNathan Whitehorn 			obits[x] = NULL;				\
1202841c0c7eSNathan Whitehorn 		} else {						\
1203d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
1204d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
1205d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
1206b55ef216SKonstantin Belousov 			error = copyin(name, ibits[x], ncpubytes);	\
1207265fc98fSSeigo Tanimura 			if (error != 0)					\
1208ace8398dSJeff Roberson 				goto done;				\
1209d6fda03aSMateusz Guzik 			if (ncpbytes != ncpubytes)			\
1210b55ef216SKonstantin Belousov 				bzero((char *)ibits[x] + ncpubytes,	\
1211b55ef216SKonstantin Belousov 				    ncpbytes - ncpubytes);		\
1212e04ac2feSJohn Baldwin 		}							\
1213d5e4d7e1SBruce Evans 	} while (0)
12148f19eb88SIan Dowse 	getbits(fd_in, 0);
12158f19eb88SIan Dowse 	getbits(fd_ou, 1);
12168f19eb88SIan Dowse 	getbits(fd_ex, 2);
1217df8bae1dSRodney W. Grimes #undef	getbits
1218841c0c7eSNathan Whitehorn 
1219841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
1220841c0c7eSNathan Whitehorn 	/*
1221841c0c7eSNathan Whitehorn 	 * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
1222841c0c7eSNathan Whitehorn 	 * we are running under 32-bit emulation. This should be more
1223841c0c7eSNathan Whitehorn 	 * generic.
1224841c0c7eSNathan Whitehorn 	 */
1225841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)						\
1226841c0c7eSNathan Whitehorn 	if (abi_nfdbits != NFDBITS && bits != NULL) {			\
1227841c0c7eSNathan Whitehorn 		int i;							\
1228841c0c7eSNathan Whitehorn 		for (i = 0; i < ncpbytes / sizeof *sbp; i++)		\
1229841c0c7eSNathan Whitehorn 			bits[i] = (bits[i] >> 32) | (bits[i] << 32);	\
1230841c0c7eSNathan Whitehorn 	}
1231841c0c7eSNathan Whitehorn #else
1232841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)
1233841c0c7eSNathan Whitehorn #endif
1234841c0c7eSNathan Whitehorn 
1235841c0c7eSNathan Whitehorn 	/* Make sure the bit order makes it through an ABI transition */
1236841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[0]);
1237841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[1]);
1238841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[2]);
1239841c0c7eSNathan Whitehorn 
1240d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
1241d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
1242df8bae1dSRodney W. Grimes 
1243cf5e4fe6SDavide Italiano 	precision = 0;
12448f19eb88SIan Dowse 	if (tvp != NULL) {
1245cf5e4fe6SDavide Italiano 		rtv = *tvp;
1246cf5e4fe6SDavide Italiano 		if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1247cf5e4fe6SDavide Italiano 		    rtv.tv_usec >= 1000000) {
1248df8bae1dSRodney W. Grimes 			error = EINVAL;
1249ace8398dSJeff Roberson 			goto done;
1250df8bae1dSRodney W. Grimes 		}
125121a37a71SAlexander Motin 		if (!timevalisset(&rtv))
1252980c545dSAlexander Motin 			asbt = 0;
125321a37a71SAlexander Motin 		else if (rtv.tv_sec <= INT32_MAX) {
1254cf5e4fe6SDavide Italiano 			rsbt = tvtosbt(rtv);
1255cf5e4fe6SDavide Italiano 			precision = rsbt;
1256cf5e4fe6SDavide Italiano 			precision >>= tc_precexp;
1257cf5e4fe6SDavide Italiano 			if (TIMESEL(&asbt, rsbt))
1258cf5e4fe6SDavide Italiano 				asbt += tc_tick_sbt;
12594bc38a5aSDavide Italiano 			if (asbt <= SBT_MAX - rsbt)
1260cf5e4fe6SDavide Italiano 				asbt += rsbt;
126121a37a71SAlexander Motin 			else
1262980c545dSAlexander Motin 				asbt = -1;
1263980c545dSAlexander Motin 		} else
1264980c545dSAlexander Motin 			asbt = -1;
1265cf5e4fe6SDavide Italiano 	} else
1266cf5e4fe6SDavide Italiano 		asbt = -1;
1267ace8398dSJeff Roberson 	seltdinit(td);
1268ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1269ace8398dSJeff Roberson 	for (;;) {
12708f19eb88SIan Dowse 		error = selscan(td, ibits, obits, nd);
1271ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1272ace8398dSJeff Roberson 			break;
1273cf5e4fe6SDavide Italiano 		error = seltdwait(td, asbt, precision);
1274ace8398dSJeff Roberson 		if (error)
1275ace8398dSJeff Roberson 			break;
1276ace8398dSJeff Roberson 		error = selrescan(td, ibits, obits);
1277ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1278ace8398dSJeff Roberson 			break;
127985f190e4SAlfred Perlstein 	}
1280ace8398dSJeff Roberson 	seltdclear(td);
1281265fc98fSSeigo Tanimura 
1282df8bae1dSRodney W. Grimes done:
1283df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
1284df8bae1dSRodney W. Grimes 	if (error == ERESTART)
1285df8bae1dSRodney W. Grimes 		error = EINTR;
1286df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
1287df8bae1dSRodney W. Grimes 		error = 0;
1288841c0c7eSNathan Whitehorn 
1289841c0c7eSNathan Whitehorn 	/* swizzle bit order back, if necessary */
1290841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[0]);
1291841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[1]);
1292841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[2]);
1293841c0c7eSNathan Whitehorn #undef swizzle_fdset
1294841c0c7eSNathan Whitehorn 
1295df8bae1dSRodney W. Grimes #define	putbits(name, x) \
1296b55ef216SKonstantin Belousov 	if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
1297df8bae1dSRodney W. Grimes 		error = error2;
1298df8bae1dSRodney W. Grimes 	if (error == 0) {
1299df8bae1dSRodney W. Grimes 		int error2;
1300df8bae1dSRodney W. Grimes 
13018f19eb88SIan Dowse 		putbits(fd_in, 0);
13028f19eb88SIan Dowse 		putbits(fd_ou, 1);
13038f19eb88SIan Dowse 		putbits(fd_ex, 2);
1304df8bae1dSRodney W. Grimes #undef putbits
1305df8bae1dSRodney W. Grimes 	}
1306d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
1307d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
1308ad2edad9SMatthew Dillon 
1309df8bae1dSRodney W. Grimes 	return (error);
1310df8bae1dSRodney W. Grimes }
131111b763dfSJeff Roberson /*
131211b763dfSJeff Roberson  * Convert a select bit set to poll flags.
1313748b9df6SJeff Roberson  *
131411b763dfSJeff Roberson  * The backend always returns POLLHUP/POLLERR if appropriate and we
131511b763dfSJeff Roberson  * return this as a set bit in any set.
131611b763dfSJeff Roberson  */
1317cae3f9ddSMark Johnston static const int select_flags[3] = {
131811b763dfSJeff Roberson     POLLRDNORM | POLLHUP | POLLERR,
131911b763dfSJeff Roberson     POLLWRNORM | POLLHUP | POLLERR,
132061e53a38SKonstantin Belousov     POLLRDBAND | POLLERR
132111b763dfSJeff Roberson };
132211b763dfSJeff Roberson 
132311b763dfSJeff Roberson /*
132411b763dfSJeff Roberson  * Compute the fo_poll flags required for a fd given by the index and
132511b763dfSJeff Roberson  * bit position in the fd_mask array.
132611b763dfSJeff Roberson  */
132711b763dfSJeff Roberson static __inline int
132860b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit)
132911b763dfSJeff Roberson {
133011b763dfSJeff Roberson 	int flags;
133111b763dfSJeff Roberson 	int msk;
133211b763dfSJeff Roberson 
133311b763dfSJeff Roberson 	flags = 0;
133411b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
133511b763dfSJeff Roberson 		if (ibits[msk] == NULL)
133611b763dfSJeff Roberson 			continue;
133760b7f468SStephane E. Potvin 		if ((ibits[msk][idx] & bit) == 0)
133811b763dfSJeff Roberson 			continue;
133911b763dfSJeff Roberson 		flags |= select_flags[msk];
134011b763dfSJeff Roberson 	}
134111b763dfSJeff Roberson 	return (flags);
134211b763dfSJeff Roberson }
134311b763dfSJeff Roberson 
134411b763dfSJeff Roberson /*
134511b763dfSJeff Roberson  * Set the appropriate output bits given a mask of fired events and the
134611b763dfSJeff Roberson  * input bits originally requested.
134711b763dfSJeff Roberson  */
134811b763dfSJeff Roberson static __inline int
134911b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
135011b763dfSJeff Roberson {
135111b763dfSJeff Roberson 	int msk;
135211b763dfSJeff Roberson 	int n;
135311b763dfSJeff Roberson 
135411b763dfSJeff Roberson 	n = 0;
135511b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
135611b763dfSJeff Roberson 		if ((events & select_flags[msk]) == 0)
135711b763dfSJeff Roberson 			continue;
135811b763dfSJeff Roberson 		if (ibits[msk] == NULL)
135911b763dfSJeff Roberson 			continue;
136011b763dfSJeff Roberson 		if ((ibits[msk][idx] & bit) == 0)
136111b763dfSJeff Roberson 			continue;
136211b763dfSJeff Roberson 		/*
136311b763dfSJeff Roberson 		 * XXX Check for a duplicate set.  This can occur because a
136411b763dfSJeff Roberson 		 * socket calls selrecord() twice for each poll() call
136511b763dfSJeff Roberson 		 * resulting in two selfds per real fd.  selrescan() will
136611b763dfSJeff Roberson 		 * call selsetbits twice as a result.
136711b763dfSJeff Roberson 		 */
136811b763dfSJeff Roberson 		if ((obits[msk][idx] & bit) != 0)
136911b763dfSJeff Roberson 			continue;
137011b763dfSJeff Roberson 		obits[msk][idx] |= bit;
137111b763dfSJeff Roberson 		n++;
137211b763dfSJeff Roberson 	}
137311b763dfSJeff Roberson 
137411b763dfSJeff Roberson 	return (n);
137511b763dfSJeff Roberson }
1376df8bae1dSRodney W. Grimes 
1377ace8398dSJeff Roberson /*
1378ace8398dSJeff Roberson  * Traverse the list of fds attached to this thread's seltd and check for
1379ace8398dSJeff Roberson  * completion.
1380ace8398dSJeff Roberson  */
1381ace8398dSJeff Roberson static int
1382ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
1383ace8398dSJeff Roberson {
138411b763dfSJeff Roberson 	struct filedesc *fdp;
138511b763dfSJeff Roberson 	struct selinfo *si;
1386ace8398dSJeff Roberson 	struct seltd *stp;
1387ace8398dSJeff Roberson 	struct selfd *sfp;
1388ace8398dSJeff Roberson 	struct selfd *sfn;
1389ace8398dSJeff Roberson 	struct file *fp;
13909cdacff1SJeff Roberson 	fd_mask bit;
13919cdacff1SJeff Roberson 	int fd, ev, n, idx;
1392a9d2f8d8SRobert Watson 	int error;
13936affe1b7SMateusz Guzik 	bool only_user;
1394ace8398dSJeff Roberson 
139511b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
1396ace8398dSJeff Roberson 	stp = td->td_sel;
139711b763dfSJeff Roberson 	n = 0;
13986affe1b7SMateusz Guzik 	only_user = FILEDESC_IS_ONLY_USER(fdp);
1399ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1400ace8398dSJeff Roberson 		fd = (int)(uintptr_t)sfp->sf_cookie;
1401ace8398dSJeff Roberson 		si = sfp->sf_si;
1402ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1403ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1404ace8398dSJeff Roberson 		if (si != NULL)
1405ace8398dSJeff Roberson 			continue;
14066affe1b7SMateusz Guzik 		if (only_user)
14076affe1b7SMateusz Guzik 			error = fget_only_user(fdp, fd, &cap_event_rights, &fp);
14086affe1b7SMateusz Guzik 		else
14096affe1b7SMateusz Guzik 			error = fget_unlocked(fdp, fd, &cap_event_rights, &fp);
14106affe1b7SMateusz Guzik 		if (__predict_false(error != 0))
1411a9d2f8d8SRobert Watson 			return (error);
141211b763dfSJeff Roberson 		idx = fd / NFDBITS;
14139cdacff1SJeff Roberson 		bit = (fd_mask)1 << (fd % NFDBITS);
141411b763dfSJeff Roberson 		ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
14156affe1b7SMateusz Guzik 		if (only_user)
14166affe1b7SMateusz Guzik 			fput_only_user(fdp, fp);
14176affe1b7SMateusz Guzik 		else
1418bf422e5fSJeff Roberson 			fdrop(fp, td);
141911b763dfSJeff Roberson 		if (ev != 0)
142011b763dfSJeff Roberson 			n += selsetbits(ibits, obits, idx, bit, ev);
1421ace8398dSJeff Roberson 	}
1422ace8398dSJeff Roberson 	stp->st_flags = 0;
1423ace8398dSJeff Roberson 	td->td_retval[0] = n;
1424ace8398dSJeff Roberson 	return (0);
1425ace8398dSJeff Roberson }
1426ace8398dSJeff Roberson 
1427ace8398dSJeff Roberson /*
1428ace8398dSJeff Roberson  * Perform the initial filedescriptor scan and register ourselves with
1429ace8398dSJeff Roberson  * each selinfo.
1430ace8398dSJeff Roberson  */
1431265fc98fSSeigo Tanimura static int
1432cc3c9df8SEd Maste selscan(struct thread *td, fd_mask **ibits, fd_mask **obits, int nfd)
1433df8bae1dSRodney W. Grimes {
143411b763dfSJeff Roberson 	struct filedesc *fdp;
1435df8bae1dSRodney W. Grimes 	struct file *fp;
14369cdacff1SJeff Roberson 	fd_mask bit;
143711b763dfSJeff Roberson 	int ev, flags, end, fd;
14389cdacff1SJeff Roberson 	int n, idx;
1439a9d2f8d8SRobert Watson 	int error;
14406affe1b7SMateusz Guzik 	bool only_user;
1441df8bae1dSRodney W. Grimes 
144211b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
144311b763dfSJeff Roberson 	n = 0;
14446affe1b7SMateusz Guzik 	only_user = FILEDESC_IS_ONLY_USER(fdp);
14459cdacff1SJeff Roberson 	for (idx = 0, fd = 0; fd < nfd; idx++) {
144611b763dfSJeff Roberson 		end = imin(fd + NFDBITS, nfd);
144711b763dfSJeff Roberson 		for (bit = 1; fd < end; bit <<= 1, fd++) {
144811b763dfSJeff Roberson 			/* Compute the list of events we're interested in. */
144911b763dfSJeff Roberson 			flags = selflags(ibits, idx, bit);
145011b763dfSJeff Roberson 			if (flags == 0)
1451f082218cSPeter Wemm 				continue;
14526affe1b7SMateusz Guzik 			if (only_user)
14536affe1b7SMateusz Guzik 				error = fget_only_user(fdp, fd, &cap_event_rights, &fp);
14546affe1b7SMateusz Guzik 			else
14556affe1b7SMateusz Guzik 				error = fget_unlocked(fdp, fd, &cap_event_rights, &fp);
14566affe1b7SMateusz Guzik 			if (__predict_false(error != 0))
1457a9d2f8d8SRobert Watson 				return (error);
1458ace8398dSJeff Roberson 			selfdalloc(td, (void *)(uintptr_t)fd);
145911b763dfSJeff Roberson 			ev = fo_poll(fp, flags, td->td_ucred, td);
14606affe1b7SMateusz Guzik 			if (only_user)
14616affe1b7SMateusz Guzik 				fput_only_user(fdp, fp);
14626affe1b7SMateusz Guzik 			else
1463bf422e5fSJeff Roberson 				fdrop(fp, td);
146411b763dfSJeff Roberson 			if (ev != 0)
146511b763dfSJeff Roberson 				n += selsetbits(ibits, obits, idx, bit, ev);
1466df8bae1dSRodney W. Grimes 		}
1467df8bae1dSRodney W. Grimes 	}
146811b763dfSJeff Roberson 
1469b40ce416SJulian Elischer 	td->td_retval[0] = n;
1470df8bae1dSRodney W. Grimes 	return (0);
1471df8bae1dSRodney W. Grimes }
1472df8bae1dSRodney W. Grimes 
147342d11757SPeter Wemm int
1474186d9c34SDmitry Chagin sys_poll(struct thread *td, struct poll_args *uap)
1475186d9c34SDmitry Chagin {
1476186d9c34SDmitry Chagin 	struct timespec ts, *tsp;
1477186d9c34SDmitry Chagin 
1478186d9c34SDmitry Chagin 	if (uap->timeout != INFTIM) {
1479186d9c34SDmitry Chagin 		if (uap->timeout < 0)
1480186d9c34SDmitry Chagin 			return (EINVAL);
1481186d9c34SDmitry Chagin 		ts.tv_sec = uap->timeout / 1000;
1482186d9c34SDmitry Chagin 		ts.tv_nsec = (uap->timeout % 1000) * 1000000;
1483186d9c34SDmitry Chagin 		tsp = &ts;
1484186d9c34SDmitry Chagin 	} else
1485186d9c34SDmitry Chagin 		tsp = NULL;
1486186d9c34SDmitry Chagin 
1487186d9c34SDmitry Chagin 	return (kern_poll(td, uap->fds, uap->nfds, tsp, NULL));
1488186d9c34SDmitry Chagin }
1489186d9c34SDmitry Chagin 
1490e884512aSDmitry Chagin /*
1491e884512aSDmitry Chagin  * kfds points to an array in the kernel.
1492e884512aSDmitry Chagin  */
1493186d9c34SDmitry Chagin int
1494e884512aSDmitry Chagin kern_poll_kfds(struct thread *td, struct pollfd *kfds, u_int nfds,
1495186d9c34SDmitry Chagin     struct timespec *tsp, sigset_t *uset)
149642d11757SPeter Wemm {
1497186d9c34SDmitry Chagin 	sbintime_t sbt, precision, tmp;
1498186d9c34SDmitry Chagin 	time_t over;
1499186d9c34SDmitry Chagin 	struct timespec ts;
1500cf5e4fe6SDavide Italiano 	int error;
150142d11757SPeter Wemm 
1502186d9c34SDmitry Chagin 	precision = 0;
1503186d9c34SDmitry Chagin 	if (tsp != NULL) {
1504186d9c34SDmitry Chagin 		if (tsp->tv_sec < 0)
1505186d9c34SDmitry Chagin 			return (EINVAL);
1506186d9c34SDmitry Chagin 		if (tsp->tv_nsec < 0 || tsp->tv_nsec >= 1000000000)
1507186d9c34SDmitry Chagin 			return (EINVAL);
1508186d9c34SDmitry Chagin 		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1509186d9c34SDmitry Chagin 			sbt = 0;
1510186d9c34SDmitry Chagin 		else {
1511186d9c34SDmitry Chagin 			ts = *tsp;
1512186d9c34SDmitry Chagin 			if (ts.tv_sec > INT32_MAX / 2) {
1513186d9c34SDmitry Chagin 				over = ts.tv_sec - INT32_MAX / 2;
1514186d9c34SDmitry Chagin 				ts.tv_sec -= over;
1515186d9c34SDmitry Chagin 			} else
1516186d9c34SDmitry Chagin 				over = 0;
1517186d9c34SDmitry Chagin 			tmp = tstosbt(ts);
1518186d9c34SDmitry Chagin 			precision = tmp;
1519186d9c34SDmitry Chagin 			precision >>= tc_precexp;
1520186d9c34SDmitry Chagin 			if (TIMESEL(&sbt, tmp))
1521186d9c34SDmitry Chagin 				sbt += tc_tick_sbt;
1522186d9c34SDmitry Chagin 			sbt += tmp;
1523186d9c34SDmitry Chagin 		}
1524186d9c34SDmitry Chagin 	} else
1525186d9c34SDmitry Chagin 		sbt = -1;
1526186d9c34SDmitry Chagin 
1527186d9c34SDmitry Chagin 	if (uset != NULL) {
1528186d9c34SDmitry Chagin 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
1529186d9c34SDmitry Chagin 		    &td->td_oldsigmask, 0);
1530186d9c34SDmitry Chagin 		if (error)
1531e884512aSDmitry Chagin 			return (error);
1532186d9c34SDmitry Chagin 		td->td_pflags |= TDP_OLDMASK;
1533186d9c34SDmitry Chagin 		/*
1534186d9c34SDmitry Chagin 		 * Make sure that ast() is called on return to
1535186d9c34SDmitry Chagin 		 * usermode and TDP_OLDMASK is cleared, restoring old
1536186d9c34SDmitry Chagin 		 * sigmask.
1537186d9c34SDmitry Chagin 		 */
1538186d9c34SDmitry Chagin 		thread_lock(td);
1539186d9c34SDmitry Chagin 		td->td_flags |= TDF_ASTPENDING;
1540186d9c34SDmitry Chagin 		thread_unlock(td);
154142d11757SPeter Wemm 	}
1542186d9c34SDmitry Chagin 
1543ace8398dSJeff Roberson 	seltdinit(td);
1544ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1545ace8398dSJeff Roberson 	for (;;) {
15462384981bSConrad Meyer 		error = pollscan(td, kfds, nfds);
1547ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1548ace8398dSJeff Roberson 			break;
1549186d9c34SDmitry Chagin 		error = seltdwait(td, sbt, precision);
1550ace8398dSJeff Roberson 		if (error)
1551ace8398dSJeff Roberson 			break;
1552ace8398dSJeff Roberson 		error = pollrescan(td);
1553ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1554ace8398dSJeff Roberson 			break;
155585f190e4SAlfred Perlstein 	}
1556ace8398dSJeff Roberson 	seltdclear(td);
1557265fc98fSSeigo Tanimura 
155842d11757SPeter Wemm 	/* poll is not restarted after signals... */
155942d11757SPeter Wemm 	if (error == ERESTART)
156042d11757SPeter Wemm 		error = EINTR;
156142d11757SPeter Wemm 	if (error == EWOULDBLOCK)
156242d11757SPeter Wemm 		error = 0;
156342d11757SPeter Wemm 	return (error);
156442d11757SPeter Wemm }
156542d11757SPeter Wemm 
1566186d9c34SDmitry Chagin int
1567186d9c34SDmitry Chagin sys_ppoll(struct thread *td, struct ppoll_args *uap)
1568186d9c34SDmitry Chagin {
1569186d9c34SDmitry Chagin 	struct timespec ts, *tsp;
1570186d9c34SDmitry Chagin 	sigset_t set, *ssp;
1571186d9c34SDmitry Chagin 	int error;
1572186d9c34SDmitry Chagin 
1573186d9c34SDmitry Chagin 	if (uap->ts != NULL) {
1574186d9c34SDmitry Chagin 		error = copyin(uap->ts, &ts, sizeof(ts));
1575186d9c34SDmitry Chagin 		if (error)
1576186d9c34SDmitry Chagin 			return (error);
1577186d9c34SDmitry Chagin 		tsp = &ts;
1578186d9c34SDmitry Chagin 	} else
1579186d9c34SDmitry Chagin 		tsp = NULL;
1580186d9c34SDmitry Chagin 	if (uap->set != NULL) {
1581186d9c34SDmitry Chagin 		error = copyin(uap->set, &set, sizeof(set));
1582186d9c34SDmitry Chagin 		if (error)
1583186d9c34SDmitry Chagin 			return (error);
1584186d9c34SDmitry Chagin 		ssp = &set;
1585186d9c34SDmitry Chagin 	} else
1586186d9c34SDmitry Chagin 		ssp = NULL;
1587186d9c34SDmitry Chagin 	return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
1588186d9c34SDmitry Chagin }
1589186d9c34SDmitry Chagin 
1590e884512aSDmitry Chagin /*
1591e884512aSDmitry Chagin  * ufds points to an array in user space.
1592e884512aSDmitry Chagin  */
1593e884512aSDmitry Chagin int
1594e884512aSDmitry Chagin kern_poll(struct thread *td, struct pollfd *ufds, u_int nfds,
1595e884512aSDmitry Chagin     struct timespec *tsp, sigset_t *set)
1596e884512aSDmitry Chagin {
1597e884512aSDmitry Chagin 	struct pollfd *kfds;
1598e884512aSDmitry Chagin 	struct pollfd stackfds[32];
1599e884512aSDmitry Chagin 	int error;
1600e884512aSDmitry Chagin 
1601e884512aSDmitry Chagin 	if (kern_poll_maxfds(nfds))
1602e884512aSDmitry Chagin 		return (EINVAL);
1603e884512aSDmitry Chagin 	if (nfds > nitems(stackfds))
1604e884512aSDmitry Chagin 		kfds = mallocarray(nfds, sizeof(*kfds), M_TEMP, M_WAITOK);
1605e884512aSDmitry Chagin 	else
1606e884512aSDmitry Chagin 		kfds = stackfds;
1607e884512aSDmitry Chagin 	error = copyin(ufds, kfds, nfds * sizeof(*kfds));
1608e884512aSDmitry Chagin 	if (error != 0)
1609e884512aSDmitry Chagin 		goto out;
1610e884512aSDmitry Chagin 
1611e884512aSDmitry Chagin 	error = kern_poll_kfds(td, kfds, nfds, tsp, set);
1612e884512aSDmitry Chagin 	if (error == 0)
1613e884512aSDmitry Chagin 		error = pollout(td, kfds, ufds, nfds);
1614e884512aSDmitry Chagin 
1615e884512aSDmitry Chagin out:
1616e884512aSDmitry Chagin 	if (nfds > nitems(stackfds))
1617e884512aSDmitry Chagin 		free(kfds, M_TEMP);
1618e884512aSDmitry Chagin 	return (error);
1619e884512aSDmitry Chagin }
1620e884512aSDmitry Chagin 
1621e884512aSDmitry Chagin bool
1622e884512aSDmitry Chagin kern_poll_maxfds(u_int nfds)
1623e884512aSDmitry Chagin {
1624e884512aSDmitry Chagin 
1625e884512aSDmitry Chagin 	/*
1626e884512aSDmitry Chagin 	 * This is kinda bogus.  We have fd limits, but that is not
1627e884512aSDmitry Chagin 	 * really related to the size of the pollfd array.  Make sure
1628e884512aSDmitry Chagin 	 * we let the process use at least FD_SETSIZE entries and at
1629e884512aSDmitry Chagin 	 * least enough for the system-wide limits.  We want to be reasonably
1630e884512aSDmitry Chagin 	 * safe, but not overly restrictive.
1631e884512aSDmitry Chagin 	 */
1632e884512aSDmitry Chagin 	return (nfds > maxfilesperproc && nfds > FD_SETSIZE);
1633e884512aSDmitry Chagin }
1634e884512aSDmitry Chagin 
163542d11757SPeter Wemm static int
1636ace8398dSJeff Roberson pollrescan(struct thread *td)
1637ace8398dSJeff Roberson {
1638ace8398dSJeff Roberson 	struct seltd *stp;
1639ace8398dSJeff Roberson 	struct selfd *sfp;
1640ace8398dSJeff Roberson 	struct selfd *sfn;
1641ace8398dSJeff Roberson 	struct selinfo *si;
1642ace8398dSJeff Roberson 	struct filedesc *fdp;
1643ace8398dSJeff Roberson 	struct file *fp;
1644ace8398dSJeff Roberson 	struct pollfd *fd;
164545e1f854SMateusz Guzik 	int n, error;
164645e1f854SMateusz Guzik 	bool only_user;
1647ace8398dSJeff Roberson 
1648ace8398dSJeff Roberson 	n = 0;
1649ace8398dSJeff Roberson 	fdp = td->td_proc->p_fd;
1650ace8398dSJeff Roberson 	stp = td->td_sel;
165145e1f854SMateusz Guzik 	only_user = FILEDESC_IS_ONLY_USER(fdp);
1652ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1653ace8398dSJeff Roberson 		fd = (struct pollfd *)sfp->sf_cookie;
1654ace8398dSJeff Roberson 		si = sfp->sf_si;
1655ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1656ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1657ace8398dSJeff Roberson 		if (si != NULL)
1658ace8398dSJeff Roberson 			continue;
165945e1f854SMateusz Guzik 		if (only_user)
166045e1f854SMateusz Guzik 			error = fget_only_user(fdp, fd->fd, &cap_event_rights, &fp);
166145e1f854SMateusz Guzik 		else
166245e1f854SMateusz Guzik 			error = fget_unlocked(fdp, fd->fd, &cap_event_rights, &fp);
166345e1f854SMateusz Guzik 		if (__predict_false(error != 0)) {
1664ace8398dSJeff Roberson 			fd->revents = POLLNVAL;
1665ace8398dSJeff Roberson 			n++;
1666ace8398dSJeff Roberson 			continue;
1667ace8398dSJeff Roberson 		}
1668ace8398dSJeff Roberson 		/*
1669ace8398dSJeff Roberson 		 * Note: backend also returns POLLHUP and
1670ace8398dSJeff Roberson 		 * POLLERR if appropriate.
1671ace8398dSJeff Roberson 		 */
1672ace8398dSJeff Roberson 		fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
167345e1f854SMateusz Guzik 		if (only_user)
167445e1f854SMateusz Guzik 			fput_only_user(fdp, fp);
167545e1f854SMateusz Guzik 		else
167645e1f854SMateusz Guzik 			fdrop(fp, td);
1677ace8398dSJeff Roberson 		if (fd->revents != 0)
1678ace8398dSJeff Roberson 			n++;
1679ace8398dSJeff Roberson 	}
1680ace8398dSJeff Roberson 	stp->st_flags = 0;
1681ace8398dSJeff Roberson 	td->td_retval[0] = n;
1682ace8398dSJeff Roberson 	return (0);
1683ace8398dSJeff Roberson }
1684ace8398dSJeff Roberson 
1685ace8398dSJeff Roberson static int
1686cc3c9df8SEd Maste pollout(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd)
1687ae81968fSRobert Watson {
1688ae81968fSRobert Watson 	int error = 0;
1689ae81968fSRobert Watson 	u_int i = 0;
16906d8feddaSKonstantin Belousov 	u_int n = 0;
1691ae81968fSRobert Watson 
1692ae81968fSRobert Watson 	for (i = 0; i < nfd; i++) {
1693ae81968fSRobert Watson 		error = copyout(&fds->revents, &ufds->revents,
1694ae81968fSRobert Watson 		    sizeof(ufds->revents));
1695ae81968fSRobert Watson 		if (error)
1696ae81968fSRobert Watson 			return (error);
16976d8feddaSKonstantin Belousov 		if (fds->revents != 0)
16986d8feddaSKonstantin Belousov 			n++;
1699ae81968fSRobert Watson 		fds++;
1700ae81968fSRobert Watson 		ufds++;
1701ae81968fSRobert Watson 	}
17026d8feddaSKonstantin Belousov 	td->td_retval[0] = n;
1703ae81968fSRobert Watson 	return (0);
1704ae81968fSRobert Watson }
1705ae81968fSRobert Watson 
1706ae81968fSRobert Watson static int
1707cc3c9df8SEd Maste pollscan(struct thread *td, struct pollfd *fds, u_int nfd)
170842d11757SPeter Wemm {
1709b1607c87SMateusz Guzik 	struct filedesc *fdp;
171042d11757SPeter Wemm 	struct file *fp;
171145e1f854SMateusz Guzik 	int i, n, error;
171245e1f854SMateusz Guzik 	bool only_user;
171342d11757SPeter Wemm 
1714b1607c87SMateusz Guzik 	n = 0;
1715b1607c87SMateusz Guzik 	fdp = td->td_proc->p_fd;
171645e1f854SMateusz Guzik 	only_user = FILEDESC_IS_ONLY_USER(fdp);
1717eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
1718b1607c87SMateusz Guzik 		if (fds->fd < 0) {
1719337c9691SJordan K. Hubbard 			fds->revents = 0;
1720b1607c87SMateusz Guzik 			continue;
1721b1607c87SMateusz Guzik 		}
172245e1f854SMateusz Guzik 		if (only_user)
172345e1f854SMateusz Guzik 			error = fget_only_user(fdp, fds->fd, &cap_event_rights, &fp);
172445e1f854SMateusz Guzik 		else
172545e1f854SMateusz Guzik 			error = fget_unlocked(fdp, fds->fd, &cap_event_rights, &fp);
172645e1f854SMateusz Guzik 		if (__predict_false(error != 0)) {
172742d11757SPeter Wemm 			fds->revents = POLLNVAL;
172842d11757SPeter Wemm 			n++;
1729b1607c87SMateusz Guzik 			continue;
1730b1607c87SMateusz Guzik 		}
17312087c896SBruce Evans 		/*
17322087c896SBruce Evans 		 * Note: backend also returns POLLHUP and
17332087c896SBruce Evans 		 * POLLERR if appropriate.
17342087c896SBruce Evans 		 */
1735ace8398dSJeff Roberson 		selfdalloc(td, fds);
173613ccadd4SBrian Feldman 		fds->revents = fo_poll(fp, fds->events,
1737ea6027a8SRobert Watson 		    td->td_ucred, td);
173845e1f854SMateusz Guzik 		if (only_user)
173945e1f854SMateusz Guzik 			fput_only_user(fdp, fp);
174045e1f854SMateusz Guzik 		else
174145e1f854SMateusz Guzik 			fdrop(fp, td);
1742f2159cc7SKonstantin Belousov 		/*
1743f2159cc7SKonstantin Belousov 		 * POSIX requires POLLOUT to be never
1744f2159cc7SKonstantin Belousov 		 * set simultaneously with POLLHUP.
1745f2159cc7SKonstantin Belousov 		 */
1746f2159cc7SKonstantin Belousov 		if ((fds->revents & POLLHUP) != 0)
1747f2159cc7SKonstantin Belousov 			fds->revents &= ~POLLOUT;
1748f2159cc7SKonstantin Belousov 
174942d11757SPeter Wemm 		if (fds->revents != 0)
175042d11757SPeter Wemm 			n++;
175142d11757SPeter Wemm 	}
1752b40ce416SJulian Elischer 	td->td_retval[0] = n;
175342d11757SPeter Wemm 	return (0);
175442d11757SPeter Wemm }
175542d11757SPeter Wemm 
175642d11757SPeter Wemm /*
1757237abf0cSDavide Italiano  * XXX This was created specifically to support netncp and netsmb.  This
1758237abf0cSDavide Italiano  * allows the caller to specify a socket to wait for events on.  It returns
1759237abf0cSDavide Italiano  * 0 if any events matched and an error otherwise.  There is no way to
1760237abf0cSDavide Italiano  * determine which events fired.
1761237abf0cSDavide Italiano  */
1762237abf0cSDavide Italiano int
1763237abf0cSDavide Italiano selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td)
1764237abf0cSDavide Italiano {
1765237abf0cSDavide Italiano 	struct timeval rtv;
1766237abf0cSDavide Italiano 	sbintime_t asbt, precision, rsbt;
1767237abf0cSDavide Italiano 	int error;
1768237abf0cSDavide Italiano 
176976665df9SPeter Wemm 	precision = 0;	/* stupid gcc! */
1770237abf0cSDavide Italiano 	if (tvp != NULL) {
1771237abf0cSDavide Italiano 		rtv = *tvp;
1772237abf0cSDavide Italiano 		if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1773237abf0cSDavide Italiano 		    rtv.tv_usec >= 1000000)
1774237abf0cSDavide Italiano 			return (EINVAL);
1775237abf0cSDavide Italiano 		if (!timevalisset(&rtv))
1776237abf0cSDavide Italiano 			asbt = 0;
1777237abf0cSDavide Italiano 		else if (rtv.tv_sec <= INT32_MAX) {
1778237abf0cSDavide Italiano 			rsbt = tvtosbt(rtv);
1779237abf0cSDavide Italiano 			precision = rsbt;
1780237abf0cSDavide Italiano 			precision >>= tc_precexp;
1781237abf0cSDavide Italiano 			if (TIMESEL(&asbt, rsbt))
1782237abf0cSDavide Italiano 				asbt += tc_tick_sbt;
17834bc38a5aSDavide Italiano 			if (asbt <= SBT_MAX - rsbt)
1784237abf0cSDavide Italiano 				asbt += rsbt;
1785237abf0cSDavide Italiano 			else
1786237abf0cSDavide Italiano 				asbt = -1;
1787237abf0cSDavide Italiano 		} else
1788237abf0cSDavide Italiano 			asbt = -1;
1789237abf0cSDavide Italiano 	} else
1790237abf0cSDavide Italiano 		asbt = -1;
1791237abf0cSDavide Italiano 	seltdinit(td);
1792237abf0cSDavide Italiano 	/*
1793237abf0cSDavide Italiano 	 * Iterate until the timeout expires or the socket becomes ready.
1794237abf0cSDavide Italiano 	 */
1795237abf0cSDavide Italiano 	for (;;) {
1796237abf0cSDavide Italiano 		selfdalloc(td, NULL);
1797*04c91ac4SBrooks Davis 		if (sopoll(so, events, NULL, td) != 0) {
1798*04c91ac4SBrooks Davis 			error = 0;
1799*04c91ac4SBrooks Davis 			break;
1800*04c91ac4SBrooks Davis 		}
1801237abf0cSDavide Italiano 		error = seltdwait(td, asbt, precision);
1802237abf0cSDavide Italiano 		if (error)
1803237abf0cSDavide Italiano 			break;
1804237abf0cSDavide Italiano 	}
1805237abf0cSDavide Italiano 	seltdclear(td);
1806237abf0cSDavide Italiano 	/* XXX Duplicates ncp/smb behavior. */
1807237abf0cSDavide Italiano 	if (error == ERESTART)
1808237abf0cSDavide Italiano 		error = 0;
1809237abf0cSDavide Italiano 	return (error);
1810237abf0cSDavide Italiano }
1811237abf0cSDavide Italiano 
1812237abf0cSDavide Italiano /*
1813ace8398dSJeff Roberson  * Preallocate two selfds associated with 'cookie'.  Some fo_poll routines
1814ace8398dSJeff Roberson  * have two select sets, one for read and another for write.
1815ace8398dSJeff Roberson  */
1816ace8398dSJeff Roberson static void
1817ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie)
1818ace8398dSJeff Roberson {
1819ace8398dSJeff Roberson 	struct seltd *stp;
1820ace8398dSJeff Roberson 
1821ace8398dSJeff Roberson 	stp = td->td_sel;
1822ace8398dSJeff Roberson 	if (stp->st_free1 == NULL)
1823ea33cca9SMateusz Guzik 		stp->st_free1 = malloc(sizeof(*stp->st_free1), M_SELFD, M_WAITOK|M_ZERO);
1824ace8398dSJeff Roberson 	stp->st_free1->sf_td = stp;
1825ace8398dSJeff Roberson 	stp->st_free1->sf_cookie = cookie;
1826ace8398dSJeff Roberson 	if (stp->st_free2 == NULL)
1827ea33cca9SMateusz Guzik 		stp->st_free2 = malloc(sizeof(*stp->st_free2), M_SELFD, M_WAITOK|M_ZERO);
1828ace8398dSJeff Roberson 	stp->st_free2->sf_td = stp;
1829ace8398dSJeff Roberson 	stp->st_free2->sf_cookie = cookie;
1830ace8398dSJeff Roberson }
1831ace8398dSJeff Roberson 
1832ace8398dSJeff Roberson static void
1833ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp)
1834ace8398dSJeff Roberson {
1835ace8398dSJeff Roberson 	STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
183631b2ac4bSMateusz Guzik 	/*
183731b2ac4bSMateusz Guzik 	 * Paired with doselwakeup.
183831b2ac4bSMateusz Guzik 	 */
183931b2ac4bSMateusz Guzik 	if (atomic_load_acq_ptr((uintptr_t *)&sfp->sf_si) != (uintptr_t)NULL) {
1840ace8398dSJeff Roberson 		mtx_lock(sfp->sf_mtx);
1841fcb5b3a4SKonstantin Belousov 		if (sfp->sf_si != NULL) {
1842ace8398dSJeff Roberson 			TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1843fcb5b3a4SKonstantin Belousov 		}
1844ace8398dSJeff Roberson 		mtx_unlock(sfp->sf_mtx);
184573f2e5f7SMateusz Guzik 	}
1846ea33cca9SMateusz Guzik 	free(sfp, M_SELFD);
184785f190e4SAlfred Perlstein }
184885f190e4SAlfred Perlstein 
18496aba400aSAttilio Rao /* Drain the waiters tied to all the selfd belonging the specified selinfo. */
18506aba400aSAttilio Rao void
1851cc3c9df8SEd Maste seldrain(struct selinfo *sip)
18526aba400aSAttilio Rao {
18536aba400aSAttilio Rao 
18546aba400aSAttilio Rao 	/*
18556aba400aSAttilio Rao 	 * This feature is already provided by doselwakeup(), thus it is
18566aba400aSAttilio Rao 	 * enough to go for it.
18576aba400aSAttilio Rao 	 * Eventually, the context, should take care to avoid races
18586aba400aSAttilio Rao 	 * between thread calling select()/poll() and file descriptor
18596aba400aSAttilio Rao 	 * detaching, but, again, the races are just the same as
18606aba400aSAttilio Rao 	 * selwakeup().
18616aba400aSAttilio Rao 	 */
18626aba400aSAttilio Rao         doselwakeup(sip, -1);
18636aba400aSAttilio Rao }
18646aba400aSAttilio Rao 
1865df8bae1dSRodney W. Grimes /*
1866df8bae1dSRodney W. Grimes  * Record a select request.
1867df8bae1dSRodney W. Grimes  */
1868df8bae1dSRodney W. Grimes void
1869cc3c9df8SEd Maste selrecord(struct thread *selector, struct selinfo *sip)
1870df8bae1dSRodney W. Grimes {
1871ace8398dSJeff Roberson 	struct selfd *sfp;
1872ace8398dSJeff Roberson 	struct seltd *stp;
1873ace8398dSJeff Roberson 	struct mtx *mtxp;
1874df8bae1dSRodney W. Grimes 
1875ace8398dSJeff Roberson 	stp = selector->td_sel;
187685f190e4SAlfred Perlstein 	/*
1877ace8398dSJeff Roberson 	 * Don't record when doing a rescan.
187885f190e4SAlfred Perlstein 	 */
1879ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_RESCAN)
1880ace8398dSJeff Roberson 		return;
1881ace8398dSJeff Roberson 	/*
1882ace8398dSJeff Roberson 	 * Grab one of the preallocated descriptors.
1883ace8398dSJeff Roberson 	 */
1884ace8398dSJeff Roberson 	sfp = NULL;
1885ace8398dSJeff Roberson 	if ((sfp = stp->st_free1) != NULL)
1886ace8398dSJeff Roberson 		stp->st_free1 = NULL;
1887ace8398dSJeff Roberson 	else if ((sfp = stp->st_free2) != NULL)
1888ace8398dSJeff Roberson 		stp->st_free2 = NULL;
1889ace8398dSJeff Roberson 	else
1890ace8398dSJeff Roberson 		panic("selrecord: No free selfd on selq");
18912141453eSJeff Roberson 	mtxp = sip->si_mtx;
18922141453eSJeff Roberson 	if (mtxp == NULL)
18932141453eSJeff Roberson 		mtxp = mtx_pool_find(mtxpool_select, sip);
1894ace8398dSJeff Roberson 	/*
1895ace8398dSJeff Roberson 	 * Initialize the sfp and queue it in the thread.
1896ace8398dSJeff Roberson 	 */
1897ace8398dSJeff Roberson 	sfp->sf_si = sip;
1898ace8398dSJeff Roberson 	sfp->sf_mtx = mtxp;
1899ace8398dSJeff Roberson 	STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1900ace8398dSJeff Roberson 	/*
1901ace8398dSJeff Roberson 	 * Now that we've locked the sip, check for initialization.
1902ace8398dSJeff Roberson 	 */
1903ace8398dSJeff Roberson 	mtx_lock(mtxp);
1904ace8398dSJeff Roberson 	if (sip->si_mtx == NULL) {
1905ace8398dSJeff Roberson 		sip->si_mtx = mtxp;
1906ace8398dSJeff Roberson 		TAILQ_INIT(&sip->si_tdlist);
190785f190e4SAlfred Perlstein 	}
1908ace8398dSJeff Roberson 	/*
1909ace8398dSJeff Roberson 	 * Add this thread to the list of selfds listening on this selinfo.
1910ace8398dSJeff Roberson 	 */
1911ace8398dSJeff Roberson 	TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1912ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1913df8bae1dSRodney W. Grimes }
1914df8bae1dSRodney W. Grimes 
1915512824f8SSeigo Tanimura /* Wake up a selecting thread. */
1916df8bae1dSRodney W. Grimes void
1917cc3c9df8SEd Maste selwakeup(struct selinfo *sip)
1918df8bae1dSRodney W. Grimes {
1919512824f8SSeigo Tanimura 	doselwakeup(sip, -1);
1920512824f8SSeigo Tanimura }
1921512824f8SSeigo Tanimura 
1922512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */
1923512824f8SSeigo Tanimura void
1924cc3c9df8SEd Maste selwakeuppri(struct selinfo *sip, int pri)
1925512824f8SSeigo Tanimura {
1926512824f8SSeigo Tanimura 	doselwakeup(sip, pri);
1927512824f8SSeigo Tanimura }
1928512824f8SSeigo Tanimura 
1929512824f8SSeigo Tanimura /*
1930512824f8SSeigo Tanimura  * Do a wakeup when a selectable event occurs.
1931512824f8SSeigo Tanimura  */
1932512824f8SSeigo Tanimura static void
1933cc3c9df8SEd Maste doselwakeup(struct selinfo *sip, int pri)
1934512824f8SSeigo Tanimura {
1935ace8398dSJeff Roberson 	struct selfd *sfp;
1936ace8398dSJeff Roberson 	struct selfd *sfn;
1937ace8398dSJeff Roberson 	struct seltd *stp;
1938df8bae1dSRodney W. Grimes 
1939ace8398dSJeff Roberson 	/* If it's not initialized there can't be any waiters. */
1940ace8398dSJeff Roberson 	if (sip->si_mtx == NULL)
1941b40ce416SJulian Elischer 		return;
1942ace8398dSJeff Roberson 	/*
1943ace8398dSJeff Roberson 	 * Locking the selinfo locks all selfds associated with it.
1944ace8398dSJeff Roberson 	 */
1945ace8398dSJeff Roberson 	mtx_lock(sip->si_mtx);
1946ace8398dSJeff Roberson 	TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1947ace8398dSJeff Roberson 		/*
1948ace8398dSJeff Roberson 		 * Once we remove this sfp from the list and clear the
1949ace8398dSJeff Roberson 		 * sf_si seltdclear will know to ignore this si.
1950ace8398dSJeff Roberson 		 */
1951ace8398dSJeff Roberson 		TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1952ace8398dSJeff Roberson 		stp = sfp->sf_td;
1953ace8398dSJeff Roberson 		mtx_lock(&stp->st_mtx);
1954ace8398dSJeff Roberson 		stp->st_flags |= SELTD_PENDING;
1955ace8398dSJeff Roberson 		cv_broadcastpri(&stp->st_wait, pri);
1956ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
195710e64782SMateusz Guzik 		/*
195810e64782SMateusz Guzik 		 * Paired with selfdfree.
195910e64782SMateusz Guzik 		 *
196010e64782SMateusz Guzik 		 * Storing this only after the wakeup provides an invariant that
196110e64782SMateusz Guzik 		 * stp is not used after selfdfree returns.
196210e64782SMateusz Guzik 		 */
196310e64782SMateusz Guzik 		atomic_store_rel_ptr((uintptr_t *)&sfp->sf_si, (uintptr_t)NULL);
1964b40ce416SJulian Elischer 	}
1965ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1966ace8398dSJeff Roberson }
1967ace8398dSJeff Roberson 
1968ace8398dSJeff Roberson static void
1969ace8398dSJeff Roberson seltdinit(struct thread *td)
1970ace8398dSJeff Roberson {
1971ace8398dSJeff Roberson 	struct seltd *stp;
1972ace8398dSJeff Roberson 
197310e64782SMateusz Guzik 	stp = td->td_sel;
197410e64782SMateusz Guzik 	if (stp != NULL) {
197510e64782SMateusz Guzik 		MPASS(stp->st_flags == 0);
197610e64782SMateusz Guzik 		MPASS(STAILQ_EMPTY(&stp->st_selq));
197710e64782SMateusz Guzik 		return;
197810e64782SMateusz Guzik 	}
197910e64782SMateusz Guzik 	stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1980ace8398dSJeff Roberson 	mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1981ace8398dSJeff Roberson 	cv_init(&stp->st_wait, "select");
1982ace8398dSJeff Roberson 	stp->st_flags = 0;
1983ace8398dSJeff Roberson 	STAILQ_INIT(&stp->st_selq);
198410e64782SMateusz Guzik 	td->td_sel = stp;
1985ace8398dSJeff Roberson }
1986ace8398dSJeff Roberson 
1987ace8398dSJeff Roberson static int
1988cf5e4fe6SDavide Italiano seltdwait(struct thread *td, sbintime_t sbt, sbintime_t precision)
1989ace8398dSJeff Roberson {
1990ace8398dSJeff Roberson 	struct seltd *stp;
1991ace8398dSJeff Roberson 	int error;
1992ace8398dSJeff Roberson 
1993ace8398dSJeff Roberson 	stp = td->td_sel;
1994ace8398dSJeff Roberson 	/*
1995ace8398dSJeff Roberson 	 * An event of interest may occur while we do not hold the seltd
1996ace8398dSJeff Roberson 	 * locked so check the pending flag before we sleep.
1997ace8398dSJeff Roberson 	 */
1998ace8398dSJeff Roberson 	mtx_lock(&stp->st_mtx);
1999ace8398dSJeff Roberson 	/*
2000ace8398dSJeff Roberson 	 * Any further calls to selrecord will be a rescan.
2001ace8398dSJeff Roberson 	 */
2002ace8398dSJeff Roberson 	stp->st_flags |= SELTD_RESCAN;
2003ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_PENDING) {
2004ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
2005ace8398dSJeff Roberson 		return (0);
2006ace8398dSJeff Roberson 	}
2007cf5e4fe6SDavide Italiano 	if (sbt == 0)
2008cf5e4fe6SDavide Italiano 		error = EWOULDBLOCK;
2009cf5e4fe6SDavide Italiano 	else if (sbt != -1)
2010cf5e4fe6SDavide Italiano 		error = cv_timedwait_sig_sbt(&stp->st_wait, &stp->st_mtx,
2011cf5e4fe6SDavide Italiano 		    sbt, precision, C_ABSOLUTE);
2012ace8398dSJeff Roberson 	else
2013ace8398dSJeff Roberson 		error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
2014ace8398dSJeff Roberson 	mtx_unlock(&stp->st_mtx);
2015ace8398dSJeff Roberson 
2016ace8398dSJeff Roberson 	return (error);
2017ace8398dSJeff Roberson }
2018ace8398dSJeff Roberson 
2019ace8398dSJeff Roberson void
2020ace8398dSJeff Roberson seltdfini(struct thread *td)
2021ace8398dSJeff Roberson {
2022ace8398dSJeff Roberson 	struct seltd *stp;
2023ace8398dSJeff Roberson 
2024ace8398dSJeff Roberson 	stp = td->td_sel;
2025ace8398dSJeff Roberson 	if (stp == NULL)
2026ace8398dSJeff Roberson 		return;
202710e64782SMateusz Guzik 	MPASS(stp->st_flags == 0);
202810e64782SMateusz Guzik 	MPASS(STAILQ_EMPTY(&stp->st_selq));
2029ace8398dSJeff Roberson 	if (stp->st_free1)
2030ea33cca9SMateusz Guzik 		free(stp->st_free1, M_SELFD);
2031ace8398dSJeff Roberson 	if (stp->st_free2)
2032ea33cca9SMateusz Guzik 		free(stp->st_free2, M_SELFD);
2033ace8398dSJeff Roberson 	td->td_sel = NULL;
203436bce27bSKonstantin Belousov 	cv_destroy(&stp->st_wait);
203536bce27bSKonstantin Belousov 	mtx_destroy(&stp->st_mtx);
2036ace8398dSJeff Roberson 	free(stp, M_SELECT);
2037ace8398dSJeff Roberson }
2038ace8398dSJeff Roberson 
2039ace8398dSJeff Roberson /*
2040ace8398dSJeff Roberson  * Remove the references to the thread from all of the objects we were
2041ace8398dSJeff Roberson  * polling.
2042ace8398dSJeff Roberson  */
2043ace8398dSJeff Roberson static void
2044ace8398dSJeff Roberson seltdclear(struct thread *td)
2045ace8398dSJeff Roberson {
2046ace8398dSJeff Roberson 	struct seltd *stp;
2047ace8398dSJeff Roberson 	struct selfd *sfp;
2048ace8398dSJeff Roberson 	struct selfd *sfn;
2049ace8398dSJeff Roberson 
2050ace8398dSJeff Roberson 	stp = td->td_sel;
2051ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
2052ace8398dSJeff Roberson 		selfdfree(stp, sfp);
2053ace8398dSJeff Roberson 	stp->st_flags = 0;
2054df8bae1dSRodney W. Grimes }
2055265fc98fSSeigo Tanimura 
20564d77a549SAlfred Perlstein static void selectinit(void *);
2057ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
2058265fc98fSSeigo Tanimura static void
2059ace8398dSJeff Roberson selectinit(void *dummy __unused)
2060265fc98fSSeigo Tanimura {
20612141453eSJeff Roberson 
20622141453eSJeff Roberson 	mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);
2063265fc98fSSeigo Tanimura }
20640acf5d0bSMark Johnston 
20650acf5d0bSMark Johnston /*
20660acf5d0bSMark Johnston  * Set up a syscall return value that follows the convention specified for
20670acf5d0bSMark Johnston  * posix_* functions.
20680acf5d0bSMark Johnston  */
20690acf5d0bSMark Johnston int
20700acf5d0bSMark Johnston kern_posix_error(struct thread *td, int error)
20710acf5d0bSMark Johnston {
20720acf5d0bSMark Johnston 
20730acf5d0bSMark Johnston 	if (error <= 0)
20740acf5d0bSMark Johnston 		return (error);
20750acf5d0bSMark Johnston 	td->td_errno = error;
20760acf5d0bSMark Johnston 	td->td_pflags |= TDP_NERRNO;
20770acf5d0bSMark Johnston 	td->td_retval[0] = error;
20780acf5d0bSMark Johnston 	return (0);
20790acf5d0bSMark Johnston }
2080