sys_generic.c revision 1.37 1 1.37 thorpej /* $NetBSD: sys_generic.c,v 1.37 1998/06/25 21:17:17 thorpej 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.36 fvdl * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
41 1.15 cgd */
42 1.37 thorpej
43 1.37 thorpej #include "opt_ktrace.h"
44 1.15 cgd
45 1.15 cgd #include <sys/param.h>
46 1.15 cgd #include <sys/systm.h>
47 1.15 cgd #include <sys/filedesc.h>
48 1.15 cgd #include <sys/ioctl.h>
49 1.15 cgd #include <sys/file.h>
50 1.15 cgd #include <sys/proc.h>
51 1.15 cgd #include <sys/socketvar.h>
52 1.22 christos #include <sys/signalvar.h>
53 1.15 cgd #include <sys/uio.h>
54 1.15 cgd #include <sys/kernel.h>
55 1.15 cgd #include <sys/stat.h>
56 1.15 cgd #include <sys/malloc.h>
57 1.28 mycroft #include <sys/poll.h>
58 1.15 cgd #ifdef KTRACE
59 1.15 cgd #include <sys/ktrace.h>
60 1.15 cgd #endif
61 1.15 cgd
62 1.16 cgd #include <sys/mount.h>
63 1.16 cgd #include <sys/syscallargs.h>
64 1.22 christos
65 1.25 mycroft int selscan __P((struct proc *, fd_mask *, fd_mask *, int, register_t *));
66 1.28 mycroft int pollscan __P((struct proc *, struct pollfd *, int, register_t *));
67 1.22 christos
68 1.15 cgd /*
69 1.15 cgd * Read system call.
70 1.15 cgd */
71 1.15 cgd /* ARGSUSED */
72 1.22 christos int
73 1.21 mycroft sys_read(p, v, retval)
74 1.15 cgd struct proc *p;
75 1.20 thorpej void *v;
76 1.20 thorpej register_t *retval;
77 1.20 thorpej {
78 1.21 mycroft register struct sys_read_args /* {
79 1.16 cgd syscallarg(int) fd;
80 1.31 cgd syscallarg(void *) buf;
81 1.31 cgd syscallarg(size_t) nbyte;
82 1.20 thorpej } */ *uap = v;
83 1.34 mycroft int fd = SCARG(uap, fd);
84 1.15 cgd register struct file *fp;
85 1.15 cgd register struct filedesc *fdp = p->p_fd;
86 1.15 cgd struct uio auio;
87 1.15 cgd struct iovec aiov;
88 1.15 cgd long cnt, error = 0;
89 1.15 cgd #ifdef KTRACE
90 1.15 cgd struct iovec ktriov;
91 1.15 cgd #endif
92 1.15 cgd
93 1.34 mycroft if ((u_int)fd >= fdp->fd_nfiles ||
94 1.34 mycroft (fp = fdp->fd_ofiles[fd]) == NULL ||
95 1.15 cgd (fp->f_flag & FREAD) == 0)
96 1.15 cgd return (EBADF);
97 1.16 cgd aiov.iov_base = (caddr_t)SCARG(uap, buf);
98 1.16 cgd aiov.iov_len = SCARG(uap, nbyte);
99 1.15 cgd auio.uio_iov = &aiov;
100 1.15 cgd auio.uio_iovcnt = 1;
101 1.16 cgd auio.uio_resid = SCARG(uap, nbyte);
102 1.15 cgd auio.uio_rw = UIO_READ;
103 1.15 cgd auio.uio_segflg = UIO_USERSPACE;
104 1.15 cgd auio.uio_procp = p;
105 1.15 cgd if (auio.uio_resid < 0)
106 1.15 cgd return EINVAL;
107 1.15 cgd #ifdef KTRACE
108 1.15 cgd /*
109 1.15 cgd * if tracing, save a copy of iovec
110 1.15 cgd */
111 1.15 cgd if (KTRPOINT(p, KTR_GENIO))
112 1.15 cgd ktriov = aiov;
113 1.15 cgd #endif
114 1.16 cgd cnt = SCARG(uap, nbyte);
115 1.22 christos error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred);
116 1.22 christos if (error)
117 1.15 cgd if (auio.uio_resid != cnt && (error == ERESTART ||
118 1.15 cgd error == EINTR || error == EWOULDBLOCK))
119 1.15 cgd error = 0;
120 1.15 cgd cnt -= auio.uio_resid;
121 1.15 cgd #ifdef KTRACE
122 1.15 cgd if (KTRPOINT(p, KTR_GENIO) && error == 0)
123 1.34 mycroft ktrgenio(p->p_tracep, fd, UIO_READ, &ktriov, cnt, error);
124 1.15 cgd #endif
125 1.15 cgd *retval = cnt;
126 1.15 cgd return (error);
127 1.15 cgd }
128 1.15 cgd
129 1.15 cgd /*
130 1.15 cgd * Scatter read system call.
131 1.15 cgd */
132 1.22 christos int
133 1.21 mycroft sys_readv(p, v, retval)
134 1.15 cgd struct proc *p;
135 1.20 thorpej void *v;
136 1.20 thorpej register_t *retval;
137 1.20 thorpej {
138 1.21 mycroft register struct sys_readv_args /* {
139 1.16 cgd syscallarg(int) fd;
140 1.31 cgd syscallarg(const struct iovec *) iovp;
141 1.34 mycroft syscallarg(int) iovcnt;
142 1.20 thorpej } */ *uap = v;
143 1.34 mycroft int fd = SCARG(uap, fd);
144 1.34 mycroft int iovcnt = SCARG(uap, iovcnt);
145 1.15 cgd register struct file *fp;
146 1.15 cgd register struct filedesc *fdp = p->p_fd;
147 1.15 cgd struct uio auio;
148 1.15 cgd register struct iovec *iov;
149 1.15 cgd struct iovec *needfree;
150 1.15 cgd struct iovec aiov[UIO_SMALLIOV];
151 1.15 cgd long i, cnt, error = 0;
152 1.15 cgd u_int iovlen;
153 1.15 cgd #ifdef KTRACE
154 1.15 cgd struct iovec *ktriov = NULL;
155 1.15 cgd #endif
156 1.15 cgd
157 1.34 mycroft if ((u_int)fd >= fdp->fd_nfiles ||
158 1.34 mycroft (fp = fdp->fd_ofiles[fd]) == NULL ||
159 1.15 cgd (fp->f_flag & FREAD) == 0)
160 1.15 cgd return (EBADF);
161 1.15 cgd /* note: can't use iovlen until iovcnt is validated */
162 1.34 mycroft iovlen = iovcnt * sizeof (struct iovec);
163 1.34 mycroft if ((u_int)iovcnt > UIO_SMALLIOV) {
164 1.34 mycroft if ((u_int)iovcnt > UIO_MAXIOV)
165 1.15 cgd return (EINVAL);
166 1.15 cgd MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
167 1.15 cgd needfree = iov;
168 1.15 cgd } else {
169 1.15 cgd iov = aiov;
170 1.15 cgd needfree = NULL;
171 1.15 cgd }
172 1.15 cgd auio.uio_iov = iov;
173 1.34 mycroft auio.uio_iovcnt = iovcnt;
174 1.15 cgd auio.uio_rw = UIO_READ;
175 1.15 cgd auio.uio_segflg = UIO_USERSPACE;
176 1.15 cgd auio.uio_procp = p;
177 1.31 cgd error = copyin(SCARG(uap, iovp), iov, iovlen);
178 1.22 christos if (error)
179 1.15 cgd goto done;
180 1.15 cgd auio.uio_resid = 0;
181 1.34 mycroft for (i = 0; i < iovcnt; i++) {
182 1.15 cgd auio.uio_resid += iov->iov_len;
183 1.15 cgd if (auio.uio_resid < 0) {
184 1.15 cgd error = EINVAL;
185 1.15 cgd goto done;
186 1.15 cgd }
187 1.15 cgd iov++;
188 1.15 cgd }
189 1.15 cgd #ifdef KTRACE
190 1.15 cgd /*
191 1.15 cgd * if tracing, save a copy of iovec
192 1.15 cgd */
193 1.15 cgd if (KTRPOINT(p, KTR_GENIO)) {
194 1.15 cgd MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
195 1.15 cgd bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
196 1.15 cgd }
197 1.15 cgd #endif
198 1.15 cgd cnt = auio.uio_resid;
199 1.22 christos error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred);
200 1.22 christos if (error)
201 1.15 cgd if (auio.uio_resid != cnt && (error == ERESTART ||
202 1.15 cgd error == EINTR || error == EWOULDBLOCK))
203 1.15 cgd error = 0;
204 1.15 cgd cnt -= auio.uio_resid;
205 1.15 cgd #ifdef KTRACE
206 1.34 mycroft if (KTRPOINT(p, KTR_GENIO))
207 1.34 mycroft if (error == 0) {
208 1.34 mycroft ktrgenio(p->p_tracep, fd, UIO_READ, ktriov, cnt,
209 1.34 mycroft error);
210 1.15 cgd FREE(ktriov, M_TEMP);
211 1.15 cgd }
212 1.15 cgd #endif
213 1.15 cgd *retval = cnt;
214 1.15 cgd done:
215 1.15 cgd if (needfree)
216 1.15 cgd FREE(needfree, M_IOV);
217 1.15 cgd return (error);
218 1.15 cgd }
219 1.15 cgd
220 1.15 cgd /*
221 1.15 cgd * Write system call
222 1.15 cgd */
223 1.22 christos int
224 1.21 mycroft sys_write(p, v, retval)
225 1.15 cgd struct proc *p;
226 1.20 thorpej void *v;
227 1.20 thorpej register_t *retval;
228 1.20 thorpej {
229 1.21 mycroft register struct sys_write_args /* {
230 1.16 cgd syscallarg(int) fd;
231 1.31 cgd syscallarg(const void *) buf;
232 1.31 cgd syscallarg(size_t) nbyte;
233 1.20 thorpej } */ *uap = v;
234 1.34 mycroft int fd = SCARG(uap, fd);
235 1.15 cgd register struct file *fp;
236 1.15 cgd register struct filedesc *fdp = p->p_fd;
237 1.15 cgd struct uio auio;
238 1.15 cgd struct iovec aiov;
239 1.15 cgd long cnt, error = 0;
240 1.15 cgd #ifdef KTRACE
241 1.15 cgd struct iovec ktriov;
242 1.15 cgd #endif
243 1.15 cgd
244 1.34 mycroft if ((u_int)fd >= fdp->fd_nfiles ||
245 1.34 mycroft (fp = fdp->fd_ofiles[fd]) == NULL ||
246 1.15 cgd (fp->f_flag & FWRITE) == 0)
247 1.15 cgd return (EBADF);
248 1.31 cgd aiov.iov_base = (char *)SCARG(uap, buf); /* XXX kills const */
249 1.16 cgd aiov.iov_len = SCARG(uap, nbyte);
250 1.15 cgd auio.uio_iov = &aiov;
251 1.15 cgd auio.uio_iovcnt = 1;
252 1.16 cgd auio.uio_resid = SCARG(uap, nbyte);
253 1.15 cgd auio.uio_rw = UIO_WRITE;
254 1.15 cgd auio.uio_segflg = UIO_USERSPACE;
255 1.15 cgd auio.uio_procp = p;
256 1.15 cgd if (auio.uio_resid < 0)
257 1.15 cgd return EINVAL;
258 1.15 cgd #ifdef KTRACE
259 1.15 cgd /*
260 1.15 cgd * if tracing, save a copy of iovec
261 1.15 cgd */
262 1.15 cgd if (KTRPOINT(p, KTR_GENIO))
263 1.15 cgd ktriov = aiov;
264 1.15 cgd #endif
265 1.16 cgd cnt = SCARG(uap, nbyte);
266 1.22 christos error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred);
267 1.22 christos if (error) {
268 1.15 cgd if (auio.uio_resid != cnt && (error == ERESTART ||
269 1.15 cgd error == EINTR || error == EWOULDBLOCK))
270 1.15 cgd error = 0;
271 1.15 cgd if (error == EPIPE)
272 1.15 cgd psignal(p, SIGPIPE);
273 1.15 cgd }
274 1.15 cgd cnt -= auio.uio_resid;
275 1.15 cgd #ifdef KTRACE
276 1.15 cgd if (KTRPOINT(p, KTR_GENIO) && error == 0)
277 1.34 mycroft ktrgenio(p->p_tracep, fd, UIO_WRITE, &ktriov, cnt, error);
278 1.15 cgd #endif
279 1.15 cgd *retval = cnt;
280 1.15 cgd return (error);
281 1.15 cgd }
282 1.15 cgd
283 1.15 cgd /*
284 1.15 cgd * Gather write system call
285 1.15 cgd */
286 1.22 christos int
287 1.21 mycroft sys_writev(p, v, retval)
288 1.15 cgd struct proc *p;
289 1.20 thorpej void *v;
290 1.20 thorpej register_t *retval;
291 1.20 thorpej {
292 1.21 mycroft register struct sys_writev_args /* {
293 1.16 cgd syscallarg(int) fd;
294 1.31 cgd syscallarg(const struct iovec *) iovp;
295 1.16 cgd syscallarg(u_int) iovcnt;
296 1.20 thorpej } */ *uap = v;
297 1.34 mycroft int fd = SCARG(uap, fd);
298 1.34 mycroft int iovcnt = SCARG(uap, iovcnt);
299 1.15 cgd register struct file *fp;
300 1.15 cgd register struct filedesc *fdp = p->p_fd;
301 1.15 cgd struct uio auio;
302 1.15 cgd register struct iovec *iov;
303 1.15 cgd struct iovec *needfree;
304 1.15 cgd struct iovec aiov[UIO_SMALLIOV];
305 1.15 cgd long i, cnt, error = 0;
306 1.15 cgd u_int iovlen;
307 1.15 cgd #ifdef KTRACE
308 1.15 cgd struct iovec *ktriov = NULL;
309 1.15 cgd #endif
310 1.15 cgd
311 1.34 mycroft if ((u_int)fd >= fdp->fd_nfiles ||
312 1.34 mycroft (fp = fdp->fd_ofiles[fd]) == NULL ||
313 1.15 cgd (fp->f_flag & FWRITE) == 0)
314 1.15 cgd return (EBADF);
315 1.15 cgd /* note: can't use iovlen until iovcnt is validated */
316 1.34 mycroft iovlen = iovcnt * sizeof (struct iovec);
317 1.34 mycroft if ((u_int)iovcnt > UIO_SMALLIOV) {
318 1.34 mycroft if ((u_int)iovcnt > UIO_MAXIOV)
319 1.15 cgd return (EINVAL);
320 1.15 cgd MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
321 1.15 cgd needfree = iov;
322 1.15 cgd } else {
323 1.15 cgd iov = aiov;
324 1.15 cgd needfree = NULL;
325 1.15 cgd }
326 1.15 cgd auio.uio_iov = iov;
327 1.34 mycroft auio.uio_iovcnt = iovcnt;
328 1.15 cgd auio.uio_rw = UIO_WRITE;
329 1.15 cgd auio.uio_segflg = UIO_USERSPACE;
330 1.15 cgd auio.uio_procp = p;
331 1.31 cgd error = copyin(SCARG(uap, iovp), iov, iovlen);
332 1.22 christos if (error)
333 1.15 cgd goto done;
334 1.15 cgd auio.uio_resid = 0;
335 1.34 mycroft for (i = 0; i < iovcnt; i++) {
336 1.15 cgd auio.uio_resid += iov->iov_len;
337 1.15 cgd if (auio.uio_resid < 0) {
338 1.15 cgd error = EINVAL;
339 1.15 cgd goto done;
340 1.15 cgd }
341 1.15 cgd iov++;
342 1.15 cgd }
343 1.15 cgd #ifdef KTRACE
344 1.15 cgd /*
345 1.15 cgd * if tracing, save a copy of iovec
346 1.15 cgd */
347 1.15 cgd if (KTRPOINT(p, KTR_GENIO)) {
348 1.15 cgd MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
349 1.15 cgd bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
350 1.15 cgd }
351 1.15 cgd #endif
352 1.15 cgd cnt = auio.uio_resid;
353 1.22 christos error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred);
354 1.22 christos if (error) {
355 1.15 cgd if (auio.uio_resid != cnt && (error == ERESTART ||
356 1.15 cgd error == EINTR || error == EWOULDBLOCK))
357 1.15 cgd error = 0;
358 1.15 cgd if (error == EPIPE)
359 1.15 cgd psignal(p, SIGPIPE);
360 1.15 cgd }
361 1.15 cgd cnt -= auio.uio_resid;
362 1.15 cgd #ifdef KTRACE
363 1.34 mycroft if (KTRPOINT(p, KTR_GENIO))
364 1.34 mycroft if (error == 0) {
365 1.34 mycroft ktrgenio(p->p_tracep, fd, UIO_WRITE, ktriov, cnt,
366 1.34 mycroft error);
367 1.15 cgd FREE(ktriov, M_TEMP);
368 1.15 cgd }
369 1.15 cgd #endif
370 1.15 cgd *retval = cnt;
371 1.15 cgd done:
372 1.15 cgd if (needfree)
373 1.15 cgd FREE(needfree, M_IOV);
374 1.15 cgd return (error);
375 1.15 cgd }
376 1.15 cgd
377 1.15 cgd /*
378 1.15 cgd * Ioctl system call
379 1.15 cgd */
380 1.15 cgd /* ARGSUSED */
381 1.22 christos int
382 1.21 mycroft sys_ioctl(p, v, retval)
383 1.15 cgd struct proc *p;
384 1.20 thorpej void *v;
385 1.20 thorpej register_t *retval;
386 1.20 thorpej {
387 1.21 mycroft register struct sys_ioctl_args /* {
388 1.16 cgd syscallarg(int) fd;
389 1.17 cgd syscallarg(u_long) com;
390 1.16 cgd syscallarg(caddr_t) data;
391 1.20 thorpej } */ *uap = v;
392 1.15 cgd register struct file *fp;
393 1.15 cgd register struct filedesc *fdp;
394 1.17 cgd register u_long com;
395 1.17 cgd register int error;
396 1.15 cgd register u_int size;
397 1.15 cgd caddr_t data, memp;
398 1.15 cgd int tmp;
399 1.15 cgd #define STK_PARAMS 128
400 1.15 cgd char stkbuf[STK_PARAMS];
401 1.15 cgd
402 1.15 cgd fdp = p->p_fd;
403 1.16 cgd if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
404 1.16 cgd (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
405 1.15 cgd return (EBADF);
406 1.15 cgd
407 1.15 cgd if ((fp->f_flag & (FREAD | FWRITE)) == 0)
408 1.15 cgd return (EBADF);
409 1.15 cgd
410 1.16 cgd switch (com = SCARG(uap, com)) {
411 1.15 cgd case FIONCLEX:
412 1.16 cgd fdp->fd_ofileflags[SCARG(uap, fd)] &= ~UF_EXCLOSE;
413 1.15 cgd return (0);
414 1.15 cgd case FIOCLEX:
415 1.16 cgd fdp->fd_ofileflags[SCARG(uap, fd)] |= UF_EXCLOSE;
416 1.15 cgd return (0);
417 1.15 cgd }
418 1.15 cgd
419 1.15 cgd /*
420 1.15 cgd * Interpret high order word to find amount of data to be
421 1.15 cgd * copied to/from the user's address space.
422 1.15 cgd */
423 1.15 cgd size = IOCPARM_LEN(com);
424 1.15 cgd if (size > IOCPARM_MAX)
425 1.15 cgd return (ENOTTY);
426 1.15 cgd memp = NULL;
427 1.15 cgd if (size > sizeof (stkbuf)) {
428 1.15 cgd memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
429 1.15 cgd data = memp;
430 1.15 cgd } else
431 1.15 cgd data = stkbuf;
432 1.15 cgd if (com&IOC_IN) {
433 1.15 cgd if (size) {
434 1.31 cgd error = copyin(SCARG(uap, data), data, size);
435 1.15 cgd if (error) {
436 1.15 cgd if (memp)
437 1.15 cgd free(memp, M_IOCTLOPS);
438 1.15 cgd return (error);
439 1.15 cgd }
440 1.15 cgd } else
441 1.16 cgd *(caddr_t *)data = SCARG(uap, data);
442 1.15 cgd } else if ((com&IOC_OUT) && size)
443 1.15 cgd /*
444 1.15 cgd * Zero the buffer so the user always
445 1.15 cgd * gets back something deterministic.
446 1.15 cgd */
447 1.15 cgd bzero(data, size);
448 1.15 cgd else if (com&IOC_VOID)
449 1.16 cgd *(caddr_t *)data = SCARG(uap, data);
450 1.15 cgd
451 1.15 cgd switch (com) {
452 1.15 cgd
453 1.15 cgd case FIONBIO:
454 1.22 christos if ((tmp = *(int *)data) != 0)
455 1.15 cgd fp->f_flag |= FNONBLOCK;
456 1.15 cgd else
457 1.15 cgd fp->f_flag &= ~FNONBLOCK;
458 1.15 cgd error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
459 1.15 cgd break;
460 1.15 cgd
461 1.15 cgd case FIOASYNC:
462 1.22 christos if ((tmp = *(int *)data) != 0)
463 1.15 cgd fp->f_flag |= FASYNC;
464 1.15 cgd else
465 1.15 cgd fp->f_flag &= ~FASYNC;
466 1.15 cgd error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
467 1.15 cgd break;
468 1.15 cgd
469 1.15 cgd case FIOSETOWN:
470 1.15 cgd tmp = *(int *)data;
471 1.15 cgd if (fp->f_type == DTYPE_SOCKET) {
472 1.15 cgd ((struct socket *)fp->f_data)->so_pgid = tmp;
473 1.15 cgd error = 0;
474 1.15 cgd break;
475 1.15 cgd }
476 1.15 cgd if (tmp <= 0) {
477 1.15 cgd tmp = -tmp;
478 1.15 cgd } else {
479 1.15 cgd struct proc *p1 = pfind(tmp);
480 1.15 cgd if (p1 == 0) {
481 1.15 cgd error = ESRCH;
482 1.15 cgd break;
483 1.15 cgd }
484 1.15 cgd tmp = p1->p_pgrp->pg_id;
485 1.15 cgd }
486 1.15 cgd error = (*fp->f_ops->fo_ioctl)
487 1.24 cgd (fp, TIOCSPGRP, (caddr_t)&tmp, p);
488 1.15 cgd break;
489 1.15 cgd
490 1.15 cgd case FIOGETOWN:
491 1.15 cgd if (fp->f_type == DTYPE_SOCKET) {
492 1.15 cgd error = 0;
493 1.15 cgd *(int *)data = ((struct socket *)fp->f_data)->so_pgid;
494 1.15 cgd break;
495 1.15 cgd }
496 1.17 cgd error = (*fp->f_ops->fo_ioctl)(fp, TIOCGPGRP, data, p);
497 1.15 cgd *(int *)data = -*(int *)data;
498 1.15 cgd break;
499 1.15 cgd
500 1.15 cgd default:
501 1.15 cgd error = (*fp->f_ops->fo_ioctl)(fp, com, data, p);
502 1.15 cgd /*
503 1.15 cgd * Copy any data to user, size was
504 1.15 cgd * already set and checked above.
505 1.15 cgd */
506 1.15 cgd if (error == 0 && (com&IOC_OUT) && size)
507 1.31 cgd error = copyout(data, SCARG(uap, data), size);
508 1.15 cgd break;
509 1.15 cgd }
510 1.15 cgd if (memp)
511 1.15 cgd free(memp, M_IOCTLOPS);
512 1.15 cgd return (error);
513 1.15 cgd }
514 1.15 cgd
515 1.15 cgd int selwait, nselcoll;
516 1.15 cgd
517 1.15 cgd /*
518 1.15 cgd * Select system call.
519 1.15 cgd */
520 1.22 christos int
521 1.21 mycroft sys_select(p, v, retval)
522 1.15 cgd register struct proc *p;
523 1.20 thorpej void *v;
524 1.20 thorpej register_t *retval;
525 1.20 thorpej {
526 1.21 mycroft register struct sys_select_args /* {
527 1.35 thorpej syscallarg(int) nd;
528 1.16 cgd syscallarg(fd_set *) in;
529 1.16 cgd syscallarg(fd_set *) ou;
530 1.16 cgd syscallarg(fd_set *) ex;
531 1.16 cgd syscallarg(struct timeval *) tv;
532 1.20 thorpej } */ *uap = v;
533 1.25 mycroft caddr_t bits;
534 1.26 cgd char smallbits[howmany(FD_SETSIZE, NFDBITS) * sizeof(fd_mask) * 6];
535 1.15 cgd struct timeval atv;
536 1.15 cgd int s, ncoll, error = 0, timo;
537 1.27 mycroft size_t ni;
538 1.15 cgd
539 1.35 thorpej if (SCARG(uap, nd) < 0)
540 1.35 thorpej return (EINVAL);
541 1.16 cgd if (SCARG(uap, nd) > p->p_fd->fd_nfiles) {
542 1.16 cgd /* forgiving; slightly wrong */
543 1.16 cgd SCARG(uap, nd) = p->p_fd->fd_nfiles;
544 1.16 cgd }
545 1.16 cgd ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask);
546 1.27 mycroft if (ni * 6 > sizeof(smallbits))
547 1.25 mycroft bits = malloc(ni * 6, M_TEMP, M_WAITOK);
548 1.25 mycroft else
549 1.26 cgd bits = smallbits;
550 1.15 cgd
551 1.15 cgd #define getbits(name, x) \
552 1.25 mycroft if (SCARG(uap, name)) { \
553 1.31 cgd error = copyin(SCARG(uap, name), bits + ni * x, ni); \
554 1.25 mycroft if (error) \
555 1.25 mycroft goto done; \
556 1.25 mycroft } else \
557 1.25 mycroft bzero(bits + ni * x, ni);
558 1.15 cgd getbits(in, 0);
559 1.15 cgd getbits(ou, 1);
560 1.15 cgd getbits(ex, 2);
561 1.15 cgd #undef getbits
562 1.15 cgd
563 1.16 cgd if (SCARG(uap, tv)) {
564 1.31 cgd error = copyin(SCARG(uap, tv), (caddr_t)&atv,
565 1.15 cgd sizeof (atv));
566 1.15 cgd if (error)
567 1.15 cgd goto done;
568 1.15 cgd if (itimerfix(&atv)) {
569 1.15 cgd error = EINVAL;
570 1.15 cgd goto done;
571 1.15 cgd }
572 1.15 cgd s = splclock();
573 1.19 mycroft timeradd(&atv, &time, &atv);
574 1.15 cgd timo = hzto(&atv);
575 1.15 cgd /*
576 1.15 cgd * Avoid inadvertently sleeping forever.
577 1.15 cgd */
578 1.15 cgd if (timo == 0)
579 1.15 cgd timo = 1;
580 1.15 cgd splx(s);
581 1.15 cgd } else
582 1.15 cgd timo = 0;
583 1.15 cgd retry:
584 1.15 cgd ncoll = nselcoll;
585 1.15 cgd p->p_flag |= P_SELECT;
586 1.25 mycroft error = selscan(p, (fd_mask *)(bits + ni * 0),
587 1.25 mycroft (fd_mask *)(bits + ni * 3), SCARG(uap, nd), retval);
588 1.15 cgd if (error || *retval)
589 1.15 cgd goto done;
590 1.15 cgd s = splhigh();
591 1.27 mycroft if (timo && timercmp(&time, &atv, >=)) {
592 1.15 cgd splx(s);
593 1.15 cgd goto done;
594 1.15 cgd }
595 1.15 cgd if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
596 1.15 cgd splx(s);
597 1.15 cgd goto retry;
598 1.15 cgd }
599 1.15 cgd p->p_flag &= ~P_SELECT;
600 1.15 cgd error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
601 1.15 cgd splx(s);
602 1.15 cgd if (error == 0)
603 1.15 cgd goto retry;
604 1.15 cgd done:
605 1.15 cgd p->p_flag &= ~P_SELECT;
606 1.15 cgd /* select is not restarted after signals... */
607 1.15 cgd if (error == ERESTART)
608 1.15 cgd error = EINTR;
609 1.15 cgd if (error == EWOULDBLOCK)
610 1.15 cgd error = 0;
611 1.27 mycroft if (error == 0) {
612 1.15 cgd #define putbits(name, x) \
613 1.27 mycroft if (SCARG(uap, name)) { \
614 1.31 cgd error = copyout(bits + ni * x, SCARG(uap, name), ni); \
615 1.27 mycroft if (error) \
616 1.27 mycroft goto out; \
617 1.27 mycroft }
618 1.25 mycroft putbits(in, 3);
619 1.25 mycroft putbits(ou, 4);
620 1.25 mycroft putbits(ex, 5);
621 1.15 cgd #undef putbits
622 1.15 cgd }
623 1.27 mycroft out:
624 1.27 mycroft if (ni * 6 > sizeof(smallbits))
625 1.25 mycroft free(bits, M_TEMP);
626 1.15 cgd return (error);
627 1.15 cgd }
628 1.15 cgd
629 1.22 christos int
630 1.25 mycroft selscan(p, ibitp, obitp, nfd, retval)
631 1.15 cgd struct proc *p;
632 1.25 mycroft fd_mask *ibitp, *obitp;
633 1.16 cgd int nfd;
634 1.16 cgd register_t *retval;
635 1.15 cgd {
636 1.15 cgd register struct filedesc *fdp = p->p_fd;
637 1.15 cgd register int msk, i, j, fd;
638 1.25 mycroft register fd_mask ibits, obits;
639 1.15 cgd struct file *fp;
640 1.15 cgd int n = 0;
641 1.28 mycroft static int flag[3] = { POLLRDNORM | POLLHUP | POLLERR,
642 1.28 mycroft POLLWRNORM | POLLHUP | POLLERR,
643 1.28 mycroft POLLRDBAND };
644 1.15 cgd
645 1.15 cgd for (msk = 0; msk < 3; msk++) {
646 1.15 cgd for (i = 0; i < nfd; i += NFDBITS) {
647 1.25 mycroft ibits = *ibitp++;
648 1.25 mycroft obits = 0;
649 1.25 mycroft while ((j = ffs(ibits)) && (fd = i + --j) < nfd) {
650 1.25 mycroft ibits &= ~(1 << j);
651 1.15 cgd fp = fdp->fd_ofiles[fd];
652 1.15 cgd if (fp == NULL)
653 1.15 cgd return (EBADF);
654 1.28 mycroft if ((*fp->f_ops->fo_poll)(fp, flag[msk], p)) {
655 1.25 mycroft obits |= (1 << j);
656 1.15 cgd n++;
657 1.15 cgd }
658 1.15 cgd }
659 1.25 mycroft *obitp++ = obits;
660 1.15 cgd }
661 1.15 cgd }
662 1.15 cgd *retval = n;
663 1.15 cgd return (0);
664 1.15 cgd }
665 1.15 cgd
666 1.28 mycroft /*
667 1.28 mycroft * Poll system call.
668 1.28 mycroft */
669 1.28 mycroft int
670 1.28 mycroft sys_poll(p, v, retval)
671 1.28 mycroft register struct proc *p;
672 1.28 mycroft void *v;
673 1.28 mycroft register_t *retval;
674 1.28 mycroft {
675 1.28 mycroft register struct sys_poll_args /* {
676 1.28 mycroft syscallarg(struct pollfd *) fds;
677 1.29 mycroft syscallarg(u_int) nfds;
678 1.28 mycroft syscallarg(int) timeout;
679 1.28 mycroft } */ *uap = v;
680 1.28 mycroft caddr_t bits;
681 1.28 mycroft char smallbits[32 * sizeof(struct pollfd)];
682 1.28 mycroft struct timeval atv;
683 1.28 mycroft int s, ncoll, error = 0, timo;
684 1.28 mycroft size_t ni;
685 1.28 mycroft
686 1.28 mycroft if (SCARG(uap, nfds) > p->p_fd->fd_nfiles) {
687 1.28 mycroft /* forgiving; slightly wrong */
688 1.28 mycroft SCARG(uap, nfds) = p->p_fd->fd_nfiles;
689 1.28 mycroft }
690 1.28 mycroft ni = SCARG(uap, nfds) * sizeof(struct pollfd);
691 1.28 mycroft if (ni > sizeof(smallbits))
692 1.28 mycroft bits = malloc(ni, M_TEMP, M_WAITOK);
693 1.28 mycroft else
694 1.28 mycroft bits = smallbits;
695 1.28 mycroft
696 1.31 cgd error = copyin(SCARG(uap, fds), bits, ni);
697 1.28 mycroft if (error)
698 1.28 mycroft goto done;
699 1.28 mycroft
700 1.30 mycroft if (SCARG(uap, timeout) != INFTIM) {
701 1.28 mycroft atv.tv_sec = SCARG(uap, timeout) / 1000;
702 1.28 mycroft atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
703 1.28 mycroft if (itimerfix(&atv)) {
704 1.28 mycroft error = EINVAL;
705 1.28 mycroft goto done;
706 1.28 mycroft }
707 1.28 mycroft s = splclock();
708 1.28 mycroft timeradd(&atv, &time, &atv);
709 1.28 mycroft timo = hzto(&atv);
710 1.28 mycroft /*
711 1.28 mycroft * Avoid inadvertently sleeping forever.
712 1.28 mycroft */
713 1.28 mycroft if (timo == 0)
714 1.28 mycroft timo = 1;
715 1.28 mycroft splx(s);
716 1.28 mycroft } else
717 1.28 mycroft timo = 0;
718 1.28 mycroft retry:
719 1.28 mycroft ncoll = nselcoll;
720 1.28 mycroft p->p_flag |= P_SELECT;
721 1.28 mycroft error = pollscan(p, (struct pollfd *)bits, SCARG(uap, nfds), retval);
722 1.28 mycroft if (error || *retval)
723 1.28 mycroft goto done;
724 1.28 mycroft s = splhigh();
725 1.28 mycroft if (timo && timercmp(&time, &atv, >=)) {
726 1.28 mycroft splx(s);
727 1.28 mycroft goto done;
728 1.28 mycroft }
729 1.28 mycroft if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
730 1.28 mycroft splx(s);
731 1.28 mycroft goto retry;
732 1.28 mycroft }
733 1.28 mycroft p->p_flag &= ~P_SELECT;
734 1.28 mycroft error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
735 1.28 mycroft splx(s);
736 1.28 mycroft if (error == 0)
737 1.28 mycroft goto retry;
738 1.28 mycroft done:
739 1.28 mycroft p->p_flag &= ~P_SELECT;
740 1.28 mycroft /* poll is not restarted after signals... */
741 1.28 mycroft if (error == ERESTART)
742 1.28 mycroft error = EINTR;
743 1.28 mycroft if (error == EWOULDBLOCK)
744 1.28 mycroft error = 0;
745 1.28 mycroft if (error == 0) {
746 1.31 cgd error = copyout(bits, SCARG(uap, fds), ni);
747 1.28 mycroft if (error)
748 1.28 mycroft goto out;
749 1.28 mycroft }
750 1.28 mycroft out:
751 1.28 mycroft if (ni > sizeof(smallbits))
752 1.28 mycroft free(bits, M_TEMP);
753 1.28 mycroft return (error);
754 1.28 mycroft }
755 1.28 mycroft
756 1.28 mycroft int
757 1.28 mycroft pollscan(p, fds, nfd, retval)
758 1.28 mycroft struct proc *p;
759 1.28 mycroft struct pollfd *fds;
760 1.28 mycroft int nfd;
761 1.28 mycroft register_t *retval;
762 1.28 mycroft {
763 1.28 mycroft register struct filedesc *fdp = p->p_fd;
764 1.28 mycroft int i;
765 1.28 mycroft struct file *fp;
766 1.28 mycroft int n = 0;
767 1.28 mycroft
768 1.28 mycroft for (i = 0; i < nfd; i++, fds++) {
769 1.33 mrg if ((u_int)fds->fd >= fdp->fd_nfiles) {
770 1.28 mycroft fds->revents = POLLNVAL;
771 1.28 mycroft n++;
772 1.28 mycroft } else {
773 1.32 mrg fp = fdp->fd_ofiles[fds->fd];
774 1.32 mrg if (fp == 0) {
775 1.32 mrg fds->revents = POLLNVAL;
776 1.28 mycroft n++;
777 1.32 mrg } else {
778 1.32 mrg fds->revents = (*fp->f_ops->fo_poll)(fp,
779 1.32 mrg fds->events | POLLERR | POLLHUP, p);
780 1.32 mrg if (fds->revents != 0)
781 1.32 mrg n++;
782 1.32 mrg }
783 1.28 mycroft }
784 1.28 mycroft }
785 1.28 mycroft *retval = n;
786 1.28 mycroft return (0);
787 1.28 mycroft }
788 1.28 mycroft
789 1.15 cgd /*ARGSUSED*/
790 1.22 christos int
791 1.28 mycroft seltrue(dev, events, p)
792 1.15 cgd dev_t dev;
793 1.28 mycroft int events;
794 1.15 cgd struct proc *p;
795 1.15 cgd {
796 1.15 cgd
797 1.28 mycroft return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
798 1.15 cgd }
799 1.15 cgd
800 1.15 cgd /*
801 1.15 cgd * Record a select request.
802 1.15 cgd */
803 1.15 cgd void
804 1.15 cgd selrecord(selector, sip)
805 1.15 cgd struct proc *selector;
806 1.15 cgd struct selinfo *sip;
807 1.15 cgd {
808 1.15 cgd struct proc *p;
809 1.15 cgd pid_t mypid;
810 1.15 cgd
811 1.15 cgd mypid = selector->p_pid;
812 1.15 cgd if (sip->si_pid == mypid)
813 1.15 cgd return;
814 1.15 cgd if (sip->si_pid && (p = pfind(sip->si_pid)) &&
815 1.15 cgd p->p_wchan == (caddr_t)&selwait)
816 1.15 cgd sip->si_flags |= SI_COLL;
817 1.15 cgd else
818 1.15 cgd sip->si_pid = mypid;
819 1.15 cgd }
820 1.15 cgd
821 1.15 cgd /*
822 1.15 cgd * Do a wakeup when a selectable event occurs.
823 1.15 cgd */
824 1.15 cgd void
825 1.15 cgd selwakeup(sip)
826 1.15 cgd register struct selinfo *sip;
827 1.15 cgd {
828 1.15 cgd register struct proc *p;
829 1.15 cgd int s;
830 1.15 cgd
831 1.15 cgd if (sip->si_pid == 0)
832 1.15 cgd return;
833 1.15 cgd if (sip->si_flags & SI_COLL) {
834 1.15 cgd nselcoll++;
835 1.15 cgd sip->si_flags &= ~SI_COLL;
836 1.15 cgd wakeup((caddr_t)&selwait);
837 1.15 cgd }
838 1.15 cgd p = pfind(sip->si_pid);
839 1.15 cgd sip->si_pid = 0;
840 1.15 cgd if (p != NULL) {
841 1.15 cgd s = splhigh();
842 1.15 cgd if (p->p_wchan == (caddr_t)&selwait) {
843 1.15 cgd if (p->p_stat == SSLEEP)
844 1.15 cgd setrunnable(p);
845 1.15 cgd else
846 1.15 cgd unsleep(p);
847 1.15 cgd } else if (p->p_flag & P_SELECT)
848 1.15 cgd p->p_flag &= ~P_SELECT;
849 1.15 cgd splx(s);
850 1.15 cgd }
851 1.15 cgd }
852