sys_generic.c revision 1.1.1.2 1 1.1.1.2 fvdl /*
2 1.1.1.2 fvdl * Copyright (c) 1982, 1986, 1989, 1993
3 1.1.1.2 fvdl * The Regents of the University of California. All rights reserved.
4 1.1.1.2 fvdl * (c) UNIX System Laboratories, Inc.
5 1.1.1.2 fvdl * All or some portions of this file are derived from material licensed
6 1.1.1.2 fvdl * to the University of California by American Telephone and Telegraph
7 1.1.1.2 fvdl * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 1.1.1.2 fvdl * the permission of UNIX System Laboratories, Inc.
9 1.1.1.2 fvdl *
10 1.1.1.2 fvdl * Redistribution and use in source and binary forms, with or without
11 1.1.1.2 fvdl * modification, are permitted provided that the following conditions
12 1.1.1.2 fvdl * are met:
13 1.1.1.2 fvdl * 1. Redistributions of source code must retain the above copyright
14 1.1.1.2 fvdl * notice, this list of conditions and the following disclaimer.
15 1.1.1.2 fvdl * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.1.2 fvdl * notice, this list of conditions and the following disclaimer in the
17 1.1.1.2 fvdl * documentation and/or other materials provided with the distribution.
18 1.1.1.2 fvdl * 3. All advertising materials mentioning features or use of this software
19 1.1.1.2 fvdl * must display the following acknowledgement:
20 1.1.1.2 fvdl * This product includes software developed by the University of
21 1.1.1.2 fvdl * California, Berkeley and its contributors.
22 1.1.1.2 fvdl * 4. Neither the name of the University nor the names of its contributors
23 1.1.1.2 fvdl * may be used to endorse or promote products derived from this software
24 1.1.1.2 fvdl * without specific prior written permission.
25 1.1.1.2 fvdl *
26 1.1.1.2 fvdl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1.1.2 fvdl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1.1.2 fvdl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1.1.2 fvdl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1.1.2 fvdl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1.1.2 fvdl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1.1.2 fvdl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1.1.2 fvdl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1.1.2 fvdl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1.1.2 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1.1.2 fvdl * SUCH DAMAGE.
37 1.1.1.2 fvdl *
38 1.1.1.2 fvdl * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94
39 1.1.1.2 fvdl */
40 1.1.1.2 fvdl
41 1.1.1.2 fvdl #include <sys/param.h>
42 1.1.1.2 fvdl #include <sys/systm.h>
43 1.1.1.2 fvdl #include <sys/filedesc.h>
44 1.1.1.2 fvdl #include <sys/ioctl.h>
45 1.1.1.2 fvdl #include <sys/file.h>
46 1.1.1.2 fvdl #include <sys/proc.h>
47 1.1.1.2 fvdl #include <sys/socketvar.h>
48 1.1.1.2 fvdl #include <sys/uio.h>
49 1.1.1.2 fvdl #include <sys/kernel.h>
50 1.1.1.2 fvdl #include <sys/stat.h>
51 1.1.1.2 fvdl #include <sys/malloc.h>
52 1.1.1.2 fvdl #ifdef KTRACE
53 1.1.1.2 fvdl #include <sys/ktrace.h>
54 1.1.1.2 fvdl #endif
55 1.1.1.2 fvdl
56 1.1.1.2 fvdl /*
57 1.1.1.2 fvdl * Read system call.
58 1.1.1.2 fvdl */
59 1.1.1.2 fvdl struct read_args {
60 1.1.1.2 fvdl int fd;
61 1.1.1.2 fvdl char *buf;
62 1.1.1.2 fvdl u_int nbyte;
63 1.1.1.2 fvdl };
64 1.1.1.2 fvdl /* ARGSUSED */
65 1.1.1.2 fvdl read(p, uap, retval)
66 1.1.1.2 fvdl struct proc *p;
67 1.1.1.2 fvdl register struct read_args *uap;
68 1.1.1.2 fvdl int *retval;
69 1.1.1.2 fvdl {
70 1.1.1.2 fvdl register struct file *fp;
71 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
72 1.1.1.2 fvdl struct uio auio;
73 1.1.1.2 fvdl struct iovec aiov;
74 1.1.1.2 fvdl long cnt, error = 0;
75 1.1.1.2 fvdl #ifdef KTRACE
76 1.1.1.2 fvdl struct iovec ktriov;
77 1.1.1.2 fvdl #endif
78 1.1.1.2 fvdl
79 1.1.1.2 fvdl if (((u_int)uap->fd) >= fdp->fd_nfiles ||
80 1.1.1.2 fvdl (fp = fdp->fd_ofiles[uap->fd]) == NULL ||
81 1.1.1.2 fvdl (fp->f_flag & FREAD) == 0)
82 1.1.1.2 fvdl return (EBADF);
83 1.1.1.2 fvdl aiov.iov_base = (caddr_t)uap->buf;
84 1.1.1.2 fvdl aiov.iov_len = uap->nbyte;
85 1.1.1.2 fvdl auio.uio_iov = &aiov;
86 1.1.1.2 fvdl auio.uio_iovcnt = 1;
87 1.1.1.2 fvdl auio.uio_resid = uap->nbyte;
88 1.1.1.2 fvdl auio.uio_rw = UIO_READ;
89 1.1.1.2 fvdl auio.uio_segflg = UIO_USERSPACE;
90 1.1.1.2 fvdl auio.uio_procp = p;
91 1.1.1.2 fvdl #ifdef KTRACE
92 1.1.1.2 fvdl /*
93 1.1.1.2 fvdl * if tracing, save a copy of iovec
94 1.1.1.2 fvdl */
95 1.1.1.2 fvdl if (KTRPOINT(p, KTR_GENIO))
96 1.1.1.2 fvdl ktriov = aiov;
97 1.1.1.2 fvdl #endif
98 1.1.1.2 fvdl cnt = uap->nbyte;
99 1.1.1.2 fvdl if (error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred))
100 1.1.1.2 fvdl if (auio.uio_resid != cnt && (error == ERESTART ||
101 1.1.1.2 fvdl error == EINTR || error == EWOULDBLOCK))
102 1.1.1.2 fvdl error = 0;
103 1.1.1.2 fvdl cnt -= auio.uio_resid;
104 1.1.1.2 fvdl #ifdef KTRACE
105 1.1.1.2 fvdl if (KTRPOINT(p, KTR_GENIO) && error == 0)
106 1.1.1.2 fvdl ktrgenio(p->p_tracep, uap->fd, UIO_READ, &ktriov, cnt, error);
107 1.1.1.2 fvdl #endif
108 1.1.1.2 fvdl *retval = cnt;
109 1.1.1.2 fvdl return (error);
110 1.1.1.2 fvdl }
111 1.1.1.2 fvdl
112 1.1.1.2 fvdl /*
113 1.1.1.2 fvdl * Scatter read system call.
114 1.1.1.2 fvdl */
115 1.1.1.2 fvdl struct readv_args {
116 1.1.1.2 fvdl int fdes;
117 1.1.1.2 fvdl struct iovec *iovp;
118 1.1.1.2 fvdl u_int iovcnt;
119 1.1.1.2 fvdl };
120 1.1.1.2 fvdl readv(p, uap, retval)
121 1.1.1.2 fvdl struct proc *p;
122 1.1.1.2 fvdl register struct readv_args *uap;
123 1.1.1.2 fvdl int *retval;
124 1.1.1.2 fvdl {
125 1.1.1.2 fvdl register struct file *fp;
126 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
127 1.1.1.2 fvdl struct uio auio;
128 1.1.1.2 fvdl register struct iovec *iov;
129 1.1.1.2 fvdl struct iovec *needfree;
130 1.1.1.2 fvdl struct iovec aiov[UIO_SMALLIOV];
131 1.1.1.2 fvdl long i, cnt, error = 0;
132 1.1.1.2 fvdl u_int iovlen;
133 1.1.1.2 fvdl #ifdef KTRACE
134 1.1.1.2 fvdl struct iovec *ktriov = NULL;
135 1.1.1.2 fvdl #endif
136 1.1.1.2 fvdl
137 1.1.1.2 fvdl if (((u_int)uap->fdes) >= fdp->fd_nfiles ||
138 1.1.1.2 fvdl (fp = fdp->fd_ofiles[uap->fdes]) == NULL ||
139 1.1.1.2 fvdl (fp->f_flag & FREAD) == 0)
140 1.1.1.2 fvdl return (EBADF);
141 1.1.1.2 fvdl /* note: can't use iovlen until iovcnt is validated */
142 1.1.1.2 fvdl iovlen = uap->iovcnt * sizeof (struct iovec);
143 1.1.1.2 fvdl if (uap->iovcnt > UIO_SMALLIOV) {
144 1.1.1.2 fvdl if (uap->iovcnt > UIO_MAXIOV)
145 1.1.1.2 fvdl return (EINVAL);
146 1.1.1.2 fvdl MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
147 1.1.1.2 fvdl needfree = iov;
148 1.1.1.2 fvdl } else {
149 1.1.1.2 fvdl iov = aiov;
150 1.1.1.2 fvdl needfree = NULL;
151 1.1.1.2 fvdl }
152 1.1.1.2 fvdl auio.uio_iov = iov;
153 1.1.1.2 fvdl auio.uio_iovcnt = uap->iovcnt;
154 1.1.1.2 fvdl auio.uio_rw = UIO_READ;
155 1.1.1.2 fvdl auio.uio_segflg = UIO_USERSPACE;
156 1.1.1.2 fvdl auio.uio_procp = p;
157 1.1.1.2 fvdl if (error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen))
158 1.1.1.2 fvdl goto done;
159 1.1.1.2 fvdl auio.uio_resid = 0;
160 1.1.1.2 fvdl for (i = 0; i < uap->iovcnt; i++) {
161 1.1.1.2 fvdl if (iov->iov_len < 0) {
162 1.1.1.2 fvdl error = EINVAL;
163 1.1.1.2 fvdl goto done;
164 1.1.1.2 fvdl }
165 1.1.1.2 fvdl auio.uio_resid += iov->iov_len;
166 1.1.1.2 fvdl if (auio.uio_resid < 0) {
167 1.1.1.2 fvdl error = EINVAL;
168 1.1.1.2 fvdl goto done;
169 1.1.1.2 fvdl }
170 1.1.1.2 fvdl iov++;
171 1.1.1.2 fvdl }
172 1.1.1.2 fvdl #ifdef KTRACE
173 1.1.1.2 fvdl /*
174 1.1.1.2 fvdl * if tracing, save a copy of iovec
175 1.1.1.2 fvdl */
176 1.1.1.2 fvdl if (KTRPOINT(p, KTR_GENIO)) {
177 1.1.1.2 fvdl MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
178 1.1.1.2 fvdl bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
179 1.1.1.2 fvdl }
180 1.1.1.2 fvdl #endif
181 1.1.1.2 fvdl cnt = auio.uio_resid;
182 1.1.1.2 fvdl if (error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred))
183 1.1.1.2 fvdl if (auio.uio_resid != cnt && (error == ERESTART ||
184 1.1.1.2 fvdl error == EINTR || error == EWOULDBLOCK))
185 1.1.1.2 fvdl error = 0;
186 1.1.1.2 fvdl cnt -= auio.uio_resid;
187 1.1.1.2 fvdl #ifdef KTRACE
188 1.1.1.2 fvdl if (ktriov != NULL) {
189 1.1.1.2 fvdl if (error == 0)
190 1.1.1.2 fvdl ktrgenio(p->p_tracep, uap->fdes, UIO_READ, ktriov,
191 1.1.1.2 fvdl cnt, error);
192 1.1.1.2 fvdl FREE(ktriov, M_TEMP);
193 1.1.1.2 fvdl }
194 1.1.1.2 fvdl #endif
195 1.1.1.2 fvdl *retval = cnt;
196 1.1.1.2 fvdl done:
197 1.1.1.2 fvdl if (needfree)
198 1.1.1.2 fvdl FREE(needfree, M_IOV);
199 1.1.1.2 fvdl return (error);
200 1.1.1.2 fvdl }
201 1.1.1.2 fvdl
202 1.1.1.2 fvdl /*
203 1.1.1.2 fvdl * Write system call
204 1.1.1.2 fvdl */
205 1.1.1.2 fvdl struct write_args {
206 1.1.1.2 fvdl int fd;
207 1.1.1.2 fvdl char *buf;
208 1.1.1.2 fvdl u_int nbyte;
209 1.1.1.2 fvdl };
210 1.1.1.2 fvdl write(p, uap, retval)
211 1.1.1.2 fvdl struct proc *p;
212 1.1.1.2 fvdl register struct write_args *uap;
213 1.1.1.2 fvdl int *retval;
214 1.1.1.2 fvdl {
215 1.1.1.2 fvdl register struct file *fp;
216 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
217 1.1.1.2 fvdl struct uio auio;
218 1.1.1.2 fvdl struct iovec aiov;
219 1.1.1.2 fvdl long cnt, error = 0;
220 1.1.1.2 fvdl #ifdef KTRACE
221 1.1.1.2 fvdl struct iovec ktriov;
222 1.1.1.2 fvdl #endif
223 1.1.1.2 fvdl
224 1.1.1.2 fvdl if (((u_int)uap->fd) >= fdp->fd_nfiles ||
225 1.1.1.2 fvdl (fp = fdp->fd_ofiles[uap->fd]) == NULL ||
226 1.1.1.2 fvdl (fp->f_flag & FWRITE) == 0)
227 1.1.1.2 fvdl return (EBADF);
228 1.1.1.2 fvdl aiov.iov_base = (caddr_t)uap->buf;
229 1.1.1.2 fvdl aiov.iov_len = uap->nbyte;
230 1.1.1.2 fvdl auio.uio_iov = &aiov;
231 1.1.1.2 fvdl auio.uio_iovcnt = 1;
232 1.1.1.2 fvdl auio.uio_resid = uap->nbyte;
233 1.1.1.2 fvdl auio.uio_rw = UIO_WRITE;
234 1.1.1.2 fvdl auio.uio_segflg = UIO_USERSPACE;
235 1.1.1.2 fvdl auio.uio_procp = p;
236 1.1.1.2 fvdl #ifdef KTRACE
237 1.1.1.2 fvdl /*
238 1.1.1.2 fvdl * if tracing, save a copy of iovec
239 1.1.1.2 fvdl */
240 1.1.1.2 fvdl if (KTRPOINT(p, KTR_GENIO))
241 1.1.1.2 fvdl ktriov = aiov;
242 1.1.1.2 fvdl #endif
243 1.1.1.2 fvdl cnt = uap->nbyte;
244 1.1.1.2 fvdl if (error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred)) {
245 1.1.1.2 fvdl if (auio.uio_resid != cnt && (error == ERESTART ||
246 1.1.1.2 fvdl error == EINTR || error == EWOULDBLOCK))
247 1.1.1.2 fvdl error = 0;
248 1.1.1.2 fvdl if (error == EPIPE)
249 1.1.1.2 fvdl psignal(p, SIGPIPE);
250 1.1.1.2 fvdl }
251 1.1.1.2 fvdl cnt -= auio.uio_resid;
252 1.1.1.2 fvdl #ifdef KTRACE
253 1.1.1.2 fvdl if (KTRPOINT(p, KTR_GENIO) && error == 0)
254 1.1.1.2 fvdl ktrgenio(p->p_tracep, uap->fd, UIO_WRITE,
255 1.1.1.2 fvdl &ktriov, cnt, error);
256 1.1.1.2 fvdl #endif
257 1.1.1.2 fvdl *retval = cnt;
258 1.1.1.2 fvdl return (error);
259 1.1.1.2 fvdl }
260 1.1.1.2 fvdl
261 1.1.1.2 fvdl /*
262 1.1.1.2 fvdl * Gather write system call
263 1.1.1.2 fvdl */
264 1.1.1.2 fvdl struct writev_args {
265 1.1.1.2 fvdl int fd;
266 1.1.1.2 fvdl struct iovec *iovp;
267 1.1.1.2 fvdl u_int iovcnt;
268 1.1.1.2 fvdl };
269 1.1.1.2 fvdl writev(p, uap, retval)
270 1.1.1.2 fvdl struct proc *p;
271 1.1.1.2 fvdl register struct writev_args *uap;
272 1.1.1.2 fvdl int *retval;
273 1.1.1.2 fvdl {
274 1.1.1.2 fvdl register struct file *fp;
275 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
276 1.1.1.2 fvdl struct uio auio;
277 1.1.1.2 fvdl register struct iovec *iov;
278 1.1.1.2 fvdl struct iovec *needfree;
279 1.1.1.2 fvdl struct iovec aiov[UIO_SMALLIOV];
280 1.1.1.2 fvdl long i, cnt, error = 0;
281 1.1.1.2 fvdl u_int iovlen;
282 1.1.1.2 fvdl #ifdef KTRACE
283 1.1.1.2 fvdl struct iovec *ktriov = NULL;
284 1.1.1.2 fvdl #endif
285 1.1.1.2 fvdl
286 1.1.1.2 fvdl if (((u_int)uap->fd) >= fdp->fd_nfiles ||
287 1.1.1.2 fvdl (fp = fdp->fd_ofiles[uap->fd]) == NULL ||
288 1.1.1.2 fvdl (fp->f_flag & FWRITE) == 0)
289 1.1.1.2 fvdl return (EBADF);
290 1.1.1.2 fvdl /* note: can't use iovlen until iovcnt is validated */
291 1.1.1.2 fvdl iovlen = uap->iovcnt * sizeof (struct iovec);
292 1.1.1.2 fvdl if (uap->iovcnt > UIO_SMALLIOV) {
293 1.1.1.2 fvdl if (uap->iovcnt > UIO_MAXIOV)
294 1.1.1.2 fvdl return (EINVAL);
295 1.1.1.2 fvdl MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
296 1.1.1.2 fvdl needfree = iov;
297 1.1.1.2 fvdl } else {
298 1.1.1.2 fvdl iov = aiov;
299 1.1.1.2 fvdl needfree = NULL;
300 1.1.1.2 fvdl }
301 1.1.1.2 fvdl auio.uio_iov = iov;
302 1.1.1.2 fvdl auio.uio_iovcnt = uap->iovcnt;
303 1.1.1.2 fvdl auio.uio_rw = UIO_WRITE;
304 1.1.1.2 fvdl auio.uio_segflg = UIO_USERSPACE;
305 1.1.1.2 fvdl auio.uio_procp = p;
306 1.1.1.2 fvdl if (error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen))
307 1.1.1.2 fvdl goto done;
308 1.1.1.2 fvdl auio.uio_resid = 0;
309 1.1.1.2 fvdl for (i = 0; i < uap->iovcnt; i++) {
310 1.1.1.2 fvdl if (iov->iov_len < 0) {
311 1.1.1.2 fvdl error = EINVAL;
312 1.1.1.2 fvdl goto done;
313 1.1.1.2 fvdl }
314 1.1.1.2 fvdl auio.uio_resid += iov->iov_len;
315 1.1.1.2 fvdl if (auio.uio_resid < 0) {
316 1.1.1.2 fvdl error = EINVAL;
317 1.1.1.2 fvdl goto done;
318 1.1.1.2 fvdl }
319 1.1.1.2 fvdl iov++;
320 1.1.1.2 fvdl }
321 1.1.1.2 fvdl #ifdef KTRACE
322 1.1.1.2 fvdl /*
323 1.1.1.2 fvdl * if tracing, save a copy of iovec
324 1.1.1.2 fvdl */
325 1.1.1.2 fvdl if (KTRPOINT(p, KTR_GENIO)) {
326 1.1.1.2 fvdl MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
327 1.1.1.2 fvdl bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
328 1.1.1.2 fvdl }
329 1.1.1.2 fvdl #endif
330 1.1.1.2 fvdl cnt = auio.uio_resid;
331 1.1.1.2 fvdl if (error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred)) {
332 1.1.1.2 fvdl if (auio.uio_resid != cnt && (error == ERESTART ||
333 1.1.1.2 fvdl error == EINTR || error == EWOULDBLOCK))
334 1.1.1.2 fvdl error = 0;
335 1.1.1.2 fvdl if (error == EPIPE)
336 1.1.1.2 fvdl psignal(p, SIGPIPE);
337 1.1.1.2 fvdl }
338 1.1.1.2 fvdl cnt -= auio.uio_resid;
339 1.1.1.2 fvdl #ifdef KTRACE
340 1.1.1.2 fvdl if (ktriov != NULL) {
341 1.1.1.2 fvdl if (error == 0)
342 1.1.1.2 fvdl ktrgenio(p->p_tracep, uap->fd, UIO_WRITE,
343 1.1.1.2 fvdl ktriov, cnt, error);
344 1.1.1.2 fvdl FREE(ktriov, M_TEMP);
345 1.1.1.2 fvdl }
346 1.1.1.2 fvdl #endif
347 1.1.1.2 fvdl *retval = cnt;
348 1.1.1.2 fvdl done:
349 1.1.1.2 fvdl if (needfree)
350 1.1.1.2 fvdl FREE(needfree, M_IOV);
351 1.1.1.2 fvdl return (error);
352 1.1.1.2 fvdl }
353 1.1.1.2 fvdl
354 1.1.1.2 fvdl /*
355 1.1.1.2 fvdl * Ioctl system call
356 1.1.1.2 fvdl */
357 1.1.1.2 fvdl struct ioctl_args {
358 1.1.1.2 fvdl int fd;
359 1.1.1.2 fvdl int com;
360 1.1.1.2 fvdl caddr_t data;
361 1.1.1.2 fvdl };
362 1.1.1.2 fvdl /* ARGSUSED */
363 1.1.1.2 fvdl ioctl(p, uap, retval)
364 1.1.1.2 fvdl struct proc *p;
365 1.1.1.2 fvdl register struct ioctl_args *uap;
366 1.1.1.2 fvdl int *retval;
367 1.1.1.2 fvdl {
368 1.1.1.2 fvdl register struct file *fp;
369 1.1.1.2 fvdl register struct filedesc *fdp;
370 1.1.1.2 fvdl register int com, error;
371 1.1.1.2 fvdl register u_int size;
372 1.1.1.2 fvdl caddr_t data, memp;
373 1.1.1.2 fvdl int tmp;
374 1.1.1.2 fvdl #define STK_PARAMS 128
375 1.1.1.2 fvdl char stkbuf[STK_PARAMS];
376 1.1.1.2 fvdl
377 1.1.1.2 fvdl fdp = p->p_fd;
378 1.1.1.2 fvdl if ((u_int)uap->fd >= fdp->fd_nfiles ||
379 1.1.1.2 fvdl (fp = fdp->fd_ofiles[uap->fd]) == NULL)
380 1.1.1.2 fvdl return (EBADF);
381 1.1.1.2 fvdl
382 1.1.1.2 fvdl if ((fp->f_flag & (FREAD | FWRITE)) == 0)
383 1.1.1.2 fvdl return (EBADF);
384 1.1.1.2 fvdl
385 1.1.1.2 fvdl switch (com = uap->com) {
386 1.1.1.2 fvdl case FIONCLEX:
387 1.1.1.2 fvdl fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
388 1.1.1.2 fvdl return (0);
389 1.1.1.2 fvdl case FIOCLEX:
390 1.1.1.2 fvdl fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
391 1.1.1.2 fvdl return (0);
392 1.1.1.2 fvdl }
393 1.1.1.2 fvdl
394 1.1.1.2 fvdl /*
395 1.1.1.2 fvdl * Interpret high order word to find amount of data to be
396 1.1.1.2 fvdl * copied to/from the user's address space.
397 1.1.1.2 fvdl */
398 1.1.1.2 fvdl size = IOCPARM_LEN(com);
399 1.1.1.2 fvdl if (size > IOCPARM_MAX)
400 1.1.1.2 fvdl return (ENOTTY);
401 1.1.1.2 fvdl memp = NULL;
402 1.1.1.2 fvdl if (size > sizeof (stkbuf)) {
403 1.1.1.2 fvdl memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
404 1.1.1.2 fvdl data = memp;
405 1.1.1.2 fvdl } else
406 1.1.1.2 fvdl data = stkbuf;
407 1.1.1.2 fvdl if (com&IOC_IN) {
408 1.1.1.2 fvdl if (size) {
409 1.1.1.2 fvdl error = copyin(uap->data, data, (u_int)size);
410 1.1.1.2 fvdl if (error) {
411 1.1.1.2 fvdl if (memp)
412 1.1.1.2 fvdl free(memp, M_IOCTLOPS);
413 1.1.1.2 fvdl return (error);
414 1.1.1.2 fvdl }
415 1.1.1.2 fvdl } else
416 1.1.1.2 fvdl *(caddr_t *)data = uap->data;
417 1.1.1.2 fvdl } else if ((com&IOC_OUT) && size)
418 1.1.1.2 fvdl /*
419 1.1.1.2 fvdl * Zero the buffer so the user always
420 1.1.1.2 fvdl * gets back something deterministic.
421 1.1.1.2 fvdl */
422 1.1.1.2 fvdl bzero(data, size);
423 1.1.1.2 fvdl else if (com&IOC_VOID)
424 1.1.1.2 fvdl *(caddr_t *)data = uap->data;
425 1.1.1.2 fvdl
426 1.1.1.2 fvdl switch (com) {
427 1.1.1.2 fvdl
428 1.1.1.2 fvdl case FIONBIO:
429 1.1.1.2 fvdl if (tmp = *(int *)data)
430 1.1.1.2 fvdl fp->f_flag |= FNONBLOCK;
431 1.1.1.2 fvdl else
432 1.1.1.2 fvdl fp->f_flag &= ~FNONBLOCK;
433 1.1.1.2 fvdl error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
434 1.1.1.2 fvdl break;
435 1.1.1.2 fvdl
436 1.1.1.2 fvdl case FIOASYNC:
437 1.1.1.2 fvdl if (tmp = *(int *)data)
438 1.1.1.2 fvdl fp->f_flag |= FASYNC;
439 1.1.1.2 fvdl else
440 1.1.1.2 fvdl fp->f_flag &= ~FASYNC;
441 1.1.1.2 fvdl error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
442 1.1.1.2 fvdl break;
443 1.1.1.2 fvdl
444 1.1.1.2 fvdl case FIOSETOWN:
445 1.1.1.2 fvdl tmp = *(int *)data;
446 1.1.1.2 fvdl if (fp->f_type == DTYPE_SOCKET) {
447 1.1.1.2 fvdl ((struct socket *)fp->f_data)->so_pgid = tmp;
448 1.1.1.2 fvdl error = 0;
449 1.1.1.2 fvdl break;
450 1.1.1.2 fvdl }
451 1.1.1.2 fvdl if (tmp <= 0) {
452 1.1.1.2 fvdl tmp = -tmp;
453 1.1.1.2 fvdl } else {
454 1.1.1.2 fvdl struct proc *p1 = pfind(tmp);
455 1.1.1.2 fvdl if (p1 == 0) {
456 1.1.1.2 fvdl error = ESRCH;
457 1.1.1.2 fvdl break;
458 1.1.1.2 fvdl }
459 1.1.1.2 fvdl tmp = p1->p_pgrp->pg_id;
460 1.1.1.2 fvdl }
461 1.1.1.2 fvdl error = (*fp->f_ops->fo_ioctl)
462 1.1.1.2 fvdl (fp, (int)TIOCSPGRP, (caddr_t)&tmp, p);
463 1.1.1.2 fvdl break;
464 1.1.1.2 fvdl
465 1.1.1.2 fvdl case FIOGETOWN:
466 1.1.1.2 fvdl if (fp->f_type == DTYPE_SOCKET) {
467 1.1.1.2 fvdl error = 0;
468 1.1.1.2 fvdl *(int *)data = ((struct socket *)fp->f_data)->so_pgid;
469 1.1.1.2 fvdl break;
470 1.1.1.2 fvdl }
471 1.1.1.2 fvdl error = (*fp->f_ops->fo_ioctl)(fp, (int)TIOCGPGRP, data, p);
472 1.1.1.2 fvdl *(int *)data = -*(int *)data;
473 1.1.1.2 fvdl break;
474 1.1.1.2 fvdl
475 1.1.1.2 fvdl default:
476 1.1.1.2 fvdl error = (*fp->f_ops->fo_ioctl)(fp, com, data, p);
477 1.1.1.2 fvdl /*
478 1.1.1.2 fvdl * Copy any data to user, size was
479 1.1.1.2 fvdl * already set and checked above.
480 1.1.1.2 fvdl */
481 1.1.1.2 fvdl if (error == 0 && (com&IOC_OUT) && size)
482 1.1.1.2 fvdl error = copyout(data, uap->data, (u_int)size);
483 1.1.1.2 fvdl break;
484 1.1.1.2 fvdl }
485 1.1.1.2 fvdl if (memp)
486 1.1.1.2 fvdl free(memp, M_IOCTLOPS);
487 1.1.1.2 fvdl return (error);
488 1.1.1.2 fvdl }
489 1.1.1.2 fvdl
490 1.1.1.2 fvdl int selwait, nselcoll;
491 1.1.1.2 fvdl
492 1.1.1.2 fvdl /*
493 1.1.1.2 fvdl * Select system call.
494 1.1.1.2 fvdl */
495 1.1.1.2 fvdl struct select_args {
496 1.1.1.2 fvdl u_int nd;
497 1.1.1.2 fvdl fd_set *in, *ou, *ex;
498 1.1.1.2 fvdl struct timeval *tv;
499 1.1.1.2 fvdl };
500 1.1.1.2 fvdl select(p, uap, retval)
501 1.1.1.2 fvdl register struct proc *p;
502 1.1.1.2 fvdl register struct select_args *uap;
503 1.1.1.2 fvdl int *retval;
504 1.1.1.2 fvdl {
505 1.1.1.2 fvdl fd_set ibits[3], obits[3];
506 1.1.1.2 fvdl struct timeval atv;
507 1.1.1.2 fvdl int s, ncoll, error = 0, timo;
508 1.1.1.2 fvdl u_int ni;
509 1.1.1.2 fvdl
510 1.1.1.2 fvdl bzero((caddr_t)ibits, sizeof(ibits));
511 1.1.1.2 fvdl bzero((caddr_t)obits, sizeof(obits));
512 1.1.1.2 fvdl if (uap->nd > FD_SETSIZE)
513 1.1.1.2 fvdl return (EINVAL);
514 1.1.1.2 fvdl if (uap->nd > p->p_fd->fd_nfiles)
515 1.1.1.2 fvdl uap->nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */
516 1.1.1.2 fvdl ni = howmany(uap->nd, NFDBITS) * sizeof(fd_mask);
517 1.1.1.2 fvdl
518 1.1.1.2 fvdl #define getbits(name, x) \
519 1.1.1.2 fvdl if (uap->name && \
520 1.1.1.2 fvdl (error = copyin((caddr_t)uap->name, (caddr_t)&ibits[x], ni))) \
521 1.1.1.2 fvdl goto done;
522 1.1.1.2 fvdl getbits(in, 0);
523 1.1.1.2 fvdl getbits(ou, 1);
524 1.1.1.2 fvdl getbits(ex, 2);
525 1.1.1.2 fvdl #undef getbits
526 1.1.1.2 fvdl
527 1.1.1.2 fvdl if (uap->tv) {
528 1.1.1.2 fvdl error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
529 1.1.1.2 fvdl sizeof (atv));
530 1.1.1.2 fvdl if (error)
531 1.1.1.2 fvdl goto done;
532 1.1.1.2 fvdl if (itimerfix(&atv)) {
533 1.1.1.2 fvdl error = EINVAL;
534 1.1.1.2 fvdl goto done;
535 1.1.1.2 fvdl }
536 1.1.1.2 fvdl s = splclock();
537 1.1.1.2 fvdl timevaladd(&atv, (struct timeval *)&time);
538 1.1.1.2 fvdl timo = hzto(&atv);
539 1.1.1.2 fvdl /*
540 1.1.1.2 fvdl * Avoid inadvertently sleeping forever.
541 1.1.1.2 fvdl */
542 1.1.1.2 fvdl if (timo == 0)
543 1.1.1.2 fvdl timo = 1;
544 1.1.1.2 fvdl splx(s);
545 1.1.1.2 fvdl } else
546 1.1.1.2 fvdl timo = 0;
547 1.1.1.2 fvdl retry:
548 1.1.1.2 fvdl ncoll = nselcoll;
549 1.1.1.2 fvdl p->p_flag |= P_SELECT;
550 1.1.1.2 fvdl error = selscan(p, ibits, obits, uap->nd, retval);
551 1.1.1.2 fvdl if (error || *retval)
552 1.1.1.2 fvdl goto done;
553 1.1.1.2 fvdl s = splhigh();
554 1.1.1.2 fvdl /* this should be timercmp(&time, &atv, >=) */
555 1.1.1.2 fvdl if (uap->tv && (time.tv_sec > atv.tv_sec ||
556 1.1.1.2 fvdl time.tv_sec == atv.tv_sec && time.tv_usec >= atv.tv_usec)) {
557 1.1.1.2 fvdl splx(s);
558 1.1.1.2 fvdl goto done;
559 1.1.1.2 fvdl }
560 1.1.1.2 fvdl if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
561 1.1.1.2 fvdl splx(s);
562 1.1.1.2 fvdl goto retry;
563 1.1.1.2 fvdl }
564 1.1.1.2 fvdl p->p_flag &= ~P_SELECT;
565 1.1.1.2 fvdl error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
566 1.1.1.2 fvdl splx(s);
567 1.1.1.2 fvdl if (error == 0)
568 1.1.1.2 fvdl goto retry;
569 1.1.1.2 fvdl done:
570 1.1.1.2 fvdl p->p_flag &= ~P_SELECT;
571 1.1.1.2 fvdl /* select is not restarted after signals... */
572 1.1.1.2 fvdl if (error == ERESTART)
573 1.1.1.2 fvdl error = EINTR;
574 1.1.1.2 fvdl if (error == EWOULDBLOCK)
575 1.1.1.2 fvdl error = 0;
576 1.1.1.2 fvdl #define putbits(name, x) \
577 1.1.1.2 fvdl if (uap->name && \
578 1.1.1.2 fvdl (error2 = copyout((caddr_t)&obits[x], (caddr_t)uap->name, ni))) \
579 1.1.1.2 fvdl error = error2;
580 1.1.1.2 fvdl if (error == 0) {
581 1.1.1.2 fvdl int error2;
582 1.1.1.2 fvdl
583 1.1.1.2 fvdl putbits(in, 0);
584 1.1.1.2 fvdl putbits(ou, 1);
585 1.1.1.2 fvdl putbits(ex, 2);
586 1.1.1.2 fvdl #undef putbits
587 1.1.1.2 fvdl }
588 1.1.1.2 fvdl return (error);
589 1.1.1.2 fvdl }
590 1.1.1.2 fvdl
591 1.1.1.2 fvdl selscan(p, ibits, obits, nfd, retval)
592 1.1.1.2 fvdl struct proc *p;
593 1.1.1.2 fvdl fd_set *ibits, *obits;
594 1.1.1.2 fvdl int nfd, *retval;
595 1.1.1.2 fvdl {
596 1.1.1.2 fvdl register struct filedesc *fdp = p->p_fd;
597 1.1.1.2 fvdl register int msk, i, j, fd;
598 1.1.1.2 fvdl register fd_mask bits;
599 1.1.1.2 fvdl struct file *fp;
600 1.1.1.2 fvdl int n = 0;
601 1.1.1.2 fvdl static int flag[3] = { FREAD, FWRITE, 0 };
602 1.1.1.2 fvdl
603 1.1.1.2 fvdl for (msk = 0; msk < 3; msk++) {
604 1.1.1.2 fvdl for (i = 0; i < nfd; i += NFDBITS) {
605 1.1.1.2 fvdl bits = ibits[msk].fds_bits[i/NFDBITS];
606 1.1.1.2 fvdl while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
607 1.1.1.2 fvdl bits &= ~(1 << j);
608 1.1.1.2 fvdl fp = fdp->fd_ofiles[fd];
609 1.1.1.2 fvdl if (fp == NULL)
610 1.1.1.2 fvdl return (EBADF);
611 1.1.1.2 fvdl if ((*fp->f_ops->fo_select)(fp, flag[msk], p)) {
612 1.1.1.2 fvdl FD_SET(fd, &obits[msk]);
613 1.1.1.2 fvdl n++;
614 1.1.1.2 fvdl }
615 1.1.1.2 fvdl }
616 1.1.1.2 fvdl }
617 1.1.1.2 fvdl }
618 1.1.1.2 fvdl *retval = n;
619 1.1.1.2 fvdl return (0);
620 1.1.1.2 fvdl }
621 1.1.1.2 fvdl
622 1.1.1.2 fvdl /*ARGSUSED*/
623 1.1.1.2 fvdl seltrue(dev, flag, p)
624 1.1.1.2 fvdl dev_t dev;
625 1.1.1.2 fvdl int flag;
626 1.1.1.2 fvdl struct proc *p;
627 1.1.1.2 fvdl {
628 1.1.1.2 fvdl
629 1.1.1.2 fvdl return (1);
630 1.1.1.2 fvdl }
631 1.1.1.2 fvdl
632 1.1.1.2 fvdl /*
633 1.1.1.2 fvdl * Record a select request.
634 1.1.1.2 fvdl */
635 1.1.1.2 fvdl void
636 1.1.1.2 fvdl selrecord(selector, sip)
637 1.1.1.2 fvdl struct proc *selector;
638 1.1.1.2 fvdl struct selinfo *sip;
639 1.1.1.2 fvdl {
640 1.1.1.2 fvdl struct proc *p;
641 1.1.1.2 fvdl pid_t mypid;
642 1.1.1.2 fvdl
643 1.1.1.2 fvdl mypid = selector->p_pid;
644 1.1.1.2 fvdl if (sip->si_pid == mypid)
645 1.1.1.2 fvdl return;
646 1.1.1.2 fvdl if (sip->si_pid && (p = pfind(sip->si_pid)) &&
647 1.1.1.2 fvdl p->p_wchan == (caddr_t)&selwait)
648 1.1.1.2 fvdl sip->si_flags |= SI_COLL;
649 1.1.1.2 fvdl else
650 1.1.1.2 fvdl sip->si_pid = mypid;
651 1.1.1.2 fvdl }
652 1.1.1.2 fvdl
653 1.1.1.2 fvdl /*
654 1.1.1.2 fvdl * Do a wakeup when a selectable event occurs.
655 1.1.1.2 fvdl */
656 1.1.1.2 fvdl void
657 1.1.1.2 fvdl selwakeup(sip)
658 1.1.1.2 fvdl register struct selinfo *sip;
659 1.1.1.2 fvdl {
660 1.1.1.2 fvdl register struct proc *p;
661 1.1.1.2 fvdl int s;
662 1.1.1.2 fvdl
663 1.1.1.2 fvdl if (sip->si_pid == 0)
664 1.1.1.2 fvdl return;
665 1.1.1.2 fvdl if (sip->si_flags & SI_COLL) {
666 1.1.1.2 fvdl nselcoll++;
667 1.1.1.2 fvdl sip->si_flags &= ~SI_COLL;
668 1.1.1.2 fvdl wakeup((caddr_t)&selwait);
669 1.1.1.2 fvdl }
670 1.1.1.2 fvdl p = pfind(sip->si_pid);
671 1.1.1.2 fvdl sip->si_pid = 0;
672 1.1.1.2 fvdl if (p != NULL) {
673 1.1.1.2 fvdl s = splhigh();
674 1.1.1.2 fvdl if (p->p_wchan == (caddr_t)&selwait) {
675 1.1.1.2 fvdl if (p->p_stat == SSLEEP)
676 1.1.1.2 fvdl setrunnable(p);
677 1.1.1.2 fvdl else
678 1.1.1.2 fvdl unsleep(p);
679 1.1.1.2 fvdl } else if (p->p_flag & P_SELECT)
680 1.1.1.2 fvdl p->p_flag &= ~P_SELECT;
681 1.1.1.2 fvdl splx(s);
682 1.1.1.2 fvdl }
683 1.1.1.2 fvdl }
684