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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_compat.h"
41 #include "opt_ktrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sysproto.h>
46 #include <sys/filedesc.h>
47 #include <sys/filio.h>
48 #include <sys/fcntl.h>
51 #include <sys/signalvar.h>
52 #include <sys/socketvar.h>
54 #include <sys/kernel.h>
55 #include <sys/limits.h>
56 #include <sys/malloc.h>
58 #include <sys/resourcevar.h>
59 #include <sys/selinfo.h>
60 #include <sys/sleepqueue.h>
61 #include <sys/syscallsubr.h>
62 #include <sys/sysctl.h>
63 #include <sys/sysent.h>
64 #include <sys/vnode.h>
67 #include <sys/condvar.h>
69 #include <sys/ktrace.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(struct thread *, struct pollfd *, u_int);
77 static int selscan(struct thread *, fd_mask **, fd_mask **, int);
78 static int dofileread(struct thread *, int, struct file *, struct uio *,
80 static int dofilewrite(struct thread *, int, struct file *, struct uio *,
82 static void doselwakeup(struct selinfo *, int);
84 #ifndef _SYS_SYSPROTO_H_
94 struct read_args *uap;
100 if (uap->nbyte > INT_MAX)
102 aiov.iov_base = uap->buf;
103 aiov.iov_len = uap->nbyte;
104 auio.uio_iov = &aiov;
106 auio.uio_resid = uap->nbyte;
107 auio.uio_segflg = UIO_USERSPACE;
108 error = kern_readv(td, uap->fd, &auio);
113 * Positioned read system call
115 #ifndef _SYS_SYSPROTO_H_
127 struct pread_args *uap;
133 if (uap->nbyte > INT_MAX)
135 aiov.iov_base = uap->buf;
136 aiov.iov_len = uap->nbyte;
137 auio.uio_iov = &aiov;
139 auio.uio_resid = uap->nbyte;
140 auio.uio_segflg = UIO_USERSPACE;
141 error = kern_preadv(td, uap->fd, &auio, uap->offset);
146 * Scatter read system call.
148 #ifndef _SYS_SYSPROTO_H_
156 readv(struct thread *td, struct readv_args *uap)
161 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
164 error = kern_readv(td, uap->fd, auio);
170 kern_readv(struct thread *td, int fd, struct uio *auio)
175 error = fget_read(td, fd, &fp);
178 error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
184 * Scatter positioned read system call.
186 #ifndef _SYS_SYSPROTO_H_
195 preadv(struct thread *td, struct preadv_args *uap)
200 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
203 error = kern_preadv(td, uap->fd, auio, uap->offset);
209 kern_preadv(td, fd, auio, offset)
218 error = fget_read(td, fd, &fp);
221 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
223 else if (offset < 0 && fp->f_vnode->v_type != VCHR)
226 error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
232 * Common code for readv and preadv that reads data in
233 * from a file using the passed in uio, offset, and flags.
236 dofileread(td, fd, fp, auio, offset, flags)
247 struct uio *ktruio = NULL;
250 /* Finish zero length reads right here */
251 if (auio->uio_resid == 0) {
252 td->td_retval[0] = 0;
255 auio->uio_rw = UIO_READ;
256 auio->uio_offset = offset;
259 if (KTRPOINT(td, KTR_GENIO))
260 ktruio = cloneuio(auio);
262 cnt = auio->uio_resid;
263 if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
264 if (auio->uio_resid != cnt && (error == ERESTART ||
265 error == EINTR || error == EWOULDBLOCK))
268 cnt -= auio->uio_resid;
270 if (ktruio != NULL) {
271 ktruio->uio_resid = cnt;
272 ktrgenio(fd, UIO_READ, ktruio, error);
275 td->td_retval[0] = cnt;
279 #ifndef _SYS_SYSPROTO_H_
289 struct write_args *uap;
295 if (uap->nbyte > INT_MAX)
297 aiov.iov_base = (void *)(uintptr_t)uap->buf;
298 aiov.iov_len = uap->nbyte;
299 auio.uio_iov = &aiov;
301 auio.uio_resid = uap->nbyte;
302 auio.uio_segflg = UIO_USERSPACE;
303 error = kern_writev(td, uap->fd, &auio);
308 * Positioned write system call.
310 #ifndef _SYS_SYSPROTO_H_
322 struct pwrite_args *uap;
328 if (uap->nbyte > INT_MAX)
330 aiov.iov_base = (void *)(uintptr_t)uap->buf;
331 aiov.iov_len = uap->nbyte;
332 auio.uio_iov = &aiov;
334 auio.uio_resid = uap->nbyte;
335 auio.uio_segflg = UIO_USERSPACE;
336 error = kern_pwritev(td, uap->fd, &auio, uap->offset);
341 * Gather write system call.
343 #ifndef _SYS_SYSPROTO_H_
351 writev(struct thread *td, struct writev_args *uap)
356 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
359 error = kern_writev(td, uap->fd, auio);
365 kern_writev(struct thread *td, int fd, struct uio *auio)
370 error = fget_write(td, fd, &fp);
373 error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
379 * Gather positioned write system call.
381 #ifndef _SYS_SYSPROTO_H_
382 struct pwritev_args {
390 pwritev(struct thread *td, struct pwritev_args *uap)
395 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
398 error = kern_pwritev(td, uap->fd, auio, uap->offset);
404 kern_pwritev(td, fd, auio, offset)
413 error = fget_write(td, fd, &fp);
416 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
418 else if (offset < 0 && fp->f_vnode->v_type != VCHR)
421 error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
427 * Common code for writev and pwritev that writes data to
428 * a file using the passed in uio, offset, and flags.
431 dofilewrite(td, fd, fp, auio, offset, flags)
442 struct uio *ktruio = NULL;
445 auio->uio_rw = UIO_WRITE;
447 auio->uio_offset = offset;
449 if (KTRPOINT(td, KTR_GENIO))
450 ktruio = cloneuio(auio);
452 cnt = auio->uio_resid;
453 if (fp->f_type == DTYPE_VNODE)
455 if ((error = fo_write(fp, auio, td->td_ucred, flags, td))) {
456 if (auio->uio_resid != cnt && (error == ERESTART ||
457 error == EINTR || error == EWOULDBLOCK))
459 /* Socket layer is responsible for issuing SIGPIPE. */
460 if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
461 PROC_LOCK(td->td_proc);
462 psignal(td->td_proc, SIGPIPE);
463 PROC_UNLOCK(td->td_proc);
466 cnt -= auio->uio_resid;
468 if (ktruio != NULL) {
469 ktruio->uio_resid = cnt;
470 ktrgenio(fd, UIO_WRITE, ktruio, error);
473 td->td_retval[0] = cnt;
477 #ifndef _SYS_SYSPROTO_H_
486 ioctl(struct thread *td, struct ioctl_args *uap)
493 if (uap->com > 0xffffffff) {
495 "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
496 td->td_proc->p_pid, td->td_proc->p_comm, uap->com);
497 uap->com &= 0xffffffff;
502 * Interpret high order word to find amount of data to be
503 * copied to/from the user's address space.
505 size = IOCPARM_LEN(com);
506 if ((size > IOCPARM_MAX) ||
507 ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) ||
508 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
509 ((com & IOC_OUT) && size == 0) ||
511 ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
513 ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
517 if (!(com & IOC_VOID))
518 data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
520 /* Integer argument. */
521 arg = (intptr_t)uap->data;
526 data = (void *)&uap->data;
528 error = copyin(uap->data, data, (u_int)size);
531 free(data, M_IOCTLOPS);
534 } else if (com & IOC_OUT) {
536 * Zero the buffer so the user always
537 * gets back something deterministic.
542 error = kern_ioctl(td, uap->fd, com, data);
544 if (error == 0 && (com & IOC_OUT))
545 error = copyout(data, uap->data, (u_int)size);
548 free(data, M_IOCTLOPS);
553 kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
556 struct filedesc *fdp;
560 if ((error = fget(td, fd, &fp)) != 0)
562 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
566 fdp = td->td_proc->p_fd;
570 fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
571 FILEDESC_XUNLOCK(fdp);
575 fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
576 FILEDESC_XUNLOCK(fdp);
580 if ((tmp = *(int *)data))
581 fp->f_flag |= FNONBLOCK;
583 fp->f_flag &= ~FNONBLOCK;
589 if ((tmp = *(int *)data))
590 fp->f_flag |= FASYNC;
592 fp->f_flag &= ~FASYNC;
598 error = fo_ioctl(fp, com, data, td->td_ucred, td);
605 * sellock and selwait are initialized in selectinit() via SYSINIT.
609 u_int nselcoll; /* Select collisions since boot */
610 SYSCTL_UINT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
612 #ifndef _SYS_SYSPROTO_H_
615 fd_set *in, *ou, *ex;
621 register struct thread *td;
622 register struct select_args *uap;
624 struct timeval tv, *tvp;
627 if (uap->tv != NULL) {
628 error = copyin(uap->tv, &tv, sizeof(tv));
635 return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp));
639 kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
640 fd_set *fd_ex, struct timeval *tvp)
642 struct filedesc *fdp;
644 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
645 * infds with the new FD_SETSIZE of 1024, and more than enough for
646 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
649 fd_mask s_selbits[howmany(2048, NFDBITS)];
650 fd_mask *ibits[3], *obits[3], *selbits, *sbp;
651 struct timeval atv, rtv, ttv;
653 u_int ncoll, nbufbytes, ncpbytes, nfdbits;
657 fdp = td->td_proc->p_fd;
660 if (nd > td->td_proc->p_fd->fd_nfiles)
661 nd = td->td_proc->p_fd->fd_nfiles; /* forgiving; slightly wrong */
662 FILEDESC_SUNLOCK(fdp);
665 * Allocate just enough bits for the non-null fd_sets. Use the
666 * preallocated auto buffer if possible.
668 nfdbits = roundup(nd, NFDBITS);
669 ncpbytes = nfdbits / NBBY;
672 nbufbytes += 2 * ncpbytes;
674 nbufbytes += 2 * ncpbytes;
676 nbufbytes += 2 * ncpbytes;
677 if (nbufbytes <= sizeof s_selbits)
678 selbits = &s_selbits[0];
680 selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
683 * Assign pointers into the bit buffers and fetch the input bits.
684 * Put the output buffers together so that they can be bzeroed
688 #define getbits(name, x) \
693 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
695 sbp += ncpbytes / sizeof *sbp; \
696 error = copyin(name, ibits[x], ncpbytes); \
698 goto done_nosellock; \
706 bzero(selbits, nbufbytes / 2);
710 if (itimerfix(&atv)) {
714 getmicrouptime(&rtv);
715 timevaladd(&atv, &rtv);
721 TAILQ_INIT(&td->td_selq);
726 td->td_flags |= TDF_SELECT;
728 mtx_unlock(&sellock);
730 error = selscan(td, ibits, obits, nd);
732 if (error || td->td_retval[0])
734 if (atv.tv_sec || atv.tv_usec) {
735 getmicrouptime(&rtv);
736 if (timevalcmp(&rtv, &atv, >=))
739 timevalsub(&ttv, &rtv);
740 timo = ttv.tv_sec > 24 * 60 * 60 ?
741 24 * 60 * 60 * hz : tvtohz(&ttv);
745 * An event of interest may occur while we do not hold
746 * sellock, so check TDF_SELECT and the number of
747 * collisions and rescan the file descriptors if
751 if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
758 error = cv_timedwait_sig(&selwait, &sellock, timo);
760 error = cv_wait_sig(&selwait, &sellock);
766 clear_selinfo_list(td);
768 td->td_flags &= ~TDF_SELECT;
770 mtx_unlock(&sellock);
773 /* select is not restarted after signals... */
774 if (error == ERESTART)
776 if (error == EWOULDBLOCK)
778 #define putbits(name, x) \
779 if (name && (error2 = copyout(obits[x], name, ncpbytes))) \
789 if (selbits != &s_selbits[0])
790 free(selbits, M_SELECT);
796 selscan(td, ibits, obits, nfd)
798 fd_mask **ibits, **obits;
805 /* Note: backend also returns POLLHUP/POLLERR if appropriate. */
806 static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
807 struct filedesc *fdp = td->td_proc->p_fd;
810 for (msk = 0; msk < 3; msk++) {
811 if (ibits[msk] == NULL)
813 for (i = 0; i < nfd; i += NFDBITS) {
814 bits = ibits[msk][i/NFDBITS];
815 /* ffs(int mask) not portable, fd_mask is long */
816 for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
819 if ((fp = fget_locked(fdp, fd)) == NULL) {
820 FILEDESC_SUNLOCK(fdp);
823 if (fo_poll(fp, flag[msk], td->td_ucred,
825 obits[msk][(fd)/NFDBITS] |=
826 ((fd_mask)1 << ((fd) % NFDBITS));
832 FILEDESC_SUNLOCK(fdp);
833 td->td_retval[0] = n;
837 #ifndef _SYS_SYSPROTO_H_
847 struct poll_args *uap;
850 struct pollfd smallbits[32];
851 struct timeval atv, rtv, ttv;
859 * This is kinda bogus. We have fd limits, but that is not
860 * really related to the size of the pollfd array. Make sure
861 * we let the process use at least FD_SETSIZE entries and at
862 * least enough for the current limits. We want to be reasonably
863 * safe, but not overly restrictive.
865 PROC_LOCK(td->td_proc);
866 if ((nfds > lim_cur(td->td_proc, RLIMIT_NOFILE)) &&
867 (nfds > FD_SETSIZE)) {
868 PROC_UNLOCK(td->td_proc);
872 PROC_UNLOCK(td->td_proc);
873 ni = nfds * sizeof(struct pollfd);
874 if (ni > sizeof(smallbits))
875 bits = malloc(ni, M_TEMP, M_WAITOK);
878 error = copyin(uap->fds, bits, ni);
881 if (uap->timeout != INFTIM) {
882 atv.tv_sec = uap->timeout / 1000;
883 atv.tv_usec = (uap->timeout % 1000) * 1000;
884 if (itimerfix(&atv)) {
888 getmicrouptime(&rtv);
889 timevaladd(&atv, &rtv);
895 TAILQ_INIT(&td->td_selq);
900 td->td_flags |= TDF_SELECT;
902 mtx_unlock(&sellock);
904 error = pollscan(td, bits, nfds);
906 if (error || td->td_retval[0])
908 if (atv.tv_sec || atv.tv_usec) {
909 getmicrouptime(&rtv);
910 if (timevalcmp(&rtv, &atv, >=))
913 timevalsub(&ttv, &rtv);
914 timo = ttv.tv_sec > 24 * 60 * 60 ?
915 24 * 60 * 60 * hz : tvtohz(&ttv);
918 * An event of interest may occur while we do not hold
919 * sellock, so check TDF_SELECT and the number of collisions
920 * and rescan the file descriptors if necessary.
923 if ((td->td_flags & TDF_SELECT) == 0 || nselcoll != ncoll) {
930 error = cv_timedwait_sig(&selwait, &sellock, timo);
932 error = cv_wait_sig(&selwait, &sellock);
938 clear_selinfo_list(td);
940 td->td_flags &= ~TDF_SELECT;
942 mtx_unlock(&sellock);
945 /* poll is not restarted after signals... */
946 if (error == ERESTART)
948 if (error == EWOULDBLOCK)
951 error = copyout(bits, uap->fds, ni);
956 if (ni > sizeof(smallbits))
963 pollscan(td, fds, nfd)
968 register struct filedesc *fdp = td->td_proc->p_fd;
974 for (i = 0; i < nfd; i++, fds++) {
975 if (fds->fd >= fdp->fd_nfiles) {
976 fds->revents = POLLNVAL;
978 } else if (fds->fd < 0) {
981 fp = fdp->fd_ofiles[fds->fd];
983 fds->revents = POLLNVAL;
987 * Note: backend also returns POLLHUP and
988 * POLLERR if appropriate.
990 fds->revents = fo_poll(fp, fds->events,
992 if (fds->revents != 0)
997 FILEDESC_SUNLOCK(fdp);
998 td->td_retval[0] = n;
1003 * OpenBSD poll system call.
1005 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
1007 #ifndef _SYS_SYSPROTO_H_
1008 struct openbsd_poll_args {
1015 openbsd_poll(td, uap)
1016 register struct thread *td;
1017 register struct openbsd_poll_args *uap;
1019 return (poll(td, (struct poll_args *)uap));
1023 * Remove the references to the thread from all of the objects we were
1026 * This code assumes that the underlying owner of the selinfo structure will
1027 * hold sellock before it changes it, and that it will unlink itself from our
1028 * list if it goes away.
1031 clear_selinfo_list(td)
1036 mtx_assert(&sellock, MA_OWNED);
1037 TAILQ_FOREACH(si, &td->td_selq, si_thrlist)
1038 si->si_thread = NULL;
1039 TAILQ_INIT(&td->td_selq);
1043 * Record a select request.
1046 selrecord(selector, sip)
1047 struct thread *selector;
1048 struct selinfo *sip;
1053 * If the selinfo's thread pointer is NULL then take ownership of it.
1055 * If the thread pointer is not NULL and it points to another
1056 * thread, then we have a collision.
1058 * If the thread pointer is not NULL and points back to us then leave
1059 * it alone as we've already added pointed it at us and added it to
1062 if (sip->si_thread == NULL) {
1063 sip->si_thread = selector;
1064 TAILQ_INSERT_TAIL(&selector->td_selq, sip, si_thrlist);
1065 } else if (sip->si_thread != selector) {
1066 sip->si_flags |= SI_COLL;
1069 mtx_unlock(&sellock);
1072 /* Wake up a selecting thread. */
1075 struct selinfo *sip;
1077 doselwakeup(sip, -1);
1080 /* Wake up a selecting thread, and set its priority. */
1082 selwakeuppri(sip, pri)
1083 struct selinfo *sip;
1086 doselwakeup(sip, pri);
1090 * Do a wakeup when a selectable event occurs.
1093 doselwakeup(sip, pri)
1094 struct selinfo *sip;
1100 td = sip->si_thread;
1101 if ((sip->si_flags & SI_COLL) != 0) {
1103 sip->si_flags &= ~SI_COLL;
1104 cv_broadcastpri(&selwait, pri);
1107 mtx_unlock(&sellock);
1110 TAILQ_REMOVE(&td->td_selq, sip, si_thrlist);
1111 sip->si_thread = NULL;
1113 td->td_flags &= ~TDF_SELECT;
1115 sleepq_remove(td, &selwait);
1116 mtx_unlock(&sellock);
1119 static void selectinit(void *);
1120 SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, selectinit, NULL)
1127 cv_init(&selwait, "select");
1128 mtx_init(&sellock, "sellck", NULL, MTX_DEF);