xref: /netbsd-src/sys/compat/netbsd32/netbsd32_select.c (revision 27578b9aac214cc7796ead81dcc5427e79d5f2a0)
1 /*	$NetBSD: netbsd32_select.c,v 1.1 2001/02/08 13:19:34 mrg Exp $	*/
2 
3 /*
4  * Copyright (c) 1998, 2001 Matthew R. Green
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/mount.h>
35 #include <sys/time.h>
36 #include <sys/vnode.h>
37 #include <sys/file.h>
38 #include <sys/filedesc.h>
39 
40 #include <sys/proc.h>
41 
42 #include <net/if.h>
43 
44 #include <compat/netbsd32/netbsd32.h>
45 #include <compat/netbsd32/netbsd32_syscall.h>
46 #include <compat/netbsd32/netbsd32_syscallargs.h>
47 #include <compat/netbsd32/netbsd32_conv.h>
48 
49 int
50 netbsd32_select(p, v, retval)
51 	struct proc *p;
52 	void *v;
53 	register_t *retval;
54 {
55 	struct netbsd32_select_args /* {
56 		syscallarg(int) nd;
57 		syscallarg(netbsd32_fd_setp_t) in;
58 		syscallarg(netbsd32_fd_setp_t) ou;
59 		syscallarg(netbsd32_fd_setp_t) ex;
60 		syscallarg(netbsd32_timevalp_t) tv;
61 	} */ *uap = v;
62 /* This one must be done in-line 'cause of the timeval */
63 	struct netbsd32_timeval tv32;
64 	caddr_t bits;
65 	char smallbits[howmany(FD_SETSIZE, NFDBITS) * sizeof(fd_mask) * 6];
66 	struct timeval atv;
67 	int s, ncoll, error = 0, timo;
68 	size_t ni;
69 	extern int	selwait, nselcoll;
70 	extern int selscan __P((struct proc *, fd_mask *, fd_mask *, int, register_t *));
71 
72 	if (SCARG(uap, nd) < 0)
73 		return (EINVAL);
74 	if (SCARG(uap, nd) > p->p_fd->fd_nfiles) {
75 		/* forgiving; slightly wrong */
76 		SCARG(uap, nd) = p->p_fd->fd_nfiles;
77 	}
78 	ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask);
79 	if (ni * 6 > sizeof(smallbits))
80 		bits = malloc(ni * 6, M_TEMP, M_WAITOK);
81 	else
82 		bits = smallbits;
83 
84 #define	getbits(name, x) \
85 	if (SCARG(uap, name)) { \
86 		error = copyin((caddr_t)(u_long)SCARG(uap, name), bits + ni * x, ni); \
87 		if (error) \
88 			goto done; \
89 	} else \
90 		memset(bits + ni * x, 0, ni);
91 	getbits(in, 0);
92 	getbits(ou, 1);
93 	getbits(ex, 2);
94 #undef	getbits
95 
96 	if (SCARG(uap, tv)) {
97 		error = copyin((caddr_t)(u_long)SCARG(uap, tv), (caddr_t)&tv32,
98 			sizeof(tv32));
99 		if (error)
100 			goto done;
101 		netbsd32_to_timeval(&tv32, &atv);
102 		if (itimerfix(&atv)) {
103 			error = EINVAL;
104 			goto done;
105 		}
106 		s = splclock();
107 		timeradd(&atv, &time, &atv);
108 		splx(s);
109 	} else
110 		timo = 0;
111 retry:
112 	ncoll = nselcoll;
113 	p->p_flag |= P_SELECT;
114 	error = selscan(p, (fd_mask *)(bits + ni * 0),
115 			   (fd_mask *)(bits + ni * 3), SCARG(uap, nd), retval);
116 	if (error || *retval)
117 		goto done;
118 	if (SCARG(uap, tv)) {
119 		/*
120 		 * We have to recalculate the timeout on every retry.
121 		 */
122 		timo = hzto(&atv);
123 		if (timo <= 0)
124 			goto done;
125 	}
126 	s = splhigh();
127 	if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
128 		splx(s);
129 		goto retry;
130 	}
131 	p->p_flag &= ~P_SELECT;
132 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
133 	splx(s);
134 	if (error == 0)
135 		goto retry;
136 done:
137 	p->p_flag &= ~P_SELECT;
138 	/* select is not restarted after signals... */
139 	if (error == ERESTART)
140 		error = EINTR;
141 	if (error == EWOULDBLOCK)
142 		error = 0;
143 	if (error == 0) {
144 #define	putbits(name, x) \
145 		if (SCARG(uap, name)) { \
146 			error = copyout(bits + ni * x, (caddr_t)(u_long)SCARG(uap, name), ni); \
147 			if (error) \
148 				goto out; \
149 		}
150 		putbits(in, 3);
151 		putbits(ou, 4);
152 		putbits(ex, 5);
153 #undef putbits
154 	}
155 out:
156 	if (ni * 6 > sizeof(smallbits))
157 		free(bits, M_TEMP);
158 	return (error);
159 }
160