2 * Copyright (c) 2000-2004 Apple Computer, 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 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/filedesc.h>
72 #include <sys/ioctl.h>
73 #include <sys/file_internal.h>
74 #include <sys/proc_internal.h>
75 #include <sys/socketvar.h>
77 #include <sys/uio_internal.h>
81 #include <sys/kernel.h>
83 #include <sys/malloc.h>
84 #include <sys/sysproto.h>
86 #include <sys/mount_internal.h>
87 #include <sys/protosw.h>
90 #include <sys/kdebug.h>
92 #include <sys/event.h>
93 #include <sys/eventvar.h>
95 #include <mach/mach_types.h>
96 #include <kern/kern_types.h>
97 #include <kern/assert.h>
98 #include <kern/kalloc.h>
99 #include <kern/thread.h>
100 #include <kern/clock.h>
102 #include <sys/mbuf.h>
103 #include <sys/socket.h>
104 #include <sys/socketvar.h>
105 #include <sys/errno.h>
106 #include <sys/syscall.h>
107 #include <sys/pipe.h>
109 #include <bsm/audit_kernel.h>
112 #include <net/route.h>
114 #include <netinet/in.h>
115 #include <netinet/in_systm.h>
116 #include <netinet/ip.h>
117 #include <netinet/in_pcb.h>
118 #include <netinet/ip_var.h>
119 #include <netinet/ip6.h>
120 #include <netinet/tcp.h>
121 #include <netinet/tcp_fsm.h>
122 #include <netinet/tcp_seq.h>
123 #include <netinet/tcp_timer.h>
124 #include <netinet/tcp_var.h>
125 #include <netinet/tcpip.h>
126 #include <netinet/tcp_debug.h>
127 /* for wait queue based select */
128 #include <kern/wait_queue.h>
129 #include <kern/kalloc.h>
131 #include <sys/ktrace.h>
133 #include <sys/vnode_internal.h>
135 int rd_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
);
136 int wr_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
);
137 extern void *get_bsduthreadarg(thread_t
);
138 extern int *get_bsduthreadrval(thread_t
);
140 __private_extern__
int dofileread(struct proc
*p
, struct fileproc
*fp
, int fd
,
141 user_addr_t bufp
, user_size_t nbyte
,
142 off_t offset
, int flags
, user_ssize_t
*retval
);
143 __private_extern__
int dofilewrite(struct proc
*p
, struct fileproc
*fp
, int fd
,
144 user_addr_t bufp
, user_size_t nbyte
,
145 off_t offset
, int flags
, user_ssize_t
*retval
);
146 __private_extern__
int preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_vnode
);
147 __private_extern__
void donefileread(struct proc
*p
, struct fileproc
*fp_ret
, int fd
);
150 extern int appletalk_inited
;
153 #define f_flag f_fglob->fg_flag
154 #define f_type f_fglob->fg_type
155 #define f_msgcount f_fglob->fg_msgcount
156 #define f_cred f_fglob->fg_cred
157 #define f_ops f_fglob->fg_ops
158 #define f_offset f_fglob->fg_offset
159 #define f_data f_fglob->fg_data
166 register struct read_args
*uap
;
167 user_ssize_t
*retval
;
173 if ( (error
= preparefileread(p
, &fp
, fd
, 0)) )
176 error
= dofileread(p
, fp
, uap
->fd
, uap
->cbuf
, uap
->nbyte
,
177 (off_t
)-1, 0, retval
);
179 donefileread(p
, fp
, fd
);
188 pread(p
, uap
, retval
)
190 register struct pread_args
*uap
;
191 user_ssize_t
*retval
;
197 if ( (error
= preparefileread(p
, &fp
, fd
, 1)) )
200 error
= dofileread(p
, fp
, uap
->fd
, uap
->buf
, uap
->nbyte
,
201 uap
->offset
, FOF_OFFSET
, retval
);
203 donefileread(p
, fp
, fd
);
206 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pread
) | DBG_FUNC_NONE
),
207 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
213 * Code common for read and pread
217 donefileread(struct proc
*p
, struct fileproc
*fp
, int fd
)
221 fp
->f_flags
&= ~FP_INCHRREAD
;
223 fp_drop(p
, fd
, fp
, 1);
228 preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_pread
)
236 error
= fp_lookup(p
, fd
, &fp
, 1);
242 if ((fp
->f_flag
& FREAD
) == 0) {
246 if (check_for_pread
&& (fp
->f_type
!= DTYPE_VNODE
)) {
250 if (fp
->f_type
== DTYPE_VNODE
) {
251 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
253 if (vp
->v_type
== VCHR
)
254 fp
->f_flags
|= FP_INCHRREAD
;
263 fp_drop(p
, fd
, fp
, 1);
269 __private_extern__
int
270 dofileread(p
, fp
, fd
, bufp
, nbyte
, offset
, flags
, retval
)
277 user_ssize_t
*retval
;
280 user_ssize_t bytecnt
;
282 char uio_buf
[ UIO_SIZEOF(1) ];
285 char ktr_uio_buf
[ UIO_SIZEOF(1) ];
289 // LP64todo - do we want to raise this?
293 if (IS_64BIT_PROCESS(p
)) {
294 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_READ
,
295 &uio_buf
[0], sizeof(uio_buf
));
297 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_READ
,
298 &uio_buf
[0], sizeof(uio_buf
));
300 uio_addiov(auio
, bufp
, nbyte
);
304 * if tracing, save a copy of iovec
306 if (KTRPOINT(p
, KTR_GENIO
)) {
309 if (IS_64BIT_PROCESS(p
)) {
310 ktruio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_READ
,
311 &ktr_uio_buf
[0], sizeof(ktr_uio_buf
));
313 ktruio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_READ
,
314 &ktr_uio_buf
[0], sizeof(ktr_uio_buf
));
316 uio_addiov(ktruio
, bufp
, nbyte
);
321 if ((error
= fo_read(fp
, auio
, fp
->f_cred
, flags
, p
))) {
322 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
323 error
== EINTR
|| error
== EWOULDBLOCK
))
326 bytecnt
-= uio_resid(auio
);
328 if (didktr
&& error
== 0) {
329 uio_setresid(ktruio
, bytecnt
);
330 ktrgenio(p
->p_tracep
, fd
, UIO_READ
, ktruio
, error
);
340 * Scatter read system call.
343 readv(p
, uap
, retval
)
345 register struct readv_args
*uap
;
346 user_ssize_t
*retval
;
351 struct user_iovec
*iovp
;
353 /* Verify range bedfore calling uio_create() */
354 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
357 /* allocate a uio large enough to hold the number of iovecs passed */
358 auio
= uio_create(uap
->iovcnt
, 0,
359 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
362 /* get location of iovecs within the uio. then copyin the iovecs from
365 iovp
= uio_iovsaddr(auio
);
368 goto ExitThisRoutine
;
370 size_of_iovec
= (IS_64BIT_PROCESS(p
) ? sizeof(struct user_iovec
) : sizeof(struct iovec
));
371 error
= copyin(uap
->iovp
, (caddr_t
)iovp
, (uap
->iovcnt
* size_of_iovec
));
373 goto ExitThisRoutine
;
376 /* finalize uio_t for use and do the IO
378 uio_calculateresid(auio
);
379 error
= rd_uio(p
, uap
->fd
, auio
, retval
);
392 write(p
, uap
, retval
)
394 register struct write_args
*uap
;
395 user_ssize_t
*retval
;
401 error
= fp_lookup(p
,fd
,&fp
,0);
404 if ((fp
->f_flag
& FWRITE
) == 0) {
407 error
= dofilewrite(p
, fp
, uap
->fd
, uap
->cbuf
, uap
->nbyte
,
408 (off_t
)-1, 0, retval
);
411 fp_drop_written(p
, fd
, fp
);
413 fp_drop(p
, fd
, fp
, 0);
421 pwrite(p
, uap
, retval
)
423 register struct pwrite_args
*uap
;
424 user_ssize_t
*retval
;
430 error
= fp_lookup(p
,fd
,&fp
,0);
434 if ((fp
->f_flag
& FWRITE
) == 0) {
437 if (fp
->f_type
!= DTYPE_VNODE
) {
440 error
= dofilewrite(p
, fp
, uap
->fd
, uap
->buf
, uap
->nbyte
,
441 uap
->offset
, FOF_OFFSET
, retval
);
445 fp_drop_written(p
, fd
, fp
);
447 fp_drop(p
, fd
, fp
, 0);
450 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pwrite
) | DBG_FUNC_NONE
),
451 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
456 __private_extern__
int
457 dofilewrite(p
, fp
, fd
, bufp
, nbyte
, offset
, flags
, retval
)
464 user_ssize_t
*retval
;
468 user_ssize_t bytecnt
;
469 char uio_buf
[ UIO_SIZEOF(1) ];
473 char ktr_uio_buf
[ UIO_SIZEOF(1) ];
476 // LP64todo - do we want to raise this?
480 if (IS_64BIT_PROCESS(p
)) {
481 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_WRITE
,
482 &uio_buf
[0], sizeof(uio_buf
));
484 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_WRITE
,
485 &uio_buf
[0], sizeof(uio_buf
));
487 uio_addiov(auio
, bufp
, nbyte
);
491 * if tracing, save a copy of iovec and uio
493 if (KTRPOINT(p
, KTR_GENIO
)) {
496 if (IS_64BIT_PROCESS(p
)) {
497 ktruio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_WRITE
,
498 &ktr_uio_buf
[0], sizeof(ktr_uio_buf
));
500 ktruio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_WRITE
,
501 &ktr_uio_buf
[0], sizeof(ktr_uio_buf
));
503 uio_addiov(ktruio
, bufp
, nbyte
);
507 if ((error
= fo_write(fp
, auio
, fp
->f_cred
, flags
, p
))) {
508 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
509 error
== EINTR
|| error
== EWOULDBLOCK
))
511 /* The socket layer handles SIGPIPE */
512 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
)
515 bytecnt
-= uio_resid(auio
);
517 if (didktr
&& error
== 0) {
518 uio_setresid(ktruio
, bytecnt
);
519 ktrgenio(p
->p_tracep
, fd
, UIO_WRITE
, ktruio
, error
);
528 * Gather write system call
531 writev(p
, uap
, retval
)
533 register struct writev_args
*uap
;
534 user_ssize_t
*retval
;
539 struct user_iovec
*iovp
;
541 /* Verify range bedfore calling uio_create() */
542 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
545 /* allocate a uio large enough to hold the number of iovecs passed */
546 auio
= uio_create(uap
->iovcnt
, 0,
547 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
550 /* get location of iovecs within the uio. then copyin the iovecs from
553 iovp
= uio_iovsaddr(auio
);
556 goto ExitThisRoutine
;
558 size_of_iovec
= (IS_64BIT_PROCESS(p
) ? sizeof(struct user_iovec
) : sizeof(struct iovec
));
559 error
= copyin(uap
->iovp
, (caddr_t
)iovp
, (uap
->iovcnt
* size_of_iovec
));
561 goto ExitThisRoutine
;
564 /* finalize uio_t for use and do the IO
566 uio_calculateresid(auio
);
567 error
= wr_uio(p
, uap
->fd
, auio
, retval
);
578 wr_uio(p
, fdes
, uio
, retval
)
582 user_ssize_t
*retval
;
588 struct iovec_64
*ktriov
= NULL
;
594 error
= fp_lookup(p
,fdes
,&fp
,0);
598 if ((fp
->f_flag
& FWRITE
) == 0) {
602 count
= uio_resid(uio
);
605 * if tracing, save a copy of iovec
607 if (KTRPOINT(p
, KTR_GENIO
)) {
608 iovlen
= uio
->uio_iovcnt
*
609 (IS_64BIT_PROCESS(p
) ? sizeof (struct iovec_64
) : sizeof (struct iovec_32
));
610 MALLOC(ktriov
, struct iovec_64
*, iovlen
, M_TEMP
, M_WAITOK
);
611 if (ktriov
!= NULL
) {
612 bcopy((caddr_t
)uio
->uio_iovs
.iov64p
, (caddr_t
)ktriov
, iovlen
);
618 error
= fo_write(fp
, uio
, fp
->f_cred
, 0, p
);
620 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
621 error
== EINTR
|| error
== EWOULDBLOCK
))
623 /* The socket layer handles SIGPIPE */
624 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
)
627 *retval
= count
- uio_resid(uio
);
632 ktruio
.uio_iovs
.iov64p
= ktriov
;
633 uio_setresid(&ktruio
, *retval
);
634 ktrgenio(p
->p_tracep
, fdes
, UIO_WRITE
, &ktruio
, error
);
636 FREE(ktriov
, M_TEMP
);
642 fp_drop_written(p
, fdes
, fp
);
644 fp_drop(p
, fdes
, fp
, 0);
650 rd_uio(p
, fdes
, uio
, retval
)
654 user_ssize_t
*retval
;
660 struct iovec_64
*ktriov
= NULL
;
666 if ( (error
= preparefileread(p
, &fp
, fdes
, 0)) )
669 count
= uio_resid(uio
);
672 * if tracing, save a copy of iovec
674 if (KTRPOINT(p
, KTR_GENIO
)) {
675 iovlen
= uio
->uio_iovcnt
*
676 (IS_64BIT_PROCESS(p
) ? sizeof (struct iovec_64
) : sizeof (struct iovec_32
));
677 MALLOC(ktriov
, struct iovec_64
*, iovlen
, M_TEMP
, M_WAITOK
);
678 if (ktriov
!= NULL
) {
679 bcopy((caddr_t
)uio
->uio_iovs
.iov64p
, (caddr_t
)ktriov
, iovlen
);
685 error
= fo_read(fp
, uio
, fp
->f_cred
, 0, p
);
688 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
689 error
== EINTR
|| error
== EWOULDBLOCK
))
692 *retval
= count
- uio_resid(uio
);
697 ktruio
.uio_iovs
.iov64p
= ktriov
;
698 uio_setresid(&ktruio
, *retval
);
699 ktrgenio(p
->p_tracep
, fdes
, UIO_READ
, &ktruio
, error
);
701 FREE(ktriov
, M_TEMP
);
704 donefileread(p
, fp
, fdes
);
714 ioctl(struct proc
*p
, register struct ioctl_args
*uap
, __unused register_t
*retval
)
723 #define STK_PARAMS 128
724 char stkbuf
[STK_PARAMS
];
727 AUDIT_ARG(fd
, uap
->fd
);
728 AUDIT_ARG(cmd
, CAST_DOWN(int, uap
->com
)); /* LP64todo: uap->com is a user-land long */
729 AUDIT_ARG(addr
, uap
->data
);
731 is64bit
= proc_is64bit(p
);
734 error
= fp_lookup(p
,fd
,&fp
,1);
740 AUDIT_ARG(file
, p
, fp
);
742 if ((fp
->f_flag
& (FREAD
| FWRITE
)) == 0) {
749 * ### LD 6/11/97 Hack Alert: this is to get AppleTalk to work
750 * while implementing an ATioctl system call
753 if (appletalk_inited
&& ((uap
->com
& 0x0000FFFF) == 0xff99)) {
754 u_long fixed_command
;
755 #ifdef APPLETALK_DEBUG
756 kprintf("ioctl: special AppleTalk \n");
759 *(user_addr_t
*)datap
= uap
->data
;
760 fixed_command
= _IOW(0, 0xff99, uap
->data
);
761 error
= fo_ioctl(fp
, fixed_command
, datap
, p
);
769 switch (com
= uap
->com
) {
771 *fdflags(p
, uap
->fd
) &= ~UF_EXCLOSE
;
775 *fdflags(p
, uap
->fd
) |= UF_EXCLOSE
;
781 * Interpret high order word to find amount of data to be
782 * copied to/from the user's address space.
784 size
= IOCPARM_LEN(com
);
785 if (size
> IOCPARM_MAX
) {
790 if (size
> sizeof (stkbuf
)) {
792 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
804 error
= copyin(uap
->data
, datap
, size
);
813 /* XXX - IOC_IN and no size? we should proably return an error here!! */
815 *(user_addr_t
*)datap
= uap
->data
;
818 *(uint32_t *)datap
= (uint32_t)uap
->data
;
821 } else if ((com
&IOC_OUT
) && size
)
823 * Zero the buffer so the user always
824 * gets back something deterministic.
827 else if (com
&IOC_VOID
) {
828 /* XXX - this is odd since IOC_VOID means no parameters */
830 *(user_addr_t
*)datap
= uap
->data
;
833 *(uint32_t *)datap
= (uint32_t)uap
->data
;
840 if ( (tmp
= *(int *)datap
) )
841 fp
->f_flag
|= FNONBLOCK
;
843 fp
->f_flag
&= ~FNONBLOCK
;
844 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
848 if ( (tmp
= *(int *)datap
) )
849 fp
->f_flag
|= FASYNC
;
851 fp
->f_flag
&= ~FASYNC
;
852 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, p
);
857 if (fp
->f_type
== DTYPE_SOCKET
) {
858 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
862 if (fp
->f_type
== DTYPE_PIPE
) {
863 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, p
);
869 struct proc
*p1
= pfind(tmp
);
874 tmp
= p1
->p_pgrp
->pg_id
;
876 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, p
);
880 if (fp
->f_type
== DTYPE_SOCKET
) {
882 *(int *)datap
= ((struct socket
*)fp
->f_data
)->so_pgid
;
885 error
= fo_ioctl(fp
, TIOCGPGRP
, datap
, p
);
886 *(int *)datap
= -*(int *)datap
;
890 error
= fo_ioctl(fp
, com
, datap
, p
);
892 * Copy any data to user, size was
893 * already set and checked above.
895 if (error
== 0 && (com
&IOC_OUT
) && size
)
896 error
= copyout(datap
, uap
->data
, (u_int
)size
);
904 fp_drop(p
, fd
, fp
, 1);
909 int selwait
, nselcoll
;
910 #define SEL_FIRSTPASS 1
911 #define SEL_SECONDPASS 2
912 extern int selcontinue(int error
);
913 extern int selprocess(int error
, int sel_pass
);
914 static int selscan(struct proc
*p
, struct _select
* sel
,
915 int nfd
, register_t
*retval
, int sel_pass
, wait_queue_sub_t wqsub
);
916 static int selcount(struct proc
*p
, u_int32_t
*ibits
, u_int32_t
*obits
,
917 int nfd
, int * count
);
918 static int seldrop(struct proc
*p
, u_int32_t
*ibits
, int nfd
);
919 extern uint64_t tvtoabstime(struct timeval
*tvp
);
922 * Select system call.
925 select(struct proc
*p
, struct select_args
*uap
, register_t
*retval
)
932 int needzerofill
= 1;
935 th_act
= current_thread();
936 uth
= get_bsdthread_info(th_act
);
937 sel
= &uth
->uu_select
;
938 retval
= (int *)get_bsduthreadrval(th_act
);
945 if (uap
->nd
> p
->p_fd
->fd_nfiles
)
946 uap
->nd
= p
->p_fd
->fd_nfiles
; /* forgiving; slightly wrong */
948 nw
= howmany(uap
->nd
, NFDBITS
);
949 ni
= nw
* sizeof(fd_mask
);
952 * if this is the first select by the thread
953 * allocate the space for bits.
955 if (sel
->nbytes
== 0) {
956 sel
->nbytes
= 3 * ni
;
957 MALLOC(sel
->ibits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
958 MALLOC(sel
->obits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
959 if ((sel
->ibits
== NULL
) || (sel
->obits
== NULL
))
960 panic("select out of memory");
965 * if the previously allocated space for the bits
966 * is smaller than what is requested. Reallocate.
968 if (sel
->nbytes
< (3 * ni
)) {
969 sel
->nbytes
= (3 * ni
);
970 FREE(sel
->ibits
, M_TEMP
);
971 FREE(sel
->obits
, M_TEMP
);
972 MALLOC(sel
->ibits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
973 MALLOC(sel
->obits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
974 if ((sel
->ibits
== NULL
) || (sel
->obits
== NULL
))
975 panic("select out of memory");
980 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
981 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
985 * get the bits from the user address space
987 #define getbits(name, x) \
989 if (uap->name && (error = copyin(uap->name, \
990 (caddr_t)&sel->ibits[(x) * nw], ni))) \
1001 if (IS_64BIT_PROCESS(p
)) {
1002 struct user_timeval atv64
;
1003 error
= copyin(uap
->tv
, (caddr_t
)&atv64
, sizeof(atv64
));
1004 /* Loses resolution - assume timeout < 68 years */
1005 atv
.tv_sec
= atv64
.tv_sec
;
1006 atv
.tv_usec
= atv64
.tv_usec
;
1008 error
= copyin(uap
->tv
, (caddr_t
)&atv
, sizeof(atv
));
1012 if (itimerfix(&atv
)) {
1017 clock_absolutetime_interval_to_deadline(
1018 tvtoabstime(&atv
), &sel
->abstime
);
1023 if ( (error
= selcount(p
, sel
->ibits
, sel
->obits
, uap
->nd
, &count
)) ) {
1028 size
= SIZEOF_WAITQUEUE_SET
+ (count
* SIZEOF_WAITQUEUE_LINK
);
1029 if (sel
->allocsize
) {
1030 if (sel
->wqset
== 0)
1031 panic("select: wql memory smashed");
1032 /* needed for the select now */
1033 if (size
> sel
->allocsize
) {
1034 kfree(sel
->wqset
, sel
->allocsize
);
1035 sel
->allocsize
= size
;
1036 sel
->wqset
= (wait_queue_set_t
)kalloc(size
);
1037 if (sel
->wqset
== (wait_queue_set_t
)NULL
)
1038 panic("failed to allocate memory for waitqueue\n");
1042 sel
->allocsize
= size
;
1043 sel
->wqset
= (wait_queue_set_t
)kalloc(sel
->allocsize
);
1044 if (sel
->wqset
== (wait_queue_set_t
)NULL
)
1045 panic("failed to allocate memory for waitqueue\n");
1047 bzero(sel
->wqset
, size
);
1048 sel
->wql
= (char *)sel
->wqset
+ SIZEOF_WAITQUEUE_SET
;
1049 wait_queue_set_init(sel
->wqset
, (SYNC_POLICY_FIFO
| SYNC_POLICY_PREPOST
));
1052 return selprocess(error
, SEL_FIRSTPASS
);
1056 selcontinue(int error
)
1058 return selprocess(error
, SEL_SECONDPASS
);
1062 selprocess(int error
, int sel_pass
)
1067 struct uthread
*uth
;
1069 struct select_args
*uap
;
1071 struct _select
*sel
;
1076 wait_result_t wait_result
;
1079 th_act
= current_thread();
1080 uap
= (struct select_args
*)get_bsduthreadarg(th_act
);
1081 retval
= (int *)get_bsduthreadrval(th_act
);
1082 uth
= get_bsdthread_info(th_act
);
1083 sel
= &uth
->uu_select
;
1085 /* if it is first pass wait queue is not setup yet */
1086 if ((error
!= 0) && (sel_pass
== SEL_FIRSTPASS
))
1088 if (sel
->count
== 0)
1096 p
->p_flag
|= P_SELECT
;
1097 /* skip scans if the select is just for timeouts */
1099 if (sel_pass
== SEL_FIRSTPASS
)
1100 wait_queue_sub_clearrefs(sel
->wqset
);
1102 error
= selscan(p
, sel
, uap
->nd
, retval
, sel_pass
, sel
->wqset
);
1103 if (error
|| *retval
) {
1107 /* if the select of log, then we canwakeup and discover some one
1108 * else already read the data; go toselct again if time permits
1122 clock_get_uptime(&now
);
1123 if (now
>= sel
->abstime
)
1128 /* cleanup obits and try again */
1130 sel_pass
= SEL_FIRSTPASS
;
1135 * To effect a poll, the timeout argument should be
1136 * non-nil, pointing to a zero-valued timeval structure.
1138 if (uap
->tv
&& sel
->abstime
== 0) {
1142 /* No spurious wakeups due to colls,no need to check for them */
1143 if ((sel_pass
== SEL_SECONDPASS
) || ((p
->p_flag
& P_SELECT
) == 0)) {
1144 sel_pass
= SEL_FIRSTPASS
;
1148 p
->p_flag
&= ~P_SELECT
;
1150 /* if the select is just for timeout skip check */
1151 if (sel
->count
&&(sel_pass
== SEL_SECONDPASS
))
1152 panic("selprocess: 2nd pass assertwaiting");
1154 /* Wait Queue Subordinate has waitqueue as first element */
1155 wait_result
= wait_queue_assert_wait((wait_queue_t
)sel
->wqset
,
1156 &selwait
, THREAD_ABORTSAFE
, sel
->abstime
);
1157 if (wait_result
!= THREAD_AWAKENED
) {
1158 /* there are no preposted events */
1159 error
= tsleep1(NULL
, PSOCK
| PCATCH
,
1160 "select", 0, selcontinue
);
1166 sel_pass
= SEL_SECONDPASS
;
1174 wait_subqueue_unlink_all(sel
->wqset
);
1175 seldrop(p
, sel
->ibits
, uap
->nd
);
1177 p
->p_flag
&= ~P_SELECT
;
1178 /* select is not restarted after signals... */
1179 if (error
== ERESTART
)
1181 if (error
== EWOULDBLOCK
)
1183 nw
= howmany(uap
->nd
, NFDBITS
);
1184 ni
= nw
* sizeof(fd_mask
);
1186 #define putbits(name, x) \
1188 if (uap->name && (error2 = \
1189 copyout((caddr_t)&sel->obits[(x) * nw], uap->name, ni))) \
1205 selscan(p
, sel
, nfd
, retval
, sel_pass
, wqsub
)
1207 struct _select
*sel
;
1211 wait_queue_sub_t wqsub
;
1213 register struct filedesc
*fdp
= p
->p_fd
;
1214 register int msk
, i
, j
, fd
;
1215 register u_int32_t bits
;
1216 struct fileproc
*fp
;
1219 static int flag
[3] = { FREAD
, FWRITE
, 0 };
1220 u_int32_t
*iptr
, *optr
;
1222 u_int32_t
*ibits
, *obits
;
1227 * Problems when reboot; due to MacOSX signal probs
1228 * in Beaker1C ; verify that the p->p_fd is valid
1238 nw
= howmany(nfd
, NFDBITS
);
1244 for (msk
= 0; msk
< 3; msk
++) {
1245 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1246 optr
= (u_int32_t
*)&obits
[msk
* nw
];
1248 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1249 bits
= iptr
[i
/NFDBITS
];
1251 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1253 fp
= fdp
->fd_ofiles
[fd
];
1256 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1260 if (sel_pass
== SEL_SECONDPASS
) {
1261 wql_ptr
= (char *)0;
1262 fp
->f_flags
&= ~FP_INSELECT
;
1263 fp
->f_waddr
= (void *)0;
1265 wql_ptr
= (wql
+ nc
* SIZEOF_WAITQUEUE_LINK
);
1266 fp
->f_flags
|= FP_INSELECT
;
1267 fp
->f_waddr
= (void *)wqsub
;
1269 if (fp
->f_ops
&& fo_select(fp
, flag
[msk
], wql_ptr
, p
)) {
1270 optr
[fd
/NFDBITS
] |= (1 << (fd
% NFDBITS
));
1283 static int poll_callback(struct kqueue
*, struct kevent
*, void *);
1285 struct poll_continue_args
{
1286 user_addr_t pca_fds
;
1292 poll(struct proc
*p
, struct poll_args
*uap
, register_t
*retval
)
1294 struct poll_continue_args
*cont
;
1298 int ncoll
, error
= 0;
1299 u_int nfds
= uap
->nfds
;
1305 * This is kinda bogus. We have fd limits, but that is not
1306 * really related to the size of the pollfd array. Make sure
1307 * we let the process use at least FD_SETSIZE entries and at
1308 * least enough for the current limits. We want to be reasonably
1309 * safe, but not overly restrictive.
1311 if (nfds
> OPEN_MAX
||
1312 (nfds
> p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
&& nfds
> FD_SETSIZE
))
1315 kq
= kqueue_alloc(p
);
1319 ni
= nfds
* sizeof(struct pollfd
) + sizeof(struct poll_continue_args
);
1320 MALLOC(cont
, struct poll_continue_args
*, ni
, M_TEMP
, M_WAITOK
);
1326 fds
= (struct pollfd
*)&cont
[1];
1327 error
= copyin(uap
->fds
, fds
, nfds
* sizeof(struct pollfd
));
1331 if (uap
->timeout
!= -1) {
1334 atv
.tv_sec
= uap
->timeout
/ 1000;
1335 atv
.tv_usec
= (uap
->timeout
% 1000) * 1000;
1336 if (itimerfix(&atv
)) {
1340 getmicrouptime(&rtv
);
1341 timevaladd(&atv
, &rtv
);
1347 /* JMM - all this P_SELECT stuff is bogus */
1349 p
->p_flag
|= P_SELECT
;
1351 for (i
= 0; i
< nfds
; i
++) {
1352 short events
= fds
[i
].events
;
1356 /* per spec, ignore fd values below zero */
1357 if (fds
[i
].fd
< 0) {
1362 /* convert the poll event into a kqueue kevent */
1363 kev
.ident
= fds
[i
].fd
;
1364 kev
.flags
= EV_ADD
| EV_ONESHOT
| EV_POLL
;
1365 kev
.fflags
= NOTE_LOWAT
;
1366 kev
.data
= 1; /* efficiency be damned: any data should trigger */
1367 kev
.udata
= CAST_USER_ADDR_T(&fds
[i
]);
1369 /* Handle input events */
1370 if (events
& ( POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
)) {
1371 kev
.filter
= EVFILT_READ
;
1372 if (!(events
& ( POLLIN
| POLLRDNORM
)))
1373 kev
.flags
|= EV_OOBAND
;
1374 kerror
= kevent_register(kq
, &kev
, p
);
1377 /* Handle output events */
1379 events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
)) {
1380 kev
.filter
= EVFILT_WRITE
;
1381 kerror
= kevent_register(kq
, &kev
, p
);
1384 /* Handle BSD extension vnode events */
1386 events
& ( POLLEXTEND
| POLLATTRIB
| POLLNLINK
| POLLWRITE
)) {
1387 kev
.filter
= EVFILT_VNODE
;
1389 if (events
& POLLEXTEND
)
1390 kev
.fflags
|= NOTE_EXTEND
;
1391 if (events
& POLLATTRIB
)
1392 kev
.fflags
|= NOTE_ATTRIB
;
1393 if (events
& POLLNLINK
)
1394 kev
.fflags
|= NOTE_LINK
;
1395 if (events
& POLLWRITE
)
1396 kev
.fflags
|= NOTE_WRITE
;
1397 kerror
= kevent_register(kq
, &kev
, p
);
1401 fds
[i
].revents
= POLLNVAL
;
1407 /* Did we have any trouble registering? */
1411 /* scan for, and possibly wait for, the kevents to trigger */
1412 cont
->pca_fds
= uap
->fds
;
1413 cont
->pca_nfds
= nfds
;
1414 cont
->pca_rfds
= rfds
;
1415 error
= kevent_scan(kq
, poll_callback
, NULL
, cont
, &atv
, p
);
1416 rfds
= cont
->pca_rfds
;
1419 p
->p_flag
&= ~P_SELECT
;
1420 /* poll is not restarted after signals... */
1421 if (error
== ERESTART
)
1423 if (error
== EWOULDBLOCK
)
1426 error
= copyout(fds
, uap
->fds
, nfds
* sizeof(struct pollfd
));
1434 kqueue_dealloc(kq
, p
);
1439 poll_callback(__unused
struct kqueue
*kq
, struct kevent
*kevp
, void *data
)
1441 struct poll_continue_args
*cont
= (struct poll_continue_args
*)data
;
1442 struct pollfd
*fds
= CAST_DOWN(struct pollfd
*, kevp
->udata
);
1445 /* convert the results back into revents */
1446 if (kevp
->flags
& EV_EOF
)
1447 fds
->revents
|= POLLHUP
;
1448 if (kevp
->flags
& EV_ERROR
)
1449 fds
->revents
|= POLLERR
;
1452 switch (kevp
->filter
) {
1454 if (fds
->revents
& POLLHUP
)
1455 mask
= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
);
1458 if (kevp
->data
!= 0)
1459 mask
|= (POLLIN
| POLLRDNORM
);
1460 if (kevp
->flags
& EV_OOBAND
)
1461 mask
|= ( POLLPRI
| POLLRDBAND
);
1463 fds
->revents
|= (fds
->events
& mask
);
1467 if (!(fds
->revents
& POLLHUP
))
1468 fds
->revents
|= (fds
->events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
));
1472 if (kevp
->fflags
& NOTE_EXTEND
)
1473 fds
->revents
|= (fds
->events
& POLLEXTEND
);
1474 if (kevp
->fflags
& NOTE_ATTRIB
)
1475 fds
->revents
|= (fds
->events
& POLLATTRIB
);
1476 if (kevp
->fflags
& NOTE_LINK
)
1477 fds
->revents
|= (fds
->events
& POLLNLINK
);
1478 if (kevp
->fflags
& NOTE_WRITE
)
1479 fds
->revents
|= (fds
->events
& POLLWRITE
);
1486 seltrue(__unused dev_t dev
, __unused
int flag
, __unused
struct proc
*p
)
1493 selcount(struct proc
*p
, u_int32_t
*ibits
, __unused u_int32_t
*obits
,
1494 int nfd
, int *count
)
1496 register struct filedesc
*fdp
= p
->p_fd
;
1497 register int msk
, i
, j
, fd
;
1498 register u_int32_t bits
;
1499 struct fileproc
*fp
;
1507 * Problems when reboot; due to MacOSX signal probs
1508 * in Beaker1C ; verify that the p->p_fd is valid
1514 nw
= howmany(nfd
, NFDBITS
);
1517 for (msk
= 0; msk
< 3; msk
++) {
1518 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1519 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1520 bits
= iptr
[i
/NFDBITS
];
1521 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1523 fp
= fdp
->fd_ofiles
[fd
];
1525 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1544 /* undo the iocounts */
1545 for (msk
= 0; msk
< 3; msk
++) {
1546 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1547 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1548 bits
= iptr
[i
/NFDBITS
];
1549 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1551 fp
= fdp
->fd_ofiles
[fd
];
1556 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
1557 p
->p_fpdrainwait
= 0;
1558 wakeup(&p
->p_fpdrainwait
);
1570 seldrop(p
, ibits
, nfd
)
1575 register struct filedesc
*fdp
= p
->p_fd
;
1576 register int msk
, i
, j
, fd
;
1577 register u_int32_t bits
;
1578 struct fileproc
*fp
;
1584 * Problems when reboot; due to MacOSX signal probs
1585 * in Beaker1C ; verify that the p->p_fd is valid
1591 nw
= howmany(nfd
, NFDBITS
);
1595 for (msk
= 0; msk
< 3; msk
++) {
1596 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1597 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1598 bits
= iptr
[i
/NFDBITS
];
1599 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1601 fp
= fdp
->fd_ofiles
[fd
];
1604 /* if you are here then it is being closed */
1605 || (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)
1613 fp
->f_flags
&= ~FP_INSELECT
;
1615 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
1616 p
->p_fpdrainwait
= 0;
1617 wakeup(&p
->p_fpdrainwait
);
1627 * Record a select request.
1630 selrecord(__unused
struct proc
*selector
, struct selinfo
*sip
, void * p_wql
)
1632 thread_t cur_act
= current_thread();
1633 struct uthread
* ut
= get_bsdthread_info(cur_act
);
1635 /* need to look at collisions */
1637 if ((p_wql
== (void *)0) && ((sip
->si_flags
& SI_INITED
) == 0)) {
1641 /*do not record if this is second pass of select */
1642 if((p_wql
== (void *)0)) {
1646 if ((sip
->si_flags
& SI_INITED
) == 0) {
1647 wait_queue_init(&sip
->si_wait_queue
, SYNC_POLICY_FIFO
);
1648 sip
->si_flags
|= SI_INITED
;
1649 sip
->si_flags
&= ~SI_CLEAR
;
1652 if (sip
->si_flags
& SI_RECORDED
) {
1653 sip
->si_flags
|= SI_COLL
;
1655 sip
->si_flags
&= ~SI_COLL
;
1657 sip
->si_flags
|= SI_RECORDED
;
1658 if (!wait_queue_member(&sip
->si_wait_queue
, ut
->uu_select
.wqset
))
1659 wait_queue_link_noalloc(&sip
->si_wait_queue
, ut
->uu_select
.wqset
,
1660 (wait_queue_link_t
)p_wql
);
1667 register struct selinfo
*sip
;
1670 if ((sip
->si_flags
& SI_INITED
) == 0) {
1674 if (sip
->si_flags
& SI_COLL
) {
1676 sip
->si_flags
&= ~SI_COLL
;
1678 /* will not support */
1679 //wakeup((caddr_t)&selwait);
1683 if (sip
->si_flags
& SI_RECORDED
) {
1684 wait_queue_wakeup_all(&sip
->si_wait_queue
, &selwait
, THREAD_AWAKENED
);
1685 sip
->si_flags
&= ~SI_RECORDED
;
1692 register struct selinfo
*sip
;
1695 if ((sip
->si_flags
& SI_INITED
) == 0) {
1698 if (sip
->si_flags
& SI_RECORDED
) {
1700 sip
->si_flags
&= ~(SI_RECORDED
| SI_COLL
);
1702 sip
->si_flags
|= SI_CLEAR
;
1703 wait_queue_unlinkall_nofree(&sip
->si_wait_queue
);
1709 #define DBG_POST 0x10
1710 #define DBG_WATCH 0x11
1711 #define DBG_WAIT 0x12
1712 #define DBG_MOD 0x13
1713 #define DBG_EWAKEUP 0x14
1714 #define DBG_ENQUEUE 0x15
1715 #define DBG_DEQUEUE 0x16
1717 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
1718 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
1719 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
1720 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
1721 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
1722 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
1723 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
1726 #define EVPROCDEQUE(p, evq) do { \
1728 if (evq->ee_flags & EV_QUEUED) { \
1729 TAILQ_REMOVE(&p->p_evlist, evq, ee_plist); \
1730 evq->ee_flags &= ~EV_QUEUED; \
1737 * called upon socket close. deque and free all events for
1738 * the socket... socket must be locked by caller.
1741 evsofree(struct socket
*sp
)
1743 struct eventqelt
*evq
, *next
;
1749 for (evq
= sp
->so_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
1750 next
= evq
->ee_slist
.tqe_next
;
1753 if (evq
->ee_flags
& EV_QUEUED
) {
1754 EVPROCDEQUE(p
, evq
);
1756 TAILQ_REMOVE(&sp
->so_evlist
, evq
, ee_slist
); // remove from socket q
1763 * called upon pipe close. deque and free all events for
1764 * the pipe... pipe must be locked by caller
1767 evpipefree(struct pipe
*cpipe
)
1769 struct eventqelt
*evq
, *next
;
1772 for (evq
= cpipe
->pipe_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
1773 next
= evq
->ee_slist
.tqe_next
;
1776 EVPROCDEQUE(p
, evq
);
1778 TAILQ_REMOVE(&cpipe
->pipe_evlist
, evq
, ee_slist
); // remove from pipe q
1785 * enqueue this event if it's not already queued. wakeup
1786 * the proc if we do queue this event to it...
1787 * entered with proc lock held... we drop it before
1788 * doing the wakeup and return in that state
1791 evprocenque(struct eventqelt
*evq
)
1798 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_START
, evq
, evq
->ee_flags
, evq
->ee_eventmask
,0,0);
1802 if (evq
->ee_flags
& EV_QUEUED
) {
1805 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1808 evq
->ee_flags
|= EV_QUEUED
;
1810 TAILQ_INSERT_TAIL(&p
->p_evlist
, evq
, ee_plist
);
1814 wakeup(&p
->p_evlist
);
1816 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1821 * pipe lock must be taken by the caller
1824 postpipeevent(struct pipe
*pipep
, int event
)
1827 struct eventqelt
*evq
;
1831 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, event
,0,0,1,0);
1833 for (evq
= pipep
->pipe_evlist
.tqh_first
;
1834 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1836 if (evq
->ee_eventmask
== 0)
1840 switch (event
& (EV_RWBYTES
| EV_RCLOSED
| EV_WCLOSED
)) {
1843 if ((evq
->ee_eventmask
& EV_RE
) && pipep
->pipe_buffer
.cnt
) {
1845 evq
->ee_req
.er_rcnt
= pipep
->pipe_buffer
.cnt
;
1847 if ((evq
->ee_eventmask
& EV_WR
) &&
1848 (pipep
->pipe_buffer
.size
- pipep
->pipe_buffer
.cnt
) >= PIPE_BUF
) {
1850 if (pipep
->pipe_state
& PIPE_EOF
) {
1851 mask
|= EV_WR
|EV_RESET
;
1855 evq
->ee_req
.er_wcnt
= pipep
->pipe_buffer
.size
- pipep
->pipe_buffer
.cnt
;
1861 if ((evq
->ee_eventmask
& EV_RE
)) {
1862 mask
|= EV_RE
|EV_RCLOSED
;
1864 if ((evq
->ee_eventmask
& EV_WR
)) {
1865 mask
|= EV_WR
|EV_WCLOSED
;
1874 * disarm... postevents are nops until this event is 'read' via
1875 * waitevent and then re-armed via modwatch
1877 evq
->ee_eventmask
= 0;
1880 * since events are disarmed until after the waitevent
1881 * the ee_req.er_xxxx fields can't change once we've
1882 * inserted this event into the proc queue...
1883 * therefore, the waitevent will see a 'consistent'
1884 * snapshot of the event, even though it won't hold
1885 * the pipe lock, and we're updating the event outside
1886 * of the proc lock, which it will hold
1888 evq
->ee_req
.er_eventbits
|= mask
;
1890 KERNEL_DEBUG(DBG_MISC_POST
, evq
, evq
->ee_req
.er_eventbits
, mask
, 1,0);
1895 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, 0,0,0,1,0);
1900 * given either a sockbuf or a socket run down the
1901 * event list and queue ready events found...
1902 * the socket must be locked by the caller
1905 postevent(struct socket
*sp
, struct sockbuf
*sb
, int event
)
1908 struct eventqelt
*evq
;
1916 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, (int)sp
, event
, 0, 0, 0);
1918 for (evq
= sp
->so_evlist
.tqh_first
;
1919 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1921 if (evq
->ee_eventmask
== 0)
1925 /* ready for reading:
1926 - byte cnt >= receive low water mark
1927 - read-half of conn closed
1928 - conn pending for listening sock
1929 - socket error pending
1932 - byte cnt avail >= send low water mark
1933 - write half of conn closed
1934 - socket error pending
1935 - non-blocking conn completed successfully
1939 - sock at out of band mark
1942 switch (event
& EV_DMASK
) {
1945 if ((evq
->ee_eventmask
& EV_EX
)) {
1946 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
1947 mask
|= EV_EX
|EV_OOB
;
1951 case EV_RWBYTES
|EV_OOB
:
1952 if ((evq
->ee_eventmask
& EV_EX
)) {
1953 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
1954 mask
|= EV_EX
|EV_OOB
;
1957 * fall into the next case
1960 if ((evq
->ee_eventmask
& EV_RE
) && soreadable(sp
)) {
1962 if ((sp
->so_type
== SOCK_STREAM
) && ((sp
->so_error
== ECONNREFUSED
) || (sp
->so_error
== ECONNRESET
))) {
1963 if ((sp
->so_pcb
== 0) || (((struct inpcb
*)sp
->so_pcb
)->inp_state
== INPCB_STATE_DEAD
) || !(tp
= sototcpcb(sp
)) ||
1964 (tp
->t_state
== TCPS_CLOSED
)) {
1965 mask
|= EV_RE
|EV_RESET
;
1971 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
1973 if (sp
->so_state
& SS_CANTRCVMORE
) {
1978 if ((evq
->ee_eventmask
& EV_WR
) && sowriteable(sp
)) {
1980 if ((sp
->so_type
== SOCK_STREAM
) && ((sp
->so_error
== ECONNREFUSED
) || (sp
->so_error
== ECONNRESET
))) {
1981 if ((sp
->so_pcb
== 0) || (((struct inpcb
*)sp
->so_pcb
)->inp_state
== INPCB_STATE_DEAD
) || !(tp
= sototcpcb(sp
)) ||
1982 (tp
->t_state
== TCPS_CLOSED
)) {
1983 mask
|= EV_WR
|EV_RESET
;
1989 evq
->ee_req
.er_wcnt
= sbspace(&sp
->so_snd
);
1994 if ((evq
->ee_eventmask
& EV_RE
)) {
1995 mask
|= EV_RE
|EV_RCONN
;
1996 evq
->ee_req
.er_rcnt
= sp
->so_qlen
+ 1; // incl this one
2001 if ((evq
->ee_eventmask
& EV_WR
)) {
2002 mask
|= EV_WR
|EV_WCONN
;
2007 if ((evq
->ee_eventmask
& EV_RE
)) {
2008 mask
|= EV_RE
|EV_RCLOSED
;
2013 if ((evq
->ee_eventmask
& EV_WR
)) {
2014 mask
|= EV_WR
|EV_WCLOSED
;
2019 if (evq
->ee_eventmask
& EV_RE
) {
2020 mask
|= EV_RE
|EV_FIN
;
2026 if (evq
->ee_eventmask
& EV_RE
) {
2027 mask
|= EV_RE
| event
;
2029 if (evq
->ee_eventmask
& EV_WR
) {
2030 mask
|= EV_WR
| event
;
2035 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, -1, 0, 0, 0);
2039 KERNEL_DEBUG(DBG_MISC_POST
, (int)evq
, evq
->ee_eventmask
, evq
->ee_req
.er_eventbits
, mask
, 0);
2043 * disarm... postevents are nops until this event is 'read' via
2044 * waitevent and then re-armed via modwatch
2046 evq
->ee_eventmask
= 0;
2049 * since events are disarmed until after the waitevent
2050 * the ee_req.er_xxxx fields can't change once we've
2051 * inserted this event into the proc queue...
2052 * since waitevent can't see this event until we
2053 * enqueue it, waitevent will see a 'consistent'
2054 * snapshot of the event, even though it won't hold
2055 * the socket lock, and we're updating the event outside
2056 * of the proc lock, which it will hold
2058 evq
->ee_req
.er_eventbits
|= mask
;
2063 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, 0, 0, 0, 0);
2068 * watchevent system call. user passes us an event to watch
2069 * for. we malloc an event object, initialize it, and queue
2070 * it to the open socket. when the event occurs, postevent()
2071 * will enque it back to our proc where we can retrieve it
2074 * should this prevent duplicate events on same socket?
2077 watchevent(proc_t p
, struct watchevent_args
*uap
, __unused
int *retval
)
2079 struct eventqelt
*evq
= (struct eventqelt
*)0;
2080 struct eventqelt
*np
= NULL
;
2081 struct eventreq
*erp
;
2082 struct fileproc
*fp
= NULL
;
2085 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_START
, 0,0,0,0,0);
2087 // get a qelt and fill with users req
2088 MALLOC(evq
, struct eventqelt
*, sizeof(struct eventqelt
), M_TEMP
, M_WAITOK
);
2091 panic("can't MALLOC evq");
2094 // get users request pkt
2095 if ( (error
= copyin(CAST_USER_ADDR_T(uap
->u_req
), (caddr_t
)erp
,
2096 sizeof(struct eventreq
))) ) {
2099 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2102 KERNEL_DEBUG(DBG_MISC_WATCH
, erp
->er_handle
,uap
->u_eventmask
,evq
,0,0);
2104 // validate, freeing qelt if errors
2108 if (erp
->er_type
!= EV_FD
) {
2110 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2112 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2113 socket_lock((struct socket
*)fp
->f_data
, 1);
2114 np
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2115 } else if (fp
->f_type
== DTYPE_PIPE
) {
2116 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2117 np
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2119 fp_drop(p
, erp
->er_handle
, fp
, 1);
2127 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2132 * only allow one watch per file per proc
2134 for ( ; np
!= NULL
; np
= np
->ee_slist
.tqe_next
) {
2135 if (np
->ee_proc
== p
) {
2136 if (fp
->f_type
== DTYPE_SOCKET
)
2137 socket_unlock((struct socket
*)fp
->f_data
, 1);
2139 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2140 fp_drop(p
, erp
->er_handle
, fp
, 0);
2143 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2147 erp
->er_ecnt
= erp
->er_rcnt
= erp
->er_wcnt
= erp
->er_eventbits
= 0;
2149 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2152 if (fp
->f_type
== DTYPE_SOCKET
) {
2153 TAILQ_INSERT_TAIL(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2154 postevent((struct socket
*)fp
->f_data
, 0, EV_RWBYTES
); // catch existing events
2156 socket_unlock((struct socket
*)fp
->f_data
, 1);
2158 TAILQ_INSERT_TAIL(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2159 postpipeevent((struct pipe
*)fp
->f_data
, EV_RWBYTES
);
2161 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2163 fp_drop_event(p
, erp
->er_handle
, fp
);
2165 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, 0,0,0,0,0);
2172 * waitevent system call.
2173 * grabs the next waiting event for this proc and returns
2174 * it. if no events, user can request to sleep with timeout
2175 * or poll mode (tv=NULL);
2178 waitevent(proc_t p
, struct waitevent_args
*uap
, int *retval
)
2181 struct eventqelt
*evq
;
2183 uint64_t abstime
, interval
;
2188 error
= copyin(CAST_USER_ADDR_T(uap
->tv
), (caddr_t
)&atv
, sizeof (atv
));
2191 if (itimerfix(&atv
)) {
2195 interval
= tvtoabstime(&atv
);
2199 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_START
, 0,0,0,0,0);
2203 if ((evq
= p
->p_evlist
.tqh_first
) != NULL
) {
2205 * found one... make a local copy while it's still on the queue
2206 * to prevent it from changing while in the midst of copying
2207 * don't want to hold the proc lock across a copyout because
2208 * it might block on a page fault at the target in user space
2210 bcopy((caddr_t
)&evq
->ee_req
, (caddr_t
)&er
, sizeof (struct eventreq
));
2212 TAILQ_REMOVE(&p
->p_evlist
, evq
, ee_plist
);
2214 evq
->ee_flags
&= ~EV_QUEUED
;
2218 error
= copyout((caddr_t
)&er
, CAST_USER_ADDR_T(uap
->u_req
), sizeof(struct eventreq
));
2220 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,
2221 evq
->ee_req
.er_handle
,evq
->ee_req
.er_eventbits
,evq
,0);
2225 if (uap
->tv
&& interval
== 0) {
2227 *retval
= 1; // poll failed
2229 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,0,0,0,0);
2233 clock_absolutetime_interval_to_deadline(interval
, &abstime
);
2237 KERNEL_DEBUG(DBG_MISC_WAIT
, 1,&p
->p_evlist
,0,0,0);
2239 error
= msleep1(&p
->p_evlist
, &p
->p_mlock
, (PSOCK
| PCATCH
), "waitevent", abstime
);
2241 KERNEL_DEBUG(DBG_MISC_WAIT
, 2,&p
->p_evlist
,0,0,0);
2245 if (error
== ERESTART
)
2247 if (error
== EWOULDBLOCK
) {
2254 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, 0,0,0,0,0);
2260 * modwatch system call. user passes in event to modify.
2261 * if we find it we reset the event bits and que/deque event
2265 modwatch(proc_t p
, struct modwatch_args
*uap
, __unused
int *retval
)
2268 struct eventreq
*erp
= &er
;
2269 struct eventqelt
*evq
;
2271 struct fileproc
*fp
;
2274 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_START
, 0,0,0,0,0);
2277 * get user's request pkt
2279 if ((error
= copyin(CAST_USER_ADDR_T(uap
->u_req
), (caddr_t
)erp
,
2280 sizeof(struct eventreq
)))) {
2281 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2286 if (erp
->er_type
!= EV_FD
) {
2288 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2290 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2291 socket_lock((struct socket
*)fp
->f_data
, 1);
2292 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2293 } else if (fp
->f_type
== DTYPE_PIPE
) {
2294 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2295 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2297 fp_drop(p
, erp
->er_handle
, fp
, 1);
2303 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2307 if ((uap
->u_eventmask
== EV_RM
) && (fp
->f_flags
& FP_WAITEVENT
)) {
2308 fp
->f_flags
&= ~FP_WAITEVENT
;
2312 // locate event if possible
2313 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2314 if (evq
->ee_proc
== p
)
2318 if (fp
->f_type
== DTYPE_SOCKET
)
2319 socket_unlock((struct socket
*)fp
->f_data
, 1);
2321 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2322 fp_drop(p
, erp
->er_handle
, fp
, 0);
2323 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2326 KERNEL_DEBUG(DBG_MISC_MOD
, erp
->er_handle
,uap
->u_eventmask
,evq
,0,0);
2328 if (uap
->u_eventmask
== EV_RM
) {
2329 EVPROCDEQUE(p
, evq
);
2331 if (fp
->f_type
== DTYPE_SOCKET
) {
2332 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2333 socket_unlock((struct socket
*)fp
->f_data
, 1);
2335 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2336 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2338 fp_drop(p
, erp
->er_handle
, fp
, 0);
2340 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, 0,0,0,0,0);
2343 switch (uap
->u_eventmask
& EV_MASK
) {
2361 case EV_EX
|EV_RE
|EV_WR
:
2362 flag
= EV_OOB
|EV_RWBYTES
;
2366 if (fp
->f_type
== DTYPE_SOCKET
)
2367 socket_unlock((struct socket
*)fp
->f_data
, 1);
2369 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2370 fp_drop(p
, erp
->er_handle
, fp
, 0);
2371 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2375 * since we're holding the socket/pipe lock, the event
2376 * cannot go from the unqueued state to the queued state
2377 * however, it can go from the queued state to the unqueued state
2378 * since that direction is protected by the proc_lock...
2379 * so do a quick check for EV_QUEUED w/o holding the proc lock
2380 * since by far the common case will be NOT EV_QUEUED, this saves
2381 * us taking the proc_lock the majority of the time
2383 if (evq
->ee_flags
& EV_QUEUED
) {
2385 * EVPROCDEQUE will recheck the state after it grabs the proc_lock
2387 EVPROCDEQUE(p
, evq
);
2390 * while the event is off the proc queue and
2391 * we're holding the socket/pipe lock
2392 * it's safe to update these fields...
2394 evq
->ee_req
.er_eventbits
= 0;
2395 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2397 if (fp
->f_type
== DTYPE_SOCKET
) {
2398 postevent((struct socket
*)fp
->f_data
, 0, flag
);
2399 socket_unlock((struct socket
*)fp
->f_data
, 1);
2402 postpipeevent((struct pipe
*)fp
->f_data
, flag
);
2403 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2405 fp_drop(p
, erp
->er_handle
, fp
, 0);
2406 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, evq
->ee_req
.er_handle
,evq
->ee_eventmask
,fp
->f_data
,flag
,0);
2410 /* this routine is called from the close of fd with proc_fdlock held */
2412 waitevent_close(struct proc
*p
, struct fileproc
*fp
)
2414 struct eventqelt
*evq
;
2417 fp
->f_flags
&= ~FP_WAITEVENT
;
2419 if (fp
->f_type
== DTYPE_SOCKET
) {
2420 socket_lock((struct socket
*)fp
->f_data
, 1);
2421 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2423 else if (fp
->f_type
== DTYPE_PIPE
) {
2424 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2425 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2433 // locate event if possible
2434 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2435 if (evq
->ee_proc
== p
)
2439 if (fp
->f_type
== DTYPE_SOCKET
)
2440 socket_unlock((struct socket
*)fp
->f_data
, 1);
2442 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2448 EVPROCDEQUE(p
, evq
);
2450 if (fp
->f_type
== DTYPE_SOCKET
) {
2451 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2452 socket_unlock((struct socket
*)fp
->f_data
, 1);
2454 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2455 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);