2 * Copyright (c) 2000-2013 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>
85 #include <sys/malloc.h>
86 #include <sys/sysproto.h>
88 #include <sys/mount_internal.h>
89 #include <sys/protosw.h>
92 #include <sys/kdebug.h>
94 #include <sys/event.h>
95 #include <sys/eventvar.h>
97 #include <sys/kauth.h>
99 #include <mach/mach_types.h>
100 #include <kern/kern_types.h>
101 #include <kern/assert.h>
102 #include <kern/kalloc.h>
103 #include <kern/thread.h>
104 #include <kern/clock.h>
105 #include <kern/ledger.h>
106 #include <kern/task.h>
108 #include <kern/telemetry.h>
111 #include <sys/mbuf.h>
112 #include <sys/domain.h>
113 #include <sys/socket.h>
114 #include <sys/socketvar.h>
115 #include <sys/errno.h>
116 #include <sys/syscall.h>
117 #include <sys/pipe.h>
119 #include <security/audit/audit.h>
122 #include <net/route.h>
124 #include <netinet/in.h>
125 #include <netinet/in_systm.h>
126 #include <netinet/ip.h>
127 #include <netinet/in_pcb.h>
128 #include <netinet/ip_var.h>
129 #include <netinet/ip6.h>
130 #include <netinet/tcp.h>
131 #include <netinet/tcp_fsm.h>
132 #include <netinet/tcp_seq.h>
133 #include <netinet/tcp_timer.h>
134 #include <netinet/tcp_var.h>
135 #include <netinet/tcpip.h>
136 #include <netinet/tcp_debug.h>
137 /* for wait queue based select */
138 #include <kern/wait_queue.h>
139 #include <kern/kalloc.h>
140 #include <sys/vnode_internal.h>
142 #include <pexpert/pexpert.h>
144 /* XXX should be in a header file somewhere */
145 void evsofree(struct socket
*);
146 void evpipefree(struct pipe
*);
147 void postpipeevent(struct pipe
*, int);
148 void postevent(struct socket
*, struct sockbuf
*, int);
149 extern kern_return_t
IOBSDGetPlatformUUID(__darwin_uuid_t uuid
, mach_timespec_t timeoutp
);
151 int rd_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
);
152 int wr_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
);
153 extern void *get_bsduthreadarg(thread_t
);
154 extern int *get_bsduthreadrval(thread_t
);
156 __private_extern__
int dofileread(vfs_context_t ctx
, struct fileproc
*fp
,
157 user_addr_t bufp
, user_size_t nbyte
,
158 off_t offset
, int flags
, user_ssize_t
*retval
);
159 __private_extern__
int dofilewrite(vfs_context_t ctx
, struct fileproc
*fp
,
160 user_addr_t bufp
, user_size_t nbyte
,
161 off_t offset
, int flags
, user_ssize_t
*retval
);
162 __private_extern__
int preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_vnode
);
163 __private_extern__
void donefileread(struct proc
*p
, struct fileproc
*fp_ret
, int fd
);
166 /* Conflict wait queue for when selects collide (opaque type) */
167 struct wait_queue select_conflict_queue
;
170 int temp_debug_13841988
= 0;
174 * Init routine called from bsd_init.c
176 void select_wait_queue_init(void);
178 select_wait_queue_init(void)
180 wait_queue_init(&select_conflict_queue
, SYNC_POLICY_FIFO
);
182 if (PE_parse_boot_argn("temp_debug_13841988", &temp_debug_13841988
, sizeof(temp_debug_13841988
))) {
183 kprintf("Temporary debugging for 13841988 enabled\n");
188 #define f_flag f_fglob->fg_flag
189 #define f_type f_fglob->fg_ops->fo_type
190 #define f_msgcount f_fglob->fg_msgcount
191 #define f_cred f_fglob->fg_cred
192 #define f_ops f_fglob->fg_ops
193 #define f_offset f_fglob->fg_offset
194 #define f_data f_fglob->fg_data
200 * preparefileread:EBADF
201 * preparefileread:ESPIPE
202 * preparefileread:ENXIO
203 * preparefileread:EBADF
207 read(struct proc
*p
, struct read_args
*uap
, user_ssize_t
*retval
)
209 __pthread_testcancel(1);
210 return(read_nocancel(p
, (struct read_nocancel_args
*)uap
, retval
));
214 read_nocancel(struct proc
*p
, struct read_nocancel_args
*uap
, user_ssize_t
*retval
)
219 struct vfs_context context
;
221 if ( (error
= preparefileread(p
, &fp
, fd
, 0)) )
224 context
= *(vfs_context_current());
225 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
227 error
= dofileread(&context
, fp
, uap
->cbuf
, uap
->nbyte
,
228 (off_t
)-1, 0, retval
);
230 donefileread(p
, fp
, fd
);
239 * preparefileread:EBADF
240 * preparefileread:ESPIPE
241 * preparefileread:ENXIO
242 * preparefileread:EBADF
246 pread(struct proc
*p
, struct pread_args
*uap
, user_ssize_t
*retval
)
248 __pthread_testcancel(1);
249 return(pread_nocancel(p
, (struct pread_nocancel_args
*)uap
, retval
));
253 pread_nocancel(struct proc
*p
, struct pread_nocancel_args
*uap
, user_ssize_t
*retval
)
255 struct fileproc
*fp
= NULL
; /* fp set by preparefileread() */
258 struct vfs_context context
;
260 if ( (error
= preparefileread(p
, &fp
, fd
, 1)) )
263 context
= *(vfs_context_current());
264 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
266 error
= dofileread(&context
, fp
, uap
->buf
, uap
->nbyte
,
267 uap
->offset
, FOF_OFFSET
, retval
);
269 donefileread(p
, fp
, fd
);
271 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pread
) | DBG_FUNC_NONE
),
272 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
279 * Code common for read and pread
283 donefileread(struct proc
*p
, struct fileproc
*fp
, int fd
)
286 fp_drop(p
, fd
, fp
, 1);
299 preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_pread
)
309 error
= fp_lookup(p
, fd
, &fp
, 1);
315 if ((fp
->f_flag
& FREAD
) == 0) {
319 if (check_for_pread
&& (fp
->f_type
!= DTYPE_VNODE
)) {
323 if (fp
->f_type
== DTYPE_VNODE
) {
324 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
326 if (check_for_pread
&& (vnode_isfifo(vp
))) {
330 if (check_for_pread
&& (vp
->v_flag
& VISTTY
)) {
342 fp_drop(p
, fd
, fp
, 1);
353 __private_extern__
int
354 dofileread(vfs_context_t ctx
, struct fileproc
*fp
,
355 user_addr_t bufp
, user_size_t nbyte
, off_t offset
, int flags
,
356 user_ssize_t
*retval
)
359 user_ssize_t bytecnt
;
361 char uio_buf
[ UIO_SIZEOF(1) ];
366 if (IS_64BIT_PROCESS(vfs_context_proc(ctx
))) {
367 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_READ
,
368 &uio_buf
[0], sizeof(uio_buf
));
370 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_READ
,
371 &uio_buf
[0], sizeof(uio_buf
));
373 uio_addiov(auio
, bufp
, nbyte
);
377 if ((error
= fo_read(fp
, auio
, flags
, ctx
))) {
378 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
379 error
== EINTR
|| error
== EWOULDBLOCK
))
382 bytecnt
-= uio_resid(auio
);
390 * Scatter read system call.
399 readv(struct proc
*p
, struct readv_args
*uap
, user_ssize_t
*retval
)
401 __pthread_testcancel(1);
402 return(readv_nocancel(p
, (struct readv_nocancel_args
*)uap
, retval
));
406 readv_nocancel(struct proc
*p
, struct readv_nocancel_args
*uap
, user_ssize_t
*retval
)
410 struct user_iovec
*iovp
;
412 /* Verify range bedfore calling uio_create() */
413 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
416 /* allocate a uio large enough to hold the number of iovecs passed */
417 auio
= uio_create(uap
->iovcnt
, 0,
418 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
421 /* get location of iovecs within the uio. then copyin the iovecs from
424 iovp
= uio_iovsaddr(auio
);
427 goto ExitThisRoutine
;
429 error
= copyin_user_iovec_array(uap
->iovp
,
430 IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
,
433 goto ExitThisRoutine
;
436 /* finalize uio_t for use and do the IO
438 error
= uio_calculateresid(auio
);
440 goto ExitThisRoutine
;
442 error
= rd_uio(p
, uap
->fd
, auio
, retval
);
460 write(struct proc
*p
, struct write_args
*uap
, user_ssize_t
*retval
)
462 __pthread_testcancel(1);
463 return(write_nocancel(p
, (struct write_nocancel_args
*)uap
, retval
));
468 write_nocancel(struct proc
*p
, struct write_nocancel_args
*uap
, user_ssize_t
*retval
)
476 error
= fp_lookup(p
,fd
,&fp
,0);
479 if ((fp
->f_flag
& FWRITE
) == 0) {
482 struct vfs_context context
= *(vfs_context_current());
483 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
485 error
= dofilewrite(&context
, fp
, uap
->cbuf
, uap
->nbyte
,
486 (off_t
)-1, 0, retval
);
489 fp_drop_written(p
, fd
, fp
);
491 fp_drop(p
, fd
, fp
, 0);
507 pwrite(struct proc
*p
, struct pwrite_args
*uap
, user_ssize_t
*retval
)
509 __pthread_testcancel(1);
510 return(pwrite_nocancel(p
, (struct pwrite_nocancel_args
*)uap
, retval
));
514 pwrite_nocancel(struct proc
*p
, struct pwrite_nocancel_args
*uap
, user_ssize_t
*retval
)
519 vnode_t vp
= (vnode_t
)0;
523 error
= fp_lookup(p
,fd
,&fp
,0);
527 if ((fp
->f_flag
& FWRITE
) == 0) {
530 struct vfs_context context
= *vfs_context_current();
531 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
533 if (fp
->f_type
!= DTYPE_VNODE
) {
537 vp
= (vnode_t
)fp
->f_fglob
->fg_data
;
538 if (vnode_isfifo(vp
)) {
542 if ((vp
->v_flag
& VISTTY
)) {
546 if (uap
->offset
== (off_t
)-1) {
551 error
= dofilewrite(&context
, fp
, uap
->buf
, uap
->nbyte
,
552 uap
->offset
, FOF_OFFSET
, retval
);
556 fp_drop_written(p
, fd
, fp
);
558 fp_drop(p
, fd
, fp
, 0);
560 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pwrite
) | DBG_FUNC_NONE
),
561 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
570 * <fo_write>:??? [indirect through struct fileops]
572 __private_extern__
int
573 dofilewrite(vfs_context_t ctx
, struct fileproc
*fp
,
574 user_addr_t bufp
, user_size_t nbyte
, off_t offset
, int flags
,
575 user_ssize_t
*retval
)
579 user_ssize_t bytecnt
;
580 char uio_buf
[ UIO_SIZEOF(1) ];
585 if (IS_64BIT_PROCESS(vfs_context_proc(ctx
))) {
586 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_WRITE
,
587 &uio_buf
[0], sizeof(uio_buf
));
589 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_WRITE
,
590 &uio_buf
[0], sizeof(uio_buf
));
592 uio_addiov(auio
, bufp
, nbyte
);
595 if ((error
= fo_write(fp
, auio
, flags
, ctx
))) {
596 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
597 error
== EINTR
|| error
== EWOULDBLOCK
))
599 /* The socket layer handles SIGPIPE */
600 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
&&
601 (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) == 0) {
602 /* XXX Raise the signal on the thread? */
603 psignal(vfs_context_proc(ctx
), SIGPIPE
);
606 bytecnt
-= uio_resid(auio
);
613 * Gather write system call
616 writev(struct proc
*p
, struct writev_args
*uap
, user_ssize_t
*retval
)
618 __pthread_testcancel(1);
619 return(writev_nocancel(p
, (struct writev_nocancel_args
*)uap
, retval
));
623 writev_nocancel(struct proc
*p
, struct writev_nocancel_args
*uap
, user_ssize_t
*retval
)
627 struct user_iovec
*iovp
;
629 AUDIT_ARG(fd
, uap
->fd
);
631 /* Verify range bedfore calling uio_create() */
632 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
635 /* allocate a uio large enough to hold the number of iovecs passed */
636 auio
= uio_create(uap
->iovcnt
, 0,
637 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
640 /* get location of iovecs within the uio. then copyin the iovecs from
643 iovp
= uio_iovsaddr(auio
);
646 goto ExitThisRoutine
;
648 error
= copyin_user_iovec_array(uap
->iovp
,
649 IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
,
652 goto ExitThisRoutine
;
655 /* finalize uio_t for use and do the IO
657 error
= uio_calculateresid(auio
);
659 goto ExitThisRoutine
;
661 error
= wr_uio(p
, uap
->fd
, auio
, retval
);
672 wr_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
)
677 struct vfs_context context
= *vfs_context_current();
679 error
= fp_lookup(p
,fdes
,&fp
,0);
683 if ((fp
->f_flag
& FWRITE
) == 0) {
687 count
= uio_resid(uio
);
689 context
.vc_ucred
= fp
->f_cred
;
690 error
= fo_write(fp
, uio
, 0, &context
);
692 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
693 error
== EINTR
|| error
== EWOULDBLOCK
))
695 /* The socket layer handles SIGPIPE */
696 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
&&
697 (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) == 0)
700 *retval
= count
- uio_resid(uio
);
704 fp_drop_written(p
, fdes
, fp
);
706 fp_drop(p
, fdes
, fp
, 0);
712 rd_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
)
717 struct vfs_context context
= *vfs_context_current();
719 if ( (error
= preparefileread(p
, &fp
, fdes
, 0)) )
722 count
= uio_resid(uio
);
724 context
.vc_ucred
= fp
->f_cred
;
726 error
= fo_read(fp
, uio
, 0, &context
);
729 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
730 error
== EINTR
|| error
== EWOULDBLOCK
))
733 *retval
= count
- uio_resid(uio
);
735 donefileread(p
, fp
, fdes
);
750 * fp_lookup:EBADF Bad file descriptor
754 ioctl(struct proc
*p
, struct ioctl_args
*uap
, __unused
int32_t *retval
)
756 struct fileproc
*fp
= NULL
;
759 caddr_t datap
= NULL
, memp
= NULL
;
760 boolean_t is64bit
= FALSE
;
762 #define STK_PARAMS 128
763 char stkbuf
[STK_PARAMS
];
765 u_long com
= uap
->com
;
766 struct vfs_context context
= *vfs_context_current();
768 AUDIT_ARG(fd
, uap
->fd
);
769 AUDIT_ARG(addr
, uap
->data
);
771 is64bit
= proc_is64bit(p
);
774 AUDIT_ARG(value64
, com
);
776 AUDIT_ARG(cmd
, CAST_DOWN_EXPLICIT(int, com
));
777 #endif /* CONFIG_AUDIT */
780 * Interpret high order word to find amount of data to be
781 * copied to/from the user's address space.
783 size
= IOCPARM_LEN(com
);
784 if (size
> IOCPARM_MAX
)
786 if (size
> sizeof (stkbuf
)) {
787 if ((memp
= (caddr_t
)kalloc(size
)) == 0)
794 error
= copyin(uap
->data
, datap
, size
);
798 /* XXX - IOC_IN and no size? we should proably return an error here!! */
800 *(user_addr_t
*)datap
= uap
->data
;
803 *(uint32_t *)datap
= (uint32_t)uap
->data
;
806 } else if ((com
& IOC_OUT
) && size
)
808 * Zero the buffer so the user always
809 * gets back something deterministic.
812 else if (com
& IOC_VOID
) {
813 /* XXX - this is odd since IOC_VOID means no parameters */
815 *(user_addr_t
*)datap
= uap
->data
;
818 *(uint32_t *)datap
= (uint32_t)uap
->data
;
823 error
= fp_lookup(p
,fd
,&fp
,1);
829 AUDIT_ARG(file
, p
, fp
);
831 if ((fp
->f_flag
& (FREAD
| FWRITE
)) == 0) {
836 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
839 error
= mac_file_check_ioctl(context
.vc_ucred
, fp
->f_fglob
, com
);
846 *fdflags(p
, fd
) &= ~UF_EXCLOSE
;
850 *fdflags(p
, fd
) |= UF_EXCLOSE
;
854 if ( (tmp
= *(int *)datap
) )
855 fp
->f_flag
|= FNONBLOCK
;
857 fp
->f_flag
&= ~FNONBLOCK
;
858 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
862 if ( (tmp
= *(int *)datap
) )
863 fp
->f_flag
|= FASYNC
;
865 fp
->f_flag
&= ~FASYNC
;
866 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
871 if (fp
->f_type
== DTYPE_SOCKET
) {
872 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
875 if (fp
->f_type
== DTYPE_PIPE
) {
876 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
882 struct proc
*p1
= proc_find(tmp
);
890 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
894 if (fp
->f_type
== DTYPE_SOCKET
) {
895 *(int *)datap
= ((struct socket
*)fp
->f_data
)->so_pgid
;
898 error
= fo_ioctl(fp
, TIOCGPGRP
, datap
, &context
);
899 *(int *)datap
= -*(int *)datap
;
903 error
= fo_ioctl(fp
, com
, datap
, &context
);
905 * Copy any data to user, size was
906 * already set and checked above.
908 if (error
== 0 && (com
& IOC_OUT
) && size
)
909 error
= copyout(datap
, uap
->data
, (u_int
)size
);
913 fp_drop(p
, fd
, fp
, 1);
922 int selwait
, nselcoll
;
923 #define SEL_FIRSTPASS 1
924 #define SEL_SECONDPASS 2
925 extern int selcontinue(int error
);
926 extern int selprocess(int error
, int sel_pass
);
927 static int selscan(struct proc
*p
, struct _select
* sel
,
928 int nfd
, int32_t *retval
, int sel_pass
, wait_queue_sub_t wqsub
);
929 static int selcount(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int *count
);
930 static int seldrop_locked(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int lim
, int *need_wakeup
, int fromselcount
);
931 static int seldrop(struct proc
*p
, u_int32_t
*ibits
, int nfd
);
934 * Select system call.
937 * EINVAL Invalid argument
938 * EAGAIN Nonconformant error if allocation fails
942 select(struct proc
*p
, struct select_args
*uap
, int32_t *retval
)
944 __pthread_testcancel(1);
945 return(select_nocancel(p
, (struct select_nocancel_args
*)uap
, retval
));
949 select_nocancel(struct proc
*p
, struct select_nocancel_args
*uap
, int32_t *retval
)
956 int needzerofill
= 1;
959 th_act
= current_thread();
960 uth
= get_bsdthread_info(th_act
);
961 sel
= &uth
->uu_select
;
962 sel
->data
= &uth
->uu_kevent
.ss_select_data
;
963 retval
= (int *)get_bsduthreadrval(th_act
);
970 /* select on thread of process that already called proc_exit() */
971 if (p
->p_fd
== NULL
) {
975 if (uap
->nd
> p
->p_fd
->fd_nfiles
)
976 uap
->nd
= p
->p_fd
->fd_nfiles
; /* forgiving; slightly wrong */
978 nw
= howmany(uap
->nd
, NFDBITS
);
979 ni
= nw
* sizeof(fd_mask
);
982 * if the previously allocated space for the bits is smaller than
983 * what is requested or no space has yet been allocated for this
984 * thread, allocate enough space now.
986 * Note: If this process fails, select() will return EAGAIN; this
987 * is the same thing pool() returns in a no-memory situation, but
988 * it is not a POSIX compliant error code for select().
990 if (sel
->nbytes
< (3 * ni
)) {
993 /* Free previous allocation, if any */
994 if (sel
->ibits
!= NULL
)
995 FREE(sel
->ibits
, M_TEMP
);
996 if (sel
->obits
!= NULL
) {
997 FREE(sel
->obits
, M_TEMP
);
998 /* NULL out; subsequent ibits allocation may fail */
1002 MALLOC(sel
->ibits
, u_int32_t
*, nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
1003 if (sel
->ibits
== NULL
)
1005 MALLOC(sel
->obits
, u_int32_t
*, nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
1006 if (sel
->obits
== NULL
) {
1007 FREE(sel
->ibits
, M_TEMP
);
1011 sel
->nbytes
= nbytes
;
1016 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
1017 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
1021 * get the bits from the user address space
1023 #define getbits(name, x) \
1025 if (uap->name && (error = copyin(uap->name, \
1026 (caddr_t)&sel->ibits[(x) * nw], ni))) \
1027 goto continuation; \
1037 if (IS_64BIT_PROCESS(p
)) {
1038 struct user64_timeval atv64
;
1039 error
= copyin(uap
->tv
, (caddr_t
)&atv64
, sizeof(atv64
));
1040 /* Loses resolution - assume timeout < 68 years */
1041 atv
.tv_sec
= atv64
.tv_sec
;
1042 atv
.tv_usec
= atv64
.tv_usec
;
1044 struct user32_timeval atv32
;
1045 error
= copyin(uap
->tv
, (caddr_t
)&atv32
, sizeof(atv32
));
1046 atv
.tv_sec
= atv32
.tv_sec
;
1047 atv
.tv_usec
= atv32
.tv_usec
;
1051 if (itimerfix(&atv
)) {
1056 clock_absolutetime_interval_to_deadline(
1057 tvtoabstime(&atv
), &sel
->data
->abstime
);
1060 sel
->data
->abstime
= 0;
1062 if ( (error
= selcount(p
, sel
->ibits
, uap
->nd
, &count
)) ) {
1066 sel
->data
->count
= count
;
1067 size
= SIZEOF_WAITQUEUE_SET
+ (count
* SIZEOF_WAITQUEUE_LINK
);
1068 if (uth
->uu_allocsize
) {
1069 if (uth
->uu_wqset
== 0)
1070 panic("select: wql memory smashed");
1071 /* needed for the select now */
1072 if (size
> uth
->uu_allocsize
) {
1073 kfree(uth
->uu_wqset
, uth
->uu_allocsize
);
1074 uth
->uu_allocsize
= size
;
1075 uth
->uu_wqset
= (wait_queue_set_t
)kalloc(size
);
1076 if (uth
->uu_wqset
== (wait_queue_set_t
)NULL
)
1077 panic("failed to allocate memory for waitqueue\n");
1080 uth
->uu_allocsize
= size
;
1081 uth
->uu_wqset
= (wait_queue_set_t
)kalloc(uth
->uu_allocsize
);
1082 if (uth
->uu_wqset
== (wait_queue_set_t
)NULL
)
1083 panic("failed to allocate memory for waitqueue\n");
1085 bzero(uth
->uu_wqset
, size
);
1086 sel
->data
->wql
= (char *)uth
->uu_wqset
+ SIZEOF_WAITQUEUE_SET
;
1087 wait_queue_set_init(uth
->uu_wqset
, (SYNC_POLICY_FIFO
| SYNC_POLICY_PREPOST
));
1093 * We have already cleaned up any state we established,
1094 * either locally or as a result of selcount(). We don't
1095 * need to wait_subqueue_unlink_all(), since we haven't set
1096 * anything at this point.
1101 return selprocess(0, SEL_FIRSTPASS
);
1105 selcontinue(int error
)
1107 return selprocess(error
, SEL_SECONDPASS
);
1114 * Parameters: error The error code from our caller
1115 * sel_pass The pass we are on
1118 selprocess(int error
, int sel_pass
)
1123 struct uthread
*uth
;
1125 struct select_args
*uap
;
1127 struct _select
*sel
;
1132 wait_result_t wait_result
;
1135 th_act
= current_thread();
1136 uap
= (struct select_args
*)get_bsduthreadarg(th_act
);
1137 retval
= (int *)get_bsduthreadrval(th_act
);
1138 uth
= get_bsdthread_info(th_act
);
1139 sel
= &uth
->uu_select
;
1141 if ((error
!= 0) && (sel_pass
== SEL_FIRSTPASS
))
1143 if (sel
->data
->count
== 0)
1147 sel_pass
= SEL_FIRSTPASS
; /* Reset for seldrop */
1152 OSBitOrAtomic(P_SELECT
, &p
->p_flag
);
1153 /* skip scans if the select is just for timeouts */
1154 if (sel
->data
->count
) {
1156 * Clear out any dangling refs from prior calls; technically
1157 * there should not be any.
1159 if (sel_pass
== SEL_FIRSTPASS
)
1160 wait_queue_sub_clearrefs(uth
->uu_wqset
);
1162 error
= selscan(p
, sel
, uap
->nd
, retval
, sel_pass
, (wait_queue_sub_t
)uth
->uu_wqset
);
1163 if (error
|| *retval
) {
1167 /* if the select of log, then we canwakeup and discover some one
1168 * else already read the data; go toselct again if time permits
1182 clock_get_uptime(&now
);
1183 if (now
>= sel
->data
->abstime
)
1188 /* cleanup obits and try again */
1190 sel_pass
= SEL_FIRSTPASS
;
1195 * To effect a poll, the timeout argument should be
1196 * non-nil, pointing to a zero-valued timeval structure.
1198 if (uap
->tv
&& sel
->data
->abstime
== 0) {
1202 /* No spurious wakeups due to colls,no need to check for them */
1203 if ((sel_pass
== SEL_SECONDPASS
) || ((p
->p_flag
& P_SELECT
) == 0)) {
1204 sel_pass
= SEL_FIRSTPASS
;
1208 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1210 /* if the select is just for timeout skip check */
1211 if (sel
->data
->count
&&(sel_pass
== SEL_SECONDPASS
))
1212 panic("selprocess: 2nd pass assertwaiting");
1214 /* Wait Queue Subordinate has waitqueue as first element */
1215 wait_result
= wait_queue_assert_wait_with_leeway((wait_queue_t
)uth
->uu_wqset
,
1216 NULL
, THREAD_ABORTSAFE
,
1217 TIMEOUT_URGENCY_USER_NORMAL
, sel
->data
->abstime
, 0);
1218 if (wait_result
!= THREAD_AWAKENED
) {
1219 /* there are no preposted events */
1220 error
= tsleep1(NULL
, PSOCK
| PCATCH
,
1221 "select", 0, selcontinue
);
1228 sel_pass
= SEL_SECONDPASS
;
1235 wait_subqueue_unlink_all(uth
->uu_wqset
);
1236 seldrop(p
, sel
->ibits
, uap
->nd
);
1238 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1239 /* select is not restarted after signals... */
1240 if (error
== ERESTART
)
1242 if (error
== EWOULDBLOCK
)
1244 nw
= howmany(uap
->nd
, NFDBITS
);
1245 ni
= nw
* sizeof(fd_mask
);
1247 #define putbits(name, x) \
1249 if (uap->name && (error2 = \
1250 copyout((caddr_t)&sel->obits[(x) * nw], uap->name, ni))) \
1269 * Parameters: p Process performing the select
1270 * sel The per-thread select context structure
1271 * nfd The number of file descriptors to scan
1272 * retval The per thread system call return area
1273 * sel_pass Which pass this is; allowed values are
1274 * SEL_FIRSTPASS and SEL_SECONDPASS
1275 * wqsub The per thread wait queue set
1277 * Returns: 0 Success
1278 * EIO Invalid p->p_fd field XXX Obsolete?
1279 * EBADF One of the files in the bit vector is
1283 selscan(struct proc
*p
, struct _select
*sel
, int nfd
, int32_t *retval
,
1284 int sel_pass
, wait_queue_sub_t wqsub
)
1286 struct filedesc
*fdp
= p
->p_fd
;
1289 struct fileproc
*fp
;
1290 int n
= 0; /* count of bits */
1291 int nc
= 0; /* bit vector offset (nc'th bit) */
1292 static int flag
[3] = { FREAD
, FWRITE
, 0 };
1293 u_int32_t
*iptr
, *optr
;
1295 u_int32_t
*ibits
, *obits
;
1299 struct vfs_context context
= *vfs_context_current();
1302 * Problems when reboot; due to MacOSX signal probs
1303 * in Beaker1C ; verify that the p->p_fd is valid
1311 wql
= sel
->data
->wql
;
1313 nw
= howmany(nfd
, NFDBITS
);
1315 count
= sel
->data
->count
;
1320 for (msk
= 0; msk
< 3; msk
++) {
1321 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1322 optr
= (u_int32_t
*)&obits
[msk
* nw
];
1324 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1325 bits
= iptr
[i
/NFDBITS
];
1327 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1329 fp
= fdp
->fd_ofiles
[fd
];
1331 if (fp
== NULL
|| (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1333 * If we abort because of a bad
1334 * fd, let the caller unwind...
1339 if (sel_pass
== SEL_SECONDPASS
) {
1340 wql_ptr
= (char *)0;
1341 if ((fp
->f_flags
& FP_INSELECT
) && (fp
->f_waddr
== (void *)wqsub
)) {
1342 fp
->f_flags
&= ~FP_INSELECT
;
1343 fp
->f_waddr
= (void *)0;
1346 wql_ptr
= (wql
+ nc
* SIZEOF_WAITQUEUE_LINK
);
1347 if (fp
->f_flags
& FP_INSELECT
) {
1348 /* someone is already in select on this fp */
1349 fp
->f_flags
|= FP_SELCONFLICT
;
1350 wait_queue_link(&select_conflict_queue
, (wait_queue_set_t
)wqsub
);
1352 fp
->f_flags
|= FP_INSELECT
;
1353 fp
->f_waddr
= (void *)wqsub
;
1357 context
.vc_ucred
= fp
->f_cred
;
1359 /* The select; set the bit, if true */
1360 if (fp
->f_ops
&& fp
->f_type
1361 && fo_select(fp
, flag
[msk
], wql_ptr
, &context
)) {
1362 optr
[fd
/NFDBITS
] |= (1 << (fd
% NFDBITS
));
1375 int poll_callback(struct kqueue
*, struct kevent64_s
*, void *);
1377 struct poll_continue_args
{
1378 user_addr_t pca_fds
;
1384 poll(struct proc
*p
, struct poll_args
*uap
, int32_t *retval
)
1386 __pthread_testcancel(1);
1387 return(poll_nocancel(p
, (struct poll_nocancel_args
*)uap
, retval
));
1392 poll_nocancel(struct proc
*p
, struct poll_nocancel_args
*uap
, int32_t *retval
)
1394 struct poll_continue_args
*cont
;
1398 int ncoll
, error
= 0;
1399 u_int nfds
= uap
->nfds
;
1405 * This is kinda bogus. We have fd limits, but that is not
1406 * really related to the size of the pollfd array. Make sure
1407 * we let the process use at least FD_SETSIZE entries and at
1408 * least enough for the current limits. We want to be reasonably
1409 * safe, but not overly restrictive.
1411 if (nfds
> OPEN_MAX
||
1412 (nfds
> p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
&& (proc_suser(p
) || nfds
> FD_SETSIZE
)))
1415 kq
= kqueue_alloc(p
);
1419 ni
= nfds
* sizeof(struct pollfd
) + sizeof(struct poll_continue_args
);
1420 MALLOC(cont
, struct poll_continue_args
*, ni
, M_TEMP
, M_WAITOK
);
1426 fds
= (struct pollfd
*)&cont
[1];
1427 error
= copyin(uap
->fds
, fds
, nfds
* sizeof(struct pollfd
));
1431 if (uap
->timeout
!= -1) {
1434 atv
.tv_sec
= uap
->timeout
/ 1000;
1435 atv
.tv_usec
= (uap
->timeout
% 1000) * 1000;
1436 if (itimerfix(&atv
)) {
1440 getmicrouptime(&rtv
);
1441 timevaladd(&atv
, &rtv
);
1447 /* JMM - all this P_SELECT stuff is bogus */
1449 OSBitOrAtomic(P_SELECT
, &p
->p_flag
);
1450 for (i
= 0; i
< nfds
; i
++) {
1451 short events
= fds
[i
].events
;
1452 struct kevent64_s kev
;
1455 /* per spec, ignore fd values below zero */
1456 if (fds
[i
].fd
< 0) {
1461 /* convert the poll event into a kqueue kevent */
1462 kev
.ident
= fds
[i
].fd
;
1463 kev
.flags
= EV_ADD
| EV_ONESHOT
| EV_POLL
;
1464 kev
.udata
= CAST_USER_ADDR_T(&fds
[i
]);
1470 /* Handle input events */
1471 if (events
& ( POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
| POLLHUP
)) {
1472 kev
.filter
= EVFILT_READ
;
1473 if (!(events
& ( POLLIN
| POLLRDNORM
)))
1474 kev
.flags
|= EV_OOBAND
;
1475 kerror
= kevent_register(kq
, &kev
, p
);
1478 /* Handle output events */
1480 events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
)) {
1481 kev
.filter
= EVFILT_WRITE
;
1482 kerror
= kevent_register(kq
, &kev
, p
);
1485 /* Handle BSD extension vnode events */
1487 events
& ( POLLEXTEND
| POLLATTRIB
| POLLNLINK
| POLLWRITE
)) {
1488 kev
.filter
= EVFILT_VNODE
;
1490 if (events
& POLLEXTEND
)
1491 kev
.fflags
|= NOTE_EXTEND
;
1492 if (events
& POLLATTRIB
)
1493 kev
.fflags
|= NOTE_ATTRIB
;
1494 if (events
& POLLNLINK
)
1495 kev
.fflags
|= NOTE_LINK
;
1496 if (events
& POLLWRITE
)
1497 kev
.fflags
|= NOTE_WRITE
;
1498 kerror
= kevent_register(kq
, &kev
, p
);
1502 fds
[i
].revents
= POLLNVAL
;
1508 /* Did we have any trouble registering? */
1512 /* scan for, and possibly wait for, the kevents to trigger */
1513 cont
->pca_fds
= uap
->fds
;
1514 cont
->pca_nfds
= nfds
;
1515 cont
->pca_rfds
= rfds
;
1516 error
= kqueue_scan(kq
, poll_callback
, NULL
, cont
, &atv
, p
);
1517 rfds
= cont
->pca_rfds
;
1520 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1521 /* poll is not restarted after signals... */
1522 if (error
== ERESTART
)
1524 if (error
== EWOULDBLOCK
)
1527 error
= copyout(fds
, uap
->fds
, nfds
* sizeof(struct pollfd
));
1540 poll_callback(__unused
struct kqueue
*kq
, struct kevent64_s
*kevp
, void *data
)
1542 struct poll_continue_args
*cont
= (struct poll_continue_args
*)data
;
1543 struct pollfd
*fds
= CAST_DOWN(struct pollfd
*, kevp
->udata
);
1544 short prev_revents
= fds
->revents
;
1547 /* convert the results back into revents */
1548 if (kevp
->flags
& EV_EOF
)
1549 fds
->revents
|= POLLHUP
;
1550 if (kevp
->flags
& EV_ERROR
)
1551 fds
->revents
|= POLLERR
;
1553 switch (kevp
->filter
) {
1555 if (fds
->revents
& POLLHUP
)
1556 mask
= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
);
1559 if (kevp
->data
!= 0)
1560 mask
|= (POLLIN
| POLLRDNORM
);
1561 if (kevp
->flags
& EV_OOBAND
)
1562 mask
|= ( POLLPRI
| POLLRDBAND
);
1564 fds
->revents
|= (fds
->events
& mask
);
1568 if (!(fds
->revents
& POLLHUP
))
1569 fds
->revents
|= (fds
->events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
));
1573 if (kevp
->fflags
& NOTE_EXTEND
)
1574 fds
->revents
|= (fds
->events
& POLLEXTEND
);
1575 if (kevp
->fflags
& NOTE_ATTRIB
)
1576 fds
->revents
|= (fds
->events
& POLLATTRIB
);
1577 if (kevp
->fflags
& NOTE_LINK
)
1578 fds
->revents
|= (fds
->events
& POLLNLINK
);
1579 if (kevp
->fflags
& NOTE_WRITE
)
1580 fds
->revents
|= (fds
->events
& POLLWRITE
);
1584 if (fds
->revents
!= 0 && prev_revents
== 0)
1591 seltrue(__unused dev_t dev
, __unused
int flag
, __unused
struct proc
*p
)
1600 * Count the number of bits set in the input bit vector, and establish an
1601 * outstanding fp->f_iocount for each of the descriptors which will be in
1602 * use in the select operation.
1604 * Parameters: p The process doing the select
1605 * ibits The input bit vector
1606 * nfd The number of fd's in the vector
1607 * countp Pointer to where to store the bit count
1609 * Returns: 0 Success
1610 * EIO Bad per process open file table
1611 * EBADF One of the bits in the input bit vector
1612 * references an invalid fd
1614 * Implicit: *countp (modified) Count of fd's
1616 * Notes: This function is the first pass under the proc_fdlock() that
1617 * permits us to recognize invalid descriptors in the bit vector;
1618 * the may, however, not remain valid through the drop and
1619 * later reacquisition of the proc_fdlock().
1622 selcount(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int *countp
)
1624 struct filedesc
*fdp
= p
->p_fd
;
1627 struct fileproc
*fp
;
1633 int need_wakeup
= 0;
1636 * Problems when reboot; due to MacOSX signal probs
1637 * in Beaker1C ; verify that the p->p_fd is valid
1643 nw
= howmany(nfd
, NFDBITS
);
1646 for (msk
= 0; msk
< 3; msk
++) {
1647 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1648 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1649 bits
= iptr
[i
/NFDBITS
];
1650 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1652 fp
= fdp
->fd_ofiles
[fd
];
1654 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1674 /* Ignore error return; it's already EBADF */
1675 (void)seldrop_locked(p
, ibits
, nfd
, n
, &need_wakeup
, 1);
1680 wakeup(&p
->p_fpdrainwait
);
1689 * Drop outstanding wait queue references set up during selscan(); drop the
1690 * outstanding per fileproc f_iocount() picked up during the selcount().
1692 * Parameters: p Process performing the select
1693 * ibits Input pit bector of fd's
1694 * nfd Number of fd's
1695 * lim Limit to number of vector entries to
1696 * consider, or -1 for "all"
1698 * need_wakeup Pointer to flag to set to do a wakeup
1699 * if f_iocont on any descriptor goes to 0
1701 * Returns: 0 Success
1702 * EBADF One or more fds in the bit vector
1703 * were invalid, but the rest
1704 * were successfully dropped
1706 * Notes: An fd make become bad while the proc_fdlock() is not held,
1707 * if a multithreaded application closes the fd out from under
1708 * the in progress select. In this case, we still have to
1709 * clean up after the set up on the remaining fds.
1712 seldrop_locked(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int lim
, int *need_wakeup
, int fromselcount
)
1714 struct filedesc
*fdp
= p
->p_fd
;
1717 struct fileproc
*fp
;
1722 uthread_t uth
= get_bsdthread_info(current_thread());
1727 * Problems when reboot; due to MacOSX signal probs
1728 * in Beaker1C ; verify that the p->p_fd is valid
1734 nw
= howmany(nfd
, NFDBITS
);
1736 for (msk
= 0; msk
< 3; msk
++) {
1737 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1738 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1739 bits
= iptr
[i
/NFDBITS
];
1740 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1742 fp
= fdp
->fd_ofiles
[fd
];
1744 * If we've already dropped as many as were
1745 * counted/scanned, then we are done.
1747 if ((fromselcount
!= 0) && (++dropcount
> lim
))
1751 /* skip (now) bad fds */
1756 * Only clear the flag if we set it. We'll
1757 * only find that we set it if we had made
1758 * at least one [partial] pass through selscan().
1760 if ((fp
->f_flags
& FP_INSELECT
) && (fp
->f_waddr
== (void *)uth
->uu_wqset
)) {
1761 fp
->f_flags
&= ~FP_INSELECT
;
1762 fp
->f_waddr
= (void *)0;
1766 if (fp
->f_iocount
< 0)
1767 panic("f_iocount overdecrement!");
1769 if (fp
->f_iocount
== 0) {
1771 * The last iocount is responsible for clearing
1772 * selconfict flag - even if we didn't set it -
1773 * and is also responsible for waking up anyone
1774 * waiting on iocounts to drain.
1776 if (fp
->f_flags
& FP_SELCONFLICT
)
1777 fp
->f_flags
&= ~FP_SELCONFLICT
;
1778 if (p
->p_fpdrainwait
) {
1779 p
->p_fpdrainwait
= 0;
1792 seldrop(struct proc
*p
, u_int32_t
*ibits
, int nfd
)
1795 int need_wakeup
= 0;
1798 error
= seldrop_locked(p
, ibits
, nfd
, nfd
, &need_wakeup
, 0);
1801 wakeup(&p
->p_fpdrainwait
);
1807 * Record a select request.
1810 selrecord(__unused
struct proc
*selector
, struct selinfo
*sip
, void * p_wql
)
1812 thread_t cur_act
= current_thread();
1813 struct uthread
* ut
= get_bsdthread_info(cur_act
);
1815 /* need to look at collisions */
1817 /*do not record if this is second pass of select */
1818 if(p_wql
== (void *)0) {
1822 if ((sip
->si_flags
& SI_INITED
) == 0) {
1823 wait_queue_init(&sip
->si_wait_queue
, SYNC_POLICY_FIFO
);
1824 sip
->si_flags
|= SI_INITED
;
1825 sip
->si_flags
&= ~SI_CLEAR
;
1828 if (sip
->si_flags
& SI_RECORDED
) {
1829 sip
->si_flags
|= SI_COLL
;
1831 sip
->si_flags
&= ~SI_COLL
;
1833 sip
->si_flags
|= SI_RECORDED
;
1834 if (!wait_queue_member(&sip
->si_wait_queue
, ut
->uu_wqset
))
1835 wait_queue_link_noalloc(&sip
->si_wait_queue
, ut
->uu_wqset
,
1836 (wait_queue_link_t
)p_wql
);
1842 selwakeup(struct selinfo
*sip
)
1845 if ((sip
->si_flags
& SI_INITED
) == 0) {
1849 if (sip
->si_flags
& SI_COLL
) {
1851 sip
->si_flags
&= ~SI_COLL
;
1853 /* will not support */
1854 //wakeup((caddr_t)&selwait);
1858 if (sip
->si_flags
& SI_RECORDED
) {
1859 wait_queue_wakeup_all(&sip
->si_wait_queue
, NULL
, THREAD_AWAKENED
);
1860 sip
->si_flags
&= ~SI_RECORDED
;
1866 selthreadclear(struct selinfo
*sip
)
1869 if ((sip
->si_flags
& SI_INITED
) == 0) {
1872 if (sip
->si_flags
& SI_RECORDED
) {
1874 sip
->si_flags
&= ~(SI_RECORDED
| SI_COLL
);
1876 sip
->si_flags
|= SI_CLEAR
;
1877 wait_queue_unlink_all(&sip
->si_wait_queue
);
1883 #define DBG_POST 0x10
1884 #define DBG_WATCH 0x11
1885 #define DBG_WAIT 0x12
1886 #define DBG_MOD 0x13
1887 #define DBG_EWAKEUP 0x14
1888 #define DBG_ENQUEUE 0x15
1889 #define DBG_DEQUEUE 0x16
1891 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
1892 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
1893 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
1894 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
1895 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
1896 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
1897 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
1900 #define EVPROCDEQUE(p, evq) do { \
1902 if (evq->ee_flags & EV_QUEUED) { \
1903 TAILQ_REMOVE(&p->p_evlist, evq, ee_plist); \
1904 evq->ee_flags &= ~EV_QUEUED; \
1911 * called upon socket close. deque and free all events for
1912 * the socket... socket must be locked by caller.
1915 evsofree(struct socket
*sp
)
1917 struct eventqelt
*evq
, *next
;
1923 for (evq
= sp
->so_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
1924 next
= evq
->ee_slist
.tqe_next
;
1927 if (evq
->ee_flags
& EV_QUEUED
) {
1928 EVPROCDEQUE(p
, evq
);
1930 TAILQ_REMOVE(&sp
->so_evlist
, evq
, ee_slist
); // remove from socket q
1937 * called upon pipe close. deque and free all events for
1938 * the pipe... pipe must be locked by caller
1941 evpipefree(struct pipe
*cpipe
)
1943 struct eventqelt
*evq
, *next
;
1946 for (evq
= cpipe
->pipe_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
1947 next
= evq
->ee_slist
.tqe_next
;
1950 EVPROCDEQUE(p
, evq
);
1952 TAILQ_REMOVE(&cpipe
->pipe_evlist
, evq
, ee_slist
); // remove from pipe q
1959 * enqueue this event if it's not already queued. wakeup
1960 * the proc if we do queue this event to it...
1961 * entered with proc lock held... we drop it before
1962 * doing the wakeup and return in that state
1965 evprocenque(struct eventqelt
*evq
)
1972 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_START
, (uint32_t)evq
, evq
->ee_flags
, evq
->ee_eventmask
,0,0);
1976 if (evq
->ee_flags
& EV_QUEUED
) {
1979 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1982 evq
->ee_flags
|= EV_QUEUED
;
1984 TAILQ_INSERT_TAIL(&p
->p_evlist
, evq
, ee_plist
);
1988 wakeup(&p
->p_evlist
);
1990 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1995 * pipe lock must be taken by the caller
1998 postpipeevent(struct pipe
*pipep
, int event
)
2001 struct eventqelt
*evq
;
2005 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, event
,0,0,1,0);
2007 for (evq
= pipep
->pipe_evlist
.tqh_first
;
2008 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2010 if (evq
->ee_eventmask
== 0)
2014 switch (event
& (EV_RWBYTES
| EV_RCLOSED
| EV_WCLOSED
)) {
2017 if ((evq
->ee_eventmask
& EV_RE
) && pipep
->pipe_buffer
.cnt
) {
2019 evq
->ee_req
.er_rcnt
= pipep
->pipe_buffer
.cnt
;
2021 if ((evq
->ee_eventmask
& EV_WR
) &&
2022 (MAX(pipep
->pipe_buffer
.size
,PIPE_SIZE
) - pipep
->pipe_buffer
.cnt
) >= PIPE_BUF
) {
2024 if (pipep
->pipe_state
& PIPE_EOF
) {
2025 mask
|= EV_WR
|EV_RESET
;
2029 evq
->ee_req
.er_wcnt
= MAX(pipep
->pipe_buffer
.size
, PIPE_SIZE
) - pipep
->pipe_buffer
.cnt
;
2035 if ((evq
->ee_eventmask
& EV_RE
)) {
2036 mask
|= EV_RE
|EV_RCLOSED
;
2038 if ((evq
->ee_eventmask
& EV_WR
)) {
2039 mask
|= EV_WR
|EV_WCLOSED
;
2048 * disarm... postevents are nops until this event is 'read' via
2049 * waitevent and then re-armed via modwatch
2051 evq
->ee_eventmask
= 0;
2054 * since events are disarmed until after the waitevent
2055 * the ee_req.er_xxxx fields can't change once we've
2056 * inserted this event into the proc queue...
2057 * therefore, the waitevent will see a 'consistent'
2058 * snapshot of the event, even though it won't hold
2059 * the pipe lock, and we're updating the event outside
2060 * of the proc lock, which it will hold
2062 evq
->ee_req
.er_eventbits
|= mask
;
2064 KERNEL_DEBUG(DBG_MISC_POST
, (uint32_t)evq
, evq
->ee_req
.er_eventbits
, mask
, 1,0);
2069 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, 0,0,0,1,0);
2074 * given either a sockbuf or a socket run down the
2075 * event list and queue ready events found...
2076 * the socket must be locked by the caller
2079 postevent(struct socket
*sp
, struct sockbuf
*sb
, int event
)
2082 struct eventqelt
*evq
;
2090 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, (int)sp
, event
, 0, 0, 0);
2092 for (evq
= sp
->so_evlist
.tqh_first
;
2093 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2095 if (evq
->ee_eventmask
== 0)
2099 /* ready for reading:
2100 - byte cnt >= receive low water mark
2101 - read-half of conn closed
2102 - conn pending for listening sock
2103 - socket error pending
2106 - byte cnt avail >= send low water mark
2107 - write half of conn closed
2108 - socket error pending
2109 - non-blocking conn completed successfully
2113 - sock at out of band mark
2116 switch (event
& EV_DMASK
) {
2119 if ((evq
->ee_eventmask
& EV_EX
)) {
2120 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
2121 mask
|= EV_EX
|EV_OOB
;
2125 case EV_RWBYTES
|EV_OOB
:
2126 if ((evq
->ee_eventmask
& EV_EX
)) {
2127 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
2128 mask
|= EV_EX
|EV_OOB
;
2131 * fall into the next case
2134 if ((evq
->ee_eventmask
& EV_RE
) && soreadable(sp
)) {
2135 /* for AFP/OT purposes; may go away in future */
2136 if ((SOCK_DOM(sp
) == PF_INET
||
2137 SOCK_DOM(sp
) == PF_INET6
) &&
2138 SOCK_PROTO(sp
) == IPPROTO_TCP
&&
2139 (sp
->so_error
== ECONNREFUSED
||
2140 sp
->so_error
== ECONNRESET
)) {
2141 if (sp
->so_pcb
== NULL
||
2142 sotoinpcb(sp
)->inp_state
==
2144 (tp
= sototcpcb(sp
)) == NULL
||
2145 tp
->t_state
== TCPS_CLOSED
) {
2146 mask
|= EV_RE
|EV_RESET
;
2151 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
2153 if (sp
->so_state
& SS_CANTRCVMORE
) {
2158 if ((evq
->ee_eventmask
& EV_WR
) && sowriteable(sp
)) {
2159 /* for AFP/OT purposes; may go away in future */
2160 if ((SOCK_DOM(sp
) == PF_INET
||
2161 SOCK_DOM(sp
) == PF_INET6
) &&
2162 SOCK_PROTO(sp
) == IPPROTO_TCP
&&
2163 (sp
->so_error
== ECONNREFUSED
||
2164 sp
->so_error
== ECONNRESET
)) {
2165 if (sp
->so_pcb
== NULL
||
2166 sotoinpcb(sp
)->inp_state
==
2168 (tp
= sototcpcb(sp
)) == NULL
||
2169 tp
->t_state
== TCPS_CLOSED
) {
2170 mask
|= EV_WR
|EV_RESET
;
2175 evq
->ee_req
.er_wcnt
= sbspace(&sp
->so_snd
);
2180 if ((evq
->ee_eventmask
& EV_RE
)) {
2181 mask
|= EV_RE
|EV_RCONN
;
2182 evq
->ee_req
.er_rcnt
= sp
->so_qlen
+ 1; // incl this one
2187 if ((evq
->ee_eventmask
& EV_WR
)) {
2188 mask
|= EV_WR
|EV_WCONN
;
2193 if ((evq
->ee_eventmask
& EV_RE
)) {
2194 mask
|= EV_RE
|EV_RCLOSED
;
2199 if ((evq
->ee_eventmask
& EV_WR
)) {
2200 mask
|= EV_WR
|EV_WCLOSED
;
2205 if (evq
->ee_eventmask
& EV_RE
) {
2206 mask
|= EV_RE
|EV_FIN
;
2212 if (evq
->ee_eventmask
& EV_RE
) {
2213 mask
|= EV_RE
| event
;
2215 if (evq
->ee_eventmask
& EV_WR
) {
2216 mask
|= EV_WR
| event
;
2221 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, -1, 0, 0, 0);
2225 KERNEL_DEBUG(DBG_MISC_POST
, (int)evq
, evq
->ee_eventmask
, evq
->ee_req
.er_eventbits
, mask
, 0);
2229 * disarm... postevents are nops until this event is 'read' via
2230 * waitevent and then re-armed via modwatch
2232 evq
->ee_eventmask
= 0;
2235 * since events are disarmed until after the waitevent
2236 * the ee_req.er_xxxx fields can't change once we've
2237 * inserted this event into the proc queue...
2238 * since waitevent can't see this event until we
2239 * enqueue it, waitevent will see a 'consistent'
2240 * snapshot of the event, even though it won't hold
2241 * the socket lock, and we're updating the event outside
2242 * of the proc lock, which it will hold
2244 evq
->ee_req
.er_eventbits
|= mask
;
2249 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, 0, 0, 0, 0);
2251 #endif /* SOCKETS */
2255 * watchevent system call. user passes us an event to watch
2256 * for. we malloc an event object, initialize it, and queue
2257 * it to the open socket. when the event occurs, postevent()
2258 * will enque it back to our proc where we can retrieve it
2261 * should this prevent duplicate events on same socket?
2264 * ENOMEM No memory for operation
2268 watchevent(proc_t p
, struct watchevent_args
*uap
, __unused
int *retval
)
2270 struct eventqelt
*evq
= (struct eventqelt
*)0;
2271 struct eventqelt
*np
= NULL
;
2272 struct eventreq64
*erp
;
2273 struct fileproc
*fp
= NULL
;
2276 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_START
, 0,0,0,0,0);
2278 // get a qelt and fill with users req
2279 MALLOC(evq
, struct eventqelt
*, sizeof(struct eventqelt
), M_TEMP
, M_WAITOK
);
2285 // get users request pkt
2287 if (IS_64BIT_PROCESS(p
)) {
2288 error
= copyin(uap
->u_req
, (caddr_t
)erp
, sizeof(struct eventreq64
));
2290 struct eventreq32 er32
;
2292 error
= copyin(uap
->u_req
, (caddr_t
)&er32
, sizeof(struct eventreq32
));
2295 * the user only passes in the
2296 * er_type, er_handle and er_data...
2297 * the other fields are initialized
2298 * below, so don't bother to copy
2300 erp
->er_type
= er32
.er_type
;
2301 erp
->er_handle
= er32
.er_handle
;
2302 erp
->er_data
= (user_addr_t
)er32
.er_data
;
2307 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2311 KERNEL_DEBUG(DBG_MISC_WATCH
, erp
->er_handle
,uap
->u_eventmask
,(uint32_t)evq
,0,0);
2313 // validate, freeing qelt if errors
2317 if (erp
->er_type
!= EV_FD
) {
2319 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2322 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2323 socket_lock((struct socket
*)fp
->f_data
, 1);
2324 np
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2325 #endif /* SOCKETS */
2326 } else if (fp
->f_type
== DTYPE_PIPE
) {
2327 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2328 np
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2330 fp_drop(p
, erp
->er_handle
, fp
, 1);
2338 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2343 * only allow one watch per file per proc
2345 for ( ; np
!= NULL
; np
= np
->ee_slist
.tqe_next
) {
2346 if (np
->ee_proc
== p
) {
2348 if (fp
->f_type
== DTYPE_SOCKET
)
2349 socket_unlock((struct socket
*)fp
->f_data
, 1);
2351 #endif /* SOCKETS */
2352 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2353 fp_drop(p
, erp
->er_handle
, fp
, 0);
2356 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2360 erp
->er_ecnt
= erp
->er_rcnt
= erp
->er_wcnt
= erp
->er_eventbits
= 0;
2362 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2366 if (fp
->f_type
== DTYPE_SOCKET
) {
2367 TAILQ_INSERT_TAIL(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2368 postevent((struct socket
*)fp
->f_data
, 0, EV_RWBYTES
); // catch existing events
2370 socket_unlock((struct socket
*)fp
->f_data
, 1);
2372 #endif /* SOCKETS */
2374 TAILQ_INSERT_TAIL(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2375 postpipeevent((struct pipe
*)fp
->f_data
, EV_RWBYTES
);
2377 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2379 fp_drop_event(p
, erp
->er_handle
, fp
);
2381 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, 0,0,0,0,0);
2388 * waitevent system call.
2389 * grabs the next waiting event for this proc and returns
2390 * it. if no events, user can request to sleep with timeout
2391 * or without or poll mode
2392 * ((tv != NULL && interval == 0) || tv == -1)
2395 waitevent(proc_t p
, struct waitevent_args
*uap
, int *retval
)
2398 struct eventqelt
*evq
;
2399 struct eventreq64
*erp
;
2400 uint64_t abstime
, interval
;
2401 boolean_t fast_poll
= FALSE
;
2403 struct eventreq64 er64
;
2404 struct eventreq32 er32
;
2412 * check for fast poll method
2414 if (IS_64BIT_PROCESS(p
)) {
2415 if (uap
->tv
== (user_addr_t
)-1)
2417 } else if (uap
->tv
== (user_addr_t
)((uint32_t)-1))
2420 if (fast_poll
== TRUE
) {
2421 if (p
->p_evlist
.tqh_first
== NULL
) {
2422 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_NONE
, -1,0,0,0,0);
2432 if (IS_64BIT_PROCESS(p
)) {
2433 struct user64_timeval atv64
;
2434 error
= copyin(uap
->tv
, (caddr_t
)&atv64
, sizeof(atv64
));
2435 /* Loses resolution - assume timeout < 68 years */
2436 atv
.tv_sec
= atv64
.tv_sec
;
2437 atv
.tv_usec
= atv64
.tv_usec
;
2439 struct user32_timeval atv32
;
2440 error
= copyin(uap
->tv
, (caddr_t
)&atv32
, sizeof(atv32
));
2441 atv
.tv_sec
= atv32
.tv_sec
;
2442 atv
.tv_usec
= atv32
.tv_usec
;
2447 if (itimerfix(&atv
)) {
2451 interval
= tvtoabstime(&atv
);
2453 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_START
, 0,0,0,0,0);
2457 if ((evq
= p
->p_evlist
.tqh_first
) != NULL
) {
2459 * found one... make a local copy while it's still on the queue
2460 * to prevent it from changing while in the midst of copying
2461 * don't want to hold the proc lock across a copyout because
2462 * it might block on a page fault at the target in user space
2466 if (IS_64BIT_PROCESS(p
))
2467 bcopy((caddr_t
)erp
, (caddr_t
)&uer
.er64
, sizeof (struct eventreq64
));
2469 uer
.er32
.er_type
= erp
->er_type
;
2470 uer
.er32
.er_handle
= erp
->er_handle
;
2471 uer
.er32
.er_data
= (uint32_t)erp
->er_data
;
2472 uer
.er32
.er_ecnt
= erp
->er_ecnt
;
2473 uer
.er32
.er_rcnt
= erp
->er_rcnt
;
2474 uer
.er32
.er_wcnt
= erp
->er_wcnt
;
2475 uer
.er32
.er_eventbits
= erp
->er_eventbits
;
2477 TAILQ_REMOVE(&p
->p_evlist
, evq
, ee_plist
);
2479 evq
->ee_flags
&= ~EV_QUEUED
;
2483 if (IS_64BIT_PROCESS(p
))
2484 error
= copyout((caddr_t
)&uer
.er64
, uap
->u_req
, sizeof(struct eventreq64
));
2486 error
= copyout((caddr_t
)&uer
.er32
, uap
->u_req
, sizeof(struct eventreq32
));
2488 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,
2489 evq
->ee_req
.er_handle
,evq
->ee_req
.er_eventbits
,(uint32_t)evq
,0);
2493 if (uap
->tv
&& interval
== 0) {
2495 *retval
= 1; // poll failed
2497 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,0,0,0,0);
2501 clock_absolutetime_interval_to_deadline(interval
, &abstime
);
2505 KERNEL_DEBUG(DBG_MISC_WAIT
, 1,(uint32_t)&p
->p_evlist
,0,0,0);
2507 error
= msleep1(&p
->p_evlist
, &p
->p_mlock
, (PSOCK
| PCATCH
), "waitevent", abstime
);
2509 KERNEL_DEBUG(DBG_MISC_WAIT
, 2,(uint32_t)&p
->p_evlist
,0,0,0);
2513 if (error
== ERESTART
)
2515 if (error
== EWOULDBLOCK
) {
2522 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, 0,0,0,0,0);
2528 * modwatch system call. user passes in event to modify.
2529 * if we find it we reset the event bits and que/deque event
2533 modwatch(proc_t p
, struct modwatch_args
*uap
, __unused
int *retval
)
2535 struct eventreq64 er
;
2536 struct eventreq64
*erp
= &er
;
2537 struct eventqelt
*evq
= NULL
; /* protected by error return */
2539 struct fileproc
*fp
;
2542 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_START
, 0,0,0,0,0);
2545 * get user's request pkt
2546 * just need the er_type and er_handle which sit above the
2547 * problematic er_data (32/64 issue)... so only copy in
2550 if ((error
= copyin(uap
->u_req
, (caddr_t
)erp
, sizeof(er
.er_type
) + sizeof(er
.er_handle
)))) {
2551 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2556 if (erp
->er_type
!= EV_FD
) {
2558 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2561 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2562 socket_lock((struct socket
*)fp
->f_data
, 1);
2563 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2564 #endif /* SOCKETS */
2565 } else if (fp
->f_type
== DTYPE_PIPE
) {
2566 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2567 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2569 fp_drop(p
, erp
->er_handle
, fp
, 1);
2575 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2579 if ((uap
->u_eventmask
== EV_RM
) && (fp
->f_flags
& FP_WAITEVENT
)) {
2580 fp
->f_flags
&= ~FP_WAITEVENT
;
2584 // locate event if possible
2585 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2586 if (evq
->ee_proc
== p
)
2591 if (fp
->f_type
== DTYPE_SOCKET
)
2592 socket_unlock((struct socket
*)fp
->f_data
, 1);
2594 #endif /* SOCKETS */
2595 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2596 fp_drop(p
, erp
->er_handle
, fp
, 0);
2597 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2600 KERNEL_DEBUG(DBG_MISC_MOD
, erp
->er_handle
,uap
->u_eventmask
,(uint32_t)evq
,0,0);
2602 if (uap
->u_eventmask
== EV_RM
) {
2603 EVPROCDEQUE(p
, evq
);
2606 if (fp
->f_type
== DTYPE_SOCKET
) {
2607 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2608 socket_unlock((struct socket
*)fp
->f_data
, 1);
2610 #endif /* SOCKETS */
2612 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2613 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2615 fp_drop(p
, erp
->er_handle
, fp
, 0);
2617 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, 0,0,0,0,0);
2620 switch (uap
->u_eventmask
& EV_MASK
) {
2638 case EV_EX
|EV_RE
|EV_WR
:
2639 flag
= EV_OOB
|EV_RWBYTES
;
2644 if (fp
->f_type
== DTYPE_SOCKET
)
2645 socket_unlock((struct socket
*)fp
->f_data
, 1);
2647 #endif /* SOCKETS */
2648 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2649 fp_drop(p
, erp
->er_handle
, fp
, 0);
2650 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2654 * since we're holding the socket/pipe lock, the event
2655 * cannot go from the unqueued state to the queued state
2656 * however, it can go from the queued state to the unqueued state
2657 * since that direction is protected by the proc_lock...
2658 * so do a quick check for EV_QUEUED w/o holding the proc lock
2659 * since by far the common case will be NOT EV_QUEUED, this saves
2660 * us taking the proc_lock the majority of the time
2662 if (evq
->ee_flags
& EV_QUEUED
) {
2664 * EVPROCDEQUE will recheck the state after it grabs the proc_lock
2666 EVPROCDEQUE(p
, evq
);
2669 * while the event is off the proc queue and
2670 * we're holding the socket/pipe lock
2671 * it's safe to update these fields...
2673 evq
->ee_req
.er_eventbits
= 0;
2674 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2677 if (fp
->f_type
== DTYPE_SOCKET
) {
2678 postevent((struct socket
*)fp
->f_data
, 0, flag
);
2679 socket_unlock((struct socket
*)fp
->f_data
, 1);
2681 #endif /* SOCKETS */
2683 postpipeevent((struct pipe
*)fp
->f_data
, flag
);
2684 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2686 fp_drop(p
, erp
->er_handle
, fp
, 0);
2687 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, evq
->ee_req
.er_handle
,evq
->ee_eventmask
,(uint32_t)fp
->f_data
,flag
,0);
2691 /* this routine is called from the close of fd with proc_fdlock held */
2693 waitevent_close(struct proc
*p
, struct fileproc
*fp
)
2695 struct eventqelt
*evq
;
2698 fp
->f_flags
&= ~FP_WAITEVENT
;
2701 if (fp
->f_type
== DTYPE_SOCKET
) {
2702 socket_lock((struct socket
*)fp
->f_data
, 1);
2703 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2705 #endif /* SOCKETS */
2706 if (fp
->f_type
== DTYPE_PIPE
) {
2707 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2708 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2716 // locate event if possible
2717 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2718 if (evq
->ee_proc
== p
)
2723 if (fp
->f_type
== DTYPE_SOCKET
)
2724 socket_unlock((struct socket
*)fp
->f_data
, 1);
2726 #endif /* SOCKETS */
2727 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2733 EVPROCDEQUE(p
, evq
);
2736 if (fp
->f_type
== DTYPE_SOCKET
) {
2737 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2738 socket_unlock((struct socket
*)fp
->f_data
, 1);
2740 #endif /* SOCKETS */
2742 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2743 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2756 * Description: Get the host UUID from IOKit and return it to user space.
2758 * Parameters: uuid_buf Pointer to buffer to receive UUID
2759 * timeout Timespec for timout
2760 * spi SPI, skip sandbox check (temporary)
2762 * Returns: 0 Success
2763 * EWOULDBLOCK Timeout is too short
2764 * copyout:EFAULT Bad user buffer
2766 * Notes: A timeout seems redundant, since if it's tolerable to not
2767 * have a system UUID in hand, then why ask for one?
2770 gethostuuid(struct proc
*p
, struct gethostuuid_args
*uap
, __unused
int32_t *retval
)
2774 mach_timespec_t mach_ts
; /* for IOKit call */
2775 __darwin_uuid_t uuid_kern
; /* for IOKit call */
2780 if (temp_debug_13841988
&& (0 == proc_get_darwinbgstate(p
->task
, &flags
)) && (flags
& PROC_FLAG_IOS_APPLICATION
)) {
2781 printf("Unauthorized access to gethostuuid() by %s(%d)\n", p
->p_comm
, proc_pid(p
));
2785 /* Perform sandbox check */
2789 /* Convert the 32/64 bit timespec into a mach_timespec_t */
2790 if ( proc_is64bit(p
) ) {
2791 struct user64_timespec ts
;
2792 error
= copyin(uap
->timeoutp
, &ts
, sizeof(ts
));
2795 mach_ts
.tv_sec
= ts
.tv_sec
;
2796 mach_ts
.tv_nsec
= ts
.tv_nsec
;
2798 struct user32_timespec ts
;
2799 error
= copyin(uap
->timeoutp
, &ts
, sizeof(ts
) );
2802 mach_ts
.tv_sec
= ts
.tv_sec
;
2803 mach_ts
.tv_nsec
= ts
.tv_nsec
;
2806 /* Call IOKit with the stack buffer to get the UUID */
2807 kret
= IOBSDGetPlatformUUID(uuid_kern
, mach_ts
);
2810 * If we get it, copy out the data to the user buffer; note that a
2811 * uuid_t is an array of characters, so this is size invariant for
2814 if (kret
== KERN_SUCCESS
) {
2815 error
= copyout(uuid_kern
, uap
->uuid_buf
, sizeof(uuid_kern
));
2817 error
= EWOULDBLOCK
;
2826 * Description: Omnibus system call for ledger operations
2829 ledger(struct proc
*p
, struct ledger_args
*args
, __unused
int32_t *retval
)
2834 int rval
, pid
, len
, error
;
2836 struct ledger_limit_args lla
;
2841 /* Finish copying in the necessary args before taking the proc lock */
2844 if (args
->cmd
== LEDGER_ENTRY_INFO
)
2845 error
= copyin(args
->arg3
, (char *)&len
, sizeof (len
));
2846 else if (args
->cmd
== LEDGER_TEMPLATE_INFO
)
2847 error
= copyin(args
->arg2
, (char *)&len
, sizeof (len
));
2849 else if (args
->cmd
== LEDGER_LIMIT
)
2850 error
= copyin(args
->arg2
, (char *)&lla
, sizeof (lla
));
2858 if (args
->cmd
!= LEDGER_TEMPLATE_INFO
) {
2860 proc
= proc_find(pid
);
2865 error
= mac_proc_check_ledger(p
, proc
, args
->cmd
);
2875 switch (args
->cmd
) {
2877 case LEDGER_LIMIT
: {
2878 if (!kauth_cred_issuser(kauth_cred_get()))
2880 rval
= ledger_limit(task
, &lla
);
2886 struct ledger_info info
;
2888 rval
= ledger_info(task
, &info
);
2891 rval
= copyout(&info
, args
->arg2
,
2896 case LEDGER_ENTRY_INFO
: {
2900 rval
= ledger_get_task_entry_info_multiple(task
, &buf
, &len
);
2902 if ((rval
== 0) && (len
> 0)) {
2903 sz
= len
* sizeof (struct ledger_entry_info
);
2904 rval
= copyout(buf
, args
->arg2
, sz
);
2908 rval
= copyout(&len
, args
->arg3
, sizeof (len
));
2912 case LEDGER_TEMPLATE_INFO
: {
2916 rval
= ledger_template_info(&buf
, &len
);
2917 if ((rval
== 0) && (len
> 0)) {
2918 sz
= len
* sizeof (struct ledger_template_info
);
2919 rval
= copyout(buf
, args
->arg1
, sz
);
2923 rval
= copyout(&len
, args
->arg2
, sizeof (len
));
2934 #if CONFIG_TELEMETRY
2936 telemetry(__unused
struct proc
*p
, struct telemetry_args
*args
, __unused
int32_t *retval
)
2940 switch (args
->cmd
) {
2941 case TELEMETRY_CMD_TIMER_EVENT
:
2942 error
= telemetry_timer_event(args
->deadline
, args
->interval
, args
->leeway
);
2951 #endif /* CONFIG_TELEMETRY */