xref: /freebsd/sys/kern/sys_generic.c (revision 45e1f8541428c19f63dba65d78a8d138e1bc6915)
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
8657a202823SKonstantin Belousov kern_specialfd(struct thread *td, int type, void *arg)
8667a202823SKonstantin Belousov {
8677a202823SKonstantin Belousov 	struct file *fp;
8687a202823SKonstantin Belousov 	struct specialfd_eventfd *ae;
8697a202823SKonstantin Belousov 	int error, fd, fflags;
8707a202823SKonstantin Belousov 
8717a202823SKonstantin Belousov 	fflags = 0;
8727a202823SKonstantin Belousov 	error = falloc_noinstall(td, &fp);
8737a202823SKonstantin Belousov 	if (error != 0)
8747a202823SKonstantin Belousov 		return (error);
8757a202823SKonstantin Belousov 
8767a202823SKonstantin Belousov 	switch (type) {
8777a202823SKonstantin Belousov 	case SPECIALFD_EVENTFD:
8787a202823SKonstantin Belousov 		ae = arg;
8797a202823SKonstantin Belousov 		if ((ae->flags & EFD_CLOEXEC) != 0)
8807a202823SKonstantin Belousov 			fflags |= O_CLOEXEC;
8817a202823SKonstantin Belousov 		error = eventfd_create_file(td, fp, ae->initval, ae->flags);
8827a202823SKonstantin Belousov 		break;
8837a202823SKonstantin Belousov 	default:
8847a202823SKonstantin Belousov 		error = EINVAL;
8857a202823SKonstantin Belousov 		break;
8867a202823SKonstantin Belousov 	}
8877a202823SKonstantin Belousov 
8887a202823SKonstantin Belousov 	if (error == 0)
8897a202823SKonstantin Belousov 		error = finstall(td, fp, &fd, fflags, NULL);
8907a202823SKonstantin Belousov 	fdrop(fp, td);
8917a202823SKonstantin Belousov 	if (error == 0)
8927a202823SKonstantin Belousov 		td->td_retval[0] = fd;
8937a202823SKonstantin Belousov 	return (error);
8947a202823SKonstantin Belousov }
8957a202823SKonstantin Belousov 
8967a202823SKonstantin Belousov int
8977a202823SKonstantin Belousov sys___specialfd(struct thread *td, struct __specialfd_args *args)
8987a202823SKonstantin Belousov {
8997a202823SKonstantin Belousov 	struct specialfd_eventfd ae;
9007a202823SKonstantin Belousov 	int error;
9017a202823SKonstantin Belousov 
9027a202823SKonstantin Belousov 	switch (args->type) {
9037a202823SKonstantin Belousov 	case SPECIALFD_EVENTFD:
9047a202823SKonstantin Belousov 		if (args->len != sizeof(struct specialfd_eventfd)) {
9057a202823SKonstantin Belousov 			error = EINVAL;
9067a202823SKonstantin Belousov 			break;
9077a202823SKonstantin Belousov 		}
9087a202823SKonstantin Belousov 		error = copyin(args->req, &ae, sizeof(ae));
9097a202823SKonstantin Belousov 		if (error != 0)
9107a202823SKonstantin Belousov 			break;
9117a202823SKonstantin Belousov 		if ((ae.flags & ~(EFD_CLOEXEC | EFD_NONBLOCK |
9127a202823SKonstantin Belousov 		    EFD_SEMAPHORE)) != 0) {
9137a202823SKonstantin Belousov 			error = EINVAL;
9147a202823SKonstantin Belousov 			break;
9157a202823SKonstantin Belousov 		}
9167a202823SKonstantin Belousov 		error = kern_specialfd(td, args->type, &ae);
9177a202823SKonstantin Belousov 		break;
9187a202823SKonstantin Belousov 	default:
9197a202823SKonstantin Belousov 		error = EINVAL;
9207a202823SKonstantin Belousov 		break;
9217a202823SKonstantin Belousov 	}
9227a202823SKonstantin Belousov 	return (error);
9237a202823SKonstantin Belousov }
9247a202823SKonstantin Belousov 
9257a202823SKonstantin Belousov int
926125dcf8cSKonstantin Belousov poll_no_poll(int events)
927125dcf8cSKonstantin Belousov {
928125dcf8cSKonstantin Belousov 	/*
929125dcf8cSKonstantin Belousov 	 * Return true for read/write.  If the user asked for something
930125dcf8cSKonstantin Belousov 	 * special, return POLLNVAL, so that clients have a way of
931125dcf8cSKonstantin Belousov 	 * determining reliably whether or not the extended
932125dcf8cSKonstantin Belousov 	 * functionality is present without hard-coding knowledge
933125dcf8cSKonstantin Belousov 	 * of specific filesystem implementations.
934125dcf8cSKonstantin Belousov 	 */
935125dcf8cSKonstantin Belousov 	if (events & ~POLLSTANDARD)
936125dcf8cSKonstantin Belousov 		return (POLLNVAL);
937125dcf8cSKonstantin Belousov 
938125dcf8cSKonstantin Belousov 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
939125dcf8cSKonstantin Belousov }
940125dcf8cSKonstantin Belousov 
941066d836bSKonstantin Belousov int
9428451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap)
943066d836bSKonstantin Belousov {
944066d836bSKonstantin Belousov 	struct timespec ts;
945066d836bSKonstantin Belousov 	struct timeval tv, *tvp;
946066d836bSKonstantin Belousov 	sigset_t set, *uset;
947066d836bSKonstantin Belousov 	int error;
948066d836bSKonstantin Belousov 
949066d836bSKonstantin Belousov 	if (uap->ts != NULL) {
950066d836bSKonstantin Belousov 		error = copyin(uap->ts, &ts, sizeof(ts));
951066d836bSKonstantin Belousov 		if (error != 0)
952066d836bSKonstantin Belousov 		    return (error);
953066d836bSKonstantin Belousov 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
954066d836bSKonstantin Belousov 		tvp = &tv;
955066d836bSKonstantin Belousov 	} else
956066d836bSKonstantin Belousov 		tvp = NULL;
957066d836bSKonstantin Belousov 	if (uap->sm != NULL) {
958066d836bSKonstantin Belousov 		error = copyin(uap->sm, &set, sizeof(set));
959066d836bSKonstantin Belousov 		if (error != 0)
960066d836bSKonstantin Belousov 			return (error);
961066d836bSKonstantin Belousov 		uset = &set;
962066d836bSKonstantin Belousov 	} else
963066d836bSKonstantin Belousov 		uset = NULL;
964066d836bSKonstantin Belousov 	return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
965066d836bSKonstantin Belousov 	    uset, NFDBITS));
966066d836bSKonstantin Belousov }
967066d836bSKonstantin Belousov 
968066d836bSKonstantin Belousov int
969066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
970066d836bSKonstantin Belousov     struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
971066d836bSKonstantin Belousov {
972066d836bSKonstantin Belousov 	int error;
973066d836bSKonstantin Belousov 
974066d836bSKonstantin Belousov 	if (uset != NULL) {
975066d836bSKonstantin Belousov 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
976066d836bSKonstantin Belousov 		    &td->td_oldsigmask, 0);
977066d836bSKonstantin Belousov 		if (error != 0)
978066d836bSKonstantin Belousov 			return (error);
979066d836bSKonstantin Belousov 		td->td_pflags |= TDP_OLDMASK;
980066d836bSKonstantin Belousov 		/*
981066d836bSKonstantin Belousov 		 * Make sure that ast() is called on return to
982066d836bSKonstantin Belousov 		 * usermode and TDP_OLDMASK is cleared, restoring old
983066d836bSKonstantin Belousov 		 * sigmask.
984066d836bSKonstantin Belousov 		 */
985066d836bSKonstantin Belousov 		thread_lock(td);
986066d836bSKonstantin Belousov 		td->td_flags |= TDF_ASTPENDING;
987066d836bSKonstantin Belousov 		thread_unlock(td);
988066d836bSKonstantin Belousov 	}
989066d836bSKonstantin Belousov 	error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
990066d836bSKonstantin Belousov 	return (error);
991066d836bSKonstantin Belousov }
992066d836bSKonstantin Belousov 
993d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
994df8bae1dSRodney W. Grimes struct select_args {
995b08f7993SSujal Patel 	int	nd;
996df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
997df8bae1dSRodney W. Grimes 	struct	timeval *tv;
998df8bae1dSRodney W. Grimes };
999d2d3e875SBruce Evans #endif
100026f9a767SRodney W. Grimes int
10018451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap)
1002df8bae1dSRodney W. Grimes {
10038f19eb88SIan Dowse 	struct timeval tv, *tvp;
10048f19eb88SIan Dowse 	int error;
10058f19eb88SIan Dowse 
10068f19eb88SIan Dowse 	if (uap->tv != NULL) {
10078f19eb88SIan Dowse 		error = copyin(uap->tv, &tv, sizeof(tv));
10088f19eb88SIan Dowse 		if (error)
10098f19eb88SIan Dowse 			return (error);
10108f19eb88SIan Dowse 		tvp = &tv;
10118f19eb88SIan Dowse 	} else
10128f19eb88SIan Dowse 		tvp = NULL;
10138f19eb88SIan Dowse 
1014b55ef216SKonstantin Belousov 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
1015b55ef216SKonstantin Belousov 	    NFDBITS));
10168f19eb88SIan Dowse }
10178f19eb88SIan Dowse 
101856be1b9aSKonstantin Belousov /*
101956be1b9aSKonstantin Belousov  * In the unlikely case when user specified n greater then the last
102056be1b9aSKonstantin Belousov  * open file descriptor, check that no bits are set after the last
102156be1b9aSKonstantin Belousov  * valid fd.  We must return EBADF if any is set.
102256be1b9aSKonstantin Belousov  *
102356be1b9aSKonstantin Belousov  * There are applications that rely on the behaviour.
102456be1b9aSKonstantin Belousov  *
1025d8bc2a17SMateusz Guzik  * nd is fd_nfiles.
102656be1b9aSKonstantin Belousov  */
102756be1b9aSKonstantin Belousov static int
102856be1b9aSKonstantin Belousov select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits)
102956be1b9aSKonstantin Belousov {
103056be1b9aSKonstantin Belousov 	char *addr, *oaddr;
103156be1b9aSKonstantin Belousov 	int b, i, res;
103256be1b9aSKonstantin Belousov 	uint8_t bits;
103356be1b9aSKonstantin Belousov 
103456be1b9aSKonstantin Belousov 	if (nd >= ndu || fd_in == NULL)
103556be1b9aSKonstantin Belousov 		return (0);
103656be1b9aSKonstantin Belousov 
103756be1b9aSKonstantin Belousov 	oaddr = NULL;
103856be1b9aSKonstantin Belousov 	bits = 0; /* silence gcc */
103956be1b9aSKonstantin Belousov 	for (i = nd; i < ndu; i++) {
104056be1b9aSKonstantin Belousov 		b = i / NBBY;
104156be1b9aSKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN
104256be1b9aSKonstantin Belousov 		addr = (char *)fd_in + b;
104356be1b9aSKonstantin Belousov #else
104456be1b9aSKonstantin Belousov 		addr = (char *)fd_in;
104556be1b9aSKonstantin Belousov 		if (abi_nfdbits == NFDBITS) {
104656be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(fd_mask)) +
104756be1b9aSKonstantin Belousov 			    sizeof(fd_mask) - 1 - b % sizeof(fd_mask);
104856be1b9aSKonstantin Belousov 		} else {
104956be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(uint32_t)) +
105056be1b9aSKonstantin Belousov 			    sizeof(uint32_t) - 1 - b % sizeof(uint32_t);
105156be1b9aSKonstantin Belousov 		}
105256be1b9aSKonstantin Belousov #endif
105356be1b9aSKonstantin Belousov 		if (addr != oaddr) {
105456be1b9aSKonstantin Belousov 			res = fubyte(addr);
105556be1b9aSKonstantin Belousov 			if (res == -1)
105656be1b9aSKonstantin Belousov 				return (EFAULT);
105756be1b9aSKonstantin Belousov 			oaddr = addr;
105856be1b9aSKonstantin Belousov 			bits = res;
105956be1b9aSKonstantin Belousov 		}
106056be1b9aSKonstantin Belousov 		if ((bits & (1 << (i % NBBY))) != 0)
106156be1b9aSKonstantin Belousov 			return (EBADF);
106256be1b9aSKonstantin Belousov 	}
106356be1b9aSKonstantin Belousov 	return (0);
106456be1b9aSKonstantin Belousov }
106556be1b9aSKonstantin Belousov 
10668f19eb88SIan Dowse int
10678f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
1068b55ef216SKonstantin Belousov     fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
10698f19eb88SIan Dowse {
1070426da3bcSAlfred Perlstein 	struct filedesc *fdp;
1071d5e4d7e1SBruce Evans 	/*
1072d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
1073d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
1074d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
1075d5e4d7e1SBruce Evans 	 * of 256.
1076d5e4d7e1SBruce Evans 	 */
1077d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
1078eb209311SAlfred Perlstein 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
1079cf5e4fe6SDavide Italiano 	struct timeval rtv;
1080cf5e4fe6SDavide Italiano 	sbintime_t asbt, precision, rsbt;
1081b55ef216SKonstantin Belousov 	u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
1082cf5e4fe6SDavide Italiano 	int error, lf, ndu;
1083df8bae1dSRodney W. Grimes 
10848f19eb88SIan Dowse 	if (nd < 0)
1085acbfbfeaSSujal Patel 		return (EINVAL);
1086426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
108756be1b9aSKonstantin Belousov 	ndu = nd;
1088d8bc2a17SMateusz Guzik 	lf = fdp->fd_nfiles;
1089d8bc2a17SMateusz Guzik 	if (nd > lf)
1090d8bc2a17SMateusz Guzik 		nd = lf;
109156be1b9aSKonstantin Belousov 
109256be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits);
109356be1b9aSKonstantin Belousov 	if (error != 0)
109456be1b9aSKonstantin Belousov 		return (error);
109556be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits);
109656be1b9aSKonstantin Belousov 	if (error != 0)
109756be1b9aSKonstantin Belousov 		return (error);
109856be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits);
109956be1b9aSKonstantin Belousov 	if (error != 0)
110056be1b9aSKonstantin Belousov 		return (error);
1101b08f7993SSujal Patel 
1102d5e4d7e1SBruce Evans 	/*
1103d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
1104d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
1105d5e4d7e1SBruce Evans 	 */
11068f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
1107d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
1108b55ef216SKonstantin Belousov 	ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
1109d5e4d7e1SBruce Evans 	nbufbytes = 0;
11108f19eb88SIan Dowse 	if (fd_in != NULL)
1111d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
11128f19eb88SIan Dowse 	if (fd_ou != NULL)
1113d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
11148f19eb88SIan Dowse 	if (fd_ex != NULL)
1115d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
1116d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
1117d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
1118d5e4d7e1SBruce Evans 	else
1119a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
1120b08f7993SSujal Patel 
1121b08f7993SSujal Patel 	/*
1122d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
1123d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
1124d5e4d7e1SBruce Evans 	 * together.
1125b08f7993SSujal Patel 	 */
1126d5e4d7e1SBruce Evans 	sbp = selbits;
1127df8bae1dSRodney W. Grimes #define	getbits(name, x) \
1128d5e4d7e1SBruce Evans 	do {								\
1129841c0c7eSNathan Whitehorn 		if (name == NULL) {					\
1130d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
1131841c0c7eSNathan Whitehorn 			obits[x] = NULL;				\
1132841c0c7eSNathan Whitehorn 		} else {						\
1133d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
1134d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
1135d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
1136b55ef216SKonstantin Belousov 			error = copyin(name, ibits[x], ncpubytes);	\
1137265fc98fSSeigo Tanimura 			if (error != 0)					\
1138ace8398dSJeff Roberson 				goto done;				\
1139d6fda03aSMateusz Guzik 			if (ncpbytes != ncpubytes)			\
1140b55ef216SKonstantin Belousov 				bzero((char *)ibits[x] + ncpubytes,	\
1141b55ef216SKonstantin Belousov 				    ncpbytes - ncpubytes);		\
1142e04ac2feSJohn Baldwin 		}							\
1143d5e4d7e1SBruce Evans 	} while (0)
11448f19eb88SIan Dowse 	getbits(fd_in, 0);
11458f19eb88SIan Dowse 	getbits(fd_ou, 1);
11468f19eb88SIan Dowse 	getbits(fd_ex, 2);
1147df8bae1dSRodney W. Grimes #undef	getbits
1148841c0c7eSNathan Whitehorn 
1149841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
1150841c0c7eSNathan Whitehorn 	/*
1151841c0c7eSNathan Whitehorn 	 * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
1152841c0c7eSNathan Whitehorn 	 * we are running under 32-bit emulation. This should be more
1153841c0c7eSNathan Whitehorn 	 * generic.
1154841c0c7eSNathan Whitehorn 	 */
1155841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)						\
1156841c0c7eSNathan Whitehorn 	if (abi_nfdbits != NFDBITS && bits != NULL) {			\
1157841c0c7eSNathan Whitehorn 		int i;							\
1158841c0c7eSNathan Whitehorn 		for (i = 0; i < ncpbytes / sizeof *sbp; i++)		\
1159841c0c7eSNathan Whitehorn 			bits[i] = (bits[i] >> 32) | (bits[i] << 32);	\
1160841c0c7eSNathan Whitehorn 	}
1161841c0c7eSNathan Whitehorn #else
1162841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)
1163841c0c7eSNathan Whitehorn #endif
1164841c0c7eSNathan Whitehorn 
1165841c0c7eSNathan Whitehorn 	/* Make sure the bit order makes it through an ABI transition */
1166841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[0]);
1167841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[1]);
1168841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[2]);
1169841c0c7eSNathan Whitehorn 
1170d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
1171d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
1172df8bae1dSRodney W. Grimes 
1173cf5e4fe6SDavide Italiano 	precision = 0;
11748f19eb88SIan Dowse 	if (tvp != NULL) {
1175cf5e4fe6SDavide Italiano 		rtv = *tvp;
1176cf5e4fe6SDavide Italiano 		if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1177cf5e4fe6SDavide Italiano 		    rtv.tv_usec >= 1000000) {
1178df8bae1dSRodney W. Grimes 			error = EINVAL;
1179ace8398dSJeff Roberson 			goto done;
1180df8bae1dSRodney W. Grimes 		}
118121a37a71SAlexander Motin 		if (!timevalisset(&rtv))
1182980c545dSAlexander Motin 			asbt = 0;
118321a37a71SAlexander Motin 		else if (rtv.tv_sec <= INT32_MAX) {
1184cf5e4fe6SDavide Italiano 			rsbt = tvtosbt(rtv);
1185cf5e4fe6SDavide Italiano 			precision = rsbt;
1186cf5e4fe6SDavide Italiano 			precision >>= tc_precexp;
1187cf5e4fe6SDavide Italiano 			if (TIMESEL(&asbt, rsbt))
1188cf5e4fe6SDavide Italiano 				asbt += tc_tick_sbt;
11894bc38a5aSDavide Italiano 			if (asbt <= SBT_MAX - rsbt)
1190cf5e4fe6SDavide Italiano 				asbt += rsbt;
119121a37a71SAlexander Motin 			else
1192980c545dSAlexander Motin 				asbt = -1;
1193980c545dSAlexander Motin 		} else
1194980c545dSAlexander Motin 			asbt = -1;
1195cf5e4fe6SDavide Italiano 	} else
1196cf5e4fe6SDavide Italiano 		asbt = -1;
1197ace8398dSJeff Roberson 	seltdinit(td);
1198ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1199ace8398dSJeff Roberson 	for (;;) {
12008f19eb88SIan Dowse 		error = selscan(td, ibits, obits, nd);
1201ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1202ace8398dSJeff Roberson 			break;
1203cf5e4fe6SDavide Italiano 		error = seltdwait(td, asbt, precision);
1204ace8398dSJeff Roberson 		if (error)
1205ace8398dSJeff Roberson 			break;
1206ace8398dSJeff Roberson 		error = selrescan(td, ibits, obits);
1207ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1208ace8398dSJeff Roberson 			break;
120985f190e4SAlfred Perlstein 	}
1210ace8398dSJeff Roberson 	seltdclear(td);
1211265fc98fSSeigo Tanimura 
1212df8bae1dSRodney W. Grimes done:
1213df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
1214df8bae1dSRodney W. Grimes 	if (error == ERESTART)
1215df8bae1dSRodney W. Grimes 		error = EINTR;
1216df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
1217df8bae1dSRodney W. Grimes 		error = 0;
1218841c0c7eSNathan Whitehorn 
1219841c0c7eSNathan Whitehorn 	/* swizzle bit order back, if necessary */
1220841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[0]);
1221841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[1]);
1222841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[2]);
1223841c0c7eSNathan Whitehorn #undef swizzle_fdset
1224841c0c7eSNathan Whitehorn 
1225df8bae1dSRodney W. Grimes #define	putbits(name, x) \
1226b55ef216SKonstantin Belousov 	if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
1227df8bae1dSRodney W. Grimes 		error = error2;
1228df8bae1dSRodney W. Grimes 	if (error == 0) {
1229df8bae1dSRodney W. Grimes 		int error2;
1230df8bae1dSRodney W. Grimes 
12318f19eb88SIan Dowse 		putbits(fd_in, 0);
12328f19eb88SIan Dowse 		putbits(fd_ou, 1);
12338f19eb88SIan Dowse 		putbits(fd_ex, 2);
1234df8bae1dSRodney W. Grimes #undef putbits
1235df8bae1dSRodney W. Grimes 	}
1236d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
1237d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
1238ad2edad9SMatthew Dillon 
1239df8bae1dSRodney W. Grimes 	return (error);
1240df8bae1dSRodney W. Grimes }
124111b763dfSJeff Roberson /*
124211b763dfSJeff Roberson  * Convert a select bit set to poll flags.
1243748b9df6SJeff Roberson  *
124411b763dfSJeff Roberson  * The backend always returns POLLHUP/POLLERR if appropriate and we
124511b763dfSJeff Roberson  * return this as a set bit in any set.
124611b763dfSJeff Roberson  */
124711b763dfSJeff Roberson static int select_flags[3] = {
124811b763dfSJeff Roberson     POLLRDNORM | POLLHUP | POLLERR,
124911b763dfSJeff Roberson     POLLWRNORM | POLLHUP | POLLERR,
125061e53a38SKonstantin Belousov     POLLRDBAND | POLLERR
125111b763dfSJeff Roberson };
125211b763dfSJeff Roberson 
125311b763dfSJeff Roberson /*
125411b763dfSJeff Roberson  * Compute the fo_poll flags required for a fd given by the index and
125511b763dfSJeff Roberson  * bit position in the fd_mask array.
125611b763dfSJeff Roberson  */
125711b763dfSJeff Roberson static __inline int
125860b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit)
125911b763dfSJeff Roberson {
126011b763dfSJeff Roberson 	int flags;
126111b763dfSJeff Roberson 	int msk;
126211b763dfSJeff Roberson 
126311b763dfSJeff Roberson 	flags = 0;
126411b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
126511b763dfSJeff Roberson 		if (ibits[msk] == NULL)
126611b763dfSJeff Roberson 			continue;
126760b7f468SStephane E. Potvin 		if ((ibits[msk][idx] & bit) == 0)
126811b763dfSJeff Roberson 			continue;
126911b763dfSJeff Roberson 		flags |= select_flags[msk];
127011b763dfSJeff Roberson 	}
127111b763dfSJeff Roberson 	return (flags);
127211b763dfSJeff Roberson }
127311b763dfSJeff Roberson 
127411b763dfSJeff Roberson /*
127511b763dfSJeff Roberson  * Set the appropriate output bits given a mask of fired events and the
127611b763dfSJeff Roberson  * input bits originally requested.
127711b763dfSJeff Roberson  */
127811b763dfSJeff Roberson static __inline int
127911b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
128011b763dfSJeff Roberson {
128111b763dfSJeff Roberson 	int msk;
128211b763dfSJeff Roberson 	int n;
128311b763dfSJeff Roberson 
128411b763dfSJeff Roberson 	n = 0;
128511b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
128611b763dfSJeff Roberson 		if ((events & select_flags[msk]) == 0)
128711b763dfSJeff Roberson 			continue;
128811b763dfSJeff Roberson 		if (ibits[msk] == NULL)
128911b763dfSJeff Roberson 			continue;
129011b763dfSJeff Roberson 		if ((ibits[msk][idx] & bit) == 0)
129111b763dfSJeff Roberson 			continue;
129211b763dfSJeff Roberson 		/*
129311b763dfSJeff Roberson 		 * XXX Check for a duplicate set.  This can occur because a
129411b763dfSJeff Roberson 		 * socket calls selrecord() twice for each poll() call
129511b763dfSJeff Roberson 		 * resulting in two selfds per real fd.  selrescan() will
129611b763dfSJeff Roberson 		 * call selsetbits twice as a result.
129711b763dfSJeff Roberson 		 */
129811b763dfSJeff Roberson 		if ((obits[msk][idx] & bit) != 0)
129911b763dfSJeff Roberson 			continue;
130011b763dfSJeff Roberson 		obits[msk][idx] |= bit;
130111b763dfSJeff Roberson 		n++;
130211b763dfSJeff Roberson 	}
130311b763dfSJeff Roberson 
130411b763dfSJeff Roberson 	return (n);
130511b763dfSJeff Roberson }
1306df8bae1dSRodney W. Grimes 
1307ace8398dSJeff Roberson /*
1308ace8398dSJeff Roberson  * Traverse the list of fds attached to this thread's seltd and check for
1309ace8398dSJeff Roberson  * completion.
1310ace8398dSJeff Roberson  */
1311ace8398dSJeff Roberson static int
1312ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
1313ace8398dSJeff Roberson {
131411b763dfSJeff Roberson 	struct filedesc *fdp;
131511b763dfSJeff Roberson 	struct selinfo *si;
1316ace8398dSJeff Roberson 	struct seltd *stp;
1317ace8398dSJeff Roberson 	struct selfd *sfp;
1318ace8398dSJeff Roberson 	struct selfd *sfn;
1319ace8398dSJeff Roberson 	struct file *fp;
13209cdacff1SJeff Roberson 	fd_mask bit;
13219cdacff1SJeff Roberson 	int fd, ev, n, idx;
1322a9d2f8d8SRobert Watson 	int error;
13236affe1b7SMateusz Guzik 	bool only_user;
1324ace8398dSJeff Roberson 
132511b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
1326ace8398dSJeff Roberson 	stp = td->td_sel;
132711b763dfSJeff Roberson 	n = 0;
13286affe1b7SMateusz Guzik 	only_user = FILEDESC_IS_ONLY_USER(fdp);
1329ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1330ace8398dSJeff Roberson 		fd = (int)(uintptr_t)sfp->sf_cookie;
1331ace8398dSJeff Roberson 		si = sfp->sf_si;
1332ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1333ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1334ace8398dSJeff Roberson 		if (si != NULL)
1335ace8398dSJeff Roberson 			continue;
13366affe1b7SMateusz Guzik 		if (only_user)
13376affe1b7SMateusz Guzik 			error = fget_only_user(fdp, fd, &cap_event_rights, &fp);
13386affe1b7SMateusz Guzik 		else
13396affe1b7SMateusz Guzik 			error = fget_unlocked(fdp, fd, &cap_event_rights, &fp);
13406affe1b7SMateusz Guzik 		if (__predict_false(error != 0))
1341a9d2f8d8SRobert Watson 			return (error);
134211b763dfSJeff Roberson 		idx = fd / NFDBITS;
13439cdacff1SJeff Roberson 		bit = (fd_mask)1 << (fd % NFDBITS);
134411b763dfSJeff Roberson 		ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
13456affe1b7SMateusz Guzik 		if (only_user)
13466affe1b7SMateusz Guzik 			fput_only_user(fdp, fp);
13476affe1b7SMateusz Guzik 		else
1348bf422e5fSJeff Roberson 			fdrop(fp, td);
134911b763dfSJeff Roberson 		if (ev != 0)
135011b763dfSJeff Roberson 			n += selsetbits(ibits, obits, idx, bit, ev);
1351ace8398dSJeff Roberson 	}
1352ace8398dSJeff Roberson 	stp->st_flags = 0;
1353ace8398dSJeff Roberson 	td->td_retval[0] = n;
1354ace8398dSJeff Roberson 	return (0);
1355ace8398dSJeff Roberson }
1356ace8398dSJeff Roberson 
1357ace8398dSJeff Roberson /*
1358ace8398dSJeff Roberson  * Perform the initial filedescriptor scan and register ourselves with
1359ace8398dSJeff Roberson  * each selinfo.
1360ace8398dSJeff Roberson  */
1361265fc98fSSeigo Tanimura static int
1362cc3c9df8SEd Maste selscan(struct thread *td, fd_mask **ibits, fd_mask **obits, int nfd)
1363df8bae1dSRodney W. Grimes {
136411b763dfSJeff Roberson 	struct filedesc *fdp;
1365df8bae1dSRodney W. Grimes 	struct file *fp;
13669cdacff1SJeff Roberson 	fd_mask bit;
136711b763dfSJeff Roberson 	int ev, flags, end, fd;
13689cdacff1SJeff Roberson 	int n, idx;
1369a9d2f8d8SRobert Watson 	int error;
13706affe1b7SMateusz Guzik 	bool only_user;
1371df8bae1dSRodney W. Grimes 
137211b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
137311b763dfSJeff Roberson 	n = 0;
13746affe1b7SMateusz Guzik 	only_user = FILEDESC_IS_ONLY_USER(fdp);
13759cdacff1SJeff Roberson 	for (idx = 0, fd = 0; fd < nfd; idx++) {
137611b763dfSJeff Roberson 		end = imin(fd + NFDBITS, nfd);
137711b763dfSJeff Roberson 		for (bit = 1; fd < end; bit <<= 1, fd++) {
137811b763dfSJeff Roberson 			/* Compute the list of events we're interested in. */
137911b763dfSJeff Roberson 			flags = selflags(ibits, idx, bit);
138011b763dfSJeff Roberson 			if (flags == 0)
1381f082218cSPeter Wemm 				continue;
13826affe1b7SMateusz Guzik 			if (only_user)
13836affe1b7SMateusz Guzik 				error = fget_only_user(fdp, fd, &cap_event_rights, &fp);
13846affe1b7SMateusz Guzik 			else
13856affe1b7SMateusz Guzik 				error = fget_unlocked(fdp, fd, &cap_event_rights, &fp);
13866affe1b7SMateusz Guzik 			if (__predict_false(error != 0))
1387a9d2f8d8SRobert Watson 				return (error);
1388ace8398dSJeff Roberson 			selfdalloc(td, (void *)(uintptr_t)fd);
138911b763dfSJeff Roberson 			ev = fo_poll(fp, flags, td->td_ucred, td);
13906affe1b7SMateusz Guzik 			if (only_user)
13916affe1b7SMateusz Guzik 				fput_only_user(fdp, fp);
13926affe1b7SMateusz Guzik 			else
1393bf422e5fSJeff Roberson 				fdrop(fp, td);
139411b763dfSJeff Roberson 			if (ev != 0)
139511b763dfSJeff Roberson 				n += selsetbits(ibits, obits, idx, bit, ev);
1396df8bae1dSRodney W. Grimes 		}
1397df8bae1dSRodney W. Grimes 	}
139811b763dfSJeff Roberson 
1399b40ce416SJulian Elischer 	td->td_retval[0] = n;
1400df8bae1dSRodney W. Grimes 	return (0);
1401df8bae1dSRodney W. Grimes }
1402df8bae1dSRodney W. Grimes 
140342d11757SPeter Wemm int
1404186d9c34SDmitry Chagin sys_poll(struct thread *td, struct poll_args *uap)
1405186d9c34SDmitry Chagin {
1406186d9c34SDmitry Chagin 	struct timespec ts, *tsp;
1407186d9c34SDmitry Chagin 
1408186d9c34SDmitry Chagin 	if (uap->timeout != INFTIM) {
1409186d9c34SDmitry Chagin 		if (uap->timeout < 0)
1410186d9c34SDmitry Chagin 			return (EINVAL);
1411186d9c34SDmitry Chagin 		ts.tv_sec = uap->timeout / 1000;
1412186d9c34SDmitry Chagin 		ts.tv_nsec = (uap->timeout % 1000) * 1000000;
1413186d9c34SDmitry Chagin 		tsp = &ts;
1414186d9c34SDmitry Chagin 	} else
1415186d9c34SDmitry Chagin 		tsp = NULL;
1416186d9c34SDmitry Chagin 
1417186d9c34SDmitry Chagin 	return (kern_poll(td, uap->fds, uap->nfds, tsp, NULL));
1418186d9c34SDmitry Chagin }
1419186d9c34SDmitry Chagin 
1420186d9c34SDmitry Chagin int
14212384981bSConrad Meyer kern_poll(struct thread *td, struct pollfd *ufds, u_int nfds,
1422186d9c34SDmitry Chagin     struct timespec *tsp, sigset_t *uset)
142342d11757SPeter Wemm {
14242384981bSConrad Meyer 	struct pollfd *kfds;
14252384981bSConrad Meyer 	struct pollfd stackfds[32];
1426186d9c34SDmitry Chagin 	sbintime_t sbt, precision, tmp;
1427186d9c34SDmitry Chagin 	time_t over;
1428186d9c34SDmitry Chagin 	struct timespec ts;
1429cf5e4fe6SDavide Italiano 	int error;
143042d11757SPeter Wemm 
1431186d9c34SDmitry Chagin 	precision = 0;
1432186d9c34SDmitry Chagin 	if (tsp != NULL) {
1433186d9c34SDmitry Chagin 		if (tsp->tv_sec < 0)
1434186d9c34SDmitry Chagin 			return (EINVAL);
1435186d9c34SDmitry Chagin 		if (tsp->tv_nsec < 0 || tsp->tv_nsec >= 1000000000)
1436186d9c34SDmitry Chagin 			return (EINVAL);
1437186d9c34SDmitry Chagin 		if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1438186d9c34SDmitry Chagin 			sbt = 0;
1439186d9c34SDmitry Chagin 		else {
1440186d9c34SDmitry Chagin 			ts = *tsp;
1441186d9c34SDmitry Chagin 			if (ts.tv_sec > INT32_MAX / 2) {
1442186d9c34SDmitry Chagin 				over = ts.tv_sec - INT32_MAX / 2;
1443186d9c34SDmitry Chagin 				ts.tv_sec -= over;
1444186d9c34SDmitry Chagin 			} else
1445186d9c34SDmitry Chagin 				over = 0;
1446186d9c34SDmitry Chagin 			tmp = tstosbt(ts);
1447186d9c34SDmitry Chagin 			precision = tmp;
1448186d9c34SDmitry Chagin 			precision >>= tc_precexp;
1449186d9c34SDmitry Chagin 			if (TIMESEL(&sbt, tmp))
1450186d9c34SDmitry Chagin 				sbt += tc_tick_sbt;
1451186d9c34SDmitry Chagin 			sbt += tmp;
1452186d9c34SDmitry Chagin 		}
1453186d9c34SDmitry Chagin 	} else
1454186d9c34SDmitry Chagin 		sbt = -1;
1455186d9c34SDmitry Chagin 
145678c2a980SConrad Meyer 	/*
145778c2a980SConrad Meyer 	 * This is kinda bogus.  We have fd limits, but that is not
145878c2a980SConrad Meyer 	 * really related to the size of the pollfd array.  Make sure
145978c2a980SConrad Meyer 	 * we let the process use at least FD_SETSIZE entries and at
146078c2a980SConrad Meyer 	 * least enough for the system-wide limits.  We want to be reasonably
146178c2a980SConrad Meyer 	 * safe, but not overly restrictive.
146278c2a980SConrad Meyer 	 */
1463374ae2a3SJeff Roberson 	if (nfds > maxfilesperproc && nfds > FD_SETSIZE)
1464ace8398dSJeff Roberson 		return (EINVAL);
14652384981bSConrad Meyer 	if (nfds > nitems(stackfds))
14662384981bSConrad Meyer 		kfds = mallocarray(nfds, sizeof(*kfds), M_TEMP, M_WAITOK);
146742d11757SPeter Wemm 	else
14682384981bSConrad Meyer 		kfds = stackfds;
14692384981bSConrad Meyer 	error = copyin(ufds, kfds, nfds * sizeof(*kfds));
147042d11757SPeter Wemm 	if (error)
1471ace8398dSJeff Roberson 		goto done;
1472186d9c34SDmitry Chagin 
1473186d9c34SDmitry Chagin 	if (uset != NULL) {
1474186d9c34SDmitry Chagin 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
1475186d9c34SDmitry Chagin 		    &td->td_oldsigmask, 0);
1476186d9c34SDmitry Chagin 		if (error)
1477ace8398dSJeff Roberson 			goto done;
1478186d9c34SDmitry Chagin 		td->td_pflags |= TDP_OLDMASK;
1479186d9c34SDmitry Chagin 		/*
1480186d9c34SDmitry Chagin 		 * Make sure that ast() is called on return to
1481186d9c34SDmitry Chagin 		 * usermode and TDP_OLDMASK is cleared, restoring old
1482186d9c34SDmitry Chagin 		 * sigmask.
1483186d9c34SDmitry Chagin 		 */
1484186d9c34SDmitry Chagin 		thread_lock(td);
1485186d9c34SDmitry Chagin 		td->td_flags |= TDF_ASTPENDING;
1486186d9c34SDmitry Chagin 		thread_unlock(td);
148742d11757SPeter Wemm 	}
1488186d9c34SDmitry Chagin 
1489ace8398dSJeff Roberson 	seltdinit(td);
1490ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1491ace8398dSJeff Roberson 	for (;;) {
14922384981bSConrad Meyer 		error = pollscan(td, kfds, nfds);
1493ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1494ace8398dSJeff Roberson 			break;
1495186d9c34SDmitry Chagin 		error = seltdwait(td, sbt, precision);
1496ace8398dSJeff Roberson 		if (error)
1497ace8398dSJeff Roberson 			break;
1498ace8398dSJeff Roberson 		error = pollrescan(td);
1499ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1500ace8398dSJeff Roberson 			break;
150185f190e4SAlfred Perlstein 	}
1502ace8398dSJeff Roberson 	seltdclear(td);
1503265fc98fSSeigo Tanimura 
150442d11757SPeter Wemm done:
150542d11757SPeter Wemm 	/* poll is not restarted after signals... */
150642d11757SPeter Wemm 	if (error == ERESTART)
150742d11757SPeter Wemm 		error = EINTR;
150842d11757SPeter Wemm 	if (error == EWOULDBLOCK)
150942d11757SPeter Wemm 		error = 0;
151042d11757SPeter Wemm 	if (error == 0) {
15112384981bSConrad Meyer 		error = pollout(td, kfds, ufds, nfds);
151242d11757SPeter Wemm 		if (error)
151342d11757SPeter Wemm 			goto out;
151442d11757SPeter Wemm 	}
151542d11757SPeter Wemm out:
15162384981bSConrad Meyer 	if (nfds > nitems(stackfds))
15172384981bSConrad Meyer 		free(kfds, M_TEMP);
151842d11757SPeter Wemm 	return (error);
151942d11757SPeter Wemm }
152042d11757SPeter Wemm 
1521186d9c34SDmitry Chagin int
1522186d9c34SDmitry Chagin sys_ppoll(struct thread *td, struct ppoll_args *uap)
1523186d9c34SDmitry Chagin {
1524186d9c34SDmitry Chagin 	struct timespec ts, *tsp;
1525186d9c34SDmitry Chagin 	sigset_t set, *ssp;
1526186d9c34SDmitry Chagin 	int error;
1527186d9c34SDmitry Chagin 
1528186d9c34SDmitry Chagin 	if (uap->ts != NULL) {
1529186d9c34SDmitry Chagin 		error = copyin(uap->ts, &ts, sizeof(ts));
1530186d9c34SDmitry Chagin 		if (error)
1531186d9c34SDmitry Chagin 			return (error);
1532186d9c34SDmitry Chagin 		tsp = &ts;
1533186d9c34SDmitry Chagin 	} else
1534186d9c34SDmitry Chagin 		tsp = NULL;
1535186d9c34SDmitry Chagin 	if (uap->set != NULL) {
1536186d9c34SDmitry Chagin 		error = copyin(uap->set, &set, sizeof(set));
1537186d9c34SDmitry Chagin 		if (error)
1538186d9c34SDmitry Chagin 			return (error);
1539186d9c34SDmitry Chagin 		ssp = &set;
1540186d9c34SDmitry Chagin 	} else
1541186d9c34SDmitry Chagin 		ssp = NULL;
1542186d9c34SDmitry Chagin 	/*
1543186d9c34SDmitry Chagin 	 * fds is still a pointer to user space. kern_poll() will
1544186d9c34SDmitry Chagin 	 * take care of copyin that array to the kernel space.
1545186d9c34SDmitry Chagin 	 */
1546186d9c34SDmitry Chagin 
1547186d9c34SDmitry Chagin 	return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
1548186d9c34SDmitry Chagin }
1549186d9c34SDmitry Chagin 
155042d11757SPeter Wemm static int
1551ace8398dSJeff Roberson pollrescan(struct thread *td)
1552ace8398dSJeff Roberson {
1553ace8398dSJeff Roberson 	struct seltd *stp;
1554ace8398dSJeff Roberson 	struct selfd *sfp;
1555ace8398dSJeff Roberson 	struct selfd *sfn;
1556ace8398dSJeff Roberson 	struct selinfo *si;
1557ace8398dSJeff Roberson 	struct filedesc *fdp;
1558ace8398dSJeff Roberson 	struct file *fp;
1559ace8398dSJeff Roberson 	struct pollfd *fd;
1560*45e1f854SMateusz Guzik 	int n, error;
1561*45e1f854SMateusz Guzik 	bool only_user;
1562ace8398dSJeff Roberson 
1563ace8398dSJeff Roberson 	n = 0;
1564ace8398dSJeff Roberson 	fdp = td->td_proc->p_fd;
1565ace8398dSJeff Roberson 	stp = td->td_sel;
1566*45e1f854SMateusz Guzik 	only_user = FILEDESC_IS_ONLY_USER(fdp);
1567ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1568ace8398dSJeff Roberson 		fd = (struct pollfd *)sfp->sf_cookie;
1569ace8398dSJeff Roberson 		si = sfp->sf_si;
1570ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1571ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1572ace8398dSJeff Roberson 		if (si != NULL)
1573ace8398dSJeff Roberson 			continue;
1574*45e1f854SMateusz Guzik 		if (only_user)
1575*45e1f854SMateusz Guzik 			error = fget_only_user(fdp, fd->fd, &cap_event_rights, &fp);
1576*45e1f854SMateusz Guzik 		else
1577*45e1f854SMateusz Guzik 			error = fget_unlocked(fdp, fd->fd, &cap_event_rights, &fp);
1578*45e1f854SMateusz Guzik 		if (__predict_false(error != 0)) {
1579ace8398dSJeff Roberson 			fd->revents = POLLNVAL;
1580ace8398dSJeff Roberson 			n++;
1581ace8398dSJeff Roberson 			continue;
1582ace8398dSJeff Roberson 		}
1583ace8398dSJeff Roberson 		/*
1584ace8398dSJeff Roberson 		 * Note: backend also returns POLLHUP and
1585ace8398dSJeff Roberson 		 * POLLERR if appropriate.
1586ace8398dSJeff Roberson 		 */
1587ace8398dSJeff Roberson 		fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1588*45e1f854SMateusz Guzik 		if (only_user)
1589*45e1f854SMateusz Guzik 			fput_only_user(fdp, fp);
1590*45e1f854SMateusz Guzik 		else
1591*45e1f854SMateusz Guzik 			fdrop(fp, td);
1592ace8398dSJeff Roberson 		if (fd->revents != 0)
1593ace8398dSJeff Roberson 			n++;
1594ace8398dSJeff Roberson 	}
1595ace8398dSJeff Roberson 	stp->st_flags = 0;
1596ace8398dSJeff Roberson 	td->td_retval[0] = n;
1597ace8398dSJeff Roberson 	return (0);
1598ace8398dSJeff Roberson }
1599ace8398dSJeff Roberson 
1600ace8398dSJeff Roberson static int
1601cc3c9df8SEd Maste pollout(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd)
1602ae81968fSRobert Watson {
1603ae81968fSRobert Watson 	int error = 0;
1604ae81968fSRobert Watson 	u_int i = 0;
16056d8feddaSKonstantin Belousov 	u_int n = 0;
1606ae81968fSRobert Watson 
1607ae81968fSRobert Watson 	for (i = 0; i < nfd; i++) {
1608ae81968fSRobert Watson 		error = copyout(&fds->revents, &ufds->revents,
1609ae81968fSRobert Watson 		    sizeof(ufds->revents));
1610ae81968fSRobert Watson 		if (error)
1611ae81968fSRobert Watson 			return (error);
16126d8feddaSKonstantin Belousov 		if (fds->revents != 0)
16136d8feddaSKonstantin Belousov 			n++;
1614ae81968fSRobert Watson 		fds++;
1615ae81968fSRobert Watson 		ufds++;
1616ae81968fSRobert Watson 	}
16176d8feddaSKonstantin Belousov 	td->td_retval[0] = n;
1618ae81968fSRobert Watson 	return (0);
1619ae81968fSRobert Watson }
1620ae81968fSRobert Watson 
1621ae81968fSRobert Watson static int
1622cc3c9df8SEd Maste pollscan(struct thread *td, struct pollfd *fds, u_int nfd)
162342d11757SPeter Wemm {
1624b1607c87SMateusz Guzik 	struct filedesc *fdp;
162542d11757SPeter Wemm 	struct file *fp;
1626*45e1f854SMateusz Guzik 	int i, n, error;
1627*45e1f854SMateusz Guzik 	bool only_user;
162842d11757SPeter Wemm 
1629b1607c87SMateusz Guzik 	n = 0;
1630b1607c87SMateusz Guzik 	fdp = td->td_proc->p_fd;
1631*45e1f854SMateusz Guzik 	only_user = FILEDESC_IS_ONLY_USER(fdp);
1632eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
1633b1607c87SMateusz Guzik 		if (fds->fd < 0) {
1634337c9691SJordan K. Hubbard 			fds->revents = 0;
1635b1607c87SMateusz Guzik 			continue;
1636b1607c87SMateusz Guzik 		}
1637*45e1f854SMateusz Guzik 		if (only_user)
1638*45e1f854SMateusz Guzik 			error = fget_only_user(fdp, fds->fd, &cap_event_rights, &fp);
1639*45e1f854SMateusz Guzik 		else
1640*45e1f854SMateusz Guzik 			error = fget_unlocked(fdp, fds->fd, &cap_event_rights, &fp);
1641*45e1f854SMateusz Guzik 		if (__predict_false(error != 0)) {
164242d11757SPeter Wemm 			fds->revents = POLLNVAL;
164342d11757SPeter Wemm 			n++;
1644b1607c87SMateusz Guzik 			continue;
1645b1607c87SMateusz Guzik 		}
16462087c896SBruce Evans 		/*
16472087c896SBruce Evans 		 * Note: backend also returns POLLHUP and
16482087c896SBruce Evans 		 * POLLERR if appropriate.
16492087c896SBruce Evans 		 */
1650ace8398dSJeff Roberson 		selfdalloc(td, fds);
165113ccadd4SBrian Feldman 		fds->revents = fo_poll(fp, fds->events,
1652ea6027a8SRobert Watson 		    td->td_ucred, td);
1653*45e1f854SMateusz Guzik 		if (only_user)
1654*45e1f854SMateusz Guzik 			fput_only_user(fdp, fp);
1655*45e1f854SMateusz Guzik 		else
1656*45e1f854SMateusz Guzik 			fdrop(fp, td);
1657f2159cc7SKonstantin Belousov 		/*
1658f2159cc7SKonstantin Belousov 		 * POSIX requires POLLOUT to be never
1659f2159cc7SKonstantin Belousov 		 * set simultaneously with POLLHUP.
1660f2159cc7SKonstantin Belousov 		 */
1661f2159cc7SKonstantin Belousov 		if ((fds->revents & POLLHUP) != 0)
1662f2159cc7SKonstantin Belousov 			fds->revents &= ~POLLOUT;
1663f2159cc7SKonstantin Belousov 
166442d11757SPeter Wemm 		if (fds->revents != 0)
166542d11757SPeter Wemm 			n++;
166642d11757SPeter Wemm 	}
1667b40ce416SJulian Elischer 	td->td_retval[0] = n;
166842d11757SPeter Wemm 	return (0);
166942d11757SPeter Wemm }
167042d11757SPeter Wemm 
167142d11757SPeter Wemm /*
1672237abf0cSDavide Italiano  * XXX This was created specifically to support netncp and netsmb.  This
1673237abf0cSDavide Italiano  * allows the caller to specify a socket to wait for events on.  It returns
1674237abf0cSDavide Italiano  * 0 if any events matched and an error otherwise.  There is no way to
1675237abf0cSDavide Italiano  * determine which events fired.
1676237abf0cSDavide Italiano  */
1677237abf0cSDavide Italiano int
1678237abf0cSDavide Italiano selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td)
1679237abf0cSDavide Italiano {
1680237abf0cSDavide Italiano 	struct timeval rtv;
1681237abf0cSDavide Italiano 	sbintime_t asbt, precision, rsbt;
1682237abf0cSDavide Italiano 	int error;
1683237abf0cSDavide Italiano 
168476665df9SPeter Wemm 	precision = 0;	/* stupid gcc! */
1685237abf0cSDavide Italiano 	if (tvp != NULL) {
1686237abf0cSDavide Italiano 		rtv = *tvp;
1687237abf0cSDavide Italiano 		if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1688237abf0cSDavide Italiano 		    rtv.tv_usec >= 1000000)
1689237abf0cSDavide Italiano 			return (EINVAL);
1690237abf0cSDavide Italiano 		if (!timevalisset(&rtv))
1691237abf0cSDavide Italiano 			asbt = 0;
1692237abf0cSDavide Italiano 		else if (rtv.tv_sec <= INT32_MAX) {
1693237abf0cSDavide Italiano 			rsbt = tvtosbt(rtv);
1694237abf0cSDavide Italiano 			precision = rsbt;
1695237abf0cSDavide Italiano 			precision >>= tc_precexp;
1696237abf0cSDavide Italiano 			if (TIMESEL(&asbt, rsbt))
1697237abf0cSDavide Italiano 				asbt += tc_tick_sbt;
16984bc38a5aSDavide Italiano 			if (asbt <= SBT_MAX - rsbt)
1699237abf0cSDavide Italiano 				asbt += rsbt;
1700237abf0cSDavide Italiano 			else
1701237abf0cSDavide Italiano 				asbt = -1;
1702237abf0cSDavide Italiano 		} else
1703237abf0cSDavide Italiano 			asbt = -1;
1704237abf0cSDavide Italiano 	} else
1705237abf0cSDavide Italiano 		asbt = -1;
1706237abf0cSDavide Italiano 	seltdinit(td);
1707237abf0cSDavide Italiano 	/*
1708237abf0cSDavide Italiano 	 * Iterate until the timeout expires or the socket becomes ready.
1709237abf0cSDavide Italiano 	 */
1710237abf0cSDavide Italiano 	for (;;) {
1711237abf0cSDavide Italiano 		selfdalloc(td, NULL);
1712237abf0cSDavide Italiano 		error = sopoll(so, events, NULL, td);
1713237abf0cSDavide Italiano 		/* error here is actually the ready events. */
1714237abf0cSDavide Italiano 		if (error)
1715237abf0cSDavide Italiano 			return (0);
1716237abf0cSDavide Italiano 		error = seltdwait(td, asbt, precision);
1717237abf0cSDavide Italiano 		if (error)
1718237abf0cSDavide Italiano 			break;
1719237abf0cSDavide Italiano 	}
1720237abf0cSDavide Italiano 	seltdclear(td);
1721237abf0cSDavide Italiano 	/* XXX Duplicates ncp/smb behavior. */
1722237abf0cSDavide Italiano 	if (error == ERESTART)
1723237abf0cSDavide Italiano 		error = 0;
1724237abf0cSDavide Italiano 	return (error);
1725237abf0cSDavide Italiano }
1726237abf0cSDavide Italiano 
1727237abf0cSDavide Italiano /*
1728ace8398dSJeff Roberson  * Preallocate two selfds associated with 'cookie'.  Some fo_poll routines
1729ace8398dSJeff Roberson  * have two select sets, one for read and another for write.
1730ace8398dSJeff Roberson  */
1731ace8398dSJeff Roberson static void
1732ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie)
1733ace8398dSJeff Roberson {
1734ace8398dSJeff Roberson 	struct seltd *stp;
1735ace8398dSJeff Roberson 
1736ace8398dSJeff Roberson 	stp = td->td_sel;
1737ace8398dSJeff Roberson 	if (stp->st_free1 == NULL)
1738ea33cca9SMateusz Guzik 		stp->st_free1 = malloc(sizeof(*stp->st_free1), M_SELFD, M_WAITOK|M_ZERO);
1739ace8398dSJeff Roberson 	stp->st_free1->sf_td = stp;
1740ace8398dSJeff Roberson 	stp->st_free1->sf_cookie = cookie;
1741ace8398dSJeff Roberson 	if (stp->st_free2 == NULL)
1742ea33cca9SMateusz Guzik 		stp->st_free2 = malloc(sizeof(*stp->st_free2), M_SELFD, M_WAITOK|M_ZERO);
1743ace8398dSJeff Roberson 	stp->st_free2->sf_td = stp;
1744ace8398dSJeff Roberson 	stp->st_free2->sf_cookie = cookie;
1745ace8398dSJeff Roberson }
1746ace8398dSJeff Roberson 
1747ace8398dSJeff Roberson static void
1748ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp)
1749ace8398dSJeff Roberson {
1750ace8398dSJeff Roberson 	STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
175131b2ac4bSMateusz Guzik 	/*
175231b2ac4bSMateusz Guzik 	 * Paired with doselwakeup.
175331b2ac4bSMateusz Guzik 	 */
175431b2ac4bSMateusz Guzik 	if (atomic_load_acq_ptr((uintptr_t *)&sfp->sf_si) != (uintptr_t)NULL) {
1755ace8398dSJeff Roberson 		mtx_lock(sfp->sf_mtx);
1756fcb5b3a4SKonstantin Belousov 		if (sfp->sf_si != NULL) {
1757ace8398dSJeff Roberson 			TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1758fcb5b3a4SKonstantin Belousov 		}
1759ace8398dSJeff Roberson 		mtx_unlock(sfp->sf_mtx);
176073f2e5f7SMateusz Guzik 	}
1761ea33cca9SMateusz Guzik 	free(sfp, M_SELFD);
176285f190e4SAlfred Perlstein }
176385f190e4SAlfred Perlstein 
17646aba400aSAttilio Rao /* Drain the waiters tied to all the selfd belonging the specified selinfo. */
17656aba400aSAttilio Rao void
1766cc3c9df8SEd Maste seldrain(struct selinfo *sip)
17676aba400aSAttilio Rao {
17686aba400aSAttilio Rao 
17696aba400aSAttilio Rao 	/*
17706aba400aSAttilio Rao 	 * This feature is already provided by doselwakeup(), thus it is
17716aba400aSAttilio Rao 	 * enough to go for it.
17726aba400aSAttilio Rao 	 * Eventually, the context, should take care to avoid races
17736aba400aSAttilio Rao 	 * between thread calling select()/poll() and file descriptor
17746aba400aSAttilio Rao 	 * detaching, but, again, the races are just the same as
17756aba400aSAttilio Rao 	 * selwakeup().
17766aba400aSAttilio Rao 	 */
17776aba400aSAttilio Rao         doselwakeup(sip, -1);
17786aba400aSAttilio Rao }
17796aba400aSAttilio Rao 
1780df8bae1dSRodney W. Grimes /*
1781df8bae1dSRodney W. Grimes  * Record a select request.
1782df8bae1dSRodney W. Grimes  */
1783df8bae1dSRodney W. Grimes void
1784cc3c9df8SEd Maste selrecord(struct thread *selector, struct selinfo *sip)
1785df8bae1dSRodney W. Grimes {
1786ace8398dSJeff Roberson 	struct selfd *sfp;
1787ace8398dSJeff Roberson 	struct seltd *stp;
1788ace8398dSJeff Roberson 	struct mtx *mtxp;
1789df8bae1dSRodney W. Grimes 
1790ace8398dSJeff Roberson 	stp = selector->td_sel;
179185f190e4SAlfred Perlstein 	/*
1792ace8398dSJeff Roberson 	 * Don't record when doing a rescan.
179385f190e4SAlfred Perlstein 	 */
1794ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_RESCAN)
1795ace8398dSJeff Roberson 		return;
1796ace8398dSJeff Roberson 	/*
1797ace8398dSJeff Roberson 	 * Grab one of the preallocated descriptors.
1798ace8398dSJeff Roberson 	 */
1799ace8398dSJeff Roberson 	sfp = NULL;
1800ace8398dSJeff Roberson 	if ((sfp = stp->st_free1) != NULL)
1801ace8398dSJeff Roberson 		stp->st_free1 = NULL;
1802ace8398dSJeff Roberson 	else if ((sfp = stp->st_free2) != NULL)
1803ace8398dSJeff Roberson 		stp->st_free2 = NULL;
1804ace8398dSJeff Roberson 	else
1805ace8398dSJeff Roberson 		panic("selrecord: No free selfd on selq");
18062141453eSJeff Roberson 	mtxp = sip->si_mtx;
18072141453eSJeff Roberson 	if (mtxp == NULL)
18082141453eSJeff Roberson 		mtxp = mtx_pool_find(mtxpool_select, sip);
1809ace8398dSJeff Roberson 	/*
1810ace8398dSJeff Roberson 	 * Initialize the sfp and queue it in the thread.
1811ace8398dSJeff Roberson 	 */
1812ace8398dSJeff Roberson 	sfp->sf_si = sip;
1813ace8398dSJeff Roberson 	sfp->sf_mtx = mtxp;
1814ace8398dSJeff Roberson 	STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1815ace8398dSJeff Roberson 	/*
1816ace8398dSJeff Roberson 	 * Now that we've locked the sip, check for initialization.
1817ace8398dSJeff Roberson 	 */
1818ace8398dSJeff Roberson 	mtx_lock(mtxp);
1819ace8398dSJeff Roberson 	if (sip->si_mtx == NULL) {
1820ace8398dSJeff Roberson 		sip->si_mtx = mtxp;
1821ace8398dSJeff Roberson 		TAILQ_INIT(&sip->si_tdlist);
182285f190e4SAlfred Perlstein 	}
1823ace8398dSJeff Roberson 	/*
1824ace8398dSJeff Roberson 	 * Add this thread to the list of selfds listening on this selinfo.
1825ace8398dSJeff Roberson 	 */
1826ace8398dSJeff Roberson 	TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1827ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1828df8bae1dSRodney W. Grimes }
1829df8bae1dSRodney W. Grimes 
1830512824f8SSeigo Tanimura /* Wake up a selecting thread. */
1831df8bae1dSRodney W. Grimes void
1832cc3c9df8SEd Maste selwakeup(struct selinfo *sip)
1833df8bae1dSRodney W. Grimes {
1834512824f8SSeigo Tanimura 	doselwakeup(sip, -1);
1835512824f8SSeigo Tanimura }
1836512824f8SSeigo Tanimura 
1837512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */
1838512824f8SSeigo Tanimura void
1839cc3c9df8SEd Maste selwakeuppri(struct selinfo *sip, int pri)
1840512824f8SSeigo Tanimura {
1841512824f8SSeigo Tanimura 	doselwakeup(sip, pri);
1842512824f8SSeigo Tanimura }
1843512824f8SSeigo Tanimura 
1844512824f8SSeigo Tanimura /*
1845512824f8SSeigo Tanimura  * Do a wakeup when a selectable event occurs.
1846512824f8SSeigo Tanimura  */
1847512824f8SSeigo Tanimura static void
1848cc3c9df8SEd Maste doselwakeup(struct selinfo *sip, int pri)
1849512824f8SSeigo Tanimura {
1850ace8398dSJeff Roberson 	struct selfd *sfp;
1851ace8398dSJeff Roberson 	struct selfd *sfn;
1852ace8398dSJeff Roberson 	struct seltd *stp;
1853df8bae1dSRodney W. Grimes 
1854ace8398dSJeff Roberson 	/* If it's not initialized there can't be any waiters. */
1855ace8398dSJeff Roberson 	if (sip->si_mtx == NULL)
1856b40ce416SJulian Elischer 		return;
1857ace8398dSJeff Roberson 	/*
1858ace8398dSJeff Roberson 	 * Locking the selinfo locks all selfds associated with it.
1859ace8398dSJeff Roberson 	 */
1860ace8398dSJeff Roberson 	mtx_lock(sip->si_mtx);
1861ace8398dSJeff Roberson 	TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1862ace8398dSJeff Roberson 		/*
1863ace8398dSJeff Roberson 		 * Once we remove this sfp from the list and clear the
1864ace8398dSJeff Roberson 		 * sf_si seltdclear will know to ignore this si.
1865ace8398dSJeff Roberson 		 */
1866ace8398dSJeff Roberson 		TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1867ace8398dSJeff Roberson 		stp = sfp->sf_td;
1868ace8398dSJeff Roberson 		mtx_lock(&stp->st_mtx);
1869ace8398dSJeff Roberson 		stp->st_flags |= SELTD_PENDING;
1870ace8398dSJeff Roberson 		cv_broadcastpri(&stp->st_wait, pri);
1871ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
187210e64782SMateusz Guzik 		/*
187310e64782SMateusz Guzik 		 * Paired with selfdfree.
187410e64782SMateusz Guzik 		 *
187510e64782SMateusz Guzik 		 * Storing this only after the wakeup provides an invariant that
187610e64782SMateusz Guzik 		 * stp is not used after selfdfree returns.
187710e64782SMateusz Guzik 		 */
187810e64782SMateusz Guzik 		atomic_store_rel_ptr((uintptr_t *)&sfp->sf_si, (uintptr_t)NULL);
1879b40ce416SJulian Elischer 	}
1880ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1881ace8398dSJeff Roberson }
1882ace8398dSJeff Roberson 
1883ace8398dSJeff Roberson static void
1884ace8398dSJeff Roberson seltdinit(struct thread *td)
1885ace8398dSJeff Roberson {
1886ace8398dSJeff Roberson 	struct seltd *stp;
1887ace8398dSJeff Roberson 
188810e64782SMateusz Guzik 	stp = td->td_sel;
188910e64782SMateusz Guzik 	if (stp != NULL) {
189010e64782SMateusz Guzik 		MPASS(stp->st_flags == 0);
189110e64782SMateusz Guzik 		MPASS(STAILQ_EMPTY(&stp->st_selq));
189210e64782SMateusz Guzik 		return;
189310e64782SMateusz Guzik 	}
189410e64782SMateusz Guzik 	stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1895ace8398dSJeff Roberson 	mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1896ace8398dSJeff Roberson 	cv_init(&stp->st_wait, "select");
1897ace8398dSJeff Roberson 	stp->st_flags = 0;
1898ace8398dSJeff Roberson 	STAILQ_INIT(&stp->st_selq);
189910e64782SMateusz Guzik 	td->td_sel = stp;
1900ace8398dSJeff Roberson }
1901ace8398dSJeff Roberson 
1902ace8398dSJeff Roberson static int
1903cf5e4fe6SDavide Italiano seltdwait(struct thread *td, sbintime_t sbt, sbintime_t precision)
1904ace8398dSJeff Roberson {
1905ace8398dSJeff Roberson 	struct seltd *stp;
1906ace8398dSJeff Roberson 	int error;
1907ace8398dSJeff Roberson 
1908ace8398dSJeff Roberson 	stp = td->td_sel;
1909ace8398dSJeff Roberson 	/*
1910ace8398dSJeff Roberson 	 * An event of interest may occur while we do not hold the seltd
1911ace8398dSJeff Roberson 	 * locked so check the pending flag before we sleep.
1912ace8398dSJeff Roberson 	 */
1913ace8398dSJeff Roberson 	mtx_lock(&stp->st_mtx);
1914ace8398dSJeff Roberson 	/*
1915ace8398dSJeff Roberson 	 * Any further calls to selrecord will be a rescan.
1916ace8398dSJeff Roberson 	 */
1917ace8398dSJeff Roberson 	stp->st_flags |= SELTD_RESCAN;
1918ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_PENDING) {
1919ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1920ace8398dSJeff Roberson 		return (0);
1921ace8398dSJeff Roberson 	}
1922cf5e4fe6SDavide Italiano 	if (sbt == 0)
1923cf5e4fe6SDavide Italiano 		error = EWOULDBLOCK;
1924cf5e4fe6SDavide Italiano 	else if (sbt != -1)
1925cf5e4fe6SDavide Italiano 		error = cv_timedwait_sig_sbt(&stp->st_wait, &stp->st_mtx,
1926cf5e4fe6SDavide Italiano 		    sbt, precision, C_ABSOLUTE);
1927ace8398dSJeff Roberson 	else
1928ace8398dSJeff Roberson 		error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
1929ace8398dSJeff Roberson 	mtx_unlock(&stp->st_mtx);
1930ace8398dSJeff Roberson 
1931ace8398dSJeff Roberson 	return (error);
1932ace8398dSJeff Roberson }
1933ace8398dSJeff Roberson 
1934ace8398dSJeff Roberson void
1935ace8398dSJeff Roberson seltdfini(struct thread *td)
1936ace8398dSJeff Roberson {
1937ace8398dSJeff Roberson 	struct seltd *stp;
1938ace8398dSJeff Roberson 
1939ace8398dSJeff Roberson 	stp = td->td_sel;
1940ace8398dSJeff Roberson 	if (stp == NULL)
1941ace8398dSJeff Roberson 		return;
194210e64782SMateusz Guzik 	MPASS(stp->st_flags == 0);
194310e64782SMateusz Guzik 	MPASS(STAILQ_EMPTY(&stp->st_selq));
1944ace8398dSJeff Roberson 	if (stp->st_free1)
1945ea33cca9SMateusz Guzik 		free(stp->st_free1, M_SELFD);
1946ace8398dSJeff Roberson 	if (stp->st_free2)
1947ea33cca9SMateusz Guzik 		free(stp->st_free2, M_SELFD);
1948ace8398dSJeff Roberson 	td->td_sel = NULL;
194936bce27bSKonstantin Belousov 	cv_destroy(&stp->st_wait);
195036bce27bSKonstantin Belousov 	mtx_destroy(&stp->st_mtx);
1951ace8398dSJeff Roberson 	free(stp, M_SELECT);
1952ace8398dSJeff Roberson }
1953ace8398dSJeff Roberson 
1954ace8398dSJeff Roberson /*
1955ace8398dSJeff Roberson  * Remove the references to the thread from all of the objects we were
1956ace8398dSJeff Roberson  * polling.
1957ace8398dSJeff Roberson  */
1958ace8398dSJeff Roberson static void
1959ace8398dSJeff Roberson seltdclear(struct thread *td)
1960ace8398dSJeff Roberson {
1961ace8398dSJeff Roberson 	struct seltd *stp;
1962ace8398dSJeff Roberson 	struct selfd *sfp;
1963ace8398dSJeff Roberson 	struct selfd *sfn;
1964ace8398dSJeff Roberson 
1965ace8398dSJeff Roberson 	stp = td->td_sel;
1966ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
1967ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1968ace8398dSJeff Roberson 	stp->st_flags = 0;
1969df8bae1dSRodney W. Grimes }
1970265fc98fSSeigo Tanimura 
19714d77a549SAlfred Perlstein static void selectinit(void *);
1972ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
1973265fc98fSSeigo Tanimura static void
1974ace8398dSJeff Roberson selectinit(void *dummy __unused)
1975265fc98fSSeigo Tanimura {
19762141453eSJeff Roberson 
19772141453eSJeff Roberson 	mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);
1978265fc98fSSeigo Tanimura }
19790acf5d0bSMark Johnston 
19800acf5d0bSMark Johnston /*
19810acf5d0bSMark Johnston  * Set up a syscall return value that follows the convention specified for
19820acf5d0bSMark Johnston  * posix_* functions.
19830acf5d0bSMark Johnston  */
19840acf5d0bSMark Johnston int
19850acf5d0bSMark Johnston kern_posix_error(struct thread *td, int error)
19860acf5d0bSMark Johnston {
19870acf5d0bSMark Johnston 
19880acf5d0bSMark Johnston 	if (error <= 0)
19890acf5d0bSMark Johnston 		return (error);
19900acf5d0bSMark Johnston 	td->td_errno = error;
19910acf5d0bSMark Johnston 	td->td_pflags |= TDP_NERRNO;
19920acf5d0bSMark Johnston 	td->td_retval[0] = error;
19930acf5d0bSMark Johnston 	return (0);
19940acf5d0bSMark Johnston }
1995