2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #include <sys/cdefs.h>
38 #include "opt_capsicum.h"
39 #include "opt_ktrace.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/sysproto.h>
44 #include <sys/capsicum.h>
45 #include <sys/filedesc.h>
46 #include <sys/filio.h>
47 #include <sys/fcntl.h>
51 #include <sys/signalvar.h>
52 #include <sys/socketvar.h>
54 #include <sys/eventfd.h>
55 #include <sys/kernel.h>
57 #include <sys/limits.h>
58 #include <sys/malloc.h>
60 #include <sys/resourcevar.h>
61 #include <sys/selinfo.h>
62 #include <sys/sleepqueue.h>
63 #include <sys/specialfd.h>
64 #include <sys/syscallsubr.h>
65 #include <sys/sysctl.h>
66 #include <sys/sysent.h>
67 #include <sys/vnode.h>
68 #include <sys/unistd.h>
71 #include <sys/condvar.h>
73 #include <sys/ktrace.h>
76 #include <security/audit/audit.h>
79 * The following macro defines how many bytes will be allocated from
80 * the stack instead of memory allocated when passing the IOCTL data
81 * structures from userspace and to the kernel. Some IOCTLs having
82 * small data structures are used very frequently and this small
83 * buffer on the stack gives a significant speedup improvement for
84 * those requests. The value of this define should be greater or equal
85 * to 64 bytes and should also be power of two. The data structure is
86 * currently hard-aligned to a 8-byte boundary on the stack. This
87 * should currently be sufficient for all supported platforms.
89 #define SYS_IOCTL_SMALL_SIZE 128 /* bytes */
90 #define SYS_IOCTL_SMALL_ALIGN 8 /* bytes */
93 static int iosize_max_clamp = 0;
94 SYSCTL_INT(_debug, OID_AUTO, iosize_max_clamp, CTLFLAG_RW,
95 &iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX");
96 static int devfs_iosize_max_clamp = 1;
97 SYSCTL_INT(_debug, OID_AUTO, devfs_iosize_max_clamp, CTLFLAG_RW,
98 &devfs_iosize_max_clamp, 0, "Clamp max i/o size to INT_MAX for devices");
102 * Assert that the return value of read(2) and write(2) syscalls fits
103 * into a register. If not, an architecture will need to provide the
104 * usermode wrappers to reconstruct the result.
106 CTASSERT(sizeof(register_t) >= sizeof(size_t));
108 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
109 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
110 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
112 static int pollout(struct thread *, struct pollfd *, struct pollfd *,
114 static int pollscan(struct thread *, struct pollfd *, u_int);
115 static int pollrescan(struct thread *);
116 static int selscan(struct thread *, fd_mask **, fd_mask **, int);
117 static int selrescan(struct thread *, fd_mask **, fd_mask **);
118 static void selfdalloc(struct thread *, void *);
119 static void selfdfree(struct seltd *, struct selfd *);
120 static int dofileread(struct thread *, int, struct file *, struct uio *,
122 static int dofilewrite(struct thread *, int, struct file *, struct uio *,
124 static void doselwakeup(struct selinfo *, int);
125 static void seltdinit(struct thread *);
126 static int seltdwait(struct thread *, sbintime_t, sbintime_t);
127 static void seltdclear(struct thread *);
130 * One seltd per-thread allocated on demand as needed.
132 * t - protected by st_mtx
133 * k - Only accessed by curthread or read-only
136 STAILQ_HEAD(, selfd) st_selq; /* (k) List of selfds. */
137 struct selfd *st_free1; /* (k) free fd for read set. */
138 struct selfd *st_free2; /* (k) free fd for write set. */
139 struct mtx st_mtx; /* Protects struct seltd */
140 struct cv st_wait; /* (t) Wait channel. */
141 int st_flags; /* (t) SELTD_ flags. */
144 #define SELTD_PENDING 0x0001 /* We have pending events. */
145 #define SELTD_RESCAN 0x0002 /* Doing a rescan. */
148 * One selfd allocated per-thread per-file-descriptor.
149 * f - protected by sf_mtx
152 STAILQ_ENTRY(selfd) sf_link; /* (k) fds owned by this td. */
153 TAILQ_ENTRY(selfd) sf_threads; /* (f) fds on this selinfo. */
154 struct selinfo *sf_si; /* (f) selinfo when linked. */
155 struct mtx *sf_mtx; /* Pointer to selinfo mtx. */
156 struct seltd *sf_td; /* (k) owning seltd. */
157 void *sf_cookie; /* (k) fd or pollfd. */
160 MALLOC_DEFINE(M_SELFD, "selfd", "selfd");
161 static struct mtx_pool *mtxpool_select;
165 devfs_iosize_max(void)
168 return (devfs_iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ?
169 INT_MAX : SSIZE_MAX);
176 return (iosize_max_clamp || SV_CURPROC_FLAG(SV_ILP32) ?
177 INT_MAX : SSIZE_MAX);
181 #ifndef _SYS_SYSPROTO_H_
189 sys_read(struct thread *td, struct read_args *uap)
195 if (uap->nbyte > IOSIZE_MAX)
197 aiov.iov_base = uap->buf;
198 aiov.iov_len = uap->nbyte;
199 auio.uio_iov = &aiov;
201 auio.uio_resid = uap->nbyte;
202 auio.uio_segflg = UIO_USERSPACE;
203 error = kern_readv(td, uap->fd, &auio);
208 * Positioned read system call
210 #ifndef _SYS_SYSPROTO_H_
220 sys_pread(struct thread *td, struct pread_args *uap)
223 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
227 kern_pread(struct thread *td, int fd, void *buf, size_t nbyte, off_t offset)
233 if (nbyte > IOSIZE_MAX)
236 aiov.iov_len = nbyte;
237 auio.uio_iov = &aiov;
239 auio.uio_resid = nbyte;
240 auio.uio_segflg = UIO_USERSPACE;
241 error = kern_preadv(td, fd, &auio, offset);
245 #if defined(COMPAT_FREEBSD6)
247 freebsd6_pread(struct thread *td, struct freebsd6_pread_args *uap)
250 return (kern_pread(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
255 * Scatter read system call.
257 #ifndef _SYS_SYSPROTO_H_
265 sys_readv(struct thread *td, struct readv_args *uap)
270 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
273 error = kern_readv(td, uap->fd, auio);
279 kern_readv(struct thread *td, int fd, struct uio *auio)
284 error = fget_read(td, fd, &cap_read_rights, &fp);
287 error = dofileread(td, fd, fp, auio, (off_t)-1, 0);
293 * Scatter positioned read system call.
295 #ifndef _SYS_SYSPROTO_H_
304 sys_preadv(struct thread *td, struct preadv_args *uap)
309 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
312 error = kern_preadv(td, uap->fd, auio, uap->offset);
318 kern_preadv(struct thread *td, int fd, struct uio *auio, off_t offset)
323 error = fget_read(td, fd, &cap_pread_rights, &fp);
326 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
328 else if (offset < 0 &&
329 (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR))
332 error = dofileread(td, fd, fp, auio, offset, FOF_OFFSET);
338 * Common code for readv and preadv that reads data in
339 * from a file using the passed in uio, offset, and flags.
342 dofileread(struct thread *td, int fd, struct file *fp, struct uio *auio,
343 off_t offset, int flags)
348 struct uio *ktruio = NULL;
353 /* Finish zero length reads right here */
354 if (auio->uio_resid == 0) {
355 td->td_retval[0] = 0;
358 auio->uio_rw = UIO_READ;
359 auio->uio_offset = offset;
362 if (KTRPOINT(td, KTR_GENIO))
363 ktruio = cloneuio(auio);
365 cnt = auio->uio_resid;
366 if ((error = fo_read(fp, auio, td->td_ucred, flags, td))) {
367 if (auio->uio_resid != cnt && (error == ERESTART ||
368 error == EINTR || error == EWOULDBLOCK))
371 cnt -= auio->uio_resid;
373 if (ktruio != NULL) {
374 ktruio->uio_resid = cnt;
375 ktrgenio(fd, UIO_READ, ktruio, error);
378 td->td_retval[0] = cnt;
382 #ifndef _SYS_SYSPROTO_H_
390 sys_write(struct thread *td, struct write_args *uap)
396 if (uap->nbyte > IOSIZE_MAX)
398 aiov.iov_base = (void *)(uintptr_t)uap->buf;
399 aiov.iov_len = uap->nbyte;
400 auio.uio_iov = &aiov;
402 auio.uio_resid = uap->nbyte;
403 auio.uio_segflg = UIO_USERSPACE;
404 error = kern_writev(td, uap->fd, &auio);
409 * Positioned write system call.
411 #ifndef _SYS_SYSPROTO_H_
421 sys_pwrite(struct thread *td, struct pwrite_args *uap)
424 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
428 kern_pwrite(struct thread *td, int fd, const void *buf, size_t nbyte,
435 if (nbyte > IOSIZE_MAX)
437 aiov.iov_base = (void *)(uintptr_t)buf;
438 aiov.iov_len = nbyte;
439 auio.uio_iov = &aiov;
441 auio.uio_resid = nbyte;
442 auio.uio_segflg = UIO_USERSPACE;
443 error = kern_pwritev(td, fd, &auio, offset);
447 #if defined(COMPAT_FREEBSD6)
449 freebsd6_pwrite(struct thread *td, struct freebsd6_pwrite_args *uap)
452 return (kern_pwrite(td, uap->fd, uap->buf, uap->nbyte, uap->offset));
457 * Gather write system call.
459 #ifndef _SYS_SYSPROTO_H_
467 sys_writev(struct thread *td, struct writev_args *uap)
472 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
475 error = kern_writev(td, uap->fd, auio);
481 kern_writev(struct thread *td, int fd, struct uio *auio)
486 error = fget_write(td, fd, &cap_write_rights, &fp);
489 error = dofilewrite(td, fd, fp, auio, (off_t)-1, 0);
495 * Gather positioned write system call.
497 #ifndef _SYS_SYSPROTO_H_
498 struct pwritev_args {
506 sys_pwritev(struct thread *td, struct pwritev_args *uap)
511 error = copyinuio(uap->iovp, uap->iovcnt, &auio);
514 error = kern_pwritev(td, uap->fd, auio, uap->offset);
520 kern_pwritev(struct thread *td, int fd, struct uio *auio, off_t offset)
525 error = fget_write(td, fd, &cap_pwrite_rights, &fp);
528 if (!(fp->f_ops->fo_flags & DFLAG_SEEKABLE))
530 else if (offset < 0 &&
531 (fp->f_vnode == NULL || fp->f_vnode->v_type != VCHR))
534 error = dofilewrite(td, fd, fp, auio, offset, FOF_OFFSET);
540 * Common code for writev and pwritev that writes data to
541 * a file using the passed in uio, offset, and flags.
544 dofilewrite(struct thread *td, int fd, struct file *fp, struct uio *auio,
545 off_t offset, int flags)
550 struct uio *ktruio = NULL;
554 auio->uio_rw = UIO_WRITE;
556 auio->uio_offset = offset;
558 if (KTRPOINT(td, KTR_GENIO))
559 ktruio = cloneuio(auio);
561 cnt = auio->uio_resid;
562 error = fo_write(fp, auio, td->td_ucred, flags, td);
564 * Socket layer is responsible for special error handling,
567 if (error != 0 && fp->f_type != DTYPE_SOCKET) {
568 if (auio->uio_resid != cnt && (error == ERESTART ||
569 error == EINTR || error == EWOULDBLOCK))
571 if (error == EPIPE) {
572 PROC_LOCK(td->td_proc);
573 tdsignal(td, SIGPIPE);
574 PROC_UNLOCK(td->td_proc);
577 cnt -= auio->uio_resid;
579 if (ktruio != NULL) {
581 ktruio->uio_resid = cnt;
582 ktrgenio(fd, UIO_WRITE, ktruio, error);
585 td->td_retval[0] = cnt;
590 * Truncate a file given a file descriptor.
592 * Can't use fget_write() here, since must return EINVAL and not EBADF if the
593 * descriptor isn't writable.
596 kern_ftruncate(struct thread *td, int fd, off_t length)
604 error = fget(td, fd, &cap_ftruncate_rights, &fp);
607 AUDIT_ARG_FILE(td->td_proc, fp);
608 if (!(fp->f_flag & FWRITE)) {
612 error = fo_truncate(fp, length, td->td_ucred, td);
617 #ifndef _SYS_SYSPROTO_H_
618 struct ftruncate_args {
625 sys_ftruncate(struct thread *td, struct ftruncate_args *uap)
628 return (kern_ftruncate(td, uap->fd, uap->length));
631 #if defined(COMPAT_43)
632 #ifndef _SYS_SYSPROTO_H_
633 struct oftruncate_args {
639 oftruncate(struct thread *td, struct oftruncate_args *uap)
642 return (kern_ftruncate(td, uap->fd, uap->length));
644 #endif /* COMPAT_43 */
646 #ifndef _SYS_SYSPROTO_H_
655 sys_ioctl(struct thread *td, struct ioctl_args *uap)
657 u_char smalldata[SYS_IOCTL_SMALL_SIZE] __aligned(SYS_IOCTL_SMALL_ALIGN);
664 if (uap->com > 0xffffffff) {
666 "WARNING pid %d (%s): ioctl sign-extension ioctl %lx\n",
667 td->td_proc->p_pid, td->td_name, uap->com);
670 com = (uint32_t)uap->com;
673 * Interpret high order word to find amount of data to be
674 * copied to/from the user's address space.
676 size = IOCPARM_LEN(com);
677 if ((size > IOCPARM_MAX) ||
678 ((com & (IOC_VOID | IOC_IN | IOC_OUT)) == 0) ||
679 #if defined(COMPAT_FREEBSD5) || defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
680 ((com & IOC_OUT) && size == 0) ||
682 ((com & (IOC_IN | IOC_OUT)) && size == 0) ||
684 ((com & IOC_VOID) && size > 0 && size != sizeof(int)))
688 if (com & IOC_VOID) {
689 /* Integer argument. */
690 arg = (intptr_t)uap->data;
694 if (size > SYS_IOCTL_SMALL_SIZE)
695 data = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
700 data = (void *)&uap->data;
702 error = copyin(uap->data, data, (u_int)size);
705 } else if (com & IOC_OUT) {
707 * Zero the buffer so the user always
708 * gets back something deterministic.
713 error = kern_ioctl(td, uap->fd, com, data);
715 if (error == 0 && (com & IOC_OUT))
716 error = copyout(data, uap->data, (u_int)size);
719 if (size > SYS_IOCTL_SMALL_SIZE)
720 free(data, M_IOCTLOPS);
725 kern_ioctl(struct thread *td, int fd, u_long com, caddr_t data)
728 struct filedesc *fdp;
729 int error, tmp, locked;
734 fdp = td->td_proc->p_fd;
747 locked = LA_UNLOCKED;
753 if ((fp = fget_noref(fdp, fd)) == NULL) {
757 if ((error = cap_ioctl_check(fdp, fd, com)) != 0) {
758 fp = NULL; /* fhold() was not called yet */
766 if (locked == LA_SLOCKED) {
767 FILEDESC_SUNLOCK(fdp);
768 locked = LA_UNLOCKED;
771 error = fget(td, fd, &cap_ioctl_rights, &fp);
777 if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
784 fdp->fd_ofiles[fd].fde_flags &= ~UF_EXCLOSE;
787 fdp->fd_ofiles[fd].fde_flags |= UF_EXCLOSE;
790 if ((tmp = *(int *)data))
791 atomic_set_int(&fp->f_flag, FNONBLOCK);
793 atomic_clear_int(&fp->f_flag, FNONBLOCK);
797 if ((tmp = *(int *)data))
798 atomic_set_int(&fp->f_flag, FASYNC);
800 atomic_clear_int(&fp->f_flag, FASYNC);
805 error = fo_ioctl(fp, com, data, td->td_ucred, td);
809 FILEDESC_XUNLOCK(fdp);
813 FILEDESC_SUNLOCK(fdp);
817 FILEDESC_UNLOCK_ASSERT(fdp);
826 sys_posix_fallocate(struct thread *td, struct posix_fallocate_args *uap)
830 error = kern_posix_fallocate(td, uap->fd, uap->offset, uap->len);
831 return (kern_posix_error(td, error));
835 kern_posix_fallocate(struct thread *td, int fd, off_t offset, off_t len)
841 if (offset < 0 || len <= 0)
843 /* Check for wrap. */
844 if (offset > OFF_MAX - len)
847 error = fget(td, fd, &cap_pwrite_rights, &fp);
850 AUDIT_ARG_FILE(td->td_proc, fp);
851 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
855 if ((fp->f_flag & FWRITE) == 0) {
860 error = fo_fallocate(fp, offset, len, td);
867 sys_fspacectl(struct thread *td, struct fspacectl_args *uap)
869 struct spacectl_range rqsr, rmsr;
872 error = copyin(uap->rqsr, &rqsr, sizeof(rqsr));
876 error = kern_fspacectl(td, uap->fd, uap->cmd, &rqsr, uap->flags,
878 if (uap->rmsr != NULL) {
879 cerror = copyout(&rmsr, uap->rmsr, sizeof(rmsr));
887 kern_fspacectl(struct thread *td, int fd, int cmd,
888 const struct spacectl_range *rqsr, int flags, struct spacectl_range *rmsrp)
891 struct spacectl_range rmsr;
896 AUDIT_ARG_FFLAGS(flags);
904 if (cmd != SPACECTL_DEALLOC ||
905 rqsr->r_offset < 0 || rqsr->r_len <= 0 ||
906 rqsr->r_offset > OFF_MAX - rqsr->r_len ||
907 (flags & ~SPACECTL_F_SUPPORTED) != 0)
910 error = fget_write(td, fd, &cap_pwrite_rights, &fp);
913 AUDIT_ARG_FILE(td->td_proc, fp);
914 if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) {
918 if ((fp->f_flag & FWRITE) == 0) {
923 error = fo_fspacectl(fp, cmd, &rmsr.r_offset, &rmsr.r_len, flags,
925 /* fspacectl is not restarted after signals if the file is modified. */
926 if (rmsr.r_len != rqsr->r_len && (error == ERESTART ||
927 error == EINTR || error == EWOULDBLOCK))
937 kern_specialfd(struct thread *td, int type, void *arg)
940 struct specialfd_eventfd *ae;
941 int error, fd, fflags;
944 error = falloc_noinstall(td, &fp);
949 case SPECIALFD_EVENTFD:
951 if ((ae->flags & EFD_CLOEXEC) != 0)
953 error = eventfd_create_file(td, fp, ae->initval, ae->flags);
961 error = finstall(td, fp, &fd, fflags, NULL);
964 td->td_retval[0] = fd;
969 sys___specialfd(struct thread *td, struct __specialfd_args *args)
971 struct specialfd_eventfd ae;
974 switch (args->type) {
975 case SPECIALFD_EVENTFD:
976 if (args->len != sizeof(struct specialfd_eventfd)) {
980 error = copyin(args->req, &ae, sizeof(ae));
983 if ((ae.flags & ~(EFD_CLOEXEC | EFD_NONBLOCK |
984 EFD_SEMAPHORE)) != 0) {
988 error = kern_specialfd(td, args->type, &ae);
998 poll_no_poll(int events)
1001 * Return true for read/write. If the user asked for something
1002 * special, return POLLNVAL, so that clients have a way of
1003 * determining reliably whether or not the extended
1004 * functionality is present without hard-coding knowledge
1005 * of specific filesystem implementations.
1007 if (events & ~POLLSTANDARD)
1010 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
1014 sys_pselect(struct thread *td, struct pselect_args *uap)
1017 struct timeval tv, *tvp;
1018 sigset_t set, *uset;
1021 if (uap->ts != NULL) {
1022 error = copyin(uap->ts, &ts, sizeof(ts));
1025 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1029 if (uap->sm != NULL) {
1030 error = copyin(uap->sm, &set, sizeof(set));
1036 return (kern_pselect(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
1041 kern_pselect(struct thread *td, int nd, fd_set *in, fd_set *ou, fd_set *ex,
1042 struct timeval *tvp, sigset_t *uset, int abi_nfdbits)
1047 error = kern_sigprocmask(td, SIG_SETMASK, uset,
1048 &td->td_oldsigmask, 0);
1051 td->td_pflags |= TDP_OLDMASK;
1053 * Make sure that ast() is called on return to
1054 * usermode and TDP_OLDMASK is cleared, restoring old
1057 ast_sched(td, TDA_SIGSUSPEND);
1059 error = kern_select(td, nd, in, ou, ex, tvp, abi_nfdbits);
1063 #ifndef _SYS_SYSPROTO_H_
1064 struct select_args {
1066 fd_set *in, *ou, *ex;
1071 sys_select(struct thread *td, struct select_args *uap)
1073 struct timeval tv, *tvp;
1076 if (uap->tv != NULL) {
1077 error = copyin(uap->tv, &tv, sizeof(tv));
1084 return (kern_select(td, uap->nd, uap->in, uap->ou, uap->ex, tvp,
1089 * In the unlikely case when user specified n greater then the last
1090 * open file descriptor, check that no bits are set after the last
1091 * valid fd. We must return EBADF if any is set.
1093 * There are applications that rely on the behaviour.
1098 select_check_badfd(fd_set *fd_in, int nd, int ndu, int abi_nfdbits)
1104 if (nd >= ndu || fd_in == NULL)
1108 bits = 0; /* silence gcc */
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)
1137 kern_select(struct thread *td, int nd, fd_set *fd_in, fd_set *fd_ou,
1138 fd_set *fd_ex, struct timeval *tvp, int abi_nfdbits)
1140 struct filedesc *fdp;
1142 * The magic 2048 here is chosen to be just enough for FD_SETSIZE
1143 * infds with the new FD_SETSIZE of 1024, and more than enough for
1144 * FD_SETSIZE infds, outfds and exceptfds with the old FD_SETSIZE
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;
1162 error = select_check_badfd(fd_in, nd, ndu, abi_nfdbits);
1165 error = select_check_badfd(fd_ou, nd, ndu, abi_nfdbits);
1168 error = select_check_badfd(fd_ex, nd, ndu, abi_nfdbits);
1173 * Allocate just enough bits for the non-null fd_sets. Use the
1174 * preallocated auto buffer if possible.
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);
1192 * Assign pointers into the bit buffers and fetch the input bits.
1193 * Put the output buffers together so that they can be bzeroed
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__)
1221 * XXX: swizzle_fdset assumes that if abi_nfdbits != NFDBITS,
1222 * we are running under 32-bit emulation. This should be more
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)
1235 /* Make sure the bit order makes it through an ABI transition */
1236 swizzle_fdset(ibits[0]);
1237 swizzle_fdset(ibits[1]);
1238 swizzle_fdset(ibits[2]);
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);
1256 precision >>= tc_precexp;
1257 if (TIMESEL(&asbt, rsbt))
1258 asbt += tc_tick_sbt;
1259 if (asbt <= SBT_MAX - rsbt)
1268 /* Iterate until the timeout expires or descriptors become ready. */
1270 error = selscan(td, ibits, obits, nd);
1271 if (error || td->td_retval[0] != 0)
1273 error = seltdwait(td, asbt, precision);
1276 error = selrescan(td, ibits, obits);
1277 if (error || td->td_retval[0] != 0)
1283 /* select is not restarted after signals... */
1284 if (error == ERESTART)
1286 if (error == EWOULDBLOCK)
1289 /* swizzle bit order back, if necessary */
1290 swizzle_fdset(obits[0]);
1291 swizzle_fdset(obits[1]);
1292 swizzle_fdset(obits[2]);
1293 #undef swizzle_fdset
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);
1312 * Convert a select bit set to poll flags.
1314 * The backend always returns POLLHUP/POLLERR if appropriate and we
1315 * return this as a set bit in any set.
1317 static const int select_flags[3] = {
1318 POLLRDNORM | POLLHUP | POLLERR,
1319 POLLWRNORM | POLLHUP | POLLERR,
1320 POLLRDBAND | POLLERR
1324 * Compute the fo_poll flags required for a fd given by the index and
1325 * bit position in the fd_mask array.
1328 selflags(fd_mask **ibits, int idx, fd_mask bit)
1334 for (msk = 0; msk < 3; msk++) {
1335 if (ibits[msk] == NULL)
1337 if ((ibits[msk][idx] & bit) == 0)
1339 flags |= select_flags[msk];
1345 * Set the appropriate output bits given a mask of fired events and the
1346 * input bits originally requested.
1349 selsetbits(fd_mask **ibits, fd_mask **obits, int idx, fd_mask bit, int events)
1355 for (msk = 0; msk < 3; msk++) {
1356 if ((events & select_flags[msk]) == 0)
1358 if (ibits[msk] == NULL)
1360 if ((ibits[msk][idx] & bit) == 0)
1363 * XXX Check for a duplicate set. This can occur because a
1364 * socket calls selrecord() twice for each poll() call
1365 * resulting in two selfds per real fd. selrescan() will
1366 * call selsetbits twice as a result.
1368 if ((obits[msk][idx] & bit) != 0)
1370 obits[msk][idx] |= bit;
1378 * Traverse the list of fds attached to this thread's seltd and check for
1382 selrescan(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;
1402 selfdfree(stp, sfp);
1403 /* If the selinfo wasn't cleared the event didn't fire. */
1407 error = fget_only_user(fdp, fd, &cap_event_rights, &fp);
1409 error = fget_unlocked(td, fd, &cap_event_rights, &fp);
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);
1420 n += selsetbits(ibits, obits, idx, bit, ev);
1423 td->td_retval[0] = n;
1428 * Perform the initial filedescriptor scan and register ourselves with
1432 selscan(struct thread *td, fd_mask **ibits, fd_mask **obits, int nfd)
1434 struct filedesc *fdp;
1437 int ev, flags, end, fd;
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++) {
1448 /* Compute the list of events we're interested in. */
1449 flags = selflags(ibits, idx, bit);
1453 error = fget_only_user(fdp, fd, &cap_event_rights, &fp);
1455 error = fget_unlocked(td, fd, &cap_event_rights, &fp);
1456 if (__predict_false(error != 0))
1458 selfdalloc(td, (void *)(uintptr_t)fd);
1459 ev = fo_poll(fp, flags, td->td_ucred, td);
1461 fput_only_user(fdp, fp);
1465 n += selsetbits(ibits, obits, idx, bit, ev);
1469 td->td_retval[0] = n;
1474 sys_poll(struct thread *td, struct poll_args *uap)
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));
1491 * kfds points to an array in the kernel.
1494 kern_poll_kfds(struct thread *td, struct pollfd *kfds, u_int nfds,
1495 struct timespec *tsp, sigset_t *uset)
1497 sbintime_t sbt, precision, tmp;
1504 if (!timespecvalid_interval(tsp))
1506 if (tsp->tv_sec == 0 && tsp->tv_nsec == 0)
1510 if (ts.tv_sec > INT32_MAX / 2) {
1511 over = ts.tv_sec - INT32_MAX / 2;
1517 precision >>= tc_precexp;
1518 if (TIMESEL(&sbt, tmp))
1526 error = kern_sigprocmask(td, SIG_SETMASK, uset,
1527 &td->td_oldsigmask, 0);
1530 td->td_pflags |= TDP_OLDMASK;
1532 * Make sure that ast() is called on return to
1533 * usermode and TDP_OLDMASK is cleared, restoring old
1536 ast_sched(td, TDA_SIGSUSPEND);
1540 /* Iterate until the timeout expires or descriptors become ready. */
1542 error = pollscan(td, kfds, nfds);
1543 if (error || td->td_retval[0] != 0)
1545 error = seltdwait(td, sbt, precision);
1548 error = pollrescan(td);
1549 if (error || td->td_retval[0] != 0)
1554 /* poll is not restarted after signals... */
1555 if (error == ERESTART)
1557 if (error == EWOULDBLOCK)
1563 sys_ppoll(struct thread *td, struct ppoll_args *uap)
1565 struct timespec ts, *tsp;
1569 if (uap->ts != NULL) {
1570 error = copyin(uap->ts, &ts, sizeof(ts));
1576 if (uap->set != NULL) {
1577 error = copyin(uap->set, &set, sizeof(set));
1583 return (kern_poll(td, uap->fds, uap->nfds, tsp, ssp));
1587 * ufds points to an array in user space.
1590 kern_poll(struct thread *td, struct pollfd *ufds, u_int nfds,
1591 struct timespec *tsp, sigset_t *set)
1593 struct pollfd *kfds;
1594 struct pollfd stackfds[32];
1597 if (kern_poll_maxfds(nfds))
1599 if (nfds > nitems(stackfds))
1600 kfds = mallocarray(nfds, sizeof(*kfds), M_TEMP, M_WAITOK);
1603 error = copyin(ufds, kfds, nfds * sizeof(*kfds));
1607 error = kern_poll_kfds(td, kfds, nfds, tsp, set);
1609 error = pollout(td, kfds, ufds, nfds);
1611 if (error == 0 && KTRPOINT(td, KTR_STRUCT_ARRAY))
1612 ktrstructarray("pollfd", UIO_USERSPACE, ufds, nfds,
1617 if (nfds > nitems(stackfds))
1623 kern_poll_maxfds(u_int nfds)
1627 * This is kinda bogus. We have fd limits, but that is not
1628 * really related to the size of the pollfd array. Make sure
1629 * we let the process use at least FD_SETSIZE entries and at
1630 * least enough for the system-wide limits. We want to be reasonably
1631 * safe, but not overly restrictive.
1633 return (nfds > maxfilesperproc && nfds > FD_SETSIZE);
1637 pollrescan(struct thread *td)
1643 struct filedesc *fdp;
1650 fdp = td->td_proc->p_fd;
1652 only_user = FILEDESC_IS_ONLY_USER(fdp);
1653 STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn) {
1654 fd = (struct pollfd *)sfp->sf_cookie;
1656 selfdfree(stp, sfp);
1657 /* If the selinfo wasn't cleared the event didn't fire. */
1661 error = fget_only_user(fdp, fd->fd, &cap_event_rights, &fp);
1663 error = fget_unlocked(td, fd->fd, &cap_event_rights, &fp);
1664 if (__predict_false(error != 0)) {
1665 fd->revents = POLLNVAL;
1670 * Note: backend also returns POLLHUP and
1671 * POLLERR if appropriate.
1673 fd->revents = fo_poll(fp, fd->events, td->td_ucred, td);
1675 fput_only_user(fdp, fp);
1678 if (fd->revents != 0)
1682 td->td_retval[0] = n;
1687 pollout(struct thread *td, struct pollfd *fds, struct pollfd *ufds, u_int nfd)
1693 for (i = 0; i < nfd; i++) {
1694 error = copyout(&fds->revents, &ufds->revents,
1695 sizeof(ufds->revents));
1698 if (fds->revents != 0)
1703 td->td_retval[0] = n;
1708 pollscan(struct thread *td, struct pollfd *fds, u_int nfd)
1710 struct filedesc *fdp;
1716 fdp = td->td_proc->p_fd;
1717 only_user = FILEDESC_IS_ONLY_USER(fdp);
1718 for (i = 0; i < nfd; i++, fds++) {
1724 error = fget_only_user(fdp, fds->fd, &cap_event_rights, &fp);
1726 error = fget_unlocked(td, fds->fd, &cap_event_rights, &fp);
1727 if (__predict_false(error != 0)) {
1728 fds->revents = POLLNVAL;
1733 * Note: backend also returns POLLHUP and
1734 * POLLERR if appropriate.
1736 selfdalloc(td, fds);
1737 fds->revents = fo_poll(fp, fds->events,
1740 fput_only_user(fdp, fp);
1744 * POSIX requires POLLOUT to be never
1745 * set simultaneously with POLLHUP.
1747 if ((fds->revents & POLLHUP) != 0)
1748 fds->revents &= ~POLLOUT;
1750 if (fds->revents != 0)
1753 td->td_retval[0] = n;
1758 * XXX This was created specifically to support netncp and netsmb. This
1759 * allows the caller to specify a socket to wait for events on. It returns
1760 * 0 if any events matched and an error otherwise. There is no way to
1761 * determine which events fired.
1764 selsocket(struct socket *so, int events, struct timeval *tvp, struct thread *td)
1767 sbintime_t asbt, precision, rsbt;
1770 precision = 0; /* stupid gcc! */
1773 if (rtv.tv_sec < 0 || rtv.tv_usec < 0 ||
1774 rtv.tv_usec >= 1000000)
1776 if (!timevalisset(&rtv))
1778 else if (rtv.tv_sec <= INT32_MAX) {
1779 rsbt = tvtosbt(rtv);
1781 precision >>= tc_precexp;
1782 if (TIMESEL(&asbt, rsbt))
1783 asbt += tc_tick_sbt;
1784 if (asbt <= SBT_MAX - rsbt)
1794 * Iterate until the timeout expires or the socket becomes ready.
1797 selfdalloc(td, NULL);
1798 if (sopoll(so, events, NULL, td) != 0) {
1802 error = seltdwait(td, asbt, precision);
1807 /* XXX Duplicates ncp/smb behavior. */
1808 if (error == ERESTART)
1814 * Preallocate two selfds associated with 'cookie'. Some fo_poll routines
1815 * have two select sets, one for read and another for write.
1818 selfdalloc(struct thread *td, void *cookie)
1823 if (stp->st_free1 == NULL)
1824 stp->st_free1 = malloc(sizeof(*stp->st_free1), M_SELFD, M_WAITOK|M_ZERO);
1825 stp->st_free1->sf_td = stp;
1826 stp->st_free1->sf_cookie = cookie;
1827 if (stp->st_free2 == NULL)
1828 stp->st_free2 = malloc(sizeof(*stp->st_free2), M_SELFD, M_WAITOK|M_ZERO);
1829 stp->st_free2->sf_td = stp;
1830 stp->st_free2->sf_cookie = cookie;
1834 selfdfree(struct seltd *stp, struct selfd *sfp)
1836 STAILQ_REMOVE(&stp->st_selq, sfp, selfd, sf_link);
1838 * Paired with doselwakeup.
1840 if (atomic_load_acq_ptr((uintptr_t *)&sfp->sf_si) != (uintptr_t)NULL) {
1841 mtx_lock(sfp->sf_mtx);
1842 if (sfp->sf_si != NULL) {
1843 TAILQ_REMOVE(&sfp->sf_si->si_tdlist, sfp, sf_threads);
1845 mtx_unlock(sfp->sf_mtx);
1850 /* Drain the waiters tied to all the selfd belonging the specified selinfo. */
1852 seldrain(struct selinfo *sip)
1856 * This feature is already provided by doselwakeup(), thus it is
1857 * enough to go for it.
1858 * Eventually, the context, should take care to avoid races
1859 * between thread calling select()/poll() and file descriptor
1860 * detaching, but, again, the races are just the same as
1863 doselwakeup(sip, -1);
1867 * Record a select request.
1870 selrecord(struct thread *selector, struct selinfo *sip)
1876 stp = selector->td_sel;
1878 * Don't record when doing a rescan.
1880 if (stp->st_flags & SELTD_RESCAN)
1883 * Grab one of the preallocated descriptors.
1886 if ((sfp = stp->st_free1) != NULL)
1887 stp->st_free1 = NULL;
1888 else if ((sfp = stp->st_free2) != NULL)
1889 stp->st_free2 = NULL;
1891 panic("selrecord: No free selfd on selq");
1894 mtxp = mtx_pool_find(mtxpool_select, sip);
1896 * Initialize the sfp and queue it in the thread.
1900 STAILQ_INSERT_TAIL(&stp->st_selq, sfp, sf_link);
1902 * Now that we've locked the sip, check for initialization.
1905 if (sip->si_mtx == NULL) {
1907 TAILQ_INIT(&sip->si_tdlist);
1910 * Add this thread to the list of selfds listening on this selinfo.
1912 TAILQ_INSERT_TAIL(&sip->si_tdlist, sfp, sf_threads);
1913 mtx_unlock(sip->si_mtx);
1916 /* Wake up a selecting thread. */
1918 selwakeup(struct selinfo *sip)
1920 doselwakeup(sip, -1);
1923 /* Wake up a selecting thread, and set its priority. */
1925 selwakeuppri(struct selinfo *sip, int pri)
1927 doselwakeup(sip, pri);
1931 * Do a wakeup when a selectable event occurs.
1934 doselwakeup(struct selinfo *sip, int pri)
1940 /* If it's not initialized there can't be any waiters. */
1941 if (sip->si_mtx == NULL)
1944 * Locking the selinfo locks all selfds associated with it.
1946 mtx_lock(sip->si_mtx);
1947 TAILQ_FOREACH_SAFE(sfp, &sip->si_tdlist, sf_threads, sfn) {
1949 * Once we remove this sfp from the list and clear the
1950 * sf_si seltdclear will know to ignore this si.
1952 TAILQ_REMOVE(&sip->si_tdlist, sfp, sf_threads);
1954 mtx_lock(&stp->st_mtx);
1955 stp->st_flags |= SELTD_PENDING;
1956 cv_broadcastpri(&stp->st_wait, pri);
1957 mtx_unlock(&stp->st_mtx);
1959 * Paired with selfdfree.
1961 * Storing this only after the wakeup provides an invariant that
1962 * stp is not used after selfdfree returns.
1964 atomic_store_rel_ptr((uintptr_t *)&sfp->sf_si, (uintptr_t)NULL);
1966 mtx_unlock(sip->si_mtx);
1970 seltdinit(struct thread *td)
1976 MPASS(stp->st_flags == 0);
1977 MPASS(STAILQ_EMPTY(&stp->st_selq));
1980 stp = malloc(sizeof(*stp), M_SELECT, M_WAITOK|M_ZERO);
1981 mtx_init(&stp->st_mtx, "sellck", NULL, MTX_DEF);
1982 cv_init(&stp->st_wait, "select");
1984 STAILQ_INIT(&stp->st_selq);
1989 seltdwait(struct thread *td, sbintime_t sbt, sbintime_t precision)
1996 * An event of interest may occur while we do not hold the seltd
1997 * locked so check the pending flag before we sleep.
1999 mtx_lock(&stp->st_mtx);
2001 * Any further calls to selrecord will be a rescan.
2003 stp->st_flags |= SELTD_RESCAN;
2004 if (stp->st_flags & SELTD_PENDING) {
2005 mtx_unlock(&stp->st_mtx);
2009 error = EWOULDBLOCK;
2011 error = cv_timedwait_sig_sbt(&stp->st_wait, &stp->st_mtx,
2012 sbt, precision, C_ABSOLUTE);
2014 error = cv_wait_sig(&stp->st_wait, &stp->st_mtx);
2015 mtx_unlock(&stp->st_mtx);
2021 seltdfini(struct thread *td)
2028 MPASS(stp->st_flags == 0);
2029 MPASS(STAILQ_EMPTY(&stp->st_selq));
2031 free(stp->st_free1, M_SELFD);
2033 free(stp->st_free2, M_SELFD);
2035 cv_destroy(&stp->st_wait);
2036 mtx_destroy(&stp->st_mtx);
2037 free(stp, M_SELECT);
2041 * Remove the references to the thread from all of the objects we were
2045 seltdclear(struct thread *td)
2052 STAILQ_FOREACH_SAFE(sfp, &stp->st_selq, sf_link, sfn)
2053 selfdfree(stp, sfp);
2057 static void selectinit(void *);
2058 SYSINIT(select, SI_SUB_SYSCALLS, SI_ORDER_ANY, selectinit, NULL);
2060 selectinit(void *dummy __unused)
2063 mtxpool_select = mtx_pool_create("select mtxpool", 128, MTX_DEF);
2067 * Set up a syscall return value that follows the convention specified for
2068 * posix_* functions.
2071 kern_posix_error(struct thread *td, int error)
2076 td->td_errno = error;
2077 td->td_pflags |= TDP_NERRNO;
2078 td->td_retval[0] = error;
2083 kcmp_cmp(uintptr_t a, uintptr_t b)
2093 kcmp_pget(struct thread *td, pid_t pid, struct proc **pp)
2095 if (pid == td->td_proc->p_pid) {
2099 return (pget(pid, PGET_CANDEBUG | PGET_NOTWEXIT | PGET_HOLD, pp));
2103 kern_kcmp(struct thread *td, pid_t pid1, pid_t pid2, int type,
2104 uintptr_t idx1, uintptr_t idx2)
2106 struct proc *p1, *p2;
2107 struct file *fp1, *fp2;
2112 error = kcmp_pget(td, pid1, &p1);
2114 error = kcmp_pget(td, pid2, &p2);
2121 error = fget_remote(td, p1, idx1, &fp1);
2123 error = fget_remote(td, p2, idx2, &fp2);
2125 if (type == KCMP_FILEOBJ)
2126 res = fo_cmp(fp1, fp2, td);
2128 res = kcmp_cmp((uintptr_t)fp1,
2136 res = kcmp_cmp((uintptr_t)p1->p_fd, (uintptr_t)p2->p_fd);
2139 res = kcmp_cmp((uintptr_t)p1->p_sigacts,
2140 (uintptr_t)p2->p_sigacts);
2143 res = kcmp_cmp((uintptr_t)p1->p_vmspace,
2144 (uintptr_t)p2->p_vmspace);
2152 if (p1 != NULL && p1 != td->td_proc)
2154 if (p2 != NULL && p2 != td->td_proc)
2157 td->td_retval[0] = res;
2162 sys_kcmp(struct thread *td, struct kcmp_args *uap)
2164 return (kern_kcmp(td, uap->pid1, uap->pid2, uap->type,
2165 uap->idx1, uap->idx2));
2169 file_kcmp_generic(struct file *fp1, struct file *fp2, struct thread *td)
2171 if (fp1->f_type != fp2->f_type)
2173 return (kcmp_cmp((uintptr_t)fp1->f_data, (uintptr_t)fp2->f_data));