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.3 2003/06/23 17:55: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 file *, int, void *,
79 static int dofilewrite __P((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_
109 read(struct read_args *uap)
111 struct proc *p = curproc;
115 if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) == NULL)
117 error = dofileread(fp, uap->fd, uap->buf, uap->nbyte, (off_t)-1, 0);
125 #ifndef _SYS_SYSPROTO_H_
135 pread(struct pread_args *uap)
137 struct proc *p = curproc;
141 if ((fp = holdfp(p->p_fd, uap->fd, FREAD)) == NULL)
143 if (fp->f_type != DTYPE_VNODE) {
146 error = dofileread(fp, uap->fd, uap->buf, uap->nbyte,
147 uap->offset, FOF_OFFSET);
154 * Code common for read and pread
157 dofileread(fp, fd, buf, nbyte, offset, flags)
164 struct proc *p = curproc;
174 aiov.iov_base = (caddr_t)buf;
175 aiov.iov_len = nbyte;
176 auio.uio_iov = &aiov;
178 auio.uio_offset = offset;
181 auio.uio_resid = nbyte;
182 auio.uio_rw = UIO_READ;
183 auio.uio_segflg = UIO_USERSPACE;
187 * if tracing, save a copy of iovec
189 if (KTRPOINT(p, KTR_GENIO)) {
197 if ((error = fo_read(fp, &auio, fp->f_cred, flags, p))) {
198 if (auio.uio_resid != cnt && (error == ERESTART ||
199 error == EINTR || error == EWOULDBLOCK))
202 cnt -= auio.uio_resid;
204 if (didktr && error == 0) {
205 ktruio.uio_iov = &ktriov;
206 ktruio.uio_resid = cnt;
207 ktrgenio(p->p_tracep, fd, UIO_READ, &ktruio, error);
210 p->p_retval[0] = cnt;
215 * Scatter read system call.
217 #ifndef _SYS_SYSPROTO_H_
225 readv(struct readv_args *uap)
227 struct proc *p = curproc;
229 struct filedesc *fdp = p->p_fd;
232 struct iovec *needfree;
233 struct iovec aiov[UIO_SMALLIOV];
234 long i, cnt, error = 0;
237 struct iovec *ktriov = NULL;
241 if ((fp = holdfp(fdp, uap->fd, FREAD)) == NULL)
243 /* note: can't use iovlen until iovcnt is validated */
244 iovlen = uap->iovcnt * sizeof (struct iovec);
245 if (uap->iovcnt > UIO_SMALLIOV) {
246 if (uap->iovcnt > UIO_MAXIOV)
248 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
255 auio.uio_iovcnt = uap->iovcnt;
256 auio.uio_rw = UIO_READ;
257 auio.uio_segflg = UIO_USERSPACE;
259 auio.uio_offset = -1;
260 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
263 for (i = 0; i < uap->iovcnt; i++) {
264 if (iov->iov_len > INT_MAX - auio.uio_resid) {
268 auio.uio_resid += iov->iov_len;
273 * if tracing, save a copy of iovec
275 if (KTRPOINT(p, KTR_GENIO)) {
276 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
277 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
281 cnt = auio.uio_resid;
282 if ((error = fo_read(fp, &auio, fp->f_cred, 0, p))) {
283 if (auio.uio_resid != cnt && (error == ERESTART ||
284 error == EINTR || error == EWOULDBLOCK))
287 cnt -= auio.uio_resid;
289 if (ktriov != NULL) {
291 ktruio.uio_iov = ktriov;
292 ktruio.uio_resid = cnt;
293 ktrgenio(p->p_tracep, uap->fd, UIO_READ, &ktruio,
296 FREE(ktriov, M_TEMP);
299 p->p_retval[0] = cnt;
303 FREE(needfree, M_IOV);
310 #ifndef _SYS_SYSPROTO_H_
318 write(struct write_args *uap)
320 struct proc *p = curproc;
324 if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL)
326 error = dofilewrite(fp, uap->fd, uap->buf, uap->nbyte, (off_t)-1, 0);
334 #ifndef _SYS_SYSPROTO_H_
344 pwrite(struct pwrite_args *uap)
346 struct proc *p = curproc;
350 if ((fp = holdfp(p->p_fd, uap->fd, FWRITE)) == NULL)
352 if (fp->f_type != DTYPE_VNODE) {
355 error = dofilewrite(fp, uap->fd, uap->buf, uap->nbyte,
356 uap->offset, FOF_OFFSET);
371 struct proc *p = curproc;
381 aiov.iov_base = (void *)(uintptr_t)buf;
382 aiov.iov_len = nbyte;
383 auio.uio_iov = &aiov;
385 auio.uio_offset = offset;
388 auio.uio_resid = nbyte;
389 auio.uio_rw = UIO_WRITE;
390 auio.uio_segflg = UIO_USERSPACE;
394 * if tracing, save a copy of iovec and uio
396 if (KTRPOINT(p, KTR_GENIO)) {
403 if (fp->f_type == DTYPE_VNODE)
405 if ((error = fo_write(fp, &auio, fp->f_cred, flags, p))) {
406 if (auio.uio_resid != cnt && (error == ERESTART ||
407 error == EINTR || error == EWOULDBLOCK))
412 cnt -= auio.uio_resid;
414 if (didktr && error == 0) {
415 ktruio.uio_iov = &ktriov;
416 ktruio.uio_resid = cnt;
417 ktrgenio(p->p_tracep, fd, UIO_WRITE, &ktruio, error);
420 p->p_retval[0] = cnt;
425 * Gather write system call
427 #ifndef _SYS_SYSPROTO_H_
435 writev(struct writev_args *uap)
437 struct proc *p = curproc;
439 struct filedesc *fdp = p->p_fd;
442 struct iovec *needfree;
443 struct iovec aiov[UIO_SMALLIOV];
444 long i, cnt, error = 0;
447 struct iovec *ktriov = NULL;
451 if ((fp = holdfp(fdp, uap->fd, FWRITE)) == NULL)
453 /* note: can't use iovlen until iovcnt is validated */
454 iovlen = uap->iovcnt * sizeof (struct iovec);
455 if (uap->iovcnt > UIO_SMALLIOV) {
456 if (uap->iovcnt > UIO_MAXIOV) {
461 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
468 auio.uio_iovcnt = uap->iovcnt;
469 auio.uio_rw = UIO_WRITE;
470 auio.uio_segflg = UIO_USERSPACE;
472 auio.uio_offset = -1;
473 if ((error = copyin((caddr_t)uap->iovp, (caddr_t)iov, iovlen)))
476 for (i = 0; i < uap->iovcnt; i++) {
477 if (iov->iov_len > INT_MAX - auio.uio_resid) {
481 auio.uio_resid += iov->iov_len;
486 * if tracing, save a copy of iovec and uio
488 if (KTRPOINT(p, KTR_GENIO)) {
489 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
490 bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
494 cnt = auio.uio_resid;
495 if (fp->f_type == DTYPE_VNODE)
497 if ((error = fo_write(fp, &auio, fp->f_cred, 0, p))) {
498 if (auio.uio_resid != cnt && (error == ERESTART ||
499 error == EINTR || error == EWOULDBLOCK))
504 cnt -= auio.uio_resid;
506 if (ktriov != NULL) {
508 ktruio.uio_iov = ktriov;
509 ktruio.uio_resid = cnt;
510 ktrgenio(p->p_tracep, uap->fd, UIO_WRITE, &ktruio,
513 FREE(ktriov, M_TEMP);
516 p->p_retval[0] = cnt;
520 FREE(needfree, M_IOV);
527 #ifndef _SYS_SYSPROTO_H_
536 ioctl(struct ioctl_args *uap)
538 struct proc *p = curproc;
540 struct filedesc *fdp;
546 #define STK_PARAMS 128
548 char stkbuf[STK_PARAMS];
553 if ((u_int)uap->fd >= fdp->fd_nfiles ||
554 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
557 if ((fp->f_flag & (FREAD | FWRITE)) == 0)
560 switch (com = uap->com) {
562 fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
565 fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
570 * Interpret high order word to find amount of data to be
571 * copied to/from the user's address space.
573 size = IOCPARM_LEN(com);
574 if (size > IOCPARM_MAX)
580 if (size > sizeof (ubuf.stkbuf)) {
581 memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
588 error = copyin(uap->data, data, (u_int)size);
591 free(memp, M_IOCTLOPS);
596 *(caddr_t *)data = uap->data;
598 } else if ((com&IOC_OUT) && size) {
600 * Zero the buffer so the user always
601 * gets back something deterministic.
604 } else if (com&IOC_VOID) {
605 *(caddr_t *)data = uap->data;
611 if ((tmp = *(int *)data))
612 fp->f_flag |= FNONBLOCK;
614 fp->f_flag &= ~FNONBLOCK;
615 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
619 if ((tmp = *(int *)data))
620 fp->f_flag |= FASYNC;
622 fp->f_flag &= ~FASYNC;
623 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, p);
627 error = fo_ioctl(fp, com, data, p);
629 * Copy any data to user, size was
630 * already set and checked above.
632 if (error == 0 && (com&IOC_OUT) && size)
633 error = copyout(data, uap->data, (u_int)size);
637 free(memp, M_IOCTLOPS);
642 static int nselcoll; /* Select collisions since boot */
644 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
647 * Select system call.
649 #ifndef _SYS_SYSPROTO_H_
652 fd_set *in, *ou, *ex;
657 select(struct select_args *uap)
659 struct proc *p = curproc;
662 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
663 * infds with the new FD_SETSIZE of 1024, and more than enough for
664 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
667 fd_mask s_selbits[howmany(2048, NFDBITS)];
668 fd_mask *ibits[3], *obits[3], *selbits, *sbp;
669 struct timeval atv, rtv, ttv;
670 int s, ncoll, error, timo;
671 u_int nbufbytes, ncpbytes, nfdbits;
675 if (uap->nd > p->p_fd->fd_nfiles)
676 uap->nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */
679 * Allocate just enough bits for the non-null fd_sets. Use the
680 * preallocated auto buffer if possible.
682 nfdbits = roundup(uap->nd, NFDBITS);
683 ncpbytes = nfdbits / NBBY;
686 nbufbytes += 2 * ncpbytes;
688 nbufbytes += 2 * ncpbytes;
690 nbufbytes += 2 * ncpbytes;
691 if (nbufbytes <= sizeof s_selbits)
692 selbits = &s_selbits[0];
694 selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
697 * Assign pointers into the bit buffers and fetch the input bits.
698 * Put the output buffers together so that they can be bzeroed
702 #define getbits(name, x) \
704 if (uap->name == NULL) \
707 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
709 sbp += ncpbytes / sizeof *sbp; \
710 error = copyin(uap->name, ibits[x], ncpbytes); \
720 bzero(selbits, nbufbytes / 2);
723 error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
727 if (itimerfix(&atv)) {
731 getmicrouptime(&rtv);
732 timevaladd(&atv, &rtv);
740 p->p_flag |= P_SELECT;
741 error = selscan(p, ibits, obits, uap->nd);
742 if (error || p->p_retval[0])
744 if (atv.tv_sec || atv.tv_usec) {
745 getmicrouptime(&rtv);
746 if (timevalcmp(&rtv, &atv, >=))
749 timevalsub(&ttv, &rtv);
750 timo = ttv.tv_sec > 24 * 60 * 60 ?
751 24 * 60 * 60 * hz : tvtohz(&ttv);
754 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
758 p->p_flag &= ~P_SELECT;
760 error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
766 p->p_flag &= ~P_SELECT;
767 /* select is not restarted after signals... */
768 if (error == ERESTART)
770 if (error == EWOULDBLOCK)
772 #define putbits(name, x) \
773 if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
783 if (selbits != &s_selbits[0])
784 free(selbits, M_SELECT);
789 selscan(p, ibits, obits, nfd)
791 fd_mask **ibits, **obits;
794 struct filedesc *fdp = p->p_fd;
799 /* Note: backend also returns POLLHUP/POLLERR if appropriate. */
800 static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
802 for (msk = 0; msk < 3; msk++) {
803 if (ibits[msk] == NULL)
805 for (i = 0; i < nfd; i += NFDBITS) {
806 bits = ibits[msk][i/NFDBITS];
807 /* ffs(int mask) not portable, fd_mask is long */
808 for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
811 fp = fdp->fd_ofiles[fd];
814 if (fo_poll(fp, flag[msk], fp->f_cred, p)) {
815 obits[msk][(fd)/NFDBITS] |=
816 ((fd_mask)1 << ((fd) % NFDBITS));
829 #ifndef _SYS_SYSPROTO_H_
837 poll(struct poll_args *uap)
840 char smallbits[32 * sizeof(struct pollfd)];
841 struct timeval atv, rtv, ttv;
842 int s, ncoll, error = 0, timo;
845 struct proc *p = curproc;
847 nfds = SCARG(uap, nfds);
849 * This is kinda bogus. We have fd limits, but that is not
850 * really related to the size of the pollfd array. Make sure
851 * we let the process use at least FD_SETSIZE entries and at
852 * least enough for the current limits. We want to be reasonably
853 * safe, but not overly restrictive.
855 if (nfds > p->p_rlimit[RLIMIT_NOFILE].rlim_cur && nfds > FD_SETSIZE)
857 ni = nfds * sizeof(struct pollfd);
858 if (ni > sizeof(smallbits))
859 bits = malloc(ni, M_TEMP, M_WAITOK);
862 error = copyin(SCARG(uap, fds), bits, ni);
865 if (SCARG(uap, timeout) != INFTIM) {
866 atv.tv_sec = SCARG(uap, timeout) / 1000;
867 atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
868 if (itimerfix(&atv)) {
872 getmicrouptime(&rtv);
873 timevaladd(&atv, &rtv);
881 p->p_flag |= P_SELECT;
882 error = pollscan(p, (struct pollfd *)bits, nfds);
883 if (error || p->p_retval[0])
885 if (atv.tv_sec || atv.tv_usec) {
886 getmicrouptime(&rtv);
887 if (timevalcmp(&rtv, &atv, >=))
890 timevalsub(&ttv, &rtv);
891 timo = ttv.tv_sec > 24 * 60 * 60 ?
892 24 * 60 * 60 * hz : tvtohz(&ttv);
895 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
899 p->p_flag &= ~P_SELECT;
900 error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "poll", timo);
905 p->p_flag &= ~P_SELECT;
906 /* poll is not restarted after signals... */
907 if (error == ERESTART)
909 if (error == EWOULDBLOCK)
912 error = copyout(bits, SCARG(uap, fds), ni);
917 if (ni > sizeof(smallbits))
923 pollscan(p, fds, nfd)
928 register struct filedesc *fdp = p->p_fd;
933 for (i = 0; i < nfd; i++, fds++) {
934 if (fds->fd >= fdp->fd_nfiles) {
935 fds->revents = POLLNVAL;
937 } else if (fds->fd < 0) {
940 fp = fdp->fd_ofiles[fds->fd];
942 fds->revents = POLLNVAL;
946 * Note: backend also returns POLLHUP and
947 * POLLERR if appropriate.
949 fds->revents = fo_poll(fp, fds->events,
951 if (fds->revents != 0)
961 * OpenBSD poll system call.
962 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
964 #ifndef _SYS_SYSPROTO_H_
965 struct openbsd_poll_args {
972 openbsd_poll(struct openbsd_poll_args *uap)
974 return (poll((struct poll_args *)uap));
979 seltrue(dev_t dev, int events, struct thread *td)
981 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
985 * Record a select request. A global wait must be used since a process/thread
986 * might go away after recording its request.
989 selrecord(struct thread *selector, struct selinfo *sip)
994 if ((p = selector->td_proc) == NULL)
995 panic("selrecord: thread needs a process");
998 if (sip->si_pid == mypid)
1000 if (sip->si_pid && (p = pfind(sip->si_pid)) &&
1001 p->p_wchan == (caddr_t)&selwait) {
1002 sip->si_flags |= SI_COLL;
1004 sip->si_pid = mypid;
1009 * Do a wakeup when a selectable event occurs.
1012 selwakeup(struct selinfo *sip)
1017 if (sip->si_pid == 0)
1019 if (sip->si_flags & SI_COLL) {
1021 sip->si_flags &= ~SI_COLL;
1022 wakeup((caddr_t)&selwait); /* YYY fixable */
1024 p = pfind(sip->si_pid);
1028 if (p->p_wchan == (caddr_t)&selwait) {
1029 if (p->p_stat == SSLEEP)
1033 } else if (p->p_flag & P_SELECT)
1034 p->p_flag &= ~P_SELECT;