2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1982, 1986, 1989, 1993
26 * The Regents of the University of California. All rights reserved.
27 * (c) UNIX System Laboratories, Inc.
28 * All or some portions of this file are derived from material licensed
29 * to the University of California by American Telephone and Telegraph
30 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
31 * the permission of UNIX System Laboratories, Inc.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/filedesc.h>
67 #include <sys/ioctl.h>
68 #include <sys/file_internal.h>
69 #include <sys/proc_internal.h>
70 #include <sys/socketvar.h>
72 #include <sys/uio_internal.h>
76 #include <sys/kernel.h>
78 #include <sys/malloc.h>
79 #include <sys/sysproto.h>
81 #include <sys/mount_internal.h>
82 #include <sys/protosw.h>
85 #include <sys/kdebug.h>
87 #include <sys/event.h>
88 #include <sys/eventvar.h>
90 #include <mach/mach_types.h>
91 #include <kern/kern_types.h>
92 #include <kern/assert.h>
93 #include <kern/kalloc.h>
94 #include <kern/thread.h>
95 #include <kern/clock.h>
98 #include <sys/socket.h>
99 #include <sys/socketvar.h>
100 #include <sys/errno.h>
101 #include <sys/syscall.h>
102 #include <sys/pipe.h>
104 #include <bsm/audit_kernel.h>
107 #include <net/route.h>
109 #include <netinet/in.h>
110 #include <netinet/in_systm.h>
111 #include <netinet/ip.h>
112 #include <netinet/in_pcb.h>
113 #include <netinet/ip_var.h>
114 #include <netinet/ip6.h>
115 #include <netinet/tcp.h>
116 #include <netinet/tcp_fsm.h>
117 #include <netinet/tcp_seq.h>
118 #include <netinet/tcp_timer.h>
119 #include <netinet/tcp_var.h>
120 #include <netinet/tcpip.h>
121 #include <netinet/tcp_debug.h>
122 /* for wait queue based select */
123 #include <kern/wait_queue.h>
124 #include <kern/kalloc.h>
126 #include <sys/ktrace.h>
128 #include <sys/vnode_internal.h>
130 int rd_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
);
131 int wr_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
);
132 extern void *get_bsduthreadarg(thread_t
);
133 extern int *get_bsduthreadrval(thread_t
);
135 __private_extern__
int dofileread(struct proc
*p
, struct fileproc
*fp
, int fd
,
136 user_addr_t bufp
, user_size_t nbyte
,
137 off_t offset
, int flags
, user_ssize_t
*retval
);
138 __private_extern__
int dofilewrite(struct proc
*p
, struct fileproc
*fp
, int fd
,
139 user_addr_t bufp
, user_size_t nbyte
,
140 off_t offset
, int flags
, user_ssize_t
*retval
);
141 __private_extern__
int preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_vnode
);
142 __private_extern__
void donefileread(struct proc
*p
, struct fileproc
*fp_ret
, int fd
);
145 extern int appletalk_inited
;
148 #define f_flag f_fglob->fg_flag
149 #define f_type f_fglob->fg_type
150 #define f_msgcount f_fglob->fg_msgcount
151 #define f_cred f_fglob->fg_cred
152 #define f_ops f_fglob->fg_ops
153 #define f_offset f_fglob->fg_offset
154 #define f_data f_fglob->fg_data
161 register struct read_args
*uap
;
162 user_ssize_t
*retval
;
168 if ( (error
= preparefileread(p
, &fp
, fd
, 0)) )
171 error
= dofileread(p
, fp
, uap
->fd
, uap
->cbuf
, uap
->nbyte
,
172 (off_t
)-1, 0, retval
);
174 donefileread(p
, fp
, fd
);
183 pread(p
, uap
, retval
)
185 register struct pread_args
*uap
;
186 user_ssize_t
*retval
;
192 if ( (error
= preparefileread(p
, &fp
, fd
, 1)) )
195 error
= dofileread(p
, fp
, uap
->fd
, uap
->buf
, uap
->nbyte
,
196 uap
->offset
, FOF_OFFSET
, retval
);
198 donefileread(p
, fp
, fd
);
201 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pread
) | DBG_FUNC_NONE
),
202 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
208 * Code common for read and pread
212 donefileread(struct proc
*p
, struct fileproc
*fp
, int fd
)
216 fp
->f_flags
&= ~FP_INCHRREAD
;
218 fp_drop(p
, fd
, fp
, 1);
223 preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_pread
)
231 error
= fp_lookup(p
, fd
, &fp
, 1);
237 if ((fp
->f_flag
& FREAD
) == 0) {
241 if (check_for_pread
&& (fp
->f_type
!= DTYPE_VNODE
)) {
245 if (fp
->f_type
== DTYPE_VNODE
) {
246 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
248 if (vp
->v_type
== VCHR
)
249 fp
->f_flags
|= FP_INCHRREAD
;
258 fp_drop(p
, fd
, fp
, 1);
264 __private_extern__
int
265 dofileread(p
, fp
, fd
, bufp
, nbyte
, offset
, flags
, retval
)
272 user_ssize_t
*retval
;
275 user_ssize_t bytecnt
;
277 char uio_buf
[ UIO_SIZEOF(1) ];
280 char ktr_uio_buf
[ UIO_SIZEOF(1) ];
284 // LP64todo - do we want to raise this?
288 if (IS_64BIT_PROCESS(p
)) {
289 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_READ
,
290 &uio_buf
[0], sizeof(uio_buf
));
292 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_READ
,
293 &uio_buf
[0], sizeof(uio_buf
));
295 uio_addiov(auio
, bufp
, nbyte
);
299 * if tracing, save a copy of iovec
301 if (KTRPOINT(p
, KTR_GENIO
)) {
304 if (IS_64BIT_PROCESS(p
)) {
305 ktruio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_READ
,
306 &ktr_uio_buf
[0], sizeof(ktr_uio_buf
));
308 ktruio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_READ
,
309 &ktr_uio_buf
[0], sizeof(ktr_uio_buf
));
311 uio_addiov(ktruio
, bufp
, nbyte
);
316 if ((error
= fo_read(fp
, auio
, fp
->f_cred
, flags
, p
))) {
317 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
318 error
== EINTR
|| error
== EWOULDBLOCK
))
321 bytecnt
-= uio_resid(auio
);
323 if (didktr
&& error
== 0) {
324 uio_setresid(ktruio
, bytecnt
);
325 ktrgenio(p
->p_tracep
, fd
, UIO_READ
, ktruio
, error
);
335 * Scatter read system call.
338 readv(p
, uap
, retval
)
340 register struct readv_args
*uap
;
341 user_ssize_t
*retval
;
346 struct user_iovec
*iovp
;
348 /* Verify range bedfore calling uio_create() */
349 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
352 /* allocate a uio large enough to hold the number of iovecs passed */
353 auio
= uio_create(uap
->iovcnt
, 0,
354 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
357 /* get location of iovecs within the uio. then copyin the iovecs from
360 iovp
= uio_iovsaddr(auio
);
363 goto ExitThisRoutine
;
365 size_of_iovec
= (IS_64BIT_PROCESS(p
) ? sizeof(struct user_iovec
) : sizeof(struct iovec
));
366 error
= copyin(uap
->iovp
, (caddr_t
)iovp
, (uap
->iovcnt
* size_of_iovec
));
368 goto ExitThisRoutine
;
371 /* finalize uio_t for use and do the IO
373 uio_calculateresid(auio
);
374 error
= rd_uio(p
, uap
->fd
, auio
, retval
);
387 write(p
, uap
, retval
)
389 register struct write_args
*uap
;
390 user_ssize_t
*retval
;
396 error
= fp_lookup(p
,fd
,&fp
,0);
399 if ((fp
->f_flag
& FWRITE
) == 0) {
402 error
= dofilewrite(p
, fp
, uap
->fd
, uap
->cbuf
, uap
->nbyte
,
403 (off_t
)-1, 0, retval
);
406 fp_drop_written(p
, fd
, fp
);
408 fp_drop(p
, fd
, fp
, 0);
416 pwrite(p
, uap
, retval
)
418 register struct pwrite_args
*uap
;
419 user_ssize_t
*retval
;
425 error
= fp_lookup(p
,fd
,&fp
,0);
429 if ((fp
->f_flag
& FWRITE
) == 0) {
432 if (fp
->f_type
!= DTYPE_VNODE
) {
435 error
= dofilewrite(p
, fp
, uap
->fd
, uap
->buf
, uap
->nbyte
,
436 uap
->offset
, FOF_OFFSET
, retval
);
440 fp_drop_written(p
, fd
, fp
);
442 fp_drop(p
, fd
, fp
, 0);
445 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pwrite
) | DBG_FUNC_NONE
),
446 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
451 __private_extern__
int
452 dofilewrite(p
, fp
, fd
, bufp
, nbyte
, offset
, flags
, retval
)
459 user_ssize_t
*retval
;
463 user_ssize_t bytecnt
;
464 char uio_buf
[ UIO_SIZEOF(1) ];
468 char ktr_uio_buf
[ UIO_SIZEOF(1) ];
471 // LP64todo - do we want to raise this?
475 if (IS_64BIT_PROCESS(p
)) {
476 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_WRITE
,
477 &uio_buf
[0], sizeof(uio_buf
));
479 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_WRITE
,
480 &uio_buf
[0], sizeof(uio_buf
));
482 uio_addiov(auio
, bufp
, nbyte
);
486 * if tracing, save a copy of iovec and uio
488 if (KTRPOINT(p
, KTR_GENIO
)) {
491 if (IS_64BIT_PROCESS(p
)) {
492 ktruio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_WRITE
,
493 &ktr_uio_buf
[0], sizeof(ktr_uio_buf
));
495 ktruio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_WRITE
,
496 &ktr_uio_buf
[0], sizeof(ktr_uio_buf
));
498 uio_addiov(ktruio
, bufp
, nbyte
);
502 if ((error
= fo_write(fp
, auio
, fp
->f_cred
, flags
, p
))) {
503 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
504 error
== EINTR
|| error
== EWOULDBLOCK
))
506 /* The socket layer handles SIGPIPE */
507 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
)
510 bytecnt
-= uio_resid(auio
);
512 if (didktr
&& error
== 0) {
513 uio_setresid(ktruio
, bytecnt
);
514 ktrgenio(p
->p_tracep
, fd
, UIO_WRITE
, ktruio
, error
);
523 * Gather write system call
526 writev(p
, uap
, retval
)
528 register struct writev_args
*uap
;
529 user_ssize_t
*retval
;
534 struct user_iovec
*iovp
;
536 /* Verify range bedfore calling uio_create() */
537 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
540 /* allocate a uio large enough to hold the number of iovecs passed */
541 auio
= uio_create(uap
->iovcnt
, 0,
542 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
545 /* get location of iovecs within the uio. then copyin the iovecs from
548 iovp
= uio_iovsaddr(auio
);
551 goto ExitThisRoutine
;
553 size_of_iovec
= (IS_64BIT_PROCESS(p
) ? sizeof(struct user_iovec
) : sizeof(struct iovec
));
554 error
= copyin(uap
->iovp
, (caddr_t
)iovp
, (uap
->iovcnt
* size_of_iovec
));
556 goto ExitThisRoutine
;
559 /* finalize uio_t for use and do the IO
561 uio_calculateresid(auio
);
562 error
= wr_uio(p
, uap
->fd
, auio
, retval
);
573 wr_uio(p
, fdes
, uio
, retval
)
577 user_ssize_t
*retval
;
583 struct iovec_64
*ktriov
= NULL
;
589 error
= fp_lookup(p
,fdes
,&fp
,0);
593 if ((fp
->f_flag
& FWRITE
) == 0) {
597 count
= uio_resid(uio
);
600 * if tracing, save a copy of iovec
602 if (KTRPOINT(p
, KTR_GENIO
)) {
603 iovlen
= uio
->uio_iovcnt
*
604 (IS_64BIT_PROCESS(p
) ? sizeof (struct iovec_64
) : sizeof (struct iovec_32
));
605 MALLOC(ktriov
, struct iovec_64
*, iovlen
, M_TEMP
, M_WAITOK
);
606 if (ktriov
!= NULL
) {
607 bcopy((caddr_t
)uio
->uio_iovs
.iov64p
, (caddr_t
)ktriov
, iovlen
);
613 error
= fo_write(fp
, uio
, fp
->f_cred
, 0, p
);
615 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
616 error
== EINTR
|| error
== EWOULDBLOCK
))
618 /* The socket layer handles SIGPIPE */
619 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
)
622 *retval
= count
- uio_resid(uio
);
627 ktruio
.uio_iovs
.iov64p
= ktriov
;
628 uio_setresid(&ktruio
, *retval
);
629 ktrgenio(p
->p_tracep
, fdes
, UIO_WRITE
, &ktruio
, error
);
631 FREE(ktriov
, M_TEMP
);
637 fp_drop_written(p
, fdes
, fp
);
639 fp_drop(p
, fdes
, fp
, 0);
645 rd_uio(p
, fdes
, uio
, retval
)
649 user_ssize_t
*retval
;
655 struct iovec_64
*ktriov
= NULL
;
661 if ( (error
= preparefileread(p
, &fp
, fdes
, 0)) )
664 count
= uio_resid(uio
);
667 * if tracing, save a copy of iovec
669 if (KTRPOINT(p
, KTR_GENIO
)) {
670 iovlen
= uio
->uio_iovcnt
*
671 (IS_64BIT_PROCESS(p
) ? sizeof (struct iovec_64
) : sizeof (struct iovec_32
));
672 MALLOC(ktriov
, struct iovec_64
*, iovlen
, M_TEMP
, M_WAITOK
);
673 if (ktriov
!= NULL
) {
674 bcopy((caddr_t
)uio
->uio_iovs
.iov64p
, (caddr_t
)ktriov
, iovlen
);
680 error
= fo_read(fp
, uio
, fp
->f_cred
, 0, p
);
683 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
684 error
== EINTR
|| error
== EWOULDBLOCK
))
687 *retval
= count
- uio_resid(uio
);
692 ktruio
.uio_iovs
.iov64p
= ktriov
;
693 uio_setresid(&ktruio
, *retval
);
694 ktrgenio(p
->p_tracep
, fdes
, UIO_READ
, &ktruio
, error
);
696 FREE(ktriov
, M_TEMP
);
699 donefileread(p
, fp
, fdes
);
709 ioctl(struct proc
*p
, register struct ioctl_args
*uap
, __unused register_t
*retval
)
718 #define STK_PARAMS 128
719 char stkbuf
[STK_PARAMS
];
722 AUDIT_ARG(fd
, uap
->fd
);
723 AUDIT_ARG(cmd
, CAST_DOWN(int, uap
->com
)); /* LP64todo: uap->com is a user-land long */
724 AUDIT_ARG(addr
, uap
->data
);
726 is64bit
= proc_is64bit(p
);
729 error
= fp_lookup(p
,fd
,&fp
,1);
735 AUDIT_ARG(file
, p
, fp
);
737 if ((fp
->f_flag
& (FREAD
| FWRITE
)) == 0) {
744 * ### LD 6/11/97 Hack Alert: this is to get AppleTalk to work
745 * while implementing an ATioctl system call
748 if (appletalk_inited
&& ((uap
->com
& 0x0000FFFF) == 0xff99)) {
749 u_long fixed_command
;
750 #ifdef APPLETALK_DEBUG
751 kprintf("ioctl: special AppleTalk \n");
754 *(user_addr_t
*)datap
= uap
->data
;
755 fixed_command
= _IOW(0, 0xff99, uap
->data
);
756 error
= fo_ioctl(fp
, fixed_command
, datap
, p
);
764 switch (com
= uap
->com
) {
766 *fdflags(p
, uap
->fd
) &= ~UF_EXCLOSE
;
770 *fdflags(p
, uap
->fd
) |= UF_EXCLOSE
;
776 * Interpret high order word to find amount of data to be
777 * copied to/from the user's address space.
779 size
= IOCPARM_LEN(com
);
780 if (size
> IOCPARM_MAX
) {
785 if (size
> sizeof (stkbuf
)) {
787 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
799 error
= copyin(uap
->data
, datap
, size
);
808 /* XXX - IOC_IN and no size? we should proably return an error here!! */
810 *(user_addr_t
*)datap
= uap
->data
;
813 *(uint32_t *)datap
= (uint32_t)uap
->data
;
816 } else if ((com
&IOC_OUT
) && size
)
818 * Zero the buffer so the user always
819 * gets back something deterministic.
822 else if (com
&IOC_VOID
) {
823 /* XXX - this is odd since IOC_VOID means no parameters */
825 *(user_addr_t
*)datap
= uap
->data
;
828 *(uint32_t *)datap
= (uint32_t)uap
->data
;
835 if ( (tmp
= *(int *)datap
) )
836 fp
->f_flag
|= FNONBLOCK
;
838 fp
->f_flag
&= ~FNONBLOCK
;
839 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
843 if ( (tmp
= *(int *)datap
) )
844 fp
->f_flag
|= FASYNC
;
846 fp
->f_flag
&= ~FASYNC
;
847 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, p
);
852 if (fp
->f_type
== DTYPE_SOCKET
) {
853 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
857 if (fp
->f_type
== DTYPE_PIPE
) {
858 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, p
);
864 struct proc
*p1
= pfind(tmp
);
869 tmp
= p1
->p_pgrp
->pg_id
;
871 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, p
);
875 if (fp
->f_type
== DTYPE_SOCKET
) {
877 *(int *)datap
= ((struct socket
*)fp
->f_data
)->so_pgid
;
880 error
= fo_ioctl(fp
, TIOCGPGRP
, datap
, p
);
881 *(int *)datap
= -*(int *)datap
;
885 error
= fo_ioctl(fp
, com
, datap
, p
);
887 * Copy any data to user, size was
888 * already set and checked above.
890 if (error
== 0 && (com
&IOC_OUT
) && size
)
891 error
= copyout(datap
, uap
->data
, (u_int
)size
);
899 fp_drop(p
, fd
, fp
, 1);
904 int selwait
, nselcoll
;
905 #define SEL_FIRSTPASS 1
906 #define SEL_SECONDPASS 2
907 extern int selcontinue(int error
);
908 extern int selprocess(int error
, int sel_pass
);
909 static int selscan(struct proc
*p
, struct _select
* sel
,
910 int nfd
, register_t
*retval
, int sel_pass
, wait_queue_sub_t wqsub
);
911 static int selcount(struct proc
*p
, u_int32_t
*ibits
, u_int32_t
*obits
,
912 int nfd
, int * count
);
913 static int seldrop(struct proc
*p
, u_int32_t
*ibits
, int nfd
);
914 extern uint64_t tvtoabstime(struct timeval
*tvp
);
917 * Select system call.
920 select(struct proc
*p
, struct select_args
*uap
, register_t
*retval
)
927 int needzerofill
= 1;
930 th_act
= current_thread();
931 uth
= get_bsdthread_info(th_act
);
932 sel
= &uth
->uu_select
;
933 retval
= (int *)get_bsduthreadrval(th_act
);
940 if (uap
->nd
> p
->p_fd
->fd_nfiles
)
941 uap
->nd
= p
->p_fd
->fd_nfiles
; /* forgiving; slightly wrong */
943 nw
= howmany(uap
->nd
, NFDBITS
);
944 ni
= nw
* sizeof(fd_mask
);
947 * if this is the first select by the thread
948 * allocate the space for bits.
950 if (sel
->nbytes
== 0) {
951 sel
->nbytes
= 3 * ni
;
952 MALLOC(sel
->ibits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
953 MALLOC(sel
->obits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
954 if ((sel
->ibits
== NULL
) || (sel
->obits
== NULL
))
955 panic("select out of memory");
960 * if the previously allocated space for the bits
961 * is smaller than what is requested. Reallocate.
963 if (sel
->nbytes
< (3 * ni
)) {
964 sel
->nbytes
= (3 * ni
);
965 FREE(sel
->ibits
, M_TEMP
);
966 FREE(sel
->obits
, M_TEMP
);
967 MALLOC(sel
->ibits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
968 MALLOC(sel
->obits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
969 if ((sel
->ibits
== NULL
) || (sel
->obits
== NULL
))
970 panic("select out of memory");
975 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
976 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
980 * get the bits from the user address space
982 #define getbits(name, x) \
984 if (uap->name && (error = copyin(uap->name, \
985 (caddr_t)&sel->ibits[(x) * nw], ni))) \
996 if (IS_64BIT_PROCESS(p
)) {
997 struct user_timeval atv64
;
998 error
= copyin(uap
->tv
, (caddr_t
)&atv64
, sizeof(atv64
));
999 /* Loses resolution - assume timeout < 68 years */
1000 atv
.tv_sec
= atv64
.tv_sec
;
1001 atv
.tv_usec
= atv64
.tv_usec
;
1003 error
= copyin(uap
->tv
, (caddr_t
)&atv
, sizeof(atv
));
1007 if (itimerfix(&atv
)) {
1012 clock_absolutetime_interval_to_deadline(
1013 tvtoabstime(&atv
), &sel
->abstime
);
1018 if ( (error
= selcount(p
, sel
->ibits
, sel
->obits
, uap
->nd
, &count
)) ) {
1023 size
= SIZEOF_WAITQUEUE_SET
+ (count
* SIZEOF_WAITQUEUE_LINK
);
1024 if (sel
->allocsize
) {
1025 if (sel
->wqset
== 0)
1026 panic("select: wql memory smashed");
1027 /* needed for the select now */
1028 if (size
> sel
->allocsize
) {
1029 kfree(sel
->wqset
, sel
->allocsize
);
1030 sel
->allocsize
= size
;
1031 sel
->wqset
= (wait_queue_set_t
)kalloc(size
);
1032 if (sel
->wqset
== (wait_queue_set_t
)NULL
)
1033 panic("failed to allocate memory for waitqueue\n");
1037 sel
->allocsize
= size
;
1038 sel
->wqset
= (wait_queue_set_t
)kalloc(sel
->allocsize
);
1039 if (sel
->wqset
== (wait_queue_set_t
)NULL
)
1040 panic("failed to allocate memory for waitqueue\n");
1042 bzero(sel
->wqset
, size
);
1043 sel
->wql
= (char *)sel
->wqset
+ SIZEOF_WAITQUEUE_SET
;
1044 wait_queue_set_init(sel
->wqset
, (SYNC_POLICY_FIFO
| SYNC_POLICY_PREPOST
));
1047 return selprocess(error
, SEL_FIRSTPASS
);
1051 selcontinue(int error
)
1053 return selprocess(error
, SEL_SECONDPASS
);
1057 selprocess(int error
, int sel_pass
)
1062 struct uthread
*uth
;
1064 struct select_args
*uap
;
1066 struct _select
*sel
;
1071 wait_result_t wait_result
;
1074 th_act
= current_thread();
1075 uap
= (struct select_args
*)get_bsduthreadarg(th_act
);
1076 retval
= (int *)get_bsduthreadrval(th_act
);
1077 uth
= get_bsdthread_info(th_act
);
1078 sel
= &uth
->uu_select
;
1080 /* if it is first pass wait queue is not setup yet */
1081 if ((error
!= 0) && (sel_pass
== SEL_FIRSTPASS
))
1083 if (sel
->count
== 0)
1091 p
->p_flag
|= P_SELECT
;
1092 /* skip scans if the select is just for timeouts */
1094 if (sel_pass
== SEL_FIRSTPASS
)
1095 wait_queue_sub_clearrefs(sel
->wqset
);
1097 error
= selscan(p
, sel
, uap
->nd
, retval
, sel_pass
, sel
->wqset
);
1098 if (error
|| *retval
) {
1102 /* if the select of log, then we canwakeup and discover some one
1103 * else already read the data; go toselct again if time permits
1117 clock_get_uptime(&now
);
1118 if (now
>= sel
->abstime
)
1123 /* cleanup obits and try again */
1125 sel_pass
= SEL_FIRSTPASS
;
1130 * To effect a poll, the timeout argument should be
1131 * non-nil, pointing to a zero-valued timeval structure.
1133 if (uap
->tv
&& sel
->abstime
== 0) {
1137 /* No spurious wakeups due to colls,no need to check for them */
1138 if ((sel_pass
== SEL_SECONDPASS
) || ((p
->p_flag
& P_SELECT
) == 0)) {
1139 sel_pass
= SEL_FIRSTPASS
;
1143 p
->p_flag
&= ~P_SELECT
;
1145 /* if the select is just for timeout skip check */
1146 if (sel
->count
&&(sel_pass
== SEL_SECONDPASS
))
1147 panic("selprocess: 2nd pass assertwaiting");
1149 /* Wait Queue Subordinate has waitqueue as first element */
1150 wait_result
= wait_queue_assert_wait((wait_queue_t
)sel
->wqset
,
1151 &selwait
, THREAD_ABORTSAFE
, sel
->abstime
);
1152 if (wait_result
!= THREAD_AWAKENED
) {
1153 /* there are no preposted events */
1154 error
= tsleep1(NULL
, PSOCK
| PCATCH
,
1155 "select", 0, selcontinue
);
1161 sel_pass
= SEL_SECONDPASS
;
1169 wait_subqueue_unlink_all(sel
->wqset
);
1170 seldrop(p
, sel
->ibits
, uap
->nd
);
1172 p
->p_flag
&= ~P_SELECT
;
1173 /* select is not restarted after signals... */
1174 if (error
== ERESTART
)
1176 if (error
== EWOULDBLOCK
)
1178 nw
= howmany(uap
->nd
, NFDBITS
);
1179 ni
= nw
* sizeof(fd_mask
);
1181 #define putbits(name, x) \
1183 if (uap->name && (error2 = \
1184 copyout((caddr_t)&sel->obits[(x) * nw], uap->name, ni))) \
1200 selscan(p
, sel
, nfd
, retval
, sel_pass
, wqsub
)
1202 struct _select
*sel
;
1206 wait_queue_sub_t wqsub
;
1208 register struct filedesc
*fdp
= p
->p_fd
;
1209 register int msk
, i
, j
, fd
;
1210 register u_int32_t bits
;
1211 struct fileproc
*fp
;
1214 static int flag
[3] = { FREAD
, FWRITE
, 0 };
1215 u_int32_t
*iptr
, *optr
;
1217 u_int32_t
*ibits
, *obits
;
1222 * Problems when reboot; due to MacOSX signal probs
1223 * in Beaker1C ; verify that the p->p_fd is valid
1233 nw
= howmany(nfd
, NFDBITS
);
1239 for (msk
= 0; msk
< 3; msk
++) {
1240 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1241 optr
= (u_int32_t
*)&obits
[msk
* nw
];
1243 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1244 bits
= iptr
[i
/NFDBITS
];
1246 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1248 fp
= fdp
->fd_ofiles
[fd
];
1251 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1255 if (sel_pass
== SEL_SECONDPASS
) {
1256 wql_ptr
= (char *)0;
1257 fp
->f_flags
&= ~FP_INSELECT
;
1258 fp
->f_waddr
= (void *)0;
1260 wql_ptr
= (wql
+ nc
* SIZEOF_WAITQUEUE_LINK
);
1261 fp
->f_flags
|= FP_INSELECT
;
1262 fp
->f_waddr
= (void *)wqsub
;
1264 if (fp
->f_ops
&& fo_select(fp
, flag
[msk
], wql_ptr
, p
)) {
1265 optr
[fd
/NFDBITS
] |= (1 << (fd
% NFDBITS
));
1278 static int poll_callback(struct kqueue
*, struct kevent
*, void *);
1280 struct poll_continue_args
{
1281 user_addr_t pca_fds
;
1287 poll(struct proc
*p
, struct poll_args
*uap
, register_t
*retval
)
1289 struct poll_continue_args
*cont
;
1293 int ncoll
, error
= 0;
1294 u_int nfds
= uap
->nfds
;
1300 * This is kinda bogus. We have fd limits, but that is not
1301 * really related to the size of the pollfd array. Make sure
1302 * we let the process use at least FD_SETSIZE entries and at
1303 * least enough for the current limits. We want to be reasonably
1304 * safe, but not overly restrictive.
1306 if (nfds
> OPEN_MAX
||
1307 (nfds
> p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
&& nfds
> FD_SETSIZE
))
1310 kq
= kqueue_alloc(p
);
1314 ni
= nfds
* sizeof(struct pollfd
) + sizeof(struct poll_continue_args
);
1315 MALLOC(cont
, struct poll_continue_args
*, ni
, M_TEMP
, M_WAITOK
);
1321 fds
= (struct pollfd
*)&cont
[1];
1322 error
= copyin(uap
->fds
, fds
, nfds
* sizeof(struct pollfd
));
1326 if (uap
->timeout
!= -1) {
1329 atv
.tv_sec
= uap
->timeout
/ 1000;
1330 atv
.tv_usec
= (uap
->timeout
% 1000) * 1000;
1331 if (itimerfix(&atv
)) {
1335 getmicrouptime(&rtv
);
1336 timevaladd(&atv
, &rtv
);
1342 /* JMM - all this P_SELECT stuff is bogus */
1344 p
->p_flag
|= P_SELECT
;
1346 for (i
= 0; i
< nfds
; i
++) {
1347 short events
= fds
[i
].events
;
1351 /* per spec, ignore fd values below zero */
1352 if (fds
[i
].fd
< 0) {
1357 /* convert the poll event into a kqueue kevent */
1358 kev
.ident
= fds
[i
].fd
;
1359 kev
.flags
= EV_ADD
| EV_ONESHOT
| EV_POLL
;
1360 kev
.fflags
= NOTE_LOWAT
;
1361 kev
.data
= 1; /* efficiency be damned: any data should trigger */
1362 kev
.udata
= CAST_USER_ADDR_T(&fds
[i
]);
1364 /* Handle input events */
1365 if (events
& ( POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
)) {
1366 kev
.filter
= EVFILT_READ
;
1367 if (!(events
& ( POLLIN
| POLLRDNORM
)))
1368 kev
.flags
|= EV_OOBAND
;
1369 kerror
= kevent_register(kq
, &kev
, p
);
1372 /* Handle output events */
1374 events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
)) {
1375 kev
.filter
= EVFILT_WRITE
;
1376 kerror
= kevent_register(kq
, &kev
, p
);
1379 /* Handle BSD extension vnode events */
1381 events
& ( POLLEXTEND
| POLLATTRIB
| POLLNLINK
| POLLWRITE
)) {
1382 kev
.filter
= EVFILT_VNODE
;
1384 if (events
& POLLEXTEND
)
1385 kev
.fflags
|= NOTE_EXTEND
;
1386 if (events
& POLLATTRIB
)
1387 kev
.fflags
|= NOTE_ATTRIB
;
1388 if (events
& POLLNLINK
)
1389 kev
.fflags
|= NOTE_LINK
;
1390 if (events
& POLLWRITE
)
1391 kev
.fflags
|= NOTE_WRITE
;
1392 kerror
= kevent_register(kq
, &kev
, p
);
1396 fds
[i
].revents
= POLLNVAL
;
1402 /* Did we have any trouble registering? */
1406 /* scan for, and possibly wait for, the kevents to trigger */
1407 cont
->pca_fds
= uap
->fds
;
1408 cont
->pca_nfds
= nfds
;
1409 cont
->pca_rfds
= rfds
;
1410 error
= kevent_scan(kq
, poll_callback
, NULL
, cont
, &atv
, p
);
1411 rfds
= cont
->pca_rfds
;
1414 p
->p_flag
&= ~P_SELECT
;
1415 /* poll is not restarted after signals... */
1416 if (error
== ERESTART
)
1418 if (error
== EWOULDBLOCK
)
1421 error
= copyout(fds
, uap
->fds
, nfds
* sizeof(struct pollfd
));
1429 kqueue_dealloc(kq
, p
);
1434 poll_callback(__unused
struct kqueue
*kq
, struct kevent
*kevp
, void *data
)
1436 struct poll_continue_args
*cont
= (struct poll_continue_args
*)data
;
1437 struct pollfd
*fds
= CAST_DOWN(struct pollfd
*, kevp
->udata
);
1440 /* convert the results back into revents */
1441 if (kevp
->flags
& EV_EOF
)
1442 fds
->revents
|= POLLHUP
;
1443 if (kevp
->flags
& EV_ERROR
)
1444 fds
->revents
|= POLLERR
;
1447 switch (kevp
->filter
) {
1449 if (fds
->revents
& POLLHUP
)
1450 mask
= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
);
1453 if (kevp
->data
!= 0)
1454 mask
|= (POLLIN
| POLLRDNORM
);
1455 if (kevp
->flags
& EV_OOBAND
)
1456 mask
|= ( POLLPRI
| POLLRDBAND
);
1458 fds
->revents
|= (fds
->events
& mask
);
1462 if (!(fds
->revents
& POLLHUP
))
1463 fds
->revents
|= (fds
->events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
));
1467 if (kevp
->fflags
& NOTE_EXTEND
)
1468 fds
->revents
|= (fds
->events
& POLLEXTEND
);
1469 if (kevp
->fflags
& NOTE_ATTRIB
)
1470 fds
->revents
|= (fds
->events
& POLLATTRIB
);
1471 if (kevp
->fflags
& NOTE_LINK
)
1472 fds
->revents
|= (fds
->events
& POLLNLINK
);
1473 if (kevp
->fflags
& NOTE_WRITE
)
1474 fds
->revents
|= (fds
->events
& POLLWRITE
);
1481 seltrue(__unused dev_t dev
, __unused
int flag
, __unused
struct proc
*p
)
1488 selcount(struct proc
*p
, u_int32_t
*ibits
, __unused u_int32_t
*obits
,
1489 int nfd
, int *count
)
1491 register struct filedesc
*fdp
= p
->p_fd
;
1492 register int msk
, i
, j
, fd
;
1493 register u_int32_t bits
;
1494 struct fileproc
*fp
;
1502 * Problems when reboot; due to MacOSX signal probs
1503 * in Beaker1C ; verify that the p->p_fd is valid
1509 nw
= howmany(nfd
, NFDBITS
);
1512 for (msk
= 0; msk
< 3; msk
++) {
1513 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1514 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1515 bits
= iptr
[i
/NFDBITS
];
1516 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1518 fp
= fdp
->fd_ofiles
[fd
];
1520 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1539 /* undo the iocounts */
1540 for (msk
= 0; msk
< 3; msk
++) {
1541 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1542 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1543 bits
= iptr
[i
/NFDBITS
];
1544 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1546 fp
= fdp
->fd_ofiles
[fd
];
1551 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
1552 p
->p_fpdrainwait
= 0;
1553 wakeup(&p
->p_fpdrainwait
);
1565 seldrop(p
, ibits
, nfd
)
1570 register struct filedesc
*fdp
= p
->p_fd
;
1571 register int msk
, i
, j
, fd
;
1572 register u_int32_t bits
;
1573 struct fileproc
*fp
;
1579 * Problems when reboot; due to MacOSX signal probs
1580 * in Beaker1C ; verify that the p->p_fd is valid
1586 nw
= howmany(nfd
, NFDBITS
);
1590 for (msk
= 0; msk
< 3; msk
++) {
1591 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1592 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1593 bits
= iptr
[i
/NFDBITS
];
1594 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1596 fp
= fdp
->fd_ofiles
[fd
];
1599 /* if you are here then it is being closed */
1600 || (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)
1608 fp
->f_flags
&= ~FP_INSELECT
;
1610 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
1611 p
->p_fpdrainwait
= 0;
1612 wakeup(&p
->p_fpdrainwait
);
1622 * Record a select request.
1625 selrecord(__unused
struct proc
*selector
, struct selinfo
*sip
, void * p_wql
)
1627 thread_t cur_act
= current_thread();
1628 struct uthread
* ut
= get_bsdthread_info(cur_act
);
1630 /* need to look at collisions */
1632 if ((p_wql
== (void *)0) && ((sip
->si_flags
& SI_INITED
) == 0)) {
1636 /*do not record if this is second pass of select */
1637 if((p_wql
== (void *)0)) {
1641 if ((sip
->si_flags
& SI_INITED
) == 0) {
1642 wait_queue_init(&sip
->si_wait_queue
, SYNC_POLICY_FIFO
);
1643 sip
->si_flags
|= SI_INITED
;
1644 sip
->si_flags
&= ~SI_CLEAR
;
1647 if (sip
->si_flags
& SI_RECORDED
) {
1648 sip
->si_flags
|= SI_COLL
;
1650 sip
->si_flags
&= ~SI_COLL
;
1652 sip
->si_flags
|= SI_RECORDED
;
1653 if (!wait_queue_member(&sip
->si_wait_queue
, ut
->uu_select
.wqset
))
1654 wait_queue_link_noalloc(&sip
->si_wait_queue
, ut
->uu_select
.wqset
,
1655 (wait_queue_link_t
)p_wql
);
1662 register struct selinfo
*sip
;
1665 if ((sip
->si_flags
& SI_INITED
) == 0) {
1669 if (sip
->si_flags
& SI_COLL
) {
1671 sip
->si_flags
&= ~SI_COLL
;
1673 /* will not support */
1674 //wakeup((caddr_t)&selwait);
1678 if (sip
->si_flags
& SI_RECORDED
) {
1679 wait_queue_wakeup_all(&sip
->si_wait_queue
, &selwait
, THREAD_AWAKENED
);
1680 sip
->si_flags
&= ~SI_RECORDED
;
1687 register struct selinfo
*sip
;
1690 if ((sip
->si_flags
& SI_INITED
) == 0) {
1693 if (sip
->si_flags
& SI_RECORDED
) {
1695 sip
->si_flags
&= ~(SI_RECORDED
| SI_COLL
);
1697 sip
->si_flags
|= SI_CLEAR
;
1698 wait_queue_unlinkall_nofree(&sip
->si_wait_queue
);
1704 #define DBG_EVENT 0x10
1706 #define DBG_POST 0x10
1707 #define DBG_WATCH 0x11
1708 #define DBG_WAIT 0x12
1709 #define DBG_MOD 0x13
1710 #define DBG_EWAKEUP 0x14
1711 #define DBG_ENQUEUE 0x15
1712 #define DBG_DEQUEUE 0x16
1714 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
1715 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
1716 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
1717 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
1718 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
1719 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
1720 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
1723 #define EVPROCDEQUE(p, evq) do { \
1725 if (evq->ee_flags & EV_QUEUED) { \
1726 TAILQ_REMOVE(&p->p_evlist, evq, ee_plist); \
1727 evq->ee_flags &= ~EV_QUEUED; \
1734 * called upon socket close. deque and free all events for
1735 * the socket... socket must be locked by caller.
1738 evsofree(struct socket
*sp
)
1740 struct eventqelt
*evq
, *next
;
1746 for (evq
= sp
->so_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
1747 next
= evq
->ee_slist
.tqe_next
;
1750 if (evq
->ee_flags
& EV_QUEUED
) {
1751 EVPROCDEQUE(p
, evq
);
1753 TAILQ_REMOVE(&sp
->so_evlist
, evq
, ee_slist
); // remove from socket q
1760 * called upon pipe close. deque and free all events for
1761 * the pipe... pipe must be locked by caller
1764 evpipefree(struct pipe
*cpipe
)
1766 struct eventqelt
*evq
, *next
;
1769 for (evq
= cpipe
->pipe_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
1770 next
= evq
->ee_slist
.tqe_next
;
1773 EVPROCDEQUE(p
, evq
);
1775 TAILQ_REMOVE(&cpipe
->pipe_evlist
, evq
, ee_slist
); // remove from pipe q
1782 * enqueue this event if it's not already queued. wakeup
1783 * the proc if we do queue this event to it...
1784 * entered with proc lock held... we drop it before
1785 * doing the wakeup and return in that state
1788 evprocenque(struct eventqelt
*evq
)
1795 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_START
, evq
, evq
->ee_flags
, evq
->ee_eventmask
,0,0);
1799 if (evq
->ee_flags
& EV_QUEUED
) {
1802 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1805 evq
->ee_flags
|= EV_QUEUED
;
1807 TAILQ_INSERT_TAIL(&p
->p_evlist
, evq
, ee_plist
);
1811 wakeup(&p
->p_evlist
);
1813 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1818 * pipe lock must be taken by the caller
1821 postpipeevent(struct pipe
*pipep
, int event
)
1824 struct eventqelt
*evq
;
1828 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, event
,0,0,1,0);
1830 for (evq
= pipep
->pipe_evlist
.tqh_first
;
1831 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1833 if (evq
->ee_eventmask
== 0)
1837 switch (event
& (EV_RWBYTES
| EV_RCLOSED
| EV_WCLOSED
)) {
1840 if ((evq
->ee_eventmask
& EV_RE
) && pipep
->pipe_buffer
.cnt
) {
1842 evq
->ee_req
.er_rcnt
= pipep
->pipe_buffer
.cnt
;
1844 if ((evq
->ee_eventmask
& EV_WR
) &&
1845 (pipep
->pipe_buffer
.size
- pipep
->pipe_buffer
.cnt
) >= PIPE_BUF
) {
1847 if (pipep
->pipe_state
& PIPE_EOF
) {
1848 mask
|= EV_WR
|EV_RESET
;
1852 evq
->ee_req
.er_wcnt
= pipep
->pipe_buffer
.size
- pipep
->pipe_buffer
.cnt
;
1858 if ((evq
->ee_eventmask
& EV_RE
)) {
1859 mask
|= EV_RE
|EV_RCLOSED
;
1861 if ((evq
->ee_eventmask
& EV_WR
)) {
1862 mask
|= EV_WR
|EV_WCLOSED
;
1871 * disarm... postevents are nops until this event is 'read' via
1872 * waitevent and then re-armed via modwatch
1874 evq
->ee_eventmask
= 0;
1877 * since events are disarmed until after the waitevent
1878 * the ee_req.er_xxxx fields can't change once we've
1879 * inserted this event into the proc queue...
1880 * therefore, the waitevent will see a 'consistent'
1881 * snapshot of the event, even though it won't hold
1882 * the pipe lock, and we're updating the event outside
1883 * of the proc lock, which it will hold
1885 evq
->ee_req
.er_eventbits
|= mask
;
1887 KERNEL_DEBUG(DBG_MISC_POST
, evq
, evq
->ee_req
.er_eventbits
, mask
, 1,0);
1892 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, 0,0,0,1,0);
1897 * given either a sockbuf or a socket run down the
1898 * event list and queue ready events found...
1899 * the socket must be locked by the caller
1902 postevent(struct socket
*sp
, struct sockbuf
*sb
, int event
)
1905 struct eventqelt
*evq
;
1913 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, (int)sp
, event
, 0, 0, 0);
1915 for (evq
= sp
->so_evlist
.tqh_first
;
1916 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1918 if (evq
->ee_eventmask
== 0)
1922 /* ready for reading:
1923 - byte cnt >= receive low water mark
1924 - read-half of conn closed
1925 - conn pending for listening sock
1926 - socket error pending
1929 - byte cnt avail >= send low water mark
1930 - write half of conn closed
1931 - socket error pending
1932 - non-blocking conn completed successfully
1936 - sock at out of band mark
1939 switch (event
& EV_DMASK
) {
1942 if ((evq
->ee_eventmask
& EV_EX
)) {
1943 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
1944 mask
|= EV_EX
|EV_OOB
;
1948 case EV_RWBYTES
|EV_OOB
:
1949 if ((evq
->ee_eventmask
& EV_EX
)) {
1950 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
1951 mask
|= EV_EX
|EV_OOB
;
1954 * fall into the next case
1957 if ((evq
->ee_eventmask
& EV_RE
) && soreadable(sp
)) {
1959 if ((sp
->so_type
== SOCK_STREAM
) && ((sp
->so_error
== ECONNREFUSED
) || (sp
->so_error
== ECONNRESET
))) {
1960 if ((sp
->so_pcb
== 0) || (((struct inpcb
*)sp
->so_pcb
)->inp_state
== INPCB_STATE_DEAD
) || !(tp
= sototcpcb(sp
)) ||
1961 (tp
->t_state
== TCPS_CLOSED
)) {
1962 mask
|= EV_RE
|EV_RESET
;
1968 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
1970 if (sp
->so_state
& SS_CANTRCVMORE
) {
1975 if ((evq
->ee_eventmask
& EV_WR
) && sowriteable(sp
)) {
1977 if ((sp
->so_type
== SOCK_STREAM
) && ((sp
->so_error
== ECONNREFUSED
) || (sp
->so_error
== ECONNRESET
))) {
1978 if ((sp
->so_pcb
== 0) || (((struct inpcb
*)sp
->so_pcb
)->inp_state
== INPCB_STATE_DEAD
) || !(tp
= sototcpcb(sp
)) ||
1979 (tp
->t_state
== TCPS_CLOSED
)) {
1980 mask
|= EV_WR
|EV_RESET
;
1986 evq
->ee_req
.er_wcnt
= sbspace(&sp
->so_snd
);
1991 if ((evq
->ee_eventmask
& EV_RE
)) {
1992 mask
|= EV_RE
|EV_RCONN
;
1993 evq
->ee_req
.er_rcnt
= sp
->so_qlen
+ 1; // incl this one
1998 if ((evq
->ee_eventmask
& EV_WR
)) {
1999 mask
|= EV_WR
|EV_WCONN
;
2004 if ((evq
->ee_eventmask
& EV_RE
)) {
2005 mask
|= EV_RE
|EV_RCLOSED
;
2010 if ((evq
->ee_eventmask
& EV_WR
)) {
2011 mask
|= EV_WR
|EV_WCLOSED
;
2016 if (evq
->ee_eventmask
& EV_RE
) {
2017 mask
|= EV_RE
|EV_FIN
;
2023 if (evq
->ee_eventmask
& EV_RE
) {
2024 mask
|= EV_RE
| event
;
2026 if (evq
->ee_eventmask
& EV_WR
) {
2027 mask
|= EV_WR
| event
;
2032 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, -1, 0, 0, 0);
2036 KERNEL_DEBUG(DBG_MISC_POST
, (int)evq
, evq
->ee_eventmask
, evq
->ee_req
.er_eventbits
, mask
, 0);
2040 * disarm... postevents are nops until this event is 'read' via
2041 * waitevent and then re-armed via modwatch
2043 evq
->ee_eventmask
= 0;
2046 * since events are disarmed until after the waitevent
2047 * the ee_req.er_xxxx fields can't change once we've
2048 * inserted this event into the proc queue...
2049 * since waitevent can't see this event until we
2050 * enqueue it, waitevent will see a 'consistent'
2051 * snapshot of the event, even though it won't hold
2052 * the socket lock, and we're updating the event outside
2053 * of the proc lock, which it will hold
2055 evq
->ee_req
.er_eventbits
|= mask
;
2060 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, 0, 0, 0, 0);
2065 * watchevent system call. user passes us an event to watch
2066 * for. we malloc an event object, initialize it, and queue
2067 * it to the open socket. when the event occurs, postevent()
2068 * will enque it back to our proc where we can retrieve it
2071 * should this prevent duplicate events on same socket?
2074 watchevent(proc_t p
, struct watchevent_args
*uap
, __unused
int *retval
)
2076 struct eventqelt
*evq
= (struct eventqelt
*)0;
2077 struct eventqelt
*np
= NULL
;
2078 struct eventreq
*erp
;
2079 struct fileproc
*fp
= NULL
;
2082 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_START
, 0,0,0,0,0);
2084 // get a qelt and fill with users req
2085 MALLOC(evq
, struct eventqelt
*, sizeof(struct eventqelt
), M_TEMP
, M_WAITOK
);
2088 panic("can't MALLOC evq");
2091 // get users request pkt
2092 if ( (error
= copyin(CAST_USER_ADDR_T(uap
->u_req
), (caddr_t
)erp
,
2093 sizeof(struct eventreq
))) ) {
2096 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2099 KERNEL_DEBUG(DBG_MISC_WATCH
, erp
->er_handle
,uap
->u_eventmask
,evq
,0,0);
2101 // validate, freeing qelt if errors
2105 if (erp
->er_type
!= EV_FD
) {
2107 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2109 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2110 socket_lock((struct socket
*)fp
->f_data
, 1);
2111 np
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2112 } else if (fp
->f_type
== DTYPE_PIPE
) {
2113 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2114 np
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2116 fp_drop(p
, erp
->er_handle
, fp
, 1);
2124 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2129 * only allow one watch per file per proc
2131 for ( ; np
!= NULL
; np
= np
->ee_slist
.tqe_next
) {
2132 if (np
->ee_proc
== p
) {
2133 if (fp
->f_type
== DTYPE_SOCKET
)
2134 socket_unlock((struct socket
*)fp
->f_data
, 1);
2136 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2137 fp_drop(p
, erp
->er_handle
, fp
, 0);
2140 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2144 erp
->er_ecnt
= erp
->er_rcnt
= erp
->er_wcnt
= erp
->er_eventbits
= 0;
2146 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2149 if (fp
->f_type
== DTYPE_SOCKET
) {
2150 TAILQ_INSERT_TAIL(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2151 postevent((struct socket
*)fp
->f_data
, 0, EV_RWBYTES
); // catch existing events
2153 socket_unlock((struct socket
*)fp
->f_data
, 1);
2155 TAILQ_INSERT_TAIL(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2156 postpipeevent((struct pipe
*)fp
->f_data
, EV_RWBYTES
);
2158 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2160 fp_drop_event(p
, erp
->er_handle
, fp
);
2162 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, 0,0,0,0,0);
2169 * waitevent system call.
2170 * grabs the next waiting event for this proc and returns
2171 * it. if no events, user can request to sleep with timeout
2172 * or poll mode (tv=NULL);
2175 waitevent(proc_t p
, struct waitevent_args
*uap
, int *retval
)
2178 struct eventqelt
*evq
;
2180 uint64_t abstime
, interval
;
2185 error
= copyin(CAST_USER_ADDR_T(uap
->tv
), (caddr_t
)&atv
, sizeof (atv
));
2188 if (itimerfix(&atv
)) {
2192 interval
= tvtoabstime(&atv
);
2196 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_START
, 0,0,0,0,0);
2200 if ((evq
= p
->p_evlist
.tqh_first
) != NULL
) {
2202 * found one... make a local copy while it's still on the queue
2203 * to prevent it from changing while in the midst of copying
2204 * don't want to hold the proc lock across a copyout because
2205 * it might block on a page fault at the target in user space
2207 bcopy((caddr_t
)&evq
->ee_req
, (caddr_t
)&er
, sizeof (struct eventreq
));
2209 TAILQ_REMOVE(&p
->p_evlist
, evq
, ee_plist
);
2211 evq
->ee_flags
&= ~EV_QUEUED
;
2215 error
= copyout((caddr_t
)&er
, CAST_USER_ADDR_T(uap
->u_req
), sizeof(struct eventreq
));
2217 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,
2218 evq
->ee_req
.er_handle
,evq
->ee_req
.er_eventbits
,evq
,0);
2222 if (uap
->tv
&& interval
== 0) {
2224 *retval
= 1; // poll failed
2226 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,0,0,0,0);
2230 clock_absolutetime_interval_to_deadline(interval
, &abstime
);
2234 KERNEL_DEBUG(DBG_MISC_WAIT
, 1,&p
->p_evlist
,0,0,0);
2236 error
= msleep1(&p
->p_evlist
, &p
->p_mlock
, (PSOCK
| PCATCH
), "waitevent", abstime
);
2238 KERNEL_DEBUG(DBG_MISC_WAIT
, 2,&p
->p_evlist
,0,0,0);
2242 if (error
== ERESTART
)
2244 if (error
== EWOULDBLOCK
) {
2251 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, 0,0,0,0,0);
2257 * modwatch system call. user passes in event to modify.
2258 * if we find it we reset the event bits and que/deque event
2262 modwatch(proc_t p
, struct modwatch_args
*uap
, __unused
int *retval
)
2265 struct eventreq
*erp
= &er
;
2266 struct eventqelt
*evq
;
2268 struct fileproc
*fp
;
2271 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_START
, 0,0,0,0,0);
2274 * get user's request pkt
2276 if ((error
= copyin(CAST_USER_ADDR_T(uap
->u_req
), (caddr_t
)erp
,
2277 sizeof(struct eventreq
)))) {
2278 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2283 if (erp
->er_type
!= EV_FD
) {
2285 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2287 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2288 socket_lock((struct socket
*)fp
->f_data
, 1);
2289 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2290 } else if (fp
->f_type
== DTYPE_PIPE
) {
2291 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2292 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2294 fp_drop(p
, erp
->er_handle
, fp
, 1);
2300 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2304 if ((uap
->u_eventmask
== EV_RM
) && (fp
->f_flags
& FP_WAITEVENT
)) {
2305 fp
->f_flags
&= ~FP_WAITEVENT
;
2309 // locate event if possible
2310 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2311 if (evq
->ee_proc
== p
)
2315 if (fp
->f_type
== DTYPE_SOCKET
)
2316 socket_unlock((struct socket
*)fp
->f_data
, 1);
2318 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2319 fp_drop(p
, erp
->er_handle
, fp
, 0);
2320 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2323 KERNEL_DEBUG(DBG_MISC_MOD
, erp
->er_handle
,uap
->u_eventmask
,evq
,0,0);
2325 if (uap
->u_eventmask
== EV_RM
) {
2326 EVPROCDEQUE(p
, evq
);
2328 if (fp
->f_type
== DTYPE_SOCKET
) {
2329 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2330 socket_unlock((struct socket
*)fp
->f_data
, 1);
2332 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2333 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2335 fp_drop(p
, erp
->er_handle
, fp
, 0);
2337 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, 0,0,0,0,0);
2340 switch (uap
->u_eventmask
& EV_MASK
) {
2358 case EV_EX
|EV_RE
|EV_WR
:
2359 flag
= EV_OOB
|EV_RWBYTES
;
2363 if (fp
->f_type
== DTYPE_SOCKET
)
2364 socket_unlock((struct socket
*)fp
->f_data
, 1);
2366 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2367 fp_drop(p
, erp
->er_handle
, fp
, 0);
2368 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2372 * since we're holding the socket/pipe lock, the event
2373 * cannot go from the unqueued state to the queued state
2374 * however, it can go from the queued state to the unqueued state
2375 * since that direction is protected by the proc_lock...
2376 * so do a quick check for EV_QUEUED w/o holding the proc lock
2377 * since by far the common case will be NOT EV_QUEUED, this saves
2378 * us taking the proc_lock the majority of the time
2380 if (evq
->ee_flags
& EV_QUEUED
) {
2382 * EVPROCDEQUE will recheck the state after it grabs the proc_lock
2384 EVPROCDEQUE(p
, evq
);
2387 * while the event is off the proc queue and
2388 * we're holding the socket/pipe lock
2389 * it's safe to update these fields...
2391 evq
->ee_req
.er_eventbits
= 0;
2392 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2394 if (fp
->f_type
== DTYPE_SOCKET
) {
2395 postevent((struct socket
*)fp
->f_data
, 0, flag
);
2396 socket_unlock((struct socket
*)fp
->f_data
, 1);
2399 postpipeevent((struct pipe
*)fp
->f_data
, flag
);
2400 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2402 fp_drop(p
, erp
->er_handle
, fp
, 0);
2403 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, evq
->ee_req
.er_handle
,evq
->ee_eventmask
,fp
->f_data
,flag
,0);
2407 /* this routine is called from the close of fd with proc_fdlock held */
2409 waitevent_close(struct proc
*p
, struct fileproc
*fp
)
2411 struct eventqelt
*evq
;
2414 fp
->f_flags
&= ~FP_WAITEVENT
;
2416 if (fp
->f_type
== DTYPE_SOCKET
) {
2417 socket_lock((struct socket
*)fp
->f_data
, 1);
2418 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2420 else if (fp
->f_type
== DTYPE_PIPE
) {
2421 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2422 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2430 // locate event if possible
2431 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2432 if (evq
->ee_proc
== p
)
2436 if (fp
->f_type
== DTYPE_SOCKET
)
2437 socket_unlock((struct socket
*)fp
->f_data
, 1);
2439 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2445 EVPROCDEQUE(p
, evq
);
2447 if (fp
->f_type
== DTYPE_SOCKET
) {
2448 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2449 socket_unlock((struct socket
*)fp
->f_data
, 1);
2451 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2452 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);