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.7 2003/07/24 01:41:25 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) {
103 read(struct read_args *uap)
105 struct thread *td = curthread;
106 struct proc *p = td->td_proc;
111 if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) == NULL)
113 error = dofileread(fp, uap->fd, uap->buf, uap->nbyte, (off_t)-1, 0);
122 pread(struct pread_args *uap)
124 struct thread *td = curthread;
125 struct proc *p = td->td_proc;
130 if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) == NULL)
132 if (fp->f_type != DTYPE_VNODE) {
135 error = dofileread(fp, uap->fd, uap->buf, uap->nbyte,
136 uap->offset, FOF_OFFSET);
143 * Code common for read and pread
146 dofileread(fp, fd, buf, nbyte, offset, flags)
153 struct thread *td = curthread;
154 struct proc *p = td->td_proc;
164 aiov.iov_base = (caddr_t)buf;
165 aiov.iov_len = nbyte;
166 auio.uio_iov = &aiov;
168 auio.uio_offset = offset;
171 auio.uio_resid = nbyte;
172 auio.uio_rw = UIO_READ;
173 auio.uio_segflg = UIO_USERSPACE;
177 * if tracing, save a copy of iovec
179 if (KTRPOINT(td, KTR_GENIO)) {
187 if ((error = fo_read(fp, &auio, fp->f_cred, flags, td))) {
188 if (auio.uio_resid != cnt && (error == ERESTART ||
189 error == EINTR || error == EWOULDBLOCK))
192 cnt -= auio.uio_resid;
194 if (didktr && error == 0) {
195 ktruio.uio_iov = &ktriov;
196 ktruio.uio_resid = cnt;
197 ktrgenio(p->p_tracep, fd, UIO_READ, &ktruio, error);
200 p->p_retval[0] = cnt;
205 * Scatter read system call.
208 readv(struct readv_args *uap)
210 struct thread *td = curthread;
211 struct proc *p = td->td_proc;
213 struct filedesc *fdp = p->p_fd;
216 struct iovec *needfree;
217 struct iovec aiov[UIO_SMALLIOV];
218 long i, cnt, error = 0;
221 struct iovec *ktriov = NULL;
225 if ((fp = holdfp(fdp, uap->fd, FREAD)) == NULL)
227 /* note: can't use iovlen until iovcnt is validated */
228 iovlen = uap->iovcnt * sizeof (struct iovec);
229 if (uap->iovcnt > UIO_SMALLIOV) {
230 if (uap->iovcnt > UIO_MAXIOV)
232 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
239 auio.uio_iovcnt = uap->iovcnt;
240 auio.uio_rw = UIO_READ;
241 auio.uio_segflg = UIO_USERSPACE;
243 auio.uio_offset = -1;
244 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
247 for (i = 0; i < uap->iovcnt; i++) {
248 if (iov->iov_len > INT_MAX - auio.uio_resid) {
252 auio.uio_resid += iov->iov_len;
257 * if tracing, save a copy of iovec
259 if (KTRPOINT(td, KTR_GENIO)) {
260 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
261 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
265 cnt = auio.uio_resid;
266 if ((error = fo_read(fp, &auio, fp->f_cred, 0, td))) {
267 if (auio.uio_resid != cnt && (error == ERESTART ||
268 error == EINTR || error == EWOULDBLOCK))
271 cnt -= auio.uio_resid;
273 if (ktriov != NULL) {
275 ktruio.uio_iov = ktriov;
276 ktruio.uio_resid = cnt;
277 ktrgenio(p->p_tracep, uap->fd, UIO_READ, &ktruio,
280 FREE(ktriov, M_TEMP);
283 p->p_retval[0] = cnt;
287 FREE(needfree, M_IOV);
295 write(struct write_args *uap)
297 struct thread *td = curthread;
298 struct proc *p = td->td_proc;
304 if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL)
306 error = dofilewrite(fp, uap->fd, uap->buf, uap->nbyte, (off_t)-1, 0);
315 pwrite(struct pwrite_args *uap)
317 struct thread *td = curthread;
318 struct proc *p = td->td_proc;
323 if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL)
325 if (fp->f_type != DTYPE_VNODE) {
328 error = dofilewrite(fp, uap->fd, uap->buf, uap->nbyte,
329 uap->offset, FOF_OFFSET);
344 struct thread *td = curthread;
345 struct proc *p = td->td_proc;
355 aiov.iov_base = (void *)(uintptr_t)buf;
356 aiov.iov_len = nbyte;
357 auio.uio_iov = &aiov;
359 auio.uio_offset = offset;
362 auio.uio_resid = nbyte;
363 auio.uio_rw = UIO_WRITE;
364 auio.uio_segflg = UIO_USERSPACE;
368 * if tracing, save a copy of iovec and uio
370 if (KTRPOINT(td, KTR_GENIO)) {
377 if (fp->f_type == DTYPE_VNODE)
379 if ((error = fo_write(fp, &auio, fp->f_cred, flags, td))) {
380 if (auio.uio_resid != cnt && (error == ERESTART ||
381 error == EINTR || error == EWOULDBLOCK))
386 cnt -= auio.uio_resid;
388 if (didktr && error == 0) {
389 ktruio.uio_iov = &ktriov;
390 ktruio.uio_resid = cnt;
391 ktrgenio(p->p_tracep, fd, UIO_WRITE, &ktruio, error);
394 p->p_retval[0] = cnt;
399 * Gather write system call
402 writev(struct writev_args *uap)
404 struct thread *td = curthread;
405 struct proc *p = td->td_proc;
407 struct filedesc *fdp;
410 struct iovec *needfree;
411 struct iovec aiov[UIO_SMALLIOV];
412 long i, cnt, error = 0;
415 struct iovec *ktriov = NULL;
422 if ((fp = holdfp(fdp, uap->fd, FWRITE)) == NULL)
424 /* note: can't use iovlen until iovcnt is validated */
425 iovlen = uap->iovcnt * sizeof (struct iovec);
426 if (uap->iovcnt > UIO_SMALLIOV) {
427 if (uap->iovcnt > UIO_MAXIOV) {
432 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
439 auio.uio_iovcnt = uap->iovcnt;
440 auio.uio_rw = UIO_WRITE;
441 auio.uio_segflg = UIO_USERSPACE;
443 auio.uio_offset = -1;
444 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
447 for (i = 0; i < uap->iovcnt; i++) {
448 if (iov->iov_len > INT_MAX - auio.uio_resid) {
452 auio.uio_resid += iov->iov_len;
457 * if tracing, save a copy of iovec and uio
459 if (KTRPOINT(td, KTR_GENIO)) {
460 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
461 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
465 cnt = auio.uio_resid;
466 if (fp->f_type == DTYPE_VNODE)
468 if ((error = fo_write(fp, &auio, fp->f_cred, 0, td))) {
469 if (auio.uio_resid != cnt && (error == ERESTART ||
470 error == EINTR || error == EWOULDBLOCK))
475 cnt -= auio.uio_resid;
477 if (ktriov != NULL) {
479 ktruio.uio_iov = ktriov;
480 ktruio.uio_resid = cnt;
481 ktrgenio(p->p_tracep, uap->fd, UIO_WRITE, &ktruio,
484 FREE(ktriov, M_TEMP);
487 p->p_retval[0] = cnt;
491 FREE(needfree, M_IOV);
500 ioctl(struct ioctl_args *uap)
502 struct thread *td = curthread;
503 struct proc *p = td->td_proc;
505 struct filedesc *fdp;
511 #define STK_PARAMS 128
513 char stkbuf[STK_PARAMS];
519 if ((u_int)uap->fd >= fdp->fd_nfiles ||
520 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
523 if ((fp->f_flag & (FREAD | FWRITE)) == 0)
526 switch (com = uap->com) {
528 fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
531 fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
536 * Interpret high order word to find amount of data to be
537 * copied to/from the user's address space.
539 size = IOCPARM_LEN(com);
540 if (size > IOCPARM_MAX)
546 if (size > sizeof (ubuf.stkbuf)) {
547 memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
554 error = copyin(uap->data, data, (u_int)size);
557 free(memp, M_IOCTLOPS);
562 *(caddr_t *)data = uap->data;
564 } else if ((com&IOC_OUT) && size) {
566 * Zero the buffer so the user always
567 * gets back something deterministic.
570 } else if (com&IOC_VOID) {
571 *(caddr_t *)data = uap->data;
577 if ((tmp = *(int *)data))
578 fp->f_flag |= FNONBLOCK;
580 fp->f_flag &= ~FNONBLOCK;
581 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td);
585 if ((tmp = *(int *)data))
586 fp->f_flag |= FASYNC;
588 fp->f_flag &= ~FASYNC;
589 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, td);
593 error = fo_ioctl(fp, com, data, td);
595 * Copy any data to user, size was
596 * already set and checked above.
598 if (error == 0 && (com&IOC_OUT) && size)
599 error = copyout(data, uap->data, (u_int)size);
603 free(memp, M_IOCTLOPS);
608 static int nselcoll; /* Select collisions since boot */
610 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
613 * Select system call.
616 select(struct select_args *uap)
618 struct proc *p = curproc;
621 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
622 * infds with the new FD_SETSIZE of 1024, and more than enough for
623 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
626 fd_mask s_selbits[howmany(2048, NFDBITS)];
627 fd_mask *ibits[3], *obits[3], *selbits, *sbp;
628 struct timeval atv, rtv, ttv;
629 int s, ncoll, error, timo;
630 u_int nbufbytes, ncpbytes, nfdbits;
634 if (uap->nd > p->p_fd->fd_nfiles)
635 uap->nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */
638 * Allocate just enough bits for the non-null fd_sets. Use the
639 * preallocated auto buffer if possible.
641 nfdbits = roundup(uap->nd, NFDBITS);
642 ncpbytes = nfdbits / NBBY;
645 nbufbytes += 2 * ncpbytes;
647 nbufbytes += 2 * ncpbytes;
649 nbufbytes += 2 * ncpbytes;
650 if (nbufbytes <= sizeof s_selbits)
651 selbits = &s_selbits[0];
653 selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
656 * Assign pointers into the bit buffers and fetch the input bits.
657 * Put the output buffers together so that they can be bzeroed
661 #define getbits(name, x) \
663 if (uap->name == NULL) \
666 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
668 sbp += ncpbytes / sizeof *sbp; \
669 error = copyin(uap->name, ibits[x], ncpbytes); \
679 bzero(selbits, nbufbytes / 2);
682 error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
686 if (itimerfix(&atv)) {
690 getmicrouptime(&rtv);
691 timevaladd(&atv, &rtv);
699 p->p_flag |= P_SELECT;
700 error = selscan(p, ibits, obits, uap->nd);
701 if (error || p->p_retval[0])
703 if (atv.tv_sec || atv.tv_usec) {
704 getmicrouptime(&rtv);
705 if (timevalcmp(&rtv, &atv, >=))
708 timevalsub(&ttv, &rtv);
709 timo = ttv.tv_sec > 24 * 60 * 60 ?
710 24 * 60 * 60 * hz : tvtohz(&ttv);
713 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
717 p->p_flag &= ~P_SELECT;
719 error = tsleep((caddr_t)&selwait, PCATCH, "select", timo);
725 p->p_flag &= ~P_SELECT;
726 /* select is not restarted after signals... */
727 if (error == ERESTART)
729 if (error == EWOULDBLOCK)
731 #define putbits(name, x) \
732 if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
742 if (selbits != &s_selbits[0])
743 free(selbits, M_SELECT);
748 selscan(struct proc *p, fd_mask **ibits, fd_mask **obits, int nfd)
750 struct thread *td = p->p_thread;
751 struct filedesc *fdp = p->p_fd;
756 /* Note: backend also returns POLLHUP/POLLERR if appropriate. */
757 static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
759 for (msk = 0; msk < 3; msk++) {
760 if (ibits[msk] == NULL)
762 for (i = 0; i < nfd; i += NFDBITS) {
763 bits = ibits[msk][i/NFDBITS];
764 /* ffs(int mask) not portable, fd_mask is long */
765 for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
768 fp = fdp->fd_ofiles[fd];
771 if (fo_poll(fp, flag[msk], fp->f_cred, td)) {
772 obits[msk][(fd)/NFDBITS] |=
773 ((fd_mask)1 << ((fd) % NFDBITS));
787 poll(struct poll_args *uap)
790 char smallbits[32 * sizeof(struct pollfd)];
791 struct timeval atv, rtv, ttv;
792 int s, ncoll, error = 0, timo;
795 struct proc *p = curproc;
797 nfds = SCARG(uap, nfds);
799 * This is kinda bogus. We have fd limits, but that is not
800 * really related to the size of the pollfd array. Make sure
801 * we let the process use at least FD_SETSIZE entries and at
802 * least enough for the current limits. We want to be reasonably
803 * safe, but not overly restrictive.
805 if (nfds > p->p_rlimit[RLIMIT_NOFILE].rlim_cur && nfds > FD_SETSIZE)
807 ni = nfds * sizeof(struct pollfd);
808 if (ni > sizeof(smallbits))
809 bits = malloc(ni, M_TEMP, M_WAITOK);
812 error = copyin(SCARG(uap, fds), bits, ni);
815 if (SCARG(uap, timeout) != INFTIM) {
816 atv.tv_sec = SCARG(uap, timeout) / 1000;
817 atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
818 if (itimerfix(&atv)) {
822 getmicrouptime(&rtv);
823 timevaladd(&atv, &rtv);
831 p->p_flag |= P_SELECT;
832 error = pollscan(p, (struct pollfd *)bits, nfds);
833 if (error || p->p_retval[0])
835 if (atv.tv_sec || atv.tv_usec) {
836 getmicrouptime(&rtv);
837 if (timevalcmp(&rtv, &atv, >=))
840 timevalsub(&ttv, &rtv);
841 timo = ttv.tv_sec > 24 * 60 * 60 ?
842 24 * 60 * 60 * hz : tvtohz(&ttv);
845 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
849 p->p_flag &= ~P_SELECT;
850 error = tsleep((caddr_t)&selwait, PCATCH, "poll", timo);
855 p->p_flag &= ~P_SELECT;
856 /* poll is not restarted after signals... */
857 if (error == ERESTART)
859 if (error == EWOULDBLOCK)
862 error = copyout(bits, SCARG(uap, fds), ni);
867 if (ni > sizeof(smallbits))
873 pollscan(struct proc *p, struct pollfd *fds, u_int nfd)
875 struct thread *td = p->p_thread;
876 struct filedesc *fdp = p->p_fd;
881 for (i = 0; i < nfd; i++, fds++) {
882 if (fds->fd >= fdp->fd_nfiles) {
883 fds->revents = POLLNVAL;
885 } else if (fds->fd < 0) {
888 fp = fdp->fd_ofiles[fds->fd];
890 fds->revents = POLLNVAL;
894 * Note: backend also returns POLLHUP and
895 * POLLERR if appropriate.
897 fds->revents = fo_poll(fp, fds->events,
899 if (fds->revents != 0)
909 * OpenBSD poll system call.
910 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
913 openbsd_poll(struct openbsd_poll_args *uap)
915 return (poll((struct poll_args *)uap));
920 seltrue(dev_t dev, int events, struct thread *td)
922 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
926 * Record a select request. A global wait must be used since a process/thread
927 * might go away after recording its request.
930 selrecord(struct thread *selector, struct selinfo *sip)
935 if ((p = selector->td_proc) == NULL)
936 panic("selrecord: thread needs a process");
939 if (sip->si_pid == mypid)
941 if (sip->si_pid && (p = pfind(sip->si_pid)) &&
942 p->p_wchan == (caddr_t)&selwait) {
943 sip->si_flags |= SI_COLL;
950 * Do a wakeup when a selectable event occurs.
953 selwakeup(struct selinfo *sip)
958 if (sip->si_pid == 0)
960 if (sip->si_flags & SI_COLL) {
962 sip->si_flags &= ~SI_COLL;
963 wakeup((caddr_t)&selwait); /* YYY fixable */
965 p = pfind(sip->si_pid);
969 if (p->p_wchan == (caddr_t)&selwait) {
970 if (p->p_stat == SSLEEP)
973 unsleep(p->p_thread);
974 } else if (p->p_flag & P_SELECT)
975 p->p_flag &= ~P_SELECT;