2 * Copyright (c) 2000-2015 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1989, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
69 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/filedesc.h>
78 #include <sys/ioctl.h>
79 #include <sys/file_internal.h>
80 #include <sys/proc_internal.h>
81 #include <sys/socketvar.h>
82 #include <sys/uio_internal.h>
83 #include <sys/kernel.h>
84 #include <sys/guarded.h>
86 #include <sys/malloc.h>
87 #include <sys/sysproto.h>
89 #include <sys/mount_internal.h>
90 #include <sys/protosw.h>
93 #include <sys/kdebug.h>
95 #include <sys/event.h>
96 #include <sys/eventvar.h>
98 #include <sys/kauth.h>
100 #include <machine/smp.h>
101 #include <mach/mach_types.h>
102 #include <kern/kern_types.h>
103 #include <kern/assert.h>
104 #include <kern/kalloc.h>
105 #include <kern/thread.h>
106 #include <kern/clock.h>
107 #include <kern/ledger.h>
108 #include <kern/task.h>
109 #include <kern/telemetry.h>
110 #include <kern/waitq.h>
111 #include <kern/sched_prim.h>
113 #include <sys/mbuf.h>
114 #include <sys/domain.h>
115 #include <sys/socket.h>
116 #include <sys/socketvar.h>
117 #include <sys/errno.h>
118 #include <sys/syscall.h>
119 #include <sys/pipe.h>
121 #include <security/audit/audit.h>
124 #include <net/route.h>
126 #include <netinet/in.h>
127 #include <netinet/in_systm.h>
128 #include <netinet/ip.h>
129 #include <netinet/in_pcb.h>
130 #include <netinet/ip_var.h>
131 #include <netinet/ip6.h>
132 #include <netinet/tcp.h>
133 #include <netinet/tcp_fsm.h>
134 #include <netinet/tcp_seq.h>
135 #include <netinet/tcp_timer.h>
136 #include <netinet/tcp_var.h>
137 #include <netinet/tcpip.h>
138 #include <netinet/tcp_debug.h>
139 /* for wait queue based select */
140 #include <kern/waitq.h>
141 #include <kern/kalloc.h>
142 #include <sys/vnode_internal.h>
145 #include <security/mac_framework.h>
148 /* XXX should be in a header file somewhere */
149 void evsofree(struct socket
*);
150 void evpipefree(struct pipe
*);
151 void postpipeevent(struct pipe
*, int);
152 void postevent(struct socket
*, struct sockbuf
*, int);
153 extern kern_return_t
IOBSDGetPlatformUUID(__darwin_uuid_t uuid
, mach_timespec_t timeoutp
);
155 int rd_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
);
156 int wr_uio(struct proc
*p
, struct fileproc
*fp
, uio_t uio
, user_ssize_t
*retval
);
158 __private_extern__
int dofileread(vfs_context_t ctx
, struct fileproc
*fp
,
159 user_addr_t bufp
, user_size_t nbyte
,
160 off_t offset
, int flags
, user_ssize_t
*retval
);
161 __private_extern__
int dofilewrite(vfs_context_t ctx
, struct fileproc
*fp
,
162 user_addr_t bufp
, user_size_t nbyte
,
163 off_t offset
, int flags
, user_ssize_t
*retval
);
164 __private_extern__
int preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_vnode
);
165 __private_extern__
void donefileread(struct proc
*p
, struct fileproc
*fp_ret
, int fd
);
167 /* Conflict wait queue for when selects collide (opaque type) */
168 struct waitq select_conflict_queue
;
171 * Init routine called from bsd_init.c
173 void select_waitq_init(void);
175 select_waitq_init(void)
177 waitq_init(&select_conflict_queue
, SYNC_POLICY_FIFO
);
180 #define f_flag f_fglob->fg_flag
181 #define f_type f_fglob->fg_ops->fo_type
182 #define f_msgcount f_fglob->fg_msgcount
183 #define f_cred f_fglob->fg_cred
184 #define f_ops f_fglob->fg_ops
185 #define f_offset f_fglob->fg_offset
186 #define f_data f_fglob->fg_data
192 * preparefileread:EBADF
193 * preparefileread:ESPIPE
194 * preparefileread:ENXIO
195 * preparefileread:EBADF
199 read(struct proc
*p
, struct read_args
*uap
, user_ssize_t
*retval
)
201 __pthread_testcancel(1);
202 return(read_nocancel(p
, (struct read_nocancel_args
*)uap
, retval
));
206 read_nocancel(struct proc
*p
, struct read_nocancel_args
*uap
, user_ssize_t
*retval
)
211 struct vfs_context context
;
213 if ( (error
= preparefileread(p
, &fp
, fd
, 0)) )
216 context
= *(vfs_context_current());
217 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
219 error
= dofileread(&context
, fp
, uap
->cbuf
, uap
->nbyte
,
220 (off_t
)-1, 0, retval
);
222 donefileread(p
, fp
, fd
);
231 * preparefileread:EBADF
232 * preparefileread:ESPIPE
233 * preparefileread:ENXIO
234 * preparefileread:EBADF
238 pread(struct proc
*p
, struct pread_args
*uap
, user_ssize_t
*retval
)
240 __pthread_testcancel(1);
241 return(pread_nocancel(p
, (struct pread_nocancel_args
*)uap
, retval
));
245 pread_nocancel(struct proc
*p
, struct pread_nocancel_args
*uap
, user_ssize_t
*retval
)
247 struct fileproc
*fp
= NULL
; /* fp set by preparefileread() */
250 struct vfs_context context
;
252 if ( (error
= preparefileread(p
, &fp
, fd
, 1)) )
255 context
= *(vfs_context_current());
256 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
258 error
= dofileread(&context
, fp
, uap
->buf
, uap
->nbyte
,
259 uap
->offset
, FOF_OFFSET
, retval
);
261 donefileread(p
, fp
, fd
);
263 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pread
) | DBG_FUNC_NONE
),
264 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
271 * Code common for read and pread
275 donefileread(struct proc
*p
, struct fileproc
*fp
, int fd
)
278 fp_drop(p
, fd
, fp
, 1);
291 preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_pread
)
301 error
= fp_lookup(p
, fd
, &fp
, 1);
307 if ((fp
->f_flag
& FREAD
) == 0) {
311 if (check_for_pread
&& (fp
->f_type
!= DTYPE_VNODE
)) {
315 if (fp
->f_type
== DTYPE_VNODE
) {
316 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
318 if (check_for_pread
&& (vnode_isfifo(vp
))) {
322 if (check_for_pread
&& (vp
->v_flag
& VISTTY
)) {
334 fp_drop(p
, fd
, fp
, 1);
345 __private_extern__
int
346 dofileread(vfs_context_t ctx
, struct fileproc
*fp
,
347 user_addr_t bufp
, user_size_t nbyte
, off_t offset
, int flags
,
348 user_ssize_t
*retval
)
351 user_ssize_t bytecnt
;
353 char uio_buf
[ UIO_SIZEOF(1) ];
358 if (IS_64BIT_PROCESS(vfs_context_proc(ctx
))) {
359 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_READ
,
360 &uio_buf
[0], sizeof(uio_buf
));
362 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_READ
,
363 &uio_buf
[0], sizeof(uio_buf
));
365 uio_addiov(auio
, bufp
, nbyte
);
369 if ((error
= fo_read(fp
, auio
, flags
, ctx
))) {
370 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
371 error
== EINTR
|| error
== EWOULDBLOCK
))
374 bytecnt
-= uio_resid(auio
);
382 * Scatter read system call.
391 readv(struct proc
*p
, struct readv_args
*uap
, user_ssize_t
*retval
)
393 __pthread_testcancel(1);
394 return(readv_nocancel(p
, (struct readv_nocancel_args
*)uap
, retval
));
398 readv_nocancel(struct proc
*p
, struct readv_nocancel_args
*uap
, user_ssize_t
*retval
)
402 struct user_iovec
*iovp
;
404 /* Verify range bedfore calling uio_create() */
405 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
408 /* allocate a uio large enough to hold the number of iovecs passed */
409 auio
= uio_create(uap
->iovcnt
, 0,
410 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
413 /* get location of iovecs within the uio. then copyin the iovecs from
416 iovp
= uio_iovsaddr(auio
);
419 goto ExitThisRoutine
;
421 error
= copyin_user_iovec_array(uap
->iovp
,
422 IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
,
425 goto ExitThisRoutine
;
428 /* finalize uio_t for use and do the IO
430 error
= uio_calculateresid(auio
);
432 goto ExitThisRoutine
;
434 error
= rd_uio(p
, uap
->fd
, auio
, retval
);
452 write(struct proc
*p
, struct write_args
*uap
, user_ssize_t
*retval
)
454 __pthread_testcancel(1);
455 return(write_nocancel(p
, (struct write_nocancel_args
*)uap
, retval
));
460 write_nocancel(struct proc
*p
, struct write_nocancel_args
*uap
, user_ssize_t
*retval
)
465 bool wrote_some
= false;
469 error
= fp_lookup(p
,fd
,&fp
,0);
472 if ((fp
->f_flag
& FWRITE
) == 0) {
474 } else if (FP_ISGUARDED(fp
, GUARD_WRITE
)) {
476 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_WRITE
);
479 struct vfs_context context
= *(vfs_context_current());
480 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
482 error
= dofilewrite(&context
, fp
, uap
->cbuf
, uap
->nbyte
,
483 (off_t
)-1, 0, retval
);
485 wrote_some
= *retval
> 0;
488 fp_drop_written(p
, fd
, fp
);
490 fp_drop(p
, fd
, fp
, 0);
506 pwrite(struct proc
*p
, struct pwrite_args
*uap
, user_ssize_t
*retval
)
508 __pthread_testcancel(1);
509 return(pwrite_nocancel(p
, (struct pwrite_nocancel_args
*)uap
, retval
));
513 pwrite_nocancel(struct proc
*p
, struct pwrite_nocancel_args
*uap
, user_ssize_t
*retval
)
518 vnode_t vp
= (vnode_t
)0;
519 bool wrote_some
= false;
523 error
= fp_lookup(p
,fd
,&fp
,0);
527 if ((fp
->f_flag
& FWRITE
) == 0) {
529 } else if (FP_ISGUARDED(fp
, GUARD_WRITE
)) {
531 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_WRITE
);
534 struct vfs_context context
= *vfs_context_current();
535 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
537 if (fp
->f_type
!= DTYPE_VNODE
) {
541 vp
= (vnode_t
)fp
->f_fglob
->fg_data
;
542 if (vnode_isfifo(vp
)) {
546 if ((vp
->v_flag
& VISTTY
)) {
550 if (uap
->offset
== (off_t
)-1) {
555 error
= dofilewrite(&context
, fp
, uap
->buf
, uap
->nbyte
,
556 uap
->offset
, FOF_OFFSET
, retval
);
557 wrote_some
= *retval
> 0;
561 fp_drop_written(p
, fd
, fp
);
563 fp_drop(p
, fd
, fp
, 0);
565 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pwrite
) | DBG_FUNC_NONE
),
566 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
575 * <fo_write>:??? [indirect through struct fileops]
577 __private_extern__
int
578 dofilewrite(vfs_context_t ctx
, struct fileproc
*fp
,
579 user_addr_t bufp
, user_size_t nbyte
, off_t offset
, int flags
,
580 user_ssize_t
*retval
)
584 user_ssize_t bytecnt
;
585 char uio_buf
[ UIO_SIZEOF(1) ];
587 if (nbyte
> INT_MAX
) {
592 if (IS_64BIT_PROCESS(vfs_context_proc(ctx
))) {
593 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_WRITE
,
594 &uio_buf
[0], sizeof(uio_buf
));
596 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_WRITE
,
597 &uio_buf
[0], sizeof(uio_buf
));
599 uio_addiov(auio
, bufp
, nbyte
);
602 if ((error
= fo_write(fp
, auio
, flags
, ctx
))) {
603 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
604 error
== EINTR
|| error
== EWOULDBLOCK
))
606 /* The socket layer handles SIGPIPE */
607 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
&&
608 (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) == 0) {
609 /* XXX Raise the signal on the thread? */
610 psignal(vfs_context_proc(ctx
), SIGPIPE
);
613 bytecnt
-= uio_resid(auio
);
620 * Gather write system call
623 writev(struct proc
*p
, struct writev_args
*uap
, user_ssize_t
*retval
)
625 __pthread_testcancel(1);
626 return(writev_nocancel(p
, (struct writev_nocancel_args
*)uap
, retval
));
630 writev_nocancel(struct proc
*p
, struct writev_nocancel_args
*uap
, user_ssize_t
*retval
)
635 struct user_iovec
*iovp
;
636 bool wrote_some
= false;
638 AUDIT_ARG(fd
, uap
->fd
);
640 /* Verify range bedfore calling uio_create() */
641 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
644 /* allocate a uio large enough to hold the number of iovecs passed */
645 auio
= uio_create(uap
->iovcnt
, 0,
646 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
649 /* get location of iovecs within the uio. then copyin the iovecs from
652 iovp
= uio_iovsaddr(auio
);
655 goto ExitThisRoutine
;
657 error
= copyin_user_iovec_array(uap
->iovp
,
658 IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
,
661 goto ExitThisRoutine
;
664 /* finalize uio_t for use and do the IO
666 error
= uio_calculateresid(auio
);
668 goto ExitThisRoutine
;
671 error
= fp_lookup(p
, uap
->fd
, &fp
, 0);
673 goto ExitThisRoutine
;
675 if ((fp
->f_flag
& FWRITE
) == 0) {
677 } else if (FP_ISGUARDED(fp
, GUARD_WRITE
)) {
679 error
= fp_guard_exception(p
, uap
->fd
, fp
, kGUARD_EXC_WRITE
);
682 error
= wr_uio(p
, fp
, auio
, retval
);
683 wrote_some
= *retval
> 0;
687 fp_drop_written(p
, uap
->fd
, fp
);
689 fp_drop(p
, uap
->fd
, fp
, 0);
700 wr_uio(struct proc
*p
, struct fileproc
*fp
, uio_t uio
, user_ssize_t
*retval
)
704 struct vfs_context context
= *vfs_context_current();
706 count
= uio_resid(uio
);
708 context
.vc_ucred
= fp
->f_cred
;
709 error
= fo_write(fp
, uio
, 0, &context
);
711 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
712 error
== EINTR
|| error
== EWOULDBLOCK
))
714 /* The socket layer handles SIGPIPE */
715 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
&&
716 (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) == 0)
719 *retval
= count
- uio_resid(uio
);
726 rd_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
)
731 struct vfs_context context
= *vfs_context_current();
733 if ( (error
= preparefileread(p
, &fp
, fdes
, 0)) )
736 count
= uio_resid(uio
);
738 context
.vc_ucred
= fp
->f_cred
;
740 error
= fo_read(fp
, uio
, 0, &context
);
743 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
744 error
== EINTR
|| error
== EWOULDBLOCK
))
747 *retval
= count
- uio_resid(uio
);
749 donefileread(p
, fp
, fdes
);
764 * fp_lookup:EBADF Bad file descriptor
768 ioctl(struct proc
*p
, struct ioctl_args
*uap
, __unused
int32_t *retval
)
770 struct fileproc
*fp
= NULL
;
773 caddr_t datap
= NULL
, memp
= NULL
;
774 boolean_t is64bit
= FALSE
;
776 #define STK_PARAMS 128
777 char stkbuf
[STK_PARAMS
] = {};
779 u_long com
= uap
->com
;
780 struct vfs_context context
= *vfs_context_current();
782 AUDIT_ARG(fd
, uap
->fd
);
783 AUDIT_ARG(addr
, uap
->data
);
785 is64bit
= proc_is64bit(p
);
788 AUDIT_ARG(value64
, com
);
790 AUDIT_ARG(cmd
, CAST_DOWN_EXPLICIT(int, com
));
791 #endif /* CONFIG_AUDIT */
794 * Interpret high order word to find amount of data to be
795 * copied to/from the user's address space.
797 size
= IOCPARM_LEN(com
);
798 if (size
> IOCPARM_MAX
)
800 if (size
> sizeof (stkbuf
)) {
801 if ((memp
= (caddr_t
)kalloc(size
)) == 0)
808 error
= copyin(uap
->data
, datap
, size
);
812 /* XXX - IOC_IN and no size? we should proably return an error here!! */
814 *(user_addr_t
*)datap
= uap
->data
;
817 *(uint32_t *)datap
= (uint32_t)uap
->data
;
820 } else if ((com
& IOC_OUT
) && size
)
822 * Zero the buffer so the user always
823 * gets back something deterministic.
826 else if (com
& IOC_VOID
) {
827 /* XXX - this is odd since IOC_VOID means no parameters */
829 *(user_addr_t
*)datap
= uap
->data
;
832 *(uint32_t *)datap
= (uint32_t)uap
->data
;
837 error
= fp_lookup(p
,fd
,&fp
,1);
843 AUDIT_ARG(file
, p
, fp
);
845 if ((fp
->f_flag
& (FREAD
| FWRITE
)) == 0) {
850 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
853 error
= mac_file_check_ioctl(context
.vc_ucred
, fp
->f_fglob
, com
);
860 *fdflags(p
, fd
) &= ~UF_EXCLOSE
;
864 *fdflags(p
, fd
) |= UF_EXCLOSE
;
868 if ( (tmp
= *(int *)datap
) )
869 fp
->f_flag
|= FNONBLOCK
;
871 fp
->f_flag
&= ~FNONBLOCK
;
872 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
876 if ( (tmp
= *(int *)datap
) )
877 fp
->f_flag
|= FASYNC
;
879 fp
->f_flag
&= ~FASYNC
;
880 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
885 if (fp
->f_type
== DTYPE_SOCKET
) {
886 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
889 if (fp
->f_type
== DTYPE_PIPE
) {
890 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
896 struct proc
*p1
= proc_find(tmp
);
904 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
908 if (fp
->f_type
== DTYPE_SOCKET
) {
909 *(int *)datap
= ((struct socket
*)fp
->f_data
)->so_pgid
;
912 error
= fo_ioctl(fp
, TIOCGPGRP
, datap
, &context
);
913 *(int *)datap
= -*(int *)datap
;
917 error
= fo_ioctl(fp
, com
, datap
, &context
);
919 * Copy any data to user, size was
920 * already set and checked above.
922 if (error
== 0 && (com
& IOC_OUT
) && size
)
923 error
= copyout(datap
, uap
->data
, (u_int
)size
);
927 fp_drop(p
, fd
, fp
, 1);
936 int selwait
, nselcoll
;
937 #define SEL_FIRSTPASS 1
938 #define SEL_SECONDPASS 2
939 extern int selcontinue(int error
);
940 extern int selprocess(int error
, int sel_pass
);
941 static int selscan(struct proc
*p
, struct _select
* sel
, struct _select_data
* seldata
,
942 int nfd
, int32_t *retval
, int sel_pass
, struct waitq_set
*wqset
);
943 static int selcount(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int *count
);
944 static int seldrop_locked(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int lim
, int *need_wakeup
, int fromselcount
);
945 static int seldrop(struct proc
*p
, u_int32_t
*ibits
, int nfd
);
946 static int select_internal(struct proc
*p
, struct select_nocancel_args
*uap
, uint64_t timeout
, int32_t *retval
);
949 * Select system call.
952 * EINVAL Invalid argument
953 * EAGAIN Nonconformant error if allocation fails
956 select(struct proc
*p
, struct select_args
*uap
, int32_t *retval
)
958 __pthread_testcancel(1);
959 return select_nocancel(p
, (struct select_nocancel_args
*)uap
, retval
);
963 select_nocancel(struct proc
*p
, struct select_nocancel_args
*uap
, int32_t *retval
)
965 uint64_t timeout
= 0;
970 if (IS_64BIT_PROCESS(p
)) {
971 struct user64_timeval atv64
;
972 err
= copyin(uap
->tv
, (caddr_t
)&atv64
, sizeof(atv64
));
973 /* Loses resolution - assume timeout < 68 years */
974 atv
.tv_sec
= atv64
.tv_sec
;
975 atv
.tv_usec
= atv64
.tv_usec
;
977 struct user32_timeval atv32
;
978 err
= copyin(uap
->tv
, (caddr_t
)&atv32
, sizeof(atv32
));
979 atv
.tv_sec
= atv32
.tv_sec
;
980 atv
.tv_usec
= atv32
.tv_usec
;
985 if (itimerfix(&atv
)) {
990 clock_absolutetime_interval_to_deadline(tvtoabstime(&atv
), &timeout
);
993 return select_internal(p
, uap
, timeout
, retval
);
997 pselect(struct proc
*p
, struct pselect_args
*uap
, int32_t *retval
)
999 __pthread_testcancel(1);
1000 return pselect_nocancel(p
, (struct pselect_nocancel_args
*)uap
, retval
);
1004 pselect_nocancel(struct proc
*p
, struct pselect_nocancel_args
*uap
, int32_t *retval
)
1008 uint64_t timeout
= 0;
1013 if (IS_64BIT_PROCESS(p
)) {
1014 struct user64_timespec ts64
;
1015 err
= copyin(uap
->ts
, (caddr_t
)&ts64
, sizeof(ts64
));
1016 ts
.tv_sec
= ts64
.tv_sec
;
1017 ts
.tv_nsec
= ts64
.tv_nsec
;
1019 struct user32_timespec ts32
;
1020 err
= copyin(uap
->ts
, (caddr_t
)&ts32
, sizeof(ts32
));
1021 ts
.tv_sec
= ts32
.tv_sec
;
1022 ts
.tv_nsec
= ts32
.tv_nsec
;
1028 if (!timespec_is_valid(&ts
)) {
1031 clock_absolutetime_interval_to_deadline(tstoabstime(&ts
), &timeout
);
1034 ut
= get_bsdthread_info(current_thread());
1036 if (uap
->mask
!= USER_ADDR_NULL
) {
1037 /* save current mask, then copyin and set new mask */
1039 err
= copyin(uap
->mask
, &newset
, sizeof(sigset_t
));
1043 ut
->uu_oldmask
= ut
->uu_sigmask
;
1044 ut
->uu_flag
|= UT_SAS_OLDMASK
;
1045 ut
->uu_sigmask
= (newset
& ~sigcantmask
);
1048 err
= select_internal(p
, (struct select_nocancel_args
*)uap
, timeout
, retval
);
1050 if (err
!= EINTR
&& ut
->uu_flag
& UT_SAS_OLDMASK
) {
1052 * Restore old mask (direct return case). NOTE: EINTR can also be returned
1053 * if the thread is cancelled. In that case, we don't reset the signal
1054 * mask to its original value (which usually happens in the signal
1055 * delivery path). This behavior is permitted by POSIX.
1057 ut
->uu_sigmask
= ut
->uu_oldmask
;
1059 ut
->uu_flag
&= ~UT_SAS_OLDMASK
;
1066 * Generic implementation of {,p}select. Care: we type-pun uap across the two
1067 * syscalls, which differ slightly. The first 4 arguments (nfds and the fd sets)
1068 * are identical. The 5th (timeout) argument points to different types, so we
1069 * unpack in the syscall-specific code, but the generic code still does a null
1070 * check on this argument to determine if a timeout was specified.
1073 select_internal(struct proc
*p
, struct select_nocancel_args
*uap
, uint64_t timeout
, int32_t *retval
)
1078 struct uthread
*uth
;
1079 struct _select
*sel
;
1080 struct _select_data
*seldata
;
1081 int needzerofill
= 1;
1085 th_act
= current_thread();
1086 uth
= get_bsdthread_info(th_act
);
1087 sel
= &uth
->uu_select
;
1088 seldata
= &uth
->uu_kevent
.ss_select_data
;
1091 seldata
->args
= uap
;
1092 seldata
->retval
= retval
;
1093 seldata
->wqp
= NULL
;
1100 /* select on thread of process that already called proc_exit() */
1101 if (p
->p_fd
== NULL
) {
1105 if (uap
->nd
> p
->p_fd
->fd_nfiles
)
1106 uap
->nd
= p
->p_fd
->fd_nfiles
; /* forgiving; slightly wrong */
1108 nw
= howmany(uap
->nd
, NFDBITS
);
1109 ni
= nw
* sizeof(fd_mask
);
1112 * if the previously allocated space for the bits is smaller than
1113 * what is requested or no space has yet been allocated for this
1114 * thread, allocate enough space now.
1116 * Note: If this process fails, select() will return EAGAIN; this
1117 * is the same thing pool() returns in a no-memory situation, but
1118 * it is not a POSIX compliant error code for select().
1120 if (sel
->nbytes
< (3 * ni
)) {
1121 int nbytes
= 3 * ni
;
1123 /* Free previous allocation, if any */
1124 if (sel
->ibits
!= NULL
)
1125 FREE(sel
->ibits
, M_TEMP
);
1126 if (sel
->obits
!= NULL
) {
1127 FREE(sel
->obits
, M_TEMP
);
1128 /* NULL out; subsequent ibits allocation may fail */
1132 MALLOC(sel
->ibits
, u_int32_t
*, nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
1133 if (sel
->ibits
== NULL
)
1135 MALLOC(sel
->obits
, u_int32_t
*, nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
1136 if (sel
->obits
== NULL
) {
1137 FREE(sel
->ibits
, M_TEMP
);
1141 sel
->nbytes
= nbytes
;
1146 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
1147 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
1151 * get the bits from the user address space
1153 #define getbits(name, x) \
1155 if (uap->name && (error = copyin(uap->name, \
1156 (caddr_t)&sel->ibits[(x) * nw], ni))) \
1157 goto continuation; \
1165 seldata
->abstime
= timeout
;
1167 if ( (error
= selcount(p
, sel
->ibits
, uap
->nd
, &count
)) ) {
1172 * We need an array of waitq pointers. This is due to the new way
1173 * in which waitqs are linked to sets. When a thread selects on a
1174 * file descriptor, a waitq (embedded in a selinfo structure) is
1175 * added to the thread's local waitq set. There is no longer any
1176 * way to directly iterate over all members of a given waitq set.
1177 * The process of linking a waitq into a set may allocate a link
1178 * table object. Because we can't iterate over all the waitqs to
1179 * which our thread waitq set belongs, we need a way of removing
1182 * Thus we need a buffer which will hold one waitq pointer
1183 * per FD being selected. During the tear-down phase we can use
1184 * these pointers to dis-associate the underlying selinfo's waitq
1185 * from our thread's waitq set.
1187 * Because we also need to allocate a waitq set for this thread,
1188 * we use a bare buffer pointer to hold all the memory. Note that
1189 * this memory is cached in the thread pointer and not reaped until
1190 * the thread exists. This is generally OK because threads that
1191 * call select tend to keep calling select repeatedly.
1193 sz
= ALIGN(sizeof(struct waitq_set
)) + (count
* sizeof(uint64_t));
1194 if (sz
> uth
->uu_wqstate_sz
) {
1195 /* (re)allocate a buffer to hold waitq pointers */
1196 if (uth
->uu_wqset
) {
1197 if (waitq_set_is_valid(uth
->uu_wqset
))
1198 waitq_set_deinit(uth
->uu_wqset
);
1199 FREE(uth
->uu_wqset
, M_SELECT
);
1200 } else if (uth
->uu_wqstate_sz
&& !uth
->uu_wqset
)
1201 panic("select: thread structure corrupt! "
1202 "uu_wqstate_sz:%ld, wqstate_buf == NULL",
1203 uth
->uu_wqstate_sz
);
1204 uth
->uu_wqstate_sz
= sz
;
1205 MALLOC(uth
->uu_wqset
, struct waitq_set
*, sz
, M_SELECT
, M_WAITOK
);
1207 panic("can't allocate %ld bytes for wqstate buffer",
1208 uth
->uu_wqstate_sz
);
1209 waitq_set_init(uth
->uu_wqset
,
1210 SYNC_POLICY_FIFO
|SYNC_POLICY_PREPOST
, NULL
, NULL
);
1213 if (!waitq_set_is_valid(uth
->uu_wqset
))
1214 waitq_set_init(uth
->uu_wqset
,
1215 SYNC_POLICY_FIFO
|SYNC_POLICY_PREPOST
, NULL
, NULL
);
1217 /* the last chunk of our buffer is an array of waitq pointers */
1218 seldata
->wqp
= (uint64_t *)((char *)(uth
->uu_wqset
) + ALIGN(sizeof(struct waitq_set
)));
1219 bzero(seldata
->wqp
, sz
- ALIGN(sizeof(struct waitq_set
)));
1221 seldata
->count
= count
;
1227 * We have already cleaned up any state we established,
1228 * either locally or as a result of selcount(). We don't
1229 * need to wait_subqueue_unlink_all(), since we haven't set
1230 * anything at this point.
1235 return selprocess(0, SEL_FIRSTPASS
);
1239 selcontinue(int error
)
1241 return selprocess(error
, SEL_SECONDPASS
);
1248 * Parameters: error The error code from our caller
1249 * sel_pass The pass we are on
1252 selprocess(int error
, int sel_pass
)
1257 struct uthread
*uth
;
1259 struct select_nocancel_args
*uap
;
1261 struct _select
*sel
;
1262 struct _select_data
*seldata
;
1267 wait_result_t wait_result
;
1270 th_act
= current_thread();
1271 uth
= get_bsdthread_info(th_act
);
1272 sel
= &uth
->uu_select
;
1273 seldata
= &uth
->uu_kevent
.ss_select_data
;
1274 uap
= seldata
->args
;
1275 retval
= seldata
->retval
;
1277 if ((error
!= 0) && (sel_pass
== SEL_FIRSTPASS
))
1279 if (seldata
->count
== 0)
1286 OSBitOrAtomic(P_SELECT
, &p
->p_flag
);
1288 /* skip scans if the select is just for timeouts */
1289 if (seldata
->count
) {
1290 error
= selscan(p
, sel
, seldata
, uap
->nd
, retval
, sel_pass
, uth
->uu_wqset
);
1291 if (error
|| *retval
) {
1294 if (prepost
|| somewakeup
) {
1296 * if the select of log, then we can wakeup and
1297 * discover some one else already read the data;
1298 * go to select again if time permits
1309 clock_get_uptime(&now
);
1310 if (now
>= seldata
->abstime
)
1315 /* cleanup obits and try again */
1317 sel_pass
= SEL_FIRSTPASS
;
1322 * To effect a poll, the timeout argument should be
1323 * non-nil, pointing to a zero-valued timeval structure.
1325 if (uap
->tv
&& seldata
->abstime
== 0) {
1329 /* No spurious wakeups due to colls,no need to check for them */
1330 if ((sel_pass
== SEL_SECONDPASS
) || ((p
->p_flag
& P_SELECT
) == 0)) {
1331 sel_pass
= SEL_FIRSTPASS
;
1335 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1337 /* if the select is just for timeout skip check */
1338 if (seldata
->count
&& (sel_pass
== SEL_SECONDPASS
))
1339 panic("selprocess: 2nd pass assertwaiting");
1341 /* waitq_set has waitqueue as first element */
1342 wait_result
= waitq_assert_wait64_leeway((struct waitq
*)uth
->uu_wqset
,
1343 NO_EVENT64
, THREAD_ABORTSAFE
,
1344 TIMEOUT_URGENCY_USER_NORMAL
,
1347 if (wait_result
!= THREAD_AWAKENED
) {
1348 /* there are no preposted events */
1349 error
= tsleep1(NULL
, PSOCK
| PCATCH
,
1350 "select", 0, selcontinue
);
1357 sel_pass
= SEL_SECONDPASS
;
1364 seldrop(p
, sel
->ibits
, uap
->nd
);
1365 waitq_set_deinit(uth
->uu_wqset
);
1367 * zero out the waitq pointer array to avoid use-after free
1368 * errors in the selcount error path (seldrop_locked) if/when
1369 * the thread re-calls select().
1371 bzero((void *)uth
->uu_wqset
, uth
->uu_wqstate_sz
);
1373 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1374 /* select is not restarted after signals... */
1375 if (error
== ERESTART
)
1377 if (error
== EWOULDBLOCK
)
1379 nw
= howmany(uap
->nd
, NFDBITS
);
1380 ni
= nw
* sizeof(fd_mask
);
1382 #define putbits(name, x) \
1384 if (uap->name && (error2 = \
1385 copyout((caddr_t)&sel->obits[(x) * nw], uap->name, ni))) \
1398 if (error
!= EINTR
&& sel_pass
== SEL_SECONDPASS
&& uth
->uu_flag
& UT_SAS_OLDMASK
) {
1399 /* restore signal mask - continuation case */
1400 uth
->uu_sigmask
= uth
->uu_oldmask
;
1401 uth
->uu_oldmask
= 0;
1402 uth
->uu_flag
&= ~UT_SAS_OLDMASK
;
1410 * remove the fileproc's underlying waitq from the supplied waitq set;
1411 * clear FP_INSELECT when appropriate
1414 * fp File proc that is potentially currently in select
1415 * wqset Waitq set to which the fileproc may belong
1416 * (usually this is the thread's private waitq set)
1418 * proc_fdlock is held
1420 static void selunlinkfp(struct fileproc
*fp
, uint64_t wqp_id
, struct waitq_set
*wqset
)
1422 int valid_set
= waitq_set_is_valid(wqset
);
1423 int valid_q
= !!wqp_id
;
1426 * This could be called (from selcount error path) before we setup
1427 * the thread's wqset. Check the wqset passed in, and only unlink if
1431 /* unlink the underlying waitq from the input set (thread waitq set) */
1432 if (valid_q
&& valid_set
)
1433 waitq_unlink_by_prepost_id(wqp_id
, wqset
);
1435 /* allow passing a NULL/invalid fp for seldrop unwind */
1436 if (!fp
|| !(fp
->f_flags
& (FP_INSELECT
|FP_SELCONFLICT
)))
1440 * We can always remove the conflict queue from our thread's set: this
1441 * will not affect other threads that potentially need to be awoken on
1442 * the conflict queue during a fileproc_drain - those sets will still
1443 * be linked with the global conflict queue, and the last waiter
1444 * on the fp clears the CONFLICT marker.
1446 if (valid_set
&& (fp
->f_flags
& FP_SELCONFLICT
))
1447 waitq_unlink(&select_conflict_queue
, wqset
);
1450 * This isn't quite right - we don't actually know if this
1451 * fileproc is in another select or not! Here we just assume
1452 * that if we were the first thread to select on the FD, then
1453 * we'll be the one to clear this flag...
1455 if (valid_set
&& fp
->f_wset
== (void *)wqset
) {
1456 fp
->f_flags
&= ~FP_INSELECT
;
1462 * connect a fileproc to the given wqset, potentially bridging to a waitq
1463 * pointed to indirectly by wq_data
1466 * fp File proc potentially currently in select
1467 * wq_data Pointer to a pointer to a waitq (could be NULL)
1468 * wqset Waitq set to which the fileproc should now belong
1469 * (usually this is the thread's private waitq set)
1472 * proc_fdlock is held
1474 static uint64_t sellinkfp(struct fileproc
*fp
, void **wq_data
, struct waitq_set
*wqset
)
1476 struct waitq
*f_wq
= NULL
;
1478 if ((fp
->f_flags
& FP_INSELECT
) != FP_INSELECT
) {
1480 panic("non-null data:%p on fp:%p not in select?!"
1481 "(wqset:%p)", wq_data
, fp
, wqset
);
1485 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
1487 * The conflict queue requires disabling interrupts, so we
1488 * need to explicitly reserve a link object to avoid a
1489 * panic/assert in the waitq code. Hopefully this extra step
1490 * can be avoided if we can split the waitq structure into
1491 * blocking and linkage sub-structures.
1493 uint64_t reserved_link
= waitq_link_reserve(&select_conflict_queue
);
1494 waitq_link(&select_conflict_queue
, wqset
, WAITQ_SHOULD_LOCK
, &reserved_link
);
1495 waitq_link_release(reserved_link
);
1499 * The wq_data parameter has potentially been set by selrecord called
1500 * from a subsystems fo_select() function. If the subsystem does not
1501 * call selrecord, then wq_data will be NULL
1503 * Use memcpy to get the value into a proper pointer because
1504 * wq_data most likely points to a stack variable that could be
1505 * unaligned on 32-bit systems.
1508 memcpy(&f_wq
, wq_data
, sizeof(f_wq
));
1509 if (!waitq_is_valid(f_wq
))
1513 /* record the first thread's wqset in the fileproc structure */
1515 fp
->f_wset
= (void *)wqset
;
1517 /* handles NULL f_wq */
1518 return waitq_get_prepost_id(f_wq
);
1525 * Parameters: p Process performing the select
1526 * sel The per-thread select context structure
1527 * nfd The number of file descriptors to scan
1528 * retval The per thread system call return area
1529 * sel_pass Which pass this is; allowed values are
1530 * SEL_FIRSTPASS and SEL_SECONDPASS
1531 * wqset The per thread wait queue set
1533 * Returns: 0 Success
1534 * EIO Invalid p->p_fd field XXX Obsolete?
1535 * EBADF One of the files in the bit vector is
1539 selscan(struct proc
*p
, struct _select
*sel
, struct _select_data
* seldata
,
1540 int nfd
, int32_t *retval
, int sel_pass
, struct waitq_set
*wqset
)
1542 struct filedesc
*fdp
= p
->p_fd
;
1545 struct fileproc
*fp
;
1546 int n
= 0; /* count of bits */
1547 int nc
= 0; /* bit vector offset (nc'th bit) */
1548 static int flag
[3] = { FREAD
, FWRITE
, 0 };
1549 u_int32_t
*iptr
, *optr
;
1551 u_int32_t
*ibits
, *obits
;
1552 uint64_t reserved_link
, *rl_ptr
= NULL
;
1554 struct vfs_context context
= *vfs_context_current();
1557 * Problems when reboot; due to MacOSX signal probs
1558 * in Beaker1C ; verify that the p->p_fd is valid
1567 nw
= howmany(nfd
, NFDBITS
);
1569 count
= seldata
->count
;
1578 for (msk
= 0; msk
< 3; msk
++) {
1579 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1580 optr
= (u_int32_t
*)&obits
[msk
* nw
];
1582 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1583 bits
= iptr
[i
/NFDBITS
];
1585 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1588 if (fd
< fdp
->fd_nfiles
)
1589 fp
= fdp
->fd_ofiles
[fd
];
1593 if (fp
== NULL
|| (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1595 * If we abort because of a bad
1596 * fd, let the caller unwind...
1601 if (sel_pass
== SEL_SECONDPASS
) {
1604 selunlinkfp(fp
, seldata
->wqp
[nc
], wqset
);
1606 reserved_link
= waitq_link_reserve((struct waitq
*)wqset
);
1607 rl_ptr
= &reserved_link
;
1608 if (fp
->f_flags
& FP_INSELECT
)
1609 /* someone is already in select on this fp */
1610 fp
->f_flags
|= FP_SELCONFLICT
;
1612 fp
->f_flags
|= FP_INSELECT
;
1615 context
.vc_ucred
= fp
->f_cred
;
1618 * stash this value b/c fo_select may replace
1619 * reserved_link with a pointer to a waitq object
1621 uint64_t rsvd
= reserved_link
;
1623 /* The select; set the bit, if true */
1624 if (fp
->f_ops
&& fp
->f_type
1625 && fo_select(fp
, flag
[msk
], rl_ptr
, &context
)) {
1626 optr
[fd
/NFDBITS
] |= (1 << (fd
% NFDBITS
));
1629 if (sel_pass
== SEL_FIRSTPASS
) {
1630 waitq_link_release(rsvd
);
1632 * If the fp's supporting selinfo structure was linked
1633 * to this thread's waitq set, then 'reserved_link'
1634 * will have been updated by selrecord to be a pointer
1635 * to the selinfo's waitq.
1637 if (reserved_link
== rsvd
)
1638 rl_ptr
= NULL
; /* fo_select never called selrecord() */
1640 * Hook up the thread's waitq set either to
1641 * the fileproc structure, or to the global
1642 * conflict queue: but only on the first
1645 seldata
->wqp
[nc
] = sellinkfp(fp
, (void **)rl_ptr
, wqset
);
1657 int poll_callback(struct kqueue
*, struct kevent_internal_s
*, void *);
1659 struct poll_continue_args
{
1660 user_addr_t pca_fds
;
1666 poll(struct proc
*p
, struct poll_args
*uap
, int32_t *retval
)
1668 __pthread_testcancel(1);
1669 return(poll_nocancel(p
, (struct poll_nocancel_args
*)uap
, retval
));
1674 poll_nocancel(struct proc
*p
, struct poll_nocancel_args
*uap
, int32_t *retval
)
1676 struct poll_continue_args
*cont
;
1680 int ncoll
, error
= 0;
1681 u_int nfds
= uap
->nfds
;
1687 * This is kinda bogus. We have fd limits, but that is not
1688 * really related to the size of the pollfd array. Make sure
1689 * we let the process use at least FD_SETSIZE entries and at
1690 * least enough for the current limits. We want to be reasonably
1691 * safe, but not overly restrictive.
1693 if (nfds
> OPEN_MAX
||
1694 (nfds
> p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
&& (proc_suser(p
) || nfds
> FD_SETSIZE
)))
1697 kq
= kqueue_alloc(p
, 0);
1701 ni
= nfds
* sizeof(struct pollfd
) + sizeof(struct poll_continue_args
);
1702 MALLOC(cont
, struct poll_continue_args
*, ni
, M_TEMP
, M_WAITOK
);
1708 fds
= (struct pollfd
*)&cont
[1];
1709 error
= copyin(uap
->fds
, fds
, nfds
* sizeof(struct pollfd
));
1713 if (uap
->timeout
!= -1) {
1716 atv
.tv_sec
= uap
->timeout
/ 1000;
1717 atv
.tv_usec
= (uap
->timeout
% 1000) * 1000;
1718 if (itimerfix(&atv
)) {
1722 getmicrouptime(&rtv
);
1723 timevaladd(&atv
, &rtv
);
1729 /* JMM - all this P_SELECT stuff is bogus */
1731 OSBitOrAtomic(P_SELECT
, &p
->p_flag
);
1732 for (i
= 0; i
< nfds
; i
++) {
1733 short events
= fds
[i
].events
;
1735 /* per spec, ignore fd values below zero */
1736 if (fds
[i
].fd
< 0) {
1741 /* convert the poll event into a kqueue kevent */
1742 struct kevent_internal_s kev
= {
1744 .flags
= EV_ADD
| EV_ONESHOT
| EV_POLL
,
1745 .udata
= CAST_USER_ADDR_T(&fds
[i
]) };
1747 /* Handle input events */
1748 if (events
& ( POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
| POLLHUP
)) {
1749 kev
.filter
= EVFILT_READ
;
1750 if (events
& ( POLLPRI
| POLLRDBAND
))
1751 kev
.flags
|= EV_OOBAND
;
1752 kevent_register(kq
, &kev
, p
);
1755 /* Handle output events */
1756 if ((kev
.flags
& EV_ERROR
) == 0 &&
1757 (events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
))) {
1758 kev
.filter
= EVFILT_WRITE
;
1759 kevent_register(kq
, &kev
, p
);
1762 /* Handle BSD extension vnode events */
1763 if ((kev
.flags
& EV_ERROR
) == 0 &&
1764 (events
& ( POLLEXTEND
| POLLATTRIB
| POLLNLINK
| POLLWRITE
))) {
1765 kev
.filter
= EVFILT_VNODE
;
1767 if (events
& POLLEXTEND
)
1768 kev
.fflags
|= NOTE_EXTEND
;
1769 if (events
& POLLATTRIB
)
1770 kev
.fflags
|= NOTE_ATTRIB
;
1771 if (events
& POLLNLINK
)
1772 kev
.fflags
|= NOTE_LINK
;
1773 if (events
& POLLWRITE
)
1774 kev
.fflags
|= NOTE_WRITE
;
1775 kevent_register(kq
, &kev
, p
);
1778 if (kev
.flags
& EV_ERROR
) {
1779 fds
[i
].revents
= POLLNVAL
;
1786 * Did we have any trouble registering?
1787 * If user space passed 0 FDs, then respect any timeout value passed.
1788 * This is an extremely inefficient sleep. If user space passed one or
1789 * more FDs, and we had trouble registering _all_ of them, then bail
1790 * out. If a subset of the provided FDs failed to register, then we
1791 * will still call the kqueue_scan function.
1793 if (nfds
&& (rfds
== nfds
))
1797 * If any events have trouble registering, an event has fired and we
1798 * shouldn't wait for events in kqueue_scan -- use the current time as
1802 getmicrouptime(&atv
);
1804 /* scan for, and possibly wait for, the kevents to trigger */
1805 cont
->pca_fds
= uap
->fds
;
1806 cont
->pca_nfds
= nfds
;
1807 cont
->pca_rfds
= rfds
;
1808 error
= kqueue_scan(kq
, poll_callback
, NULL
, cont
, NULL
, &atv
, p
);
1809 rfds
= cont
->pca_rfds
;
1812 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1813 /* poll is not restarted after signals... */
1814 if (error
== ERESTART
)
1816 if (error
== EWOULDBLOCK
)
1819 error
= copyout(fds
, uap
->fds
, nfds
* sizeof(struct pollfd
));
1832 poll_callback(__unused
struct kqueue
*kq
, struct kevent_internal_s
*kevp
, void *data
)
1834 struct poll_continue_args
*cont
= (struct poll_continue_args
*)data
;
1835 struct pollfd
*fds
= CAST_DOWN(struct pollfd
*, kevp
->udata
);
1836 short prev_revents
= fds
->revents
;
1839 /* convert the results back into revents */
1840 if (kevp
->flags
& EV_EOF
)
1841 fds
->revents
|= POLLHUP
;
1842 if (kevp
->flags
& EV_ERROR
)
1843 fds
->revents
|= POLLERR
;
1845 switch (kevp
->filter
) {
1847 if (fds
->revents
& POLLHUP
)
1848 mask
= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
);
1850 mask
= (POLLIN
| POLLRDNORM
);
1851 if (kevp
->flags
& EV_OOBAND
)
1852 mask
|= (POLLPRI
| POLLRDBAND
);
1854 fds
->revents
|= (fds
->events
& mask
);
1858 if (!(fds
->revents
& POLLHUP
))
1859 fds
->revents
|= (fds
->events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
));
1863 if (kevp
->fflags
& NOTE_EXTEND
)
1864 fds
->revents
|= (fds
->events
& POLLEXTEND
);
1865 if (kevp
->fflags
& NOTE_ATTRIB
)
1866 fds
->revents
|= (fds
->events
& POLLATTRIB
);
1867 if (kevp
->fflags
& NOTE_LINK
)
1868 fds
->revents
|= (fds
->events
& POLLNLINK
);
1869 if (kevp
->fflags
& NOTE_WRITE
)
1870 fds
->revents
|= (fds
->events
& POLLWRITE
);
1874 if (fds
->revents
!= 0 && prev_revents
== 0)
1881 seltrue(__unused dev_t dev
, __unused
int flag
, __unused
struct proc
*p
)
1890 * Count the number of bits set in the input bit vector, and establish an
1891 * outstanding fp->f_iocount for each of the descriptors which will be in
1892 * use in the select operation.
1894 * Parameters: p The process doing the select
1895 * ibits The input bit vector
1896 * nfd The number of fd's in the vector
1897 * countp Pointer to where to store the bit count
1899 * Returns: 0 Success
1900 * EIO Bad per process open file table
1901 * EBADF One of the bits in the input bit vector
1902 * references an invalid fd
1904 * Implicit: *countp (modified) Count of fd's
1906 * Notes: This function is the first pass under the proc_fdlock() that
1907 * permits us to recognize invalid descriptors in the bit vector;
1908 * the may, however, not remain valid through the drop and
1909 * later reacquisition of the proc_fdlock().
1912 selcount(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int *countp
)
1914 struct filedesc
*fdp
= p
->p_fd
;
1917 struct fileproc
*fp
;
1923 int need_wakeup
= 0;
1926 * Problems when reboot; due to MacOSX signal probs
1927 * in Beaker1C ; verify that the p->p_fd is valid
1933 nw
= howmany(nfd
, NFDBITS
);
1936 for (msk
= 0; msk
< 3; msk
++) {
1937 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1938 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1939 bits
= iptr
[i
/NFDBITS
];
1940 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1943 if (fd
< fdp
->fd_nfiles
)
1944 fp
= fdp
->fd_ofiles
[fd
];
1949 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1969 /* Ignore error return; it's already EBADF */
1970 (void)seldrop_locked(p
, ibits
, nfd
, n
, &need_wakeup
, 1);
1975 wakeup(&p
->p_fpdrainwait
);
1984 * Drop outstanding wait queue references set up during selscan(); drop the
1985 * outstanding per fileproc f_iocount() picked up during the selcount().
1987 * Parameters: p Process performing the select
1988 * ibits Input bit bector of fd's
1989 * nfd Number of fd's
1990 * lim Limit to number of vector entries to
1991 * consider, or -1 for "all"
1993 * need_wakeup Pointer to flag to set to do a wakeup
1994 * if f_iocont on any descriptor goes to 0
1996 * Returns: 0 Success
1997 * EBADF One or more fds in the bit vector
1998 * were invalid, but the rest
1999 * were successfully dropped
2001 * Notes: An fd make become bad while the proc_fdlock() is not held,
2002 * if a multithreaded application closes the fd out from under
2003 * the in progress select. In this case, we still have to
2004 * clean up after the set up on the remaining fds.
2007 seldrop_locked(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int lim
, int *need_wakeup
, int fromselcount
)
2009 struct filedesc
*fdp
= p
->p_fd
;
2010 int msk
, i
, j
, nc
, fd
;
2012 struct fileproc
*fp
;
2017 uthread_t uth
= get_bsdthread_info(current_thread());
2018 struct _select_data
*seldata
;
2023 * Problems when reboot; due to MacOSX signal probs
2024 * in Beaker1C ; verify that the p->p_fd is valid
2030 nw
= howmany(nfd
, NFDBITS
);
2031 seldata
= &uth
->uu_kevent
.ss_select_data
;
2034 for (msk
= 0; msk
< 3; msk
++) {
2035 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
2036 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
2037 bits
= iptr
[i
/NFDBITS
];
2038 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
2040 fp
= fdp
->fd_ofiles
[fd
];
2042 * If we've already dropped as many as were
2043 * counted/scanned, then we are done.
2045 if ((fromselcount
!= 0) && (++dropcount
> lim
))
2049 * unlink even potentially NULL fileprocs.
2050 * If the FD was closed from under us, we
2051 * still need to cleanup the waitq links!
2054 seldata
->wqp
? seldata
->wqp
[nc
] : 0,
2060 /* skip (now) bad fds */
2066 if (fp
->f_iocount
< 0)
2067 panic("f_iocount overdecrement!");
2069 if (fp
->f_iocount
== 0) {
2071 * The last iocount is responsible for clearing
2072 * selconfict flag - even if we didn't set it -
2073 * and is also responsible for waking up anyone
2074 * waiting on iocounts to drain.
2076 if (fp
->f_flags
& FP_SELCONFLICT
)
2077 fp
->f_flags
&= ~FP_SELCONFLICT
;
2078 if (p
->p_fpdrainwait
) {
2079 p
->p_fpdrainwait
= 0;
2092 seldrop(struct proc
*p
, u_int32_t
*ibits
, int nfd
)
2095 int need_wakeup
= 0;
2098 error
= seldrop_locked(p
, ibits
, nfd
, nfd
, &need_wakeup
, 0);
2101 wakeup(&p
->p_fpdrainwait
);
2107 * Record a select request.
2110 selrecord(__unused
struct proc
*selector
, struct selinfo
*sip
, void *s_data
)
2112 thread_t cur_act
= current_thread();
2113 struct uthread
* ut
= get_bsdthread_info(cur_act
);
2114 /* on input, s_data points to the 64-bit ID of a reserved link object */
2115 uint64_t *reserved_link
= (uint64_t *)s_data
;
2117 /* need to look at collisions */
2119 /*do not record if this is second pass of select */
2123 if ((sip
->si_flags
& SI_INITED
) == 0) {
2124 waitq_init(&sip
->si_waitq
, SYNC_POLICY_FIFO
);
2125 sip
->si_flags
|= SI_INITED
;
2126 sip
->si_flags
&= ~SI_CLEAR
;
2129 if (sip
->si_flags
& SI_RECORDED
)
2130 sip
->si_flags
|= SI_COLL
;
2132 sip
->si_flags
&= ~SI_COLL
;
2134 sip
->si_flags
|= SI_RECORDED
;
2135 /* note: this checks for pre-existing linkage */
2136 waitq_link(&sip
->si_waitq
, ut
->uu_wqset
,
2137 WAITQ_SHOULD_LOCK
, reserved_link
);
2140 * Always consume the reserved link.
2141 * We can always call waitq_link_release() safely because if
2142 * waitq_link is successful, it consumes the link and resets the
2143 * value to 0, in which case our call to release becomes a no-op.
2144 * If waitq_link fails, then the following release call will actually
2145 * release the reserved link object.
2147 waitq_link_release(*reserved_link
);
2151 * Use the s_data pointer as an output parameter as well
2152 * This avoids changing the prototype for this function which is
2153 * used by many kexts. We need to surface the waitq object
2154 * associated with the selinfo we just added to the thread's select
2155 * set. New waitq sets do not have back-pointers to set members, so
2156 * the only way to clear out set linkage objects is to go from the
2157 * waitq to the set. We use a memcpy because s_data could be
2158 * pointing to an unaligned value on the stack
2159 * (especially on 32-bit systems)
2161 void *wqptr
= (void *)&sip
->si_waitq
;
2162 memcpy((void *)s_data
, (void *)&wqptr
, sizeof(void *));
2168 selwakeup(struct selinfo
*sip
)
2171 if ((sip
->si_flags
& SI_INITED
) == 0) {
2175 if (sip
->si_flags
& SI_COLL
) {
2177 sip
->si_flags
&= ~SI_COLL
;
2179 /* will not support */
2180 //wakeup((caddr_t)&selwait);
2184 if (sip
->si_flags
& SI_RECORDED
) {
2185 waitq_wakeup64_all(&sip
->si_waitq
, NO_EVENT64
,
2186 THREAD_AWAKENED
, WAITQ_ALL_PRIORITIES
);
2187 sip
->si_flags
&= ~SI_RECORDED
;
2193 selthreadclear(struct selinfo
*sip
)
2197 if ((sip
->si_flags
& SI_INITED
) == 0) {
2200 if (sip
->si_flags
& SI_RECORDED
) {
2202 sip
->si_flags
&= ~(SI_RECORDED
| SI_COLL
);
2204 sip
->si_flags
|= SI_CLEAR
;
2205 sip
->si_flags
&= ~SI_INITED
;
2207 wq
= &sip
->si_waitq
;
2210 * Higher level logic may have a handle on this waitq's prepost ID,
2211 * but that's OK because the waitq_deinit will remove/invalidate the
2212 * prepost object (as well as mark the waitq invalid). This de-couples
2213 * us from any callers that may have a handle to this waitq via the
2222 #define DBG_POST 0x10
2223 #define DBG_WATCH 0x11
2224 #define DBG_WAIT 0x12
2225 #define DBG_MOD 0x13
2226 #define DBG_EWAKEUP 0x14
2227 #define DBG_ENQUEUE 0x15
2228 #define DBG_DEQUEUE 0x16
2230 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
2231 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
2232 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
2233 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
2234 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
2235 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
2236 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
2239 #define EVPROCDEQUE(p, evq) do { \
2241 if (evq->ee_flags & EV_QUEUED) { \
2242 TAILQ_REMOVE(&p->p_evlist, evq, ee_plist); \
2243 evq->ee_flags &= ~EV_QUEUED; \
2250 * called upon socket close. deque and free all events for
2251 * the socket... socket must be locked by caller.
2254 evsofree(struct socket
*sp
)
2256 struct eventqelt
*evq
, *next
;
2262 for (evq
= sp
->so_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
2263 next
= evq
->ee_slist
.tqe_next
;
2266 if (evq
->ee_flags
& EV_QUEUED
) {
2267 EVPROCDEQUE(p
, evq
);
2269 TAILQ_REMOVE(&sp
->so_evlist
, evq
, ee_slist
); // remove from socket q
2276 * called upon pipe close. deque and free all events for
2277 * the pipe... pipe must be locked by caller
2280 evpipefree(struct pipe
*cpipe
)
2282 struct eventqelt
*evq
, *next
;
2285 for (evq
= cpipe
->pipe_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
2286 next
= evq
->ee_slist
.tqe_next
;
2289 EVPROCDEQUE(p
, evq
);
2291 TAILQ_REMOVE(&cpipe
->pipe_evlist
, evq
, ee_slist
); // remove from pipe q
2298 * enqueue this event if it's not already queued. wakeup
2299 * the proc if we do queue this event to it...
2300 * entered with proc lock held... we drop it before
2301 * doing the wakeup and return in that state
2304 evprocenque(struct eventqelt
*evq
)
2311 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_START
, (uint32_t)evq
, evq
->ee_flags
, evq
->ee_eventmask
,0,0);
2315 if (evq
->ee_flags
& EV_QUEUED
) {
2318 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
2321 evq
->ee_flags
|= EV_QUEUED
;
2323 TAILQ_INSERT_TAIL(&p
->p_evlist
, evq
, ee_plist
);
2327 wakeup(&p
->p_evlist
);
2329 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
2334 * pipe lock must be taken by the caller
2337 postpipeevent(struct pipe
*pipep
, int event
)
2340 struct eventqelt
*evq
;
2344 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, event
,0,0,1,0);
2346 for (evq
= pipep
->pipe_evlist
.tqh_first
;
2347 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2349 if (evq
->ee_eventmask
== 0)
2353 switch (event
& (EV_RWBYTES
| EV_RCLOSED
| EV_WCLOSED
)) {
2356 if ((evq
->ee_eventmask
& EV_RE
) && pipep
->pipe_buffer
.cnt
) {
2358 evq
->ee_req
.er_rcnt
= pipep
->pipe_buffer
.cnt
;
2360 if ((evq
->ee_eventmask
& EV_WR
) &&
2361 (MAX(pipep
->pipe_buffer
.size
,PIPE_SIZE
) - pipep
->pipe_buffer
.cnt
) >= PIPE_BUF
) {
2363 if (pipep
->pipe_state
& PIPE_EOF
) {
2364 mask
|= EV_WR
|EV_RESET
;
2368 evq
->ee_req
.er_wcnt
= MAX(pipep
->pipe_buffer
.size
, PIPE_SIZE
) - pipep
->pipe_buffer
.cnt
;
2374 if ((evq
->ee_eventmask
& EV_RE
)) {
2375 mask
|= EV_RE
|EV_RCLOSED
;
2377 if ((evq
->ee_eventmask
& EV_WR
)) {
2378 mask
|= EV_WR
|EV_WCLOSED
;
2387 * disarm... postevents are nops until this event is 'read' via
2388 * waitevent and then re-armed via modwatch
2390 evq
->ee_eventmask
= 0;
2393 * since events are disarmed until after the waitevent
2394 * the ee_req.er_xxxx fields can't change once we've
2395 * inserted this event into the proc queue...
2396 * therefore, the waitevent will see a 'consistent'
2397 * snapshot of the event, even though it won't hold
2398 * the pipe lock, and we're updating the event outside
2399 * of the proc lock, which it will hold
2401 evq
->ee_req
.er_eventbits
|= mask
;
2403 KERNEL_DEBUG(DBG_MISC_POST
, (uint32_t)evq
, evq
->ee_req
.er_eventbits
, mask
, 1,0);
2408 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, 0,0,0,1,0);
2413 * given either a sockbuf or a socket run down the
2414 * event list and queue ready events found...
2415 * the socket must be locked by the caller
2418 postevent(struct socket
*sp
, struct sockbuf
*sb
, int event
)
2421 struct eventqelt
*evq
;
2429 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, (int)sp
, event
, 0, 0, 0);
2431 for (evq
= sp
->so_evlist
.tqh_first
;
2432 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2434 if (evq
->ee_eventmask
== 0)
2438 /* ready for reading:
2439 - byte cnt >= receive low water mark
2440 - read-half of conn closed
2441 - conn pending for listening sock
2442 - socket error pending
2445 - byte cnt avail >= send low water mark
2446 - write half of conn closed
2447 - socket error pending
2448 - non-blocking conn completed successfully
2452 - sock at out of band mark
2455 switch (event
& EV_DMASK
) {
2458 if ((evq
->ee_eventmask
& EV_EX
)) {
2459 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
2460 mask
|= EV_EX
|EV_OOB
;
2464 case EV_RWBYTES
|EV_OOB
:
2465 if ((evq
->ee_eventmask
& EV_EX
)) {
2466 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
2467 mask
|= EV_EX
|EV_OOB
;
2470 * fall into the next case
2473 if ((evq
->ee_eventmask
& EV_RE
) && soreadable(sp
)) {
2474 /* for AFP/OT purposes; may go away in future */
2475 if ((SOCK_DOM(sp
) == PF_INET
||
2476 SOCK_DOM(sp
) == PF_INET6
) &&
2477 SOCK_PROTO(sp
) == IPPROTO_TCP
&&
2478 (sp
->so_error
== ECONNREFUSED
||
2479 sp
->so_error
== ECONNRESET
)) {
2480 if (sp
->so_pcb
== NULL
||
2481 sotoinpcb(sp
)->inp_state
==
2483 (tp
= sototcpcb(sp
)) == NULL
||
2484 tp
->t_state
== TCPS_CLOSED
) {
2485 mask
|= EV_RE
|EV_RESET
;
2490 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
2492 if (sp
->so_state
& SS_CANTRCVMORE
) {
2497 if ((evq
->ee_eventmask
& EV_WR
) && sowriteable(sp
)) {
2498 /* for AFP/OT purposes; may go away in future */
2499 if ((SOCK_DOM(sp
) == PF_INET
||
2500 SOCK_DOM(sp
) == PF_INET6
) &&
2501 SOCK_PROTO(sp
) == IPPROTO_TCP
&&
2502 (sp
->so_error
== ECONNREFUSED
||
2503 sp
->so_error
== ECONNRESET
)) {
2504 if (sp
->so_pcb
== NULL
||
2505 sotoinpcb(sp
)->inp_state
==
2507 (tp
= sototcpcb(sp
)) == NULL
||
2508 tp
->t_state
== TCPS_CLOSED
) {
2509 mask
|= EV_WR
|EV_RESET
;
2514 evq
->ee_req
.er_wcnt
= sbspace(&sp
->so_snd
);
2519 if ((evq
->ee_eventmask
& EV_RE
)) {
2520 mask
|= EV_RE
|EV_RCONN
;
2521 evq
->ee_req
.er_rcnt
= sp
->so_qlen
+ 1; // incl this one
2526 if ((evq
->ee_eventmask
& EV_WR
)) {
2527 mask
|= EV_WR
|EV_WCONN
;
2532 if ((evq
->ee_eventmask
& EV_RE
)) {
2533 mask
|= EV_RE
|EV_RCLOSED
;
2538 if ((evq
->ee_eventmask
& EV_WR
)) {
2539 mask
|= EV_WR
|EV_WCLOSED
;
2544 if (evq
->ee_eventmask
& EV_RE
) {
2545 mask
|= EV_RE
|EV_FIN
;
2551 if (evq
->ee_eventmask
& EV_RE
) {
2552 mask
|= EV_RE
| event
;
2554 if (evq
->ee_eventmask
& EV_WR
) {
2555 mask
|= EV_WR
| event
;
2560 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, -1, 0, 0, 0);
2564 KERNEL_DEBUG(DBG_MISC_POST
, (int)evq
, evq
->ee_eventmask
, evq
->ee_req
.er_eventbits
, mask
, 0);
2568 * disarm... postevents are nops until this event is 'read' via
2569 * waitevent and then re-armed via modwatch
2571 evq
->ee_eventmask
= 0;
2574 * since events are disarmed until after the waitevent
2575 * the ee_req.er_xxxx fields can't change once we've
2576 * inserted this event into the proc queue...
2577 * since waitevent can't see this event until we
2578 * enqueue it, waitevent will see a 'consistent'
2579 * snapshot of the event, even though it won't hold
2580 * the socket lock, and we're updating the event outside
2581 * of the proc lock, which it will hold
2583 evq
->ee_req
.er_eventbits
|= mask
;
2588 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, 0, 0, 0, 0);
2590 #endif /* SOCKETS */
2594 * watchevent system call. user passes us an event to watch
2595 * for. we malloc an event object, initialize it, and queue
2596 * it to the open socket. when the event occurs, postevent()
2597 * will enque it back to our proc where we can retrieve it
2600 * should this prevent duplicate events on same socket?
2603 * ENOMEM No memory for operation
2607 watchevent(proc_t p
, struct watchevent_args
*uap
, __unused
int *retval
)
2609 struct eventqelt
*evq
= (struct eventqelt
*)0;
2610 struct eventqelt
*np
= NULL
;
2611 struct eventreq64
*erp
;
2612 struct fileproc
*fp
= NULL
;
2615 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_START
, 0,0,0,0,0);
2617 // get a qelt and fill with users req
2618 MALLOC(evq
, struct eventqelt
*, sizeof(struct eventqelt
), M_TEMP
, M_WAITOK
);
2624 // get users request pkt
2626 if (IS_64BIT_PROCESS(p
)) {
2627 error
= copyin(uap
->u_req
, (caddr_t
)erp
, sizeof(struct eventreq64
));
2629 struct eventreq32 er32
;
2631 error
= copyin(uap
->u_req
, (caddr_t
)&er32
, sizeof(struct eventreq32
));
2634 * the user only passes in the
2635 * er_type, er_handle and er_data...
2636 * the other fields are initialized
2637 * below, so don't bother to copy
2639 erp
->er_type
= er32
.er_type
;
2640 erp
->er_handle
= er32
.er_handle
;
2641 erp
->er_data
= (user_addr_t
)er32
.er_data
;
2646 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2650 KERNEL_DEBUG(DBG_MISC_WATCH
, erp
->er_handle
,uap
->u_eventmask
,(uint32_t)evq
,0,0);
2652 // validate, freeing qelt if errors
2656 if (erp
->er_type
!= EV_FD
) {
2658 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2661 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2662 socket_lock((struct socket
*)fp
->f_data
, 1);
2663 np
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2664 #endif /* SOCKETS */
2665 } else if (fp
->f_type
== DTYPE_PIPE
) {
2666 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2667 np
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2669 fp_drop(p
, erp
->er_handle
, fp
, 1);
2677 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2682 * only allow one watch per file per proc
2684 for ( ; np
!= NULL
; np
= np
->ee_slist
.tqe_next
) {
2685 if (np
->ee_proc
== p
) {
2687 if (fp
->f_type
== DTYPE_SOCKET
)
2688 socket_unlock((struct socket
*)fp
->f_data
, 1);
2690 #endif /* SOCKETS */
2691 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2692 fp_drop(p
, erp
->er_handle
, fp
, 0);
2695 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2699 erp
->er_ecnt
= erp
->er_rcnt
= erp
->er_wcnt
= erp
->er_eventbits
= 0;
2701 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2705 if (fp
->f_type
== DTYPE_SOCKET
) {
2706 TAILQ_INSERT_TAIL(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2707 postevent((struct socket
*)fp
->f_data
, 0, EV_RWBYTES
); // catch existing events
2709 socket_unlock((struct socket
*)fp
->f_data
, 1);
2711 #endif /* SOCKETS */
2713 TAILQ_INSERT_TAIL(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2714 postpipeevent((struct pipe
*)fp
->f_data
, EV_RWBYTES
);
2716 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2718 fp_drop_event(p
, erp
->er_handle
, fp
);
2720 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, 0,0,0,0,0);
2727 * waitevent system call.
2728 * grabs the next waiting event for this proc and returns
2729 * it. if no events, user can request to sleep with timeout
2730 * or without or poll mode
2731 * ((tv != NULL && interval == 0) || tv == -1)
2734 waitevent(proc_t p
, struct waitevent_args
*uap
, int *retval
)
2737 struct eventqelt
*evq
;
2738 struct eventreq64
*erp
;
2739 uint64_t abstime
, interval
;
2740 boolean_t fast_poll
= FALSE
;
2742 struct eventreq64 er64
;
2743 struct eventreq32 er32
;
2751 * check for fast poll method
2753 if (IS_64BIT_PROCESS(p
)) {
2754 if (uap
->tv
== (user_addr_t
)-1)
2756 } else if (uap
->tv
== (user_addr_t
)((uint32_t)-1))
2759 if (fast_poll
== TRUE
) {
2760 if (p
->p_evlist
.tqh_first
== NULL
) {
2761 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_NONE
, -1,0,0,0,0);
2771 if (IS_64BIT_PROCESS(p
)) {
2772 struct user64_timeval atv64
;
2773 error
= copyin(uap
->tv
, (caddr_t
)&atv64
, sizeof(atv64
));
2774 /* Loses resolution - assume timeout < 68 years */
2775 atv
.tv_sec
= atv64
.tv_sec
;
2776 atv
.tv_usec
= atv64
.tv_usec
;
2778 struct user32_timeval atv32
;
2779 error
= copyin(uap
->tv
, (caddr_t
)&atv32
, sizeof(atv32
));
2780 atv
.tv_sec
= atv32
.tv_sec
;
2781 atv
.tv_usec
= atv32
.tv_usec
;
2786 if (itimerfix(&atv
)) {
2790 interval
= tvtoabstime(&atv
);
2792 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_START
, 0,0,0,0,0);
2796 if ((evq
= p
->p_evlist
.tqh_first
) != NULL
) {
2798 * found one... make a local copy while it's still on the queue
2799 * to prevent it from changing while in the midst of copying
2800 * don't want to hold the proc lock across a copyout because
2801 * it might block on a page fault at the target in user space
2805 if (IS_64BIT_PROCESS(p
))
2806 bcopy((caddr_t
)erp
, (caddr_t
)&uer
.er64
, sizeof (struct eventreq64
));
2808 uer
.er32
.er_type
= erp
->er_type
;
2809 uer
.er32
.er_handle
= erp
->er_handle
;
2810 uer
.er32
.er_data
= (uint32_t)erp
->er_data
;
2811 uer
.er32
.er_ecnt
= erp
->er_ecnt
;
2812 uer
.er32
.er_rcnt
= erp
->er_rcnt
;
2813 uer
.er32
.er_wcnt
= erp
->er_wcnt
;
2814 uer
.er32
.er_eventbits
= erp
->er_eventbits
;
2816 TAILQ_REMOVE(&p
->p_evlist
, evq
, ee_plist
);
2818 evq
->ee_flags
&= ~EV_QUEUED
;
2822 if (IS_64BIT_PROCESS(p
))
2823 error
= copyout((caddr_t
)&uer
.er64
, uap
->u_req
, sizeof(struct eventreq64
));
2825 error
= copyout((caddr_t
)&uer
.er32
, uap
->u_req
, sizeof(struct eventreq32
));
2827 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,
2828 evq
->ee_req
.er_handle
,evq
->ee_req
.er_eventbits
,(uint32_t)evq
,0);
2832 if (uap
->tv
&& interval
== 0) {
2834 *retval
= 1; // poll failed
2836 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,0,0,0,0);
2840 clock_absolutetime_interval_to_deadline(interval
, &abstime
);
2844 KERNEL_DEBUG(DBG_MISC_WAIT
, 1,(uint32_t)&p
->p_evlist
,0,0,0);
2846 error
= msleep1(&p
->p_evlist
, &p
->p_mlock
, (PSOCK
| PCATCH
), "waitevent", abstime
);
2848 KERNEL_DEBUG(DBG_MISC_WAIT
, 2,(uint32_t)&p
->p_evlist
,0,0,0);
2852 if (error
== ERESTART
)
2854 if (error
== EWOULDBLOCK
) {
2861 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, 0,0,0,0,0);
2867 * modwatch system call. user passes in event to modify.
2868 * if we find it we reset the event bits and que/deque event
2872 modwatch(proc_t p
, struct modwatch_args
*uap
, __unused
int *retval
)
2874 struct eventreq64 er
;
2875 struct eventreq64
*erp
= &er
;
2876 struct eventqelt
*evq
= NULL
; /* protected by error return */
2878 struct fileproc
*fp
;
2881 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_START
, 0,0,0,0,0);
2884 * get user's request pkt
2885 * just need the er_type and er_handle which sit above the
2886 * problematic er_data (32/64 issue)... so only copy in
2889 if ((error
= copyin(uap
->u_req
, (caddr_t
)erp
, sizeof(er
.er_type
) + sizeof(er
.er_handle
)))) {
2890 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2895 if (erp
->er_type
!= EV_FD
) {
2897 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2900 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2901 socket_lock((struct socket
*)fp
->f_data
, 1);
2902 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2903 #endif /* SOCKETS */
2904 } else if (fp
->f_type
== DTYPE_PIPE
) {
2905 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2906 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2908 fp_drop(p
, erp
->er_handle
, fp
, 1);
2914 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2918 if ((uap
->u_eventmask
== EV_RM
) && (fp
->f_flags
& FP_WAITEVENT
)) {
2919 fp
->f_flags
&= ~FP_WAITEVENT
;
2923 // locate event if possible
2924 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2925 if (evq
->ee_proc
== p
)
2930 if (fp
->f_type
== DTYPE_SOCKET
)
2931 socket_unlock((struct socket
*)fp
->f_data
, 1);
2933 #endif /* SOCKETS */
2934 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2935 fp_drop(p
, erp
->er_handle
, fp
, 0);
2936 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2939 KERNEL_DEBUG(DBG_MISC_MOD
, erp
->er_handle
,uap
->u_eventmask
,(uint32_t)evq
,0,0);
2941 if (uap
->u_eventmask
== EV_RM
) {
2942 EVPROCDEQUE(p
, evq
);
2945 if (fp
->f_type
== DTYPE_SOCKET
) {
2946 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2947 socket_unlock((struct socket
*)fp
->f_data
, 1);
2949 #endif /* SOCKETS */
2951 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2952 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2954 fp_drop(p
, erp
->er_handle
, fp
, 0);
2956 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, 0,0,0,0,0);
2959 switch (uap
->u_eventmask
& EV_MASK
) {
2977 case EV_EX
|EV_RE
|EV_WR
:
2978 flag
= EV_OOB
|EV_RWBYTES
;
2983 if (fp
->f_type
== DTYPE_SOCKET
)
2984 socket_unlock((struct socket
*)fp
->f_data
, 1);
2986 #endif /* SOCKETS */
2987 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2988 fp_drop(p
, erp
->er_handle
, fp
, 0);
2989 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2993 * since we're holding the socket/pipe lock, the event
2994 * cannot go from the unqueued state to the queued state
2995 * however, it can go from the queued state to the unqueued state
2996 * since that direction is protected by the proc_lock...
2997 * so do a quick check for EV_QUEUED w/o holding the proc lock
2998 * since by far the common case will be NOT EV_QUEUED, this saves
2999 * us taking the proc_lock the majority of the time
3001 if (evq
->ee_flags
& EV_QUEUED
) {
3003 * EVPROCDEQUE will recheck the state after it grabs the proc_lock
3005 EVPROCDEQUE(p
, evq
);
3008 * while the event is off the proc queue and
3009 * we're holding the socket/pipe lock
3010 * it's safe to update these fields...
3012 evq
->ee_req
.er_eventbits
= 0;
3013 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
3016 if (fp
->f_type
== DTYPE_SOCKET
) {
3017 postevent((struct socket
*)fp
->f_data
, 0, flag
);
3018 socket_unlock((struct socket
*)fp
->f_data
, 1);
3020 #endif /* SOCKETS */
3022 postpipeevent((struct pipe
*)fp
->f_data
, flag
);
3023 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
3025 fp_drop(p
, erp
->er_handle
, fp
, 0);
3026 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, evq
->ee_req
.er_handle
,evq
->ee_eventmask
,(uint32_t)fp
->f_data
,flag
,0);
3030 /* this routine is called from the close of fd with proc_fdlock held */
3032 waitevent_close(struct proc
*p
, struct fileproc
*fp
)
3034 struct eventqelt
*evq
;
3037 fp
->f_flags
&= ~FP_WAITEVENT
;
3040 if (fp
->f_type
== DTYPE_SOCKET
) {
3041 socket_lock((struct socket
*)fp
->f_data
, 1);
3042 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
3044 #endif /* SOCKETS */
3045 if (fp
->f_type
== DTYPE_PIPE
) {
3046 PIPE_LOCK((struct pipe
*)fp
->f_data
);
3047 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
3055 // locate event if possible
3056 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
3057 if (evq
->ee_proc
== p
)
3062 if (fp
->f_type
== DTYPE_SOCKET
)
3063 socket_unlock((struct socket
*)fp
->f_data
, 1);
3065 #endif /* SOCKETS */
3066 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
3072 EVPROCDEQUE(p
, evq
);
3075 if (fp
->f_type
== DTYPE_SOCKET
) {
3076 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
3077 socket_unlock((struct socket
*)fp
->f_data
, 1);
3079 #endif /* SOCKETS */
3081 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
3082 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
3095 * Description: Get the host UUID from IOKit and return it to user space.
3097 * Parameters: uuid_buf Pointer to buffer to receive UUID
3098 * timeout Timespec for timout
3099 * spi SPI, skip sandbox check (temporary)
3101 * Returns: 0 Success
3102 * EWOULDBLOCK Timeout is too short
3103 * copyout:EFAULT Bad user buffer
3104 * mac_system_check_info:EPERM Client not allowed to perform this operation
3106 * Notes: A timeout seems redundant, since if it's tolerable to not
3107 * have a system UUID in hand, then why ask for one?
3110 gethostuuid(struct proc
*p
, struct gethostuuid_args
*uap
, __unused
int32_t *retval
)
3114 mach_timespec_t mach_ts
; /* for IOKit call */
3115 __darwin_uuid_t uuid_kern
; /* for IOKit call */
3120 if ((error
= mac_system_check_info(kauth_cred_get(), "hw.uuid")) != 0) {
3121 /* EPERM invokes userspace upcall if present */
3128 /* Convert the 32/64 bit timespec into a mach_timespec_t */
3129 if ( proc_is64bit(p
) ) {
3130 struct user64_timespec ts
;
3131 error
= copyin(uap
->timeoutp
, &ts
, sizeof(ts
));
3134 mach_ts
.tv_sec
= ts
.tv_sec
;
3135 mach_ts
.tv_nsec
= ts
.tv_nsec
;
3137 struct user32_timespec ts
;
3138 error
= copyin(uap
->timeoutp
, &ts
, sizeof(ts
) );
3141 mach_ts
.tv_sec
= ts
.tv_sec
;
3142 mach_ts
.tv_nsec
= ts
.tv_nsec
;
3145 /* Call IOKit with the stack buffer to get the UUID */
3146 kret
= IOBSDGetPlatformUUID(uuid_kern
, mach_ts
);
3149 * If we get it, copy out the data to the user buffer; note that a
3150 * uuid_t is an array of characters, so this is size invariant for
3153 if (kret
== KERN_SUCCESS
) {
3154 error
= copyout(uuid_kern
, uap
->uuid_buf
, sizeof(uuid_kern
));
3156 error
= EWOULDBLOCK
;
3165 * Description: Omnibus system call for ledger operations
3168 ledger(struct proc
*p
, struct ledger_args
*args
, __unused
int32_t *retval
)
3173 int rval
, pid
, len
, error
;
3175 struct ledger_limit_args lla
;
3180 /* Finish copying in the necessary args before taking the proc lock */
3183 if (args
->cmd
== LEDGER_ENTRY_INFO
)
3184 error
= copyin(args
->arg3
, (char *)&len
, sizeof (len
));
3185 else if (args
->cmd
== LEDGER_TEMPLATE_INFO
)
3186 error
= copyin(args
->arg2
, (char *)&len
, sizeof (len
));
3187 else if (args
->cmd
== LEDGER_LIMIT
)
3189 error
= copyin(args
->arg2
, (char *)&lla
, sizeof (lla
));
3193 else if ((args
->cmd
< 0) || (args
->cmd
> LEDGER_MAX_CMD
))
3202 if (args
->cmd
!= LEDGER_TEMPLATE_INFO
) {
3204 proc
= proc_find(pid
);
3209 error
= mac_proc_check_ledger(p
, proc
, args
->cmd
);
3219 switch (args
->cmd
) {
3221 case LEDGER_LIMIT
: {
3222 if (!kauth_cred_issuser(kauth_cred_get()))
3224 rval
= ledger_limit(task
, &lla
);
3230 struct ledger_info info
;
3232 rval
= ledger_info(task
, &info
);
3235 rval
= copyout(&info
, args
->arg2
,
3240 case LEDGER_ENTRY_INFO
: {
3244 rval
= ledger_get_task_entry_info_multiple(task
, &buf
, &len
);
3246 if ((rval
== 0) && (len
>= 0)) {
3247 sz
= len
* sizeof (struct ledger_entry_info
);
3248 rval
= copyout(buf
, args
->arg2
, sz
);
3252 rval
= copyout(&len
, args
->arg3
, sizeof (len
));
3256 case LEDGER_TEMPLATE_INFO
: {
3260 rval
= ledger_template_info(&buf
, &len
);
3261 if ((rval
== 0) && (len
>= 0)) {
3262 sz
= len
* sizeof (struct ledger_template_info
);
3263 rval
= copyout(buf
, args
->arg1
, sz
);
3267 rval
= copyout(&len
, args
->arg2
, sizeof (len
));
3272 panic("ledger syscall logic error -- command type %d", args
->cmd
);
3281 telemetry(__unused
struct proc
*p
, struct telemetry_args
*args
, __unused
int32_t *retval
)
3285 switch (args
->cmd
) {
3286 #if CONFIG_TELEMETRY
3287 case TELEMETRY_CMD_TIMER_EVENT
:
3288 error
= telemetry_timer_event(args
->deadline
, args
->interval
, args
->leeway
);
3290 #endif /* CONFIG_TELEMETRY */
3291 case TELEMETRY_CMD_VOUCHER_NAME
:
3292 if (thread_set_voucher_name((mach_port_name_t
)args
->deadline
))
3304 #if DEVELOPMENT || DEBUG
3305 #if CONFIG_WAITQ_DEBUG
3306 static uint64_t g_wqset_num
= 0;
3309 struct waitq_set
*wqset
;
3312 static queue_head_t g_wqset_list
;
3313 static struct waitq_set
*g_waitq_set
= NULL
;
3315 static inline struct waitq_set
*sysctl_get_wqset(int idx
)
3317 struct g_wqset
*gwqs
;
3320 queue_init(&g_wqset_list
);
3322 /* don't bother with locks: this is test-only code! */
3323 qe_foreach_element(gwqs
, &g_wqset_list
, link
) {
3324 if ((int)(wqset_id(gwqs
->wqset
) & 0xffffffff) == idx
)
3328 /* allocate a new one */
3330 gwqs
= (struct g_wqset
*)kalloc(sizeof(*gwqs
));
3331 assert(gwqs
!= NULL
);
3333 gwqs
->wqset
= waitq_set_alloc(SYNC_POLICY_FIFO
|SYNC_POLICY_PREPOST
, NULL
);
3334 enqueue_tail(&g_wqset_list
, &gwqs
->link
);
3335 printf("[WQ]: created new waitq set 0x%llx\n", wqset_id(gwqs
->wqset
));
3340 #define MAX_GLOBAL_TEST_QUEUES 64
3341 static int g_wq_init
= 0;
3342 static struct waitq g_wq
[MAX_GLOBAL_TEST_QUEUES
];
3344 static inline struct waitq
*global_test_waitq(int idx
)
3351 for (int i
= 0; i
< MAX_GLOBAL_TEST_QUEUES
; i
++)
3352 waitq_init(&g_wq
[i
], SYNC_POLICY_FIFO
);
3355 return &g_wq
[idx
% MAX_GLOBAL_TEST_QUEUES
];
3358 static int sysctl_waitq_wakeup_one SYSCTL_HANDLER_ARGS
3360 #pragma unused(oidp, arg1, arg2)
3363 struct waitq
*waitq
;
3365 int64_t event64
= 0;
3367 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3372 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3375 index
= (int)((-event64
) & 0xffffffff);
3376 waitq
= wqset_waitq(sysctl_get_wqset(index
));
3379 index
= (int)event64
;
3380 waitq
= global_test_waitq(index
);
3385 printf("[WQ]: Waking one thread on waitq [%d] event:0x%llx\n",
3387 kr
= waitq_wakeup64_one(waitq
, (event64_t
)event64
, THREAD_AWAKENED
,
3388 WAITQ_ALL_PRIORITIES
);
3389 printf("[WQ]: \tkr=%d\n", kr
);
3391 return SYSCTL_OUT(req
, &kr
, sizeof(kr
));
3393 SYSCTL_PROC(_kern
, OID_AUTO
, waitq_wakeup_one
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3394 0, 0, sysctl_waitq_wakeup_one
, "Q", "wakeup one thread waiting on given event");
3397 static int sysctl_waitq_wakeup_all SYSCTL_HANDLER_ARGS
3399 #pragma unused(oidp, arg1, arg2)
3402 struct waitq
*waitq
;
3404 int64_t event64
= 0;
3406 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3411 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3414 index
= (int)((-event64
) & 0xffffffff);
3415 waitq
= wqset_waitq(sysctl_get_wqset(index
));
3418 index
= (int)event64
;
3419 waitq
= global_test_waitq(index
);
3424 printf("[WQ]: Waking all threads on waitq [%d] event:0x%llx\n",
3426 kr
= waitq_wakeup64_all(waitq
, (event64_t
)event64
,
3427 THREAD_AWAKENED
, WAITQ_ALL_PRIORITIES
);
3428 printf("[WQ]: \tkr=%d\n", kr
);
3430 return SYSCTL_OUT(req
, &kr
, sizeof(kr
));
3432 SYSCTL_PROC(_kern
, OID_AUTO
, waitq_wakeup_all
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3433 0, 0, sysctl_waitq_wakeup_all
, "Q", "wakeup all threads waiting on given event");
3436 static int sysctl_waitq_wait SYSCTL_HANDLER_ARGS
3438 #pragma unused(oidp, arg1, arg2)
3441 struct waitq
*waitq
;
3443 int64_t event64
= 0;
3445 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3450 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3453 index
= (int)((-event64
) & 0xffffffff);
3454 waitq
= wqset_waitq(sysctl_get_wqset(index
));
3457 index
= (int)event64
;
3458 waitq
= global_test_waitq(index
);
3463 printf("[WQ]: Current thread waiting on waitq [%d] event:0x%llx\n",
3465 kr
= waitq_assert_wait64(waitq
, (event64_t
)event64
, THREAD_INTERRUPTIBLE
, 0);
3466 if (kr
== THREAD_WAITING
)
3467 thread_block(THREAD_CONTINUE_NULL
);
3468 printf("[WQ]: \tWoke Up: kr=%d\n", kr
);
3470 return SYSCTL_OUT(req
, &kr
, sizeof(kr
));
3472 SYSCTL_PROC(_kern
, OID_AUTO
, waitq_wait
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3473 0, 0, sysctl_waitq_wait
, "Q", "start waiting on given event");
3476 static int sysctl_wqset_select SYSCTL_HANDLER_ARGS
3478 #pragma unused(oidp, arg1, arg2)
3480 struct waitq_set
*wqset
;
3481 uint64_t event64
= 0;
3483 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3490 wqset
= sysctl_get_wqset((int)(event64
& 0xffffffff));
3491 g_waitq_set
= wqset
;
3493 event64
= wqset_id(wqset
);
3494 printf("[WQ]: selected wqset 0x%llx\n", event64
);
3498 event64
= wqset_id(g_waitq_set
);
3500 event64
= (uint64_t)(-1);
3502 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3504 SYSCTL_PROC(_kern
, OID_AUTO
, wqset_select
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3505 0, 0, sysctl_wqset_select
, "Q", "select/create a global waitq set");
3508 static int sysctl_waitq_link SYSCTL_HANDLER_ARGS
3510 #pragma unused(oidp, arg1, arg2)
3513 struct waitq
*waitq
;
3514 struct waitq_set
*wqset
;
3516 uint64_t reserved_link
= 0;
3517 int64_t event64
= 0;
3519 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3524 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3527 g_waitq_set
= sysctl_get_wqset(1);
3528 wqset
= g_waitq_set
;
3531 struct waitq_set
*tmp
;
3532 index
= (int)((-event64
) & 0xffffffff);
3533 tmp
= sysctl_get_wqset(index
);
3536 waitq
= wqset_waitq(tmp
);
3539 index
= (int)event64
;
3540 waitq
= global_test_waitq(index
);
3543 printf("[WQ]: linking waitq [%d] to global wqset (0x%llx)\n",
3544 index
, wqset_id(wqset
));
3545 reserved_link
= waitq_link_reserve(waitq
);
3546 kr
= waitq_link(waitq
, wqset
, WAITQ_SHOULD_LOCK
, &reserved_link
);
3547 waitq_link_release(reserved_link
);
3549 printf("[WQ]: \tkr=%d\n", kr
);
3552 return SYSCTL_OUT(req
, &kr
, sizeof(kr
));
3554 SYSCTL_PROC(_kern
, OID_AUTO
, waitq_link
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3555 0, 0, sysctl_waitq_link
, "Q", "link global waitq to test waitq set");
3558 static int sysctl_waitq_unlink SYSCTL_HANDLER_ARGS
3560 #pragma unused(oidp, arg1, arg2)
3563 struct waitq
*waitq
;
3564 struct waitq_set
*wqset
;
3566 uint64_t event64
= 0;
3568 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3573 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3576 g_waitq_set
= sysctl_get_wqset(1);
3577 wqset
= g_waitq_set
;
3579 index
= (int)event64
;
3580 waitq
= global_test_waitq(index
);
3582 printf("[WQ]: unlinking waitq [%d] from global wqset (0x%llx)\n",
3583 index
, wqset_id(wqset
));
3585 kr
= waitq_unlink(waitq
, wqset
);
3586 printf("[WQ]: \tkr=%d\n", kr
);
3588 return SYSCTL_OUT(req
, &kr
, sizeof(kr
));
3590 SYSCTL_PROC(_kern
, OID_AUTO
, waitq_unlink
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3591 0, 0, sysctl_waitq_unlink
, "Q", "unlink global waitq from test waitq set");
3594 static int sysctl_waitq_clear_prepost SYSCTL_HANDLER_ARGS
3596 #pragma unused(oidp, arg1, arg2)
3597 struct waitq
*waitq
;
3598 uint64_t event64
= 0;
3601 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3606 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3608 index
= (int)event64
;
3609 waitq
= global_test_waitq(index
);
3611 printf("[WQ]: clearing prepost on waitq [%d]\n", index
);
3612 waitq_clear_prepost(waitq
);
3614 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3616 SYSCTL_PROC(_kern
, OID_AUTO
, waitq_clear_prepost
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3617 0, 0, sysctl_waitq_clear_prepost
, "Q", "clear prepost on given waitq");
3620 static int sysctl_wqset_unlink_all SYSCTL_HANDLER_ARGS
3622 #pragma unused(oidp, arg1, arg2)
3624 struct waitq_set
*wqset
;
3626 uint64_t event64
= 0;
3628 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3633 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3636 g_waitq_set
= sysctl_get_wqset(1);
3637 wqset
= g_waitq_set
;
3639 printf("[WQ]: unlinking all queues from global wqset (0x%llx)\n",
3642 kr
= waitq_set_unlink_all(wqset
);
3643 printf("[WQ]: \tkr=%d\n", kr
);
3645 return SYSCTL_OUT(req
, &kr
, sizeof(kr
));
3647 SYSCTL_PROC(_kern
, OID_AUTO
, wqset_unlink_all
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3648 0, 0, sysctl_wqset_unlink_all
, "Q", "unlink all queues from test waitq set");
3651 static int sysctl_wqset_clear_preposts SYSCTL_HANDLER_ARGS
3653 #pragma unused(oidp, arg1, arg2)
3654 struct waitq_set
*wqset
= NULL
;
3655 uint64_t event64
= 0;
3658 error
= SYSCTL_IN(req
, &event64
, sizeof(event64
));
3665 index
= (int)((event64
) & 0xffffffff);
3666 wqset
= sysctl_get_wqset(index
);
3667 assert(wqset
!= NULL
);
3669 printf("[WQ]: clearing preposts on wqset 0x%llx\n", wqset_id(wqset
));
3670 waitq_set_clear_preposts(wqset
);
3674 event64
= wqset_id(wqset
);
3676 event64
= (uint64_t)(-1);
3678 return SYSCTL_OUT(req
, &event64
, sizeof(event64
));
3680 SYSCTL_PROC(_kern
, OID_AUTO
, wqset_clear_preposts
, CTLTYPE_QUAD
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
3681 0, 0, sysctl_wqset_clear_preposts
, "Q", "clear preposts on given waitq set");
3683 #endif /* CONFIG_WAITQ_DEBUG */
3684 #endif /* DEVELOPMENT || DEBUG */