42#include "opt_capsicum.h"
43#include "opt_ktrace.h"
47#include <sys/sysproto.h>
48#include <sys/capsicum.h>
49#include <sys/filedesc.h>
55#include <sys/signalvar.h>
56#include <sys/socketvar.h>
58#include <sys/eventfd.h>
59#include <sys/kernel.h>
61#include <sys/limits.h>
62#include <sys/malloc.h>
64#include <sys/resourcevar.h>
65#include <sys/selinfo.h>
66#include <sys/sleepqueue.h>
67#include <sys/specialfd.h>
68#include <sys/syscallsubr.h>
69#include <sys/sysctl.h>
70#include <sys/sysent.h>
74#include <sys/condvar.h>
76#include <sys/ktrace.h>
79#include <security/audit/audit.h>
92#define SYS_IOCTL_SMALL_SIZE 128
93#define SYS_IOCTL_SMALL_ALIGN 8
96static int iosize_max_clamp = 0;
97SYSCTL_INT(_debug, OID_AUTO, iosize_max_clamp, CTLFLAG_RW,
98 &iosize_max_clamp, 0,
"Clamp max i/o size to INT_MAX");
99static int devfs_iosize_max_clamp = 1;
100SYSCTL_INT(_debug, OID_AUTO, devfs_iosize_max_clamp, CTLFLAG_RW,
101 &devfs_iosize_max_clamp, 0,
"Clamp max i/o size to INT_MAX for devices");
115static int pollout(
struct thread *,
struct pollfd *,
struct pollfd *,
117static int pollscan(
struct thread *,
struct pollfd *, u_int);
119static int selscan(
struct thread *, fd_mask **, fd_mask **,
int);
120static int selrescan(
struct thread *, fd_mask **, fd_mask **);
121static void selfdalloc(
struct thread *,
void *);
123static int dofileread(
struct thread *,
int,
struct file *,
struct uio *,
125static int dofilewrite(
struct thread *,
int,
struct file *,
struct uio *,
129static int seltdwait(
struct thread *, sbintime_t, sbintime_t);
140 struct selfd *st_free1;
141 struct selfd *st_free2;
147#define SELTD_PENDING 0x0001
148#define SELTD_RESCAN 0x0002
155 STAILQ_ENTRY(
selfd) sf_link;
156 TAILQ_ENTRY(
selfd) sf_threads;
157 struct selinfo *sf_si;
168devfs_iosize_max(
void)
171 return (devfs_iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ?
172 INT_MAX : SSIZE_MAX);
179 return (iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ?
180 INT_MAX : SSIZE_MAX);
184#ifndef _SYS_SYSPROTO_H_
198 if (uap->
nbyte > IOSIZE_MAX)
200 aiov.iov_base = uap->
buf;
201 aiov.iov_len = uap->
nbyte;
202 auio.uio_iov = &aiov;
204 auio.uio_resid = uap->
nbyte;
205 auio.uio_segflg = UIO_USERSPACE;
213#ifndef _SYS_SYSPROTO_H_
236 if (nbyte > IOSIZE_MAX)
239 aiov.iov_len = nbyte;
240 auio.uio_iov = &aiov;
242 auio.uio_resid = nbyte;
243 auio.uio_segflg = UIO_USERSPACE;
248#if defined(COMPAT_FREEBSD6)
250freebsd6_pread(
struct thread *td,
struct freebsd6_pread_args *uap)
253 return (
kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
260#ifndef _SYS_SYSPROTO_H_
298#ifndef _SYS_SYSPROTO_H_
329 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
331 else if (offset < 0 &&
332 (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR))
335 error =
dofileread(td,
fd, fp, auio, offset, FOF_OFFSET);
345dofileread(
struct thread *td,
int fd,
struct file *fp,
struct uio *auio,
346 off_t offset,
int flags)
351 struct uio *ktruio = NULL;
357 if (auio->uio_resid == 0) {
358 td->td_retval[0] = 0;
361 auio->uio_rw = UIO_READ;
362 auio->uio_offset = offset;
365 if (KTRPOINT(td, KTR_GENIO))
368 cnt = auio->uio_resid;
369 if ((error = fo_read(fp, auio, td->td_ucred,
flags, td))) {
370 if (auio->uio_resid != cnt && (error == ERESTART ||
371 error == EINTR || error == EWOULDBLOCK))
374 cnt -= auio->uio_resid;
376 if (ktruio != NULL) {
377 ktruio->uio_resid = cnt;
378 ktrgenio(
fd, UIO_READ, ktruio, error);
381 td->td_retval[0] = cnt;
385#ifndef _SYS_SYSPROTO_H_
399 if (uap->
nbyte > IOSIZE_MAX)
401 aiov.iov_base = (
void *)(uintptr_t)uap->
buf;
402 aiov.iov_len = uap->
nbyte;
403 auio.uio_iov = &aiov;
405 auio.uio_resid = uap->
nbyte;
406 auio.uio_segflg = UIO_USERSPACE;
414#ifndef _SYS_SYSPROTO_H_
438 if (nbyte > IOSIZE_MAX)
440 aiov.iov_base = (
void *)(uintptr_t)
buf;
441 aiov.iov_len = nbyte;
442 auio.uio_iov = &aiov;
444 auio.uio_resid = nbyte;
445 auio.uio_segflg = UIO_USERSPACE;
450#if defined(COMPAT_FREEBSD6)
452freebsd6_pwrite(
struct thread *td,
struct freebsd6_pwrite_args *uap)
455 return (
kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
462#ifndef _SYS_SYSPROTO_H_
500#ifndef _SYS_SYSPROTO_H_
531 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
533 else if (offset < 0 &&
534 (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR))
548 off_t offset,
int flags)
553 struct uio *ktruio = NULL;
557 auio->uio_rw = UIO_WRITE;
559 auio->uio_offset = offset;
561 if (KTRPOINT(td, KTR_GENIO))
564 cnt = auio->uio_resid;
565 if ((error = fo_write(fp, auio, td->td_ucred,
flags, td))) {
566 if (auio->uio_resid != cnt && (error == ERESTART ||
567 error == EINTR || error == EWOULDBLOCK))
570 if (fp->f_type != DTYPE_SOCKET && error == EPIPE) {
571 PROC_LOCK(td->td_proc);
573 PROC_UNLOCK(td->td_proc);
576 cnt -= auio->uio_resid;
578 if (ktruio != NULL) {
579 ktruio->uio_resid = cnt;
580 ktrgenio(
fd, UIO_WRITE, ktruio, error);
583 td->td_retval[0] = cnt;
605 AUDIT_ARG_FILE(td->td_proc, fp);
606 if (!(fp->f_flag & FWRITE)) {
610 error = fo_truncate(fp, length, td->td_ucred, td);
615#ifndef _SYS_SYSPROTO_H_
629#if defined(COMPAT_43)
630#ifndef _SYS_SYSPROTO_H_
631struct oftruncate_args {
637oftruncate(
struct thread *td,
struct oftruncate_args *uap)
644#ifndef _SYS_SYSPROTO_H_
662 if (uap->
com > 0xffffffff) {
664 "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
665 td->td_proc->p_pid, td->td_name, uap->
com);
668 com = (uint32_t)uap->
com;
674 size = IOCPARM_LEN(com);
675 if ((size > IOCPARM_MAX) ||
676 ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) ||
677#
if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
678 ((com & IOC_OUT) && size == 0) ||
680 ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
682 ((com & IOC_VOID) && size > 0 && size !=
sizeof(int)))
686 if (com & IOC_VOID) {
688 arg = (intptr_t)uap->
data;
693 data =
malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
700 error = copyin(uap->
data,
data, (u_int)size);
703 }
else if (com & IOC_OUT) {
713 if (error == 0 && (com & IOC_OUT))
714 error = copyout(
data, uap->
data, (u_int)size);
726 struct filedesc *fdp;
727 int error, tmp, locked;
732 fdp = td->td_proc->p_fd;
745 locked = LA_UNLOCKED;
751 if ((fp = fget_noref(fdp,
fd)) == NULL) {
755 if ((error = cap_ioctl_check(fdp,
fd, com)) != 0) {
764 if (locked == LA_SLOCKED) {
765 FILEDESC_SUNLOCK(fdp);
766 locked = LA_UNLOCKED;
775 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
782 fdp->fd_ofiles[
fd].fde_flags &= ~UF_EXCLOSE;
785 fdp->fd_ofiles[
fd].fde_flags |= UF_EXCLOSE;
788 if ((tmp = *(
int *)
data))
789 atomic_set_int(&fp->f_flag, FNONBLOCK);
791 atomic_clear_int(&fp->f_flag, FNONBLOCK);
795 if ((tmp = *(
int *)
data))
796 atomic_set_int(&fp->f_flag, FASYNC);
798 atomic_clear_int(&fp->f_flag, FASYNC);
803 error = fo_ioctl(fp, com,
data, td->td_ucred, td);
807 FILEDESC_XUNLOCK(fdp);
811 FILEDESC_SUNLOCK(fdp);
815 FILEDESC_UNLOCK_ASSERT(fdp);
839 if (offset < 0 || len <= 0)
842 if (offset > OFF_MAX - len)
848 AUDIT_ARG_FILE(td->td_proc, fp);
849 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
853 if ((fp->f_flag & FWRITE) == 0) {
858 error = fo_fallocate(fp, offset, len, td);
867 struct spacectl_range rqsr, rmsr;
870 error = copyin(uap->rqsr, &rqsr,
sizeof(rqsr));
876 if (uap->rmsr != NULL) {
877 cerror = copyout(&rmsr, uap->rmsr,
sizeof(rmsr));
886 const struct spacectl_range *rqsr,
int flags,
struct spacectl_range *rmsrp)
889 struct spacectl_range rmsr;
894 AUDIT_ARG_FFLAGS(
flags);
902 if (cmd != SPACECTL_DEALLOC ||
903 rqsr->r_offset < 0 || rqsr->r_len <= 0 ||
904 rqsr->r_offset > OFF_MAX - rqsr->r_len ||
905 (
flags & ~SPACECTL_F_SUPPORTED) != 0)
911 AUDIT_ARG_FILE(td->td_proc, fp);
912 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
916 if ((fp->f_flag & FWRITE) == 0) {
921 error = fo_fspacectl(fp, cmd, &rmsr.r_offset, &rmsr.r_len,
flags,
924 if (rmsr.r_len != rqsr->r_len && (error == ERESTART ||
925 error == EINTR || error == EWOULDBLOCK))
938 struct specialfd_eventfd *ae;
939 int error,
fd, fflags;
942 error = falloc_noinstall(td, &fp);
947 case SPECIALFD_EVENTFD:
949 if ((ae->flags & EFD_CLOEXEC) != 0)
962 td->td_retval[0] =
fd;
969 struct specialfd_eventfd ae;
972 switch (args->type) {
973 case SPECIALFD_EVENTFD:
974 if (args->len !=
sizeof(
struct specialfd_eventfd)) {
978 error = copyin(args->req, &ae,
sizeof(ae));
981 if ((ae.flags & ~(EFD_CLOEXEC | EFD_NONBLOCK |
982 EFD_SEMAPHORE)) != 0) {
1005 if (events & ~POLLSTANDARD)
1008 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1015 struct timeval tv, *tvp;
1016 sigset_t
set, *uset;
1019 if (uap->ts != NULL) {
1020 error = copyin(uap->ts, &
ts,
sizeof(
ts));
1023 TIMESPEC_TO_TIMEVAL(&tv, &
ts);
1027 if (uap->sm != NULL) {
1028 error = copyin(uap->sm, &
set,
sizeof(
set));
1034 return (
kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
1040 struct timeval *tvp, sigset_t *uset,
int abi_nfdbits)
1046 &td->td_oldsigmask, 0);
1049 td->td_pflags |= TDP_OLDMASK;
1056 td->td_flags |= TDF_ASTPENDING;
1059 error =
kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
1063#ifndef _SYS_SYSPROTO_H_
1073 struct timeval tv, *tvp;
1076 if (uap->
tv != NULL) {
1077 error = copyin(uap->
tv, &tv,
sizeof(tv));
1104 if (nd >= ndu || fd_in == NULL)
1109 for (i = nd; i < ndu; i++) {
1111#if BYTE_ORDER == LITTLE_ENDIAN
1112 addr = (
char *)fd_in + b;
1114 addr = (
char *)fd_in;
1115 if (abi_nfdbits == NFDBITS) {
1116 addr += rounddown(b,
sizeof(fd_mask)) +
1117 sizeof(fd_mask) - 1 - b %
sizeof(fd_mask);
1119 addr += rounddown(b,
sizeof(uint32_t)) +
1120 sizeof(uint32_t) - 1 - b %
sizeof(uint32_t);
1123 if (
addr != oaddr) {
1130 if ((bits & (1 << (i % NBBY))) != 0)
1138 fd_set *fd_ex,
struct timeval *tvp,
int abi_nfdbits)
1140 struct filedesc *fdp;
1147 fd_mask s_selbits[howmany(2048, NFDBITS)];
1148 fd_mask *ibits[3], *obits[3], *selbits, *sbp;
1150 sbintime_t asbt, precision, rsbt;
1151 u_int nbufbytes, ncpbytes, ncpubytes, nfdbits;
1156 fdp = td->td_proc->p_fd;
1158 lf = fdp->fd_nfiles;
1176 nfdbits = roundup(nd, NFDBITS);
1177 ncpbytes = nfdbits / NBBY;
1178 ncpubytes = roundup(nd, abi_nfdbits) / NBBY;
1181 nbufbytes += 2 * ncpbytes;
1183 nbufbytes += 2 * ncpbytes;
1185 nbufbytes += 2 * ncpbytes;
1186 if (nbufbytes <=
sizeof s_selbits)
1187 selbits = &s_selbits[0];
1189 selbits =
malloc(nbufbytes, M_SELECT, M_WAITOK);
1197#define getbits(name, x) \
1199 if (name == NULL) { \
1203 ibits[x] = sbp + nbufbytes / 2 / sizeof *sbp; \
1205 sbp += ncpbytes / sizeof *sbp; \
1206 error = copyin(name, ibits[x], ncpubytes); \
1209 if (ncpbytes != ncpubytes) \
1210 bzero((char *)ibits[x] + ncpubytes, \
1211 ncpbytes - ncpubytes); \
1219#if BYTE_ORDER == BIG_ENDIAN && defined(__LP64__)
1225#define swizzle_fdset(bits) \
1226 if (abi_nfdbits != NFDBITS && bits != NULL) { \
1228 for (i = 0; i < ncpbytes / sizeof *sbp; i++) \
1229 bits[i] = (bits[i] >> 32) | (bits[i] << 32); \
1232#define swizzle_fdset(bits)
1241 bzero(selbits, nbufbytes / 2);
1246 if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1247 rtv.tv_usec >= 1000000) {
1251 if (!timevalisset(&rtv))
1253 else if (rtv.tv_sec <= INT32_MAX) {
1254 rsbt = tvtosbt(rtv);
1257 if (TIMESEL(&asbt, rsbt))
1259 if (asbt <= SBT_MAX - rsbt)
1270 error =
selscan(td, ibits, obits, nd);
1271 if (error || td->td_retval[0] != 0)
1277 if (error || td->td_retval[0] != 0)
1284 if (error == ERESTART)
1286 if (error == EWOULDBLOCK)
1295#define putbits(name, x) \
1296 if (name && (error2 = copyout(obits[x], name, ncpubytes))) \
1306 if (selbits != &s_selbits[0])
1307 free(selbits, M_SELECT);
1318 POLLRDNORM | POLLHUP | POLLERR,
1319 POLLWRNORM | POLLHUP | POLLERR,
1320 POLLRDBAND | POLLERR
1334 for (msk = 0; msk < 3; msk++) {
1335 if (ibits[msk] == NULL)
1337 if ((ibits[msk][idx] & bit) == 0)
1349selsetbits(fd_mask **ibits, fd_mask **obits,
int idx, fd_mask bit,
int events)
1355 for (msk = 0; msk < 3; msk++) {
1358 if (ibits[msk] == NULL)
1360 if ((ibits[msk][idx] & bit) == 0)
1368 if ((obits[msk][idx] & bit) != 0)
1370 obits[msk][idx] |= bit;
1382selrescan(
struct thread *td, fd_mask **ibits, fd_mask **obits)
1384 struct filedesc *fdp;
1395 fdp = td->td_proc->p_fd;
1398 only_user = FILEDESC_IS_ONLY_USER(fdp);
1399 STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1400 fd = (int)(uintptr_t)sfp->sf_cookie;
1410 if (__predict_false(error != 0))
1413 bit = (fd_mask)1 << (
fd % NFDBITS);
1414 ev = fo_poll(fp,
selflags(ibits, idx, bit), td->td_ucred, td);
1416 fput_only_user(fdp, fp);
1423 td->td_retval[0] = n;
1432selscan(
struct thread *td, fd_mask **ibits, fd_mask **obits,
int nfd)
1434 struct filedesc *fdp;
1442 fdp = td->td_proc->p_fd;
1444 only_user = FILEDESC_IS_ONLY_USER(fdp);
1445 for (idx = 0,
fd = 0;
fd < nfd; idx++) {
1446 end = imin(
fd + NFDBITS, nfd);
1447 for (bit = 1;
fd < end; bit <<= 1,
fd++) {
1456 if (__predict_false(error != 0))
1459 ev = fo_poll(fp,
flags, td->td_ucred, td);
1461 fput_only_user(fdp, fp);
1469 td->td_retval[0] = n;
1476 struct timespec
ts, *tsp;
1478 if (uap->timeout != INFTIM) {
1479 if (uap->timeout < 0)
1481 ts.tv_sec = uap->timeout / 1000;
1482 ts.tv_nsec = (uap->timeout % 1000) * 1000000;
1487 return (
kern_poll(td, uap->fds, uap->nfds, tsp, NULL));
1495 struct timespec *tsp, sigset_t *uset)
1497 sbintime_t sbt, precision, tmp;
1504 if (tsp->tv_sec < 0)
1506 if (tsp->tv_nsec < 0 || tsp->tv_nsec >= 1000000000)
1508 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1512 if (
ts.tv_sec > INT32_MAX / 2) {
1513 over =
ts.tv_sec - INT32_MAX / 2;
1520 if (TIMESEL(&sbt, tmp))
1529 &td->td_oldsigmask, 0);
1532 td->td_pflags |= TDP_OLDMASK;
1539 td->td_flags |= TDF_ASTPENDING;
1547 if (error || td->td_retval[0] != 0)
1553 if (error || td->td_retval[0] != 0)
1559 if (error == ERESTART)
1561 if (error == EWOULDBLOCK)
1569 struct timespec
ts, *tsp;
1573 if (uap->ts != NULL) {
1574 error = copyin(uap->ts, &
ts,
sizeof(
ts));
1580 if (uap->set != NULL) {
1581 error = copyin(uap->set, &
set,
sizeof(
set));
1587 return (
kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
1594kern_poll(
struct thread *td,
struct pollfd *ufds, u_int nfds,
1595 struct timespec *tsp, sigset_t *
set)
1597 struct pollfd *kfds;
1598 struct pollfd stackfds[32];
1603 if (nfds > nitems(stackfds))
1604 kfds =
mallocarray(nfds,
sizeof(*kfds), M_TEMP, M_WAITOK);
1607 error = copyin(ufds, kfds, nfds *
sizeof(*kfds));
1613 error =
pollout(td, kfds, ufds, nfds);
1616 if (nfds > nitems(stackfds))
1642 struct filedesc *fdp;
1649 fdp = td->td_proc->p_fd;
1651 only_user = FILEDESC_IS_ONLY_USER(fdp);
1652 STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1653 fd = (
struct pollfd *)sfp->sf_cookie;
1663 if (__predict_false(error != 0)) {
1664 fd->revents = POLLNVAL;
1672 fd->revents = fo_poll(fp,
fd->events, td->td_ucred, td);
1674 fput_only_user(fdp, fp);
1677 if (
fd->revents != 0)
1681 td->td_retval[0] = n;
1686pollout(
struct thread *td,
struct pollfd *fds,
struct pollfd *ufds, u_int nfd)
1692 for (i = 0; i < nfd; i++) {
1693 error = copyout(&fds->revents, &ufds->revents,
1694 sizeof(ufds->revents));
1697 if (fds->revents != 0)
1702 td->td_retval[0] = n;
1707pollscan(
struct thread *td,
struct pollfd *fds, u_int nfd)
1709 struct filedesc *fdp;
1715 fdp = td->td_proc->p_fd;
1716 only_user = FILEDESC_IS_ONLY_USER(fdp);
1717 for (i = 0; i < nfd; i++, fds++) {
1726 if (__predict_false(error != 0)) {
1727 fds->revents = POLLNVAL;
1736 fds->revents = fo_poll(fp, fds->events,
1739 fput_only_user(fdp, fp);
1746 if ((fds->revents & POLLHUP) != 0)
1747 fds->revents &= ~POLLOUT;
1749 if (fds->revents != 0)
1752 td->td_retval[0] = n;
1763selsocket(
struct socket *so,
int events,
struct timeval *tvp,
struct thread *td)
1766 sbintime_t asbt, precision, rsbt;
1772 if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1773 rtv.tv_usec >= 1000000)
1775 if (!timevalisset(&rtv))
1777 else if (rtv.tv_sec <= INT32_MAX) {
1778 rsbt = tvtosbt(rtv);
1781 if (TIMESEL(&asbt, rsbt))
1783 if (asbt <= SBT_MAX - rsbt)
1797 if (
sopoll(so, events, NULL, td) != 0) {
1807 if (error == ERESTART)
1822 if (stp->st_free1 == NULL)
1823 stp->st_free1 =
malloc(
sizeof(*stp->st_free1), M_SELFD, M_WAITOK|M_ZERO);
1824 stp->st_free1->sf_td = stp;
1825 stp->st_free1->sf_cookie = cookie;
1826 if (stp->st_free2 == NULL)
1827 stp->st_free2 =
malloc(
sizeof(*stp->st_free2), M_SELFD, M_WAITOK|M_ZERO);
1828 stp->st_free2->sf_td = stp;
1829 stp->st_free2->sf_cookie = cookie;
1835 STAILQ_REMOVE(&stp->st_selq, sfp,
selfd, sf_link);
1839 if (atomic_load_acq_ptr((uintptr_t *)&sfp->sf_si) != (uintptr_t)NULL) {
1840 mtx_lock(sfp->sf_mtx);
1841 if (sfp->sf_si != NULL) {
1842 TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1844 mtx_unlock(sfp->sf_mtx);
1875 stp = selector->td_sel;
1885 if ((sfp = stp->st_free1) != NULL)
1886 stp->st_free1 = NULL;
1887 else if ((sfp = stp->st_free2) != NULL)
1888 stp->st_free2 = NULL;
1890 panic(
"selrecord: No free selfd on selq");
1899 STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1904 if (sip->si_mtx == NULL) {
1906 TAILQ_INIT(&sip->si_tdlist);
1911 TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1912 mtx_unlock(sip->si_mtx);
1940 if (sip->si_mtx == NULL)
1945 mtx_lock(sip->si_mtx);
1946 TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1951 TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1953 mtx_lock(&stp->st_mtx);
1956 mtx_unlock(&stp->st_mtx);
1963 atomic_store_rel_ptr((uintptr_t *)&sfp->sf_si, (uintptr_t)NULL);
1965 mtx_unlock(sip->si_mtx);
1975 MPASS(stp->st_flags == 0);
1976 MPASS(STAILQ_EMPTY(&stp->st_selq));
1979 stp =
malloc(
sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1980 mtx_init(&stp->st_mtx,
"sellck", NULL, MTX_DEF);
1981 cv_init(&stp->st_wait,
"select");
1983 STAILQ_INIT(&stp->st_selq);
1988seltdwait(
struct thread *td, sbintime_t sbt, sbintime_t precision)
1998 mtx_lock(&stp->st_mtx);
2004 mtx_unlock(&stp->st_mtx);
2008 error = EWOULDBLOCK;
2010 error = cv_timedwait_sig_sbt(&stp->st_wait, &stp->st_mtx,
2011 sbt, precision, C_ABSOLUTE);
2013 error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
2014 mtx_unlock(&stp->st_mtx);
2027 MPASS(stp->st_flags == 0);
2028 MPASS(STAILQ_EMPTY(&stp->st_selq));
2030 free(stp->st_free1, M_SELFD);
2032 free(stp->st_free2, M_SELFD);
2035 mtx_destroy(&stp->st_mtx);
2036 free(stp, M_SELECT);
2051 STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
2075 td->td_errno = error;
2076 td->td_pflags |= TDP_NERRNO;
2077 td->td_retval[0] = error;
device_property_type_t type
SYSCTL_INT(ASLR_NODE_OID, OID_AUTO, enable, CTLFLAG_RWTUN, &__elfN(aslr_enabled), 0, ": enable address map randomization")
void cv_init(struct cv *cvp, const char *desc)
void cv_destroy(struct cv *cvp)
void cv_broadcastpri(struct cv *cvp, int pri)
static STAILQ_HEAD(cn_device)
int finstall(struct thread *td, struct file *fp, int *fd, int flags, struct filecaps *fcaps)
int fget_write(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
int fget_only_user(struct filedesc *fdp, int fd, cap_rights_t *needrightsp, struct file **fpp)
int fget(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
int fget_unlocked(struct thread *td, int fd, cap_rights_t *needrightsp, struct file **fpp)
int fget_read(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
void *() malloc(size_t size, struct malloc_type *mtp, int flags)
void * mallocarray(size_t nmemb, size_t size, struct malloc_type *type, int flags)
void free(void *addr, struct malloc_type *mtp)
struct mtx_pool * mtx_pool_create(const char *mtx_name, int pool_size, int opts)
struct mtx * mtx_pool_find(struct mtx_pool *pool, void *ptr)
void panic(const char *fmt,...)
void tdsignal(struct thread *td, int sig)
int kern_sigprocmask(struct thread *td, int how, sigset_t *set, sigset_t *oset, int flags)
struct tidbatch __aligned
__read_mostly cap_rights_t cap_event_rights
__read_mostly cap_rights_t cap_write_rights
__read_mostly cap_rights_t cap_pread_rights
__read_mostly cap_rights_t cap_pwrite_rights
__read_mostly cap_rights_t cap_ioctl_rights
__read_mostly cap_rights_t cap_ftruncate_rights
__read_mostly cap_rights_t cap_read_rights
int printf(const char *fmt,...)
int copyinuio(const struct iovec *iovp, u_int iovcnt, struct uio **uiop)
struct uio * cloneuio(struct uio *uiop)
int eventfd_create_file(struct thread *td, struct file *fp, uint32_t initval, int flags)
void selwakeuppri(struct selinfo *sip, int pri)
int sys_fspacectl(struct thread *td, struct fspacectl_args *uap)
static const int select_flags[3]
int sys_poll(struct thread *td, struct poll_args *uap)
int sys_select(struct thread *td, struct select_args *uap)
void seldrain(struct selinfo *sip)
int kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
int sys_write(struct thread *td, struct write_args *uap)
static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer")
static void seltdinit(struct thread *)
int sys_pselect(struct thread *td, struct pselect_args *uap)
static void selfdfree(struct seltd *, struct selfd *)
int kern_poll(struct thread *td, struct pollfd *ufds, u_int nfds, struct timespec *tsp, sigset_t *set)
int sys_ppoll(struct thread *td, struct ppoll_args *uap)
static int selrescan(struct thread *, fd_mask **, fd_mask **)
static __inline int selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
void selrecord(struct thread *selector, struct selinfo *sip)
int sys_readv(struct thread *td, struct readv_args *uap)
#define swizzle_fdset(bits)
int kern_fspacectl(struct thread *td, int fd, int cmd, const struct spacectl_range *rqsr, int flags, struct spacectl_range *rmsrp)
static int seltdwait(struct thread *, sbintime_t, sbintime_t)
static void selectinit(void *)
static int selscan(struct thread *, fd_mask **, fd_mask **, int)
static int pollout(struct thread *, struct pollfd *, struct pollfd *, u_int)
int kern_posix_error(struct thread *td, int error)
int sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
int kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
int kern_specialfd(struct thread *td, int type, void *arg)
#define SYS_IOCTL_SMALL_ALIGN
static void selfdalloc(struct thread *, void *)
int sys___specialfd(struct thread *td, struct __specialfd_args *args)
static void seltdclear(struct thread *)
int kern_readv(struct thread *td, int fd, struct uio *auio)
int sys_pwrite(struct thread *td, struct pwrite_args *uap)
static __inline int selflags(fd_mask **ibits, int idx, fd_mask bit)
int sys_read(struct thread *td, struct read_args *uap)
#define SYS_IOCTL_SMALL_SIZE
static int pollscan(struct thread *, struct pollfd *, u_int)
int kern_writev(struct thread *td, int fd, struct uio *auio)
int kern_pwritev(struct thread *td, int fd, struct uio *auio, off_t offset)
int kern_preadv(struct thread *td, int fd, struct uio *auio, off_t offset)
int sys_pwritev(struct thread *td, struct pwritev_args *uap)
int sys_preadv(struct thread *td, struct preadv_args *uap)
void selwakeup(struct selinfo *sip)
int poll_no_poll(int events)
int selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td)
int kern_pread(struct thread *td, int fd, void *buf, size_t nbyte, off_t offset)
int kern_pwrite(struct thread *td, int fd, const void *buf, size_t nbyte, off_t offset)
static int dofilewrite(struct thread *, int, struct file *, struct uio *, off_t, int)
int sys_writev(struct thread *td, struct writev_args *uap)
static void doselwakeup(struct selinfo *, int)
int sys_ftruncate(struct thread *td, struct ftruncate_args *uap)
void seltdfini(struct thread *td)
int kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex, struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
bool kern_poll_maxfds(u_int nfds)
SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL)
static int dofileread(struct thread *, int, struct file *, struct uio *, off_t, int)
CTASSERT(sizeof(register_t) >=sizeof(size_t))
int sys_ioctl(struct thread *td, struct ioctl_args *uap)
int sys_pread(struct thread *td, struct pread_args *uap)
int kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou, fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
static struct mtx_pool * mtxpool_select
static int select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits)
int kern_ftruncate(struct thread *td, int fd, off_t length)
int kern_poll_kfds(struct thread *td, struct pollfd *kfds, u_int nfds, struct timespec *tsp, sigset_t *uset)
static int pollrescan(struct thread *)
int sopoll(struct socket *so, int events, struct ucred *active_cred, struct thread *td)