2 * Copyright (c) 2000-2002 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>
69 #include <sys/socketvar.h>
71 #include <sys/kernel.h>
73 #include <sys/malloc.h>
75 #include <sys/mount.h>
76 #include <sys/protosw.h>
79 #include <sys/kdebug.h>
80 #include <kern/assert.h>
81 #include <kern/thread_act.h>
84 #include <sys/socket.h>
85 #include <sys/socketvar.h>
86 #include <sys/errno.h>
89 #include <net/route.h>
91 #include <netinet/in.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/ip.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet/ip_var.h>
96 #include <netinet/ip6.h>
97 #include <netinet/tcp.h>
98 #include <netinet/tcp_fsm.h>
99 #include <netinet/tcp_seq.h>
100 #include <netinet/tcp_timer.h>
101 #include <netinet/tcp_var.h>
102 #include <netinet/tcpip.h>
103 #include <netinet/tcp_debug.h>
104 /* for wait queue based select */
105 #include <kern/wait_queue.h>
107 #include <sys/ktrace.h>
110 static int dofileread
__P((struct proc
*, struct file
*, int, void *,
111 size_t, off_t
, int, int*));
112 static int dofilewrite
__P((struct proc
*, struct file
*, int,
113 const void *, size_t, off_t
, int, int*));
116 holdfp(fdp
, fd
, flag
)
117 struct filedesc
* fdp
;
122 if (((u_int
)fd
) >= fdp
->fd_nfiles
||
123 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
124 (fp
->f_flag
& flag
) == 0) {
134 #ifndef _SYS_SYSPROTO_H_
144 register struct read_args
*uap
;
147 register struct file
*fp
;
150 if ((fp
= holdfp(p
->p_fd
, uap
->fd
, FREAD
)) == NULL
)
152 error
= dofileread(p
, fp
, uap
->fd
, uap
->cbuf
, uap
->nbyte
,
153 (off_t
)-1, 0, retval
);
161 #ifndef _SYS_SYSPROTO_H_
166 #ifdef DOUBLE_ALIGN_PARAMS
173 pread(p
, uap
, retval
)
175 register struct pread_args
*uap
;
178 register struct file
*fp
;
181 if ((fp
= holdfp(p
->p_fd
, uap
->fd
, FREAD
)) == NULL
)
183 if (fp
->f_type
!= DTYPE_VNODE
) {
186 error
= dofileread(p
, fp
, uap
->fd
, uap
->buf
, uap
->nbyte
,
187 uap
->offset
, FOF_OFFSET
, retval
);
194 * Code common for read and pread
197 dofileread(p
, fp
, fd
, buf
, nbyte
, offset
, flags
, retval
)
215 aiov
.iov_base
= (caddr_t
)buf
;
216 aiov
.iov_len
= nbyte
;
217 auio
.uio_iov
= &aiov
;
219 auio
.uio_offset
= offset
;
222 auio
.uio_resid
= nbyte
;
223 auio
.uio_rw
= UIO_READ
;
224 auio
.uio_segflg
= UIO_USERSPACE
;
228 * if tracing, save a copy of iovec
230 if (KTRPOINT(p
, KTR_GENIO
)) {
238 if ((error
= fo_read(fp
, &auio
, fp
->f_cred
, flags
, p
))) {
239 if (auio
.uio_resid
!= cnt
&& (error
== ERESTART
||
240 error
== EINTR
|| error
== EWOULDBLOCK
))
243 cnt
-= auio
.uio_resid
;
245 if (didktr
&& error
== 0) {
246 ktruio
.uio_iov
= &ktriov
;
247 ktruio
.uio_resid
= cnt
;
248 ktrgenio(p
->p_tracep
, fd
, UIO_READ
, &ktruio
, error
,
257 * Scatter read system call.
259 #ifndef _SYS_SYSPROTO_H_
267 readv(p
, uap
, retval
)
269 register struct readv_args
*uap
;
273 register struct iovec
*iov
;
275 struct iovec aiov
[UIO_SMALLIOV
];
277 if (uap
->iovcnt
> UIO_SMALLIOV
) {
278 if (uap
->iovcnt
> UIO_MAXIOV
)
280 if ((iov
= (struct iovec
*)
281 kalloc(sizeof(struct iovec
) * (uap
->iovcnt
))) == 0)
286 auio
.uio_iovcnt
= uap
->iovcnt
;
287 auio
.uio_rw
= UIO_READ
;
288 error
= copyin((caddr_t
)uap
->iovp
, (caddr_t
)iov
,
289 uap
->iovcnt
* sizeof (struct iovec
));
291 error
= rwuio(p
, uap
->fd
, &auio
, UIO_READ
, retval
);
292 if (uap
->iovcnt
> UIO_SMALLIOV
)
293 kfree(iov
, sizeof(struct iovec
)*uap
->iovcnt
);
300 #ifndef _SYS_SYSPROTO_H_
308 write(p
, uap
, retval
)
310 register struct write_args
*uap
;
313 register struct file
*fp
;
316 if ((fp
= holdfp(p
->p_fd
, uap
->fd
, FWRITE
)) == NULL
)
318 error
= dofilewrite(p
, fp
, uap
->fd
, uap
->cbuf
, uap
->nbyte
,
319 (off_t
)-1, 0, retval
);
327 #ifndef _SYS_SYSPROTO_H_
332 #ifdef DOUBLE_ALIGN_PARAMS
339 pwrite(p
, uap
, retval
)
341 register struct pwrite_args
*uap
;
344 register struct file
*fp
;
347 if ((fp
= holdfp(p
->p_fd
, uap
->fd
, FWRITE
)) == NULL
)
349 if (fp
->f_type
!= DTYPE_VNODE
) {
352 error
= dofilewrite(p
, fp
, uap
->fd
, uap
->buf
, uap
->nbyte
,
353 uap
->offset
, FOF_OFFSET
, retval
);
360 dofilewrite(p
, fp
, fd
, buf
, nbyte
, offset
, flags
, retval
)
378 aiov
.iov_base
= (void *)(uintptr_t)buf
;
379 aiov
.iov_len
= nbyte
;
380 auio
.uio_iov
= &aiov
;
382 auio
.uio_offset
= offset
;
385 auio
.uio_resid
= nbyte
;
386 auio
.uio_rw
= UIO_WRITE
;
387 auio
.uio_segflg
= UIO_USERSPACE
;
391 * if tracing, save a copy of iovec and uio
393 if (KTRPOINT(p
, KTR_GENIO
)) {
400 if (fp
->f_type
== DTYPE_VNODE
)
402 if ((error
= fo_write(fp
, &auio
, fp
->f_cred
, flags
, p
))) {
403 if (auio
.uio_resid
!= cnt
&& (error
== ERESTART
||
404 error
== EINTR
|| error
== EWOULDBLOCK
))
409 cnt
-= auio
.uio_resid
;
411 if (didktr
&& error
== 0) {
412 ktruio
.uio_iov
= &ktriov
;
413 ktruio
.uio_resid
= cnt
;
414 ktrgenio(p
->p_tracep
, fd
, UIO_WRITE
, &ktruio
, error
,
423 * Gather write system call
425 #ifndef _SYS_SYSPROTO_H_
433 writev(p
, uap
, retval
)
435 register struct writev_args
*uap
;
439 register struct iovec
*iov
;
441 struct iovec aiov
[UIO_SMALLIOV
];
443 if (uap
->iovcnt
> UIO_SMALLIOV
) {
444 if (uap
->iovcnt
> UIO_MAXIOV
)
446 if ((iov
= (struct iovec
*)
447 kalloc(sizeof(struct iovec
) * (uap
->iovcnt
))) == 0)
452 auio
.uio_iovcnt
= uap
->iovcnt
;
453 auio
.uio_rw
= UIO_WRITE
;
454 error
= copyin((caddr_t
)uap
->iovp
, (caddr_t
)iov
,
455 uap
->iovcnt
* sizeof (struct iovec
));
457 error
= rwuio(p
, uap
->fd
, &auio
, UIO_WRITE
, retval
);
458 if (uap
->iovcnt
> UIO_SMALLIOV
)
459 kfree(iov
, sizeof(struct iovec
)*uap
->iovcnt
);
464 rwuio(p
, fdes
, uio
, rw
, retval
)
467 register struct uio
*uio
;
472 register struct iovec
*iov
;
473 int i
, count
, flag
, error
;
475 struct iovec
*ktriov
;
481 if (error
= fdgetf(p
, fdes
, &fp
))
484 if ((fp
->f_flag
&(rw
==UIO_READ
? FREAD
: FWRITE
)) == 0) {
488 uio
->uio_segflg
= UIO_USERSPACE
;
491 for (i
= 0; i
< uio
->uio_iovcnt
; i
++) {
492 if (iov
->iov_len
< 0) {
495 uio
->uio_resid
+= iov
->iov_len
;
496 if (uio
->uio_resid
< 0) {
501 count
= uio
->uio_resid
;
504 * if tracing, save a copy of iovec
506 if (KTRPOINT(p
, KTR_GENIO
)) {
507 iovlen
= uio
->uio_iovcnt
* sizeof (struct iovec
);
508 MALLOC(ktriov
, struct iovec
*, iovlen
, M_TEMP
, M_WAITOK
);
509 bcopy((caddr_t
)uio
->uio_iov
, (caddr_t
)ktriov
, iovlen
);
515 if (rw
== UIO_READ
) {
516 if (error
= fo_read(fp
, uio
, fp
->f_cred
, 0, p
))
517 if (uio
->uio_resid
!= count
&& (error
== ERESTART
||
518 error
== EINTR
|| error
== EWOULDBLOCK
))
521 if (fp
->f_type
== DTYPE_VNODE
)
523 if (error
= fo_write(fp
, uio
, fp
->f_cred
, 0, p
)) {
524 if (uio
->uio_resid
!= count
&& (error
== ERESTART
||
525 error
== EINTR
|| error
== EWOULDBLOCK
))
527 /* The socket layer handles SIGPIPE */
528 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
)
533 *retval
= count
- uio
->uio_resid
;
538 ktruio
.uio_iov
= ktriov
;
539 ktruio
.uio_resid
= *retval
;
540 ktrgenio(p
->p_tracep
, fdes
, rw
, &ktruio
, error
,
543 FREE(ktriov
, M_TEMP
);
553 #ifndef _SYS_SYSPROTO_H_
561 ioctl(p
, uap
, retval
)
563 register struct ioctl_args
*uap
;
572 #define STK_PARAMS 128
573 char stkbuf
[STK_PARAMS
];
575 if (error
= fdgetf(p
, uap
->fd
, &fp
))
578 if ((fp
->f_flag
& (FREAD
| FWRITE
)) == 0)
583 * ### LD 6/11/97 Hack Alert: this is to get AppleTalk to work
584 * while implementing an ATioctl system call
587 extern int appletalk_inited
;
589 if (appletalk_inited
&& ((uap
->com
& 0x0000FFFF) == 0xff99)) {
590 #ifdef APPLETALK_DEBUG
591 kprintf("ioctl: special AppleTalk \n");
593 error
= fo_ioctl(fp
, uap
->com
, uap
->data
, p
);
601 switch (com
= uap
->com
) {
603 *fdflags(p
, uap
->fd
) &= ~UF_EXCLOSE
;
606 *fdflags(p
, uap
->fd
) |= UF_EXCLOSE
;
611 * Interpret high order word to find amount of data to be
612 * copied to/from the user's address space.
614 size
= IOCPARM_LEN(com
);
615 if (size
> IOCPARM_MAX
)
618 if (size
> sizeof (stkbuf
)) {
619 if ((memp
= (caddr_t
)kalloc(size
)) == 0)
626 error
= copyin(uap
->data
, data
, (u_int
)size
);
633 *(caddr_t
*)data
= uap
->data
;
634 } else if ((com
&IOC_OUT
) && size
)
636 * Zero the buffer so the user always
637 * gets back something deterministic.
640 else if (com
&IOC_VOID
)
641 *(caddr_t
*)data
= uap
->data
;
646 if (tmp
= *(int *)data
)
647 fp
->f_flag
|= FNONBLOCK
;
649 fp
->f_flag
&= ~FNONBLOCK
;
650 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
654 if (tmp
= *(int *)data
)
655 fp
->f_flag
|= FASYNC
;
657 fp
->f_flag
&= ~FASYNC
;
658 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, p
);
663 if (fp
->f_type
== DTYPE_SOCKET
) {
664 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
671 struct proc
*p1
= pfind(tmp
);
676 tmp
= p1
->p_pgrp
->pg_id
;
678 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, p
);
682 if (fp
->f_type
== DTYPE_SOCKET
) {
684 *(int *)data
= ((struct socket
*)fp
->f_data
)->so_pgid
;
687 error
= fo_ioctl(fp
, TIOCGPGRP
, data
, p
);
688 *(int *)data
= -*(int *)data
;
692 error
= fo_ioctl(fp
, com
, data
, p
);
694 * Copy any data to user, size was
695 * already set and checked above.
697 if (error
== 0 && (com
&IOC_OUT
) && size
)
698 error
= copyout(data
, uap
->data
, (u_int
)size
);
706 int selwait
, nselcoll
;
707 #define SEL_FIRSTPASS 1
708 #define SEL_SECONDPASS 2
709 extern int selcontinue(int error
);
710 extern int selprocess(int error
, int sel_pass
);
711 static int selscan(struct proc
*p
, struct _select
* sel
,
712 int nfd
, register_t
*retval
, int sel_pass
);
713 static int selcount(struct proc
*p
, u_int32_t
*ibits
, u_int32_t
*obits
,
714 int nfd
, int * count
, int * nfcount
);
715 extern uint64_t tvtoabstime(struct timeval
*tvp
);
718 * Select system call.
720 #ifndef _SYS_SYSPROTO_H_
730 select(p
, uap
, retval
)
731 register struct proc
*p
;
732 register struct select_args
*uap
;
740 int needzerofill
= 1;
745 th_act
= current_act();
746 uth
= get_bsdthread_info(th_act
);
747 sel
= &uth
->uu_state
.ss_select
;
748 retval
= (int *)get_bsduthreadrval(th_act
);
755 if (uap
->nd
> p
->p_fd
->fd_nfiles
)
756 uap
->nd
= p
->p_fd
->fd_nfiles
; /* forgiving; slightly wrong */
758 nw
= howmany(uap
->nd
, NFDBITS
);
759 ni
= nw
* sizeof(fd_mask
);
762 * if this is the first select by the thread
763 * allocate the space for bits.
765 if (sel
->nbytes
== 0) {
766 sel
->nbytes
= 3 * ni
;
767 MALLOC(sel
->ibits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
);
768 MALLOC(sel
->obits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
);
769 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
770 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
775 * if the previously allocated space for the bits
776 * is smaller than what is requested. Reallocate.
778 if (sel
->nbytes
< (3 * ni
)) {
779 sel
->nbytes
= (3 * ni
);
780 FREE(sel
->ibits
, M_TEMP
);
781 FREE(sel
->obits
, M_TEMP
);
782 MALLOC(sel
->ibits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
);
783 MALLOC(sel
->obits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
);
784 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
785 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
790 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
791 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
795 * get the bits from the user address space
797 #define getbits(name, x) \
799 if (uap->name && (error = copyin((caddr_t)uap->name, \
800 (caddr_t)&sel->ibits[(x) * nw], ni))) \
812 error
= copyin((caddr_t
)uap
->tv
, (caddr_t
)&atv
, sizeof (atv
));
815 if (itimerfix(&atv
)) {
820 clock_absolutetime_interval_to_deadline(
821 tvtoabstime(&atv
), &sel
->abstime
);
827 if (error
= selcount(p
, sel
->ibits
, sel
->obits
, uap
->nd
, &count
, &nfcount
)) {
831 sel
->nfcount
= nfcount
;
833 size
= SIZEOF_WAITQUEUE_SUB
+ (count
* SIZEOF_WAITQUEUE_LINK
);
834 if (sel
->allocsize
) {
835 if (uth
->uu_wqsub
== 0)
836 panic("select: wql memory smashed");
837 /* needed for the select now */
838 if (size
> sel
->allocsize
) {
839 kfree(uth
->uu_wqsub
, sel
->allocsize
);
840 sel
->allocsize
= size
;
841 uth
->uu_wqsub
= (wait_queue_sub_t
)kalloc(sel
->allocsize
);
842 if (uth
->uu_wqsub
== (wait_queue_sub_t
)NULL
)
843 panic("failed to allocate memory for waitqueue\n");
844 sel
->wql
= (char *)uth
->uu_wqsub
+ SIZEOF_WAITQUEUE_SUB
;
848 sel
->allocsize
= size
;
849 uth
->uu_wqsub
= (wait_queue_sub_t
)kalloc(sel
->allocsize
);
850 if (uth
->uu_wqsub
== (wait_queue_sub_t
)NULL
)
851 panic("failed to allocate memory for waitqueue\n");
852 sel
->wql
= (char *)uth
->uu_wqsub
+ SIZEOF_WAITQUEUE_SUB
;
854 bzero(uth
->uu_wqsub
, size
);
855 wait_queue_sub_init(uth
->uu_wqsub
, (SYNC_POLICY_FIFO
| SYNC_POLICY_PREPOST
));
858 return selprocess(error
, SEL_FIRSTPASS
);
862 selcontinue(int error
)
864 return selprocess(error
, SEL_SECONDPASS
);
868 selprocess(error
, sel_pass
)
875 struct select_args
*uap
;
882 wait_result_t wait_result
;
885 th_act
= current_act();
886 uap
= (struct select_args
*)get_bsduthreadarg(th_act
);
887 retval
= (int *)get_bsduthreadrval(th_act
);
888 uth
= get_bsdthread_info(th_act
);
889 sel
= &uth
->uu_state
.ss_select
;
891 /* if it is first pass wait queue is not setup yet */
892 if ((error
!= 0) && (sel_pass
== SEL_FIRSTPASS
))
902 p
->p_flag
|= P_SELECT
;
903 /* skip scans if the select is just for timeouts */
905 if (sel_pass
== SEL_FIRSTPASS
)
906 wait_queue_sub_clearrefs(uth
->uu_wqsub
);
908 error
= selscan(p
, sel
, uap
->nd
, retval
, sel_pass
);
909 if (error
|| *retval
) {
913 /* if the select of log, then we canwakeup and discover some one
914 * else already read the data; go toselct again if time permits
928 clock_get_uptime(&now
);
929 if (now
>= sel
->abstime
)
934 /* cleanup obits and try again */
936 sel_pass
= SEL_FIRSTPASS
;
941 * To effect a poll, the timeout argument should be
942 * non-nil, pointing to a zero-valued timeval structure.
944 if (uap
->tv
&& sel
->abstime
== 0) {
948 /* No spurious wakeups due to colls,no need to check for them */
949 if ((sel_pass
== SEL_SECONDPASS
) || ((p
->p_flag
& P_SELECT
) == 0)) {
950 sel_pass
= SEL_FIRSTPASS
;
954 p
->p_flag
&= ~P_SELECT
;
956 /* if the select is just for timeout skip check */
957 if (sel
->count
&&(sel_pass
== SEL_SECONDPASS
))
958 panic("selprocess: 2nd pass assertwaiting");
960 /* Wait Queue Subordinate has waitqueue as first element */
961 wait_result
= wait_queue_assert_wait((wait_queue_t
)uth
->uu_wqsub
,
962 &selwait
, THREAD_ABORTSAFE
);
963 if (wait_result
!= THREAD_AWAKENED
) {
964 /* there are no preposted events */
965 error
= tsleep1(NULL
, PSOCK
| PCATCH
,
966 "select", sel
->abstime
, selcontinue
);
972 sel_pass
= SEL_SECONDPASS
;
980 wait_subqueue_unlink_all(uth
->uu_wqsub
);
981 p
->p_flag
&= ~P_SELECT
;
982 /* select is not restarted after signals... */
983 if (error
== ERESTART
)
985 if (error
== EWOULDBLOCK
)
987 nw
= howmany(uap
->nd
, NFDBITS
);
988 ni
= nw
* sizeof(fd_mask
);
990 #define putbits(name, x) \
992 if (uap->name && (error2 = copyout((caddr_t)&sel->obits[(x) * nw], \
993 (caddr_t)uap->name, ni))) \
1009 selscan(p
, sel
, nfd
, retval
, sel_pass
)
1011 struct _select
*sel
;
1016 register struct filedesc
*fdp
= p
->p_fd
;
1017 register int msk
, i
, j
, fd
;
1018 register u_int32_t bits
;
1022 static int flag
[3] = { FREAD
, FWRITE
, 0 };
1023 u_int32_t
*iptr
, *optr
;
1025 u_int32_t
*ibits
, *obits
;
1032 * Problems when reboot; due to MacOSX signal probs
1033 * in Beaker1C ; verify that the p->p_fd is valid
1045 nfcount
= sel
->nfcount
;
1047 if (nfcount
> count
)
1048 panic("selcount count<nfcount");
1050 nw
= howmany(nfd
, NFDBITS
);
1053 if ( nfcount
< count
) {
1054 /* some or all in kernel funnel */
1055 for (msk
= 0; msk
< 3; msk
++) {
1056 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1057 optr
= (u_int32_t
*)&obits
[msk
* nw
];
1058 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1059 bits
= iptr
[i
/NFDBITS
];
1060 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1062 fp
= fdp
->fd_ofiles
[fd
];
1064 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1067 if (sel_pass
== SEL_SECONDPASS
)
1068 wql_ptr
= (char *)0;
1070 wql_ptr
= (wql
+ nc
* SIZEOF_WAITQUEUE_LINK
);
1071 if (fp
->f_ops
&& (fp
->f_type
!= DTYPE_SOCKET
)
1072 && fo_select(fp
, flag
[msk
], wql_ptr
, p
)) {
1073 optr
[fd
/NFDBITS
] |= (1 << (fd
% NFDBITS
));
1083 /* socket file descriptors for scan */
1084 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1087 for (msk
= 0; msk
< 3; msk
++) {
1088 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1089 optr
= (u_int32_t
*)&obits
[msk
* nw
];
1090 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1091 bits
= iptr
[i
/NFDBITS
];
1092 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1094 fp
= fdp
->fd_ofiles
[fd
];
1096 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1097 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1100 if (sel_pass
== SEL_SECONDPASS
)
1101 wql_ptr
= (char *)0;
1103 wql_ptr
= (wql
+ nc
* SIZEOF_WAITQUEUE_LINK
);
1104 if (fp
->f_ops
&& (fp
->f_type
== DTYPE_SOCKET
) &&
1105 fo_select(fp
, flag
[msk
], wql_ptr
, p
)) {
1106 optr
[fd
/NFDBITS
] |= (1 << (fd
% NFDBITS
));
1113 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1122 seltrue(dev
, flag
, p
)
1132 selcount(p
, ibits
, obits
, nfd
, count
, nfcount
)
1134 u_int32_t
*ibits
, *obits
;
1139 register struct filedesc
*fdp
= p
->p_fd
;
1140 register int msk
, i
, j
, fd
;
1141 register u_int32_t bits
;
1146 static int flag
[3] = { FREAD
, FWRITE
, 0 };
1147 u_int32_t
*iptr
, *fptr
, *fbits
;
1151 * Problems when reboot; due to MacOSX signal probs
1152 * in Beaker1C ; verify that the p->p_fd is valid
1160 nw
= howmany(nfd
, NFDBITS
);
1163 for (msk
= 0; msk
< 3; msk
++) {
1164 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1165 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1166 bits
= iptr
[i
/NFDBITS
];
1167 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1169 fp
= fdp
->fd_ofiles
[fd
];
1171 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1176 if (fp
->f_type
== DTYPE_SOCKET
)
1188 * Record a select request.
1191 selrecord(selector
, sip
, p_wql
)
1192 struct proc
*selector
;
1193 struct selinfo
*sip
;
1196 thread_act_t cur_act
= current_act();
1197 struct uthread
* ut
= get_bsdthread_info(cur_act
);
1199 /* need to look at collisions */
1201 if ((p_wql
== (void *)0) && ((sip
->si_flags
& SI_INITED
) == 0)) {
1205 /*do not record if this is second pass of select */
1206 if((p_wql
== (void *)0)) {
1210 if ((sip
->si_flags
& SI_INITED
) == 0) {
1211 wait_queue_init(&sip
->wait_queue
, SYNC_POLICY_FIFO
);
1212 sip
->si_flags
|= SI_INITED
;
1213 sip
->si_flags
&= ~SI_CLEAR
;
1216 if (sip
->si_flags
& SI_RECORDED
) {
1217 sip
->si_flags
|= SI_COLL
;
1219 sip
->si_flags
&= ~SI_COLL
;
1221 sip
->si_flags
|= SI_RECORDED
;
1222 if (!wait_queue_member(&sip
->wait_queue
, ut
->uu_wqsub
))
1223 wait_queue_link_noalloc(&sip
->wait_queue
, ut
->uu_wqsub
, (wait_queue_link_t
)p_wql
);
1230 register struct selinfo
*sip
;
1233 if ((sip
->si_flags
& SI_INITED
) == 0) {
1237 if (sip
->si_flags
& SI_COLL
) {
1239 sip
->si_flags
&= ~SI_COLL
;
1241 /* will not support */
1242 //wakeup((caddr_t)&selwait);
1246 if (sip
->si_flags
& SI_RECORDED
) {
1247 wait_queue_wakeup_all(&sip
->wait_queue
, &selwait
, THREAD_AWAKENED
);
1248 sip
->si_flags
&= ~SI_RECORDED
;
1255 register struct selinfo
*sip
;
1258 if ((sip
->si_flags
& SI_INITED
) == 0) {
1261 if (sip
->si_flags
& SI_RECORDED
) {
1263 sip
->si_flags
&= ~(SI_RECORDED
| SI_COLL
);
1265 sip
->si_flags
|= SI_CLEAR
;
1266 wait_queue_unlinkall_nofree(&sip
->wait_queue
);
1270 extern struct eventqelt
*evprocdeque(struct proc
*p
, struct eventqelt
*eqp
);
1273 * called upon socket close. deque and free all events for
1277 evsofree(struct socket
*sp
)
1279 struct eventqelt
*eqp
, *next
;
1281 if (sp
== NULL
) return;
1283 for (eqp
= sp
->so_evlist
.tqh_first
; eqp
!= NULL
; eqp
= next
) {
1284 next
= eqp
->ee_slist
.tqe_next
;
1285 evprocdeque(eqp
->ee_proc
, eqp
); // remove from proc q if there
1286 TAILQ_REMOVE(&sp
->so_evlist
, eqp
, ee_slist
); // remove from socket q
1292 #define DBG_EVENT 0x10
1294 #define DBG_POST 0x10
1295 #define DBG_WATCH 0x11
1296 #define DBG_WAIT 0x12
1297 #define DBG_MOD 0x13
1298 #define DBG_EWAKEUP 0x14
1299 #define DBG_ENQUEUE 0x15
1300 #define DBG_DEQUEUE 0x16
1302 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
1303 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
1304 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
1305 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
1306 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
1307 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
1308 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
1312 * enque this event if it's not already queued. wakeup
1313 the proc if we do queue this event to it.
1316 evprocenque(struct eventqelt
*eqp
)
1321 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_START
, eqp
, eqp
->ee_flags
, eqp
->ee_eventmask
,0,0);
1322 if (eqp
->ee_flags
& EV_QUEUED
) {
1323 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1326 eqp
->ee_flags
|= EV_QUEUED
;
1327 eqp
->ee_eventmask
= 0; // disarm
1329 TAILQ_INSERT_TAIL(&p
->p_evlist
, eqp
, ee_plist
);
1330 KERNEL_DEBUG(DBG_MISC_EWAKEUP
,0,0,0,eqp
,0);
1331 wakeup(&p
->p_evlist
);
1332 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1336 * given either a sockbuf or a socket run down the
1337 * event list and queue ready events found
1340 postevent(struct socket
*sp
, struct sockbuf
*sb
, int event
)
1343 struct eventqelt
*evq
;
1344 register struct tcpcb
*tp
;
1346 if (sb
) sp
= sb
->sb_so
;
1347 if (!sp
|| sp
->so_evlist
.tqh_first
== NULL
) return;
1349 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, event
,0,0,0,0);
1351 for (evq
= sp
->so_evlist
.tqh_first
;
1352 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1356 /* ready for reading:
1357 - byte cnt >= receive low water mark
1358 - read-half of conn closed
1359 - conn pending for listening sock
1360 - socket error pending
1363 - byte cnt avail >= send low water mark
1364 - write half of conn closed
1365 - socket error pending
1366 - non-blocking conn completed successfully
1370 - sock at out of band mark
1373 switch (event
& EV_DMASK
) {
1377 case EV_RWBYTES
|EV_OOB
:
1378 if (event
& EV_OOB
) {
1379 if ((evq
->ee_eventmask
& EV_EX
)) {
1380 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
))) {
1381 mask
|= EV_EX
|EV_OOB
;
1385 if (event
& EV_RWBYTES
) {
1386 if ((evq
->ee_eventmask
& EV_RE
) && soreadable(sp
)) {
1387 if ((sp
->so_type
== SOCK_STREAM
) && (sp
->so_error
== ECONNREFUSED
) ||
1388 (sp
->so_error
== ECONNRESET
)) {
1389 if ((sp
->so_pcb
== 0) ||
1390 !(tp
= sototcpcb(sp
)) ||
1391 (tp
->t_state
== TCPS_CLOSED
)) {
1392 mask
|= EV_RE
|EV_RESET
;
1396 if (sp
->so_state
& SS_CANTRCVMORE
) {
1397 mask
|= EV_RE
|EV_FIN
;
1398 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
1402 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
1405 if ((evq
->ee_eventmask
& EV_WR
) && sowriteable(sp
)) {
1406 if ((sp
->so_type
== SOCK_STREAM
) &&(sp
->so_error
== ECONNREFUSED
) ||
1407 (sp
->so_error
== ECONNRESET
)) {
1408 if ((sp
->so_pcb
== 0) ||
1409 !(tp
= sototcpcb(sp
)) ||
1410 (tp
->t_state
== TCPS_CLOSED
)) {
1411 mask
|= EV_WR
|EV_RESET
;
1416 evq
->ee_req
.er_wcnt
= sbspace(&sp
->so_snd
);
1422 if ((evq
->ee_eventmask
& EV_RE
)) {
1423 evq
->ee_req
.er_rcnt
= sp
->so_qlen
+ 1; // incl this one
1424 mask
|= EV_RE
|EV_RCONN
;
1429 if ((evq
->ee_eventmask
& EV_WR
)) {
1430 mask
|= EV_WR
|EV_WCONN
;
1435 if ((evq
->ee_eventmask
& EV_RE
)) {
1436 mask
|= EV_RE
|EV_RCLOSED
;
1441 if ((evq
->ee_eventmask
& EV_WR
)) {
1442 mask
|= EV_WR
|EV_WCLOSED
;
1447 if (evq
->ee_eventmask
& EV_RE
) {
1448 mask
|= EV_RE
|EV_FIN
;
1454 if (evq
->ee_eventmask
& EV_RE
) {
1455 mask
|= EV_RE
| event
;
1457 if (evq
->ee_eventmask
& EV_WR
) {
1458 mask
|= EV_WR
| event
;
1467 evq
->ee_req
.er_eventbits
|= mask
;
1468 KERNEL_DEBUG(DBG_MISC_POST
, evq
, evq
->ee_req
.er_eventbits
, mask
,0,0);
1472 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, 0,0,0,0,0);
1476 * remove and return the first event (eqp=NULL) or a specific
1477 * event, or return NULL if no events found
1480 evprocdeque(struct proc
*p
, struct eventqelt
*eqp
)
1483 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_START
,p
,eqp
,0,0,0);
1485 if (eqp
&& ((eqp
->ee_flags
& EV_QUEUED
) == NULL
)) {
1486 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_END
,0,0,0,0,0);
1489 if (p
->p_evlist
.tqh_first
== NULL
) {
1490 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_END
,0,0,0,0,0);
1493 if (eqp
== NULL
) { // remove first
1494 eqp
= p
->p_evlist
.tqh_first
;
1496 TAILQ_REMOVE(&p
->p_evlist
, eqp
, ee_plist
);
1497 eqp
->ee_flags
&= ~EV_QUEUED
;
1498 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_END
,eqp
,0,0,0,0);
1502 struct evwatch_args
{
1503 struct eventreq
*u_req
;
1509 * watchevent system call. user passes us an event to watch
1510 * for. we malloc an event object, initialize it, and queue
1511 * it to the open socket. when the event occurs, postevent()
1512 * will enque it back to our proc where we can retrieve it
1515 * should this prevent duplicate events on same socket?
1518 watchevent(p
, uap
, retval
)
1520 struct evwatch_args
*uap
;
1523 struct eventqelt
*eqp
= (struct eventqelt
*)0;
1524 struct eventqelt
*np
;
1525 struct eventreq
*erp
;
1530 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_START
, 0,0,0,0,0);
1532 // get a qelt and fill with users req
1533 MALLOC(eqp
, struct eventqelt
*, sizeof(struct eventqelt
), M_TEMP
, M_WAITOK
);
1534 if (!eqp
) panic("can't MALLOC eqp");
1536 // get users request pkt
1537 if (error
= copyin((caddr_t
)uap
->u_req
, (caddr_t
)erp
,
1538 sizeof(struct eventreq
))) {
1540 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
1543 KERNEL_DEBUG(DBG_MISC_WATCH
, erp
->er_handle
,uap
->u_eventmask
,eqp
,0,0);
1544 // validate, freeing qelt if errors
1546 if (erp
->er_type
!= EV_FD
) {
1548 } else if (erp
->er_handle
< 0) {
1550 } else if (erp
->er_handle
> p
->p_fd
->fd_nfiles
) {
1552 } else if ((fp
= *fdfile(p
, erp
->er_handle
)) == NULL
) {
1554 } else if (fp
->f_type
!= DTYPE_SOCKET
) {
1559 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
1563 erp
->er_rcnt
= erp
->er_wcnt
= erp
->er_eventbits
= 0;
1565 eqp
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
1568 sp
= (struct socket
*)fp
->f_data
;
1571 // only allow one watch per file per proc
1572 for (np
= sp
->so_evlist
.tqh_first
; np
!= NULL
; np
= np
->ee_slist
.tqe_next
) {
1573 if (np
->ee_proc
== p
) {
1575 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
1580 TAILQ_INSERT_TAIL(&sp
->so_evlist
, eqp
, ee_slist
);
1581 postevent(sp
, 0, EV_RWBYTES
); // catch existing events
1582 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, 0,0,0,0,0);
1586 struct evwait_args
{
1587 struct eventreq
*u_req
;
1592 * waitevent system call.
1593 * grabs the next waiting event for this proc and returns
1594 * it. if no events, user can request to sleep with timeout
1595 * or poll mode (tv=NULL);
1598 waitevent(p
, uap
, retval
)
1600 struct evwait_args
*uap
;
1604 struct eventqelt
*eqp
;
1605 uint64_t abstime
, interval
;
1610 error
= copyin((caddr_t
)uap
->tv
, (caddr_t
)&atv
, sizeof (atv
));
1613 if (itimerfix(&atv
)) {
1618 interval
= tvtoabstime(&atv
);
1621 abstime
= interval
= 0;
1623 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_START
, 0,0,0,0,0);
1626 if ((eqp
= evprocdeque(p
,NULL
)) != NULL
) {
1627 error
= copyout((caddr_t
)&eqp
->ee_req
,
1628 (caddr_t
)uap
->u_req
, sizeof(struct eventreq
));
1629 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,
1630 eqp
->ee_req
.er_handle
,eqp
->ee_req
.er_eventbits
,eqp
,0);
1635 if (uap
->tv
&& interval
== 0) {
1636 *retval
= 1; // poll failed
1637 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,0,0,0,0);
1643 clock_absolutetime_interval_to_deadline(interval
, &abstime
)
1645 KERNEL_DEBUG(DBG_MISC_WAIT
, 1,&p
->p_evlist
,0,0,0);
1646 error
= tsleep1(&p
->p_evlist
, PSOCK
| PCATCH
,
1647 "waitevent", abstime
, (int (*)(int))0);
1648 KERNEL_DEBUG(DBG_MISC_WAIT
, 2,&p
->p_evlist
,0,0,0);
1651 if (error
== ERESTART
)
1653 if (error
== EWOULDBLOCK
) {
1659 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, 0,0,0,0,0);
1664 struct modwatch_args
{
1665 struct eventreq
*u_req
;
1670 * modwatch system call. user passes in event to modify.
1671 * if we find it we reset the event bits and que/deque event
1675 modwatch(p
, uap
, retval
)
1677 struct modwatch_args
*uap
;
1681 struct eventreq
*erp
= &er
;
1682 struct eventqelt
*evq
;
1688 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_START
, 0,0,0,0,0);
1690 // get users request pkt
1691 if (error
= copyin((caddr_t
)uap
->u_req
, (caddr_t
)erp
,
1692 sizeof(struct eventreq
))) return(error
);
1694 if (erp
->er_type
!= EV_FD
) return(EINVAL
);
1695 if (erp
->er_handle
< 0) return(EBADF
);
1696 if (erp
->er_handle
> p
->p_fd
->fd_nfiles
) return(EBADF
);
1697 if ((fp
= *fdfile(p
, erp
->er_handle
)) == NULL
)
1699 if (fp
->f_type
!= DTYPE_SOCKET
) return(EINVAL
); // for now must be sock
1700 sp
= (struct socket
*)fp
->f_data
;
1704 // locate event if possible
1705 for (evq
= sp
->so_evlist
.tqh_first
;
1706 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1707 if (evq
->ee_proc
== p
) break;
1711 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
1714 KERNEL_DEBUG(DBG_MISC_MOD
, erp
->er_handle
,uap
->u_eventmask
,evq
,0,0);
1716 if (uap
->u_eventmask
== EV_RM
) {
1717 evprocdeque(p
, evq
);
1718 TAILQ_REMOVE(&sp
->so_evlist
, evq
, ee_slist
);
1720 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, 0,0,0,0,0);
1724 switch (uap
->u_eventmask
& EV_MASK
) {
1742 case EV_EX
|EV_RE
|EV_WR
:
1743 flag
= EV_OOB
|EV_RWBYTES
;
1750 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
1751 evprocdeque(p
, evq
);
1752 evq
->ee_req
.er_eventbits
= 0;
1753 postevent(sp
, 0, flag
);
1754 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, evq
->ee_req
.er_handle
,evq
->ee_eventmask
,sp
,flag
,0);