2 * Copyright (c) 2000-2010 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>
98 #include <mach/mach_types.h>
99 #include <kern/kern_types.h>
100 #include <kern/assert.h>
101 #include <kern/kalloc.h>
102 #include <kern/thread.h>
103 #include <kern/clock.h>
104 #include <kern/ledger.h>
105 #include <kern/task.h>
107 #include <sys/mbuf.h>
108 #include <sys/socket.h>
109 #include <sys/socketvar.h>
110 #include <sys/errno.h>
111 #include <sys/syscall.h>
112 #include <sys/pipe.h>
114 #include <security/audit/audit.h>
117 #include <net/route.h>
119 #include <netinet/in.h>
120 #include <netinet/in_systm.h>
121 #include <netinet/ip.h>
122 #include <netinet/in_pcb.h>
123 #include <netinet/ip_var.h>
124 #include <netinet/ip6.h>
125 #include <netinet/tcp.h>
126 #include <netinet/tcp_fsm.h>
127 #include <netinet/tcp_seq.h>
128 #include <netinet/tcp_timer.h>
129 #include <netinet/tcp_var.h>
130 #include <netinet/tcpip.h>
131 #include <netinet/tcp_debug.h>
132 /* for wait queue based select */
133 #include <kern/wait_queue.h>
134 #include <kern/kalloc.h>
135 #include <sys/vnode_internal.h>
137 /* XXX should be in a header file somewhere */
138 void evsofree(struct socket
*);
139 void evpipefree(struct pipe
*);
140 void postpipeevent(struct pipe
*, int);
141 void postevent(struct socket
*, struct sockbuf
*, int);
142 extern kern_return_t
IOBSDGetPlatformUUID(__darwin_uuid_t uuid
, mach_timespec_t timeoutp
);
144 int rd_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
);
145 int wr_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
);
146 extern void *get_bsduthreadarg(thread_t
);
147 extern int *get_bsduthreadrval(thread_t
);
149 __private_extern__
int dofileread(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 dofilewrite(vfs_context_t ctx
, struct fileproc
*fp
,
153 user_addr_t bufp
, user_size_t nbyte
,
154 off_t offset
, int flags
, user_ssize_t
*retval
);
155 __private_extern__
int preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_vnode
);
156 __private_extern__
void donefileread(struct proc
*p
, struct fileproc
*fp_ret
, int fd
);
159 /* Conflict wait queue for when selects collide (opaque type) */
160 struct wait_queue select_conflict_queue
;
163 * Init routine called from bsd_init.c
165 void select_wait_queue_init(void);
167 select_wait_queue_init(void)
169 wait_queue_init(&select_conflict_queue
, SYNC_POLICY_FIFO
);
174 extern int appletalk_inited
;
177 #define f_flag f_fglob->fg_flag
178 #define f_type f_fglob->fg_type
179 #define f_msgcount f_fglob->fg_msgcount
180 #define f_cred f_fglob->fg_cred
181 #define f_ops f_fglob->fg_ops
182 #define f_offset f_fglob->fg_offset
183 #define f_data f_fglob->fg_data
189 * preparefileread:EBADF
190 * preparefileread:ESPIPE
191 * preparefileread:ENXIO
192 * preparefileread:EBADF
196 read(struct proc
*p
, struct read_args
*uap
, user_ssize_t
*retval
)
198 __pthread_testcancel(1);
199 return(read_nocancel(p
, (struct read_nocancel_args
*)uap
, retval
));
203 read_nocancel(struct proc
*p
, struct read_nocancel_args
*uap
, user_ssize_t
*retval
)
208 struct vfs_context context
;
210 if ( (error
= preparefileread(p
, &fp
, fd
, 0)) )
213 context
= *(vfs_context_current());
214 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
216 error
= dofileread(&context
, fp
, uap
->cbuf
, uap
->nbyte
,
217 (off_t
)-1, 0, retval
);
219 donefileread(p
, fp
, fd
);
228 * preparefileread:EBADF
229 * preparefileread:ESPIPE
230 * preparefileread:ENXIO
231 * preparefileread:EBADF
235 pread(struct proc
*p
, struct pread_args
*uap
, user_ssize_t
*retval
)
237 __pthread_testcancel(1);
238 return(pread_nocancel(p
, (struct pread_nocancel_args
*)uap
, retval
));
242 pread_nocancel(struct proc
*p
, struct pread_nocancel_args
*uap
, user_ssize_t
*retval
)
244 struct fileproc
*fp
= NULL
; /* fp set by preparefileread() */
247 struct vfs_context context
;
249 if ( (error
= preparefileread(p
, &fp
, fd
, 1)) )
252 context
= *(vfs_context_current());
253 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
255 error
= dofileread(&context
, fp
, uap
->buf
, uap
->nbyte
,
256 uap
->offset
, FOF_OFFSET
, retval
);
258 donefileread(p
, fp
, fd
);
260 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pread
) | DBG_FUNC_NONE
),
261 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
268 * Code common for read and pread
272 donefileread(struct proc
*p
, struct fileproc
*fp
, int fd
)
276 fp
->f_flags
&= ~FP_INCHRREAD
;
278 fp_drop(p
, fd
, fp
, 1);
291 preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_pread
)
301 error
= fp_lookup(p
, fd
, &fp
, 1);
307 if ((fp
->f_flag
& FREAD
) == 0) {
311 if (check_for_pread
&& (fp
->f_type
!= DTYPE_VNODE
)) {
315 if (fp
->f_type
== DTYPE_VNODE
) {
316 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
318 if (check_for_pread
&& (vnode_isfifo(vp
))) {
322 if (check_for_pread
&& (vp
->v_flag
& VISTTY
)) {
326 if (vp
->v_type
== VCHR
)
327 fp
->f_flags
|= FP_INCHRREAD
;
336 fp_drop(p
, fd
, fp
, 1);
347 __private_extern__
int
348 dofileread(vfs_context_t ctx
, struct fileproc
*fp
,
349 user_addr_t bufp
, user_size_t nbyte
, off_t offset
, int flags
,
350 user_ssize_t
*retval
)
353 user_ssize_t bytecnt
;
355 char uio_buf
[ UIO_SIZEOF(1) ];
360 if (IS_64BIT_PROCESS(vfs_context_proc(ctx
))) {
361 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_READ
,
362 &uio_buf
[0], sizeof(uio_buf
));
364 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_READ
,
365 &uio_buf
[0], sizeof(uio_buf
));
367 uio_addiov(auio
, bufp
, nbyte
);
371 if ((error
= fo_read(fp
, auio
, flags
, ctx
))) {
372 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
373 error
== EINTR
|| error
== EWOULDBLOCK
))
376 bytecnt
-= uio_resid(auio
);
384 * Scatter read system call.
393 readv(struct proc
*p
, struct readv_args
*uap
, user_ssize_t
*retval
)
395 __pthread_testcancel(1);
396 return(readv_nocancel(p
, (struct readv_nocancel_args
*)uap
, retval
));
400 readv_nocancel(struct proc
*p
, struct readv_nocancel_args
*uap
, user_ssize_t
*retval
)
404 struct user_iovec
*iovp
;
406 /* Verify range bedfore calling uio_create() */
407 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
410 /* allocate a uio large enough to hold the number of iovecs passed */
411 auio
= uio_create(uap
->iovcnt
, 0,
412 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
415 /* get location of iovecs within the uio. then copyin the iovecs from
418 iovp
= uio_iovsaddr(auio
);
421 goto ExitThisRoutine
;
423 error
= copyin_user_iovec_array(uap
->iovp
,
424 IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
,
427 goto ExitThisRoutine
;
430 /* finalize uio_t for use and do the IO
432 uio_calculateresid(auio
);
433 error
= rd_uio(p
, uap
->fd
, auio
, retval
);
451 write(struct proc
*p
, struct write_args
*uap
, user_ssize_t
*retval
)
453 __pthread_testcancel(1);
454 return(write_nocancel(p
, (struct write_nocancel_args
*)uap
, retval
));
459 write_nocancel(struct proc
*p
, struct write_nocancel_args
*uap
, user_ssize_t
*retval
)
467 error
= fp_lookup(p
,fd
,&fp
,0);
470 if ((fp
->f_flag
& FWRITE
) == 0) {
473 struct vfs_context context
= *(vfs_context_current());
474 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
476 error
= dofilewrite(&context
, fp
, uap
->cbuf
, uap
->nbyte
,
477 (off_t
)-1, 0, retval
);
480 fp_drop_written(p
, fd
, fp
);
482 fp_drop(p
, fd
, fp
, 0);
498 pwrite(struct proc
*p
, struct pwrite_args
*uap
, user_ssize_t
*retval
)
500 __pthread_testcancel(1);
501 return(pwrite_nocancel(p
, (struct pwrite_nocancel_args
*)uap
, retval
));
505 pwrite_nocancel(struct proc
*p
, struct pwrite_nocancel_args
*uap
, user_ssize_t
*retval
)
510 vnode_t vp
= (vnode_t
)0;
514 error
= fp_lookup(p
,fd
,&fp
,0);
518 if ((fp
->f_flag
& FWRITE
) == 0) {
521 struct vfs_context context
= *vfs_context_current();
522 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
524 if (fp
->f_type
!= DTYPE_VNODE
) {
528 vp
= (vnode_t
)fp
->f_fglob
->fg_data
;
529 if (vnode_isfifo(vp
)) {
533 if ((vp
->v_flag
& VISTTY
)) {
537 if (uap
->offset
== (off_t
)-1) {
542 error
= dofilewrite(&context
, fp
, uap
->buf
, uap
->nbyte
,
543 uap
->offset
, FOF_OFFSET
, retval
);
547 fp_drop_written(p
, fd
, fp
);
549 fp_drop(p
, fd
, fp
, 0);
551 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pwrite
) | DBG_FUNC_NONE
),
552 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
561 * <fo_write>:??? [indirect through struct fileops]
563 __private_extern__
int
564 dofilewrite(vfs_context_t ctx
, struct fileproc
*fp
,
565 user_addr_t bufp
, user_size_t nbyte
, off_t offset
, int flags
,
566 user_ssize_t
*retval
)
570 user_ssize_t bytecnt
;
571 char uio_buf
[ UIO_SIZEOF(1) ];
576 if (IS_64BIT_PROCESS(vfs_context_proc(ctx
))) {
577 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_WRITE
,
578 &uio_buf
[0], sizeof(uio_buf
));
580 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_WRITE
,
581 &uio_buf
[0], sizeof(uio_buf
));
583 uio_addiov(auio
, bufp
, nbyte
);
586 if ((error
= fo_write(fp
, auio
, flags
, ctx
))) {
587 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
588 error
== EINTR
|| error
== EWOULDBLOCK
))
590 /* The socket layer handles SIGPIPE */
591 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
&&
592 (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) == 0) {
593 /* XXX Raise the signal on the thread? */
594 psignal(vfs_context_proc(ctx
), SIGPIPE
);
597 bytecnt
-= uio_resid(auio
);
604 * Gather write system call
607 writev(struct proc
*p
, struct writev_args
*uap
, user_ssize_t
*retval
)
609 __pthread_testcancel(1);
610 return(writev_nocancel(p
, (struct writev_nocancel_args
*)uap
, retval
));
614 writev_nocancel(struct proc
*p
, struct writev_nocancel_args
*uap
, user_ssize_t
*retval
)
618 struct user_iovec
*iovp
;
620 AUDIT_ARG(fd
, uap
->fd
);
622 /* Verify range bedfore calling uio_create() */
623 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
626 /* allocate a uio large enough to hold the number of iovecs passed */
627 auio
= uio_create(uap
->iovcnt
, 0,
628 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
631 /* get location of iovecs within the uio. then copyin the iovecs from
634 iovp
= uio_iovsaddr(auio
);
637 goto ExitThisRoutine
;
639 error
= copyin_user_iovec_array(uap
->iovp
,
640 IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
,
643 goto ExitThisRoutine
;
646 /* finalize uio_t for use and do the IO
648 uio_calculateresid(auio
);
649 error
= wr_uio(p
, uap
->fd
, auio
, retval
);
660 wr_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
)
665 struct vfs_context context
= *vfs_context_current();
667 error
= fp_lookup(p
,fdes
,&fp
,0);
671 if ((fp
->f_flag
& FWRITE
) == 0) {
675 count
= uio_resid(uio
);
677 context
.vc_ucred
= fp
->f_cred
;
678 error
= fo_write(fp
, uio
, 0, &context
);
680 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
681 error
== EINTR
|| error
== EWOULDBLOCK
))
683 /* The socket layer handles SIGPIPE */
684 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
&&
685 (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) == 0)
688 *retval
= count
- uio_resid(uio
);
692 fp_drop_written(p
, fdes
, fp
);
694 fp_drop(p
, fdes
, fp
, 0);
700 rd_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
)
705 struct vfs_context context
= *vfs_context_current();
707 if ( (error
= preparefileread(p
, &fp
, fdes
, 0)) )
710 count
= uio_resid(uio
);
712 context
.vc_ucred
= fp
->f_cred
;
714 error
= fo_read(fp
, uio
, 0, &context
);
717 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
718 error
== EINTR
|| error
== EWOULDBLOCK
))
721 *retval
= count
- uio_resid(uio
);
723 donefileread(p
, fp
, fdes
);
738 * fp_lookup:EBADF Bad file descriptor
742 ioctl(struct proc
*p
, struct ioctl_args
*uap
, __unused
int32_t *retval
)
751 #define STK_PARAMS 128
752 char stkbuf
[STK_PARAMS
];
754 struct vfs_context context
= *vfs_context_current();
756 AUDIT_ARG(fd
, uap
->fd
);
757 AUDIT_ARG(addr
, uap
->data
);
759 is64bit
= proc_is64bit(p
);
762 AUDIT_ARG(value64
, uap
->com
);
764 AUDIT_ARG(cmd
, CAST_DOWN_EXPLICIT(int, uap
->com
));
765 #endif /* CONFIG_AUDIT */
768 error
= fp_lookup(p
,fd
,&fp
,1);
774 AUDIT_ARG(file
, p
, fp
);
776 if ((fp
->f_flag
& (FREAD
| FWRITE
)) == 0) {
781 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
784 error
= mac_file_check_ioctl(context
.vc_ucred
, fp
->f_fglob
, uap
->com
);
791 * ### LD 6/11/97 Hack Alert: this is to get AppleTalk to work
792 * while implementing an ATioctl system call
795 if (appletalk_inited
&& ((uap
->com
& 0x0000FFFF) == 0xff99)) {
796 u_long fixed_command
;
798 #ifdef APPLETALK_DEBUG
799 kprintf("ioctl: special AppleTalk \n");
802 *(user_addr_t
*)datap
= uap
->data
;
803 fixed_command
= _IOW(0, 0xff99, uap
->data
);
804 error
= fo_ioctl(fp
, fixed_command
, datap
, &context
);
812 switch (com
= uap
->com
) {
814 *fdflags(p
, uap
->fd
) &= ~UF_EXCLOSE
;
818 *fdflags(p
, uap
->fd
) |= UF_EXCLOSE
;
824 * Interpret high order word to find amount of data to be
825 * copied to/from the user's address space.
827 size
= IOCPARM_LEN(com
);
828 if (size
> IOCPARM_MAX
) {
833 if (size
> sizeof (stkbuf
)) {
835 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
847 error
= copyin(uap
->data
, datap
, size
);
856 /* XXX - IOC_IN and no size? we should proably return an error here!! */
858 *(user_addr_t
*)datap
= uap
->data
;
861 *(uint32_t *)datap
= (uint32_t)uap
->data
;
864 } else if ((com
&IOC_OUT
) && size
)
866 * Zero the buffer so the user always
867 * gets back something deterministic.
870 else if (com
&IOC_VOID
) {
871 /* XXX - this is odd since IOC_VOID means no parameters */
873 *(user_addr_t
*)datap
= uap
->data
;
876 *(uint32_t *)datap
= (uint32_t)uap
->data
;
883 if ( (tmp
= *(int *)datap
) )
884 fp
->f_flag
|= FNONBLOCK
;
886 fp
->f_flag
&= ~FNONBLOCK
;
887 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
891 if ( (tmp
= *(int *)datap
) )
892 fp
->f_flag
|= FASYNC
;
894 fp
->f_flag
&= ~FASYNC
;
895 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
900 if (fp
->f_type
== DTYPE_SOCKET
) {
901 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
905 if (fp
->f_type
== DTYPE_PIPE
) {
906 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
912 struct proc
*p1
= proc_find(tmp
);
920 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
924 if (fp
->f_type
== DTYPE_SOCKET
) {
926 *(int *)datap
= ((struct socket
*)fp
->f_data
)->so_pgid
;
929 error
= fo_ioctl(fp
, TIOCGPGRP
, datap
, &context
);
930 *(int *)datap
= -*(int *)datap
;
934 error
= fo_ioctl(fp
, com
, datap
, &context
);
936 * Copy any data to user, size was
937 * already set and checked above.
939 if (error
== 0 && (com
&IOC_OUT
) && size
)
940 error
= copyout(datap
, uap
->data
, (u_int
)size
);
948 fp_drop(p
, fd
, fp
, 1);
953 int selwait
, nselcoll
;
954 #define SEL_FIRSTPASS 1
955 #define SEL_SECONDPASS 2
956 extern int selcontinue(int error
);
957 extern int selprocess(int error
, int sel_pass
);
958 static int selscan(struct proc
*p
, struct _select
* sel
,
959 int nfd
, int32_t *retval
, int sel_pass
, wait_queue_sub_t wqsub
);
960 static int selcount(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int *count
);
961 static int seldrop_locked(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int lim
, int *need_wakeup
, int fromselcount
);
962 static int seldrop(struct proc
*p
, u_int32_t
*ibits
, int nfd
);
965 * Select system call.
968 * EINVAL Invalid argument
969 * EAGAIN Nonconformant error if allocation fails
973 select(struct proc
*p
, struct select_args
*uap
, int32_t *retval
)
975 __pthread_testcancel(1);
976 return(select_nocancel(p
, (struct select_nocancel_args
*)uap
, retval
));
980 select_nocancel(struct proc
*p
, struct select_nocancel_args
*uap
, int32_t *retval
)
987 int needzerofill
= 1;
990 th_act
= current_thread();
991 uth
= get_bsdthread_info(th_act
);
992 sel
= &uth
->uu_select
;
993 retval
= (int *)get_bsduthreadrval(th_act
);
1000 /* select on thread of process that already called proc_exit() */
1001 if (p
->p_fd
== NULL
) {
1005 if (uap
->nd
> p
->p_fd
->fd_nfiles
)
1006 uap
->nd
= p
->p_fd
->fd_nfiles
; /* forgiving; slightly wrong */
1008 nw
= howmany(uap
->nd
, NFDBITS
);
1009 ni
= nw
* sizeof(fd_mask
);
1012 * if the previously allocated space for the bits is smaller than
1013 * what is requested or no space has yet been allocated for this
1014 * thread, allocate enough space now.
1016 * Note: If this process fails, select() will return EAGAIN; this
1017 * is the same thing pool() returns in a no-memory situation, but
1018 * it is not a POSIX compliant error code for select().
1020 if (sel
->nbytes
< (3 * ni
)) {
1021 int nbytes
= 3 * ni
;
1023 /* Free previous allocation, if any */
1024 if (sel
->ibits
!= NULL
)
1025 FREE(sel
->ibits
, M_TEMP
);
1026 if (sel
->obits
!= NULL
) {
1027 FREE(sel
->obits
, M_TEMP
);
1028 /* NULL out; subsequent ibits allocation may fail */
1032 MALLOC(sel
->ibits
, u_int32_t
*, nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
1033 if (sel
->ibits
== NULL
)
1035 MALLOC(sel
->obits
, u_int32_t
*, nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
1036 if (sel
->obits
== NULL
) {
1037 FREE(sel
->ibits
, M_TEMP
);
1041 sel
->nbytes
= nbytes
;
1046 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
1047 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
1051 * get the bits from the user address space
1053 #define getbits(name, x) \
1055 if (uap->name && (error = copyin(uap->name, \
1056 (caddr_t)&sel->ibits[(x) * nw], ni))) \
1057 goto continuation; \
1067 if (IS_64BIT_PROCESS(p
)) {
1068 struct user64_timeval atv64
;
1069 error
= copyin(uap
->tv
, (caddr_t
)&atv64
, sizeof(atv64
));
1070 /* Loses resolution - assume timeout < 68 years */
1071 atv
.tv_sec
= atv64
.tv_sec
;
1072 atv
.tv_usec
= atv64
.tv_usec
;
1074 struct user32_timeval atv32
;
1075 error
= copyin(uap
->tv
, (caddr_t
)&atv32
, sizeof(atv32
));
1076 atv
.tv_sec
= atv32
.tv_sec
;
1077 atv
.tv_usec
= atv32
.tv_usec
;
1081 if (itimerfix(&atv
)) {
1086 clock_absolutetime_interval_to_deadline(
1087 tvtoabstime(&atv
), &sel
->abstime
);
1092 if ( (error
= selcount(p
, sel
->ibits
, uap
->nd
, &count
)) ) {
1097 size
= SIZEOF_WAITQUEUE_SET
+ (count
* SIZEOF_WAITQUEUE_LINK
);
1098 if (uth
->uu_allocsize
) {
1099 if (uth
->uu_wqset
== 0)
1100 panic("select: wql memory smashed");
1101 /* needed for the select now */
1102 if (size
> uth
->uu_allocsize
) {
1103 kfree(uth
->uu_wqset
, uth
->uu_allocsize
);
1104 uth
->uu_allocsize
= size
;
1105 uth
->uu_wqset
= (wait_queue_set_t
)kalloc(size
);
1106 if (uth
->uu_wqset
== (wait_queue_set_t
)NULL
)
1107 panic("failed to allocate memory for waitqueue\n");
1110 uth
->uu_allocsize
= size
;
1111 uth
->uu_wqset
= (wait_queue_set_t
)kalloc(uth
->uu_allocsize
);
1112 if (uth
->uu_wqset
== (wait_queue_set_t
)NULL
)
1113 panic("failed to allocate memory for waitqueue\n");
1115 bzero(uth
->uu_wqset
, size
);
1116 sel
->wql
= (char *)uth
->uu_wqset
+ SIZEOF_WAITQUEUE_SET
;
1117 wait_queue_set_init(uth
->uu_wqset
, (SYNC_POLICY_FIFO
| SYNC_POLICY_PREPOST
));
1123 * We have already cleaned up any state we established,
1124 * either locally or as a result of selcount(). We don't
1125 * need to wait_subqueue_unlink_all(), since we haven't set
1126 * anything at this point.
1131 return selprocess(0, SEL_FIRSTPASS
);
1135 selcontinue(int error
)
1137 return selprocess(error
, SEL_SECONDPASS
);
1144 * Parameters: error The error code from our caller
1145 * sel_pass The pass we are on
1148 selprocess(int error
, int sel_pass
)
1153 struct uthread
*uth
;
1155 struct select_args
*uap
;
1157 struct _select
*sel
;
1162 wait_result_t wait_result
;
1165 th_act
= current_thread();
1166 uap
= (struct select_args
*)get_bsduthreadarg(th_act
);
1167 retval
= (int *)get_bsduthreadrval(th_act
);
1168 uth
= get_bsdthread_info(th_act
);
1169 sel
= &uth
->uu_select
;
1171 if ((error
!= 0) && (sel_pass
== SEL_FIRSTPASS
))
1173 if (sel
->count
== 0)
1177 sel_pass
= SEL_FIRSTPASS
; /* Reset for seldrop */
1182 OSBitOrAtomic(P_SELECT
, &p
->p_flag
);
1183 /* skip scans if the select is just for timeouts */
1186 * Clear out any dangling refs from prior calls; technically
1187 * there should not be any.
1189 if (sel_pass
== SEL_FIRSTPASS
)
1190 wait_queue_sub_clearrefs(uth
->uu_wqset
);
1192 error
= selscan(p
, sel
, uap
->nd
, retval
, sel_pass
, (wait_queue_sub_t
)uth
->uu_wqset
);
1193 if (error
|| *retval
) {
1197 /* if the select of log, then we canwakeup and discover some one
1198 * else already read the data; go toselct again if time permits
1212 clock_get_uptime(&now
);
1213 if (now
>= sel
->abstime
)
1218 /* cleanup obits and try again */
1220 sel_pass
= SEL_FIRSTPASS
;
1225 * To effect a poll, the timeout argument should be
1226 * non-nil, pointing to a zero-valued timeval structure.
1228 if (uap
->tv
&& sel
->abstime
== 0) {
1232 /* No spurious wakeups due to colls,no need to check for them */
1233 if ((sel_pass
== SEL_SECONDPASS
) || ((p
->p_flag
& P_SELECT
) == 0)) {
1234 sel_pass
= SEL_FIRSTPASS
;
1238 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1240 /* if the select is just for timeout skip check */
1241 if (sel
->count
&&(sel_pass
== SEL_SECONDPASS
))
1242 panic("selprocess: 2nd pass assertwaiting");
1244 /* Wait Queue Subordinate has waitqueue as first element */
1245 wait_result
= wait_queue_assert_wait((wait_queue_t
)uth
->uu_wqset
,
1246 NULL
, THREAD_ABORTSAFE
, sel
->abstime
);
1247 if (wait_result
!= THREAD_AWAKENED
) {
1248 /* there are no preposted events */
1249 error
= tsleep1(NULL
, PSOCK
| PCATCH
,
1250 "select", 0, selcontinue
);
1257 sel_pass
= SEL_SECONDPASS
;
1264 wait_subqueue_unlink_all(uth
->uu_wqset
);
1265 seldrop(p
, sel
->ibits
, uap
->nd
);
1267 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1268 /* select is not restarted after signals... */
1269 if (error
== ERESTART
)
1271 if (error
== EWOULDBLOCK
)
1273 nw
= howmany(uap
->nd
, NFDBITS
);
1274 ni
= nw
* sizeof(fd_mask
);
1276 #define putbits(name, x) \
1278 if (uap->name && (error2 = \
1279 copyout((caddr_t)&sel->obits[(x) * nw], uap->name, ni))) \
1298 * Parameters: p Process performing the select
1299 * sel The per-thread select context structure
1300 * nfd The number of file descriptors to scan
1301 * retval The per thread system call return area
1302 * sel_pass Which pass this is; allowed values are
1303 * SEL_FIRSTPASS and SEL_SECONDPASS
1304 * wqsub The per thread wait queue set
1306 * Returns: 0 Success
1307 * EIO Invalid p->p_fd field XXX Obsolete?
1308 * EBADF One of the files in the bit vector is
1312 selscan(struct proc
*p
, struct _select
*sel
, int nfd
, int32_t *retval
,
1313 int sel_pass
, wait_queue_sub_t wqsub
)
1315 struct filedesc
*fdp
= p
->p_fd
;
1318 struct fileproc
*fp
;
1319 int n
= 0; /* count of bits */
1320 int nc
= 0; /* bit vector offset (nc'th bit) */
1321 static int flag
[3] = { FREAD
, FWRITE
, 0 };
1322 u_int32_t
*iptr
, *optr
;
1324 u_int32_t
*ibits
, *obits
;
1328 struct vfs_context context
= *vfs_context_current();
1331 * Problems when reboot; due to MacOSX signal probs
1332 * in Beaker1C ; verify that the p->p_fd is valid
1342 nw
= howmany(nfd
, NFDBITS
);
1349 for (msk
= 0; msk
< 3; msk
++) {
1350 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1351 optr
= (u_int32_t
*)&obits
[msk
* nw
];
1353 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1354 bits
= iptr
[i
/NFDBITS
];
1356 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1358 fp
= fdp
->fd_ofiles
[fd
];
1360 if (fp
== NULL
|| (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1362 * If we abort because of a bad
1363 * fd, let the caller unwind...
1368 if (sel_pass
== SEL_SECONDPASS
) {
1369 wql_ptr
= (char *)0;
1370 if ((fp
->f_flags
& FP_INSELECT
) && (fp
->f_waddr
== (void *)wqsub
)) {
1371 fp
->f_flags
&= ~FP_INSELECT
;
1372 fp
->f_waddr
= (void *)0;
1375 wql_ptr
= (wql
+ nc
* SIZEOF_WAITQUEUE_LINK
);
1376 if (fp
->f_flags
& FP_INSELECT
) {
1377 /* someone is already in select on this fp */
1378 fp
->f_flags
|= FP_SELCONFLICT
;
1379 wait_queue_link(&select_conflict_queue
, (wait_queue_set_t
)wqsub
);
1381 fp
->f_flags
|= FP_INSELECT
;
1382 fp
->f_waddr
= (void *)wqsub
;
1386 context
.vc_ucred
= fp
->f_cred
;
1388 /* The select; set the bit, if true */
1390 && fo_select(fp
, flag
[msk
], wql_ptr
, &context
)) {
1391 optr
[fd
/NFDBITS
] |= (1 << (fd
% NFDBITS
));
1404 int poll_callback(struct kqueue
*, struct kevent64_s
*, void *);
1406 struct poll_continue_args
{
1407 user_addr_t pca_fds
;
1413 poll(struct proc
*p
, struct poll_args
*uap
, int32_t *retval
)
1415 __pthread_testcancel(1);
1416 return(poll_nocancel(p
, (struct poll_nocancel_args
*)uap
, retval
));
1421 poll_nocancel(struct proc
*p
, struct poll_nocancel_args
*uap
, int32_t *retval
)
1423 struct poll_continue_args
*cont
;
1427 int ncoll
, error
= 0;
1428 u_int nfds
= uap
->nfds
;
1434 * This is kinda bogus. We have fd limits, but that is not
1435 * really related to the size of the pollfd array. Make sure
1436 * we let the process use at least FD_SETSIZE entries and at
1437 * least enough for the current limits. We want to be reasonably
1438 * safe, but not overly restrictive.
1440 if (nfds
> OPEN_MAX
||
1441 (nfds
> p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
&& (proc_suser(p
) || nfds
> FD_SETSIZE
)))
1444 kq
= kqueue_alloc(p
);
1448 ni
= nfds
* sizeof(struct pollfd
) + sizeof(struct poll_continue_args
);
1449 MALLOC(cont
, struct poll_continue_args
*, ni
, M_TEMP
, M_WAITOK
);
1455 fds
= (struct pollfd
*)&cont
[1];
1456 error
= copyin(uap
->fds
, fds
, nfds
* sizeof(struct pollfd
));
1460 if (uap
->timeout
!= -1) {
1463 atv
.tv_sec
= uap
->timeout
/ 1000;
1464 atv
.tv_usec
= (uap
->timeout
% 1000) * 1000;
1465 if (itimerfix(&atv
)) {
1469 getmicrouptime(&rtv
);
1470 timevaladd(&atv
, &rtv
);
1476 /* JMM - all this P_SELECT stuff is bogus */
1478 OSBitOrAtomic(P_SELECT
, &p
->p_flag
);
1479 for (i
= 0; i
< nfds
; i
++) {
1480 short events
= fds
[i
].events
;
1481 struct kevent64_s kev
;
1484 /* per spec, ignore fd values below zero */
1485 if (fds
[i
].fd
< 0) {
1490 /* convert the poll event into a kqueue kevent */
1491 kev
.ident
= fds
[i
].fd
;
1492 kev
.flags
= EV_ADD
| EV_ONESHOT
| EV_POLL
;
1493 kev
.udata
= CAST_USER_ADDR_T(&fds
[i
]);
1499 /* Handle input events */
1500 if (events
& ( POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
| POLLHUP
)) {
1501 kev
.filter
= EVFILT_READ
;
1502 if (!(events
& ( POLLIN
| POLLRDNORM
)))
1503 kev
.flags
|= EV_OOBAND
;
1504 kerror
= kevent_register(kq
, &kev
, p
);
1507 /* Handle output events */
1509 events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
)) {
1510 kev
.filter
= EVFILT_WRITE
;
1511 kerror
= kevent_register(kq
, &kev
, p
);
1514 /* Handle BSD extension vnode events */
1516 events
& ( POLLEXTEND
| POLLATTRIB
| POLLNLINK
| POLLWRITE
)) {
1517 kev
.filter
= EVFILT_VNODE
;
1519 if (events
& POLLEXTEND
)
1520 kev
.fflags
|= NOTE_EXTEND
;
1521 if (events
& POLLATTRIB
)
1522 kev
.fflags
|= NOTE_ATTRIB
;
1523 if (events
& POLLNLINK
)
1524 kev
.fflags
|= NOTE_LINK
;
1525 if (events
& POLLWRITE
)
1526 kev
.fflags
|= NOTE_WRITE
;
1527 kerror
= kevent_register(kq
, &kev
, p
);
1531 fds
[i
].revents
= POLLNVAL
;
1537 /* Did we have any trouble registering? */
1541 /* scan for, and possibly wait for, the kevents to trigger */
1542 cont
->pca_fds
= uap
->fds
;
1543 cont
->pca_nfds
= nfds
;
1544 cont
->pca_rfds
= rfds
;
1545 error
= kqueue_scan(kq
, poll_callback
, NULL
, cont
, &atv
, p
);
1546 rfds
= cont
->pca_rfds
;
1549 OSBitAndAtomic(~((uint32_t)P_SELECT
), &p
->p_flag
);
1550 /* poll is not restarted after signals... */
1551 if (error
== ERESTART
)
1553 if (error
== EWOULDBLOCK
)
1556 error
= copyout(fds
, uap
->fds
, nfds
* sizeof(struct pollfd
));
1569 poll_callback(__unused
struct kqueue
*kq
, struct kevent64_s
*kevp
, void *data
)
1571 struct poll_continue_args
*cont
= (struct poll_continue_args
*)data
;
1572 struct pollfd
*fds
= CAST_DOWN(struct pollfd
*, kevp
->udata
);
1573 short prev_revents
= fds
->revents
;
1576 /* convert the results back into revents */
1577 if (kevp
->flags
& EV_EOF
)
1578 fds
->revents
|= POLLHUP
;
1579 if (kevp
->flags
& EV_ERROR
)
1580 fds
->revents
|= POLLERR
;
1582 switch (kevp
->filter
) {
1584 if (fds
->revents
& POLLHUP
)
1585 mask
= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
);
1588 if (kevp
->data
!= 0)
1589 mask
|= (POLLIN
| POLLRDNORM
);
1590 if (kevp
->flags
& EV_OOBAND
)
1591 mask
|= ( POLLPRI
| POLLRDBAND
);
1593 fds
->revents
|= (fds
->events
& mask
);
1597 if (!(fds
->revents
& POLLHUP
))
1598 fds
->revents
|= (fds
->events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
));
1602 if (kevp
->fflags
& NOTE_EXTEND
)
1603 fds
->revents
|= (fds
->events
& POLLEXTEND
);
1604 if (kevp
->fflags
& NOTE_ATTRIB
)
1605 fds
->revents
|= (fds
->events
& POLLATTRIB
);
1606 if (kevp
->fflags
& NOTE_LINK
)
1607 fds
->revents
|= (fds
->events
& POLLNLINK
);
1608 if (kevp
->fflags
& NOTE_WRITE
)
1609 fds
->revents
|= (fds
->events
& POLLWRITE
);
1613 if (fds
->revents
!= 0 && prev_revents
== 0)
1620 seltrue(__unused dev_t dev
, __unused
int flag
, __unused
struct proc
*p
)
1629 * Count the number of bits set in the input bit vector, and establish an
1630 * outstanding fp->f_iocount for each of the descriptors which will be in
1631 * use in the select operation.
1633 * Parameters: p The process doing the select
1634 * ibits The input bit vector
1635 * nfd The number of fd's in the vector
1636 * countp Pointer to where to store the bit count
1638 * Returns: 0 Success
1639 * EIO Bad per process open file table
1640 * EBADF One of the bits in the input bit vector
1641 * references an invalid fd
1643 * Implicit: *countp (modified) Count of fd's
1645 * Notes: This function is the first pass under the proc_fdlock() that
1646 * permits us to recognize invalid descriptors in the bit vector;
1647 * the may, however, not remain valid through the drop and
1648 * later reacquisition of the proc_fdlock().
1651 selcount(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int *countp
)
1653 struct filedesc
*fdp
= p
->p_fd
;
1656 struct fileproc
*fp
;
1662 int need_wakeup
= 0;
1665 * Problems when reboot; due to MacOSX signal probs
1666 * in Beaker1C ; verify that the p->p_fd is valid
1672 nw
= howmany(nfd
, NFDBITS
);
1675 for (msk
= 0; msk
< 3; msk
++) {
1676 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1677 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1678 bits
= iptr
[i
/NFDBITS
];
1679 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1681 fp
= fdp
->fd_ofiles
[fd
];
1683 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1703 /* Ignore error return; it's already EBADF */
1704 (void)seldrop_locked(p
, ibits
, nfd
, n
, &need_wakeup
, 1);
1709 wakeup(&p
->p_fpdrainwait
);
1718 * Drop outstanding wait queue references set up during selscan(); drop the
1719 * outstanding per fileproc f_iocount() picked up during the selcount().
1721 * Parameters: p Process performing the select
1722 * ibits Input pit bector of fd's
1723 * nfd Number of fd's
1724 * lim Limit to number of vector entries to
1725 * consider, or -1 for "all"
1727 * need_wakeup Pointer to flag to set to do a wakeup
1728 * if f_iocont on any descriptor goes to 0
1730 * Returns: 0 Success
1731 * EBADF One or more fds in the bit vector
1732 * were invalid, but the rest
1733 * were successfully dropped
1735 * Notes: An fd make become bad while the proc_fdlock() is not held,
1736 * if a multithreaded application closes the fd out from under
1737 * the in progress select. In this case, we still have to
1738 * clean up after the set up on the remaining fds.
1741 seldrop_locked(struct proc
*p
, u_int32_t
*ibits
, int nfd
, int lim
, int *need_wakeup
, int fromselcount
)
1743 struct filedesc
*fdp
= p
->p_fd
;
1746 struct fileproc
*fp
;
1751 uthread_t uth
= get_bsdthread_info(current_thread());
1756 * Problems when reboot; due to MacOSX signal probs
1757 * in Beaker1C ; verify that the p->p_fd is valid
1763 nw
= howmany(nfd
, NFDBITS
);
1765 for (msk
= 0; msk
< 3; msk
++) {
1766 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1767 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1768 bits
= iptr
[i
/NFDBITS
];
1769 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1771 fp
= fdp
->fd_ofiles
[fd
];
1773 * If we've already dropped as many as were
1774 * counted/scanned, then we are done.
1776 if ((fromselcount
!= 0) && (++dropcount
> lim
))
1780 /* skip (now) bad fds */
1785 * Only clear the flag if we set it. We'll
1786 * only find that we set it if we had made
1787 * at least one [partial] pass through selscan().
1789 if ((fp
->f_flags
& FP_INSELECT
) && (fp
->f_waddr
== (void *)uth
->uu_wqset
)) {
1790 fp
->f_flags
&= ~FP_INSELECT
;
1791 fp
->f_waddr
= (void *)0;
1795 if (fp
->f_iocount
< 0)
1796 panic("f_iocount overdecrement!");
1798 if (fp
->f_iocount
== 0) {
1800 * The last iocount is responsible for clearing
1801 * selconfict flag - even if we didn't set it -
1802 * and is also responsible for waking up anyone
1803 * waiting on iocounts to drain.
1805 if (fp
->f_flags
& FP_SELCONFLICT
)
1806 fp
->f_flags
&= ~FP_SELCONFLICT
;
1807 if (p
->p_fpdrainwait
) {
1808 p
->p_fpdrainwait
= 0;
1821 seldrop(struct proc
*p
, u_int32_t
*ibits
, int nfd
)
1824 int need_wakeup
= 0;
1827 error
= seldrop_locked(p
, ibits
, nfd
, nfd
, &need_wakeup
, 0);
1830 wakeup(&p
->p_fpdrainwait
);
1836 * Record a select request.
1839 selrecord(__unused
struct proc
*selector
, struct selinfo
*sip
, void * p_wql
)
1841 thread_t cur_act
= current_thread();
1842 struct uthread
* ut
= get_bsdthread_info(cur_act
);
1844 /* need to look at collisions */
1846 /*do not record if this is second pass of select */
1847 if(p_wql
== (void *)0) {
1851 if ((sip
->si_flags
& SI_INITED
) == 0) {
1852 wait_queue_init(&sip
->si_wait_queue
, SYNC_POLICY_FIFO
);
1853 sip
->si_flags
|= SI_INITED
;
1854 sip
->si_flags
&= ~SI_CLEAR
;
1857 if (sip
->si_flags
& SI_RECORDED
) {
1858 sip
->si_flags
|= SI_COLL
;
1860 sip
->si_flags
&= ~SI_COLL
;
1862 sip
->si_flags
|= SI_RECORDED
;
1863 if (!wait_queue_member(&sip
->si_wait_queue
, ut
->uu_wqset
))
1864 wait_queue_link_noalloc(&sip
->si_wait_queue
, ut
->uu_wqset
,
1865 (wait_queue_link_t
)p_wql
);
1871 selwakeup(struct selinfo
*sip
)
1874 if ((sip
->si_flags
& SI_INITED
) == 0) {
1878 if (sip
->si_flags
& SI_COLL
) {
1880 sip
->si_flags
&= ~SI_COLL
;
1882 /* will not support */
1883 //wakeup((caddr_t)&selwait);
1887 if (sip
->si_flags
& SI_RECORDED
) {
1888 wait_queue_wakeup_all(&sip
->si_wait_queue
, NULL
, THREAD_AWAKENED
);
1889 sip
->si_flags
&= ~SI_RECORDED
;
1895 selthreadclear(struct selinfo
*sip
)
1898 if ((sip
->si_flags
& SI_INITED
) == 0) {
1901 if (sip
->si_flags
& SI_RECORDED
) {
1903 sip
->si_flags
&= ~(SI_RECORDED
| SI_COLL
);
1905 sip
->si_flags
|= SI_CLEAR
;
1906 wait_queue_unlink_all(&sip
->si_wait_queue
);
1912 #define DBG_POST 0x10
1913 #define DBG_WATCH 0x11
1914 #define DBG_WAIT 0x12
1915 #define DBG_MOD 0x13
1916 #define DBG_EWAKEUP 0x14
1917 #define DBG_ENQUEUE 0x15
1918 #define DBG_DEQUEUE 0x16
1920 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
1921 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
1922 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
1923 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
1924 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
1925 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
1926 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
1929 #define EVPROCDEQUE(p, evq) do { \
1931 if (evq->ee_flags & EV_QUEUED) { \
1932 TAILQ_REMOVE(&p->p_evlist, evq, ee_plist); \
1933 evq->ee_flags &= ~EV_QUEUED; \
1940 * called upon socket close. deque and free all events for
1941 * the socket... socket must be locked by caller.
1944 evsofree(struct socket
*sp
)
1946 struct eventqelt
*evq
, *next
;
1952 for (evq
= sp
->so_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
1953 next
= evq
->ee_slist
.tqe_next
;
1956 if (evq
->ee_flags
& EV_QUEUED
) {
1957 EVPROCDEQUE(p
, evq
);
1959 TAILQ_REMOVE(&sp
->so_evlist
, evq
, ee_slist
); // remove from socket q
1966 * called upon pipe close. deque and free all events for
1967 * the pipe... pipe must be locked by caller
1970 evpipefree(struct pipe
*cpipe
)
1972 struct eventqelt
*evq
, *next
;
1975 for (evq
= cpipe
->pipe_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
1976 next
= evq
->ee_slist
.tqe_next
;
1979 EVPROCDEQUE(p
, evq
);
1981 TAILQ_REMOVE(&cpipe
->pipe_evlist
, evq
, ee_slist
); // remove from pipe q
1988 * enqueue this event if it's not already queued. wakeup
1989 * the proc if we do queue this event to it...
1990 * entered with proc lock held... we drop it before
1991 * doing the wakeup and return in that state
1994 evprocenque(struct eventqelt
*evq
)
2001 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_START
, (uint32_t)evq
, evq
->ee_flags
, evq
->ee_eventmask
,0,0);
2005 if (evq
->ee_flags
& EV_QUEUED
) {
2008 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
2011 evq
->ee_flags
|= EV_QUEUED
;
2013 TAILQ_INSERT_TAIL(&p
->p_evlist
, evq
, ee_plist
);
2017 wakeup(&p
->p_evlist
);
2019 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
2024 * pipe lock must be taken by the caller
2027 postpipeevent(struct pipe
*pipep
, int event
)
2030 struct eventqelt
*evq
;
2034 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, event
,0,0,1,0);
2036 for (evq
= pipep
->pipe_evlist
.tqh_first
;
2037 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2039 if (evq
->ee_eventmask
== 0)
2043 switch (event
& (EV_RWBYTES
| EV_RCLOSED
| EV_WCLOSED
)) {
2046 if ((evq
->ee_eventmask
& EV_RE
) && pipep
->pipe_buffer
.cnt
) {
2048 evq
->ee_req
.er_rcnt
= pipep
->pipe_buffer
.cnt
;
2050 if ((evq
->ee_eventmask
& EV_WR
) &&
2051 (MAX(pipep
->pipe_buffer
.size
,PIPE_SIZE
) - pipep
->pipe_buffer
.cnt
) >= PIPE_BUF
) {
2053 if (pipep
->pipe_state
& PIPE_EOF
) {
2054 mask
|= EV_WR
|EV_RESET
;
2058 evq
->ee_req
.er_wcnt
= MAX(pipep
->pipe_buffer
.size
, PIPE_SIZE
) - pipep
->pipe_buffer
.cnt
;
2064 if ((evq
->ee_eventmask
& EV_RE
)) {
2065 mask
|= EV_RE
|EV_RCLOSED
;
2067 if ((evq
->ee_eventmask
& EV_WR
)) {
2068 mask
|= EV_WR
|EV_WCLOSED
;
2077 * disarm... postevents are nops until this event is 'read' via
2078 * waitevent and then re-armed via modwatch
2080 evq
->ee_eventmask
= 0;
2083 * since events are disarmed until after the waitevent
2084 * the ee_req.er_xxxx fields can't change once we've
2085 * inserted this event into the proc queue...
2086 * therefore, the waitevent will see a 'consistent'
2087 * snapshot of the event, even though it won't hold
2088 * the pipe lock, and we're updating the event outside
2089 * of the proc lock, which it will hold
2091 evq
->ee_req
.er_eventbits
|= mask
;
2093 KERNEL_DEBUG(DBG_MISC_POST
, (uint32_t)evq
, evq
->ee_req
.er_eventbits
, mask
, 1,0);
2098 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, 0,0,0,1,0);
2103 * given either a sockbuf or a socket run down the
2104 * event list and queue ready events found...
2105 * the socket must be locked by the caller
2108 postevent(struct socket
*sp
, struct sockbuf
*sb
, int event
)
2111 struct eventqelt
*evq
;
2119 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, (int)sp
, event
, 0, 0, 0);
2121 for (evq
= sp
->so_evlist
.tqh_first
;
2122 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2124 if (evq
->ee_eventmask
== 0)
2128 /* ready for reading:
2129 - byte cnt >= receive low water mark
2130 - read-half of conn closed
2131 - conn pending for listening sock
2132 - socket error pending
2135 - byte cnt avail >= send low water mark
2136 - write half of conn closed
2137 - socket error pending
2138 - non-blocking conn completed successfully
2142 - sock at out of band mark
2145 switch (event
& EV_DMASK
) {
2148 if ((evq
->ee_eventmask
& EV_EX
)) {
2149 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
2150 mask
|= EV_EX
|EV_OOB
;
2154 case EV_RWBYTES
|EV_OOB
:
2155 if ((evq
->ee_eventmask
& EV_EX
)) {
2156 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
2157 mask
|= EV_EX
|EV_OOB
;
2160 * fall into the next case
2163 if ((evq
->ee_eventmask
& EV_RE
) && soreadable(sp
)) {
2165 if ((sp
->so_type
== SOCK_STREAM
) && ((sp
->so_error
== ECONNREFUSED
) || (sp
->so_error
== ECONNRESET
))) {
2166 if ((sp
->so_pcb
== 0) || (((struct inpcb
*)sp
->so_pcb
)->inp_state
== INPCB_STATE_DEAD
) || !(tp
= sototcpcb(sp
)) ||
2167 (tp
->t_state
== TCPS_CLOSED
)) {
2168 mask
|= EV_RE
|EV_RESET
;
2174 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
2176 if (sp
->so_state
& SS_CANTRCVMORE
) {
2181 if ((evq
->ee_eventmask
& EV_WR
) && sowriteable(sp
)) {
2183 if ((sp
->so_type
== SOCK_STREAM
) && ((sp
->so_error
== ECONNREFUSED
) || (sp
->so_error
== ECONNRESET
))) {
2184 if ((sp
->so_pcb
== 0) || (((struct inpcb
*)sp
->so_pcb
)->inp_state
== INPCB_STATE_DEAD
) || !(tp
= sototcpcb(sp
)) ||
2185 (tp
->t_state
== TCPS_CLOSED
)) {
2186 mask
|= EV_WR
|EV_RESET
;
2192 evq
->ee_req
.er_wcnt
= sbspace(&sp
->so_snd
);
2197 if ((evq
->ee_eventmask
& EV_RE
)) {
2198 mask
|= EV_RE
|EV_RCONN
;
2199 evq
->ee_req
.er_rcnt
= sp
->so_qlen
+ 1; // incl this one
2204 if ((evq
->ee_eventmask
& EV_WR
)) {
2205 mask
|= EV_WR
|EV_WCONN
;
2210 if ((evq
->ee_eventmask
& EV_RE
)) {
2211 mask
|= EV_RE
|EV_RCLOSED
;
2216 if ((evq
->ee_eventmask
& EV_WR
)) {
2217 mask
|= EV_WR
|EV_WCLOSED
;
2222 if (evq
->ee_eventmask
& EV_RE
) {
2223 mask
|= EV_RE
|EV_FIN
;
2229 if (evq
->ee_eventmask
& EV_RE
) {
2230 mask
|= EV_RE
| event
;
2232 if (evq
->ee_eventmask
& EV_WR
) {
2233 mask
|= EV_WR
| event
;
2238 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, -1, 0, 0, 0);
2242 KERNEL_DEBUG(DBG_MISC_POST
, (int)evq
, evq
->ee_eventmask
, evq
->ee_req
.er_eventbits
, mask
, 0);
2246 * disarm... postevents are nops until this event is 'read' via
2247 * waitevent and then re-armed via modwatch
2249 evq
->ee_eventmask
= 0;
2252 * since events are disarmed until after the waitevent
2253 * the ee_req.er_xxxx fields can't change once we've
2254 * inserted this event into the proc queue...
2255 * since waitevent can't see this event until we
2256 * enqueue it, waitevent will see a 'consistent'
2257 * snapshot of the event, even though it won't hold
2258 * the socket lock, and we're updating the event outside
2259 * of the proc lock, which it will hold
2261 evq
->ee_req
.er_eventbits
|= mask
;
2266 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, 0, 0, 0, 0);
2268 #endif /* SOCKETS */
2272 * watchevent system call. user passes us an event to watch
2273 * for. we malloc an event object, initialize it, and queue
2274 * it to the open socket. when the event occurs, postevent()
2275 * will enque it back to our proc where we can retrieve it
2278 * should this prevent duplicate events on same socket?
2281 * ENOMEM No memory for operation
2285 watchevent(proc_t p
, struct watchevent_args
*uap
, __unused
int *retval
)
2287 struct eventqelt
*evq
= (struct eventqelt
*)0;
2288 struct eventqelt
*np
= NULL
;
2289 struct eventreq64
*erp
;
2290 struct fileproc
*fp
= NULL
;
2293 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_START
, 0,0,0,0,0);
2295 // get a qelt and fill with users req
2296 MALLOC(evq
, struct eventqelt
*, sizeof(struct eventqelt
), M_TEMP
, M_WAITOK
);
2302 // get users request pkt
2304 if (IS_64BIT_PROCESS(p
)) {
2305 error
= copyin(uap
->u_req
, (caddr_t
)erp
, sizeof(struct eventreq64
));
2307 struct eventreq32 er32
;
2309 error
= copyin(uap
->u_req
, (caddr_t
)&er32
, sizeof(struct eventreq32
));
2312 * the user only passes in the
2313 * er_type, er_handle and er_data...
2314 * the other fields are initialized
2315 * below, so don't bother to copy
2317 erp
->er_type
= er32
.er_type
;
2318 erp
->er_handle
= er32
.er_handle
;
2319 erp
->er_data
= (user_addr_t
)er32
.er_data
;
2324 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2328 KERNEL_DEBUG(DBG_MISC_WATCH
, erp
->er_handle
,uap
->u_eventmask
,(uint32_t)evq
,0,0);
2330 // validate, freeing qelt if errors
2334 if (erp
->er_type
!= EV_FD
) {
2336 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2339 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2340 socket_lock((struct socket
*)fp
->f_data
, 1);
2341 np
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2342 #endif /* SOCKETS */
2343 } else if (fp
->f_type
== DTYPE_PIPE
) {
2344 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2345 np
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2347 fp_drop(p
, erp
->er_handle
, fp
, 1);
2355 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2360 * only allow one watch per file per proc
2362 for ( ; np
!= NULL
; np
= np
->ee_slist
.tqe_next
) {
2363 if (np
->ee_proc
== p
) {
2365 if (fp
->f_type
== DTYPE_SOCKET
)
2366 socket_unlock((struct socket
*)fp
->f_data
, 1);
2368 #endif /* SOCKETS */
2369 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2370 fp_drop(p
, erp
->er_handle
, fp
, 0);
2373 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2377 erp
->er_ecnt
= erp
->er_rcnt
= erp
->er_wcnt
= erp
->er_eventbits
= 0;
2379 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2383 if (fp
->f_type
== DTYPE_SOCKET
) {
2384 TAILQ_INSERT_TAIL(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2385 postevent((struct socket
*)fp
->f_data
, 0, EV_RWBYTES
); // catch existing events
2387 socket_unlock((struct socket
*)fp
->f_data
, 1);
2389 #endif /* SOCKETS */
2391 TAILQ_INSERT_TAIL(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2392 postpipeevent((struct pipe
*)fp
->f_data
, EV_RWBYTES
);
2394 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2396 fp_drop_event(p
, erp
->er_handle
, fp
);
2398 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, 0,0,0,0,0);
2405 * waitevent system call.
2406 * grabs the next waiting event for this proc and returns
2407 * it. if no events, user can request to sleep with timeout
2408 * or without or poll mode
2409 * ((tv != NULL && interval == 0) || tv == -1)
2412 waitevent(proc_t p
, struct waitevent_args
*uap
, int *retval
)
2415 struct eventqelt
*evq
;
2416 struct eventreq64
*erp
;
2417 uint64_t abstime
, interval
;
2418 boolean_t fast_poll
= FALSE
;
2420 struct eventreq64 er64
;
2421 struct eventreq32 er32
;
2429 * check for fast poll method
2431 if (IS_64BIT_PROCESS(p
)) {
2432 if (uap
->tv
== (user_addr_t
)-1)
2434 } else if (uap
->tv
== (user_addr_t
)((uint32_t)-1))
2437 if (fast_poll
== TRUE
) {
2438 if (p
->p_evlist
.tqh_first
== NULL
) {
2439 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_NONE
, -1,0,0,0,0);
2449 if (IS_64BIT_PROCESS(p
)) {
2450 struct user64_timeval atv64
;
2451 error
= copyin(uap
->tv
, (caddr_t
)&atv64
, sizeof(atv64
));
2452 /* Loses resolution - assume timeout < 68 years */
2453 atv
.tv_sec
= atv64
.tv_sec
;
2454 atv
.tv_usec
= atv64
.tv_usec
;
2456 struct user32_timeval atv32
;
2457 error
= copyin(uap
->tv
, (caddr_t
)&atv32
, sizeof(atv32
));
2458 atv
.tv_sec
= atv32
.tv_sec
;
2459 atv
.tv_usec
= atv32
.tv_usec
;
2464 if (itimerfix(&atv
)) {
2468 interval
= tvtoabstime(&atv
);
2470 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_START
, 0,0,0,0,0);
2474 if ((evq
= p
->p_evlist
.tqh_first
) != NULL
) {
2476 * found one... make a local copy while it's still on the queue
2477 * to prevent it from changing while in the midst of copying
2478 * don't want to hold the proc lock across a copyout because
2479 * it might block on a page fault at the target in user space
2483 if (IS_64BIT_PROCESS(p
))
2484 bcopy((caddr_t
)erp
, (caddr_t
)&uer
.er64
, sizeof (struct eventreq64
));
2486 uer
.er32
.er_type
= erp
->er_type
;
2487 uer
.er32
.er_handle
= erp
->er_handle
;
2488 uer
.er32
.er_data
= (uint32_t)erp
->er_data
;
2489 uer
.er32
.er_ecnt
= erp
->er_ecnt
;
2490 uer
.er32
.er_rcnt
= erp
->er_rcnt
;
2491 uer
.er32
.er_wcnt
= erp
->er_wcnt
;
2492 uer
.er32
.er_eventbits
= erp
->er_eventbits
;
2494 TAILQ_REMOVE(&p
->p_evlist
, evq
, ee_plist
);
2496 evq
->ee_flags
&= ~EV_QUEUED
;
2500 if (IS_64BIT_PROCESS(p
))
2501 error
= copyout((caddr_t
)&uer
.er64
, uap
->u_req
, sizeof(struct eventreq64
));
2503 error
= copyout((caddr_t
)&uer
.er32
, uap
->u_req
, sizeof(struct eventreq32
));
2505 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,
2506 evq
->ee_req
.er_handle
,evq
->ee_req
.er_eventbits
,(uint32_t)evq
,0);
2510 if (uap
->tv
&& interval
== 0) {
2512 *retval
= 1; // poll failed
2514 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,0,0,0,0);
2518 clock_absolutetime_interval_to_deadline(interval
, &abstime
);
2522 KERNEL_DEBUG(DBG_MISC_WAIT
, 1,(uint32_t)&p
->p_evlist
,0,0,0);
2524 error
= msleep1(&p
->p_evlist
, &p
->p_mlock
, (PSOCK
| PCATCH
), "waitevent", abstime
);
2526 KERNEL_DEBUG(DBG_MISC_WAIT
, 2,(uint32_t)&p
->p_evlist
,0,0,0);
2530 if (error
== ERESTART
)
2532 if (error
== EWOULDBLOCK
) {
2539 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, 0,0,0,0,0);
2545 * modwatch system call. user passes in event to modify.
2546 * if we find it we reset the event bits and que/deque event
2550 modwatch(proc_t p
, struct modwatch_args
*uap
, __unused
int *retval
)
2552 struct eventreq64 er
;
2553 struct eventreq64
*erp
= &er
;
2554 struct eventqelt
*evq
= NULL
; /* protected by error return */
2556 struct fileproc
*fp
;
2559 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_START
, 0,0,0,0,0);
2562 * get user's request pkt
2563 * just need the er_type and er_handle which sit above the
2564 * problematic er_data (32/64 issue)... so only copy in
2567 if ((error
= copyin(uap
->u_req
, (caddr_t
)erp
, sizeof(er
.er_type
) + sizeof(er
.er_handle
)))) {
2568 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2573 if (erp
->er_type
!= EV_FD
) {
2575 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2578 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2579 socket_lock((struct socket
*)fp
->f_data
, 1);
2580 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2581 #endif /* SOCKETS */
2582 } else if (fp
->f_type
== DTYPE_PIPE
) {
2583 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2584 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2586 fp_drop(p
, erp
->er_handle
, fp
, 1);
2592 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2596 if ((uap
->u_eventmask
== EV_RM
) && (fp
->f_flags
& FP_WAITEVENT
)) {
2597 fp
->f_flags
&= ~FP_WAITEVENT
;
2601 // locate event if possible
2602 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2603 if (evq
->ee_proc
== p
)
2608 if (fp
->f_type
== DTYPE_SOCKET
)
2609 socket_unlock((struct socket
*)fp
->f_data
, 1);
2611 #endif /* SOCKETS */
2612 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2613 fp_drop(p
, erp
->er_handle
, fp
, 0);
2614 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2617 KERNEL_DEBUG(DBG_MISC_MOD
, erp
->er_handle
,uap
->u_eventmask
,(uint32_t)evq
,0,0);
2619 if (uap
->u_eventmask
== EV_RM
) {
2620 EVPROCDEQUE(p
, evq
);
2623 if (fp
->f_type
== DTYPE_SOCKET
) {
2624 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2625 socket_unlock((struct socket
*)fp
->f_data
, 1);
2627 #endif /* SOCKETS */
2629 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2630 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2632 fp_drop(p
, erp
->er_handle
, fp
, 0);
2634 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, 0,0,0,0,0);
2637 switch (uap
->u_eventmask
& EV_MASK
) {
2655 case EV_EX
|EV_RE
|EV_WR
:
2656 flag
= EV_OOB
|EV_RWBYTES
;
2661 if (fp
->f_type
== DTYPE_SOCKET
)
2662 socket_unlock((struct socket
*)fp
->f_data
, 1);
2664 #endif /* SOCKETS */
2665 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2666 fp_drop(p
, erp
->er_handle
, fp
, 0);
2667 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2671 * since we're holding the socket/pipe lock, the event
2672 * cannot go from the unqueued state to the queued state
2673 * however, it can go from the queued state to the unqueued state
2674 * since that direction is protected by the proc_lock...
2675 * so do a quick check for EV_QUEUED w/o holding the proc lock
2676 * since by far the common case will be NOT EV_QUEUED, this saves
2677 * us taking the proc_lock the majority of the time
2679 if (evq
->ee_flags
& EV_QUEUED
) {
2681 * EVPROCDEQUE will recheck the state after it grabs the proc_lock
2683 EVPROCDEQUE(p
, evq
);
2686 * while the event is off the proc queue and
2687 * we're holding the socket/pipe lock
2688 * it's safe to update these fields...
2690 evq
->ee_req
.er_eventbits
= 0;
2691 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2694 if (fp
->f_type
== DTYPE_SOCKET
) {
2695 postevent((struct socket
*)fp
->f_data
, 0, flag
);
2696 socket_unlock((struct socket
*)fp
->f_data
, 1);
2698 #endif /* SOCKETS */
2700 postpipeevent((struct pipe
*)fp
->f_data
, flag
);
2701 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2703 fp_drop(p
, erp
->er_handle
, fp
, 0);
2704 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, evq
->ee_req
.er_handle
,evq
->ee_eventmask
,(uint32_t)fp
->f_data
,flag
,0);
2708 /* this routine is called from the close of fd with proc_fdlock held */
2710 waitevent_close(struct proc
*p
, struct fileproc
*fp
)
2712 struct eventqelt
*evq
;
2715 fp
->f_flags
&= ~FP_WAITEVENT
;
2718 if (fp
->f_type
== DTYPE_SOCKET
) {
2719 socket_lock((struct socket
*)fp
->f_data
, 1);
2720 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2722 #endif /* SOCKETS */
2723 if (fp
->f_type
== DTYPE_PIPE
) {
2724 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2725 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2733 // locate event if possible
2734 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2735 if (evq
->ee_proc
== p
)
2740 if (fp
->f_type
== DTYPE_SOCKET
)
2741 socket_unlock((struct socket
*)fp
->f_data
, 1);
2743 #endif /* SOCKETS */
2744 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2750 EVPROCDEQUE(p
, evq
);
2753 if (fp
->f_type
== DTYPE_SOCKET
) {
2754 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2755 socket_unlock((struct socket
*)fp
->f_data
, 1);
2757 #endif /* SOCKETS */
2759 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2760 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2773 * Description: Get the host UUID from IOKit and return it to user space.
2775 * Parameters: uuid_buf Pointer to buffer to receive UUID
2776 * timeout Timespec for timout
2778 * Returns: 0 Success
2779 * EWOULDBLOCK Timeout is too short
2780 * copyout:EFAULT Bad user buffer
2782 * Notes: A timeout seems redundant, since if it's tolerable to not
2783 * have a system UUID in hand, then why ask for one?
2786 gethostuuid(struct proc
*p
, struct gethostuuid_args
*uap
, __unused
int32_t *retval
)
2790 mach_timespec_t mach_ts
; /* for IOKit call */
2791 __darwin_uuid_t uuid_kern
; /* for IOKit call */
2793 /* Convert the 32/64 bit timespec into a mach_timespec_t */
2794 if ( proc_is64bit(p
) ) {
2795 struct user64_timespec ts
;
2796 error
= copyin(uap
->timeoutp
, &ts
, sizeof(ts
));
2799 mach_ts
.tv_sec
= ts
.tv_sec
;
2800 mach_ts
.tv_nsec
= ts
.tv_nsec
;
2802 struct user32_timespec ts
;
2803 error
= copyin(uap
->timeoutp
, &ts
, sizeof(ts
) );
2806 mach_ts
.tv_sec
= ts
.tv_sec
;
2807 mach_ts
.tv_nsec
= ts
.tv_nsec
;
2810 /* Call IOKit with the stack buffer to get the UUID */
2811 kret
= IOBSDGetPlatformUUID(uuid_kern
, mach_ts
);
2814 * If we get it, copy out the data to the user buffer; note that a
2815 * uuid_t is an array of characters, so this is size invariant for
2818 if (kret
== KERN_SUCCESS
) {
2819 error
= copyout(uuid_kern
, uap
->uuid_buf
, sizeof(uuid_kern
));
2821 error
= EWOULDBLOCK
;
2830 * Description: Omnibus system call for ledger operations
2833 ledger(struct proc
*p
, struct ledger_args
*args
, __unused
int32_t *retval
)
2835 int rval
, pid
, len
, error
;
2837 struct ledger_limit_args lla
;
2842 /* Finish copying in the necessary args before taking the proc lock */
2845 if (args
->cmd
== LEDGER_ENTRY_INFO
)
2846 error
= copyin(args
->arg3
, (char *)&len
, sizeof (len
));
2847 else if (args
->cmd
== LEDGER_TEMPLATE_INFO
)
2848 error
= copyin(args
->arg2
, (char *)&len
, sizeof (len
));
2850 else if (args
->cmd
== LEDGER_LIMIT
)
2851 error
= copyin(args
->arg2
, (char *)&lla
, sizeof (lla
));
2859 if (args
->cmd
!= LEDGER_TEMPLATE_INFO
) {
2861 proc
= proc_find(pid
);
2866 error
= mac_proc_check_ledger(p
, proc
, args
->cmd
);
2876 switch (args
->cmd
) {
2878 case LEDGER_LIMIT
: {
2881 rval
= ledger_limit(task
, &lla
);
2887 struct ledger_info info
;
2889 rval
= ledger_info(task
, &info
);
2892 rval
= copyout(&info
, args
->arg2
,
2897 case LEDGER_ENTRY_INFO
: {
2901 rval
= ledger_entry_info(task
, &buf
, &len
);
2903 if ((rval
== 0) && (len
> 0)) {
2904 sz
= len
* sizeof (struct ledger_entry_info
);
2905 rval
= copyout(buf
, args
->arg2
, sz
);
2909 rval
= copyout(&len
, args
->arg3
, sizeof (len
));
2913 case LEDGER_TEMPLATE_INFO
: {
2917 rval
= ledger_template_info(&buf
, &len
);
2918 if ((rval
== 0) && (len
> 0)) {
2919 sz
= len
* sizeof (struct ledger_template_info
);
2920 rval
= copyout(buf
, args
->arg1
, sz
);
2924 rval
= copyout(&len
, args
->arg2
, sizeof (len
));