xref: /freebsd/sys/kern/sys_generic.c (revision 6d8fedda2ce56c34d484708c7ba4ce5019c5a620)
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 
402de92a38SPeter Wemm #include "opt_compat.h"
41db6a20e2SGarrett Wollman #include "opt_ktrace.h"
42db6a20e2SGarrett Wollman 
43df8bae1dSRodney W. Grimes #include <sys/param.h>
44df8bae1dSRodney W. Grimes #include <sys/systm.h>
45d2d3e875SBruce Evans #include <sys/sysproto.h>
46df8bae1dSRodney W. Grimes #include <sys/filedesc.h>
4720982410SBruce Evans #include <sys/filio.h>
483ac4d1efSBruce Evans #include <sys/fcntl.h>
49df8bae1dSRodney W. Grimes #include <sys/file.h>
50df8bae1dSRodney W. Grimes #include <sys/proc.h>
51797f2d22SPoul-Henning Kamp #include <sys/signalvar.h>
52df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
53df8bae1dSRodney W. Grimes #include <sys/uio.h>
54df8bae1dSRodney W. Grimes #include <sys/kernel.h>
55e4650294SJohn Baldwin #include <sys/ktr.h>
56104a9b7eSAlexander Kabaev #include <sys/limits.h>
57df8bae1dSRodney W. Grimes #include <sys/malloc.h>
5842d11757SPeter Wemm #include <sys/poll.h>
5989b71647SPeter Wemm #include <sys/resourcevar.h>
600a2c3d48SGarrett Wollman #include <sys/selinfo.h>
6144f3b092SJohn Baldwin #include <sys/sleepqueue.h>
628f19eb88SIan Dowse #include <sys/syscallsubr.h>
638cb96f20SPeter Wemm #include <sys/sysctl.h>
6442d11757SPeter Wemm #include <sys/sysent.h>
659bbee259SAndrey A. Chernov #include <sys/vnode.h>
66279d7226SMatthew Dillon #include <sys/bio.h>
67279d7226SMatthew Dillon #include <sys/buf.h>
68265fc98fSSeigo Tanimura #include <sys/condvar.h>
69df8bae1dSRodney W. Grimes #ifdef KTRACE
70df8bae1dSRodney W. Grimes #include <sys/ktrace.h>
71df8bae1dSRodney W. Grimes #endif
72df8bae1dSRodney W. Grimes 
73e4650294SJohn Baldwin #include <security/audit/audit.h>
74ace8398dSJeff Roberson 
75a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
76a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
77a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_IOV, "iov", "large iov's");
7855166637SPoul-Henning Kamp 
79*6d8feddaSKonstantin Belousov static int	pollout(struct thread *, struct pollfd *, struct pollfd *,
80*6d8feddaSKonstantin Belousov 		    u_int);
81bbbb04ceSAlfred Perlstein static int	pollscan(struct thread *, struct pollfd *, u_int);
82ace8398dSJeff Roberson static int	pollrescan(struct thread *);
83bbbb04ceSAlfred Perlstein static int	selscan(struct thread *, fd_mask **, fd_mask **, int);
84ace8398dSJeff Roberson static int	selrescan(struct thread *, fd_mask **, fd_mask **);
85ace8398dSJeff Roberson static void	selfdalloc(struct thread *, void *);
86ace8398dSJeff Roberson static void	selfdfree(struct seltd *, struct selfd *);
87bcd9e0ddSJohn Baldwin static int	dofileread(struct thread *, int, struct file *, struct uio *,
88bcd9e0ddSJohn Baldwin 		    off_t, int);
89bcd9e0ddSJohn Baldwin static int	dofilewrite(struct thread *, int, struct file *, struct uio *,
90bcd9e0ddSJohn Baldwin 		    off_t, int);
91512824f8SSeigo Tanimura static void	doselwakeup(struct selinfo *, int);
92ace8398dSJeff Roberson static void	seltdinit(struct thread *);
93ace8398dSJeff Roberson static int	seltdwait(struct thread *, int);
94ace8398dSJeff Roberson static void	seltdclear(struct thread *);
95ace8398dSJeff Roberson 
96ace8398dSJeff Roberson /*
97ace8398dSJeff Roberson  * One seltd per-thread allocated on demand as needed.
98ace8398dSJeff Roberson  *
99ace8398dSJeff Roberson  *	t - protected by st_mtx
100ace8398dSJeff Roberson  * 	k - Only accessed by curthread or read-only
101ace8398dSJeff Roberson  */
102ace8398dSJeff Roberson struct seltd {
103ace8398dSJeff Roberson 	STAILQ_HEAD(, selfd)	st_selq;	/* (k) List of selfds. */
104ace8398dSJeff Roberson 	struct selfd		*st_free1;	/* (k) free fd for read set. */
105ace8398dSJeff Roberson 	struct selfd		*st_free2;	/* (k) free fd for write set. */
106ace8398dSJeff Roberson 	struct mtx		st_mtx;		/* Protects struct seltd */
107ace8398dSJeff Roberson 	struct cv		st_wait;	/* (t) Wait channel. */
108ace8398dSJeff Roberson 	int			st_flags;	/* (t) SELTD_ flags. */
109ace8398dSJeff Roberson };
110ace8398dSJeff Roberson 
111ace8398dSJeff Roberson #define	SELTD_PENDING	0x0001			/* We have pending events. */
112ace8398dSJeff Roberson #define	SELTD_RESCAN	0x0002			/* Doing a rescan. */
113ace8398dSJeff Roberson 
114ace8398dSJeff Roberson /*
115ace8398dSJeff Roberson  * One selfd allocated per-thread per-file-descriptor.
116ace8398dSJeff Roberson  *	f - protected by sf_mtx
117ace8398dSJeff Roberson  */
118ace8398dSJeff Roberson struct selfd {
119ace8398dSJeff Roberson 	STAILQ_ENTRY(selfd)	sf_link;	/* (k) fds owned by this td. */
120ace8398dSJeff Roberson 	TAILQ_ENTRY(selfd)	sf_threads;	/* (f) fds on this selinfo. */
121ace8398dSJeff Roberson 	struct selinfo		*sf_si;		/* (f) selinfo when linked. */
122ace8398dSJeff Roberson 	struct mtx		*sf_mtx;	/* Pointer to selinfo mtx. */
123ace8398dSJeff Roberson 	struct seltd		*sf_td;		/* (k) owning seltd. */
124ace8398dSJeff Roberson 	void			*sf_cookie;	/* (k) fd or pollfd. */
125ace8398dSJeff Roberson };
126ace8398dSJeff Roberson 
127ace8398dSJeff Roberson static uma_zone_t selfd_zone;
1282141453eSJeff Roberson static struct mtx_pool *mtxpool_select;
1298fe387abSDmitrij Tejblum 
130d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
131df8bae1dSRodney W. Grimes struct read_args {
132df8bae1dSRodney W. Grimes 	int	fd;
133134e06feSBruce Evans 	void	*buf;
134134e06feSBruce Evans 	size_t	nbyte;
135df8bae1dSRodney W. Grimes };
136d2d3e875SBruce Evans #endif
13726f9a767SRodney W. Grimes int
138b40ce416SJulian Elischer read(td, uap)
139b40ce416SJulian Elischer 	struct thread *td;
140b064d43dSMatthew Dillon 	struct read_args *uap;
141df8bae1dSRodney W. Grimes {
142bcd9e0ddSJohn Baldwin 	struct uio auio;
143bcd9e0ddSJohn Baldwin 	struct iovec aiov;
144279d7226SMatthew Dillon 	int error;
145df8bae1dSRodney W. Grimes 
146bcd9e0ddSJohn Baldwin 	if (uap->nbyte > INT_MAX)
147bcd9e0ddSJohn Baldwin 		return (EINVAL);
148bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
149bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
150bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
151bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
152bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
153bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
154bcd9e0ddSJohn Baldwin 	error = kern_readv(td, uap->fd, &auio);
155279d7226SMatthew Dillon 	return(error);
156df8bae1dSRodney W. Grimes }
157df8bae1dSRodney W. Grimes 
158df8bae1dSRodney W. Grimes /*
159bcd9e0ddSJohn Baldwin  * Positioned read system call
1604160ccd9SAlan Cox  */
1614160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
1624160ccd9SAlan Cox struct pread_args {
1634160ccd9SAlan Cox 	int	fd;
1644160ccd9SAlan Cox 	void	*buf;
1654160ccd9SAlan Cox 	size_t	nbyte;
1668fe387abSDmitrij Tejblum 	int	pad;
1674160ccd9SAlan Cox 	off_t	offset;
1684160ccd9SAlan Cox };
1694160ccd9SAlan Cox #endif
1704160ccd9SAlan Cox int
171b40ce416SJulian Elischer pread(td, uap)
172b40ce416SJulian Elischer 	struct thread *td;
173b064d43dSMatthew Dillon 	struct pread_args *uap;
1744160ccd9SAlan Cox {
1754160ccd9SAlan Cox 	struct uio auio;
1764160ccd9SAlan Cox 	struct iovec aiov;
177bcd9e0ddSJohn Baldwin 	int error;
1784160ccd9SAlan Cox 
179bcd9e0ddSJohn Baldwin 	if (uap->nbyte > INT_MAX)
180bcd9e0ddSJohn Baldwin 		return (EINVAL);
181bcd9e0ddSJohn Baldwin 	aiov.iov_base = uap->buf;
182bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
1834160ccd9SAlan Cox 	auio.uio_iov = &aiov;
1844160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
185bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
1864160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
187bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, &auio, uap->offset);
1884160ccd9SAlan Cox 	return(error);
1894160ccd9SAlan Cox }
1904160ccd9SAlan Cox 
191c2815ad5SPeter Wemm int
192c2815ad5SPeter Wemm freebsd6_pread(td, uap)
193c2815ad5SPeter Wemm 	struct thread *td;
194c2815ad5SPeter Wemm 	struct freebsd6_pread_args *uap;
195c2815ad5SPeter Wemm {
196c2815ad5SPeter Wemm 	struct pread_args oargs;
197c2815ad5SPeter Wemm 
198c2815ad5SPeter Wemm 	oargs.fd = uap->fd;
199c2815ad5SPeter Wemm 	oargs.buf = uap->buf;
200c2815ad5SPeter Wemm 	oargs.nbyte = uap->nbyte;
201c2815ad5SPeter Wemm 	oargs.offset = uap->offset;
202c2815ad5SPeter Wemm 	return (pread(td, &oargs));
203c2815ad5SPeter Wemm }
204c2815ad5SPeter Wemm 
2054160ccd9SAlan Cox /*
206df8bae1dSRodney W. Grimes  * Scatter read system call.
207df8bae1dSRodney W. Grimes  */
208d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
209df8bae1dSRodney W. Grimes struct readv_args {
2107147b19dSBruce Evans 	int	fd;
211df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
212df8bae1dSRodney W. Grimes 	u_int	iovcnt;
213df8bae1dSRodney W. Grimes };
214d2d3e875SBruce Evans #endif
21526f9a767SRodney W. Grimes int
216552afd9cSPoul-Henning Kamp readv(struct thread *td, struct readv_args *uap)
217df8bae1dSRodney W. Grimes {
218b88ec951SJohn Baldwin 	struct uio *auio;
219b88ec951SJohn Baldwin 	int error;
220b88ec951SJohn Baldwin 
221b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
222b88ec951SJohn Baldwin 	if (error)
223b88ec951SJohn Baldwin 		return (error);
224b88ec951SJohn Baldwin 	error = kern_readv(td, uap->fd, auio);
225b88ec951SJohn Baldwin 	free(auio, M_IOV);
226b88ec951SJohn Baldwin 	return (error);
227b88ec951SJohn Baldwin }
228b88ec951SJohn Baldwin 
229b88ec951SJohn Baldwin int
230b88ec951SJohn Baldwin kern_readv(struct thread *td, int fd, struct uio *auio)
231b88ec951SJohn Baldwin {
232b064d43dSMatthew Dillon 	struct file *fp;
233bcd9e0ddSJohn Baldwin 	int error;
234bcd9e0ddSJohn Baldwin 
235bcd9e0ddSJohn Baldwin 	error = fget_read(td, fd, &fp);
236bcd9e0ddSJohn Baldwin 	if (error)
237bcd9e0ddSJohn Baldwin 		return (error);
238bcd9e0ddSJohn Baldwin 	error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
239bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
240bcd9e0ddSJohn Baldwin 	return (error);
241bcd9e0ddSJohn Baldwin }
242bcd9e0ddSJohn Baldwin 
243bcd9e0ddSJohn Baldwin /*
244bcd9e0ddSJohn Baldwin  * Scatter positioned read system call.
245bcd9e0ddSJohn Baldwin  */
246bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
247bcd9e0ddSJohn Baldwin struct preadv_args {
248bcd9e0ddSJohn Baldwin 	int	fd;
249bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
250bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
251bcd9e0ddSJohn Baldwin 	off_t	offset;
252bcd9e0ddSJohn Baldwin };
253bcd9e0ddSJohn Baldwin #endif
254bcd9e0ddSJohn Baldwin int
255bcd9e0ddSJohn Baldwin preadv(struct thread *td, struct preadv_args *uap)
256bcd9e0ddSJohn Baldwin {
257bcd9e0ddSJohn Baldwin 	struct uio *auio;
258bcd9e0ddSJohn Baldwin 	int error;
259bcd9e0ddSJohn Baldwin 
260bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
261bcd9e0ddSJohn Baldwin 	if (error)
262bcd9e0ddSJohn Baldwin 		return (error);
263bcd9e0ddSJohn Baldwin 	error = kern_preadv(td, uap->fd, auio, uap->offset);
264bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
265bcd9e0ddSJohn Baldwin 	return (error);
266bcd9e0ddSJohn Baldwin }
267bcd9e0ddSJohn Baldwin 
268bcd9e0ddSJohn Baldwin int
269bcd9e0ddSJohn Baldwin kern_preadv(td, fd, auio, offset)
270bcd9e0ddSJohn Baldwin 	struct thread *td;
271bcd9e0ddSJohn Baldwin 	int fd;
272bcd9e0ddSJohn Baldwin 	struct uio *auio;
273bcd9e0ddSJohn Baldwin 	off_t offset;
274bcd9e0ddSJohn Baldwin {
275bcd9e0ddSJohn Baldwin 	struct file *fp;
276bcd9e0ddSJohn Baldwin 	int error;
277bcd9e0ddSJohn Baldwin 
278bcd9e0ddSJohn Baldwin 	error = fget_read(td, fd, &fp);
279bcd9e0ddSJohn Baldwin 	if (error)
280bcd9e0ddSJohn Baldwin 		return (error);
281bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
282bcd9e0ddSJohn Baldwin 		error = ESPIPE;
283bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
284bcd9e0ddSJohn Baldwin 		error = EINVAL;
285bcd9e0ddSJohn Baldwin 	else
286bcd9e0ddSJohn Baldwin 		error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
287bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
288bcd9e0ddSJohn Baldwin 	return (error);
289bcd9e0ddSJohn Baldwin }
290bcd9e0ddSJohn Baldwin 
291bcd9e0ddSJohn Baldwin /*
292bcd9e0ddSJohn Baldwin  * Common code for readv and preadv that reads data in
293bcd9e0ddSJohn Baldwin  * from a file using the passed in uio, offset, and flags.
294bcd9e0ddSJohn Baldwin  */
295bcd9e0ddSJohn Baldwin static int
296bcd9e0ddSJohn Baldwin dofileread(td, fd, fp, auio, offset, flags)
297bcd9e0ddSJohn Baldwin 	struct thread *td;
298bcd9e0ddSJohn Baldwin 	int fd;
299bcd9e0ddSJohn Baldwin 	struct file *fp;
300bcd9e0ddSJohn Baldwin 	struct uio *auio;
301bcd9e0ddSJohn Baldwin 	off_t offset;
302bcd9e0ddSJohn Baldwin 	int flags;
303bcd9e0ddSJohn Baldwin {
304bcd9e0ddSJohn Baldwin 	ssize_t cnt;
30582641acdSAlan Cox 	int error;
306df8bae1dSRodney W. Grimes #ifdef KTRACE
307552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
308df8bae1dSRodney W. Grimes #endif
309df8bae1dSRodney W. Grimes 
3104f8d23d6SPoul-Henning Kamp 	/* Finish zero length reads right here */
3114f8d23d6SPoul-Henning Kamp 	if (auio->uio_resid == 0) {
3124f8d23d6SPoul-Henning Kamp 		td->td_retval[0] = 0;
3134f8d23d6SPoul-Henning Kamp 		return(0);
3144f8d23d6SPoul-Henning Kamp 	}
315552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_READ;
316bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
317552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
318df8bae1dSRodney W. Grimes #ifdef KTRACE
319552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
320552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
321df8bae1dSRodney W. Grimes #endif
322552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
323bcd9e0ddSJohn Baldwin 	if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
324552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
325df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
326df8bae1dSRodney W. Grimes 			error = 0;
327279d7226SMatthew Dillon 	}
328552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
329df8bae1dSRodney W. Grimes #ifdef KTRACE
330552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
331552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
332b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_READ, ktruio, error);
333df8bae1dSRodney W. Grimes 	}
334df8bae1dSRodney W. Grimes #endif
335b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
336df8bae1dSRodney W. Grimes 	return (error);
337df8bae1dSRodney W. Grimes }
338df8bae1dSRodney W. Grimes 
339d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
340df8bae1dSRodney W. Grimes struct write_args {
341df8bae1dSRodney W. Grimes 	int	fd;
342134e06feSBruce Evans 	const void *buf;
343134e06feSBruce Evans 	size_t	nbyte;
344df8bae1dSRodney W. Grimes };
345d2d3e875SBruce Evans #endif
34626f9a767SRodney W. Grimes int
347b40ce416SJulian Elischer write(td, uap)
348b40ce416SJulian Elischer 	struct thread *td;
349b064d43dSMatthew Dillon 	struct write_args *uap;
350df8bae1dSRodney W. Grimes {
351bcd9e0ddSJohn Baldwin 	struct uio auio;
352bcd9e0ddSJohn Baldwin 	struct iovec aiov;
353279d7226SMatthew Dillon 	int error;
354df8bae1dSRodney W. Grimes 
355bcd9e0ddSJohn Baldwin 	if (uap->nbyte > INT_MAX)
356bcd9e0ddSJohn Baldwin 		return (EINVAL);
357bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
358bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
359bcd9e0ddSJohn Baldwin 	auio.uio_iov = &aiov;
360bcd9e0ddSJohn Baldwin 	auio.uio_iovcnt = 1;
361bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
362bcd9e0ddSJohn Baldwin 	auio.uio_segflg = UIO_USERSPACE;
363bcd9e0ddSJohn Baldwin 	error = kern_writev(td, uap->fd, &auio);
364279d7226SMatthew Dillon 	return(error);
365df8bae1dSRodney W. Grimes }
366df8bae1dSRodney W. Grimes 
367df8bae1dSRodney W. Grimes /*
3680c14ff0eSRobert Watson  * Positioned write system call.
3694160ccd9SAlan Cox  */
3704160ccd9SAlan Cox #ifndef _SYS_SYSPROTO_H_
3714160ccd9SAlan Cox struct pwrite_args {
3724160ccd9SAlan Cox 	int	fd;
3734160ccd9SAlan Cox 	const void *buf;
3744160ccd9SAlan Cox 	size_t	nbyte;
3758fe387abSDmitrij Tejblum 	int	pad;
3764160ccd9SAlan Cox 	off_t	offset;
3774160ccd9SAlan Cox };
3784160ccd9SAlan Cox #endif
3794160ccd9SAlan Cox int
380b40ce416SJulian Elischer pwrite(td, uap)
381b40ce416SJulian Elischer 	struct thread *td;
382b064d43dSMatthew Dillon 	struct pwrite_args *uap;
3834160ccd9SAlan Cox {
3844160ccd9SAlan Cox 	struct uio auio;
3854160ccd9SAlan Cox 	struct iovec aiov;
386bcd9e0ddSJohn Baldwin 	int error;
3874160ccd9SAlan Cox 
388bcd9e0ddSJohn Baldwin 	if (uap->nbyte > INT_MAX)
389bcd9e0ddSJohn Baldwin 		return (EINVAL);
390bcd9e0ddSJohn Baldwin 	aiov.iov_base = (void *)(uintptr_t)uap->buf;
391bcd9e0ddSJohn Baldwin 	aiov.iov_len = uap->nbyte;
3924160ccd9SAlan Cox 	auio.uio_iov = &aiov;
3934160ccd9SAlan Cox 	auio.uio_iovcnt = 1;
394bcd9e0ddSJohn Baldwin 	auio.uio_resid = uap->nbyte;
3954160ccd9SAlan Cox 	auio.uio_segflg = UIO_USERSPACE;
396bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, &auio, uap->offset);
3974160ccd9SAlan Cox 	return(error);
3984160ccd9SAlan Cox }
3994160ccd9SAlan Cox 
400c2815ad5SPeter Wemm int
401c2815ad5SPeter Wemm freebsd6_pwrite(td, uap)
402c2815ad5SPeter Wemm 	struct thread *td;
403c2815ad5SPeter Wemm 	struct freebsd6_pwrite_args *uap;
404c2815ad5SPeter Wemm {
405c2815ad5SPeter Wemm 	struct pwrite_args oargs;
406c2815ad5SPeter Wemm 
407c2815ad5SPeter Wemm 	oargs.fd = uap->fd;
408c2815ad5SPeter Wemm 	oargs.buf = uap->buf;
409c2815ad5SPeter Wemm 	oargs.nbyte = uap->nbyte;
410c2815ad5SPeter Wemm 	oargs.offset = uap->offset;
411c2815ad5SPeter Wemm 	return (pwrite(td, &oargs));
412c2815ad5SPeter Wemm }
413c2815ad5SPeter Wemm 
4144160ccd9SAlan Cox /*
4150c14ff0eSRobert Watson  * Gather write system call.
416df8bae1dSRodney W. Grimes  */
417d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
418df8bae1dSRodney W. Grimes struct writev_args {
419df8bae1dSRodney W. Grimes 	int	fd;
420df8bae1dSRodney W. Grimes 	struct	iovec *iovp;
421df8bae1dSRodney W. Grimes 	u_int	iovcnt;
422df8bae1dSRodney W. Grimes };
423d2d3e875SBruce Evans #endif
42426f9a767SRodney W. Grimes int
425552afd9cSPoul-Henning Kamp writev(struct thread *td, struct writev_args *uap)
426df8bae1dSRodney W. Grimes {
427b88ec951SJohn Baldwin 	struct uio *auio;
428b88ec951SJohn Baldwin 	int error;
429b88ec951SJohn Baldwin 
430b88ec951SJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
431b88ec951SJohn Baldwin 	if (error)
432b88ec951SJohn Baldwin 		return (error);
433b88ec951SJohn Baldwin 	error = kern_writev(td, uap->fd, auio);
434b88ec951SJohn Baldwin 	free(auio, M_IOV);
435b88ec951SJohn Baldwin 	return (error);
436b88ec951SJohn Baldwin }
437b88ec951SJohn Baldwin 
438b88ec951SJohn Baldwin int
439b88ec951SJohn Baldwin kern_writev(struct thread *td, int fd, struct uio *auio)
440b88ec951SJohn Baldwin {
441b064d43dSMatthew Dillon 	struct file *fp;
442bcd9e0ddSJohn Baldwin 	int error;
443bcd9e0ddSJohn Baldwin 
444bcd9e0ddSJohn Baldwin 	error = fget_write(td, fd, &fp);
445bcd9e0ddSJohn Baldwin 	if (error)
446af56abaaSJohn Baldwin 		return (error);
447bcd9e0ddSJohn Baldwin 	error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
448bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
449bcd9e0ddSJohn Baldwin 	return (error);
450bcd9e0ddSJohn Baldwin }
451bcd9e0ddSJohn Baldwin 
452bcd9e0ddSJohn Baldwin /*
4530c14ff0eSRobert Watson  * Gather positioned write system call.
454bcd9e0ddSJohn Baldwin  */
455bcd9e0ddSJohn Baldwin #ifndef _SYS_SYSPROTO_H_
456bcd9e0ddSJohn Baldwin struct pwritev_args {
457bcd9e0ddSJohn Baldwin 	int	fd;
458bcd9e0ddSJohn Baldwin 	struct	iovec *iovp;
459bcd9e0ddSJohn Baldwin 	u_int	iovcnt;
460bcd9e0ddSJohn Baldwin 	off_t	offset;
461bcd9e0ddSJohn Baldwin };
462bcd9e0ddSJohn Baldwin #endif
463bcd9e0ddSJohn Baldwin int
464bcd9e0ddSJohn Baldwin pwritev(struct thread *td, struct pwritev_args *uap)
465bcd9e0ddSJohn Baldwin {
466bcd9e0ddSJohn Baldwin 	struct uio *auio;
467bcd9e0ddSJohn Baldwin 	int error;
468bcd9e0ddSJohn Baldwin 
469bcd9e0ddSJohn Baldwin 	error = copyinuio(uap->iovp, uap->iovcnt, &auio);
470bcd9e0ddSJohn Baldwin 	if (error)
471bcd9e0ddSJohn Baldwin 		return (error);
472bcd9e0ddSJohn Baldwin 	error = kern_pwritev(td, uap->fd, auio, uap->offset);
473bcd9e0ddSJohn Baldwin 	free(auio, M_IOV);
474bcd9e0ddSJohn Baldwin 	return (error);
475bcd9e0ddSJohn Baldwin }
476bcd9e0ddSJohn Baldwin 
477bcd9e0ddSJohn Baldwin int
478bcd9e0ddSJohn Baldwin kern_pwritev(td, fd, auio, offset)
479bcd9e0ddSJohn Baldwin 	struct thread *td;
480bcd9e0ddSJohn Baldwin 	struct uio *auio;
481bcd9e0ddSJohn Baldwin 	int fd;
482bcd9e0ddSJohn Baldwin 	off_t offset;
483bcd9e0ddSJohn Baldwin {
484bcd9e0ddSJohn Baldwin 	struct file *fp;
485bcd9e0ddSJohn Baldwin 	int error;
486bcd9e0ddSJohn Baldwin 
487bcd9e0ddSJohn Baldwin 	error = fget_write(td, fd, &fp);
488bcd9e0ddSJohn Baldwin 	if (error)
489af56abaaSJohn Baldwin 		return (error);
490bcd9e0ddSJohn Baldwin 	if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
491bcd9e0ddSJohn Baldwin 		error = ESPIPE;
492bcd9e0ddSJohn Baldwin 	else if (offset < 0 && fp->f_vnode->v_type != VCHR)
493bcd9e0ddSJohn Baldwin 		error = EINVAL;
494bcd9e0ddSJohn Baldwin 	else
495bcd9e0ddSJohn Baldwin 		error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
496bcd9e0ddSJohn Baldwin 	fdrop(fp, td);
497bcd9e0ddSJohn Baldwin 	return (error);
498bcd9e0ddSJohn Baldwin }
499bcd9e0ddSJohn Baldwin 
500bcd9e0ddSJohn Baldwin /*
501bcd9e0ddSJohn Baldwin  * Common code for writev and pwritev that writes data to
502bcd9e0ddSJohn Baldwin  * a file using the passed in uio, offset, and flags.
503bcd9e0ddSJohn Baldwin  */
504bcd9e0ddSJohn Baldwin static int
505bcd9e0ddSJohn Baldwin dofilewrite(td, fd, fp, auio, offset, flags)
506bcd9e0ddSJohn Baldwin 	struct thread *td;
507bcd9e0ddSJohn Baldwin 	int fd;
508bcd9e0ddSJohn Baldwin 	struct file *fp;
509bcd9e0ddSJohn Baldwin 	struct uio *auio;
510bcd9e0ddSJohn Baldwin 	off_t offset;
511bcd9e0ddSJohn Baldwin 	int flags;
512bcd9e0ddSJohn Baldwin {
513bcd9e0ddSJohn Baldwin 	ssize_t cnt;
514552afd9cSPoul-Henning Kamp 	int error;
515df8bae1dSRodney W. Grimes #ifdef KTRACE
516552afd9cSPoul-Henning Kamp 	struct uio *ktruio = NULL;
517df8bae1dSRodney W. Grimes #endif
518df8bae1dSRodney W. Grimes 
519552afd9cSPoul-Henning Kamp 	auio->uio_rw = UIO_WRITE;
520552afd9cSPoul-Henning Kamp 	auio->uio_td = td;
521bcd9e0ddSJohn Baldwin 	auio->uio_offset = offset;
522df8bae1dSRodney W. Grimes #ifdef KTRACE
523552afd9cSPoul-Henning Kamp 	if (KTRPOINT(td, KTR_GENIO))
524552afd9cSPoul-Henning Kamp 		ktruio = cloneuio(auio);
525df8bae1dSRodney W. Grimes #endif
526552afd9cSPoul-Henning Kamp 	cnt = auio->uio_resid;
527a41ce5d3SMatthew Dillon 	if (fp->f_type == DTYPE_VNODE)
5289440653dSMatthew Dillon 		bwillwrite();
529bcd9e0ddSJohn Baldwin 	if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
530552afd9cSPoul-Henning Kamp 		if (auio->uio_resid != cnt && (error == ERESTART ||
531df8bae1dSRodney W. Grimes 		    error == EINTR || error == EWOULDBLOCK))
532df8bae1dSRodney W. Grimes 			error = 0;
533bcd9e0ddSJohn Baldwin 		/* Socket layer is responsible for issuing SIGPIPE. */
5346f7ca813SBruce M Simpson 		if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
535b40ce416SJulian Elischer 			PROC_LOCK(td->td_proc);
5367a6f3d78SJohn Baldwin 			tdsignal(td, SIGPIPE);
537b40ce416SJulian Elischer 			PROC_UNLOCK(td->td_proc);
53819eb87d2SJohn Baldwin 		}
539df8bae1dSRodney W. Grimes 	}
540552afd9cSPoul-Henning Kamp 	cnt -= auio->uio_resid;
541df8bae1dSRodney W. Grimes #ifdef KTRACE
542552afd9cSPoul-Henning Kamp 	if (ktruio != NULL) {
543552afd9cSPoul-Henning Kamp 		ktruio->uio_resid = cnt;
544b88ec951SJohn Baldwin 		ktrgenio(fd, UIO_WRITE, ktruio, error);
545df8bae1dSRodney W. Grimes 	}
546df8bae1dSRodney W. Grimes #endif
547b40ce416SJulian Elischer 	td->td_retval[0] = cnt;
548df8bae1dSRodney W. Grimes 	return (error);
549df8bae1dSRodney W. Grimes }
550df8bae1dSRodney W. Grimes 
551e4650294SJohn Baldwin /*
552e4650294SJohn Baldwin  * Truncate a file given a file descriptor.
553e4650294SJohn Baldwin  *
554e4650294SJohn Baldwin  * Can't use fget_write() here, since must return EINVAL and not EBADF if the
555e4650294SJohn Baldwin  * descriptor isn't writable.
556e4650294SJohn Baldwin  */
557e4650294SJohn Baldwin int
558e4650294SJohn Baldwin kern_ftruncate(td, fd, length)
559e4650294SJohn Baldwin 	struct thread *td;
560e4650294SJohn Baldwin 	int fd;
561e4650294SJohn Baldwin 	off_t length;
562e4650294SJohn Baldwin {
563e4650294SJohn Baldwin 	struct file *fp;
564e4650294SJohn Baldwin 	int error;
565e4650294SJohn Baldwin 
56614961ba7SRobert Watson 	AUDIT_ARG_FD(fd);
567e4650294SJohn Baldwin 	if (length < 0)
568e4650294SJohn Baldwin 		return (EINVAL);
569e4650294SJohn Baldwin 	error = fget(td, fd, &fp);
570e4650294SJohn Baldwin 	if (error)
571e4650294SJohn Baldwin 		return (error);
57214961ba7SRobert Watson 	AUDIT_ARG_FILE(td->td_proc, fp);
573e4650294SJohn Baldwin 	if (!(fp->f_flag & FWRITE)) {
574e4650294SJohn Baldwin 		fdrop(fp, td);
575e4650294SJohn Baldwin 		return (EINVAL);
576e4650294SJohn Baldwin 	}
577e4650294SJohn Baldwin 	error = fo_truncate(fp, length, td->td_ucred, td);
578e4650294SJohn Baldwin 	fdrop(fp, td);
579e4650294SJohn Baldwin 	return (error);
580e4650294SJohn Baldwin }
581e4650294SJohn Baldwin 
582e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
583e4650294SJohn Baldwin struct ftruncate_args {
584e4650294SJohn Baldwin 	int	fd;
585e4650294SJohn Baldwin 	int	pad;
586e4650294SJohn Baldwin 	off_t	length;
587e4650294SJohn Baldwin };
588e4650294SJohn Baldwin #endif
589e4650294SJohn Baldwin int
590e4650294SJohn Baldwin ftruncate(td, uap)
591e4650294SJohn Baldwin 	struct thread *td;
592e4650294SJohn Baldwin 	struct ftruncate_args *uap;
593e4650294SJohn Baldwin {
594e4650294SJohn Baldwin 
595e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
596e4650294SJohn Baldwin }
597e4650294SJohn Baldwin 
598e4650294SJohn Baldwin #if defined(COMPAT_43)
599e4650294SJohn Baldwin #ifndef _SYS_SYSPROTO_H_
600e4650294SJohn Baldwin struct oftruncate_args {
601e4650294SJohn Baldwin 	int	fd;
602e4650294SJohn Baldwin 	long	length;
603e4650294SJohn Baldwin };
604e4650294SJohn Baldwin #endif
605e4650294SJohn Baldwin int
606e4650294SJohn Baldwin oftruncate(td, uap)
607e4650294SJohn Baldwin 	struct thread *td;
608e4650294SJohn Baldwin 	struct oftruncate_args *uap;
609e4650294SJohn Baldwin {
610e4650294SJohn Baldwin 
611e4650294SJohn Baldwin 	return (kern_ftruncate(td, uap->fd, uap->length));
612e4650294SJohn Baldwin }
613e4650294SJohn Baldwin #endif /* COMPAT_43 */
614e4650294SJohn Baldwin 
615d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
616df8bae1dSRodney W. Grimes struct ioctl_args {
617df8bae1dSRodney W. Grimes 	int	fd;
618069e9bc1SDoug Rabson 	u_long	com;
619df8bae1dSRodney W. Grimes 	caddr_t	data;
620df8bae1dSRodney W. Grimes };
621d2d3e875SBruce Evans #endif
622df8bae1dSRodney W. Grimes /* ARGSUSED */
62326f9a767SRodney W. Grimes int
6243e15c66fSPoul-Henning Kamp ioctl(struct thread *td, struct ioctl_args *uap)
625df8bae1dSRodney W. Grimes {
6263e15c66fSPoul-Henning Kamp 	u_long com;
6279fddcc66SRuslan Ermilov 	int arg, error;
6283e15c66fSPoul-Henning Kamp 	u_int size;
6299fddcc66SRuslan Ermilov 	caddr_t data;
630df8bae1dSRodney W. Grimes 
6319fc6aa06SPoul-Henning Kamp 	if (uap->com > 0xffffffff) {
6329fc6aa06SPoul-Henning Kamp 		printf(
6339fc6aa06SPoul-Henning Kamp 		    "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
634431f8906SJulian Elischer 		    td->td_proc->p_pid, td->td_name, uap->com);
6359fc6aa06SPoul-Henning Kamp 		uap->com &= 0xffffffff;
6369fc6aa06SPoul-Henning Kamp 	}
637d9f46233SJohn Baldwin 	com = uap->com;
638df8bae1dSRodney W. Grimes 
639df8bae1dSRodney W. Grimes 	/*
640df8bae1dSRodney W. Grimes 	 * Interpret high order word to find amount of data to be
641df8bae1dSRodney W. Grimes 	 * copied to/from the user's address space.
642df8bae1dSRodney W. Grimes 	 */
643df8bae1dSRodney W. Grimes 	size = IOCPARM_LEN(com);
644ca51b19bSPoul-Henning Kamp 	if ((size > IOCPARM_MAX) ||
645ca51b19bSPoul-Henning Kamp 	    ((com & (IOC_VOID  | IOC_IN | IOC_OUT)) == 0) ||
6462de92a38SPeter Wemm #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
6472de92a38SPeter Wemm 	    ((com & IOC_OUT) && size == 0) ||
6482de92a38SPeter Wemm #else
6492de92a38SPeter Wemm 	    ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
6502de92a38SPeter Wemm #endif
6519fddcc66SRuslan Ermilov 	    ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
652426da3bcSAlfred Perlstein 		return (ENOTTY);
653279d7226SMatthew Dillon 
654ca51b19bSPoul-Henning Kamp 	if (size > 0) {
655715457f6SDavid E. O'Brien 		if (com & IOC_VOID) {
6569fddcc66SRuslan Ermilov 			/* Integer argument. */
6579fddcc66SRuslan Ermilov 			arg = (intptr_t)uap->data;
6589fddcc66SRuslan Ermilov 			data = (void *)&arg;
6599fddcc66SRuslan Ermilov 			size = 0;
660715457f6SDavid E. O'Brien 		} else
661715457f6SDavid E. O'Brien 			data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
6629fddcc66SRuslan Ermilov 	} else
6639fddcc66SRuslan Ermilov 		data = (void *)&uap->data;
664df8bae1dSRodney W. Grimes 	if (com & IOC_IN) {
665df8bae1dSRodney W. Grimes 		error = copyin(uap->data, data, (u_int)size);
666df8bae1dSRodney W. Grimes 		if (error) {
667a1b0a180SRuslan Ermilov 			if (size > 0)
6689fddcc66SRuslan Ermilov 				free(data, M_IOCTLOPS);
6693e15c66fSPoul-Henning Kamp 			return (error);
670df8bae1dSRodney W. Grimes 		}
671ca51b19bSPoul-Henning Kamp 	} else if (com & IOC_OUT) {
672df8bae1dSRodney W. Grimes 		/*
673df8bae1dSRodney W. Grimes 		 * Zero the buffer so the user always
674df8bae1dSRodney W. Grimes 		 * gets back something deterministic.
675df8bae1dSRodney W. Grimes 		 */
676df8bae1dSRodney W. Grimes 		bzero(data, size);
677279d7226SMatthew Dillon 	}
678df8bae1dSRodney W. Grimes 
679d9f46233SJohn Baldwin 	error = kern_ioctl(td, uap->fd, com, data);
680d9f46233SJohn Baldwin 
681d9f46233SJohn Baldwin 	if (error == 0 && (com & IOC_OUT))
682d9f46233SJohn Baldwin 		error = copyout(data, uap->data, (u_int)size);
683d9f46233SJohn Baldwin 
6849fddcc66SRuslan Ermilov 	if (size > 0)
6859fddcc66SRuslan Ermilov 		free(data, M_IOCTLOPS);
686d9f46233SJohn Baldwin 	return (error);
687d9f46233SJohn Baldwin }
688d9f46233SJohn Baldwin 
689d9f46233SJohn Baldwin int
690d9f46233SJohn Baldwin kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
691d9f46233SJohn Baldwin {
692d9f46233SJohn Baldwin 	struct file *fp;
693d9f46233SJohn Baldwin 	struct filedesc *fdp;
694d9f46233SJohn Baldwin 	int error;
695d9f46233SJohn Baldwin 	int tmp;
696d9f46233SJohn Baldwin 
69764c9a4d9SRobert Watson 	AUDIT_ARG_FD(fd);
69864c9a4d9SRobert Watson 	AUDIT_ARG_CMD(com);
699d9f46233SJohn Baldwin 	if ((error = fget(td, fd, &fp)) != 0)
700d9f46233SJohn Baldwin 		return (error);
701d9f46233SJohn Baldwin 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
702d9f46233SJohn Baldwin 		fdrop(fp, td);
703d9f46233SJohn Baldwin 		return (EBADF);
704d9f46233SJohn Baldwin 	}
705d9f46233SJohn Baldwin 	fdp = td->td_proc->p_fd;
706d9f46233SJohn Baldwin 	switch (com) {
707d9f46233SJohn Baldwin 	case FIONCLEX:
7085e3f7694SRobert Watson 		FILEDESC_XLOCK(fdp);
709d9f46233SJohn Baldwin 		fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
7105e3f7694SRobert Watson 		FILEDESC_XUNLOCK(fdp);
711d9f46233SJohn Baldwin 		goto out;
712d9f46233SJohn Baldwin 	case FIOCLEX:
7135e3f7694SRobert Watson 		FILEDESC_XLOCK(fdp);
714d9f46233SJohn Baldwin 		fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
7155e3f7694SRobert Watson 		FILEDESC_XUNLOCK(fdp);
716d9f46233SJohn Baldwin 		goto out;
717d9f46233SJohn Baldwin 	case FIONBIO:
718bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
719397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FNONBLOCK);
720df8bae1dSRodney W. Grimes 		else
721397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FNONBLOCK);
7228ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
723d9f46233SJohn Baldwin 		break;
724d9f46233SJohn Baldwin 	case FIOASYNC:
725bb56ec4aSPoul-Henning Kamp 		if ((tmp = *(int *)data))
726397c19d1SJeff Roberson 			atomic_set_int(&fp->f_flag, FASYNC);
727df8bae1dSRodney W. Grimes 		else
728397c19d1SJeff Roberson 			atomic_clear_int(&fp->f_flag, FASYNC);
7298ccf264fSPoul-Henning Kamp 		data = (void *)&tmp;
730d9f46233SJohn Baldwin 		break;
731df8bae1dSRodney W. Grimes 	}
7328ccf264fSPoul-Henning Kamp 
7338ccf264fSPoul-Henning Kamp 	error = fo_ioctl(fp, com, data, td->td_ucred, td);
734d9f46233SJohn Baldwin out:
735b40ce416SJulian Elischer 	fdrop(fp, td);
736df8bae1dSRodney W. Grimes 	return (error);
737df8bae1dSRodney W. Grimes }
738df8bae1dSRodney W. Grimes 
739125dcf8cSKonstantin Belousov int
740125dcf8cSKonstantin Belousov poll_no_poll(int events)
741125dcf8cSKonstantin Belousov {
742125dcf8cSKonstantin Belousov 	/*
743125dcf8cSKonstantin Belousov 	 * Return true for read/write.  If the user asked for something
744125dcf8cSKonstantin Belousov 	 * special, return POLLNVAL, so that clients have a way of
745125dcf8cSKonstantin Belousov 	 * determining reliably whether or not the extended
746125dcf8cSKonstantin Belousov 	 * functionality is present without hard-coding knowledge
747125dcf8cSKonstantin Belousov 	 * of specific filesystem implementations.
748125dcf8cSKonstantin Belousov 	 */
749125dcf8cSKonstantin Belousov 	if (events & ~POLLSTANDARD)
750125dcf8cSKonstantin Belousov 		return (POLLNVAL);
751125dcf8cSKonstantin Belousov 
752125dcf8cSKonstantin Belousov 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
753125dcf8cSKonstantin Belousov }
754125dcf8cSKonstantin Belousov 
755066d836bSKonstantin Belousov int
756066d836bSKonstantin Belousov pselect(struct thread *td, struct pselect_args *uap)
757066d836bSKonstantin Belousov {
758066d836bSKonstantin Belousov 	struct timespec ts;
759066d836bSKonstantin Belousov 	struct timeval tv, *tvp;
760066d836bSKonstantin Belousov 	sigset_t set, *uset;
761066d836bSKonstantin Belousov 	int error;
762066d836bSKonstantin Belousov 
763066d836bSKonstantin Belousov 	if (uap->ts != NULL) {
764066d836bSKonstantin Belousov 		error = copyin(uap->ts, &ts, sizeof(ts));
765066d836bSKonstantin Belousov 		if (error != 0)
766066d836bSKonstantin Belousov 		    return (error);
767066d836bSKonstantin Belousov 		TIMESPEC_TO_TIMEVAL(&tv, &ts);
768066d836bSKonstantin Belousov 		tvp = &tv;
769066d836bSKonstantin Belousov 	} else
770066d836bSKonstantin Belousov 		tvp = NULL;
771066d836bSKonstantin Belousov 	if (uap->sm != NULL) {
772066d836bSKonstantin Belousov 		error = copyin(uap->sm, &set, sizeof(set));
773066d836bSKonstantin Belousov 		if (error != 0)
774066d836bSKonstantin Belousov 			return (error);
775066d836bSKonstantin Belousov 		uset = &set;
776066d836bSKonstantin Belousov 	} else
777066d836bSKonstantin Belousov 		uset = NULL;
778066d836bSKonstantin Belousov 	return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
779066d836bSKonstantin Belousov 	    uset, NFDBITS));
780066d836bSKonstantin Belousov }
781066d836bSKonstantin Belousov 
782066d836bSKonstantin Belousov int
783066d836bSKonstantin Belousov kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
784066d836bSKonstantin Belousov     struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
785066d836bSKonstantin Belousov {
786066d836bSKonstantin Belousov 	int error;
787066d836bSKonstantin Belousov 
788066d836bSKonstantin Belousov 	if (uset != NULL) {
789066d836bSKonstantin Belousov 		error = kern_sigprocmask(td, SIG_SETMASK, uset,
790066d836bSKonstantin Belousov 		    &td->td_oldsigmask, 0);
791066d836bSKonstantin Belousov 		if (error != 0)
792066d836bSKonstantin Belousov 			return (error);
793066d836bSKonstantin Belousov 		td->td_pflags |= TDP_OLDMASK;
794066d836bSKonstantin Belousov 		/*
795066d836bSKonstantin Belousov 		 * Make sure that ast() is called on return to
796066d836bSKonstantin Belousov 		 * usermode and TDP_OLDMASK is cleared, restoring old
797066d836bSKonstantin Belousov 		 * sigmask.
798066d836bSKonstantin Belousov 		 */
799066d836bSKonstantin Belousov 		thread_lock(td);
800066d836bSKonstantin Belousov 		td->td_flags |= TDF_ASTPENDING;
801066d836bSKonstantin Belousov 		thread_unlock(td);
802066d836bSKonstantin Belousov 	}
803066d836bSKonstantin Belousov 	error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
804066d836bSKonstantin Belousov 	return (error);
805066d836bSKonstantin Belousov }
806066d836bSKonstantin Belousov 
807d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
808df8bae1dSRodney W. Grimes struct select_args {
809b08f7993SSujal Patel 	int	nd;
810df8bae1dSRodney W. Grimes 	fd_set	*in, *ou, *ex;
811df8bae1dSRodney W. Grimes 	struct	timeval *tv;
812df8bae1dSRodney W. Grimes };
813d2d3e875SBruce Evans #endif
81426f9a767SRodney W. Grimes int
815066d836bSKonstantin Belousov select(struct thread *td, struct select_args *uap)
816df8bae1dSRodney W. Grimes {
8178f19eb88SIan Dowse 	struct timeval tv, *tvp;
8188f19eb88SIan Dowse 	int error;
8198f19eb88SIan Dowse 
8208f19eb88SIan Dowse 	if (uap->tv != NULL) {
8218f19eb88SIan Dowse 		error = copyin(uap->tv, &tv, sizeof(tv));
8228f19eb88SIan Dowse 		if (error)
8238f19eb88SIan Dowse 			return (error);
8248f19eb88SIan Dowse 		tvp = &tv;
8258f19eb88SIan Dowse 	} else
8268f19eb88SIan Dowse 		tvp = NULL;
8278f19eb88SIan Dowse 
828b55ef216SKonstantin Belousov 	return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
829b55ef216SKonstantin Belousov 	    NFDBITS));
8308f19eb88SIan Dowse }
8318f19eb88SIan Dowse 
8328f19eb88SIan Dowse int
8338f19eb88SIan Dowse kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
834b55ef216SKonstantin Belousov     fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
8358f19eb88SIan Dowse {
836426da3bcSAlfred Perlstein 	struct filedesc *fdp;
837d5e4d7e1SBruce Evans 	/*
838d5e4d7e1SBruce Evans 	 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
839d5e4d7e1SBruce Evans 	 * infds with the new FD_SETSIZE of 1024, and more than enough for
840d5e4d7e1SBruce Evans 	 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
841d5e4d7e1SBruce Evans 	 * of 256.
842d5e4d7e1SBruce Evans 	 */
843d5e4d7e1SBruce Evans 	fd_mask s_selbits[howmany(2048, NFDBITS)];
844eb209311SAlfred Perlstein 	fd_mask *ibits[3], *obits[3], *selbits, *sbp;
84500af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
8469ae6d334SKelly Yancey 	int error, timo;
847b55ef216SKonstantin Belousov 	u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
848df8bae1dSRodney W. Grimes 
8498f19eb88SIan Dowse 	if (nd < 0)
850acbfbfeaSSujal Patel 		return (EINVAL);
851426da3bcSAlfred Perlstein 	fdp = td->td_proc->p_fd;
8522141453eSJeff Roberson 	if (nd > fdp->fd_lastfile + 1)
8532141453eSJeff Roberson 		nd = fdp->fd_lastfile + 1;
854b08f7993SSujal Patel 
855d5e4d7e1SBruce Evans 	/*
856d5e4d7e1SBruce Evans 	 * Allocate just enough bits for the non-null fd_sets.  Use the
857d5e4d7e1SBruce Evans 	 * preallocated auto buffer if possible.
858d5e4d7e1SBruce Evans 	 */
8598f19eb88SIan Dowse 	nfdbits = roundup(nd, NFDBITS);
860d5e4d7e1SBruce Evans 	ncpbytes = nfdbits / NBBY;
861b55ef216SKonstantin Belousov 	ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
862d5e4d7e1SBruce Evans 	nbufbytes = 0;
8638f19eb88SIan Dowse 	if (fd_in != NULL)
864d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
8658f19eb88SIan Dowse 	if (fd_ou != NULL)
866d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
8678f19eb88SIan Dowse 	if (fd_ex != NULL)
868d5e4d7e1SBruce Evans 		nbufbytes += 2 * ncpbytes;
869d5e4d7e1SBruce Evans 	if (nbufbytes <= sizeof s_selbits)
870d5e4d7e1SBruce Evans 		selbits = &s_selbits[0];
871d5e4d7e1SBruce Evans 	else
872a163d034SWarner Losh 		selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
873b08f7993SSujal Patel 
874b08f7993SSujal Patel 	/*
875d5e4d7e1SBruce Evans 	 * Assign pointers into the bit buffers and fetch the input bits.
876d5e4d7e1SBruce Evans 	 * Put the output buffers together so that they can be bzeroed
877d5e4d7e1SBruce Evans 	 * together.
878b08f7993SSujal Patel 	 */
879d5e4d7e1SBruce Evans 	sbp = selbits;
880df8bae1dSRodney W. Grimes #define	getbits(name, x) \
881d5e4d7e1SBruce Evans 	do {								\
882841c0c7eSNathan Whitehorn 		if (name == NULL) {					\
883d5e4d7e1SBruce Evans 			ibits[x] = NULL;				\
884841c0c7eSNathan Whitehorn 			obits[x] = NULL;				\
885841c0c7eSNathan Whitehorn 		} else {						\
886d5e4d7e1SBruce Evans 			ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp;	\
887d5e4d7e1SBruce Evans 			obits[x] = sbp;					\
888d5e4d7e1SBruce Evans 			sbp += ncpbytes / sizeof *sbp;			\
889b55ef216SKonstantin Belousov 			error = copyin(name, ibits[x], ncpubytes);	\
890265fc98fSSeigo Tanimura 			if (error != 0)					\
891ace8398dSJeff Roberson 				goto done;				\
892b55ef216SKonstantin Belousov 			bzero((char *)ibits[x] + ncpubytes,		\
893b55ef216SKonstantin Belousov 			    ncpbytes - ncpubytes);			\
894e04ac2feSJohn Baldwin 		}							\
895d5e4d7e1SBruce Evans 	} while (0)
8968f19eb88SIan Dowse 	getbits(fd_in, 0);
8978f19eb88SIan Dowse 	getbits(fd_ou, 1);
8988f19eb88SIan Dowse 	getbits(fd_ex, 2);
899df8bae1dSRodney W. Grimes #undef	getbits
900841c0c7eSNathan Whitehorn 
901841c0c7eSNathan Whitehorn #if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
902841c0c7eSNathan Whitehorn 	/*
903841c0c7eSNathan Whitehorn 	 * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
904841c0c7eSNathan Whitehorn 	 * we are running under 32-bit emulation. This should be more
905841c0c7eSNathan Whitehorn 	 * generic.
906841c0c7eSNathan Whitehorn 	 */
907841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)						\
908841c0c7eSNathan Whitehorn 	if (abi_nfdbits != NFDBITS && bits != NULL) {			\
909841c0c7eSNathan Whitehorn 		int i;							\
910841c0c7eSNathan Whitehorn 		for (i = 0; i < ncpbytes / sizeof *sbp; i++)		\
911841c0c7eSNathan Whitehorn 			bits[i] = (bits[i] >> 32) | (bits[i] << 32);	\
912841c0c7eSNathan Whitehorn 	}
913841c0c7eSNathan Whitehorn #else
914841c0c7eSNathan Whitehorn #define swizzle_fdset(bits)
915841c0c7eSNathan Whitehorn #endif
916841c0c7eSNathan Whitehorn 
917841c0c7eSNathan Whitehorn 	/* Make sure the bit order makes it through an ABI transition */
918841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[0]);
919841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[1]);
920841c0c7eSNathan Whitehorn 	swizzle_fdset(ibits[2]);
921841c0c7eSNathan Whitehorn 
922d5e4d7e1SBruce Evans 	if (nbufbytes != 0)
923d5e4d7e1SBruce Evans 		bzero(selbits, nbufbytes / 2);
924df8bae1dSRodney W. Grimes 
9258f19eb88SIan Dowse 	if (tvp != NULL) {
9268f19eb88SIan Dowse 		atv = *tvp;
927df8bae1dSRodney W. Grimes 		if (itimerfix(&atv)) {
928df8bae1dSRodney W. Grimes 			error = EINVAL;
929ace8398dSJeff Roberson 			goto done;
930df8bae1dSRodney W. Grimes 		}
931c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
93200af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
9339c386f6bSJohn Baldwin 	} else {
93400af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
9359c386f6bSJohn Baldwin 		atv.tv_usec = 0;
9369c386f6bSJohn Baldwin 	}
93700af9731SPoul-Henning Kamp 	timo = 0;
938ace8398dSJeff Roberson 	seltdinit(td);
939ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
940ace8398dSJeff Roberson 	for (;;) {
9418f19eb88SIan Dowse 		error = selscan(td, ibits, obits, nd);
942ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
943ace8398dSJeff Roberson 			break;
9444da144c0SJohn Baldwin 		if (atv.tv_sec || atv.tv_usec) {
945c21410e1SPoul-Henning Kamp 			getmicrouptime(&rtv);
94685f190e4SAlfred Perlstein 			if (timevalcmp(&rtv, &atv, >=))
947ace8398dSJeff Roberson 				break;
94800af9731SPoul-Henning Kamp 			ttv = atv;
94900af9731SPoul-Henning Kamp 			timevalsub(&ttv, &rtv);
95000af9731SPoul-Henning Kamp 			timo = ttv.tv_sec > 24 * 60 * 60 ?
95100af9731SPoul-Henning Kamp 			    24 * 60 * 60 * hz : tvtohz(&ttv);
952df8bae1dSRodney W. Grimes 		}
953ace8398dSJeff Roberson 		error = seltdwait(td, timo);
954ace8398dSJeff Roberson 		if (error)
955ace8398dSJeff Roberson 			break;
956ace8398dSJeff Roberson 		error = selrescan(td, ibits, obits);
957ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
958ace8398dSJeff Roberson 			break;
95985f190e4SAlfred Perlstein 	}
960ace8398dSJeff Roberson 	seltdclear(td);
961265fc98fSSeigo Tanimura 
962df8bae1dSRodney W. Grimes done:
963df8bae1dSRodney W. Grimes 	/* select is not restarted after signals... */
964df8bae1dSRodney W. Grimes 	if (error == ERESTART)
965df8bae1dSRodney W. Grimes 		error = EINTR;
966df8bae1dSRodney W. Grimes 	if (error == EWOULDBLOCK)
967df8bae1dSRodney W. Grimes 		error = 0;
968841c0c7eSNathan Whitehorn 
969841c0c7eSNathan Whitehorn 	/* swizzle bit order back, if necessary */
970841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[0]);
971841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[1]);
972841c0c7eSNathan Whitehorn 	swizzle_fdset(obits[2]);
973841c0c7eSNathan Whitehorn #undef swizzle_fdset
974841c0c7eSNathan Whitehorn 
975df8bae1dSRodney W. Grimes #define	putbits(name, x) \
976b55ef216SKonstantin Belousov 	if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
977df8bae1dSRodney W. Grimes 		error = error2;
978df8bae1dSRodney W. Grimes 	if (error == 0) {
979df8bae1dSRodney W. Grimes 		int error2;
980df8bae1dSRodney W. Grimes 
9818f19eb88SIan Dowse 		putbits(fd_in, 0);
9828f19eb88SIan Dowse 		putbits(fd_ou, 1);
9838f19eb88SIan Dowse 		putbits(fd_ex, 2);
984df8bae1dSRodney W. Grimes #undef putbits
985df8bae1dSRodney W. Grimes 	}
986d5e4d7e1SBruce Evans 	if (selbits != &s_selbits[0])
987d5e4d7e1SBruce Evans 		free(selbits, M_SELECT);
988ad2edad9SMatthew Dillon 
989df8bae1dSRodney W. Grimes 	return (error);
990df8bae1dSRodney W. Grimes }
99111b763dfSJeff Roberson /*
99211b763dfSJeff Roberson  * Convert a select bit set to poll flags.
993748b9df6SJeff Roberson  *
99411b763dfSJeff Roberson  * The backend always returns POLLHUP/POLLERR if appropriate and we
99511b763dfSJeff Roberson  * return this as a set bit in any set.
99611b763dfSJeff Roberson  */
99711b763dfSJeff Roberson static int select_flags[3] = {
99811b763dfSJeff Roberson     POLLRDNORM | POLLHUP | POLLERR,
99911b763dfSJeff Roberson     POLLWRNORM | POLLHUP | POLLERR,
100061e53a38SKonstantin Belousov     POLLRDBAND | POLLERR
100111b763dfSJeff Roberson };
100211b763dfSJeff Roberson 
100311b763dfSJeff Roberson /*
100411b763dfSJeff Roberson  * Compute the fo_poll flags required for a fd given by the index and
100511b763dfSJeff Roberson  * bit position in the fd_mask array.
100611b763dfSJeff Roberson  */
100711b763dfSJeff Roberson static __inline int
100860b7f468SStephane E. Potvin selflags(fd_mask **ibits, int idx, fd_mask bit)
100911b763dfSJeff Roberson {
101011b763dfSJeff Roberson 	int flags;
101111b763dfSJeff Roberson 	int msk;
101211b763dfSJeff Roberson 
101311b763dfSJeff Roberson 	flags = 0;
101411b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
101511b763dfSJeff Roberson 		if (ibits[msk] == NULL)
101611b763dfSJeff Roberson 			continue;
101760b7f468SStephane E. Potvin 		if ((ibits[msk][idx] & bit) == 0)
101811b763dfSJeff Roberson 			continue;
101911b763dfSJeff Roberson 		flags |= select_flags[msk];
102011b763dfSJeff Roberson 	}
102111b763dfSJeff Roberson 	return (flags);
102211b763dfSJeff Roberson }
102311b763dfSJeff Roberson 
102411b763dfSJeff Roberson /*
102511b763dfSJeff Roberson  * Set the appropriate output bits given a mask of fired events and the
102611b763dfSJeff Roberson  * input bits originally requested.
102711b763dfSJeff Roberson  */
102811b763dfSJeff Roberson static __inline int
102911b763dfSJeff Roberson selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
103011b763dfSJeff Roberson {
103111b763dfSJeff Roberson 	int msk;
103211b763dfSJeff Roberson 	int n;
103311b763dfSJeff Roberson 
103411b763dfSJeff Roberson 	n = 0;
103511b763dfSJeff Roberson 	for (msk = 0; msk < 3; msk++) {
103611b763dfSJeff Roberson 		if ((events & select_flags[msk]) == 0)
103711b763dfSJeff Roberson 			continue;
103811b763dfSJeff Roberson 		if (ibits[msk] == NULL)
103911b763dfSJeff Roberson 			continue;
104011b763dfSJeff Roberson 		if ((ibits[msk][idx] & bit) == 0)
104111b763dfSJeff Roberson 			continue;
104211b763dfSJeff Roberson 		/*
104311b763dfSJeff Roberson 		 * XXX Check for a duplicate set.  This can occur because a
104411b763dfSJeff Roberson 		 * socket calls selrecord() twice for each poll() call
104511b763dfSJeff Roberson 		 * resulting in two selfds per real fd.  selrescan() will
104611b763dfSJeff Roberson 		 * call selsetbits twice as a result.
104711b763dfSJeff Roberson 		 */
104811b763dfSJeff Roberson 		if ((obits[msk][idx] & bit) != 0)
104911b763dfSJeff Roberson 			continue;
105011b763dfSJeff Roberson 		obits[msk][idx] |= bit;
105111b763dfSJeff Roberson 		n++;
105211b763dfSJeff Roberson 	}
105311b763dfSJeff Roberson 
105411b763dfSJeff Roberson 	return (n);
105511b763dfSJeff Roberson }
1056df8bae1dSRodney W. Grimes 
1057ace8398dSJeff Roberson /*
1058ace8398dSJeff Roberson  * Traverse the list of fds attached to this thread's seltd and check for
1059ace8398dSJeff Roberson  * completion.
1060ace8398dSJeff Roberson  */
1061ace8398dSJeff Roberson static int
1062ace8398dSJeff Roberson selrescan(struct thread *td, fd_mask **ibits, fd_mask **obits)
1063ace8398dSJeff Roberson {
106411b763dfSJeff Roberson 	struct filedesc *fdp;
106511b763dfSJeff Roberson 	struct selinfo *si;
1066ace8398dSJeff Roberson 	struct seltd *stp;
1067ace8398dSJeff Roberson 	struct selfd *sfp;
1068ace8398dSJeff Roberson 	struct selfd *sfn;
1069ace8398dSJeff Roberson 	struct file *fp;
10709cdacff1SJeff Roberson 	fd_mask bit;
10719cdacff1SJeff Roberson 	int fd, ev, n, idx;
1072ace8398dSJeff Roberson 
107311b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
1074ace8398dSJeff Roberson 	stp = td->td_sel;
107511b763dfSJeff Roberson 	n = 0;
1076ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1077ace8398dSJeff Roberson 		fd = (int)(uintptr_t)sfp->sf_cookie;
1078ace8398dSJeff Roberson 		si = sfp->sf_si;
1079ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1080ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1081ace8398dSJeff Roberson 		if (si != NULL)
1082ace8398dSJeff Roberson 			continue;
1083bf422e5fSJeff Roberson 		if ((fp = fget_unlocked(fdp, fd)) == NULL)
1084ace8398dSJeff Roberson 			return (EBADF);
108511b763dfSJeff Roberson 		idx = fd / NFDBITS;
10869cdacff1SJeff Roberson 		bit = (fd_mask)1 << (fd % NFDBITS);
108711b763dfSJeff Roberson 		ev = fo_poll(fp, selflags(ibits, idx, bit), td->td_ucred, td);
1088bf422e5fSJeff Roberson 		fdrop(fp, td);
108911b763dfSJeff Roberson 		if (ev != 0)
109011b763dfSJeff Roberson 			n += selsetbits(ibits, obits, idx, bit, ev);
1091ace8398dSJeff Roberson 	}
1092ace8398dSJeff Roberson 	stp->st_flags = 0;
1093ace8398dSJeff Roberson 	td->td_retval[0] = n;
1094ace8398dSJeff Roberson 	return (0);
1095ace8398dSJeff Roberson }
1096ace8398dSJeff Roberson 
1097ace8398dSJeff Roberson /*
1098ace8398dSJeff Roberson  * Perform the initial filedescriptor scan and register ourselves with
1099ace8398dSJeff Roberson  * each selinfo.
1100ace8398dSJeff Roberson  */
1101265fc98fSSeigo Tanimura static int
1102b40ce416SJulian Elischer selscan(td, ibits, obits, nfd)
1103b40ce416SJulian Elischer 	struct thread *td;
1104b08f7993SSujal Patel 	fd_mask **ibits, **obits;
1105cb226aaaSPoul-Henning Kamp 	int nfd;
1106df8bae1dSRodney W. Grimes {
110711b763dfSJeff Roberson 	struct filedesc *fdp;
1108df8bae1dSRodney W. Grimes 	struct file *fp;
11099cdacff1SJeff Roberson 	fd_mask bit;
111011b763dfSJeff Roberson 	int ev, flags, end, fd;
11119cdacff1SJeff Roberson 	int n, idx;
1112df8bae1dSRodney W. Grimes 
111311b763dfSJeff Roberson 	fdp = td->td_proc->p_fd;
111411b763dfSJeff Roberson 	n = 0;
11159cdacff1SJeff Roberson 	for (idx = 0, fd = 0; fd < nfd; idx++) {
111611b763dfSJeff Roberson 		end = imin(fd + NFDBITS, nfd);
111711b763dfSJeff Roberson 		for (bit = 1; fd < end; bit <<= 1, fd++) {
111811b763dfSJeff Roberson 			/* Compute the list of events we're interested in. */
111911b763dfSJeff Roberson 			flags = selflags(ibits, idx, bit);
112011b763dfSJeff Roberson 			if (flags == 0)
1121f082218cSPeter Wemm 				continue;
1122bf422e5fSJeff Roberson 			if ((fp = fget_unlocked(fdp, fd)) == NULL)
1123df8bae1dSRodney W. Grimes 				return (EBADF);
1124ace8398dSJeff Roberson 			selfdalloc(td, (void *)(uintptr_t)fd);
112511b763dfSJeff Roberson 			ev = fo_poll(fp, flags, td->td_ucred, td);
1126bf422e5fSJeff Roberson 			fdrop(fp, td);
112711b763dfSJeff Roberson 			if (ev != 0)
112811b763dfSJeff Roberson 				n += selsetbits(ibits, obits, idx, bit, ev);
1129df8bae1dSRodney W. Grimes 		}
1130df8bae1dSRodney W. Grimes 	}
113111b763dfSJeff Roberson 
1132b40ce416SJulian Elischer 	td->td_retval[0] = n;
1133df8bae1dSRodney W. Grimes 	return (0);
1134df8bae1dSRodney W. Grimes }
1135df8bae1dSRodney W. Grimes 
113642d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
113742d11757SPeter Wemm struct poll_args {
113842d11757SPeter Wemm 	struct pollfd *fds;
113942d11757SPeter Wemm 	u_int	nfds;
114042d11757SPeter Wemm 	int	timeout;
114142d11757SPeter Wemm };
114242d11757SPeter Wemm #endif
114342d11757SPeter Wemm int
1144b40ce416SJulian Elischer poll(td, uap)
1145b40ce416SJulian Elischer 	struct thread *td;
1146ea0237edSJonathan Lemon 	struct poll_args *uap;
114742d11757SPeter Wemm {
11482580f4e5SAndre Oppermann 	struct pollfd *bits;
11492580f4e5SAndre Oppermann 	struct pollfd smallbits[32];
115000af9731SPoul-Henning Kamp 	struct timeval atv, rtv, ttv;
11519ae6d334SKelly Yancey 	int error = 0, timo;
1152ace8398dSJeff Roberson 	u_int nfds;
115342d11757SPeter Wemm 	size_t ni;
115442d11757SPeter Wemm 
1155d1e405c5SAlfred Perlstein 	nfds = uap->nfds;
1156374ae2a3SJeff Roberson 	if (nfds > maxfilesperproc && nfds > FD_SETSIZE)
1157ace8398dSJeff Roberson 		return (EINVAL);
115889b71647SPeter Wemm 	ni = nfds * sizeof(struct pollfd);
115942d11757SPeter Wemm 	if (ni > sizeof(smallbits))
1160a163d034SWarner Losh 		bits = malloc(ni, M_TEMP, M_WAITOK);
116142d11757SPeter Wemm 	else
116242d11757SPeter Wemm 		bits = smallbits;
1163d1e405c5SAlfred Perlstein 	error = copyin(uap->fds, bits, ni);
116442d11757SPeter Wemm 	if (error)
1165ace8398dSJeff Roberson 		goto done;
1166d1e405c5SAlfred Perlstein 	if (uap->timeout != INFTIM) {
1167d1e405c5SAlfred Perlstein 		atv.tv_sec = uap->timeout / 1000;
1168d1e405c5SAlfred Perlstein 		atv.tv_usec = (uap->timeout % 1000) * 1000;
116942d11757SPeter Wemm 		if (itimerfix(&atv)) {
117042d11757SPeter Wemm 			error = EINVAL;
1171ace8398dSJeff Roberson 			goto done;
117242d11757SPeter Wemm 		}
1173c21410e1SPoul-Henning Kamp 		getmicrouptime(&rtv);
117400af9731SPoul-Henning Kamp 		timevaladd(&atv, &rtv);
11759c386f6bSJohn Baldwin 	} else {
117600af9731SPoul-Henning Kamp 		atv.tv_sec = 0;
11779c386f6bSJohn Baldwin 		atv.tv_usec = 0;
11789c386f6bSJohn Baldwin 	}
117900af9731SPoul-Henning Kamp 	timo = 0;
1180ace8398dSJeff Roberson 	seltdinit(td);
1181ace8398dSJeff Roberson 	/* Iterate until the timeout expires or descriptors become ready. */
1182ace8398dSJeff Roberson 	for (;;) {
11832580f4e5SAndre Oppermann 		error = pollscan(td, bits, nfds);
1184ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1185ace8398dSJeff Roberson 			break;
11864da144c0SJohn Baldwin 		if (atv.tv_sec || atv.tv_usec) {
1187c21410e1SPoul-Henning Kamp 			getmicrouptime(&rtv);
118885f190e4SAlfred Perlstein 			if (timevalcmp(&rtv, &atv, >=))
1189ace8398dSJeff Roberson 				break;
119000af9731SPoul-Henning Kamp 			ttv = atv;
119100af9731SPoul-Henning Kamp 			timevalsub(&ttv, &rtv);
119200af9731SPoul-Henning Kamp 			timo = ttv.tv_sec > 24 * 60 * 60 ?
119300af9731SPoul-Henning Kamp 			    24 * 60 * 60 * hz : tvtohz(&ttv);
119442d11757SPeter Wemm 		}
1195ace8398dSJeff Roberson 		error = seltdwait(td, timo);
1196ace8398dSJeff Roberson 		if (error)
1197ace8398dSJeff Roberson 			break;
1198ace8398dSJeff Roberson 		error = pollrescan(td);
1199ace8398dSJeff Roberson 		if (error || td->td_retval[0] != 0)
1200ace8398dSJeff Roberson 			break;
120185f190e4SAlfred Perlstein 	}
1202ace8398dSJeff Roberson 	seltdclear(td);
1203265fc98fSSeigo Tanimura 
120442d11757SPeter Wemm done:
120542d11757SPeter Wemm 	/* poll is not restarted after signals... */
120642d11757SPeter Wemm 	if (error == ERESTART)
120742d11757SPeter Wemm 		error = EINTR;
120842d11757SPeter Wemm 	if (error == EWOULDBLOCK)
120942d11757SPeter Wemm 		error = 0;
121042d11757SPeter Wemm 	if (error == 0) {
1211*6d8feddaSKonstantin Belousov 		error = pollout(td, bits, uap->fds, nfds);
121242d11757SPeter Wemm 		if (error)
121342d11757SPeter Wemm 			goto out;
121442d11757SPeter Wemm 	}
121542d11757SPeter Wemm out:
121642d11757SPeter Wemm 	if (ni > sizeof(smallbits))
121742d11757SPeter Wemm 		free(bits, M_TEMP);
121842d11757SPeter Wemm 	return (error);
121942d11757SPeter Wemm }
122042d11757SPeter Wemm 
122142d11757SPeter Wemm static int
1222ace8398dSJeff Roberson pollrescan(struct thread *td)
1223ace8398dSJeff Roberson {
1224ace8398dSJeff Roberson 	struct seltd *stp;
1225ace8398dSJeff Roberson 	struct selfd *sfp;
1226ace8398dSJeff Roberson 	struct selfd *sfn;
1227ace8398dSJeff Roberson 	struct selinfo *si;
1228ace8398dSJeff Roberson 	struct filedesc *fdp;
1229ace8398dSJeff Roberson 	struct file *fp;
1230ace8398dSJeff Roberson 	struct pollfd *fd;
1231ace8398dSJeff Roberson 	int n;
1232ace8398dSJeff Roberson 
1233ace8398dSJeff Roberson 	n = 0;
1234ace8398dSJeff Roberson 	fdp = td->td_proc->p_fd;
1235ace8398dSJeff Roberson 	stp = td->td_sel;
1236ace8398dSJeff Roberson 	FILEDESC_SLOCK(fdp);
1237ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1238ace8398dSJeff Roberson 		fd = (struct pollfd *)sfp->sf_cookie;
1239ace8398dSJeff Roberson 		si = sfp->sf_si;
1240ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1241ace8398dSJeff Roberson 		/* If the selinfo wasn't cleared the event didn't fire. */
1242ace8398dSJeff Roberson 		if (si != NULL)
1243ace8398dSJeff Roberson 			continue;
1244ace8398dSJeff Roberson 		fp = fdp->fd_ofiles[fd->fd];
1245ace8398dSJeff Roberson 		if (fp == NULL) {
1246ace8398dSJeff Roberson 			fd->revents = POLLNVAL;
1247ace8398dSJeff Roberson 			n++;
1248ace8398dSJeff Roberson 			continue;
1249ace8398dSJeff Roberson 		}
1250ace8398dSJeff Roberson 		/*
1251ace8398dSJeff Roberson 		 * Note: backend also returns POLLHUP and
1252ace8398dSJeff Roberson 		 * POLLERR if appropriate.
1253ace8398dSJeff Roberson 		 */
1254ace8398dSJeff Roberson 		fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1255ace8398dSJeff Roberson 		if (fd->revents != 0)
1256ace8398dSJeff Roberson 			n++;
1257ace8398dSJeff Roberson 	}
1258ace8398dSJeff Roberson 	FILEDESC_SUNLOCK(fdp);
1259ace8398dSJeff Roberson 	stp->st_flags = 0;
1260ace8398dSJeff Roberson 	td->td_retval[0] = n;
1261ace8398dSJeff Roberson 	return (0);
1262ace8398dSJeff Roberson }
1263ace8398dSJeff Roberson 
1264ace8398dSJeff Roberson 
1265ace8398dSJeff Roberson static int
1266*6d8feddaSKonstantin Belousov pollout(td, fds, ufds, nfd)
1267*6d8feddaSKonstantin Belousov 	struct thread *td;
1268ae81968fSRobert Watson 	struct pollfd *fds;
1269ae81968fSRobert Watson 	struct pollfd *ufds;
1270ae81968fSRobert Watson 	u_int nfd;
1271ae81968fSRobert Watson {
1272ae81968fSRobert Watson 	int error = 0;
1273ae81968fSRobert Watson 	u_int i = 0;
1274*6d8feddaSKonstantin Belousov 	u_int n = 0;
1275ae81968fSRobert Watson 
1276ae81968fSRobert Watson 	for (i = 0; i < nfd; i++) {
1277ae81968fSRobert Watson 		error = copyout(&fds->revents, &ufds->revents,
1278ae81968fSRobert Watson 		    sizeof(ufds->revents));
1279ae81968fSRobert Watson 		if (error)
1280ae81968fSRobert Watson 			return (error);
1281*6d8feddaSKonstantin Belousov 		if (fds->revents != 0)
1282*6d8feddaSKonstantin Belousov 			n++;
1283ae81968fSRobert Watson 		fds++;
1284ae81968fSRobert Watson 		ufds++;
1285ae81968fSRobert Watson 	}
1286*6d8feddaSKonstantin Belousov 	td->td_retval[0] = n;
1287ae81968fSRobert Watson 	return (0);
1288ae81968fSRobert Watson }
1289ae81968fSRobert Watson 
1290ae81968fSRobert Watson static int
1291b40ce416SJulian Elischer pollscan(td, fds, nfd)
1292b40ce416SJulian Elischer 	struct thread *td;
129342d11757SPeter Wemm 	struct pollfd *fds;
1294ea0237edSJonathan Lemon 	u_int nfd;
129542d11757SPeter Wemm {
1296ace8398dSJeff Roberson 	struct filedesc *fdp = td->td_proc->p_fd;
129742d11757SPeter Wemm 	int i;
129842d11757SPeter Wemm 	struct file *fp;
129942d11757SPeter Wemm 	int n = 0;
130042d11757SPeter Wemm 
13015e3f7694SRobert Watson 	FILEDESC_SLOCK(fdp);
1302eb209311SAlfred Perlstein 	for (i = 0; i < nfd; i++, fds++) {
1303337c9691SJordan K. Hubbard 		if (fds->fd >= fdp->fd_nfiles) {
130442d11757SPeter Wemm 			fds->revents = POLLNVAL;
130542d11757SPeter Wemm 			n++;
1306337c9691SJordan K. Hubbard 		} else if (fds->fd < 0) {
1307337c9691SJordan K. Hubbard 			fds->revents = 0;
130842d11757SPeter Wemm 		} else {
130942d11757SPeter Wemm 			fp = fdp->fd_ofiles[fds->fd];
1310279d7226SMatthew Dillon 			if (fp == NULL) {
131142d11757SPeter Wemm 				fds->revents = POLLNVAL;
131242d11757SPeter Wemm 				n++;
131342d11757SPeter Wemm 			} else {
13142087c896SBruce Evans 				/*
13152087c896SBruce Evans 				 * Note: backend also returns POLLHUP and
13162087c896SBruce Evans 				 * POLLERR if appropriate.
13172087c896SBruce Evans 				 */
1318ace8398dSJeff Roberson 				selfdalloc(td, fds);
131913ccadd4SBrian Feldman 				fds->revents = fo_poll(fp, fds->events,
1320ea6027a8SRobert Watson 				    td->td_ucred, td);
1321f2159cc7SKonstantin Belousov 				/*
1322f2159cc7SKonstantin Belousov 				 * POSIX requires POLLOUT to be never
1323f2159cc7SKonstantin Belousov 				 * set simultaneously with POLLHUP.
1324f2159cc7SKonstantin Belousov 				 */
1325f2159cc7SKonstantin Belousov 				if ((fds->revents & POLLHUP) != 0)
1326f2159cc7SKonstantin Belousov 					fds->revents &= ~POLLOUT;
1327f2159cc7SKonstantin Belousov 
132842d11757SPeter Wemm 				if (fds->revents != 0)
132942d11757SPeter Wemm 					n++;
133042d11757SPeter Wemm 			}
133142d11757SPeter Wemm 		}
133242d11757SPeter Wemm 	}
13335e3f7694SRobert Watson 	FILEDESC_SUNLOCK(fdp);
1334b40ce416SJulian Elischer 	td->td_retval[0] = n;
133542d11757SPeter Wemm 	return (0);
133642d11757SPeter Wemm }
133742d11757SPeter Wemm 
133842d11757SPeter Wemm /*
133942d11757SPeter Wemm  * OpenBSD poll system call.
13400c14ff0eSRobert Watson  *
134142d11757SPeter Wemm  * XXX this isn't quite a true representation..  OpenBSD uses select ops.
134242d11757SPeter Wemm  */
134342d11757SPeter Wemm #ifndef _SYS_SYSPROTO_H_
134442d11757SPeter Wemm struct openbsd_poll_args {
134542d11757SPeter Wemm 	struct pollfd *fds;
134642d11757SPeter Wemm 	u_int	nfds;
134742d11757SPeter Wemm 	int	timeout;
134842d11757SPeter Wemm };
134942d11757SPeter Wemm #endif
135042d11757SPeter Wemm int
1351b40ce416SJulian Elischer openbsd_poll(td, uap)
1352b40ce416SJulian Elischer 	register struct thread *td;
135342d11757SPeter Wemm 	register struct openbsd_poll_args *uap;
135442d11757SPeter Wemm {
1355b40ce416SJulian Elischer 	return (poll(td, (struct poll_args *)uap));
135642d11757SPeter Wemm }
135742d11757SPeter Wemm 
135885f190e4SAlfred Perlstein /*
1359ace8398dSJeff Roberson  * XXX This was created specifically to support netncp and netsmb.  This
1360ace8398dSJeff Roberson  * allows the caller to specify a socket to wait for events on.  It returns
1361ace8398dSJeff Roberson  * 0 if any events matched and an error otherwise.  There is no way to
1362ace8398dSJeff Roberson  * determine which events fired.
136385f190e4SAlfred Perlstein  */
1364ace8398dSJeff Roberson int
1365ace8398dSJeff Roberson selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td)
136685f190e4SAlfred Perlstein {
1367ace8398dSJeff Roberson 	struct timeval atv, rtv, ttv;
1368ace8398dSJeff Roberson 	int error, timo;
136985f190e4SAlfred Perlstein 
1370ace8398dSJeff Roberson 	if (tvp != NULL) {
1371ace8398dSJeff Roberson 		atv = *tvp;
1372ace8398dSJeff Roberson 		if (itimerfix(&atv))
1373ace8398dSJeff Roberson 			return (EINVAL);
1374ace8398dSJeff Roberson 		getmicrouptime(&rtv);
1375ace8398dSJeff Roberson 		timevaladd(&atv, &rtv);
1376ace8398dSJeff Roberson 	} else {
1377ace8398dSJeff Roberson 		atv.tv_sec = 0;
1378ace8398dSJeff Roberson 		atv.tv_usec = 0;
1379ace8398dSJeff Roberson 	}
1380ace8398dSJeff Roberson 
1381ace8398dSJeff Roberson 	timo = 0;
1382ace8398dSJeff Roberson 	seltdinit(td);
1383ace8398dSJeff Roberson 	/*
1384ace8398dSJeff Roberson 	 * Iterate until the timeout expires or the socket becomes ready.
1385ace8398dSJeff Roberson 	 */
1386ace8398dSJeff Roberson 	for (;;) {
1387ace8398dSJeff Roberson 		selfdalloc(td, NULL);
1388ace8398dSJeff Roberson 		error = sopoll(so, events, NULL, td);
1389ace8398dSJeff Roberson 		/* error here is actually the ready events. */
1390ace8398dSJeff Roberson 		if (error)
1391ace8398dSJeff Roberson 			return (0);
1392ace8398dSJeff Roberson 		if (atv.tv_sec || atv.tv_usec) {
1393ace8398dSJeff Roberson 			getmicrouptime(&rtv);
1394ace8398dSJeff Roberson 			if (timevalcmp(&rtv, &atv, >=)) {
1395ace8398dSJeff Roberson 				seltdclear(td);
1396ace8398dSJeff Roberson 				return (EWOULDBLOCK);
1397ace8398dSJeff Roberson 			}
1398ace8398dSJeff Roberson 			ttv = atv;
1399ace8398dSJeff Roberson 			timevalsub(&ttv, &rtv);
1400ace8398dSJeff Roberson 			timo = ttv.tv_sec > 24 * 60 * 60 ?
1401ace8398dSJeff Roberson 			    24 * 60 * 60 * hz : tvtohz(&ttv);
1402ace8398dSJeff Roberson 		}
1403ace8398dSJeff Roberson 		error = seltdwait(td, timo);
1404ace8398dSJeff Roberson 		seltdclear(td);
1405ace8398dSJeff Roberson 		if (error)
1406ace8398dSJeff Roberson 			break;
1407ace8398dSJeff Roberson 	}
1408ace8398dSJeff Roberson 	/* XXX Duplicates ncp/smb behavior. */
1409ace8398dSJeff Roberson 	if (error == ERESTART)
1410ace8398dSJeff Roberson 		error = 0;
1411ace8398dSJeff Roberson 	return (error);
1412ace8398dSJeff Roberson }
1413ace8398dSJeff Roberson 
1414ace8398dSJeff Roberson /*
1415ace8398dSJeff Roberson  * Preallocate two selfds associated with 'cookie'.  Some fo_poll routines
1416ace8398dSJeff Roberson  * have two select sets, one for read and another for write.
1417ace8398dSJeff Roberson  */
1418ace8398dSJeff Roberson static void
1419ace8398dSJeff Roberson selfdalloc(struct thread *td, void *cookie)
1420ace8398dSJeff Roberson {
1421ace8398dSJeff Roberson 	struct seltd *stp;
1422ace8398dSJeff Roberson 
1423ace8398dSJeff Roberson 	stp = td->td_sel;
1424ace8398dSJeff Roberson 	if (stp->st_free1 == NULL)
1425ace8398dSJeff Roberson 		stp->st_free1 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1426ace8398dSJeff Roberson 	stp->st_free1->sf_td = stp;
1427ace8398dSJeff Roberson 	stp->st_free1->sf_cookie = cookie;
1428ace8398dSJeff Roberson 	if (stp->st_free2 == NULL)
1429ace8398dSJeff Roberson 		stp->st_free2 = uma_zalloc(selfd_zone, M_WAITOK|M_ZERO);
1430ace8398dSJeff Roberson 	stp->st_free2->sf_td = stp;
1431ace8398dSJeff Roberson 	stp->st_free2->sf_cookie = cookie;
1432ace8398dSJeff Roberson }
1433ace8398dSJeff Roberson 
1434ace8398dSJeff Roberson static void
1435ace8398dSJeff Roberson selfdfree(struct seltd *stp, struct selfd *sfp)
1436ace8398dSJeff Roberson {
1437ace8398dSJeff Roberson 	STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
1438ace8398dSJeff Roberson 	mtx_lock(sfp->sf_mtx);
1439ace8398dSJeff Roberson 	if (sfp->sf_si)
1440ace8398dSJeff Roberson 		TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1441ace8398dSJeff Roberson 	mtx_unlock(sfp->sf_mtx);
1442ace8398dSJeff Roberson 	uma_zfree(selfd_zone, sfp);
144385f190e4SAlfred Perlstein }
144485f190e4SAlfred Perlstein 
1445df8bae1dSRodney W. Grimes /*
1446df8bae1dSRodney W. Grimes  * Record a select request.
1447df8bae1dSRodney W. Grimes  */
1448df8bae1dSRodney W. Grimes void
1449df8bae1dSRodney W. Grimes selrecord(selector, sip)
1450b40ce416SJulian Elischer 	struct thread *selector;
1451df8bae1dSRodney W. Grimes 	struct selinfo *sip;
1452df8bae1dSRodney W. Grimes {
1453ace8398dSJeff Roberson 	struct selfd *sfp;
1454ace8398dSJeff Roberson 	struct seltd *stp;
1455ace8398dSJeff Roberson 	struct mtx *mtxp;
1456df8bae1dSRodney W. Grimes 
1457ace8398dSJeff Roberson 	stp = selector->td_sel;
145885f190e4SAlfred Perlstein 	/*
1459ace8398dSJeff Roberson 	 * Don't record when doing a rescan.
146085f190e4SAlfred Perlstein 	 */
1461ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_RESCAN)
1462ace8398dSJeff Roberson 		return;
1463ace8398dSJeff Roberson 	/*
1464ace8398dSJeff Roberson 	 * Grab one of the preallocated descriptors.
1465ace8398dSJeff Roberson 	 */
1466ace8398dSJeff Roberson 	sfp = NULL;
1467ace8398dSJeff Roberson 	if ((sfp = stp->st_free1) != NULL)
1468ace8398dSJeff Roberson 		stp->st_free1 = NULL;
1469ace8398dSJeff Roberson 	else if ((sfp = stp->st_free2) != NULL)
1470ace8398dSJeff Roberson 		stp->st_free2 = NULL;
1471ace8398dSJeff Roberson 	else
1472ace8398dSJeff Roberson 		panic("selrecord: No free selfd on selq");
14732141453eSJeff Roberson 	mtxp = sip->si_mtx;
14742141453eSJeff Roberson 	if (mtxp == NULL)
14752141453eSJeff Roberson 		mtxp = mtx_pool_find(mtxpool_select, sip);
1476ace8398dSJeff Roberson 	/*
1477ace8398dSJeff Roberson 	 * Initialize the sfp and queue it in the thread.
1478ace8398dSJeff Roberson 	 */
1479ace8398dSJeff Roberson 	sfp->sf_si = sip;
1480ace8398dSJeff Roberson 	sfp->sf_mtx = mtxp;
1481ace8398dSJeff Roberson 	STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1482ace8398dSJeff Roberson 	/*
1483ace8398dSJeff Roberson 	 * Now that we've locked the sip, check for initialization.
1484ace8398dSJeff Roberson 	 */
1485ace8398dSJeff Roberson 	mtx_lock(mtxp);
1486ace8398dSJeff Roberson 	if (sip->si_mtx == NULL) {
1487ace8398dSJeff Roberson 		sip->si_mtx = mtxp;
1488ace8398dSJeff Roberson 		TAILQ_INIT(&sip->si_tdlist);
148985f190e4SAlfred Perlstein 	}
1490ace8398dSJeff Roberson 	/*
1491ace8398dSJeff Roberson 	 * Add this thread to the list of selfds listening on this selinfo.
1492ace8398dSJeff Roberson 	 */
1493ace8398dSJeff Roberson 	TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1494ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1495df8bae1dSRodney W. Grimes }
1496df8bae1dSRodney W. Grimes 
1497512824f8SSeigo Tanimura /* Wake up a selecting thread. */
1498df8bae1dSRodney W. Grimes void
1499df8bae1dSRodney W. Grimes selwakeup(sip)
150085f190e4SAlfred Perlstein 	struct selinfo *sip;
1501df8bae1dSRodney W. Grimes {
1502512824f8SSeigo Tanimura 	doselwakeup(sip, -1);
1503512824f8SSeigo Tanimura }
1504512824f8SSeigo Tanimura 
1505512824f8SSeigo Tanimura /* Wake up a selecting thread, and set its priority. */
1506512824f8SSeigo Tanimura void
1507512824f8SSeigo Tanimura selwakeuppri(sip, pri)
1508512824f8SSeigo Tanimura 	struct selinfo *sip;
1509512824f8SSeigo Tanimura 	int pri;
1510512824f8SSeigo Tanimura {
1511512824f8SSeigo Tanimura 	doselwakeup(sip, pri);
1512512824f8SSeigo Tanimura }
1513512824f8SSeigo Tanimura 
1514512824f8SSeigo Tanimura /*
1515512824f8SSeigo Tanimura  * Do a wakeup when a selectable event occurs.
1516512824f8SSeigo Tanimura  */
1517512824f8SSeigo Tanimura static void
1518512824f8SSeigo Tanimura doselwakeup(sip, pri)
1519512824f8SSeigo Tanimura 	struct selinfo *sip;
1520512824f8SSeigo Tanimura 	int pri;
1521512824f8SSeigo Tanimura {
1522ace8398dSJeff Roberson 	struct selfd *sfp;
1523ace8398dSJeff Roberson 	struct selfd *sfn;
1524ace8398dSJeff Roberson 	struct seltd *stp;
1525df8bae1dSRodney W. Grimes 
1526ace8398dSJeff Roberson 	/* If it's not initialized there can't be any waiters. */
1527ace8398dSJeff Roberson 	if (sip->si_mtx == NULL)
1528b40ce416SJulian Elischer 		return;
1529ace8398dSJeff Roberson 	/*
1530ace8398dSJeff Roberson 	 * Locking the selinfo locks all selfds associated with it.
1531ace8398dSJeff Roberson 	 */
1532ace8398dSJeff Roberson 	mtx_lock(sip->si_mtx);
1533ace8398dSJeff Roberson 	TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1534ace8398dSJeff Roberson 		/*
1535ace8398dSJeff Roberson 		 * Once we remove this sfp from the list and clear the
1536ace8398dSJeff Roberson 		 * sf_si seltdclear will know to ignore this si.
1537ace8398dSJeff Roberson 		 */
1538ace8398dSJeff Roberson 		TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1539ace8398dSJeff Roberson 		sfp->sf_si = NULL;
1540ace8398dSJeff Roberson 		stp = sfp->sf_td;
1541ace8398dSJeff Roberson 		mtx_lock(&stp->st_mtx);
1542ace8398dSJeff Roberson 		stp->st_flags |= SELTD_PENDING;
1543ace8398dSJeff Roberson 		cv_broadcastpri(&stp->st_wait, pri);
1544ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1545b40ce416SJulian Elischer 	}
1546ace8398dSJeff Roberson 	mtx_unlock(sip->si_mtx);
1547ace8398dSJeff Roberson }
1548ace8398dSJeff Roberson 
1549ace8398dSJeff Roberson static void
1550ace8398dSJeff Roberson seltdinit(struct thread *td)
1551ace8398dSJeff Roberson {
1552ace8398dSJeff Roberson 	struct seltd *stp;
1553ace8398dSJeff Roberson 
1554ace8398dSJeff Roberson 	if ((stp = td->td_sel) != NULL)
1555ace8398dSJeff Roberson 		goto out;
1556ace8398dSJeff Roberson 	td->td_sel = stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1557ace8398dSJeff Roberson 	mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1558ace8398dSJeff Roberson 	cv_init(&stp->st_wait, "select");
1559ace8398dSJeff Roberson out:
1560ace8398dSJeff Roberson 	stp->st_flags = 0;
1561ace8398dSJeff Roberson 	STAILQ_INIT(&stp->st_selq);
1562ace8398dSJeff Roberson }
1563ace8398dSJeff Roberson 
1564ace8398dSJeff Roberson static int
1565ace8398dSJeff Roberson seltdwait(struct thread *td, int timo)
1566ace8398dSJeff Roberson {
1567ace8398dSJeff Roberson 	struct seltd *stp;
1568ace8398dSJeff Roberson 	int error;
1569ace8398dSJeff Roberson 
1570ace8398dSJeff Roberson 	stp = td->td_sel;
1571ace8398dSJeff Roberson 	/*
1572ace8398dSJeff Roberson 	 * An event of interest may occur while we do not hold the seltd
1573ace8398dSJeff Roberson 	 * locked so check the pending flag before we sleep.
1574ace8398dSJeff Roberson 	 */
1575ace8398dSJeff Roberson 	mtx_lock(&stp->st_mtx);
1576ace8398dSJeff Roberson 	/*
1577ace8398dSJeff Roberson 	 * Any further calls to selrecord will be a rescan.
1578ace8398dSJeff Roberson 	 */
1579ace8398dSJeff Roberson 	stp->st_flags |= SELTD_RESCAN;
1580ace8398dSJeff Roberson 	if (stp->st_flags & SELTD_PENDING) {
1581ace8398dSJeff Roberson 		mtx_unlock(&stp->st_mtx);
1582ace8398dSJeff Roberson 		return (0);
1583ace8398dSJeff Roberson 	}
1584ace8398dSJeff Roberson 	if (timo > 0)
1585ace8398dSJeff Roberson 		error = cv_timedwait_sig(&stp->st_wait, &stp->st_mtx, timo);
1586ace8398dSJeff Roberson 	else
1587ace8398dSJeff Roberson 		error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
1588ace8398dSJeff Roberson 	mtx_unlock(&stp->st_mtx);
1589ace8398dSJeff Roberson 
1590ace8398dSJeff Roberson 	return (error);
1591ace8398dSJeff Roberson }
1592ace8398dSJeff Roberson 
1593ace8398dSJeff Roberson void
1594ace8398dSJeff Roberson seltdfini(struct thread *td)
1595ace8398dSJeff Roberson {
1596ace8398dSJeff Roberson 	struct seltd *stp;
1597ace8398dSJeff Roberson 
1598ace8398dSJeff Roberson 	stp = td->td_sel;
1599ace8398dSJeff Roberson 	if (stp == NULL)
1600ace8398dSJeff Roberson 		return;
1601ace8398dSJeff Roberson 	if (stp->st_free1)
1602ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free1);
1603ace8398dSJeff Roberson 	if (stp->st_free2)
1604ace8398dSJeff Roberson 		uma_zfree(selfd_zone, stp->st_free2);
1605ace8398dSJeff Roberson 	td->td_sel = NULL;
1606ace8398dSJeff Roberson 	free(stp, M_SELECT);
1607ace8398dSJeff Roberson }
1608ace8398dSJeff Roberson 
1609ace8398dSJeff Roberson /*
1610ace8398dSJeff Roberson  * Remove the references to the thread from all of the objects we were
1611ace8398dSJeff Roberson  * polling.
1612ace8398dSJeff Roberson  */
1613ace8398dSJeff Roberson static void
1614ace8398dSJeff Roberson seltdclear(struct thread *td)
1615ace8398dSJeff Roberson {
1616ace8398dSJeff Roberson 	struct seltd *stp;
1617ace8398dSJeff Roberson 	struct selfd *sfp;
1618ace8398dSJeff Roberson 	struct selfd *sfn;
1619ace8398dSJeff Roberson 
1620ace8398dSJeff Roberson 	stp = td->td_sel;
1621ace8398dSJeff Roberson 	STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
1622ace8398dSJeff Roberson 		selfdfree(stp, sfp);
1623ace8398dSJeff Roberson 	stp->st_flags = 0;
1624df8bae1dSRodney W. Grimes }
1625265fc98fSSeigo Tanimura 
16264d77a549SAlfred Perlstein static void selectinit(void *);
1627ace8398dSJeff Roberson SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
1628265fc98fSSeigo Tanimura static void
1629ace8398dSJeff Roberson selectinit(void *dummy __unused)
1630265fc98fSSeigo Tanimura {
16312141453eSJeff Roberson 
1632ace8398dSJeff Roberson 	selfd_zone = uma_zcreate("selfd", sizeof(struct selfd), NULL, NULL,
1633ace8398dSJeff Roberson 	    NULL, NULL, UMA_ALIGN_PTR, 0);
16342141453eSJeff Roberson 	mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);
1635265fc98fSSeigo Tanimura }
1636