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