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) {
135 #ifndef _SYS_SYSPROTO_H_
145 register struct read_args
*uap
;
148 register struct file
*fp
;
151 if ((fp
= holdfp(p
->p_fd
, uap
->fd
, FREAD
)) == NULL
)
153 error
= dofileread(p
, fp
, uap
->fd
, uap
->cbuf
, uap
->nbyte
,
154 (off_t
)-1, 0, retval
);
162 #ifndef _SYS_SYSPROTO_H_
167 #ifdef DOUBLE_ALIGN_PARAMS
174 pread(p
, uap
, retval
)
176 register struct pread_args
*uap
;
179 register struct file
*fp
;
182 if ((fp
= holdfp(p
->p_fd
, uap
->fd
, FREAD
)) == NULL
)
184 if (fp
->f_type
!= DTYPE_VNODE
) {
187 error
= dofileread(p
, fp
, uap
->fd
, uap
->buf
, uap
->nbyte
,
188 uap
->offset
, FOF_OFFSET
, retval
);
195 * Code common for read and pread
198 dofileread(p
, fp
, fd
, buf
, nbyte
, offset
, flags
, retval
)
216 aiov
.iov_base
= (caddr_t
)buf
;
217 aiov
.iov_len
= nbyte
;
218 auio
.uio_iov
= &aiov
;
220 auio
.uio_offset
= offset
;
223 auio
.uio_resid
= nbyte
;
224 auio
.uio_rw
= UIO_READ
;
225 auio
.uio_segflg
= UIO_USERSPACE
;
229 * if tracing, save a copy of iovec
231 if (KTRPOINT(p
, KTR_GENIO
)) {
239 if ((error
= fo_read(fp
, &auio
, fp
->f_cred
, flags
, p
))) {
240 if (auio
.uio_resid
!= cnt
&& (error
== ERESTART
||
241 error
== EINTR
|| error
== EWOULDBLOCK
))
244 cnt
-= auio
.uio_resid
;
246 if (didktr
&& error
== 0) {
247 ktruio
.uio_iov
= &ktriov
;
248 ktruio
.uio_resid
= cnt
;
249 ktrgenio(p
->p_tracep
, fd
, UIO_READ
, &ktruio
, error
,
258 * Scatter read system call.
260 #ifndef _SYS_SYSPROTO_H_
268 readv(p
, uap
, retval
)
270 register struct readv_args
*uap
;
274 register struct iovec
*iov
;
276 struct iovec aiov
[UIO_SMALLIOV
];
278 if (uap
->iovcnt
> UIO_SMALLIOV
) {
279 if (uap
->iovcnt
> UIO_MAXIOV
)
281 if ((iov
= (struct iovec
*)
282 kalloc(sizeof(struct iovec
) * (uap
->iovcnt
))) == 0)
287 auio
.uio_iovcnt
= uap
->iovcnt
;
288 auio
.uio_rw
= UIO_READ
;
289 error
= copyin((caddr_t
)uap
->iovp
, (caddr_t
)iov
,
290 uap
->iovcnt
* sizeof (struct iovec
));
292 error
= rwuio(p
, uap
->fd
, &auio
, UIO_READ
, retval
);
293 if (uap
->iovcnt
> UIO_SMALLIOV
)
294 kfree(iov
, sizeof(struct iovec
)*uap
->iovcnt
);
301 #ifndef _SYS_SYSPROTO_H_
309 write(p
, uap
, retval
)
311 register struct write_args
*uap
;
314 register struct file
*fp
;
317 if ((fp
= holdfp(p
->p_fd
, uap
->fd
, FWRITE
)) == NULL
)
319 error
= dofilewrite(p
, fp
, uap
->fd
, uap
->cbuf
, uap
->nbyte
,
320 (off_t
)-1, 0, retval
);
328 #ifndef _SYS_SYSPROTO_H_
333 #ifdef DOUBLE_ALIGN_PARAMS
340 pwrite(p
, uap
, retval
)
342 register struct pwrite_args
*uap
;
345 register struct file
*fp
;
348 if ((fp
= holdfp(p
->p_fd
, uap
->fd
, FWRITE
)) == NULL
)
350 if (fp
->f_type
!= DTYPE_VNODE
) {
353 error
= dofilewrite(p
, fp
, uap
->fd
, uap
->buf
, uap
->nbyte
,
354 uap
->offset
, FOF_OFFSET
, retval
);
361 dofilewrite(p
, fp
, fd
, buf
, nbyte
, offset
, flags
, retval
)
379 aiov
.iov_base
= (void *)(uintptr_t)buf
;
380 aiov
.iov_len
= nbyte
;
381 auio
.uio_iov
= &aiov
;
383 auio
.uio_offset
= offset
;
386 auio
.uio_resid
= nbyte
;
387 auio
.uio_rw
= UIO_WRITE
;
388 auio
.uio_segflg
= UIO_USERSPACE
;
392 * if tracing, save a copy of iovec and uio
394 if (KTRPOINT(p
, KTR_GENIO
)) {
401 if (fp
->f_type
== DTYPE_VNODE
)
403 if ((error
= fo_write(fp
, &auio
, fp
->f_cred
, flags
, p
))) {
404 if (auio
.uio_resid
!= cnt
&& (error
== ERESTART
||
405 error
== EINTR
|| error
== EWOULDBLOCK
))
410 cnt
-= auio
.uio_resid
;
412 if (didktr
&& error
== 0) {
413 ktruio
.uio_iov
= &ktriov
;
414 ktruio
.uio_resid
= cnt
;
415 ktrgenio(p
->p_tracep
, fd
, UIO_WRITE
, &ktruio
, error
,
424 * Gather write system call
426 #ifndef _SYS_SYSPROTO_H_
434 writev(p
, uap
, retval
)
436 register struct writev_args
*uap
;
440 register struct iovec
*iov
;
442 struct iovec aiov
[UIO_SMALLIOV
];
444 if (uap
->iovcnt
> UIO_SMALLIOV
) {
445 if (uap
->iovcnt
> UIO_MAXIOV
)
447 if ((iov
= (struct iovec
*)
448 kalloc(sizeof(struct iovec
) * (uap
->iovcnt
))) == 0)
453 auio
.uio_iovcnt
= uap
->iovcnt
;
454 auio
.uio_rw
= UIO_WRITE
;
455 error
= copyin((caddr_t
)uap
->iovp
, (caddr_t
)iov
,
456 uap
->iovcnt
* sizeof (struct iovec
));
458 error
= rwuio(p
, uap
->fd
, &auio
, UIO_WRITE
, retval
);
459 if (uap
->iovcnt
> UIO_SMALLIOV
)
460 kfree(iov
, sizeof(struct iovec
)*uap
->iovcnt
);
465 rwuio(p
, fdes
, uio
, rw
, retval
)
468 register struct uio
*uio
;
473 register struct iovec
*iov
;
474 int i
, count
, flag
, error
;
476 struct iovec
*ktriov
;
482 if (error
= fdgetf(p
, fdes
, &fp
))
485 if ((fp
->f_flag
&(rw
==UIO_READ
? FREAD
: FWRITE
)) == 0) {
489 uio
->uio_segflg
= UIO_USERSPACE
;
492 for (i
= 0; i
< uio
->uio_iovcnt
; i
++) {
493 if (iov
->iov_len
< 0) {
496 uio
->uio_resid
+= iov
->iov_len
;
497 if (uio
->uio_resid
< 0) {
502 count
= uio
->uio_resid
;
505 * if tracing, save a copy of iovec
507 if (KTRPOINT(p
, KTR_GENIO
)) {
508 iovlen
= uio
->uio_iovcnt
* sizeof (struct iovec
);
509 MALLOC(ktriov
, struct iovec
*, iovlen
, M_TEMP
, M_WAITOK
);
510 bcopy((caddr_t
)uio
->uio_iov
, (caddr_t
)ktriov
, iovlen
);
516 if (rw
== UIO_READ
) {
517 if (error
= fo_read(fp
, uio
, fp
->f_cred
, 0, p
))
518 if (uio
->uio_resid
!= count
&& (error
== ERESTART
||
519 error
== EINTR
|| error
== EWOULDBLOCK
))
522 if (fp
->f_type
== DTYPE_VNODE
)
524 if (error
= fo_write(fp
, uio
, fp
->f_cred
, 0, p
)) {
525 if (uio
->uio_resid
!= count
&& (error
== ERESTART
||
526 error
== EINTR
|| error
== EWOULDBLOCK
))
528 /* The socket layer handles SIGPIPE */
529 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
)
534 *retval
= count
- uio
->uio_resid
;
539 ktruio
.uio_iov
= ktriov
;
540 ktruio
.uio_resid
= *retval
;
541 ktrgenio(p
->p_tracep
, fdes
, rw
, &ktruio
, error
,
544 FREE(ktriov
, M_TEMP
);
554 #ifndef _SYS_SYSPROTO_H_
562 ioctl(p
, uap
, retval
)
564 register struct ioctl_args
*uap
;
573 #define STK_PARAMS 128
574 char stkbuf
[STK_PARAMS
];
576 if (error
= fdgetf(p
, uap
->fd
, &fp
))
579 if ((fp
->f_flag
& (FREAD
| FWRITE
)) == 0)
584 * ### LD 6/11/97 Hack Alert: this is to get AppleTalk to work
585 * while implementing an ATioctl system call
588 extern int appletalk_inited
;
590 if (appletalk_inited
&& ((uap
->com
& 0x0000FFFF) == 0xff99)) {
591 #ifdef APPLETALK_DEBUG
592 kprintf("ioctl: special AppleTalk \n");
594 error
= fo_ioctl(fp
, uap
->com
, uap
->data
, p
);
602 switch (com
= uap
->com
) {
604 *fdflags(p
, uap
->fd
) &= ~UF_EXCLOSE
;
607 *fdflags(p
, uap
->fd
) |= UF_EXCLOSE
;
612 * Interpret high order word to find amount of data to be
613 * copied to/from the user's address space.
615 size
= IOCPARM_LEN(com
);
616 if (size
> IOCPARM_MAX
)
619 if (size
> sizeof (stkbuf
)) {
620 if ((memp
= (caddr_t
)kalloc(size
)) == 0)
627 error
= copyin(uap
->data
, data
, (u_int
)size
);
634 *(caddr_t
*)data
= uap
->data
;
635 } else if ((com
&IOC_OUT
) && size
)
637 * Zero the buffer so the user always
638 * gets back something deterministic.
641 else if (com
&IOC_VOID
)
642 *(caddr_t
*)data
= uap
->data
;
647 if (tmp
= *(int *)data
)
648 fp
->f_flag
|= FNONBLOCK
;
650 fp
->f_flag
&= ~FNONBLOCK
;
651 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
655 if (tmp
= *(int *)data
)
656 fp
->f_flag
|= FASYNC
;
658 fp
->f_flag
&= ~FASYNC
;
659 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, p
);
664 if (fp
->f_type
== DTYPE_SOCKET
) {
665 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
672 struct proc
*p1
= pfind(tmp
);
677 tmp
= p1
->p_pgrp
->pg_id
;
679 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, p
);
683 if (fp
->f_type
== DTYPE_SOCKET
) {
685 *(int *)data
= ((struct socket
*)fp
->f_data
)->so_pgid
;
688 error
= fo_ioctl(fp
, TIOCGPGRP
, data
, p
);
689 *(int *)data
= -*(int *)data
;
693 error
= fo_ioctl(fp
, com
, data
, p
);
695 * Copy any data to user, size was
696 * already set and checked above.
698 if (error
== 0 && (com
&IOC_OUT
) && size
)
699 error
= copyout(data
, uap
->data
, (u_int
)size
);
707 int selwait
, nselcoll
;
708 #define SEL_FIRSTPASS 1
709 #define SEL_SECONDPASS 2
710 extern int selcontinue(int error
);
711 extern int selprocess(int error
, int sel_pass
);
712 static int selscan(struct proc
*p
, struct _select
* sel
,
713 int nfd
, register_t
*retval
, int sel_pass
);
714 static int selcount(struct proc
*p
, u_int32_t
*ibits
, u_int32_t
*obits
,
715 int nfd
, int * count
, int * nfcount
);
716 extern uint64_t tvtoabstime(struct timeval
*tvp
);
719 * Select system call.
721 #ifndef _SYS_SYSPROTO_H_
731 select(p
, uap
, retval
)
732 register struct proc
*p
;
733 register struct select_args
*uap
;
741 int needzerofill
= 1;
746 th_act
= current_act();
747 uth
= get_bsdthread_info(th_act
);
748 sel
= &uth
->uu_state
.ss_select
;
749 retval
= (int *)get_bsduthreadrval(th_act
);
756 if (uap
->nd
> p
->p_fd
->fd_nfiles
)
757 uap
->nd
= p
->p_fd
->fd_nfiles
; /* forgiving; slightly wrong */
759 nw
= howmany(uap
->nd
, NFDBITS
);
760 ni
= nw
* sizeof(fd_mask
);
763 * if this is the first select by the thread
764 * allocate the space for bits.
766 if (sel
->nbytes
== 0) {
767 sel
->nbytes
= 3 * ni
;
768 MALLOC(sel
->ibits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
);
769 MALLOC(sel
->obits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
);
770 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
771 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
776 * if the previously allocated space for the bits
777 * is smaller than what is requested. Reallocate.
779 if (sel
->nbytes
< (3 * ni
)) {
780 sel
->nbytes
= (3 * ni
);
781 FREE(sel
->ibits
, M_TEMP
);
782 FREE(sel
->obits
, M_TEMP
);
783 MALLOC(sel
->ibits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
);
784 MALLOC(sel
->obits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
);
785 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
786 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
791 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
792 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
796 * get the bits from the user address space
798 #define getbits(name, x) \
800 if (uap->name && (error = copyin((caddr_t)uap->name, \
801 (caddr_t)&sel->ibits[(x) * nw], ni))) \
813 error
= copyin((caddr_t
)uap
->tv
, (caddr_t
)&atv
, sizeof (atv
));
816 if (itimerfix(&atv
)) {
821 clock_absolutetime_interval_to_deadline(
822 tvtoabstime(&atv
), &sel
->abstime
);
828 if (error
= selcount(p
, sel
->ibits
, sel
->obits
, uap
->nd
, &count
, &nfcount
)) {
832 sel
->nfcount
= nfcount
;
834 size
= SIZEOF_WAITQUEUE_SUB
+ (count
* SIZEOF_WAITQUEUE_LINK
);
835 if (sel
->allocsize
) {
836 if (uth
->uu_wqsub
== 0)
837 panic("select: wql memory smashed");
838 /* needed for the select now */
839 if (size
> sel
->allocsize
) {
840 kfree(uth
->uu_wqsub
, sel
->allocsize
);
841 sel
->allocsize
= size
;
842 uth
->uu_wqsub
= (wait_queue_sub_t
)kalloc(sel
->allocsize
);
843 if (uth
->uu_wqsub
== (wait_queue_sub_t
)NULL
)
844 panic("failed to allocate memory for waitqueue\n");
845 sel
->wql
= (char *)uth
->uu_wqsub
+ SIZEOF_WAITQUEUE_SUB
;
849 sel
->allocsize
= size
;
850 uth
->uu_wqsub
= (wait_queue_sub_t
)kalloc(sel
->allocsize
);
851 if (uth
->uu_wqsub
== (wait_queue_sub_t
)NULL
)
852 panic("failed to allocate memory for waitqueue\n");
853 sel
->wql
= (char *)uth
->uu_wqsub
+ SIZEOF_WAITQUEUE_SUB
;
855 bzero(uth
->uu_wqsub
, size
);
856 wait_queue_sub_init(uth
->uu_wqsub
, (SYNC_POLICY_FIFO
| SYNC_POLICY_PREPOST
));
859 return selprocess(error
, SEL_FIRSTPASS
);
863 selcontinue(int error
)
865 return selprocess(error
, SEL_SECONDPASS
);
869 selprocess(error
, sel_pass
)
876 struct select_args
*uap
;
883 wait_result_t wait_result
;
886 th_act
= current_act();
887 uap
= (struct select_args
*)get_bsduthreadarg(th_act
);
888 retval
= (int *)get_bsduthreadrval(th_act
);
889 uth
= get_bsdthread_info(th_act
);
890 sel
= &uth
->uu_state
.ss_select
;
892 /* if it is first pass wait queue is not setup yet */
893 if ((error
!= 0) && (sel_pass
== SEL_FIRSTPASS
))
903 p
->p_flag
|= P_SELECT
;
904 /* skip scans if the select is just for timeouts */
906 if (sel_pass
== SEL_FIRSTPASS
)
907 wait_queue_sub_clearrefs(uth
->uu_wqsub
);
909 error
= selscan(p
, sel
, uap
->nd
, retval
, sel_pass
);
910 if (error
|| *retval
) {
914 /* if the select of log, then we canwakeup and discover some one
915 * else already read the data; go toselct again if time permits
929 clock_get_uptime(&now
);
930 if (now
>= sel
->abstime
)
935 /* cleanup obits and try again */
937 sel_pass
= SEL_FIRSTPASS
;
942 * To effect a poll, the timeout argument should be
943 * non-nil, pointing to a zero-valued timeval structure.
945 if (uap
->tv
&& sel
->abstime
== 0) {
949 /* No spurious wakeups due to colls,no need to check for them */
950 if ((sel_pass
== SEL_SECONDPASS
) || ((p
->p_flag
& P_SELECT
) == 0)) {
951 sel_pass
= SEL_FIRSTPASS
;
955 p
->p_flag
&= ~P_SELECT
;
957 /* if the select is just for timeout skip check */
958 if (sel
->count
&&(sel_pass
== SEL_SECONDPASS
))
959 panic("selprocess: 2nd pass assertwaiting");
961 /* Wait Queue Subordinate has waitqueue as first element */
962 wait_result
= wait_queue_assert_wait((wait_queue_t
)uth
->uu_wqsub
,
963 &selwait
, THREAD_ABORTSAFE
);
964 if (wait_result
!= THREAD_AWAKENED
) {
965 /* there are no preposted events */
966 error
= tsleep1(NULL
, PSOCK
| PCATCH
,
967 "select", sel
->abstime
, selcontinue
);
973 sel_pass
= SEL_SECONDPASS
;
981 wait_subqueue_unlink_all(uth
->uu_wqsub
);
982 p
->p_flag
&= ~P_SELECT
;
983 /* select is not restarted after signals... */
984 if (error
== ERESTART
)
986 if (error
== EWOULDBLOCK
)
988 nw
= howmany(uap
->nd
, NFDBITS
);
989 ni
= nw
* sizeof(fd_mask
);
991 #define putbits(name, x) \
993 if (uap->name && (error2 = copyout((caddr_t)&sel->obits[(x) * nw], \
994 (caddr_t)uap->name, ni))) \
1010 selscan(p
, sel
, nfd
, retval
, sel_pass
)
1012 struct _select
*sel
;
1017 register struct filedesc
*fdp
= p
->p_fd
;
1018 register int msk
, i
, j
, fd
;
1019 register u_int32_t bits
;
1023 static int flag
[3] = { FREAD
, FWRITE
, 0 };
1024 u_int32_t
*iptr
, *optr
;
1026 u_int32_t
*ibits
, *obits
;
1033 * Problems when reboot; due to MacOSX signal probs
1034 * in Beaker1C ; verify that the p->p_fd is valid
1046 nfcount
= sel
->nfcount
;
1048 if (nfcount
> count
)
1049 panic("selcount count<nfcount");
1051 nw
= howmany(nfd
, NFDBITS
);
1054 if ( nfcount
< count
) {
1055 /* some or all in kernel funnel */
1056 for (msk
= 0; msk
< 3; msk
++) {
1057 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1058 optr
= (u_int32_t
*)&obits
[msk
* nw
];
1059 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1060 bits
= iptr
[i
/NFDBITS
];
1061 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1063 fp
= fdp
->fd_ofiles
[fd
];
1065 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1068 if (sel_pass
== SEL_SECONDPASS
)
1069 wql_ptr
= (char *)0;
1071 wql_ptr
= (wql
+ nc
* SIZEOF_WAITQUEUE_LINK
);
1072 if (fp
->f_ops
&& (fp
->f_type
!= DTYPE_SOCKET
)
1073 && fo_select(fp
, flag
[msk
], wql_ptr
, p
)) {
1074 optr
[fd
/NFDBITS
] |= (1 << (fd
% NFDBITS
));
1084 /* socket file descriptors for scan */
1085 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1088 for (msk
= 0; msk
< 3; msk
++) {
1089 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1090 optr
= (u_int32_t
*)&obits
[msk
* nw
];
1091 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1092 bits
= iptr
[i
/NFDBITS
];
1093 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1095 fp
= fdp
->fd_ofiles
[fd
];
1097 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1098 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1101 if (sel_pass
== SEL_SECONDPASS
)
1102 wql_ptr
= (char *)0;
1104 wql_ptr
= (wql
+ nc
* SIZEOF_WAITQUEUE_LINK
);
1105 if (fp
->f_ops
&& (fp
->f_type
== DTYPE_SOCKET
) &&
1106 fo_select(fp
, flag
[msk
], wql_ptr
, p
)) {
1107 optr
[fd
/NFDBITS
] |= (1 << (fd
% NFDBITS
));
1114 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1123 seltrue(dev
, flag
, p
)
1133 selcount(p
, ibits
, obits
, nfd
, count
, nfcount
)
1135 u_int32_t
*ibits
, *obits
;
1140 register struct filedesc
*fdp
= p
->p_fd
;
1141 register int msk
, i
, j
, fd
;
1142 register u_int32_t bits
;
1147 static int flag
[3] = { FREAD
, FWRITE
, 0 };
1148 u_int32_t
*iptr
, *fptr
, *fbits
;
1152 * Problems when reboot; due to MacOSX signal probs
1153 * in Beaker1C ; verify that the p->p_fd is valid
1161 nw
= howmany(nfd
, NFDBITS
);
1164 for (msk
= 0; msk
< 3; msk
++) {
1165 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1166 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1167 bits
= iptr
[i
/NFDBITS
];
1168 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1170 fp
= fdp
->fd_ofiles
[fd
];
1172 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1177 if (fp
->f_type
== DTYPE_SOCKET
)
1189 * Record a select request.
1192 selrecord(selector
, sip
, p_wql
)
1193 struct proc
*selector
;
1194 struct selinfo
*sip
;
1197 thread_act_t cur_act
= current_act();
1198 struct uthread
* ut
= get_bsdthread_info(cur_act
);
1200 /* need to look at collisions */
1202 if ((p_wql
== (void *)0) && ((sip
->si_flags
& SI_INITED
) == 0)) {
1206 /*do not record if this is second pass of select */
1207 if((p_wql
== (void *)0)) {
1211 if ((sip
->si_flags
& SI_INITED
) == 0) {
1212 wait_queue_init(&sip
->wait_queue
, SYNC_POLICY_FIFO
);
1213 sip
->si_flags
|= SI_INITED
;
1214 sip
->si_flags
&= ~SI_CLEAR
;
1217 if (sip
->si_flags
& SI_RECORDED
) {
1218 sip
->si_flags
|= SI_COLL
;
1220 sip
->si_flags
&= ~SI_COLL
;
1222 sip
->si_flags
|= SI_RECORDED
;
1223 if (!wait_queue_member(&sip
->wait_queue
, ut
->uu_wqsub
))
1224 wait_queue_link_noalloc(&sip
->wait_queue
, ut
->uu_wqsub
, (wait_queue_link_t
)p_wql
);
1231 register struct selinfo
*sip
;
1234 if ((sip
->si_flags
& SI_INITED
) == 0) {
1238 if (sip
->si_flags
& SI_COLL
) {
1240 sip
->si_flags
&= ~SI_COLL
;
1242 /* will not support */
1243 //wakeup((caddr_t)&selwait);
1247 if (sip
->si_flags
& SI_RECORDED
) {
1248 wait_queue_wakeup_all(&sip
->wait_queue
, &selwait
, THREAD_AWAKENED
);
1249 sip
->si_flags
&= ~SI_RECORDED
;
1256 register struct selinfo
*sip
;
1259 if ((sip
->si_flags
& SI_INITED
) == 0) {
1262 if (sip
->si_flags
& SI_RECORDED
) {
1264 sip
->si_flags
&= ~(SI_RECORDED
| SI_COLL
);
1266 sip
->si_flags
|= SI_CLEAR
;
1267 wait_queue_unlinkall_nofree(&sip
->wait_queue
);
1271 extern struct eventqelt
*evprocdeque(struct proc
*p
, struct eventqelt
*eqp
);
1274 * called upon socket close. deque and free all events for
1278 evsofree(struct socket
*sp
)
1280 struct eventqelt
*eqp
, *next
;
1282 if (sp
== NULL
) return;
1284 for (eqp
= sp
->so_evlist
.tqh_first
; eqp
!= NULL
; eqp
= next
) {
1285 next
= eqp
->ee_slist
.tqe_next
;
1286 evprocdeque(eqp
->ee_proc
, eqp
); // remove from proc q if there
1287 TAILQ_REMOVE(&sp
->so_evlist
, eqp
, ee_slist
); // remove from socket q
1293 #define DBG_EVENT 0x10
1295 #define DBG_POST 0x10
1296 #define DBG_WATCH 0x11
1297 #define DBG_WAIT 0x12
1298 #define DBG_MOD 0x13
1299 #define DBG_EWAKEUP 0x14
1300 #define DBG_ENQUEUE 0x15
1301 #define DBG_DEQUEUE 0x16
1303 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
1304 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
1305 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
1306 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
1307 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
1308 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
1309 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
1313 * enque this event if it's not already queued. wakeup
1314 the proc if we do queue this event to it.
1317 evprocenque(struct eventqelt
*eqp
)
1322 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_START
, eqp
, eqp
->ee_flags
, eqp
->ee_eventmask
,0,0);
1323 if (eqp
->ee_flags
& EV_QUEUED
) {
1324 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1327 eqp
->ee_flags
|= EV_QUEUED
;
1328 eqp
->ee_eventmask
= 0; // disarm
1330 TAILQ_INSERT_TAIL(&p
->p_evlist
, eqp
, ee_plist
);
1331 KERNEL_DEBUG(DBG_MISC_EWAKEUP
,0,0,0,eqp
,0);
1332 wakeup(&p
->p_evlist
);
1333 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1337 * given either a sockbuf or a socket run down the
1338 * event list and queue ready events found
1341 postevent(struct socket
*sp
, struct sockbuf
*sb
, int event
)
1344 struct eventqelt
*evq
;
1345 register struct tcpcb
*tp
;
1347 if (sb
) sp
= sb
->sb_so
;
1348 if (!sp
|| sp
->so_evlist
.tqh_first
== NULL
) return;
1350 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, event
,0,0,0,0);
1352 for (evq
= sp
->so_evlist
.tqh_first
;
1353 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1357 /* ready for reading:
1358 - byte cnt >= receive low water mark
1359 - read-half of conn closed
1360 - conn pending for listening sock
1361 - socket error pending
1364 - byte cnt avail >= send low water mark
1365 - write half of conn closed
1366 - socket error pending
1367 - non-blocking conn completed successfully
1371 - sock at out of band mark
1374 switch (event
& EV_DMASK
) {
1378 case EV_RWBYTES
|EV_OOB
:
1379 if (event
& EV_OOB
) {
1380 if ((evq
->ee_eventmask
& EV_EX
)) {
1381 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
))) {
1382 mask
|= EV_EX
|EV_OOB
;
1386 if (event
& EV_RWBYTES
) {
1387 if ((evq
->ee_eventmask
& EV_RE
) && soreadable(sp
)) {
1388 if ((sp
->so_type
== SOCK_STREAM
) && (sp
->so_error
== ECONNREFUSED
) ||
1389 (sp
->so_error
== ECONNRESET
)) {
1390 if ((sp
->so_pcb
== 0) ||
1391 !(tp
= sototcpcb(sp
)) ||
1392 (tp
->t_state
== TCPS_CLOSED
)) {
1393 mask
|= EV_RE
|EV_RESET
;
1397 if (sp
->so_state
& SS_CANTRCVMORE
) {
1398 mask
|= EV_RE
|EV_FIN
;
1399 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
1403 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
1406 if ((evq
->ee_eventmask
& EV_WR
) && sowriteable(sp
)) {
1407 if ((sp
->so_type
== SOCK_STREAM
) &&(sp
->so_error
== ECONNREFUSED
) ||
1408 (sp
->so_error
== ECONNRESET
)) {
1409 if ((sp
->so_pcb
== 0) ||
1410 !(tp
= sototcpcb(sp
)) ||
1411 (tp
->t_state
== TCPS_CLOSED
)) {
1412 mask
|= EV_WR
|EV_RESET
;
1417 evq
->ee_req
.er_wcnt
= sbspace(&sp
->so_snd
);
1423 if ((evq
->ee_eventmask
& EV_RE
)) {
1424 evq
->ee_req
.er_rcnt
= sp
->so_qlen
+ 1; // incl this one
1425 mask
|= EV_RE
|EV_RCONN
;
1430 if ((evq
->ee_eventmask
& EV_WR
)) {
1431 mask
|= EV_WR
|EV_WCONN
;
1436 if ((evq
->ee_eventmask
& EV_RE
)) {
1437 mask
|= EV_RE
|EV_RCLOSED
;
1442 if ((evq
->ee_eventmask
& EV_WR
)) {
1443 mask
|= EV_WR
|EV_WCLOSED
;
1448 if (evq
->ee_eventmask
& EV_RE
) {
1449 mask
|= EV_RE
|EV_FIN
;
1455 if (evq
->ee_eventmask
& EV_RE
) {
1456 mask
|= EV_RE
| event
;
1458 if (evq
->ee_eventmask
& EV_WR
) {
1459 mask
|= EV_WR
| event
;
1468 evq
->ee_req
.er_eventbits
|= mask
;
1469 KERNEL_DEBUG(DBG_MISC_POST
, evq
, evq
->ee_req
.er_eventbits
, mask
,0,0);
1473 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, 0,0,0,0,0);
1477 * remove and return the first event (eqp=NULL) or a specific
1478 * event, or return NULL if no events found
1481 evprocdeque(struct proc
*p
, struct eventqelt
*eqp
)
1484 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_START
,p
,eqp
,0,0,0);
1486 if (eqp
&& ((eqp
->ee_flags
& EV_QUEUED
) == NULL
)) {
1487 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_END
,0,0,0,0,0);
1490 if (p
->p_evlist
.tqh_first
== NULL
) {
1491 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_END
,0,0,0,0,0);
1494 if (eqp
== NULL
) { // remove first
1495 eqp
= p
->p_evlist
.tqh_first
;
1497 TAILQ_REMOVE(&p
->p_evlist
, eqp
, ee_plist
);
1498 eqp
->ee_flags
&= ~EV_QUEUED
;
1499 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_END
,eqp
,0,0,0,0);
1503 struct evwatch_args
{
1504 struct eventreq
*u_req
;
1510 * watchevent system call. user passes us an event to watch
1511 * for. we malloc an event object, initialize it, and queue
1512 * it to the open socket. when the event occurs, postevent()
1513 * will enque it back to our proc where we can retrieve it
1516 * should this prevent duplicate events on same socket?
1519 watchevent(p
, uap
, retval
)
1521 struct evwatch_args
*uap
;
1524 struct eventqelt
*eqp
= (struct eventqelt
*)0;
1525 struct eventqelt
*np
;
1526 struct eventreq
*erp
;
1531 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_START
, 0,0,0,0,0);
1533 // get a qelt and fill with users req
1534 MALLOC(eqp
, struct eventqelt
*, sizeof(struct eventqelt
), M_TEMP
, M_WAITOK
);
1535 if (!eqp
) panic("can't MALLOC eqp");
1537 // get users request pkt
1538 if (error
= copyin((caddr_t
)uap
->u_req
, (caddr_t
)erp
,
1539 sizeof(struct eventreq
))) {
1541 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
1544 KERNEL_DEBUG(DBG_MISC_WATCH
, erp
->er_handle
,uap
->u_eventmask
,eqp
,0,0);
1545 // validate, freeing qelt if errors
1547 if (erp
->er_type
!= EV_FD
) {
1549 } else if (erp
->er_handle
< 0) {
1551 } else if (erp
->er_handle
> p
->p_fd
->fd_nfiles
) {
1553 } else if ((fp
= *fdfile(p
, erp
->er_handle
)) == NULL
) {
1555 } else if (fp
->f_type
!= DTYPE_SOCKET
) {
1560 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
1564 erp
->er_rcnt
= erp
->er_wcnt
= erp
->er_eventbits
= 0;
1566 eqp
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
1569 sp
= (struct socket
*)fp
->f_data
;
1572 // only allow one watch per file per proc
1573 for (np
= sp
->so_evlist
.tqh_first
; np
!= NULL
; np
= np
->ee_slist
.tqe_next
) {
1574 if (np
->ee_proc
== p
) {
1576 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
1581 TAILQ_INSERT_TAIL(&sp
->so_evlist
, eqp
, ee_slist
);
1582 postevent(sp
, 0, EV_RWBYTES
); // catch existing events
1583 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, 0,0,0,0,0);
1587 struct evwait_args
{
1588 struct eventreq
*u_req
;
1593 * waitevent system call.
1594 * grabs the next waiting event for this proc and returns
1595 * it. if no events, user can request to sleep with timeout
1596 * or poll mode (tv=NULL);
1599 waitevent(p
, uap
, retval
)
1601 struct evwait_args
*uap
;
1605 struct eventqelt
*eqp
;
1606 uint64_t abstime
, interval
;
1611 error
= copyin((caddr_t
)uap
->tv
, (caddr_t
)&atv
, sizeof (atv
));
1614 if (itimerfix(&atv
)) {
1619 interval
= tvtoabstime(&atv
);
1622 abstime
= interval
= 0;
1624 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_START
, 0,0,0,0,0);
1627 if ((eqp
= evprocdeque(p
,NULL
)) != NULL
) {
1628 error
= copyout((caddr_t
)&eqp
->ee_req
,
1629 (caddr_t
)uap
->u_req
, sizeof(struct eventreq
));
1630 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,
1631 eqp
->ee_req
.er_handle
,eqp
->ee_req
.er_eventbits
,eqp
,0);
1636 if (uap
->tv
&& interval
== 0) {
1637 *retval
= 1; // poll failed
1638 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,0,0,0,0);
1644 clock_absolutetime_interval_to_deadline(interval
, &abstime
)
1646 KERNEL_DEBUG(DBG_MISC_WAIT
, 1,&p
->p_evlist
,0,0,0);
1647 error
= tsleep1(&p
->p_evlist
, PSOCK
| PCATCH
,
1648 "waitevent", abstime
, (int (*)(int))0);
1649 KERNEL_DEBUG(DBG_MISC_WAIT
, 2,&p
->p_evlist
,0,0,0);
1652 if (error
== ERESTART
)
1654 if (error
== EWOULDBLOCK
) {
1660 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, 0,0,0,0,0);
1665 struct modwatch_args
{
1666 struct eventreq
*u_req
;
1671 * modwatch system call. user passes in event to modify.
1672 * if we find it we reset the event bits and que/deque event
1676 modwatch(p
, uap
, retval
)
1678 struct modwatch_args
*uap
;
1682 struct eventreq
*erp
= &er
;
1683 struct eventqelt
*evq
;
1689 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_START
, 0,0,0,0,0);
1691 // get users request pkt
1692 if (error
= copyin((caddr_t
)uap
->u_req
, (caddr_t
)erp
,
1693 sizeof(struct eventreq
))) return(error
);
1695 if (erp
->er_type
!= EV_FD
) return(EINVAL
);
1696 if (erp
->er_handle
< 0) return(EBADF
);
1697 if (erp
->er_handle
> p
->p_fd
->fd_nfiles
) return(EBADF
);
1698 if ((fp
= *fdfile(p
, erp
->er_handle
)) == NULL
)
1700 if (fp
->f_type
!= DTYPE_SOCKET
) return(EINVAL
); // for now must be sock
1701 sp
= (struct socket
*)fp
->f_data
;
1705 // locate event if possible
1706 for (evq
= sp
->so_evlist
.tqh_first
;
1707 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1708 if (evq
->ee_proc
== p
) break;
1712 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
1715 KERNEL_DEBUG(DBG_MISC_MOD
, erp
->er_handle
,uap
->u_eventmask
,evq
,0,0);
1717 if (uap
->u_eventmask
== EV_RM
) {
1718 evprocdeque(p
, evq
);
1719 TAILQ_REMOVE(&sp
->so_evlist
, evq
, ee_slist
);
1721 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, 0,0,0,0,0);
1725 switch (uap
->u_eventmask
& EV_MASK
) {
1743 case EV_EX
|EV_RE
|EV_WR
:
1744 flag
= EV_OOB
|EV_RWBYTES
;
1751 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
1752 evprocdeque(p
, evq
);
1753 evq
->ee_req
.er_eventbits
= 0;
1754 postevent(sp
, 0, flag
);
1755 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, evq
->ee_req
.er_handle
,evq
->ee_eventmask
,sp
,flag
,0);