xref: /freebsd/sys/kern/sys_generic.c (revision 8bb9a904d58713c59b66e7f35ae6865260620e6f)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *	@(#)sys_generic.c	8.5 (Berkeley) 1/21/94
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37677b542eSDavid E. O'Brien #include <sys/cdefs.h>
38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
39677b542eSDavid E. O'Brien 
40a9d2f8d8SRobert Watson #include "opt_capsicum.h"
412de92a38SPeter Wemm #include "opt_compat.h"
42db6a20e2SGarrett Wollman #include "opt_ktrace.h"
43db6a20e2SGarrett Wollman 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
46d2d3e875SBruce Evans #include <sys/sysproto.h>
47a9d2f8d8SRobert Watson #include <sys/capability.h>
48df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
4920982410SBruce Evans #include <sys/filio.h>
503ac4d1efSBruce Evans #include <sys/fcntl.h>
51df8bae1dSRodney W. Grimes #include <sys/file.h>
52df8bae1dSRodney W. Grimes #include <sys/proc.h>
53797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
54df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
55df8bae1dSRodney W. Grimes #include <sys/uio.h>
56df8bae1dSRodney W. Grimes #include <sys/kernel.h>
57e4650294SJohn Baldwin #include <sys/ktr.h>
58104a9b7eSAlexander Kabaev #include <sys/limits.h>
59df8bae1dSRodney W. Grimes #include <sys/malloc.h>
6042d11757SPeter Wemm #include <sys/poll.h>
6189b71647SPeter Wemm #include <sys/resourcevar.h>
620a2c3d48SGarrett Wollman #include <sys/selinfo.h>
6344f3b092SJohn Baldwin #include <sys/sleepqueue.h>
648f19eb88SIan Dowse #include <sys/syscallsubr.h>
658cb96f20SPeter Wemm #include <sys/sysctl.h>
6642d11757SPeter Wemm #include <sys/sysent.h>
679bbee259SAndrey A. Chernov #include <sys/vnode.h>
68279d7226SMatthew Dillon #include <sys/bio.h>
69279d7226SMatthew Dillon #include <sys/buf.h>
70265fc98fSSeigo Tanimura #include <sys/condvar.h>
71df8bae1dSRodney W. Grimes #ifdef KTRACE
72df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
73df8bae1dSRodney W. Grimes #endif
74df8bae1dSRodney W. Grimes 
75e4650294SJohn Baldwin #include <security/audit/audit.h>
76ace8398dSJeff Roberson 
77526d0bd5SKonstantin Belousov int iosize_max_clamp = 1;
78*8bb9a904SKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, iosize_max_clamp, CTLFLAG_RW,
79*8bb9a904SKonstantin Belousov     &iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX");
80*8bb9a904SKonstantin Belousov /*
81*8bb9a904SKonstantin Belousov  * Assert that the return value of read(2) and write(2) syscalls fits
82*8bb9a904SKonstantin Belousov  * into a register.  If not, an architecture will need to provide the
83*8bb9a904SKonstantin Belousov  * usermode wrappers to reconstruct the result.
84*8bb9a904SKonstantin Belousov  */
85*8bb9a904SKonstantin Belousov CTASSERT(sizeof(register_t) >= sizeof(size_t));
86526d0bd5SKonstantin Belousov 
87a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
88a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
89a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's");
9055166637SPoul-Henning Kamp 
916d8feddaSKonstantin Belousov static int	pollout(struct thread *, struct pollfd *, struct pollfd *,
926d8feddaSKonstantin Belousov 		    u_int);
93bbbb04ceSAlfred Perlstein static int	pollscan(struct thread *, struct pollfd *, u_int);
94ace8398dSJeff Roberson static int	pollrescan(struct thread *);
95bbbb04ceSAlfred Perlstein static int	selscan(struct thread *, fd_mask **, fd_mask **, int);
96ace8398dSJeff Roberson static int	selrescan(struct thread *, fd_mask **, fd_mask **);
97ace8398dSJeff Roberson static void	selfdalloc(struct thread *, void *);
98ace8398dSJeff Roberson static void	selfdfree(struct seltd *, struct selfd *);
99bcd9e0ddSJohn Baldwin static int	dofileread(struct thread *, int, struct file *, struct uio *,
100bcd9e0ddSJohn Baldwin 		    off_t, int);
101bcd9e0ddSJohn Baldwin static int	dofilewrite(struct thread *, int, struct file *, struct uio *,
102bcd9e0ddSJohn Baldwin 		    off_t, int);
103512824f8SSeigo Tanimura static void	doselwakeup(struct selinfo *, int);
104ace8398dSJeff Roberson static void	seltdinit(struct thread *);
105ace8398dSJeff Roberson static int	seltdwait(struct thread *, int);
106ace8398dSJeff Roberson static void	seltdclear(struct thread *);
107ace8398dSJeff Roberson 
108ace8398dSJeff Roberson /*
109ace8398dSJeff Roberson  * One seltd per-thread allocated on demand as needed.
110ace8398dSJeff Roberson  *
111ace8398dSJeff Roberson  *	t - protected by st_mtx
112ace8398dSJeff Roberson  * 	k - Only accessed by curthread or read-only
113ace8398dSJeff Roberson  */
114ace8398dSJeff Roberson struct seltd {
115ace8398dSJeff Roberson 	STAILQ_HEAD(, selfd)	st_selq;	/* (k) List of selfds. */
116ace8398dSJeff Roberson 	struct selfd		*st_free1;	/* (k) free fd for read set. */
117ace8398dSJeff Roberson 	struct selfd		*st_free2;	/* (k) free fd for write set. */
118ace8398dSJeff Roberson 	struct mtx		st_mtx;		/* Protects struct seltd */
119ace8398dSJeff Roberson 	struct cv		st_wait;	/* (t) Wait channel. */
120ace8398dSJeff Roberson 	int			st_flags;	/* (t) SELTD_ flags. */
121ace8398dSJeff Roberson };
122ace8398dSJeff Roberson 
123ace8398dSJeff Roberson #define	SELTD_PENDING	0x0001			/* We have pending events. */
124ace8398dSJeff Roberson #define	SELTD_RESCAN	0x0002			/* Doing a rescan. */
125ace8398dSJeff Roberson 
126ace8398dSJeff Roberson /*
127ace8398dSJeff Roberson  * One selfd allocated per-thread per-file-descriptor.
128ace8398dSJeff Roberson  *	f - protected by sf_mtx
129ace8398dSJeff Roberson  */
130ace8398dSJeff Roberson struct selfd {
131ace8398dSJeff Roberson 	STAILQ_ENTRY(selfd)	sf_link;	/* (k) fds owned by this td. */
132ace8398dSJeff Roberson 	TAILQ_ENTRY(selfd)	sf_threads;	/* (f) fds on this selinfo. */
133ace8398dSJeff Roberson 	struct selinfo		*sf_si;		/* (f) selinfo when linked. */
134ace8398dSJeff Roberson 	struct mtx		*sf_mtx;	/* Pointer to selinfo mtx. */
135ace8398dSJeff Roberson 	struct seltd		*sf_td;		/* (k) owning seltd. */
136ace8398dSJeff Roberson 	void			*sf_cookie;	/* (k) fd or pollfd. */
137ace8398dSJeff Roberson };
138ace8398dSJeff Roberson 
139ace8398dSJeff Roberson static uma_zone_t selfd_zone;
1402141453eSJeff Roberson static struct mtx_pool *mtxpool_select;
1418fe387abSDmitrij Tejblum 
142d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
143df8bae1dSRodney W. Grimes struct read_args {
144df8bae1dSRodney W. Grimes 	int	fd;
145134e06feSBruce Evans 	void	*buf;
146134e06feSBruce Evans 	size_t	nbyte;
147df8bae1dSRodney W. Grimes };
148d2d3e875SBruce Evans #endif
14926f9a767SRodney W. Grimes int
1508451d0ddSKip Macy sys_read(td, uap)
151b40ce416SJulian Elischer 	struct thread *td;
152b064d43dSMatthew Dillon 	struct read_args *uap;
153df8bae1dSRodney W. Grimes {
154bcd9e0ddSJohn Baldwin 	struct uio auio;
155bcd9e0ddSJohn Baldwin 	struct iovec aiov;
156279d7226SMatthew Dillon 	int error;
157df8bae1dSRodney W. Grimes 
158526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
159bcd9e0ddSJohn Baldwin 		return (EINVAL);
160bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
161bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
162bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
163bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
164bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
165bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
166bcd9e0ddSJohn Baldwin 	error = kern_readv(td, uap->fd, &auio);
167279d7226SMatthew Dillon 	return(error);
168df8bae1dSRodney W. Grimes }
169df8bae1dSRodney W. Grimes 
170df8bae1dSRodney W. Grimes /*
171bcd9e0ddSJohn Baldwin  * Positioned read system call
1724160ccd9SAlan Cox  */
1734160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
1744160ccd9SAlan Cox struct pread_args {
1754160ccd9SAlan Cox 	int	fd;
1764160ccd9SAlan Cox 	void	*buf;
1774160ccd9SAlan Cox 	size_t	nbyte;
1788fe387abSDmitrij Tejblum 	int	pad;
1794160ccd9SAlan Cox 	off_t	offset;
1804160ccd9SAlan Cox };
1814160ccd9SAlan Cox #endif
1824160ccd9SAlan Cox int
1838451d0ddSKip Macy sys_pread(td, uap)
184b40ce416SJulian Elischer 	struct thread *td;
185b064d43dSMatthew Dillon 	struct pread_args *uap;
1864160ccd9SAlan Cox {
1874160ccd9SAlan Cox 	struct uio auio;
1884160ccd9SAlan Cox 	struct iovec aiov;
189bcd9e0ddSJohn Baldwin 	int error;
1904160ccd9SAlan Cox 
191526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
192bcd9e0ddSJohn Baldwin 		return (EINVAL);
193bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
194bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
1954160ccd9SAlan Cox 	auio.uio_iov = &aiov;
1964160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
197bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
1984160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
199bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, &auio, uap->offset);
2004160ccd9SAlan Cox 	return(error);
2014160ccd9SAlan Cox }
2024160ccd9SAlan Cox 
203c2815ad5SPeter Wemm int
204c2815ad5SPeter Wemm freebsd6_pread(td, uap)
205c2815ad5SPeter Wemm 	struct thread *td;
206c2815ad5SPeter Wemm 	struct freebsd6_pread_args *uap;
207c2815ad5SPeter Wemm {
208c2815ad5SPeter Wemm 	struct pread_args oargs;
209c2815ad5SPeter Wemm 
210c2815ad5SPeter Wemm 	oargs.fd = uap->fd;
211c2815ad5SPeter Wemm 	oargs.buf = uap->buf;
212c2815ad5SPeter Wemm 	oargs.nbyte = uap->nbyte;
213c2815ad5SPeter Wemm 	oargs.offset = uap->offset;
2148451d0ddSKip Macy 	return (sys_pread(td, &oargs));
215c2815ad5SPeter Wemm }
216c2815ad5SPeter Wemm 
2174160ccd9SAlan Cox /*
218df8bae1dSRodney W. Grimes  * Scatter read system call.
219df8bae1dSRodney W. Grimes  */
220d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
221df8bae1dSRodney W. Grimes struct readv_args {
2227147b19dSBruce Evans 	int	fd;
223df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
224df8bae1dSRodney W. Grimes 	u_int	iovcnt;
225df8bae1dSRodney W. Grimes };
226d2d3e875SBruce Evans #endif
22726f9a767SRodney W. Grimes int
2288451d0ddSKip Macy sys_readv(struct thread *td, struct readv_args *uap)
229df8bae1dSRodney W. Grimes {
230b88ec951SJohn Baldwin 	struct uio *auio;
231b88ec951SJohn Baldwin 	int error;
232b88ec951SJohn Baldwin 
233b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
234b88ec951SJohn Baldwin 	if (error)
235b88ec951SJohn Baldwin 		return (error);
236b88ec951SJohn Baldwin 	error = kern_readv(td, uap->fd, auio);
237b88ec951SJohn Baldwin 	free(auio, M_IOV);
238b88ec951SJohn Baldwin 	return (error);
239b88ec951SJohn Baldwin }
240b88ec951SJohn Baldwin 
241b88ec951SJohn Baldwin int
242b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio)
243b88ec951SJohn Baldwin {
244b064d43dSMatthew Dillon 	struct file *fp;
245bcd9e0ddSJohn Baldwin 	int error;
246bcd9e0ddSJohn Baldwin 
247a9d2f8d8SRobert Watson 	error = fget_read(td, fd, CAP_READ | CAP_SEEK, &fp);
248bcd9e0ddSJohn Baldwin 	if (error)
249bcd9e0ddSJohn Baldwin 		return (error);
250bcd9e0ddSJohn Baldwin 	error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
251bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
252bcd9e0ddSJohn Baldwin 	return (error);
253bcd9e0ddSJohn Baldwin }
254bcd9e0ddSJohn Baldwin 
255bcd9e0ddSJohn Baldwin /*
256bcd9e0ddSJohn Baldwin  * Scatter positioned read system call.
257bcd9e0ddSJohn Baldwin  */
258bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
259bcd9e0ddSJohn Baldwin struct preadv_args {
260bcd9e0ddSJohn Baldwin 	int	fd;
261bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
262bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
263bcd9e0ddSJohn Baldwin 	off_t	offset;
264bcd9e0ddSJohn Baldwin };
265bcd9e0ddSJohn Baldwin #endif
266bcd9e0ddSJohn Baldwin int
2678451d0ddSKip Macy sys_preadv(struct thread *td, struct preadv_args *uap)
268bcd9e0ddSJohn Baldwin {
269bcd9e0ddSJohn Baldwin 	struct uio *auio;
270bcd9e0ddSJohn Baldwin 	int error;
271bcd9e0ddSJohn Baldwin 
272bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
273bcd9e0ddSJohn Baldwin 	if (error)
274bcd9e0ddSJohn Baldwin 		return (error);
275bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, auio, uap->offset);
276bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
277bcd9e0ddSJohn Baldwin 	return (error);
278bcd9e0ddSJohn Baldwin }
279bcd9e0ddSJohn Baldwin 
280bcd9e0ddSJohn Baldwin int
281bcd9e0ddSJohn Baldwin kern_preadv(td, fd, auio, offset)
282bcd9e0ddSJohn Baldwin 	struct thread *td;
283bcd9e0ddSJohn Baldwin 	int fd;
284bcd9e0ddSJohn Baldwin 	struct uio *auio;
285bcd9e0ddSJohn Baldwin 	off_t offset;
286bcd9e0ddSJohn Baldwin {
287bcd9e0ddSJohn Baldwin 	struct file *fp;
288bcd9e0ddSJohn Baldwin 	int error;
289bcd9e0ddSJohn Baldwin 
290a9d2f8d8SRobert Watson 	error = fget_read(td, fd, CAP_READ, &fp);
291bcd9e0ddSJohn Baldwin 	if (error)
292bcd9e0ddSJohn Baldwin 		return (error);
293bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
294bcd9e0ddSJohn Baldwin 		error = ESPIPE;
295bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
296bcd9e0ddSJohn Baldwin 		error = EINVAL;
297bcd9e0ddSJohn Baldwin 	else
298bcd9e0ddSJohn Baldwin 		error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
299bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
300bcd9e0ddSJohn Baldwin 	return (error);
301bcd9e0ddSJohn Baldwin }
302bcd9e0ddSJohn Baldwin 
303bcd9e0ddSJohn Baldwin /*
304bcd9e0ddSJohn Baldwin  * Common code for readv and preadv that reads data in
305bcd9e0ddSJohn Baldwin  * from a file using the passed in uio, offset, and flags.
306bcd9e0ddSJohn Baldwin  */
307bcd9e0ddSJohn Baldwin static int
308bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags)
309bcd9e0ddSJohn Baldwin 	struct thread *td;
310bcd9e0ddSJohn Baldwin 	int fd;
311bcd9e0ddSJohn Baldwin 	struct file *fp;
312bcd9e0ddSJohn Baldwin 	struct uio *auio;
313bcd9e0ddSJohn Baldwin 	off_t offset;
314bcd9e0ddSJohn Baldwin 	int flags;
315bcd9e0ddSJohn Baldwin {
316bcd9e0ddSJohn Baldwin 	ssize_t cnt;
31782641acdSAlan Cox 	int error;
318df8bae1dSRodney W. Grimes #ifdef KTRACE
319552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
320df8bae1dSRodney W. Grimes #endif
321df8bae1dSRodney W. Grimes 
3224f8d23d6SPoul-Henning Kamp 	/* Finish zero length reads right here */
3234f8d23d6SPoul-Henning Kamp 	if (auio->uio_resid == 0) {
3244f8d23d6SPoul-Henning Kamp 		td->td_retval[0] = 0;
3254f8d23d6SPoul-Henning Kamp 		return(0);
3264f8d23d6SPoul-Henning Kamp 	}
327552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_READ;
328bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
329552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
330df8bae1dSRodney W. Grimes #ifdef KTRACE
331552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
332552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
333df8bae1dSRodney W. Grimes #endif
334552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
335bcd9e0ddSJohn Baldwin 	if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
336552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
337df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
338df8bae1dSRodney W. Grimes 			error = 0;
339279d7226SMatthew Dillon 	}
340552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
341df8bae1dSRodney W. Grimes #ifdef KTRACE
342552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
343552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
344b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_READ, ktruio, error);
345df8bae1dSRodney W. Grimes 	}
346df8bae1dSRodney W. Grimes #endif
347b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
348df8bae1dSRodney W. Grimes 	return (error);
349df8bae1dSRodney W. Grimes }
350df8bae1dSRodney W. Grimes 
351d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
352df8bae1dSRodney W. Grimes struct write_args {
353df8bae1dSRodney W. Grimes 	int	fd;
354134e06feSBruce Evans 	const void *buf;
355134e06feSBruce Evans 	size_t	nbyte;
356df8bae1dSRodney W. Grimes };
357d2d3e875SBruce Evans #endif
35826f9a767SRodney W. Grimes int
3598451d0ddSKip Macy sys_write(td, uap)
360b40ce416SJulian Elischer 	struct thread *td;
361b064d43dSMatthew Dillon 	struct write_args *uap;
362df8bae1dSRodney W. Grimes {
363bcd9e0ddSJohn Baldwin 	struct uio auio;
364bcd9e0ddSJohn Baldwin 	struct iovec aiov;
365279d7226SMatthew Dillon 	int error;
366df8bae1dSRodney W. Grimes 
367526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
368bcd9e0ddSJohn Baldwin 		return (EINVAL);
369bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
370bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
371bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
372bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
373bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
374bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
375bcd9e0ddSJohn Baldwin 	error = kern_writev(td, uap->fd, &auio);
376279d7226SMatthew Dillon 	return(error);
377df8bae1dSRodney W. Grimes }
378df8bae1dSRodney W. Grimes 
379df8bae1dSRodney W. Grimes /*
3800c14ff0eSRobert Watson  * Positioned write system call.
3814160ccd9SAlan Cox  */
3824160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
3834160ccd9SAlan Cox struct pwrite_args {
3844160ccd9SAlan Cox 	int	fd;
3854160ccd9SAlan Cox 	const void *buf;
3864160ccd9SAlan Cox 	size_t	nbyte;
3878fe387abSDmitrij Tejblum 	int	pad;
3884160ccd9SAlan Cox 	off_t	offset;
3894160ccd9SAlan Cox };
3904160ccd9SAlan Cox #endif
3914160ccd9SAlan Cox int
3928451d0ddSKip Macy sys_pwrite(td, uap)
393b40ce416SJulian Elischer 	struct thread *td;
394b064d43dSMatthew Dillon 	struct pwrite_args *uap;
3954160ccd9SAlan Cox {
3964160ccd9SAlan Cox 	struct uio auio;
3974160ccd9SAlan Cox 	struct iovec aiov;
398bcd9e0ddSJohn Baldwin 	int error;
3994160ccd9SAlan Cox 
400526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
401bcd9e0ddSJohn Baldwin 		return (EINVAL);
402bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
403bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
4044160ccd9SAlan Cox 	auio.uio_iov = &aiov;
4054160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
406bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
4074160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
408bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, &auio, uap->offset);
4094160ccd9SAlan Cox 	return(error);
4104160ccd9SAlan Cox }
4114160ccd9SAlan Cox 
412c2815ad5SPeter Wemm int
413c2815ad5SPeter Wemm freebsd6_pwrite(td, uap)
414c2815ad5SPeter Wemm 	struct thread *td;
415c2815ad5SPeter Wemm 	struct freebsd6_pwrite_args *uap;
416c2815ad5SPeter Wemm {
417c2815ad5SPeter Wemm 	struct pwrite_args oargs;
418c2815ad5SPeter Wemm 
419c2815ad5SPeter Wemm 	oargs.fd = uap->fd;
420c2815ad5SPeter Wemm 	oargs.buf = uap->buf;
421c2815ad5SPeter Wemm 	oargs.nbyte = uap->nbyte;
422c2815ad5SPeter Wemm 	oargs.offset = uap->offset;
4238451d0ddSKip Macy 	return (sys_pwrite(td, &oargs));
424c2815ad5SPeter Wemm }
425c2815ad5SPeter Wemm 
4264160ccd9SAlan Cox /*
4270c14ff0eSRobert Watson  * Gather write system call.
428df8bae1dSRodney W. Grimes  */
429d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
430df8bae1dSRodney W. Grimes struct writev_args {
431df8bae1dSRodney W. Grimes 	int	fd;
432df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
433df8bae1dSRodney W. Grimes 	u_int	iovcnt;
434df8bae1dSRodney W. Grimes };
435d2d3e875SBruce Evans #endif
43626f9a767SRodney W. Grimes int
4378451d0ddSKip Macy sys_writev(struct thread *td, struct writev_args *uap)
438df8bae1dSRodney W. Grimes {
439b88ec951SJohn Baldwin 	struct uio *auio;
440b88ec951SJohn Baldwin 	int error;
441b88ec951SJohn Baldwin 
442b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
443b88ec951SJohn Baldwin 	if (error)
444b88ec951SJohn Baldwin 		return (error);
445b88ec951SJohn Baldwin 	error = kern_writev(td, uap->fd, auio);
446b88ec951SJohn Baldwin 	free(auio, M_IOV);
447b88ec951SJohn Baldwin 	return (error);
448b88ec951SJohn Baldwin }
449b88ec951SJohn Baldwin 
450b88ec951SJohn Baldwin int
451b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio)
452b88ec951SJohn Baldwin {
453b064d43dSMatthew Dillon 	struct file *fp;
454bcd9e0ddSJohn Baldwin 	int error;
455bcd9e0ddSJohn Baldwin 
456a9d2f8d8SRobert Watson 	error = fget_write(td, fd, CAP_WRITE | CAP_SEEK, &fp);
457bcd9e0ddSJohn Baldwin 	if (error)
458af56abaaSJohn Baldwin 		return (error);
459bcd9e0ddSJohn Baldwin 	error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
460bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
461bcd9e0ddSJohn Baldwin 	return (error);
462bcd9e0ddSJohn Baldwin }
463bcd9e0ddSJohn Baldwin 
464bcd9e0ddSJohn Baldwin /*
4650c14ff0eSRobert Watson  * Gather positioned write system call.
466bcd9e0ddSJohn Baldwin  */
467bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
468bcd9e0ddSJohn Baldwin struct pwritev_args {
469bcd9e0ddSJohn Baldwin 	int	fd;
470bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
471bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
472bcd9e0ddSJohn Baldwin 	off_t	offset;
473bcd9e0ddSJohn Baldwin };
474bcd9e0ddSJohn Baldwin #endif
475bcd9e0ddSJohn Baldwin int
4768451d0ddSKip Macy sys_pwritev(struct thread *td, struct pwritev_args *uap)
477bcd9e0ddSJohn Baldwin {
478bcd9e0ddSJohn Baldwin 	struct uio *auio;
479bcd9e0ddSJohn Baldwin 	int error;
480bcd9e0ddSJohn Baldwin 
481bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
482bcd9e0ddSJohn Baldwin 	if (error)
483bcd9e0ddSJohn Baldwin 		return (error);
484bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, auio, uap->offset);
485bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
486bcd9e0ddSJohn Baldwin 	return (error);
487bcd9e0ddSJohn Baldwin }
488bcd9e0ddSJohn Baldwin 
489bcd9e0ddSJohn Baldwin int
490bcd9e0ddSJohn Baldwin kern_pwritev(td, fd, auio, offset)
491bcd9e0ddSJohn Baldwin 	struct thread *td;
492bcd9e0ddSJohn Baldwin 	struct uio *auio;
493bcd9e0ddSJohn Baldwin 	int fd;
494bcd9e0ddSJohn Baldwin 	off_t offset;
495bcd9e0ddSJohn Baldwin {
496bcd9e0ddSJohn Baldwin 	struct file *fp;
497bcd9e0ddSJohn Baldwin 	int error;
498bcd9e0ddSJohn Baldwin 
499a9d2f8d8SRobert Watson 	error = fget_write(td, fd, CAP_WRITE, &fp);
500bcd9e0ddSJohn Baldwin 	if (error)
501af56abaaSJohn Baldwin 		return (error);
502bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
503bcd9e0ddSJohn Baldwin 		error = ESPIPE;
504bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
505bcd9e0ddSJohn Baldwin 		error = EINVAL;
506bcd9e0ddSJohn Baldwin 	else
507bcd9e0ddSJohn Baldwin 		error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
508bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
509bcd9e0ddSJohn Baldwin 	return (error);
510bcd9e0ddSJohn Baldwin }
511bcd9e0ddSJohn Baldwin 
512bcd9e0ddSJohn Baldwin /*
513bcd9e0ddSJohn Baldwin  * Common code for writev and pwritev that writes data to
514bcd9e0ddSJohn Baldwin  * a file using the passed in uio, offset, and flags.
515bcd9e0ddSJohn Baldwin  */
516bcd9e0ddSJohn Baldwin static int
517bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags)
518bcd9e0ddSJohn Baldwin 	struct thread *td;
519bcd9e0ddSJohn Baldwin 	int fd;
520bcd9e0ddSJohn Baldwin 	struct file *fp;
521bcd9e0ddSJohn Baldwin 	struct uio *auio;
522bcd9e0ddSJohn Baldwin 	off_t offset;
523bcd9e0ddSJohn Baldwin 	int flags;
524bcd9e0ddSJohn Baldwin {
525bcd9e0ddSJohn Baldwin 	ssize_t cnt;
526552afd9cSPoul-Henning Kamp 	int error;
527df8bae1dSRodney W. Grimes #ifdef KTRACE
528552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
529df8bae1dSRodney W. Grimes #endif
530df8bae1dSRodney W. Grimes 
531552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_WRITE;
532552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
533bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
534df8bae1dSRodney W. Grimes #ifdef KTRACE
535552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
536552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
537df8bae1dSRodney W. Grimes #endif
538552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
539a41ce5d3SMatthew Dillon 	if (fp->f_type == DTYPE_VNODE)
5409440653dSMatthew Dillon 		bwillwrite();
541bcd9e0ddSJohn Baldwin 	if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
542552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
543df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
544df8bae1dSRodney W. Grimes 			error = 0;
545bcd9e0ddSJohn Baldwin 		/* Socket layer is responsible for issuing SIGPIPE. */
5466f7ca813SBruce M Simpson 		if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
547b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
5487a6f3d78SJohn Baldwin 			tdsignal(td, SIGPIPE);
549b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
55019eb87d2SJohn Baldwin 		}
551df8bae1dSRodney W. Grimes 	}
552552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
553df8bae1dSRodney W. Grimes #ifdef KTRACE
554552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
555552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
556b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_WRITE, ktruio, error);
557df8bae1dSRodney W. Grimes 	}
558df8bae1dSRodney W. Grimes #endif
559b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
560df8bae1dSRodney W. Grimes 	return (error);
561df8bae1dSRodney W. Grimes }
562df8bae1dSRodney W. Grimes 
563e4650294SJohn Baldwin /*
564e4650294SJohn Baldwin  * Truncate a file given a file descriptor.
565e4650294SJohn Baldwin  *
566e4650294SJohn Baldwin  * Can't use fget_write() here, since must return EINVAL and not EBADF if the
567e4650294SJohn Baldwin  * descriptor isn't writable.
568e4650294SJohn Baldwin  */
569e4650294SJohn Baldwin int
570e4650294SJohn Baldwin kern_ftruncate(td, fd, length)
571e4650294SJohn Baldwin 	struct thread *td;
572e4650294SJohn Baldwin 	int fd;
573e4650294SJohn Baldwin 	off_t length;
574e4650294SJohn Baldwin {
575e4650294SJohn Baldwin 	struct file *fp;
576e4650294SJohn Baldwin 	int error;
577e4650294SJohn Baldwin 
57814961ba7SRobert Watson 	AUDIT_ARG_FD(fd);
579e4650294SJohn Baldwin 	if (length < 0)
580e4650294SJohn Baldwin 		return (EINVAL);
581a9d2f8d8SRobert Watson 	error = fget(td, fd, CAP_FTRUNCATE, &fp);
582e4650294SJohn Baldwin 	if (error)
583e4650294SJohn Baldwin 		return (error);
58414961ba7SRobert Watson 	AUDIT_ARG_FILE(td->td_proc, fp);
585e4650294SJohn Baldwin 	if (!(fp->f_flag & FWRITE)) {
586e4650294SJohn Baldwin 		fdrop(fp, td);
587e4650294SJohn Baldwin 		return (EINVAL);
588e4650294SJohn Baldwin 	}
589e4650294SJohn Baldwin 	error = fo_truncate(fp, length, td->td_ucred, td);
590e4650294SJohn Baldwin 	fdrop(fp, td);
591e4650294SJohn Baldwin 	return (error);
592e4650294SJohn Baldwin }
593e4650294SJohn Baldwin 
594e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
595e4650294SJohn Baldwin struct ftruncate_args {
596e4650294SJohn Baldwin 	int	fd;
597e4650294SJohn Baldwin 	int	pad;
598e4650294SJohn Baldwin 	off_t	length;
599e4650294SJohn Baldwin };
600e4650294SJohn Baldwin #endif
601e4650294SJohn Baldwin int
6028451d0ddSKip Macy sys_ftruncate(td, uap)
603e4650294SJohn Baldwin 	struct thread *td;
604e4650294SJohn Baldwin 	struct ftruncate_args *uap;
605e4650294SJohn Baldwin {
606e4650294SJohn Baldwin 
607e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
608e4650294SJohn Baldwin }
609e4650294SJohn Baldwin 
610e4650294SJohn Baldwin #if defined(COMPAT_43)
611e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
612e4650294SJohn Baldwin struct oftruncate_args {
613e4650294SJohn Baldwin 	int	fd;
614e4650294SJohn Baldwin 	long	length;
615e4650294SJohn Baldwin };
616e4650294SJohn Baldwin #endif
617e4650294SJohn Baldwin int
618e4650294SJohn Baldwin oftruncate(td, uap)
619e4650294SJohn Baldwin 	struct thread *td;
620e4650294SJohn Baldwin 	struct oftruncate_args *uap;
621e4650294SJohn Baldwin {
622e4650294SJohn Baldwin 
623e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
624e4650294SJohn Baldwin }
625e4650294SJohn Baldwin #endif /* COMPAT_43 */
626e4650294SJohn Baldwin 
627d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
628df8bae1dSRodney W. Grimes struct ioctl_args {
629df8bae1dSRodney W. Grimes 	int	fd;
630069e9bc1SDoug Rabson 	u_long	com;
631df8bae1dSRodney W. Grimes 	caddr_t	data;
632df8bae1dSRodney W. Grimes };
633d2d3e875SBruce Evans #endif
634df8bae1dSRodney W. Grimes /* ARGSUSED */
63526f9a767SRodney W. Grimes int
6368451d0ddSKip Macy sys_ioctl(struct thread *td, struct ioctl_args *uap)
637df8bae1dSRodney W. Grimes {
6383e15c66fSPoul-Henning Kamp 	u_long com;
6399fddcc66SRuslan Ermilov 	int arg, error;
6403e15c66fSPoul-Henning Kamp 	u_int size;
6419fddcc66SRuslan Ermilov 	caddr_t data;
642df8bae1dSRodney W. Grimes 
6439fc6aa06SPoul-Henning Kamp 	if (uap->com > 0xffffffff) {
6449fc6aa06SPoul-Henning Kamp 		printf(
6459fc6aa06SPoul-Henning Kamp 		    "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
646431f8906SJulian Elischer 		    td->td_proc->p_pid, td->td_name, uap->com);
6479fc6aa06SPoul-Henning Kamp 		uap->com &= 0xffffffff;
6489fc6aa06SPoul-Henning Kamp 	}
649d9f46233SJohn Baldwin 	com = uap->com;
650df8bae1dSRodney W. Grimes 
651df8bae1dSRodney W. Grimes 	/*
652df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
653df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
654df8bae1dSRodney W. Grimes 	 */
655df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
656ca51b19bSPoul-Henning Kamp 	if ((size > IOCPARM_MAX) ||
657ca51b19bSPoul-Henning Kamp 	    ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
6582de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
6592de92a38SPeter Wemm 	    ((com & IOC_OUT) && size == 0) ||
6602de92a38SPeter Wemm #else
6612de92a38SPeter Wemm 	    ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
6622de92a38SPeter Wemm #endif
6639fddcc66SRuslan Ermilov 	    ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
664426da3bcSAlfred Perlstein 		return (ENOTTY);
665279d7226SMatthew Dillon 
666ca51b19bSPoul-Henning Kamp 	if (size > 0) {
667715457f6SDavid E. O'Brien 		if (com & IOC_VOID) {
6689fddcc66SRuslan Ermilov 			/* Integer argument. */
6699fddcc66SRuslan Ermilov 			arg = (intptr_t)uap->data;
6709fddcc66SRuslan Ermilov 			data = (void *)&arg;
6719fddcc66SRuslan Ermilov 			size = 0;
672715457f6SDavid E. O'Brien 		} else
673715457f6SDavid E. O'Brien 			data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
6749fddcc66SRuslan Ermilov 	} else
6759fddcc66SRuslan Ermilov 		data = (void *)&uap->data;
676df8bae1dSRodney W. Grimes 	if (com & IOC_IN) {
677df8bae1dSRodney W. Grimes 		error = copyin(uap->data, data, (u_int)size);
678df8bae1dSRodney W. Grimes 		if (error) {
679a1b0a180SRuslan Ermilov 			if (size > 0)
6809fddcc66SRuslan Ermilov 				free(data, M_IOCTLOPS);
6813e15c66fSPoul-Henning Kamp 			return (error);
682df8bae1dSRodney W. Grimes 		}
683ca51b19bSPoul-Henning Kamp 	} else if (com & IOC_OUT) {
684df8bae1dSRodney W. Grimes 		/*
685df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
686df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
687df8bae1dSRodney W. Grimes 		 */
688df8bae1dSRodney W. Grimes 		bzero(data, size);
689279d7226SMatthew Dillon 	}
690df8bae1dSRodney W. Grimes 
691d9f46233SJohn Baldwin 	error = kern_ioctl(td, uap->fd, com, data);
692d9f46233SJohn Baldwin 
693d9f46233SJohn Baldwin 	if (error == 0 && (com & IOC_OUT))
694d9f46233SJohn Baldwin 		error = copyout(data, uap->data, (u_int)size);
695d9f46233SJohn Baldwin 
6969fddcc66SRuslan Ermilov 	if (size > 0)
6979fddcc66SRuslan Ermilov 		free(data, M_IOCTLOPS);
698d9f46233SJohn Baldwin 	return (error);
699d9f46233SJohn Baldwin }
700d9f46233SJohn Baldwin 
701d9f46233SJohn Baldwin int
702d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
703d9f46233SJohn Baldwin {
704d9f46233SJohn Baldwin 	struct file *fp;
705d9f46233SJohn Baldwin 	struct filedesc *fdp;
706d9f46233SJohn Baldwin 	int error;
707d9f46233SJohn Baldwin 	int tmp;
708d9f46233SJohn Baldwin 
70964c9a4d9SRobert Watson 	AUDIT_ARG_FD(fd);
71064c9a4d9SRobert Watson 	AUDIT_ARG_CMD(com);
711a9d2f8d8SRobert Watson 	if ((error = fget(td, fd, CAP_IOCTL, &fp)) != 0)
712d9f46233SJohn Baldwin 		return (error);
713d9f46233SJohn Baldwin 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
714d9f46233SJohn Baldwin 		fdrop(fp, td);
715d9f46233SJohn Baldwin 		return (EBADF);
716d9f46233SJohn Baldwin 	}
717d9f46233SJohn Baldwin 	fdp = td->td_proc->p_fd;
718d9f46233SJohn Baldwin 	switch (com) {
719d9f46233SJohn Baldwin 	case FIONCLEX:
7205e3f7694SRobert Watson 		FILEDESC_XLOCK(fdp);
721d9f46233SJohn Baldwin 		fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
7225e3f7694SRobert Watson 		FILEDESC_XUNLOCK(fdp);
723d9f46233SJohn Baldwin 		goto out;
724d9f46233SJohn Baldwin 	case FIOCLEX:
7255e3f7694SRobert Watson 		FILEDESC_XLOCK(fdp);
726d9f46233SJohn Baldwin 		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
7275e3f7694SRobert Watson 		FILEDESC_XUNLOCK(fdp);
728d9f46233SJohn Baldwin 		goto out;
729d9f46233SJohn Baldwin 	case FIONBIO:
730bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
731397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FNONBLOCK);
732df8bae1dSRodney W. Grimes 		else
733397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FNONBLOCK);
7348ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
735d9f46233SJohn Baldwin 		break;
736d9f46233SJohn Baldwin 	case FIOASYNC:
737bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
738397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FASYNC);
739df8bae1dSRodney W. Grimes 		else
740397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FASYNC);
7418ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
742d9f46233SJohn Baldwin 		break;
743df8bae1dSRodney W. Grimes 	}
7448ccf264fSPoul-Henning Kamp 
7458ccf264fSPoul-Henning Kamp 	error = fo_ioctl(fp, com, data, td->td_ucred, td);
746d9f46233SJohn Baldwin out:
747b40ce416SJulian Elischer 	fdrop(fp, td);
748df8bae1dSRodney W. Grimes 	return (error);
749df8bae1dSRodney W. Grimes }
750df8bae1dSRodney W. Grimes 
751125dcf8cSKonstantin Belousov int
752125dcf8cSKonstantin Belousov poll_no_poll(int events)
753125dcf8cSKonstantin Belousov {
754125dcf8cSKonstantin Belousov 	/*
755125dcf8cSKonstantin Belousov 	 * Return true for read/write.  If the user asked for something
756125dcf8cSKonstantin Belousov 	 * special, return POLLNVAL, so that clients have a way of
757125dcf8cSKonstantin Belousov 	 * determining reliably whether or not the extended
758125dcf8cSKonstantin Belousov 	 * functionality is present without hard-coding knowledge
759125dcf8cSKonstantin Belousov 	 * of specific filesystem implementations.
760125dcf8cSKonstantin Belousov 	 */
761125dcf8cSKonstantin Belousov 	if (events & ~POLLSTANDARD)
762125dcf8cSKonstantin Belousov 		return (POLLNVAL);
763125dcf8cSKonstantin Belousov 
764125dcf8cSKonstantin Belousov 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
765125dcf8cSKonstantin Belousov }
766125dcf8cSKonstantin Belousov 
767066d836bSKonstantin Belousov int
7688451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap)
769066d836bSKonstantin Belousov {
770066d836bSKonstantin Belousov 	struct timespec ts;
771066d836bSKonstantin Belousov 	struct timeval tv, *tvp;
772066d836bSKonstantin Belousov 	sigset_t set, *uset;
773066d836bSKonstantin Belousov 	int error;
774066d836bSKonstantin Belousov 
775066d836bSKonstantin Belousov 	if (uap->ts != NULL) {
776066d836bSKonstantin Belousov 		error = copyin(uap->ts, &ts, sizeof(ts));
777066d836bSKonstantin Belousov 		if (error != 0)
778066d836bSKonstantin Belousov 		    return (error);
779066d836bSKonstantin Belousov 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
780066d836bSKonstantin Belousov 		tvp = &tv;
781066d836bSKonstantin Belousov 	} else
782066d836bSKonstantin Belousov 		tvp = NULL;
783066d836bSKonstantin Belousov 	if (uap->sm != NULL) {
784066d836bSKonstantin Belousov 		error = copyin(uap->sm, &set, sizeof(set));
785066d836bSKonstantin Belousov 		if (error != 0)
786066d836bSKonstantin Belousov 			return (error);
787066d836bSKonstantin Belousov 		uset = &set;
788066d836bSKonstantin Belousov 	} else
789066d836bSKonstantin Belousov 		uset = NULL;
790066d836bSKonstantin Belousov 	return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
791066d836bSKonstantin Belousov 	    uset, NFDBITS));
792066d836bSKonstantin Belousov }
793066d836bSKonstantin Belousov 
794066d836bSKonstantin Belousov int
795066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
796066d836bSKonstantin Belousov     struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
797066d836bSKonstantin Belousov {
798066d836bSKonstantin Belousov 	int error;
799066d836bSKonstantin Belousov 
800066d836bSKonstantin Belousov 	if (uset != NULL) {
801066d836bSKonstantin Belousov 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
802066d836bSKonstantin Belousov 		    &td->td_oldsigmask, 0);
803066d836bSKonstantin Belousov 		if (error != 0)
804066d836bSKonstantin Belousov 			return (error);
805066d836bSKonstantin Belousov 		td->td_pflags |= TDP_OLDMASK;
806066d836bSKonstantin Belousov 		/*
807066d836bSKonstantin Belousov 		 * Make sure that ast() is called on return to
808066d836bSKonstantin Belousov 		 * usermode and TDP_OLDMASK is cleared, restoring old
809066d836bSKonstantin Belousov 		 * sigmask.
810066d836bSKonstantin Belousov 		 */
811066d836bSKonstantin Belousov 		thread_lock(td);
812066d836bSKonstantin Belousov 		td->td_flags |= TDF_ASTPENDING;
813066d836bSKonstantin Belousov 		thread_unlock(td);
814066d836bSKonstantin Belousov 	}
815066d836bSKonstantin Belousov 	error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
816066d836bSKonstantin Belousov 	return (error);
817066d836bSKonstantin Belousov }
818066d836bSKonstantin Belousov 
819d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
820df8bae1dSRodney W. Grimes struct select_args {
821b08f7993SSujal Patel 	int	nd;
822df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
823df8bae1dSRodney W. Grimes 	struct	timeval *tv;
824df8bae1dSRodney W. Grimes };
825d2d3e875SBruce Evans #endif
82626f9a767SRodney W. Grimes int
8278451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap)
828df8bae1dSRodney W. Grimes {
8298f19eb88SIan Dowse 	struct timeval tv, *tvp;
8308f19eb88SIan Dowse 	int error;
8318f19eb88SIan Dowse 
8328f19eb88SIan Dowse 	if (uap->tv != NULL) {
8338f19eb88SIan Dowse 		error = copyin(uap->tv, &tv, sizeof(tv));
8348f19eb88SIan Dowse 		if (error)
8358f19eb88SIan Dowse 			return (error);
8368f19eb88SIan Dowse 		tvp = &tv;
8378f19eb88SIan Dowse 	} else
8388f19eb88SIan Dowse 		tvp = NULL;
8398f19eb88SIan Dowse 
840b55ef216SKonstantin Belousov 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
841b55ef216SKonstantin Belousov 	    NFDBITS));
8428f19eb88SIan Dowse }
8438f19eb88SIan Dowse 
84456be1b9aSKonstantin Belousov /*
84556be1b9aSKonstantin Belousov  * In the unlikely case when user specified n greater then the last
84656be1b9aSKonstantin Belousov  * open file descriptor, check that no bits are set after the last
84756be1b9aSKonstantin Belousov  * valid fd.  We must return EBADF if any is set.
84856be1b9aSKonstantin Belousov  *
84956be1b9aSKonstantin Belousov  * There are applications that rely on the behaviour.
85056be1b9aSKonstantin Belousov  *
85156be1b9aSKonstantin Belousov  * nd is fd_lastfile + 1.
85256be1b9aSKonstantin Belousov  */
85356be1b9aSKonstantin Belousov static int
85456be1b9aSKonstantin Belousov select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits)
85556be1b9aSKonstantin Belousov {
85656be1b9aSKonstantin Belousov 	char *addr, *oaddr;
85756be1b9aSKonstantin Belousov 	int b, i, res;
85856be1b9aSKonstantin Belousov 	uint8_t bits;
85956be1b9aSKonstantin Belousov 
86056be1b9aSKonstantin Belousov 	if (nd >= ndu || fd_in == NULL)
86156be1b9aSKonstantin Belousov 		return (0);
86256be1b9aSKonstantin Belousov 
86356be1b9aSKonstantin Belousov 	oaddr = NULL;
86456be1b9aSKonstantin Belousov 	bits = 0; /* silence gcc */
86556be1b9aSKonstantin Belousov 	for (i = nd; i < ndu; i++) {
86656be1b9aSKonstantin Belousov 		b = i / NBBY;
86756be1b9aSKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN
86856be1b9aSKonstantin Belousov 		addr = (char *)fd_in + b;
86956be1b9aSKonstantin Belousov #else
87056be1b9aSKonstantin Belousov 		addr = (char *)fd_in;
87156be1b9aSKonstantin Belousov 		if (abi_nfdbits == NFDBITS) {
87256be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(fd_mask)) +
87356be1b9aSKonstantin Belousov 			    sizeof(fd_mask) - 1 - b % sizeof(fd_mask);
87456be1b9aSKonstantin Belousov 		} else {
87556be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(uint32_t)) +
87656be1b9aSKonstantin Belousov 			    sizeof(uint32_t) - 1 - b % sizeof(uint32_t);
87756be1b9aSKonstantin Belousov 		}
87856be1b9aSKonstantin Belousov #endif
87956be1b9aSKonstantin Belousov 		if (addr != oaddr) {
88056be1b9aSKonstantin Belousov 			res = fubyte(addr);
88156be1b9aSKonstantin Belousov 			if (res == -1)
88256be1b9aSKonstantin Belousov 				return (EFAULT);
88356be1b9aSKonstantin Belousov 			oaddr = addr;
88456be1b9aSKonstantin Belousov 			bits = res;
88556be1b9aSKonstantin Belousov 		}
88656be1b9aSKonstantin Belousov 		if ((bits & (1 << (i % NBBY))) != 0)
88756be1b9aSKonstantin Belousov 			return (EBADF);
88856be1b9aSKonstantin Belousov 	}
88956be1b9aSKonstantin Belousov 	return (0);
89056be1b9aSKonstantin Belousov }
89156be1b9aSKonstantin Belousov 
8928f19eb88SIan Dowse int
8938f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
894b55ef216SKonstantin Belousov     fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
8958f19eb88SIan Dowse {
896426da3bcSAlfred Perlstein 	struct filedesc *fdp;
897d5e4d7e1SBruce Evans 	/*
898d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
899d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
900d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
901d5e4d7e1SBruce Evans 	 * of 256.
902d5e4d7e1SBruce Evans 	 */
903d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
904eb209311SAlfred Perlstein 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
90500af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
90656be1b9aSKonstantin Belousov 	int error, lf, ndu, timo;
907b55ef216SKonstantin Belousov 	u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
908df8bae1dSRodney W. Grimes 
9098f19eb88SIan Dowse 	if (nd < 0)
910acbfbfeaSSujal Patel 		return (EINVAL);
911426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
91256be1b9aSKonstantin Belousov 	ndu = nd;
91356be1b9aSKonstantin Belousov 	lf = fdp->fd_lastfile;
91456be1b9aSKonstantin Belousov 	if (nd > lf + 1)
91556be1b9aSKonstantin Belousov 		nd = lf + 1;
91656be1b9aSKonstantin Belousov 
91756be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits);
91856be1b9aSKonstantin Belousov 	if (error != 0)
91956be1b9aSKonstantin Belousov 		return (error);
92056be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits);
92156be1b9aSKonstantin Belousov 	if (error != 0)
92256be1b9aSKonstantin Belousov 		return (error);
92356be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits);
92456be1b9aSKonstantin Belousov 	if (error != 0)
92556be1b9aSKonstantin Belousov 		return (error);
926b08f7993SSujal Patel 
927d5e4d7e1SBruce Evans 	/*
928d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
929d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
930d5e4d7e1SBruce Evans 	 */
9318f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
932d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
933b55ef216SKonstantin Belousov 	ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
934d5e4d7e1SBruce Evans 	nbufbytes = 0;
9358f19eb88SIan Dowse 	if (fd_in != NULL)
936d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
9378f19eb88SIan Dowse 	if (fd_ou != NULL)
938d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
9398f19eb88SIan Dowse 	if (fd_ex != NULL)
940d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
941d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
942d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
943d5e4d7e1SBruce Evans 	else
944a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
945b08f7993SSujal Patel 
946b08f7993SSujal Patel 	/*
947d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
948d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
949d5e4d7e1SBruce Evans 	 * together.
950b08f7993SSujal Patel 	 */
951d5e4d7e1SBruce Evans 	sbp = selbits;
952df8bae1dSRodney W. Grimes #define	getbits(name, x) \
953d5e4d7e1SBruce Evans 	do {								\
954841c0c7eSNathan Whitehorn 		if (name == NULL) {					\
955d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
956841c0c7eSNathan Whitehorn 			obits[x] = NULL;				\
957841c0c7eSNathan Whitehorn 		} else {						\
958d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
959d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
960d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
961b55ef216SKonstantin Belousov 			error = copyin(name, ibits[x], ncpubytes);	\
962265fc98fSSeigo Tanimura 			if (error != 0)					\
963ace8398dSJeff Roberson 				goto done;				\
964b55ef216SKonstantin Belousov 			bzero((char *)ibits[x] + ncpubytes,		\
965b55ef216SKonstantin Belousov 			    ncpbytes - ncpubytes);			\
966e04ac2feSJohn Baldwin 		}							\
967d5e4d7e1SBruce Evans 	} while (0)
9688f19eb88SIan Dowse 	getbits(fd_in, 0);
9698f19eb88SIan Dowse 	getbits(fd_ou, 1);
9708f19eb88SIan Dowse 	getbits(fd_ex, 2);
971df8bae1dSRodney W. Grimes #undef	getbits
972841c0c7eSNathan Whitehorn 
973841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
974841c0c7eSNathan Whitehorn 	/*
975841c0c7eSNathan Whitehorn 	 * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
976841c0c7eSNathan Whitehorn 	 * we are running under 32-bit emulation. This should be more
977841c0c7eSNathan Whitehorn 	 * generic.
978841c0c7eSNathan Whitehorn 	 */
979841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)						\
980841c0c7eSNathan Whitehorn 	if (abi_nfdbits != NFDBITS && bits != NULL) {			\
981841c0c7eSNathan Whitehorn 		int i;							\
982841c0c7eSNathan Whitehorn 		for (i = 0; i < ncpbytes / sizeof *sbp; i++)		\
983841c0c7eSNathan Whitehorn 			bits[i] = (bits[i] >> 32) | (bits[i] << 32);	\
984841c0c7eSNathan Whitehorn 	}
985841c0c7eSNathan Whitehorn #else
986841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)
987841c0c7eSNathan Whitehorn #endif
988841c0c7eSNathan Whitehorn 
989841c0c7eSNathan Whitehorn 	/* Make sure the bit order makes it through an ABI transition */
990841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[0]);
991841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[1]);
992841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[2]);
993841c0c7eSNathan Whitehorn 
994d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
995d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
996df8bae1dSRodney W. Grimes 
9978f19eb88SIan Dowse 	if (tvp != NULL) {
9988f19eb88SIan Dowse 		atv = *tvp;
999df8bae1dSRodney W. Grimes 		if (itimerfix(&atv)) {
1000df8bae1dSRodney W. Grimes 			error = EINVAL;
1001ace8398dSJeff Roberson 			goto done;
1002df8bae1dSRodney W. Grimes 		}
1003c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
100400af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
10059c386f6bSJohn Baldwin 	} else {
100600af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
10079c386f6bSJohn Baldwin 		atv.tv_usec = 0;
10089c386f6bSJohn Baldwin 	}
100900af9731SPoul-Henning Kamp 	timo = 0;
1010ace8398dSJeff Roberson 	seltdinit(td);
1011ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1012ace8398dSJeff Roberson 	for (;;) {
10138f19eb88SIan Dowse 		error = selscan(td, ibits, obits, nd);
1014ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1015ace8398dSJeff Roberson 			break;
10164da144c0SJohn Baldwin 		if (atv.tv_sec || atv.tv_usec) {
1017c21410e1SPoul-Henning Kamp 			getmicrouptime(&rtv);
101885f190e4SAlfred Perlstein 			if (timevalcmp(&rtv, &atv, >=))
1019ace8398dSJeff Roberson 				break;
102000af9731SPoul-Henning Kamp 			ttv = atv;
102100af9731SPoul-Henning Kamp 			timevalsub(&ttv, &rtv);
102200af9731SPoul-Henning Kamp 			timo = ttv.tv_sec > 24 * 60 * 60 ?
102300af9731SPoul-Henning Kamp 			    24 * 60 * 60 * hz : tvtohz(&ttv);
1024df8bae1dSRodney W. Grimes 		}
1025ace8398dSJeff Roberson 		error = seltdwait(td, timo);
1026ace8398dSJeff Roberson 		if (error)
1027ace8398dSJeff Roberson 			break;
1028ace8398dSJeff Roberson 		error = selrescan(td, ibits, obits);
1029ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1030ace8398dSJeff Roberson 			break;
103185f190e4SAlfred Perlstein 	}
1032ace8398dSJeff Roberson 	seltdclear(td);
1033265fc98fSSeigo Tanimura 
1034df8bae1dSRodney W. Grimes done:
1035df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
1036df8bae1dSRodney W. Grimes 	if (error == ERESTART)
1037df8bae1dSRodney W. Grimes 		error = EINTR;
1038df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
1039df8bae1dSRodney W. Grimes 		error = 0;
1040841c0c7eSNathan Whitehorn 
1041841c0c7eSNathan Whitehorn 	/* swizzle bit order back, if necessary */
1042841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[0]);
1043841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[1]);
1044841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[2]);
1045841c0c7eSNathan Whitehorn #undef swizzle_fdset
1046841c0c7eSNathan Whitehorn 
1047df8bae1dSRodney W. Grimes #define	putbits(name, x) \
1048b55ef216SKonstantin Belousov 	if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
1049df8bae1dSRodney W. Grimes 		error = error2;
1050df8bae1dSRodney W. Grimes 	if (error == 0) {
1051df8bae1dSRodney W. Grimes 		int error2;
1052df8bae1dSRodney W. Grimes 
10538f19eb88SIan Dowse 		putbits(fd_in, 0);
10548f19eb88SIan Dowse 		putbits(fd_ou, 1);
10558f19eb88SIan Dowse 		putbits(fd_ex, 2);
1056df8bae1dSRodney W. Grimes #undef putbits
1057df8bae1dSRodney W. Grimes 	}
1058d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
1059d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
1060ad2edad9SMatthew Dillon 
1061df8bae1dSRodney W. Grimes 	return (error);
1062df8bae1dSRodney W. Grimes }
106311b763dfSJeff Roberson /*
106411b763dfSJeff Roberson  * Convert a select bit set to poll flags.
1065748b9df6SJeff Roberson  *
106611b763dfSJeff Roberson  * The backend always returns POLLHUP/POLLERR if appropriate and we
106711b763dfSJeff Roberson  * return this as a set bit in any set.
106811b763dfSJeff Roberson  */
106911b763dfSJeff Roberson static int select_flags[3] = {
107011b763dfSJeff Roberson     POLLRDNORM | POLLHUP | POLLERR,
107111b763dfSJeff Roberson     POLLWRNORM | POLLHUP | POLLERR,
107261e53a38SKonstantin Belousov     POLLRDBAND | POLLERR
107311b763dfSJeff Roberson };
107411b763dfSJeff Roberson 
107511b763dfSJeff Roberson /*
107611b763dfSJeff Roberson  * Compute the fo_poll flags required for a fd given by the index and
107711b763dfSJeff Roberson  * bit position in the fd_mask array.
107811b763dfSJeff Roberson  */
107911b763dfSJeff Roberson static __inline int
108060b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit)
108111b763dfSJeff Roberson {
108211b763dfSJeff Roberson 	int flags;
108311b763dfSJeff Roberson 	int msk;
108411b763dfSJeff Roberson 
108511b763dfSJeff Roberson 	flags = 0;
108611b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
108711b763dfSJeff Roberson 		if (ibits[msk] == NULL)
108811b763dfSJeff Roberson 			continue;
108960b7f468SStephane E. Potvin 		if ((ibits[msk][idx] & bit) == 0)
109011b763dfSJeff Roberson 			continue;
109111b763dfSJeff Roberson 		flags |= select_flags[msk];
109211b763dfSJeff Roberson 	}
109311b763dfSJeff Roberson 	return (flags);
109411b763dfSJeff Roberson }
109511b763dfSJeff Roberson 
109611b763dfSJeff Roberson /*
109711b763dfSJeff Roberson  * Set the appropriate output bits given a mask of fired events and the
109811b763dfSJeff Roberson  * input bits originally requested.
109911b763dfSJeff Roberson  */
110011b763dfSJeff Roberson static __inline int
110111b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
110211b763dfSJeff Roberson {
110311b763dfSJeff Roberson 	int msk;
110411b763dfSJeff Roberson 	int n;
110511b763dfSJeff Roberson 
110611b763dfSJeff Roberson 	n = 0;
110711b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
110811b763dfSJeff Roberson 		if ((events & select_flags[msk]) == 0)
110911b763dfSJeff Roberson 			continue;
111011b763dfSJeff Roberson 		if (ibits[msk] == NULL)
111111b763dfSJeff Roberson 			continue;
111211b763dfSJeff Roberson 		if ((ibits[msk][idx] & bit) == 0)
111311b763dfSJeff Roberson 			continue;
111411b763dfSJeff Roberson 		/*
111511b763dfSJeff Roberson 		 * XXX Check for a duplicate set.  This can occur because a
111611b763dfSJeff Roberson 		 * socket calls selrecord() twice for each poll() call
111711b763dfSJeff Roberson 		 * resulting in two selfds per real fd.  selrescan() will
111811b763dfSJeff Roberson 		 * call selsetbits twice as a result.
111911b763dfSJeff Roberson 		 */
112011b763dfSJeff Roberson 		if ((obits[msk][idx] & bit) != 0)
112111b763dfSJeff Roberson 			continue;
112211b763dfSJeff Roberson 		obits[msk][idx] |= bit;
112311b763dfSJeff Roberson 		n++;
112411b763dfSJeff Roberson 	}
112511b763dfSJeff Roberson 
112611b763dfSJeff Roberson 	return (n);
112711b763dfSJeff Roberson }
1128df8bae1dSRodney W. Grimes 
1129a9d2f8d8SRobert Watson static __inline int
1130a9d2f8d8SRobert Watson getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp)
1131a9d2f8d8SRobert Watson {
1132a9d2f8d8SRobert Watson 	struct file *fp;
1133a9d2f8d8SRobert Watson #ifdef CAPABILITIES
1134a9d2f8d8SRobert Watson 	struct file *fp_fromcap;
1135a9d2f8d8SRobert Watson 	int error;
1136a9d2f8d8SRobert Watson #endif
1137a9d2f8d8SRobert Watson 
1138a9d2f8d8SRobert Watson 	if ((fp = fget_unlocked(fdp, fd)) == NULL)
1139a9d2f8d8SRobert Watson 		return (EBADF);
1140a9d2f8d8SRobert Watson #ifdef CAPABILITIES
1141a9d2f8d8SRobert Watson 	/*
1142a9d2f8d8SRobert Watson 	 * If the file descriptor is for a capability, test rights and use
1143a9d2f8d8SRobert Watson 	 * the file descriptor references by the capability.
1144a9d2f8d8SRobert Watson 	 */
1145d1b6899eSJonathan Anderson 	error = cap_funwrap(fp, CAP_POLL_EVENT, &fp_fromcap);
1146a9d2f8d8SRobert Watson 	if (error) {
1147a9d2f8d8SRobert Watson 		fdrop(fp, curthread);
1148a9d2f8d8SRobert Watson 		return (error);
1149a9d2f8d8SRobert Watson 	}
1150a9d2f8d8SRobert Watson 	if (fp != fp_fromcap) {
1151a9d2f8d8SRobert Watson 		fhold(fp_fromcap);
1152a9d2f8d8SRobert Watson 		fdrop(fp, curthread);
1153a9d2f8d8SRobert Watson 		fp = fp_fromcap;
1154a9d2f8d8SRobert Watson 	}
1155a9d2f8d8SRobert Watson #endif /* CAPABILITIES */
1156a9d2f8d8SRobert Watson 	*fpp = fp;
1157a9d2f8d8SRobert Watson 	return (0);
1158a9d2f8d8SRobert Watson }
1159a9d2f8d8SRobert Watson 
1160ace8398dSJeff Roberson /*
1161ace8398dSJeff Roberson  * Traverse the list of fds attached to this thread's seltd and check for
1162ace8398dSJeff Roberson  * completion.
1163ace8398dSJeff Roberson  */
1164ace8398dSJeff Roberson static int
1165ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
1166ace8398dSJeff Roberson {
116711b763dfSJeff Roberson 	struct filedesc *fdp;
116811b763dfSJeff Roberson 	struct selinfo *si;
1169ace8398dSJeff Roberson 	struct seltd *stp;
1170ace8398dSJeff Roberson 	struct selfd *sfp;
1171ace8398dSJeff Roberson 	struct selfd *sfn;
1172ace8398dSJeff Roberson 	struct file *fp;
11739cdacff1SJeff Roberson 	fd_mask bit;
11749cdacff1SJeff Roberson 	int fd, ev, n, idx;
1175a9d2f8d8SRobert Watson 	int error;
1176ace8398dSJeff Roberson 
117711b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
1178ace8398dSJeff Roberson 	stp = td->td_sel;
117911b763dfSJeff Roberson 	n = 0;
1180ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1181ace8398dSJeff Roberson 		fd = (int)(uintptr_t)sfp->sf_cookie;
1182ace8398dSJeff Roberson 		si = sfp->sf_si;
1183ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1184ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1185ace8398dSJeff Roberson 		if (si != NULL)
1186ace8398dSJeff Roberson 			continue;
1187a9d2f8d8SRobert Watson 		error = getselfd_cap(fdp, fd, &fp);
1188a9d2f8d8SRobert Watson 		if (error)
1189a9d2f8d8SRobert Watson 			return (error);
119011b763dfSJeff Roberson 		idx = fd / NFDBITS;
11919cdacff1SJeff Roberson 		bit = (fd_mask)1 << (fd % NFDBITS);
119211b763dfSJeff Roberson 		ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
1193bf422e5fSJeff Roberson 		fdrop(fp, td);
119411b763dfSJeff Roberson 		if (ev != 0)
119511b763dfSJeff Roberson 			n += selsetbits(ibits, obits, idx, bit, ev);
1196ace8398dSJeff Roberson 	}
1197ace8398dSJeff Roberson 	stp->st_flags = 0;
1198ace8398dSJeff Roberson 	td->td_retval[0] = n;
1199ace8398dSJeff Roberson 	return (0);
1200ace8398dSJeff Roberson }
1201ace8398dSJeff Roberson 
1202ace8398dSJeff Roberson /*
1203ace8398dSJeff Roberson  * Perform the initial filedescriptor scan and register ourselves with
1204ace8398dSJeff Roberson  * each selinfo.
1205ace8398dSJeff Roberson  */
1206265fc98fSSeigo Tanimura static int
1207b40ce416SJulian Elischer selscan(td, ibits, obits, nfd)
1208b40ce416SJulian Elischer 	struct thread *td;
1209b08f7993SSujal Patel 	fd_mask **ibits, **obits;
1210cb226aaaSPoul-Henning Kamp 	int nfd;
1211df8bae1dSRodney W. Grimes {
121211b763dfSJeff Roberson 	struct filedesc *fdp;
1213df8bae1dSRodney W. Grimes 	struct file *fp;
12149cdacff1SJeff Roberson 	fd_mask bit;
121511b763dfSJeff Roberson 	int ev, flags, end, fd;
12169cdacff1SJeff Roberson 	int n, idx;
1217a9d2f8d8SRobert Watson 	int error;
1218df8bae1dSRodney W. Grimes 
121911b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
122011b763dfSJeff Roberson 	n = 0;
12219cdacff1SJeff Roberson 	for (idx = 0, fd = 0; fd < nfd; idx++) {
122211b763dfSJeff Roberson 		end = imin(fd + NFDBITS, nfd);
122311b763dfSJeff Roberson 		for (bit = 1; fd < end; bit <<= 1, fd++) {
122411b763dfSJeff Roberson 			/* Compute the list of events we're interested in. */
122511b763dfSJeff Roberson 			flags = selflags(ibits, idx, bit);
122611b763dfSJeff Roberson 			if (flags == 0)
1227f082218cSPeter Wemm 				continue;
1228a9d2f8d8SRobert Watson 			error = getselfd_cap(fdp, fd, &fp);
1229a9d2f8d8SRobert Watson 			if (error)
1230a9d2f8d8SRobert Watson 				return (error);
1231ace8398dSJeff Roberson 			selfdalloc(td, (void *)(uintptr_t)fd);
123211b763dfSJeff Roberson 			ev = fo_poll(fp, flags, td->td_ucred, td);
1233bf422e5fSJeff Roberson 			fdrop(fp, td);
123411b763dfSJeff Roberson 			if (ev != 0)
123511b763dfSJeff Roberson 				n += selsetbits(ibits, obits, idx, bit, ev);
1236df8bae1dSRodney W. Grimes 		}
1237df8bae1dSRodney W. Grimes 	}
123811b763dfSJeff Roberson 
1239b40ce416SJulian Elischer 	td->td_retval[0] = n;
1240df8bae1dSRodney W. Grimes 	return (0);
1241df8bae1dSRodney W. Grimes }
1242df8bae1dSRodney W. Grimes 
124342d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
124442d11757SPeter Wemm struct poll_args {
124542d11757SPeter Wemm 	struct pollfd *fds;
124642d11757SPeter Wemm 	u_int	nfds;
124742d11757SPeter Wemm 	int	timeout;
124842d11757SPeter Wemm };
124942d11757SPeter Wemm #endif
125042d11757SPeter Wemm int
12518451d0ddSKip Macy sys_poll(td, uap)
1252b40ce416SJulian Elischer 	struct thread *td;
1253ea0237edSJonathan Lemon 	struct poll_args *uap;
125442d11757SPeter Wemm {
12552580f4e5SAndre Oppermann 	struct pollfd *bits;
12562580f4e5SAndre Oppermann 	struct pollfd smallbits[32];
125700af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
12589ae6d334SKelly Yancey 	int error = 0, timo;
1259ace8398dSJeff Roberson 	u_int nfds;
126042d11757SPeter Wemm 	size_t ni;
126142d11757SPeter Wemm 
1262d1e405c5SAlfred Perlstein 	nfds = uap->nfds;
1263374ae2a3SJeff Roberson 	if (nfds > maxfilesperproc && nfds > FD_SETSIZE)
1264ace8398dSJeff Roberson 		return (EINVAL);
126589b71647SPeter Wemm 	ni = nfds * sizeof(struct pollfd);
126642d11757SPeter Wemm 	if (ni > sizeof(smallbits))
1267a163d034SWarner Losh 		bits = malloc(ni, M_TEMP, M_WAITOK);
126842d11757SPeter Wemm 	else
126942d11757SPeter Wemm 		bits = smallbits;
1270d1e405c5SAlfred Perlstein 	error = copyin(uap->fds, bits, ni);
127142d11757SPeter Wemm 	if (error)
1272ace8398dSJeff Roberson 		goto done;
1273d1e405c5SAlfred Perlstein 	if (uap->timeout != INFTIM) {
1274d1e405c5SAlfred Perlstein 		atv.tv_sec = uap->timeout / 1000;
1275d1e405c5SAlfred Perlstein 		atv.tv_usec = (uap->timeout % 1000) * 1000;
127642d11757SPeter Wemm 		if (itimerfix(&atv)) {
127742d11757SPeter Wemm 			error = EINVAL;
1278ace8398dSJeff Roberson 			goto done;
127942d11757SPeter Wemm 		}
1280c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
128100af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
12829c386f6bSJohn Baldwin 	} else {
128300af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
12849c386f6bSJohn Baldwin 		atv.tv_usec = 0;
12859c386f6bSJohn Baldwin 	}
128600af9731SPoul-Henning Kamp 	timo = 0;
1287ace8398dSJeff Roberson 	seltdinit(td);
1288ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1289ace8398dSJeff Roberson 	for (;;) {
12902580f4e5SAndre Oppermann 		error = pollscan(td, bits, nfds);
1291ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1292ace8398dSJeff Roberson 			break;
12934da144c0SJohn Baldwin 		if (atv.tv_sec || atv.tv_usec) {
1294c21410e1SPoul-Henning Kamp 			getmicrouptime(&rtv);
129585f190e4SAlfred Perlstein 			if (timevalcmp(&rtv, &atv, >=))
1296ace8398dSJeff Roberson 				break;
129700af9731SPoul-Henning Kamp 			ttv = atv;
129800af9731SPoul-Henning Kamp 			timevalsub(&ttv, &rtv);
129900af9731SPoul-Henning Kamp 			timo = ttv.tv_sec > 24 * 60 * 60 ?
130000af9731SPoul-Henning Kamp 			    24 * 60 * 60 * hz : tvtohz(&ttv);
130142d11757SPeter Wemm 		}
1302ace8398dSJeff Roberson 		error = seltdwait(td, timo);
1303ace8398dSJeff Roberson 		if (error)
1304ace8398dSJeff Roberson 			break;
1305ace8398dSJeff Roberson 		error = pollrescan(td);
1306ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1307ace8398dSJeff Roberson 			break;
130885f190e4SAlfred Perlstein 	}
1309ace8398dSJeff Roberson 	seltdclear(td);
1310265fc98fSSeigo Tanimura 
131142d11757SPeter Wemm done:
131242d11757SPeter Wemm 	/* poll is not restarted after signals... */
131342d11757SPeter Wemm 	if (error == ERESTART)
131442d11757SPeter Wemm 		error = EINTR;
131542d11757SPeter Wemm 	if (error == EWOULDBLOCK)
131642d11757SPeter Wemm 		error = 0;
131742d11757SPeter Wemm 	if (error == 0) {
13186d8feddaSKonstantin Belousov 		error = pollout(td, bits, uap->fds, nfds);
131942d11757SPeter Wemm 		if (error)
132042d11757SPeter Wemm 			goto out;
132142d11757SPeter Wemm 	}
132242d11757SPeter Wemm out:
132342d11757SPeter Wemm 	if (ni > sizeof(smallbits))
132442d11757SPeter Wemm 		free(bits, M_TEMP);
132542d11757SPeter Wemm 	return (error);
132642d11757SPeter Wemm }
132742d11757SPeter Wemm 
132842d11757SPeter Wemm static int
1329ace8398dSJeff Roberson pollrescan(struct thread *td)
1330ace8398dSJeff Roberson {
1331ace8398dSJeff Roberson 	struct seltd *stp;
1332ace8398dSJeff Roberson 	struct selfd *sfp;
1333ace8398dSJeff Roberson 	struct selfd *sfn;
1334ace8398dSJeff Roberson 	struct selinfo *si;
1335ace8398dSJeff Roberson 	struct filedesc *fdp;
1336ace8398dSJeff Roberson 	struct file *fp;
1337ace8398dSJeff Roberson 	struct pollfd *fd;
1338ace8398dSJeff Roberson 	int n;
1339ace8398dSJeff Roberson 
1340ace8398dSJeff Roberson 	n = 0;
1341ace8398dSJeff Roberson 	fdp = td->td_proc->p_fd;
1342ace8398dSJeff Roberson 	stp = td->td_sel;
1343ace8398dSJeff Roberson 	FILEDESC_SLOCK(fdp);
1344ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1345ace8398dSJeff Roberson 		fd = (struct pollfd *)sfp->sf_cookie;
1346ace8398dSJeff Roberson 		si = sfp->sf_si;
1347ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1348ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1349ace8398dSJeff Roberson 		if (si != NULL)
1350ace8398dSJeff Roberson 			continue;
1351ace8398dSJeff Roberson 		fp = fdp->fd_ofiles[fd->fd];
1352d6f72489SJonathan Anderson #ifdef CAPABILITIES
1353d6f72489SJonathan Anderson 		if ((fp == NULL)
1354d6f72489SJonathan Anderson 		    || (cap_funwrap(fp, CAP_POLL_EVENT, &fp) != 0)) {
1355d6f72489SJonathan Anderson #else
1356ace8398dSJeff Roberson 		if (fp == NULL) {
1357d6f72489SJonathan Anderson #endif
1358ace8398dSJeff Roberson 			fd->revents = POLLNVAL;
1359ace8398dSJeff Roberson 			n++;
1360ace8398dSJeff Roberson 			continue;
1361ace8398dSJeff Roberson 		}
1362d6f72489SJonathan Anderson 
1363ace8398dSJeff Roberson 		/*
1364ace8398dSJeff Roberson 		 * Note: backend also returns POLLHUP and
1365ace8398dSJeff Roberson 		 * POLLERR if appropriate.
1366ace8398dSJeff Roberson 		 */
1367ace8398dSJeff Roberson 		fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1368ace8398dSJeff Roberson 		if (fd->revents != 0)
1369ace8398dSJeff Roberson 			n++;
1370ace8398dSJeff Roberson 	}
1371ace8398dSJeff Roberson 	FILEDESC_SUNLOCK(fdp);
1372ace8398dSJeff Roberson 	stp->st_flags = 0;
1373ace8398dSJeff Roberson 	td->td_retval[0] = n;
1374ace8398dSJeff Roberson 	return (0);
1375ace8398dSJeff Roberson }
1376ace8398dSJeff Roberson 
1377ace8398dSJeff Roberson 
1378ace8398dSJeff Roberson static int
13796d8feddaSKonstantin Belousov pollout(td, fds, ufds, nfd)
13806d8feddaSKonstantin Belousov 	struct thread *td;
1381ae81968fSRobert Watson 	struct pollfd *fds;
1382ae81968fSRobert Watson 	struct pollfd *ufds;
1383ae81968fSRobert Watson 	u_int nfd;
1384ae81968fSRobert Watson {
1385ae81968fSRobert Watson 	int error = 0;
1386ae81968fSRobert Watson 	u_int i = 0;
13876d8feddaSKonstantin Belousov 	u_int n = 0;
1388ae81968fSRobert Watson 
1389ae81968fSRobert Watson 	for (i = 0; i < nfd; i++) {
1390ae81968fSRobert Watson 		error = copyout(&fds->revents, &ufds->revents,
1391ae81968fSRobert Watson 		    sizeof(ufds->revents));
1392ae81968fSRobert Watson 		if (error)
1393ae81968fSRobert Watson 			return (error);
13946d8feddaSKonstantin Belousov 		if (fds->revents != 0)
13956d8feddaSKonstantin Belousov 			n++;
1396ae81968fSRobert Watson 		fds++;
1397ae81968fSRobert Watson 		ufds++;
1398ae81968fSRobert Watson 	}
13996d8feddaSKonstantin Belousov 	td->td_retval[0] = n;
1400ae81968fSRobert Watson 	return (0);
1401ae81968fSRobert Watson }
1402ae81968fSRobert Watson 
1403ae81968fSRobert Watson static int
1404b40ce416SJulian Elischer pollscan(td, fds, nfd)
1405b40ce416SJulian Elischer 	struct thread *td;
140642d11757SPeter Wemm 	struct pollfd *fds;
1407ea0237edSJonathan Lemon 	u_int nfd;
140842d11757SPeter Wemm {
1409ace8398dSJeff Roberson 	struct filedesc *fdp = td->td_proc->p_fd;
141042d11757SPeter Wemm 	int i;
141142d11757SPeter Wemm 	struct file *fp;
141242d11757SPeter Wemm 	int n = 0;
141342d11757SPeter Wemm 
14145e3f7694SRobert Watson 	FILEDESC_SLOCK(fdp);
1415eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
1416337c9691SJordan K. Hubbard 		if (fds->fd >= fdp->fd_nfiles) {
141742d11757SPeter Wemm 			fds->revents = POLLNVAL;
141842d11757SPeter Wemm 			n++;
1419337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
1420337c9691SJordan K. Hubbard 			fds->revents = 0;
142142d11757SPeter Wemm 		} else {
142242d11757SPeter Wemm 			fp = fdp->fd_ofiles[fds->fd];
1423d6f72489SJonathan Anderson #ifdef CAPABILITIES
1424d6f72489SJonathan Anderson 			if ((fp == NULL)
1425d6f72489SJonathan Anderson 			    || (cap_funwrap(fp, CAP_POLL_EVENT, &fp) != 0)) {
1426d6f72489SJonathan Anderson #else
1427279d7226SMatthew Dillon 			if (fp == NULL) {
1428d6f72489SJonathan Anderson #endif
142942d11757SPeter Wemm 				fds->revents = POLLNVAL;
143042d11757SPeter Wemm 				n++;
143142d11757SPeter Wemm 			} else {
14322087c896SBruce Evans 				/*
14332087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
14342087c896SBruce Evans 				 * POLLERR if appropriate.
14352087c896SBruce Evans 				 */
1436ace8398dSJeff Roberson 				selfdalloc(td, fds);
143713ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
1438ea6027a8SRobert Watson 				    td->td_ucred, td);
1439f2159cc7SKonstantin Belousov 				/*
1440f2159cc7SKonstantin Belousov 				 * POSIX requires POLLOUT to be never
1441f2159cc7SKonstantin Belousov 				 * set simultaneously with POLLHUP.
1442f2159cc7SKonstantin Belousov 				 */
1443f2159cc7SKonstantin Belousov 				if ((fds->revents & POLLHUP) != 0)
1444f2159cc7SKonstantin Belousov 					fds->revents &= ~POLLOUT;
1445f2159cc7SKonstantin Belousov 
144642d11757SPeter Wemm 				if (fds->revents != 0)
144742d11757SPeter Wemm 					n++;
144842d11757SPeter Wemm 			}
144942d11757SPeter Wemm 		}
145042d11757SPeter Wemm 	}
14515e3f7694SRobert Watson 	FILEDESC_SUNLOCK(fdp);
1452b40ce416SJulian Elischer 	td->td_retval[0] = n;
145342d11757SPeter Wemm 	return (0);
145442d11757SPeter Wemm }
145542d11757SPeter Wemm 
145642d11757SPeter Wemm /*
145742d11757SPeter Wemm  * OpenBSD poll system call.
14580c14ff0eSRobert Watson  *
145942d11757SPeter Wemm  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
146042d11757SPeter Wemm  */
146142d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
146242d11757SPeter Wemm struct openbsd_poll_args {
146342d11757SPeter Wemm 	struct pollfd *fds;
146442d11757SPeter Wemm 	u_int	nfds;
146542d11757SPeter Wemm 	int	timeout;
146642d11757SPeter Wemm };
146742d11757SPeter Wemm #endif
146842d11757SPeter Wemm int
14698451d0ddSKip Macy sys_openbsd_poll(td, uap)
1470b40ce416SJulian Elischer 	register struct thread *td;
147142d11757SPeter Wemm 	register struct openbsd_poll_args *uap;
147242d11757SPeter Wemm {
14738451d0ddSKip Macy 	return (sys_poll(td, (struct poll_args *)uap));
147442d11757SPeter Wemm }
147542d11757SPeter Wemm 
147685f190e4SAlfred Perlstein /*
1477ace8398dSJeff Roberson  * XXX This was created specifically to support netncp and netsmb.  This
1478ace8398dSJeff Roberson  * allows the caller to specify a socket to wait for events on.  It returns
1479ace8398dSJeff Roberson  * 0 if any events matched and an error otherwise.  There is no way to
1480ace8398dSJeff Roberson  * determine which events fired.
148185f190e4SAlfred Perlstein  */
1482ace8398dSJeff Roberson int
1483ace8398dSJeff Roberson selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td)
148485f190e4SAlfred Perlstein {
1485ace8398dSJeff Roberson 	struct timeval atv, rtv, ttv;
1486ace8398dSJeff Roberson 	int error, timo;
148785f190e4SAlfred Perlstein 
1488ace8398dSJeff Roberson 	if (tvp != NULL) {
1489ace8398dSJeff Roberson 		atv = *tvp;
1490ace8398dSJeff Roberson 		if (itimerfix(&atv))
1491ace8398dSJeff Roberson 			return (EINVAL);
1492ace8398dSJeff Roberson 		getmicrouptime(&rtv);
1493ace8398dSJeff Roberson 		timevaladd(&atv, &rtv);
1494ace8398dSJeff Roberson 	} else {
1495ace8398dSJeff Roberson 		atv.tv_sec = 0;
1496ace8398dSJeff Roberson 		atv.tv_usec = 0;
1497ace8398dSJeff Roberson 	}
1498ace8398dSJeff Roberson 
1499ace8398dSJeff Roberson 	timo = 0;
1500ace8398dSJeff Roberson 	seltdinit(td);
1501ace8398dSJeff Roberson 	/*
1502ace8398dSJeff Roberson 	 * Iterate until the timeout expires or the socket becomes ready.
1503ace8398dSJeff Roberson 	 */
1504ace8398dSJeff Roberson 	for (;;) {
1505ace8398dSJeff Roberson 		selfdalloc(td, NULL);
1506ace8398dSJeff Roberson 		error = sopoll(so, events, NULL, td);
1507ace8398dSJeff Roberson 		/* error here is actually the ready events. */
1508ace8398dSJeff Roberson 		if (error)
1509ace8398dSJeff Roberson 			return (0);
1510ace8398dSJeff Roberson 		if (atv.tv_sec || atv.tv_usec) {
1511ace8398dSJeff Roberson 			getmicrouptime(&rtv);
1512ace8398dSJeff Roberson 			if (timevalcmp(&rtv, &atv, >=)) {
1513ace8398dSJeff Roberson 				seltdclear(td);
1514ace8398dSJeff Roberson 				return (EWOULDBLOCK);
1515ace8398dSJeff Roberson 			}
1516ace8398dSJeff Roberson 			ttv = atv;
1517ace8398dSJeff Roberson 			timevalsub(&ttv, &rtv);
1518ace8398dSJeff Roberson 			timo = ttv.tv_sec > 24 * 60 * 60 ?
1519ace8398dSJeff Roberson 			    24 * 60 * 60 * hz : tvtohz(&ttv);
1520ace8398dSJeff Roberson 		}
1521ace8398dSJeff Roberson 		error = seltdwait(td, timo);
1522ace8398dSJeff Roberson 		seltdclear(td);
1523ace8398dSJeff Roberson 		if (error)
1524ace8398dSJeff Roberson 			break;
1525ace8398dSJeff Roberson 	}
1526ace8398dSJeff Roberson 	/* XXX Duplicates ncp/smb behavior. */
1527ace8398dSJeff Roberson 	if (error == ERESTART)
1528ace8398dSJeff Roberson 		error = 0;
1529ace8398dSJeff Roberson 	return (error);
1530ace8398dSJeff Roberson }
1531ace8398dSJeff Roberson 
1532ace8398dSJeff Roberson /*
1533ace8398dSJeff Roberson  * Preallocate two selfds associated with 'cookie'.  Some fo_poll routines
1534ace8398dSJeff Roberson  * have two select sets, one for read and another for write.
1535ace8398dSJeff Roberson  */
1536ace8398dSJeff Roberson static void
1537ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie)
1538ace8398dSJeff Roberson {
1539ace8398dSJeff Roberson 	struct seltd *stp;
1540ace8398dSJeff Roberson 
1541ace8398dSJeff Roberson 	stp = td->td_sel;
1542ace8398dSJeff Roberson 	if (stp->st_free1 == NULL)
1543ace8398dSJeff Roberson 		stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1544ace8398dSJeff Roberson 	stp->st_free1->sf_td = stp;
1545ace8398dSJeff Roberson 	stp->st_free1->sf_cookie = cookie;
1546ace8398dSJeff Roberson 	if (stp->st_free2 == NULL)
1547ace8398dSJeff Roberson 		stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1548ace8398dSJeff Roberson 	stp->st_free2->sf_td = stp;
1549ace8398dSJeff Roberson 	stp->st_free2->sf_cookie = cookie;
1550ace8398dSJeff Roberson }
1551ace8398dSJeff Roberson 
1552ace8398dSJeff Roberson static void
1553ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp)
1554ace8398dSJeff Roberson {
1555ace8398dSJeff Roberson 	STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
1556ace8398dSJeff Roberson 	mtx_lock(sfp->sf_mtx);
1557ace8398dSJeff Roberson 	if (sfp->sf_si)
1558ace8398dSJeff Roberson 		TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1559ace8398dSJeff Roberson 	mtx_unlock(sfp->sf_mtx);
1560ace8398dSJeff Roberson 	uma_zfree(selfd_zone, sfp);
156185f190e4SAlfred Perlstein }
156285f190e4SAlfred Perlstein 
15636aba400aSAttilio Rao /* Drain the waiters tied to all the selfd belonging the specified selinfo. */
15646aba400aSAttilio Rao void
15656aba400aSAttilio Rao seldrain(sip)
15666aba400aSAttilio Rao         struct selinfo *sip;
15676aba400aSAttilio Rao {
15686aba400aSAttilio Rao 
15696aba400aSAttilio Rao 	/*
15706aba400aSAttilio Rao 	 * This feature is already provided by doselwakeup(), thus it is
15716aba400aSAttilio Rao 	 * enough to go for it.
15726aba400aSAttilio Rao 	 * Eventually, the context, should take care to avoid races
15736aba400aSAttilio Rao 	 * between thread calling select()/poll() and file descriptor
15746aba400aSAttilio Rao 	 * detaching, but, again, the races are just the same as
15756aba400aSAttilio Rao 	 * selwakeup().
15766aba400aSAttilio Rao 	 */
15776aba400aSAttilio Rao         doselwakeup(sip, -1);
15786aba400aSAttilio Rao }
15796aba400aSAttilio Rao 
1580df8bae1dSRodney W. Grimes /*
1581df8bae1dSRodney W. Grimes  * Record a select request.
1582df8bae1dSRodney W. Grimes  */
1583df8bae1dSRodney W. Grimes void
1584df8bae1dSRodney W. Grimes selrecord(selector, sip)
1585b40ce416SJulian Elischer 	struct thread *selector;
1586df8bae1dSRodney W. Grimes 	struct selinfo *sip;
1587df8bae1dSRodney W. Grimes {
1588ace8398dSJeff Roberson 	struct selfd *sfp;
1589ace8398dSJeff Roberson 	struct seltd *stp;
1590ace8398dSJeff Roberson 	struct mtx *mtxp;
1591df8bae1dSRodney W. Grimes 
1592ace8398dSJeff Roberson 	stp = selector->td_sel;
159385f190e4SAlfred Perlstein 	/*
1594ace8398dSJeff Roberson 	 * Don't record when doing a rescan.
159585f190e4SAlfred Perlstein 	 */
1596ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_RESCAN)
1597ace8398dSJeff Roberson 		return;
1598ace8398dSJeff Roberson 	/*
1599ace8398dSJeff Roberson 	 * Grab one of the preallocated descriptors.
1600ace8398dSJeff Roberson 	 */
1601ace8398dSJeff Roberson 	sfp = NULL;
1602ace8398dSJeff Roberson 	if ((sfp = stp->st_free1) != NULL)
1603ace8398dSJeff Roberson 		stp->st_free1 = NULL;
1604ace8398dSJeff Roberson 	else if ((sfp = stp->st_free2) != NULL)
1605ace8398dSJeff Roberson 		stp->st_free2 = NULL;
1606ace8398dSJeff Roberson 	else
1607ace8398dSJeff Roberson 		panic("selrecord: No free selfd on selq");
16082141453eSJeff Roberson 	mtxp = sip->si_mtx;
16092141453eSJeff Roberson 	if (mtxp == NULL)
16102141453eSJeff Roberson 		mtxp = mtx_pool_find(mtxpool_select, sip);
1611ace8398dSJeff Roberson 	/*
1612ace8398dSJeff Roberson 	 * Initialize the sfp and queue it in the thread.
1613ace8398dSJeff Roberson 	 */
1614ace8398dSJeff Roberson 	sfp->sf_si = sip;
1615ace8398dSJeff Roberson 	sfp->sf_mtx = mtxp;
1616ace8398dSJeff Roberson 	STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1617ace8398dSJeff Roberson 	/*
1618ace8398dSJeff Roberson 	 * Now that we've locked the sip, check for initialization.
1619ace8398dSJeff Roberson 	 */
1620ace8398dSJeff Roberson 	mtx_lock(mtxp);
1621ace8398dSJeff Roberson 	if (sip->si_mtx == NULL) {
1622ace8398dSJeff Roberson 		sip->si_mtx = mtxp;
1623ace8398dSJeff Roberson 		TAILQ_INIT(&sip->si_tdlist);
162485f190e4SAlfred Perlstein 	}
1625ace8398dSJeff Roberson 	/*
1626ace8398dSJeff Roberson 	 * Add this thread to the list of selfds listening on this selinfo.
1627ace8398dSJeff Roberson 	 */
1628ace8398dSJeff Roberson 	TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1629ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1630df8bae1dSRodney W. Grimes }
1631df8bae1dSRodney W. Grimes 
1632512824f8SSeigo Tanimura /* Wake up a selecting thread. */
1633df8bae1dSRodney W. Grimes void
1634df8bae1dSRodney W. Grimes selwakeup(sip)
163585f190e4SAlfred Perlstein 	struct selinfo *sip;
1636df8bae1dSRodney W. Grimes {
1637512824f8SSeigo Tanimura 	doselwakeup(sip, -1);
1638512824f8SSeigo Tanimura }
1639512824f8SSeigo Tanimura 
1640512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */
1641512824f8SSeigo Tanimura void
1642512824f8SSeigo Tanimura selwakeuppri(sip, pri)
1643512824f8SSeigo Tanimura 	struct selinfo *sip;
1644512824f8SSeigo Tanimura 	int pri;
1645512824f8SSeigo Tanimura {
1646512824f8SSeigo Tanimura 	doselwakeup(sip, pri);
1647512824f8SSeigo Tanimura }
1648512824f8SSeigo Tanimura 
1649512824f8SSeigo Tanimura /*
1650512824f8SSeigo Tanimura  * Do a wakeup when a selectable event occurs.
1651512824f8SSeigo Tanimura  */
1652512824f8SSeigo Tanimura static void
1653512824f8SSeigo Tanimura doselwakeup(sip, pri)
1654512824f8SSeigo Tanimura 	struct selinfo *sip;
1655512824f8SSeigo Tanimura 	int pri;
1656512824f8SSeigo Tanimura {
1657ace8398dSJeff Roberson 	struct selfd *sfp;
1658ace8398dSJeff Roberson 	struct selfd *sfn;
1659ace8398dSJeff Roberson 	struct seltd *stp;
1660df8bae1dSRodney W. Grimes 
1661ace8398dSJeff Roberson 	/* If it's not initialized there can't be any waiters. */
1662ace8398dSJeff Roberson 	if (sip->si_mtx == NULL)
1663b40ce416SJulian Elischer 		return;
1664ace8398dSJeff Roberson 	/*
1665ace8398dSJeff Roberson 	 * Locking the selinfo locks all selfds associated with it.
1666ace8398dSJeff Roberson 	 */
1667ace8398dSJeff Roberson 	mtx_lock(sip->si_mtx);
1668ace8398dSJeff Roberson 	TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1669ace8398dSJeff Roberson 		/*
1670ace8398dSJeff Roberson 		 * Once we remove this sfp from the list and clear the
1671ace8398dSJeff Roberson 		 * sf_si seltdclear will know to ignore this si.
1672ace8398dSJeff Roberson 		 */
1673ace8398dSJeff Roberson 		TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1674ace8398dSJeff Roberson 		sfp->sf_si = NULL;
1675ace8398dSJeff Roberson 		stp = sfp->sf_td;
1676ace8398dSJeff Roberson 		mtx_lock(&stp->st_mtx);
1677ace8398dSJeff Roberson 		stp->st_flags |= SELTD_PENDING;
1678ace8398dSJeff Roberson 		cv_broadcastpri(&stp->st_wait, pri);
1679ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1680b40ce416SJulian Elischer 	}
1681ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1682ace8398dSJeff Roberson }
1683ace8398dSJeff Roberson 
1684ace8398dSJeff Roberson static void
1685ace8398dSJeff Roberson seltdinit(struct thread *td)
1686ace8398dSJeff Roberson {
1687ace8398dSJeff Roberson 	struct seltd *stp;
1688ace8398dSJeff Roberson 
1689ace8398dSJeff Roberson 	if ((stp = td->td_sel) != NULL)
1690ace8398dSJeff Roberson 		goto out;
1691ace8398dSJeff Roberson 	td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1692ace8398dSJeff Roberson 	mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1693ace8398dSJeff Roberson 	cv_init(&stp->st_wait, "select");
1694ace8398dSJeff Roberson out:
1695ace8398dSJeff Roberson 	stp->st_flags = 0;
1696ace8398dSJeff Roberson 	STAILQ_INIT(&stp->st_selq);
1697ace8398dSJeff Roberson }
1698ace8398dSJeff Roberson 
1699ace8398dSJeff Roberson static int
1700ace8398dSJeff Roberson seltdwait(struct thread *td, int timo)
1701ace8398dSJeff Roberson {
1702ace8398dSJeff Roberson 	struct seltd *stp;
1703ace8398dSJeff Roberson 	int error;
1704ace8398dSJeff Roberson 
1705ace8398dSJeff Roberson 	stp = td->td_sel;
1706ace8398dSJeff Roberson 	/*
1707ace8398dSJeff Roberson 	 * An event of interest may occur while we do not hold the seltd
1708ace8398dSJeff Roberson 	 * locked so check the pending flag before we sleep.
1709ace8398dSJeff Roberson 	 */
1710ace8398dSJeff Roberson 	mtx_lock(&stp->st_mtx);
1711ace8398dSJeff Roberson 	/*
1712ace8398dSJeff Roberson 	 * Any further calls to selrecord will be a rescan.
1713ace8398dSJeff Roberson 	 */
1714ace8398dSJeff Roberson 	stp->st_flags |= SELTD_RESCAN;
1715ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_PENDING) {
1716ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1717ace8398dSJeff Roberson 		return (0);
1718ace8398dSJeff Roberson 	}
1719ace8398dSJeff Roberson 	if (timo > 0)
1720ace8398dSJeff Roberson 		error = cv_timedwait_sig(&stp->st_wait, &stp->st_mtx, timo);
1721ace8398dSJeff Roberson 	else
1722ace8398dSJeff Roberson 		error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
1723ace8398dSJeff Roberson 	mtx_unlock(&stp->st_mtx);
1724ace8398dSJeff Roberson 
1725ace8398dSJeff Roberson 	return (error);
1726ace8398dSJeff Roberson }
1727ace8398dSJeff Roberson 
1728ace8398dSJeff Roberson void
1729ace8398dSJeff Roberson seltdfini(struct thread *td)
1730ace8398dSJeff Roberson {
1731ace8398dSJeff Roberson 	struct seltd *stp;
1732ace8398dSJeff Roberson 
1733ace8398dSJeff Roberson 	stp = td->td_sel;
1734ace8398dSJeff Roberson 	if (stp == NULL)
1735ace8398dSJeff Roberson 		return;
1736ace8398dSJeff Roberson 	if (stp->st_free1)
1737ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free1);
1738ace8398dSJeff Roberson 	if (stp->st_free2)
1739ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free2);
1740ace8398dSJeff Roberson 	td->td_sel = NULL;
1741ace8398dSJeff Roberson 	free(stp, M_SELECT);
1742ace8398dSJeff Roberson }
1743ace8398dSJeff Roberson 
1744ace8398dSJeff Roberson /*
1745ace8398dSJeff Roberson  * Remove the references to the thread from all of the objects we were
1746ace8398dSJeff Roberson  * polling.
1747ace8398dSJeff Roberson  */
1748ace8398dSJeff Roberson static void
1749ace8398dSJeff Roberson seltdclear(struct thread *td)
1750ace8398dSJeff Roberson {
1751ace8398dSJeff Roberson 	struct seltd *stp;
1752ace8398dSJeff Roberson 	struct selfd *sfp;
1753ace8398dSJeff Roberson 	struct selfd *sfn;
1754ace8398dSJeff Roberson 
1755ace8398dSJeff Roberson 	stp = td->td_sel;
1756ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
1757ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1758ace8398dSJeff Roberson 	stp->st_flags = 0;
1759df8bae1dSRodney W. Grimes }
1760265fc98fSSeigo Tanimura 
17614d77a549SAlfred Perlstein static void selectinit(void *);
1762ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
1763265fc98fSSeigo Tanimura static void
1764ace8398dSJeff Roberson selectinit(void *dummy __unused)
1765265fc98fSSeigo Tanimura {
17662141453eSJeff Roberson 
1767ace8398dSJeff Roberson 	selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL,
1768ace8398dSJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, 0);
17692141453eSJeff Roberson 	mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);
1770265fc98fSSeigo Tanimura }
1771