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.16 2004/01/07 11:04:18 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/kern_syscall.h>
58 #include <sys/malloc.h>
60 #include <sys/resourcevar.h>
61 #include <sys/sysctl.h>
62 #include <sys/sysent.h>
65 #include <sys/ktrace.h>
68 #include <vm/vm_page.h>
69 #include <sys/file2.h>
71 #include <machine/limits.h>
73 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
74 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
75 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
77 static int pollscan (struct proc *, struct pollfd *, u_int, int *);
78 static int selscan (struct proc *, fd_mask **, fd_mask **,
88 if (((u_int)fd) >= fdp->fd_nfiles ||
89 (fp = fdp->fd_ofiles[fd]) == NULL ||
90 (fp->f_flag & flag) == 0) {
101 read(struct read_args *uap)
103 struct thread *td = curthread;
108 aiov.iov_base = uap->buf;
109 aiov.iov_len = uap->nbyte;
110 auio.uio_iov = &aiov;
112 auio.uio_offset = -1;
113 auio.uio_resid = uap->nbyte;
114 auio.uio_rw = UIO_READ;
115 auio.uio_segflg = UIO_USERSPACE;
118 error = kern_readv(uap->fd, &auio, 0, &uap->sysmsg_result);
127 pread(struct pread_args *uap)
129 struct thread *td = curthread;
134 aiov.iov_base = uap->buf;
135 aiov.iov_len = uap->nbyte;
136 auio.uio_iov = &aiov;
138 auio.uio_offset = uap->offset;
139 auio.uio_resid = uap->nbyte;
140 auio.uio_rw = UIO_READ;
141 auio.uio_segflg = UIO_USERSPACE;
144 error = kern_readv(uap->fd, &auio, FOF_OFFSET, &uap->sysmsg_result);
150 readv(struct readv_args *uap)
152 struct thread *td = curthread;
154 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
157 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
162 auio.uio_iovcnt = uap->iovcnt;
163 auio.uio_offset = -1;
164 auio.uio_rw = UIO_READ;
165 auio.uio_segflg = UIO_USERSPACE;
168 error = kern_readv(uap->fd, &auio, 0, &uap->sysmsg_result);
170 iovec_free(&iov, aiov);
175 kern_readv(int fd, struct uio *auio, int flags, int *res)
177 struct thread *td = curthread;
178 struct proc *p = td->td_proc;
180 struct filedesc *fdp = p->p_fd;
183 struct iovec *ktriov = NULL;
189 fp = holdfp(fdp, fd, FREAD);
192 if (flags & FOF_OFFSET && fp->f_type != DTYPE_VNODE) {
196 if (auio->uio_resid < 0) {
202 * if tracing, save a copy of iovec
204 if (KTRPOINT(td, KTR_GENIO)) {
205 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
207 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
208 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
212 len = auio->uio_resid;
213 error = fo_read(fp, auio, fp->f_cred, flags, td);
215 if (auio->uio_resid != len && (error == ERESTART ||
216 error == EINTR || error == EWOULDBLOCK))
220 if (ktriov != NULL) {
222 ktruio.uio_iov = ktriov;
223 ktruio.uio_resid = len - auio->uio_resid;
224 ktrgenio(p->p_tracep, fd, UIO_READ, &ktruio, error);
226 FREE(ktriov, M_TEMP);
230 *res = len - auio->uio_resid;
240 write(struct write_args *uap)
242 struct thread *td = curthread;
247 aiov.iov_base = (void *)(uintptr_t)uap->buf;
248 aiov.iov_len = uap->nbyte;
249 auio.uio_iov = &aiov;
251 auio.uio_offset = -1;
252 auio.uio_resid = uap->nbyte;
253 auio.uio_rw = UIO_WRITE;
254 auio.uio_segflg = UIO_USERSPACE;
257 error = kern_writev(uap->fd, &auio, 0, &uap->sysmsg_result);
266 pwrite(struct pwrite_args *uap)
268 struct thread *td = curthread;
273 aiov.iov_base = (void *)(uintptr_t)uap->buf;
274 aiov.iov_len = uap->nbyte;
275 auio.uio_iov = &aiov;
277 auio.uio_offset = uap->offset;
278 auio.uio_resid = uap->nbyte;
279 auio.uio_rw = UIO_WRITE;
280 auio.uio_segflg = UIO_USERSPACE;
283 error = kern_writev(uap->fd, &auio, FOF_OFFSET, &uap->sysmsg_result);
289 writev(struct writev_args *uap)
291 struct thread *td = curthread;
293 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
296 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
301 auio.uio_iovcnt = uap->iovcnt;
302 auio.uio_offset = -1;
303 auio.uio_rw = UIO_WRITE;
304 auio.uio_segflg = UIO_USERSPACE;
307 error = kern_writev(uap->fd, &auio, 0, &uap->sysmsg_result);
309 iovec_free(&iov, aiov);
314 * Gather write system call
317 kern_writev(int fd, struct uio *auio, int flags, int *res)
319 struct thread *td = curthread;
320 struct proc *p = td->td_proc;
322 struct filedesc *fdp = p->p_fd;
325 struct iovec *ktriov = NULL;
331 fp = holdfp(fdp, fd, FWRITE);
334 if ((flags & FOF_OFFSET) && fp->f_type != DTYPE_VNODE) {
338 if (auio->uio_resid < 0) {
344 * if tracing, save a copy of iovec and uio
346 if (KTRPOINT(td, KTR_GENIO)) {
347 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
349 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
350 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
354 len = auio->uio_resid;
355 if (fp->f_type == DTYPE_VNODE)
357 error = fo_write(fp, auio, fp->f_cred, flags, td);
359 if (auio->uio_resid != len && (error == ERESTART ||
360 error == EINTR || error == EWOULDBLOCK))
366 if (ktriov != NULL) {
368 ktruio.uio_iov = ktriov;
369 ktruio.uio_resid = len - auio->uio_resid;
370 ktrgenio(p->p_tracep, fd, UIO_WRITE, &ktruio, error);
372 FREE(ktriov, M_TEMP);
376 *res = len - auio->uio_resid;
387 ioctl(struct ioctl_args *uap)
389 struct thread *td = curthread;
390 struct proc *p = td->td_proc;
392 struct filedesc *fdp;
398 #define STK_PARAMS 128
400 char stkbuf[STK_PARAMS];
406 if ((u_int)uap->fd >= fdp->fd_nfiles ||
407 (fp = fdp->fd_ofiles[uap->fd]) == NULL)
410 if ((fp->f_flag & (FREAD | FWRITE)) == 0)
413 switch (com = uap->com) {
415 fdp->fd_ofileflags[uap->fd] &= ~UF_EXCLOSE;
418 fdp->fd_ofileflags[uap->fd] |= UF_EXCLOSE;
423 * Interpret high order word to find amount of data to be
424 * copied to/from the user's address space.
426 size = IOCPARM_LEN(com);
427 if (size > IOCPARM_MAX)
433 if (size > sizeof (ubuf.stkbuf)) {
434 memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
441 error = copyin(uap->data, data, (u_int)size);
444 free(memp, M_IOCTLOPS);
449 *(caddr_t *)data = uap->data;
451 } else if ((com&IOC_OUT) && size) {
453 * Zero the buffer so the user always
454 * gets back something deterministic.
457 } else if (com&IOC_VOID) {
458 *(caddr_t *)data = uap->data;
464 if ((tmp = *(int *)data))
465 fp->f_flag |= FNONBLOCK;
467 fp->f_flag &= ~FNONBLOCK;
468 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td);
472 if ((tmp = *(int *)data))
473 fp->f_flag |= FASYNC;
475 fp->f_flag &= ~FASYNC;
476 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, td);
480 error = fo_ioctl(fp, com, data, td);
482 * Copy any data to user, size was
483 * already set and checked above.
485 if (error == 0 && (com&IOC_OUT) && size)
486 error = copyout(data, uap->data, (u_int)size);
490 free(memp, M_IOCTLOPS);
495 static int nselcoll; /* Select collisions since boot */
497 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
500 * Select system call.
503 select(struct select_args *uap)
505 struct proc *p = curproc;
508 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
509 * infds with the new FD_SETSIZE of 1024, and more than enough for
510 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
513 fd_mask s_selbits[howmany(2048, NFDBITS)];
514 fd_mask *ibits[3], *obits[3], *selbits, *sbp;
515 struct timeval atv, rtv, ttv;
516 int s, ncoll, error, timo;
517 u_int nbufbytes, ncpbytes, nfdbits;
521 if (uap->nd > p->p_fd->fd_nfiles)
522 uap->nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */
525 * Allocate just enough bits for the non-null fd_sets. Use the
526 * preallocated auto buffer if possible.
528 nfdbits = roundup(uap->nd, NFDBITS);
529 ncpbytes = nfdbits / NBBY;
532 nbufbytes += 2 * ncpbytes;
534 nbufbytes += 2 * ncpbytes;
536 nbufbytes += 2 * ncpbytes;
537 if (nbufbytes <= sizeof s_selbits)
538 selbits = &s_selbits[0];
540 selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
543 * Assign pointers into the bit buffers and fetch the input bits.
544 * Put the output buffers together so that they can be bzeroed
548 #define getbits(name, x) \
550 if (uap->name == NULL) \
553 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
555 sbp += ncpbytes / sizeof *sbp; \
556 error = copyin(uap->name, ibits[x], ncpbytes); \
566 bzero(selbits, nbufbytes / 2);
569 error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
573 if (itimerfix(&atv)) {
577 getmicrouptime(&rtv);
578 timevaladd(&atv, &rtv);
586 p->p_flag |= P_SELECT;
587 error = selscan(p, ibits, obits, uap->nd, &uap->sysmsg_result);
588 if (error || uap->sysmsg_result)
590 if (atv.tv_sec || atv.tv_usec) {
591 getmicrouptime(&rtv);
592 if (timevalcmp(&rtv, &atv, >=))
595 timevalsub(&ttv, &rtv);
596 timo = ttv.tv_sec > 24 * 60 * 60 ?
597 24 * 60 * 60 * hz : tvtohz_high(&ttv);
600 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
604 p->p_flag &= ~P_SELECT;
606 error = tsleep((caddr_t)&selwait, PCATCH, "select", timo);
612 p->p_flag &= ~P_SELECT;
613 /* select is not restarted after signals... */
614 if (error == ERESTART)
616 if (error == EWOULDBLOCK)
618 #define putbits(name, x) \
619 if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
629 if (selbits != &s_selbits[0])
630 free(selbits, M_SELECT);
635 selscan(struct proc *p, fd_mask **ibits, fd_mask **obits, int nfd, int *res)
637 struct thread *td = p->p_thread;
638 struct filedesc *fdp = p->p_fd;
643 /* Note: backend also returns POLLHUP/POLLERR if appropriate. */
644 static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
646 for (msk = 0; msk < 3; msk++) {
647 if (ibits[msk] == NULL)
649 for (i = 0; i < nfd; i += NFDBITS) {
650 bits = ibits[msk][i/NFDBITS];
651 /* ffs(int mask) not portable, fd_mask is long */
652 for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
655 fp = fdp->fd_ofiles[fd];
658 if (fo_poll(fp, flag[msk], fp->f_cred, td)) {
659 obits[msk][(fd)/NFDBITS] |=
660 ((fd_mask)1 << ((fd) % NFDBITS));
674 poll(struct poll_args *uap)
677 char smallbits[32 * sizeof(struct pollfd)];
678 struct timeval atv, rtv, ttv;
679 int s, ncoll, error = 0, timo;
682 struct proc *p = curproc;
684 nfds = SCARG(uap, nfds);
686 * This is kinda bogus. We have fd limits, but that is not
687 * really related to the size of the pollfd array. Make sure
688 * we let the process use at least FD_SETSIZE entries and at
689 * least enough for the current limits. We want to be reasonably
690 * safe, but not overly restrictive.
692 if (nfds > p->p_rlimit[RLIMIT_NOFILE].rlim_cur && nfds > FD_SETSIZE)
694 ni = nfds * sizeof(struct pollfd);
695 if (ni > sizeof(smallbits))
696 bits = malloc(ni, M_TEMP, M_WAITOK);
699 error = copyin(SCARG(uap, fds), bits, ni);
702 if (SCARG(uap, timeout) != INFTIM) {
703 atv.tv_sec = SCARG(uap, timeout) / 1000;
704 atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
705 if (itimerfix(&atv)) {
709 getmicrouptime(&rtv);
710 timevaladd(&atv, &rtv);
718 p->p_flag |= P_SELECT;
719 error = pollscan(p, (struct pollfd *)bits, nfds, &uap->sysmsg_result);
720 if (error || uap->sysmsg_result)
722 if (atv.tv_sec || atv.tv_usec) {
723 getmicrouptime(&rtv);
724 if (timevalcmp(&rtv, &atv, >=))
727 timevalsub(&ttv, &rtv);
728 timo = ttv.tv_sec > 24 * 60 * 60 ?
729 24 * 60 * 60 * hz : tvtohz_high(&ttv);
732 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
736 p->p_flag &= ~P_SELECT;
737 error = tsleep((caddr_t)&selwait, PCATCH, "poll", timo);
742 p->p_flag &= ~P_SELECT;
743 /* poll is not restarted after signals... */
744 if (error == ERESTART)
746 if (error == EWOULDBLOCK)
749 error = copyout(bits, SCARG(uap, fds), ni);
754 if (ni > sizeof(smallbits))
760 pollscan(struct proc *p, struct pollfd *fds, u_int nfd, int *res)
762 struct thread *td = p->p_thread;
763 struct filedesc *fdp = p->p_fd;
768 for (i = 0; i < nfd; i++, fds++) {
769 if (fds->fd >= fdp->fd_nfiles) {
770 fds->revents = POLLNVAL;
772 } else if (fds->fd < 0) {
775 fp = fdp->fd_ofiles[fds->fd];
777 fds->revents = POLLNVAL;
781 * Note: backend also returns POLLHUP and
782 * POLLERR if appropriate.
784 fds->revents = fo_poll(fp, fds->events,
786 if (fds->revents != 0)
796 * OpenBSD poll system call.
797 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
800 openbsd_poll(struct openbsd_poll_args *uap)
802 return (poll((struct poll_args *)uap));
807 seltrue(dev_t dev, int events, struct thread *td)
809 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
813 * Record a select request. A global wait must be used since a process/thread
814 * might go away after recording its request.
817 selrecord(struct thread *selector, struct selinfo *sip)
822 if ((p = selector->td_proc) == NULL)
823 panic("selrecord: thread needs a process");
826 if (sip->si_pid == mypid)
828 if (sip->si_pid && (p = pfind(sip->si_pid)) &&
829 p->p_wchan == (caddr_t)&selwait) {
830 sip->si_flags |= SI_COLL;
837 * Do a wakeup when a selectable event occurs.
840 selwakeup(struct selinfo *sip)
845 if (sip->si_pid == 0)
847 if (sip->si_flags & SI_COLL) {
849 sip->si_flags &= ~SI_COLL;
850 wakeup((caddr_t)&selwait); /* YYY fixable */
852 p = pfind(sip->si_pid);
856 if (p->p_wchan == (caddr_t)&selwait) {
857 if (p->p_stat == SSLEEP)
860 unsleep(p->p_thread);
861 } else if (p->p_flag & P_SELECT)
862 p->p_flag &= ~P_SELECT;