Index: sys/compat/freebsd32/freebsd32_misc.c =================================================================== --- sys/compat/freebsd32/freebsd32_misc.c +++ sys/compat/freebsd32/freebsd32_misc.c @@ -3771,7 +3771,7 @@ } else ssp = NULL; - return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp)); + return (kern_poll_ufds(td, uap->fds, uap->nfds, tsp, ssp)); } int Index: sys/compat/linux/linux_misc.c =================================================================== --- sys/compat/linux/linux_misc.c +++ sys/compat/linux/linux_misc.c @@ -2459,7 +2459,7 @@ } else tsp = NULL; - error = kern_poll(td, args->fds, args->nfds, tsp, ssp); + error = kern_poll_ufds(td, args->fds, args->nfds, tsp, ssp); if (error == 0 && args->tsp != NULL) { if (td->td_retval[0]) { Index: sys/kern/sys_generic.c =================================================================== --- sys/kern/sys_generic.c +++ sys/kern/sys_generic.c @@ -128,6 +128,8 @@ static void seltdinit(struct thread *); static int seltdwait(struct thread *, sbintime_t, sbintime_t); static void seltdclear(struct thread *); +static int kern_poll(struct thread *, struct pollfd *, u_int, + struct timespec *, sigset_t *); /* * One seltd per-thread allocated on demand as needed. @@ -1414,15 +1416,13 @@ } else tsp = NULL; - return (kern_poll(td, uap->fds, uap->nfds, tsp, NULL)); + return (kern_poll_ufds(td, uap->fds, uap->nfds, tsp, NULL)); } -int -kern_poll(struct thread *td, struct pollfd *ufds, u_int nfds, +static int +kern_poll(struct thread *td, struct pollfd *kfds, u_int nfds, struct timespec *tsp, sigset_t *uset) { - struct pollfd *kfds; - struct pollfd stackfds[32]; sbintime_t sbt, precision, tmp; time_t over; struct timespec ts; @@ -1453,28 +1453,11 @@ } else sbt = -1; - /* - * This is kinda bogus. We have fd limits, but that is not - * really related to the size of the pollfd array. Make sure - * we let the process use at least FD_SETSIZE entries and at - * least enough for the system-wide limits. We want to be reasonably - * safe, but not overly restrictive. - */ - if (nfds > maxfilesperproc && nfds > FD_SETSIZE) - return (EINVAL); - if (nfds > nitems(stackfds)) - kfds = mallocarray(nfds, sizeof(*kfds), M_TEMP, M_WAITOK); - else - kfds = stackfds; - error = copyin(ufds, kfds, nfds * sizeof(*kfds)); - if (error) - goto done; - if (uset != NULL) { error = kern_sigprocmask(td, SIG_SETMASK, uset, &td->td_oldsigmask, 0); if (error) - goto done; + return (error); td->td_pflags |= TDP_OLDMASK; /* * Make sure that ast() is called on return to @@ -1501,20 +1484,11 @@ } seltdclear(td); -done: /* poll is not restarted after signals... */ if (error == ERESTART) error = EINTR; if (error == EWOULDBLOCK) error = 0; - if (error == 0) { - error = pollout(td, kfds, ufds, nfds); - if (error) - goto out; - } -out: - if (nfds > nitems(stackfds)) - free(kfds, M_TEMP); return (error); } @@ -1540,11 +1514,58 @@ } else ssp = NULL; /* - * fds is still a pointer to user space. kern_poll() will + * fds is still a pointer to user space. kern_poll_ufds() will * take care of copyin that array to the kernel space. */ - return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp)); + return (kern_poll_ufds(td, uap->fds, uap->nfds, tsp, ssp)); +} + +int +kern_poll_ufds(struct thread *td, struct pollfd *ufds, u_int nfds, + struct timespec *tsp, sigset_t *set) +{ + struct pollfd *kfds; + struct pollfd stackfds[32]; + int error; + + error = kern_poll_maxfds(nfds); + if (error != 0) + return (error); + + if (nfds > nitems(stackfds)) + kfds = mallocarray(nfds, sizeof(*kfds), M_TEMP, M_WAITOK); + else + kfds = stackfds; + error = copyin(ufds, kfds, nfds * sizeof(*kfds)); + if (error != 0) + goto out; + + error = kern_poll(td, kfds, nfds, tsp, set); + if (error == 0) + error = pollout(td, kfds, ufds, nfds); + +out: + if (nfds > nitems(stackfds)) + free(kfds, M_TEMP); + return (error); +} + +int +kern_poll_maxfds(u_int nfds) +{ + + /* + * This is kinda bogus. We have fd limits, but that is not + * really related to the size of the pollfd array. Make sure + * we let the process use at least FD_SETSIZE entries and at + * least enough for the system-wide limits. We want to be reasonably + * safe, but not overly restrictive. + */ + if (nfds > maxfilesperproc && nfds > FD_SETSIZE) + return (EINVAL); + else + return (0); } static int Index: sys/sys/syscallsubr.h =================================================================== --- sys/sys/syscallsubr.h +++ sys/sys/syscallsubr.h @@ -223,8 +223,9 @@ enum uio_seg pathseg, int name, u_long flags, long *valuep); int kern_pipe(struct thread *td, int fildes[2], int flags, struct filecaps *fcaps1, struct filecaps *fcaps2); -int kern_poll(struct thread *td, struct pollfd *fds, u_int nfds, +int kern_poll_ufds(struct thread *td, struct pollfd *fds, u_int nfds, struct timespec *tsp, sigset_t *uset); +int kern_poll_maxfds(u_int nfds); int kern_posix_error(struct thread *td, int error); int kern_posix_fadvise(struct thread *td, int fd, off_t offset, off_t len, int advice);