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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
38 * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94
39 * $FreeBSD: src/sys/kern/sys_generic.c,v 1.55.2.10 2001/03/17 10:39:32 peter Exp $
40 * $DragonFly: src/sys/kern/sys_generic.c,v 1.2 2003/06/17 04:28:41 dillon Exp $
43 #include "opt_ktrace.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/sysproto.h>
48 #include <sys/filedesc.h>
49 #include <sys/filio.h>
50 #include <sys/fcntl.h>
53 #include <sys/signalvar.h>
54 #include <sys/socketvar.h>
56 #include <sys/kernel.h>
57 #include <sys/malloc.h>
59 #include <sys/resourcevar.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysent.h>
64 #include <sys/ktrace.h>
67 #include <vm/vm_page.h>
69 #include <machine/limits.h>
71 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
72 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
73 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
75 static int pollscan __P((struct proc *, struct pollfd *, u_int));
76 static int selscan __P((struct proc *, fd_mask **, fd_mask **, int));
77 static int dofileread __P((struct proc *, struct file *, int, void *,
79 static int dofilewrite __P((struct proc *, struct file *, int,
80 const void *, size_t, off_t, int));
89 if (((u_int)fd) >= fdp->fd_nfiles ||
90 (fp = fdp->fd_ofiles[fd]) == NULL ||
91 (fp->f_flag & flag) == 0) {
101 #ifndef _SYS_SYSPROTO_H_
111 register struct read_args *uap;
113 register struct file *fp;
116 if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) == NULL)
118 error = dofileread(p, fp, uap->fd, uap->buf, uap->nbyte, (off_t)-1, 0);
126 #ifndef _SYS_SYSPROTO_H_
138 register struct pread_args *uap;
140 register struct file *fp;
143 if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) == NULL)
145 if (fp->f_type != DTYPE_VNODE) {
148 error = dofileread(p, fp, uap->fd, uap->buf, uap->nbyte,
149 uap->offset, FOF_OFFSET);
156 * Code common for read and pread
159 dofileread(p, fp, fd, buf, nbyte, offset, flags)
176 aiov.iov_base = (caddr_t)buf;
177 aiov.iov_len = nbyte;
178 auio.uio_iov = &aiov;
180 auio.uio_offset = offset;
183 auio.uio_resid = nbyte;
184 auio.uio_rw = UIO_READ;
185 auio.uio_segflg = UIO_USERSPACE;
189 * if tracing, save a copy of iovec
191 if (KTRPOINT(p, KTR_GENIO)) {
199 if ((error = fo_read(fp, &auio, fp->f_cred, flags, p))) {
200 if (auio.uio_resid != cnt && (error == ERESTART ||
201 error == EINTR || error == EWOULDBLOCK))
204 cnt -= auio.uio_resid;
206 if (didktr && error == 0) {
207 ktruio.uio_iov = &ktriov;
208 ktruio.uio_resid = cnt;
209 ktrgenio(p->p_tracep, fd, UIO_READ, &ktruio, error);
212 p->p_retval[0] = cnt;
217 * Scatter read system call.
219 #ifndef _SYS_SYSPROTO_H_
229 register struct readv_args *uap;
231 register struct file *fp;
232 register struct filedesc *fdp = p->p_fd;
234 register struct iovec *iov;
235 struct iovec *needfree;
236 struct iovec aiov[UIO_SMALLIOV];
237 long i, cnt, error = 0;
240 struct iovec *ktriov = NULL;
244 if ((fp = holdfp(fdp, uap->fd, FREAD)) == NULL)
246 /* note: can't use iovlen until iovcnt is validated */
247 iovlen = uap->iovcnt * sizeof (struct iovec);
248 if (uap->iovcnt > UIO_SMALLIOV) {
249 if (uap->iovcnt > UIO_MAXIOV)
251 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
258 auio.uio_iovcnt = uap->iovcnt;
259 auio.uio_rw = UIO_READ;
260 auio.uio_segflg = UIO_USERSPACE;
262 auio.uio_offset = -1;
263 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
266 for (i = 0; i < uap->iovcnt; i++) {
267 if (iov->iov_len > INT_MAX - auio.uio_resid) {
271 auio.uio_resid += iov->iov_len;
276 * if tracing, save a copy of iovec
278 if (KTRPOINT(p, KTR_GENIO)) {
279 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
280 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
284 cnt = auio.uio_resid;
285 if ((error = fo_read(fp, &auio, fp->f_cred, 0, p))) {
286 if (auio.uio_resid != cnt && (error == ERESTART ||
287 error == EINTR || error == EWOULDBLOCK))
290 cnt -= auio.uio_resid;
292 if (ktriov != NULL) {
294 ktruio.uio_iov = ktriov;
295 ktruio.uio_resid = cnt;
296 ktrgenio(p->p_tracep, uap->fd, UIO_READ, &ktruio,
299 FREE(ktriov, M_TEMP);
302 p->p_retval[0] = cnt;
306 FREE(needfree, M_IOV);
313 #ifndef _SYS_SYSPROTO_H_
323 register struct write_args *uap;
325 register struct file *fp;
328 if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL)
330 error = dofilewrite(p, fp, uap->fd, uap->buf, uap->nbyte, (off_t)-1, 0);
338 #ifndef _SYS_SYSPROTO_H_
350 register struct pwrite_args *uap;
352 register struct file *fp;
355 if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL)
357 if (fp->f_type != DTYPE_VNODE) {
360 error = dofilewrite(p, fp, uap->fd, uap->buf, uap->nbyte,
361 uap->offset, FOF_OFFSET);
368 dofilewrite(p, fp, fd, buf, nbyte, offset, flags)
385 aiov.iov_base = (void *)(uintptr_t)buf;
386 aiov.iov_len = nbyte;
387 auio.uio_iov = &aiov;
389 auio.uio_offset = offset;
392 auio.uio_resid = nbyte;
393 auio.uio_rw = UIO_WRITE;
394 auio.uio_segflg = UIO_USERSPACE;
398 * if tracing, save a copy of iovec and uio
400 if (KTRPOINT(p, KTR_GENIO)) {
407 if (fp->f_type == DTYPE_VNODE)
409 if ((error = fo_write(fp, &auio, fp->f_cred, flags, p))) {
410 if (auio.uio_resid != cnt && (error == ERESTART ||
411 error == EINTR || error == EWOULDBLOCK))
416 cnt -= auio.uio_resid;
418 if (didktr && error == 0) {
419 ktruio.uio_iov = &ktriov;
420 ktruio.uio_resid = cnt;
421 ktrgenio(p->p_tracep, fd, UIO_WRITE, &ktruio, error);
424 p->p_retval[0] = cnt;
429 * Gather write system call
431 #ifndef _SYS_SYSPROTO_H_
441 register struct writev_args *uap;
443 register struct file *fp;
444 register struct filedesc *fdp = p->p_fd;
446 register struct iovec *iov;
447 struct iovec *needfree;
448 struct iovec aiov[UIO_SMALLIOV];
449 long i, cnt, error = 0;
452 struct iovec *ktriov = NULL;
456 if ((fp = holdfp(fdp, uap->fd, FWRITE)) == NULL)
458 /* note: can't use iovlen until iovcnt is validated */
459 iovlen = uap->iovcnt * sizeof (struct iovec);
460 if (uap->iovcnt > UIO_SMALLIOV) {
461 if (uap->iovcnt > UIO_MAXIOV) {
466 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
473 auio.uio_iovcnt = uap->iovcnt;
474 auio.uio_rw = UIO_WRITE;
475 auio.uio_segflg = UIO_USERSPACE;
477 auio.uio_offset = -1;
478 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
481 for (i = 0; i < uap->iovcnt; i++) {
482 if (iov->iov_len > INT_MAX - auio.uio_resid) {
486 auio.uio_resid += iov->iov_len;
491 * if tracing, save a copy of iovec and uio
493 if (KTRPOINT(p, KTR_GENIO)) {
494 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
495 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
499 cnt = auio.uio_resid;
500 if (fp->f_type == DTYPE_VNODE)
502 if ((error = fo_write(fp, &auio, fp->f_cred, 0, p))) {
503 if (auio.uio_resid != cnt && (error == ERESTART ||
504 error == EINTR || error == EWOULDBLOCK))
509 cnt -= auio.uio_resid;
511 if (ktriov != NULL) {
513 ktruio.uio_iov = ktriov;
514 ktruio.uio_resid = cnt;
515 ktrgenio(p->p_tracep, uap->fd, UIO_WRITE, &ktruio,
518 FREE(ktriov, M_TEMP);
521 p->p_retval[0] = cnt;
525 FREE(needfree, M_IOV);
532 #ifndef _SYS_SYSPROTO_H_
543 register struct ioctl_args *uap;
545 register struct file *fp;
546 register struct filedesc *fdp;
552 #define STK_PARAMS 128
554 char stkbuf[STK_PARAMS];
559 if ((u_int)uap->fd >= fdp->fd_nfiles ||
560 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
563 if ((fp->f_flag & (FREAD | FWRITE)) == 0)
566 switch (com = uap->com) {
568 fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
571 fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
576 * Interpret high order word to find amount of data to be
577 * copied to/from the user's address space.
579 size = IOCPARM_LEN(com);
580 if (size > IOCPARM_MAX)
586 if (size > sizeof (ubuf.stkbuf)) {
587 memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
594 error = copyin(uap->data, data, (u_int)size);
597 free(memp, M_IOCTLOPS);
602 *(caddr_t *)data = uap->data;
604 } else if ((com&IOC_OUT) && size) {
606 * Zero the buffer so the user always
607 * gets back something deterministic.
610 } else if (com&IOC_VOID) {
611 *(caddr_t *)data = uap->data;
617 if ((tmp = *(int *)data))
618 fp->f_flag |= FNONBLOCK;
620 fp->f_flag &= ~FNONBLOCK;
621 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
625 if ((tmp = *(int *)data))
626 fp->f_flag |= FASYNC;
628 fp->f_flag &= ~FASYNC;
629 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, p);
633 error = fo_ioctl(fp, com, data, p);
635 * Copy any data to user, size was
636 * already set and checked above.
638 if (error == 0 && (com&IOC_OUT) && size)
639 error = copyout(data, uap->data, (u_int)size);
643 free(memp, M_IOCTLOPS);
648 static int nselcoll; /* Select collisions since boot */
650 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
653 * Select system call.
655 #ifndef _SYS_SYSPROTO_H_
658 fd_set *in, *ou, *ex;
664 register struct proc *p;
665 register struct select_args *uap;
668 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
669 * infds with the new FD_SETSIZE of 1024, and more than enough for
670 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
673 fd_mask s_selbits[howmany(2048, NFDBITS)];
674 fd_mask *ibits[3], *obits[3], *selbits, *sbp;
675 struct timeval atv, rtv, ttv;
676 int s, ncoll, error, timo;
677 u_int nbufbytes, ncpbytes, nfdbits;
681 if (uap->nd > p->p_fd->fd_nfiles)
682 uap->nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */
685 * Allocate just enough bits for the non-null fd_sets. Use the
686 * preallocated auto buffer if possible.
688 nfdbits = roundup(uap->nd, NFDBITS);
689 ncpbytes = nfdbits / NBBY;
692 nbufbytes += 2 * ncpbytes;
694 nbufbytes += 2 * ncpbytes;
696 nbufbytes += 2 * ncpbytes;
697 if (nbufbytes <= sizeof s_selbits)
698 selbits = &s_selbits[0];
700 selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
703 * Assign pointers into the bit buffers and fetch the input bits.
704 * Put the output buffers together so that they can be bzeroed
708 #define getbits(name, x) \
710 if (uap->name == NULL) \
713 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
715 sbp += ncpbytes / sizeof *sbp; \
716 error = copyin(uap->name, ibits[x], ncpbytes); \
726 bzero(selbits, nbufbytes / 2);
729 error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
733 if (itimerfix(&atv)) {
737 getmicrouptime(&rtv);
738 timevaladd(&atv, &rtv);
746 p->p_flag |= P_SELECT;
747 error = selscan(p, ibits, obits, uap->nd);
748 if (error || p->p_retval[0])
750 if (atv.tv_sec || atv.tv_usec) {
751 getmicrouptime(&rtv);
752 if (timevalcmp(&rtv, &atv, >=))
755 timevalsub(&ttv, &rtv);
756 timo = ttv.tv_sec > 24 * 60 * 60 ?
757 24 * 60 * 60 * hz : tvtohz(&ttv);
760 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
764 p->p_flag &= ~P_SELECT;
766 error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
772 p->p_flag &= ~P_SELECT;
773 /* select is not restarted after signals... */
774 if (error == ERESTART)
776 if (error == EWOULDBLOCK)
778 #define putbits(name, x) \
779 if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
789 if (selbits != &s_selbits[0])
790 free(selbits, M_SELECT);
795 selscan(p, ibits, obits, nfd)
797 fd_mask **ibits, **obits;
800 struct filedesc *fdp = p->p_fd;
805 /* Note: backend also returns POLLHUP/POLLERR if appropriate. */
806 static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
808 for (msk = 0; msk < 3; msk++) {
809 if (ibits[msk] == NULL)
811 for (i = 0; i < nfd; i += NFDBITS) {
812 bits = ibits[msk][i/NFDBITS];
813 /* ffs(int mask) not portable, fd_mask is long */
814 for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
817 fp = fdp->fd_ofiles[fd];
820 if (fo_poll(fp, flag[msk], fp->f_cred, p)) {
821 obits[msk][(fd)/NFDBITS] |=
822 ((fd_mask)1 << ((fd) % NFDBITS));
835 #ifndef _SYS_SYSPROTO_H_
845 struct poll_args *uap;
848 char smallbits[32 * sizeof(struct pollfd)];
849 struct timeval atv, rtv, ttv;
850 int s, ncoll, error = 0, timo;
854 nfds = SCARG(uap, nfds);
856 * This is kinda bogus. We have fd limits, but that is not
857 * really related to the size of the pollfd array. Make sure
858 * we let the process use at least FD_SETSIZE entries and at
859 * least enough for the current limits. We want to be reasonably
860 * safe, but not overly restrictive.
862 if (nfds > p->p_rlimit[RLIMIT_NOFILE].rlim_cur && nfds > FD_SETSIZE)
864 ni = nfds * sizeof(struct pollfd);
865 if (ni > sizeof(smallbits))
866 bits = malloc(ni, M_TEMP, M_WAITOK);
869 error = copyin(SCARG(uap, fds), bits, ni);
872 if (SCARG(uap, timeout) != INFTIM) {
873 atv.tv_sec = SCARG(uap, timeout) / 1000;
874 atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
875 if (itimerfix(&atv)) {
879 getmicrouptime(&rtv);
880 timevaladd(&atv, &rtv);
888 p->p_flag |= P_SELECT;
889 error = pollscan(p, (struct pollfd *)bits, nfds);
890 if (error || p->p_retval[0])
892 if (atv.tv_sec || atv.tv_usec) {
893 getmicrouptime(&rtv);
894 if (timevalcmp(&rtv, &atv, >=))
897 timevalsub(&ttv, &rtv);
898 timo = ttv.tv_sec > 24 * 60 * 60 ?
899 24 * 60 * 60 * hz : tvtohz(&ttv);
902 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
906 p->p_flag &= ~P_SELECT;
907 error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "poll", timo);
912 p->p_flag &= ~P_SELECT;
913 /* poll is not restarted after signals... */
914 if (error == ERESTART)
916 if (error == EWOULDBLOCK)
919 error = copyout(bits, SCARG(uap, fds), ni);
924 if (ni > sizeof(smallbits))
930 pollscan(p, fds, nfd)
935 register struct filedesc *fdp = p->p_fd;
940 for (i = 0; i < nfd; i++, fds++) {
941 if (fds->fd >= fdp->fd_nfiles) {
942 fds->revents = POLLNVAL;
944 } else if (fds->fd < 0) {
947 fp = fdp->fd_ofiles[fds->fd];
949 fds->revents = POLLNVAL;
953 * Note: backend also returns POLLHUP and
954 * POLLERR if appropriate.
956 fds->revents = fo_poll(fp, fds->events,
958 if (fds->revents != 0)
968 * OpenBSD poll system call.
969 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
971 #ifndef _SYS_SYSPROTO_H_
972 struct openbsd_poll_args {
980 register struct proc *p;
981 register struct openbsd_poll_args *uap;
983 return (poll(p, (struct poll_args *)uap));
988 seltrue(dev, events, p)
994 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
998 * Record a select request.
1001 selrecord(selector, sip)
1002 struct proc *selector;
1003 struct selinfo *sip;
1008 mypid = selector->p_pid;
1009 if (sip->si_pid == mypid)
1011 if (sip->si_pid && (p = pfind(sip->si_pid)) &&
1012 p->p_wchan == (caddr_t)&selwait)
1013 sip->si_flags |= SI_COLL;
1015 sip->si_pid = mypid;
1019 * Do a wakeup when a selectable event occurs.
1023 register struct selinfo *sip;
1025 register struct proc *p;
1028 if (sip->si_pid == 0)
1030 if (sip->si_flags & SI_COLL) {
1032 sip->si_flags &= ~SI_COLL;
1033 wakeup((caddr_t)&selwait);
1035 p = pfind(sip->si_pid);
1039 if (p->p_wchan == (caddr_t)&selwait) {
1040 if (p->p_stat == SSLEEP)
1044 } else if (p->p_flag & P_SELECT)
1045 p->p_flag &= ~P_SELECT;