1 //==========================================================================
3 // sys/kern/sys_generic.c
7 //==========================================================================
8 //####BSDCOPYRIGHTBEGIN####
10 // -------------------------------------------
12 // Portions of this software may have been derived from OpenBSD or other sources,
13 // and are covered by the appropriate copyright disclaimers included herein.
15 // -------------------------------------------
17 //####BSDCOPYRIGHTEND####
18 //==========================================================================
19 //#####DESCRIPTIONBEGIN####
22 // Contributors: gthomas
28 //####DESCRIPTIONEND####
30 //==========================================================================
33 /* $OpenBSD: sys_generic.c,v 1.22 1999/11/29 22:02:14 deraadt Exp $ */
34 /* $NetBSD: sys_generic.c,v 1.24 1996/03/29 00:25:32 cgd Exp $ */
37 * Copyright (c) 1996 Theo de Raadt
38 * Copyright (c) 1982, 1986, 1989, 1993
39 * The Regents of the University of California. All rights reserved.
40 * (c) UNIX System Laboratories, Inc.
41 * All or some portions of this file are derived from material licensed
42 * to the University of California by American Telephone and Telegraph
43 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
44 * the permission of UNIX System Laboratories, Inc.
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. All advertising materials mentioning features or use of this software
55 * must display the following acknowledgement:
56 * This product includes software developed by the University of
57 * California, Berkeley and its contributors.
58 * 4. Neither the name of the University nor the names of its contributors
59 * may be used to endorse or promote products derived from this software
60 * without specific prior written permission.
62 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
63 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
66 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74 * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94
77 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/filedesc.h>
82 #include <sys/ioctl.h>
84 #include <cyg/io/file.h>
88 #include <sys/resourcevar.h>
90 #include <sys/socketvar.h>
92 #include <sys/signalvar.h>
93 #include <sys/kernel.h>
97 #include <sys/malloc.h>
102 #include <sys/ktrace.h>
106 #include <sys/mount.h>
108 #include <sys/syscallargs.h>
111 int selscan __P((struct proc *, fd_set *, fd_set *, int, register_t *));
112 int seltrue __P((dev_t, int, struct proc *));
113 void pollscan __P((struct proc *, struct pollfd *, int, register_t *));
121 sys_read(struct sys_read_args *uap, register_t *retval)
125 sys_read(p, v, retval)
132 register struct sys_read_args /* {
134 syscallarg(void *) buf;
135 syscallarg(size_t) nbyte;
137 register struct filedesc *fdp = p->p_fd;
148 if (getfp((u_int)SCARG(uap, fd), &fp) ||
150 if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
151 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
153 (fp->f_flag & FREAD) == 0)
155 /* Don't allow nbyte to be larger than max return val */
156 if (SCARG(uap, nbyte) > SSIZE_MAX)
158 aiov.iov_base = (caddr_t)SCARG(uap, buf);
159 aiov.iov_len = SCARG(uap, nbyte);
160 auio.uio_iov = &aiov;
162 auio.uio_resid = SCARG(uap, nbyte);
163 auio.uio_rw = UIO_READ;
164 auio.uio_segflg = UIO_USERSPACE;
170 * if tracing, save a copy of iovec
172 if (KTRPOINT(p, KTR_GENIO))
175 cnt = SCARG(uap, nbyte);
177 error = (*fp->f_ops->fo_read)(fp, &auio);
179 error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred);
183 if (auio.uio_resid != cnt && (
185 if (auio.uio_resid != cnt && (error == ERESTART ||
187 error == EINTR || error == EWOULDBLOCK))
189 cnt -= auio.uio_resid;
191 if (KTRPOINT(p, KTR_GENIO) && error == 0)
192 ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_READ, &ktriov,
202 * Scatter read system call.
205 sys_readv(p, v, retval)
210 register struct sys_readv_args /* {
212 syscallarg(struct iovec *) iovp;
213 syscallarg(int) iovcnt;
215 register struct file *fp;
216 register struct filedesc *fdp = p->p_fd;
218 register struct iovec *iov;
219 struct iovec *needfree;
220 struct iovec aiov[UIO_SMALLIOV];
221 long i, cnt, error = 0;
224 struct iovec *ktriov = NULL;
227 if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
228 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
229 (fp->f_flag & FREAD) == 0)
231 if (SCARG(uap, iovcnt) <= 0)
233 /* note: can't use iovlen until iovcnt is validated */
234 iovlen = SCARG(uap, iovcnt) * sizeof (struct iovec);
235 if (SCARG(uap, iovcnt) > UIO_SMALLIOV) {
236 if (SCARG(uap, iovcnt) > IOV_MAX)
238 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
245 auio.uio_iovcnt = SCARG(uap, iovcnt);
246 auio.uio_rw = UIO_READ;
247 auio.uio_segflg = UIO_USERSPACE;
249 error = copyin((caddr_t)SCARG(uap, iovp), (caddr_t)iov, iovlen);
253 for (i = 0; i < SCARG(uap, iovcnt); i++, iov++) {
254 /* Don't allow sum > SSIZE_MAX */
255 if (iov->iov_len > SSIZE_MAX ||
256 (auio.uio_resid += iov->iov_len) > SSIZE_MAX) {
263 * if tracing, save a copy of iovec
265 if (KTRPOINT(p, KTR_GENIO)) {
266 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
267 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
270 cnt = auio.uio_resid;
271 error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred);
273 if (auio.uio_resid != cnt && (error == ERESTART ||
274 error == EINTR || error == EWOULDBLOCK))
276 cnt -= auio.uio_resid;
278 if (ktriov != NULL) {
280 ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_READ, ktriov,
282 FREE(ktriov, M_TEMP);
288 FREE(needfree, M_IOV);
298 sys_write(struct sys_write_args *uap, register_t *retval)
301 sys_write(p, v, retval)
308 register struct sys_write_args /* {
310 syscallarg(void *) buf;
311 syscallarg(size_t) nbyte;
313 register struct filedesc *fdp = p->p_fd;
324 if (getfp((u_int)SCARG(uap, fd), &fp) ||
326 if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
327 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
329 (fp->f_flag & FWRITE) == 0)
331 /* Don't allow nbyte to be larger than max return val */
332 if (SCARG(uap, nbyte) > SSIZE_MAX)
334 aiov.iov_base = (caddr_t)SCARG(uap, buf);
335 aiov.iov_len = SCARG(uap, nbyte);
336 auio.uio_iov = &aiov;
338 auio.uio_resid = SCARG(uap, nbyte);
339 auio.uio_rw = UIO_WRITE;
340 auio.uio_segflg = UIO_USERSPACE;
346 * if tracing, save a copy of iovec
348 if (KTRPOINT(p, KTR_GENIO))
351 cnt = SCARG(uap, nbyte);
353 error = (*fp->f_ops->fo_write)(fp, &auio);
355 error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred);
359 if (auio.uio_resid != cnt &&
360 (error == EINTR || error == EWOULDBLOCK))
363 if (auio.uio_resid != cnt && (error == ERESTART ||
364 error == EINTR || error == EWOULDBLOCK))
370 cnt -= auio.uio_resid;
372 if (KTRPOINT(p, KTR_GENIO) && error == 0)
373 ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_WRITE,
374 &ktriov, cnt, error);
382 * Gather write system call
385 sys_writev(p, v, retval)
390 register struct sys_writev_args /* {
392 syscallarg(struct iovec *) iovp;
393 syscallarg(int) iovcnt;
395 register struct file *fp;
396 register struct filedesc *fdp = p->p_fd;
398 register struct iovec *iov;
399 struct iovec *needfree;
400 struct iovec aiov[UIO_SMALLIOV];
401 long i, cnt, error = 0;
404 struct iovec *ktriov = NULL;
407 if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
408 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
409 (fp->f_flag & FWRITE) == 0)
411 if (SCARG(uap, iovcnt) <= 0)
413 /* note: can't use iovlen until iovcnt is validated */
414 iovlen = SCARG(uap, iovcnt) * sizeof (struct iovec);
415 if (SCARG(uap, iovcnt) > UIO_SMALLIOV) {
416 if (SCARG(uap, iovcnt) > IOV_MAX)
418 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
425 auio.uio_iovcnt = SCARG(uap, iovcnt);
426 auio.uio_rw = UIO_WRITE;
427 auio.uio_segflg = UIO_USERSPACE;
429 error = copyin((caddr_t)SCARG(uap, iovp), (caddr_t)iov, iovlen);
433 for (i = 0; i < SCARG(uap, iovcnt); i++, iov++) {
434 /* Don't allow sum > SSIZE_MAX */
435 if (iov->iov_len > SSIZE_MAX ||
436 (auio.uio_resid += iov->iov_len) > SSIZE_MAX) {
443 * if tracing, save a copy of iovec
445 if (KTRPOINT(p, KTR_GENIO)) {
446 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
447 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
450 cnt = auio.uio_resid;
451 error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred);
453 if (auio.uio_resid != cnt && (error == ERESTART ||
454 error == EINTR || error == EWOULDBLOCK))
459 cnt -= auio.uio_resid;
461 if (ktriov != NULL) {
463 ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_WRITE,
465 FREE(ktriov, M_TEMP);
471 FREE(needfree, M_IOV);
481 sys_ioctl(struct sys_ioctl_args *uap, register_t *retval)
485 sys_ioctl(p, v, retval)
492 register struct sys_ioctl_args /* {
494 syscallarg(u_long) com;
495 syscallarg(caddr_t) data;
497 register struct filedesc *fdp;
505 #define STK_PARAMS 128
506 char stkbuf[STK_PARAMS];
509 if (getfp(SCARG(uap, fd), &fp))
512 if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
513 (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
517 if ((fp->f_flag & (FREAD | FWRITE)) == 0)
521 com = SCARG(uap, com);
523 switch (com = SCARG(uap, com)) {
525 fdp->fd_ofileflags[SCARG(uap, fd)] &= ~UF_EXCLOSE;
528 fdp->fd_ofileflags[SCARG(uap, fd)] |= UF_EXCLOSE;
534 * Interpret high order word to find amount of data to be
535 * copied to/from the user's address space.
537 size = IOCPARM_LEN(com);
539 if (size > IOCPARM_MAX)
543 if (size > sizeof (stkbuf)) {
544 memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
550 error = copyin(SCARG(uap, data), data, (u_int)size);
553 free(memp, M_IOCTLOPS);
557 *(caddr_t *)data = SCARG(uap, data);
558 } else if ((com&IOC_OUT) && size)
560 * Zero the buffer so the user always
561 * gets back something deterministic.
564 else if (com&IOC_VOID)
565 *(caddr_t *)data = SCARG(uap, data);
570 if ((tmp = *(int *)data) != 0)
571 fp->f_flag |= FNONBLOCK;
573 fp->f_flag &= ~FNONBLOCK;
575 error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (CYG_ADDRWORD)&tmp);
577 error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
582 if ((tmp = *(int *)data) != 0)
583 fp->f_flag |= FASYNC;
585 fp->f_flag &= ~FASYNC;
587 error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (CYG_ADDRWORD)&tmp);
589 error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
596 if (fp->f_type == DTYPE_SOCKET) {
597 struct socket *so = (struct socket *)fp->f_data;
600 so->so_siguid = p->p_cred->p_ruid;
601 so->so_sigeuid = p->p_ucred->cr_uid;
608 struct proc *p1 = pfind(tmp);
613 tmp = p1->p_pgrp->pg_id;
615 error = (*fp->f_ops->fo_ioctl)
616 (fp, TIOCSPGRP, (caddr_t)&tmp, p);
620 if (fp->f_type == DTYPE_SOCKET) {
622 *(int *)data = ((struct socket *)fp->f_data)->so_pgid;
625 error = (*fp->f_ops->fo_ioctl)(fp, TIOCGPGRP, data, p);
626 *(int *)data = -*(int *)data;
631 error = (*fp->f_ops->fo_ioctl)(fp, com, (CYG_ADDRWORD)data);
633 error = (*fp->f_ops->fo_ioctl)(fp, com, data, p);
636 * Copy any data to user, size was
637 * already set and checked above.
639 if (error == 0 && (com&IOC_OUT) && size)
640 error = copyout(data, SCARG(uap, data), (u_int)size);
644 free(memp, M_IOCTLOPS);
649 int selwait, nselcoll;
652 * Select system call.
655 sys_select(p, v, retval)
656 register struct proc *p;
660 register struct sys_select_args /* {
662 syscallarg(fd_set *) in;
663 syscallarg(fd_set *) ou;
664 syscallarg(fd_set *) ex;
665 syscallarg(struct timeval *) tv;
667 fd_set bits[6], *pibits[3], *pobits[3];
669 int s, ncoll, error = 0, timo;
672 if (SCARG(uap, nd) > p->p_fd->fd_nfiles) {
673 /* forgiving; slightly wrong */
674 SCARG(uap, nd) = p->p_fd->fd_nfiles;
676 ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask);
677 if (SCARG(uap, nd) > FD_SETSIZE) {
680 if ((mbits = malloc(ni * 6, M_TEMP, M_WAITOK)) == NULL) {
684 bzero(mbits, ni * 6);
685 pibits[0] = (fd_set *)&mbits[ni * 0];
686 pibits[1] = (fd_set *)&mbits[ni * 1];
687 pibits[2] = (fd_set *)&mbits[ni * 2];
688 pobits[0] = (fd_set *)&mbits[ni * 3];
689 pobits[1] = (fd_set *)&mbits[ni * 4];
690 pobits[2] = (fd_set *)&mbits[ni * 5];
692 bzero((caddr_t)bits, sizeof(bits));
693 pibits[0] = &bits[0];
694 pibits[1] = &bits[1];
695 pibits[2] = &bits[2];
696 pobits[0] = &bits[3];
697 pobits[1] = &bits[4];
698 pobits[2] = &bits[5];
701 #define getbits(name, x) \
702 if (SCARG(uap, name) && (error = copyin((caddr_t)SCARG(uap, name), \
703 (caddr_t)pibits[x], ni))) \
710 if (SCARG(uap, tv)) {
711 error = copyin((caddr_t)SCARG(uap, tv), (caddr_t)&atv,
715 if (itimerfix(&atv)) {
720 timeradd(&atv, &time, &atv);
723 * Avoid inadvertently sleeping forever.
732 p->p_flag |= P_SELECT;
733 error = selscan(p, pibits[0], pobits[0], SCARG(uap, nd), retval);
734 if (error || *retval)
737 /* this should be timercmp(&time, &atv, >=) */
738 if (SCARG(uap, tv) && (time.tv_sec > atv.tv_sec ||
739 (time.tv_sec == atv.tv_sec && time.tv_usec >= atv.tv_usec))) {
743 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
747 p->p_flag &= ~P_SELECT;
748 error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
753 p->p_flag &= ~P_SELECT;
754 /* select is not restarted after signals... */
755 if (error == ERESTART)
757 if (error == EWOULDBLOCK)
759 #define putbits(name, x) \
760 if (SCARG(uap, name) && (error2 = copyout((caddr_t)pobits[x], \
761 (caddr_t)SCARG(uap, name), ni))) \
773 if (pibits[0] != &bits[0])
774 free(pibits[0], M_TEMP);
779 selscan(p, ibits, obits, nfd, retval)
781 fd_set *ibits, *obits;
785 caddr_t cibits = (caddr_t)ibits, cobits = (caddr_t)obits;
786 register struct filedesc *fdp = p->p_fd;
787 register int msk, i, j, fd;
788 register fd_mask bits;
791 static int flag[3] = { FREAD, FWRITE, 0 };
794 * if nfd > FD_SETSIZE then the fd_set's contain nfd bits (rounded
795 * up to the next byte) otherwise the fd_set's are normal sized.
798 if (nfd > FD_SETSIZE)
799 ni = howmany(nfd, NFDBITS) * sizeof(fd_mask);
801 for (msk = 0; msk < 3; msk++) {
802 fd_set *pibits = (fd_set *)&cibits[msk*ni];
803 fd_set *pobits = (fd_set *)&cobits[msk*ni];
805 for (i = 0; i < nfd; i += NFDBITS) {
806 bits = pibits->fds_bits[i/NFDBITS];
807 while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
809 fp = fdp->fd_ofiles[fd];
812 if ((*fp->f_ops->fo_select)(fp, flag[msk], p)) {
825 seltrue(dev, flag, p)
835 * Record a select request.
838 selrecord(selector, sip)
839 struct proc *selector;
845 mypid = selector->p_pid;
846 if (sip->si_selpid == mypid)
848 if (sip->si_selpid && (p = pfind(sip->si_selpid)) &&
849 p->p_wchan == (caddr_t)&selwait)
850 sip->si_flags |= SI_COLL;
852 sip->si_selpid = mypid;
856 * Do a wakeup when a selectable event occurs.
860 register struct selinfo *sip;
862 register struct proc *p;
865 if (sip->si_selpid == 0)
867 if (sip->si_flags & SI_COLL) {
869 sip->si_flags &= ~SI_COLL;
870 wakeup((caddr_t)&selwait);
872 p = pfind(sip->si_selpid);
876 if (p->p_wchan == (caddr_t)&selwait) {
877 if (p->p_stat == SSLEEP)
881 } else if (p->p_flag & P_SELECT)
882 p->p_flag &= ~P_SELECT;
888 pollscan(p, pl, nfd, retval)
894 register struct filedesc *fdp = p->p_fd;
898 static int flag[3] = { FREAD, FWRITE, 0 };
899 static int pflag[3] = { POLLIN|POLLRDNORM, POLLOUT, POLLERR };
902 * XXX: We need to implement the rest of the flags.
904 for (i = 0; i < nfd; i++) {
905 /* Check the file descriptor. */
908 if (pl[i].fd >= fdp->fd_nfiles) {
909 pl[i].revents = POLLNVAL;
914 fp = fdp->fd_ofiles[pl[i].fd];
916 pl[i].revents = POLLNVAL;
920 for (x = msk = 0; msk < 3; msk++) {
921 if (pl[i].events & pflag[msk]) {
922 if ((*fp->f_ops->fo_select)(fp, flag[msk], p)) {
923 pl[i].revents |= pflag[msk] &
936 * We are using the same mechanism as select only we encode/decode args
940 sys_poll(p, v, retval)
941 register struct proc *p;
945 struct sys_poll_args *uap = v;
947 struct pollfd pfds[4], *pl = pfds;
948 int msec = SCARG(uap, timeout);
950 int timo, ncoll, i, s, error, error2;
951 extern int nselcoll, selwait;
953 /* Standards say no more than MAX_OPEN; this is possibly better. */
954 if (SCARG(uap, nfds) > min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur,
958 sz = sizeof(struct pollfd) * SCARG(uap, nfds);
960 /* optimize for the default case, of a small nfds value */
961 if (sz > sizeof(pfds))
962 pl = (struct pollfd *) malloc(sz, M_TEMP, M_WAITOK);
964 if ((error = copyin(SCARG(uap, fds), pl, sz)) != 0)
967 for (i = 0; i < SCARG(uap, nfds); i++)
971 atv.tv_sec = msec / 1000;
972 atv.tv_usec = (msec - (atv.tv_sec * 1000)) * 1000;
974 if (itimerfix(&atv)) {
979 timeradd(&atv, &time, &atv);
982 * Avoid inadvertently sleeping forever.
992 p->p_flag |= P_SELECT;
993 pollscan(p, pl, SCARG(uap, nfds), retval);
997 if (timo && timercmp(&time, &atv, >=)) {
1001 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
1005 p->p_flag &= ~P_SELECT;
1006 error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "poll", timo);
1012 p->p_flag &= ~P_SELECT;
1013 /* poll is not restarted after signals... */
1014 if (error == ERESTART)
1016 if (error == EWOULDBLOCK)
1018 if ((error2 = copyout(pl, SCARG(uap, fds), sz)) != 0)
1022 free((char *) pl, M_TEMP);