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 $
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/sysproto.h>
47 #include <sys/filedesc.h>
48 #include <sys/filio.h>
49 #include <sys/fcntl.h>
52 #include <sys/signalvar.h>
53 #include <sys/socketvar.h>
55 #include <sys/kernel.h>
56 #include <sys/malloc.h>
58 #include <sys/resourcevar.h>
59 #include <sys/sysctl.h>
60 #include <sys/sysent.h>
63 #include <sys/ktrace.h>
66 #include <vm/vm_page.h>
68 #include <machine/limits.h>
70 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
71 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
72 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
74 static int pollscan __P((struct proc *, struct pollfd *, u_int));
75 static int selscan __P((struct proc *, fd_mask **, fd_mask **, int));
76 static int dofileread __P((struct proc *, struct file *, int, void *,
78 static int dofilewrite __P((struct proc *, struct file *, int,
79 const void *, size_t, off_t, int));
88 if (((u_int)fd) >= fdp->fd_nfiles ||
89 (fp = fdp->fd_ofiles[fd]) == NULL ||
90 (fp->f_flag & flag) == 0) {
100 #ifndef _SYS_SYSPROTO_H_
110 register struct read_args *uap;
112 register struct file *fp;
115 if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) == NULL)
117 error = dofileread(p, fp, uap->fd, uap->buf, uap->nbyte, (off_t)-1, 0);
125 #ifndef _SYS_SYSPROTO_H_
137 register struct pread_args *uap;
139 register struct file *fp;
142 if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) == NULL)
144 if (fp->f_type != DTYPE_VNODE) {
147 error = dofileread(p, fp, uap->fd, uap->buf, uap->nbyte,
148 uap->offset, FOF_OFFSET);
155 * Code common for read and pread
158 dofileread(p, fp, fd, buf, nbyte, offset, flags)
175 aiov.iov_base = (caddr_t)buf;
176 aiov.iov_len = nbyte;
177 auio.uio_iov = &aiov;
179 auio.uio_offset = offset;
182 auio.uio_resid = nbyte;
183 auio.uio_rw = UIO_READ;
184 auio.uio_segflg = UIO_USERSPACE;
188 * if tracing, save a copy of iovec
190 if (KTRPOINT(p, KTR_GENIO)) {
198 if ((error = fo_read(fp, &auio, fp->f_cred, flags, p))) {
199 if (auio.uio_resid != cnt && (error == ERESTART ||
200 error == EINTR || error == EWOULDBLOCK))
203 cnt -= auio.uio_resid;
205 if (didktr && error == 0) {
206 ktruio.uio_iov = &ktriov;
207 ktruio.uio_resid = cnt;
208 ktrgenio(p->p_tracep, fd, UIO_READ, &ktruio, error);
211 p->p_retval[0] = cnt;
216 * Scatter read system call.
218 #ifndef _SYS_SYSPROTO_H_
228 register struct readv_args *uap;
230 register struct file *fp;
231 register struct filedesc *fdp = p->p_fd;
233 register struct iovec *iov;
234 struct iovec *needfree;
235 struct iovec aiov[UIO_SMALLIOV];
236 long i, cnt, error = 0;
239 struct iovec *ktriov = NULL;
243 if ((fp = holdfp(fdp, uap->fd, FREAD)) == NULL)
245 /* note: can't use iovlen until iovcnt is validated */
246 iovlen = uap->iovcnt * sizeof (struct iovec);
247 if (uap->iovcnt > UIO_SMALLIOV) {
248 if (uap->iovcnt > UIO_MAXIOV)
250 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
257 auio.uio_iovcnt = uap->iovcnt;
258 auio.uio_rw = UIO_READ;
259 auio.uio_segflg = UIO_USERSPACE;
261 auio.uio_offset = -1;
262 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
265 for (i = 0; i < uap->iovcnt; i++) {
266 if (iov->iov_len > INT_MAX - auio.uio_resid) {
270 auio.uio_resid += iov->iov_len;
275 * if tracing, save a copy of iovec
277 if (KTRPOINT(p, KTR_GENIO)) {
278 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
279 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
283 cnt = auio.uio_resid;
284 if ((error = fo_read(fp, &auio, fp->f_cred, 0, p))) {
285 if (auio.uio_resid != cnt && (error == ERESTART ||
286 error == EINTR || error == EWOULDBLOCK))
289 cnt -= auio.uio_resid;
291 if (ktriov != NULL) {
293 ktruio.uio_iov = ktriov;
294 ktruio.uio_resid = cnt;
295 ktrgenio(p->p_tracep, uap->fd, UIO_READ, &ktruio,
298 FREE(ktriov, M_TEMP);
301 p->p_retval[0] = cnt;
305 FREE(needfree, M_IOV);
312 #ifndef _SYS_SYSPROTO_H_
322 register struct write_args *uap;
324 register struct file *fp;
327 if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL)
329 error = dofilewrite(p, fp, uap->fd, uap->buf, uap->nbyte, (off_t)-1, 0);
337 #ifndef _SYS_SYSPROTO_H_
349 register struct pwrite_args *uap;
351 register struct file *fp;
354 if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL)
356 if (fp->f_type != DTYPE_VNODE) {
359 error = dofilewrite(p, fp, uap->fd, uap->buf, uap->nbyte,
360 uap->offset, FOF_OFFSET);
367 dofilewrite(p, fp, fd, buf, nbyte, offset, flags)
384 aiov.iov_base = (void *)(uintptr_t)buf;
385 aiov.iov_len = nbyte;
386 auio.uio_iov = &aiov;
388 auio.uio_offset = offset;
391 auio.uio_resid = nbyte;
392 auio.uio_rw = UIO_WRITE;
393 auio.uio_segflg = UIO_USERSPACE;
397 * if tracing, save a copy of iovec and uio
399 if (KTRPOINT(p, KTR_GENIO)) {
406 if (fp->f_type == DTYPE_VNODE)
408 if ((error = fo_write(fp, &auio, fp->f_cred, flags, p))) {
409 if (auio.uio_resid != cnt && (error == ERESTART ||
410 error == EINTR || error == EWOULDBLOCK))
415 cnt -= auio.uio_resid;
417 if (didktr && error == 0) {
418 ktruio.uio_iov = &ktriov;
419 ktruio.uio_resid = cnt;
420 ktrgenio(p->p_tracep, fd, UIO_WRITE, &ktruio, error);
423 p->p_retval[0] = cnt;
428 * Gather write system call
430 #ifndef _SYS_SYSPROTO_H_
440 register struct writev_args *uap;
442 register struct file *fp;
443 register struct filedesc *fdp = p->p_fd;
445 register struct iovec *iov;
446 struct iovec *needfree;
447 struct iovec aiov[UIO_SMALLIOV];
448 long i, cnt, error = 0;
451 struct iovec *ktriov = NULL;
455 if ((fp = holdfp(fdp, uap->fd, FWRITE)) == NULL)
457 /* note: can't use iovlen until iovcnt is validated */
458 iovlen = uap->iovcnt * sizeof (struct iovec);
459 if (uap->iovcnt > UIO_SMALLIOV) {
460 if (uap->iovcnt > UIO_MAXIOV) {
465 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
472 auio.uio_iovcnt = uap->iovcnt;
473 auio.uio_rw = UIO_WRITE;
474 auio.uio_segflg = UIO_USERSPACE;
476 auio.uio_offset = -1;
477 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
480 for (i = 0; i < uap->iovcnt; i++) {
481 if (iov->iov_len > INT_MAX - auio.uio_resid) {
485 auio.uio_resid += iov->iov_len;
490 * if tracing, save a copy of iovec and uio
492 if (KTRPOINT(p, KTR_GENIO)) {
493 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
494 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
498 cnt = auio.uio_resid;
499 if (fp->f_type == DTYPE_VNODE)
501 if ((error = fo_write(fp, &auio, fp->f_cred, 0, p))) {
502 if (auio.uio_resid != cnt && (error == ERESTART ||
503 error == EINTR || error == EWOULDBLOCK))
508 cnt -= auio.uio_resid;
510 if (ktriov != NULL) {
512 ktruio.uio_iov = ktriov;
513 ktruio.uio_resid = cnt;
514 ktrgenio(p->p_tracep, uap->fd, UIO_WRITE, &ktruio,
517 FREE(ktriov, M_TEMP);
520 p->p_retval[0] = cnt;
524 FREE(needfree, M_IOV);
531 #ifndef _SYS_SYSPROTO_H_
542 register struct ioctl_args *uap;
544 register struct file *fp;
545 register struct filedesc *fdp;
551 #define STK_PARAMS 128
553 char stkbuf[STK_PARAMS];
558 if ((u_int)uap->fd >= fdp->fd_nfiles ||
559 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
562 if ((fp->f_flag & (FREAD | FWRITE)) == 0)
565 switch (com = uap->com) {
567 fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
570 fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
575 * Interpret high order word to find amount of data to be
576 * copied to/from the user's address space.
578 size = IOCPARM_LEN(com);
579 if (size > IOCPARM_MAX)
585 if (size > sizeof (ubuf.stkbuf)) {
586 memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
593 error = copyin(uap->data, data, (u_int)size);
596 free(memp, M_IOCTLOPS);
601 *(caddr_t *)data = uap->data;
603 } else if ((com&IOC_OUT) && size) {
605 * Zero the buffer so the user always
606 * gets back something deterministic.
609 } else if (com&IOC_VOID) {
610 *(caddr_t *)data = uap->data;
616 if ((tmp = *(int *)data))
617 fp->f_flag |= FNONBLOCK;
619 fp->f_flag &= ~FNONBLOCK;
620 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
624 if ((tmp = *(int *)data))
625 fp->f_flag |= FASYNC;
627 fp->f_flag &= ~FASYNC;
628 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, p);
632 error = fo_ioctl(fp, com, data, p);
634 * Copy any data to user, size was
635 * already set and checked above.
637 if (error == 0 && (com&IOC_OUT) && size)
638 error = copyout(data, uap->data, (u_int)size);
642 free(memp, M_IOCTLOPS);
647 static int nselcoll; /* Select collisions since boot */
649 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
652 * Select system call.
654 #ifndef _SYS_SYSPROTO_H_
657 fd_set *in, *ou, *ex;
663 register struct proc *p;
664 register struct select_args *uap;
667 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
668 * infds with the new FD_SETSIZE of 1024, and more than enough for
669 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
672 fd_mask s_selbits[howmany(2048, NFDBITS)];
673 fd_mask *ibits[3], *obits[3], *selbits, *sbp;
674 struct timeval atv, rtv, ttv;
675 int s, ncoll, error, timo;
676 u_int nbufbytes, ncpbytes, nfdbits;
680 if (uap->nd > p->p_fd->fd_nfiles)
681 uap->nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */
684 * Allocate just enough bits for the non-null fd_sets. Use the
685 * preallocated auto buffer if possible.
687 nfdbits = roundup(uap->nd, NFDBITS);
688 ncpbytes = nfdbits / NBBY;
691 nbufbytes += 2 * ncpbytes;
693 nbufbytes += 2 * ncpbytes;
695 nbufbytes += 2 * ncpbytes;
696 if (nbufbytes <= sizeof s_selbits)
697 selbits = &s_selbits[0];
699 selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
702 * Assign pointers into the bit buffers and fetch the input bits.
703 * Put the output buffers together so that they can be bzeroed
707 #define getbits(name, x) \
709 if (uap->name == NULL) \
712 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
714 sbp += ncpbytes / sizeof *sbp; \
715 error = copyin(uap->name, ibits[x], ncpbytes); \
725 bzero(selbits, nbufbytes / 2);
728 error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
732 if (itimerfix(&atv)) {
736 getmicrouptime(&rtv);
737 timevaladd(&atv, &rtv);
745 p->p_flag |= P_SELECT;
746 error = selscan(p, ibits, obits, uap->nd);
747 if (error || p->p_retval[0])
749 if (atv.tv_sec || atv.tv_usec) {
750 getmicrouptime(&rtv);
751 if (timevalcmp(&rtv, &atv, >=))
754 timevalsub(&ttv, &rtv);
755 timo = ttv.tv_sec > 24 * 60 * 60 ?
756 24 * 60 * 60 * hz : tvtohz(&ttv);
759 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
763 p->p_flag &= ~P_SELECT;
765 error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
771 p->p_flag &= ~P_SELECT;
772 /* select is not restarted after signals... */
773 if (error == ERESTART)
775 if (error == EWOULDBLOCK)
777 #define putbits(name, x) \
778 if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
788 if (selbits != &s_selbits[0])
789 free(selbits, M_SELECT);
794 selscan(p, ibits, obits, nfd)
796 fd_mask **ibits, **obits;
799 struct filedesc *fdp = p->p_fd;
804 /* Note: backend also returns POLLHUP/POLLERR if appropriate. */
805 static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
807 for (msk = 0; msk < 3; msk++) {
808 if (ibits[msk] == NULL)
810 for (i = 0; i < nfd; i += NFDBITS) {
811 bits = ibits[msk][i/NFDBITS];
812 /* ffs(int mask) not portable, fd_mask is long */
813 for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
816 fp = fdp->fd_ofiles[fd];
819 if (fo_poll(fp, flag[msk], fp->f_cred, p)) {
820 obits[msk][(fd)/NFDBITS] |=
821 ((fd_mask)1 << ((fd) % NFDBITS));
834 #ifndef _SYS_SYSPROTO_H_
844 struct poll_args *uap;
847 char smallbits[32 * sizeof(struct pollfd)];
848 struct timeval atv, rtv, ttv;
849 int s, ncoll, error = 0, timo;
853 nfds = SCARG(uap, nfds);
855 * This is kinda bogus. We have fd limits, but that is not
856 * really related to the size of the pollfd array. Make sure
857 * we let the process use at least FD_SETSIZE entries and at
858 * least enough for the current limits. We want to be reasonably
859 * safe, but not overly restrictive.
861 if (nfds > p->p_rlimit[RLIMIT_NOFILE].rlim_cur && nfds > FD_SETSIZE)
863 ni = nfds * sizeof(struct pollfd);
864 if (ni > sizeof(smallbits))
865 bits = malloc(ni, M_TEMP, M_WAITOK);
868 error = copyin(SCARG(uap, fds), bits, ni);
871 if (SCARG(uap, timeout) != INFTIM) {
872 atv.tv_sec = SCARG(uap, timeout) / 1000;
873 atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
874 if (itimerfix(&atv)) {
878 getmicrouptime(&rtv);
879 timevaladd(&atv, &rtv);
887 p->p_flag |= P_SELECT;
888 error = pollscan(p, (struct pollfd *)bits, nfds);
889 if (error || p->p_retval[0])
891 if (atv.tv_sec || atv.tv_usec) {
892 getmicrouptime(&rtv);
893 if (timevalcmp(&rtv, &atv, >=))
896 timevalsub(&ttv, &rtv);
897 timo = ttv.tv_sec > 24 * 60 * 60 ?
898 24 * 60 * 60 * hz : tvtohz(&ttv);
901 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
905 p->p_flag &= ~P_SELECT;
906 error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "poll", timo);
911 p->p_flag &= ~P_SELECT;
912 /* poll is not restarted after signals... */
913 if (error == ERESTART)
915 if (error == EWOULDBLOCK)
918 error = copyout(bits, SCARG(uap, fds), ni);
923 if (ni > sizeof(smallbits))
929 pollscan(p, fds, nfd)
934 register struct filedesc *fdp = p->p_fd;
939 for (i = 0; i < nfd; i++, fds++) {
940 if (fds->fd >= fdp->fd_nfiles) {
941 fds->revents = POLLNVAL;
943 } else if (fds->fd < 0) {
946 fp = fdp->fd_ofiles[fds->fd];
948 fds->revents = POLLNVAL;
952 * Note: backend also returns POLLHUP and
953 * POLLERR if appropriate.
955 fds->revents = fo_poll(fp, fds->events,
957 if (fds->revents != 0)
967 * OpenBSD poll system call.
968 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
970 #ifndef _SYS_SYSPROTO_H_
971 struct openbsd_poll_args {
979 register struct proc *p;
980 register struct openbsd_poll_args *uap;
982 return (poll(p, (struct poll_args *)uap));
987 seltrue(dev, events, p)
993 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
997 * Record a select request.
1000 selrecord(selector, sip)
1001 struct proc *selector;
1002 struct selinfo *sip;
1007 mypid = selector->p_pid;
1008 if (sip->si_pid == mypid)
1010 if (sip->si_pid && (p = pfind(sip->si_pid)) &&
1011 p->p_wchan == (caddr_t)&selwait)
1012 sip->si_flags |= SI_COLL;
1014 sip->si_pid = mypid;
1018 * Do a wakeup when a selectable event occurs.
1022 register struct selinfo *sip;
1024 register struct proc *p;
1027 if (sip->si_pid == 0)
1029 if (sip->si_flags & SI_COLL) {
1031 sip->si_flags &= ~SI_COLL;
1032 wakeup((caddr_t)&selwait);
1034 p = pfind(sip->si_pid);
1038 if (p->p_wchan == (caddr_t)&selwait) {
1039 if (p->p_stat == SSLEEP)
1043 } else if (p->p_flag & P_SELECT)
1044 p->p_flag &= ~P_SELECT;