xref: /freebsd/sys/kern/sys_generic.c (revision 8451d0dd78c0a9c0383485b18aba9f081a9db8bb)
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 
77a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
78a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
79a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's");
8055166637SPoul-Henning Kamp 
816d8feddaSKonstantin Belousov static int	pollout(struct thread *, struct pollfd *, struct pollfd *,
826d8feddaSKonstantin Belousov 		    u_int);
83bbbb04ceSAlfred Perlstein static int	pollscan(struct thread *, struct pollfd *, u_int);
84ace8398dSJeff Roberson static int	pollrescan(struct thread *);
85bbbb04ceSAlfred Perlstein static int	selscan(struct thread *, fd_mask **, fd_mask **, int);
86ace8398dSJeff Roberson static int	selrescan(struct thread *, fd_mask **, fd_mask **);
87ace8398dSJeff Roberson static void	selfdalloc(struct thread *, void *);
88ace8398dSJeff Roberson static void	selfdfree(struct seltd *, struct selfd *);
89bcd9e0ddSJohn Baldwin static int	dofileread(struct thread *, int, struct file *, struct uio *,
90bcd9e0ddSJohn Baldwin 		    off_t, int);
91bcd9e0ddSJohn Baldwin static int	dofilewrite(struct thread *, int, struct file *, struct uio *,
92bcd9e0ddSJohn Baldwin 		    off_t, int);
93512824f8SSeigo Tanimura static void	doselwakeup(struct selinfo *, int);
94ace8398dSJeff Roberson static void	seltdinit(struct thread *);
95ace8398dSJeff Roberson static int	seltdwait(struct thread *, int);
96ace8398dSJeff Roberson static void	seltdclear(struct thread *);
97ace8398dSJeff Roberson 
98ace8398dSJeff Roberson /*
99ace8398dSJeff Roberson  * One seltd per-thread allocated on demand as needed.
100ace8398dSJeff Roberson  *
101ace8398dSJeff Roberson  *	t - protected by st_mtx
102ace8398dSJeff Roberson  * 	k - Only accessed by curthread or read-only
103ace8398dSJeff Roberson  */
104ace8398dSJeff Roberson struct seltd {
105ace8398dSJeff Roberson 	STAILQ_HEAD(, selfd)	st_selq;	/* (k) List of selfds. */
106ace8398dSJeff Roberson 	struct selfd		*st_free1;	/* (k) free fd for read set. */
107ace8398dSJeff Roberson 	struct selfd		*st_free2;	/* (k) free fd for write set. */
108ace8398dSJeff Roberson 	struct mtx		st_mtx;		/* Protects struct seltd */
109ace8398dSJeff Roberson 	struct cv		st_wait;	/* (t) Wait channel. */
110ace8398dSJeff Roberson 	int			st_flags;	/* (t) SELTD_ flags. */
111ace8398dSJeff Roberson };
112ace8398dSJeff Roberson 
113ace8398dSJeff Roberson #define	SELTD_PENDING	0x0001			/* We have pending events. */
114ace8398dSJeff Roberson #define	SELTD_RESCAN	0x0002			/* Doing a rescan. */
115ace8398dSJeff Roberson 
116ace8398dSJeff Roberson /*
117ace8398dSJeff Roberson  * One selfd allocated per-thread per-file-descriptor.
118ace8398dSJeff Roberson  *	f - protected by sf_mtx
119ace8398dSJeff Roberson  */
120ace8398dSJeff Roberson struct selfd {
121ace8398dSJeff Roberson 	STAILQ_ENTRY(selfd)	sf_link;	/* (k) fds owned by this td. */
122ace8398dSJeff Roberson 	TAILQ_ENTRY(selfd)	sf_threads;	/* (f) fds on this selinfo. */
123ace8398dSJeff Roberson 	struct selinfo		*sf_si;		/* (f) selinfo when linked. */
124ace8398dSJeff Roberson 	struct mtx		*sf_mtx;	/* Pointer to selinfo mtx. */
125ace8398dSJeff Roberson 	struct seltd		*sf_td;		/* (k) owning seltd. */
126ace8398dSJeff Roberson 	void			*sf_cookie;	/* (k) fd or pollfd. */
127ace8398dSJeff Roberson };
128ace8398dSJeff Roberson 
129ace8398dSJeff Roberson static uma_zone_t selfd_zone;
1302141453eSJeff Roberson static struct mtx_pool *mtxpool_select;
1318fe387abSDmitrij Tejblum 
132d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
133df8bae1dSRodney W. Grimes struct read_args {
134df8bae1dSRodney W. Grimes 	int	fd;
135134e06feSBruce Evans 	void	*buf;
136134e06feSBruce Evans 	size_t	nbyte;
137df8bae1dSRodney W. Grimes };
138d2d3e875SBruce Evans #endif
13926f9a767SRodney W. Grimes int
140*8451d0ddSKip Macy sys_read(td, uap)
141b40ce416SJulian Elischer 	struct thread *td;
142b064d43dSMatthew Dillon 	struct read_args *uap;
143df8bae1dSRodney W. Grimes {
144bcd9e0ddSJohn Baldwin 	struct uio auio;
145bcd9e0ddSJohn Baldwin 	struct iovec aiov;
146279d7226SMatthew Dillon 	int error;
147df8bae1dSRodney W. Grimes 
148bcd9e0ddSJohn Baldwin 	if (uap->nbyte > INT_MAX)
149bcd9e0ddSJohn Baldwin 		return (EINVAL);
150bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
151bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
152bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
153bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
154bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
155bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
156bcd9e0ddSJohn Baldwin 	error = kern_readv(td, uap->fd, &auio);
157279d7226SMatthew Dillon 	return(error);
158df8bae1dSRodney W. Grimes }
159df8bae1dSRodney W. Grimes 
160df8bae1dSRodney W. Grimes /*
161bcd9e0ddSJohn Baldwin  * Positioned read system call
1624160ccd9SAlan Cox  */
1634160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
1644160ccd9SAlan Cox struct pread_args {
1654160ccd9SAlan Cox 	int	fd;
1664160ccd9SAlan Cox 	void	*buf;
1674160ccd9SAlan Cox 	size_t	nbyte;
1688fe387abSDmitrij Tejblum 	int	pad;
1694160ccd9SAlan Cox 	off_t	offset;
1704160ccd9SAlan Cox };
1714160ccd9SAlan Cox #endif
1724160ccd9SAlan Cox int
173*8451d0ddSKip Macy sys_pread(td, uap)
174b40ce416SJulian Elischer 	struct thread *td;
175b064d43dSMatthew Dillon 	struct pread_args *uap;
1764160ccd9SAlan Cox {
1774160ccd9SAlan Cox 	struct uio auio;
1784160ccd9SAlan Cox 	struct iovec aiov;
179bcd9e0ddSJohn Baldwin 	int error;
1804160ccd9SAlan Cox 
181bcd9e0ddSJohn Baldwin 	if (uap->nbyte > INT_MAX)
182bcd9e0ddSJohn Baldwin 		return (EINVAL);
183bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
184bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
1854160ccd9SAlan Cox 	auio.uio_iov = &aiov;
1864160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
187bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
1884160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
189bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, &auio, uap->offset);
1904160ccd9SAlan Cox 	return(error);
1914160ccd9SAlan Cox }
1924160ccd9SAlan Cox 
193c2815ad5SPeter Wemm int
194c2815ad5SPeter Wemm freebsd6_pread(td, uap)
195c2815ad5SPeter Wemm 	struct thread *td;
196c2815ad5SPeter Wemm 	struct freebsd6_pread_args *uap;
197c2815ad5SPeter Wemm {
198c2815ad5SPeter Wemm 	struct pread_args oargs;
199c2815ad5SPeter Wemm 
200c2815ad5SPeter Wemm 	oargs.fd = uap->fd;
201c2815ad5SPeter Wemm 	oargs.buf = uap->buf;
202c2815ad5SPeter Wemm 	oargs.nbyte = uap->nbyte;
203c2815ad5SPeter Wemm 	oargs.offset = uap->offset;
204*8451d0ddSKip Macy 	return (sys_pread(td, &oargs));
205c2815ad5SPeter Wemm }
206c2815ad5SPeter Wemm 
2074160ccd9SAlan Cox /*
208df8bae1dSRodney W. Grimes  * Scatter read system call.
209df8bae1dSRodney W. Grimes  */
210d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
211df8bae1dSRodney W. Grimes struct readv_args {
2127147b19dSBruce Evans 	int	fd;
213df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
214df8bae1dSRodney W. Grimes 	u_int	iovcnt;
215df8bae1dSRodney W. Grimes };
216d2d3e875SBruce Evans #endif
21726f9a767SRodney W. Grimes int
218*8451d0ddSKip Macy sys_readv(struct thread *td, struct readv_args *uap)
219df8bae1dSRodney W. Grimes {
220b88ec951SJohn Baldwin 	struct uio *auio;
221b88ec951SJohn Baldwin 	int error;
222b88ec951SJohn Baldwin 
223b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
224b88ec951SJohn Baldwin 	if (error)
225b88ec951SJohn Baldwin 		return (error);
226b88ec951SJohn Baldwin 	error = kern_readv(td, uap->fd, auio);
227b88ec951SJohn Baldwin 	free(auio, M_IOV);
228b88ec951SJohn Baldwin 	return (error);
229b88ec951SJohn Baldwin }
230b88ec951SJohn Baldwin 
231b88ec951SJohn Baldwin int
232b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio)
233b88ec951SJohn Baldwin {
234b064d43dSMatthew Dillon 	struct file *fp;
235bcd9e0ddSJohn Baldwin 	int error;
236bcd9e0ddSJohn Baldwin 
237a9d2f8d8SRobert Watson 	error = fget_read(td, fd, CAP_READ | CAP_SEEK, &fp);
238bcd9e0ddSJohn Baldwin 	if (error)
239bcd9e0ddSJohn Baldwin 		return (error);
240bcd9e0ddSJohn Baldwin 	error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
241bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
242bcd9e0ddSJohn Baldwin 	return (error);
243bcd9e0ddSJohn Baldwin }
244bcd9e0ddSJohn Baldwin 
245bcd9e0ddSJohn Baldwin /*
246bcd9e0ddSJohn Baldwin  * Scatter positioned read system call.
247bcd9e0ddSJohn Baldwin  */
248bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
249bcd9e0ddSJohn Baldwin struct preadv_args {
250bcd9e0ddSJohn Baldwin 	int	fd;
251bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
252bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
253bcd9e0ddSJohn Baldwin 	off_t	offset;
254bcd9e0ddSJohn Baldwin };
255bcd9e0ddSJohn Baldwin #endif
256bcd9e0ddSJohn Baldwin int
257*8451d0ddSKip Macy sys_preadv(struct thread *td, struct preadv_args *uap)
258bcd9e0ddSJohn Baldwin {
259bcd9e0ddSJohn Baldwin 	struct uio *auio;
260bcd9e0ddSJohn Baldwin 	int error;
261bcd9e0ddSJohn Baldwin 
262bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
263bcd9e0ddSJohn Baldwin 	if (error)
264bcd9e0ddSJohn Baldwin 		return (error);
265bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, auio, uap->offset);
266bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
267bcd9e0ddSJohn Baldwin 	return (error);
268bcd9e0ddSJohn Baldwin }
269bcd9e0ddSJohn Baldwin 
270bcd9e0ddSJohn Baldwin int
271bcd9e0ddSJohn Baldwin kern_preadv(td, fd, auio, offset)
272bcd9e0ddSJohn Baldwin 	struct thread *td;
273bcd9e0ddSJohn Baldwin 	int fd;
274bcd9e0ddSJohn Baldwin 	struct uio *auio;
275bcd9e0ddSJohn Baldwin 	off_t offset;
276bcd9e0ddSJohn Baldwin {
277bcd9e0ddSJohn Baldwin 	struct file *fp;
278bcd9e0ddSJohn Baldwin 	int error;
279bcd9e0ddSJohn Baldwin 
280a9d2f8d8SRobert Watson 	error = fget_read(td, fd, CAP_READ, &fp);
281bcd9e0ddSJohn Baldwin 	if (error)
282bcd9e0ddSJohn Baldwin 		return (error);
283bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
284bcd9e0ddSJohn Baldwin 		error = ESPIPE;
285bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
286bcd9e0ddSJohn Baldwin 		error = EINVAL;
287bcd9e0ddSJohn Baldwin 	else
288bcd9e0ddSJohn Baldwin 		error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
289bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
290bcd9e0ddSJohn Baldwin 	return (error);
291bcd9e0ddSJohn Baldwin }
292bcd9e0ddSJohn Baldwin 
293bcd9e0ddSJohn Baldwin /*
294bcd9e0ddSJohn Baldwin  * Common code for readv and preadv that reads data in
295bcd9e0ddSJohn Baldwin  * from a file using the passed in uio, offset, and flags.
296bcd9e0ddSJohn Baldwin  */
297bcd9e0ddSJohn Baldwin static int
298bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags)
299bcd9e0ddSJohn Baldwin 	struct thread *td;
300bcd9e0ddSJohn Baldwin 	int fd;
301bcd9e0ddSJohn Baldwin 	struct file *fp;
302bcd9e0ddSJohn Baldwin 	struct uio *auio;
303bcd9e0ddSJohn Baldwin 	off_t offset;
304bcd9e0ddSJohn Baldwin 	int flags;
305bcd9e0ddSJohn Baldwin {
306bcd9e0ddSJohn Baldwin 	ssize_t cnt;
30782641acdSAlan Cox 	int error;
308df8bae1dSRodney W. Grimes #ifdef KTRACE
309552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
310df8bae1dSRodney W. Grimes #endif
311df8bae1dSRodney W. Grimes 
3124f8d23d6SPoul-Henning Kamp 	/* Finish zero length reads right here */
3134f8d23d6SPoul-Henning Kamp 	if (auio->uio_resid == 0) {
3144f8d23d6SPoul-Henning Kamp 		td->td_retval[0] = 0;
3154f8d23d6SPoul-Henning Kamp 		return(0);
3164f8d23d6SPoul-Henning Kamp 	}
317552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_READ;
318bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
319552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
320df8bae1dSRodney W. Grimes #ifdef KTRACE
321552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
322552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
323df8bae1dSRodney W. Grimes #endif
324552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
325bcd9e0ddSJohn Baldwin 	if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
326552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
327df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
328df8bae1dSRodney W. Grimes 			error = 0;
329279d7226SMatthew Dillon 	}
330552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
331df8bae1dSRodney W. Grimes #ifdef KTRACE
332552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
333552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
334b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_READ, ktruio, error);
335df8bae1dSRodney W. Grimes 	}
336df8bae1dSRodney W. Grimes #endif
337b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
338df8bae1dSRodney W. Grimes 	return (error);
339df8bae1dSRodney W. Grimes }
340df8bae1dSRodney W. Grimes 
341d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
342df8bae1dSRodney W. Grimes struct write_args {
343df8bae1dSRodney W. Grimes 	int	fd;
344134e06feSBruce Evans 	const void *buf;
345134e06feSBruce Evans 	size_t	nbyte;
346df8bae1dSRodney W. Grimes };
347d2d3e875SBruce Evans #endif
34826f9a767SRodney W. Grimes int
349*8451d0ddSKip Macy sys_write(td, uap)
350b40ce416SJulian Elischer 	struct thread *td;
351b064d43dSMatthew Dillon 	struct write_args *uap;
352df8bae1dSRodney W. Grimes {
353bcd9e0ddSJohn Baldwin 	struct uio auio;
354bcd9e0ddSJohn Baldwin 	struct iovec aiov;
355279d7226SMatthew Dillon 	int error;
356df8bae1dSRodney W. Grimes 
357bcd9e0ddSJohn Baldwin 	if (uap->nbyte > INT_MAX)
358bcd9e0ddSJohn Baldwin 		return (EINVAL);
359bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
360bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
361bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
362bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
363bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
364bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
365bcd9e0ddSJohn Baldwin 	error = kern_writev(td, uap->fd, &auio);
366279d7226SMatthew Dillon 	return(error);
367df8bae1dSRodney W. Grimes }
368df8bae1dSRodney W. Grimes 
369df8bae1dSRodney W. Grimes /*
3700c14ff0eSRobert Watson  * Positioned write system call.
3714160ccd9SAlan Cox  */
3724160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
3734160ccd9SAlan Cox struct pwrite_args {
3744160ccd9SAlan Cox 	int	fd;
3754160ccd9SAlan Cox 	const void *buf;
3764160ccd9SAlan Cox 	size_t	nbyte;
3778fe387abSDmitrij Tejblum 	int	pad;
3784160ccd9SAlan Cox 	off_t	offset;
3794160ccd9SAlan Cox };
3804160ccd9SAlan Cox #endif
3814160ccd9SAlan Cox int
382*8451d0ddSKip Macy sys_pwrite(td, uap)
383b40ce416SJulian Elischer 	struct thread *td;
384b064d43dSMatthew Dillon 	struct pwrite_args *uap;
3854160ccd9SAlan Cox {
3864160ccd9SAlan Cox 	struct uio auio;
3874160ccd9SAlan Cox 	struct iovec aiov;
388bcd9e0ddSJohn Baldwin 	int error;
3894160ccd9SAlan Cox 
390bcd9e0ddSJohn Baldwin 	if (uap->nbyte > INT_MAX)
391bcd9e0ddSJohn Baldwin 		return (EINVAL);
392bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
393bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
3944160ccd9SAlan Cox 	auio.uio_iov = &aiov;
3954160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
396bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
3974160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
398bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, &auio, uap->offset);
3994160ccd9SAlan Cox 	return(error);
4004160ccd9SAlan Cox }
4014160ccd9SAlan Cox 
402c2815ad5SPeter Wemm int
403c2815ad5SPeter Wemm freebsd6_pwrite(td, uap)
404c2815ad5SPeter Wemm 	struct thread *td;
405c2815ad5SPeter Wemm 	struct freebsd6_pwrite_args *uap;
406c2815ad5SPeter Wemm {
407c2815ad5SPeter Wemm 	struct pwrite_args oargs;
408c2815ad5SPeter Wemm 
409c2815ad5SPeter Wemm 	oargs.fd = uap->fd;
410c2815ad5SPeter Wemm 	oargs.buf = uap->buf;
411c2815ad5SPeter Wemm 	oargs.nbyte = uap->nbyte;
412c2815ad5SPeter Wemm 	oargs.offset = uap->offset;
413*8451d0ddSKip Macy 	return (sys_pwrite(td, &oargs));
414c2815ad5SPeter Wemm }
415c2815ad5SPeter Wemm 
4164160ccd9SAlan Cox /*
4170c14ff0eSRobert Watson  * Gather write system call.
418df8bae1dSRodney W. Grimes  */
419d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
420df8bae1dSRodney W. Grimes struct writev_args {
421df8bae1dSRodney W. Grimes 	int	fd;
422df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
423df8bae1dSRodney W. Grimes 	u_int	iovcnt;
424df8bae1dSRodney W. Grimes };
425d2d3e875SBruce Evans #endif
42626f9a767SRodney W. Grimes int
427*8451d0ddSKip Macy sys_writev(struct thread *td, struct writev_args *uap)
428df8bae1dSRodney W. Grimes {
429b88ec951SJohn Baldwin 	struct uio *auio;
430b88ec951SJohn Baldwin 	int error;
431b88ec951SJohn Baldwin 
432b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
433b88ec951SJohn Baldwin 	if (error)
434b88ec951SJohn Baldwin 		return (error);
435b88ec951SJohn Baldwin 	error = kern_writev(td, uap->fd, auio);
436b88ec951SJohn Baldwin 	free(auio, M_IOV);
437b88ec951SJohn Baldwin 	return (error);
438b88ec951SJohn Baldwin }
439b88ec951SJohn Baldwin 
440b88ec951SJohn Baldwin int
441b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio)
442b88ec951SJohn Baldwin {
443b064d43dSMatthew Dillon 	struct file *fp;
444bcd9e0ddSJohn Baldwin 	int error;
445bcd9e0ddSJohn Baldwin 
446a9d2f8d8SRobert Watson 	error = fget_write(td, fd, CAP_WRITE | CAP_SEEK, &fp);
447bcd9e0ddSJohn Baldwin 	if (error)
448af56abaaSJohn Baldwin 		return (error);
449bcd9e0ddSJohn Baldwin 	error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
450bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
451bcd9e0ddSJohn Baldwin 	return (error);
452bcd9e0ddSJohn Baldwin }
453bcd9e0ddSJohn Baldwin 
454bcd9e0ddSJohn Baldwin /*
4550c14ff0eSRobert Watson  * Gather positioned write system call.
456bcd9e0ddSJohn Baldwin  */
457bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
458bcd9e0ddSJohn Baldwin struct pwritev_args {
459bcd9e0ddSJohn Baldwin 	int	fd;
460bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
461bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
462bcd9e0ddSJohn Baldwin 	off_t	offset;
463bcd9e0ddSJohn Baldwin };
464bcd9e0ddSJohn Baldwin #endif
465bcd9e0ddSJohn Baldwin int
466*8451d0ddSKip Macy sys_pwritev(struct thread *td, struct pwritev_args *uap)
467bcd9e0ddSJohn Baldwin {
468bcd9e0ddSJohn Baldwin 	struct uio *auio;
469bcd9e0ddSJohn Baldwin 	int error;
470bcd9e0ddSJohn Baldwin 
471bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
472bcd9e0ddSJohn Baldwin 	if (error)
473bcd9e0ddSJohn Baldwin 		return (error);
474bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, auio, uap->offset);
475bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
476bcd9e0ddSJohn Baldwin 	return (error);
477bcd9e0ddSJohn Baldwin }
478bcd9e0ddSJohn Baldwin 
479bcd9e0ddSJohn Baldwin int
480bcd9e0ddSJohn Baldwin kern_pwritev(td, fd, auio, offset)
481bcd9e0ddSJohn Baldwin 	struct thread *td;
482bcd9e0ddSJohn Baldwin 	struct uio *auio;
483bcd9e0ddSJohn Baldwin 	int fd;
484bcd9e0ddSJohn Baldwin 	off_t offset;
485bcd9e0ddSJohn Baldwin {
486bcd9e0ddSJohn Baldwin 	struct file *fp;
487bcd9e0ddSJohn Baldwin 	int error;
488bcd9e0ddSJohn Baldwin 
489a9d2f8d8SRobert Watson 	error = fget_write(td, fd, CAP_WRITE, &fp);
490bcd9e0ddSJohn Baldwin 	if (error)
491af56abaaSJohn Baldwin 		return (error);
492bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
493bcd9e0ddSJohn Baldwin 		error = ESPIPE;
494bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
495bcd9e0ddSJohn Baldwin 		error = EINVAL;
496bcd9e0ddSJohn Baldwin 	else
497bcd9e0ddSJohn Baldwin 		error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
498bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
499bcd9e0ddSJohn Baldwin 	return (error);
500bcd9e0ddSJohn Baldwin }
501bcd9e0ddSJohn Baldwin 
502bcd9e0ddSJohn Baldwin /*
503bcd9e0ddSJohn Baldwin  * Common code for writev and pwritev that writes data to
504bcd9e0ddSJohn Baldwin  * a file using the passed in uio, offset, and flags.
505bcd9e0ddSJohn Baldwin  */
506bcd9e0ddSJohn Baldwin static int
507bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags)
508bcd9e0ddSJohn Baldwin 	struct thread *td;
509bcd9e0ddSJohn Baldwin 	int fd;
510bcd9e0ddSJohn Baldwin 	struct file *fp;
511bcd9e0ddSJohn Baldwin 	struct uio *auio;
512bcd9e0ddSJohn Baldwin 	off_t offset;
513bcd9e0ddSJohn Baldwin 	int flags;
514bcd9e0ddSJohn Baldwin {
515bcd9e0ddSJohn Baldwin 	ssize_t cnt;
516552afd9cSPoul-Henning Kamp 	int error;
517df8bae1dSRodney W. Grimes #ifdef KTRACE
518552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
519df8bae1dSRodney W. Grimes #endif
520df8bae1dSRodney W. Grimes 
521552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_WRITE;
522552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
523bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
524df8bae1dSRodney W. Grimes #ifdef KTRACE
525552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
526552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
527df8bae1dSRodney W. Grimes #endif
528552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
529a41ce5d3SMatthew Dillon 	if (fp->f_type == DTYPE_VNODE)
5309440653dSMatthew Dillon 		bwillwrite();
531bcd9e0ddSJohn Baldwin 	if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
532552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
533df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
534df8bae1dSRodney W. Grimes 			error = 0;
535bcd9e0ddSJohn Baldwin 		/* Socket layer is responsible for issuing SIGPIPE. */
5366f7ca813SBruce M Simpson 		if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
537b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
5387a6f3d78SJohn Baldwin 			tdsignal(td, SIGPIPE);
539b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
54019eb87d2SJohn Baldwin 		}
541df8bae1dSRodney W. Grimes 	}
542552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
543df8bae1dSRodney W. Grimes #ifdef KTRACE
544552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
545552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
546b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_WRITE, ktruio, error);
547df8bae1dSRodney W. Grimes 	}
548df8bae1dSRodney W. Grimes #endif
549b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
550df8bae1dSRodney W. Grimes 	return (error);
551df8bae1dSRodney W. Grimes }
552df8bae1dSRodney W. Grimes 
553e4650294SJohn Baldwin /*
554e4650294SJohn Baldwin  * Truncate a file given a file descriptor.
555e4650294SJohn Baldwin  *
556e4650294SJohn Baldwin  * Can't use fget_write() here, since must return EINVAL and not EBADF if the
557e4650294SJohn Baldwin  * descriptor isn't writable.
558e4650294SJohn Baldwin  */
559e4650294SJohn Baldwin int
560e4650294SJohn Baldwin kern_ftruncate(td, fd, length)
561e4650294SJohn Baldwin 	struct thread *td;
562e4650294SJohn Baldwin 	int fd;
563e4650294SJohn Baldwin 	off_t length;
564e4650294SJohn Baldwin {
565e4650294SJohn Baldwin 	struct file *fp;
566e4650294SJohn Baldwin 	int error;
567e4650294SJohn Baldwin 
56814961ba7SRobert Watson 	AUDIT_ARG_FD(fd);
569e4650294SJohn Baldwin 	if (length < 0)
570e4650294SJohn Baldwin 		return (EINVAL);
571a9d2f8d8SRobert Watson 	error = fget(td, fd, CAP_FTRUNCATE, &fp);
572e4650294SJohn Baldwin 	if (error)
573e4650294SJohn Baldwin 		return (error);
57414961ba7SRobert Watson 	AUDIT_ARG_FILE(td->td_proc, fp);
575e4650294SJohn Baldwin 	if (!(fp->f_flag & FWRITE)) {
576e4650294SJohn Baldwin 		fdrop(fp, td);
577e4650294SJohn Baldwin 		return (EINVAL);
578e4650294SJohn Baldwin 	}
579e4650294SJohn Baldwin 	error = fo_truncate(fp, length, td->td_ucred, td);
580e4650294SJohn Baldwin 	fdrop(fp, td);
581e4650294SJohn Baldwin 	return (error);
582e4650294SJohn Baldwin }
583e4650294SJohn Baldwin 
584e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
585e4650294SJohn Baldwin struct ftruncate_args {
586e4650294SJohn Baldwin 	int	fd;
587e4650294SJohn Baldwin 	int	pad;
588e4650294SJohn Baldwin 	off_t	length;
589e4650294SJohn Baldwin };
590e4650294SJohn Baldwin #endif
591e4650294SJohn Baldwin int
592*8451d0ddSKip Macy sys_ftruncate(td, uap)
593e4650294SJohn Baldwin 	struct thread *td;
594e4650294SJohn Baldwin 	struct ftruncate_args *uap;
595e4650294SJohn Baldwin {
596e4650294SJohn Baldwin 
597e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
598e4650294SJohn Baldwin }
599e4650294SJohn Baldwin 
600e4650294SJohn Baldwin #if defined(COMPAT_43)
601e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
602e4650294SJohn Baldwin struct oftruncate_args {
603e4650294SJohn Baldwin 	int	fd;
604e4650294SJohn Baldwin 	long	length;
605e4650294SJohn Baldwin };
606e4650294SJohn Baldwin #endif
607e4650294SJohn Baldwin int
608e4650294SJohn Baldwin oftruncate(td, uap)
609e4650294SJohn Baldwin 	struct thread *td;
610e4650294SJohn Baldwin 	struct oftruncate_args *uap;
611e4650294SJohn Baldwin {
612e4650294SJohn Baldwin 
613e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
614e4650294SJohn Baldwin }
615e4650294SJohn Baldwin #endif /* COMPAT_43 */
616e4650294SJohn Baldwin 
617d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
618df8bae1dSRodney W. Grimes struct ioctl_args {
619df8bae1dSRodney W. Grimes 	int	fd;
620069e9bc1SDoug Rabson 	u_long	com;
621df8bae1dSRodney W. Grimes 	caddr_t	data;
622df8bae1dSRodney W. Grimes };
623d2d3e875SBruce Evans #endif
624df8bae1dSRodney W. Grimes /* ARGSUSED */
62526f9a767SRodney W. Grimes int
626*8451d0ddSKip Macy sys_ioctl(struct thread *td, struct ioctl_args *uap)
627df8bae1dSRodney W. Grimes {
6283e15c66fSPoul-Henning Kamp 	u_long com;
6299fddcc66SRuslan Ermilov 	int arg, error;
6303e15c66fSPoul-Henning Kamp 	u_int size;
6319fddcc66SRuslan Ermilov 	caddr_t data;
632df8bae1dSRodney W. Grimes 
6339fc6aa06SPoul-Henning Kamp 	if (uap->com > 0xffffffff) {
6349fc6aa06SPoul-Henning Kamp 		printf(
6359fc6aa06SPoul-Henning Kamp 		    "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
636431f8906SJulian Elischer 		    td->td_proc->p_pid, td->td_name, uap->com);
6379fc6aa06SPoul-Henning Kamp 		uap->com &= 0xffffffff;
6389fc6aa06SPoul-Henning Kamp 	}
639d9f46233SJohn Baldwin 	com = uap->com;
640df8bae1dSRodney W. Grimes 
641df8bae1dSRodney W. Grimes 	/*
642df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
643df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
644df8bae1dSRodney W. Grimes 	 */
645df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
646ca51b19bSPoul-Henning Kamp 	if ((size > IOCPARM_MAX) ||
647ca51b19bSPoul-Henning Kamp 	    ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
6482de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
6492de92a38SPeter Wemm 	    ((com & IOC_OUT) && size == 0) ||
6502de92a38SPeter Wemm #else
6512de92a38SPeter Wemm 	    ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
6522de92a38SPeter Wemm #endif
6539fddcc66SRuslan Ermilov 	    ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
654426da3bcSAlfred Perlstein 		return (ENOTTY);
655279d7226SMatthew Dillon 
656ca51b19bSPoul-Henning Kamp 	if (size > 0) {
657715457f6SDavid E. O'Brien 		if (com & IOC_VOID) {
6589fddcc66SRuslan Ermilov 			/* Integer argument. */
6599fddcc66SRuslan Ermilov 			arg = (intptr_t)uap->data;
6609fddcc66SRuslan Ermilov 			data = (void *)&arg;
6619fddcc66SRuslan Ermilov 			size = 0;
662715457f6SDavid E. O'Brien 		} else
663715457f6SDavid E. O'Brien 			data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
6649fddcc66SRuslan Ermilov 	} else
6659fddcc66SRuslan Ermilov 		data = (void *)&uap->data;
666df8bae1dSRodney W. Grimes 	if (com & IOC_IN) {
667df8bae1dSRodney W. Grimes 		error = copyin(uap->data, data, (u_int)size);
668df8bae1dSRodney W. Grimes 		if (error) {
669a1b0a180SRuslan Ermilov 			if (size > 0)
6709fddcc66SRuslan Ermilov 				free(data, M_IOCTLOPS);
6713e15c66fSPoul-Henning Kamp 			return (error);
672df8bae1dSRodney W. Grimes 		}
673ca51b19bSPoul-Henning Kamp 	} else if (com & IOC_OUT) {
674df8bae1dSRodney W. Grimes 		/*
675df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
676df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
677df8bae1dSRodney W. Grimes 		 */
678df8bae1dSRodney W. Grimes 		bzero(data, size);
679279d7226SMatthew Dillon 	}
680df8bae1dSRodney W. Grimes 
681d9f46233SJohn Baldwin 	error = kern_ioctl(td, uap->fd, com, data);
682d9f46233SJohn Baldwin 
683d9f46233SJohn Baldwin 	if (error == 0 && (com & IOC_OUT))
684d9f46233SJohn Baldwin 		error = copyout(data, uap->data, (u_int)size);
685d9f46233SJohn Baldwin 
6869fddcc66SRuslan Ermilov 	if (size > 0)
6879fddcc66SRuslan Ermilov 		free(data, M_IOCTLOPS);
688d9f46233SJohn Baldwin 	return (error);
689d9f46233SJohn Baldwin }
690d9f46233SJohn Baldwin 
691d9f46233SJohn Baldwin int
692d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
693d9f46233SJohn Baldwin {
694d9f46233SJohn Baldwin 	struct file *fp;
695d9f46233SJohn Baldwin 	struct filedesc *fdp;
696d9f46233SJohn Baldwin 	int error;
697d9f46233SJohn Baldwin 	int tmp;
698d9f46233SJohn Baldwin 
69964c9a4d9SRobert Watson 	AUDIT_ARG_FD(fd);
70064c9a4d9SRobert Watson 	AUDIT_ARG_CMD(com);
701a9d2f8d8SRobert Watson 	if ((error = fget(td, fd, CAP_IOCTL, &fp)) != 0)
702d9f46233SJohn Baldwin 		return (error);
703d9f46233SJohn Baldwin 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
704d9f46233SJohn Baldwin 		fdrop(fp, td);
705d9f46233SJohn Baldwin 		return (EBADF);
706d9f46233SJohn Baldwin 	}
707d9f46233SJohn Baldwin 	fdp = td->td_proc->p_fd;
708d9f46233SJohn Baldwin 	switch (com) {
709d9f46233SJohn Baldwin 	case FIONCLEX:
7105e3f7694SRobert Watson 		FILEDESC_XLOCK(fdp);
711d9f46233SJohn Baldwin 		fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
7125e3f7694SRobert Watson 		FILEDESC_XUNLOCK(fdp);
713d9f46233SJohn Baldwin 		goto out;
714d9f46233SJohn Baldwin 	case FIOCLEX:
7155e3f7694SRobert Watson 		FILEDESC_XLOCK(fdp);
716d9f46233SJohn Baldwin 		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
7175e3f7694SRobert Watson 		FILEDESC_XUNLOCK(fdp);
718d9f46233SJohn Baldwin 		goto out;
719d9f46233SJohn Baldwin 	case FIONBIO:
720bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
721397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FNONBLOCK);
722df8bae1dSRodney W. Grimes 		else
723397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FNONBLOCK);
7248ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
725d9f46233SJohn Baldwin 		break;
726d9f46233SJohn Baldwin 	case FIOASYNC:
727bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
728397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FASYNC);
729df8bae1dSRodney W. Grimes 		else
730397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FASYNC);
7318ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
732d9f46233SJohn Baldwin 		break;
733df8bae1dSRodney W. Grimes 	}
7348ccf264fSPoul-Henning Kamp 
7358ccf264fSPoul-Henning Kamp 	error = fo_ioctl(fp, com, data, td->td_ucred, td);
736d9f46233SJohn Baldwin out:
737b40ce416SJulian Elischer 	fdrop(fp, td);
738df8bae1dSRodney W. Grimes 	return (error);
739df8bae1dSRodney W. Grimes }
740df8bae1dSRodney W. Grimes 
741125dcf8cSKonstantin Belousov int
742125dcf8cSKonstantin Belousov poll_no_poll(int events)
743125dcf8cSKonstantin Belousov {
744125dcf8cSKonstantin Belousov 	/*
745125dcf8cSKonstantin Belousov 	 * Return true for read/write.  If the user asked for something
746125dcf8cSKonstantin Belousov 	 * special, return POLLNVAL, so that clients have a way of
747125dcf8cSKonstantin Belousov 	 * determining reliably whether or not the extended
748125dcf8cSKonstantin Belousov 	 * functionality is present without hard-coding knowledge
749125dcf8cSKonstantin Belousov 	 * of specific filesystem implementations.
750125dcf8cSKonstantin Belousov 	 */
751125dcf8cSKonstantin Belousov 	if (events & ~POLLSTANDARD)
752125dcf8cSKonstantin Belousov 		return (POLLNVAL);
753125dcf8cSKonstantin Belousov 
754125dcf8cSKonstantin Belousov 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
755125dcf8cSKonstantin Belousov }
756125dcf8cSKonstantin Belousov 
757066d836bSKonstantin Belousov int
758*8451d0ddSKip Macy sys_pselect(struct thread *td, struct pselect_args *uap)
759066d836bSKonstantin Belousov {
760066d836bSKonstantin Belousov 	struct timespec ts;
761066d836bSKonstantin Belousov 	struct timeval tv, *tvp;
762066d836bSKonstantin Belousov 	sigset_t set, *uset;
763066d836bSKonstantin Belousov 	int error;
764066d836bSKonstantin Belousov 
765066d836bSKonstantin Belousov 	if (uap->ts != NULL) {
766066d836bSKonstantin Belousov 		error = copyin(uap->ts, &ts, sizeof(ts));
767066d836bSKonstantin Belousov 		if (error != 0)
768066d836bSKonstantin Belousov 		    return (error);
769066d836bSKonstantin Belousov 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
770066d836bSKonstantin Belousov 		tvp = &tv;
771066d836bSKonstantin Belousov 	} else
772066d836bSKonstantin Belousov 		tvp = NULL;
773066d836bSKonstantin Belousov 	if (uap->sm != NULL) {
774066d836bSKonstantin Belousov 		error = copyin(uap->sm, &set, sizeof(set));
775066d836bSKonstantin Belousov 		if (error != 0)
776066d836bSKonstantin Belousov 			return (error);
777066d836bSKonstantin Belousov 		uset = &set;
778066d836bSKonstantin Belousov 	} else
779066d836bSKonstantin Belousov 		uset = NULL;
780066d836bSKonstantin Belousov 	return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
781066d836bSKonstantin Belousov 	    uset, NFDBITS));
782066d836bSKonstantin Belousov }
783066d836bSKonstantin Belousov 
784066d836bSKonstantin Belousov int
785066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
786066d836bSKonstantin Belousov     struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
787066d836bSKonstantin Belousov {
788066d836bSKonstantin Belousov 	int error;
789066d836bSKonstantin Belousov 
790066d836bSKonstantin Belousov 	if (uset != NULL) {
791066d836bSKonstantin Belousov 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
792066d836bSKonstantin Belousov 		    &td->td_oldsigmask, 0);
793066d836bSKonstantin Belousov 		if (error != 0)
794066d836bSKonstantin Belousov 			return (error);
795066d836bSKonstantin Belousov 		td->td_pflags |= TDP_OLDMASK;
796066d836bSKonstantin Belousov 		/*
797066d836bSKonstantin Belousov 		 * Make sure that ast() is called on return to
798066d836bSKonstantin Belousov 		 * usermode and TDP_OLDMASK is cleared, restoring old
799066d836bSKonstantin Belousov 		 * sigmask.
800066d836bSKonstantin Belousov 		 */
801066d836bSKonstantin Belousov 		thread_lock(td);
802066d836bSKonstantin Belousov 		td->td_flags |= TDF_ASTPENDING;
803066d836bSKonstantin Belousov 		thread_unlock(td);
804066d836bSKonstantin Belousov 	}
805066d836bSKonstantin Belousov 	error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
806066d836bSKonstantin Belousov 	return (error);
807066d836bSKonstantin Belousov }
808066d836bSKonstantin Belousov 
809d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
810df8bae1dSRodney W. Grimes struct select_args {
811b08f7993SSujal Patel 	int	nd;
812df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
813df8bae1dSRodney W. Grimes 	struct	timeval *tv;
814df8bae1dSRodney W. Grimes };
815d2d3e875SBruce Evans #endif
81626f9a767SRodney W. Grimes int
817*8451d0ddSKip Macy sys_select(struct thread *td, struct select_args *uap)
818df8bae1dSRodney W. Grimes {
8198f19eb88SIan Dowse 	struct timeval tv, *tvp;
8208f19eb88SIan Dowse 	int error;
8218f19eb88SIan Dowse 
8228f19eb88SIan Dowse 	if (uap->tv != NULL) {
8238f19eb88SIan Dowse 		error = copyin(uap->tv, &tv, sizeof(tv));
8248f19eb88SIan Dowse 		if (error)
8258f19eb88SIan Dowse 			return (error);
8268f19eb88SIan Dowse 		tvp = &tv;
8278f19eb88SIan Dowse 	} else
8288f19eb88SIan Dowse 		tvp = NULL;
8298f19eb88SIan Dowse 
830b55ef216SKonstantin Belousov 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
831b55ef216SKonstantin Belousov 	    NFDBITS));
8328f19eb88SIan Dowse }
8338f19eb88SIan Dowse 
8348f19eb88SIan Dowse int
8358f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
836b55ef216SKonstantin Belousov     fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
8378f19eb88SIan Dowse {
838426da3bcSAlfred Perlstein 	struct filedesc *fdp;
839d5e4d7e1SBruce Evans 	/*
840d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
841d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
842d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
843d5e4d7e1SBruce Evans 	 * of 256.
844d5e4d7e1SBruce Evans 	 */
845d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
846eb209311SAlfred Perlstein 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
84700af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
8489ae6d334SKelly Yancey 	int error, timo;
849b55ef216SKonstantin Belousov 	u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
850df8bae1dSRodney W. Grimes 
8518f19eb88SIan Dowse 	if (nd < 0)
852acbfbfeaSSujal Patel 		return (EINVAL);
853426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
8542141453eSJeff Roberson 	if (nd > fdp->fd_lastfile + 1)
8552141453eSJeff Roberson 		nd = fdp->fd_lastfile + 1;
856b08f7993SSujal Patel 
857d5e4d7e1SBruce Evans 	/*
858d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
859d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
860d5e4d7e1SBruce Evans 	 */
8618f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
862d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
863b55ef216SKonstantin Belousov 	ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
864d5e4d7e1SBruce Evans 	nbufbytes = 0;
8658f19eb88SIan Dowse 	if (fd_in != NULL)
866d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
8678f19eb88SIan Dowse 	if (fd_ou != NULL)
868d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
8698f19eb88SIan Dowse 	if (fd_ex != NULL)
870d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
871d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
872d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
873d5e4d7e1SBruce Evans 	else
874a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
875b08f7993SSujal Patel 
876b08f7993SSujal Patel 	/*
877d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
878d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
879d5e4d7e1SBruce Evans 	 * together.
880b08f7993SSujal Patel 	 */
881d5e4d7e1SBruce Evans 	sbp = selbits;
882df8bae1dSRodney W. Grimes #define	getbits(name, x) \
883d5e4d7e1SBruce Evans 	do {								\
884841c0c7eSNathan Whitehorn 		if (name == NULL) {					\
885d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
886841c0c7eSNathan Whitehorn 			obits[x] = NULL;				\
887841c0c7eSNathan Whitehorn 		} else {						\
888d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
889d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
890d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
891b55ef216SKonstantin Belousov 			error = copyin(name, ibits[x], ncpubytes);	\
892265fc98fSSeigo Tanimura 			if (error != 0)					\
893ace8398dSJeff Roberson 				goto done;				\
894b55ef216SKonstantin Belousov 			bzero((char *)ibits[x] + ncpubytes,		\
895b55ef216SKonstantin Belousov 			    ncpbytes - ncpubytes);			\
896e04ac2feSJohn Baldwin 		}							\
897d5e4d7e1SBruce Evans 	} while (0)
8988f19eb88SIan Dowse 	getbits(fd_in, 0);
8998f19eb88SIan Dowse 	getbits(fd_ou, 1);
9008f19eb88SIan Dowse 	getbits(fd_ex, 2);
901df8bae1dSRodney W. Grimes #undef	getbits
902841c0c7eSNathan Whitehorn 
903841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
904841c0c7eSNathan Whitehorn 	/*
905841c0c7eSNathan Whitehorn 	 * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
906841c0c7eSNathan Whitehorn 	 * we are running under 32-bit emulation. This should be more
907841c0c7eSNathan Whitehorn 	 * generic.
908841c0c7eSNathan Whitehorn 	 */
909841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)						\
910841c0c7eSNathan Whitehorn 	if (abi_nfdbits != NFDBITS && bits != NULL) {			\
911841c0c7eSNathan Whitehorn 		int i;							\
912841c0c7eSNathan Whitehorn 		for (i = 0; i < ncpbytes / sizeof *sbp; i++)		\
913841c0c7eSNathan Whitehorn 			bits[i] = (bits[i] >> 32) | (bits[i] << 32);	\
914841c0c7eSNathan Whitehorn 	}
915841c0c7eSNathan Whitehorn #else
916841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)
917841c0c7eSNathan Whitehorn #endif
918841c0c7eSNathan Whitehorn 
919841c0c7eSNathan Whitehorn 	/* Make sure the bit order makes it through an ABI transition */
920841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[0]);
921841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[1]);
922841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[2]);
923841c0c7eSNathan Whitehorn 
924d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
925d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
926df8bae1dSRodney W. Grimes 
9278f19eb88SIan Dowse 	if (tvp != NULL) {
9288f19eb88SIan Dowse 		atv = *tvp;
929df8bae1dSRodney W. Grimes 		if (itimerfix(&atv)) {
930df8bae1dSRodney W. Grimes 			error = EINVAL;
931ace8398dSJeff Roberson 			goto done;
932df8bae1dSRodney W. Grimes 		}
933c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
93400af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
9359c386f6bSJohn Baldwin 	} else {
93600af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
9379c386f6bSJohn Baldwin 		atv.tv_usec = 0;
9389c386f6bSJohn Baldwin 	}
93900af9731SPoul-Henning Kamp 	timo = 0;
940ace8398dSJeff Roberson 	seltdinit(td);
941ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
942ace8398dSJeff Roberson 	for (;;) {
9438f19eb88SIan Dowse 		error = selscan(td, ibits, obits, nd);
944ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
945ace8398dSJeff Roberson 			break;
9464da144c0SJohn Baldwin 		if (atv.tv_sec || atv.tv_usec) {
947c21410e1SPoul-Henning Kamp 			getmicrouptime(&rtv);
94885f190e4SAlfred Perlstein 			if (timevalcmp(&rtv, &atv, >=))
949ace8398dSJeff Roberson 				break;
95000af9731SPoul-Henning Kamp 			ttv = atv;
95100af9731SPoul-Henning Kamp 			timevalsub(&ttv, &rtv);
95200af9731SPoul-Henning Kamp 			timo = ttv.tv_sec > 24 * 60 * 60 ?
95300af9731SPoul-Henning Kamp 			    24 * 60 * 60 * hz : tvtohz(&ttv);
954df8bae1dSRodney W. Grimes 		}
955ace8398dSJeff Roberson 		error = seltdwait(td, timo);
956ace8398dSJeff Roberson 		if (error)
957ace8398dSJeff Roberson 			break;
958ace8398dSJeff Roberson 		error = selrescan(td, ibits, obits);
959ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
960ace8398dSJeff Roberson 			break;
96185f190e4SAlfred Perlstein 	}
962ace8398dSJeff Roberson 	seltdclear(td);
963265fc98fSSeigo Tanimura 
964df8bae1dSRodney W. Grimes done:
965df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
966df8bae1dSRodney W. Grimes 	if (error == ERESTART)
967df8bae1dSRodney W. Grimes 		error = EINTR;
968df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
969df8bae1dSRodney W. Grimes 		error = 0;
970841c0c7eSNathan Whitehorn 
971841c0c7eSNathan Whitehorn 	/* swizzle bit order back, if necessary */
972841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[0]);
973841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[1]);
974841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[2]);
975841c0c7eSNathan Whitehorn #undef swizzle_fdset
976841c0c7eSNathan Whitehorn 
977df8bae1dSRodney W. Grimes #define	putbits(name, x) \
978b55ef216SKonstantin Belousov 	if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
979df8bae1dSRodney W. Grimes 		error = error2;
980df8bae1dSRodney W. Grimes 	if (error == 0) {
981df8bae1dSRodney W. Grimes 		int error2;
982df8bae1dSRodney W. Grimes 
9838f19eb88SIan Dowse 		putbits(fd_in, 0);
9848f19eb88SIan Dowse 		putbits(fd_ou, 1);
9858f19eb88SIan Dowse 		putbits(fd_ex, 2);
986df8bae1dSRodney W. Grimes #undef putbits
987df8bae1dSRodney W. Grimes 	}
988d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
989d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
990ad2edad9SMatthew Dillon 
991df8bae1dSRodney W. Grimes 	return (error);
992df8bae1dSRodney W. Grimes }
99311b763dfSJeff Roberson /*
99411b763dfSJeff Roberson  * Convert a select bit set to poll flags.
995748b9df6SJeff Roberson  *
99611b763dfSJeff Roberson  * The backend always returns POLLHUP/POLLERR if appropriate and we
99711b763dfSJeff Roberson  * return this as a set bit in any set.
99811b763dfSJeff Roberson  */
99911b763dfSJeff Roberson static int select_flags[3] = {
100011b763dfSJeff Roberson     POLLRDNORM | POLLHUP | POLLERR,
100111b763dfSJeff Roberson     POLLWRNORM | POLLHUP | POLLERR,
100261e53a38SKonstantin Belousov     POLLRDBAND | POLLERR
100311b763dfSJeff Roberson };
100411b763dfSJeff Roberson 
100511b763dfSJeff Roberson /*
100611b763dfSJeff Roberson  * Compute the fo_poll flags required for a fd given by the index and
100711b763dfSJeff Roberson  * bit position in the fd_mask array.
100811b763dfSJeff Roberson  */
100911b763dfSJeff Roberson static __inline int
101060b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit)
101111b763dfSJeff Roberson {
101211b763dfSJeff Roberson 	int flags;
101311b763dfSJeff Roberson 	int msk;
101411b763dfSJeff Roberson 
101511b763dfSJeff Roberson 	flags = 0;
101611b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
101711b763dfSJeff Roberson 		if (ibits[msk] == NULL)
101811b763dfSJeff Roberson 			continue;
101960b7f468SStephane E. Potvin 		if ((ibits[msk][idx] & bit) == 0)
102011b763dfSJeff Roberson 			continue;
102111b763dfSJeff Roberson 		flags |= select_flags[msk];
102211b763dfSJeff Roberson 	}
102311b763dfSJeff Roberson 	return (flags);
102411b763dfSJeff Roberson }
102511b763dfSJeff Roberson 
102611b763dfSJeff Roberson /*
102711b763dfSJeff Roberson  * Set the appropriate output bits given a mask of fired events and the
102811b763dfSJeff Roberson  * input bits originally requested.
102911b763dfSJeff Roberson  */
103011b763dfSJeff Roberson static __inline int
103111b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
103211b763dfSJeff Roberson {
103311b763dfSJeff Roberson 	int msk;
103411b763dfSJeff Roberson 	int n;
103511b763dfSJeff Roberson 
103611b763dfSJeff Roberson 	n = 0;
103711b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
103811b763dfSJeff Roberson 		if ((events & select_flags[msk]) == 0)
103911b763dfSJeff Roberson 			continue;
104011b763dfSJeff Roberson 		if (ibits[msk] == NULL)
104111b763dfSJeff Roberson 			continue;
104211b763dfSJeff Roberson 		if ((ibits[msk][idx] & bit) == 0)
104311b763dfSJeff Roberson 			continue;
104411b763dfSJeff Roberson 		/*
104511b763dfSJeff Roberson 		 * XXX Check for a duplicate set.  This can occur because a
104611b763dfSJeff Roberson 		 * socket calls selrecord() twice for each poll() call
104711b763dfSJeff Roberson 		 * resulting in two selfds per real fd.  selrescan() will
104811b763dfSJeff Roberson 		 * call selsetbits twice as a result.
104911b763dfSJeff Roberson 		 */
105011b763dfSJeff Roberson 		if ((obits[msk][idx] & bit) != 0)
105111b763dfSJeff Roberson 			continue;
105211b763dfSJeff Roberson 		obits[msk][idx] |= bit;
105311b763dfSJeff Roberson 		n++;
105411b763dfSJeff Roberson 	}
105511b763dfSJeff Roberson 
105611b763dfSJeff Roberson 	return (n);
105711b763dfSJeff Roberson }
1058df8bae1dSRodney W. Grimes 
1059a9d2f8d8SRobert Watson static __inline int
1060a9d2f8d8SRobert Watson getselfd_cap(struct filedesc *fdp, int fd, struct file **fpp)
1061a9d2f8d8SRobert Watson {
1062a9d2f8d8SRobert Watson 	struct file *fp;
1063a9d2f8d8SRobert Watson #ifdef CAPABILITIES
1064a9d2f8d8SRobert Watson 	struct file *fp_fromcap;
1065a9d2f8d8SRobert Watson 	int error;
1066a9d2f8d8SRobert Watson #endif
1067a9d2f8d8SRobert Watson 
1068a9d2f8d8SRobert Watson 	if ((fp = fget_unlocked(fdp, fd)) == NULL)
1069a9d2f8d8SRobert Watson 		return (EBADF);
1070a9d2f8d8SRobert Watson #ifdef CAPABILITIES
1071a9d2f8d8SRobert Watson 	/*
1072a9d2f8d8SRobert Watson 	 * If the file descriptor is for a capability, test rights and use
1073a9d2f8d8SRobert Watson 	 * the file descriptor references by the capability.
1074a9d2f8d8SRobert Watson 	 */
1075d1b6899eSJonathan Anderson 	error = cap_funwrap(fp, CAP_POLL_EVENT, &fp_fromcap);
1076a9d2f8d8SRobert Watson 	if (error) {
1077a9d2f8d8SRobert Watson 		fdrop(fp, curthread);
1078a9d2f8d8SRobert Watson 		return (error);
1079a9d2f8d8SRobert Watson 	}
1080a9d2f8d8SRobert Watson 	if (fp != fp_fromcap) {
1081a9d2f8d8SRobert Watson 		fhold(fp_fromcap);
1082a9d2f8d8SRobert Watson 		fdrop(fp, curthread);
1083a9d2f8d8SRobert Watson 		fp = fp_fromcap;
1084a9d2f8d8SRobert Watson 	}
1085a9d2f8d8SRobert Watson #endif /* CAPABILITIES */
1086a9d2f8d8SRobert Watson 	*fpp = fp;
1087a9d2f8d8SRobert Watson 	return (0);
1088a9d2f8d8SRobert Watson }
1089a9d2f8d8SRobert Watson 
1090ace8398dSJeff Roberson /*
1091ace8398dSJeff Roberson  * Traverse the list of fds attached to this thread's seltd and check for
1092ace8398dSJeff Roberson  * completion.
1093ace8398dSJeff Roberson  */
1094ace8398dSJeff Roberson static int
1095ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
1096ace8398dSJeff Roberson {
109711b763dfSJeff Roberson 	struct filedesc *fdp;
109811b763dfSJeff Roberson 	struct selinfo *si;
1099ace8398dSJeff Roberson 	struct seltd *stp;
1100ace8398dSJeff Roberson 	struct selfd *sfp;
1101ace8398dSJeff Roberson 	struct selfd *sfn;
1102ace8398dSJeff Roberson 	struct file *fp;
11039cdacff1SJeff Roberson 	fd_mask bit;
11049cdacff1SJeff Roberson 	int fd, ev, n, idx;
1105a9d2f8d8SRobert Watson 	int error;
1106ace8398dSJeff Roberson 
110711b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
1108ace8398dSJeff Roberson 	stp = td->td_sel;
110911b763dfSJeff Roberson 	n = 0;
1110ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1111ace8398dSJeff Roberson 		fd = (int)(uintptr_t)sfp->sf_cookie;
1112ace8398dSJeff Roberson 		si = sfp->sf_si;
1113ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1114ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1115ace8398dSJeff Roberson 		if (si != NULL)
1116ace8398dSJeff Roberson 			continue;
1117a9d2f8d8SRobert Watson 		error = getselfd_cap(fdp, fd, &fp);
1118a9d2f8d8SRobert Watson 		if (error)
1119a9d2f8d8SRobert Watson 			return (error);
112011b763dfSJeff Roberson 		idx = fd / NFDBITS;
11219cdacff1SJeff Roberson 		bit = (fd_mask)1 << (fd % NFDBITS);
112211b763dfSJeff Roberson 		ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
1123bf422e5fSJeff Roberson 		fdrop(fp, td);
112411b763dfSJeff Roberson 		if (ev != 0)
112511b763dfSJeff Roberson 			n += selsetbits(ibits, obits, idx, bit, ev);
1126ace8398dSJeff Roberson 	}
1127ace8398dSJeff Roberson 	stp->st_flags = 0;
1128ace8398dSJeff Roberson 	td->td_retval[0] = n;
1129ace8398dSJeff Roberson 	return (0);
1130ace8398dSJeff Roberson }
1131ace8398dSJeff Roberson 
1132ace8398dSJeff Roberson /*
1133ace8398dSJeff Roberson  * Perform the initial filedescriptor scan and register ourselves with
1134ace8398dSJeff Roberson  * each selinfo.
1135ace8398dSJeff Roberson  */
1136265fc98fSSeigo Tanimura static int
1137b40ce416SJulian Elischer selscan(td, ibits, obits, nfd)
1138b40ce416SJulian Elischer 	struct thread *td;
1139b08f7993SSujal Patel 	fd_mask **ibits, **obits;
1140cb226aaaSPoul-Henning Kamp 	int nfd;
1141df8bae1dSRodney W. Grimes {
114211b763dfSJeff Roberson 	struct filedesc *fdp;
1143df8bae1dSRodney W. Grimes 	struct file *fp;
11449cdacff1SJeff Roberson 	fd_mask bit;
114511b763dfSJeff Roberson 	int ev, flags, end, fd;
11469cdacff1SJeff Roberson 	int n, idx;
1147a9d2f8d8SRobert Watson 	int error;
1148df8bae1dSRodney W. Grimes 
114911b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
115011b763dfSJeff Roberson 	n = 0;
11519cdacff1SJeff Roberson 	for (idx = 0, fd = 0; fd < nfd; idx++) {
115211b763dfSJeff Roberson 		end = imin(fd + NFDBITS, nfd);
115311b763dfSJeff Roberson 		for (bit = 1; fd < end; bit <<= 1, fd++) {
115411b763dfSJeff Roberson 			/* Compute the list of events we're interested in. */
115511b763dfSJeff Roberson 			flags = selflags(ibits, idx, bit);
115611b763dfSJeff Roberson 			if (flags == 0)
1157f082218cSPeter Wemm 				continue;
1158a9d2f8d8SRobert Watson 			error = getselfd_cap(fdp, fd, &fp);
1159a9d2f8d8SRobert Watson 			if (error)
1160a9d2f8d8SRobert Watson 				return (error);
1161ace8398dSJeff Roberson 			selfdalloc(td, (void *)(uintptr_t)fd);
116211b763dfSJeff Roberson 			ev = fo_poll(fp, flags, td->td_ucred, td);
1163bf422e5fSJeff Roberson 			fdrop(fp, td);
116411b763dfSJeff Roberson 			if (ev != 0)
116511b763dfSJeff Roberson 				n += selsetbits(ibits, obits, idx, bit, ev);
1166df8bae1dSRodney W. Grimes 		}
1167df8bae1dSRodney W. Grimes 	}
116811b763dfSJeff Roberson 
1169b40ce416SJulian Elischer 	td->td_retval[0] = n;
1170df8bae1dSRodney W. Grimes 	return (0);
1171df8bae1dSRodney W. Grimes }
1172df8bae1dSRodney W. Grimes 
117342d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
117442d11757SPeter Wemm struct poll_args {
117542d11757SPeter Wemm 	struct pollfd *fds;
117642d11757SPeter Wemm 	u_int	nfds;
117742d11757SPeter Wemm 	int	timeout;
117842d11757SPeter Wemm };
117942d11757SPeter Wemm #endif
118042d11757SPeter Wemm int
1181*8451d0ddSKip Macy sys_poll(td, uap)
1182b40ce416SJulian Elischer 	struct thread *td;
1183ea0237edSJonathan Lemon 	struct poll_args *uap;
118442d11757SPeter Wemm {
11852580f4e5SAndre Oppermann 	struct pollfd *bits;
11862580f4e5SAndre Oppermann 	struct pollfd smallbits[32];
118700af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
11889ae6d334SKelly Yancey 	int error = 0, timo;
1189ace8398dSJeff Roberson 	u_int nfds;
119042d11757SPeter Wemm 	size_t ni;
119142d11757SPeter Wemm 
1192d1e405c5SAlfred Perlstein 	nfds = uap->nfds;
1193374ae2a3SJeff Roberson 	if (nfds > maxfilesperproc && nfds > FD_SETSIZE)
1194ace8398dSJeff Roberson 		return (EINVAL);
119589b71647SPeter Wemm 	ni = nfds * sizeof(struct pollfd);
119642d11757SPeter Wemm 	if (ni > sizeof(smallbits))
1197a163d034SWarner Losh 		bits = malloc(ni, M_TEMP, M_WAITOK);
119842d11757SPeter Wemm 	else
119942d11757SPeter Wemm 		bits = smallbits;
1200d1e405c5SAlfred Perlstein 	error = copyin(uap->fds, bits, ni);
120142d11757SPeter Wemm 	if (error)
1202ace8398dSJeff Roberson 		goto done;
1203d1e405c5SAlfred Perlstein 	if (uap->timeout != INFTIM) {
1204d1e405c5SAlfred Perlstein 		atv.tv_sec = uap->timeout / 1000;
1205d1e405c5SAlfred Perlstein 		atv.tv_usec = (uap->timeout % 1000) * 1000;
120642d11757SPeter Wemm 		if (itimerfix(&atv)) {
120742d11757SPeter Wemm 			error = EINVAL;
1208ace8398dSJeff Roberson 			goto done;
120942d11757SPeter Wemm 		}
1210c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
121100af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
12129c386f6bSJohn Baldwin 	} else {
121300af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
12149c386f6bSJohn Baldwin 		atv.tv_usec = 0;
12159c386f6bSJohn Baldwin 	}
121600af9731SPoul-Henning Kamp 	timo = 0;
1217ace8398dSJeff Roberson 	seltdinit(td);
1218ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1219ace8398dSJeff Roberson 	for (;;) {
12202580f4e5SAndre Oppermann 		error = pollscan(td, bits, nfds);
1221ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1222ace8398dSJeff Roberson 			break;
12234da144c0SJohn Baldwin 		if (atv.tv_sec || atv.tv_usec) {
1224c21410e1SPoul-Henning Kamp 			getmicrouptime(&rtv);
122585f190e4SAlfred Perlstein 			if (timevalcmp(&rtv, &atv, >=))
1226ace8398dSJeff Roberson 				break;
122700af9731SPoul-Henning Kamp 			ttv = atv;
122800af9731SPoul-Henning Kamp 			timevalsub(&ttv, &rtv);
122900af9731SPoul-Henning Kamp 			timo = ttv.tv_sec > 24 * 60 * 60 ?
123000af9731SPoul-Henning Kamp 			    24 * 60 * 60 * hz : tvtohz(&ttv);
123142d11757SPeter Wemm 		}
1232ace8398dSJeff Roberson 		error = seltdwait(td, timo);
1233ace8398dSJeff Roberson 		if (error)
1234ace8398dSJeff Roberson 			break;
1235ace8398dSJeff Roberson 		error = pollrescan(td);
1236ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1237ace8398dSJeff Roberson 			break;
123885f190e4SAlfred Perlstein 	}
1239ace8398dSJeff Roberson 	seltdclear(td);
1240265fc98fSSeigo Tanimura 
124142d11757SPeter Wemm done:
124242d11757SPeter Wemm 	/* poll is not restarted after signals... */
124342d11757SPeter Wemm 	if (error == ERESTART)
124442d11757SPeter Wemm 		error = EINTR;
124542d11757SPeter Wemm 	if (error == EWOULDBLOCK)
124642d11757SPeter Wemm 		error = 0;
124742d11757SPeter Wemm 	if (error == 0) {
12486d8feddaSKonstantin Belousov 		error = pollout(td, bits, uap->fds, nfds);
124942d11757SPeter Wemm 		if (error)
125042d11757SPeter Wemm 			goto out;
125142d11757SPeter Wemm 	}
125242d11757SPeter Wemm out:
125342d11757SPeter Wemm 	if (ni > sizeof(smallbits))
125442d11757SPeter Wemm 		free(bits, M_TEMP);
125542d11757SPeter Wemm 	return (error);
125642d11757SPeter Wemm }
125742d11757SPeter Wemm 
125842d11757SPeter Wemm static int
1259ace8398dSJeff Roberson pollrescan(struct thread *td)
1260ace8398dSJeff Roberson {
1261ace8398dSJeff Roberson 	struct seltd *stp;
1262ace8398dSJeff Roberson 	struct selfd *sfp;
1263ace8398dSJeff Roberson 	struct selfd *sfn;
1264ace8398dSJeff Roberson 	struct selinfo *si;
1265ace8398dSJeff Roberson 	struct filedesc *fdp;
1266ace8398dSJeff Roberson 	struct file *fp;
1267ace8398dSJeff Roberson 	struct pollfd *fd;
1268ace8398dSJeff Roberson 	int n;
1269ace8398dSJeff Roberson 
1270ace8398dSJeff Roberson 	n = 0;
1271ace8398dSJeff Roberson 	fdp = td->td_proc->p_fd;
1272ace8398dSJeff Roberson 	stp = td->td_sel;
1273ace8398dSJeff Roberson 	FILEDESC_SLOCK(fdp);
1274ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1275ace8398dSJeff Roberson 		fd = (struct pollfd *)sfp->sf_cookie;
1276ace8398dSJeff Roberson 		si = sfp->sf_si;
1277ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1278ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1279ace8398dSJeff Roberson 		if (si != NULL)
1280ace8398dSJeff Roberson 			continue;
1281ace8398dSJeff Roberson 		fp = fdp->fd_ofiles[fd->fd];
1282d6f72489SJonathan Anderson #ifdef CAPABILITIES
1283d6f72489SJonathan Anderson 		if ((fp == NULL)
1284d6f72489SJonathan Anderson 		    || (cap_funwrap(fp, CAP_POLL_EVENT, &fp) != 0)) {
1285d6f72489SJonathan Anderson #else
1286ace8398dSJeff Roberson 		if (fp == NULL) {
1287d6f72489SJonathan Anderson #endif
1288ace8398dSJeff Roberson 			fd->revents = POLLNVAL;
1289ace8398dSJeff Roberson 			n++;
1290ace8398dSJeff Roberson 			continue;
1291ace8398dSJeff Roberson 		}
1292d6f72489SJonathan Anderson 
1293ace8398dSJeff Roberson 		/*
1294ace8398dSJeff Roberson 		 * Note: backend also returns POLLHUP and
1295ace8398dSJeff Roberson 		 * POLLERR if appropriate.
1296ace8398dSJeff Roberson 		 */
1297ace8398dSJeff Roberson 		fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1298ace8398dSJeff Roberson 		if (fd->revents != 0)
1299ace8398dSJeff Roberson 			n++;
1300ace8398dSJeff Roberson 	}
1301ace8398dSJeff Roberson 	FILEDESC_SUNLOCK(fdp);
1302ace8398dSJeff Roberson 	stp->st_flags = 0;
1303ace8398dSJeff Roberson 	td->td_retval[0] = n;
1304ace8398dSJeff Roberson 	return (0);
1305ace8398dSJeff Roberson }
1306ace8398dSJeff Roberson 
1307ace8398dSJeff Roberson 
1308ace8398dSJeff Roberson static int
13096d8feddaSKonstantin Belousov pollout(td, fds, ufds, nfd)
13106d8feddaSKonstantin Belousov 	struct thread *td;
1311ae81968fSRobert Watson 	struct pollfd *fds;
1312ae81968fSRobert Watson 	struct pollfd *ufds;
1313ae81968fSRobert Watson 	u_int nfd;
1314ae81968fSRobert Watson {
1315ae81968fSRobert Watson 	int error = 0;
1316ae81968fSRobert Watson 	u_int i = 0;
13176d8feddaSKonstantin Belousov 	u_int n = 0;
1318ae81968fSRobert Watson 
1319ae81968fSRobert Watson 	for (i = 0; i < nfd; i++) {
1320ae81968fSRobert Watson 		error = copyout(&fds->revents, &ufds->revents,
1321ae81968fSRobert Watson 		    sizeof(ufds->revents));
1322ae81968fSRobert Watson 		if (error)
1323ae81968fSRobert Watson 			return (error);
13246d8feddaSKonstantin Belousov 		if (fds->revents != 0)
13256d8feddaSKonstantin Belousov 			n++;
1326ae81968fSRobert Watson 		fds++;
1327ae81968fSRobert Watson 		ufds++;
1328ae81968fSRobert Watson 	}
13296d8feddaSKonstantin Belousov 	td->td_retval[0] = n;
1330ae81968fSRobert Watson 	return (0);
1331ae81968fSRobert Watson }
1332ae81968fSRobert Watson 
1333ae81968fSRobert Watson static int
1334b40ce416SJulian Elischer pollscan(td, fds, nfd)
1335b40ce416SJulian Elischer 	struct thread *td;
133642d11757SPeter Wemm 	struct pollfd *fds;
1337ea0237edSJonathan Lemon 	u_int nfd;
133842d11757SPeter Wemm {
1339ace8398dSJeff Roberson 	struct filedesc *fdp = td->td_proc->p_fd;
134042d11757SPeter Wemm 	int i;
134142d11757SPeter Wemm 	struct file *fp;
134242d11757SPeter Wemm 	int n = 0;
134342d11757SPeter Wemm 
13445e3f7694SRobert Watson 	FILEDESC_SLOCK(fdp);
1345eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
1346337c9691SJordan K. Hubbard 		if (fds->fd >= fdp->fd_nfiles) {
134742d11757SPeter Wemm 			fds->revents = POLLNVAL;
134842d11757SPeter Wemm 			n++;
1349337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
1350337c9691SJordan K. Hubbard 			fds->revents = 0;
135142d11757SPeter Wemm 		} else {
135242d11757SPeter Wemm 			fp = fdp->fd_ofiles[fds->fd];
1353d6f72489SJonathan Anderson #ifdef CAPABILITIES
1354d6f72489SJonathan Anderson 			if ((fp == NULL)
1355d6f72489SJonathan Anderson 			    || (cap_funwrap(fp, CAP_POLL_EVENT, &fp) != 0)) {
1356d6f72489SJonathan Anderson #else
1357279d7226SMatthew Dillon 			if (fp == NULL) {
1358d6f72489SJonathan Anderson #endif
135942d11757SPeter Wemm 				fds->revents = POLLNVAL;
136042d11757SPeter Wemm 				n++;
136142d11757SPeter Wemm 			} else {
13622087c896SBruce Evans 				/*
13632087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
13642087c896SBruce Evans 				 * POLLERR if appropriate.
13652087c896SBruce Evans 				 */
1366ace8398dSJeff Roberson 				selfdalloc(td, fds);
136713ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
1368ea6027a8SRobert Watson 				    td->td_ucred, td);
1369f2159cc7SKonstantin Belousov 				/*
1370f2159cc7SKonstantin Belousov 				 * POSIX requires POLLOUT to be never
1371f2159cc7SKonstantin Belousov 				 * set simultaneously with POLLHUP.
1372f2159cc7SKonstantin Belousov 				 */
1373f2159cc7SKonstantin Belousov 				if ((fds->revents & POLLHUP) != 0)
1374f2159cc7SKonstantin Belousov 					fds->revents &= ~POLLOUT;
1375f2159cc7SKonstantin Belousov 
137642d11757SPeter Wemm 				if (fds->revents != 0)
137742d11757SPeter Wemm 					n++;
137842d11757SPeter Wemm 			}
137942d11757SPeter Wemm 		}
138042d11757SPeter Wemm 	}
13815e3f7694SRobert Watson 	FILEDESC_SUNLOCK(fdp);
1382b40ce416SJulian Elischer 	td->td_retval[0] = n;
138342d11757SPeter Wemm 	return (0);
138442d11757SPeter Wemm }
138542d11757SPeter Wemm 
138642d11757SPeter Wemm /*
138742d11757SPeter Wemm  * OpenBSD poll system call.
13880c14ff0eSRobert Watson  *
138942d11757SPeter Wemm  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
139042d11757SPeter Wemm  */
139142d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
139242d11757SPeter Wemm struct openbsd_poll_args {
139342d11757SPeter Wemm 	struct pollfd *fds;
139442d11757SPeter Wemm 	u_int	nfds;
139542d11757SPeter Wemm 	int	timeout;
139642d11757SPeter Wemm };
139742d11757SPeter Wemm #endif
139842d11757SPeter Wemm int
1399*8451d0ddSKip Macy sys_openbsd_poll(td, uap)
1400b40ce416SJulian Elischer 	register struct thread *td;
140142d11757SPeter Wemm 	register struct openbsd_poll_args *uap;
140242d11757SPeter Wemm {
1403*8451d0ddSKip Macy 	return (sys_poll(td, (struct poll_args *)uap));
140442d11757SPeter Wemm }
140542d11757SPeter Wemm 
140685f190e4SAlfred Perlstein /*
1407ace8398dSJeff Roberson  * XXX This was created specifically to support netncp and netsmb.  This
1408ace8398dSJeff Roberson  * allows the caller to specify a socket to wait for events on.  It returns
1409ace8398dSJeff Roberson  * 0 if any events matched and an error otherwise.  There is no way to
1410ace8398dSJeff Roberson  * determine which events fired.
141185f190e4SAlfred Perlstein  */
1412ace8398dSJeff Roberson int
1413ace8398dSJeff Roberson selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td)
141485f190e4SAlfred Perlstein {
1415ace8398dSJeff Roberson 	struct timeval atv, rtv, ttv;
1416ace8398dSJeff Roberson 	int error, timo;
141785f190e4SAlfred Perlstein 
1418ace8398dSJeff Roberson 	if (tvp != NULL) {
1419ace8398dSJeff Roberson 		atv = *tvp;
1420ace8398dSJeff Roberson 		if (itimerfix(&atv))
1421ace8398dSJeff Roberson 			return (EINVAL);
1422ace8398dSJeff Roberson 		getmicrouptime(&rtv);
1423ace8398dSJeff Roberson 		timevaladd(&atv, &rtv);
1424ace8398dSJeff Roberson 	} else {
1425ace8398dSJeff Roberson 		atv.tv_sec = 0;
1426ace8398dSJeff Roberson 		atv.tv_usec = 0;
1427ace8398dSJeff Roberson 	}
1428ace8398dSJeff Roberson 
1429ace8398dSJeff Roberson 	timo = 0;
1430ace8398dSJeff Roberson 	seltdinit(td);
1431ace8398dSJeff Roberson 	/*
1432ace8398dSJeff Roberson 	 * Iterate until the timeout expires or the socket becomes ready.
1433ace8398dSJeff Roberson 	 */
1434ace8398dSJeff Roberson 	for (;;) {
1435ace8398dSJeff Roberson 		selfdalloc(td, NULL);
1436ace8398dSJeff Roberson 		error = sopoll(so, events, NULL, td);
1437ace8398dSJeff Roberson 		/* error here is actually the ready events. */
1438ace8398dSJeff Roberson 		if (error)
1439ace8398dSJeff Roberson 			return (0);
1440ace8398dSJeff Roberson 		if (atv.tv_sec || atv.tv_usec) {
1441ace8398dSJeff Roberson 			getmicrouptime(&rtv);
1442ace8398dSJeff Roberson 			if (timevalcmp(&rtv, &atv, >=)) {
1443ace8398dSJeff Roberson 				seltdclear(td);
1444ace8398dSJeff Roberson 				return (EWOULDBLOCK);
1445ace8398dSJeff Roberson 			}
1446ace8398dSJeff Roberson 			ttv = atv;
1447ace8398dSJeff Roberson 			timevalsub(&ttv, &rtv);
1448ace8398dSJeff Roberson 			timo = ttv.tv_sec > 24 * 60 * 60 ?
1449ace8398dSJeff Roberson 			    24 * 60 * 60 * hz : tvtohz(&ttv);
1450ace8398dSJeff Roberson 		}
1451ace8398dSJeff Roberson 		error = seltdwait(td, timo);
1452ace8398dSJeff Roberson 		seltdclear(td);
1453ace8398dSJeff Roberson 		if (error)
1454ace8398dSJeff Roberson 			break;
1455ace8398dSJeff Roberson 	}
1456ace8398dSJeff Roberson 	/* XXX Duplicates ncp/smb behavior. */
1457ace8398dSJeff Roberson 	if (error == ERESTART)
1458ace8398dSJeff Roberson 		error = 0;
1459ace8398dSJeff Roberson 	return (error);
1460ace8398dSJeff Roberson }
1461ace8398dSJeff Roberson 
1462ace8398dSJeff Roberson /*
1463ace8398dSJeff Roberson  * Preallocate two selfds associated with 'cookie'.  Some fo_poll routines
1464ace8398dSJeff Roberson  * have two select sets, one for read and another for write.
1465ace8398dSJeff Roberson  */
1466ace8398dSJeff Roberson static void
1467ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie)
1468ace8398dSJeff Roberson {
1469ace8398dSJeff Roberson 	struct seltd *stp;
1470ace8398dSJeff Roberson 
1471ace8398dSJeff Roberson 	stp = td->td_sel;
1472ace8398dSJeff Roberson 	if (stp->st_free1 == NULL)
1473ace8398dSJeff Roberson 		stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1474ace8398dSJeff Roberson 	stp->st_free1->sf_td = stp;
1475ace8398dSJeff Roberson 	stp->st_free1->sf_cookie = cookie;
1476ace8398dSJeff Roberson 	if (stp->st_free2 == NULL)
1477ace8398dSJeff Roberson 		stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1478ace8398dSJeff Roberson 	stp->st_free2->sf_td = stp;
1479ace8398dSJeff Roberson 	stp->st_free2->sf_cookie = cookie;
1480ace8398dSJeff Roberson }
1481ace8398dSJeff Roberson 
1482ace8398dSJeff Roberson static void
1483ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp)
1484ace8398dSJeff Roberson {
1485ace8398dSJeff Roberson 	STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
1486ace8398dSJeff Roberson 	mtx_lock(sfp->sf_mtx);
1487ace8398dSJeff Roberson 	if (sfp->sf_si)
1488ace8398dSJeff Roberson 		TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1489ace8398dSJeff Roberson 	mtx_unlock(sfp->sf_mtx);
1490ace8398dSJeff Roberson 	uma_zfree(selfd_zone, sfp);
149185f190e4SAlfred Perlstein }
149285f190e4SAlfred Perlstein 
14936aba400aSAttilio Rao /* Drain the waiters tied to all the selfd belonging the specified selinfo. */
14946aba400aSAttilio Rao void
14956aba400aSAttilio Rao seldrain(sip)
14966aba400aSAttilio Rao         struct selinfo *sip;
14976aba400aSAttilio Rao {
14986aba400aSAttilio Rao 
14996aba400aSAttilio Rao 	/*
15006aba400aSAttilio Rao 	 * This feature is already provided by doselwakeup(), thus it is
15016aba400aSAttilio Rao 	 * enough to go for it.
15026aba400aSAttilio Rao 	 * Eventually, the context, should take care to avoid races
15036aba400aSAttilio Rao 	 * between thread calling select()/poll() and file descriptor
15046aba400aSAttilio Rao 	 * detaching, but, again, the races are just the same as
15056aba400aSAttilio Rao 	 * selwakeup().
15066aba400aSAttilio Rao 	 */
15076aba400aSAttilio Rao         doselwakeup(sip, -1);
15086aba400aSAttilio Rao }
15096aba400aSAttilio Rao 
1510df8bae1dSRodney W. Grimes /*
1511df8bae1dSRodney W. Grimes  * Record a select request.
1512df8bae1dSRodney W. Grimes  */
1513df8bae1dSRodney W. Grimes void
1514df8bae1dSRodney W. Grimes selrecord(selector, sip)
1515b40ce416SJulian Elischer 	struct thread *selector;
1516df8bae1dSRodney W. Grimes 	struct selinfo *sip;
1517df8bae1dSRodney W. Grimes {
1518ace8398dSJeff Roberson 	struct selfd *sfp;
1519ace8398dSJeff Roberson 	struct seltd *stp;
1520ace8398dSJeff Roberson 	struct mtx *mtxp;
1521df8bae1dSRodney W. Grimes 
1522ace8398dSJeff Roberson 	stp = selector->td_sel;
152385f190e4SAlfred Perlstein 	/*
1524ace8398dSJeff Roberson 	 * Don't record when doing a rescan.
152585f190e4SAlfred Perlstein 	 */
1526ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_RESCAN)
1527ace8398dSJeff Roberson 		return;
1528ace8398dSJeff Roberson 	/*
1529ace8398dSJeff Roberson 	 * Grab one of the preallocated descriptors.
1530ace8398dSJeff Roberson 	 */
1531ace8398dSJeff Roberson 	sfp = NULL;
1532ace8398dSJeff Roberson 	if ((sfp = stp->st_free1) != NULL)
1533ace8398dSJeff Roberson 		stp->st_free1 = NULL;
1534ace8398dSJeff Roberson 	else if ((sfp = stp->st_free2) != NULL)
1535ace8398dSJeff Roberson 		stp->st_free2 = NULL;
1536ace8398dSJeff Roberson 	else
1537ace8398dSJeff Roberson 		panic("selrecord: No free selfd on selq");
15382141453eSJeff Roberson 	mtxp = sip->si_mtx;
15392141453eSJeff Roberson 	if (mtxp == NULL)
15402141453eSJeff Roberson 		mtxp = mtx_pool_find(mtxpool_select, sip);
1541ace8398dSJeff Roberson 	/*
1542ace8398dSJeff Roberson 	 * Initialize the sfp and queue it in the thread.
1543ace8398dSJeff Roberson 	 */
1544ace8398dSJeff Roberson 	sfp->sf_si = sip;
1545ace8398dSJeff Roberson 	sfp->sf_mtx = mtxp;
1546ace8398dSJeff Roberson 	STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1547ace8398dSJeff Roberson 	/*
1548ace8398dSJeff Roberson 	 * Now that we've locked the sip, check for initialization.
1549ace8398dSJeff Roberson 	 */
1550ace8398dSJeff Roberson 	mtx_lock(mtxp);
1551ace8398dSJeff Roberson 	if (sip->si_mtx == NULL) {
1552ace8398dSJeff Roberson 		sip->si_mtx = mtxp;
1553ace8398dSJeff Roberson 		TAILQ_INIT(&sip->si_tdlist);
155485f190e4SAlfred Perlstein 	}
1555ace8398dSJeff Roberson 	/*
1556ace8398dSJeff Roberson 	 * Add this thread to the list of selfds listening on this selinfo.
1557ace8398dSJeff Roberson 	 */
1558ace8398dSJeff Roberson 	TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1559ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1560df8bae1dSRodney W. Grimes }
1561df8bae1dSRodney W. Grimes 
1562512824f8SSeigo Tanimura /* Wake up a selecting thread. */
1563df8bae1dSRodney W. Grimes void
1564df8bae1dSRodney W. Grimes selwakeup(sip)
156585f190e4SAlfred Perlstein 	struct selinfo *sip;
1566df8bae1dSRodney W. Grimes {
1567512824f8SSeigo Tanimura 	doselwakeup(sip, -1);
1568512824f8SSeigo Tanimura }
1569512824f8SSeigo Tanimura 
1570512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */
1571512824f8SSeigo Tanimura void
1572512824f8SSeigo Tanimura selwakeuppri(sip, pri)
1573512824f8SSeigo Tanimura 	struct selinfo *sip;
1574512824f8SSeigo Tanimura 	int pri;
1575512824f8SSeigo Tanimura {
1576512824f8SSeigo Tanimura 	doselwakeup(sip, pri);
1577512824f8SSeigo Tanimura }
1578512824f8SSeigo Tanimura 
1579512824f8SSeigo Tanimura /*
1580512824f8SSeigo Tanimura  * Do a wakeup when a selectable event occurs.
1581512824f8SSeigo Tanimura  */
1582512824f8SSeigo Tanimura static void
1583512824f8SSeigo Tanimura doselwakeup(sip, pri)
1584512824f8SSeigo Tanimura 	struct selinfo *sip;
1585512824f8SSeigo Tanimura 	int pri;
1586512824f8SSeigo Tanimura {
1587ace8398dSJeff Roberson 	struct selfd *sfp;
1588ace8398dSJeff Roberson 	struct selfd *sfn;
1589ace8398dSJeff Roberson 	struct seltd *stp;
1590df8bae1dSRodney W. Grimes 
1591ace8398dSJeff Roberson 	/* If it's not initialized there can't be any waiters. */
1592ace8398dSJeff Roberson 	if (sip->si_mtx == NULL)
1593b40ce416SJulian Elischer 		return;
1594ace8398dSJeff Roberson 	/*
1595ace8398dSJeff Roberson 	 * Locking the selinfo locks all selfds associated with it.
1596ace8398dSJeff Roberson 	 */
1597ace8398dSJeff Roberson 	mtx_lock(sip->si_mtx);
1598ace8398dSJeff Roberson 	TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1599ace8398dSJeff Roberson 		/*
1600ace8398dSJeff Roberson 		 * Once we remove this sfp from the list and clear the
1601ace8398dSJeff Roberson 		 * sf_si seltdclear will know to ignore this si.
1602ace8398dSJeff Roberson 		 */
1603ace8398dSJeff Roberson 		TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1604ace8398dSJeff Roberson 		sfp->sf_si = NULL;
1605ace8398dSJeff Roberson 		stp = sfp->sf_td;
1606ace8398dSJeff Roberson 		mtx_lock(&stp->st_mtx);
1607ace8398dSJeff Roberson 		stp->st_flags |= SELTD_PENDING;
1608ace8398dSJeff Roberson 		cv_broadcastpri(&stp->st_wait, pri);
1609ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1610b40ce416SJulian Elischer 	}
1611ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1612ace8398dSJeff Roberson }
1613ace8398dSJeff Roberson 
1614ace8398dSJeff Roberson static void
1615ace8398dSJeff Roberson seltdinit(struct thread *td)
1616ace8398dSJeff Roberson {
1617ace8398dSJeff Roberson 	struct seltd *stp;
1618ace8398dSJeff Roberson 
1619ace8398dSJeff Roberson 	if ((stp = td->td_sel) != NULL)
1620ace8398dSJeff Roberson 		goto out;
1621ace8398dSJeff Roberson 	td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1622ace8398dSJeff Roberson 	mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1623ace8398dSJeff Roberson 	cv_init(&stp->st_wait, "select");
1624ace8398dSJeff Roberson out:
1625ace8398dSJeff Roberson 	stp->st_flags = 0;
1626ace8398dSJeff Roberson 	STAILQ_INIT(&stp->st_selq);
1627ace8398dSJeff Roberson }
1628ace8398dSJeff Roberson 
1629ace8398dSJeff Roberson static int
1630ace8398dSJeff Roberson seltdwait(struct thread *td, int timo)
1631ace8398dSJeff Roberson {
1632ace8398dSJeff Roberson 	struct seltd *stp;
1633ace8398dSJeff Roberson 	int error;
1634ace8398dSJeff Roberson 
1635ace8398dSJeff Roberson 	stp = td->td_sel;
1636ace8398dSJeff Roberson 	/*
1637ace8398dSJeff Roberson 	 * An event of interest may occur while we do not hold the seltd
1638ace8398dSJeff Roberson 	 * locked so check the pending flag before we sleep.
1639ace8398dSJeff Roberson 	 */
1640ace8398dSJeff Roberson 	mtx_lock(&stp->st_mtx);
1641ace8398dSJeff Roberson 	/*
1642ace8398dSJeff Roberson 	 * Any further calls to selrecord will be a rescan.
1643ace8398dSJeff Roberson 	 */
1644ace8398dSJeff Roberson 	stp->st_flags |= SELTD_RESCAN;
1645ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_PENDING) {
1646ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1647ace8398dSJeff Roberson 		return (0);
1648ace8398dSJeff Roberson 	}
1649ace8398dSJeff Roberson 	if (timo > 0)
1650ace8398dSJeff Roberson 		error = cv_timedwait_sig(&stp->st_wait, &stp->st_mtx, timo);
1651ace8398dSJeff Roberson 	else
1652ace8398dSJeff Roberson 		error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
1653ace8398dSJeff Roberson 	mtx_unlock(&stp->st_mtx);
1654ace8398dSJeff Roberson 
1655ace8398dSJeff Roberson 	return (error);
1656ace8398dSJeff Roberson }
1657ace8398dSJeff Roberson 
1658ace8398dSJeff Roberson void
1659ace8398dSJeff Roberson seltdfini(struct thread *td)
1660ace8398dSJeff Roberson {
1661ace8398dSJeff Roberson 	struct seltd *stp;
1662ace8398dSJeff Roberson 
1663ace8398dSJeff Roberson 	stp = td->td_sel;
1664ace8398dSJeff Roberson 	if (stp == NULL)
1665ace8398dSJeff Roberson 		return;
1666ace8398dSJeff Roberson 	if (stp->st_free1)
1667ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free1);
1668ace8398dSJeff Roberson 	if (stp->st_free2)
1669ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free2);
1670ace8398dSJeff Roberson 	td->td_sel = NULL;
1671ace8398dSJeff Roberson 	free(stp, M_SELECT);
1672ace8398dSJeff Roberson }
1673ace8398dSJeff Roberson 
1674ace8398dSJeff Roberson /*
1675ace8398dSJeff Roberson  * Remove the references to the thread from all of the objects we were
1676ace8398dSJeff Roberson  * polling.
1677ace8398dSJeff Roberson  */
1678ace8398dSJeff Roberson static void
1679ace8398dSJeff Roberson seltdclear(struct thread *td)
1680ace8398dSJeff Roberson {
1681ace8398dSJeff Roberson 	struct seltd *stp;
1682ace8398dSJeff Roberson 	struct selfd *sfp;
1683ace8398dSJeff Roberson 	struct selfd *sfn;
1684ace8398dSJeff Roberson 
1685ace8398dSJeff Roberson 	stp = td->td_sel;
1686ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
1687ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1688ace8398dSJeff Roberson 	stp->st_flags = 0;
1689df8bae1dSRodney W. Grimes }
1690265fc98fSSeigo Tanimura 
16914d77a549SAlfred Perlstein static void selectinit(void *);
1692ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
1693265fc98fSSeigo Tanimura static void
1694ace8398dSJeff Roberson selectinit(void *dummy __unused)
1695265fc98fSSeigo Tanimura {
16962141453eSJeff Roberson 
1697ace8398dSJeff Roberson 	selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL,
1698ace8398dSJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, 0);
16992141453eSJeff Roberson 	mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);
1700265fc98fSSeigo Tanimura }
1701