2 * Copyright (c) 2000-2014 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 <mach/mach_types.h>
101 #include <kern/kern_types.h>
102 #include <kern/assert.h>
103 #include <kern/kalloc.h>
104 #include <kern/thread.h>
105 #include <kern/clock.h>
106 #include <kern/ledger.h>
107 #include <kern/task.h>
108 #include <kern/telemetry.h>
110 #include <sys/mbuf.h>
111 #include <sys/domain.h>
112 #include <sys/socket.h>
113 #include <sys/socketvar.h>
114 #include <sys/errno.h>
115 #include <sys/syscall.h>
116 #include <sys/pipe.h>
118 #include <security/audit/audit.h>
121 #include <net/route.h>
123 #include <netinet/in.h>
124 #include <netinet/in_systm.h>
125 #include <netinet/ip.h>
126 #include <netinet/in_pcb.h>
127 #include <netinet/ip_var.h>
128 #include <netinet/ip6.h>
129 #include <netinet/tcp.h>
130 #include <netinet/tcp_fsm.h>
131 #include <netinet/tcp_seq.h>
132 #include <netinet/tcp_timer.h>
133 #include <netinet/tcp_var.h>
134 #include <netinet/tcpip.h>
135 #include <netinet/tcp_debug.h>
136 /* for wait queue based select */
137 #include <kern/wait_queue.h>
138 #include <kern/kalloc.h>
139 #include <sys/vnode_internal.h>
141 /* XXX should be in a header file somewhere */
142 void evsofree(struct socket
*);
143 void evpipefree(struct pipe
*);
144 void postpipeevent(struct pipe
*, int);
145 void postevent(struct socket
*, struct sockbuf
*, int);
146 extern kern_return_t
IOBSDGetPlatformUUID(__darwin_uuid_t uuid
, mach_timespec_t timeoutp
);
148 int rd_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
);
149 int wr_uio(struct proc
*p
, struct fileproc
*fp
, uio_t uio
, user_ssize_t
*retval
);
151 __private_extern__
int dofileread(vfs_context_t ctx
, struct fileproc
*fp
,
152 user_addr_t bufp
, user_size_t nbyte
,
153 off_t offset
, int flags
, user_ssize_t
*retval
);
154 __private_extern__
int dofilewrite(vfs_context_t ctx
, struct fileproc
*fp
,
155 user_addr_t bufp
, user_size_t nbyte
,
156 off_t offset
, int flags
, user_ssize_t
*retval
);
157 __private_extern__
int preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_vnode
);
158 __private_extern__
void donefileread(struct proc
*p
, struct fileproc
*fp_ret
, int fd
);
161 /* Conflict wait queue for when selects collide (opaque type) */
162 struct wait_queue select_conflict_queue
;
165 * Init routine called from bsd_init.c
167 void select_wait_queue_init(void);
169 select_wait_queue_init(void)
171 wait_queue_init(&select_conflict_queue
, SYNC_POLICY_FIFO
);
174 #define f_flag f_fglob->fg_flag
175 #define f_type f_fglob->fg_ops->fo_type
176 #define f_msgcount f_fglob->fg_msgcount
177 #define f_cred f_fglob->fg_cred
178 #define f_ops f_fglob->fg_ops
179 #define f_offset f_fglob->fg_offset
180 #define f_data f_fglob->fg_data
186 * preparefileread:EBADF
187 * preparefileread:ESPIPE
188 * preparefileread:ENXIO
189 * preparefileread:EBADF
193 read(struct proc
*p
, struct read_args
*uap
, user_ssize_t
*retval
)
195 __pthread_testcancel(1);
196 return(read_nocancel(p
, (struct read_nocancel_args
*)uap
, retval
));
200 read_nocancel(struct proc
*p
, struct read_nocancel_args
*uap
, user_ssize_t
*retval
)
205 struct vfs_context context
;
207 if ( (error
= preparefileread(p
, &fp
, fd
, 0)) )
210 context
= *(vfs_context_current());
211 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
213 error
= dofileread(&context
, fp
, uap
->cbuf
, uap
->nbyte
,
214 (off_t
)-1, 0, retval
);
216 donefileread(p
, fp
, fd
);
225 * preparefileread:EBADF
226 * preparefileread:ESPIPE
227 * preparefileread:ENXIO
228 * preparefileread:EBADF
232 pread(struct proc
*p
, struct pread_args
*uap
, user_ssize_t
*retval
)
234 __pthread_testcancel(1);
235 return(pread_nocancel(p
, (struct pread_nocancel_args
*)uap
, retval
));
239 pread_nocancel(struct proc
*p
, struct pread_nocancel_args
*uap
, user_ssize_t
*retval
)
241 struct fileproc
*fp
= NULL
; /* fp set by preparefileread() */
244 struct vfs_context context
;
246 if ( (error
= preparefileread(p
, &fp
, fd
, 1)) )
249 context
= *(vfs_context_current());
250 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
252 error
= dofileread(&context
, fp
, uap
->buf
, uap
->nbyte
,
253 uap
->offset
, FOF_OFFSET
, retval
);
255 donefileread(p
, fp
, fd
);
257 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pread
) | DBG_FUNC_NONE
),
258 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
265 * Code common for read and pread
269 donefileread(struct proc
*p
, struct fileproc
*fp
, int fd
)
272 fp_drop(p
, fd
, fp
, 1);
285 preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_pread
)
295 error
= fp_lookup(p
, fd
, &fp
, 1);
301 if ((fp
->f_flag
& FREAD
) == 0) {
305 if (check_for_pread
&& (fp
->f_type
!= DTYPE_VNODE
)) {
309 if (fp
->f_type
== DTYPE_VNODE
) {
310 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
312 if (check_for_pread
&& (vnode_isfifo(vp
))) {
316 if (check_for_pread
&& (vp
->v_flag
& VISTTY
)) {
328 fp_drop(p
, fd
, fp
, 1);
339 __private_extern__
int
340 dofileread(vfs_context_t ctx
, struct fileproc
*fp
,
341 user_addr_t bufp
, user_size_t nbyte
, off_t offset
, int flags
,
342 user_ssize_t
*retval
)
345 user_ssize_t bytecnt
;
347 char uio_buf
[ UIO_SIZEOF(1) ];
352 if (IS_64BIT_PROCESS(vfs_context_proc(ctx
))) {
353 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_READ
,
354 &uio_buf
[0], sizeof(uio_buf
));
356 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_READ
,
357 &uio_buf
[0], sizeof(uio_buf
));
359 uio_addiov(auio
, bufp
, nbyte
);
363 if ((error
= fo_read(fp
, auio
, flags
, ctx
))) {
364 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
365 error
== EINTR
|| error
== EWOULDBLOCK
))
368 bytecnt
-= uio_resid(auio
);
376 * Scatter read system call.
385 readv(struct proc
*p
, struct readv_args
*uap
, user_ssize_t
*retval
)
387 __pthread_testcancel(1);
388 return(readv_nocancel(p
, (struct readv_nocancel_args
*)uap
, retval
));
392 readv_nocancel(struct proc
*p
, struct readv_nocancel_args
*uap
, user_ssize_t
*retval
)
396 struct user_iovec
*iovp
;
398 /* Verify range bedfore calling uio_create() */
399 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
402 /* allocate a uio large enough to hold the number of iovecs passed */
403 auio
= uio_create(uap
->iovcnt
, 0,
404 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
407 /* get location of iovecs within the uio. then copyin the iovecs from
410 iovp
= uio_iovsaddr(auio
);
413 goto ExitThisRoutine
;
415 error
= copyin_user_iovec_array(uap
->iovp
,
416 IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
,
419 goto ExitThisRoutine
;
422 /* finalize uio_t for use and do the IO
424 error
= uio_calculateresid(auio
);
426 goto ExitThisRoutine
;
428 error
= rd_uio(p
, uap
->fd
, auio
, retval
);
446 write(struct proc
*p
, struct write_args
*uap
, user_ssize_t
*retval
)
448 __pthread_testcancel(1);
449 return(write_nocancel(p
, (struct write_nocancel_args
*)uap
, retval
));
454 write_nocancel(struct proc
*p
, struct write_nocancel_args
*uap
, user_ssize_t
*retval
)
459 bool wrote_some
= false;
463 error
= fp_lookup(p
,fd
,&fp
,0);
466 if ((fp
->f_flag
& FWRITE
) == 0) {
468 } else if (FP_ISGUARDED(fp
, GUARD_WRITE
)) {
470 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_WRITE
);
473 struct vfs_context context
= *(vfs_context_current());
474 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
476 error
= dofilewrite(&context
, fp
, uap
->cbuf
, uap
->nbyte
,
477 (off_t
)-1, 0, retval
);
479 wrote_some
= *retval
> 0;
482 fp_drop_written(p
, fd
, fp
);
484 fp_drop(p
, fd
, fp
, 0);
500 pwrite(struct proc
*p
, struct pwrite_args
*uap
, user_ssize_t
*retval
)
502 __pthread_testcancel(1);
503 return(pwrite_nocancel(p
, (struct pwrite_nocancel_args
*)uap
, retval
));
507 pwrite_nocancel(struct proc
*p
, struct pwrite_nocancel_args
*uap
, user_ssize_t
*retval
)
512 vnode_t vp
= (vnode_t
)0;
513 bool wrote_some
= false;
517 error
= fp_lookup(p
,fd
,&fp
,0);
521 if ((fp
->f_flag
& FWRITE
) == 0) {
523 } else if (FP_ISGUARDED(fp
, GUARD_WRITE
)) {
525 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_WRITE
);
528 struct vfs_context context
= *vfs_context_current();
529 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
531 if (fp
->f_type
!= DTYPE_VNODE
) {
535 vp
= (vnode_t
)fp
->f_fglob
->fg_data
;
536 if (vnode_isfifo(vp
)) {
540 if ((vp
->v_flag
& VISTTY
)) {
544 if (uap
->offset
== (off_t
)-1) {
549 error
= dofilewrite(&context
, fp
, uap
->buf
, uap
->nbyte
,
550 uap
->offset
, FOF_OFFSET
, retval
);
551 wrote_some
= *retval
> 0;
555 fp_drop_written(p
, fd
, fp
);
557 fp_drop(p
, fd
, fp
, 0);
559 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pwrite
) | DBG_FUNC_NONE
),
560 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
569 * <fo_write>:??? [indirect through struct fileops]
571 __private_extern__
int
572 dofilewrite(vfs_context_t ctx
, struct fileproc
*fp
,
573 user_addr_t bufp
, user_size_t nbyte
, off_t offset
, int flags
,
574 user_ssize_t
*retval
)
578 user_ssize_t bytecnt
;
579 char uio_buf
[ UIO_SIZEOF(1) ];
581 if (nbyte
> INT_MAX
) {
586 if (IS_64BIT_PROCESS(vfs_context_proc(ctx
))) {
587 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_WRITE
,
588 &uio_buf
[0], sizeof(uio_buf
));
590 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_WRITE
,
591 &uio_buf
[0], sizeof(uio_buf
));
593 uio_addiov(auio
, bufp
, nbyte
);
596 if ((error
= fo_write(fp
, auio
, flags
, ctx
))) {
597 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
598 error
== EINTR
|| error
== EWOULDBLOCK
))
600 /* The socket layer handles SIGPIPE */
601 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
&&
602 (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) == 0) {
603 /* XXX Raise the signal on the thread? */
604 psignal(vfs_context_proc(ctx
), SIGPIPE
);
607 bytecnt
-= uio_resid(auio
);
614 * Gather write system call
617 writev(struct proc
*p
, struct writev_args
*uap
, user_ssize_t
*retval
)
619 __pthread_testcancel(1);
620 return(writev_nocancel(p
, (struct writev_nocancel_args
*)uap
, retval
));
624 writev_nocancel(struct proc
*p
, struct writev_nocancel_args
*uap
, user_ssize_t
*retval
)
629 struct user_iovec
*iovp
;
630 bool wrote_some
= false;
632 AUDIT_ARG(fd
, uap
->fd
);
634 /* Verify range bedfore calling uio_create() */
635 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
638 /* allocate a uio large enough to hold the number of iovecs passed */
639 auio
= uio_create(uap
->iovcnt
, 0,
640 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
643 /* get location of iovecs within the uio. then copyin the iovecs from
646 iovp
= uio_iovsaddr(auio
);
649 goto ExitThisRoutine
;
651 error
= copyin_user_iovec_array(uap
->iovp
,
652 IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
,
655 goto ExitThisRoutine
;
658 /* finalize uio_t for use and do the IO
660 error
= uio_calculateresid(auio
);
662 goto ExitThisRoutine
;
665 error
= fp_lookup(p
, uap
->fd
, &fp
, 0);
667 goto ExitThisRoutine
;
669 if ((fp
->f_flag
& FWRITE
) == 0) {
671 } else if (FP_ISGUARDED(fp
, GUARD_WRITE
)) {
673 error
= fp_guard_exception(p
, uap
->fd
, fp
, kGUARD_EXC_WRITE
);
676 error
= wr_uio(p
, fp
, auio
, retval
);
677 wrote_some
= *retval
> 0;
681 fp_drop_written(p
, uap
->fd
, fp
);
683 fp_drop(p
, uap
->fd
, fp
, 0);
694 wr_uio(struct proc
*p
, struct fileproc
*fp
, uio_t uio
, user_ssize_t
*retval
)
698 struct vfs_context context
= *vfs_context_current();
700 count
= uio_resid(uio
);
702 context
.vc_ucred
= fp
->f_cred
;
703 error
= fo_write(fp
, uio
, 0, &context
);
705 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
706 error
== EINTR
|| error
== EWOULDBLOCK
))
708 /* The socket layer handles SIGPIPE */
709 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
&&
710 (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) == 0)
713 *retval
= count
- uio_resid(uio
);
720 rd_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
)
725 struct vfs_context context
= *vfs_context_current();
727 if ( (error
= preparefileread(p
, &fp
, fdes
, 0)) )
730 count
= uio_resid(uio
);
732 context
.vc_ucred
= fp
->f_cred
;
734 error
= fo_read(fp
, uio
, 0, &context
);
737 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
738 error
== EINTR
|| error
== EWOULDBLOCK
))
741 *retval
= count
- uio_resid(uio
);
743 donefileread(p
, fp
, fdes
);
758 * fp_lookup:EBADF Bad file descriptor
762 ioctl(struct proc
*p
, struct ioctl_args
*uap
, __unused
int32_t *retval
)
764 struct fileproc
*fp
= NULL
;
767 caddr_t datap
= NULL
, memp
= NULL
;
768 boolean_t is64bit
= FALSE
;
770 #define STK_PARAMS 128
771 char stkbuf
[STK_PARAMS
];
773 u_long com
= uap
->com
;
774 struct vfs_context context
= *vfs_context_current();
776 AUDIT_ARG(fd
, uap
->fd
);
777 AUDIT_ARG(addr
, uap
->data
);
779 is64bit
= proc_is64bit(p
);
782 AUDIT_ARG(value64
, com
);
784 AUDIT_ARG(cmd
, CAST_DOWN_EXPLICIT(int, com
));
785 #endif /* CONFIG_AUDIT */
788 * Interpret high order word to find amount of data to be
789 * copied to/from the user's address space.
791 size
= IOCPARM_LEN(com
);
792 if (size
> IOCPARM_MAX
)
794 if (size
> sizeof (stkbuf
)) {
795 if ((memp
= (caddr_t
)kalloc(size
)) == 0)
802 error
= copyin(uap
->data
, datap
, size
);
806 /* XXX - IOC_IN and no size? we should proably return an error here!! */
808 *(user_addr_t
*)datap
= uap
->data
;
811 *(uint32_t *)datap
= (uint32_t)uap
->data
;
814 } else if ((com
& IOC_OUT
) && size
)
816 * Zero the buffer so the user always
817 * gets back something deterministic.
820 else if (com
& IOC_VOID
) {
821 /* XXX - this is odd since IOC_VOID means no parameters */
823 *(user_addr_t
*)datap
= uap
->data
;
826 *(uint32_t *)datap
= (uint32_t)uap
->data
;
831 error
= fp_lookup(p
,fd
,&fp
,1);
837 AUDIT_ARG(file
, p
, fp
);
839 if ((fp
->f_flag
& (FREAD
| FWRITE
)) == 0) {
844 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
847 error
= mac_file_check_ioctl(context
.vc_ucred
, fp
->f_fglob
, com
);
854 *fdflags(p
, fd
) &= ~UF_EXCLOSE
;
858 *fdflags(p
, fd
) |= UF_EXCLOSE
;
862 if ( (tmp
= *(int *)datap
) )
863 fp
->f_flag
|= FNONBLOCK
;
865 fp
->f_flag
&= ~FNONBLOCK
;
866 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
870 if ( (tmp
= *(int *)datap
) )
871 fp
->f_flag
|= FASYNC
;
873 fp
->f_flag
&= ~FASYNC
;
874 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
879 if (fp
->f_type
== DTYPE_SOCKET
) {
880 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
883 if (fp
->f_type
== DTYPE_PIPE
) {
884 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
890 struct proc
*p1
= proc_find(tmp
);
898 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
902 if (fp
->f_type
== DTYPE_SOCKET
) {
903 *(int *)datap
= ((struct socket
*)fp
->f_data
)->so_pgid
;
906 error
= fo_ioctl(fp
, TIOCGPGRP
, datap
, &context
);
907 *(int *)datap
= -*(int *)datap
;
911 error
= fo_ioctl(fp
, com
, datap
, &context
);
913 * Copy any data to user, size was
914 * already set and checked above.
916 if (error
== 0 && (com
& IOC_OUT
) && size
)
917 error
= copyout(datap
, uap
->data
, (u_int
)size
);
921 fp_drop(p
, fd
, fp
, 1);
930 int selwait
, nselcoll
;
931 #define SEL_FIRSTPASS 1
932 #define SEL_SECONDPASS 2
933 extern int selcontinue(int error
);
934 extern int selprocess(int error
, int sel_pass
);
935 static int selscan(struct proc
*p
, struct _select
* sel
, struct _select_data
* seldata
,
936 int nfd
, int32_t *retval
, int sel_pass
, wait_queue_sub_t wqsub
);
937 static int selcount(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int *count
);
938 static int seldrop_locked(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int lim
, int *need_wakeup
, int fromselcount
);
939 static int seldrop(struct proc
*p
, u_int32_t
*ibits
, int nfd
);
942 * Select system call.
945 * EINVAL Invalid argument
946 * EAGAIN Nonconformant error if allocation fails
950 select(struct proc
*p
, struct select_args
*uap
, int32_t *retval
)
952 __pthread_testcancel(1);
953 return(select_nocancel(p
, (struct select_nocancel_args
*)uap
, retval
));
957 select_nocancel(struct proc
*p
, struct select_nocancel_args
*uap
, int32_t *retval
)
964 struct _select_data
*seldata
;
965 int needzerofill
= 1;
968 th_act
= current_thread();
969 uth
= get_bsdthread_info(th_act
);
970 sel
= &uth
->uu_select
;
971 seldata
= &uth
->uu_kevent
.ss_select_data
;
975 seldata
->retval
= retval
;
981 /* select on thread of process that already called proc_exit() */
982 if (p
->p_fd
== NULL
) {
986 if (uap
->nd
> p
->p_fd
->fd_nfiles
)
987 uap
->nd
= p
->p_fd
->fd_nfiles
; /* forgiving; slightly wrong */
989 nw
= howmany(uap
->nd
, NFDBITS
);
990 ni
= nw
* sizeof(fd_mask
);
993 * if the previously allocated space for the bits is smaller than
994 * what is requested or no space has yet been allocated for this
995 * thread, allocate enough space now.
997 * Note: If this process fails, select() will return EAGAIN; this
998 * is the same thing pool() returns in a no-memory situation, but
999 * it is not a POSIX compliant error code for select().
1001 if (sel
->nbytes
< (3 * ni
)) {
1002 int nbytes
= 3 * ni
;
1004 /* Free previous allocation, if any */
1005 if (sel
->ibits
!= NULL
)
1006 FREE(sel
->ibits
, M_TEMP
);
1007 if (sel
->obits
!= NULL
) {
1008 FREE(sel
->obits
, M_TEMP
);
1009 /* NULL out; subsequent ibits allocation may fail */
1013 MALLOC(sel
->ibits
, u_int32_t
*, nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
1014 if (sel
->ibits
== NULL
)
1016 MALLOC(sel
->obits
, u_int32_t
*, nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
1017 if (sel
->obits
== NULL
) {
1018 FREE(sel
->ibits
, M_TEMP
);
1022 sel
->nbytes
= nbytes
;
1027 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
1028 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
1032 * get the bits from the user address space
1034 #define getbits(name, x) \
1036 if (uap->name && (error = copyin(uap->name, \
1037 (caddr_t)&sel->ibits[(x) * nw], ni))) \
1038 goto continuation; \
1048 if (IS_64BIT_PROCESS(p
)) {
1049 struct user64_timeval atv64
;
1050 error
= copyin(uap
->tv
, (caddr_t
)&atv64
, sizeof(atv64
));
1051 /* Loses resolution - assume timeout < 68 years */
1052 atv
.tv_sec
= atv64
.tv_sec
;
1053 atv
.tv_usec
= atv64
.tv_usec
;
1055 struct user32_timeval atv32
;
1056 error
= copyin(uap
->tv
, (caddr_t
)&atv32
, sizeof(atv32
));
1057 atv
.tv_sec
= atv32
.tv_sec
;
1058 atv
.tv_usec
= atv32
.tv_usec
;
1062 if (itimerfix(&atv
)) {
1067 clock_absolutetime_interval_to_deadline(
1068 tvtoabstime(&atv
), &seldata
->abstime
);
1071 seldata
->abstime
= 0;
1073 if ( (error
= selcount(p
, sel
->ibits
, uap
->nd
, &count
)) ) {
1077 seldata
->count
= count
;
1078 size
= SIZEOF_WAITQUEUE_SET
+ (count
* SIZEOF_WAITQUEUE_LINK
);
1079 if (uth
->uu_allocsize
) {
1080 if (uth
->uu_wqset
== 0)
1081 panic("select: wql memory smashed");
1082 /* needed for the select now */
1083 if (size
> uth
->uu_allocsize
) {
1084 kfree(uth
->uu_wqset
, uth
->uu_allocsize
);
1085 uth
->uu_allocsize
= size
;
1086 uth
->uu_wqset
= (wait_queue_set_t
)kalloc(size
);
1087 if (uth
->uu_wqset
== (wait_queue_set_t
)NULL
)
1088 panic("failed to allocate memory for waitqueue\n");
1091 uth
->uu_allocsize
= size
;
1092 uth
->uu_wqset
= (wait_queue_set_t
)kalloc(uth
->uu_allocsize
);
1093 if (uth
->uu_wqset
== (wait_queue_set_t
)NULL
)
1094 panic("failed to allocate memory for waitqueue\n");
1096 bzero(uth
->uu_wqset
, size
);
1097 seldata
->wql
= (char *)uth
->uu_wqset
+ SIZEOF_WAITQUEUE_SET
;
1098 wait_queue_set_init(uth
->uu_wqset
, (SYNC_POLICY_FIFO
| SYNC_POLICY_PREPOST
));
1104 * We have already cleaned up any state we established,
1105 * either locally or as a result of selcount(). We don't
1106 * need to wait_subqueue_unlink_all(), since we haven't set
1107 * anything at this point.
1112 return selprocess(0, SEL_FIRSTPASS
);
1116 selcontinue(int error
)
1118 return selprocess(error
, SEL_SECONDPASS
);
1125 * Parameters: error The error code from our caller
1126 * sel_pass The pass we are on
1129 selprocess(int error
, int sel_pass
)
1134 struct uthread
*uth
;
1136 struct select_nocancel_args
*uap
;
1138 struct _select
*sel
;
1139 struct _select_data
*seldata
;
1144 wait_result_t wait_result
;
1147 th_act
= current_thread();
1148 uth
= get_bsdthread_info(th_act
);
1149 sel
= &uth
->uu_select
;
1150 seldata
= &uth
->uu_kevent
.ss_select_data
;
1151 uap
= seldata
->args
;
1152 retval
= seldata
->retval
;
1154 if ((error
!= 0) && (sel_pass
== SEL_FIRSTPASS
))
1156 if (seldata
->count
== 0)
1160 sel_pass
= SEL_FIRSTPASS
; /* Reset for seldrop */
1165 OSBitOrAtomic(P_SELECT
, &p
->p_flag
);
1166 /* skip scans if the select is just for timeouts */
1167 if (seldata
->count
) {
1169 * Clear out any dangling refs from prior calls; technically
1170 * there should not be any.
1172 if (sel_pass
== SEL_FIRSTPASS
)
1173 wait_queue_sub_clearrefs(uth
->uu_wqset
);
1175 error
= selscan(p
, sel
, seldata
, uap
->nd
, retval
, sel_pass
, (wait_queue_sub_t
)uth
->uu_wqset
);
1176 if (error
|| *retval
) {
1180 /* if the select of log, then we canwakeup and discover some one
1181 * else already read the data; go toselct again if time permits
1195 clock_get_uptime(&now
);
1196 if (now
>= seldata
->abstime
)
1201 /* cleanup obits and try again */
1203 sel_pass
= SEL_FIRSTPASS
;
1208 * To effect a poll, the timeout argument should be
1209 * non-nil, pointing to a zero-valued timeval structure.
1211 if (uap
->tv
&& seldata
->abstime
== 0) {
1215 /* No spurious wakeups due to colls,no need to check for them */
1216 if ((sel_pass
== SEL_SECONDPASS
) || ((p
->p_flag
& P_SELECT
) == 0)) {
1217 sel_pass
= SEL_FIRSTPASS
;
1221 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1223 /* if the select is just for timeout skip check */
1224 if (seldata
->count
&&(sel_pass
== SEL_SECONDPASS
))
1225 panic("selprocess: 2nd pass assertwaiting");
1227 /* Wait Queue Subordinate has waitqueue as first element */
1228 wait_result
= wait_queue_assert_wait_with_leeway((wait_queue_t
)uth
->uu_wqset
,
1229 NULL
, THREAD_ABORTSAFE
,
1230 TIMEOUT_URGENCY_USER_NORMAL
, seldata
->abstime
, 0);
1231 if (wait_result
!= THREAD_AWAKENED
) {
1232 /* there are no preposted events */
1233 error
= tsleep1(NULL
, PSOCK
| PCATCH
,
1234 "select", 0, selcontinue
);
1241 sel_pass
= SEL_SECONDPASS
;
1248 wait_subqueue_unlink_all(uth
->uu_wqset
);
1249 seldrop(p
, sel
->ibits
, uap
->nd
);
1251 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1252 /* select is not restarted after signals... */
1253 if (error
== ERESTART
)
1255 if (error
== EWOULDBLOCK
)
1257 nw
= howmany(uap
->nd
, NFDBITS
);
1258 ni
= nw
* sizeof(fd_mask
);
1260 #define putbits(name, x) \
1262 if (uap->name && (error2 = \
1263 copyout((caddr_t)&sel->obits[(x) * nw], uap->name, ni))) \
1282 * Parameters: p Process performing the select
1283 * sel The per-thread select context structure
1284 * nfd The number of file descriptors to scan
1285 * retval The per thread system call return area
1286 * sel_pass Which pass this is; allowed values are
1287 * SEL_FIRSTPASS and SEL_SECONDPASS
1288 * wqsub The per thread wait queue set
1290 * Returns: 0 Success
1291 * EIO Invalid p->p_fd field XXX Obsolete?
1292 * EBADF One of the files in the bit vector is
1296 selscan(struct proc
*p
, struct _select
*sel
, struct _select_data
* seldata
, int nfd
, int32_t *retval
,
1297 int sel_pass
, wait_queue_sub_t wqsub
)
1299 struct filedesc
*fdp
= p
->p_fd
;
1302 struct fileproc
*fp
;
1303 int n
= 0; /* count of bits */
1304 int nc
= 0; /* bit vector offset (nc'th bit) */
1305 static int flag
[3] = { FREAD
, FWRITE
, 0 };
1306 u_int32_t
*iptr
, *optr
;
1308 u_int32_t
*ibits
, *obits
;
1312 struct vfs_context context
= *vfs_context_current();
1315 * Problems when reboot; due to MacOSX signal probs
1316 * in Beaker1C ; verify that the p->p_fd is valid
1326 nw
= howmany(nfd
, NFDBITS
);
1328 count
= seldata
->count
;
1333 for (msk
= 0; msk
< 3; msk
++) {
1334 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1335 optr
= (u_int32_t
*)&obits
[msk
* nw
];
1337 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1338 bits
= iptr
[i
/NFDBITS
];
1340 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1343 if (fd
< fdp
->fd_nfiles
)
1344 fp
= fdp
->fd_ofiles
[fd
];
1348 if (fp
== NULL
|| (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1350 * If we abort because of a bad
1351 * fd, let the caller unwind...
1356 if (sel_pass
== SEL_SECONDPASS
) {
1357 wql_ptr
= (char *)0;
1358 if ((fp
->f_flags
& FP_INSELECT
) && (fp
->f_waddr
== (void *)wqsub
)) {
1359 fp
->f_flags
&= ~FP_INSELECT
;
1360 fp
->f_waddr
= (void *)0;
1363 wql_ptr
= (wql
+ nc
* SIZEOF_WAITQUEUE_LINK
);
1364 if (fp
->f_flags
& FP_INSELECT
) {
1365 /* someone is already in select on this fp */
1366 fp
->f_flags
|= FP_SELCONFLICT
;
1367 wait_queue_link(&select_conflict_queue
, (wait_queue_set_t
)wqsub
);
1369 fp
->f_flags
|= FP_INSELECT
;
1370 fp
->f_waddr
= (void *)wqsub
;
1374 context
.vc_ucred
= fp
->f_cred
;
1376 /* The select; set the bit, if true */
1377 if (fp
->f_ops
&& fp
->f_type
1378 && fo_select(fp
, flag
[msk
], wql_ptr
, &context
)) {
1379 optr
[fd
/NFDBITS
] |= (1 << (fd
% NFDBITS
));
1392 int poll_callback(struct kqueue
*, struct kevent64_s
*, void *);
1394 struct poll_continue_args
{
1395 user_addr_t pca_fds
;
1401 poll(struct proc
*p
, struct poll_args
*uap
, int32_t *retval
)
1403 __pthread_testcancel(1);
1404 return(poll_nocancel(p
, (struct poll_nocancel_args
*)uap
, retval
));
1409 poll_nocancel(struct proc
*p
, struct poll_nocancel_args
*uap
, int32_t *retval
)
1411 struct poll_continue_args
*cont
;
1415 int ncoll
, error
= 0;
1416 u_int nfds
= uap
->nfds
;
1422 * This is kinda bogus. We have fd limits, but that is not
1423 * really related to the size of the pollfd array. Make sure
1424 * we let the process use at least FD_SETSIZE entries and at
1425 * least enough for the current limits. We want to be reasonably
1426 * safe, but not overly restrictive.
1428 if (nfds
> OPEN_MAX
||
1429 (nfds
> p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
&& (proc_suser(p
) || nfds
> FD_SETSIZE
)))
1432 kq
= kqueue_alloc(p
);
1436 ni
= nfds
* sizeof(struct pollfd
) + sizeof(struct poll_continue_args
);
1437 MALLOC(cont
, struct poll_continue_args
*, ni
, M_TEMP
, M_WAITOK
);
1443 fds
= (struct pollfd
*)&cont
[1];
1444 error
= copyin(uap
->fds
, fds
, nfds
* sizeof(struct pollfd
));
1448 if (uap
->timeout
!= -1) {
1451 atv
.tv_sec
= uap
->timeout
/ 1000;
1452 atv
.tv_usec
= (uap
->timeout
% 1000) * 1000;
1453 if (itimerfix(&atv
)) {
1457 getmicrouptime(&rtv
);
1458 timevaladd(&atv
, &rtv
);
1464 /* JMM - all this P_SELECT stuff is bogus */
1466 OSBitOrAtomic(P_SELECT
, &p
->p_flag
);
1467 for (i
= 0; i
< nfds
; i
++) {
1468 short events
= fds
[i
].events
;
1469 struct kevent64_s kev
;
1472 /* per spec, ignore fd values below zero */
1473 if (fds
[i
].fd
< 0) {
1478 /* convert the poll event into a kqueue kevent */
1479 kev
.ident
= fds
[i
].fd
;
1480 kev
.flags
= EV_ADD
| EV_ONESHOT
| EV_POLL
;
1481 kev
.udata
= CAST_USER_ADDR_T(&fds
[i
]);
1487 /* Handle input events */
1488 if (events
& ( POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
| POLLHUP
)) {
1489 kev
.filter
= EVFILT_READ
;
1490 if (events
& ( POLLPRI
| POLLRDBAND
))
1491 kev
.flags
|= EV_OOBAND
;
1492 kerror
= kevent_register(kq
, &kev
, p
);
1495 /* Handle output events */
1497 events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
)) {
1498 kev
.filter
= EVFILT_WRITE
;
1499 kerror
= kevent_register(kq
, &kev
, p
);
1502 /* Handle BSD extension vnode events */
1504 events
& ( POLLEXTEND
| POLLATTRIB
| POLLNLINK
| POLLWRITE
)) {
1505 kev
.filter
= EVFILT_VNODE
;
1507 if (events
& POLLEXTEND
)
1508 kev
.fflags
|= NOTE_EXTEND
;
1509 if (events
& POLLATTRIB
)
1510 kev
.fflags
|= NOTE_ATTRIB
;
1511 if (events
& POLLNLINK
)
1512 kev
.fflags
|= NOTE_LINK
;
1513 if (events
& POLLWRITE
)
1514 kev
.fflags
|= NOTE_WRITE
;
1515 kerror
= kevent_register(kq
, &kev
, p
);
1519 fds
[i
].revents
= POLLNVAL
;
1525 /* Did we have any trouble registering? */
1529 /* scan for, and possibly wait for, the kevents to trigger */
1530 cont
->pca_fds
= uap
->fds
;
1531 cont
->pca_nfds
= nfds
;
1532 cont
->pca_rfds
= rfds
;
1533 error
= kqueue_scan(kq
, poll_callback
, NULL
, cont
, &atv
, p
);
1534 rfds
= cont
->pca_rfds
;
1537 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1538 /* poll is not restarted after signals... */
1539 if (error
== ERESTART
)
1541 if (error
== EWOULDBLOCK
)
1544 error
= copyout(fds
, uap
->fds
, nfds
* sizeof(struct pollfd
));
1557 poll_callback(__unused
struct kqueue
*kq
, struct kevent64_s
*kevp
, void *data
)
1559 struct poll_continue_args
*cont
= (struct poll_continue_args
*)data
;
1560 struct pollfd
*fds
= CAST_DOWN(struct pollfd
*, kevp
->udata
);
1561 short prev_revents
= fds
->revents
;
1564 /* convert the results back into revents */
1565 if (kevp
->flags
& EV_EOF
)
1566 fds
->revents
|= POLLHUP
;
1567 if (kevp
->flags
& EV_ERROR
)
1568 fds
->revents
|= POLLERR
;
1570 switch (kevp
->filter
) {
1572 if (fds
->revents
& POLLHUP
)
1573 mask
= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
);
1575 if ((kevp
->flags
& EV_ERROR
) == 0 && kevp
->data
!= 0)
1576 mask
= (POLLIN
| POLLRDNORM
);
1577 if (kevp
->flags
& EV_OOBAND
)
1578 mask
|= ( POLLPRI
| POLLRDBAND
);
1580 fds
->revents
|= (fds
->events
& mask
);
1584 if (!(fds
->revents
& POLLHUP
))
1585 fds
->revents
|= (fds
->events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
));
1589 if (kevp
->fflags
& NOTE_EXTEND
)
1590 fds
->revents
|= (fds
->events
& POLLEXTEND
);
1591 if (kevp
->fflags
& NOTE_ATTRIB
)
1592 fds
->revents
|= (fds
->events
& POLLATTRIB
);
1593 if (kevp
->fflags
& NOTE_LINK
)
1594 fds
->revents
|= (fds
->events
& POLLNLINK
);
1595 if (kevp
->fflags
& NOTE_WRITE
)
1596 fds
->revents
|= (fds
->events
& POLLWRITE
);
1600 if (fds
->revents
!= 0 && prev_revents
== 0)
1607 seltrue(__unused dev_t dev
, __unused
int flag
, __unused
struct proc
*p
)
1616 * Count the number of bits set in the input bit vector, and establish an
1617 * outstanding fp->f_iocount for each of the descriptors which will be in
1618 * use in the select operation.
1620 * Parameters: p The process doing the select
1621 * ibits The input bit vector
1622 * nfd The number of fd's in the vector
1623 * countp Pointer to where to store the bit count
1625 * Returns: 0 Success
1626 * EIO Bad per process open file table
1627 * EBADF One of the bits in the input bit vector
1628 * references an invalid fd
1630 * Implicit: *countp (modified) Count of fd's
1632 * Notes: This function is the first pass under the proc_fdlock() that
1633 * permits us to recognize invalid descriptors in the bit vector;
1634 * the may, however, not remain valid through the drop and
1635 * later reacquisition of the proc_fdlock().
1638 selcount(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int *countp
)
1640 struct filedesc
*fdp
= p
->p_fd
;
1643 struct fileproc
*fp
;
1649 int need_wakeup
= 0;
1652 * Problems when reboot; due to MacOSX signal probs
1653 * in Beaker1C ; verify that the p->p_fd is valid
1659 nw
= howmany(nfd
, NFDBITS
);
1662 for (msk
= 0; msk
< 3; msk
++) {
1663 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1664 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1665 bits
= iptr
[i
/NFDBITS
];
1666 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1669 if (fd
< fdp
->fd_nfiles
)
1670 fp
= fdp
->fd_ofiles
[fd
];
1675 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1695 /* Ignore error return; it's already EBADF */
1696 (void)seldrop_locked(p
, ibits
, nfd
, n
, &need_wakeup
, 1);
1701 wakeup(&p
->p_fpdrainwait
);
1710 * Drop outstanding wait queue references set up during selscan(); drop the
1711 * outstanding per fileproc f_iocount() picked up during the selcount().
1713 * Parameters: p Process performing the select
1714 * ibits Input pit bector of fd's
1715 * nfd Number of fd's
1716 * lim Limit to number of vector entries to
1717 * consider, or -1 for "all"
1719 * need_wakeup Pointer to flag to set to do a wakeup
1720 * if f_iocont on any descriptor goes to 0
1722 * Returns: 0 Success
1723 * EBADF One or more fds in the bit vector
1724 * were invalid, but the rest
1725 * were successfully dropped
1727 * Notes: An fd make become bad while the proc_fdlock() is not held,
1728 * if a multithreaded application closes the fd out from under
1729 * the in progress select. In this case, we still have to
1730 * clean up after the set up on the remaining fds.
1733 seldrop_locked(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int lim
, int *need_wakeup
, int fromselcount
)
1735 struct filedesc
*fdp
= p
->p_fd
;
1738 struct fileproc
*fp
;
1743 uthread_t uth
= get_bsdthread_info(current_thread());
1748 * Problems when reboot; due to MacOSX signal probs
1749 * in Beaker1C ; verify that the p->p_fd is valid
1755 nw
= howmany(nfd
, NFDBITS
);
1757 for (msk
= 0; msk
< 3; msk
++) {
1758 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1759 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1760 bits
= iptr
[i
/NFDBITS
];
1761 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1763 fp
= fdp
->fd_ofiles
[fd
];
1765 * If we've already dropped as many as were
1766 * counted/scanned, then we are done.
1768 if ((fromselcount
!= 0) && (++dropcount
> lim
))
1772 /* skip (now) bad fds */
1777 * Only clear the flag if we set it. We'll
1778 * only find that we set it if we had made
1779 * at least one [partial] pass through selscan().
1781 if ((fp
->f_flags
& FP_INSELECT
) && (fp
->f_waddr
== (void *)uth
->uu_wqset
)) {
1782 fp
->f_flags
&= ~FP_INSELECT
;
1783 fp
->f_waddr
= (void *)0;
1787 if (fp
->f_iocount
< 0)
1788 panic("f_iocount overdecrement!");
1790 if (fp
->f_iocount
== 0) {
1792 * The last iocount is responsible for clearing
1793 * selconfict flag - even if we didn't set it -
1794 * and is also responsible for waking up anyone
1795 * waiting on iocounts to drain.
1797 if (fp
->f_flags
& FP_SELCONFLICT
)
1798 fp
->f_flags
&= ~FP_SELCONFLICT
;
1799 if (p
->p_fpdrainwait
) {
1800 p
->p_fpdrainwait
= 0;
1813 seldrop(struct proc
*p
, u_int32_t
*ibits
, int nfd
)
1816 int need_wakeup
= 0;
1819 error
= seldrop_locked(p
, ibits
, nfd
, nfd
, &need_wakeup
, 0);
1822 wakeup(&p
->p_fpdrainwait
);
1828 * Record a select request.
1831 selrecord(__unused
struct proc
*selector
, struct selinfo
*sip
, void * p_wql
)
1833 thread_t cur_act
= current_thread();
1834 struct uthread
* ut
= get_bsdthread_info(cur_act
);
1836 /* need to look at collisions */
1838 /*do not record if this is second pass of select */
1839 if(p_wql
== (void *)0) {
1843 if ((sip
->si_flags
& SI_INITED
) == 0) {
1844 wait_queue_init(&sip
->si_wait_queue
, SYNC_POLICY_FIFO
);
1845 sip
->si_flags
|= SI_INITED
;
1846 sip
->si_flags
&= ~SI_CLEAR
;
1849 if (sip
->si_flags
& SI_RECORDED
) {
1850 sip
->si_flags
|= SI_COLL
;
1852 sip
->si_flags
&= ~SI_COLL
;
1854 sip
->si_flags
|= SI_RECORDED
;
1855 if (!wait_queue_member(&sip
->si_wait_queue
, ut
->uu_wqset
))
1856 wait_queue_link_noalloc(&sip
->si_wait_queue
, ut
->uu_wqset
,
1857 (wait_queue_link_t
)p_wql
);
1863 selwakeup(struct selinfo
*sip
)
1866 if ((sip
->si_flags
& SI_INITED
) == 0) {
1870 if (sip
->si_flags
& SI_COLL
) {
1872 sip
->si_flags
&= ~SI_COLL
;
1874 /* will not support */
1875 //wakeup((caddr_t)&selwait);
1879 if (sip
->si_flags
& SI_RECORDED
) {
1880 wait_queue_wakeup_all(&sip
->si_wait_queue
, NULL
, THREAD_AWAKENED
);
1881 sip
->si_flags
&= ~SI_RECORDED
;
1887 selthreadclear(struct selinfo
*sip
)
1890 if ((sip
->si_flags
& SI_INITED
) == 0) {
1893 if (sip
->si_flags
& SI_RECORDED
) {
1895 sip
->si_flags
&= ~(SI_RECORDED
| SI_COLL
);
1897 sip
->si_flags
|= SI_CLEAR
;
1898 wait_queue_unlink_all(&sip
->si_wait_queue
);
1904 #define DBG_POST 0x10
1905 #define DBG_WATCH 0x11
1906 #define DBG_WAIT 0x12
1907 #define DBG_MOD 0x13
1908 #define DBG_EWAKEUP 0x14
1909 #define DBG_ENQUEUE 0x15
1910 #define DBG_DEQUEUE 0x16
1912 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
1913 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
1914 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
1915 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
1916 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
1917 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
1918 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
1921 #define EVPROCDEQUE(p, evq) do { \
1923 if (evq->ee_flags & EV_QUEUED) { \
1924 TAILQ_REMOVE(&p->p_evlist, evq, ee_plist); \
1925 evq->ee_flags &= ~EV_QUEUED; \
1932 * called upon socket close. deque and free all events for
1933 * the socket... socket must be locked by caller.
1936 evsofree(struct socket
*sp
)
1938 struct eventqelt
*evq
, *next
;
1944 for (evq
= sp
->so_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
1945 next
= evq
->ee_slist
.tqe_next
;
1948 if (evq
->ee_flags
& EV_QUEUED
) {
1949 EVPROCDEQUE(p
, evq
);
1951 TAILQ_REMOVE(&sp
->so_evlist
, evq
, ee_slist
); // remove from socket q
1958 * called upon pipe close. deque and free all events for
1959 * the pipe... pipe must be locked by caller
1962 evpipefree(struct pipe
*cpipe
)
1964 struct eventqelt
*evq
, *next
;
1967 for (evq
= cpipe
->pipe_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
1968 next
= evq
->ee_slist
.tqe_next
;
1971 EVPROCDEQUE(p
, evq
);
1973 TAILQ_REMOVE(&cpipe
->pipe_evlist
, evq
, ee_slist
); // remove from pipe q
1980 * enqueue this event if it's not already queued. wakeup
1981 * the proc if we do queue this event to it...
1982 * entered with proc lock held... we drop it before
1983 * doing the wakeup and return in that state
1986 evprocenque(struct eventqelt
*evq
)
1993 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_START
, (uint32_t)evq
, evq
->ee_flags
, evq
->ee_eventmask
,0,0);
1997 if (evq
->ee_flags
& EV_QUEUED
) {
2000 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
2003 evq
->ee_flags
|= EV_QUEUED
;
2005 TAILQ_INSERT_TAIL(&p
->p_evlist
, evq
, ee_plist
);
2009 wakeup(&p
->p_evlist
);
2011 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
2016 * pipe lock must be taken by the caller
2019 postpipeevent(struct pipe
*pipep
, int event
)
2022 struct eventqelt
*evq
;
2026 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, event
,0,0,1,0);
2028 for (evq
= pipep
->pipe_evlist
.tqh_first
;
2029 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2031 if (evq
->ee_eventmask
== 0)
2035 switch (event
& (EV_RWBYTES
| EV_RCLOSED
| EV_WCLOSED
)) {
2038 if ((evq
->ee_eventmask
& EV_RE
) && pipep
->pipe_buffer
.cnt
) {
2040 evq
->ee_req
.er_rcnt
= pipep
->pipe_buffer
.cnt
;
2042 if ((evq
->ee_eventmask
& EV_WR
) &&
2043 (MAX(pipep
->pipe_buffer
.size
,PIPE_SIZE
) - pipep
->pipe_buffer
.cnt
) >= PIPE_BUF
) {
2045 if (pipep
->pipe_state
& PIPE_EOF
) {
2046 mask
|= EV_WR
|EV_RESET
;
2050 evq
->ee_req
.er_wcnt
= MAX(pipep
->pipe_buffer
.size
, PIPE_SIZE
) - pipep
->pipe_buffer
.cnt
;
2056 if ((evq
->ee_eventmask
& EV_RE
)) {
2057 mask
|= EV_RE
|EV_RCLOSED
;
2059 if ((evq
->ee_eventmask
& EV_WR
)) {
2060 mask
|= EV_WR
|EV_WCLOSED
;
2069 * disarm... postevents are nops until this event is 'read' via
2070 * waitevent and then re-armed via modwatch
2072 evq
->ee_eventmask
= 0;
2075 * since events are disarmed until after the waitevent
2076 * the ee_req.er_xxxx fields can't change once we've
2077 * inserted this event into the proc queue...
2078 * therefore, the waitevent will see a 'consistent'
2079 * snapshot of the event, even though it won't hold
2080 * the pipe lock, and we're updating the event outside
2081 * of the proc lock, which it will hold
2083 evq
->ee_req
.er_eventbits
|= mask
;
2085 KERNEL_DEBUG(DBG_MISC_POST
, (uint32_t)evq
, evq
->ee_req
.er_eventbits
, mask
, 1,0);
2090 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, 0,0,0,1,0);
2095 * given either a sockbuf or a socket run down the
2096 * event list and queue ready events found...
2097 * the socket must be locked by the caller
2100 postevent(struct socket
*sp
, struct sockbuf
*sb
, int event
)
2103 struct eventqelt
*evq
;
2111 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, (int)sp
, event
, 0, 0, 0);
2113 for (evq
= sp
->so_evlist
.tqh_first
;
2114 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2116 if (evq
->ee_eventmask
== 0)
2120 /* ready for reading:
2121 - byte cnt >= receive low water mark
2122 - read-half of conn closed
2123 - conn pending for listening sock
2124 - socket error pending
2127 - byte cnt avail >= send low water mark
2128 - write half of conn closed
2129 - socket error pending
2130 - non-blocking conn completed successfully
2134 - sock at out of band mark
2137 switch (event
& EV_DMASK
) {
2140 if ((evq
->ee_eventmask
& EV_EX
)) {
2141 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
2142 mask
|= EV_EX
|EV_OOB
;
2146 case EV_RWBYTES
|EV_OOB
:
2147 if ((evq
->ee_eventmask
& EV_EX
)) {
2148 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
2149 mask
|= EV_EX
|EV_OOB
;
2152 * fall into the next case
2155 if ((evq
->ee_eventmask
& EV_RE
) && soreadable(sp
)) {
2156 /* for AFP/OT purposes; may go away in future */
2157 if ((SOCK_DOM(sp
) == PF_INET
||
2158 SOCK_DOM(sp
) == PF_INET6
) &&
2159 SOCK_PROTO(sp
) == IPPROTO_TCP
&&
2160 (sp
->so_error
== ECONNREFUSED
||
2161 sp
->so_error
== ECONNRESET
)) {
2162 if (sp
->so_pcb
== NULL
||
2163 sotoinpcb(sp
)->inp_state
==
2165 (tp
= sototcpcb(sp
)) == NULL
||
2166 tp
->t_state
== TCPS_CLOSED
) {
2167 mask
|= EV_RE
|EV_RESET
;
2172 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
2174 if (sp
->so_state
& SS_CANTRCVMORE
) {
2179 if ((evq
->ee_eventmask
& EV_WR
) && sowriteable(sp
)) {
2180 /* for AFP/OT purposes; may go away in future */
2181 if ((SOCK_DOM(sp
) == PF_INET
||
2182 SOCK_DOM(sp
) == PF_INET6
) &&
2183 SOCK_PROTO(sp
) == IPPROTO_TCP
&&
2184 (sp
->so_error
== ECONNREFUSED
||
2185 sp
->so_error
== ECONNRESET
)) {
2186 if (sp
->so_pcb
== NULL
||
2187 sotoinpcb(sp
)->inp_state
==
2189 (tp
= sototcpcb(sp
)) == NULL
||
2190 tp
->t_state
== TCPS_CLOSED
) {
2191 mask
|= EV_WR
|EV_RESET
;
2196 evq
->ee_req
.er_wcnt
= sbspace(&sp
->so_snd
);
2201 if ((evq
->ee_eventmask
& EV_RE
)) {
2202 mask
|= EV_RE
|EV_RCONN
;
2203 evq
->ee_req
.er_rcnt
= sp
->so_qlen
+ 1; // incl this one
2208 if ((evq
->ee_eventmask
& EV_WR
)) {
2209 mask
|= EV_WR
|EV_WCONN
;
2214 if ((evq
->ee_eventmask
& EV_RE
)) {
2215 mask
|= EV_RE
|EV_RCLOSED
;
2220 if ((evq
->ee_eventmask
& EV_WR
)) {
2221 mask
|= EV_WR
|EV_WCLOSED
;
2226 if (evq
->ee_eventmask
& EV_RE
) {
2227 mask
|= EV_RE
|EV_FIN
;
2233 if (evq
->ee_eventmask
& EV_RE
) {
2234 mask
|= EV_RE
| event
;
2236 if (evq
->ee_eventmask
& EV_WR
) {
2237 mask
|= EV_WR
| event
;
2242 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, -1, 0, 0, 0);
2246 KERNEL_DEBUG(DBG_MISC_POST
, (int)evq
, evq
->ee_eventmask
, evq
->ee_req
.er_eventbits
, mask
, 0);
2250 * disarm... postevents are nops until this event is 'read' via
2251 * waitevent and then re-armed via modwatch
2253 evq
->ee_eventmask
= 0;
2256 * since events are disarmed until after the waitevent
2257 * the ee_req.er_xxxx fields can't change once we've
2258 * inserted this event into the proc queue...
2259 * since waitevent can't see this event until we
2260 * enqueue it, waitevent will see a 'consistent'
2261 * snapshot of the event, even though it won't hold
2262 * the socket lock, and we're updating the event outside
2263 * of the proc lock, which it will hold
2265 evq
->ee_req
.er_eventbits
|= mask
;
2270 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, 0, 0, 0, 0);
2272 #endif /* SOCKETS */
2276 * watchevent system call. user passes us an event to watch
2277 * for. we malloc an event object, initialize it, and queue
2278 * it to the open socket. when the event occurs, postevent()
2279 * will enque it back to our proc where we can retrieve it
2282 * should this prevent duplicate events on same socket?
2285 * ENOMEM No memory for operation
2289 watchevent(proc_t p
, struct watchevent_args
*uap
, __unused
int *retval
)
2291 struct eventqelt
*evq
= (struct eventqelt
*)0;
2292 struct eventqelt
*np
= NULL
;
2293 struct eventreq64
*erp
;
2294 struct fileproc
*fp
= NULL
;
2297 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_START
, 0,0,0,0,0);
2299 // get a qelt and fill with users req
2300 MALLOC(evq
, struct eventqelt
*, sizeof(struct eventqelt
), M_TEMP
, M_WAITOK
);
2306 // get users request pkt
2308 if (IS_64BIT_PROCESS(p
)) {
2309 error
= copyin(uap
->u_req
, (caddr_t
)erp
, sizeof(struct eventreq64
));
2311 struct eventreq32 er32
;
2313 error
= copyin(uap
->u_req
, (caddr_t
)&er32
, sizeof(struct eventreq32
));
2316 * the user only passes in the
2317 * er_type, er_handle and er_data...
2318 * the other fields are initialized
2319 * below, so don't bother to copy
2321 erp
->er_type
= er32
.er_type
;
2322 erp
->er_handle
= er32
.er_handle
;
2323 erp
->er_data
= (user_addr_t
)er32
.er_data
;
2328 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2332 KERNEL_DEBUG(DBG_MISC_WATCH
, erp
->er_handle
,uap
->u_eventmask
,(uint32_t)evq
,0,0);
2334 // validate, freeing qelt if errors
2338 if (erp
->er_type
!= EV_FD
) {
2340 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2343 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2344 socket_lock((struct socket
*)fp
->f_data
, 1);
2345 np
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2346 #endif /* SOCKETS */
2347 } else if (fp
->f_type
== DTYPE_PIPE
) {
2348 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2349 np
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2351 fp_drop(p
, erp
->er_handle
, fp
, 1);
2359 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2364 * only allow one watch per file per proc
2366 for ( ; np
!= NULL
; np
= np
->ee_slist
.tqe_next
) {
2367 if (np
->ee_proc
== p
) {
2369 if (fp
->f_type
== DTYPE_SOCKET
)
2370 socket_unlock((struct socket
*)fp
->f_data
, 1);
2372 #endif /* SOCKETS */
2373 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2374 fp_drop(p
, erp
->er_handle
, fp
, 0);
2377 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2381 erp
->er_ecnt
= erp
->er_rcnt
= erp
->er_wcnt
= erp
->er_eventbits
= 0;
2383 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2387 if (fp
->f_type
== DTYPE_SOCKET
) {
2388 TAILQ_INSERT_TAIL(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2389 postevent((struct socket
*)fp
->f_data
, 0, EV_RWBYTES
); // catch existing events
2391 socket_unlock((struct socket
*)fp
->f_data
, 1);
2393 #endif /* SOCKETS */
2395 TAILQ_INSERT_TAIL(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2396 postpipeevent((struct pipe
*)fp
->f_data
, EV_RWBYTES
);
2398 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2400 fp_drop_event(p
, erp
->er_handle
, fp
);
2402 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, 0,0,0,0,0);
2409 * waitevent system call.
2410 * grabs the next waiting event for this proc and returns
2411 * it. if no events, user can request to sleep with timeout
2412 * or without or poll mode
2413 * ((tv != NULL && interval == 0) || tv == -1)
2416 waitevent(proc_t p
, struct waitevent_args
*uap
, int *retval
)
2419 struct eventqelt
*evq
;
2420 struct eventreq64
*erp
;
2421 uint64_t abstime
, interval
;
2422 boolean_t fast_poll
= FALSE
;
2424 struct eventreq64 er64
;
2425 struct eventreq32 er32
;
2433 * check for fast poll method
2435 if (IS_64BIT_PROCESS(p
)) {
2436 if (uap
->tv
== (user_addr_t
)-1)
2438 } else if (uap
->tv
== (user_addr_t
)((uint32_t)-1))
2441 if (fast_poll
== TRUE
) {
2442 if (p
->p_evlist
.tqh_first
== NULL
) {
2443 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_NONE
, -1,0,0,0,0);
2453 if (IS_64BIT_PROCESS(p
)) {
2454 struct user64_timeval atv64
;
2455 error
= copyin(uap
->tv
, (caddr_t
)&atv64
, sizeof(atv64
));
2456 /* Loses resolution - assume timeout < 68 years */
2457 atv
.tv_sec
= atv64
.tv_sec
;
2458 atv
.tv_usec
= atv64
.tv_usec
;
2460 struct user32_timeval atv32
;
2461 error
= copyin(uap
->tv
, (caddr_t
)&atv32
, sizeof(atv32
));
2462 atv
.tv_sec
= atv32
.tv_sec
;
2463 atv
.tv_usec
= atv32
.tv_usec
;
2468 if (itimerfix(&atv
)) {
2472 interval
= tvtoabstime(&atv
);
2474 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_START
, 0,0,0,0,0);
2478 if ((evq
= p
->p_evlist
.tqh_first
) != NULL
) {
2480 * found one... make a local copy while it's still on the queue
2481 * to prevent it from changing while in the midst of copying
2482 * don't want to hold the proc lock across a copyout because
2483 * it might block on a page fault at the target in user space
2487 if (IS_64BIT_PROCESS(p
))
2488 bcopy((caddr_t
)erp
, (caddr_t
)&uer
.er64
, sizeof (struct eventreq64
));
2490 uer
.er32
.er_type
= erp
->er_type
;
2491 uer
.er32
.er_handle
= erp
->er_handle
;
2492 uer
.er32
.er_data
= (uint32_t)erp
->er_data
;
2493 uer
.er32
.er_ecnt
= erp
->er_ecnt
;
2494 uer
.er32
.er_rcnt
= erp
->er_rcnt
;
2495 uer
.er32
.er_wcnt
= erp
->er_wcnt
;
2496 uer
.er32
.er_eventbits
= erp
->er_eventbits
;
2498 TAILQ_REMOVE(&p
->p_evlist
, evq
, ee_plist
);
2500 evq
->ee_flags
&= ~EV_QUEUED
;
2504 if (IS_64BIT_PROCESS(p
))
2505 error
= copyout((caddr_t
)&uer
.er64
, uap
->u_req
, sizeof(struct eventreq64
));
2507 error
= copyout((caddr_t
)&uer
.er32
, uap
->u_req
, sizeof(struct eventreq32
));
2509 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,
2510 evq
->ee_req
.er_handle
,evq
->ee_req
.er_eventbits
,(uint32_t)evq
,0);
2514 if (uap
->tv
&& interval
== 0) {
2516 *retval
= 1; // poll failed
2518 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,0,0,0,0);
2522 clock_absolutetime_interval_to_deadline(interval
, &abstime
);
2526 KERNEL_DEBUG(DBG_MISC_WAIT
, 1,(uint32_t)&p
->p_evlist
,0,0,0);
2528 error
= msleep1(&p
->p_evlist
, &p
->p_mlock
, (PSOCK
| PCATCH
), "waitevent", abstime
);
2530 KERNEL_DEBUG(DBG_MISC_WAIT
, 2,(uint32_t)&p
->p_evlist
,0,0,0);
2534 if (error
== ERESTART
)
2536 if (error
== EWOULDBLOCK
) {
2543 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, 0,0,0,0,0);
2549 * modwatch system call. user passes in event to modify.
2550 * if we find it we reset the event bits and que/deque event
2554 modwatch(proc_t p
, struct modwatch_args
*uap
, __unused
int *retval
)
2556 struct eventreq64 er
;
2557 struct eventreq64
*erp
= &er
;
2558 struct eventqelt
*evq
= NULL
; /* protected by error return */
2560 struct fileproc
*fp
;
2563 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_START
, 0,0,0,0,0);
2566 * get user's request pkt
2567 * just need the er_type and er_handle which sit above the
2568 * problematic er_data (32/64 issue)... so only copy in
2571 if ((error
= copyin(uap
->u_req
, (caddr_t
)erp
, sizeof(er
.er_type
) + sizeof(er
.er_handle
)))) {
2572 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2577 if (erp
->er_type
!= EV_FD
) {
2579 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2582 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2583 socket_lock((struct socket
*)fp
->f_data
, 1);
2584 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2585 #endif /* SOCKETS */
2586 } else if (fp
->f_type
== DTYPE_PIPE
) {
2587 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2588 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2590 fp_drop(p
, erp
->er_handle
, fp
, 1);
2596 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2600 if ((uap
->u_eventmask
== EV_RM
) && (fp
->f_flags
& FP_WAITEVENT
)) {
2601 fp
->f_flags
&= ~FP_WAITEVENT
;
2605 // locate event if possible
2606 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2607 if (evq
->ee_proc
== p
)
2612 if (fp
->f_type
== DTYPE_SOCKET
)
2613 socket_unlock((struct socket
*)fp
->f_data
, 1);
2615 #endif /* SOCKETS */
2616 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2617 fp_drop(p
, erp
->er_handle
, fp
, 0);
2618 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2621 KERNEL_DEBUG(DBG_MISC_MOD
, erp
->er_handle
,uap
->u_eventmask
,(uint32_t)evq
,0,0);
2623 if (uap
->u_eventmask
== EV_RM
) {
2624 EVPROCDEQUE(p
, evq
);
2627 if (fp
->f_type
== DTYPE_SOCKET
) {
2628 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2629 socket_unlock((struct socket
*)fp
->f_data
, 1);
2631 #endif /* SOCKETS */
2633 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2634 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2636 fp_drop(p
, erp
->er_handle
, fp
, 0);
2638 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, 0,0,0,0,0);
2641 switch (uap
->u_eventmask
& EV_MASK
) {
2659 case EV_EX
|EV_RE
|EV_WR
:
2660 flag
= EV_OOB
|EV_RWBYTES
;
2665 if (fp
->f_type
== DTYPE_SOCKET
)
2666 socket_unlock((struct socket
*)fp
->f_data
, 1);
2668 #endif /* SOCKETS */
2669 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2670 fp_drop(p
, erp
->er_handle
, fp
, 0);
2671 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2675 * since we're holding the socket/pipe lock, the event
2676 * cannot go from the unqueued state to the queued state
2677 * however, it can go from the queued state to the unqueued state
2678 * since that direction is protected by the proc_lock...
2679 * so do a quick check for EV_QUEUED w/o holding the proc lock
2680 * since by far the common case will be NOT EV_QUEUED, this saves
2681 * us taking the proc_lock the majority of the time
2683 if (evq
->ee_flags
& EV_QUEUED
) {
2685 * EVPROCDEQUE will recheck the state after it grabs the proc_lock
2687 EVPROCDEQUE(p
, evq
);
2690 * while the event is off the proc queue and
2691 * we're holding the socket/pipe lock
2692 * it's safe to update these fields...
2694 evq
->ee_req
.er_eventbits
= 0;
2695 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2698 if (fp
->f_type
== DTYPE_SOCKET
) {
2699 postevent((struct socket
*)fp
->f_data
, 0, flag
);
2700 socket_unlock((struct socket
*)fp
->f_data
, 1);
2702 #endif /* SOCKETS */
2704 postpipeevent((struct pipe
*)fp
->f_data
, flag
);
2705 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2707 fp_drop(p
, erp
->er_handle
, fp
, 0);
2708 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, evq
->ee_req
.er_handle
,evq
->ee_eventmask
,(uint32_t)fp
->f_data
,flag
,0);
2712 /* this routine is called from the close of fd with proc_fdlock held */
2714 waitevent_close(struct proc
*p
, struct fileproc
*fp
)
2716 struct eventqelt
*evq
;
2719 fp
->f_flags
&= ~FP_WAITEVENT
;
2722 if (fp
->f_type
== DTYPE_SOCKET
) {
2723 socket_lock((struct socket
*)fp
->f_data
, 1);
2724 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2726 #endif /* SOCKETS */
2727 if (fp
->f_type
== DTYPE_PIPE
) {
2728 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2729 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2737 // locate event if possible
2738 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2739 if (evq
->ee_proc
== p
)
2744 if (fp
->f_type
== DTYPE_SOCKET
)
2745 socket_unlock((struct socket
*)fp
->f_data
, 1);
2747 #endif /* SOCKETS */
2748 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2754 EVPROCDEQUE(p
, evq
);
2757 if (fp
->f_type
== DTYPE_SOCKET
) {
2758 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2759 socket_unlock((struct socket
*)fp
->f_data
, 1);
2761 #endif /* SOCKETS */
2763 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2764 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2777 * Description: Get the host UUID from IOKit and return it to user space.
2779 * Parameters: uuid_buf Pointer to buffer to receive UUID
2780 * timeout Timespec for timout
2781 * spi SPI, skip sandbox check (temporary)
2783 * Returns: 0 Success
2784 * EWOULDBLOCK Timeout is too short
2785 * copyout:EFAULT Bad user buffer
2786 * mac_system_check_info:EPERM Client not allowed to perform this operation
2788 * Notes: A timeout seems redundant, since if it's tolerable to not
2789 * have a system UUID in hand, then why ask for one?
2792 gethostuuid(struct proc
*p
, struct gethostuuid_args
*uap
, __unused
int32_t *retval
)
2796 mach_timespec_t mach_ts
; /* for IOKit call */
2797 __darwin_uuid_t uuid_kern
; /* for IOKit call */
2802 /* Convert the 32/64 bit timespec into a mach_timespec_t */
2803 if ( proc_is64bit(p
) ) {
2804 struct user64_timespec ts
;
2805 error
= copyin(uap
->timeoutp
, &ts
, sizeof(ts
));
2808 mach_ts
.tv_sec
= ts
.tv_sec
;
2809 mach_ts
.tv_nsec
= ts
.tv_nsec
;
2811 struct user32_timespec ts
;
2812 error
= copyin(uap
->timeoutp
, &ts
, sizeof(ts
) );
2815 mach_ts
.tv_sec
= ts
.tv_sec
;
2816 mach_ts
.tv_nsec
= ts
.tv_nsec
;
2819 /* Call IOKit with the stack buffer to get the UUID */
2820 kret
= IOBSDGetPlatformUUID(uuid_kern
, mach_ts
);
2823 * If we get it, copy out the data to the user buffer; note that a
2824 * uuid_t is an array of characters, so this is size invariant for
2827 if (kret
== KERN_SUCCESS
) {
2828 error
= copyout(uuid_kern
, uap
->uuid_buf
, sizeof(uuid_kern
));
2830 error
= EWOULDBLOCK
;
2839 * Description: Omnibus system call for ledger operations
2842 ledger(struct proc
*p
, struct ledger_args
*args
, __unused
int32_t *retval
)
2847 int rval
, pid
, len
, error
;
2849 struct ledger_limit_args lla
;
2854 /* Finish copying in the necessary args before taking the proc lock */
2857 if (args
->cmd
== LEDGER_ENTRY_INFO
)
2858 error
= copyin(args
->arg3
, (char *)&len
, sizeof (len
));
2859 else if (args
->cmd
== LEDGER_TEMPLATE_INFO
)
2860 error
= copyin(args
->arg2
, (char *)&len
, sizeof (len
));
2862 else if (args
->cmd
== LEDGER_LIMIT
)
2863 error
= copyin(args
->arg2
, (char *)&lla
, sizeof (lla
));
2871 if (args
->cmd
!= LEDGER_TEMPLATE_INFO
) {
2873 proc
= proc_find(pid
);
2878 error
= mac_proc_check_ledger(p
, proc
, args
->cmd
);
2888 switch (args
->cmd
) {
2890 case LEDGER_LIMIT
: {
2891 if (!kauth_cred_issuser(kauth_cred_get()))
2893 rval
= ledger_limit(task
, &lla
);
2899 struct ledger_info info
;
2901 rval
= ledger_info(task
, &info
);
2904 rval
= copyout(&info
, args
->arg2
,
2909 case LEDGER_ENTRY_INFO
: {
2913 rval
= ledger_get_task_entry_info_multiple(task
, &buf
, &len
);
2915 if ((rval
== 0) && (len
> 0)) {
2916 sz
= len
* sizeof (struct ledger_entry_info
);
2917 rval
= copyout(buf
, args
->arg2
, sz
);
2921 rval
= copyout(&len
, args
->arg3
, sizeof (len
));
2925 case LEDGER_TEMPLATE_INFO
: {
2929 rval
= ledger_template_info(&buf
, &len
);
2930 if ((rval
== 0) && (len
> 0)) {
2931 sz
= len
* sizeof (struct ledger_template_info
);
2932 rval
= copyout(buf
, args
->arg1
, sz
);
2936 rval
= copyout(&len
, args
->arg2
, sizeof (len
));
2948 telemetry(__unused
struct proc
*p
, struct telemetry_args
*args
, __unused
int32_t *retval
)
2952 switch (args
->cmd
) {
2953 #if CONFIG_TELEMETRY
2954 case TELEMETRY_CMD_TIMER_EVENT
:
2955 error
= telemetry_timer_event(args
->deadline
, args
->interval
, args
->leeway
);
2957 #endif /* CONFIG_TELEMETRY */
2958 case TELEMETRY_CMD_VOUCHER_NAME
:
2959 if (thread_set_voucher_name((mach_port_name_t
)args
->deadline
))