2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
24 * Copyright (c) 1982, 1986, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 * (c) UNIX System Laboratories, Inc.
27 * All or some portions of this file are derived from material licensed
28 * to the University of California by American Telephone and Telegraph
29 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
30 * the permission of UNIX System Laboratories, Inc.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/filedesc.h>
66 #include <sys/ioctl.h>
67 #include <sys/file_internal.h>
68 #include <sys/proc_internal.h>
69 #include <sys/socketvar.h>
71 #include <sys/uio_internal.h>
75 #include <sys/kernel.h>
77 #include <sys/malloc.h>
78 #include <sys/sysproto.h>
80 #include <sys/mount_internal.h>
81 #include <sys/protosw.h>
84 #include <sys/kdebug.h>
86 #include <sys/event.h>
87 #include <sys/eventvar.h>
89 #include <mach/mach_types.h>
90 #include <kern/kern_types.h>
91 #include <kern/assert.h>
92 #include <kern/kalloc.h>
93 #include <kern/thread.h>
94 #include <kern/clock.h>
97 #include <sys/socket.h>
98 #include <sys/socketvar.h>
99 #include <sys/errno.h>
100 #include <sys/syscall.h>
101 #include <sys/pipe.h>
103 #include <bsm/audit_kernel.h>
106 #include <net/route.h>
108 #include <netinet/in.h>
109 #include <netinet/in_systm.h>
110 #include <netinet/ip.h>
111 #include <netinet/in_pcb.h>
112 #include <netinet/ip_var.h>
113 #include <netinet/ip6.h>
114 #include <netinet/tcp.h>
115 #include <netinet/tcp_fsm.h>
116 #include <netinet/tcp_seq.h>
117 #include <netinet/tcp_timer.h>
118 #include <netinet/tcp_var.h>
119 #include <netinet/tcpip.h>
120 #include <netinet/tcp_debug.h>
121 /* for wait queue based select */
122 #include <kern/wait_queue.h>
123 #include <kern/kalloc.h>
125 #include <sys/ktrace.h>
127 #include <sys/vnode_internal.h>
129 int rd_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
);
130 int wr_uio(struct proc
*p
, int fdes
, uio_t uio
, user_ssize_t
*retval
);
131 extern void *get_bsduthreadarg(thread_t
);
132 extern int *get_bsduthreadrval(thread_t
);
134 __private_extern__
int dofileread(struct proc
*p
, struct fileproc
*fp
, int fd
,
135 user_addr_t bufp
, user_size_t nbyte
,
136 off_t offset
, int flags
, user_ssize_t
*retval
);
137 __private_extern__
int dofilewrite(struct proc
*p
, struct fileproc
*fp
, int fd
,
138 user_addr_t bufp
, user_size_t nbyte
,
139 off_t offset
, int flags
, user_ssize_t
*retval
);
140 __private_extern__
int preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_vnode
);
141 __private_extern__
void donefileread(struct proc
*p
, struct fileproc
*fp_ret
, int fd
);
144 extern int appletalk_inited
;
147 #define f_flag f_fglob->fg_flag
148 #define f_type f_fglob->fg_type
149 #define f_msgcount f_fglob->fg_msgcount
150 #define f_cred f_fglob->fg_cred
151 #define f_ops f_fglob->fg_ops
152 #define f_offset f_fglob->fg_offset
153 #define f_data f_fglob->fg_data
160 register struct read_args
*uap
;
161 user_ssize_t
*retval
;
167 if ( (error
= preparefileread(p
, &fp
, fd
, 0)) )
170 error
= dofileread(p
, fp
, uap
->fd
, uap
->cbuf
, uap
->nbyte
,
171 (off_t
)-1, 0, retval
);
173 donefileread(p
, fp
, fd
);
182 pread(p
, uap
, retval
)
184 register struct pread_args
*uap
;
185 user_ssize_t
*retval
;
191 if ( (error
= preparefileread(p
, &fp
, fd
, 1)) )
194 error
= dofileread(p
, fp
, uap
->fd
, uap
->buf
, uap
->nbyte
,
195 uap
->offset
, FOF_OFFSET
, retval
);
197 donefileread(p
, fp
, fd
);
200 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pread
) | DBG_FUNC_NONE
),
201 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
207 * Code common for read and pread
211 donefileread(struct proc
*p
, struct fileproc
*fp
, int fd
)
215 fp
->f_flags
&= ~FP_INCHRREAD
;
217 fp_drop(p
, fd
, fp
, 1);
222 preparefileread(struct proc
*p
, struct fileproc
**fp_ret
, int fd
, int check_for_pread
)
230 error
= fp_lookup(p
, fd
, &fp
, 1);
236 if ((fp
->f_flag
& FREAD
) == 0) {
240 if (check_for_pread
&& (fp
->f_type
!= DTYPE_VNODE
)) {
244 if (fp
->f_type
== DTYPE_VNODE
) {
245 vp
= (struct vnode
*)fp
->f_fglob
->fg_data
;
247 if (vp
->v_type
== VCHR
)
248 fp
->f_flags
|= FP_INCHRREAD
;
257 fp_drop(p
, fd
, fp
, 1);
263 __private_extern__
int
264 dofileread(p
, fp
, fd
, bufp
, nbyte
, offset
, flags
, retval
)
271 user_ssize_t
*retval
;
274 user_ssize_t bytecnt
;
276 char uio_buf
[ UIO_SIZEOF(1) ];
279 char ktr_uio_buf
[ UIO_SIZEOF(1) ];
283 // LP64todo - do we want to raise this?
287 if (IS_64BIT_PROCESS(p
)) {
288 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_READ
,
289 &uio_buf
[0], sizeof(uio_buf
));
291 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_READ
,
292 &uio_buf
[0], sizeof(uio_buf
));
294 uio_addiov(auio
, bufp
, nbyte
);
298 * if tracing, save a copy of iovec
300 if (KTRPOINT(p
, KTR_GENIO
)) {
303 if (IS_64BIT_PROCESS(p
)) {
304 ktruio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_READ
,
305 &ktr_uio_buf
[0], sizeof(ktr_uio_buf
));
307 ktruio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_READ
,
308 &ktr_uio_buf
[0], sizeof(ktr_uio_buf
));
310 uio_addiov(ktruio
, bufp
, nbyte
);
315 if ((error
= fo_read(fp
, auio
, fp
->f_cred
, flags
, p
))) {
316 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
317 error
== EINTR
|| error
== EWOULDBLOCK
))
320 bytecnt
-= uio_resid(auio
);
322 if (didktr
&& error
== 0) {
323 uio_setresid(ktruio
, bytecnt
);
324 ktrgenio(p
->p_tracep
, fd
, UIO_READ
, ktruio
, error
);
334 * Scatter read system call.
337 readv(p
, uap
, retval
)
339 register struct readv_args
*uap
;
340 user_ssize_t
*retval
;
345 struct user_iovec
*iovp
;
347 /* Verify range bedfore calling uio_create() */
348 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
351 /* allocate a uio large enough to hold the number of iovecs passed */
352 auio
= uio_create(uap
->iovcnt
, 0,
353 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
356 /* get location of iovecs within the uio. then copyin the iovecs from
359 iovp
= uio_iovsaddr(auio
);
362 goto ExitThisRoutine
;
364 size_of_iovec
= (IS_64BIT_PROCESS(p
) ? sizeof(struct user_iovec
) : sizeof(struct iovec
));
365 error
= copyin(uap
->iovp
, (caddr_t
)iovp
, (uap
->iovcnt
* size_of_iovec
));
367 goto ExitThisRoutine
;
370 /* finalize uio_t for use and do the IO
372 uio_calculateresid(auio
);
373 error
= rd_uio(p
, uap
->fd
, auio
, retval
);
386 write(p
, uap
, retval
)
388 register struct write_args
*uap
;
389 user_ssize_t
*retval
;
395 error
= fp_lookup(p
,fd
,&fp
,0);
398 if ((fp
->f_flag
& FWRITE
) == 0) {
401 error
= dofilewrite(p
, fp
, uap
->fd
, uap
->cbuf
, uap
->nbyte
,
402 (off_t
)-1, 0, retval
);
405 fp_drop_written(p
, fd
, fp
);
407 fp_drop(p
, fd
, fp
, 0);
415 pwrite(p
, uap
, retval
)
417 register struct pwrite_args
*uap
;
418 user_ssize_t
*retval
;
424 error
= fp_lookup(p
,fd
,&fp
,0);
428 if ((fp
->f_flag
& FWRITE
) == 0) {
431 if (fp
->f_type
!= DTYPE_VNODE
) {
434 error
= dofilewrite(p
, fp
, uap
->fd
, uap
->buf
, uap
->nbyte
,
435 uap
->offset
, FOF_OFFSET
, retval
);
439 fp_drop_written(p
, fd
, fp
);
441 fp_drop(p
, fd
, fp
, 0);
444 KERNEL_DEBUG_CONSTANT((BSDDBG_CODE(DBG_BSD_SC_EXTENDED_INFO
, SYS_pwrite
) | DBG_FUNC_NONE
),
445 uap
->fd
, uap
->nbyte
, (unsigned int)((uap
->offset
>> 32)), (unsigned int)(uap
->offset
), 0);
450 __private_extern__
int
451 dofilewrite(p
, fp
, fd
, bufp
, nbyte
, offset
, flags
, retval
)
458 user_ssize_t
*retval
;
462 user_ssize_t bytecnt
;
463 char uio_buf
[ UIO_SIZEOF(1) ];
467 char ktr_uio_buf
[ UIO_SIZEOF(1) ];
470 // LP64todo - do we want to raise this?
474 if (IS_64BIT_PROCESS(p
)) {
475 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_WRITE
,
476 &uio_buf
[0], sizeof(uio_buf
));
478 auio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_WRITE
,
479 &uio_buf
[0], sizeof(uio_buf
));
481 uio_addiov(auio
, bufp
, nbyte
);
485 * if tracing, save a copy of iovec and uio
487 if (KTRPOINT(p
, KTR_GENIO
)) {
490 if (IS_64BIT_PROCESS(p
)) {
491 ktruio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE64
, UIO_WRITE
,
492 &ktr_uio_buf
[0], sizeof(ktr_uio_buf
));
494 ktruio
= uio_createwithbuffer(1, offset
, UIO_USERSPACE32
, UIO_WRITE
,
495 &ktr_uio_buf
[0], sizeof(ktr_uio_buf
));
497 uio_addiov(ktruio
, bufp
, nbyte
);
501 if ((error
= fo_write(fp
, auio
, fp
->f_cred
, flags
, p
))) {
502 if (uio_resid(auio
) != bytecnt
&& (error
== ERESTART
||
503 error
== EINTR
|| error
== EWOULDBLOCK
))
505 /* The socket layer handles SIGPIPE */
506 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
)
509 bytecnt
-= uio_resid(auio
);
511 if (didktr
&& error
== 0) {
512 uio_setresid(ktruio
, bytecnt
);
513 ktrgenio(p
->p_tracep
, fd
, UIO_WRITE
, ktruio
, error
);
522 * Gather write system call
525 writev(p
, uap
, retval
)
527 register struct writev_args
*uap
;
528 user_ssize_t
*retval
;
533 struct user_iovec
*iovp
;
535 /* Verify range bedfore calling uio_create() */
536 if (uap
->iovcnt
<= 0 || uap
->iovcnt
> UIO_MAXIOV
)
539 /* allocate a uio large enough to hold the number of iovecs passed */
540 auio
= uio_create(uap
->iovcnt
, 0,
541 (IS_64BIT_PROCESS(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
),
544 /* get location of iovecs within the uio. then copyin the iovecs from
547 iovp
= uio_iovsaddr(auio
);
550 goto ExitThisRoutine
;
552 size_of_iovec
= (IS_64BIT_PROCESS(p
) ? sizeof(struct user_iovec
) : sizeof(struct iovec
));
553 error
= copyin(uap
->iovp
, (caddr_t
)iovp
, (uap
->iovcnt
* size_of_iovec
));
555 goto ExitThisRoutine
;
558 /* finalize uio_t for use and do the IO
560 uio_calculateresid(auio
);
561 error
= wr_uio(p
, uap
->fd
, auio
, retval
);
572 wr_uio(p
, fdes
, uio
, retval
)
576 user_ssize_t
*retval
;
582 struct iovec_64
*ktriov
= NULL
;
588 error
= fp_lookup(p
,fdes
,&fp
,0);
592 if ((fp
->f_flag
& FWRITE
) == 0) {
596 count
= uio_resid(uio
);
599 * if tracing, save a copy of iovec
601 if (KTRPOINT(p
, KTR_GENIO
)) {
602 iovlen
= uio
->uio_iovcnt
*
603 (IS_64BIT_PROCESS(p
) ? sizeof (struct iovec_64
) : sizeof (struct iovec_32
));
604 MALLOC(ktriov
, struct iovec_64
*, iovlen
, M_TEMP
, M_WAITOK
);
605 if (ktriov
!= NULL
) {
606 bcopy((caddr_t
)uio
->uio_iovs
.iov64p
, (caddr_t
)ktriov
, iovlen
);
612 error
= fo_write(fp
, uio
, fp
->f_cred
, 0, p
);
614 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
615 error
== EINTR
|| error
== EWOULDBLOCK
))
617 /* The socket layer handles SIGPIPE */
618 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
)
621 *retval
= count
- uio_resid(uio
);
626 ktruio
.uio_iovs
.iov64p
= ktriov
;
627 uio_setresid(&ktruio
, *retval
);
628 ktrgenio(p
->p_tracep
, fdes
, UIO_WRITE
, &ktruio
, error
);
630 FREE(ktriov
, M_TEMP
);
636 fp_drop_written(p
, fdes
, fp
);
638 fp_drop(p
, fdes
, fp
, 0);
644 rd_uio(p
, fdes
, uio
, retval
)
648 user_ssize_t
*retval
;
654 struct iovec_64
*ktriov
= NULL
;
660 if ( (error
= preparefileread(p
, &fp
, fdes
, 0)) )
663 count
= uio_resid(uio
);
666 * if tracing, save a copy of iovec
668 if (KTRPOINT(p
, KTR_GENIO
)) {
669 iovlen
= uio
->uio_iovcnt
*
670 (IS_64BIT_PROCESS(p
) ? sizeof (struct iovec_64
) : sizeof (struct iovec_32
));
671 MALLOC(ktriov
, struct iovec_64
*, iovlen
, M_TEMP
, M_WAITOK
);
672 if (ktriov
!= NULL
) {
673 bcopy((caddr_t
)uio
->uio_iovs
.iov64p
, (caddr_t
)ktriov
, iovlen
);
679 error
= fo_read(fp
, uio
, fp
->f_cred
, 0, p
);
682 if (uio_resid(uio
) != count
&& (error
== ERESTART
||
683 error
== EINTR
|| error
== EWOULDBLOCK
))
686 *retval
= count
- uio_resid(uio
);
691 ktruio
.uio_iovs
.iov64p
= ktriov
;
692 uio_setresid(&ktruio
, *retval
);
693 ktrgenio(p
->p_tracep
, fdes
, UIO_READ
, &ktruio
, error
);
695 FREE(ktriov
, M_TEMP
);
698 donefileread(p
, fp
, fdes
);
708 ioctl(struct proc
*p
, register struct ioctl_args
*uap
, __unused register_t
*retval
)
717 #define STK_PARAMS 128
718 char stkbuf
[STK_PARAMS
];
721 AUDIT_ARG(fd
, uap
->fd
);
722 AUDIT_ARG(cmd
, CAST_DOWN(int, uap
->com
)); /* LP64todo: uap->com is a user-land long */
723 AUDIT_ARG(addr
, uap
->data
);
725 is64bit
= proc_is64bit(p
);
728 error
= fp_lookup(p
,fd
,&fp
,1);
734 AUDIT_ARG(file
, p
, fp
);
736 if ((fp
->f_flag
& (FREAD
| FWRITE
)) == 0) {
743 * ### LD 6/11/97 Hack Alert: this is to get AppleTalk to work
744 * while implementing an ATioctl system call
747 if (appletalk_inited
&& ((uap
->com
& 0x0000FFFF) == 0xff99)) {
748 u_long fixed_command
;
749 #ifdef APPLETALK_DEBUG
750 kprintf("ioctl: special AppleTalk \n");
753 *(user_addr_t
*)datap
= uap
->data
;
754 fixed_command
= _IOW(0, 0xff99, uap
->data
);
755 error
= fo_ioctl(fp
, fixed_command
, datap
, p
);
763 switch (com
= uap
->com
) {
765 *fdflags(p
, uap
->fd
) &= ~UF_EXCLOSE
;
769 *fdflags(p
, uap
->fd
) |= UF_EXCLOSE
;
775 * Interpret high order word to find amount of data to be
776 * copied to/from the user's address space.
778 size
= IOCPARM_LEN(com
);
779 if (size
> IOCPARM_MAX
) {
784 if (size
> sizeof (stkbuf
)) {
786 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
798 error
= copyin(uap
->data
, datap
, size
);
807 /* XXX - IOC_IN and no size? we should proably return an error here!! */
809 *(user_addr_t
*)datap
= uap
->data
;
812 *(uint32_t *)datap
= (uint32_t)uap
->data
;
815 } else if ((com
&IOC_OUT
) && size
)
817 * Zero the buffer so the user always
818 * gets back something deterministic.
821 else if (com
&IOC_VOID
) {
822 /* XXX - this is odd since IOC_VOID means no parameters */
824 *(user_addr_t
*)datap
= uap
->data
;
827 *(uint32_t *)datap
= (uint32_t)uap
->data
;
834 if ( (tmp
= *(int *)datap
) )
835 fp
->f_flag
|= FNONBLOCK
;
837 fp
->f_flag
&= ~FNONBLOCK
;
838 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
842 if ( (tmp
= *(int *)datap
) )
843 fp
->f_flag
|= FASYNC
;
845 fp
->f_flag
&= ~FASYNC
;
846 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, p
);
851 if (fp
->f_type
== DTYPE_SOCKET
) {
852 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
856 if (fp
->f_type
== DTYPE_PIPE
) {
857 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, p
);
863 struct proc
*p1
= pfind(tmp
);
868 tmp
= p1
->p_pgrp
->pg_id
;
870 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, p
);
874 if (fp
->f_type
== DTYPE_SOCKET
) {
876 *(int *)datap
= ((struct socket
*)fp
->f_data
)->so_pgid
;
879 error
= fo_ioctl(fp
, TIOCGPGRP
, datap
, p
);
880 *(int *)datap
= -*(int *)datap
;
884 error
= fo_ioctl(fp
, com
, datap
, p
);
886 * Copy any data to user, size was
887 * already set and checked above.
889 if (error
== 0 && (com
&IOC_OUT
) && size
)
890 error
= copyout(datap
, uap
->data
, (u_int
)size
);
898 fp_drop(p
, fd
, fp
, 1);
903 int selwait
, nselcoll
;
904 #define SEL_FIRSTPASS 1
905 #define SEL_SECONDPASS 2
906 extern int selcontinue(int error
);
907 extern int selprocess(int error
, int sel_pass
);
908 static int selscan(struct proc
*p
, struct _select
* sel
,
909 int nfd
, register_t
*retval
, int sel_pass
, wait_queue_sub_t wqsub
);
910 static int selcount(struct proc
*p
, u_int32_t
*ibits
, u_int32_t
*obits
,
911 int nfd
, int * count
);
912 static int seldrop(struct proc
*p
, u_int32_t
*ibits
, int nfd
);
913 extern uint64_t tvtoabstime(struct timeval
*tvp
);
916 * Select system call.
919 select(struct proc
*p
, struct select_args
*uap
, register_t
*retval
)
926 int needzerofill
= 1;
929 th_act
= current_thread();
930 uth
= get_bsdthread_info(th_act
);
931 sel
= &uth
->uu_select
;
932 retval
= (int *)get_bsduthreadrval(th_act
);
939 if (uap
->nd
> p
->p_fd
->fd_nfiles
)
940 uap
->nd
= p
->p_fd
->fd_nfiles
; /* forgiving; slightly wrong */
942 nw
= howmany(uap
->nd
, NFDBITS
);
943 ni
= nw
* sizeof(fd_mask
);
946 * if this is the first select by the thread
947 * allocate the space for bits.
949 if (sel
->nbytes
== 0) {
950 sel
->nbytes
= 3 * ni
;
951 MALLOC(sel
->ibits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
952 MALLOC(sel
->obits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
953 if ((sel
->ibits
== NULL
) || (sel
->obits
== NULL
))
954 panic("select out of memory");
959 * if the previously allocated space for the bits
960 * is smaller than what is requested. Reallocate.
962 if (sel
->nbytes
< (3 * ni
)) {
963 sel
->nbytes
= (3 * ni
);
964 FREE(sel
->ibits
, M_TEMP
);
965 FREE(sel
->obits
, M_TEMP
);
966 MALLOC(sel
->ibits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
967 MALLOC(sel
->obits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
| M_ZERO
);
968 if ((sel
->ibits
== NULL
) || (sel
->obits
== NULL
))
969 panic("select out of memory");
974 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
975 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
979 * get the bits from the user address space
981 #define getbits(name, x) \
983 if (uap->name && (error = copyin(uap->name, \
984 (caddr_t)&sel->ibits[(x) * nw], ni))) \
995 if (IS_64BIT_PROCESS(p
)) {
996 struct user_timeval atv64
;
997 error
= copyin(uap
->tv
, (caddr_t
)&atv64
, sizeof(atv64
));
998 /* Loses resolution - assume timeout < 68 years */
999 atv
.tv_sec
= atv64
.tv_sec
;
1000 atv
.tv_usec
= atv64
.tv_usec
;
1002 error
= copyin(uap
->tv
, (caddr_t
)&atv
, sizeof(atv
));
1006 if (itimerfix(&atv
)) {
1011 clock_absolutetime_interval_to_deadline(
1012 tvtoabstime(&atv
), &sel
->abstime
);
1017 if ( (error
= selcount(p
, sel
->ibits
, sel
->obits
, uap
->nd
, &count
)) ) {
1022 size
= SIZEOF_WAITQUEUE_SET
+ (count
* SIZEOF_WAITQUEUE_LINK
);
1023 if (sel
->allocsize
) {
1024 if (sel
->wqset
== 0)
1025 panic("select: wql memory smashed");
1026 /* needed for the select now */
1027 if (size
> sel
->allocsize
) {
1028 kfree(sel
->wqset
, sel
->allocsize
);
1029 sel
->allocsize
= size
;
1030 sel
->wqset
= (wait_queue_set_t
)kalloc(size
);
1031 if (sel
->wqset
== (wait_queue_set_t
)NULL
)
1032 panic("failed to allocate memory for waitqueue\n");
1036 sel
->allocsize
= size
;
1037 sel
->wqset
= (wait_queue_set_t
)kalloc(sel
->allocsize
);
1038 if (sel
->wqset
== (wait_queue_set_t
)NULL
)
1039 panic("failed to allocate memory for waitqueue\n");
1041 bzero(sel
->wqset
, size
);
1042 sel
->wql
= (char *)sel
->wqset
+ SIZEOF_WAITQUEUE_SET
;
1043 wait_queue_set_init(sel
->wqset
, (SYNC_POLICY_FIFO
| SYNC_POLICY_PREPOST
));
1046 return selprocess(error
, SEL_FIRSTPASS
);
1050 selcontinue(int error
)
1052 return selprocess(error
, SEL_SECONDPASS
);
1056 selprocess(int error
, int sel_pass
)
1061 struct uthread
*uth
;
1063 struct select_args
*uap
;
1065 struct _select
*sel
;
1070 wait_result_t wait_result
;
1073 th_act
= current_thread();
1074 uap
= (struct select_args
*)get_bsduthreadarg(th_act
);
1075 retval
= (int *)get_bsduthreadrval(th_act
);
1076 uth
= get_bsdthread_info(th_act
);
1077 sel
= &uth
->uu_select
;
1079 /* if it is first pass wait queue is not setup yet */
1080 if ((error
!= 0) && (sel_pass
== SEL_FIRSTPASS
))
1082 if (sel
->count
== 0)
1090 p
->p_flag
|= P_SELECT
;
1091 /* skip scans if the select is just for timeouts */
1093 if (sel_pass
== SEL_FIRSTPASS
)
1094 wait_queue_sub_clearrefs(sel
->wqset
);
1096 error
= selscan(p
, sel
, uap
->nd
, retval
, sel_pass
, sel
->wqset
);
1097 if (error
|| *retval
) {
1101 /* if the select of log, then we canwakeup and discover some one
1102 * else already read the data; go toselct again if time permits
1116 clock_get_uptime(&now
);
1117 if (now
>= sel
->abstime
)
1122 /* cleanup obits and try again */
1124 sel_pass
= SEL_FIRSTPASS
;
1129 * To effect a poll, the timeout argument should be
1130 * non-nil, pointing to a zero-valued timeval structure.
1132 if (uap
->tv
&& sel
->abstime
== 0) {
1136 /* No spurious wakeups due to colls,no need to check for them */
1137 if ((sel_pass
== SEL_SECONDPASS
) || ((p
->p_flag
& P_SELECT
) == 0)) {
1138 sel_pass
= SEL_FIRSTPASS
;
1142 p
->p_flag
&= ~P_SELECT
;
1144 /* if the select is just for timeout skip check */
1145 if (sel
->count
&&(sel_pass
== SEL_SECONDPASS
))
1146 panic("selprocess: 2nd pass assertwaiting");
1148 /* Wait Queue Subordinate has waitqueue as first element */
1149 wait_result
= wait_queue_assert_wait((wait_queue_t
)sel
->wqset
,
1150 &selwait
, THREAD_ABORTSAFE
, sel
->abstime
);
1151 if (wait_result
!= THREAD_AWAKENED
) {
1152 /* there are no preposted events */
1153 error
= tsleep1(NULL
, PSOCK
| PCATCH
,
1154 "select", 0, selcontinue
);
1160 sel_pass
= SEL_SECONDPASS
;
1168 wait_subqueue_unlink_all(sel
->wqset
);
1169 seldrop(p
, sel
->ibits
, uap
->nd
);
1171 p
->p_flag
&= ~P_SELECT
;
1172 /* select is not restarted after signals... */
1173 if (error
== ERESTART
)
1175 if (error
== EWOULDBLOCK
)
1177 nw
= howmany(uap
->nd
, NFDBITS
);
1178 ni
= nw
* sizeof(fd_mask
);
1180 #define putbits(name, x) \
1182 if (uap->name && (error2 = \
1183 copyout((caddr_t)&sel->obits[(x) * nw], uap->name, ni))) \
1199 selscan(p
, sel
, nfd
, retval
, sel_pass
, wqsub
)
1201 struct _select
*sel
;
1205 wait_queue_sub_t wqsub
;
1207 register struct filedesc
*fdp
= p
->p_fd
;
1208 register int msk
, i
, j
, fd
;
1209 register u_int32_t bits
;
1210 struct fileproc
*fp
;
1213 static int flag
[3] = { FREAD
, FWRITE
, 0 };
1214 u_int32_t
*iptr
, *optr
;
1216 u_int32_t
*ibits
, *obits
;
1221 * Problems when reboot; due to MacOSX signal probs
1222 * in Beaker1C ; verify that the p->p_fd is valid
1232 nw
= howmany(nfd
, NFDBITS
);
1238 for (msk
= 0; msk
< 3; msk
++) {
1239 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1240 optr
= (u_int32_t
*)&obits
[msk
* nw
];
1242 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1243 bits
= iptr
[i
/NFDBITS
];
1245 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1247 fp
= fdp
->fd_ofiles
[fd
];
1250 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1254 if (sel_pass
== SEL_SECONDPASS
) {
1255 wql_ptr
= (char *)0;
1256 fp
->f_flags
&= ~FP_INSELECT
;
1257 fp
->f_waddr
= (void *)0;
1259 wql_ptr
= (wql
+ nc
* SIZEOF_WAITQUEUE_LINK
);
1260 fp
->f_flags
|= FP_INSELECT
;
1261 fp
->f_waddr
= (void *)wqsub
;
1263 if (fp
->f_ops
&& fo_select(fp
, flag
[msk
], wql_ptr
, p
)) {
1264 optr
[fd
/NFDBITS
] |= (1 << (fd
% NFDBITS
));
1277 static int poll_callback(struct kqueue
*, struct kevent
*, void *);
1279 struct poll_continue_args
{
1280 user_addr_t pca_fds
;
1286 poll(struct proc
*p
, struct poll_args
*uap
, register_t
*retval
)
1288 struct poll_continue_args
*cont
;
1292 int ncoll
, error
= 0;
1293 u_int nfds
= uap
->nfds
;
1299 * This is kinda bogus. We have fd limits, but that is not
1300 * really related to the size of the pollfd array. Make sure
1301 * we let the process use at least FD_SETSIZE entries and at
1302 * least enough for the current limits. We want to be reasonably
1303 * safe, but not overly restrictive.
1305 if (nfds
> OPEN_MAX
||
1306 (nfds
> p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
&& nfds
> FD_SETSIZE
))
1309 kq
= kqueue_alloc(p
);
1313 ni
= nfds
* sizeof(struct pollfd
) + sizeof(struct poll_continue_args
);
1314 MALLOC(cont
, struct poll_continue_args
*, ni
, M_TEMP
, M_WAITOK
);
1320 fds
= (struct pollfd
*)&cont
[1];
1321 error
= copyin(uap
->fds
, fds
, nfds
* sizeof(struct pollfd
));
1325 if (uap
->timeout
!= -1) {
1328 atv
.tv_sec
= uap
->timeout
/ 1000;
1329 atv
.tv_usec
= (uap
->timeout
% 1000) * 1000;
1330 if (itimerfix(&atv
)) {
1334 getmicrouptime(&rtv
);
1335 timevaladd(&atv
, &rtv
);
1341 /* JMM - all this P_SELECT stuff is bogus */
1343 p
->p_flag
|= P_SELECT
;
1345 for (i
= 0; i
< nfds
; i
++) {
1346 short events
= fds
[i
].events
;
1350 /* per spec, ignore fd values below zero */
1351 if (fds
[i
].fd
< 0) {
1356 /* convert the poll event into a kqueue kevent */
1357 kev
.ident
= fds
[i
].fd
;
1358 kev
.flags
= EV_ADD
| EV_ONESHOT
| EV_POLL
;
1359 kev
.fflags
= NOTE_LOWAT
;
1360 kev
.data
= 1; /* efficiency be damned: any data should trigger */
1361 kev
.udata
= CAST_USER_ADDR_T(&fds
[i
]);
1363 /* Handle input events */
1364 if (events
& ( POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
)) {
1365 kev
.filter
= EVFILT_READ
;
1366 if (!(events
& ( POLLIN
| POLLRDNORM
)))
1367 kev
.flags
|= EV_OOBAND
;
1368 kerror
= kevent_register(kq
, &kev
, p
);
1371 /* Handle output events */
1373 events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
)) {
1374 kev
.filter
= EVFILT_WRITE
;
1375 kerror
= kevent_register(kq
, &kev
, p
);
1378 /* Handle BSD extension vnode events */
1380 events
& ( POLLEXTEND
| POLLATTRIB
| POLLNLINK
| POLLWRITE
)) {
1381 kev
.filter
= EVFILT_VNODE
;
1383 if (events
& POLLEXTEND
)
1384 kev
.fflags
|= NOTE_EXTEND
;
1385 if (events
& POLLATTRIB
)
1386 kev
.fflags
|= NOTE_ATTRIB
;
1387 if (events
& POLLNLINK
)
1388 kev
.fflags
|= NOTE_LINK
;
1389 if (events
& POLLWRITE
)
1390 kev
.fflags
|= NOTE_WRITE
;
1391 kerror
= kevent_register(kq
, &kev
, p
);
1395 fds
[i
].revents
= POLLNVAL
;
1401 /* Did we have any trouble registering? */
1405 /* scan for, and possibly wait for, the kevents to trigger */
1406 cont
->pca_fds
= uap
->fds
;
1407 cont
->pca_nfds
= nfds
;
1408 cont
->pca_rfds
= rfds
;
1409 error
= kevent_scan(kq
, poll_callback
, NULL
, cont
, &atv
, p
);
1410 rfds
= cont
->pca_rfds
;
1413 p
->p_flag
&= ~P_SELECT
;
1414 /* poll is not restarted after signals... */
1415 if (error
== ERESTART
)
1417 if (error
== EWOULDBLOCK
)
1420 error
= copyout(fds
, uap
->fds
, nfds
* sizeof(struct pollfd
));
1428 kqueue_dealloc(kq
, p
);
1433 poll_callback(__unused
struct kqueue
*kq
, struct kevent
*kevp
, void *data
)
1435 struct poll_continue_args
*cont
= (struct poll_continue_args
*)data
;
1436 struct pollfd
*fds
= CAST_DOWN(struct pollfd
*, kevp
->udata
);
1439 /* convert the results back into revents */
1440 if (kevp
->flags
& EV_EOF
)
1441 fds
->revents
|= POLLHUP
;
1442 if (kevp
->flags
& EV_ERROR
)
1443 fds
->revents
|= POLLERR
;
1446 switch (kevp
->filter
) {
1448 if (fds
->revents
& POLLHUP
)
1449 mask
= (POLLIN
| POLLRDNORM
| POLLPRI
| POLLRDBAND
);
1452 if (kevp
->data
!= 0)
1453 mask
|= (POLLIN
| POLLRDNORM
);
1454 if (kevp
->flags
& EV_OOBAND
)
1455 mask
|= ( POLLPRI
| POLLRDBAND
);
1457 fds
->revents
|= (fds
->events
& mask
);
1461 if (!(fds
->revents
& POLLHUP
))
1462 fds
->revents
|= (fds
->events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
));
1466 if (kevp
->fflags
& NOTE_EXTEND
)
1467 fds
->revents
|= (fds
->events
& POLLEXTEND
);
1468 if (kevp
->fflags
& NOTE_ATTRIB
)
1469 fds
->revents
|= (fds
->events
& POLLATTRIB
);
1470 if (kevp
->fflags
& NOTE_LINK
)
1471 fds
->revents
|= (fds
->events
& POLLNLINK
);
1472 if (kevp
->fflags
& NOTE_WRITE
)
1473 fds
->revents
|= (fds
->events
& POLLWRITE
);
1480 seltrue(__unused dev_t dev
, __unused
int flag
, __unused
struct proc
*p
)
1487 selcount(struct proc
*p
, u_int32_t
*ibits
, __unused u_int32_t
*obits
,
1488 int nfd
, int *count
)
1490 register struct filedesc
*fdp
= p
->p_fd
;
1491 register int msk
, i
, j
, fd
;
1492 register u_int32_t bits
;
1493 struct fileproc
*fp
;
1501 * Problems when reboot; due to MacOSX signal probs
1502 * in Beaker1C ; verify that the p->p_fd is valid
1508 nw
= howmany(nfd
, NFDBITS
);
1511 for (msk
= 0; msk
< 3; msk
++) {
1512 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1513 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1514 bits
= iptr
[i
/NFDBITS
];
1515 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1517 fp
= fdp
->fd_ofiles
[fd
];
1519 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1538 /* undo the iocounts */
1539 for (msk
= 0; msk
< 3; msk
++) {
1540 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1541 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1542 bits
= iptr
[i
/NFDBITS
];
1543 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1545 fp
= fdp
->fd_ofiles
[fd
];
1550 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
1551 p
->p_fpdrainwait
= 0;
1552 wakeup(&p
->p_fpdrainwait
);
1564 seldrop(p
, ibits
, nfd
)
1569 register struct filedesc
*fdp
= p
->p_fd
;
1570 register int msk
, i
, j
, fd
;
1571 register u_int32_t bits
;
1572 struct fileproc
*fp
;
1578 * Problems when reboot; due to MacOSX signal probs
1579 * in Beaker1C ; verify that the p->p_fd is valid
1585 nw
= howmany(nfd
, NFDBITS
);
1589 for (msk
= 0; msk
< 3; msk
++) {
1590 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1591 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1592 bits
= iptr
[i
/NFDBITS
];
1593 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1595 fp
= fdp
->fd_ofiles
[fd
];
1598 /* if you are here then it is being closed */
1599 || (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)
1607 fp
->f_flags
&= ~FP_INSELECT
;
1609 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
1610 p
->p_fpdrainwait
= 0;
1611 wakeup(&p
->p_fpdrainwait
);
1621 * Record a select request.
1624 selrecord(__unused
struct proc
*selector
, struct selinfo
*sip
, void * p_wql
)
1626 thread_t cur_act
= current_thread();
1627 struct uthread
* ut
= get_bsdthread_info(cur_act
);
1629 /* need to look at collisions */
1631 if ((p_wql
== (void *)0) && ((sip
->si_flags
& SI_INITED
) == 0)) {
1635 /*do not record if this is second pass of select */
1636 if((p_wql
== (void *)0)) {
1640 if ((sip
->si_flags
& SI_INITED
) == 0) {
1641 wait_queue_init(&sip
->si_wait_queue
, SYNC_POLICY_FIFO
);
1642 sip
->si_flags
|= SI_INITED
;
1643 sip
->si_flags
&= ~SI_CLEAR
;
1646 if (sip
->si_flags
& SI_RECORDED
) {
1647 sip
->si_flags
|= SI_COLL
;
1649 sip
->si_flags
&= ~SI_COLL
;
1651 sip
->si_flags
|= SI_RECORDED
;
1652 if (!wait_queue_member(&sip
->si_wait_queue
, ut
->uu_select
.wqset
))
1653 wait_queue_link_noalloc(&sip
->si_wait_queue
, ut
->uu_select
.wqset
,
1654 (wait_queue_link_t
)p_wql
);
1661 register struct selinfo
*sip
;
1664 if ((sip
->si_flags
& SI_INITED
) == 0) {
1668 if (sip
->si_flags
& SI_COLL
) {
1670 sip
->si_flags
&= ~SI_COLL
;
1672 /* will not support */
1673 //wakeup((caddr_t)&selwait);
1677 if (sip
->si_flags
& SI_RECORDED
) {
1678 wait_queue_wakeup_all(&sip
->si_wait_queue
, &selwait
, THREAD_AWAKENED
);
1679 sip
->si_flags
&= ~SI_RECORDED
;
1686 register struct selinfo
*sip
;
1689 if ((sip
->si_flags
& SI_INITED
) == 0) {
1692 if (sip
->si_flags
& SI_RECORDED
) {
1694 sip
->si_flags
&= ~(SI_RECORDED
| SI_COLL
);
1696 sip
->si_flags
|= SI_CLEAR
;
1697 wait_queue_unlinkall_nofree(&sip
->si_wait_queue
);
1703 #define DBG_EVENT 0x10
1705 #define DBG_POST 0x10
1706 #define DBG_WATCH 0x11
1707 #define DBG_WAIT 0x12
1708 #define DBG_MOD 0x13
1709 #define DBG_EWAKEUP 0x14
1710 #define DBG_ENQUEUE 0x15
1711 #define DBG_DEQUEUE 0x16
1713 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
1714 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
1715 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
1716 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
1717 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
1718 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
1719 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
1722 #define EVPROCDEQUE(p, evq) do { \
1724 if (evq->ee_flags & EV_QUEUED) { \
1725 TAILQ_REMOVE(&p->p_evlist, evq, ee_plist); \
1726 evq->ee_flags &= ~EV_QUEUED; \
1733 * called upon socket close. deque and free all events for
1734 * the socket... socket must be locked by caller.
1737 evsofree(struct socket
*sp
)
1739 struct eventqelt
*evq
, *next
;
1745 for (evq
= sp
->so_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
1746 next
= evq
->ee_slist
.tqe_next
;
1749 if (evq
->ee_flags
& EV_QUEUED
) {
1750 EVPROCDEQUE(p
, evq
);
1752 TAILQ_REMOVE(&sp
->so_evlist
, evq
, ee_slist
); // remove from socket q
1759 * called upon pipe close. deque and free all events for
1760 * the pipe... pipe must be locked by caller
1763 evpipefree(struct pipe
*cpipe
)
1765 struct eventqelt
*evq
, *next
;
1768 for (evq
= cpipe
->pipe_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
1769 next
= evq
->ee_slist
.tqe_next
;
1772 EVPROCDEQUE(p
, evq
);
1774 TAILQ_REMOVE(&cpipe
->pipe_evlist
, evq
, ee_slist
); // remove from pipe q
1781 * enqueue this event if it's not already queued. wakeup
1782 * the proc if we do queue this event to it...
1783 * entered with proc lock held... we drop it before
1784 * doing the wakeup and return in that state
1787 evprocenque(struct eventqelt
*evq
)
1794 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_START
, evq
, evq
->ee_flags
, evq
->ee_eventmask
,0,0);
1798 if (evq
->ee_flags
& EV_QUEUED
) {
1801 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1804 evq
->ee_flags
|= EV_QUEUED
;
1806 TAILQ_INSERT_TAIL(&p
->p_evlist
, evq
, ee_plist
);
1810 wakeup(&p
->p_evlist
);
1812 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1817 * pipe lock must be taken by the caller
1820 postpipeevent(struct pipe
*pipep
, int event
)
1823 struct eventqelt
*evq
;
1827 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, event
,0,0,1,0);
1829 for (evq
= pipep
->pipe_evlist
.tqh_first
;
1830 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1832 if (evq
->ee_eventmask
== 0)
1836 switch (event
& (EV_RWBYTES
| EV_RCLOSED
| EV_WCLOSED
)) {
1839 if ((evq
->ee_eventmask
& EV_RE
) && pipep
->pipe_buffer
.cnt
) {
1841 evq
->ee_req
.er_rcnt
= pipep
->pipe_buffer
.cnt
;
1843 if ((evq
->ee_eventmask
& EV_WR
) &&
1844 (pipep
->pipe_buffer
.size
- pipep
->pipe_buffer
.cnt
) >= PIPE_BUF
) {
1846 if (pipep
->pipe_state
& PIPE_EOF
) {
1847 mask
|= EV_WR
|EV_RESET
;
1851 evq
->ee_req
.er_wcnt
= pipep
->pipe_buffer
.size
- pipep
->pipe_buffer
.cnt
;
1857 if ((evq
->ee_eventmask
& EV_RE
)) {
1858 mask
|= EV_RE
|EV_RCLOSED
;
1860 if ((evq
->ee_eventmask
& EV_WR
)) {
1861 mask
|= EV_WR
|EV_WCLOSED
;
1870 * disarm... postevents are nops until this event is 'read' via
1871 * waitevent and then re-armed via modwatch
1873 evq
->ee_eventmask
= 0;
1876 * since events are disarmed until after the waitevent
1877 * the ee_req.er_xxxx fields can't change once we've
1878 * inserted this event into the proc queue...
1879 * therefore, the waitevent will see a 'consistent'
1880 * snapshot of the event, even though it won't hold
1881 * the pipe lock, and we're updating the event outside
1882 * of the proc lock, which it will hold
1884 evq
->ee_req
.er_eventbits
|= mask
;
1886 KERNEL_DEBUG(DBG_MISC_POST
, evq
, evq
->ee_req
.er_eventbits
, mask
, 1,0);
1891 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, 0,0,0,1,0);
1896 * given either a sockbuf or a socket run down the
1897 * event list and queue ready events found...
1898 * the socket must be locked by the caller
1901 postevent(struct socket
*sp
, struct sockbuf
*sb
, int event
)
1904 struct eventqelt
*evq
;
1912 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, (int)sp
, event
, 0, 0, 0);
1914 for (evq
= sp
->so_evlist
.tqh_first
;
1915 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1917 if (evq
->ee_eventmask
== 0)
1921 /* ready for reading:
1922 - byte cnt >= receive low water mark
1923 - read-half of conn closed
1924 - conn pending for listening sock
1925 - socket error pending
1928 - byte cnt avail >= send low water mark
1929 - write half of conn closed
1930 - socket error pending
1931 - non-blocking conn completed successfully
1935 - sock at out of band mark
1938 switch (event
& EV_DMASK
) {
1941 if ((evq
->ee_eventmask
& EV_EX
)) {
1942 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
1943 mask
|= EV_EX
|EV_OOB
;
1947 case EV_RWBYTES
|EV_OOB
:
1948 if ((evq
->ee_eventmask
& EV_EX
)) {
1949 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
1950 mask
|= EV_EX
|EV_OOB
;
1953 * fall into the next case
1956 if ((evq
->ee_eventmask
& EV_RE
) && soreadable(sp
)) {
1958 if ((sp
->so_type
== SOCK_STREAM
) && ((sp
->so_error
== ECONNREFUSED
) || (sp
->so_error
== ECONNRESET
))) {
1959 if ((sp
->so_pcb
== 0) || (((struct inpcb
*)sp
->so_pcb
)->inp_state
== INPCB_STATE_DEAD
) || !(tp
= sototcpcb(sp
)) ||
1960 (tp
->t_state
== TCPS_CLOSED
)) {
1961 mask
|= EV_RE
|EV_RESET
;
1967 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
1969 if (sp
->so_state
& SS_CANTRCVMORE
) {
1974 if ((evq
->ee_eventmask
& EV_WR
) && sowriteable(sp
)) {
1976 if ((sp
->so_type
== SOCK_STREAM
) && ((sp
->so_error
== ECONNREFUSED
) || (sp
->so_error
== ECONNRESET
))) {
1977 if ((sp
->so_pcb
== 0) || (((struct inpcb
*)sp
->so_pcb
)->inp_state
== INPCB_STATE_DEAD
) || !(tp
= sototcpcb(sp
)) ||
1978 (tp
->t_state
== TCPS_CLOSED
)) {
1979 mask
|= EV_WR
|EV_RESET
;
1985 evq
->ee_req
.er_wcnt
= sbspace(&sp
->so_snd
);
1990 if ((evq
->ee_eventmask
& EV_RE
)) {
1991 mask
|= EV_RE
|EV_RCONN
;
1992 evq
->ee_req
.er_rcnt
= sp
->so_qlen
+ 1; // incl this one
1997 if ((evq
->ee_eventmask
& EV_WR
)) {
1998 mask
|= EV_WR
|EV_WCONN
;
2003 if ((evq
->ee_eventmask
& EV_RE
)) {
2004 mask
|= EV_RE
|EV_RCLOSED
;
2009 if ((evq
->ee_eventmask
& EV_WR
)) {
2010 mask
|= EV_WR
|EV_WCLOSED
;
2015 if (evq
->ee_eventmask
& EV_RE
) {
2016 mask
|= EV_RE
|EV_FIN
;
2022 if (evq
->ee_eventmask
& EV_RE
) {
2023 mask
|= EV_RE
| event
;
2025 if (evq
->ee_eventmask
& EV_WR
) {
2026 mask
|= EV_WR
| event
;
2031 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, -1, 0, 0, 0);
2035 KERNEL_DEBUG(DBG_MISC_POST
, (int)evq
, evq
->ee_eventmask
, evq
->ee_req
.er_eventbits
, mask
, 0);
2039 * disarm... postevents are nops until this event is 'read' via
2040 * waitevent and then re-armed via modwatch
2042 evq
->ee_eventmask
= 0;
2045 * since events are disarmed until after the waitevent
2046 * the ee_req.er_xxxx fields can't change once we've
2047 * inserted this event into the proc queue...
2048 * since waitevent can't see this event until we
2049 * enqueue it, waitevent will see a 'consistent'
2050 * snapshot of the event, even though it won't hold
2051 * the socket lock, and we're updating the event outside
2052 * of the proc lock, which it will hold
2054 evq
->ee_req
.er_eventbits
|= mask
;
2059 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, 0, 0, 0, 0);
2064 * watchevent system call. user passes us an event to watch
2065 * for. we malloc an event object, initialize it, and queue
2066 * it to the open socket. when the event occurs, postevent()
2067 * will enque it back to our proc where we can retrieve it
2070 * should this prevent duplicate events on same socket?
2073 watchevent(proc_t p
, struct watchevent_args
*uap
, __unused
int *retval
)
2075 struct eventqelt
*evq
= (struct eventqelt
*)0;
2076 struct eventqelt
*np
= NULL
;
2077 struct eventreq
*erp
;
2078 struct fileproc
*fp
= NULL
;
2081 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_START
, 0,0,0,0,0);
2083 // get a qelt and fill with users req
2084 MALLOC(evq
, struct eventqelt
*, sizeof(struct eventqelt
), M_TEMP
, M_WAITOK
);
2087 panic("can't MALLOC evq");
2090 // get users request pkt
2091 if ( (error
= copyin(CAST_USER_ADDR_T(uap
->u_req
), (caddr_t
)erp
,
2092 sizeof(struct eventreq
))) ) {
2095 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2098 KERNEL_DEBUG(DBG_MISC_WATCH
, erp
->er_handle
,uap
->u_eventmask
,evq
,0,0);
2100 // validate, freeing qelt if errors
2104 if (erp
->er_type
!= EV_FD
) {
2106 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2108 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2109 socket_lock((struct socket
*)fp
->f_data
, 1);
2110 np
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2111 } else if (fp
->f_type
== DTYPE_PIPE
) {
2112 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2113 np
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2115 fp_drop(p
, erp
->er_handle
, fp
, 1);
2123 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2128 * only allow one watch per file per proc
2130 for ( ; np
!= NULL
; np
= np
->ee_slist
.tqe_next
) {
2131 if (np
->ee_proc
== p
) {
2132 if (fp
->f_type
== DTYPE_SOCKET
)
2133 socket_unlock((struct socket
*)fp
->f_data
, 1);
2135 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2136 fp_drop(p
, erp
->er_handle
, fp
, 0);
2139 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2143 erp
->er_ecnt
= erp
->er_rcnt
= erp
->er_wcnt
= erp
->er_eventbits
= 0;
2145 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2148 if (fp
->f_type
== DTYPE_SOCKET
) {
2149 TAILQ_INSERT_TAIL(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2150 postevent((struct socket
*)fp
->f_data
, 0, EV_RWBYTES
); // catch existing events
2152 socket_unlock((struct socket
*)fp
->f_data
, 1);
2154 TAILQ_INSERT_TAIL(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2155 postpipeevent((struct pipe
*)fp
->f_data
, EV_RWBYTES
);
2157 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2159 fp_drop_event(p
, erp
->er_handle
, fp
);
2161 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, 0,0,0,0,0);
2168 * waitevent system call.
2169 * grabs the next waiting event for this proc and returns
2170 * it. if no events, user can request to sleep with timeout
2171 * or poll mode (tv=NULL);
2174 waitevent(proc_t p
, struct waitevent_args
*uap
, int *retval
)
2177 struct eventqelt
*evq
;
2179 uint64_t abstime
, interval
;
2184 error
= copyin(CAST_USER_ADDR_T(uap
->tv
), (caddr_t
)&atv
, sizeof (atv
));
2187 if (itimerfix(&atv
)) {
2191 interval
= tvtoabstime(&atv
);
2195 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_START
, 0,0,0,0,0);
2199 if ((evq
= p
->p_evlist
.tqh_first
) != NULL
) {
2201 * found one... make a local copy while it's still on the queue
2202 * to prevent it from changing while in the midst of copying
2203 * don't want to hold the proc lock across a copyout because
2204 * it might block on a page fault at the target in user space
2206 bcopy((caddr_t
)&evq
->ee_req
, (caddr_t
)&er
, sizeof (struct eventreq
));
2208 TAILQ_REMOVE(&p
->p_evlist
, evq
, ee_plist
);
2210 evq
->ee_flags
&= ~EV_QUEUED
;
2214 error
= copyout((caddr_t
)&er
, CAST_USER_ADDR_T(uap
->u_req
), sizeof(struct eventreq
));
2216 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,
2217 evq
->ee_req
.er_handle
,evq
->ee_req
.er_eventbits
,evq
,0);
2221 if (uap
->tv
&& interval
== 0) {
2223 *retval
= 1; // poll failed
2225 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,0,0,0,0);
2229 clock_absolutetime_interval_to_deadline(interval
, &abstime
);
2233 KERNEL_DEBUG(DBG_MISC_WAIT
, 1,&p
->p_evlist
,0,0,0);
2235 error
= msleep1(&p
->p_evlist
, &p
->p_mlock
, (PSOCK
| PCATCH
), "waitevent", abstime
);
2237 KERNEL_DEBUG(DBG_MISC_WAIT
, 2,&p
->p_evlist
,0,0,0);
2241 if (error
== ERESTART
)
2243 if (error
== EWOULDBLOCK
) {
2250 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, 0,0,0,0,0);
2256 * modwatch system call. user passes in event to modify.
2257 * if we find it we reset the event bits and que/deque event
2261 modwatch(proc_t p
, struct modwatch_args
*uap
, __unused
int *retval
)
2264 struct eventreq
*erp
= &er
;
2265 struct eventqelt
*evq
;
2267 struct fileproc
*fp
;
2270 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_START
, 0,0,0,0,0);
2273 * get user's request pkt
2275 if ((error
= copyin(CAST_USER_ADDR_T(uap
->u_req
), (caddr_t
)erp
,
2276 sizeof(struct eventreq
)))) {
2277 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2282 if (erp
->er_type
!= EV_FD
) {
2284 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2286 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2287 socket_lock((struct socket
*)fp
->f_data
, 1);
2288 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2289 } else if (fp
->f_type
== DTYPE_PIPE
) {
2290 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2291 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2293 fp_drop(p
, erp
->er_handle
, fp
, 1);
2299 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2303 if ((uap
->u_eventmask
== EV_RM
) && (fp
->f_flags
& FP_WAITEVENT
)) {
2304 fp
->f_flags
&= ~FP_WAITEVENT
;
2308 // locate event if possible
2309 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2310 if (evq
->ee_proc
== p
)
2314 if (fp
->f_type
== DTYPE_SOCKET
)
2315 socket_unlock((struct socket
*)fp
->f_data
, 1);
2317 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2318 fp_drop(p
, erp
->er_handle
, fp
, 0);
2319 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2322 KERNEL_DEBUG(DBG_MISC_MOD
, erp
->er_handle
,uap
->u_eventmask
,evq
,0,0);
2324 if (uap
->u_eventmask
== EV_RM
) {
2325 EVPROCDEQUE(p
, evq
);
2327 if (fp
->f_type
== DTYPE_SOCKET
) {
2328 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2329 socket_unlock((struct socket
*)fp
->f_data
, 1);
2331 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2332 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2334 fp_drop(p
, erp
->er_handle
, fp
, 0);
2336 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, 0,0,0,0,0);
2339 switch (uap
->u_eventmask
& EV_MASK
) {
2357 case EV_EX
|EV_RE
|EV_WR
:
2358 flag
= EV_OOB
|EV_RWBYTES
;
2362 if (fp
->f_type
== DTYPE_SOCKET
)
2363 socket_unlock((struct socket
*)fp
->f_data
, 1);
2365 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2366 fp_drop(p
, erp
->er_handle
, fp
, 0);
2367 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2371 * since we're holding the socket/pipe lock, the event
2372 * cannot go from the unqueued state to the queued state
2373 * however, it can go from the queued state to the unqueued state
2374 * since that direction is protected by the proc_lock...
2375 * so do a quick check for EV_QUEUED w/o holding the proc lock
2376 * since by far the common case will be NOT EV_QUEUED, this saves
2377 * us taking the proc_lock the majority of the time
2379 if (evq
->ee_flags
& EV_QUEUED
) {
2381 * EVPROCDEQUE will recheck the state after it grabs the proc_lock
2383 EVPROCDEQUE(p
, evq
);
2386 * while the event is off the proc queue and
2387 * we're holding the socket/pipe lock
2388 * it's safe to update these fields...
2390 evq
->ee_req
.er_eventbits
= 0;
2391 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2393 if (fp
->f_type
== DTYPE_SOCKET
) {
2394 postevent((struct socket
*)fp
->f_data
, 0, flag
);
2395 socket_unlock((struct socket
*)fp
->f_data
, 1);
2398 postpipeevent((struct pipe
*)fp
->f_data
, flag
);
2399 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2401 fp_drop(p
, erp
->er_handle
, fp
, 0);
2402 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, evq
->ee_req
.er_handle
,evq
->ee_eventmask
,fp
->f_data
,flag
,0);
2406 /* this routine is called from the close of fd with proc_fdlock held */
2408 waitevent_close(struct proc
*p
, struct fileproc
*fp
)
2410 struct eventqelt
*evq
;
2413 fp
->f_flags
&= ~FP_WAITEVENT
;
2415 if (fp
->f_type
== DTYPE_SOCKET
) {
2416 socket_lock((struct socket
*)fp
->f_data
, 1);
2417 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2419 else if (fp
->f_type
== DTYPE_PIPE
) {
2420 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2421 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2429 // locate event if possible
2430 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2431 if (evq
->ee_proc
== p
)
2435 if (fp
->f_type
== DTYPE_SOCKET
)
2436 socket_unlock((struct socket
*)fp
->f_data
, 1);
2438 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2444 EVPROCDEQUE(p
, evq
);
2446 if (fp
->f_type
== DTYPE_SOCKET
) {
2447 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2448 socket_unlock((struct socket
*)fp
->f_data
, 1);
2450 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2451 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);