xref: /freebsd/sys/kern/sys_generic.c (revision 2609222ab459852b4ae585584d2e68f40d525bb1)
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>
52*2609222aSPawel Jakub Dawidek #include <sys/lock.h>
53df8bae1dSRodney W. Grimes #include <sys/proc.h>
54797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
55df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
56df8bae1dSRodney W. Grimes #include <sys/uio.h>
57df8bae1dSRodney W. Grimes #include <sys/kernel.h>
58e4650294SJohn Baldwin #include <sys/ktr.h>
59104a9b7eSAlexander Kabaev #include <sys/limits.h>
60df8bae1dSRodney W. Grimes #include <sys/malloc.h>
6142d11757SPeter Wemm #include <sys/poll.h>
6289b71647SPeter Wemm #include <sys/resourcevar.h>
630a2c3d48SGarrett Wollman #include <sys/selinfo.h>
6444f3b092SJohn Baldwin #include <sys/sleepqueue.h>
658f19eb88SIan Dowse #include <sys/syscallsubr.h>
668cb96f20SPeter Wemm #include <sys/sysctl.h>
6742d11757SPeter Wemm #include <sys/sysent.h>
689bbee259SAndrey A. Chernov #include <sys/vnode.h>
69279d7226SMatthew Dillon #include <sys/bio.h>
70279d7226SMatthew Dillon #include <sys/buf.h>
71265fc98fSSeigo Tanimura #include <sys/condvar.h>
72df8bae1dSRodney W. Grimes #ifdef KTRACE
73df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
74df8bae1dSRodney W. Grimes #endif
75df8bae1dSRodney W. Grimes 
76e4650294SJohn Baldwin #include <security/audit/audit.h>
77ace8398dSJeff Roberson 
78526d0bd5SKonstantin Belousov int iosize_max_clamp = 1;
798bb9a904SKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, iosize_max_clamp, CTLFLAG_RW,
808bb9a904SKonstantin Belousov     &iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX");
818bb9a904SKonstantin Belousov /*
828bb9a904SKonstantin Belousov  * Assert that the return value of read(2) and write(2) syscalls fits
838bb9a904SKonstantin Belousov  * into a register.  If not, an architecture will need to provide the
848bb9a904SKonstantin Belousov  * usermode wrappers to reconstruct the result.
858bb9a904SKonstantin Belousov  */
868bb9a904SKonstantin Belousov CTASSERT(sizeof(register_t) >= sizeof(size_t));
87526d0bd5SKonstantin Belousov 
88a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
89a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
90a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's");
9155166637SPoul-Henning Kamp 
926d8feddaSKonstantin Belousov static int	pollout(struct thread *, struct pollfd *, struct pollfd *,
936d8feddaSKonstantin Belousov 		    u_int);
94bbbb04ceSAlfred Perlstein static int	pollscan(struct thread *, struct pollfd *, u_int);
95ace8398dSJeff Roberson static int	pollrescan(struct thread *);
96bbbb04ceSAlfred Perlstein static int	selscan(struct thread *, fd_mask **, fd_mask **, int);
97ace8398dSJeff Roberson static int	selrescan(struct thread *, fd_mask **, fd_mask **);
98ace8398dSJeff Roberson static void	selfdalloc(struct thread *, void *);
99ace8398dSJeff Roberson static void	selfdfree(struct seltd *, struct selfd *);
100bcd9e0ddSJohn Baldwin static int	dofileread(struct thread *, int, struct file *, struct uio *,
101bcd9e0ddSJohn Baldwin 		    off_t, int);
102bcd9e0ddSJohn Baldwin static int	dofilewrite(struct thread *, int, struct file *, struct uio *,
103bcd9e0ddSJohn Baldwin 		    off_t, int);
104512824f8SSeigo Tanimura static void	doselwakeup(struct selinfo *, int);
105ace8398dSJeff Roberson static void	seltdinit(struct thread *);
106ace8398dSJeff Roberson static int	seltdwait(struct thread *, int);
107ace8398dSJeff Roberson static void	seltdclear(struct thread *);
108ace8398dSJeff Roberson 
109ace8398dSJeff Roberson /*
110ace8398dSJeff Roberson  * One seltd per-thread allocated on demand as needed.
111ace8398dSJeff Roberson  *
112ace8398dSJeff Roberson  *	t - protected by st_mtx
113ace8398dSJeff Roberson  * 	k - Only accessed by curthread or read-only
114ace8398dSJeff Roberson  */
115ace8398dSJeff Roberson struct seltd {
116ace8398dSJeff Roberson 	STAILQ_HEAD(, selfd)	st_selq;	/* (k) List of selfds. */
117ace8398dSJeff Roberson 	struct selfd		*st_free1;	/* (k) free fd for read set. */
118ace8398dSJeff Roberson 	struct selfd		*st_free2;	/* (k) free fd for write set. */
119ace8398dSJeff Roberson 	struct mtx		st_mtx;		/* Protects struct seltd */
120ace8398dSJeff Roberson 	struct cv		st_wait;	/* (t) Wait channel. */
121ace8398dSJeff Roberson 	int			st_flags;	/* (t) SELTD_ flags. */
122ace8398dSJeff Roberson };
123ace8398dSJeff Roberson 
124ace8398dSJeff Roberson #define	SELTD_PENDING	0x0001			/* We have pending events. */
125ace8398dSJeff Roberson #define	SELTD_RESCAN	0x0002			/* Doing a rescan. */
126ace8398dSJeff Roberson 
127ace8398dSJeff Roberson /*
128ace8398dSJeff Roberson  * One selfd allocated per-thread per-file-descriptor.
129ace8398dSJeff Roberson  *	f - protected by sf_mtx
130ace8398dSJeff Roberson  */
131ace8398dSJeff Roberson struct selfd {
132ace8398dSJeff Roberson 	STAILQ_ENTRY(selfd)	sf_link;	/* (k) fds owned by this td. */
133ace8398dSJeff Roberson 	TAILQ_ENTRY(selfd)	sf_threads;	/* (f) fds on this selinfo. */
134ace8398dSJeff Roberson 	struct selinfo		*sf_si;		/* (f) selinfo when linked. */
135ace8398dSJeff Roberson 	struct mtx		*sf_mtx;	/* Pointer to selinfo mtx. */
136ace8398dSJeff Roberson 	struct seltd		*sf_td;		/* (k) owning seltd. */
137ace8398dSJeff Roberson 	void			*sf_cookie;	/* (k) fd or pollfd. */
138ace8398dSJeff Roberson };
139ace8398dSJeff Roberson 
140ace8398dSJeff Roberson static uma_zone_t selfd_zone;
1412141453eSJeff Roberson static struct mtx_pool *mtxpool_select;
1428fe387abSDmitrij Tejblum 
143d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
144df8bae1dSRodney W. Grimes struct read_args {
145df8bae1dSRodney W. Grimes 	int	fd;
146134e06feSBruce Evans 	void	*buf;
147134e06feSBruce Evans 	size_t	nbyte;
148df8bae1dSRodney W. Grimes };
149d2d3e875SBruce Evans #endif
15026f9a767SRodney W. Grimes int
1518451d0ddSKip Macy sys_read(td, uap)
152b40ce416SJulian Elischer 	struct thread *td;
153b064d43dSMatthew Dillon 	struct read_args *uap;
154df8bae1dSRodney W. Grimes {
155bcd9e0ddSJohn Baldwin 	struct uio auio;
156bcd9e0ddSJohn Baldwin 	struct iovec aiov;
157279d7226SMatthew Dillon 	int error;
158df8bae1dSRodney W. Grimes 
159526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
160bcd9e0ddSJohn Baldwin 		return (EINVAL);
161bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
162bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
163bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
164bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
165bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
166bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
167bcd9e0ddSJohn Baldwin 	error = kern_readv(td, uap->fd, &auio);
168279d7226SMatthew Dillon 	return(error);
169df8bae1dSRodney W. Grimes }
170df8bae1dSRodney W. Grimes 
171df8bae1dSRodney W. Grimes /*
172bcd9e0ddSJohn Baldwin  * Positioned read system call
1734160ccd9SAlan Cox  */
1744160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
1754160ccd9SAlan Cox struct pread_args {
1764160ccd9SAlan Cox 	int	fd;
1774160ccd9SAlan Cox 	void	*buf;
1784160ccd9SAlan Cox 	size_t	nbyte;
1798fe387abSDmitrij Tejblum 	int	pad;
1804160ccd9SAlan Cox 	off_t	offset;
1814160ccd9SAlan Cox };
1824160ccd9SAlan Cox #endif
1834160ccd9SAlan Cox int
1848451d0ddSKip Macy sys_pread(td, uap)
185b40ce416SJulian Elischer 	struct thread *td;
186b064d43dSMatthew Dillon 	struct pread_args *uap;
1874160ccd9SAlan Cox {
1884160ccd9SAlan Cox 	struct uio auio;
1894160ccd9SAlan Cox 	struct iovec aiov;
190bcd9e0ddSJohn Baldwin 	int error;
1914160ccd9SAlan Cox 
192526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
193bcd9e0ddSJohn Baldwin 		return (EINVAL);
194bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
195bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
1964160ccd9SAlan Cox 	auio.uio_iov = &aiov;
1974160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
198bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
1994160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
200bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, &auio, uap->offset);
2014160ccd9SAlan Cox 	return(error);
2024160ccd9SAlan Cox }
2034160ccd9SAlan Cox 
204c2815ad5SPeter Wemm int
205c2815ad5SPeter Wemm freebsd6_pread(td, uap)
206c2815ad5SPeter Wemm 	struct thread *td;
207c2815ad5SPeter Wemm 	struct freebsd6_pread_args *uap;
208c2815ad5SPeter Wemm {
209c2815ad5SPeter Wemm 	struct pread_args oargs;
210c2815ad5SPeter Wemm 
211c2815ad5SPeter Wemm 	oargs.fd = uap->fd;
212c2815ad5SPeter Wemm 	oargs.buf = uap->buf;
213c2815ad5SPeter Wemm 	oargs.nbyte = uap->nbyte;
214c2815ad5SPeter Wemm 	oargs.offset = uap->offset;
2158451d0ddSKip Macy 	return (sys_pread(td, &oargs));
216c2815ad5SPeter Wemm }
217c2815ad5SPeter Wemm 
2184160ccd9SAlan Cox /*
219df8bae1dSRodney W. Grimes  * Scatter read system call.
220df8bae1dSRodney W. Grimes  */
221d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
222df8bae1dSRodney W. Grimes struct readv_args {
2237147b19dSBruce Evans 	int	fd;
224df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
225df8bae1dSRodney W. Grimes 	u_int	iovcnt;
226df8bae1dSRodney W. Grimes };
227d2d3e875SBruce Evans #endif
22826f9a767SRodney W. Grimes int
2298451d0ddSKip Macy sys_readv(struct thread *td, struct readv_args *uap)
230df8bae1dSRodney W. Grimes {
231b88ec951SJohn Baldwin 	struct uio *auio;
232b88ec951SJohn Baldwin 	int error;
233b88ec951SJohn Baldwin 
234b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
235b88ec951SJohn Baldwin 	if (error)
236b88ec951SJohn Baldwin 		return (error);
237b88ec951SJohn Baldwin 	error = kern_readv(td, uap->fd, auio);
238b88ec951SJohn Baldwin 	free(auio, M_IOV);
239b88ec951SJohn Baldwin 	return (error);
240b88ec951SJohn Baldwin }
241b88ec951SJohn Baldwin 
242b88ec951SJohn Baldwin int
243b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio)
244b88ec951SJohn Baldwin {
245b064d43dSMatthew Dillon 	struct file *fp;
246bcd9e0ddSJohn Baldwin 	int error;
247bcd9e0ddSJohn Baldwin 
248*2609222aSPawel Jakub Dawidek 	error = fget_read(td, fd, CAP_READ, &fp);
249bcd9e0ddSJohn Baldwin 	if (error)
250bcd9e0ddSJohn Baldwin 		return (error);
251bcd9e0ddSJohn Baldwin 	error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
252bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
253bcd9e0ddSJohn Baldwin 	return (error);
254bcd9e0ddSJohn Baldwin }
255bcd9e0ddSJohn Baldwin 
256bcd9e0ddSJohn Baldwin /*
257bcd9e0ddSJohn Baldwin  * Scatter positioned read system call.
258bcd9e0ddSJohn Baldwin  */
259bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
260bcd9e0ddSJohn Baldwin struct preadv_args {
261bcd9e0ddSJohn Baldwin 	int	fd;
262bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
263bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
264bcd9e0ddSJohn Baldwin 	off_t	offset;
265bcd9e0ddSJohn Baldwin };
266bcd9e0ddSJohn Baldwin #endif
267bcd9e0ddSJohn Baldwin int
2688451d0ddSKip Macy sys_preadv(struct thread *td, struct preadv_args *uap)
269bcd9e0ddSJohn Baldwin {
270bcd9e0ddSJohn Baldwin 	struct uio *auio;
271bcd9e0ddSJohn Baldwin 	int error;
272bcd9e0ddSJohn Baldwin 
273bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
274bcd9e0ddSJohn Baldwin 	if (error)
275bcd9e0ddSJohn Baldwin 		return (error);
276bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, auio, uap->offset);
277bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
278bcd9e0ddSJohn Baldwin 	return (error);
279bcd9e0ddSJohn Baldwin }
280bcd9e0ddSJohn Baldwin 
281bcd9e0ddSJohn Baldwin int
282bcd9e0ddSJohn Baldwin kern_preadv(td, fd, auio, offset)
283bcd9e0ddSJohn Baldwin 	struct thread *td;
284bcd9e0ddSJohn Baldwin 	int fd;
285bcd9e0ddSJohn Baldwin 	struct uio *auio;
286bcd9e0ddSJohn Baldwin 	off_t offset;
287bcd9e0ddSJohn Baldwin {
288bcd9e0ddSJohn Baldwin 	struct file *fp;
289bcd9e0ddSJohn Baldwin 	int error;
290bcd9e0ddSJohn Baldwin 
291*2609222aSPawel Jakub Dawidek 	error = fget_read(td, fd, CAP_PREAD, &fp);
292bcd9e0ddSJohn Baldwin 	if (error)
293bcd9e0ddSJohn Baldwin 		return (error);
294bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
295bcd9e0ddSJohn Baldwin 		error = ESPIPE;
296bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
297bcd9e0ddSJohn Baldwin 		error = EINVAL;
298bcd9e0ddSJohn Baldwin 	else
299bcd9e0ddSJohn Baldwin 		error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
300bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
301bcd9e0ddSJohn Baldwin 	return (error);
302bcd9e0ddSJohn Baldwin }
303bcd9e0ddSJohn Baldwin 
304bcd9e0ddSJohn Baldwin /*
305bcd9e0ddSJohn Baldwin  * Common code for readv and preadv that reads data in
306bcd9e0ddSJohn Baldwin  * from a file using the passed in uio, offset, and flags.
307bcd9e0ddSJohn Baldwin  */
308bcd9e0ddSJohn Baldwin static int
309bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags)
310bcd9e0ddSJohn Baldwin 	struct thread *td;
311bcd9e0ddSJohn Baldwin 	int fd;
312bcd9e0ddSJohn Baldwin 	struct file *fp;
313bcd9e0ddSJohn Baldwin 	struct uio *auio;
314bcd9e0ddSJohn Baldwin 	off_t offset;
315bcd9e0ddSJohn Baldwin 	int flags;
316bcd9e0ddSJohn Baldwin {
317bcd9e0ddSJohn Baldwin 	ssize_t cnt;
31882641acdSAlan Cox 	int error;
319df8bae1dSRodney W. Grimes #ifdef KTRACE
320552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
321df8bae1dSRodney W. Grimes #endif
322df8bae1dSRodney W. Grimes 
3234f8d23d6SPoul-Henning Kamp 	/* Finish zero length reads right here */
3244f8d23d6SPoul-Henning Kamp 	if (auio->uio_resid == 0) {
3254f8d23d6SPoul-Henning Kamp 		td->td_retval[0] = 0;
3264f8d23d6SPoul-Henning Kamp 		return(0);
3274f8d23d6SPoul-Henning Kamp 	}
328552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_READ;
329bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
330552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
331df8bae1dSRodney W. Grimes #ifdef KTRACE
332552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
333552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
334df8bae1dSRodney W. Grimes #endif
335552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
336bcd9e0ddSJohn Baldwin 	if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
337552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
338df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
339df8bae1dSRodney W. Grimes 			error = 0;
340279d7226SMatthew Dillon 	}
341552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
342df8bae1dSRodney W. Grimes #ifdef KTRACE
343552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
344552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
345b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_READ, ktruio, error);
346df8bae1dSRodney W. Grimes 	}
347df8bae1dSRodney W. Grimes #endif
348b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
349df8bae1dSRodney W. Grimes 	return (error);
350df8bae1dSRodney W. Grimes }
351df8bae1dSRodney W. Grimes 
352d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
353df8bae1dSRodney W. Grimes struct write_args {
354df8bae1dSRodney W. Grimes 	int	fd;
355134e06feSBruce Evans 	const void *buf;
356134e06feSBruce Evans 	size_t	nbyte;
357df8bae1dSRodney W. Grimes };
358d2d3e875SBruce Evans #endif
35926f9a767SRodney W. Grimes int
3608451d0ddSKip Macy sys_write(td, uap)
361b40ce416SJulian Elischer 	struct thread *td;
362b064d43dSMatthew Dillon 	struct write_args *uap;
363df8bae1dSRodney W. Grimes {
364bcd9e0ddSJohn Baldwin 	struct uio auio;
365bcd9e0ddSJohn Baldwin 	struct iovec aiov;
366279d7226SMatthew Dillon 	int error;
367df8bae1dSRodney W. Grimes 
368526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
369bcd9e0ddSJohn Baldwin 		return (EINVAL);
370bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
371bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
372bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
373bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
374bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
375bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
376bcd9e0ddSJohn Baldwin 	error = kern_writev(td, uap->fd, &auio);
377279d7226SMatthew Dillon 	return(error);
378df8bae1dSRodney W. Grimes }
379df8bae1dSRodney W. Grimes 
380df8bae1dSRodney W. Grimes /*
3810c14ff0eSRobert Watson  * Positioned write system call.
3824160ccd9SAlan Cox  */
3834160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
3844160ccd9SAlan Cox struct pwrite_args {
3854160ccd9SAlan Cox 	int	fd;
3864160ccd9SAlan Cox 	const void *buf;
3874160ccd9SAlan Cox 	size_t	nbyte;
3888fe387abSDmitrij Tejblum 	int	pad;
3894160ccd9SAlan Cox 	off_t	offset;
3904160ccd9SAlan Cox };
3914160ccd9SAlan Cox #endif
3924160ccd9SAlan Cox int
3938451d0ddSKip Macy sys_pwrite(td, uap)
394b40ce416SJulian Elischer 	struct thread *td;
395b064d43dSMatthew Dillon 	struct pwrite_args *uap;
3964160ccd9SAlan Cox {
3974160ccd9SAlan Cox 	struct uio auio;
3984160ccd9SAlan Cox 	struct iovec aiov;
399bcd9e0ddSJohn Baldwin 	int error;
4004160ccd9SAlan Cox 
401526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
402bcd9e0ddSJohn Baldwin 		return (EINVAL);
403bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
404bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
4054160ccd9SAlan Cox 	auio.uio_iov = &aiov;
4064160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
407bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
4084160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
409bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, &auio, uap->offset);
4104160ccd9SAlan Cox 	return(error);
4114160ccd9SAlan Cox }
4124160ccd9SAlan Cox 
413c2815ad5SPeter Wemm int
414c2815ad5SPeter Wemm freebsd6_pwrite(td, uap)
415c2815ad5SPeter Wemm 	struct thread *td;
416c2815ad5SPeter Wemm 	struct freebsd6_pwrite_args *uap;
417c2815ad5SPeter Wemm {
418c2815ad5SPeter Wemm 	struct pwrite_args oargs;
419c2815ad5SPeter Wemm 
420c2815ad5SPeter Wemm 	oargs.fd = uap->fd;
421c2815ad5SPeter Wemm 	oargs.buf = uap->buf;
422c2815ad5SPeter Wemm 	oargs.nbyte = uap->nbyte;
423c2815ad5SPeter Wemm 	oargs.offset = uap->offset;
4248451d0ddSKip Macy 	return (sys_pwrite(td, &oargs));
425c2815ad5SPeter Wemm }
426c2815ad5SPeter Wemm 
4274160ccd9SAlan Cox /*
4280c14ff0eSRobert Watson  * Gather write system call.
429df8bae1dSRodney W. Grimes  */
430d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
431df8bae1dSRodney W. Grimes struct writev_args {
432df8bae1dSRodney W. Grimes 	int	fd;
433df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
434df8bae1dSRodney W. Grimes 	u_int	iovcnt;
435df8bae1dSRodney W. Grimes };
436d2d3e875SBruce Evans #endif
43726f9a767SRodney W. Grimes int
4388451d0ddSKip Macy sys_writev(struct thread *td, struct writev_args *uap)
439df8bae1dSRodney W. Grimes {
440b88ec951SJohn Baldwin 	struct uio *auio;
441b88ec951SJohn Baldwin 	int error;
442b88ec951SJohn Baldwin 
443b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
444b88ec951SJohn Baldwin 	if (error)
445b88ec951SJohn Baldwin 		return (error);
446b88ec951SJohn Baldwin 	error = kern_writev(td, uap->fd, auio);
447b88ec951SJohn Baldwin 	free(auio, M_IOV);
448b88ec951SJohn Baldwin 	return (error);
449b88ec951SJohn Baldwin }
450b88ec951SJohn Baldwin 
451b88ec951SJohn Baldwin int
452b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio)
453b88ec951SJohn Baldwin {
454b064d43dSMatthew Dillon 	struct file *fp;
455bcd9e0ddSJohn Baldwin 	int error;
456bcd9e0ddSJohn Baldwin 
457*2609222aSPawel Jakub Dawidek 	error = fget_write(td, fd, CAP_WRITE, &fp);
458bcd9e0ddSJohn Baldwin 	if (error)
459af56abaaSJohn Baldwin 		return (error);
460bcd9e0ddSJohn Baldwin 	error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
461bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
462bcd9e0ddSJohn Baldwin 	return (error);
463bcd9e0ddSJohn Baldwin }
464bcd9e0ddSJohn Baldwin 
465bcd9e0ddSJohn Baldwin /*
4660c14ff0eSRobert Watson  * Gather positioned write system call.
467bcd9e0ddSJohn Baldwin  */
468bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
469bcd9e0ddSJohn Baldwin struct pwritev_args {
470bcd9e0ddSJohn Baldwin 	int	fd;
471bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
472bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
473bcd9e0ddSJohn Baldwin 	off_t	offset;
474bcd9e0ddSJohn Baldwin };
475bcd9e0ddSJohn Baldwin #endif
476bcd9e0ddSJohn Baldwin int
4778451d0ddSKip Macy sys_pwritev(struct thread *td, struct pwritev_args *uap)
478bcd9e0ddSJohn Baldwin {
479bcd9e0ddSJohn Baldwin 	struct uio *auio;
480bcd9e0ddSJohn Baldwin 	int error;
481bcd9e0ddSJohn Baldwin 
482bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
483bcd9e0ddSJohn Baldwin 	if (error)
484bcd9e0ddSJohn Baldwin 		return (error);
485bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, auio, uap->offset);
486bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
487bcd9e0ddSJohn Baldwin 	return (error);
488bcd9e0ddSJohn Baldwin }
489bcd9e0ddSJohn Baldwin 
490bcd9e0ddSJohn Baldwin int
491bcd9e0ddSJohn Baldwin kern_pwritev(td, fd, auio, offset)
492bcd9e0ddSJohn Baldwin 	struct thread *td;
493bcd9e0ddSJohn Baldwin 	struct uio *auio;
494bcd9e0ddSJohn Baldwin 	int fd;
495bcd9e0ddSJohn Baldwin 	off_t offset;
496bcd9e0ddSJohn Baldwin {
497bcd9e0ddSJohn Baldwin 	struct file *fp;
498bcd9e0ddSJohn Baldwin 	int error;
499bcd9e0ddSJohn Baldwin 
500*2609222aSPawel Jakub Dawidek 	error = fget_write(td, fd, CAP_PWRITE, &fp);
501bcd9e0ddSJohn Baldwin 	if (error)
502af56abaaSJohn Baldwin 		return (error);
503bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
504bcd9e0ddSJohn Baldwin 		error = ESPIPE;
505bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
506bcd9e0ddSJohn Baldwin 		error = EINVAL;
507bcd9e0ddSJohn Baldwin 	else
508bcd9e0ddSJohn Baldwin 		error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
509bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
510bcd9e0ddSJohn Baldwin 	return (error);
511bcd9e0ddSJohn Baldwin }
512bcd9e0ddSJohn Baldwin 
513bcd9e0ddSJohn Baldwin /*
514bcd9e0ddSJohn Baldwin  * Common code for writev and pwritev that writes data to
515bcd9e0ddSJohn Baldwin  * a file using the passed in uio, offset, and flags.
516bcd9e0ddSJohn Baldwin  */
517bcd9e0ddSJohn Baldwin static int
518bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags)
519bcd9e0ddSJohn Baldwin 	struct thread *td;
520bcd9e0ddSJohn Baldwin 	int fd;
521bcd9e0ddSJohn Baldwin 	struct file *fp;
522bcd9e0ddSJohn Baldwin 	struct uio *auio;
523bcd9e0ddSJohn Baldwin 	off_t offset;
524bcd9e0ddSJohn Baldwin 	int flags;
525bcd9e0ddSJohn Baldwin {
526bcd9e0ddSJohn Baldwin 	ssize_t cnt;
527552afd9cSPoul-Henning Kamp 	int error;
528df8bae1dSRodney W. Grimes #ifdef KTRACE
529552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
530df8bae1dSRodney W. Grimes #endif
531df8bae1dSRodney W. Grimes 
532552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_WRITE;
533552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
534bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
535df8bae1dSRodney W. Grimes #ifdef KTRACE
536552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
537552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
538df8bae1dSRodney W. Grimes #endif
539552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
540ad9789f6SKonstantin Belousov 	if (fp->f_type == DTYPE_VNODE &&
541ad9789f6SKonstantin Belousov 	    (fp->f_vnread_flags & FDEVFS_VNODE) == 0)
5429440653dSMatthew Dillon 		bwillwrite();
543bcd9e0ddSJohn Baldwin 	if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
544552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
545df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
546df8bae1dSRodney W. Grimes 			error = 0;
547bcd9e0ddSJohn Baldwin 		/* Socket layer is responsible for issuing SIGPIPE. */
5486f7ca813SBruce M Simpson 		if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
549b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
5507a6f3d78SJohn Baldwin 			tdsignal(td, SIGPIPE);
551b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
55219eb87d2SJohn Baldwin 		}
553df8bae1dSRodney W. Grimes 	}
554552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
555df8bae1dSRodney W. Grimes #ifdef KTRACE
556552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
557552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
558b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_WRITE, ktruio, error);
559df8bae1dSRodney W. Grimes 	}
560df8bae1dSRodney W. Grimes #endif
561b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
562df8bae1dSRodney W. Grimes 	return (error);
563df8bae1dSRodney W. Grimes }
564df8bae1dSRodney W. Grimes 
565e4650294SJohn Baldwin /*
566e4650294SJohn Baldwin  * Truncate a file given a file descriptor.
567e4650294SJohn Baldwin  *
568e4650294SJohn Baldwin  * Can't use fget_write() here, since must return EINVAL and not EBADF if the
569e4650294SJohn Baldwin  * descriptor isn't writable.
570e4650294SJohn Baldwin  */
571e4650294SJohn Baldwin int
572e4650294SJohn Baldwin kern_ftruncate(td, fd, length)
573e4650294SJohn Baldwin 	struct thread *td;
574e4650294SJohn Baldwin 	int fd;
575e4650294SJohn Baldwin 	off_t length;
576e4650294SJohn Baldwin {
577e4650294SJohn Baldwin 	struct file *fp;
578e4650294SJohn Baldwin 	int error;
579e4650294SJohn Baldwin 
58014961ba7SRobert Watson 	AUDIT_ARG_FD(fd);
581e4650294SJohn Baldwin 	if (length < 0)
582e4650294SJohn Baldwin 		return (EINVAL);
583a9d2f8d8SRobert Watson 	error = fget(td, fd, CAP_FTRUNCATE, &fp);
584e4650294SJohn Baldwin 	if (error)
585e4650294SJohn Baldwin 		return (error);
58614961ba7SRobert Watson 	AUDIT_ARG_FILE(td->td_proc, fp);
587e4650294SJohn Baldwin 	if (!(fp->f_flag & FWRITE)) {
588e4650294SJohn Baldwin 		fdrop(fp, td);
589e4650294SJohn Baldwin 		return (EINVAL);
590e4650294SJohn Baldwin 	}
591e4650294SJohn Baldwin 	error = fo_truncate(fp, length, td->td_ucred, td);
592e4650294SJohn Baldwin 	fdrop(fp, td);
593e4650294SJohn Baldwin 	return (error);
594e4650294SJohn Baldwin }
595e4650294SJohn Baldwin 
596e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
597e4650294SJohn Baldwin struct ftruncate_args {
598e4650294SJohn Baldwin 	int	fd;
599e4650294SJohn Baldwin 	int	pad;
600e4650294SJohn Baldwin 	off_t	length;
601e4650294SJohn Baldwin };
602e4650294SJohn Baldwin #endif
603e4650294SJohn Baldwin int
6048451d0ddSKip Macy sys_ftruncate(td, uap)
605e4650294SJohn Baldwin 	struct thread *td;
606e4650294SJohn Baldwin 	struct ftruncate_args *uap;
607e4650294SJohn Baldwin {
608e4650294SJohn Baldwin 
609e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
610e4650294SJohn Baldwin }
611e4650294SJohn Baldwin 
612e4650294SJohn Baldwin #if defined(COMPAT_43)
613e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
614e4650294SJohn Baldwin struct oftruncate_args {
615e4650294SJohn Baldwin 	int	fd;
616e4650294SJohn Baldwin 	long	length;
617e4650294SJohn Baldwin };
618e4650294SJohn Baldwin #endif
619e4650294SJohn Baldwin int
620e4650294SJohn Baldwin oftruncate(td, uap)
621e4650294SJohn Baldwin 	struct thread *td;
622e4650294SJohn Baldwin 	struct oftruncate_args *uap;
623e4650294SJohn Baldwin {
624e4650294SJohn Baldwin 
625e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
626e4650294SJohn Baldwin }
627e4650294SJohn Baldwin #endif /* COMPAT_43 */
628e4650294SJohn Baldwin 
629d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
630df8bae1dSRodney W. Grimes struct ioctl_args {
631df8bae1dSRodney W. Grimes 	int	fd;
632069e9bc1SDoug Rabson 	u_long	com;
633df8bae1dSRodney W. Grimes 	caddr_t	data;
634df8bae1dSRodney W. Grimes };
635d2d3e875SBruce Evans #endif
636df8bae1dSRodney W. Grimes /* ARGSUSED */
63726f9a767SRodney W. Grimes int
6388451d0ddSKip Macy sys_ioctl(struct thread *td, struct ioctl_args *uap)
639df8bae1dSRodney W. Grimes {
6403e15c66fSPoul-Henning Kamp 	u_long com;
6419fddcc66SRuslan Ermilov 	int arg, error;
6423e15c66fSPoul-Henning Kamp 	u_int size;
6439fddcc66SRuslan Ermilov 	caddr_t data;
644df8bae1dSRodney W. Grimes 
6459fc6aa06SPoul-Henning Kamp 	if (uap->com > 0xffffffff) {
6469fc6aa06SPoul-Henning Kamp 		printf(
6479fc6aa06SPoul-Henning Kamp 		    "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
648431f8906SJulian Elischer 		    td->td_proc->p_pid, td->td_name, uap->com);
6499fc6aa06SPoul-Henning Kamp 		uap->com &= 0xffffffff;
6509fc6aa06SPoul-Henning Kamp 	}
651d9f46233SJohn Baldwin 	com = uap->com;
652df8bae1dSRodney W. Grimes 
653df8bae1dSRodney W. Grimes 	/*
654df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
655df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
656df8bae1dSRodney W. Grimes 	 */
657df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
658ca51b19bSPoul-Henning Kamp 	if ((size > IOCPARM_MAX) ||
659ca51b19bSPoul-Henning Kamp 	    ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
6602de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
6612de92a38SPeter Wemm 	    ((com & IOC_OUT) && size == 0) ||
6622de92a38SPeter Wemm #else
6632de92a38SPeter Wemm 	    ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
6642de92a38SPeter Wemm #endif
6659fddcc66SRuslan Ermilov 	    ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
666426da3bcSAlfred Perlstein 		return (ENOTTY);
667279d7226SMatthew Dillon 
668ca51b19bSPoul-Henning Kamp 	if (size > 0) {
669715457f6SDavid E. O'Brien 		if (com & IOC_VOID) {
6709fddcc66SRuslan Ermilov 			/* Integer argument. */
6719fddcc66SRuslan Ermilov 			arg = (intptr_t)uap->data;
6729fddcc66SRuslan Ermilov 			data = (void *)&arg;
6739fddcc66SRuslan Ermilov 			size = 0;
674715457f6SDavid E. O'Brien 		} else
675715457f6SDavid E. O'Brien 			data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
6769fddcc66SRuslan Ermilov 	} else
6779fddcc66SRuslan Ermilov 		data = (void *)&uap->data;
678df8bae1dSRodney W. Grimes 	if (com & IOC_IN) {
679df8bae1dSRodney W. Grimes 		error = copyin(uap->data, data, (u_int)size);
680df8bae1dSRodney W. Grimes 		if (error) {
681a1b0a180SRuslan Ermilov 			if (size > 0)
6829fddcc66SRuslan Ermilov 				free(data, M_IOCTLOPS);
6833e15c66fSPoul-Henning Kamp 			return (error);
684df8bae1dSRodney W. Grimes 		}
685ca51b19bSPoul-Henning Kamp 	} else if (com & IOC_OUT) {
686df8bae1dSRodney W. Grimes 		/*
687df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
688df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
689df8bae1dSRodney W. Grimes 		 */
690df8bae1dSRodney W. Grimes 		bzero(data, size);
691279d7226SMatthew Dillon 	}
692df8bae1dSRodney W. Grimes 
693d9f46233SJohn Baldwin 	error = kern_ioctl(td, uap->fd, com, data);
694d9f46233SJohn Baldwin 
695d9f46233SJohn Baldwin 	if (error == 0 && (com & IOC_OUT))
696d9f46233SJohn Baldwin 		error = copyout(data, uap->data, (u_int)size);
697d9f46233SJohn Baldwin 
6989fddcc66SRuslan Ermilov 	if (size > 0)
6999fddcc66SRuslan Ermilov 		free(data, M_IOCTLOPS);
700d9f46233SJohn Baldwin 	return (error);
701d9f46233SJohn Baldwin }
702d9f46233SJohn Baldwin 
703d9f46233SJohn Baldwin int
704d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
705d9f46233SJohn Baldwin {
706d9f46233SJohn Baldwin 	struct file *fp;
707d9f46233SJohn Baldwin 	struct filedesc *fdp;
708*2609222aSPawel Jakub Dawidek 	int error, tmp, locked;
709d9f46233SJohn Baldwin 
71064c9a4d9SRobert Watson 	AUDIT_ARG_FD(fd);
71164c9a4d9SRobert Watson 	AUDIT_ARG_CMD(com);
712*2609222aSPawel Jakub Dawidek 
713d9f46233SJohn Baldwin 	fdp = td->td_proc->p_fd;
714*2609222aSPawel Jakub Dawidek 
715d9f46233SJohn Baldwin 	switch (com) {
716d9f46233SJohn Baldwin 	case FIONCLEX:
717d9f46233SJohn Baldwin 	case FIOCLEX:
7185e3f7694SRobert Watson 		FILEDESC_XLOCK(fdp);
719*2609222aSPawel Jakub Dawidek 		locked = LA_XLOCKED;
720*2609222aSPawel Jakub Dawidek 		break;
721*2609222aSPawel Jakub Dawidek 	default:
722*2609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
723*2609222aSPawel Jakub Dawidek 		FILEDESC_SLOCK(fdp);
724*2609222aSPawel Jakub Dawidek 		locked = LA_SLOCKED;
725*2609222aSPawel Jakub Dawidek #else
726*2609222aSPawel Jakub Dawidek 		locked = LA_UNLOCKED;
727*2609222aSPawel Jakub Dawidek #endif
728*2609222aSPawel Jakub Dawidek 		break;
729*2609222aSPawel Jakub Dawidek 	}
730*2609222aSPawel Jakub Dawidek 
731*2609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
732*2609222aSPawel Jakub Dawidek 	if ((fp = fget_locked(fdp, fd)) == NULL) {
733*2609222aSPawel Jakub Dawidek 		error = EBADF;
734*2609222aSPawel Jakub Dawidek 		goto out;
735*2609222aSPawel Jakub Dawidek 	}
736*2609222aSPawel Jakub Dawidek 	if ((error = cap_ioctl_check(fdp, fd, com)) != 0) {
737*2609222aSPawel Jakub Dawidek 		fp = NULL;	/* fhold() was not called yet */
738*2609222aSPawel Jakub Dawidek 		goto out;
739*2609222aSPawel Jakub Dawidek 	}
740*2609222aSPawel Jakub Dawidek 	fhold(fp);
741*2609222aSPawel Jakub Dawidek 	if (locked == LA_SLOCKED) {
742*2609222aSPawel Jakub Dawidek 		FILEDESC_SUNLOCK(fdp);
743*2609222aSPawel Jakub Dawidek 		locked = LA_UNLOCKED;
744*2609222aSPawel Jakub Dawidek 	}
745*2609222aSPawel Jakub Dawidek #else
746*2609222aSPawel Jakub Dawidek 	if ((error = fget(td, fd, CAP_IOCTL, &fp)) != 0) {
747*2609222aSPawel Jakub Dawidek 		fp = NULL;
748*2609222aSPawel Jakub Dawidek 		goto out;
749*2609222aSPawel Jakub Dawidek 	}
750*2609222aSPawel Jakub Dawidek #endif
751*2609222aSPawel Jakub Dawidek 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
752*2609222aSPawel Jakub Dawidek 		error = EBADF;
753*2609222aSPawel Jakub Dawidek 		goto out;
754*2609222aSPawel Jakub Dawidek 	}
755*2609222aSPawel Jakub Dawidek 
756*2609222aSPawel Jakub Dawidek 	switch (com) {
757*2609222aSPawel Jakub Dawidek 	case FIONCLEX:
758*2609222aSPawel Jakub Dawidek 		fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE;
759*2609222aSPawel Jakub Dawidek 		goto out;
760*2609222aSPawel Jakub Dawidek 	case FIOCLEX:
761*2609222aSPawel Jakub Dawidek 		fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE;
762d9f46233SJohn Baldwin 		goto out;
763d9f46233SJohn Baldwin 	case FIONBIO:
764bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
765397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FNONBLOCK);
766df8bae1dSRodney W. Grimes 		else
767397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FNONBLOCK);
7688ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
769d9f46233SJohn Baldwin 		break;
770d9f46233SJohn Baldwin 	case FIOASYNC:
771bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
772397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FASYNC);
773df8bae1dSRodney W. Grimes 		else
774397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FASYNC);
7758ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
776d9f46233SJohn Baldwin 		break;
777df8bae1dSRodney W. Grimes 	}
7788ccf264fSPoul-Henning Kamp 
7798ccf264fSPoul-Henning Kamp 	error = fo_ioctl(fp, com, data, td->td_ucred, td);
780d9f46233SJohn Baldwin out:
781*2609222aSPawel Jakub Dawidek 	switch (locked) {
782*2609222aSPawel Jakub Dawidek 	case LA_XLOCKED:
783*2609222aSPawel Jakub Dawidek 		FILEDESC_XUNLOCK(fdp);
784*2609222aSPawel Jakub Dawidek 		break;
785*2609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
786*2609222aSPawel Jakub Dawidek 	case LA_SLOCKED:
787*2609222aSPawel Jakub Dawidek 		FILEDESC_SUNLOCK(fdp);
788*2609222aSPawel Jakub Dawidek 		break;
789*2609222aSPawel Jakub Dawidek #endif
790*2609222aSPawel Jakub Dawidek 	default:
791*2609222aSPawel Jakub Dawidek 		FILEDESC_UNLOCK_ASSERT(fdp);
792*2609222aSPawel Jakub Dawidek 		break;
793*2609222aSPawel Jakub Dawidek 	}
794*2609222aSPawel Jakub Dawidek 	if (fp != NULL)
795b40ce416SJulian Elischer 		fdrop(fp, td);
796df8bae1dSRodney W. Grimes 	return (error);
797df8bae1dSRodney W. Grimes }
798df8bae1dSRodney W. Grimes 
799125dcf8cSKonstantin Belousov int
800125dcf8cSKonstantin Belousov poll_no_poll(int events)
801125dcf8cSKonstantin Belousov {
802125dcf8cSKonstantin Belousov 	/*
803125dcf8cSKonstantin Belousov 	 * Return true for read/write.  If the user asked for something
804125dcf8cSKonstantin Belousov 	 * special, return POLLNVAL, so that clients have a way of
805125dcf8cSKonstantin Belousov 	 * determining reliably whether or not the extended
806125dcf8cSKonstantin Belousov 	 * functionality is present without hard-coding knowledge
807125dcf8cSKonstantin Belousov 	 * of specific filesystem implementations.
808125dcf8cSKonstantin Belousov 	 */
809125dcf8cSKonstantin Belousov 	if (events & ~POLLSTANDARD)
810125dcf8cSKonstantin Belousov 		return (POLLNVAL);
811125dcf8cSKonstantin Belousov 
812125dcf8cSKonstantin Belousov 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
813125dcf8cSKonstantin Belousov }
814125dcf8cSKonstantin Belousov 
815066d836bSKonstantin Belousov int
8168451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap)
817066d836bSKonstantin Belousov {
818066d836bSKonstantin Belousov 	struct timespec ts;
819066d836bSKonstantin Belousov 	struct timeval tv, *tvp;
820066d836bSKonstantin Belousov 	sigset_t set, *uset;
821066d836bSKonstantin Belousov 	int error;
822066d836bSKonstantin Belousov 
823066d836bSKonstantin Belousov 	if (uap->ts != NULL) {
824066d836bSKonstantin Belousov 		error = copyin(uap->ts, &ts, sizeof(ts));
825066d836bSKonstantin Belousov 		if (error != 0)
826066d836bSKonstantin Belousov 		    return (error);
827066d836bSKonstantin Belousov 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
828066d836bSKonstantin Belousov 		tvp = &tv;
829066d836bSKonstantin Belousov 	} else
830066d836bSKonstantin Belousov 		tvp = NULL;
831066d836bSKonstantin Belousov 	if (uap->sm != NULL) {
832066d836bSKonstantin Belousov 		error = copyin(uap->sm, &set, sizeof(set));
833066d836bSKonstantin Belousov 		if (error != 0)
834066d836bSKonstantin Belousov 			return (error);
835066d836bSKonstantin Belousov 		uset = &set;
836066d836bSKonstantin Belousov 	} else
837066d836bSKonstantin Belousov 		uset = NULL;
838066d836bSKonstantin Belousov 	return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
839066d836bSKonstantin Belousov 	    uset, NFDBITS));
840066d836bSKonstantin Belousov }
841066d836bSKonstantin Belousov 
842066d836bSKonstantin Belousov int
843066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
844066d836bSKonstantin Belousov     struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
845066d836bSKonstantin Belousov {
846066d836bSKonstantin Belousov 	int error;
847066d836bSKonstantin Belousov 
848066d836bSKonstantin Belousov 	if (uset != NULL) {
849066d836bSKonstantin Belousov 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
850066d836bSKonstantin Belousov 		    &td->td_oldsigmask, 0);
851066d836bSKonstantin Belousov 		if (error != 0)
852066d836bSKonstantin Belousov 			return (error);
853066d836bSKonstantin Belousov 		td->td_pflags |= TDP_OLDMASK;
854066d836bSKonstantin Belousov 		/*
855066d836bSKonstantin Belousov 		 * Make sure that ast() is called on return to
856066d836bSKonstantin Belousov 		 * usermode and TDP_OLDMASK is cleared, restoring old
857066d836bSKonstantin Belousov 		 * sigmask.
858066d836bSKonstantin Belousov 		 */
859066d836bSKonstantin Belousov 		thread_lock(td);
860066d836bSKonstantin Belousov 		td->td_flags |= TDF_ASTPENDING;
861066d836bSKonstantin Belousov 		thread_unlock(td);
862066d836bSKonstantin Belousov 	}
863066d836bSKonstantin Belousov 	error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
864066d836bSKonstantin Belousov 	return (error);
865066d836bSKonstantin Belousov }
866066d836bSKonstantin Belousov 
867d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
868df8bae1dSRodney W. Grimes struct select_args {
869b08f7993SSujal Patel 	int	nd;
870df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
871df8bae1dSRodney W. Grimes 	struct	timeval *tv;
872df8bae1dSRodney W. Grimes };
873d2d3e875SBruce Evans #endif
87426f9a767SRodney W. Grimes int
8758451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap)
876df8bae1dSRodney W. Grimes {
8778f19eb88SIan Dowse 	struct timeval tv, *tvp;
8788f19eb88SIan Dowse 	int error;
8798f19eb88SIan Dowse 
8808f19eb88SIan Dowse 	if (uap->tv != NULL) {
8818f19eb88SIan Dowse 		error = copyin(uap->tv, &tv, sizeof(tv));
8828f19eb88SIan Dowse 		if (error)
8838f19eb88SIan Dowse 			return (error);
8848f19eb88SIan Dowse 		tvp = &tv;
8858f19eb88SIan Dowse 	} else
8868f19eb88SIan Dowse 		tvp = NULL;
8878f19eb88SIan Dowse 
888b55ef216SKonstantin Belousov 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
889b55ef216SKonstantin Belousov 	    NFDBITS));
8908f19eb88SIan Dowse }
8918f19eb88SIan Dowse 
89256be1b9aSKonstantin Belousov /*
89356be1b9aSKonstantin Belousov  * In the unlikely case when user specified n greater then the last
89456be1b9aSKonstantin Belousov  * open file descriptor, check that no bits are set after the last
89556be1b9aSKonstantin Belousov  * valid fd.  We must return EBADF if any is set.
89656be1b9aSKonstantin Belousov  *
89756be1b9aSKonstantin Belousov  * There are applications that rely on the behaviour.
89856be1b9aSKonstantin Belousov  *
89956be1b9aSKonstantin Belousov  * nd is fd_lastfile + 1.
90056be1b9aSKonstantin Belousov  */
90156be1b9aSKonstantin Belousov static int
90256be1b9aSKonstantin Belousov select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits)
90356be1b9aSKonstantin Belousov {
90456be1b9aSKonstantin Belousov 	char *addr, *oaddr;
90556be1b9aSKonstantin Belousov 	int b, i, res;
90656be1b9aSKonstantin Belousov 	uint8_t bits;
90756be1b9aSKonstantin Belousov 
90856be1b9aSKonstantin Belousov 	if (nd >= ndu || fd_in == NULL)
90956be1b9aSKonstantin Belousov 		return (0);
91056be1b9aSKonstantin Belousov 
91156be1b9aSKonstantin Belousov 	oaddr = NULL;
91256be1b9aSKonstantin Belousov 	bits = 0; /* silence gcc */
91356be1b9aSKonstantin Belousov 	for (i = nd; i < ndu; i++) {
91456be1b9aSKonstantin Belousov 		b = i / NBBY;
91556be1b9aSKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN
91656be1b9aSKonstantin Belousov 		addr = (char *)fd_in + b;
91756be1b9aSKonstantin Belousov #else
91856be1b9aSKonstantin Belousov 		addr = (char *)fd_in;
91956be1b9aSKonstantin Belousov 		if (abi_nfdbits == NFDBITS) {
92056be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(fd_mask)) +
92156be1b9aSKonstantin Belousov 			    sizeof(fd_mask) - 1 - b % sizeof(fd_mask);
92256be1b9aSKonstantin Belousov 		} else {
92356be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(uint32_t)) +
92456be1b9aSKonstantin Belousov 			    sizeof(uint32_t) - 1 - b % sizeof(uint32_t);
92556be1b9aSKonstantin Belousov 		}
92656be1b9aSKonstantin Belousov #endif
92756be1b9aSKonstantin Belousov 		if (addr != oaddr) {
92856be1b9aSKonstantin Belousov 			res = fubyte(addr);
92956be1b9aSKonstantin Belousov 			if (res == -1)
93056be1b9aSKonstantin Belousov 				return (EFAULT);
93156be1b9aSKonstantin Belousov 			oaddr = addr;
93256be1b9aSKonstantin Belousov 			bits = res;
93356be1b9aSKonstantin Belousov 		}
93456be1b9aSKonstantin Belousov 		if ((bits & (1 << (i % NBBY))) != 0)
93556be1b9aSKonstantin Belousov 			return (EBADF);
93656be1b9aSKonstantin Belousov 	}
93756be1b9aSKonstantin Belousov 	return (0);
93856be1b9aSKonstantin Belousov }
93956be1b9aSKonstantin Belousov 
9408f19eb88SIan Dowse int
9418f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
942b55ef216SKonstantin Belousov     fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
9438f19eb88SIan Dowse {
944426da3bcSAlfred Perlstein 	struct filedesc *fdp;
945d5e4d7e1SBruce Evans 	/*
946d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
947d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
948d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
949d5e4d7e1SBruce Evans 	 * of 256.
950d5e4d7e1SBruce Evans 	 */
951d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
952eb209311SAlfred Perlstein 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
95300af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
95456be1b9aSKonstantin Belousov 	int error, lf, ndu, timo;
955b55ef216SKonstantin Belousov 	u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
956df8bae1dSRodney W. Grimes 
9578f19eb88SIan Dowse 	if (nd < 0)
958acbfbfeaSSujal Patel 		return (EINVAL);
959426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
96056be1b9aSKonstantin Belousov 	ndu = nd;
96156be1b9aSKonstantin Belousov 	lf = fdp->fd_lastfile;
96256be1b9aSKonstantin Belousov 	if (nd > lf + 1)
96356be1b9aSKonstantin Belousov 		nd = lf + 1;
96456be1b9aSKonstantin Belousov 
96556be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits);
96656be1b9aSKonstantin Belousov 	if (error != 0)
96756be1b9aSKonstantin Belousov 		return (error);
96856be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits);
96956be1b9aSKonstantin Belousov 	if (error != 0)
97056be1b9aSKonstantin Belousov 		return (error);
97156be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits);
97256be1b9aSKonstantin Belousov 	if (error != 0)
97356be1b9aSKonstantin Belousov 		return (error);
974b08f7993SSujal Patel 
975d5e4d7e1SBruce Evans 	/*
976d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
977d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
978d5e4d7e1SBruce Evans 	 */
9798f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
980d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
981b55ef216SKonstantin Belousov 	ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
982d5e4d7e1SBruce Evans 	nbufbytes = 0;
9838f19eb88SIan Dowse 	if (fd_in != NULL)
984d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
9858f19eb88SIan Dowse 	if (fd_ou != NULL)
986d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
9878f19eb88SIan Dowse 	if (fd_ex != NULL)
988d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
989d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
990d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
991d5e4d7e1SBruce Evans 	else
992a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
993b08f7993SSujal Patel 
994b08f7993SSujal Patel 	/*
995d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
996d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
997d5e4d7e1SBruce Evans 	 * together.
998b08f7993SSujal Patel 	 */
999d5e4d7e1SBruce Evans 	sbp = selbits;
1000df8bae1dSRodney W. Grimes #define	getbits(name, x) \
1001d5e4d7e1SBruce Evans 	do {								\
1002841c0c7eSNathan Whitehorn 		if (name == NULL) {					\
1003d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
1004841c0c7eSNathan Whitehorn 			obits[x] = NULL;				\
1005841c0c7eSNathan Whitehorn 		} else {						\
1006d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
1007d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
1008d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
1009b55ef216SKonstantin Belousov 			error = copyin(name, ibits[x], ncpubytes);	\
1010265fc98fSSeigo Tanimura 			if (error != 0)					\
1011ace8398dSJeff Roberson 				goto done;				\
1012b55ef216SKonstantin Belousov 			bzero((char *)ibits[x] + ncpubytes,		\
1013b55ef216SKonstantin Belousov 			    ncpbytes - ncpubytes);			\
1014e04ac2feSJohn Baldwin 		}							\
1015d5e4d7e1SBruce Evans 	} while (0)
10168f19eb88SIan Dowse 	getbits(fd_in, 0);
10178f19eb88SIan Dowse 	getbits(fd_ou, 1);
10188f19eb88SIan Dowse 	getbits(fd_ex, 2);
1019df8bae1dSRodney W. Grimes #undef	getbits
1020841c0c7eSNathan Whitehorn 
1021841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
1022841c0c7eSNathan Whitehorn 	/*
1023841c0c7eSNathan Whitehorn 	 * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
1024841c0c7eSNathan Whitehorn 	 * we are running under 32-bit emulation. This should be more
1025841c0c7eSNathan Whitehorn 	 * generic.
1026841c0c7eSNathan Whitehorn 	 */
1027841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)						\
1028841c0c7eSNathan Whitehorn 	if (abi_nfdbits != NFDBITS && bits != NULL) {			\
1029841c0c7eSNathan Whitehorn 		int i;							\
1030841c0c7eSNathan Whitehorn 		for (i = 0; i < ncpbytes / sizeof *sbp; i++)		\
1031841c0c7eSNathan Whitehorn 			bits[i] = (bits[i] >> 32) | (bits[i] << 32);	\
1032841c0c7eSNathan Whitehorn 	}
1033841c0c7eSNathan Whitehorn #else
1034841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)
1035841c0c7eSNathan Whitehorn #endif
1036841c0c7eSNathan Whitehorn 
1037841c0c7eSNathan Whitehorn 	/* Make sure the bit order makes it through an ABI transition */
1038841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[0]);
1039841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[1]);
1040841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[2]);
1041841c0c7eSNathan Whitehorn 
1042d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
1043d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
1044df8bae1dSRodney W. Grimes 
10458f19eb88SIan Dowse 	if (tvp != NULL) {
10468f19eb88SIan Dowse 		atv = *tvp;
1047df8bae1dSRodney W. Grimes 		if (itimerfix(&atv)) {
1048df8bae1dSRodney W. Grimes 			error = EINVAL;
1049ace8398dSJeff Roberson 			goto done;
1050df8bae1dSRodney W. Grimes 		}
1051c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
105200af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
10539c386f6bSJohn Baldwin 	} else {
105400af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
10559c386f6bSJohn Baldwin 		atv.tv_usec = 0;
10569c386f6bSJohn Baldwin 	}
105700af9731SPoul-Henning Kamp 	timo = 0;
1058ace8398dSJeff Roberson 	seltdinit(td);
1059ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1060ace8398dSJeff Roberson 	for (;;) {
10618f19eb88SIan Dowse 		error = selscan(td, ibits, obits, nd);
1062ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1063ace8398dSJeff Roberson 			break;
10644da144c0SJohn Baldwin 		if (atv.tv_sec || atv.tv_usec) {
1065c21410e1SPoul-Henning Kamp 			getmicrouptime(&rtv);
106685f190e4SAlfred Perlstein 			if (timevalcmp(&rtv, &atv, >=))
1067ace8398dSJeff Roberson 				break;
106800af9731SPoul-Henning Kamp 			ttv = atv;
106900af9731SPoul-Henning Kamp 			timevalsub(&ttv, &rtv);
107000af9731SPoul-Henning Kamp 			timo = ttv.tv_sec > 24 * 60 * 60 ?
107100af9731SPoul-Henning Kamp 			    24 * 60 * 60 * hz : tvtohz(&ttv);
1072df8bae1dSRodney W. Grimes 		}
1073ace8398dSJeff Roberson 		error = seltdwait(td, timo);
1074ace8398dSJeff Roberson 		if (error)
1075ace8398dSJeff Roberson 			break;
1076ace8398dSJeff Roberson 		error = selrescan(td, ibits, obits);
1077ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1078ace8398dSJeff Roberson 			break;
107985f190e4SAlfred Perlstein 	}
1080ace8398dSJeff Roberson 	seltdclear(td);
1081265fc98fSSeigo Tanimura 
1082df8bae1dSRodney W. Grimes done:
1083df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
1084df8bae1dSRodney W. Grimes 	if (error == ERESTART)
1085df8bae1dSRodney W. Grimes 		error = EINTR;
1086df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
1087df8bae1dSRodney W. Grimes 		error = 0;
1088841c0c7eSNathan Whitehorn 
1089841c0c7eSNathan Whitehorn 	/* swizzle bit order back, if necessary */
1090841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[0]);
1091841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[1]);
1092841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[2]);
1093841c0c7eSNathan Whitehorn #undef swizzle_fdset
1094841c0c7eSNathan Whitehorn 
1095df8bae1dSRodney W. Grimes #define	putbits(name, x) \
1096b55ef216SKonstantin Belousov 	if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
1097df8bae1dSRodney W. Grimes 		error = error2;
1098df8bae1dSRodney W. Grimes 	if (error == 0) {
1099df8bae1dSRodney W. Grimes 		int error2;
1100df8bae1dSRodney W. Grimes 
11018f19eb88SIan Dowse 		putbits(fd_in, 0);
11028f19eb88SIan Dowse 		putbits(fd_ou, 1);
11038f19eb88SIan Dowse 		putbits(fd_ex, 2);
1104df8bae1dSRodney W. Grimes #undef putbits
1105df8bae1dSRodney W. Grimes 	}
1106d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
1107d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
1108ad2edad9SMatthew Dillon 
1109df8bae1dSRodney W. Grimes 	return (error);
1110df8bae1dSRodney W. Grimes }
111111b763dfSJeff Roberson /*
111211b763dfSJeff Roberson  * Convert a select bit set to poll flags.
1113748b9df6SJeff Roberson  *
111411b763dfSJeff Roberson  * The backend always returns POLLHUP/POLLERR if appropriate and we
111511b763dfSJeff Roberson  * return this as a set bit in any set.
111611b763dfSJeff Roberson  */
111711b763dfSJeff Roberson static int select_flags[3] = {
111811b763dfSJeff Roberson     POLLRDNORM | POLLHUP | POLLERR,
111911b763dfSJeff Roberson     POLLWRNORM | POLLHUP | POLLERR,
112061e53a38SKonstantin Belousov     POLLRDBAND | POLLERR
112111b763dfSJeff Roberson };
112211b763dfSJeff Roberson 
112311b763dfSJeff Roberson /*
112411b763dfSJeff Roberson  * Compute the fo_poll flags required for a fd given by the index and
112511b763dfSJeff Roberson  * bit position in the fd_mask array.
112611b763dfSJeff Roberson  */
112711b763dfSJeff Roberson static __inline int
112860b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit)
112911b763dfSJeff Roberson {
113011b763dfSJeff Roberson 	int flags;
113111b763dfSJeff Roberson 	int msk;
113211b763dfSJeff Roberson 
113311b763dfSJeff Roberson 	flags = 0;
113411b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
113511b763dfSJeff Roberson 		if (ibits[msk] == NULL)
113611b763dfSJeff Roberson 			continue;
113760b7f468SStephane E. Potvin 		if ((ibits[msk][idx] & bit) == 0)
113811b763dfSJeff Roberson 			continue;
113911b763dfSJeff Roberson 		flags |= select_flags[msk];
114011b763dfSJeff Roberson 	}
114111b763dfSJeff Roberson 	return (flags);
114211b763dfSJeff Roberson }
114311b763dfSJeff Roberson 
114411b763dfSJeff Roberson /*
114511b763dfSJeff Roberson  * Set the appropriate output bits given a mask of fired events and the
114611b763dfSJeff Roberson  * input bits originally requested.
114711b763dfSJeff Roberson  */
114811b763dfSJeff Roberson static __inline int
114911b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
115011b763dfSJeff Roberson {
115111b763dfSJeff Roberson 	int msk;
115211b763dfSJeff Roberson 	int n;
115311b763dfSJeff Roberson 
115411b763dfSJeff Roberson 	n = 0;
115511b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
115611b763dfSJeff Roberson 		if ((events & select_flags[msk]) == 0)
115711b763dfSJeff Roberson 			continue;
115811b763dfSJeff Roberson 		if (ibits[msk] == NULL)
115911b763dfSJeff Roberson 			continue;
116011b763dfSJeff Roberson 		if ((ibits[msk][idx] & bit) == 0)
116111b763dfSJeff Roberson 			continue;
116211b763dfSJeff Roberson 		/*
116311b763dfSJeff Roberson 		 * XXX Check for a duplicate set.  This can occur because a
116411b763dfSJeff Roberson 		 * socket calls selrecord() twice for each poll() call
116511b763dfSJeff Roberson 		 * resulting in two selfds per real fd.  selrescan() will
116611b763dfSJeff Roberson 		 * call selsetbits twice as a result.
116711b763dfSJeff Roberson 		 */
116811b763dfSJeff Roberson 		if ((obits[msk][idx] & bit) != 0)
116911b763dfSJeff Roberson 			continue;
117011b763dfSJeff Roberson 		obits[msk][idx] |= bit;
117111b763dfSJeff Roberson 		n++;
117211b763dfSJeff Roberson 	}
117311b763dfSJeff Roberson 
117411b763dfSJeff Roberson 	return (n);
117511b763dfSJeff Roberson }
1176df8bae1dSRodney W. Grimes 
1177a9d2f8d8SRobert Watson static __inline int
1178a9d2f8d8SRobert Watson getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp)
1179a9d2f8d8SRobert Watson {
1180a9d2f8d8SRobert Watson 
1181*2609222aSPawel Jakub Dawidek 	return (fget_unlocked(fdp, fd, CAP_POLL_EVENT, 0, fpp, NULL));
1182a9d2f8d8SRobert Watson }
1183a9d2f8d8SRobert Watson 
1184ace8398dSJeff Roberson /*
1185ace8398dSJeff Roberson  * Traverse the list of fds attached to this thread's seltd and check for
1186ace8398dSJeff Roberson  * completion.
1187ace8398dSJeff Roberson  */
1188ace8398dSJeff Roberson static int
1189ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
1190ace8398dSJeff Roberson {
119111b763dfSJeff Roberson 	struct filedesc *fdp;
119211b763dfSJeff Roberson 	struct selinfo *si;
1193ace8398dSJeff Roberson 	struct seltd *stp;
1194ace8398dSJeff Roberson 	struct selfd *sfp;
1195ace8398dSJeff Roberson 	struct selfd *sfn;
1196ace8398dSJeff Roberson 	struct file *fp;
11979cdacff1SJeff Roberson 	fd_mask bit;
11989cdacff1SJeff Roberson 	int fd, ev, n, idx;
1199a9d2f8d8SRobert Watson 	int error;
1200ace8398dSJeff Roberson 
120111b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
1202ace8398dSJeff Roberson 	stp = td->td_sel;
120311b763dfSJeff Roberson 	n = 0;
1204ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1205ace8398dSJeff Roberson 		fd = (int)(uintptr_t)sfp->sf_cookie;
1206ace8398dSJeff Roberson 		si = sfp->sf_si;
1207ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1208ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1209ace8398dSJeff Roberson 		if (si != NULL)
1210ace8398dSJeff Roberson 			continue;
1211a9d2f8d8SRobert Watson 		error = getselfd_cap(fdp, fd, &fp);
1212a9d2f8d8SRobert Watson 		if (error)
1213a9d2f8d8SRobert Watson 			return (error);
121411b763dfSJeff Roberson 		idx = fd / NFDBITS;
12159cdacff1SJeff Roberson 		bit = (fd_mask)1 << (fd % NFDBITS);
121611b763dfSJeff Roberson 		ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
1217bf422e5fSJeff Roberson 		fdrop(fp, td);
121811b763dfSJeff Roberson 		if (ev != 0)
121911b763dfSJeff Roberson 			n += selsetbits(ibits, obits, idx, bit, ev);
1220ace8398dSJeff Roberson 	}
1221ace8398dSJeff Roberson 	stp->st_flags = 0;
1222ace8398dSJeff Roberson 	td->td_retval[0] = n;
1223ace8398dSJeff Roberson 	return (0);
1224ace8398dSJeff Roberson }
1225ace8398dSJeff Roberson 
1226ace8398dSJeff Roberson /*
1227ace8398dSJeff Roberson  * Perform the initial filedescriptor scan and register ourselves with
1228ace8398dSJeff Roberson  * each selinfo.
1229ace8398dSJeff Roberson  */
1230265fc98fSSeigo Tanimura static int
1231b40ce416SJulian Elischer selscan(td, ibits, obits, nfd)
1232b40ce416SJulian Elischer 	struct thread *td;
1233b08f7993SSujal Patel 	fd_mask **ibits, **obits;
1234cb226aaaSPoul-Henning Kamp 	int nfd;
1235df8bae1dSRodney W. Grimes {
123611b763dfSJeff Roberson 	struct filedesc *fdp;
1237df8bae1dSRodney W. Grimes 	struct file *fp;
12389cdacff1SJeff Roberson 	fd_mask bit;
123911b763dfSJeff Roberson 	int ev, flags, end, fd;
12409cdacff1SJeff Roberson 	int n, idx;
1241a9d2f8d8SRobert Watson 	int error;
1242df8bae1dSRodney W. Grimes 
124311b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
124411b763dfSJeff Roberson 	n = 0;
12459cdacff1SJeff Roberson 	for (idx = 0, fd = 0; fd < nfd; idx++) {
124611b763dfSJeff Roberson 		end = imin(fd + NFDBITS, nfd);
124711b763dfSJeff Roberson 		for (bit = 1; fd < end; bit <<= 1, fd++) {
124811b763dfSJeff Roberson 			/* Compute the list of events we're interested in. */
124911b763dfSJeff Roberson 			flags = selflags(ibits, idx, bit);
125011b763dfSJeff Roberson 			if (flags == 0)
1251f082218cSPeter Wemm 				continue;
1252a9d2f8d8SRobert Watson 			error = getselfd_cap(fdp, fd, &fp);
1253a9d2f8d8SRobert Watson 			if (error)
1254a9d2f8d8SRobert Watson 				return (error);
1255ace8398dSJeff Roberson 			selfdalloc(td, (void *)(uintptr_t)fd);
125611b763dfSJeff Roberson 			ev = fo_poll(fp, flags, td->td_ucred, td);
1257bf422e5fSJeff Roberson 			fdrop(fp, td);
125811b763dfSJeff Roberson 			if (ev != 0)
125911b763dfSJeff Roberson 				n += selsetbits(ibits, obits, idx, bit, ev);
1260df8bae1dSRodney W. Grimes 		}
1261df8bae1dSRodney W. Grimes 	}
126211b763dfSJeff Roberson 
1263b40ce416SJulian Elischer 	td->td_retval[0] = n;
1264df8bae1dSRodney W. Grimes 	return (0);
1265df8bae1dSRodney W. Grimes }
1266df8bae1dSRodney W. Grimes 
126742d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
126842d11757SPeter Wemm struct poll_args {
126942d11757SPeter Wemm 	struct pollfd *fds;
127042d11757SPeter Wemm 	u_int	nfds;
127142d11757SPeter Wemm 	int	timeout;
127242d11757SPeter Wemm };
127342d11757SPeter Wemm #endif
127442d11757SPeter Wemm int
12758451d0ddSKip Macy sys_poll(td, uap)
1276b40ce416SJulian Elischer 	struct thread *td;
1277ea0237edSJonathan Lemon 	struct poll_args *uap;
127842d11757SPeter Wemm {
12792580f4e5SAndre Oppermann 	struct pollfd *bits;
12802580f4e5SAndre Oppermann 	struct pollfd smallbits[32];
128100af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
128268cefd64SDavide Italiano 	int error, timo;
1283ace8398dSJeff Roberson 	u_int nfds;
128442d11757SPeter Wemm 	size_t ni;
128542d11757SPeter Wemm 
1286d1e405c5SAlfred Perlstein 	nfds = uap->nfds;
1287374ae2a3SJeff Roberson 	if (nfds > maxfilesperproc && nfds > FD_SETSIZE)
1288ace8398dSJeff Roberson 		return (EINVAL);
128989b71647SPeter Wemm 	ni = nfds * sizeof(struct pollfd);
129042d11757SPeter Wemm 	if (ni > sizeof(smallbits))
1291a163d034SWarner Losh 		bits = malloc(ni, M_TEMP, M_WAITOK);
129242d11757SPeter Wemm 	else
129342d11757SPeter Wemm 		bits = smallbits;
1294d1e405c5SAlfred Perlstein 	error = copyin(uap->fds, bits, ni);
129542d11757SPeter Wemm 	if (error)
1296ace8398dSJeff Roberson 		goto done;
1297d1e405c5SAlfred Perlstein 	if (uap->timeout != INFTIM) {
1298d1e405c5SAlfred Perlstein 		atv.tv_sec = uap->timeout / 1000;
1299d1e405c5SAlfred Perlstein 		atv.tv_usec = (uap->timeout % 1000) * 1000;
130042d11757SPeter Wemm 		if (itimerfix(&atv)) {
130142d11757SPeter Wemm 			error = EINVAL;
1302ace8398dSJeff Roberson 			goto done;
130342d11757SPeter Wemm 		}
1304c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
130500af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
13069c386f6bSJohn Baldwin 	} else {
130700af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
13089c386f6bSJohn Baldwin 		atv.tv_usec = 0;
13099c386f6bSJohn Baldwin 	}
131000af9731SPoul-Henning Kamp 	timo = 0;
1311ace8398dSJeff Roberson 	seltdinit(td);
1312ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1313ace8398dSJeff Roberson 	for (;;) {
13142580f4e5SAndre Oppermann 		error = pollscan(td, bits, nfds);
1315ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1316ace8398dSJeff Roberson 			break;
13174da144c0SJohn Baldwin 		if (atv.tv_sec || atv.tv_usec) {
1318c21410e1SPoul-Henning Kamp 			getmicrouptime(&rtv);
131985f190e4SAlfred Perlstein 			if (timevalcmp(&rtv, &atv, >=))
1320ace8398dSJeff Roberson 				break;
132100af9731SPoul-Henning Kamp 			ttv = atv;
132200af9731SPoul-Henning Kamp 			timevalsub(&ttv, &rtv);
132300af9731SPoul-Henning Kamp 			timo = ttv.tv_sec > 24 * 60 * 60 ?
132400af9731SPoul-Henning Kamp 			    24 * 60 * 60 * hz : tvtohz(&ttv);
132542d11757SPeter Wemm 		}
1326ace8398dSJeff Roberson 		error = seltdwait(td, timo);
1327ace8398dSJeff Roberson 		if (error)
1328ace8398dSJeff Roberson 			break;
1329ace8398dSJeff Roberson 		error = pollrescan(td);
1330ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1331ace8398dSJeff Roberson 			break;
133285f190e4SAlfred Perlstein 	}
1333ace8398dSJeff Roberson 	seltdclear(td);
1334265fc98fSSeigo Tanimura 
133542d11757SPeter Wemm done:
133642d11757SPeter Wemm 	/* poll is not restarted after signals... */
133742d11757SPeter Wemm 	if (error == ERESTART)
133842d11757SPeter Wemm 		error = EINTR;
133942d11757SPeter Wemm 	if (error == EWOULDBLOCK)
134042d11757SPeter Wemm 		error = 0;
134142d11757SPeter Wemm 	if (error == 0) {
13426d8feddaSKonstantin Belousov 		error = pollout(td, bits, uap->fds, nfds);
134342d11757SPeter Wemm 		if (error)
134442d11757SPeter Wemm 			goto out;
134542d11757SPeter Wemm 	}
134642d11757SPeter Wemm out:
134742d11757SPeter Wemm 	if (ni > sizeof(smallbits))
134842d11757SPeter Wemm 		free(bits, M_TEMP);
134942d11757SPeter Wemm 	return (error);
135042d11757SPeter Wemm }
135142d11757SPeter Wemm 
135242d11757SPeter Wemm static int
1353ace8398dSJeff Roberson pollrescan(struct thread *td)
1354ace8398dSJeff Roberson {
1355ace8398dSJeff Roberson 	struct seltd *stp;
1356ace8398dSJeff Roberson 	struct selfd *sfp;
1357ace8398dSJeff Roberson 	struct selfd *sfn;
1358ace8398dSJeff Roberson 	struct selinfo *si;
1359ace8398dSJeff Roberson 	struct filedesc *fdp;
1360ace8398dSJeff Roberson 	struct file *fp;
1361ace8398dSJeff Roberson 	struct pollfd *fd;
1362ace8398dSJeff Roberson 	int n;
1363ace8398dSJeff Roberson 
1364ace8398dSJeff Roberson 	n = 0;
1365ace8398dSJeff Roberson 	fdp = td->td_proc->p_fd;
1366ace8398dSJeff Roberson 	stp = td->td_sel;
1367ace8398dSJeff Roberson 	FILEDESC_SLOCK(fdp);
1368ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1369ace8398dSJeff Roberson 		fd = (struct pollfd *)sfp->sf_cookie;
1370ace8398dSJeff Roberson 		si = sfp->sf_si;
1371ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1372ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1373ace8398dSJeff Roberson 		if (si != NULL)
1374ace8398dSJeff Roberson 			continue;
1375*2609222aSPawel Jakub Dawidek 		fp = fdp->fd_ofiles[fd->fd].fde_file;
1376d6f72489SJonathan Anderson #ifdef CAPABILITIES
1377*2609222aSPawel Jakub Dawidek 		if (fp == NULL ||
1378*2609222aSPawel Jakub Dawidek 		    cap_check(cap_rights(fdp, fd->fd), CAP_POLL_EVENT) != 0)
1379d6f72489SJonathan Anderson #else
1380*2609222aSPawel Jakub Dawidek 		if (fp == NULL)
1381d6f72489SJonathan Anderson #endif
1382*2609222aSPawel Jakub Dawidek 		{
1383ace8398dSJeff Roberson 			fd->revents = POLLNVAL;
1384ace8398dSJeff Roberson 			n++;
1385ace8398dSJeff Roberson 			continue;
1386ace8398dSJeff Roberson 		}
1387d6f72489SJonathan Anderson 
1388ace8398dSJeff Roberson 		/*
1389ace8398dSJeff Roberson 		 * Note: backend also returns POLLHUP and
1390ace8398dSJeff Roberson 		 * POLLERR if appropriate.
1391ace8398dSJeff Roberson 		 */
1392ace8398dSJeff Roberson 		fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1393ace8398dSJeff Roberson 		if (fd->revents != 0)
1394ace8398dSJeff Roberson 			n++;
1395ace8398dSJeff Roberson 	}
1396ace8398dSJeff Roberson 	FILEDESC_SUNLOCK(fdp);
1397ace8398dSJeff Roberson 	stp->st_flags = 0;
1398ace8398dSJeff Roberson 	td->td_retval[0] = n;
1399ace8398dSJeff Roberson 	return (0);
1400ace8398dSJeff Roberson }
1401ace8398dSJeff Roberson 
1402ace8398dSJeff Roberson 
1403ace8398dSJeff Roberson static int
14046d8feddaSKonstantin Belousov pollout(td, fds, ufds, nfd)
14056d8feddaSKonstantin Belousov 	struct thread *td;
1406ae81968fSRobert Watson 	struct pollfd *fds;
1407ae81968fSRobert Watson 	struct pollfd *ufds;
1408ae81968fSRobert Watson 	u_int nfd;
1409ae81968fSRobert Watson {
1410ae81968fSRobert Watson 	int error = 0;
1411ae81968fSRobert Watson 	u_int i = 0;
14126d8feddaSKonstantin Belousov 	u_int n = 0;
1413ae81968fSRobert Watson 
1414ae81968fSRobert Watson 	for (i = 0; i < nfd; i++) {
1415ae81968fSRobert Watson 		error = copyout(&fds->revents, &ufds->revents,
1416ae81968fSRobert Watson 		    sizeof(ufds->revents));
1417ae81968fSRobert Watson 		if (error)
1418ae81968fSRobert Watson 			return (error);
14196d8feddaSKonstantin Belousov 		if (fds->revents != 0)
14206d8feddaSKonstantin Belousov 			n++;
1421ae81968fSRobert Watson 		fds++;
1422ae81968fSRobert Watson 		ufds++;
1423ae81968fSRobert Watson 	}
14246d8feddaSKonstantin Belousov 	td->td_retval[0] = n;
1425ae81968fSRobert Watson 	return (0);
1426ae81968fSRobert Watson }
1427ae81968fSRobert Watson 
1428ae81968fSRobert Watson static int
1429b40ce416SJulian Elischer pollscan(td, fds, nfd)
1430b40ce416SJulian Elischer 	struct thread *td;
143142d11757SPeter Wemm 	struct pollfd *fds;
1432ea0237edSJonathan Lemon 	u_int nfd;
143342d11757SPeter Wemm {
1434ace8398dSJeff Roberson 	struct filedesc *fdp = td->td_proc->p_fd;
143542d11757SPeter Wemm 	struct file *fp;
1436*2609222aSPawel Jakub Dawidek 	int i, n = 0;
143742d11757SPeter Wemm 
14385e3f7694SRobert Watson 	FILEDESC_SLOCK(fdp);
1439eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
1440337c9691SJordan K. Hubbard 		if (fds->fd >= fdp->fd_nfiles) {
144142d11757SPeter Wemm 			fds->revents = POLLNVAL;
144242d11757SPeter Wemm 			n++;
1443337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
1444337c9691SJordan K. Hubbard 			fds->revents = 0;
144542d11757SPeter Wemm 		} else {
1446*2609222aSPawel Jakub Dawidek 			fp = fdp->fd_ofiles[fds->fd].fde_file;
1447d6f72489SJonathan Anderson #ifdef CAPABILITIES
1448*2609222aSPawel Jakub Dawidek 			if (fp == NULL ||
1449*2609222aSPawel Jakub Dawidek 			    cap_check(cap_rights(fdp, fds->fd),
1450*2609222aSPawel Jakub Dawidek 			    CAP_POLL_EVENT) != 0)
1451d6f72489SJonathan Anderson #else
1452*2609222aSPawel Jakub Dawidek 			if (fp == NULL)
1453d6f72489SJonathan Anderson #endif
1454*2609222aSPawel Jakub Dawidek 			{
145542d11757SPeter Wemm 				fds->revents = POLLNVAL;
145642d11757SPeter Wemm 				n++;
145742d11757SPeter Wemm 			} else {
14582087c896SBruce Evans 				/*
14592087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
14602087c896SBruce Evans 				 * POLLERR if appropriate.
14612087c896SBruce Evans 				 */
1462ace8398dSJeff Roberson 				selfdalloc(td, fds);
146313ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
1464ea6027a8SRobert Watson 				    td->td_ucred, td);
1465f2159cc7SKonstantin Belousov 				/*
1466f2159cc7SKonstantin Belousov 				 * POSIX requires POLLOUT to be never
1467f2159cc7SKonstantin Belousov 				 * set simultaneously with POLLHUP.
1468f2159cc7SKonstantin Belousov 				 */
1469f2159cc7SKonstantin Belousov 				if ((fds->revents & POLLHUP) != 0)
1470f2159cc7SKonstantin Belousov 					fds->revents &= ~POLLOUT;
1471f2159cc7SKonstantin Belousov 
147242d11757SPeter Wemm 				if (fds->revents != 0)
147342d11757SPeter Wemm 					n++;
147442d11757SPeter Wemm 			}
147542d11757SPeter Wemm 		}
147642d11757SPeter Wemm 	}
14775e3f7694SRobert Watson 	FILEDESC_SUNLOCK(fdp);
1478b40ce416SJulian Elischer 	td->td_retval[0] = n;
147942d11757SPeter Wemm 	return (0);
148042d11757SPeter Wemm }
148142d11757SPeter Wemm 
148242d11757SPeter Wemm /*
148342d11757SPeter Wemm  * OpenBSD poll system call.
14840c14ff0eSRobert Watson  *
148542d11757SPeter Wemm  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
148642d11757SPeter Wemm  */
148742d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
148842d11757SPeter Wemm struct openbsd_poll_args {
148942d11757SPeter Wemm 	struct pollfd *fds;
149042d11757SPeter Wemm 	u_int	nfds;
149142d11757SPeter Wemm 	int	timeout;
149242d11757SPeter Wemm };
149342d11757SPeter Wemm #endif
149442d11757SPeter Wemm int
14958451d0ddSKip Macy sys_openbsd_poll(td, uap)
1496b40ce416SJulian Elischer 	register struct thread *td;
149742d11757SPeter Wemm 	register struct openbsd_poll_args *uap;
149842d11757SPeter Wemm {
14998451d0ddSKip Macy 	return (sys_poll(td, (struct poll_args *)uap));
150042d11757SPeter Wemm }
150142d11757SPeter Wemm 
150285f190e4SAlfred Perlstein /*
1503ace8398dSJeff Roberson  * Preallocate two selfds associated with 'cookie'.  Some fo_poll routines
1504ace8398dSJeff Roberson  * have two select sets, one for read and another for write.
1505ace8398dSJeff Roberson  */
1506ace8398dSJeff Roberson static void
1507ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie)
1508ace8398dSJeff Roberson {
1509ace8398dSJeff Roberson 	struct seltd *stp;
1510ace8398dSJeff Roberson 
1511ace8398dSJeff Roberson 	stp = td->td_sel;
1512ace8398dSJeff Roberson 	if (stp->st_free1 == NULL)
1513ace8398dSJeff Roberson 		stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1514ace8398dSJeff Roberson 	stp->st_free1->sf_td = stp;
1515ace8398dSJeff Roberson 	stp->st_free1->sf_cookie = cookie;
1516ace8398dSJeff Roberson 	if (stp->st_free2 == NULL)
1517ace8398dSJeff Roberson 		stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1518ace8398dSJeff Roberson 	stp->st_free2->sf_td = stp;
1519ace8398dSJeff Roberson 	stp->st_free2->sf_cookie = cookie;
1520ace8398dSJeff Roberson }
1521ace8398dSJeff Roberson 
1522ace8398dSJeff Roberson static void
1523ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp)
1524ace8398dSJeff Roberson {
1525ace8398dSJeff Roberson 	STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
1526ace8398dSJeff Roberson 	mtx_lock(sfp->sf_mtx);
1527ace8398dSJeff Roberson 	if (sfp->sf_si)
1528ace8398dSJeff Roberson 		TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1529ace8398dSJeff Roberson 	mtx_unlock(sfp->sf_mtx);
1530ace8398dSJeff Roberson 	uma_zfree(selfd_zone, sfp);
153185f190e4SAlfred Perlstein }
153285f190e4SAlfred Perlstein 
15336aba400aSAttilio Rao /* Drain the waiters tied to all the selfd belonging the specified selinfo. */
15346aba400aSAttilio Rao void
15356aba400aSAttilio Rao seldrain(sip)
15366aba400aSAttilio Rao         struct selinfo *sip;
15376aba400aSAttilio Rao {
15386aba400aSAttilio Rao 
15396aba400aSAttilio Rao 	/*
15406aba400aSAttilio Rao 	 * This feature is already provided by doselwakeup(), thus it is
15416aba400aSAttilio Rao 	 * enough to go for it.
15426aba400aSAttilio Rao 	 * Eventually, the context, should take care to avoid races
15436aba400aSAttilio Rao 	 * between thread calling select()/poll() and file descriptor
15446aba400aSAttilio Rao 	 * detaching, but, again, the races are just the same as
15456aba400aSAttilio Rao 	 * selwakeup().
15466aba400aSAttilio Rao 	 */
15476aba400aSAttilio Rao         doselwakeup(sip, -1);
15486aba400aSAttilio Rao }
15496aba400aSAttilio Rao 
1550df8bae1dSRodney W. Grimes /*
1551df8bae1dSRodney W. Grimes  * Record a select request.
1552df8bae1dSRodney W. Grimes  */
1553df8bae1dSRodney W. Grimes void
1554df8bae1dSRodney W. Grimes selrecord(selector, sip)
1555b40ce416SJulian Elischer 	struct thread *selector;
1556df8bae1dSRodney W. Grimes 	struct selinfo *sip;
1557df8bae1dSRodney W. Grimes {
1558ace8398dSJeff Roberson 	struct selfd *sfp;
1559ace8398dSJeff Roberson 	struct seltd *stp;
1560ace8398dSJeff Roberson 	struct mtx *mtxp;
1561df8bae1dSRodney W. Grimes 
1562ace8398dSJeff Roberson 	stp = selector->td_sel;
156385f190e4SAlfred Perlstein 	/*
1564ace8398dSJeff Roberson 	 * Don't record when doing a rescan.
156585f190e4SAlfred Perlstein 	 */
1566ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_RESCAN)
1567ace8398dSJeff Roberson 		return;
1568ace8398dSJeff Roberson 	/*
1569ace8398dSJeff Roberson 	 * Grab one of the preallocated descriptors.
1570ace8398dSJeff Roberson 	 */
1571ace8398dSJeff Roberson 	sfp = NULL;
1572ace8398dSJeff Roberson 	if ((sfp = stp->st_free1) != NULL)
1573ace8398dSJeff Roberson 		stp->st_free1 = NULL;
1574ace8398dSJeff Roberson 	else if ((sfp = stp->st_free2) != NULL)
1575ace8398dSJeff Roberson 		stp->st_free2 = NULL;
1576ace8398dSJeff Roberson 	else
1577ace8398dSJeff Roberson 		panic("selrecord: No free selfd on selq");
15782141453eSJeff Roberson 	mtxp = sip->si_mtx;
15792141453eSJeff Roberson 	if (mtxp == NULL)
15802141453eSJeff Roberson 		mtxp = mtx_pool_find(mtxpool_select, sip);
1581ace8398dSJeff Roberson 	/*
1582ace8398dSJeff Roberson 	 * Initialize the sfp and queue it in the thread.
1583ace8398dSJeff Roberson 	 */
1584ace8398dSJeff Roberson 	sfp->sf_si = sip;
1585ace8398dSJeff Roberson 	sfp->sf_mtx = mtxp;
1586ace8398dSJeff Roberson 	STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1587ace8398dSJeff Roberson 	/*
1588ace8398dSJeff Roberson 	 * Now that we've locked the sip, check for initialization.
1589ace8398dSJeff Roberson 	 */
1590ace8398dSJeff Roberson 	mtx_lock(mtxp);
1591ace8398dSJeff Roberson 	if (sip->si_mtx == NULL) {
1592ace8398dSJeff Roberson 		sip->si_mtx = mtxp;
1593ace8398dSJeff Roberson 		TAILQ_INIT(&sip->si_tdlist);
159485f190e4SAlfred Perlstein 	}
1595ace8398dSJeff Roberson 	/*
1596ace8398dSJeff Roberson 	 * Add this thread to the list of selfds listening on this selinfo.
1597ace8398dSJeff Roberson 	 */
1598ace8398dSJeff Roberson 	TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1599ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1600df8bae1dSRodney W. Grimes }
1601df8bae1dSRodney W. Grimes 
1602512824f8SSeigo Tanimura /* Wake up a selecting thread. */
1603df8bae1dSRodney W. Grimes void
1604df8bae1dSRodney W. Grimes selwakeup(sip)
160585f190e4SAlfred Perlstein 	struct selinfo *sip;
1606df8bae1dSRodney W. Grimes {
1607512824f8SSeigo Tanimura 	doselwakeup(sip, -1);
1608512824f8SSeigo Tanimura }
1609512824f8SSeigo Tanimura 
1610512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */
1611512824f8SSeigo Tanimura void
1612512824f8SSeigo Tanimura selwakeuppri(sip, pri)
1613512824f8SSeigo Tanimura 	struct selinfo *sip;
1614512824f8SSeigo Tanimura 	int pri;
1615512824f8SSeigo Tanimura {
1616512824f8SSeigo Tanimura 	doselwakeup(sip, pri);
1617512824f8SSeigo Tanimura }
1618512824f8SSeigo Tanimura 
1619512824f8SSeigo Tanimura /*
1620512824f8SSeigo Tanimura  * Do a wakeup when a selectable event occurs.
1621512824f8SSeigo Tanimura  */
1622512824f8SSeigo Tanimura static void
1623512824f8SSeigo Tanimura doselwakeup(sip, pri)
1624512824f8SSeigo Tanimura 	struct selinfo *sip;
1625512824f8SSeigo Tanimura 	int pri;
1626512824f8SSeigo Tanimura {
1627ace8398dSJeff Roberson 	struct selfd *sfp;
1628ace8398dSJeff Roberson 	struct selfd *sfn;
1629ace8398dSJeff Roberson 	struct seltd *stp;
1630df8bae1dSRodney W. Grimes 
1631ace8398dSJeff Roberson 	/* If it's not initialized there can't be any waiters. */
1632ace8398dSJeff Roberson 	if (sip->si_mtx == NULL)
1633b40ce416SJulian Elischer 		return;
1634ace8398dSJeff Roberson 	/*
1635ace8398dSJeff Roberson 	 * Locking the selinfo locks all selfds associated with it.
1636ace8398dSJeff Roberson 	 */
1637ace8398dSJeff Roberson 	mtx_lock(sip->si_mtx);
1638ace8398dSJeff Roberson 	TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1639ace8398dSJeff Roberson 		/*
1640ace8398dSJeff Roberson 		 * Once we remove this sfp from the list and clear the
1641ace8398dSJeff Roberson 		 * sf_si seltdclear will know to ignore this si.
1642ace8398dSJeff Roberson 		 */
1643ace8398dSJeff Roberson 		TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1644ace8398dSJeff Roberson 		sfp->sf_si = NULL;
1645ace8398dSJeff Roberson 		stp = sfp->sf_td;
1646ace8398dSJeff Roberson 		mtx_lock(&stp->st_mtx);
1647ace8398dSJeff Roberson 		stp->st_flags |= SELTD_PENDING;
1648ace8398dSJeff Roberson 		cv_broadcastpri(&stp->st_wait, pri);
1649ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1650b40ce416SJulian Elischer 	}
1651ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1652ace8398dSJeff Roberson }
1653ace8398dSJeff Roberson 
1654ace8398dSJeff Roberson static void
1655ace8398dSJeff Roberson seltdinit(struct thread *td)
1656ace8398dSJeff Roberson {
1657ace8398dSJeff Roberson 	struct seltd *stp;
1658ace8398dSJeff Roberson 
1659ace8398dSJeff Roberson 	if ((stp = td->td_sel) != NULL)
1660ace8398dSJeff Roberson 		goto out;
1661ace8398dSJeff Roberson 	td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1662ace8398dSJeff Roberson 	mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1663ace8398dSJeff Roberson 	cv_init(&stp->st_wait, "select");
1664ace8398dSJeff Roberson out:
1665ace8398dSJeff Roberson 	stp->st_flags = 0;
1666ace8398dSJeff Roberson 	STAILQ_INIT(&stp->st_selq);
1667ace8398dSJeff Roberson }
1668ace8398dSJeff Roberson 
1669ace8398dSJeff Roberson static int
1670ace8398dSJeff Roberson seltdwait(struct thread *td, int timo)
1671ace8398dSJeff Roberson {
1672ace8398dSJeff Roberson 	struct seltd *stp;
1673ace8398dSJeff Roberson 	int error;
1674ace8398dSJeff Roberson 
1675ace8398dSJeff Roberson 	stp = td->td_sel;
1676ace8398dSJeff Roberson 	/*
1677ace8398dSJeff Roberson 	 * An event of interest may occur while we do not hold the seltd
1678ace8398dSJeff Roberson 	 * locked so check the pending flag before we sleep.
1679ace8398dSJeff Roberson 	 */
1680ace8398dSJeff Roberson 	mtx_lock(&stp->st_mtx);
1681ace8398dSJeff Roberson 	/*
1682ace8398dSJeff Roberson 	 * Any further calls to selrecord will be a rescan.
1683ace8398dSJeff Roberson 	 */
1684ace8398dSJeff Roberson 	stp->st_flags |= SELTD_RESCAN;
1685ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_PENDING) {
1686ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1687ace8398dSJeff Roberson 		return (0);
1688ace8398dSJeff Roberson 	}
1689ace8398dSJeff Roberson 	if (timo > 0)
1690ace8398dSJeff Roberson 		error = cv_timedwait_sig(&stp->st_wait, &stp->st_mtx, timo);
1691ace8398dSJeff Roberson 	else
1692ace8398dSJeff Roberson 		error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
1693ace8398dSJeff Roberson 	mtx_unlock(&stp->st_mtx);
1694ace8398dSJeff Roberson 
1695ace8398dSJeff Roberson 	return (error);
1696ace8398dSJeff Roberson }
1697ace8398dSJeff Roberson 
1698ace8398dSJeff Roberson void
1699ace8398dSJeff Roberson seltdfini(struct thread *td)
1700ace8398dSJeff Roberson {
1701ace8398dSJeff Roberson 	struct seltd *stp;
1702ace8398dSJeff Roberson 
1703ace8398dSJeff Roberson 	stp = td->td_sel;
1704ace8398dSJeff Roberson 	if (stp == NULL)
1705ace8398dSJeff Roberson 		return;
1706ace8398dSJeff Roberson 	if (stp->st_free1)
1707ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free1);
1708ace8398dSJeff Roberson 	if (stp->st_free2)
1709ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free2);
1710ace8398dSJeff Roberson 	td->td_sel = NULL;
1711ace8398dSJeff Roberson 	free(stp, M_SELECT);
1712ace8398dSJeff Roberson }
1713ace8398dSJeff Roberson 
1714ace8398dSJeff Roberson /*
1715ace8398dSJeff Roberson  * Remove the references to the thread from all of the objects we were
1716ace8398dSJeff Roberson  * polling.
1717ace8398dSJeff Roberson  */
1718ace8398dSJeff Roberson static void
1719ace8398dSJeff Roberson seltdclear(struct thread *td)
1720ace8398dSJeff Roberson {
1721ace8398dSJeff Roberson 	struct seltd *stp;
1722ace8398dSJeff Roberson 	struct selfd *sfp;
1723ace8398dSJeff Roberson 	struct selfd *sfn;
1724ace8398dSJeff Roberson 
1725ace8398dSJeff Roberson 	stp = td->td_sel;
1726ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
1727ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1728ace8398dSJeff Roberson 	stp->st_flags = 0;
1729df8bae1dSRodney W. Grimes }
1730265fc98fSSeigo Tanimura 
17314d77a549SAlfred Perlstein static void selectinit(void *);
1732ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
1733265fc98fSSeigo Tanimura static void
1734ace8398dSJeff Roberson selectinit(void *dummy __unused)
1735265fc98fSSeigo Tanimura {
17362141453eSJeff Roberson 
1737ace8398dSJeff Roberson 	selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL,
1738ace8398dSJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, 0);
17392141453eSJeff Roberson 	mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);
1740265fc98fSSeigo Tanimura }
1741