2 * Copyright (c) 2000-2007 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 <mach/mach_types.h>
98 #include <kern/kern_types.h>
99 #include <kern/assert.h>
100 #include <kern/kalloc.h>
101 #include <kern/thread.h>
102 #include <kern/clock.h>
104 #include <sys/mbuf.h>
105 #include <sys/socket.h>
106 #include <sys/socketvar.h>
107 #include <sys/errno.h>
108 #include <sys/syscall.h>
109 #include <sys/pipe.h>
111 #include <security/audit/audit.h>
114 #include <net/route.h>
116 #include <netinet/in.h>
117 #include <netinet/in_systm.h>
118 #include <netinet/ip.h>
119 #include <netinet/in_pcb.h>
120 #include <netinet/ip_var.h>
121 #include <netinet/ip6.h>
122 #include <netinet/tcp.h>
123 #include <netinet/tcp_fsm.h>
124 #include <netinet/tcp_seq.h>
125 #include <netinet/tcp_timer.h>
126 #include <netinet/tcp_var.h>
127 #include <netinet/tcpip.h>
128 #include <netinet/tcp_debug.h>
129 /* for wait queue based select */
130 #include <kern/wait_queue.h>
131 #include <kern/kalloc.h>
132 #include <sys/vnode_internal.h>
134 /* XXX should be in a header file somewhere */
135 void evsofree(struct socket
*);
136 void evpipefree(struct pipe
*);
137 void postpipeevent(struct pipe
*, int);
138 void postevent(struct socket
*, struct sockbuf
*, int);
139 extern kern_return_t
IOBSDGetPlatformUUID(__darwin_uuid_t uuid
, mach_timespec_t timeoutp
);
141 int rd_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
);
142 int wr_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
);
143 extern void *get_bsduthreadarg(thread_t
);
144 extern int *get_bsduthreadrval(thread_t
);
146 __private_extern__
int dofileread(vfs_context_t ctx
, struct fileproc
*fp
,
147 user_addr_t bufp
, user_size_t nbyte
,
148 off_t offset
, int flags
, user_ssize_t
*retval
);
149 __private_extern__
int dofilewrite(vfs_context_t ctx
, struct fileproc
*fp
,
150 user_addr_t bufp
, user_size_t nbyte
,
151 off_t offset
, int flags
, user_ssize_t
*retval
);
152 __private_extern__
int preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_vnode
);
153 __private_extern__
void donefileread(struct proc
*p
, struct fileproc
*fp_ret
, int fd
);
156 extern int appletalk_inited
;
159 #define f_flag f_fglob->fg_flag
160 #define f_type f_fglob->fg_type
161 #define f_msgcount f_fglob->fg_msgcount
162 #define f_cred f_fglob->fg_cred
163 #define f_ops f_fglob->fg_ops
164 #define f_offset f_fglob->fg_offset
165 #define f_data f_fglob->fg_data
171 * preparefileread:EBADF
172 * preparefileread:ESPIPE
173 * preparefileread:ENXIO
174 * preparefileread:EBADF
178 read(struct proc
*p
, struct read_args
*uap
, user_ssize_t
*retval
)
180 __pthread_testcancel(1);
181 return(read_nocancel(p
, (struct read_nocancel_args
*)uap
, retval
));
185 read_nocancel(struct proc
*p
, struct read_nocancel_args
*uap
, user_ssize_t
*retval
)
190 struct vfs_context context
;
192 if ( (error
= preparefileread(p
, &fp
, fd
, 0)) )
195 context
= *(vfs_context_current());
196 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
198 error
= dofileread(&context
, fp
, uap
->cbuf
, uap
->nbyte
,
199 (off_t
)-1, 0, retval
);
201 donefileread(p
, fp
, fd
);
210 * preparefileread:EBADF
211 * preparefileread:ESPIPE
212 * preparefileread:ENXIO
213 * preparefileread:EBADF
217 pread(struct proc
*p
, struct pread_args
*uap
, user_ssize_t
*retval
)
219 __pthread_testcancel(1);
220 return(pread_nocancel(p
, (struct pread_nocancel_args
*)uap
, retval
));
224 pread_nocancel(struct proc
*p
, struct pread_nocancel_args
*uap
, user_ssize_t
*retval
)
226 struct fileproc
*fp
= NULL
; /* fp set by preparefileread() */
229 struct vfs_context context
;
231 if ( (error
= preparefileread(p
, &fp
, fd
, 1)) )
234 context
= *(vfs_context_current());
235 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
237 error
= dofileread(&context
, fp
, uap
->buf
, uap
->nbyte
,
238 uap
->offset
, FOF_OFFSET
, retval
);
240 donefileread(p
, fp
, fd
);
243 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pread
) | DBG_FUNC_NONE
),
244 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
251 * Code common for read and pread
255 donefileread(struct proc
*p
, struct fileproc
*fp
, int fd
)
259 fp
->f_flags
&= ~FP_INCHRREAD
;
261 fp_drop(p
, fd
, fp
, 1);
274 preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_pread
)
284 error
= fp_lookup(p
, fd
, &fp
, 1);
290 if ((fp
->f_flag
& FREAD
) == 0) {
294 if (check_for_pread
&& (fp
->f_type
!= DTYPE_VNODE
)) {
298 if (fp
->f_type
== DTYPE_VNODE
) {
299 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
301 if (check_for_pread
&& (vnode_isfifo(vp
))) {
305 if (check_for_pread
&& (vp
->v_flag
& VISTTY
)) {
309 if (vp
->v_type
== VCHR
)
310 fp
->f_flags
|= FP_INCHRREAD
;
319 fp_drop(p
, fd
, fp
, 1);
330 __private_extern__
int
331 dofileread(vfs_context_t ctx
, struct fileproc
*fp
,
332 user_addr_t bufp
, user_size_t nbyte
, off_t offset
, int flags
,
333 user_ssize_t
*retval
)
336 user_ssize_t bytecnt
;
338 char uio_buf
[ UIO_SIZEOF(1) ];
343 if (IS_64BIT_PROCESS(vfs_context_proc(ctx
))) {
344 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_READ
,
345 &uio_buf
[0], sizeof(uio_buf
));
347 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_READ
,
348 &uio_buf
[0], sizeof(uio_buf
));
350 uio_addiov(auio
, bufp
, nbyte
);
354 if ((error
= fo_read(fp
, auio
, flags
, ctx
))) {
355 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
356 error
== EINTR
|| error
== EWOULDBLOCK
))
359 bytecnt
-= uio_resid(auio
);
367 * Scatter read system call.
376 readv(struct proc
*p
, struct readv_args
*uap
, user_ssize_t
*retval
)
378 __pthread_testcancel(1);
379 return(readv_nocancel(p
, (struct readv_nocancel_args
*)uap
, retval
));
383 readv_nocancel(struct proc
*p
, struct readv_nocancel_args
*uap
, user_ssize_t
*retval
)
387 struct user_iovec
*iovp
;
389 /* Verify range bedfore calling uio_create() */
390 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
393 /* allocate a uio large enough to hold the number of iovecs passed */
394 auio
= uio_create(uap
->iovcnt
, 0,
395 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
398 /* get location of iovecs within the uio. then copyin the iovecs from
401 iovp
= uio_iovsaddr(auio
);
404 goto ExitThisRoutine
;
406 error
= copyin_user_iovec_array(uap
->iovp
,
407 IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
,
410 goto ExitThisRoutine
;
413 /* finalize uio_t for use and do the IO
415 uio_calculateresid(auio
);
416 error
= rd_uio(p
, uap
->fd
, auio
, retval
);
434 write(struct proc
*p
, struct write_args
*uap
, user_ssize_t
*retval
)
436 __pthread_testcancel(1);
437 return(write_nocancel(p
, (struct write_nocancel_args
*)uap
, retval
));
442 write_nocancel(struct proc
*p
, struct write_nocancel_args
*uap
, user_ssize_t
*retval
)
450 error
= fp_lookup(p
,fd
,&fp
,0);
453 if ((fp
->f_flag
& FWRITE
) == 0) {
456 struct vfs_context context
= *(vfs_context_current());
457 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
459 error
= dofilewrite(&context
, fp
, uap
->cbuf
, uap
->nbyte
,
460 (off_t
)-1, 0, retval
);
463 fp_drop_written(p
, fd
, fp
);
465 fp_drop(p
, fd
, fp
, 0);
481 pwrite(struct proc
*p
, struct pwrite_args
*uap
, user_ssize_t
*retval
)
483 __pthread_testcancel(1);
484 return(pwrite_nocancel(p
, (struct pwrite_nocancel_args
*)uap
, retval
));
488 pwrite_nocancel(struct proc
*p
, struct pwrite_nocancel_args
*uap
, user_ssize_t
*retval
)
493 vnode_t vp
= (vnode_t
)0;
497 error
= fp_lookup(p
,fd
,&fp
,0);
501 if ((fp
->f_flag
& FWRITE
) == 0) {
504 struct vfs_context context
= *vfs_context_current();
505 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
507 if (fp
->f_type
!= DTYPE_VNODE
) {
511 vp
= (vnode_t
)fp
->f_fglob
->fg_data
;
512 if (vnode_isfifo(vp
)) {
516 if ((vp
->v_flag
& VISTTY
)) {
520 if (uap
->offset
== (off_t
)-1) {
525 error
= dofilewrite(&context
, fp
, uap
->buf
, uap
->nbyte
,
526 uap
->offset
, FOF_OFFSET
, retval
);
530 fp_drop_written(p
, fd
, fp
);
532 fp_drop(p
, fd
, fp
, 0);
535 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pwrite
) | DBG_FUNC_NONE
),
536 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
545 * <fo_write>:??? [indirect through struct fileops]
547 __private_extern__
int
548 dofilewrite(vfs_context_t ctx
, struct fileproc
*fp
,
549 user_addr_t bufp
, user_size_t nbyte
, off_t offset
, int flags
,
550 user_ssize_t
*retval
)
554 user_ssize_t bytecnt
;
555 char uio_buf
[ UIO_SIZEOF(1) ];
560 if (IS_64BIT_PROCESS(vfs_context_proc(ctx
))) {
561 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_WRITE
,
562 &uio_buf
[0], sizeof(uio_buf
));
564 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_WRITE
,
565 &uio_buf
[0], sizeof(uio_buf
));
567 uio_addiov(auio
, bufp
, nbyte
);
570 if ((error
= fo_write(fp
, auio
, flags
, ctx
))) {
571 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
572 error
== EINTR
|| error
== EWOULDBLOCK
))
574 /* The socket layer handles SIGPIPE */
575 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
) {
576 /* XXX Raise the signal on the thread? */
577 psignal(vfs_context_proc(ctx
), SIGPIPE
);
580 bytecnt
-= uio_resid(auio
);
587 * Gather write system call
590 writev(struct proc
*p
, struct writev_args
*uap
, user_ssize_t
*retval
)
592 __pthread_testcancel(1);
593 return(writev_nocancel(p
, (struct writev_nocancel_args
*)uap
, retval
));
597 writev_nocancel(struct proc
*p
, struct writev_nocancel_args
*uap
, user_ssize_t
*retval
)
601 struct user_iovec
*iovp
;
603 AUDIT_ARG(fd
, uap
->fd
);
605 /* Verify range bedfore calling uio_create() */
606 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
609 /* allocate a uio large enough to hold the number of iovecs passed */
610 auio
= uio_create(uap
->iovcnt
, 0,
611 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
614 /* get location of iovecs within the uio. then copyin the iovecs from
617 iovp
= uio_iovsaddr(auio
);
620 goto ExitThisRoutine
;
622 error
= copyin_user_iovec_array(uap
->iovp
,
623 IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
,
626 goto ExitThisRoutine
;
629 /* finalize uio_t for use and do the IO
631 uio_calculateresid(auio
);
632 error
= wr_uio(p
, uap
->fd
, auio
, retval
);
643 wr_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
)
648 struct vfs_context context
= *vfs_context_current();
650 error
= fp_lookup(p
,fdes
,&fp
,0);
654 if ((fp
->f_flag
& FWRITE
) == 0) {
658 count
= uio_resid(uio
);
660 context
.vc_ucred
= fp
->f_cred
;
661 error
= fo_write(fp
, uio
, 0, &context
);
663 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
664 error
== EINTR
|| error
== EWOULDBLOCK
))
666 /* The socket layer handles SIGPIPE */
667 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
)
670 *retval
= count
- uio_resid(uio
);
674 fp_drop_written(p
, fdes
, fp
);
676 fp_drop(p
, fdes
, fp
, 0);
682 rd_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
)
687 struct vfs_context context
= *vfs_context_current();
689 if ( (error
= preparefileread(p
, &fp
, fdes
, 0)) )
692 count
= uio_resid(uio
);
694 context
.vc_ucred
= fp
->f_cred
;
696 error
= fo_read(fp
, uio
, 0, &context
);
699 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
700 error
== EINTR
|| error
== EWOULDBLOCK
))
703 *retval
= count
- uio_resid(uio
);
705 donefileread(p
, fp
, fdes
);
720 * fp_lookup:EBADF Bad file descriptor
724 ioctl(struct proc
*p
, struct ioctl_args
*uap
, __unused
int32_t *retval
)
733 #define STK_PARAMS 128
734 char stkbuf
[STK_PARAMS
];
736 struct vfs_context context
= *vfs_context_current();
738 AUDIT_ARG(fd
, uap
->fd
);
739 AUDIT_ARG(addr
, uap
->data
);
741 is64bit
= proc_is64bit(p
);
744 AUDIT_ARG(value64
, uap
->com
);
746 AUDIT_ARG(cmd
, CAST_DOWN_EXPLICIT(int, uap
->com
));
747 #endif /* CONFIG_AUDIT */
750 error
= fp_lookup(p
,fd
,&fp
,1);
756 AUDIT_ARG(file
, p
, fp
);
758 if ((fp
->f_flag
& (FREAD
| FWRITE
)) == 0) {
763 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
766 error
= mac_file_check_ioctl(context
.vc_ucred
, fp
->f_fglob
, uap
->com
);
773 * ### LD 6/11/97 Hack Alert: this is to get AppleTalk to work
774 * while implementing an ATioctl system call
777 if (appletalk_inited
&& ((uap
->com
& 0x0000FFFF) == 0xff99)) {
778 u_long fixed_command
;
780 #ifdef APPLETALK_DEBUG
781 kprintf("ioctl: special AppleTalk \n");
784 *(user_addr_t
*)datap
= uap
->data
;
785 fixed_command
= _IOW(0, 0xff99, uap
->data
);
786 error
= fo_ioctl(fp
, fixed_command
, datap
, &context
);
794 switch (com
= uap
->com
) {
796 *fdflags(p
, uap
->fd
) &= ~UF_EXCLOSE
;
800 *fdflags(p
, uap
->fd
) |= UF_EXCLOSE
;
806 * Interpret high order word to find amount of data to be
807 * copied to/from the user's address space.
809 size
= IOCPARM_LEN(com
);
810 if (size
> IOCPARM_MAX
) {
815 if (size
> sizeof (stkbuf
)) {
817 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
829 error
= copyin(uap
->data
, datap
, size
);
838 /* XXX - IOC_IN and no size? we should proably return an error here!! */
840 *(user_addr_t
*)datap
= uap
->data
;
843 *(uint32_t *)datap
= (uint32_t)uap
->data
;
846 } else if ((com
&IOC_OUT
) && size
)
848 * Zero the buffer so the user always
849 * gets back something deterministic.
852 else if (com
&IOC_VOID
) {
853 /* XXX - this is odd since IOC_VOID means no parameters */
855 *(user_addr_t
*)datap
= uap
->data
;
858 *(uint32_t *)datap
= (uint32_t)uap
->data
;
865 if ( (tmp
= *(int *)datap
) )
866 fp
->f_flag
|= FNONBLOCK
;
868 fp
->f_flag
&= ~FNONBLOCK
;
869 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
873 if ( (tmp
= *(int *)datap
) )
874 fp
->f_flag
|= FASYNC
;
876 fp
->f_flag
&= ~FASYNC
;
877 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
882 if (fp
->f_type
== DTYPE_SOCKET
) {
883 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
887 if (fp
->f_type
== DTYPE_PIPE
) {
888 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
894 struct proc
*p1
= proc_find(tmp
);
902 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
906 if (fp
->f_type
== DTYPE_SOCKET
) {
908 *(int *)datap
= ((struct socket
*)fp
->f_data
)->so_pgid
;
911 error
= fo_ioctl(fp
, TIOCGPGRP
, datap
, &context
);
912 *(int *)datap
= -*(int *)datap
;
916 error
= fo_ioctl(fp
, com
, datap
, &context
);
918 * Copy any data to user, size was
919 * already set and checked above.
921 if (error
== 0 && (com
&IOC_OUT
) && size
)
922 error
= copyout(datap
, uap
->data
, (u_int
)size
);
930 fp_drop(p
, fd
, fp
, 1);
935 int selwait
, nselcoll
;
936 #define SEL_FIRSTPASS 1
937 #define SEL_SECONDPASS 2
938 extern int selcontinue(int error
);
939 extern int selprocess(int error
, int sel_pass
);
940 static int selscan(struct proc
*p
, struct _select
* sel
,
941 int nfd
, int32_t *retval
, int sel_pass
, wait_queue_sub_t wqsub
);
942 static int selcount(struct proc
*p
, u_int32_t
*ibits
, u_int32_t
*obits
,
943 int nfd
, int * count
, int *kfcount
);
944 static int seldrop(struct proc
*p
, u_int32_t
*ibits
, int nfd
);
947 * Select system call.
950 * EINVAL Invalid argument
951 * EAGAIN Nonconformant error if allocation fails
955 select(struct proc
*p
, struct select_args
*uap
, int32_t *retval
)
957 __pthread_testcancel(1);
958 return(select_nocancel(p
, (struct select_nocancel_args
*)uap
, retval
));
962 select_nocancel(struct proc
*p
, struct select_nocancel_args
*uap
, int32_t *retval
)
969 int needzerofill
= 1;
973 th_act
= current_thread();
974 uth
= get_bsdthread_info(th_act
);
975 sel
= &uth
->uu_select
;
976 retval
= (int *)get_bsduthreadrval(th_act
);
983 /* select on thread of process that already called proc_exit() */
984 if (p
->p_fd
== NULL
) {
988 if (uap
->nd
> p
->p_fd
->fd_nfiles
)
989 uap
->nd
= p
->p_fd
->fd_nfiles
; /* forgiving; slightly wrong */
991 nw
= howmany(uap
->nd
, NFDBITS
);
992 ni
= nw
* sizeof(fd_mask
);
995 * if the previously allocated space for the bits is smaller than
996 * what is requested or no space has yet been allocated for this
997 * thread, allocate enough space now.
999 * Note: If this process fails, select() will return EAGAIN; this
1000 * is the same thing pool() returns in a no-memory situation, but
1001 * it is not a POSIX compliant error code for select().
1003 if (sel
->nbytes
< (3 * ni
)) {
1004 int nbytes
= 3 * ni
;
1006 /* Free previous allocation, if any */
1007 if (sel
->ibits
!= NULL
)
1008 FREE(sel
->ibits
, M_TEMP
);
1009 if (sel
->obits
!= NULL
) {
1010 FREE(sel
->obits
, M_TEMP
);
1011 /* NULL out; subsequent ibits allocation may fail */
1015 MALLOC(sel
->ibits
, u_int32_t
*, nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
1016 if (sel
->ibits
== NULL
)
1018 MALLOC(sel
->obits
, u_int32_t
*, nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
1019 if (sel
->obits
== NULL
) {
1020 FREE(sel
->ibits
, M_TEMP
);
1024 sel
->nbytes
= nbytes
;
1029 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
1030 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
1034 * get the bits from the user address space
1036 #define getbits(name, x) \
1038 if (uap->name && (error = copyin(uap->name, \
1039 (caddr_t)&sel->ibits[(x) * nw], ni))) \
1040 goto continuation; \
1050 if (IS_64BIT_PROCESS(p
)) {
1051 struct user64_timeval atv64
;
1052 error
= copyin(uap
->tv
, (caddr_t
)&atv64
, sizeof(atv64
));
1053 /* Loses resolution - assume timeout < 68 years */
1054 atv
.tv_sec
= atv64
.tv_sec
;
1055 atv
.tv_usec
= atv64
.tv_usec
;
1057 struct user32_timeval atv32
;
1058 error
= copyin(uap
->tv
, (caddr_t
)&atv32
, sizeof(atv32
));
1059 atv
.tv_sec
= atv32
.tv_sec
;
1060 atv
.tv_usec
= atv32
.tv_usec
;
1064 if (itimerfix(&atv
)) {
1069 clock_absolutetime_interval_to_deadline(
1070 tvtoabstime(&atv
), &sel
->abstime
);
1076 if ( (error
= selcount(p
, sel
->ibits
, sel
->obits
, uap
->nd
, &count
, &kfcount
)) ) {
1081 sel
->kfcount
= kfcount
;
1082 size
= SIZEOF_WAITQUEUE_SET
+ (count
* SIZEOF_WAITQUEUE_LINK
);
1083 if (uth
->uu_allocsize
) {
1084 if (uth
->uu_wqset
== 0)
1085 panic("select: wql memory smashed");
1086 /* needed for the select now */
1087 if (size
> uth
->uu_allocsize
) {
1088 kfree(uth
->uu_wqset
, uth
->uu_allocsize
);
1089 uth
->uu_allocsize
= size
;
1090 uth
->uu_wqset
= (wait_queue_set_t
)kalloc(size
);
1091 if (uth
->uu_wqset
== (wait_queue_set_t
)NULL
)
1092 panic("failed to allocate memory for waitqueue\n");
1096 uth
->uu_allocsize
= size
;
1097 uth
->uu_wqset
= (wait_queue_set_t
)kalloc(uth
->uu_allocsize
);
1098 if (uth
->uu_wqset
== (wait_queue_set_t
)NULL
)
1099 panic("failed to allocate memory for waitqueue\n");
1101 bzero(uth
->uu_wqset
, size
);
1102 sel
->wql
= (char *)uth
->uu_wqset
+ SIZEOF_WAITQUEUE_SET
;
1103 wait_queue_set_init(uth
->uu_wqset
, (SYNC_POLICY_FIFO
| SYNC_POLICY_PREPOST
));
1106 return selprocess(error
, SEL_FIRSTPASS
);
1110 selcontinue(int error
)
1112 return selprocess(error
, SEL_SECONDPASS
);
1116 selprocess(int error
, int sel_pass
)
1121 struct uthread
*uth
;
1123 struct select_args
*uap
;
1125 struct _select
*sel
;
1130 wait_result_t wait_result
;
1133 th_act
= current_thread();
1134 uap
= (struct select_args
*)get_bsduthreadarg(th_act
);
1135 retval
= (int *)get_bsduthreadrval(th_act
);
1136 uth
= get_bsdthread_info(th_act
);
1137 sel
= &uth
->uu_select
;
1139 /* if it is first pass wait queue is not setup yet */
1140 if ((error
!= 0) && (sel_pass
== SEL_FIRSTPASS
))
1142 if (sel
->count
== 0)
1150 OSBitOrAtomic(P_SELECT
, &p
->p_flag
);
1151 /* skip scans if the select is just for timeouts */
1153 if (sel_pass
== SEL_FIRSTPASS
)
1154 wait_queue_sub_clearrefs(uth
->uu_wqset
);
1156 error
= selscan(p
, sel
, uap
->nd
, retval
, sel_pass
, (wait_queue_sub_t
)uth
->uu_wqset
);
1157 if (error
|| *retval
) {
1161 /* if the select of log, then we canwakeup and discover some one
1162 * else already read the data; go toselct again if time permits
1176 clock_get_uptime(&now
);
1177 if (now
>= sel
->abstime
)
1182 /* cleanup obits and try again */
1184 sel_pass
= SEL_FIRSTPASS
;
1189 * To effect a poll, the timeout argument should be
1190 * non-nil, pointing to a zero-valued timeval structure.
1192 if (uap
->tv
&& sel
->abstime
== 0) {
1196 /* No spurious wakeups due to colls,no need to check for them */
1197 if ((sel_pass
== SEL_SECONDPASS
) || ((p
->p_flag
& P_SELECT
) == 0)) {
1198 sel_pass
= SEL_FIRSTPASS
;
1202 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1204 /* if the select is just for timeout skip check */
1205 if (sel
->count
&&(sel_pass
== SEL_SECONDPASS
))
1206 panic("selprocess: 2nd pass assertwaiting");
1208 /* Wait Queue Subordinate has waitqueue as first element */
1209 wait_result
= wait_queue_assert_wait((wait_queue_t
)uth
->uu_wqset
,
1210 NULL
, THREAD_ABORTSAFE
, sel
->abstime
);
1211 if (wait_result
!= THREAD_AWAKENED
) {
1212 /* there are no preposted events */
1213 error
= tsleep1(NULL
, PSOCK
| PCATCH
,
1214 "select", 0, selcontinue
);
1220 sel_pass
= SEL_SECONDPASS
;
1228 wait_subqueue_unlink_all(uth
->uu_wqset
);
1229 seldrop(p
, sel
->ibits
, uap
->nd
);
1231 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1232 /* select is not restarted after signals... */
1233 if (error
== ERESTART
)
1235 if (error
== EWOULDBLOCK
)
1237 nw
= howmany(uap
->nd
, NFDBITS
);
1238 ni
= nw
* sizeof(fd_mask
);
1240 #define putbits(name, x) \
1242 if (uap->name && (error2 = \
1243 copyout((caddr_t)&sel->obits[(x) * nw], uap->name, ni))) \
1259 selscan(struct proc
*p
, struct _select
*sel
, int nfd
, int32_t *retval
,
1260 int sel_pass
, wait_queue_sub_t wqsub
)
1262 struct filedesc
*fdp
= p
->p_fd
;
1265 struct fileproc
*fp
;
1268 static int flag
[3] = { FREAD
, FWRITE
, 0 };
1269 u_int32_t
*iptr
, *optr
;
1271 u_int32_t
*ibits
, *obits
;
1276 struct vfs_context context
= *vfs_context_current();
1279 * Problems when reboot; due to MacOSX signal probs
1280 * in Beaker1C ; verify that the p->p_fd is valid
1290 nw
= howmany(nfd
, NFDBITS
);
1293 kfcount
= sel
->kfcount
;
1295 if (kfcount
> count
)
1296 panic("selscan: count < kfcount");
1300 for (msk
= 0; msk
< 3; msk
++) {
1301 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1302 optr
= (u_int32_t
*)&obits
[msk
* nw
];
1304 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1305 bits
= iptr
[i
/NFDBITS
];
1307 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1309 fp
= fdp
->fd_ofiles
[fd
];
1312 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1316 if (sel_pass
== SEL_SECONDPASS
) {
1317 wql_ptr
= (char *)0;
1318 fp
->f_flags
&= ~FP_INSELECT
;
1319 fp
->f_waddr
= (void *)0;
1321 wql_ptr
= (wql
+ nc
* SIZEOF_WAITQUEUE_LINK
);
1322 fp
->f_flags
|= FP_INSELECT
;
1323 fp
->f_waddr
= (void *)wqsub
;
1326 context
.vc_ucred
= fp
->f_cred
;
1328 if (fp
->f_ops
&& (fp
->f_type
== DTYPE_VNODE
)
1329 && ((vp
= (struct vnode
*)fp
->f_data
) != NULLVP
)
1330 && (vp
->v_type
== VCHR
)
1331 && fo_select(fp
, flag
[msk
], wql_ptr
, &context
)) {
1332 optr
[fd
/NFDBITS
] |= (1 << (fd
% NFDBITS
));
1343 if (kfcount
!= count
) {
1345 for (msk
= 0; msk
< 3; msk
++) {
1346 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1347 optr
= (u_int32_t
*)&obits
[msk
* nw
];
1349 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1350 bits
= iptr
[i
/NFDBITS
];
1352 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1354 fp
= fdp
->fd_ofiles
[fd
];
1357 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1361 if (sel_pass
== SEL_SECONDPASS
) {
1362 wql_ptr
= (char *)0;
1363 fp
->f_flags
&= ~FP_INSELECT
;
1364 fp
->f_waddr
= (void *)0;
1366 wql_ptr
= (wql
+ nc
* SIZEOF_WAITQUEUE_LINK
);
1367 fp
->f_flags
|= FP_INSELECT
;
1368 fp
->f_waddr
= (void *)wqsub
;
1371 context
.vc_ucred
= fp
->f_cred
;
1374 ((fp
->f_type
!= DTYPE_VNODE
)
1375 || (((vp
= (struct vnode
*)fp
->f_data
) != NULLVP
)
1376 && (vp
->v_type
!= VCHR
))
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
.fflags
= NOTE_LOWAT
;
1482 kev
.data
= 1; /* efficiency be damned: any data should trigger */
1483 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
& ( POLLIN
| POLLRDNORM
)))
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
);
1563 /* convert the results back into revents */
1564 if (kevp
->flags
& EV_EOF
)
1565 fds
->revents
|= POLLHUP
;
1566 if (kevp
->flags
& EV_ERROR
)
1567 fds
->revents
|= POLLERR
;
1569 switch (kevp
->filter
) {
1571 if (fds
->revents
& POLLHUP
)
1572 mask
= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
);
1575 if (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
);
1607 seltrue(__unused dev_t dev
, __unused
int flag
, __unused
struct proc
*p
)
1614 selcount(struct proc
*p
, u_int32_t
*ibits
, __unused u_int32_t
*obits
,
1615 int nfd
, int *countp
, int * kfcountp
)
1617 struct filedesc
*fdp
= p
->p_fd
;
1620 struct fileproc
*fp
;
1630 * Problems when reboot; due to MacOSX signal probs
1631 * in Beaker1C ; verify that the p->p_fd is valid
1638 nw
= howmany(nfd
, NFDBITS
);
1641 for (msk
= 0; msk
< 3; msk
++) {
1642 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1643 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1644 bits
= iptr
[i
/NFDBITS
];
1645 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1647 fp
= fdp
->fd_ofiles
[fd
];
1649 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1656 if ((fp
->f_type
== DTYPE_VNODE
)
1657 && ((vp
= (struct vnode
*)fp
->f_data
) != NULLVP
)
1658 && (vp
->v_type
== VCHR
) )
1675 /* undo the iocounts */
1676 for (msk
= 0; msk
< 3; msk
++) {
1677 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1678 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1679 bits
= iptr
[i
/NFDBITS
];
1680 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1682 fp
= fdp
->fd_ofiles
[fd
];
1687 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
1688 p
->p_fpdrainwait
= 0;
1689 wakeup(&p
->p_fpdrainwait
);
1701 seldrop(struct proc
*p
, u_int32_t
*ibits
, int nfd
)
1703 struct filedesc
*fdp
= p
->p_fd
;
1706 struct fileproc
*fp
;
1712 * Problems when reboot; due to MacOSX signal probs
1713 * in Beaker1C ; verify that the p->p_fd is valid
1719 nw
= howmany(nfd
, NFDBITS
);
1723 for (msk
= 0; msk
< 3; msk
++) {
1724 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1725 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1726 bits
= iptr
[i
/NFDBITS
];
1727 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1729 fp
= fdp
->fd_ofiles
[fd
];
1732 /* if you are here then it is being closed */
1733 || (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)
1741 fp
->f_flags
&= ~FP_INSELECT
;
1743 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
1744 p
->p_fpdrainwait
= 0;
1745 wakeup(&p
->p_fpdrainwait
);
1755 * Record a select request.
1758 selrecord(__unused
struct proc
*selector
, struct selinfo
*sip
, void * p_wql
)
1760 thread_t cur_act
= current_thread();
1761 struct uthread
* ut
= get_bsdthread_info(cur_act
);
1763 /* need to look at collisions */
1765 if ((p_wql
== (void *)0) && ((sip
->si_flags
& SI_INITED
) == 0)) {
1769 /*do not record if this is second pass of select */
1770 if((p_wql
== (void *)0)) {
1774 if ((sip
->si_flags
& SI_INITED
) == 0) {
1775 wait_queue_init(&sip
->si_wait_queue
, SYNC_POLICY_FIFO
);
1776 sip
->si_flags
|= SI_INITED
;
1777 sip
->si_flags
&= ~SI_CLEAR
;
1780 if (sip
->si_flags
& SI_RECORDED
) {
1781 sip
->si_flags
|= SI_COLL
;
1783 sip
->si_flags
&= ~SI_COLL
;
1785 sip
->si_flags
|= SI_RECORDED
;
1786 if (!wait_queue_member(&sip
->si_wait_queue
, ut
->uu_wqset
))
1787 wait_queue_link_noalloc(&sip
->si_wait_queue
, ut
->uu_wqset
,
1788 (wait_queue_link_t
)p_wql
);
1794 selwakeup(struct selinfo
*sip
)
1797 if ((sip
->si_flags
& SI_INITED
) == 0) {
1801 if (sip
->si_flags
& SI_COLL
) {
1803 sip
->si_flags
&= ~SI_COLL
;
1805 /* will not support */
1806 //wakeup((caddr_t)&selwait);
1810 if (sip
->si_flags
& SI_RECORDED
) {
1811 wait_queue_wakeup_all(&sip
->si_wait_queue
, NULL
, THREAD_AWAKENED
);
1812 sip
->si_flags
&= ~SI_RECORDED
;
1818 selthreadclear(struct selinfo
*sip
)
1821 if ((sip
->si_flags
& SI_INITED
) == 0) {
1824 if (sip
->si_flags
& SI_RECORDED
) {
1826 sip
->si_flags
&= ~(SI_RECORDED
| SI_COLL
);
1828 sip
->si_flags
|= SI_CLEAR
;
1829 wait_queue_unlink_all(&sip
->si_wait_queue
);
1835 #define DBG_POST 0x10
1836 #define DBG_WATCH 0x11
1837 #define DBG_WAIT 0x12
1838 #define DBG_MOD 0x13
1839 #define DBG_EWAKEUP 0x14
1840 #define DBG_ENQUEUE 0x15
1841 #define DBG_DEQUEUE 0x16
1843 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
1844 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
1845 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
1846 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
1847 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
1848 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
1849 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
1852 #define EVPROCDEQUE(p, evq) do { \
1854 if (evq->ee_flags & EV_QUEUED) { \
1855 TAILQ_REMOVE(&p->p_evlist, evq, ee_plist); \
1856 evq->ee_flags &= ~EV_QUEUED; \
1863 * called upon socket close. deque and free all events for
1864 * the socket... socket must be locked by caller.
1867 evsofree(struct socket
*sp
)
1869 struct eventqelt
*evq
, *next
;
1875 for (evq
= sp
->so_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
1876 next
= evq
->ee_slist
.tqe_next
;
1879 if (evq
->ee_flags
& EV_QUEUED
) {
1880 EVPROCDEQUE(p
, evq
);
1882 TAILQ_REMOVE(&sp
->so_evlist
, evq
, ee_slist
); // remove from socket q
1889 * called upon pipe close. deque and free all events for
1890 * the pipe... pipe must be locked by caller
1893 evpipefree(struct pipe
*cpipe
)
1895 struct eventqelt
*evq
, *next
;
1898 for (evq
= cpipe
->pipe_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
1899 next
= evq
->ee_slist
.tqe_next
;
1902 EVPROCDEQUE(p
, evq
);
1904 TAILQ_REMOVE(&cpipe
->pipe_evlist
, evq
, ee_slist
); // remove from pipe q
1911 * enqueue this event if it's not already queued. wakeup
1912 * the proc if we do queue this event to it...
1913 * entered with proc lock held... we drop it before
1914 * doing the wakeup and return in that state
1917 evprocenque(struct eventqelt
*evq
)
1924 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_START
, (uint32_t)evq
, evq
->ee_flags
, evq
->ee_eventmask
,0,0);
1928 if (evq
->ee_flags
& EV_QUEUED
) {
1931 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1934 evq
->ee_flags
|= EV_QUEUED
;
1936 TAILQ_INSERT_TAIL(&p
->p_evlist
, evq
, ee_plist
);
1940 wakeup(&p
->p_evlist
);
1942 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1947 * pipe lock must be taken by the caller
1950 postpipeevent(struct pipe
*pipep
, int event
)
1953 struct eventqelt
*evq
;
1957 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, event
,0,0,1,0);
1959 for (evq
= pipep
->pipe_evlist
.tqh_first
;
1960 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1962 if (evq
->ee_eventmask
== 0)
1966 switch (event
& (EV_RWBYTES
| EV_RCLOSED
| EV_WCLOSED
)) {
1969 if ((evq
->ee_eventmask
& EV_RE
) && pipep
->pipe_buffer
.cnt
) {
1971 evq
->ee_req
.er_rcnt
= pipep
->pipe_buffer
.cnt
;
1973 if ((evq
->ee_eventmask
& EV_WR
) &&
1974 (pipep
->pipe_buffer
.size
- pipep
->pipe_buffer
.cnt
) >= PIPE_BUF
) {
1976 if (pipep
->pipe_state
& PIPE_EOF
) {
1977 mask
|= EV_WR
|EV_RESET
;
1981 evq
->ee_req
.er_wcnt
= pipep
->pipe_buffer
.size
- pipep
->pipe_buffer
.cnt
;
1987 if ((evq
->ee_eventmask
& EV_RE
)) {
1988 mask
|= EV_RE
|EV_RCLOSED
;
1990 if ((evq
->ee_eventmask
& EV_WR
)) {
1991 mask
|= EV_WR
|EV_WCLOSED
;
2000 * disarm... postevents are nops until this event is 'read' via
2001 * waitevent and then re-armed via modwatch
2003 evq
->ee_eventmask
= 0;
2006 * since events are disarmed until after the waitevent
2007 * the ee_req.er_xxxx fields can't change once we've
2008 * inserted this event into the proc queue...
2009 * therefore, the waitevent will see a 'consistent'
2010 * snapshot of the event, even though it won't hold
2011 * the pipe lock, and we're updating the event outside
2012 * of the proc lock, which it will hold
2014 evq
->ee_req
.er_eventbits
|= mask
;
2016 KERNEL_DEBUG(DBG_MISC_POST
, (uint32_t)evq
, evq
->ee_req
.er_eventbits
, mask
, 1,0);
2021 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, 0,0,0,1,0);
2026 * given either a sockbuf or a socket run down the
2027 * event list and queue ready events found...
2028 * the socket must be locked by the caller
2031 postevent(struct socket
*sp
, struct sockbuf
*sb
, int event
)
2034 struct eventqelt
*evq
;
2042 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, (int)sp
, event
, 0, 0, 0);
2044 for (evq
= sp
->so_evlist
.tqh_first
;
2045 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2047 if (evq
->ee_eventmask
== 0)
2051 /* ready for reading:
2052 - byte cnt >= receive low water mark
2053 - read-half of conn closed
2054 - conn pending for listening sock
2055 - socket error pending
2058 - byte cnt avail >= send low water mark
2059 - write half of conn closed
2060 - socket error pending
2061 - non-blocking conn completed successfully
2065 - sock at out of band mark
2068 switch (event
& EV_DMASK
) {
2071 if ((evq
->ee_eventmask
& EV_EX
)) {
2072 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
2073 mask
|= EV_EX
|EV_OOB
;
2077 case EV_RWBYTES
|EV_OOB
:
2078 if ((evq
->ee_eventmask
& EV_EX
)) {
2079 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
2080 mask
|= EV_EX
|EV_OOB
;
2083 * fall into the next case
2086 if ((evq
->ee_eventmask
& EV_RE
) && soreadable(sp
)) {
2088 if ((sp
->so_type
== SOCK_STREAM
) && ((sp
->so_error
== ECONNREFUSED
) || (sp
->so_error
== ECONNRESET
))) {
2089 if ((sp
->so_pcb
== 0) || (((struct inpcb
*)sp
->so_pcb
)->inp_state
== INPCB_STATE_DEAD
) || !(tp
= sototcpcb(sp
)) ||
2090 (tp
->t_state
== TCPS_CLOSED
)) {
2091 mask
|= EV_RE
|EV_RESET
;
2097 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
2099 if (sp
->so_state
& SS_CANTRCVMORE
) {
2104 if ((evq
->ee_eventmask
& EV_WR
) && sowriteable(sp
)) {
2106 if ((sp
->so_type
== SOCK_STREAM
) && ((sp
->so_error
== ECONNREFUSED
) || (sp
->so_error
== ECONNRESET
))) {
2107 if ((sp
->so_pcb
== 0) || (((struct inpcb
*)sp
->so_pcb
)->inp_state
== INPCB_STATE_DEAD
) || !(tp
= sototcpcb(sp
)) ||
2108 (tp
->t_state
== TCPS_CLOSED
)) {
2109 mask
|= EV_WR
|EV_RESET
;
2115 evq
->ee_req
.er_wcnt
= sbspace(&sp
->so_snd
);
2120 if ((evq
->ee_eventmask
& EV_RE
)) {
2121 mask
|= EV_RE
|EV_RCONN
;
2122 evq
->ee_req
.er_rcnt
= sp
->so_qlen
+ 1; // incl this one
2127 if ((evq
->ee_eventmask
& EV_WR
)) {
2128 mask
|= EV_WR
|EV_WCONN
;
2133 if ((evq
->ee_eventmask
& EV_RE
)) {
2134 mask
|= EV_RE
|EV_RCLOSED
;
2139 if ((evq
->ee_eventmask
& EV_WR
)) {
2140 mask
|= EV_WR
|EV_WCLOSED
;
2145 if (evq
->ee_eventmask
& EV_RE
) {
2146 mask
|= EV_RE
|EV_FIN
;
2152 if (evq
->ee_eventmask
& EV_RE
) {
2153 mask
|= EV_RE
| event
;
2155 if (evq
->ee_eventmask
& EV_WR
) {
2156 mask
|= EV_WR
| event
;
2161 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, -1, 0, 0, 0);
2165 KERNEL_DEBUG(DBG_MISC_POST
, (int)evq
, evq
->ee_eventmask
, evq
->ee_req
.er_eventbits
, mask
, 0);
2169 * disarm... postevents are nops until this event is 'read' via
2170 * waitevent and then re-armed via modwatch
2172 evq
->ee_eventmask
= 0;
2175 * since events are disarmed until after the waitevent
2176 * the ee_req.er_xxxx fields can't change once we've
2177 * inserted this event into the proc queue...
2178 * since waitevent can't see this event until we
2179 * enqueue it, waitevent will see a 'consistent'
2180 * snapshot of the event, even though it won't hold
2181 * the socket lock, and we're updating the event outside
2182 * of the proc lock, which it will hold
2184 evq
->ee_req
.er_eventbits
|= mask
;
2189 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, 0, 0, 0, 0);
2191 #endif /* SOCKETS */
2195 * watchevent system call. user passes us an event to watch
2196 * for. we malloc an event object, initialize it, and queue
2197 * it to the open socket. when the event occurs, postevent()
2198 * will enque it back to our proc where we can retrieve it
2201 * should this prevent duplicate events on same socket?
2204 * ENOMEM No memory for operation
2208 watchevent(proc_t p
, struct watchevent_args
*uap
, __unused
int *retval
)
2210 struct eventqelt
*evq
= (struct eventqelt
*)0;
2211 struct eventqelt
*np
= NULL
;
2212 struct eventreq64
*erp
;
2213 struct fileproc
*fp
= NULL
;
2216 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_START
, 0,0,0,0,0);
2218 // get a qelt and fill with users req
2219 MALLOC(evq
, struct eventqelt
*, sizeof(struct eventqelt
), M_TEMP
, M_WAITOK
);
2225 // get users request pkt
2227 if (IS_64BIT_PROCESS(p
)) {
2228 error
= copyin(uap
->u_req
, (caddr_t
)erp
, sizeof(struct eventreq64
));
2230 struct eventreq32 er32
;
2232 error
= copyin(uap
->u_req
, (caddr_t
)&er32
, sizeof(struct eventreq32
));
2235 * the user only passes in the
2236 * er_type, er_handle and er_data...
2237 * the other fields are initialized
2238 * below, so don't bother to copy
2240 erp
->er_type
= er32
.er_type
;
2241 erp
->er_handle
= er32
.er_handle
;
2242 erp
->er_data
= (user_addr_t
)er32
.er_data
;
2247 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2251 KERNEL_DEBUG(DBG_MISC_WATCH
, erp
->er_handle
,uap
->u_eventmask
,(uint32_t)evq
,0,0);
2253 // validate, freeing qelt if errors
2257 if (erp
->er_type
!= EV_FD
) {
2259 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2262 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2263 socket_lock((struct socket
*)fp
->f_data
, 1);
2264 np
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2265 #endif /* SOCKETS */
2266 } else if (fp
->f_type
== DTYPE_PIPE
) {
2267 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2268 np
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2270 fp_drop(p
, erp
->er_handle
, fp
, 1);
2278 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2283 * only allow one watch per file per proc
2285 for ( ; np
!= NULL
; np
= np
->ee_slist
.tqe_next
) {
2286 if (np
->ee_proc
== p
) {
2288 if (fp
->f_type
== DTYPE_SOCKET
)
2289 socket_unlock((struct socket
*)fp
->f_data
, 1);
2291 #endif /* SOCKETS */
2292 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2293 fp_drop(p
, erp
->er_handle
, fp
, 0);
2296 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2300 erp
->er_ecnt
= erp
->er_rcnt
= erp
->er_wcnt
= erp
->er_eventbits
= 0;
2302 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2306 if (fp
->f_type
== DTYPE_SOCKET
) {
2307 TAILQ_INSERT_TAIL(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2308 postevent((struct socket
*)fp
->f_data
, 0, EV_RWBYTES
); // catch existing events
2310 socket_unlock((struct socket
*)fp
->f_data
, 1);
2312 #endif /* SOCKETS */
2314 TAILQ_INSERT_TAIL(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2315 postpipeevent((struct pipe
*)fp
->f_data
, EV_RWBYTES
);
2317 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2319 fp_drop_event(p
, erp
->er_handle
, fp
);
2321 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, 0,0,0,0,0);
2328 * waitevent system call.
2329 * grabs the next waiting event for this proc and returns
2330 * it. if no events, user can request to sleep with timeout
2331 * or without or poll mode
2332 * ((tv != NULL && interval == 0) || tv == -1)
2335 waitevent(proc_t p
, struct waitevent_args
*uap
, int *retval
)
2338 struct eventqelt
*evq
;
2339 struct eventreq64
*erp
;
2340 uint64_t abstime
, interval
;
2341 boolean_t fast_poll
= FALSE
;
2343 struct eventreq64 er64
;
2344 struct eventreq32 er32
;
2352 * check for fast poll method
2354 if (IS_64BIT_PROCESS(p
)) {
2355 if (uap
->tv
== (user_addr_t
)-1)
2357 } else if (uap
->tv
== (user_addr_t
)((uint32_t)-1))
2360 if (fast_poll
== TRUE
) {
2361 if (p
->p_evlist
.tqh_first
== NULL
) {
2362 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_NONE
, -1,0,0,0,0);
2372 if (IS_64BIT_PROCESS(p
)) {
2373 struct user64_timeval atv64
;
2374 error
= copyin(uap
->tv
, (caddr_t
)&atv64
, sizeof(atv64
));
2375 /* Loses resolution - assume timeout < 68 years */
2376 atv
.tv_sec
= atv64
.tv_sec
;
2377 atv
.tv_usec
= atv64
.tv_usec
;
2379 struct user32_timeval atv32
;
2380 error
= copyin(uap
->tv
, (caddr_t
)&atv32
, sizeof(atv32
));
2381 atv
.tv_sec
= atv32
.tv_sec
;
2382 atv
.tv_usec
= atv32
.tv_usec
;
2387 if (itimerfix(&atv
)) {
2391 interval
= tvtoabstime(&atv
);
2393 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_START
, 0,0,0,0,0);
2397 if ((evq
= p
->p_evlist
.tqh_first
) != NULL
) {
2399 * found one... make a local copy while it's still on the queue
2400 * to prevent it from changing while in the midst of copying
2401 * don't want to hold the proc lock across a copyout because
2402 * it might block on a page fault at the target in user space
2406 if (IS_64BIT_PROCESS(p
))
2407 bcopy((caddr_t
)erp
, (caddr_t
)&uer
.er64
, sizeof (struct eventreq64
));
2409 uer
.er32
.er_type
= erp
->er_type
;
2410 uer
.er32
.er_handle
= erp
->er_handle
;
2411 uer
.er32
.er_data
= (uint32_t)erp
->er_data
;
2412 uer
.er32
.er_ecnt
= erp
->er_ecnt
;
2413 uer
.er32
.er_rcnt
= erp
->er_rcnt
;
2414 uer
.er32
.er_wcnt
= erp
->er_wcnt
;
2415 uer
.er32
.er_eventbits
= erp
->er_eventbits
;
2417 TAILQ_REMOVE(&p
->p_evlist
, evq
, ee_plist
);
2419 evq
->ee_flags
&= ~EV_QUEUED
;
2423 if (IS_64BIT_PROCESS(p
))
2424 error
= copyout((caddr_t
)&uer
.er64
, uap
->u_req
, sizeof(struct eventreq64
));
2426 error
= copyout((caddr_t
)&uer
.er32
, uap
->u_req
, sizeof(struct eventreq32
));
2428 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,
2429 evq
->ee_req
.er_handle
,evq
->ee_req
.er_eventbits
,(uint32_t)evq
,0);
2433 if (uap
->tv
&& interval
== 0) {
2435 *retval
= 1; // poll failed
2437 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,0,0,0,0);
2441 clock_absolutetime_interval_to_deadline(interval
, &abstime
);
2445 KERNEL_DEBUG(DBG_MISC_WAIT
, 1,(uint32_t)&p
->p_evlist
,0,0,0);
2447 error
= msleep1(&p
->p_evlist
, &p
->p_mlock
, (PSOCK
| PCATCH
), "waitevent", abstime
);
2449 KERNEL_DEBUG(DBG_MISC_WAIT
, 2,(uint32_t)&p
->p_evlist
,0,0,0);
2453 if (error
== ERESTART
)
2455 if (error
== EWOULDBLOCK
) {
2462 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, 0,0,0,0,0);
2468 * modwatch system call. user passes in event to modify.
2469 * if we find it we reset the event bits and que/deque event
2473 modwatch(proc_t p
, struct modwatch_args
*uap
, __unused
int *retval
)
2475 struct eventreq64 er
;
2476 struct eventreq64
*erp
= &er
;
2477 struct eventqelt
*evq
= NULL
; /* protected by error return */
2479 struct fileproc
*fp
;
2482 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_START
, 0,0,0,0,0);
2485 * get user's request pkt
2486 * just need the er_type and er_handle which sit above the
2487 * problematic er_data (32/64 issue)... so only copy in
2490 if ((error
= copyin(uap
->u_req
, (caddr_t
)erp
, sizeof(er
.er_type
) + sizeof(er
.er_handle
)))) {
2491 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2496 if (erp
->er_type
!= EV_FD
) {
2498 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2501 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2502 socket_lock((struct socket
*)fp
->f_data
, 1);
2503 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2504 #endif /* SOCKETS */
2505 } else if (fp
->f_type
== DTYPE_PIPE
) {
2506 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2507 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2509 fp_drop(p
, erp
->er_handle
, fp
, 1);
2515 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2519 if ((uap
->u_eventmask
== EV_RM
) && (fp
->f_flags
& FP_WAITEVENT
)) {
2520 fp
->f_flags
&= ~FP_WAITEVENT
;
2524 // locate event if possible
2525 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2526 if (evq
->ee_proc
== p
)
2531 if (fp
->f_type
== DTYPE_SOCKET
)
2532 socket_unlock((struct socket
*)fp
->f_data
, 1);
2534 #endif /* SOCKETS */
2535 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2536 fp_drop(p
, erp
->er_handle
, fp
, 0);
2537 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2540 KERNEL_DEBUG(DBG_MISC_MOD
, erp
->er_handle
,uap
->u_eventmask
,(uint32_t)evq
,0,0);
2542 if (uap
->u_eventmask
== EV_RM
) {
2543 EVPROCDEQUE(p
, evq
);
2546 if (fp
->f_type
== DTYPE_SOCKET
) {
2547 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2548 socket_unlock((struct socket
*)fp
->f_data
, 1);
2550 #endif /* SOCKETS */
2552 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2553 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2555 fp_drop(p
, erp
->er_handle
, fp
, 0);
2557 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, 0,0,0,0,0);
2560 switch (uap
->u_eventmask
& EV_MASK
) {
2578 case EV_EX
|EV_RE
|EV_WR
:
2579 flag
= EV_OOB
|EV_RWBYTES
;
2584 if (fp
->f_type
== DTYPE_SOCKET
)
2585 socket_unlock((struct socket
*)fp
->f_data
, 1);
2587 #endif /* SOCKETS */
2588 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2589 fp_drop(p
, erp
->er_handle
, fp
, 0);
2590 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2594 * since we're holding the socket/pipe lock, the event
2595 * cannot go from the unqueued state to the queued state
2596 * however, it can go from the queued state to the unqueued state
2597 * since that direction is protected by the proc_lock...
2598 * so do a quick check for EV_QUEUED w/o holding the proc lock
2599 * since by far the common case will be NOT EV_QUEUED, this saves
2600 * us taking the proc_lock the majority of the time
2602 if (evq
->ee_flags
& EV_QUEUED
) {
2604 * EVPROCDEQUE will recheck the state after it grabs the proc_lock
2606 EVPROCDEQUE(p
, evq
);
2609 * while the event is off the proc queue and
2610 * we're holding the socket/pipe lock
2611 * it's safe to update these fields...
2613 evq
->ee_req
.er_eventbits
= 0;
2614 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2617 if (fp
->f_type
== DTYPE_SOCKET
) {
2618 postevent((struct socket
*)fp
->f_data
, 0, flag
);
2619 socket_unlock((struct socket
*)fp
->f_data
, 1);
2621 #endif /* SOCKETS */
2623 postpipeevent((struct pipe
*)fp
->f_data
, flag
);
2624 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2626 fp_drop(p
, erp
->er_handle
, fp
, 0);
2627 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, evq
->ee_req
.er_handle
,evq
->ee_eventmask
,(uint32_t)fp
->f_data
,flag
,0);
2631 /* this routine is called from the close of fd with proc_fdlock held */
2633 waitevent_close(struct proc
*p
, struct fileproc
*fp
)
2635 struct eventqelt
*evq
;
2638 fp
->f_flags
&= ~FP_WAITEVENT
;
2641 if (fp
->f_type
== DTYPE_SOCKET
) {
2642 socket_lock((struct socket
*)fp
->f_data
, 1);
2643 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2645 #endif /* SOCKETS */
2646 if (fp
->f_type
== DTYPE_PIPE
) {
2647 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2648 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2656 // locate event if possible
2657 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2658 if (evq
->ee_proc
== p
)
2663 if (fp
->f_type
== DTYPE_SOCKET
)
2664 socket_unlock((struct socket
*)fp
->f_data
, 1);
2666 #endif /* SOCKETS */
2667 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2673 EVPROCDEQUE(p
, evq
);
2676 if (fp
->f_type
== DTYPE_SOCKET
) {
2677 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2678 socket_unlock((struct socket
*)fp
->f_data
, 1);
2680 #endif /* SOCKETS */
2682 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2683 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2696 * Description: Get the host UUID from IOKit and return it to user space.
2698 * Parameters: uuid_buf Pointer to buffer to receive UUID
2699 * timeout Timespec for timout
2701 * Returns: 0 Success
2702 * EWOULDBLOCK Timeout is too short
2703 * copyout:EFAULT Bad user buffer
2705 * Notes: A timeout seems redundant, since if it's tolerable to not
2706 * have a system UUID in hand, then why ask for one?
2709 gethostuuid(struct proc
*p
, struct gethostuuid_args
*uap
, __unused
int32_t *retval
)
2713 mach_timespec_t mach_ts
; /* for IOKit call */
2714 __darwin_uuid_t uuid_kern
; /* for IOKit call */
2716 /* Convert the 32/64 bit timespec into a mach_timespec_t */
2717 if ( proc_is64bit(p
) ) {
2718 struct user64_timespec ts
;
2719 error
= copyin(uap
->timeoutp
, &ts
, sizeof(ts
));
2722 mach_ts
.tv_sec
= ts
.tv_sec
;
2723 mach_ts
.tv_nsec
= ts
.tv_nsec
;
2725 struct user32_timespec ts
;
2726 error
= copyin(uap
->timeoutp
, &ts
, sizeof(ts
) );
2729 mach_ts
.tv_sec
= ts
.tv_sec
;
2730 mach_ts
.tv_nsec
= ts
.tv_nsec
;
2733 /* Call IOKit with the stack buffer to get the UUID */
2734 kret
= IOBSDGetPlatformUUID(uuid_kern
, mach_ts
);
2737 * If we get it, copy out the data to the user buffer; note that a
2738 * uuid_t is an array of characters, so this is size invariant for
2741 if (kret
== KERN_SUCCESS
) {
2742 error
= copyout(uuid_kern
, uap
->uuid_buf
, sizeof(uuid_kern
));
2744 error
= EWOULDBLOCK
;