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.4 2003/06/25 03:55:57 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>
68 #include <sys/file2.h>
70 #include <machine/limits.h>
72 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
73 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
74 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
76 static int pollscan __P((struct proc *, struct pollfd *, u_int));
77 static int selscan __P((struct proc *, fd_mask **, fd_mask **, int));
78 static int dofileread __P((struct file *, int, void *,
80 static int dofilewrite __P((struct file *, int,
81 const void *, size_t, off_t, int));
90 if (((u_int)fd) >= fdp->fd_nfiles ||
91 (fp = fdp->fd_ofiles[fd]) == NULL ||
92 (fp->f_flag & flag) == 0) {
102 #ifndef _SYS_SYSPROTO_H_
110 read(struct read_args *uap)
112 struct thread *td = curthread;
113 struct proc *p = td->td_proc;
118 if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) == NULL)
120 error = dofileread(fp, uap->fd, uap->buf, uap->nbyte, (off_t)-1, 0);
128 #ifndef _SYS_SYSPROTO_H_
138 pread(struct pread_args *uap)
140 struct thread *td = curthread;
141 struct proc *p = td->td_proc;
146 if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) == NULL)
148 if (fp->f_type != DTYPE_VNODE) {
151 error = dofileread(fp, uap->fd, uap->buf, uap->nbyte,
152 uap->offset, FOF_OFFSET);
159 * Code common for read and pread
162 dofileread(fp, fd, buf, nbyte, offset, flags)
169 struct thread *td = curthread;
170 struct proc *p = td->td_proc;
180 aiov.iov_base = (caddr_t)buf;
181 aiov.iov_len = nbyte;
182 auio.uio_iov = &aiov;
184 auio.uio_offset = offset;
187 auio.uio_resid = nbyte;
188 auio.uio_rw = UIO_READ;
189 auio.uio_segflg = UIO_USERSPACE;
193 * if tracing, save a copy of iovec
195 if (KTRPOINT(td, KTR_GENIO)) {
203 if ((error = fo_read(fp, &auio, fp->f_cred, flags, td))) {
204 if (auio.uio_resid != cnt && (error == ERESTART ||
205 error == EINTR || error == EWOULDBLOCK))
208 cnt -= auio.uio_resid;
210 if (didktr && error == 0) {
211 ktruio.uio_iov = &ktriov;
212 ktruio.uio_resid = cnt;
213 ktrgenio(p->p_tracep, fd, UIO_READ, &ktruio, error);
216 p->p_retval[0] = cnt;
221 * Scatter read system call.
223 #ifndef _SYS_SYSPROTO_H_
231 readv(struct readv_args *uap)
233 struct thread *td = curthread;
234 struct proc *p = td->td_proc;
236 struct filedesc *fdp = p->p_fd;
239 struct iovec *needfree;
240 struct iovec aiov[UIO_SMALLIOV];
241 long i, cnt, error = 0;
244 struct iovec *ktriov = NULL;
248 if ((fp = holdfp(fdp, uap->fd, FREAD)) == NULL)
250 /* note: can't use iovlen until iovcnt is validated */
251 iovlen = uap->iovcnt * sizeof (struct iovec);
252 if (uap->iovcnt > UIO_SMALLIOV) {
253 if (uap->iovcnt > UIO_MAXIOV)
255 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
262 auio.uio_iovcnt = uap->iovcnt;
263 auio.uio_rw = UIO_READ;
264 auio.uio_segflg = UIO_USERSPACE;
266 auio.uio_offset = -1;
267 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
270 for (i = 0; i < uap->iovcnt; i++) {
271 if (iov->iov_len > INT_MAX - auio.uio_resid) {
275 auio.uio_resid += iov->iov_len;
280 * if tracing, save a copy of iovec
282 if (KTRPOINT(td, KTR_GENIO)) {
283 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
284 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
288 cnt = auio.uio_resid;
289 if ((error = fo_read(fp, &auio, fp->f_cred, 0, td))) {
290 if (auio.uio_resid != cnt && (error == ERESTART ||
291 error == EINTR || error == EWOULDBLOCK))
294 cnt -= auio.uio_resid;
296 if (ktriov != NULL) {
298 ktruio.uio_iov = ktriov;
299 ktruio.uio_resid = cnt;
300 ktrgenio(p->p_tracep, uap->fd, UIO_READ, &ktruio,
303 FREE(ktriov, M_TEMP);
306 p->p_retval[0] = cnt;
310 FREE(needfree, M_IOV);
317 #ifndef _SYS_SYSPROTO_H_
325 write(struct write_args *uap)
327 struct thread *td = curthread;
328 struct proc *p = td->td_proc;
334 if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL)
336 error = dofilewrite(fp, uap->fd, uap->buf, uap->nbyte, (off_t)-1, 0);
344 #ifndef _SYS_SYSPROTO_H_
354 pwrite(struct pwrite_args *uap)
356 struct thread *td = curthread;
357 struct proc *p = td->td_proc;
362 if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL)
364 if (fp->f_type != DTYPE_VNODE) {
367 error = dofilewrite(fp, uap->fd, uap->buf, uap->nbyte,
368 uap->offset, FOF_OFFSET);
383 struct thread *td = curthread;
384 struct proc *p = td->td_proc;
394 aiov.iov_base = (void *)(uintptr_t)buf;
395 aiov.iov_len = nbyte;
396 auio.uio_iov = &aiov;
398 auio.uio_offset = offset;
401 auio.uio_resid = nbyte;
402 auio.uio_rw = UIO_WRITE;
403 auio.uio_segflg = UIO_USERSPACE;
407 * if tracing, save a copy of iovec and uio
409 if (KTRPOINT(td, KTR_GENIO)) {
416 if (fp->f_type == DTYPE_VNODE)
418 if ((error = fo_write(fp, &auio, fp->f_cred, flags, td))) {
419 if (auio.uio_resid != cnt && (error == ERESTART ||
420 error == EINTR || error == EWOULDBLOCK))
425 cnt -= auio.uio_resid;
427 if (didktr && error == 0) {
428 ktruio.uio_iov = &ktriov;
429 ktruio.uio_resid = cnt;
430 ktrgenio(p->p_tracep, fd, UIO_WRITE, &ktruio, error);
433 p->p_retval[0] = cnt;
438 * Gather write system call
440 #ifndef _SYS_SYSPROTO_H_
448 writev(struct writev_args *uap)
450 struct thread *td = curthread;
451 struct proc *p = td->td_proc;
453 struct filedesc *fdp;
456 struct iovec *needfree;
457 struct iovec aiov[UIO_SMALLIOV];
458 long i, cnt, error = 0;
461 struct iovec *ktriov = NULL;
468 if ((fp = holdfp(fdp, uap->fd, FWRITE)) == NULL)
470 /* note: can't use iovlen until iovcnt is validated */
471 iovlen = uap->iovcnt * sizeof (struct iovec);
472 if (uap->iovcnt > UIO_SMALLIOV) {
473 if (uap->iovcnt > UIO_MAXIOV) {
478 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
485 auio.uio_iovcnt = uap->iovcnt;
486 auio.uio_rw = UIO_WRITE;
487 auio.uio_segflg = UIO_USERSPACE;
489 auio.uio_offset = -1;
490 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
493 for (i = 0; i < uap->iovcnt; i++) {
494 if (iov->iov_len > INT_MAX - auio.uio_resid) {
498 auio.uio_resid += iov->iov_len;
503 * if tracing, save a copy of iovec and uio
505 if (KTRPOINT(td, KTR_GENIO)) {
506 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
507 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
511 cnt = auio.uio_resid;
512 if (fp->f_type == DTYPE_VNODE)
514 if ((error = fo_write(fp, &auio, fp->f_cred, 0, td))) {
515 if (auio.uio_resid != cnt && (error == ERESTART ||
516 error == EINTR || error == EWOULDBLOCK))
521 cnt -= auio.uio_resid;
523 if (ktriov != NULL) {
525 ktruio.uio_iov = ktriov;
526 ktruio.uio_resid = cnt;
527 ktrgenio(p->p_tracep, uap->fd, UIO_WRITE, &ktruio,
530 FREE(ktriov, M_TEMP);
533 p->p_retval[0] = cnt;
537 FREE(needfree, M_IOV);
544 #ifndef _SYS_SYSPROTO_H_
553 ioctl(struct ioctl_args *uap)
555 struct thread *td = curthread;
556 struct proc *p = td->td_proc;
558 struct filedesc *fdp;
564 #define STK_PARAMS 128
566 char stkbuf[STK_PARAMS];
572 if ((u_int)uap->fd >= fdp->fd_nfiles ||
573 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
576 if ((fp->f_flag & (FREAD | FWRITE)) == 0)
579 switch (com = uap->com) {
581 fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
584 fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
589 * Interpret high order word to find amount of data to be
590 * copied to/from the user's address space.
592 size = IOCPARM_LEN(com);
593 if (size > IOCPARM_MAX)
599 if (size > sizeof (ubuf.stkbuf)) {
600 memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
607 error = copyin(uap->data, data, (u_int)size);
610 free(memp, M_IOCTLOPS);
615 *(caddr_t *)data = uap->data;
617 } else if ((com&IOC_OUT) && size) {
619 * Zero the buffer so the user always
620 * gets back something deterministic.
623 } else if (com&IOC_VOID) {
624 *(caddr_t *)data = uap->data;
630 if ((tmp = *(int *)data))
631 fp->f_flag |= FNONBLOCK;
633 fp->f_flag &= ~FNONBLOCK;
634 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td);
638 if ((tmp = *(int *)data))
639 fp->f_flag |= FASYNC;
641 fp->f_flag &= ~FASYNC;
642 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, td);
646 error = fo_ioctl(fp, com, data, td);
648 * Copy any data to user, size was
649 * already set and checked above.
651 if (error == 0 && (com&IOC_OUT) && size)
652 error = copyout(data, uap->data, (u_int)size);
656 free(memp, M_IOCTLOPS);
661 static int nselcoll; /* Select collisions since boot */
663 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
666 * Select system call.
668 #ifndef _SYS_SYSPROTO_H_
671 fd_set *in, *ou, *ex;
676 select(struct select_args *uap)
678 struct proc *p = curproc;
681 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
682 * infds with the new FD_SETSIZE of 1024, and more than enough for
683 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
686 fd_mask s_selbits[howmany(2048, NFDBITS)];
687 fd_mask *ibits[3], *obits[3], *selbits, *sbp;
688 struct timeval atv, rtv, ttv;
689 int s, ncoll, error, timo;
690 u_int nbufbytes, ncpbytes, nfdbits;
694 if (uap->nd > p->p_fd->fd_nfiles)
695 uap->nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */
698 * Allocate just enough bits for the non-null fd_sets. Use the
699 * preallocated auto buffer if possible.
701 nfdbits = roundup(uap->nd, NFDBITS);
702 ncpbytes = nfdbits / NBBY;
705 nbufbytes += 2 * ncpbytes;
707 nbufbytes += 2 * ncpbytes;
709 nbufbytes += 2 * ncpbytes;
710 if (nbufbytes <= sizeof s_selbits)
711 selbits = &s_selbits[0];
713 selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
716 * Assign pointers into the bit buffers and fetch the input bits.
717 * Put the output buffers together so that they can be bzeroed
721 #define getbits(name, x) \
723 if (uap->name == NULL) \
726 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
728 sbp += ncpbytes / sizeof *sbp; \
729 error = copyin(uap->name, ibits[x], ncpbytes); \
739 bzero(selbits, nbufbytes / 2);
742 error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
746 if (itimerfix(&atv)) {
750 getmicrouptime(&rtv);
751 timevaladd(&atv, &rtv);
759 p->p_flag |= P_SELECT;
760 error = selscan(p, ibits, obits, uap->nd);
761 if (error || p->p_retval[0])
763 if (atv.tv_sec || atv.tv_usec) {
764 getmicrouptime(&rtv);
765 if (timevalcmp(&rtv, &atv, >=))
768 timevalsub(&ttv, &rtv);
769 timo = ttv.tv_sec > 24 * 60 * 60 ?
770 24 * 60 * 60 * hz : tvtohz(&ttv);
773 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
777 p->p_flag &= ~P_SELECT;
779 error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
785 p->p_flag &= ~P_SELECT;
786 /* select is not restarted after signals... */
787 if (error == ERESTART)
789 if (error == EWOULDBLOCK)
791 #define putbits(name, x) \
792 if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
802 if (selbits != &s_selbits[0])
803 free(selbits, M_SELECT);
808 selscan(struct proc *p, fd_mask **ibits, fd_mask **obits, int nfd)
810 struct thread *td = p->p_thread;
811 struct filedesc *fdp = p->p_fd;
816 /* Note: backend also returns POLLHUP/POLLERR if appropriate. */
817 static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
819 for (msk = 0; msk < 3; msk++) {
820 if (ibits[msk] == NULL)
822 for (i = 0; i < nfd; i += NFDBITS) {
823 bits = ibits[msk][i/NFDBITS];
824 /* ffs(int mask) not portable, fd_mask is long */
825 for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
828 fp = fdp->fd_ofiles[fd];
831 if (fo_poll(fp, flag[msk], fp->f_cred, td)) {
832 obits[msk][(fd)/NFDBITS] |=
833 ((fd_mask)1 << ((fd) % NFDBITS));
846 #ifndef _SYS_SYSPROTO_H_
854 poll(struct poll_args *uap)
857 char smallbits[32 * sizeof(struct pollfd)];
858 struct timeval atv, rtv, ttv;
859 int s, ncoll, error = 0, timo;
862 struct proc *p = curproc;
864 nfds = SCARG(uap, nfds);
866 * This is kinda bogus. We have fd limits, but that is not
867 * really related to the size of the pollfd array. Make sure
868 * we let the process use at least FD_SETSIZE entries and at
869 * least enough for the current limits. We want to be reasonably
870 * safe, but not overly restrictive.
872 if (nfds > p->p_rlimit[RLIMIT_NOFILE].rlim_cur && nfds > FD_SETSIZE)
874 ni = nfds * sizeof(struct pollfd);
875 if (ni > sizeof(smallbits))
876 bits = malloc(ni, M_TEMP, M_WAITOK);
879 error = copyin(SCARG(uap, fds), bits, ni);
882 if (SCARG(uap, timeout) != INFTIM) {
883 atv.tv_sec = SCARG(uap, timeout) / 1000;
884 atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
885 if (itimerfix(&atv)) {
889 getmicrouptime(&rtv);
890 timevaladd(&atv, &rtv);
898 p->p_flag |= P_SELECT;
899 error = pollscan(p, (struct pollfd *)bits, nfds);
900 if (error || p->p_retval[0])
902 if (atv.tv_sec || atv.tv_usec) {
903 getmicrouptime(&rtv);
904 if (timevalcmp(&rtv, &atv, >=))
907 timevalsub(&ttv, &rtv);
908 timo = ttv.tv_sec > 24 * 60 * 60 ?
909 24 * 60 * 60 * hz : tvtohz(&ttv);
912 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
916 p->p_flag &= ~P_SELECT;
917 error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "poll", timo);
922 p->p_flag &= ~P_SELECT;
923 /* poll is not restarted after signals... */
924 if (error == ERESTART)
926 if (error == EWOULDBLOCK)
929 error = copyout(bits, SCARG(uap, fds), ni);
934 if (ni > sizeof(smallbits))
940 pollscan(struct proc *p, struct pollfd *fds, u_int nfd)
942 struct thread *td = p->p_thread;
943 struct filedesc *fdp = p->p_fd;
948 for (i = 0; i < nfd; i++, fds++) {
949 if (fds->fd >= fdp->fd_nfiles) {
950 fds->revents = POLLNVAL;
952 } else if (fds->fd < 0) {
955 fp = fdp->fd_ofiles[fds->fd];
957 fds->revents = POLLNVAL;
961 * Note: backend also returns POLLHUP and
962 * POLLERR if appropriate.
964 fds->revents = fo_poll(fp, fds->events,
966 if (fds->revents != 0)
976 * OpenBSD poll system call.
977 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
979 #ifndef _SYS_SYSPROTO_H_
980 struct openbsd_poll_args {
987 openbsd_poll(struct openbsd_poll_args *uap)
989 return (poll((struct poll_args *)uap));
994 seltrue(dev_t dev, int events, struct thread *td)
996 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1000 * Record a select request. A global wait must be used since a process/thread
1001 * might go away after recording its request.
1004 selrecord(struct thread *selector, struct selinfo *sip)
1009 if ((p = selector->td_proc) == NULL)
1010 panic("selrecord: thread needs a process");
1013 if (sip->si_pid == mypid)
1015 if (sip->si_pid && (p = pfind(sip->si_pid)) &&
1016 p->p_wchan == (caddr_t)&selwait) {
1017 sip->si_flags |= SI_COLL;
1019 sip->si_pid = mypid;
1024 * Do a wakeup when a selectable event occurs.
1027 selwakeup(struct selinfo *sip)
1032 if (sip->si_pid == 0)
1034 if (sip->si_flags & SI_COLL) {
1036 sip->si_flags &= ~SI_COLL;
1037 wakeup((caddr_t)&selwait); /* YYY fixable */
1039 p = pfind(sip->si_pid);
1043 if (p->p_wchan == (caddr_t)&selwait) {
1044 if (p->p_stat == SSLEEP)
1048 } else if (p->p_flag & P_SELECT)
1049 p->p_flag &= ~P_SELECT;