xref: /freebsd/sys/kern/sys_generic.c (revision cf5e4fe6bb7b91ecf38a5d8e7231d064fa788e33)
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>
522609222aSPawel 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 *);
106*cf5e4fe6SDavide Italiano static int	seltdwait(struct thread *, sbintime_t, sbintime_t);
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 
2482609222aSPawel 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 
2912609222aSPawel 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 
4572609222aSPawel 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 
5002609222aSPawel 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;
7082609222aSPawel Jakub Dawidek 	int error, tmp, locked;
709d9f46233SJohn Baldwin 
71064c9a4d9SRobert Watson 	AUDIT_ARG_FD(fd);
71164c9a4d9SRobert Watson 	AUDIT_ARG_CMD(com);
7122609222aSPawel Jakub Dawidek 
713d9f46233SJohn Baldwin 	fdp = td->td_proc->p_fd;
7142609222aSPawel Jakub Dawidek 
715d9f46233SJohn Baldwin 	switch (com) {
716d9f46233SJohn Baldwin 	case FIONCLEX:
717d9f46233SJohn Baldwin 	case FIOCLEX:
7185e3f7694SRobert Watson 		FILEDESC_XLOCK(fdp);
7192609222aSPawel Jakub Dawidek 		locked = LA_XLOCKED;
7202609222aSPawel Jakub Dawidek 		break;
7212609222aSPawel Jakub Dawidek 	default:
7222609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
7232609222aSPawel Jakub Dawidek 		FILEDESC_SLOCK(fdp);
7242609222aSPawel Jakub Dawidek 		locked = LA_SLOCKED;
7252609222aSPawel Jakub Dawidek #else
7262609222aSPawel Jakub Dawidek 		locked = LA_UNLOCKED;
7272609222aSPawel Jakub Dawidek #endif
7282609222aSPawel Jakub Dawidek 		break;
7292609222aSPawel Jakub Dawidek 	}
7302609222aSPawel Jakub Dawidek 
7312609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
7322609222aSPawel Jakub Dawidek 	if ((fp = fget_locked(fdp, fd)) == NULL) {
7332609222aSPawel Jakub Dawidek 		error = EBADF;
7342609222aSPawel Jakub Dawidek 		goto out;
7352609222aSPawel Jakub Dawidek 	}
7362609222aSPawel Jakub Dawidek 	if ((error = cap_ioctl_check(fdp, fd, com)) != 0) {
7372609222aSPawel Jakub Dawidek 		fp = NULL;	/* fhold() was not called yet */
7382609222aSPawel Jakub Dawidek 		goto out;
7392609222aSPawel Jakub Dawidek 	}
7402609222aSPawel Jakub Dawidek 	fhold(fp);
7412609222aSPawel Jakub Dawidek 	if (locked == LA_SLOCKED) {
7422609222aSPawel Jakub Dawidek 		FILEDESC_SUNLOCK(fdp);
7432609222aSPawel Jakub Dawidek 		locked = LA_UNLOCKED;
7442609222aSPawel Jakub Dawidek 	}
7452609222aSPawel Jakub Dawidek #else
7462609222aSPawel Jakub Dawidek 	if ((error = fget(td, fd, CAP_IOCTL, &fp)) != 0) {
7472609222aSPawel Jakub Dawidek 		fp = NULL;
7482609222aSPawel Jakub Dawidek 		goto out;
7492609222aSPawel Jakub Dawidek 	}
7502609222aSPawel Jakub Dawidek #endif
7512609222aSPawel Jakub Dawidek 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
7522609222aSPawel Jakub Dawidek 		error = EBADF;
7532609222aSPawel Jakub Dawidek 		goto out;
7542609222aSPawel Jakub Dawidek 	}
7552609222aSPawel Jakub Dawidek 
7562609222aSPawel Jakub Dawidek 	switch (com) {
7572609222aSPawel Jakub Dawidek 	case FIONCLEX:
7582609222aSPawel Jakub Dawidek 		fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE;
7592609222aSPawel Jakub Dawidek 		goto out;
7602609222aSPawel Jakub Dawidek 	case FIOCLEX:
7612609222aSPawel 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:
7812609222aSPawel Jakub Dawidek 	switch (locked) {
7822609222aSPawel Jakub Dawidek 	case LA_XLOCKED:
7832609222aSPawel Jakub Dawidek 		FILEDESC_XUNLOCK(fdp);
7842609222aSPawel Jakub Dawidek 		break;
7852609222aSPawel Jakub Dawidek #ifdef CAPABILITIES
7862609222aSPawel Jakub Dawidek 	case LA_SLOCKED:
7872609222aSPawel Jakub Dawidek 		FILEDESC_SUNLOCK(fdp);
7882609222aSPawel Jakub Dawidek 		break;
7892609222aSPawel Jakub Dawidek #endif
7902609222aSPawel Jakub Dawidek 	default:
7912609222aSPawel Jakub Dawidek 		FILEDESC_UNLOCK_ASSERT(fdp);
7922609222aSPawel Jakub Dawidek 		break;
7932609222aSPawel Jakub Dawidek 	}
7942609222aSPawel 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;
953*cf5e4fe6SDavide Italiano 	struct timeval rtv;
954*cf5e4fe6SDavide Italiano 	sbintime_t asbt, precision, rsbt;
955b55ef216SKonstantin Belousov 	u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
956*cf5e4fe6SDavide Italiano 	int error, lf, ndu;
957df8bae1dSRodney W. Grimes 
9588f19eb88SIan Dowse 	if (nd < 0)
959acbfbfeaSSujal Patel 		return (EINVAL);
960426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
96156be1b9aSKonstantin Belousov 	ndu = nd;
96256be1b9aSKonstantin Belousov 	lf = fdp->fd_lastfile;
96356be1b9aSKonstantin Belousov 	if (nd > lf + 1)
96456be1b9aSKonstantin Belousov 		nd = lf + 1;
96556be1b9aSKonstantin Belousov 
96656be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits);
96756be1b9aSKonstantin Belousov 	if (error != 0)
96856be1b9aSKonstantin Belousov 		return (error);
96956be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits);
97056be1b9aSKonstantin Belousov 	if (error != 0)
97156be1b9aSKonstantin Belousov 		return (error);
97256be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits);
97356be1b9aSKonstantin Belousov 	if (error != 0)
97456be1b9aSKonstantin Belousov 		return (error);
975b08f7993SSujal Patel 
976d5e4d7e1SBruce Evans 	/*
977d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
978d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
979d5e4d7e1SBruce Evans 	 */
9808f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
981d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
982b55ef216SKonstantin Belousov 	ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
983d5e4d7e1SBruce Evans 	nbufbytes = 0;
9848f19eb88SIan Dowse 	if (fd_in != NULL)
985d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
9868f19eb88SIan Dowse 	if (fd_ou != NULL)
987d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
9888f19eb88SIan Dowse 	if (fd_ex != NULL)
989d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
990d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
991d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
992d5e4d7e1SBruce Evans 	else
993a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
994b08f7993SSujal Patel 
995b08f7993SSujal Patel 	/*
996d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
997d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
998d5e4d7e1SBruce Evans 	 * together.
999b08f7993SSujal Patel 	 */
1000d5e4d7e1SBruce Evans 	sbp = selbits;
1001df8bae1dSRodney W. Grimes #define	getbits(name, x) \
1002d5e4d7e1SBruce Evans 	do {								\
1003841c0c7eSNathan Whitehorn 		if (name == NULL) {					\
1004d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
1005841c0c7eSNathan Whitehorn 			obits[x] = NULL;				\
1006841c0c7eSNathan Whitehorn 		} else {						\
1007d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
1008d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
1009d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
1010b55ef216SKonstantin Belousov 			error = copyin(name, ibits[x], ncpubytes);	\
1011265fc98fSSeigo Tanimura 			if (error != 0)					\
1012ace8398dSJeff Roberson 				goto done;				\
1013b55ef216SKonstantin Belousov 			bzero((char *)ibits[x] + ncpubytes,		\
1014b55ef216SKonstantin Belousov 			    ncpbytes - ncpubytes);			\
1015e04ac2feSJohn Baldwin 		}							\
1016d5e4d7e1SBruce Evans 	} while (0)
10178f19eb88SIan Dowse 	getbits(fd_in, 0);
10188f19eb88SIan Dowse 	getbits(fd_ou, 1);
10198f19eb88SIan Dowse 	getbits(fd_ex, 2);
1020df8bae1dSRodney W. Grimes #undef	getbits
1021841c0c7eSNathan Whitehorn 
1022841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
1023841c0c7eSNathan Whitehorn 	/*
1024841c0c7eSNathan Whitehorn 	 * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
1025841c0c7eSNathan Whitehorn 	 * we are running under 32-bit emulation. This should be more
1026841c0c7eSNathan Whitehorn 	 * generic.
1027841c0c7eSNathan Whitehorn 	 */
1028841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)						\
1029841c0c7eSNathan Whitehorn 	if (abi_nfdbits != NFDBITS && bits != NULL) {			\
1030841c0c7eSNathan Whitehorn 		int i;							\
1031841c0c7eSNathan Whitehorn 		for (i = 0; i < ncpbytes / sizeof *sbp; i++)		\
1032841c0c7eSNathan Whitehorn 			bits[i] = (bits[i] >> 32) | (bits[i] << 32);	\
1033841c0c7eSNathan Whitehorn 	}
1034841c0c7eSNathan Whitehorn #else
1035841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)
1036841c0c7eSNathan Whitehorn #endif
1037841c0c7eSNathan Whitehorn 
1038841c0c7eSNathan Whitehorn 	/* Make sure the bit order makes it through an ABI transition */
1039841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[0]);
1040841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[1]);
1041841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[2]);
1042841c0c7eSNathan Whitehorn 
1043d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
1044d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
1045df8bae1dSRodney W. Grimes 
1046*cf5e4fe6SDavide Italiano 	precision = 0;
10478f19eb88SIan Dowse 	if (tvp != NULL) {
1048*cf5e4fe6SDavide Italiano 		rtv = *tvp;
1049*cf5e4fe6SDavide Italiano 		if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1050*cf5e4fe6SDavide Italiano 		    rtv.tv_usec >= 1000000) {
1051df8bae1dSRodney W. Grimes 			error = EINVAL;
1052ace8398dSJeff Roberson 			goto done;
1053df8bae1dSRodney W. Grimes 		}
1054*cf5e4fe6SDavide Italiano 		rsbt = tvtosbt(rtv);
1055*cf5e4fe6SDavide Italiano 		precision = rsbt;
1056*cf5e4fe6SDavide Italiano 		precision >>= tc_precexp;
1057*cf5e4fe6SDavide Italiano 		if (TIMESEL(&asbt, rsbt))
1058*cf5e4fe6SDavide Italiano 			asbt += tc_tick_sbt;
1059*cf5e4fe6SDavide Italiano 		asbt += rsbt;
1060*cf5e4fe6SDavide Italiano 	} else
1061*cf5e4fe6SDavide Italiano 		asbt = -1;
1062ace8398dSJeff Roberson 	seltdinit(td);
1063ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1064ace8398dSJeff Roberson 	for (;;) {
10658f19eb88SIan Dowse 		error = selscan(td, ibits, obits, nd);
1066ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1067ace8398dSJeff Roberson 			break;
1068*cf5e4fe6SDavide Italiano 		error = seltdwait(td, asbt, precision);
1069ace8398dSJeff Roberson 		if (error)
1070ace8398dSJeff Roberson 			break;
1071ace8398dSJeff Roberson 		error = selrescan(td, ibits, obits);
1072ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1073ace8398dSJeff Roberson 			break;
107485f190e4SAlfred Perlstein 	}
1075ace8398dSJeff Roberson 	seltdclear(td);
1076265fc98fSSeigo Tanimura 
1077df8bae1dSRodney W. Grimes done:
1078df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
1079df8bae1dSRodney W. Grimes 	if (error == ERESTART)
1080df8bae1dSRodney W. Grimes 		error = EINTR;
1081df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
1082df8bae1dSRodney W. Grimes 		error = 0;
1083841c0c7eSNathan Whitehorn 
1084841c0c7eSNathan Whitehorn 	/* swizzle bit order back, if necessary */
1085841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[0]);
1086841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[1]);
1087841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[2]);
1088841c0c7eSNathan Whitehorn #undef swizzle_fdset
1089841c0c7eSNathan Whitehorn 
1090df8bae1dSRodney W. Grimes #define	putbits(name, x) \
1091b55ef216SKonstantin Belousov 	if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
1092df8bae1dSRodney W. Grimes 		error = error2;
1093df8bae1dSRodney W. Grimes 	if (error == 0) {
1094df8bae1dSRodney W. Grimes 		int error2;
1095df8bae1dSRodney W. Grimes 
10968f19eb88SIan Dowse 		putbits(fd_in, 0);
10978f19eb88SIan Dowse 		putbits(fd_ou, 1);
10988f19eb88SIan Dowse 		putbits(fd_ex, 2);
1099df8bae1dSRodney W. Grimes #undef putbits
1100df8bae1dSRodney W. Grimes 	}
1101d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
1102d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
1103ad2edad9SMatthew Dillon 
1104df8bae1dSRodney W. Grimes 	return (error);
1105df8bae1dSRodney W. Grimes }
110611b763dfSJeff Roberson /*
110711b763dfSJeff Roberson  * Convert a select bit set to poll flags.
1108748b9df6SJeff Roberson  *
110911b763dfSJeff Roberson  * The backend always returns POLLHUP/POLLERR if appropriate and we
111011b763dfSJeff Roberson  * return this as a set bit in any set.
111111b763dfSJeff Roberson  */
111211b763dfSJeff Roberson static int select_flags[3] = {
111311b763dfSJeff Roberson     POLLRDNORM | POLLHUP | POLLERR,
111411b763dfSJeff Roberson     POLLWRNORM | POLLHUP | POLLERR,
111561e53a38SKonstantin Belousov     POLLRDBAND | POLLERR
111611b763dfSJeff Roberson };
111711b763dfSJeff Roberson 
111811b763dfSJeff Roberson /*
111911b763dfSJeff Roberson  * Compute the fo_poll flags required for a fd given by the index and
112011b763dfSJeff Roberson  * bit position in the fd_mask array.
112111b763dfSJeff Roberson  */
112211b763dfSJeff Roberson static __inline int
112360b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit)
112411b763dfSJeff Roberson {
112511b763dfSJeff Roberson 	int flags;
112611b763dfSJeff Roberson 	int msk;
112711b763dfSJeff Roberson 
112811b763dfSJeff Roberson 	flags = 0;
112911b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
113011b763dfSJeff Roberson 		if (ibits[msk] == NULL)
113111b763dfSJeff Roberson 			continue;
113260b7f468SStephane E. Potvin 		if ((ibits[msk][idx] & bit) == 0)
113311b763dfSJeff Roberson 			continue;
113411b763dfSJeff Roberson 		flags |= select_flags[msk];
113511b763dfSJeff Roberson 	}
113611b763dfSJeff Roberson 	return (flags);
113711b763dfSJeff Roberson }
113811b763dfSJeff Roberson 
113911b763dfSJeff Roberson /*
114011b763dfSJeff Roberson  * Set the appropriate output bits given a mask of fired events and the
114111b763dfSJeff Roberson  * input bits originally requested.
114211b763dfSJeff Roberson  */
114311b763dfSJeff Roberson static __inline int
114411b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
114511b763dfSJeff Roberson {
114611b763dfSJeff Roberson 	int msk;
114711b763dfSJeff Roberson 	int n;
114811b763dfSJeff Roberson 
114911b763dfSJeff Roberson 	n = 0;
115011b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
115111b763dfSJeff Roberson 		if ((events & select_flags[msk]) == 0)
115211b763dfSJeff Roberson 			continue;
115311b763dfSJeff Roberson 		if (ibits[msk] == NULL)
115411b763dfSJeff Roberson 			continue;
115511b763dfSJeff Roberson 		if ((ibits[msk][idx] & bit) == 0)
115611b763dfSJeff Roberson 			continue;
115711b763dfSJeff Roberson 		/*
115811b763dfSJeff Roberson 		 * XXX Check for a duplicate set.  This can occur because a
115911b763dfSJeff Roberson 		 * socket calls selrecord() twice for each poll() call
116011b763dfSJeff Roberson 		 * resulting in two selfds per real fd.  selrescan() will
116111b763dfSJeff Roberson 		 * call selsetbits twice as a result.
116211b763dfSJeff Roberson 		 */
116311b763dfSJeff Roberson 		if ((obits[msk][idx] & bit) != 0)
116411b763dfSJeff Roberson 			continue;
116511b763dfSJeff Roberson 		obits[msk][idx] |= bit;
116611b763dfSJeff Roberson 		n++;
116711b763dfSJeff Roberson 	}
116811b763dfSJeff Roberson 
116911b763dfSJeff Roberson 	return (n);
117011b763dfSJeff Roberson }
1171df8bae1dSRodney W. Grimes 
1172a9d2f8d8SRobert Watson static __inline int
1173a9d2f8d8SRobert Watson getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp)
1174a9d2f8d8SRobert Watson {
1175a9d2f8d8SRobert Watson 
11762609222aSPawel Jakub Dawidek 	return (fget_unlocked(fdp, fd, CAP_POLL_EVENT, 0, fpp, NULL));
1177a9d2f8d8SRobert Watson }
1178a9d2f8d8SRobert Watson 
1179ace8398dSJeff Roberson /*
1180ace8398dSJeff Roberson  * Traverse the list of fds attached to this thread's seltd and check for
1181ace8398dSJeff Roberson  * completion.
1182ace8398dSJeff Roberson  */
1183ace8398dSJeff Roberson static int
1184ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
1185ace8398dSJeff Roberson {
118611b763dfSJeff Roberson 	struct filedesc *fdp;
118711b763dfSJeff Roberson 	struct selinfo *si;
1188ace8398dSJeff Roberson 	struct seltd *stp;
1189ace8398dSJeff Roberson 	struct selfd *sfp;
1190ace8398dSJeff Roberson 	struct selfd *sfn;
1191ace8398dSJeff Roberson 	struct file *fp;
11929cdacff1SJeff Roberson 	fd_mask bit;
11939cdacff1SJeff Roberson 	int fd, ev, n, idx;
1194a9d2f8d8SRobert Watson 	int error;
1195ace8398dSJeff Roberson 
119611b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
1197ace8398dSJeff Roberson 	stp = td->td_sel;
119811b763dfSJeff Roberson 	n = 0;
1199ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1200ace8398dSJeff Roberson 		fd = (int)(uintptr_t)sfp->sf_cookie;
1201ace8398dSJeff Roberson 		si = sfp->sf_si;
1202ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1203ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1204ace8398dSJeff Roberson 		if (si != NULL)
1205ace8398dSJeff Roberson 			continue;
1206a9d2f8d8SRobert Watson 		error = getselfd_cap(fdp, fd, &fp);
1207a9d2f8d8SRobert Watson 		if (error)
1208a9d2f8d8SRobert Watson 			return (error);
120911b763dfSJeff Roberson 		idx = fd / NFDBITS;
12109cdacff1SJeff Roberson 		bit = (fd_mask)1 << (fd % NFDBITS);
121111b763dfSJeff Roberson 		ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
1212bf422e5fSJeff Roberson 		fdrop(fp, td);
121311b763dfSJeff Roberson 		if (ev != 0)
121411b763dfSJeff Roberson 			n += selsetbits(ibits, obits, idx, bit, ev);
1215ace8398dSJeff Roberson 	}
1216ace8398dSJeff Roberson 	stp->st_flags = 0;
1217ace8398dSJeff Roberson 	td->td_retval[0] = n;
1218ace8398dSJeff Roberson 	return (0);
1219ace8398dSJeff Roberson }
1220ace8398dSJeff Roberson 
1221ace8398dSJeff Roberson /*
1222ace8398dSJeff Roberson  * Perform the initial filedescriptor scan and register ourselves with
1223ace8398dSJeff Roberson  * each selinfo.
1224ace8398dSJeff Roberson  */
1225265fc98fSSeigo Tanimura static int
1226b40ce416SJulian Elischer selscan(td, ibits, obits, nfd)
1227b40ce416SJulian Elischer 	struct thread *td;
1228b08f7993SSujal Patel 	fd_mask **ibits, **obits;
1229cb226aaaSPoul-Henning Kamp 	int nfd;
1230df8bae1dSRodney W. Grimes {
123111b763dfSJeff Roberson 	struct filedesc *fdp;
1232df8bae1dSRodney W. Grimes 	struct file *fp;
12339cdacff1SJeff Roberson 	fd_mask bit;
123411b763dfSJeff Roberson 	int ev, flags, end, fd;
12359cdacff1SJeff Roberson 	int n, idx;
1236a9d2f8d8SRobert Watson 	int error;
1237df8bae1dSRodney W. Grimes 
123811b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
123911b763dfSJeff Roberson 	n = 0;
12409cdacff1SJeff Roberson 	for (idx = 0, fd = 0; fd < nfd; idx++) {
124111b763dfSJeff Roberson 		end = imin(fd + NFDBITS, nfd);
124211b763dfSJeff Roberson 		for (bit = 1; fd < end; bit <<= 1, fd++) {
124311b763dfSJeff Roberson 			/* Compute the list of events we're interested in. */
124411b763dfSJeff Roberson 			flags = selflags(ibits, idx, bit);
124511b763dfSJeff Roberson 			if (flags == 0)
1246f082218cSPeter Wemm 				continue;
1247a9d2f8d8SRobert Watson 			error = getselfd_cap(fdp, fd, &fp);
1248a9d2f8d8SRobert Watson 			if (error)
1249a9d2f8d8SRobert Watson 				return (error);
1250ace8398dSJeff Roberson 			selfdalloc(td, (void *)(uintptr_t)fd);
125111b763dfSJeff Roberson 			ev = fo_poll(fp, flags, td->td_ucred, td);
1252bf422e5fSJeff Roberson 			fdrop(fp, td);
125311b763dfSJeff Roberson 			if (ev != 0)
125411b763dfSJeff Roberson 				n += selsetbits(ibits, obits, idx, bit, ev);
1255df8bae1dSRodney W. Grimes 		}
1256df8bae1dSRodney W. Grimes 	}
125711b763dfSJeff Roberson 
1258b40ce416SJulian Elischer 	td->td_retval[0] = n;
1259df8bae1dSRodney W. Grimes 	return (0);
1260df8bae1dSRodney W. Grimes }
1261df8bae1dSRodney W. Grimes 
126242d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
126342d11757SPeter Wemm struct poll_args {
126442d11757SPeter Wemm 	struct pollfd *fds;
126542d11757SPeter Wemm 	u_int	nfds;
126642d11757SPeter Wemm 	int	timeout;
126742d11757SPeter Wemm };
126842d11757SPeter Wemm #endif
126942d11757SPeter Wemm int
12708451d0ddSKip Macy sys_poll(td, uap)
1271b40ce416SJulian Elischer 	struct thread *td;
1272ea0237edSJonathan Lemon 	struct poll_args *uap;
127342d11757SPeter Wemm {
12742580f4e5SAndre Oppermann 	struct pollfd *bits;
12752580f4e5SAndre Oppermann 	struct pollfd smallbits[32];
1276*cf5e4fe6SDavide Italiano 	sbintime_t asbt, precision, rsbt;
1277ace8398dSJeff Roberson 	u_int nfds;
1278*cf5e4fe6SDavide Italiano 	int error;
127942d11757SPeter Wemm 	size_t ni;
128042d11757SPeter Wemm 
1281d1e405c5SAlfred Perlstein 	nfds = uap->nfds;
1282374ae2a3SJeff Roberson 	if (nfds > maxfilesperproc && nfds > FD_SETSIZE)
1283ace8398dSJeff Roberson 		return (EINVAL);
128489b71647SPeter Wemm 	ni = nfds * sizeof(struct pollfd);
128542d11757SPeter Wemm 	if (ni > sizeof(smallbits))
1286a163d034SWarner Losh 		bits = malloc(ni, M_TEMP, M_WAITOK);
128742d11757SPeter Wemm 	else
128842d11757SPeter Wemm 		bits = smallbits;
1289d1e405c5SAlfred Perlstein 	error = copyin(uap->fds, bits, ni);
129042d11757SPeter Wemm 	if (error)
1291ace8398dSJeff Roberson 		goto done;
1292*cf5e4fe6SDavide Italiano 	precision = 0;
1293d1e405c5SAlfred Perlstein 	if (uap->timeout != INFTIM) {
1294*cf5e4fe6SDavide Italiano 		if (uap->timeout < 0) {
129542d11757SPeter Wemm 			error = EINVAL;
1296ace8398dSJeff Roberson 			goto done;
129742d11757SPeter Wemm 		}
1298*cf5e4fe6SDavide Italiano 		rsbt = SBT_1MS * uap->timeout;
1299*cf5e4fe6SDavide Italiano 		precision = rsbt;
1300*cf5e4fe6SDavide Italiano 		precision >>= tc_precexp;
1301*cf5e4fe6SDavide Italiano 		if (TIMESEL(&asbt, rsbt))
1302*cf5e4fe6SDavide Italiano 			asbt += tc_tick_sbt;
1303*cf5e4fe6SDavide Italiano 		asbt += rsbt;
1304*cf5e4fe6SDavide Italiano 	} else
1305*cf5e4fe6SDavide Italiano 		asbt = -1;
1306ace8398dSJeff Roberson 	seltdinit(td);
1307ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1308ace8398dSJeff Roberson 	for (;;) {
13092580f4e5SAndre Oppermann 		error = pollscan(td, bits, nfds);
1310ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1311ace8398dSJeff Roberson 			break;
1312*cf5e4fe6SDavide Italiano 		error = seltdwait(td, asbt, precision);
1313ace8398dSJeff Roberson 		if (error)
1314ace8398dSJeff Roberson 			break;
1315ace8398dSJeff Roberson 		error = pollrescan(td);
1316ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1317ace8398dSJeff Roberson 			break;
131885f190e4SAlfred Perlstein 	}
1319ace8398dSJeff Roberson 	seltdclear(td);
1320265fc98fSSeigo Tanimura 
132142d11757SPeter Wemm done:
132242d11757SPeter Wemm 	/* poll is not restarted after signals... */
132342d11757SPeter Wemm 	if (error == ERESTART)
132442d11757SPeter Wemm 		error = EINTR;
132542d11757SPeter Wemm 	if (error == EWOULDBLOCK)
132642d11757SPeter Wemm 		error = 0;
132742d11757SPeter Wemm 	if (error == 0) {
13286d8feddaSKonstantin Belousov 		error = pollout(td, bits, uap->fds, nfds);
132942d11757SPeter Wemm 		if (error)
133042d11757SPeter Wemm 			goto out;
133142d11757SPeter Wemm 	}
133242d11757SPeter Wemm out:
133342d11757SPeter Wemm 	if (ni > sizeof(smallbits))
133442d11757SPeter Wemm 		free(bits, M_TEMP);
133542d11757SPeter Wemm 	return (error);
133642d11757SPeter Wemm }
133742d11757SPeter Wemm 
133842d11757SPeter Wemm static int
1339ace8398dSJeff Roberson pollrescan(struct thread *td)
1340ace8398dSJeff Roberson {
1341ace8398dSJeff Roberson 	struct seltd *stp;
1342ace8398dSJeff Roberson 	struct selfd *sfp;
1343ace8398dSJeff Roberson 	struct selfd *sfn;
1344ace8398dSJeff Roberson 	struct selinfo *si;
1345ace8398dSJeff Roberson 	struct filedesc *fdp;
1346ace8398dSJeff Roberson 	struct file *fp;
1347ace8398dSJeff Roberson 	struct pollfd *fd;
1348ace8398dSJeff Roberson 	int n;
1349ace8398dSJeff Roberson 
1350ace8398dSJeff Roberson 	n = 0;
1351ace8398dSJeff Roberson 	fdp = td->td_proc->p_fd;
1352ace8398dSJeff Roberson 	stp = td->td_sel;
1353ace8398dSJeff Roberson 	FILEDESC_SLOCK(fdp);
1354ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1355ace8398dSJeff Roberson 		fd = (struct pollfd *)sfp->sf_cookie;
1356ace8398dSJeff Roberson 		si = sfp->sf_si;
1357ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1358ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1359ace8398dSJeff Roberson 		if (si != NULL)
1360ace8398dSJeff Roberson 			continue;
13612609222aSPawel Jakub Dawidek 		fp = fdp->fd_ofiles[fd->fd].fde_file;
1362d6f72489SJonathan Anderson #ifdef CAPABILITIES
13632609222aSPawel Jakub Dawidek 		if (fp == NULL ||
13642609222aSPawel Jakub Dawidek 		    cap_check(cap_rights(fdp, fd->fd), CAP_POLL_EVENT) != 0)
1365d6f72489SJonathan Anderson #else
13662609222aSPawel Jakub Dawidek 		if (fp == NULL)
1367d6f72489SJonathan Anderson #endif
13682609222aSPawel Jakub Dawidek 		{
1369ace8398dSJeff Roberson 			fd->revents = POLLNVAL;
1370ace8398dSJeff Roberson 			n++;
1371ace8398dSJeff Roberson 			continue;
1372ace8398dSJeff Roberson 		}
1373d6f72489SJonathan Anderson 
1374ace8398dSJeff Roberson 		/*
1375ace8398dSJeff Roberson 		 * Note: backend also returns POLLHUP and
1376ace8398dSJeff Roberson 		 * POLLERR if appropriate.
1377ace8398dSJeff Roberson 		 */
1378ace8398dSJeff Roberson 		fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1379ace8398dSJeff Roberson 		if (fd->revents != 0)
1380ace8398dSJeff Roberson 			n++;
1381ace8398dSJeff Roberson 	}
1382ace8398dSJeff Roberson 	FILEDESC_SUNLOCK(fdp);
1383ace8398dSJeff Roberson 	stp->st_flags = 0;
1384ace8398dSJeff Roberson 	td->td_retval[0] = n;
1385ace8398dSJeff Roberson 	return (0);
1386ace8398dSJeff Roberson }
1387ace8398dSJeff Roberson 
1388ace8398dSJeff Roberson 
1389ace8398dSJeff Roberson static int
13906d8feddaSKonstantin Belousov pollout(td, fds, ufds, nfd)
13916d8feddaSKonstantin Belousov 	struct thread *td;
1392ae81968fSRobert Watson 	struct pollfd *fds;
1393ae81968fSRobert Watson 	struct pollfd *ufds;
1394ae81968fSRobert Watson 	u_int nfd;
1395ae81968fSRobert Watson {
1396ae81968fSRobert Watson 	int error = 0;
1397ae81968fSRobert Watson 	u_int i = 0;
13986d8feddaSKonstantin Belousov 	u_int n = 0;
1399ae81968fSRobert Watson 
1400ae81968fSRobert Watson 	for (i = 0; i < nfd; i++) {
1401ae81968fSRobert Watson 		error = copyout(&fds->revents, &ufds->revents,
1402ae81968fSRobert Watson 		    sizeof(ufds->revents));
1403ae81968fSRobert Watson 		if (error)
1404ae81968fSRobert Watson 			return (error);
14056d8feddaSKonstantin Belousov 		if (fds->revents != 0)
14066d8feddaSKonstantin Belousov 			n++;
1407ae81968fSRobert Watson 		fds++;
1408ae81968fSRobert Watson 		ufds++;
1409ae81968fSRobert Watson 	}
14106d8feddaSKonstantin Belousov 	td->td_retval[0] = n;
1411ae81968fSRobert Watson 	return (0);
1412ae81968fSRobert Watson }
1413ae81968fSRobert Watson 
1414ae81968fSRobert Watson static int
1415b40ce416SJulian Elischer pollscan(td, fds, nfd)
1416b40ce416SJulian Elischer 	struct thread *td;
141742d11757SPeter Wemm 	struct pollfd *fds;
1418ea0237edSJonathan Lemon 	u_int nfd;
141942d11757SPeter Wemm {
1420ace8398dSJeff Roberson 	struct filedesc *fdp = td->td_proc->p_fd;
142142d11757SPeter Wemm 	struct file *fp;
14222609222aSPawel Jakub Dawidek 	int i, n = 0;
142342d11757SPeter Wemm 
14245e3f7694SRobert Watson 	FILEDESC_SLOCK(fdp);
1425eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
1426337c9691SJordan K. Hubbard 		if (fds->fd >= fdp->fd_nfiles) {
142742d11757SPeter Wemm 			fds->revents = POLLNVAL;
142842d11757SPeter Wemm 			n++;
1429337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
1430337c9691SJordan K. Hubbard 			fds->revents = 0;
143142d11757SPeter Wemm 		} else {
14322609222aSPawel Jakub Dawidek 			fp = fdp->fd_ofiles[fds->fd].fde_file;
1433d6f72489SJonathan Anderson #ifdef CAPABILITIES
14342609222aSPawel Jakub Dawidek 			if (fp == NULL ||
14352609222aSPawel Jakub Dawidek 			    cap_check(cap_rights(fdp, fds->fd),
14362609222aSPawel Jakub Dawidek 			    CAP_POLL_EVENT) != 0)
1437d6f72489SJonathan Anderson #else
14382609222aSPawel Jakub Dawidek 			if (fp == NULL)
1439d6f72489SJonathan Anderson #endif
14402609222aSPawel Jakub Dawidek 			{
144142d11757SPeter Wemm 				fds->revents = POLLNVAL;
144242d11757SPeter Wemm 				n++;
144342d11757SPeter Wemm 			} else {
14442087c896SBruce Evans 				/*
14452087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
14462087c896SBruce Evans 				 * POLLERR if appropriate.
14472087c896SBruce Evans 				 */
1448ace8398dSJeff Roberson 				selfdalloc(td, fds);
144913ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
1450ea6027a8SRobert Watson 				    td->td_ucred, td);
1451f2159cc7SKonstantin Belousov 				/*
1452f2159cc7SKonstantin Belousov 				 * POSIX requires POLLOUT to be never
1453f2159cc7SKonstantin Belousov 				 * set simultaneously with POLLHUP.
1454f2159cc7SKonstantin Belousov 				 */
1455f2159cc7SKonstantin Belousov 				if ((fds->revents & POLLHUP) != 0)
1456f2159cc7SKonstantin Belousov 					fds->revents &= ~POLLOUT;
1457f2159cc7SKonstantin Belousov 
145842d11757SPeter Wemm 				if (fds->revents != 0)
145942d11757SPeter Wemm 					n++;
146042d11757SPeter Wemm 			}
146142d11757SPeter Wemm 		}
146242d11757SPeter Wemm 	}
14635e3f7694SRobert Watson 	FILEDESC_SUNLOCK(fdp);
1464b40ce416SJulian Elischer 	td->td_retval[0] = n;
146542d11757SPeter Wemm 	return (0);
146642d11757SPeter Wemm }
146742d11757SPeter Wemm 
146842d11757SPeter Wemm /*
146942d11757SPeter Wemm  * OpenBSD poll system call.
14700c14ff0eSRobert Watson  *
147142d11757SPeter Wemm  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
147242d11757SPeter Wemm  */
147342d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
147442d11757SPeter Wemm struct openbsd_poll_args {
147542d11757SPeter Wemm 	struct pollfd *fds;
147642d11757SPeter Wemm 	u_int	nfds;
147742d11757SPeter Wemm 	int	timeout;
147842d11757SPeter Wemm };
147942d11757SPeter Wemm #endif
148042d11757SPeter Wemm int
14818451d0ddSKip Macy sys_openbsd_poll(td, uap)
1482b40ce416SJulian Elischer 	register struct thread *td;
148342d11757SPeter Wemm 	register struct openbsd_poll_args *uap;
148442d11757SPeter Wemm {
14858451d0ddSKip Macy 	return (sys_poll(td, (struct poll_args *)uap));
148642d11757SPeter Wemm }
148742d11757SPeter Wemm 
148885f190e4SAlfred Perlstein /*
1489ace8398dSJeff Roberson  * Preallocate two selfds associated with 'cookie'.  Some fo_poll routines
1490ace8398dSJeff Roberson  * have two select sets, one for read and another for write.
1491ace8398dSJeff Roberson  */
1492ace8398dSJeff Roberson static void
1493ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie)
1494ace8398dSJeff Roberson {
1495ace8398dSJeff Roberson 	struct seltd *stp;
1496ace8398dSJeff Roberson 
1497ace8398dSJeff Roberson 	stp = td->td_sel;
1498ace8398dSJeff Roberson 	if (stp->st_free1 == NULL)
1499ace8398dSJeff Roberson 		stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1500ace8398dSJeff Roberson 	stp->st_free1->sf_td = stp;
1501ace8398dSJeff Roberson 	stp->st_free1->sf_cookie = cookie;
1502ace8398dSJeff Roberson 	if (stp->st_free2 == NULL)
1503ace8398dSJeff Roberson 		stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1504ace8398dSJeff Roberson 	stp->st_free2->sf_td = stp;
1505ace8398dSJeff Roberson 	stp->st_free2->sf_cookie = cookie;
1506ace8398dSJeff Roberson }
1507ace8398dSJeff Roberson 
1508ace8398dSJeff Roberson static void
1509ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp)
1510ace8398dSJeff Roberson {
1511ace8398dSJeff Roberson 	STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
1512ace8398dSJeff Roberson 	mtx_lock(sfp->sf_mtx);
1513ace8398dSJeff Roberson 	if (sfp->sf_si)
1514ace8398dSJeff Roberson 		TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1515ace8398dSJeff Roberson 	mtx_unlock(sfp->sf_mtx);
1516ace8398dSJeff Roberson 	uma_zfree(selfd_zone, sfp);
151785f190e4SAlfred Perlstein }
151885f190e4SAlfred Perlstein 
15196aba400aSAttilio Rao /* Drain the waiters tied to all the selfd belonging the specified selinfo. */
15206aba400aSAttilio Rao void
15216aba400aSAttilio Rao seldrain(sip)
15226aba400aSAttilio Rao         struct selinfo *sip;
15236aba400aSAttilio Rao {
15246aba400aSAttilio Rao 
15256aba400aSAttilio Rao 	/*
15266aba400aSAttilio Rao 	 * This feature is already provided by doselwakeup(), thus it is
15276aba400aSAttilio Rao 	 * enough to go for it.
15286aba400aSAttilio Rao 	 * Eventually, the context, should take care to avoid races
15296aba400aSAttilio Rao 	 * between thread calling select()/poll() and file descriptor
15306aba400aSAttilio Rao 	 * detaching, but, again, the races are just the same as
15316aba400aSAttilio Rao 	 * selwakeup().
15326aba400aSAttilio Rao 	 */
15336aba400aSAttilio Rao         doselwakeup(sip, -1);
15346aba400aSAttilio Rao }
15356aba400aSAttilio Rao 
1536df8bae1dSRodney W. Grimes /*
1537df8bae1dSRodney W. Grimes  * Record a select request.
1538df8bae1dSRodney W. Grimes  */
1539df8bae1dSRodney W. Grimes void
1540df8bae1dSRodney W. Grimes selrecord(selector, sip)
1541b40ce416SJulian Elischer 	struct thread *selector;
1542df8bae1dSRodney W. Grimes 	struct selinfo *sip;
1543df8bae1dSRodney W. Grimes {
1544ace8398dSJeff Roberson 	struct selfd *sfp;
1545ace8398dSJeff Roberson 	struct seltd *stp;
1546ace8398dSJeff Roberson 	struct mtx *mtxp;
1547df8bae1dSRodney W. Grimes 
1548ace8398dSJeff Roberson 	stp = selector->td_sel;
154985f190e4SAlfred Perlstein 	/*
1550ace8398dSJeff Roberson 	 * Don't record when doing a rescan.
155185f190e4SAlfred Perlstein 	 */
1552ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_RESCAN)
1553ace8398dSJeff Roberson 		return;
1554ace8398dSJeff Roberson 	/*
1555ace8398dSJeff Roberson 	 * Grab one of the preallocated descriptors.
1556ace8398dSJeff Roberson 	 */
1557ace8398dSJeff Roberson 	sfp = NULL;
1558ace8398dSJeff Roberson 	if ((sfp = stp->st_free1) != NULL)
1559ace8398dSJeff Roberson 		stp->st_free1 = NULL;
1560ace8398dSJeff Roberson 	else if ((sfp = stp->st_free2) != NULL)
1561ace8398dSJeff Roberson 		stp->st_free2 = NULL;
1562ace8398dSJeff Roberson 	else
1563ace8398dSJeff Roberson 		panic("selrecord: No free selfd on selq");
15642141453eSJeff Roberson 	mtxp = sip->si_mtx;
15652141453eSJeff Roberson 	if (mtxp == NULL)
15662141453eSJeff Roberson 		mtxp = mtx_pool_find(mtxpool_select, sip);
1567ace8398dSJeff Roberson 	/*
1568ace8398dSJeff Roberson 	 * Initialize the sfp and queue it in the thread.
1569ace8398dSJeff Roberson 	 */
1570ace8398dSJeff Roberson 	sfp->sf_si = sip;
1571ace8398dSJeff Roberson 	sfp->sf_mtx = mtxp;
1572ace8398dSJeff Roberson 	STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1573ace8398dSJeff Roberson 	/*
1574ace8398dSJeff Roberson 	 * Now that we've locked the sip, check for initialization.
1575ace8398dSJeff Roberson 	 */
1576ace8398dSJeff Roberson 	mtx_lock(mtxp);
1577ace8398dSJeff Roberson 	if (sip->si_mtx == NULL) {
1578ace8398dSJeff Roberson 		sip->si_mtx = mtxp;
1579ace8398dSJeff Roberson 		TAILQ_INIT(&sip->si_tdlist);
158085f190e4SAlfred Perlstein 	}
1581ace8398dSJeff Roberson 	/*
1582ace8398dSJeff Roberson 	 * Add this thread to the list of selfds listening on this selinfo.
1583ace8398dSJeff Roberson 	 */
1584ace8398dSJeff Roberson 	TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1585ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1586df8bae1dSRodney W. Grimes }
1587df8bae1dSRodney W. Grimes 
1588512824f8SSeigo Tanimura /* Wake up a selecting thread. */
1589df8bae1dSRodney W. Grimes void
1590df8bae1dSRodney W. Grimes selwakeup(sip)
159185f190e4SAlfred Perlstein 	struct selinfo *sip;
1592df8bae1dSRodney W. Grimes {
1593512824f8SSeigo Tanimura 	doselwakeup(sip, -1);
1594512824f8SSeigo Tanimura }
1595512824f8SSeigo Tanimura 
1596512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */
1597512824f8SSeigo Tanimura void
1598512824f8SSeigo Tanimura selwakeuppri(sip, pri)
1599512824f8SSeigo Tanimura 	struct selinfo *sip;
1600512824f8SSeigo Tanimura 	int pri;
1601512824f8SSeigo Tanimura {
1602512824f8SSeigo Tanimura 	doselwakeup(sip, pri);
1603512824f8SSeigo Tanimura }
1604512824f8SSeigo Tanimura 
1605512824f8SSeigo Tanimura /*
1606512824f8SSeigo Tanimura  * Do a wakeup when a selectable event occurs.
1607512824f8SSeigo Tanimura  */
1608512824f8SSeigo Tanimura static void
1609512824f8SSeigo Tanimura doselwakeup(sip, pri)
1610512824f8SSeigo Tanimura 	struct selinfo *sip;
1611512824f8SSeigo Tanimura 	int pri;
1612512824f8SSeigo Tanimura {
1613ace8398dSJeff Roberson 	struct selfd *sfp;
1614ace8398dSJeff Roberson 	struct selfd *sfn;
1615ace8398dSJeff Roberson 	struct seltd *stp;
1616df8bae1dSRodney W. Grimes 
1617ace8398dSJeff Roberson 	/* If it's not initialized there can't be any waiters. */
1618ace8398dSJeff Roberson 	if (sip->si_mtx == NULL)
1619b40ce416SJulian Elischer 		return;
1620ace8398dSJeff Roberson 	/*
1621ace8398dSJeff Roberson 	 * Locking the selinfo locks all selfds associated with it.
1622ace8398dSJeff Roberson 	 */
1623ace8398dSJeff Roberson 	mtx_lock(sip->si_mtx);
1624ace8398dSJeff Roberson 	TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1625ace8398dSJeff Roberson 		/*
1626ace8398dSJeff Roberson 		 * Once we remove this sfp from the list and clear the
1627ace8398dSJeff Roberson 		 * sf_si seltdclear will know to ignore this si.
1628ace8398dSJeff Roberson 		 */
1629ace8398dSJeff Roberson 		TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1630ace8398dSJeff Roberson 		sfp->sf_si = NULL;
1631ace8398dSJeff Roberson 		stp = sfp->sf_td;
1632ace8398dSJeff Roberson 		mtx_lock(&stp->st_mtx);
1633ace8398dSJeff Roberson 		stp->st_flags |= SELTD_PENDING;
1634ace8398dSJeff Roberson 		cv_broadcastpri(&stp->st_wait, pri);
1635ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1636b40ce416SJulian Elischer 	}
1637ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1638ace8398dSJeff Roberson }
1639ace8398dSJeff Roberson 
1640ace8398dSJeff Roberson static void
1641ace8398dSJeff Roberson seltdinit(struct thread *td)
1642ace8398dSJeff Roberson {
1643ace8398dSJeff Roberson 	struct seltd *stp;
1644ace8398dSJeff Roberson 
1645ace8398dSJeff Roberson 	if ((stp = td->td_sel) != NULL)
1646ace8398dSJeff Roberson 		goto out;
1647ace8398dSJeff Roberson 	td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1648ace8398dSJeff Roberson 	mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1649ace8398dSJeff Roberson 	cv_init(&stp->st_wait, "select");
1650ace8398dSJeff Roberson out:
1651ace8398dSJeff Roberson 	stp->st_flags = 0;
1652ace8398dSJeff Roberson 	STAILQ_INIT(&stp->st_selq);
1653ace8398dSJeff Roberson }
1654ace8398dSJeff Roberson 
1655ace8398dSJeff Roberson static int
1656*cf5e4fe6SDavide Italiano seltdwait(struct thread *td, sbintime_t sbt, sbintime_t precision)
1657ace8398dSJeff Roberson {
1658ace8398dSJeff Roberson 	struct seltd *stp;
1659ace8398dSJeff Roberson 	int error;
1660ace8398dSJeff Roberson 
1661ace8398dSJeff Roberson 	stp = td->td_sel;
1662ace8398dSJeff Roberson 	/*
1663ace8398dSJeff Roberson 	 * An event of interest may occur while we do not hold the seltd
1664ace8398dSJeff Roberson 	 * locked so check the pending flag before we sleep.
1665ace8398dSJeff Roberson 	 */
1666ace8398dSJeff Roberson 	mtx_lock(&stp->st_mtx);
1667ace8398dSJeff Roberson 	/*
1668ace8398dSJeff Roberson 	 * Any further calls to selrecord will be a rescan.
1669ace8398dSJeff Roberson 	 */
1670ace8398dSJeff Roberson 	stp->st_flags |= SELTD_RESCAN;
1671ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_PENDING) {
1672ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1673ace8398dSJeff Roberson 		return (0);
1674ace8398dSJeff Roberson 	}
1675*cf5e4fe6SDavide Italiano 	if (sbt == 0)
1676*cf5e4fe6SDavide Italiano 		error = EWOULDBLOCK;
1677*cf5e4fe6SDavide Italiano 	else if (sbt != -1)
1678*cf5e4fe6SDavide Italiano 		error = cv_timedwait_sig_sbt(&stp->st_wait, &stp->st_mtx,
1679*cf5e4fe6SDavide Italiano 		    sbt, precision, C_ABSOLUTE);
1680ace8398dSJeff Roberson 	else
1681ace8398dSJeff Roberson 		error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
1682ace8398dSJeff Roberson 	mtx_unlock(&stp->st_mtx);
1683ace8398dSJeff Roberson 
1684ace8398dSJeff Roberson 	return (error);
1685ace8398dSJeff Roberson }
1686ace8398dSJeff Roberson 
1687ace8398dSJeff Roberson void
1688ace8398dSJeff Roberson seltdfini(struct thread *td)
1689ace8398dSJeff Roberson {
1690ace8398dSJeff Roberson 	struct seltd *stp;
1691ace8398dSJeff Roberson 
1692ace8398dSJeff Roberson 	stp = td->td_sel;
1693ace8398dSJeff Roberson 	if (stp == NULL)
1694ace8398dSJeff Roberson 		return;
1695ace8398dSJeff Roberson 	if (stp->st_free1)
1696ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free1);
1697ace8398dSJeff Roberson 	if (stp->st_free2)
1698ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free2);
1699ace8398dSJeff Roberson 	td->td_sel = NULL;
1700ace8398dSJeff Roberson 	free(stp, M_SELECT);
1701ace8398dSJeff Roberson }
1702ace8398dSJeff Roberson 
1703ace8398dSJeff Roberson /*
1704ace8398dSJeff Roberson  * Remove the references to the thread from all of the objects we were
1705ace8398dSJeff Roberson  * polling.
1706ace8398dSJeff Roberson  */
1707ace8398dSJeff Roberson static void
1708ace8398dSJeff Roberson seltdclear(struct thread *td)
1709ace8398dSJeff Roberson {
1710ace8398dSJeff Roberson 	struct seltd *stp;
1711ace8398dSJeff Roberson 	struct selfd *sfp;
1712ace8398dSJeff Roberson 	struct selfd *sfn;
1713ace8398dSJeff Roberson 
1714ace8398dSJeff Roberson 	stp = td->td_sel;
1715ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
1716ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1717ace8398dSJeff Roberson 	stp->st_flags = 0;
1718df8bae1dSRodney W. Grimes }
1719265fc98fSSeigo Tanimura 
17204d77a549SAlfred Perlstein static void selectinit(void *);
1721ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
1722265fc98fSSeigo Tanimura static void
1723ace8398dSJeff Roberson selectinit(void *dummy __unused)
1724265fc98fSSeigo Tanimura {
17252141453eSJeff Roberson 
1726ace8398dSJeff Roberson 	selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL,
1727ace8398dSJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, 0);
17282141453eSJeff Roberson 	mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);
1729265fc98fSSeigo Tanimura }
1730