xref: /freebsd/sys/kern/sys_generic.c (revision 526d0bd547574b185147f03c13e2db7abe566a08)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1989, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  * (c) UNIX System Laboratories, Inc.
5df8bae1dSRodney W. Grimes  * All or some portions of this file are derived from material licensed
6df8bae1dSRodney W. Grimes  * to the University of California by American Telephone and Telegraph
7df8bae1dSRodney W. Grimes  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8df8bae1dSRodney W. Grimes  * the permission of UNIX System Laboratories, Inc.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *	@(#)sys_generic.c	8.5 (Berkeley) 1/21/94
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37677b542eSDavid E. O'Brien #include <sys/cdefs.h>
38677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
39677b542eSDavid E. O'Brien 
40a9d2f8d8SRobert Watson #include "opt_capsicum.h"
412de92a38SPeter Wemm #include "opt_compat.h"
42db6a20e2SGarrett Wollman #include "opt_ktrace.h"
43db6a20e2SGarrett Wollman 
44df8bae1dSRodney W. Grimes #include <sys/param.h>
45df8bae1dSRodney W. Grimes #include <sys/systm.h>
46d2d3e875SBruce Evans #include <sys/sysproto.h>
47a9d2f8d8SRobert Watson #include <sys/capability.h>
48df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
4920982410SBruce Evans #include <sys/filio.h>
503ac4d1efSBruce Evans #include <sys/fcntl.h>
51df8bae1dSRodney W. Grimes #include <sys/file.h>
52df8bae1dSRodney W. Grimes #include <sys/proc.h>
53797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
54df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
55df8bae1dSRodney W. Grimes #include <sys/uio.h>
56df8bae1dSRodney W. Grimes #include <sys/kernel.h>
57e4650294SJohn Baldwin #include <sys/ktr.h>
58104a9b7eSAlexander Kabaev #include <sys/limits.h>
59df8bae1dSRodney W. Grimes #include <sys/malloc.h>
6042d11757SPeter Wemm #include <sys/poll.h>
6189b71647SPeter Wemm #include <sys/resourcevar.h>
620a2c3d48SGarrett Wollman #include <sys/selinfo.h>
6344f3b092SJohn Baldwin #include <sys/sleepqueue.h>
648f19eb88SIan Dowse #include <sys/syscallsubr.h>
658cb96f20SPeter Wemm #include <sys/sysctl.h>
6642d11757SPeter Wemm #include <sys/sysent.h>
679bbee259SAndrey A. Chernov #include <sys/vnode.h>
68279d7226SMatthew Dillon #include <sys/bio.h>
69279d7226SMatthew Dillon #include <sys/buf.h>
70265fc98fSSeigo Tanimura #include <sys/condvar.h>
71df8bae1dSRodney W. Grimes #ifdef KTRACE
72df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
73df8bae1dSRodney W. Grimes #endif
74df8bae1dSRodney W. Grimes 
75e4650294SJohn Baldwin #include <security/audit/audit.h>
76ace8398dSJeff Roberson 
77*526d0bd5SKonstantin Belousov int iosize_max_clamp = 1;
78*526d0bd5SKonstantin Belousov SYSCTL_INT(_debug, OID_AUTO, iosize_max_clamp, CTLFLAG_RW, &iosize_max_clamp, 0,
79*526d0bd5SKonstantin Belousov     "Clamp max i/o size to INT_MAX");
80*526d0bd5SKonstantin Belousov 
81a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
82a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
83a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's");
8455166637SPoul-Henning Kamp 
856d8feddaSKonstantin Belousov static int	pollout(struct thread *, struct pollfd *, struct pollfd *,
866d8feddaSKonstantin Belousov 		    u_int);
87bbbb04ceSAlfred Perlstein static int	pollscan(struct thread *, struct pollfd *, u_int);
88ace8398dSJeff Roberson static int	pollrescan(struct thread *);
89bbbb04ceSAlfred Perlstein static int	selscan(struct thread *, fd_mask **, fd_mask **, int);
90ace8398dSJeff Roberson static int	selrescan(struct thread *, fd_mask **, fd_mask **);
91ace8398dSJeff Roberson static void	selfdalloc(struct thread *, void *);
92ace8398dSJeff Roberson static void	selfdfree(struct seltd *, struct selfd *);
93bcd9e0ddSJohn Baldwin static int	dofileread(struct thread *, int, struct file *, struct uio *,
94bcd9e0ddSJohn Baldwin 		    off_t, int);
95bcd9e0ddSJohn Baldwin static int	dofilewrite(struct thread *, int, struct file *, struct uio *,
96bcd9e0ddSJohn Baldwin 		    off_t, int);
97512824f8SSeigo Tanimura static void	doselwakeup(struct selinfo *, int);
98ace8398dSJeff Roberson static void	seltdinit(struct thread *);
99ace8398dSJeff Roberson static int	seltdwait(struct thread *, int);
100ace8398dSJeff Roberson static void	seltdclear(struct thread *);
101ace8398dSJeff Roberson 
102ace8398dSJeff Roberson /*
103ace8398dSJeff Roberson  * One seltd per-thread allocated on demand as needed.
104ace8398dSJeff Roberson  *
105ace8398dSJeff Roberson  *	t - protected by st_mtx
106ace8398dSJeff Roberson  * 	k - Only accessed by curthread or read-only
107ace8398dSJeff Roberson  */
108ace8398dSJeff Roberson struct seltd {
109ace8398dSJeff Roberson 	STAILQ_HEAD(, selfd)	st_selq;	/* (k) List of selfds. */
110ace8398dSJeff Roberson 	struct selfd		*st_free1;	/* (k) free fd for read set. */
111ace8398dSJeff Roberson 	struct selfd		*st_free2;	/* (k) free fd for write set. */
112ace8398dSJeff Roberson 	struct mtx		st_mtx;		/* Protects struct seltd */
113ace8398dSJeff Roberson 	struct cv		st_wait;	/* (t) Wait channel. */
114ace8398dSJeff Roberson 	int			st_flags;	/* (t) SELTD_ flags. */
115ace8398dSJeff Roberson };
116ace8398dSJeff Roberson 
117ace8398dSJeff Roberson #define	SELTD_PENDING	0x0001			/* We have pending events. */
118ace8398dSJeff Roberson #define	SELTD_RESCAN	0x0002			/* Doing a rescan. */
119ace8398dSJeff Roberson 
120ace8398dSJeff Roberson /*
121ace8398dSJeff Roberson  * One selfd allocated per-thread per-file-descriptor.
122ace8398dSJeff Roberson  *	f - protected by sf_mtx
123ace8398dSJeff Roberson  */
124ace8398dSJeff Roberson struct selfd {
125ace8398dSJeff Roberson 	STAILQ_ENTRY(selfd)	sf_link;	/* (k) fds owned by this td. */
126ace8398dSJeff Roberson 	TAILQ_ENTRY(selfd)	sf_threads;	/* (f) fds on this selinfo. */
127ace8398dSJeff Roberson 	struct selinfo		*sf_si;		/* (f) selinfo when linked. */
128ace8398dSJeff Roberson 	struct mtx		*sf_mtx;	/* Pointer to selinfo mtx. */
129ace8398dSJeff Roberson 	struct seltd		*sf_td;		/* (k) owning seltd. */
130ace8398dSJeff Roberson 	void			*sf_cookie;	/* (k) fd or pollfd. */
131ace8398dSJeff Roberson };
132ace8398dSJeff Roberson 
133ace8398dSJeff Roberson static uma_zone_t selfd_zone;
1342141453eSJeff Roberson static struct mtx_pool *mtxpool_select;
1358fe387abSDmitrij Tejblum 
136d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
137df8bae1dSRodney W. Grimes struct read_args {
138df8bae1dSRodney W. Grimes 	int	fd;
139134e06feSBruce Evans 	void	*buf;
140134e06feSBruce Evans 	size_t	nbyte;
141df8bae1dSRodney W. Grimes };
142d2d3e875SBruce Evans #endif
14326f9a767SRodney W. Grimes int
1448451d0ddSKip Macy sys_read(td, uap)
145b40ce416SJulian Elischer 	struct thread *td;
146b064d43dSMatthew Dillon 	struct read_args *uap;
147df8bae1dSRodney W. Grimes {
148bcd9e0ddSJohn Baldwin 	struct uio auio;
149bcd9e0ddSJohn Baldwin 	struct iovec aiov;
150279d7226SMatthew Dillon 	int error;
151df8bae1dSRodney W. Grimes 
152*526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
153bcd9e0ddSJohn Baldwin 		return (EINVAL);
154bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
155bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
156bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
157bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
158bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
159bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
160bcd9e0ddSJohn Baldwin 	error = kern_readv(td, uap->fd, &auio);
161279d7226SMatthew Dillon 	return(error);
162df8bae1dSRodney W. Grimes }
163df8bae1dSRodney W. Grimes 
164df8bae1dSRodney W. Grimes /*
165bcd9e0ddSJohn Baldwin  * Positioned read system call
1664160ccd9SAlan Cox  */
1674160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
1684160ccd9SAlan Cox struct pread_args {
1694160ccd9SAlan Cox 	int	fd;
1704160ccd9SAlan Cox 	void	*buf;
1714160ccd9SAlan Cox 	size_t	nbyte;
1728fe387abSDmitrij Tejblum 	int	pad;
1734160ccd9SAlan Cox 	off_t	offset;
1744160ccd9SAlan Cox };
1754160ccd9SAlan Cox #endif
1764160ccd9SAlan Cox int
1778451d0ddSKip Macy sys_pread(td, uap)
178b40ce416SJulian Elischer 	struct thread *td;
179b064d43dSMatthew Dillon 	struct pread_args *uap;
1804160ccd9SAlan Cox {
1814160ccd9SAlan Cox 	struct uio auio;
1824160ccd9SAlan Cox 	struct iovec aiov;
183bcd9e0ddSJohn Baldwin 	int error;
1844160ccd9SAlan Cox 
185*526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
186bcd9e0ddSJohn Baldwin 		return (EINVAL);
187bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
188bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
1894160ccd9SAlan Cox 	auio.uio_iov = &aiov;
1904160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
191bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
1924160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
193bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, &auio, uap->offset);
1944160ccd9SAlan Cox 	return(error);
1954160ccd9SAlan Cox }
1964160ccd9SAlan Cox 
197c2815ad5SPeter Wemm int
198c2815ad5SPeter Wemm freebsd6_pread(td, uap)
199c2815ad5SPeter Wemm 	struct thread *td;
200c2815ad5SPeter Wemm 	struct freebsd6_pread_args *uap;
201c2815ad5SPeter Wemm {
202c2815ad5SPeter Wemm 	struct pread_args oargs;
203c2815ad5SPeter Wemm 
204c2815ad5SPeter Wemm 	oargs.fd = uap->fd;
205c2815ad5SPeter Wemm 	oargs.buf = uap->buf;
206c2815ad5SPeter Wemm 	oargs.nbyte = uap->nbyte;
207c2815ad5SPeter Wemm 	oargs.offset = uap->offset;
2088451d0ddSKip Macy 	return (sys_pread(td, &oargs));
209c2815ad5SPeter Wemm }
210c2815ad5SPeter Wemm 
2114160ccd9SAlan Cox /*
212df8bae1dSRodney W. Grimes  * Scatter read system call.
213df8bae1dSRodney W. Grimes  */
214d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
215df8bae1dSRodney W. Grimes struct readv_args {
2167147b19dSBruce Evans 	int	fd;
217df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
218df8bae1dSRodney W. Grimes 	u_int	iovcnt;
219df8bae1dSRodney W. Grimes };
220d2d3e875SBruce Evans #endif
22126f9a767SRodney W. Grimes int
2228451d0ddSKip Macy sys_readv(struct thread *td, struct readv_args *uap)
223df8bae1dSRodney W. Grimes {
224b88ec951SJohn Baldwin 	struct uio *auio;
225b88ec951SJohn Baldwin 	int error;
226b88ec951SJohn Baldwin 
227b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
228b88ec951SJohn Baldwin 	if (error)
229b88ec951SJohn Baldwin 		return (error);
230b88ec951SJohn Baldwin 	error = kern_readv(td, uap->fd, auio);
231b88ec951SJohn Baldwin 	free(auio, M_IOV);
232b88ec951SJohn Baldwin 	return (error);
233b88ec951SJohn Baldwin }
234b88ec951SJohn Baldwin 
235b88ec951SJohn Baldwin int
236b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio)
237b88ec951SJohn Baldwin {
238b064d43dSMatthew Dillon 	struct file *fp;
239bcd9e0ddSJohn Baldwin 	int error;
240bcd9e0ddSJohn Baldwin 
241a9d2f8d8SRobert Watson 	error = fget_read(td, fd, CAP_READ | CAP_SEEK, &fp);
242bcd9e0ddSJohn Baldwin 	if (error)
243bcd9e0ddSJohn Baldwin 		return (error);
244bcd9e0ddSJohn Baldwin 	error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
245bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
246bcd9e0ddSJohn Baldwin 	return (error);
247bcd9e0ddSJohn Baldwin }
248bcd9e0ddSJohn Baldwin 
249bcd9e0ddSJohn Baldwin /*
250bcd9e0ddSJohn Baldwin  * Scatter positioned read system call.
251bcd9e0ddSJohn Baldwin  */
252bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
253bcd9e0ddSJohn Baldwin struct preadv_args {
254bcd9e0ddSJohn Baldwin 	int	fd;
255bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
256bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
257bcd9e0ddSJohn Baldwin 	off_t	offset;
258bcd9e0ddSJohn Baldwin };
259bcd9e0ddSJohn Baldwin #endif
260bcd9e0ddSJohn Baldwin int
2618451d0ddSKip Macy sys_preadv(struct thread *td, struct preadv_args *uap)
262bcd9e0ddSJohn Baldwin {
263bcd9e0ddSJohn Baldwin 	struct uio *auio;
264bcd9e0ddSJohn Baldwin 	int error;
265bcd9e0ddSJohn Baldwin 
266bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
267bcd9e0ddSJohn Baldwin 	if (error)
268bcd9e0ddSJohn Baldwin 		return (error);
269bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, auio, uap->offset);
270bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
271bcd9e0ddSJohn Baldwin 	return (error);
272bcd9e0ddSJohn Baldwin }
273bcd9e0ddSJohn Baldwin 
274bcd9e0ddSJohn Baldwin int
275bcd9e0ddSJohn Baldwin kern_preadv(td, fd, auio, offset)
276bcd9e0ddSJohn Baldwin 	struct thread *td;
277bcd9e0ddSJohn Baldwin 	int fd;
278bcd9e0ddSJohn Baldwin 	struct uio *auio;
279bcd9e0ddSJohn Baldwin 	off_t offset;
280bcd9e0ddSJohn Baldwin {
281bcd9e0ddSJohn Baldwin 	struct file *fp;
282bcd9e0ddSJohn Baldwin 	int error;
283bcd9e0ddSJohn Baldwin 
284a9d2f8d8SRobert Watson 	error = fget_read(td, fd, CAP_READ, &fp);
285bcd9e0ddSJohn Baldwin 	if (error)
286bcd9e0ddSJohn Baldwin 		return (error);
287bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
288bcd9e0ddSJohn Baldwin 		error = ESPIPE;
289bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
290bcd9e0ddSJohn Baldwin 		error = EINVAL;
291bcd9e0ddSJohn Baldwin 	else
292bcd9e0ddSJohn Baldwin 		error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
293bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
294bcd9e0ddSJohn Baldwin 	return (error);
295bcd9e0ddSJohn Baldwin }
296bcd9e0ddSJohn Baldwin 
297bcd9e0ddSJohn Baldwin /*
298bcd9e0ddSJohn Baldwin  * Common code for readv and preadv that reads data in
299bcd9e0ddSJohn Baldwin  * from a file using the passed in uio, offset, and flags.
300bcd9e0ddSJohn Baldwin  */
301bcd9e0ddSJohn Baldwin static int
302bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags)
303bcd9e0ddSJohn Baldwin 	struct thread *td;
304bcd9e0ddSJohn Baldwin 	int fd;
305bcd9e0ddSJohn Baldwin 	struct file *fp;
306bcd9e0ddSJohn Baldwin 	struct uio *auio;
307bcd9e0ddSJohn Baldwin 	off_t offset;
308bcd9e0ddSJohn Baldwin 	int flags;
309bcd9e0ddSJohn Baldwin {
310bcd9e0ddSJohn Baldwin 	ssize_t cnt;
31182641acdSAlan Cox 	int error;
312df8bae1dSRodney W. Grimes #ifdef KTRACE
313552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
314df8bae1dSRodney W. Grimes #endif
315df8bae1dSRodney W. Grimes 
3164f8d23d6SPoul-Henning Kamp 	/* Finish zero length reads right here */
3174f8d23d6SPoul-Henning Kamp 	if (auio->uio_resid == 0) {
3184f8d23d6SPoul-Henning Kamp 		td->td_retval[0] = 0;
3194f8d23d6SPoul-Henning Kamp 		return(0);
3204f8d23d6SPoul-Henning Kamp 	}
321552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_READ;
322bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
323552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
324df8bae1dSRodney W. Grimes #ifdef KTRACE
325552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
326552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
327df8bae1dSRodney W. Grimes #endif
328552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
329bcd9e0ddSJohn Baldwin 	if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
330552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
331df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
332df8bae1dSRodney W. Grimes 			error = 0;
333279d7226SMatthew Dillon 	}
334552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
335df8bae1dSRodney W. Grimes #ifdef KTRACE
336552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
337552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
338b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_READ, ktruio, error);
339df8bae1dSRodney W. Grimes 	}
340df8bae1dSRodney W. Grimes #endif
341*526d0bd5SKonstantin Belousov #if SSIZE_MAX > LONG_MAX
342*526d0bd5SKonstantin Belousov 	td->td_retval[1] = cnt >> (sizeof(register_t) * CHAR_BIT);
343b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
344*526d0bd5SKonstantin Belousov #else
345*526d0bd5SKonstantin Belousov 	td->td_retval[0] = cnt;
346*526d0bd5SKonstantin Belousov #endif
347df8bae1dSRodney W. Grimes 	return (error);
348df8bae1dSRodney W. Grimes }
349df8bae1dSRodney W. Grimes 
350d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
351df8bae1dSRodney W. Grimes struct write_args {
352df8bae1dSRodney W. Grimes 	int	fd;
353134e06feSBruce Evans 	const void *buf;
354134e06feSBruce Evans 	size_t	nbyte;
355df8bae1dSRodney W. Grimes };
356d2d3e875SBruce Evans #endif
35726f9a767SRodney W. Grimes int
3588451d0ddSKip Macy sys_write(td, uap)
359b40ce416SJulian Elischer 	struct thread *td;
360b064d43dSMatthew Dillon 	struct write_args *uap;
361df8bae1dSRodney W. Grimes {
362bcd9e0ddSJohn Baldwin 	struct uio auio;
363bcd9e0ddSJohn Baldwin 	struct iovec aiov;
364279d7226SMatthew Dillon 	int error;
365df8bae1dSRodney W. Grimes 
366*526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
367bcd9e0ddSJohn Baldwin 		return (EINVAL);
368bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
369bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
370bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
371bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
372bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
373bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
374bcd9e0ddSJohn Baldwin 	error = kern_writev(td, uap->fd, &auio);
375279d7226SMatthew Dillon 	return(error);
376df8bae1dSRodney W. Grimes }
377df8bae1dSRodney W. Grimes 
378df8bae1dSRodney W. Grimes /*
3790c14ff0eSRobert Watson  * Positioned write system call.
3804160ccd9SAlan Cox  */
3814160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
3824160ccd9SAlan Cox struct pwrite_args {
3834160ccd9SAlan Cox 	int	fd;
3844160ccd9SAlan Cox 	const void *buf;
3854160ccd9SAlan Cox 	size_t	nbyte;
3868fe387abSDmitrij Tejblum 	int	pad;
3874160ccd9SAlan Cox 	off_t	offset;
3884160ccd9SAlan Cox };
3894160ccd9SAlan Cox #endif
3904160ccd9SAlan Cox int
3918451d0ddSKip Macy sys_pwrite(td, uap)
392b40ce416SJulian Elischer 	struct thread *td;
393b064d43dSMatthew Dillon 	struct pwrite_args *uap;
3944160ccd9SAlan Cox {
3954160ccd9SAlan Cox 	struct uio auio;
3964160ccd9SAlan Cox 	struct iovec aiov;
397bcd9e0ddSJohn Baldwin 	int error;
3984160ccd9SAlan Cox 
399*526d0bd5SKonstantin Belousov 	if (uap->nbyte > IOSIZE_MAX)
400bcd9e0ddSJohn Baldwin 		return (EINVAL);
401bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
402bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
4034160ccd9SAlan Cox 	auio.uio_iov = &aiov;
4044160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
405bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
4064160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
407bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, &auio, uap->offset);
4084160ccd9SAlan Cox 	return(error);
4094160ccd9SAlan Cox }
4104160ccd9SAlan Cox 
411c2815ad5SPeter Wemm int
412c2815ad5SPeter Wemm freebsd6_pwrite(td, uap)
413c2815ad5SPeter Wemm 	struct thread *td;
414c2815ad5SPeter Wemm 	struct freebsd6_pwrite_args *uap;
415c2815ad5SPeter Wemm {
416c2815ad5SPeter Wemm 	struct pwrite_args oargs;
417c2815ad5SPeter Wemm 
418c2815ad5SPeter Wemm 	oargs.fd = uap->fd;
419c2815ad5SPeter Wemm 	oargs.buf = uap->buf;
420c2815ad5SPeter Wemm 	oargs.nbyte = uap->nbyte;
421c2815ad5SPeter Wemm 	oargs.offset = uap->offset;
4228451d0ddSKip Macy 	return (sys_pwrite(td, &oargs));
423c2815ad5SPeter Wemm }
424c2815ad5SPeter Wemm 
4254160ccd9SAlan Cox /*
4260c14ff0eSRobert Watson  * Gather write system call.
427df8bae1dSRodney W. Grimes  */
428d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
429df8bae1dSRodney W. Grimes struct writev_args {
430df8bae1dSRodney W. Grimes 	int	fd;
431df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
432df8bae1dSRodney W. Grimes 	u_int	iovcnt;
433df8bae1dSRodney W. Grimes };
434d2d3e875SBruce Evans #endif
43526f9a767SRodney W. Grimes int
4368451d0ddSKip Macy sys_writev(struct thread *td, struct writev_args *uap)
437df8bae1dSRodney W. Grimes {
438b88ec951SJohn Baldwin 	struct uio *auio;
439b88ec951SJohn Baldwin 	int error;
440b88ec951SJohn Baldwin 
441b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
442b88ec951SJohn Baldwin 	if (error)
443b88ec951SJohn Baldwin 		return (error);
444b88ec951SJohn Baldwin 	error = kern_writev(td, uap->fd, auio);
445b88ec951SJohn Baldwin 	free(auio, M_IOV);
446b88ec951SJohn Baldwin 	return (error);
447b88ec951SJohn Baldwin }
448b88ec951SJohn Baldwin 
449b88ec951SJohn Baldwin int
450b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio)
451b88ec951SJohn Baldwin {
452b064d43dSMatthew Dillon 	struct file *fp;
453bcd9e0ddSJohn Baldwin 	int error;
454bcd9e0ddSJohn Baldwin 
455a9d2f8d8SRobert Watson 	error = fget_write(td, fd, CAP_WRITE | CAP_SEEK, &fp);
456bcd9e0ddSJohn Baldwin 	if (error)
457af56abaaSJohn Baldwin 		return (error);
458bcd9e0ddSJohn Baldwin 	error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
459bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
460bcd9e0ddSJohn Baldwin 	return (error);
461bcd9e0ddSJohn Baldwin }
462bcd9e0ddSJohn Baldwin 
463bcd9e0ddSJohn Baldwin /*
4640c14ff0eSRobert Watson  * Gather positioned write system call.
465bcd9e0ddSJohn Baldwin  */
466bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
467bcd9e0ddSJohn Baldwin struct pwritev_args {
468bcd9e0ddSJohn Baldwin 	int	fd;
469bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
470bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
471bcd9e0ddSJohn Baldwin 	off_t	offset;
472bcd9e0ddSJohn Baldwin };
473bcd9e0ddSJohn Baldwin #endif
474bcd9e0ddSJohn Baldwin int
4758451d0ddSKip Macy sys_pwritev(struct thread *td, struct pwritev_args *uap)
476bcd9e0ddSJohn Baldwin {
477bcd9e0ddSJohn Baldwin 	struct uio *auio;
478bcd9e0ddSJohn Baldwin 	int error;
479bcd9e0ddSJohn Baldwin 
480bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
481bcd9e0ddSJohn Baldwin 	if (error)
482bcd9e0ddSJohn Baldwin 		return (error);
483bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, auio, uap->offset);
484bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
485bcd9e0ddSJohn Baldwin 	return (error);
486bcd9e0ddSJohn Baldwin }
487bcd9e0ddSJohn Baldwin 
488bcd9e0ddSJohn Baldwin int
489bcd9e0ddSJohn Baldwin kern_pwritev(td, fd, auio, offset)
490bcd9e0ddSJohn Baldwin 	struct thread *td;
491bcd9e0ddSJohn Baldwin 	struct uio *auio;
492bcd9e0ddSJohn Baldwin 	int fd;
493bcd9e0ddSJohn Baldwin 	off_t offset;
494bcd9e0ddSJohn Baldwin {
495bcd9e0ddSJohn Baldwin 	struct file *fp;
496bcd9e0ddSJohn Baldwin 	int error;
497bcd9e0ddSJohn Baldwin 
498a9d2f8d8SRobert Watson 	error = fget_write(td, fd, CAP_WRITE, &fp);
499bcd9e0ddSJohn Baldwin 	if (error)
500af56abaaSJohn Baldwin 		return (error);
501bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
502bcd9e0ddSJohn Baldwin 		error = ESPIPE;
503bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
504bcd9e0ddSJohn Baldwin 		error = EINVAL;
505bcd9e0ddSJohn Baldwin 	else
506bcd9e0ddSJohn Baldwin 		error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
507bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
508bcd9e0ddSJohn Baldwin 	return (error);
509bcd9e0ddSJohn Baldwin }
510bcd9e0ddSJohn Baldwin 
511bcd9e0ddSJohn Baldwin /*
512bcd9e0ddSJohn Baldwin  * Common code for writev and pwritev that writes data to
513bcd9e0ddSJohn Baldwin  * a file using the passed in uio, offset, and flags.
514bcd9e0ddSJohn Baldwin  */
515bcd9e0ddSJohn Baldwin static int
516bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags)
517bcd9e0ddSJohn Baldwin 	struct thread *td;
518bcd9e0ddSJohn Baldwin 	int fd;
519bcd9e0ddSJohn Baldwin 	struct file *fp;
520bcd9e0ddSJohn Baldwin 	struct uio *auio;
521bcd9e0ddSJohn Baldwin 	off_t offset;
522bcd9e0ddSJohn Baldwin 	int flags;
523bcd9e0ddSJohn Baldwin {
524bcd9e0ddSJohn Baldwin 	ssize_t cnt;
525552afd9cSPoul-Henning Kamp 	int error;
526df8bae1dSRodney W. Grimes #ifdef KTRACE
527552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
528df8bae1dSRodney W. Grimes #endif
529df8bae1dSRodney W. Grimes 
530552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_WRITE;
531552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
532bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
533df8bae1dSRodney W. Grimes #ifdef KTRACE
534552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
535552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
536df8bae1dSRodney W. Grimes #endif
537552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
538a41ce5d3SMatthew Dillon 	if (fp->f_type == DTYPE_VNODE)
5399440653dSMatthew Dillon 		bwillwrite();
540bcd9e0ddSJohn Baldwin 	if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
541552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
542df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
543df8bae1dSRodney W. Grimes 			error = 0;
544bcd9e0ddSJohn Baldwin 		/* Socket layer is responsible for issuing SIGPIPE. */
5456f7ca813SBruce M Simpson 		if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
546b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
5477a6f3d78SJohn Baldwin 			tdsignal(td, SIGPIPE);
548b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
54919eb87d2SJohn Baldwin 		}
550df8bae1dSRodney W. Grimes 	}
551552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
552df8bae1dSRodney W. Grimes #ifdef KTRACE
553552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
554552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
555b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_WRITE, ktruio, error);
556df8bae1dSRodney W. Grimes 	}
557df8bae1dSRodney W. Grimes #endif
558*526d0bd5SKonstantin Belousov #if SSIZE_MAX > LONG_MAX
559*526d0bd5SKonstantin Belousov 	td->td_retval[1] = cnt >> (sizeof(register_t) * CHAR_BIT);
560b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
561*526d0bd5SKonstantin Belousov #else
562*526d0bd5SKonstantin Belousov 	td->td_retval[0] = cnt;
563*526d0bd5SKonstantin Belousov #endif
564df8bae1dSRodney W. Grimes 	return (error);
565df8bae1dSRodney W. Grimes }
566df8bae1dSRodney W. Grimes 
567e4650294SJohn Baldwin /*
568e4650294SJohn Baldwin  * Truncate a file given a file descriptor.
569e4650294SJohn Baldwin  *
570e4650294SJohn Baldwin  * Can't use fget_write() here, since must return EINVAL and not EBADF if the
571e4650294SJohn Baldwin  * descriptor isn't writable.
572e4650294SJohn Baldwin  */
573e4650294SJohn Baldwin int
574e4650294SJohn Baldwin kern_ftruncate(td, fd, length)
575e4650294SJohn Baldwin 	struct thread *td;
576e4650294SJohn Baldwin 	int fd;
577e4650294SJohn Baldwin 	off_t length;
578e4650294SJohn Baldwin {
579e4650294SJohn Baldwin 	struct file *fp;
580e4650294SJohn Baldwin 	int error;
581e4650294SJohn Baldwin 
58214961ba7SRobert Watson 	AUDIT_ARG_FD(fd);
583e4650294SJohn Baldwin 	if (length < 0)
584e4650294SJohn Baldwin 		return (EINVAL);
585a9d2f8d8SRobert Watson 	error = fget(td, fd, CAP_FTRUNCATE, &fp);
586e4650294SJohn Baldwin 	if (error)
587e4650294SJohn Baldwin 		return (error);
58814961ba7SRobert Watson 	AUDIT_ARG_FILE(td->td_proc, fp);
589e4650294SJohn Baldwin 	if (!(fp->f_flag & FWRITE)) {
590e4650294SJohn Baldwin 		fdrop(fp, td);
591e4650294SJohn Baldwin 		return (EINVAL);
592e4650294SJohn Baldwin 	}
593e4650294SJohn Baldwin 	error = fo_truncate(fp, length, td->td_ucred, td);
594e4650294SJohn Baldwin 	fdrop(fp, td);
595e4650294SJohn Baldwin 	return (error);
596e4650294SJohn Baldwin }
597e4650294SJohn Baldwin 
598e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
599e4650294SJohn Baldwin struct ftruncate_args {
600e4650294SJohn Baldwin 	int	fd;
601e4650294SJohn Baldwin 	int	pad;
602e4650294SJohn Baldwin 	off_t	length;
603e4650294SJohn Baldwin };
604e4650294SJohn Baldwin #endif
605e4650294SJohn Baldwin int
6068451d0ddSKip Macy sys_ftruncate(td, uap)
607e4650294SJohn Baldwin 	struct thread *td;
608e4650294SJohn Baldwin 	struct ftruncate_args *uap;
609e4650294SJohn Baldwin {
610e4650294SJohn Baldwin 
611e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
612e4650294SJohn Baldwin }
613e4650294SJohn Baldwin 
614e4650294SJohn Baldwin #if defined(COMPAT_43)
615e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
616e4650294SJohn Baldwin struct oftruncate_args {
617e4650294SJohn Baldwin 	int	fd;
618e4650294SJohn Baldwin 	long	length;
619e4650294SJohn Baldwin };
620e4650294SJohn Baldwin #endif
621e4650294SJohn Baldwin int
622e4650294SJohn Baldwin oftruncate(td, uap)
623e4650294SJohn Baldwin 	struct thread *td;
624e4650294SJohn Baldwin 	struct oftruncate_args *uap;
625e4650294SJohn Baldwin {
626e4650294SJohn Baldwin 
627e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
628e4650294SJohn Baldwin }
629e4650294SJohn Baldwin #endif /* COMPAT_43 */
630e4650294SJohn Baldwin 
631d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
632df8bae1dSRodney W. Grimes struct ioctl_args {
633df8bae1dSRodney W. Grimes 	int	fd;
634069e9bc1SDoug Rabson 	u_long	com;
635df8bae1dSRodney W. Grimes 	caddr_t	data;
636df8bae1dSRodney W. Grimes };
637d2d3e875SBruce Evans #endif
638df8bae1dSRodney W. Grimes /* ARGSUSED */
63926f9a767SRodney W. Grimes int
6408451d0ddSKip Macy sys_ioctl(struct thread *td, struct ioctl_args *uap)
641df8bae1dSRodney W. Grimes {
6423e15c66fSPoul-Henning Kamp 	u_long com;
6439fddcc66SRuslan Ermilov 	int arg, error;
6443e15c66fSPoul-Henning Kamp 	u_int size;
6459fddcc66SRuslan Ermilov 	caddr_t data;
646df8bae1dSRodney W. Grimes 
6479fc6aa06SPoul-Henning Kamp 	if (uap->com > 0xffffffff) {
6489fc6aa06SPoul-Henning Kamp 		printf(
6499fc6aa06SPoul-Henning Kamp 		    "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
650431f8906SJulian Elischer 		    td->td_proc->p_pid, td->td_name, uap->com);
6519fc6aa06SPoul-Henning Kamp 		uap->com &= 0xffffffff;
6529fc6aa06SPoul-Henning Kamp 	}
653d9f46233SJohn Baldwin 	com = uap->com;
654df8bae1dSRodney W. Grimes 
655df8bae1dSRodney W. Grimes 	/*
656df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
657df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
658df8bae1dSRodney W. Grimes 	 */
659df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
660ca51b19bSPoul-Henning Kamp 	if ((size > IOCPARM_MAX) ||
661ca51b19bSPoul-Henning Kamp 	    ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
6622de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
6632de92a38SPeter Wemm 	    ((com & IOC_OUT) && size == 0) ||
6642de92a38SPeter Wemm #else
6652de92a38SPeter Wemm 	    ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
6662de92a38SPeter Wemm #endif
6679fddcc66SRuslan Ermilov 	    ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
668426da3bcSAlfred Perlstein 		return (ENOTTY);
669279d7226SMatthew Dillon 
670ca51b19bSPoul-Henning Kamp 	if (size > 0) {
671715457f6SDavid E. O'Brien 		if (com & IOC_VOID) {
6729fddcc66SRuslan Ermilov 			/* Integer argument. */
6739fddcc66SRuslan Ermilov 			arg = (intptr_t)uap->data;
6749fddcc66SRuslan Ermilov 			data = (void *)&arg;
6759fddcc66SRuslan Ermilov 			size = 0;
676715457f6SDavid E. O'Brien 		} else
677715457f6SDavid E. O'Brien 			data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
6789fddcc66SRuslan Ermilov 	} else
6799fddcc66SRuslan Ermilov 		data = (void *)&uap->data;
680df8bae1dSRodney W. Grimes 	if (com & IOC_IN) {
681df8bae1dSRodney W. Grimes 		error = copyin(uap->data, data, (u_int)size);
682df8bae1dSRodney W. Grimes 		if (error) {
683a1b0a180SRuslan Ermilov 			if (size > 0)
6849fddcc66SRuslan Ermilov 				free(data, M_IOCTLOPS);
6853e15c66fSPoul-Henning Kamp 			return (error);
686df8bae1dSRodney W. Grimes 		}
687ca51b19bSPoul-Henning Kamp 	} else if (com & IOC_OUT) {
688df8bae1dSRodney W. Grimes 		/*
689df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
690df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
691df8bae1dSRodney W. Grimes 		 */
692df8bae1dSRodney W. Grimes 		bzero(data, size);
693279d7226SMatthew Dillon 	}
694df8bae1dSRodney W. Grimes 
695d9f46233SJohn Baldwin 	error = kern_ioctl(td, uap->fd, com, data);
696d9f46233SJohn Baldwin 
697d9f46233SJohn Baldwin 	if (error == 0 && (com & IOC_OUT))
698d9f46233SJohn Baldwin 		error = copyout(data, uap->data, (u_int)size);
699d9f46233SJohn Baldwin 
7009fddcc66SRuslan Ermilov 	if (size > 0)
7019fddcc66SRuslan Ermilov 		free(data, M_IOCTLOPS);
702d9f46233SJohn Baldwin 	return (error);
703d9f46233SJohn Baldwin }
704d9f46233SJohn Baldwin 
705d9f46233SJohn Baldwin int
706d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
707d9f46233SJohn Baldwin {
708d9f46233SJohn Baldwin 	struct file *fp;
709d9f46233SJohn Baldwin 	struct filedesc *fdp;
710d9f46233SJohn Baldwin 	int error;
711d9f46233SJohn Baldwin 	int tmp;
712d9f46233SJohn Baldwin 
71364c9a4d9SRobert Watson 	AUDIT_ARG_FD(fd);
71464c9a4d9SRobert Watson 	AUDIT_ARG_CMD(com);
715a9d2f8d8SRobert Watson 	if ((error = fget(td, fd, CAP_IOCTL, &fp)) != 0)
716d9f46233SJohn Baldwin 		return (error);
717d9f46233SJohn Baldwin 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
718d9f46233SJohn Baldwin 		fdrop(fp, td);
719d9f46233SJohn Baldwin 		return (EBADF);
720d9f46233SJohn Baldwin 	}
721d9f46233SJohn Baldwin 	fdp = td->td_proc->p_fd;
722d9f46233SJohn Baldwin 	switch (com) {
723d9f46233SJohn Baldwin 	case FIONCLEX:
7245e3f7694SRobert Watson 		FILEDESC_XLOCK(fdp);
725d9f46233SJohn Baldwin 		fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
7265e3f7694SRobert Watson 		FILEDESC_XUNLOCK(fdp);
727d9f46233SJohn Baldwin 		goto out;
728d9f46233SJohn Baldwin 	case FIOCLEX:
7295e3f7694SRobert Watson 		FILEDESC_XLOCK(fdp);
730d9f46233SJohn Baldwin 		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
7315e3f7694SRobert Watson 		FILEDESC_XUNLOCK(fdp);
732d9f46233SJohn Baldwin 		goto out;
733d9f46233SJohn Baldwin 	case FIONBIO:
734bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
735397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FNONBLOCK);
736df8bae1dSRodney W. Grimes 		else
737397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FNONBLOCK);
7388ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
739d9f46233SJohn Baldwin 		break;
740d9f46233SJohn Baldwin 	case FIOASYNC:
741bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
742397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FASYNC);
743df8bae1dSRodney W. Grimes 		else
744397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FASYNC);
7458ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
746d9f46233SJohn Baldwin 		break;
747df8bae1dSRodney W. Grimes 	}
7488ccf264fSPoul-Henning Kamp 
7498ccf264fSPoul-Henning Kamp 	error = fo_ioctl(fp, com, data, td->td_ucred, td);
750d9f46233SJohn Baldwin out:
751b40ce416SJulian Elischer 	fdrop(fp, td);
752df8bae1dSRodney W. Grimes 	return (error);
753df8bae1dSRodney W. Grimes }
754df8bae1dSRodney W. Grimes 
755125dcf8cSKonstantin Belousov int
756125dcf8cSKonstantin Belousov poll_no_poll(int events)
757125dcf8cSKonstantin Belousov {
758125dcf8cSKonstantin Belousov 	/*
759125dcf8cSKonstantin Belousov 	 * Return true for read/write.  If the user asked for something
760125dcf8cSKonstantin Belousov 	 * special, return POLLNVAL, so that clients have a way of
761125dcf8cSKonstantin Belousov 	 * determining reliably whether or not the extended
762125dcf8cSKonstantin Belousov 	 * functionality is present without hard-coding knowledge
763125dcf8cSKonstantin Belousov 	 * of specific filesystem implementations.
764125dcf8cSKonstantin Belousov 	 */
765125dcf8cSKonstantin Belousov 	if (events & ~POLLSTANDARD)
766125dcf8cSKonstantin Belousov 		return (POLLNVAL);
767125dcf8cSKonstantin Belousov 
768125dcf8cSKonstantin Belousov 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
769125dcf8cSKonstantin Belousov }
770125dcf8cSKonstantin Belousov 
771066d836bSKonstantin Belousov int
7728451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap)
773066d836bSKonstantin Belousov {
774066d836bSKonstantin Belousov 	struct timespec ts;
775066d836bSKonstantin Belousov 	struct timeval tv, *tvp;
776066d836bSKonstantin Belousov 	sigset_t set, *uset;
777066d836bSKonstantin Belousov 	int error;
778066d836bSKonstantin Belousov 
779066d836bSKonstantin Belousov 	if (uap->ts != NULL) {
780066d836bSKonstantin Belousov 		error = copyin(uap->ts, &ts, sizeof(ts));
781066d836bSKonstantin Belousov 		if (error != 0)
782066d836bSKonstantin Belousov 		    return (error);
783066d836bSKonstantin Belousov 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
784066d836bSKonstantin Belousov 		tvp = &tv;
785066d836bSKonstantin Belousov 	} else
786066d836bSKonstantin Belousov 		tvp = NULL;
787066d836bSKonstantin Belousov 	if (uap->sm != NULL) {
788066d836bSKonstantin Belousov 		error = copyin(uap->sm, &set, sizeof(set));
789066d836bSKonstantin Belousov 		if (error != 0)
790066d836bSKonstantin Belousov 			return (error);
791066d836bSKonstantin Belousov 		uset = &set;
792066d836bSKonstantin Belousov 	} else
793066d836bSKonstantin Belousov 		uset = NULL;
794066d836bSKonstantin Belousov 	return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
795066d836bSKonstantin Belousov 	    uset, NFDBITS));
796066d836bSKonstantin Belousov }
797066d836bSKonstantin Belousov 
798066d836bSKonstantin Belousov int
799066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
800066d836bSKonstantin Belousov     struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
801066d836bSKonstantin Belousov {
802066d836bSKonstantin Belousov 	int error;
803066d836bSKonstantin Belousov 
804066d836bSKonstantin Belousov 	if (uset != NULL) {
805066d836bSKonstantin Belousov 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
806066d836bSKonstantin Belousov 		    &td->td_oldsigmask, 0);
807066d836bSKonstantin Belousov 		if (error != 0)
808066d836bSKonstantin Belousov 			return (error);
809066d836bSKonstantin Belousov 		td->td_pflags |= TDP_OLDMASK;
810066d836bSKonstantin Belousov 		/*
811066d836bSKonstantin Belousov 		 * Make sure that ast() is called on return to
812066d836bSKonstantin Belousov 		 * usermode and TDP_OLDMASK is cleared, restoring old
813066d836bSKonstantin Belousov 		 * sigmask.
814066d836bSKonstantin Belousov 		 */
815066d836bSKonstantin Belousov 		thread_lock(td);
816066d836bSKonstantin Belousov 		td->td_flags |= TDF_ASTPENDING;
817066d836bSKonstantin Belousov 		thread_unlock(td);
818066d836bSKonstantin Belousov 	}
819066d836bSKonstantin Belousov 	error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
820066d836bSKonstantin Belousov 	return (error);
821066d836bSKonstantin Belousov }
822066d836bSKonstantin Belousov 
823d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
824df8bae1dSRodney W. Grimes struct select_args {
825b08f7993SSujal Patel 	int	nd;
826df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
827df8bae1dSRodney W. Grimes 	struct	timeval *tv;
828df8bae1dSRodney W. Grimes };
829d2d3e875SBruce Evans #endif
83026f9a767SRodney W. Grimes int
8318451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap)
832df8bae1dSRodney W. Grimes {
8338f19eb88SIan Dowse 	struct timeval tv, *tvp;
8348f19eb88SIan Dowse 	int error;
8358f19eb88SIan Dowse 
8368f19eb88SIan Dowse 	if (uap->tv != NULL) {
8378f19eb88SIan Dowse 		error = copyin(uap->tv, &tv, sizeof(tv));
8388f19eb88SIan Dowse 		if (error)
8398f19eb88SIan Dowse 			return (error);
8408f19eb88SIan Dowse 		tvp = &tv;
8418f19eb88SIan Dowse 	} else
8428f19eb88SIan Dowse 		tvp = NULL;
8438f19eb88SIan Dowse 
844b55ef216SKonstantin Belousov 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
845b55ef216SKonstantin Belousov 	    NFDBITS));
8468f19eb88SIan Dowse }
8478f19eb88SIan Dowse 
84856be1b9aSKonstantin Belousov /*
84956be1b9aSKonstantin Belousov  * In the unlikely case when user specified n greater then the last
85056be1b9aSKonstantin Belousov  * open file descriptor, check that no bits are set after the last
85156be1b9aSKonstantin Belousov  * valid fd.  We must return EBADF if any is set.
85256be1b9aSKonstantin Belousov  *
85356be1b9aSKonstantin Belousov  * There are applications that rely on the behaviour.
85456be1b9aSKonstantin Belousov  *
85556be1b9aSKonstantin Belousov  * nd is fd_lastfile + 1.
85656be1b9aSKonstantin Belousov  */
85756be1b9aSKonstantin Belousov static int
85856be1b9aSKonstantin Belousov select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits)
85956be1b9aSKonstantin Belousov {
86056be1b9aSKonstantin Belousov 	char *addr, *oaddr;
86156be1b9aSKonstantin Belousov 	int b, i, res;
86256be1b9aSKonstantin Belousov 	uint8_t bits;
86356be1b9aSKonstantin Belousov 
86456be1b9aSKonstantin Belousov 	if (nd >= ndu || fd_in == NULL)
86556be1b9aSKonstantin Belousov 		return (0);
86656be1b9aSKonstantin Belousov 
86756be1b9aSKonstantin Belousov 	oaddr = NULL;
86856be1b9aSKonstantin Belousov 	bits = 0; /* silence gcc */
86956be1b9aSKonstantin Belousov 	for (i = nd; i < ndu; i++) {
87056be1b9aSKonstantin Belousov 		b = i / NBBY;
87156be1b9aSKonstantin Belousov #if BYTE_ORDER == LITTLE_ENDIAN
87256be1b9aSKonstantin Belousov 		addr = (char *)fd_in + b;
87356be1b9aSKonstantin Belousov #else
87456be1b9aSKonstantin Belousov 		addr = (char *)fd_in;
87556be1b9aSKonstantin Belousov 		if (abi_nfdbits == NFDBITS) {
87656be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(fd_mask)) +
87756be1b9aSKonstantin Belousov 			    sizeof(fd_mask) - 1 - b % sizeof(fd_mask);
87856be1b9aSKonstantin Belousov 		} else {
87956be1b9aSKonstantin Belousov 			addr += rounddown(b, sizeof(uint32_t)) +
88056be1b9aSKonstantin Belousov 			    sizeof(uint32_t) - 1 - b % sizeof(uint32_t);
88156be1b9aSKonstantin Belousov 		}
88256be1b9aSKonstantin Belousov #endif
88356be1b9aSKonstantin Belousov 		if (addr != oaddr) {
88456be1b9aSKonstantin Belousov 			res = fubyte(addr);
88556be1b9aSKonstantin Belousov 			if (res == -1)
88656be1b9aSKonstantin Belousov 				return (EFAULT);
88756be1b9aSKonstantin Belousov 			oaddr = addr;
88856be1b9aSKonstantin Belousov 			bits = res;
88956be1b9aSKonstantin Belousov 		}
89056be1b9aSKonstantin Belousov 		if ((bits & (1 << (i % NBBY))) != 0)
89156be1b9aSKonstantin Belousov 			return (EBADF);
89256be1b9aSKonstantin Belousov 	}
89356be1b9aSKonstantin Belousov 	return (0);
89456be1b9aSKonstantin Belousov }
89556be1b9aSKonstantin Belousov 
8968f19eb88SIan Dowse int
8978f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
898b55ef216SKonstantin Belousov     fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
8998f19eb88SIan Dowse {
900426da3bcSAlfred Perlstein 	struct filedesc *fdp;
901d5e4d7e1SBruce Evans 	/*
902d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
903d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
904d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
905d5e4d7e1SBruce Evans 	 * of 256.
906d5e4d7e1SBruce Evans 	 */
907d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
908eb209311SAlfred Perlstein 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
90900af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
91056be1b9aSKonstantin Belousov 	int error, lf, ndu, timo;
911b55ef216SKonstantin Belousov 	u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
912df8bae1dSRodney W. Grimes 
9138f19eb88SIan Dowse 	if (nd < 0)
914acbfbfeaSSujal Patel 		return (EINVAL);
915426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
91656be1b9aSKonstantin Belousov 	ndu = nd;
91756be1b9aSKonstantin Belousov 	lf = fdp->fd_lastfile;
91856be1b9aSKonstantin Belousov 	if (nd > lf + 1)
91956be1b9aSKonstantin Belousov 		nd = lf + 1;
92056be1b9aSKonstantin Belousov 
92156be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits);
92256be1b9aSKonstantin Belousov 	if (error != 0)
92356be1b9aSKonstantin Belousov 		return (error);
92456be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits);
92556be1b9aSKonstantin Belousov 	if (error != 0)
92656be1b9aSKonstantin Belousov 		return (error);
92756be1b9aSKonstantin Belousov 	error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits);
92856be1b9aSKonstantin Belousov 	if (error != 0)
92956be1b9aSKonstantin Belousov 		return (error);
930b08f7993SSujal Patel 
931d5e4d7e1SBruce Evans 	/*
932d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
933d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
934d5e4d7e1SBruce Evans 	 */
9358f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
936d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
937b55ef216SKonstantin Belousov 	ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
938d5e4d7e1SBruce Evans 	nbufbytes = 0;
9398f19eb88SIan Dowse 	if (fd_in != NULL)
940d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
9418f19eb88SIan Dowse 	if (fd_ou != NULL)
942d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
9438f19eb88SIan Dowse 	if (fd_ex != NULL)
944d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
945d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
946d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
947d5e4d7e1SBruce Evans 	else
948a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
949b08f7993SSujal Patel 
950b08f7993SSujal Patel 	/*
951d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
952d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
953d5e4d7e1SBruce Evans 	 * together.
954b08f7993SSujal Patel 	 */
955d5e4d7e1SBruce Evans 	sbp = selbits;
956df8bae1dSRodney W. Grimes #define	getbits(name, x) \
957d5e4d7e1SBruce Evans 	do {								\
958841c0c7eSNathan Whitehorn 		if (name == NULL) {					\
959d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
960841c0c7eSNathan Whitehorn 			obits[x] = NULL;				\
961841c0c7eSNathan Whitehorn 		} else {						\
962d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
963d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
964d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
965b55ef216SKonstantin Belousov 			error = copyin(name, ibits[x], ncpubytes);	\
966265fc98fSSeigo Tanimura 			if (error != 0)					\
967ace8398dSJeff Roberson 				goto done;				\
968b55ef216SKonstantin Belousov 			bzero((char *)ibits[x] + ncpubytes,		\
969b55ef216SKonstantin Belousov 			    ncpbytes - ncpubytes);			\
970e04ac2feSJohn Baldwin 		}							\
971d5e4d7e1SBruce Evans 	} while (0)
9728f19eb88SIan Dowse 	getbits(fd_in, 0);
9738f19eb88SIan Dowse 	getbits(fd_ou, 1);
9748f19eb88SIan Dowse 	getbits(fd_ex, 2);
975df8bae1dSRodney W. Grimes #undef	getbits
976841c0c7eSNathan Whitehorn 
977841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
978841c0c7eSNathan Whitehorn 	/*
979841c0c7eSNathan Whitehorn 	 * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
980841c0c7eSNathan Whitehorn 	 * we are running under 32-bit emulation. This should be more
981841c0c7eSNathan Whitehorn 	 * generic.
982841c0c7eSNathan Whitehorn 	 */
983841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)						\
984841c0c7eSNathan Whitehorn 	if (abi_nfdbits != NFDBITS && bits != NULL) {			\
985841c0c7eSNathan Whitehorn 		int i;							\
986841c0c7eSNathan Whitehorn 		for (i = 0; i < ncpbytes / sizeof *sbp; i++)		\
987841c0c7eSNathan Whitehorn 			bits[i] = (bits[i] >> 32) | (bits[i] << 32);	\
988841c0c7eSNathan Whitehorn 	}
989841c0c7eSNathan Whitehorn #else
990841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)
991841c0c7eSNathan Whitehorn #endif
992841c0c7eSNathan Whitehorn 
993841c0c7eSNathan Whitehorn 	/* Make sure the bit order makes it through an ABI transition */
994841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[0]);
995841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[1]);
996841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[2]);
997841c0c7eSNathan Whitehorn 
998d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
999d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
1000df8bae1dSRodney W. Grimes 
10018f19eb88SIan Dowse 	if (tvp != NULL) {
10028f19eb88SIan Dowse 		atv = *tvp;
1003df8bae1dSRodney W. Grimes 		if (itimerfix(&atv)) {
1004df8bae1dSRodney W. Grimes 			error = EINVAL;
1005ace8398dSJeff Roberson 			goto done;
1006df8bae1dSRodney W. Grimes 		}
1007c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
100800af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
10099c386f6bSJohn Baldwin 	} else {
101000af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
10119c386f6bSJohn Baldwin 		atv.tv_usec = 0;
10129c386f6bSJohn Baldwin 	}
101300af9731SPoul-Henning Kamp 	timo = 0;
1014ace8398dSJeff Roberson 	seltdinit(td);
1015ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1016ace8398dSJeff Roberson 	for (;;) {
10178f19eb88SIan Dowse 		error = selscan(td, ibits, obits, nd);
1018ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1019ace8398dSJeff Roberson 			break;
10204da144c0SJohn Baldwin 		if (atv.tv_sec || atv.tv_usec) {
1021c21410e1SPoul-Henning Kamp 			getmicrouptime(&rtv);
102285f190e4SAlfred Perlstein 			if (timevalcmp(&rtv, &atv, >=))
1023ace8398dSJeff Roberson 				break;
102400af9731SPoul-Henning Kamp 			ttv = atv;
102500af9731SPoul-Henning Kamp 			timevalsub(&ttv, &rtv);
102600af9731SPoul-Henning Kamp 			timo = ttv.tv_sec > 24 * 60 * 60 ?
102700af9731SPoul-Henning Kamp 			    24 * 60 * 60 * hz : tvtohz(&ttv);
1028df8bae1dSRodney W. Grimes 		}
1029ace8398dSJeff Roberson 		error = seltdwait(td, timo);
1030ace8398dSJeff Roberson 		if (error)
1031ace8398dSJeff Roberson 			break;
1032ace8398dSJeff Roberson 		error = selrescan(td, ibits, obits);
1033ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1034ace8398dSJeff Roberson 			break;
103585f190e4SAlfred Perlstein 	}
1036ace8398dSJeff Roberson 	seltdclear(td);
1037265fc98fSSeigo Tanimura 
1038df8bae1dSRodney W. Grimes done:
1039df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
1040df8bae1dSRodney W. Grimes 	if (error == ERESTART)
1041df8bae1dSRodney W. Grimes 		error = EINTR;
1042df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
1043df8bae1dSRodney W. Grimes 		error = 0;
1044841c0c7eSNathan Whitehorn 
1045841c0c7eSNathan Whitehorn 	/* swizzle bit order back, if necessary */
1046841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[0]);
1047841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[1]);
1048841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[2]);
1049841c0c7eSNathan Whitehorn #undef swizzle_fdset
1050841c0c7eSNathan Whitehorn 
1051df8bae1dSRodney W. Grimes #define	putbits(name, x) \
1052b55ef216SKonstantin Belousov 	if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
1053df8bae1dSRodney W. Grimes 		error = error2;
1054df8bae1dSRodney W. Grimes 	if (error == 0) {
1055df8bae1dSRodney W. Grimes 		int error2;
1056df8bae1dSRodney W. Grimes 
10578f19eb88SIan Dowse 		putbits(fd_in, 0);
10588f19eb88SIan Dowse 		putbits(fd_ou, 1);
10598f19eb88SIan Dowse 		putbits(fd_ex, 2);
1060df8bae1dSRodney W. Grimes #undef putbits
1061df8bae1dSRodney W. Grimes 	}
1062d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
1063d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
1064ad2edad9SMatthew Dillon 
1065df8bae1dSRodney W. Grimes 	return (error);
1066df8bae1dSRodney W. Grimes }
106711b763dfSJeff Roberson /*
106811b763dfSJeff Roberson  * Convert a select bit set to poll flags.
1069748b9df6SJeff Roberson  *
107011b763dfSJeff Roberson  * The backend always returns POLLHUP/POLLERR if appropriate and we
107111b763dfSJeff Roberson  * return this as a set bit in any set.
107211b763dfSJeff Roberson  */
107311b763dfSJeff Roberson static int select_flags[3] = {
107411b763dfSJeff Roberson     POLLRDNORM | POLLHUP | POLLERR,
107511b763dfSJeff Roberson     POLLWRNORM | POLLHUP | POLLERR,
107661e53a38SKonstantin Belousov     POLLRDBAND | POLLERR
107711b763dfSJeff Roberson };
107811b763dfSJeff Roberson 
107911b763dfSJeff Roberson /*
108011b763dfSJeff Roberson  * Compute the fo_poll flags required for a fd given by the index and
108111b763dfSJeff Roberson  * bit position in the fd_mask array.
108211b763dfSJeff Roberson  */
108311b763dfSJeff Roberson static __inline int
108460b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit)
108511b763dfSJeff Roberson {
108611b763dfSJeff Roberson 	int flags;
108711b763dfSJeff Roberson 	int msk;
108811b763dfSJeff Roberson 
108911b763dfSJeff Roberson 	flags = 0;
109011b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
109111b763dfSJeff Roberson 		if (ibits[msk] == NULL)
109211b763dfSJeff Roberson 			continue;
109360b7f468SStephane E. Potvin 		if ((ibits[msk][idx] & bit) == 0)
109411b763dfSJeff Roberson 			continue;
109511b763dfSJeff Roberson 		flags |= select_flags[msk];
109611b763dfSJeff Roberson 	}
109711b763dfSJeff Roberson 	return (flags);
109811b763dfSJeff Roberson }
109911b763dfSJeff Roberson 
110011b763dfSJeff Roberson /*
110111b763dfSJeff Roberson  * Set the appropriate output bits given a mask of fired events and the
110211b763dfSJeff Roberson  * input bits originally requested.
110311b763dfSJeff Roberson  */
110411b763dfSJeff Roberson static __inline int
110511b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
110611b763dfSJeff Roberson {
110711b763dfSJeff Roberson 	int msk;
110811b763dfSJeff Roberson 	int n;
110911b763dfSJeff Roberson 
111011b763dfSJeff Roberson 	n = 0;
111111b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
111211b763dfSJeff Roberson 		if ((events & select_flags[msk]) == 0)
111311b763dfSJeff Roberson 			continue;
111411b763dfSJeff Roberson 		if (ibits[msk] == NULL)
111511b763dfSJeff Roberson 			continue;
111611b763dfSJeff Roberson 		if ((ibits[msk][idx] & bit) == 0)
111711b763dfSJeff Roberson 			continue;
111811b763dfSJeff Roberson 		/*
111911b763dfSJeff Roberson 		 * XXX Check for a duplicate set.  This can occur because a
112011b763dfSJeff Roberson 		 * socket calls selrecord() twice for each poll() call
112111b763dfSJeff Roberson 		 * resulting in two selfds per real fd.  selrescan() will
112211b763dfSJeff Roberson 		 * call selsetbits twice as a result.
112311b763dfSJeff Roberson 		 */
112411b763dfSJeff Roberson 		if ((obits[msk][idx] & bit) != 0)
112511b763dfSJeff Roberson 			continue;
112611b763dfSJeff Roberson 		obits[msk][idx] |= bit;
112711b763dfSJeff Roberson 		n++;
112811b763dfSJeff Roberson 	}
112911b763dfSJeff Roberson 
113011b763dfSJeff Roberson 	return (n);
113111b763dfSJeff Roberson }
1132df8bae1dSRodney W. Grimes 
1133a9d2f8d8SRobert Watson static __inline int
1134a9d2f8d8SRobert Watson getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp)
1135a9d2f8d8SRobert Watson {
1136a9d2f8d8SRobert Watson 	struct file *fp;
1137a9d2f8d8SRobert Watson #ifdef CAPABILITIES
1138a9d2f8d8SRobert Watson 	struct file *fp_fromcap;
1139a9d2f8d8SRobert Watson 	int error;
1140a9d2f8d8SRobert Watson #endif
1141a9d2f8d8SRobert Watson 
1142a9d2f8d8SRobert Watson 	if ((fp = fget_unlocked(fdp, fd)) == NULL)
1143a9d2f8d8SRobert Watson 		return (EBADF);
1144a9d2f8d8SRobert Watson #ifdef CAPABILITIES
1145a9d2f8d8SRobert Watson 	/*
1146a9d2f8d8SRobert Watson 	 * If the file descriptor is for a capability, test rights and use
1147a9d2f8d8SRobert Watson 	 * the file descriptor references by the capability.
1148a9d2f8d8SRobert Watson 	 */
1149d1b6899eSJonathan Anderson 	error = cap_funwrap(fp, CAP_POLL_EVENT, &fp_fromcap);
1150a9d2f8d8SRobert Watson 	if (error) {
1151a9d2f8d8SRobert Watson 		fdrop(fp, curthread);
1152a9d2f8d8SRobert Watson 		return (error);
1153a9d2f8d8SRobert Watson 	}
1154a9d2f8d8SRobert Watson 	if (fp != fp_fromcap) {
1155a9d2f8d8SRobert Watson 		fhold(fp_fromcap);
1156a9d2f8d8SRobert Watson 		fdrop(fp, curthread);
1157a9d2f8d8SRobert Watson 		fp = fp_fromcap;
1158a9d2f8d8SRobert Watson 	}
1159a9d2f8d8SRobert Watson #endif /* CAPABILITIES */
1160a9d2f8d8SRobert Watson 	*fpp = fp;
1161a9d2f8d8SRobert Watson 	return (0);
1162a9d2f8d8SRobert Watson }
1163a9d2f8d8SRobert Watson 
1164ace8398dSJeff Roberson /*
1165ace8398dSJeff Roberson  * Traverse the list of fds attached to this thread's seltd and check for
1166ace8398dSJeff Roberson  * completion.
1167ace8398dSJeff Roberson  */
1168ace8398dSJeff Roberson static int
1169ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
1170ace8398dSJeff Roberson {
117111b763dfSJeff Roberson 	struct filedesc *fdp;
117211b763dfSJeff Roberson 	struct selinfo *si;
1173ace8398dSJeff Roberson 	struct seltd *stp;
1174ace8398dSJeff Roberson 	struct selfd *sfp;
1175ace8398dSJeff Roberson 	struct selfd *sfn;
1176ace8398dSJeff Roberson 	struct file *fp;
11779cdacff1SJeff Roberson 	fd_mask bit;
11789cdacff1SJeff Roberson 	int fd, ev, n, idx;
1179a9d2f8d8SRobert Watson 	int error;
1180ace8398dSJeff Roberson 
118111b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
1182ace8398dSJeff Roberson 	stp = td->td_sel;
118311b763dfSJeff Roberson 	n = 0;
1184ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1185ace8398dSJeff Roberson 		fd = (int)(uintptr_t)sfp->sf_cookie;
1186ace8398dSJeff Roberson 		si = sfp->sf_si;
1187ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1188ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1189ace8398dSJeff Roberson 		if (si != NULL)
1190ace8398dSJeff Roberson 			continue;
1191a9d2f8d8SRobert Watson 		error = getselfd_cap(fdp, fd, &fp);
1192a9d2f8d8SRobert Watson 		if (error)
1193a9d2f8d8SRobert Watson 			return (error);
119411b763dfSJeff Roberson 		idx = fd / NFDBITS;
11959cdacff1SJeff Roberson 		bit = (fd_mask)1 << (fd % NFDBITS);
119611b763dfSJeff Roberson 		ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
1197bf422e5fSJeff Roberson 		fdrop(fp, td);
119811b763dfSJeff Roberson 		if (ev != 0)
119911b763dfSJeff Roberson 			n += selsetbits(ibits, obits, idx, bit, ev);
1200ace8398dSJeff Roberson 	}
1201ace8398dSJeff Roberson 	stp->st_flags = 0;
1202ace8398dSJeff Roberson 	td->td_retval[0] = n;
1203ace8398dSJeff Roberson 	return (0);
1204ace8398dSJeff Roberson }
1205ace8398dSJeff Roberson 
1206ace8398dSJeff Roberson /*
1207ace8398dSJeff Roberson  * Perform the initial filedescriptor scan and register ourselves with
1208ace8398dSJeff Roberson  * each selinfo.
1209ace8398dSJeff Roberson  */
1210265fc98fSSeigo Tanimura static int
1211b40ce416SJulian Elischer selscan(td, ibits, obits, nfd)
1212b40ce416SJulian Elischer 	struct thread *td;
1213b08f7993SSujal Patel 	fd_mask **ibits, **obits;
1214cb226aaaSPoul-Henning Kamp 	int nfd;
1215df8bae1dSRodney W. Grimes {
121611b763dfSJeff Roberson 	struct filedesc *fdp;
1217df8bae1dSRodney W. Grimes 	struct file *fp;
12189cdacff1SJeff Roberson 	fd_mask bit;
121911b763dfSJeff Roberson 	int ev, flags, end, fd;
12209cdacff1SJeff Roberson 	int n, idx;
1221a9d2f8d8SRobert Watson 	int error;
1222df8bae1dSRodney W. Grimes 
122311b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
122411b763dfSJeff Roberson 	n = 0;
12259cdacff1SJeff Roberson 	for (idx = 0, fd = 0; fd < nfd; idx++) {
122611b763dfSJeff Roberson 		end = imin(fd + NFDBITS, nfd);
122711b763dfSJeff Roberson 		for (bit = 1; fd < end; bit <<= 1, fd++) {
122811b763dfSJeff Roberson 			/* Compute the list of events we're interested in. */
122911b763dfSJeff Roberson 			flags = selflags(ibits, idx, bit);
123011b763dfSJeff Roberson 			if (flags == 0)
1231f082218cSPeter Wemm 				continue;
1232a9d2f8d8SRobert Watson 			error = getselfd_cap(fdp, fd, &fp);
1233a9d2f8d8SRobert Watson 			if (error)
1234a9d2f8d8SRobert Watson 				return (error);
1235ace8398dSJeff Roberson 			selfdalloc(td, (void *)(uintptr_t)fd);
123611b763dfSJeff Roberson 			ev = fo_poll(fp, flags, td->td_ucred, td);
1237bf422e5fSJeff Roberson 			fdrop(fp, td);
123811b763dfSJeff Roberson 			if (ev != 0)
123911b763dfSJeff Roberson 				n += selsetbits(ibits, obits, idx, bit, ev);
1240df8bae1dSRodney W. Grimes 		}
1241df8bae1dSRodney W. Grimes 	}
124211b763dfSJeff Roberson 
1243b40ce416SJulian Elischer 	td->td_retval[0] = n;
1244df8bae1dSRodney W. Grimes 	return (0);
1245df8bae1dSRodney W. Grimes }
1246df8bae1dSRodney W. Grimes 
124742d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
124842d11757SPeter Wemm struct poll_args {
124942d11757SPeter Wemm 	struct pollfd *fds;
125042d11757SPeter Wemm 	u_int	nfds;
125142d11757SPeter Wemm 	int	timeout;
125242d11757SPeter Wemm };
125342d11757SPeter Wemm #endif
125442d11757SPeter Wemm int
12558451d0ddSKip Macy sys_poll(td, uap)
1256b40ce416SJulian Elischer 	struct thread *td;
1257ea0237edSJonathan Lemon 	struct poll_args *uap;
125842d11757SPeter Wemm {
12592580f4e5SAndre Oppermann 	struct pollfd *bits;
12602580f4e5SAndre Oppermann 	struct pollfd smallbits[32];
126100af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
12629ae6d334SKelly Yancey 	int error = 0, timo;
1263ace8398dSJeff Roberson 	u_int nfds;
126442d11757SPeter Wemm 	size_t ni;
126542d11757SPeter Wemm 
1266d1e405c5SAlfred Perlstein 	nfds = uap->nfds;
1267374ae2a3SJeff Roberson 	if (nfds > maxfilesperproc && nfds > FD_SETSIZE)
1268ace8398dSJeff Roberson 		return (EINVAL);
126989b71647SPeter Wemm 	ni = nfds * sizeof(struct pollfd);
127042d11757SPeter Wemm 	if (ni > sizeof(smallbits))
1271a163d034SWarner Losh 		bits = malloc(ni, M_TEMP, M_WAITOK);
127242d11757SPeter Wemm 	else
127342d11757SPeter Wemm 		bits = smallbits;
1274d1e405c5SAlfred Perlstein 	error = copyin(uap->fds, bits, ni);
127542d11757SPeter Wemm 	if (error)
1276ace8398dSJeff Roberson 		goto done;
1277d1e405c5SAlfred Perlstein 	if (uap->timeout != INFTIM) {
1278d1e405c5SAlfred Perlstein 		atv.tv_sec = uap->timeout / 1000;
1279d1e405c5SAlfred Perlstein 		atv.tv_usec = (uap->timeout % 1000) * 1000;
128042d11757SPeter Wemm 		if (itimerfix(&atv)) {
128142d11757SPeter Wemm 			error = EINVAL;
1282ace8398dSJeff Roberson 			goto done;
128342d11757SPeter Wemm 		}
1284c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
128500af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
12869c386f6bSJohn Baldwin 	} else {
128700af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
12889c386f6bSJohn Baldwin 		atv.tv_usec = 0;
12899c386f6bSJohn Baldwin 	}
129000af9731SPoul-Henning Kamp 	timo = 0;
1291ace8398dSJeff Roberson 	seltdinit(td);
1292ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1293ace8398dSJeff Roberson 	for (;;) {
12942580f4e5SAndre Oppermann 		error = pollscan(td, bits, nfds);
1295ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1296ace8398dSJeff Roberson 			break;
12974da144c0SJohn Baldwin 		if (atv.tv_sec || atv.tv_usec) {
1298c21410e1SPoul-Henning Kamp 			getmicrouptime(&rtv);
129985f190e4SAlfred Perlstein 			if (timevalcmp(&rtv, &atv, >=))
1300ace8398dSJeff Roberson 				break;
130100af9731SPoul-Henning Kamp 			ttv = atv;
130200af9731SPoul-Henning Kamp 			timevalsub(&ttv, &rtv);
130300af9731SPoul-Henning Kamp 			timo = ttv.tv_sec > 24 * 60 * 60 ?
130400af9731SPoul-Henning Kamp 			    24 * 60 * 60 * hz : tvtohz(&ttv);
130542d11757SPeter Wemm 		}
1306ace8398dSJeff Roberson 		error = seltdwait(td, timo);
1307ace8398dSJeff Roberson 		if (error)
1308ace8398dSJeff Roberson 			break;
1309ace8398dSJeff Roberson 		error = pollrescan(td);
1310ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1311ace8398dSJeff Roberson 			break;
131285f190e4SAlfred Perlstein 	}
1313ace8398dSJeff Roberson 	seltdclear(td);
1314265fc98fSSeigo Tanimura 
131542d11757SPeter Wemm done:
131642d11757SPeter Wemm 	/* poll is not restarted after signals... */
131742d11757SPeter Wemm 	if (error == ERESTART)
131842d11757SPeter Wemm 		error = EINTR;
131942d11757SPeter Wemm 	if (error == EWOULDBLOCK)
132042d11757SPeter Wemm 		error = 0;
132142d11757SPeter Wemm 	if (error == 0) {
13226d8feddaSKonstantin Belousov 		error = pollout(td, bits, uap->fds, nfds);
132342d11757SPeter Wemm 		if (error)
132442d11757SPeter Wemm 			goto out;
132542d11757SPeter Wemm 	}
132642d11757SPeter Wemm out:
132742d11757SPeter Wemm 	if (ni > sizeof(smallbits))
132842d11757SPeter Wemm 		free(bits, M_TEMP);
132942d11757SPeter Wemm 	return (error);
133042d11757SPeter Wemm }
133142d11757SPeter Wemm 
133242d11757SPeter Wemm static int
1333ace8398dSJeff Roberson pollrescan(struct thread *td)
1334ace8398dSJeff Roberson {
1335ace8398dSJeff Roberson 	struct seltd *stp;
1336ace8398dSJeff Roberson 	struct selfd *sfp;
1337ace8398dSJeff Roberson 	struct selfd *sfn;
1338ace8398dSJeff Roberson 	struct selinfo *si;
1339ace8398dSJeff Roberson 	struct filedesc *fdp;
1340ace8398dSJeff Roberson 	struct file *fp;
1341ace8398dSJeff Roberson 	struct pollfd *fd;
1342ace8398dSJeff Roberson 	int n;
1343ace8398dSJeff Roberson 
1344ace8398dSJeff Roberson 	n = 0;
1345ace8398dSJeff Roberson 	fdp = td->td_proc->p_fd;
1346ace8398dSJeff Roberson 	stp = td->td_sel;
1347ace8398dSJeff Roberson 	FILEDESC_SLOCK(fdp);
1348ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1349ace8398dSJeff Roberson 		fd = (struct pollfd *)sfp->sf_cookie;
1350ace8398dSJeff Roberson 		si = sfp->sf_si;
1351ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1352ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1353ace8398dSJeff Roberson 		if (si != NULL)
1354ace8398dSJeff Roberson 			continue;
1355ace8398dSJeff Roberson 		fp = fdp->fd_ofiles[fd->fd];
1356d6f72489SJonathan Anderson #ifdef CAPABILITIES
1357d6f72489SJonathan Anderson 		if ((fp == NULL)
1358d6f72489SJonathan Anderson 		    || (cap_funwrap(fp, CAP_POLL_EVENT, &fp) != 0)) {
1359d6f72489SJonathan Anderson #else
1360ace8398dSJeff Roberson 		if (fp == NULL) {
1361d6f72489SJonathan Anderson #endif
1362ace8398dSJeff Roberson 			fd->revents = POLLNVAL;
1363ace8398dSJeff Roberson 			n++;
1364ace8398dSJeff Roberson 			continue;
1365ace8398dSJeff Roberson 		}
1366d6f72489SJonathan Anderson 
1367ace8398dSJeff Roberson 		/*
1368ace8398dSJeff Roberson 		 * Note: backend also returns POLLHUP and
1369ace8398dSJeff Roberson 		 * POLLERR if appropriate.
1370ace8398dSJeff Roberson 		 */
1371ace8398dSJeff Roberson 		fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1372ace8398dSJeff Roberson 		if (fd->revents != 0)
1373ace8398dSJeff Roberson 			n++;
1374ace8398dSJeff Roberson 	}
1375ace8398dSJeff Roberson 	FILEDESC_SUNLOCK(fdp);
1376ace8398dSJeff Roberson 	stp->st_flags = 0;
1377ace8398dSJeff Roberson 	td->td_retval[0] = n;
1378ace8398dSJeff Roberson 	return (0);
1379ace8398dSJeff Roberson }
1380ace8398dSJeff Roberson 
1381ace8398dSJeff Roberson 
1382ace8398dSJeff Roberson static int
13836d8feddaSKonstantin Belousov pollout(td, fds, ufds, nfd)
13846d8feddaSKonstantin Belousov 	struct thread *td;
1385ae81968fSRobert Watson 	struct pollfd *fds;
1386ae81968fSRobert Watson 	struct pollfd *ufds;
1387ae81968fSRobert Watson 	u_int nfd;
1388ae81968fSRobert Watson {
1389ae81968fSRobert Watson 	int error = 0;
1390ae81968fSRobert Watson 	u_int i = 0;
13916d8feddaSKonstantin Belousov 	u_int n = 0;
1392ae81968fSRobert Watson 
1393ae81968fSRobert Watson 	for (i = 0; i < nfd; i++) {
1394ae81968fSRobert Watson 		error = copyout(&fds->revents, &ufds->revents,
1395ae81968fSRobert Watson 		    sizeof(ufds->revents));
1396ae81968fSRobert Watson 		if (error)
1397ae81968fSRobert Watson 			return (error);
13986d8feddaSKonstantin Belousov 		if (fds->revents != 0)
13996d8feddaSKonstantin Belousov 			n++;
1400ae81968fSRobert Watson 		fds++;
1401ae81968fSRobert Watson 		ufds++;
1402ae81968fSRobert Watson 	}
14036d8feddaSKonstantin Belousov 	td->td_retval[0] = n;
1404ae81968fSRobert Watson 	return (0);
1405ae81968fSRobert Watson }
1406ae81968fSRobert Watson 
1407ae81968fSRobert Watson static int
1408b40ce416SJulian Elischer pollscan(td, fds, nfd)
1409b40ce416SJulian Elischer 	struct thread *td;
141042d11757SPeter Wemm 	struct pollfd *fds;
1411ea0237edSJonathan Lemon 	u_int nfd;
141242d11757SPeter Wemm {
1413ace8398dSJeff Roberson 	struct filedesc *fdp = td->td_proc->p_fd;
141442d11757SPeter Wemm 	int i;
141542d11757SPeter Wemm 	struct file *fp;
141642d11757SPeter Wemm 	int n = 0;
141742d11757SPeter Wemm 
14185e3f7694SRobert Watson 	FILEDESC_SLOCK(fdp);
1419eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
1420337c9691SJordan K. Hubbard 		if (fds->fd >= fdp->fd_nfiles) {
142142d11757SPeter Wemm 			fds->revents = POLLNVAL;
142242d11757SPeter Wemm 			n++;
1423337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
1424337c9691SJordan K. Hubbard 			fds->revents = 0;
142542d11757SPeter Wemm 		} else {
142642d11757SPeter Wemm 			fp = fdp->fd_ofiles[fds->fd];
1427d6f72489SJonathan Anderson #ifdef CAPABILITIES
1428d6f72489SJonathan Anderson 			if ((fp == NULL)
1429d6f72489SJonathan Anderson 			    || (cap_funwrap(fp, CAP_POLL_EVENT, &fp) != 0)) {
1430d6f72489SJonathan Anderson #else
1431279d7226SMatthew Dillon 			if (fp == NULL) {
1432d6f72489SJonathan Anderson #endif
143342d11757SPeter Wemm 				fds->revents = POLLNVAL;
143442d11757SPeter Wemm 				n++;
143542d11757SPeter Wemm 			} else {
14362087c896SBruce Evans 				/*
14372087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
14382087c896SBruce Evans 				 * POLLERR if appropriate.
14392087c896SBruce Evans 				 */
1440ace8398dSJeff Roberson 				selfdalloc(td, fds);
144113ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
1442ea6027a8SRobert Watson 				    td->td_ucred, td);
1443f2159cc7SKonstantin Belousov 				/*
1444f2159cc7SKonstantin Belousov 				 * POSIX requires POLLOUT to be never
1445f2159cc7SKonstantin Belousov 				 * set simultaneously with POLLHUP.
1446f2159cc7SKonstantin Belousov 				 */
1447f2159cc7SKonstantin Belousov 				if ((fds->revents & POLLHUP) != 0)
1448f2159cc7SKonstantin Belousov 					fds->revents &= ~POLLOUT;
1449f2159cc7SKonstantin Belousov 
145042d11757SPeter Wemm 				if (fds->revents != 0)
145142d11757SPeter Wemm 					n++;
145242d11757SPeter Wemm 			}
145342d11757SPeter Wemm 		}
145442d11757SPeter Wemm 	}
14555e3f7694SRobert Watson 	FILEDESC_SUNLOCK(fdp);
1456b40ce416SJulian Elischer 	td->td_retval[0] = n;
145742d11757SPeter Wemm 	return (0);
145842d11757SPeter Wemm }
145942d11757SPeter Wemm 
146042d11757SPeter Wemm /*
146142d11757SPeter Wemm  * OpenBSD poll system call.
14620c14ff0eSRobert Watson  *
146342d11757SPeter Wemm  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
146442d11757SPeter Wemm  */
146542d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
146642d11757SPeter Wemm struct openbsd_poll_args {
146742d11757SPeter Wemm 	struct pollfd *fds;
146842d11757SPeter Wemm 	u_int	nfds;
146942d11757SPeter Wemm 	int	timeout;
147042d11757SPeter Wemm };
147142d11757SPeter Wemm #endif
147242d11757SPeter Wemm int
14738451d0ddSKip Macy sys_openbsd_poll(td, uap)
1474b40ce416SJulian Elischer 	register struct thread *td;
147542d11757SPeter Wemm 	register struct openbsd_poll_args *uap;
147642d11757SPeter Wemm {
14778451d0ddSKip Macy 	return (sys_poll(td, (struct poll_args *)uap));
147842d11757SPeter Wemm }
147942d11757SPeter Wemm 
148085f190e4SAlfred Perlstein /*
1481ace8398dSJeff Roberson  * XXX This was created specifically to support netncp and netsmb.  This
1482ace8398dSJeff Roberson  * allows the caller to specify a socket to wait for events on.  It returns
1483ace8398dSJeff Roberson  * 0 if any events matched and an error otherwise.  There is no way to
1484ace8398dSJeff Roberson  * determine which events fired.
148585f190e4SAlfred Perlstein  */
1486ace8398dSJeff Roberson int
1487ace8398dSJeff Roberson selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td)
148885f190e4SAlfred Perlstein {
1489ace8398dSJeff Roberson 	struct timeval atv, rtv, ttv;
1490ace8398dSJeff Roberson 	int error, timo;
149185f190e4SAlfred Perlstein 
1492ace8398dSJeff Roberson 	if (tvp != NULL) {
1493ace8398dSJeff Roberson 		atv = *tvp;
1494ace8398dSJeff Roberson 		if (itimerfix(&atv))
1495ace8398dSJeff Roberson 			return (EINVAL);
1496ace8398dSJeff Roberson 		getmicrouptime(&rtv);
1497ace8398dSJeff Roberson 		timevaladd(&atv, &rtv);
1498ace8398dSJeff Roberson 	} else {
1499ace8398dSJeff Roberson 		atv.tv_sec = 0;
1500ace8398dSJeff Roberson 		atv.tv_usec = 0;
1501ace8398dSJeff Roberson 	}
1502ace8398dSJeff Roberson 
1503ace8398dSJeff Roberson 	timo = 0;
1504ace8398dSJeff Roberson 	seltdinit(td);
1505ace8398dSJeff Roberson 	/*
1506ace8398dSJeff Roberson 	 * Iterate until the timeout expires or the socket becomes ready.
1507ace8398dSJeff Roberson 	 */
1508ace8398dSJeff Roberson 	for (;;) {
1509ace8398dSJeff Roberson 		selfdalloc(td, NULL);
1510ace8398dSJeff Roberson 		error = sopoll(so, events, NULL, td);
1511ace8398dSJeff Roberson 		/* error here is actually the ready events. */
1512ace8398dSJeff Roberson 		if (error)
1513ace8398dSJeff Roberson 			return (0);
1514ace8398dSJeff Roberson 		if (atv.tv_sec || atv.tv_usec) {
1515ace8398dSJeff Roberson 			getmicrouptime(&rtv);
1516ace8398dSJeff Roberson 			if (timevalcmp(&rtv, &atv, >=)) {
1517ace8398dSJeff Roberson 				seltdclear(td);
1518ace8398dSJeff Roberson 				return (EWOULDBLOCK);
1519ace8398dSJeff Roberson 			}
1520ace8398dSJeff Roberson 			ttv = atv;
1521ace8398dSJeff Roberson 			timevalsub(&ttv, &rtv);
1522ace8398dSJeff Roberson 			timo = ttv.tv_sec > 24 * 60 * 60 ?
1523ace8398dSJeff Roberson 			    24 * 60 * 60 * hz : tvtohz(&ttv);
1524ace8398dSJeff Roberson 		}
1525ace8398dSJeff Roberson 		error = seltdwait(td, timo);
1526ace8398dSJeff Roberson 		seltdclear(td);
1527ace8398dSJeff Roberson 		if (error)
1528ace8398dSJeff Roberson 			break;
1529ace8398dSJeff Roberson 	}
1530ace8398dSJeff Roberson 	/* XXX Duplicates ncp/smb behavior. */
1531ace8398dSJeff Roberson 	if (error == ERESTART)
1532ace8398dSJeff Roberson 		error = 0;
1533ace8398dSJeff Roberson 	return (error);
1534ace8398dSJeff Roberson }
1535ace8398dSJeff Roberson 
1536ace8398dSJeff Roberson /*
1537ace8398dSJeff Roberson  * Preallocate two selfds associated with 'cookie'.  Some fo_poll routines
1538ace8398dSJeff Roberson  * have two select sets, one for read and another for write.
1539ace8398dSJeff Roberson  */
1540ace8398dSJeff Roberson static void
1541ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie)
1542ace8398dSJeff Roberson {
1543ace8398dSJeff Roberson 	struct seltd *stp;
1544ace8398dSJeff Roberson 
1545ace8398dSJeff Roberson 	stp = td->td_sel;
1546ace8398dSJeff Roberson 	if (stp->st_free1 == NULL)
1547ace8398dSJeff Roberson 		stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1548ace8398dSJeff Roberson 	stp->st_free1->sf_td = stp;
1549ace8398dSJeff Roberson 	stp->st_free1->sf_cookie = cookie;
1550ace8398dSJeff Roberson 	if (stp->st_free2 == NULL)
1551ace8398dSJeff Roberson 		stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1552ace8398dSJeff Roberson 	stp->st_free2->sf_td = stp;
1553ace8398dSJeff Roberson 	stp->st_free2->sf_cookie = cookie;
1554ace8398dSJeff Roberson }
1555ace8398dSJeff Roberson 
1556ace8398dSJeff Roberson static void
1557ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp)
1558ace8398dSJeff Roberson {
1559ace8398dSJeff Roberson 	STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
1560ace8398dSJeff Roberson 	mtx_lock(sfp->sf_mtx);
1561ace8398dSJeff Roberson 	if (sfp->sf_si)
1562ace8398dSJeff Roberson 		TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1563ace8398dSJeff Roberson 	mtx_unlock(sfp->sf_mtx);
1564ace8398dSJeff Roberson 	uma_zfree(selfd_zone, sfp);
156585f190e4SAlfred Perlstein }
156685f190e4SAlfred Perlstein 
15676aba400aSAttilio Rao /* Drain the waiters tied to all the selfd belonging the specified selinfo. */
15686aba400aSAttilio Rao void
15696aba400aSAttilio Rao seldrain(sip)
15706aba400aSAttilio Rao         struct selinfo *sip;
15716aba400aSAttilio Rao {
15726aba400aSAttilio Rao 
15736aba400aSAttilio Rao 	/*
15746aba400aSAttilio Rao 	 * This feature is already provided by doselwakeup(), thus it is
15756aba400aSAttilio Rao 	 * enough to go for it.
15766aba400aSAttilio Rao 	 * Eventually, the context, should take care to avoid races
15776aba400aSAttilio Rao 	 * between thread calling select()/poll() and file descriptor
15786aba400aSAttilio Rao 	 * detaching, but, again, the races are just the same as
15796aba400aSAttilio Rao 	 * selwakeup().
15806aba400aSAttilio Rao 	 */
15816aba400aSAttilio Rao         doselwakeup(sip, -1);
15826aba400aSAttilio Rao }
15836aba400aSAttilio Rao 
1584df8bae1dSRodney W. Grimes /*
1585df8bae1dSRodney W. Grimes  * Record a select request.
1586df8bae1dSRodney W. Grimes  */
1587df8bae1dSRodney W. Grimes void
1588df8bae1dSRodney W. Grimes selrecord(selector, sip)
1589b40ce416SJulian Elischer 	struct thread *selector;
1590df8bae1dSRodney W. Grimes 	struct selinfo *sip;
1591df8bae1dSRodney W. Grimes {
1592ace8398dSJeff Roberson 	struct selfd *sfp;
1593ace8398dSJeff Roberson 	struct seltd *stp;
1594ace8398dSJeff Roberson 	struct mtx *mtxp;
1595df8bae1dSRodney W. Grimes 
1596ace8398dSJeff Roberson 	stp = selector->td_sel;
159785f190e4SAlfred Perlstein 	/*
1598ace8398dSJeff Roberson 	 * Don't record when doing a rescan.
159985f190e4SAlfred Perlstein 	 */
1600ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_RESCAN)
1601ace8398dSJeff Roberson 		return;
1602ace8398dSJeff Roberson 	/*
1603ace8398dSJeff Roberson 	 * Grab one of the preallocated descriptors.
1604ace8398dSJeff Roberson 	 */
1605ace8398dSJeff Roberson 	sfp = NULL;
1606ace8398dSJeff Roberson 	if ((sfp = stp->st_free1) != NULL)
1607ace8398dSJeff Roberson 		stp->st_free1 = NULL;
1608ace8398dSJeff Roberson 	else if ((sfp = stp->st_free2) != NULL)
1609ace8398dSJeff Roberson 		stp->st_free2 = NULL;
1610ace8398dSJeff Roberson 	else
1611ace8398dSJeff Roberson 		panic("selrecord: No free selfd on selq");
16122141453eSJeff Roberson 	mtxp = sip->si_mtx;
16132141453eSJeff Roberson 	if (mtxp == NULL)
16142141453eSJeff Roberson 		mtxp = mtx_pool_find(mtxpool_select, sip);
1615ace8398dSJeff Roberson 	/*
1616ace8398dSJeff Roberson 	 * Initialize the sfp and queue it in the thread.
1617ace8398dSJeff Roberson 	 */
1618ace8398dSJeff Roberson 	sfp->sf_si = sip;
1619ace8398dSJeff Roberson 	sfp->sf_mtx = mtxp;
1620ace8398dSJeff Roberson 	STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1621ace8398dSJeff Roberson 	/*
1622ace8398dSJeff Roberson 	 * Now that we've locked the sip, check for initialization.
1623ace8398dSJeff Roberson 	 */
1624ace8398dSJeff Roberson 	mtx_lock(mtxp);
1625ace8398dSJeff Roberson 	if (sip->si_mtx == NULL) {
1626ace8398dSJeff Roberson 		sip->si_mtx = mtxp;
1627ace8398dSJeff Roberson 		TAILQ_INIT(&sip->si_tdlist);
162885f190e4SAlfred Perlstein 	}
1629ace8398dSJeff Roberson 	/*
1630ace8398dSJeff Roberson 	 * Add this thread to the list of selfds listening on this selinfo.
1631ace8398dSJeff Roberson 	 */
1632ace8398dSJeff Roberson 	TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1633ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1634df8bae1dSRodney W. Grimes }
1635df8bae1dSRodney W. Grimes 
1636512824f8SSeigo Tanimura /* Wake up a selecting thread. */
1637df8bae1dSRodney W. Grimes void
1638df8bae1dSRodney W. Grimes selwakeup(sip)
163985f190e4SAlfred Perlstein 	struct selinfo *sip;
1640df8bae1dSRodney W. Grimes {
1641512824f8SSeigo Tanimura 	doselwakeup(sip, -1);
1642512824f8SSeigo Tanimura }
1643512824f8SSeigo Tanimura 
1644512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */
1645512824f8SSeigo Tanimura void
1646512824f8SSeigo Tanimura selwakeuppri(sip, pri)
1647512824f8SSeigo Tanimura 	struct selinfo *sip;
1648512824f8SSeigo Tanimura 	int pri;
1649512824f8SSeigo Tanimura {
1650512824f8SSeigo Tanimura 	doselwakeup(sip, pri);
1651512824f8SSeigo Tanimura }
1652512824f8SSeigo Tanimura 
1653512824f8SSeigo Tanimura /*
1654512824f8SSeigo Tanimura  * Do a wakeup when a selectable event occurs.
1655512824f8SSeigo Tanimura  */
1656512824f8SSeigo Tanimura static void
1657512824f8SSeigo Tanimura doselwakeup(sip, pri)
1658512824f8SSeigo Tanimura 	struct selinfo *sip;
1659512824f8SSeigo Tanimura 	int pri;
1660512824f8SSeigo Tanimura {
1661ace8398dSJeff Roberson 	struct selfd *sfp;
1662ace8398dSJeff Roberson 	struct selfd *sfn;
1663ace8398dSJeff Roberson 	struct seltd *stp;
1664df8bae1dSRodney W. Grimes 
1665ace8398dSJeff Roberson 	/* If it's not initialized there can't be any waiters. */
1666ace8398dSJeff Roberson 	if (sip->si_mtx == NULL)
1667b40ce416SJulian Elischer 		return;
1668ace8398dSJeff Roberson 	/*
1669ace8398dSJeff Roberson 	 * Locking the selinfo locks all selfds associated with it.
1670ace8398dSJeff Roberson 	 */
1671ace8398dSJeff Roberson 	mtx_lock(sip->si_mtx);
1672ace8398dSJeff Roberson 	TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1673ace8398dSJeff Roberson 		/*
1674ace8398dSJeff Roberson 		 * Once we remove this sfp from the list and clear the
1675ace8398dSJeff Roberson 		 * sf_si seltdclear will know to ignore this si.
1676ace8398dSJeff Roberson 		 */
1677ace8398dSJeff Roberson 		TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1678ace8398dSJeff Roberson 		sfp->sf_si = NULL;
1679ace8398dSJeff Roberson 		stp = sfp->sf_td;
1680ace8398dSJeff Roberson 		mtx_lock(&stp->st_mtx);
1681ace8398dSJeff Roberson 		stp->st_flags |= SELTD_PENDING;
1682ace8398dSJeff Roberson 		cv_broadcastpri(&stp->st_wait, pri);
1683ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1684b40ce416SJulian Elischer 	}
1685ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1686ace8398dSJeff Roberson }
1687ace8398dSJeff Roberson 
1688ace8398dSJeff Roberson static void
1689ace8398dSJeff Roberson seltdinit(struct thread *td)
1690ace8398dSJeff Roberson {
1691ace8398dSJeff Roberson 	struct seltd *stp;
1692ace8398dSJeff Roberson 
1693ace8398dSJeff Roberson 	if ((stp = td->td_sel) != NULL)
1694ace8398dSJeff Roberson 		goto out;
1695ace8398dSJeff Roberson 	td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1696ace8398dSJeff Roberson 	mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1697ace8398dSJeff Roberson 	cv_init(&stp->st_wait, "select");
1698ace8398dSJeff Roberson out:
1699ace8398dSJeff Roberson 	stp->st_flags = 0;
1700ace8398dSJeff Roberson 	STAILQ_INIT(&stp->st_selq);
1701ace8398dSJeff Roberson }
1702ace8398dSJeff Roberson 
1703ace8398dSJeff Roberson static int
1704ace8398dSJeff Roberson seltdwait(struct thread *td, int timo)
1705ace8398dSJeff Roberson {
1706ace8398dSJeff Roberson 	struct seltd *stp;
1707ace8398dSJeff Roberson 	int error;
1708ace8398dSJeff Roberson 
1709ace8398dSJeff Roberson 	stp = td->td_sel;
1710ace8398dSJeff Roberson 	/*
1711ace8398dSJeff Roberson 	 * An event of interest may occur while we do not hold the seltd
1712ace8398dSJeff Roberson 	 * locked so check the pending flag before we sleep.
1713ace8398dSJeff Roberson 	 */
1714ace8398dSJeff Roberson 	mtx_lock(&stp->st_mtx);
1715ace8398dSJeff Roberson 	/*
1716ace8398dSJeff Roberson 	 * Any further calls to selrecord will be a rescan.
1717ace8398dSJeff Roberson 	 */
1718ace8398dSJeff Roberson 	stp->st_flags |= SELTD_RESCAN;
1719ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_PENDING) {
1720ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1721ace8398dSJeff Roberson 		return (0);
1722ace8398dSJeff Roberson 	}
1723ace8398dSJeff Roberson 	if (timo > 0)
1724ace8398dSJeff Roberson 		error = cv_timedwait_sig(&stp->st_wait, &stp->st_mtx, timo);
1725ace8398dSJeff Roberson 	else
1726ace8398dSJeff Roberson 		error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
1727ace8398dSJeff Roberson 	mtx_unlock(&stp->st_mtx);
1728ace8398dSJeff Roberson 
1729ace8398dSJeff Roberson 	return (error);
1730ace8398dSJeff Roberson }
1731ace8398dSJeff Roberson 
1732ace8398dSJeff Roberson void
1733ace8398dSJeff Roberson seltdfini(struct thread *td)
1734ace8398dSJeff Roberson {
1735ace8398dSJeff Roberson 	struct seltd *stp;
1736ace8398dSJeff Roberson 
1737ace8398dSJeff Roberson 	stp = td->td_sel;
1738ace8398dSJeff Roberson 	if (stp == NULL)
1739ace8398dSJeff Roberson 		return;
1740ace8398dSJeff Roberson 	if (stp->st_free1)
1741ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free1);
1742ace8398dSJeff Roberson 	if (stp->st_free2)
1743ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free2);
1744ace8398dSJeff Roberson 	td->td_sel = NULL;
1745ace8398dSJeff Roberson 	free(stp, M_SELECT);
1746ace8398dSJeff Roberson }
1747ace8398dSJeff Roberson 
1748ace8398dSJeff Roberson /*
1749ace8398dSJeff Roberson  * Remove the references to the thread from all of the objects we were
1750ace8398dSJeff Roberson  * polling.
1751ace8398dSJeff Roberson  */
1752ace8398dSJeff Roberson static void
1753ace8398dSJeff Roberson seltdclear(struct thread *td)
1754ace8398dSJeff Roberson {
1755ace8398dSJeff Roberson 	struct seltd *stp;
1756ace8398dSJeff Roberson 	struct selfd *sfp;
1757ace8398dSJeff Roberson 	struct selfd *sfn;
1758ace8398dSJeff Roberson 
1759ace8398dSJeff Roberson 	stp = td->td_sel;
1760ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
1761ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1762ace8398dSJeff Roberson 	stp->st_flags = 0;
1763df8bae1dSRodney W. Grimes }
1764265fc98fSSeigo Tanimura 
17654d77a549SAlfred Perlstein static void selectinit(void *);
1766ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
1767265fc98fSSeigo Tanimura static void
1768ace8398dSJeff Roberson selectinit(void *dummy __unused)
1769265fc98fSSeigo Tanimura {
17702141453eSJeff Roberson 
1771ace8398dSJeff Roberson 	selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL,
1772ace8398dSJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, 0);
17732141453eSJeff Roberson 	mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);
1774265fc98fSSeigo Tanimura }
1775