2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
32 * Copyright (c) 1982, 1986, 1989, 1993
33 * The Regents of the University of California. All rights reserved.
34 * (c) UNIX System Laboratories, Inc.
35 * All or some portions of this file are derived from material licensed
36 * to the University of California by American Telephone and Telegraph
37 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
38 * the permission of UNIX System Laboratories, Inc.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
43 * 1. Redistributions of source code must retain the above copyright
44 * notice, this list of conditions and the following disclaimer.
45 * 2. Redistributions in binary form must reproduce the above copyright
46 * notice, this list of conditions and the following disclaimer in the
47 * documentation and/or other materials provided with the distribution.
48 * 3. All advertising materials mentioning features or use of this software
49 * must display the following acknowledgement:
50 * This product includes software developed by the University of
51 * California, Berkeley and its contributors.
52 * 4. Neither the name of the University nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
56 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/filedesc.h>
74 #include <sys/ioctl.h>
75 #include <sys/file_internal.h>
76 #include <sys/proc_internal.h>
77 #include <sys/socketvar.h>
79 #include <sys/uio_internal.h>
83 #include <sys/kernel.h>
85 #include <sys/malloc.h>
86 #include <sys/sysproto.h>
88 #include <sys/mount_internal.h>
89 #include <sys/protosw.h>
92 #include <sys/kdebug.h>
94 #include <sys/event.h>
95 #include <sys/eventvar.h>
97 #include <mach/mach_types.h>
98 #include <kern/kern_types.h>
99 #include <kern/assert.h>
100 #include <kern/kalloc.h>
101 #include <kern/thread.h>
102 #include <kern/clock.h>
104 #include <sys/mbuf.h>
105 #include <sys/socket.h>
106 #include <sys/socketvar.h>
107 #include <sys/errno.h>
108 #include <sys/syscall.h>
109 #include <sys/pipe.h>
111 #include <bsm/audit_kernel.h>
114 #include <net/route.h>
116 #include <netinet/in.h>
117 #include <netinet/in_systm.h>
118 #include <netinet/ip.h>
119 #include <netinet/in_pcb.h>
120 #include <netinet/ip_var.h>
121 #include <netinet/ip6.h>
122 #include <netinet/tcp.h>
123 #include <netinet/tcp_fsm.h>
124 #include <netinet/tcp_seq.h>
125 #include <netinet/tcp_timer.h>
126 #include <netinet/tcp_var.h>
127 #include <netinet/tcpip.h>
128 #include <netinet/tcp_debug.h>
129 /* for wait queue based select */
130 #include <kern/wait_queue.h>
131 #include <kern/kalloc.h>
133 #include <sys/ktrace.h>
135 #include <sys/vnode_internal.h>
137 int rd_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
);
138 int wr_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
);
139 extern void *get_bsduthreadarg(thread_t
);
140 extern int *get_bsduthreadrval(thread_t
);
142 __private_extern__
int dofileread(struct proc
*p
, struct fileproc
*fp
, int fd
,
143 user_addr_t bufp
, user_size_t nbyte
,
144 off_t offset
, int flags
, user_ssize_t
*retval
);
145 __private_extern__
int dofilewrite(struct proc
*p
, struct fileproc
*fp
, int fd
,
146 user_addr_t bufp
, user_size_t nbyte
,
147 off_t offset
, int flags
, user_ssize_t
*retval
);
148 __private_extern__
int preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_vnode
);
149 __private_extern__
void donefileread(struct proc
*p
, struct fileproc
*fp_ret
, int fd
);
152 extern int appletalk_inited
;
155 #define f_flag f_fglob->fg_flag
156 #define f_type f_fglob->fg_type
157 #define f_msgcount f_fglob->fg_msgcount
158 #define f_cred f_fglob->fg_cred
159 #define f_ops f_fglob->fg_ops
160 #define f_offset f_fglob->fg_offset
161 #define f_data f_fglob->fg_data
168 register struct read_args
*uap
;
169 user_ssize_t
*retval
;
175 if ( (error
= preparefileread(p
, &fp
, fd
, 0)) )
178 error
= dofileread(p
, fp
, uap
->fd
, uap
->cbuf
, uap
->nbyte
,
179 (off_t
)-1, 0, retval
);
181 donefileread(p
, fp
, fd
);
190 pread(p
, uap
, retval
)
192 register struct pread_args
*uap
;
193 user_ssize_t
*retval
;
199 if ( (error
= preparefileread(p
, &fp
, fd
, 1)) )
202 error
= dofileread(p
, fp
, uap
->fd
, uap
->buf
, uap
->nbyte
,
203 uap
->offset
, FOF_OFFSET
, retval
);
205 donefileread(p
, fp
, fd
);
208 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pread
) | DBG_FUNC_NONE
),
209 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
215 * Code common for read and pread
219 donefileread(struct proc
*p
, struct fileproc
*fp
, int fd
)
223 fp
->f_flags
&= ~FP_INCHRREAD
;
225 fp_drop(p
, fd
, fp
, 1);
230 preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_pread
)
238 error
= fp_lookup(p
, fd
, &fp
, 1);
244 if ((fp
->f_flag
& FREAD
) == 0) {
248 if (check_for_pread
&& (fp
->f_type
!= DTYPE_VNODE
)) {
252 if (fp
->f_type
== DTYPE_VNODE
) {
253 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
255 if (vp
->v_type
== VCHR
)
256 fp
->f_flags
|= FP_INCHRREAD
;
265 fp_drop(p
, fd
, fp
, 1);
271 __private_extern__
int
272 dofileread(p
, fp
, fd
, bufp
, nbyte
, offset
, flags
, retval
)
279 user_ssize_t
*retval
;
282 user_ssize_t bytecnt
;
284 char uio_buf
[ UIO_SIZEOF(1) ];
287 char ktr_uio_buf
[ UIO_SIZEOF(1) ];
291 // LP64todo - do we want to raise this?
295 if (IS_64BIT_PROCESS(p
)) {
296 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_READ
,
297 &uio_buf
[0], sizeof(uio_buf
));
299 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_READ
,
300 &uio_buf
[0], sizeof(uio_buf
));
302 uio_addiov(auio
, bufp
, nbyte
);
306 * if tracing, save a copy of iovec
308 if (KTRPOINT(p
, KTR_GENIO
)) {
311 if (IS_64BIT_PROCESS(p
)) {
312 ktruio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_READ
,
313 &ktr_uio_buf
[0], sizeof(ktr_uio_buf
));
315 ktruio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_READ
,
316 &ktr_uio_buf
[0], sizeof(ktr_uio_buf
));
318 uio_addiov(ktruio
, bufp
, nbyte
);
323 if ((error
= fo_read(fp
, auio
, fp
->f_cred
, flags
, p
))) {
324 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
325 error
== EINTR
|| error
== EWOULDBLOCK
))
328 bytecnt
-= uio_resid(auio
);
330 if (didktr
&& error
== 0) {
331 uio_setresid(ktruio
, bytecnt
);
332 ktrgenio(p
->p_tracep
, fd
, UIO_READ
, ktruio
, error
);
342 * Scatter read system call.
345 readv(p
, uap
, retval
)
347 register struct readv_args
*uap
;
348 user_ssize_t
*retval
;
353 struct user_iovec
*iovp
;
355 /* Verify range bedfore calling uio_create() */
356 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
359 /* allocate a uio large enough to hold the number of iovecs passed */
360 auio
= uio_create(uap
->iovcnt
, 0,
361 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
364 /* get location of iovecs within the uio. then copyin the iovecs from
367 iovp
= uio_iovsaddr(auio
);
370 goto ExitThisRoutine
;
372 size_of_iovec
= (IS_64BIT_PROCESS(p
) ? sizeof(struct user_iovec
) : sizeof(struct iovec
));
373 error
= copyin(uap
->iovp
, (caddr_t
)iovp
, (uap
->iovcnt
* size_of_iovec
));
375 goto ExitThisRoutine
;
378 /* finalize uio_t for use and do the IO
380 uio_calculateresid(auio
);
381 error
= rd_uio(p
, uap
->fd
, auio
, retval
);
394 write(p
, uap
, retval
)
396 register struct write_args
*uap
;
397 user_ssize_t
*retval
;
403 error
= fp_lookup(p
,fd
,&fp
,0);
406 if ((fp
->f_flag
& FWRITE
) == 0) {
409 error
= dofilewrite(p
, fp
, uap
->fd
, uap
->cbuf
, uap
->nbyte
,
410 (off_t
)-1, 0, retval
);
413 fp_drop_written(p
, fd
, fp
);
415 fp_drop(p
, fd
, fp
, 0);
423 pwrite(p
, uap
, retval
)
425 register struct pwrite_args
*uap
;
426 user_ssize_t
*retval
;
432 error
= fp_lookup(p
,fd
,&fp
,0);
436 if ((fp
->f_flag
& FWRITE
) == 0) {
439 if (fp
->f_type
!= DTYPE_VNODE
) {
442 error
= dofilewrite(p
, fp
, uap
->fd
, uap
->buf
, uap
->nbyte
,
443 uap
->offset
, FOF_OFFSET
, retval
);
447 fp_drop_written(p
, fd
, fp
);
449 fp_drop(p
, fd
, fp
, 0);
452 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pwrite
) | DBG_FUNC_NONE
),
453 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
458 __private_extern__
int
459 dofilewrite(p
, fp
, fd
, bufp
, nbyte
, offset
, flags
, retval
)
466 user_ssize_t
*retval
;
470 user_ssize_t bytecnt
;
471 char uio_buf
[ UIO_SIZEOF(1) ];
475 char ktr_uio_buf
[ UIO_SIZEOF(1) ];
478 // LP64todo - do we want to raise this?
482 if (IS_64BIT_PROCESS(p
)) {
483 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_WRITE
,
484 &uio_buf
[0], sizeof(uio_buf
));
486 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_WRITE
,
487 &uio_buf
[0], sizeof(uio_buf
));
489 uio_addiov(auio
, bufp
, nbyte
);
493 * if tracing, save a copy of iovec and uio
495 if (KTRPOINT(p
, KTR_GENIO
)) {
498 if (IS_64BIT_PROCESS(p
)) {
499 ktruio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_WRITE
,
500 &ktr_uio_buf
[0], sizeof(ktr_uio_buf
));
502 ktruio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_WRITE
,
503 &ktr_uio_buf
[0], sizeof(ktr_uio_buf
));
505 uio_addiov(ktruio
, bufp
, nbyte
);
509 if ((error
= fo_write(fp
, auio
, fp
->f_cred
, flags
, p
))) {
510 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
511 error
== EINTR
|| error
== EWOULDBLOCK
))
513 /* The socket layer handles SIGPIPE */
514 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
)
517 bytecnt
-= uio_resid(auio
);
519 if (didktr
&& error
== 0) {
520 uio_setresid(ktruio
, bytecnt
);
521 ktrgenio(p
->p_tracep
, fd
, UIO_WRITE
, ktruio
, error
);
530 * Gather write system call
533 writev(p
, uap
, retval
)
535 register struct writev_args
*uap
;
536 user_ssize_t
*retval
;
541 struct user_iovec
*iovp
;
543 /* Verify range bedfore calling uio_create() */
544 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
547 /* allocate a uio large enough to hold the number of iovecs passed */
548 auio
= uio_create(uap
->iovcnt
, 0,
549 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
552 /* get location of iovecs within the uio. then copyin the iovecs from
555 iovp
= uio_iovsaddr(auio
);
558 goto ExitThisRoutine
;
560 size_of_iovec
= (IS_64BIT_PROCESS(p
) ? sizeof(struct user_iovec
) : sizeof(struct iovec
));
561 error
= copyin(uap
->iovp
, (caddr_t
)iovp
, (uap
->iovcnt
* size_of_iovec
));
563 goto ExitThisRoutine
;
566 /* finalize uio_t for use and do the IO
568 uio_calculateresid(auio
);
569 error
= wr_uio(p
, uap
->fd
, auio
, retval
);
580 wr_uio(p
, fdes
, uio
, retval
)
584 user_ssize_t
*retval
;
590 struct iovec_64
*ktriov
= NULL
;
596 error
= fp_lookup(p
,fdes
,&fp
,0);
600 if ((fp
->f_flag
& FWRITE
) == 0) {
604 count
= uio_resid(uio
);
607 * if tracing, save a copy of iovec
609 if (KTRPOINT(p
, KTR_GENIO
)) {
610 iovlen
= uio
->uio_iovcnt
*
611 (IS_64BIT_PROCESS(p
) ? sizeof (struct iovec_64
) : sizeof (struct iovec_32
));
612 MALLOC(ktriov
, struct iovec_64
*, iovlen
, M_TEMP
, M_WAITOK
);
613 if (ktriov
!= NULL
) {
614 bcopy((caddr_t
)uio
->uio_iovs
.iov64p
, (caddr_t
)ktriov
, iovlen
);
620 error
= fo_write(fp
, uio
, fp
->f_cred
, 0, p
);
622 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
623 error
== EINTR
|| error
== EWOULDBLOCK
))
625 /* The socket layer handles SIGPIPE */
626 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
)
629 *retval
= count
- uio_resid(uio
);
634 ktruio
.uio_iovs
.iov64p
= ktriov
;
635 uio_setresid(&ktruio
, *retval
);
636 ktrgenio(p
->p_tracep
, fdes
, UIO_WRITE
, &ktruio
, error
);
638 FREE(ktriov
, M_TEMP
);
644 fp_drop_written(p
, fdes
, fp
);
646 fp_drop(p
, fdes
, fp
, 0);
652 rd_uio(p
, fdes
, uio
, retval
)
656 user_ssize_t
*retval
;
662 struct iovec_64
*ktriov
= NULL
;
668 if ( (error
= preparefileread(p
, &fp
, fdes
, 0)) )
671 count
= uio_resid(uio
);
674 * if tracing, save a copy of iovec
676 if (KTRPOINT(p
, KTR_GENIO
)) {
677 iovlen
= uio
->uio_iovcnt
*
678 (IS_64BIT_PROCESS(p
) ? sizeof (struct iovec_64
) : sizeof (struct iovec_32
));
679 MALLOC(ktriov
, struct iovec_64
*, iovlen
, M_TEMP
, M_WAITOK
);
680 if (ktriov
!= NULL
) {
681 bcopy((caddr_t
)uio
->uio_iovs
.iov64p
, (caddr_t
)ktriov
, iovlen
);
687 error
= fo_read(fp
, uio
, fp
->f_cred
, 0, p
);
690 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
691 error
== EINTR
|| error
== EWOULDBLOCK
))
694 *retval
= count
- uio_resid(uio
);
699 ktruio
.uio_iovs
.iov64p
= ktriov
;
700 uio_setresid(&ktruio
, *retval
);
701 ktrgenio(p
->p_tracep
, fdes
, UIO_READ
, &ktruio
, error
);
703 FREE(ktriov
, M_TEMP
);
706 donefileread(p
, fp
, fdes
);
716 ioctl(struct proc
*p
, register struct ioctl_args
*uap
, __unused register_t
*retval
)
725 #define STK_PARAMS 128
726 char stkbuf
[STK_PARAMS
];
729 AUDIT_ARG(fd
, uap
->fd
);
730 AUDIT_ARG(cmd
, CAST_DOWN(int, uap
->com
)); /* LP64todo: uap->com is a user-land long */
731 AUDIT_ARG(addr
, uap
->data
);
733 is64bit
= proc_is64bit(p
);
736 error
= fp_lookup(p
,fd
,&fp
,1);
742 AUDIT_ARG(file
, p
, fp
);
744 if ((fp
->f_flag
& (FREAD
| FWRITE
)) == 0) {
751 * ### LD 6/11/97 Hack Alert: this is to get AppleTalk to work
752 * while implementing an ATioctl system call
755 if (appletalk_inited
&& ((uap
->com
& 0x0000FFFF) == 0xff99)) {
756 u_long fixed_command
;
757 #ifdef APPLETALK_DEBUG
758 kprintf("ioctl: special AppleTalk \n");
761 *(user_addr_t
*)datap
= uap
->data
;
762 fixed_command
= _IOW(0, 0xff99, uap
->data
);
763 error
= fo_ioctl(fp
, fixed_command
, datap
, p
);
771 switch (com
= uap
->com
) {
773 *fdflags(p
, uap
->fd
) &= ~UF_EXCLOSE
;
777 *fdflags(p
, uap
->fd
) |= UF_EXCLOSE
;
783 * Interpret high order word to find amount of data to be
784 * copied to/from the user's address space.
786 size
= IOCPARM_LEN(com
);
787 if (size
> IOCPARM_MAX
) {
792 if (size
> sizeof (stkbuf
)) {
794 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
806 error
= copyin(uap
->data
, datap
, size
);
815 /* XXX - IOC_IN and no size? we should proably return an error here!! */
817 *(user_addr_t
*)datap
= uap
->data
;
820 *(uint32_t *)datap
= (uint32_t)uap
->data
;
823 } else if ((com
&IOC_OUT
) && size
)
825 * Zero the buffer so the user always
826 * gets back something deterministic.
829 else if (com
&IOC_VOID
) {
830 /* XXX - this is odd since IOC_VOID means no parameters */
832 *(user_addr_t
*)datap
= uap
->data
;
835 *(uint32_t *)datap
= (uint32_t)uap
->data
;
842 if ( (tmp
= *(int *)datap
) )
843 fp
->f_flag
|= FNONBLOCK
;
845 fp
->f_flag
&= ~FNONBLOCK
;
846 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
850 if ( (tmp
= *(int *)datap
) )
851 fp
->f_flag
|= FASYNC
;
853 fp
->f_flag
&= ~FASYNC
;
854 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, p
);
859 if (fp
->f_type
== DTYPE_SOCKET
) {
860 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
864 if (fp
->f_type
== DTYPE_PIPE
) {
865 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, p
);
871 struct proc
*p1
= pfind(tmp
);
876 tmp
= p1
->p_pgrp
->pg_id
;
878 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, p
);
882 if (fp
->f_type
== DTYPE_SOCKET
) {
884 *(int *)datap
= ((struct socket
*)fp
->f_data
)->so_pgid
;
887 error
= fo_ioctl(fp
, TIOCGPGRP
, datap
, p
);
888 *(int *)datap
= -*(int *)datap
;
892 error
= fo_ioctl(fp
, com
, datap
, p
);
894 * Copy any data to user, size was
895 * already set and checked above.
897 if (error
== 0 && (com
&IOC_OUT
) && size
)
898 error
= copyout(datap
, uap
->data
, (u_int
)size
);
906 fp_drop(p
, fd
, fp
, 1);
911 int selwait
, nselcoll
;
912 #define SEL_FIRSTPASS 1
913 #define SEL_SECONDPASS 2
914 extern int selcontinue(int error
);
915 extern int selprocess(int error
, int sel_pass
);
916 static int selscan(struct proc
*p
, struct _select
* sel
,
917 int nfd
, register_t
*retval
, int sel_pass
, wait_queue_sub_t wqsub
);
918 static int selcount(struct proc
*p
, u_int32_t
*ibits
, u_int32_t
*obits
,
919 int nfd
, int * count
);
920 static int seldrop(struct proc
*p
, u_int32_t
*ibits
, int nfd
);
921 extern uint64_t tvtoabstime(struct timeval
*tvp
);
924 * Select system call.
927 select(struct proc
*p
, struct select_args
*uap
, register_t
*retval
)
934 int needzerofill
= 1;
937 th_act
= current_thread();
938 uth
= get_bsdthread_info(th_act
);
939 sel
= &uth
->uu_select
;
940 retval
= (int *)get_bsduthreadrval(th_act
);
947 if (uap
->nd
> p
->p_fd
->fd_nfiles
)
948 uap
->nd
= p
->p_fd
->fd_nfiles
; /* forgiving; slightly wrong */
950 nw
= howmany(uap
->nd
, NFDBITS
);
951 ni
= nw
* sizeof(fd_mask
);
954 * if this is the first select by the thread
955 * allocate the space for bits.
957 if (sel
->nbytes
== 0) {
958 sel
->nbytes
= 3 * ni
;
959 MALLOC(sel
->ibits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
960 MALLOC(sel
->obits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
961 if ((sel
->ibits
== NULL
) || (sel
->obits
== NULL
))
962 panic("select out of memory");
967 * if the previously allocated space for the bits
968 * is smaller than what is requested. Reallocate.
970 if (sel
->nbytes
< (3 * ni
)) {
971 sel
->nbytes
= (3 * ni
);
972 FREE(sel
->ibits
, M_TEMP
);
973 FREE(sel
->obits
, M_TEMP
);
974 MALLOC(sel
->ibits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
975 MALLOC(sel
->obits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
976 if ((sel
->ibits
== NULL
) || (sel
->obits
== NULL
))
977 panic("select out of memory");
982 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
983 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
987 * get the bits from the user address space
989 #define getbits(name, x) \
991 if (uap->name && (error = copyin(uap->name, \
992 (caddr_t)&sel->ibits[(x) * nw], ni))) \
1003 if (IS_64BIT_PROCESS(p
)) {
1004 struct user_timeval atv64
;
1005 error
= copyin(uap
->tv
, (caddr_t
)&atv64
, sizeof(atv64
));
1006 /* Loses resolution - assume timeout < 68 years */
1007 atv
.tv_sec
= atv64
.tv_sec
;
1008 atv
.tv_usec
= atv64
.tv_usec
;
1010 error
= copyin(uap
->tv
, (caddr_t
)&atv
, sizeof(atv
));
1014 if (itimerfix(&atv
)) {
1019 clock_absolutetime_interval_to_deadline(
1020 tvtoabstime(&atv
), &sel
->abstime
);
1025 if ( (error
= selcount(p
, sel
->ibits
, sel
->obits
, uap
->nd
, &count
)) ) {
1030 size
= SIZEOF_WAITQUEUE_SET
+ (count
* SIZEOF_WAITQUEUE_LINK
);
1031 if (sel
->allocsize
) {
1032 if (sel
->wqset
== 0)
1033 panic("select: wql memory smashed");
1034 /* needed for the select now */
1035 if (size
> sel
->allocsize
) {
1036 kfree(sel
->wqset
, sel
->allocsize
);
1037 sel
->allocsize
= size
;
1038 sel
->wqset
= (wait_queue_set_t
)kalloc(size
);
1039 if (sel
->wqset
== (wait_queue_set_t
)NULL
)
1040 panic("failed to allocate memory for waitqueue\n");
1044 sel
->allocsize
= size
;
1045 sel
->wqset
= (wait_queue_set_t
)kalloc(sel
->allocsize
);
1046 if (sel
->wqset
== (wait_queue_set_t
)NULL
)
1047 panic("failed to allocate memory for waitqueue\n");
1049 bzero(sel
->wqset
, size
);
1050 sel
->wql
= (char *)sel
->wqset
+ SIZEOF_WAITQUEUE_SET
;
1051 wait_queue_set_init(sel
->wqset
, (SYNC_POLICY_FIFO
| SYNC_POLICY_PREPOST
));
1054 return selprocess(error
, SEL_FIRSTPASS
);
1058 selcontinue(int error
)
1060 return selprocess(error
, SEL_SECONDPASS
);
1064 selprocess(int error
, int sel_pass
)
1069 struct uthread
*uth
;
1071 struct select_args
*uap
;
1073 struct _select
*sel
;
1078 wait_result_t wait_result
;
1081 th_act
= current_thread();
1082 uap
= (struct select_args
*)get_bsduthreadarg(th_act
);
1083 retval
= (int *)get_bsduthreadrval(th_act
);
1084 uth
= get_bsdthread_info(th_act
);
1085 sel
= &uth
->uu_select
;
1087 /* if it is first pass wait queue is not setup yet */
1088 if ((error
!= 0) && (sel_pass
== SEL_FIRSTPASS
))
1090 if (sel
->count
== 0)
1098 p
->p_flag
|= P_SELECT
;
1099 /* skip scans if the select is just for timeouts */
1101 if (sel_pass
== SEL_FIRSTPASS
)
1102 wait_queue_sub_clearrefs(sel
->wqset
);
1104 error
= selscan(p
, sel
, uap
->nd
, retval
, sel_pass
, sel
->wqset
);
1105 if (error
|| *retval
) {
1109 /* if the select of log, then we canwakeup and discover some one
1110 * else already read the data; go toselct again if time permits
1124 clock_get_uptime(&now
);
1125 if (now
>= sel
->abstime
)
1130 /* cleanup obits and try again */
1132 sel_pass
= SEL_FIRSTPASS
;
1137 * To effect a poll, the timeout argument should be
1138 * non-nil, pointing to a zero-valued timeval structure.
1140 if (uap
->tv
&& sel
->abstime
== 0) {
1144 /* No spurious wakeups due to colls,no need to check for them */
1145 if ((sel_pass
== SEL_SECONDPASS
) || ((p
->p_flag
& P_SELECT
) == 0)) {
1146 sel_pass
= SEL_FIRSTPASS
;
1150 p
->p_flag
&= ~P_SELECT
;
1152 /* if the select is just for timeout skip check */
1153 if (sel
->count
&&(sel_pass
== SEL_SECONDPASS
))
1154 panic("selprocess: 2nd pass assertwaiting");
1156 /* Wait Queue Subordinate has waitqueue as first element */
1157 wait_result
= wait_queue_assert_wait((wait_queue_t
)sel
->wqset
,
1158 &selwait
, THREAD_ABORTSAFE
, sel
->abstime
);
1159 if (wait_result
!= THREAD_AWAKENED
) {
1160 /* there are no preposted events */
1161 error
= tsleep1(NULL
, PSOCK
| PCATCH
,
1162 "select", 0, selcontinue
);
1168 sel_pass
= SEL_SECONDPASS
;
1176 wait_subqueue_unlink_all(sel
->wqset
);
1177 seldrop(p
, sel
->ibits
, uap
->nd
);
1179 p
->p_flag
&= ~P_SELECT
;
1180 /* select is not restarted after signals... */
1181 if (error
== ERESTART
)
1183 if (error
== EWOULDBLOCK
)
1185 nw
= howmany(uap
->nd
, NFDBITS
);
1186 ni
= nw
* sizeof(fd_mask
);
1188 #define putbits(name, x) \
1190 if (uap->name && (error2 = \
1191 copyout((caddr_t)&sel->obits[(x) * nw], uap->name, ni))) \
1207 selscan(p
, sel
, nfd
, retval
, sel_pass
, wqsub
)
1209 struct _select
*sel
;
1213 wait_queue_sub_t wqsub
;
1215 register struct filedesc
*fdp
= p
->p_fd
;
1216 register int msk
, i
, j
, fd
;
1217 register u_int32_t bits
;
1218 struct fileproc
*fp
;
1221 static int flag
[3] = { FREAD
, FWRITE
, 0 };
1222 u_int32_t
*iptr
, *optr
;
1224 u_int32_t
*ibits
, *obits
;
1229 * Problems when reboot; due to MacOSX signal probs
1230 * in Beaker1C ; verify that the p->p_fd is valid
1240 nw
= howmany(nfd
, NFDBITS
);
1246 for (msk
= 0; msk
< 3; msk
++) {
1247 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1248 optr
= (u_int32_t
*)&obits
[msk
* nw
];
1250 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1251 bits
= iptr
[i
/NFDBITS
];
1253 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1255 fp
= fdp
->fd_ofiles
[fd
];
1258 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1262 if (sel_pass
== SEL_SECONDPASS
) {
1263 wql_ptr
= (char *)0;
1264 fp
->f_flags
&= ~FP_INSELECT
;
1265 fp
->f_waddr
= (void *)0;
1267 wql_ptr
= (wql
+ nc
* SIZEOF_WAITQUEUE_LINK
);
1268 fp
->f_flags
|= FP_INSELECT
;
1269 fp
->f_waddr
= (void *)wqsub
;
1271 if (fp
->f_ops
&& fo_select(fp
, flag
[msk
], wql_ptr
, p
)) {
1272 optr
[fd
/NFDBITS
] |= (1 << (fd
% NFDBITS
));
1285 static int poll_callback(struct kqueue
*, struct kevent
*, void *);
1287 struct poll_continue_args
{
1288 user_addr_t pca_fds
;
1294 poll(struct proc
*p
, struct poll_args
*uap
, register_t
*retval
)
1296 struct poll_continue_args
*cont
;
1300 int ncoll
, error
= 0;
1301 u_int nfds
= uap
->nfds
;
1307 * This is kinda bogus. We have fd limits, but that is not
1308 * really related to the size of the pollfd array. Make sure
1309 * we let the process use at least FD_SETSIZE entries and at
1310 * least enough for the current limits. We want to be reasonably
1311 * safe, but not overly restrictive.
1313 if (nfds
> OPEN_MAX
||
1314 (nfds
> p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
&& nfds
> FD_SETSIZE
))
1317 kq
= kqueue_alloc(p
);
1321 ni
= nfds
* sizeof(struct pollfd
) + sizeof(struct poll_continue_args
);
1322 MALLOC(cont
, struct poll_continue_args
*, ni
, M_TEMP
, M_WAITOK
);
1328 fds
= (struct pollfd
*)&cont
[1];
1329 error
= copyin(uap
->fds
, fds
, nfds
* sizeof(struct pollfd
));
1333 if (uap
->timeout
!= -1) {
1336 atv
.tv_sec
= uap
->timeout
/ 1000;
1337 atv
.tv_usec
= (uap
->timeout
% 1000) * 1000;
1338 if (itimerfix(&atv
)) {
1342 getmicrouptime(&rtv
);
1343 timevaladd(&atv
, &rtv
);
1349 /* JMM - all this P_SELECT stuff is bogus */
1351 p
->p_flag
|= P_SELECT
;
1353 for (i
= 0; i
< nfds
; i
++) {
1354 short events
= fds
[i
].events
;
1358 /* per spec, ignore fd values below zero */
1359 if (fds
[i
].fd
< 0) {
1364 /* convert the poll event into a kqueue kevent */
1365 kev
.ident
= fds
[i
].fd
;
1366 kev
.flags
= EV_ADD
| EV_ONESHOT
| EV_POLL
;
1367 kev
.fflags
= NOTE_LOWAT
;
1368 kev
.data
= 1; /* efficiency be damned: any data should trigger */
1369 kev
.udata
= CAST_USER_ADDR_T(&fds
[i
]);
1371 /* Handle input events */
1372 if (events
& ( POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
)) {
1373 kev
.filter
= EVFILT_READ
;
1374 if (!(events
& ( POLLIN
| POLLRDNORM
)))
1375 kev
.flags
|= EV_OOBAND
;
1376 kerror
= kevent_register(kq
, &kev
, p
);
1379 /* Handle output events */
1381 events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
)) {
1382 kev
.filter
= EVFILT_WRITE
;
1383 kerror
= kevent_register(kq
, &kev
, p
);
1386 /* Handle BSD extension vnode events */
1388 events
& ( POLLEXTEND
| POLLATTRIB
| POLLNLINK
| POLLWRITE
)) {
1389 kev
.filter
= EVFILT_VNODE
;
1391 if (events
& POLLEXTEND
)
1392 kev
.fflags
|= NOTE_EXTEND
;
1393 if (events
& POLLATTRIB
)
1394 kev
.fflags
|= NOTE_ATTRIB
;
1395 if (events
& POLLNLINK
)
1396 kev
.fflags
|= NOTE_LINK
;
1397 if (events
& POLLWRITE
)
1398 kev
.fflags
|= NOTE_WRITE
;
1399 kerror
= kevent_register(kq
, &kev
, p
);
1403 fds
[i
].revents
= POLLNVAL
;
1409 /* Did we have any trouble registering? */
1413 /* scan for, and possibly wait for, the kevents to trigger */
1414 cont
->pca_fds
= uap
->fds
;
1415 cont
->pca_nfds
= nfds
;
1416 cont
->pca_rfds
= rfds
;
1417 error
= kevent_scan(kq
, poll_callback
, NULL
, cont
, &atv
, p
);
1418 rfds
= cont
->pca_rfds
;
1421 p
->p_flag
&= ~P_SELECT
;
1422 /* poll is not restarted after signals... */
1423 if (error
== ERESTART
)
1425 if (error
== EWOULDBLOCK
)
1428 error
= copyout(fds
, uap
->fds
, nfds
* sizeof(struct pollfd
));
1436 kqueue_dealloc(kq
, p
);
1441 poll_callback(__unused
struct kqueue
*kq
, struct kevent
*kevp
, void *data
)
1443 struct poll_continue_args
*cont
= (struct poll_continue_args
*)data
;
1444 struct pollfd
*fds
= CAST_DOWN(struct pollfd
*, kevp
->udata
);
1447 /* convert the results back into revents */
1448 if (kevp
->flags
& EV_EOF
)
1449 fds
->revents
|= POLLHUP
;
1450 if (kevp
->flags
& EV_ERROR
)
1451 fds
->revents
|= POLLERR
;
1454 switch (kevp
->filter
) {
1456 if (fds
->revents
& POLLHUP
)
1457 mask
= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
);
1460 if (kevp
->data
!= 0)
1461 mask
|= (POLLIN
| POLLRDNORM
);
1462 if (kevp
->flags
& EV_OOBAND
)
1463 mask
|= ( POLLPRI
| POLLRDBAND
);
1465 fds
->revents
|= (fds
->events
& mask
);
1469 if (!(fds
->revents
& POLLHUP
))
1470 fds
->revents
|= (fds
->events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
));
1474 if (kevp
->fflags
& NOTE_EXTEND
)
1475 fds
->revents
|= (fds
->events
& POLLEXTEND
);
1476 if (kevp
->fflags
& NOTE_ATTRIB
)
1477 fds
->revents
|= (fds
->events
& POLLATTRIB
);
1478 if (kevp
->fflags
& NOTE_LINK
)
1479 fds
->revents
|= (fds
->events
& POLLNLINK
);
1480 if (kevp
->fflags
& NOTE_WRITE
)
1481 fds
->revents
|= (fds
->events
& POLLWRITE
);
1488 seltrue(__unused dev_t dev
, __unused
int flag
, __unused
struct proc
*p
)
1495 selcount(struct proc
*p
, u_int32_t
*ibits
, __unused u_int32_t
*obits
,
1496 int nfd
, int *count
)
1498 register struct filedesc
*fdp
= p
->p_fd
;
1499 register int msk
, i
, j
, fd
;
1500 register u_int32_t bits
;
1501 struct fileproc
*fp
;
1509 * Problems when reboot; due to MacOSX signal probs
1510 * in Beaker1C ; verify that the p->p_fd is valid
1516 nw
= howmany(nfd
, NFDBITS
);
1519 for (msk
= 0; msk
< 3; msk
++) {
1520 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1521 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1522 bits
= iptr
[i
/NFDBITS
];
1523 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1525 fp
= fdp
->fd_ofiles
[fd
];
1527 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1546 /* undo the iocounts */
1547 for (msk
= 0; msk
< 3; msk
++) {
1548 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1549 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1550 bits
= iptr
[i
/NFDBITS
];
1551 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1553 fp
= fdp
->fd_ofiles
[fd
];
1558 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
1559 p
->p_fpdrainwait
= 0;
1560 wakeup(&p
->p_fpdrainwait
);
1572 seldrop(p
, ibits
, nfd
)
1577 register struct filedesc
*fdp
= p
->p_fd
;
1578 register int msk
, i
, j
, fd
;
1579 register u_int32_t bits
;
1580 struct fileproc
*fp
;
1586 * Problems when reboot; due to MacOSX signal probs
1587 * in Beaker1C ; verify that the p->p_fd is valid
1593 nw
= howmany(nfd
, NFDBITS
);
1597 for (msk
= 0; msk
< 3; msk
++) {
1598 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1599 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1600 bits
= iptr
[i
/NFDBITS
];
1601 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1603 fp
= fdp
->fd_ofiles
[fd
];
1606 /* if you are here then it is being closed */
1607 || (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)
1615 fp
->f_flags
&= ~FP_INSELECT
;
1617 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
1618 p
->p_fpdrainwait
= 0;
1619 wakeup(&p
->p_fpdrainwait
);
1629 * Record a select request.
1632 selrecord(__unused
struct proc
*selector
, struct selinfo
*sip
, void * p_wql
)
1634 thread_t cur_act
= current_thread();
1635 struct uthread
* ut
= get_bsdthread_info(cur_act
);
1637 /* need to look at collisions */
1639 if ((p_wql
== (void *)0) && ((sip
->si_flags
& SI_INITED
) == 0)) {
1643 /*do not record if this is second pass of select */
1644 if((p_wql
== (void *)0)) {
1648 if ((sip
->si_flags
& SI_INITED
) == 0) {
1649 wait_queue_init(&sip
->si_wait_queue
, SYNC_POLICY_FIFO
);
1650 sip
->si_flags
|= SI_INITED
;
1651 sip
->si_flags
&= ~SI_CLEAR
;
1654 if (sip
->si_flags
& SI_RECORDED
) {
1655 sip
->si_flags
|= SI_COLL
;
1657 sip
->si_flags
&= ~SI_COLL
;
1659 sip
->si_flags
|= SI_RECORDED
;
1660 if (!wait_queue_member(&sip
->si_wait_queue
, ut
->uu_select
.wqset
))
1661 wait_queue_link_noalloc(&sip
->si_wait_queue
, ut
->uu_select
.wqset
,
1662 (wait_queue_link_t
)p_wql
);
1669 register struct selinfo
*sip
;
1672 if ((sip
->si_flags
& SI_INITED
) == 0) {
1676 if (sip
->si_flags
& SI_COLL
) {
1678 sip
->si_flags
&= ~SI_COLL
;
1680 /* will not support */
1681 //wakeup((caddr_t)&selwait);
1685 if (sip
->si_flags
& SI_RECORDED
) {
1686 wait_queue_wakeup_all(&sip
->si_wait_queue
, &selwait
, THREAD_AWAKENED
);
1687 sip
->si_flags
&= ~SI_RECORDED
;
1694 register struct selinfo
*sip
;
1697 if ((sip
->si_flags
& SI_INITED
) == 0) {
1700 if (sip
->si_flags
& SI_RECORDED
) {
1702 sip
->si_flags
&= ~(SI_RECORDED
| SI_COLL
);
1704 sip
->si_flags
|= SI_CLEAR
;
1705 wait_queue_unlinkall_nofree(&sip
->si_wait_queue
);
1711 #define DBG_EVENT 0x10
1713 #define DBG_POST 0x10
1714 #define DBG_WATCH 0x11
1715 #define DBG_WAIT 0x12
1716 #define DBG_MOD 0x13
1717 #define DBG_EWAKEUP 0x14
1718 #define DBG_ENQUEUE 0x15
1719 #define DBG_DEQUEUE 0x16
1721 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
1722 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
1723 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
1724 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
1725 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
1726 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
1727 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
1730 #define EVPROCDEQUE(p, evq) do { \
1732 if (evq->ee_flags & EV_QUEUED) { \
1733 TAILQ_REMOVE(&p->p_evlist, evq, ee_plist); \
1734 evq->ee_flags &= ~EV_QUEUED; \
1741 * called upon socket close. deque and free all events for
1742 * the socket... socket must be locked by caller.
1745 evsofree(struct socket
*sp
)
1747 struct eventqelt
*evq
, *next
;
1753 for (evq
= sp
->so_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
1754 next
= evq
->ee_slist
.tqe_next
;
1757 if (evq
->ee_flags
& EV_QUEUED
) {
1758 EVPROCDEQUE(p
, evq
);
1760 TAILQ_REMOVE(&sp
->so_evlist
, evq
, ee_slist
); // remove from socket q
1767 * called upon pipe close. deque and free all events for
1768 * the pipe... pipe must be locked by caller
1771 evpipefree(struct pipe
*cpipe
)
1773 struct eventqelt
*evq
, *next
;
1776 for (evq
= cpipe
->pipe_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
1777 next
= evq
->ee_slist
.tqe_next
;
1780 EVPROCDEQUE(p
, evq
);
1782 TAILQ_REMOVE(&cpipe
->pipe_evlist
, evq
, ee_slist
); // remove from pipe q
1789 * enqueue this event if it's not already queued. wakeup
1790 * the proc if we do queue this event to it...
1791 * entered with proc lock held... we drop it before
1792 * doing the wakeup and return in that state
1795 evprocenque(struct eventqelt
*evq
)
1802 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_START
, evq
, evq
->ee_flags
, evq
->ee_eventmask
,0,0);
1806 if (evq
->ee_flags
& EV_QUEUED
) {
1809 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1812 evq
->ee_flags
|= EV_QUEUED
;
1814 TAILQ_INSERT_TAIL(&p
->p_evlist
, evq
, ee_plist
);
1818 wakeup(&p
->p_evlist
);
1820 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1825 * pipe lock must be taken by the caller
1828 postpipeevent(struct pipe
*pipep
, int event
)
1831 struct eventqelt
*evq
;
1835 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, event
,0,0,1,0);
1837 for (evq
= pipep
->pipe_evlist
.tqh_first
;
1838 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1840 if (evq
->ee_eventmask
== 0)
1844 switch (event
& (EV_RWBYTES
| EV_RCLOSED
| EV_WCLOSED
)) {
1847 if ((evq
->ee_eventmask
& EV_RE
) && pipep
->pipe_buffer
.cnt
) {
1849 evq
->ee_req
.er_rcnt
= pipep
->pipe_buffer
.cnt
;
1851 if ((evq
->ee_eventmask
& EV_WR
) &&
1852 (pipep
->pipe_buffer
.size
- pipep
->pipe_buffer
.cnt
) >= PIPE_BUF
) {
1854 if (pipep
->pipe_state
& PIPE_EOF
) {
1855 mask
|= EV_WR
|EV_RESET
;
1859 evq
->ee_req
.er_wcnt
= pipep
->pipe_buffer
.size
- pipep
->pipe_buffer
.cnt
;
1865 if ((evq
->ee_eventmask
& EV_RE
)) {
1866 mask
|= EV_RE
|EV_RCLOSED
;
1868 if ((evq
->ee_eventmask
& EV_WR
)) {
1869 mask
|= EV_WR
|EV_WCLOSED
;
1878 * disarm... postevents are nops until this event is 'read' via
1879 * waitevent and then re-armed via modwatch
1881 evq
->ee_eventmask
= 0;
1884 * since events are disarmed until after the waitevent
1885 * the ee_req.er_xxxx fields can't change once we've
1886 * inserted this event into the proc queue...
1887 * therefore, the waitevent will see a 'consistent'
1888 * snapshot of the event, even though it won't hold
1889 * the pipe lock, and we're updating the event outside
1890 * of the proc lock, which it will hold
1892 evq
->ee_req
.er_eventbits
|= mask
;
1894 KERNEL_DEBUG(DBG_MISC_POST
, evq
, evq
->ee_req
.er_eventbits
, mask
, 1,0);
1899 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, 0,0,0,1,0);
1904 * given either a sockbuf or a socket run down the
1905 * event list and queue ready events found...
1906 * the socket must be locked by the caller
1909 postevent(struct socket
*sp
, struct sockbuf
*sb
, int event
)
1912 struct eventqelt
*evq
;
1920 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, (int)sp
, event
, 0, 0, 0);
1922 for (evq
= sp
->so_evlist
.tqh_first
;
1923 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1925 if (evq
->ee_eventmask
== 0)
1929 /* ready for reading:
1930 - byte cnt >= receive low water mark
1931 - read-half of conn closed
1932 - conn pending for listening sock
1933 - socket error pending
1936 - byte cnt avail >= send low water mark
1937 - write half of conn closed
1938 - socket error pending
1939 - non-blocking conn completed successfully
1943 - sock at out of band mark
1946 switch (event
& EV_DMASK
) {
1949 if ((evq
->ee_eventmask
& EV_EX
)) {
1950 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
1951 mask
|= EV_EX
|EV_OOB
;
1955 case EV_RWBYTES
|EV_OOB
:
1956 if ((evq
->ee_eventmask
& EV_EX
)) {
1957 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
1958 mask
|= EV_EX
|EV_OOB
;
1961 * fall into the next case
1964 if ((evq
->ee_eventmask
& EV_RE
) && soreadable(sp
)) {
1966 if ((sp
->so_type
== SOCK_STREAM
) && ((sp
->so_error
== ECONNREFUSED
) || (sp
->so_error
== ECONNRESET
))) {
1967 if ((sp
->so_pcb
== 0) || (((struct inpcb
*)sp
->so_pcb
)->inp_state
== INPCB_STATE_DEAD
) || !(tp
= sototcpcb(sp
)) ||
1968 (tp
->t_state
== TCPS_CLOSED
)) {
1969 mask
|= EV_RE
|EV_RESET
;
1975 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
1977 if (sp
->so_state
& SS_CANTRCVMORE
) {
1982 if ((evq
->ee_eventmask
& EV_WR
) && sowriteable(sp
)) {
1984 if ((sp
->so_type
== SOCK_STREAM
) && ((sp
->so_error
== ECONNREFUSED
) || (sp
->so_error
== ECONNRESET
))) {
1985 if ((sp
->so_pcb
== 0) || (((struct inpcb
*)sp
->so_pcb
)->inp_state
== INPCB_STATE_DEAD
) || !(tp
= sototcpcb(sp
)) ||
1986 (tp
->t_state
== TCPS_CLOSED
)) {
1987 mask
|= EV_WR
|EV_RESET
;
1993 evq
->ee_req
.er_wcnt
= sbspace(&sp
->so_snd
);
1998 if ((evq
->ee_eventmask
& EV_RE
)) {
1999 mask
|= EV_RE
|EV_RCONN
;
2000 evq
->ee_req
.er_rcnt
= sp
->so_qlen
+ 1; // incl this one
2005 if ((evq
->ee_eventmask
& EV_WR
)) {
2006 mask
|= EV_WR
|EV_WCONN
;
2011 if ((evq
->ee_eventmask
& EV_RE
)) {
2012 mask
|= EV_RE
|EV_RCLOSED
;
2017 if ((evq
->ee_eventmask
& EV_WR
)) {
2018 mask
|= EV_WR
|EV_WCLOSED
;
2023 if (evq
->ee_eventmask
& EV_RE
) {
2024 mask
|= EV_RE
|EV_FIN
;
2030 if (evq
->ee_eventmask
& EV_RE
) {
2031 mask
|= EV_RE
| event
;
2033 if (evq
->ee_eventmask
& EV_WR
) {
2034 mask
|= EV_WR
| event
;
2039 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, -1, 0, 0, 0);
2043 KERNEL_DEBUG(DBG_MISC_POST
, (int)evq
, evq
->ee_eventmask
, evq
->ee_req
.er_eventbits
, mask
, 0);
2047 * disarm... postevents are nops until this event is 'read' via
2048 * waitevent and then re-armed via modwatch
2050 evq
->ee_eventmask
= 0;
2053 * since events are disarmed until after the waitevent
2054 * the ee_req.er_xxxx fields can't change once we've
2055 * inserted this event into the proc queue...
2056 * since waitevent can't see this event until we
2057 * enqueue it, waitevent will see a 'consistent'
2058 * snapshot of the event, even though it won't hold
2059 * the socket lock, and we're updating the event outside
2060 * of the proc lock, which it will hold
2062 evq
->ee_req
.er_eventbits
|= mask
;
2067 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, 0, 0, 0, 0);
2072 * watchevent system call. user passes us an event to watch
2073 * for. we malloc an event object, initialize it, and queue
2074 * it to the open socket. when the event occurs, postevent()
2075 * will enque it back to our proc where we can retrieve it
2078 * should this prevent duplicate events on same socket?
2081 watchevent(proc_t p
, struct watchevent_args
*uap
, __unused
int *retval
)
2083 struct eventqelt
*evq
= (struct eventqelt
*)0;
2084 struct eventqelt
*np
= NULL
;
2085 struct eventreq
*erp
;
2086 struct fileproc
*fp
= NULL
;
2089 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_START
, 0,0,0,0,0);
2091 // get a qelt and fill with users req
2092 MALLOC(evq
, struct eventqelt
*, sizeof(struct eventqelt
), M_TEMP
, M_WAITOK
);
2095 panic("can't MALLOC evq");
2098 // get users request pkt
2099 if ( (error
= copyin(CAST_USER_ADDR_T(uap
->u_req
), (caddr_t
)erp
,
2100 sizeof(struct eventreq
))) ) {
2103 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2106 KERNEL_DEBUG(DBG_MISC_WATCH
, erp
->er_handle
,uap
->u_eventmask
,evq
,0,0);
2108 // validate, freeing qelt if errors
2112 if (erp
->er_type
!= EV_FD
) {
2114 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2116 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2117 socket_lock((struct socket
*)fp
->f_data
, 1);
2118 np
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2119 } else if (fp
->f_type
== DTYPE_PIPE
) {
2120 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2121 np
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2123 fp_drop(p
, erp
->er_handle
, fp
, 1);
2131 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2136 * only allow one watch per file per proc
2138 for ( ; np
!= NULL
; np
= np
->ee_slist
.tqe_next
) {
2139 if (np
->ee_proc
== p
) {
2140 if (fp
->f_type
== DTYPE_SOCKET
)
2141 socket_unlock((struct socket
*)fp
->f_data
, 1);
2143 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2144 fp_drop(p
, erp
->er_handle
, fp
, 0);
2147 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2151 erp
->er_ecnt
= erp
->er_rcnt
= erp
->er_wcnt
= erp
->er_eventbits
= 0;
2153 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2156 if (fp
->f_type
== DTYPE_SOCKET
) {
2157 TAILQ_INSERT_TAIL(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2158 postevent((struct socket
*)fp
->f_data
, 0, EV_RWBYTES
); // catch existing events
2160 socket_unlock((struct socket
*)fp
->f_data
, 1);
2162 TAILQ_INSERT_TAIL(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2163 postpipeevent((struct pipe
*)fp
->f_data
, EV_RWBYTES
);
2165 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2167 fp_drop_event(p
, erp
->er_handle
, fp
);
2169 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, 0,0,0,0,0);
2176 * waitevent system call.
2177 * grabs the next waiting event for this proc and returns
2178 * it. if no events, user can request to sleep with timeout
2179 * or poll mode (tv=NULL);
2182 waitevent(proc_t p
, struct waitevent_args
*uap
, int *retval
)
2185 struct eventqelt
*evq
;
2187 uint64_t abstime
, interval
;
2192 error
= copyin(CAST_USER_ADDR_T(uap
->tv
), (caddr_t
)&atv
, sizeof (atv
));
2195 if (itimerfix(&atv
)) {
2199 interval
= tvtoabstime(&atv
);
2203 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_START
, 0,0,0,0,0);
2207 if ((evq
= p
->p_evlist
.tqh_first
) != NULL
) {
2209 * found one... make a local copy while it's still on the queue
2210 * to prevent it from changing while in the midst of copying
2211 * don't want to hold the proc lock across a copyout because
2212 * it might block on a page fault at the target in user space
2214 bcopy((caddr_t
)&evq
->ee_req
, (caddr_t
)&er
, sizeof (struct eventreq
));
2216 TAILQ_REMOVE(&p
->p_evlist
, evq
, ee_plist
);
2218 evq
->ee_flags
&= ~EV_QUEUED
;
2222 error
= copyout((caddr_t
)&er
, CAST_USER_ADDR_T(uap
->u_req
), sizeof(struct eventreq
));
2224 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,
2225 evq
->ee_req
.er_handle
,evq
->ee_req
.er_eventbits
,evq
,0);
2229 if (uap
->tv
&& interval
== 0) {
2231 *retval
= 1; // poll failed
2233 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,0,0,0,0);
2237 clock_absolutetime_interval_to_deadline(interval
, &abstime
);
2241 KERNEL_DEBUG(DBG_MISC_WAIT
, 1,&p
->p_evlist
,0,0,0);
2243 error
= msleep1(&p
->p_evlist
, &p
->p_mlock
, (PSOCK
| PCATCH
), "waitevent", abstime
);
2245 KERNEL_DEBUG(DBG_MISC_WAIT
, 2,&p
->p_evlist
,0,0,0);
2249 if (error
== ERESTART
)
2251 if (error
== EWOULDBLOCK
) {
2258 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, 0,0,0,0,0);
2264 * modwatch system call. user passes in event to modify.
2265 * if we find it we reset the event bits and que/deque event
2269 modwatch(proc_t p
, struct modwatch_args
*uap
, __unused
int *retval
)
2272 struct eventreq
*erp
= &er
;
2273 struct eventqelt
*evq
;
2275 struct fileproc
*fp
;
2278 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_START
, 0,0,0,0,0);
2281 * get user's request pkt
2283 if ((error
= copyin(CAST_USER_ADDR_T(uap
->u_req
), (caddr_t
)erp
,
2284 sizeof(struct eventreq
)))) {
2285 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2290 if (erp
->er_type
!= EV_FD
) {
2292 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2294 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2295 socket_lock((struct socket
*)fp
->f_data
, 1);
2296 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2297 } else if (fp
->f_type
== DTYPE_PIPE
) {
2298 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2299 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2301 fp_drop(p
, erp
->er_handle
, fp
, 1);
2307 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2311 if ((uap
->u_eventmask
== EV_RM
) && (fp
->f_flags
& FP_WAITEVENT
)) {
2312 fp
->f_flags
&= ~FP_WAITEVENT
;
2316 // locate event if possible
2317 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2318 if (evq
->ee_proc
== p
)
2322 if (fp
->f_type
== DTYPE_SOCKET
)
2323 socket_unlock((struct socket
*)fp
->f_data
, 1);
2325 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2326 fp_drop(p
, erp
->er_handle
, fp
, 0);
2327 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2330 KERNEL_DEBUG(DBG_MISC_MOD
, erp
->er_handle
,uap
->u_eventmask
,evq
,0,0);
2332 if (uap
->u_eventmask
== EV_RM
) {
2333 EVPROCDEQUE(p
, evq
);
2335 if (fp
->f_type
== DTYPE_SOCKET
) {
2336 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2337 socket_unlock((struct socket
*)fp
->f_data
, 1);
2339 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2340 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2342 fp_drop(p
, erp
->er_handle
, fp
, 0);
2344 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, 0,0,0,0,0);
2347 switch (uap
->u_eventmask
& EV_MASK
) {
2365 case EV_EX
|EV_RE
|EV_WR
:
2366 flag
= EV_OOB
|EV_RWBYTES
;
2370 if (fp
->f_type
== DTYPE_SOCKET
)
2371 socket_unlock((struct socket
*)fp
->f_data
, 1);
2373 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2374 fp_drop(p
, erp
->er_handle
, fp
, 0);
2375 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2379 * since we're holding the socket/pipe lock, the event
2380 * cannot go from the unqueued state to the queued state
2381 * however, it can go from the queued state to the unqueued state
2382 * since that direction is protected by the proc_lock...
2383 * so do a quick check for EV_QUEUED w/o holding the proc lock
2384 * since by far the common case will be NOT EV_QUEUED, this saves
2385 * us taking the proc_lock the majority of the time
2387 if (evq
->ee_flags
& EV_QUEUED
) {
2389 * EVPROCDEQUE will recheck the state after it grabs the proc_lock
2391 EVPROCDEQUE(p
, evq
);
2394 * while the event is off the proc queue and
2395 * we're holding the socket/pipe lock
2396 * it's safe to update these fields...
2398 evq
->ee_req
.er_eventbits
= 0;
2399 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2401 if (fp
->f_type
== DTYPE_SOCKET
) {
2402 postevent((struct socket
*)fp
->f_data
, 0, flag
);
2403 socket_unlock((struct socket
*)fp
->f_data
, 1);
2406 postpipeevent((struct pipe
*)fp
->f_data
, flag
);
2407 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2409 fp_drop(p
, erp
->er_handle
, fp
, 0);
2410 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, evq
->ee_req
.er_handle
,evq
->ee_eventmask
,fp
->f_data
,flag
,0);
2414 /* this routine is called from the close of fd with proc_fdlock held */
2416 waitevent_close(struct proc
*p
, struct fileproc
*fp
)
2418 struct eventqelt
*evq
;
2421 fp
->f_flags
&= ~FP_WAITEVENT
;
2423 if (fp
->f_type
== DTYPE_SOCKET
) {
2424 socket_lock((struct socket
*)fp
->f_data
, 1);
2425 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2427 else if (fp
->f_type
== DTYPE_PIPE
) {
2428 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2429 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2437 // locate event if possible
2438 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2439 if (evq
->ee_proc
== p
)
2443 if (fp
->f_type
== DTYPE_SOCKET
)
2444 socket_unlock((struct socket
*)fp
->f_data
, 1);
2446 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2452 EVPROCDEQUE(p
, evq
);
2454 if (fp
->f_type
== DTYPE_SOCKET
) {
2455 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2456 socket_unlock((struct socket
*)fp
->f_data
, 1);
2458 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2459 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);