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