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
);
1438 /* convert the results back into revents */
1439 if (kevp
->flags
& EV_EOF
)
1440 fds
->revents
|= POLLHUP
;
1441 if (kevp
->flags
& EV_ERROR
)
1442 fds
->revents
|= POLLERR
;
1445 switch (kevp
->filter
) {
1447 if (kevp
->data
!= 0)
1448 fds
->revents
|= (fds
->events
& ( POLLIN
| POLLRDNORM
));
1449 if (kevp
->flags
& EV_OOBAND
)
1450 fds
->revents
|= (fds
->events
& ( POLLPRI
| POLLRDBAND
));
1454 if (!(fds
->revents
& POLLHUP
))
1455 fds
->revents
|= (fds
->events
& ( POLLOUT
| POLLWRNORM
| POLLWRBAND
));
1459 if (kevp
->fflags
& NOTE_EXTEND
)
1460 fds
->revents
|= (fds
->events
& POLLEXTEND
);
1461 if (kevp
->fflags
& NOTE_ATTRIB
)
1462 fds
->revents
|= (fds
->events
& POLLATTRIB
);
1463 if (kevp
->fflags
& NOTE_LINK
)
1464 fds
->revents
|= (fds
->events
& POLLNLINK
);
1465 if (kevp
->fflags
& NOTE_WRITE
)
1466 fds
->revents
|= (fds
->events
& POLLWRITE
);
1473 seltrue(__unused dev_t dev
, __unused
int flag
, __unused
struct proc
*p
)
1480 selcount(struct proc
*p
, u_int32_t
*ibits
, __unused u_int32_t
*obits
,
1481 int nfd
, int *count
)
1483 register struct filedesc
*fdp
= p
->p_fd
;
1484 register int msk
, i
, j
, fd
;
1485 register u_int32_t bits
;
1486 struct fileproc
*fp
;
1494 * Problems when reboot; due to MacOSX signal probs
1495 * in Beaker1C ; verify that the p->p_fd is valid
1501 nw
= howmany(nfd
, NFDBITS
);
1504 for (msk
= 0; msk
< 3; msk
++) {
1505 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1506 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1507 bits
= iptr
[i
/NFDBITS
];
1508 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1510 fp
= fdp
->fd_ofiles
[fd
];
1512 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1531 /* undo the iocounts */
1532 for (msk
= 0; msk
< 3; msk
++) {
1533 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1534 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1535 bits
= iptr
[i
/NFDBITS
];
1536 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1538 fp
= fdp
->fd_ofiles
[fd
];
1543 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
1544 p
->p_fpdrainwait
= 0;
1545 wakeup(&p
->p_fpdrainwait
);
1557 seldrop(p
, ibits
, nfd
)
1562 register struct filedesc
*fdp
= p
->p_fd
;
1563 register int msk
, i
, j
, fd
;
1564 register u_int32_t bits
;
1565 struct fileproc
*fp
;
1571 * Problems when reboot; due to MacOSX signal probs
1572 * in Beaker1C ; verify that the p->p_fd is valid
1578 nw
= howmany(nfd
, NFDBITS
);
1582 for (msk
= 0; msk
< 3; msk
++) {
1583 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1584 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1585 bits
= iptr
[i
/NFDBITS
];
1586 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1588 fp
= fdp
->fd_ofiles
[fd
];
1591 /* if you are here then it is being closed */
1592 || (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)
1600 fp
->f_flags
&= ~FP_INSELECT
;
1602 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
1603 p
->p_fpdrainwait
= 0;
1604 wakeup(&p
->p_fpdrainwait
);
1614 * Record a select request.
1617 selrecord(__unused
struct proc
*selector
, struct selinfo
*sip
, void * p_wql
)
1619 thread_t cur_act
= current_thread();
1620 struct uthread
* ut
= get_bsdthread_info(cur_act
);
1622 /* need to look at collisions */
1624 if ((p_wql
== (void *)0) && ((sip
->si_flags
& SI_INITED
) == 0)) {
1628 /*do not record if this is second pass of select */
1629 if((p_wql
== (void *)0)) {
1633 if ((sip
->si_flags
& SI_INITED
) == 0) {
1634 wait_queue_init(&sip
->si_wait_queue
, SYNC_POLICY_FIFO
);
1635 sip
->si_flags
|= SI_INITED
;
1636 sip
->si_flags
&= ~SI_CLEAR
;
1639 if (sip
->si_flags
& SI_RECORDED
) {
1640 sip
->si_flags
|= SI_COLL
;
1642 sip
->si_flags
&= ~SI_COLL
;
1644 sip
->si_flags
|= SI_RECORDED
;
1645 if (!wait_queue_member(&sip
->si_wait_queue
, ut
->uu_select
.wqset
))
1646 wait_queue_link_noalloc(&sip
->si_wait_queue
, ut
->uu_select
.wqset
,
1647 (wait_queue_link_t
)p_wql
);
1654 register struct selinfo
*sip
;
1657 if ((sip
->si_flags
& SI_INITED
) == 0) {
1661 if (sip
->si_flags
& SI_COLL
) {
1663 sip
->si_flags
&= ~SI_COLL
;
1665 /* will not support */
1666 //wakeup((caddr_t)&selwait);
1670 if (sip
->si_flags
& SI_RECORDED
) {
1671 wait_queue_wakeup_all(&sip
->si_wait_queue
, &selwait
, THREAD_AWAKENED
);
1672 sip
->si_flags
&= ~SI_RECORDED
;
1679 register struct selinfo
*sip
;
1682 if ((sip
->si_flags
& SI_INITED
) == 0) {
1685 if (sip
->si_flags
& SI_RECORDED
) {
1687 sip
->si_flags
&= ~(SI_RECORDED
| SI_COLL
);
1689 sip
->si_flags
|= SI_CLEAR
;
1690 wait_queue_unlinkall_nofree(&sip
->si_wait_queue
);
1696 #define DBG_EVENT 0x10
1698 #define DBG_POST 0x10
1699 #define DBG_WATCH 0x11
1700 #define DBG_WAIT 0x12
1701 #define DBG_MOD 0x13
1702 #define DBG_EWAKEUP 0x14
1703 #define DBG_ENQUEUE 0x15
1704 #define DBG_DEQUEUE 0x16
1706 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
1707 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
1708 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
1709 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
1710 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
1711 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
1712 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
1715 #define EVPROCDEQUE(p, evq) do { \
1717 if (evq->ee_flags & EV_QUEUED) { \
1718 TAILQ_REMOVE(&p->p_evlist, evq, ee_plist); \
1719 evq->ee_flags &= ~EV_QUEUED; \
1726 * called upon socket close. deque and free all events for
1727 * the socket... socket must be locked by caller.
1730 evsofree(struct socket
*sp
)
1732 struct eventqelt
*evq
, *next
;
1738 for (evq
= sp
->so_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
1739 next
= evq
->ee_slist
.tqe_next
;
1742 if (evq
->ee_flags
& EV_QUEUED
) {
1743 EVPROCDEQUE(p
, evq
);
1745 TAILQ_REMOVE(&sp
->so_evlist
, evq
, ee_slist
); // remove from socket q
1752 * called upon pipe close. deque and free all events for
1753 * the pipe... pipe must be locked by caller
1756 evpipefree(struct pipe
*cpipe
)
1758 struct eventqelt
*evq
, *next
;
1761 for (evq
= cpipe
->pipe_evlist
.tqh_first
; evq
!= NULL
; evq
= next
) {
1762 next
= evq
->ee_slist
.tqe_next
;
1765 EVPROCDEQUE(p
, evq
);
1767 TAILQ_REMOVE(&cpipe
->pipe_evlist
, evq
, ee_slist
); // remove from pipe q
1774 * enqueue this event if it's not already queued. wakeup
1775 * the proc if we do queue this event to it...
1776 * entered with proc lock held... we drop it before
1777 * doing the wakeup and return in that state
1780 evprocenque(struct eventqelt
*evq
)
1787 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_START
, evq
, evq
->ee_flags
, evq
->ee_eventmask
,0,0);
1791 if (evq
->ee_flags
& EV_QUEUED
) {
1794 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1797 evq
->ee_flags
|= EV_QUEUED
;
1799 TAILQ_INSERT_TAIL(&p
->p_evlist
, evq
, ee_plist
);
1803 wakeup(&p
->p_evlist
);
1805 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1810 * pipe lock must be taken by the caller
1813 postpipeevent(struct pipe
*pipep
, int event
)
1816 struct eventqelt
*evq
;
1820 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, event
,0,0,1,0);
1822 for (evq
= pipep
->pipe_evlist
.tqh_first
;
1823 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1825 if (evq
->ee_eventmask
== 0)
1829 switch (event
& (EV_RWBYTES
| EV_RCLOSED
| EV_WCLOSED
)) {
1832 if ((evq
->ee_eventmask
& EV_RE
) && pipep
->pipe_buffer
.cnt
) {
1834 evq
->ee_req
.er_rcnt
= pipep
->pipe_buffer
.cnt
;
1836 if ((evq
->ee_eventmask
& EV_WR
) &&
1837 (pipep
->pipe_buffer
.size
- pipep
->pipe_buffer
.cnt
) >= PIPE_BUF
) {
1839 if (pipep
->pipe_state
& PIPE_EOF
) {
1840 mask
|= EV_WR
|EV_RESET
;
1844 evq
->ee_req
.er_wcnt
= pipep
->pipe_buffer
.size
- pipep
->pipe_buffer
.cnt
;
1850 if ((evq
->ee_eventmask
& EV_RE
)) {
1851 mask
|= EV_RE
|EV_RCLOSED
;
1853 if ((evq
->ee_eventmask
& EV_WR
)) {
1854 mask
|= EV_WR
|EV_WCLOSED
;
1863 * disarm... postevents are nops until this event is 'read' via
1864 * waitevent and then re-armed via modwatch
1866 evq
->ee_eventmask
= 0;
1869 * since events are disarmed until after the waitevent
1870 * the ee_req.er_xxxx fields can't change once we've
1871 * inserted this event into the proc queue...
1872 * therefore, the waitevent will see a 'consistent'
1873 * snapshot of the event, even though it won't hold
1874 * the pipe lock, and we're updating the event outside
1875 * of the proc lock, which it will hold
1877 evq
->ee_req
.er_eventbits
|= mask
;
1879 KERNEL_DEBUG(DBG_MISC_POST
, evq
, evq
->ee_req
.er_eventbits
, mask
, 1,0);
1884 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, 0,0,0,1,0);
1889 * given either a sockbuf or a socket run down the
1890 * event list and queue ready events found...
1891 * the socket must be locked by the caller
1894 postevent(struct socket
*sp
, struct sockbuf
*sb
, int event
)
1897 struct eventqelt
*evq
;
1905 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, (int)sp
, event
, 0, 0, 0);
1907 for (evq
= sp
->so_evlist
.tqh_first
;
1908 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1910 if (evq
->ee_eventmask
== 0)
1914 /* ready for reading:
1915 - byte cnt >= receive low water mark
1916 - read-half of conn closed
1917 - conn pending for listening sock
1918 - socket error pending
1921 - byte cnt avail >= send low water mark
1922 - write half of conn closed
1923 - socket error pending
1924 - non-blocking conn completed successfully
1928 - sock at out of band mark
1931 switch (event
& EV_DMASK
) {
1934 if ((evq
->ee_eventmask
& EV_EX
)) {
1935 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
1936 mask
|= EV_EX
|EV_OOB
;
1940 case EV_RWBYTES
|EV_OOB
:
1941 if ((evq
->ee_eventmask
& EV_EX
)) {
1942 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
)))
1943 mask
|= EV_EX
|EV_OOB
;
1946 * fall into the next case
1949 if ((evq
->ee_eventmask
& EV_RE
) && soreadable(sp
)) {
1951 if ((sp
->so_type
== SOCK_STREAM
) && ((sp
->so_error
== ECONNREFUSED
) || (sp
->so_error
== ECONNRESET
))) {
1952 if ((sp
->so_pcb
== 0) || (((struct inpcb
*)sp
->so_pcb
)->inp_state
== INPCB_STATE_DEAD
) || !(tp
= sototcpcb(sp
)) ||
1953 (tp
->t_state
== TCPS_CLOSED
)) {
1954 mask
|= EV_RE
|EV_RESET
;
1960 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
1962 if (sp
->so_state
& SS_CANTRCVMORE
) {
1967 if ((evq
->ee_eventmask
& EV_WR
) && sowriteable(sp
)) {
1969 if ((sp
->so_type
== SOCK_STREAM
) && ((sp
->so_error
== ECONNREFUSED
) || (sp
->so_error
== ECONNRESET
))) {
1970 if ((sp
->so_pcb
== 0) || (((struct inpcb
*)sp
->so_pcb
)->inp_state
== INPCB_STATE_DEAD
) || !(tp
= sototcpcb(sp
)) ||
1971 (tp
->t_state
== TCPS_CLOSED
)) {
1972 mask
|= EV_WR
|EV_RESET
;
1978 evq
->ee_req
.er_wcnt
= sbspace(&sp
->so_snd
);
1983 if ((evq
->ee_eventmask
& EV_RE
)) {
1984 mask
|= EV_RE
|EV_RCONN
;
1985 evq
->ee_req
.er_rcnt
= sp
->so_qlen
+ 1; // incl this one
1990 if ((evq
->ee_eventmask
& EV_WR
)) {
1991 mask
|= EV_WR
|EV_WCONN
;
1996 if ((evq
->ee_eventmask
& EV_RE
)) {
1997 mask
|= EV_RE
|EV_RCLOSED
;
2002 if ((evq
->ee_eventmask
& EV_WR
)) {
2003 mask
|= EV_WR
|EV_WCLOSED
;
2008 if (evq
->ee_eventmask
& EV_RE
) {
2009 mask
|= EV_RE
|EV_FIN
;
2015 if (evq
->ee_eventmask
& EV_RE
) {
2016 mask
|= EV_RE
| event
;
2018 if (evq
->ee_eventmask
& EV_WR
) {
2019 mask
|= EV_WR
| event
;
2024 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, -1, 0, 0, 0);
2028 KERNEL_DEBUG(DBG_MISC_POST
, (int)evq
, evq
->ee_eventmask
, evq
->ee_req
.er_eventbits
, mask
, 0);
2032 * disarm... postevents are nops until this event is 'read' via
2033 * waitevent and then re-armed via modwatch
2035 evq
->ee_eventmask
= 0;
2038 * since events are disarmed until after the waitevent
2039 * the ee_req.er_xxxx fields can't change once we've
2040 * inserted this event into the proc queue...
2041 * since waitevent can't see this event until we
2042 * enqueue it, waitevent will see a 'consistent'
2043 * snapshot of the event, even though it won't hold
2044 * the socket lock, and we're updating the event outside
2045 * of the proc lock, which it will hold
2047 evq
->ee_req
.er_eventbits
|= mask
;
2052 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, (int)sp
, 0, 0, 0, 0);
2057 * watchevent system call. user passes us an event to watch
2058 * for. we malloc an event object, initialize it, and queue
2059 * it to the open socket. when the event occurs, postevent()
2060 * will enque it back to our proc where we can retrieve it
2063 * should this prevent duplicate events on same socket?
2066 watchevent(proc_t p
, struct watchevent_args
*uap
, __unused
int *retval
)
2068 struct eventqelt
*evq
= (struct eventqelt
*)0;
2069 struct eventqelt
*np
= NULL
;
2070 struct eventreq
*erp
;
2071 struct fileproc
*fp
= NULL
;
2074 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_START
, 0,0,0,0,0);
2076 // get a qelt and fill with users req
2077 MALLOC(evq
, struct eventqelt
*, sizeof(struct eventqelt
), M_TEMP
, M_WAITOK
);
2080 panic("can't MALLOC evq");
2083 // get users request pkt
2084 if ( (error
= copyin(CAST_USER_ADDR_T(uap
->u_req
), (caddr_t
)erp
,
2085 sizeof(struct eventreq
))) ) {
2088 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2091 KERNEL_DEBUG(DBG_MISC_WATCH
, erp
->er_handle
,uap
->u_eventmask
,evq
,0,0);
2093 // validate, freeing qelt if errors
2097 if (erp
->er_type
!= EV_FD
) {
2099 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2101 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2102 socket_lock((struct socket
*)fp
->f_data
, 1);
2103 np
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2104 } else if (fp
->f_type
== DTYPE_PIPE
) {
2105 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2106 np
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2108 fp_drop(p
, erp
->er_handle
, fp
, 1);
2116 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
2121 * only allow one watch per file per proc
2123 for ( ; np
!= NULL
; np
= np
->ee_slist
.tqe_next
) {
2124 if (np
->ee_proc
== p
) {
2125 if (fp
->f_type
== DTYPE_SOCKET
)
2126 socket_unlock((struct socket
*)fp
->f_data
, 1);
2128 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2129 fp_drop(p
, erp
->er_handle
, fp
, 0);
2132 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2136 erp
->er_ecnt
= erp
->er_rcnt
= erp
->er_wcnt
= erp
->er_eventbits
= 0;
2138 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2141 if (fp
->f_type
== DTYPE_SOCKET
) {
2142 TAILQ_INSERT_TAIL(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2143 postevent((struct socket
*)fp
->f_data
, 0, EV_RWBYTES
); // catch existing events
2145 socket_unlock((struct socket
*)fp
->f_data
, 1);
2147 TAILQ_INSERT_TAIL(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2148 postpipeevent((struct pipe
*)fp
->f_data
, EV_RWBYTES
);
2150 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2152 fp_drop_event(p
, erp
->er_handle
, fp
);
2154 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, 0,0,0,0,0);
2161 * waitevent system call.
2162 * grabs the next waiting event for this proc and returns
2163 * it. if no events, user can request to sleep with timeout
2164 * or poll mode (tv=NULL);
2167 waitevent(proc_t p
, struct waitevent_args
*uap
, int *retval
)
2170 struct eventqelt
*evq
;
2172 uint64_t abstime
, interval
;
2177 error
= copyin(CAST_USER_ADDR_T(uap
->tv
), (caddr_t
)&atv
, sizeof (atv
));
2180 if (itimerfix(&atv
)) {
2184 interval
= tvtoabstime(&atv
);
2188 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_START
, 0,0,0,0,0);
2192 if ((evq
= p
->p_evlist
.tqh_first
) != NULL
) {
2194 * found one... make a local copy while it's still on the queue
2195 * to prevent it from changing while in the midst of copying
2196 * don't want to hold the proc lock across a copyout because
2197 * it might block on a page fault at the target in user space
2199 bcopy((caddr_t
)&evq
->ee_req
, (caddr_t
)&er
, sizeof (struct eventreq
));
2201 TAILQ_REMOVE(&p
->p_evlist
, evq
, ee_plist
);
2203 evq
->ee_flags
&= ~EV_QUEUED
;
2207 error
= copyout((caddr_t
)&er
, CAST_USER_ADDR_T(uap
->u_req
), sizeof(struct eventreq
));
2209 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,
2210 evq
->ee_req
.er_handle
,evq
->ee_req
.er_eventbits
,evq
,0);
2214 if (uap
->tv
&& interval
== 0) {
2216 *retval
= 1; // poll failed
2218 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,0,0,0,0);
2222 clock_absolutetime_interval_to_deadline(interval
, &abstime
);
2226 KERNEL_DEBUG(DBG_MISC_WAIT
, 1,&p
->p_evlist
,0,0,0);
2228 error
= msleep1(&p
->p_evlist
, &p
->p_mlock
, (PSOCK
| PCATCH
), "waitevent", abstime
);
2230 KERNEL_DEBUG(DBG_MISC_WAIT
, 2,&p
->p_evlist
,0,0,0);
2234 if (error
== ERESTART
)
2236 if (error
== EWOULDBLOCK
) {
2243 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, 0,0,0,0,0);
2249 * modwatch system call. user passes in event to modify.
2250 * if we find it we reset the event bits and que/deque event
2254 modwatch(proc_t p
, struct modwatch_args
*uap
, __unused
int *retval
)
2257 struct eventreq
*erp
= &er
;
2258 struct eventqelt
*evq
;
2260 struct fileproc
*fp
;
2263 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_START
, 0,0,0,0,0);
2266 * get user's request pkt
2268 if ((error
= copyin(CAST_USER_ADDR_T(uap
->u_req
), (caddr_t
)erp
,
2269 sizeof(struct eventreq
)))) {
2270 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2275 if (erp
->er_type
!= EV_FD
) {
2277 } else if ((error
= fp_lookup(p
, erp
->er_handle
, &fp
, 1)) != 0) {
2279 } else if (fp
->f_type
== DTYPE_SOCKET
) {
2280 socket_lock((struct socket
*)fp
->f_data
, 1);
2281 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2282 } else if (fp
->f_type
== DTYPE_PIPE
) {
2283 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2284 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2286 fp_drop(p
, erp
->er_handle
, fp
, 1);
2292 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, error
,0,0,0,0);
2296 if ((uap
->u_eventmask
== EV_RM
) && (fp
->f_flags
& FP_WAITEVENT
)) {
2297 fp
->f_flags
&= ~FP_WAITEVENT
;
2301 // locate event if possible
2302 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2303 if (evq
->ee_proc
== p
)
2307 if (fp
->f_type
== DTYPE_SOCKET
)
2308 socket_unlock((struct socket
*)fp
->f_data
, 1);
2310 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2311 fp_drop(p
, erp
->er_handle
, fp
, 0);
2312 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2315 KERNEL_DEBUG(DBG_MISC_MOD
, erp
->er_handle
,uap
->u_eventmask
,evq
,0,0);
2317 if (uap
->u_eventmask
== EV_RM
) {
2318 EVPROCDEQUE(p
, evq
);
2320 if (fp
->f_type
== DTYPE_SOCKET
) {
2321 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2322 socket_unlock((struct socket
*)fp
->f_data
, 1);
2324 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2325 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2327 fp_drop(p
, erp
->er_handle
, fp
, 0);
2329 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, 0,0,0,0,0);
2332 switch (uap
->u_eventmask
& EV_MASK
) {
2350 case EV_EX
|EV_RE
|EV_WR
:
2351 flag
= EV_OOB
|EV_RWBYTES
;
2355 if (fp
->f_type
== DTYPE_SOCKET
)
2356 socket_unlock((struct socket
*)fp
->f_data
, 1);
2358 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2359 fp_drop(p
, erp
->er_handle
, fp
, 0);
2360 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
2364 * since we're holding the socket/pipe lock, the event
2365 * cannot go from the unqueued state to the queued state
2366 * however, it can go from the queued state to the unqueued state
2367 * since that direction is protected by the proc_lock...
2368 * so do a quick check for EV_QUEUED w/o holding the proc lock
2369 * since by far the common case will be NOT EV_QUEUED, this saves
2370 * us taking the proc_lock the majority of the time
2372 if (evq
->ee_flags
& EV_QUEUED
) {
2374 * EVPROCDEQUE will recheck the state after it grabs the proc_lock
2376 EVPROCDEQUE(p
, evq
);
2379 * while the event is off the proc queue and
2380 * we're holding the socket/pipe lock
2381 * it's safe to update these fields...
2383 evq
->ee_req
.er_eventbits
= 0;
2384 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
2386 if (fp
->f_type
== DTYPE_SOCKET
) {
2387 postevent((struct socket
*)fp
->f_data
, 0, flag
);
2388 socket_unlock((struct socket
*)fp
->f_data
, 1);
2391 postpipeevent((struct pipe
*)fp
->f_data
, flag
);
2392 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2394 fp_drop(p
, erp
->er_handle
, fp
, 0);
2395 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, evq
->ee_req
.er_handle
,evq
->ee_eventmask
,fp
->f_data
,flag
,0);
2399 /* this routine is called from the close of fd with proc_fdlock held */
2401 waitevent_close(struct proc
*p
, struct fileproc
*fp
)
2403 struct eventqelt
*evq
;
2406 fp
->f_flags
&= ~FP_WAITEVENT
;
2408 if (fp
->f_type
== DTYPE_SOCKET
) {
2409 socket_lock((struct socket
*)fp
->f_data
, 1);
2410 evq
= ((struct socket
*)fp
->f_data
)->so_evlist
.tqh_first
;
2412 else if (fp
->f_type
== DTYPE_PIPE
) {
2413 PIPE_LOCK((struct pipe
*)fp
->f_data
);
2414 evq
= ((struct pipe
*)fp
->f_data
)->pipe_evlist
.tqh_first
;
2422 // locate event if possible
2423 for ( ; evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
2424 if (evq
->ee_proc
== p
)
2428 if (fp
->f_type
== DTYPE_SOCKET
)
2429 socket_unlock((struct socket
*)fp
->f_data
, 1);
2431 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);
2437 EVPROCDEQUE(p
, evq
);
2439 if (fp
->f_type
== DTYPE_SOCKET
) {
2440 TAILQ_REMOVE(&((struct socket
*)fp
->f_data
)->so_evlist
, evq
, ee_slist
);
2441 socket_unlock((struct socket
*)fp
->f_data
, 1);
2443 TAILQ_REMOVE(&((struct pipe
*)fp
->f_data
)->pipe_evlist
, evq
, ee_slist
);
2444 PIPE_UNLOCK((struct pipe
*)fp
->f_data
);