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.18 2004/09/13 23:41:18 drhodus 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>
59 #include <sys/mapped_ioctl.h>
61 #include <sys/queue.h>
62 #include <sys/resourcevar.h>
63 #include <sys/sysctl.h>
64 #include <sys/sysent.h>
67 #include <sys/ktrace.h>
70 #include <vm/vm_page.h>
71 #include <sys/file2.h>
73 #include <machine/limits.h>
75 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
76 static MALLOC_DEFINE(M_IOCTLMAP, "ioctlmap", "mapped ioctl handler buffer");
77 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
78 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
80 static int pollscan (struct proc *, struct pollfd *, u_int, int *);
81 static int selscan (struct proc *, fd_mask **, fd_mask **,
91 if (((u_int)fd) >= fdp->fd_nfiles ||
92 (fp = fdp->fd_ofiles[fd]) == NULL ||
93 (fp->f_flag & flag) == 0) {
104 read(struct read_args *uap)
106 struct thread *td = curthread;
111 aiov.iov_base = uap->buf;
112 aiov.iov_len = uap->nbyte;
113 auio.uio_iov = &aiov;
115 auio.uio_offset = -1;
116 auio.uio_resid = uap->nbyte;
117 auio.uio_rw = UIO_READ;
118 auio.uio_segflg = UIO_USERSPACE;
121 error = kern_readv(uap->fd, &auio, 0, &uap->sysmsg_result);
130 pread(struct pread_args *uap)
132 struct thread *td = curthread;
137 aiov.iov_base = uap->buf;
138 aiov.iov_len = uap->nbyte;
139 auio.uio_iov = &aiov;
141 auio.uio_offset = uap->offset;
142 auio.uio_resid = uap->nbyte;
143 auio.uio_rw = UIO_READ;
144 auio.uio_segflg = UIO_USERSPACE;
147 error = kern_readv(uap->fd, &auio, FOF_OFFSET, &uap->sysmsg_result);
153 readv(struct readv_args *uap)
155 struct thread *td = curthread;
157 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
160 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
165 auio.uio_iovcnt = uap->iovcnt;
166 auio.uio_offset = -1;
167 auio.uio_rw = UIO_READ;
168 auio.uio_segflg = UIO_USERSPACE;
171 error = kern_readv(uap->fd, &auio, 0, &uap->sysmsg_result);
173 iovec_free(&iov, aiov);
178 kern_readv(int fd, struct uio *auio, int flags, int *res)
180 struct thread *td = curthread;
181 struct proc *p = td->td_proc;
183 struct filedesc *fdp = p->p_fd;
186 struct iovec *ktriov = NULL;
192 fp = holdfp(fdp, fd, FREAD);
195 if (flags & FOF_OFFSET && fp->f_type != DTYPE_VNODE) {
199 if (auio->uio_resid < 0) {
205 * if tracing, save a copy of iovec
207 if (KTRPOINT(td, KTR_GENIO)) {
208 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
210 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
211 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
215 len = auio->uio_resid;
216 error = fo_read(fp, auio, fp->f_cred, flags, td);
218 if (auio->uio_resid != len && (error == ERESTART ||
219 error == EINTR || error == EWOULDBLOCK))
223 if (ktriov != NULL) {
225 ktruio.uio_iov = ktriov;
226 ktruio.uio_resid = len - auio->uio_resid;
227 ktrgenio(p->p_tracep, fd, UIO_READ, &ktruio, error);
229 FREE(ktriov, M_TEMP);
233 *res = len - auio->uio_resid;
243 write(struct write_args *uap)
245 struct thread *td = curthread;
250 aiov.iov_base = (void *)(uintptr_t)uap->buf;
251 aiov.iov_len = uap->nbyte;
252 auio.uio_iov = &aiov;
254 auio.uio_offset = -1;
255 auio.uio_resid = uap->nbyte;
256 auio.uio_rw = UIO_WRITE;
257 auio.uio_segflg = UIO_USERSPACE;
260 error = kern_writev(uap->fd, &auio, 0, &uap->sysmsg_result);
269 pwrite(struct pwrite_args *uap)
271 struct thread *td = curthread;
276 aiov.iov_base = (void *)(uintptr_t)uap->buf;
277 aiov.iov_len = uap->nbyte;
278 auio.uio_iov = &aiov;
280 auio.uio_offset = uap->offset;
281 auio.uio_resid = uap->nbyte;
282 auio.uio_rw = UIO_WRITE;
283 auio.uio_segflg = UIO_USERSPACE;
286 error = kern_writev(uap->fd, &auio, FOF_OFFSET, &uap->sysmsg_result);
292 writev(struct writev_args *uap)
294 struct thread *td = curthread;
296 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
299 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
304 auio.uio_iovcnt = uap->iovcnt;
305 auio.uio_offset = -1;
306 auio.uio_rw = UIO_WRITE;
307 auio.uio_segflg = UIO_USERSPACE;
310 error = kern_writev(uap->fd, &auio, 0, &uap->sysmsg_result);
312 iovec_free(&iov, aiov);
317 * Gather write system call
320 kern_writev(int fd, struct uio *auio, int flags, int *res)
322 struct thread *td = curthread;
323 struct proc *p = td->td_proc;
325 struct filedesc *fdp = p->p_fd;
328 struct iovec *ktriov = NULL;
334 fp = holdfp(fdp, fd, FWRITE);
337 if ((flags & FOF_OFFSET) && fp->f_type != DTYPE_VNODE) {
341 if (auio->uio_resid < 0) {
347 * if tracing, save a copy of iovec and uio
349 if (KTRPOINT(td, KTR_GENIO)) {
350 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
352 MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
353 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
357 len = auio->uio_resid;
358 if (fp->f_type == DTYPE_VNODE)
360 error = fo_write(fp, auio, fp->f_cred, flags, td);
362 if (auio->uio_resid != len && (error == ERESTART ||
363 error == EINTR || error == EWOULDBLOCK))
369 if (ktriov != NULL) {
371 ktruio.uio_iov = ktriov;
372 ktruio.uio_resid = len - auio->uio_resid;
373 ktrgenio(p->p_tracep, fd, UIO_WRITE, &ktruio, error);
375 FREE(ktriov, M_TEMP);
379 *res = len - auio->uio_resid;
390 ioctl(struct ioctl_args *uap)
392 return(mapped_ioctl(uap->fd, uap->com, uap->data, NULL));
395 struct ioctl_map_entry {
397 struct ioctl_map_range *cmd_ranges;
398 LIST_ENTRY(ioctl_map_entry) entries;
402 mapped_ioctl(int fd, u_long com, caddr_t uspc_data, struct ioctl_map *map)
404 struct thread *td = curthread;
405 struct proc *p = td->td_proc;
407 struct filedesc *fdp;
408 struct ioctl_map_range *iomc = NULL;
414 #define STK_PARAMS 128
416 char stkbuf[STK_PARAMS];
422 if ((u_int)fd >= fdp->fd_nfiles ||
423 (fp = fdp->fd_ofiles[fd]) == NULL)
426 if ((fp->f_flag & (FREAD | FWRITE)) == 0)
429 if (map != NULL) { /* obey translation map */
431 struct ioctl_map_entry *e;
433 maskcmd = com & map->mask;
435 LIST_FOREACH(e, &map->mapping, entries) {
436 for (iomc = e->cmd_ranges; iomc->start != 0 ||
437 iomc->maptocmd != 0 || iomc->func != NULL;
439 if (maskcmd >= iomc->start &&
440 maskcmd <= iomc->end)
444 /* Did we find a match? */
445 if (iomc->start != 0 || iomc->maptocmd != 0 ||
451 (iomc->start == 0 && iomc->maptocmd == 0
452 && iomc->func == NULL)) {
453 printf("%s: 'ioctl' fd=%d, cmd=0x%lx ('%c',%d) not implemented\n",
454 map->sys, fd, maskcmd,
455 (int)((maskcmd >> 8) & 0xff),
456 (int)(maskcmd & 0xff));
460 com = iomc->maptocmd;
465 fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
468 fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
473 * Interpret high order word to find amount of data to be
474 * copied to/from the user's address space.
476 size = IOCPARM_LEN(com);
477 if (size > IOCPARM_MAX)
483 if (size > sizeof (ubuf.stkbuf)) {
484 memp = malloc(size, M_IOCTLOPS, M_WAITOK);
489 if ((com & IOC_IN) != 0) {
491 error = copyin(uspc_data, data, (u_int)size);
494 free(memp, M_IOCTLOPS);
499 *(caddr_t *)data = uspc_data;
501 } else if ((com & IOC_OUT) != 0 && size) {
503 * Zero the buffer so the user always
504 * gets back something deterministic.
507 } else if ((com & IOC_VOID) != 0) {
508 *(caddr_t *)data = uspc_data;
514 if ((tmp = *(int *)data))
515 fp->f_flag |= FNONBLOCK;
517 fp->f_flag &= ~FNONBLOCK;
518 error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, td);
522 if ((tmp = *(int *)data))
523 fp->f_flag |= FASYNC;
525 fp->f_flag &= ~FASYNC;
526 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, td);
531 * If there is a override function,
532 * call it instead of directly routing the call
534 if (map != NULL && iomc->func != NULL)
535 error = iomc->func(fp, com, ocom, data, td);
537 error = fo_ioctl(fp, com, data, td);
539 * Copy any data to user, size was
540 * already set and checked above.
542 if (error == 0 && (com & IOC_OUT) != 0 && size != 0)
543 error = copyout(data, uspc_data, (u_int)size);
547 free(memp, M_IOCTLOPS);
553 mapped_ioctl_register_handler(struct ioctl_map_handler *he)
555 struct ioctl_map_entry *ne;
557 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL &&
558 he->subsys != NULL && *he->subsys != '\0');
560 ne = malloc(sizeof(struct ioctl_map_entry), M_IOCTLMAP, M_WAITOK);
562 ne->subsys = he->subsys;
563 ne->cmd_ranges = he->cmd_ranges;
565 LIST_INSERT_HEAD(&he->map->mapping, ne, entries);
571 mapped_ioctl_unregister_handler(struct ioctl_map_handler *he)
573 struct ioctl_map_entry *ne;
575 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL);
577 LIST_FOREACH(ne, &he->map->mapping, entries) {
578 if (ne->cmd_ranges != he->cmd_ranges)
580 LIST_REMOVE(ne, entries);
581 free(ne, M_IOCTLMAP);
587 static int nselcoll; /* Select collisions since boot */
589 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
592 * Select system call.
595 select(struct select_args *uap)
597 struct proc *p = curproc;
600 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
601 * infds with the new FD_SETSIZE of 1024, and more than enough for
602 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
605 fd_mask s_selbits[howmany(2048, NFDBITS)];
606 fd_mask *ibits[3], *obits[3], *selbits, *sbp;
607 struct timeval atv, rtv, ttv;
608 int s, ncoll, error, timo;
609 u_int nbufbytes, ncpbytes, nfdbits;
613 if (uap->nd > p->p_fd->fd_nfiles)
614 uap->nd = p->p_fd->fd_nfiles; /* forgiving; slightly wrong */
617 * Allocate just enough bits for the non-null fd_sets. Use the
618 * preallocated auto buffer if possible.
620 nfdbits = roundup(uap->nd, NFDBITS);
621 ncpbytes = nfdbits / NBBY;
624 nbufbytes += 2 * ncpbytes;
626 nbufbytes += 2 * ncpbytes;
628 nbufbytes += 2 * ncpbytes;
629 if (nbufbytes <= sizeof s_selbits)
630 selbits = &s_selbits[0];
632 selbits = malloc(nbufbytes, M_SELECT, M_WAITOK);
635 * Assign pointers into the bit buffers and fetch the input bits.
636 * Put the output buffers together so that they can be bzeroed
640 #define getbits(name, x) \
642 if (uap->name == NULL) \
645 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
647 sbp += ncpbytes / sizeof *sbp; \
648 error = copyin(uap->name, ibits[x], ncpbytes); \
658 bzero(selbits, nbufbytes / 2);
661 error = copyin((caddr_t)uap->tv, (caddr_t)&atv,
665 if (itimerfix(&atv)) {
669 getmicrouptime(&rtv);
670 timevaladd(&atv, &rtv);
678 p->p_flag |= P_SELECT;
679 error = selscan(p, ibits, obits, uap->nd, &uap->sysmsg_result);
680 if (error || uap->sysmsg_result)
682 if (atv.tv_sec || atv.tv_usec) {
683 getmicrouptime(&rtv);
684 if (timevalcmp(&rtv, &atv, >=))
687 timevalsub(&ttv, &rtv);
688 timo = ttv.tv_sec > 24 * 60 * 60 ?
689 24 * 60 * 60 * hz : tvtohz_high(&ttv);
692 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
696 p->p_flag &= ~P_SELECT;
698 error = tsleep((caddr_t)&selwait, PCATCH, "select", timo);
704 p->p_flag &= ~P_SELECT;
705 /* select is not restarted after signals... */
706 if (error == ERESTART)
708 if (error == EWOULDBLOCK)
710 #define putbits(name, x) \
711 if (uap->name && (error2 = copyout(obits[x], uap->name, ncpbytes))) \
721 if (selbits != &s_selbits[0])
722 free(selbits, M_SELECT);
727 selscan(struct proc *p, fd_mask **ibits, fd_mask **obits, int nfd, int *res)
729 struct thread *td = p->p_thread;
730 struct filedesc *fdp = p->p_fd;
735 /* Note: backend also returns POLLHUP/POLLERR if appropriate. */
736 static int flag[3] = { POLLRDNORM, POLLWRNORM, POLLRDBAND };
738 for (msk = 0; msk < 3; msk++) {
739 if (ibits[msk] == NULL)
741 for (i = 0; i < nfd; i += NFDBITS) {
742 bits = ibits[msk][i/NFDBITS];
743 /* ffs(int mask) not portable, fd_mask is long */
744 for (fd = i; bits && fd < nfd; fd++, bits >>= 1) {
747 fp = fdp->fd_ofiles[fd];
750 if (fo_poll(fp, flag[msk], fp->f_cred, td)) {
751 obits[msk][(fd)/NFDBITS] |=
752 ((fd_mask)1 << ((fd) % NFDBITS));
766 poll(struct poll_args *uap)
769 struct pollfd smallbits[32];
770 struct timeval atv, rtv, ttv;
771 int s, ncoll, error = 0, timo;
774 struct proc *p = curproc;
776 nfds = SCARG(uap, nfds);
778 * This is kinda bogus. We have fd limits, but that is not
779 * really related to the size of the pollfd array. Make sure
780 * we let the process use at least FD_SETSIZE entries and at
781 * least enough for the current limits. We want to be reasonably
782 * safe, but not overly restrictive.
784 if (nfds > p->p_rlimit[RLIMIT_NOFILE].rlim_cur && nfds > FD_SETSIZE)
786 ni = nfds * sizeof(struct pollfd);
787 if (ni > sizeof(smallbits))
788 bits = malloc(ni, M_TEMP, M_WAITOK);
791 error = copyin(SCARG(uap, fds), bits, ni);
794 if (SCARG(uap, timeout) != INFTIM) {
795 atv.tv_sec = SCARG(uap, timeout) / 1000;
796 atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
797 if (itimerfix(&atv)) {
801 getmicrouptime(&rtv);
802 timevaladd(&atv, &rtv);
810 p->p_flag |= P_SELECT;
811 error = pollscan(p, bits, nfds, &uap->sysmsg_result);
812 if (error || uap->sysmsg_result)
814 if (atv.tv_sec || atv.tv_usec) {
815 getmicrouptime(&rtv);
816 if (timevalcmp(&rtv, &atv, >=))
819 timevalsub(&ttv, &rtv);
820 timo = ttv.tv_sec > 24 * 60 * 60 ?
821 24 * 60 * 60 * hz : tvtohz_high(&ttv);
824 if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
828 p->p_flag &= ~P_SELECT;
829 error = tsleep((caddr_t)&selwait, PCATCH, "poll", timo);
834 p->p_flag &= ~P_SELECT;
835 /* poll is not restarted after signals... */
836 if (error == ERESTART)
838 if (error == EWOULDBLOCK)
841 error = copyout(bits, SCARG(uap, fds), ni);
846 if (ni > sizeof(smallbits))
852 pollscan(struct proc *p, struct pollfd *fds, u_int nfd, int *res)
854 struct thread *td = p->p_thread;
855 struct filedesc *fdp = p->p_fd;
860 for (i = 0; i < nfd; i++, fds++) {
861 if (fds->fd >= fdp->fd_nfiles) {
862 fds->revents = POLLNVAL;
864 } else if (fds->fd < 0) {
867 fp = fdp->fd_ofiles[fds->fd];
869 fds->revents = POLLNVAL;
873 * Note: backend also returns POLLHUP and
874 * POLLERR if appropriate.
876 fds->revents = fo_poll(fp, fds->events,
878 if (fds->revents != 0)
888 * OpenBSD poll system call.
889 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
892 openbsd_poll(struct openbsd_poll_args *uap)
894 return (poll((struct poll_args *)uap));
899 seltrue(dev_t dev, int events, struct thread *td)
901 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
905 * Record a select request. A global wait must be used since a process/thread
906 * might go away after recording its request.
909 selrecord(struct thread *selector, struct selinfo *sip)
914 if ((p = selector->td_proc) == NULL)
915 panic("selrecord: thread needs a process");
918 if (sip->si_pid == mypid)
920 if (sip->si_pid && (p = pfind(sip->si_pid)) &&
921 p->p_wchan == (caddr_t)&selwait) {
922 sip->si_flags |= SI_COLL;
929 * Do a wakeup when a selectable event occurs.
932 selwakeup(struct selinfo *sip)
937 if (sip->si_pid == 0)
939 if (sip->si_flags & SI_COLL) {
941 sip->si_flags &= ~SI_COLL;
942 wakeup((caddr_t)&selwait); /* YYY fixable */
944 p = pfind(sip->si_pid);
948 if (p->p_wchan == (caddr_t)&selwait) {
949 if (p->p_stat == SSLEEP)
952 unsleep(p->p_thread);
953 } else if (p->p_flag & P_SELECT)
954 p->p_flag &= ~P_SELECT;