2 * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */
27 * Copyright (c) 1982, 1986, 1989, 1993
28 * The Regents of the University of California. All rights reserved.
29 * (c) UNIX System Laboratories, Inc.
30 * All or some portions of this file are derived from material licensed
31 * to the University of California by American Telephone and Telegraph
32 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
33 * the permission of UNIX System Laboratories, Inc.
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * @(#)sys_generic.c 8.9 (Berkeley) 2/14/95
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/filedesc.h>
69 #include <sys/ioctl.h>
72 #include <sys/socketvar.h>
74 #include <sys/kernel.h>
76 #include <sys/malloc.h>
78 #include <sys/mount.h>
79 #include <sys/protosw.h>
82 #include <sys/kdebug.h>
83 #include <kern/assert.h>
84 #include <kern/thread_act.h>
87 #include <sys/socket.h>
88 #include <sys/socketvar.h>
89 #include <sys/errno.h>
92 #include <net/route.h>
94 #include <netinet/in.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/ip.h>
97 #include <netinet/in_pcb.h>
98 #include <netinet/ip_var.h>
99 #include <netinet/ip6.h>
100 #include <netinet/tcp.h>
101 #include <netinet/tcp_fsm.h>
102 #include <netinet/tcp_seq.h>
103 #include <netinet/tcp_timer.h>
104 #include <netinet/tcp_var.h>
105 #include <netinet/tcpip.h>
106 #include <netinet/tcp_debug.h>
107 /* for wait queue based select */
108 #include <kern/wait_queue.h>
110 #include <sys/ktrace.h>
113 static int dofileread
__P((struct proc
*, struct file
*, int, void *,
114 size_t, off_t
, int, int*));
115 static int dofilewrite
__P((struct proc
*, struct file
*, int,
116 const void *, size_t, off_t
, int, int*));
119 holdfp(fdp
, fd
, flag
)
120 struct filedesc
* fdp
;
125 if (((u_int
)fd
) >= fdp
->fd_nfiles
||
126 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
127 (fp
->f_flag
& flag
) == 0) {
138 #ifndef _SYS_SYSPROTO_H_
148 register struct read_args
*uap
;
151 register struct file
*fp
;
154 if ((fp
= holdfp(p
->p_fd
, uap
->fd
, FREAD
)) == NULL
)
156 error
= dofileread(p
, fp
, uap
->fd
, uap
->cbuf
, uap
->nbyte
,
157 (off_t
)-1, 0, retval
);
165 #ifndef _SYS_SYSPROTO_H_
170 #ifdef DOUBLE_ALIGN_PARAMS
177 pread(p
, uap
, retval
)
179 register struct pread_args
*uap
;
182 register struct file
*fp
;
185 if ((fp
= holdfp(p
->p_fd
, uap
->fd
, FREAD
)) == NULL
)
187 if (fp
->f_type
!= DTYPE_VNODE
) {
190 error
= dofileread(p
, fp
, uap
->fd
, uap
->buf
, uap
->nbyte
,
191 uap
->offset
, FOF_OFFSET
, retval
);
198 * Code common for read and pread
201 dofileread(p
, fp
, fd
, buf
, nbyte
, offset
, flags
, retval
)
219 aiov
.iov_base
= (caddr_t
)buf
;
220 aiov
.iov_len
= nbyte
;
221 auio
.uio_iov
= &aiov
;
223 auio
.uio_offset
= offset
;
226 auio
.uio_resid
= nbyte
;
227 auio
.uio_rw
= UIO_READ
;
228 auio
.uio_segflg
= UIO_USERSPACE
;
232 * if tracing, save a copy of iovec
234 if (KTRPOINT(p
, KTR_GENIO
)) {
242 if ((error
= fo_read(fp
, &auio
, fp
->f_cred
, flags
, p
))) {
243 if (auio
.uio_resid
!= cnt
&& (error
== ERESTART
||
244 error
== EINTR
|| error
== EWOULDBLOCK
))
247 cnt
-= auio
.uio_resid
;
249 if (didktr
&& error
== 0) {
250 ktruio
.uio_iov
= &ktriov
;
251 ktruio
.uio_resid
= cnt
;
252 ktrgenio(p
->p_tracep
, fd
, UIO_READ
, &ktruio
, error
,
261 * Scatter read system call.
263 #ifndef _SYS_SYSPROTO_H_
271 readv(p
, uap
, retval
)
273 register struct readv_args
*uap
;
277 register struct iovec
*iov
;
279 struct iovec aiov
[UIO_SMALLIOV
];
281 if (uap
->iovcnt
> UIO_SMALLIOV
) {
282 if (uap
->iovcnt
> UIO_MAXIOV
)
284 if ((iov
= (struct iovec
*)
285 kalloc(sizeof(struct iovec
) * (uap
->iovcnt
))) == 0)
290 auio
.uio_iovcnt
= uap
->iovcnt
;
291 auio
.uio_rw
= UIO_READ
;
292 error
= copyin((caddr_t
)uap
->iovp
, (caddr_t
)iov
,
293 uap
->iovcnt
* sizeof (struct iovec
));
295 error
= rwuio(p
, uap
->fd
, &auio
, UIO_READ
, retval
);
296 if (uap
->iovcnt
> UIO_SMALLIOV
)
297 kfree(iov
, sizeof(struct iovec
)*uap
->iovcnt
);
304 #ifndef _SYS_SYSPROTO_H_
312 write(p
, uap
, retval
)
314 register struct write_args
*uap
;
317 register struct file
*fp
;
320 if ((fp
= holdfp(p
->p_fd
, uap
->fd
, FWRITE
)) == NULL
)
322 error
= dofilewrite(p
, fp
, uap
->fd
, uap
->cbuf
, uap
->nbyte
,
323 (off_t
)-1, 0, retval
);
331 #ifndef _SYS_SYSPROTO_H_
336 #ifdef DOUBLE_ALIGN_PARAMS
343 pwrite(p
, uap
, retval
)
345 register struct pwrite_args
*uap
;
348 register struct file
*fp
;
351 if ((fp
= holdfp(p
->p_fd
, uap
->fd
, FWRITE
)) == NULL
)
353 if (fp
->f_type
!= DTYPE_VNODE
) {
356 error
= dofilewrite(p
, fp
, uap
->fd
, uap
->buf
, uap
->nbyte
,
357 uap
->offset
, FOF_OFFSET
, retval
);
364 dofilewrite(p
, fp
, fd
, buf
, nbyte
, offset
, flags
, retval
)
382 aiov
.iov_base
= (void *)(uintptr_t)buf
;
383 aiov
.iov_len
= nbyte
;
384 auio
.uio_iov
= &aiov
;
386 auio
.uio_offset
= offset
;
389 auio
.uio_resid
= nbyte
;
390 auio
.uio_rw
= UIO_WRITE
;
391 auio
.uio_segflg
= UIO_USERSPACE
;
395 * if tracing, save a copy of iovec and uio
397 if (KTRPOINT(p
, KTR_GENIO
)) {
404 if (fp
->f_type
== DTYPE_VNODE
)
406 if ((error
= fo_write(fp
, &auio
, fp
->f_cred
, flags
, p
))) {
407 if (auio
.uio_resid
!= cnt
&& (error
== ERESTART
||
408 error
== EINTR
|| error
== EWOULDBLOCK
))
413 cnt
-= auio
.uio_resid
;
415 if (didktr
&& error
== 0) {
416 ktruio
.uio_iov
= &ktriov
;
417 ktruio
.uio_resid
= cnt
;
418 ktrgenio(p
->p_tracep
, fd
, UIO_WRITE
, &ktruio
, error
,
427 * Gather write system call
429 #ifndef _SYS_SYSPROTO_H_
437 writev(p
, uap
, retval
)
439 register struct writev_args
*uap
;
443 register struct iovec
*iov
;
445 struct iovec aiov
[UIO_SMALLIOV
];
447 if (uap
->iovcnt
> UIO_SMALLIOV
) {
448 if (uap
->iovcnt
> UIO_MAXIOV
)
450 if ((iov
= (struct iovec
*)
451 kalloc(sizeof(struct iovec
) * (uap
->iovcnt
))) == 0)
456 auio
.uio_iovcnt
= uap
->iovcnt
;
457 auio
.uio_rw
= UIO_WRITE
;
458 error
= copyin((caddr_t
)uap
->iovp
, (caddr_t
)iov
,
459 uap
->iovcnt
* sizeof (struct iovec
));
461 error
= rwuio(p
, uap
->fd
, &auio
, UIO_WRITE
, retval
);
462 if (uap
->iovcnt
> UIO_SMALLIOV
)
463 kfree(iov
, sizeof(struct iovec
)*uap
->iovcnt
);
468 rwuio(p
, fdes
, uio
, rw
, retval
)
471 register struct uio
*uio
;
476 register struct iovec
*iov
;
477 int i
, count
, flag
, error
;
479 struct iovec
*ktriov
;
485 if (error
= fdgetf(p
, fdes
, &fp
))
488 if ((fp
->f_flag
&(rw
==UIO_READ
? FREAD
: FWRITE
)) == 0) {
492 uio
->uio_segflg
= UIO_USERSPACE
;
495 for (i
= 0; i
< uio
->uio_iovcnt
; i
++) {
496 if (iov
->iov_len
< 0) {
499 uio
->uio_resid
+= iov
->iov_len
;
500 if (uio
->uio_resid
< 0) {
505 count
= uio
->uio_resid
;
508 * if tracing, save a copy of iovec
510 if (KTRPOINT(p
, KTR_GENIO
)) {
511 iovlen
= uio
->uio_iovcnt
* sizeof (struct iovec
);
512 MALLOC(ktriov
, struct iovec
*, iovlen
, M_TEMP
, M_WAITOK
);
513 bcopy((caddr_t
)uio
->uio_iov
, (caddr_t
)ktriov
, iovlen
);
519 if (rw
== UIO_READ
) {
520 if (error
= fo_read(fp
, uio
, fp
->f_cred
, 0, p
))
521 if (uio
->uio_resid
!= count
&& (error
== ERESTART
||
522 error
== EINTR
|| error
== EWOULDBLOCK
))
525 if (fp
->f_type
== DTYPE_VNODE
)
527 if (error
= fo_write(fp
, uio
, fp
->f_cred
, 0, p
)) {
528 if (uio
->uio_resid
!= count
&& (error
== ERESTART
||
529 error
== EINTR
|| error
== EWOULDBLOCK
))
531 /* The socket layer handles SIGPIPE */
532 if (error
== EPIPE
&& fp
->f_type
!= DTYPE_SOCKET
)
537 *retval
= count
- uio
->uio_resid
;
542 ktruio
.uio_iov
= ktriov
;
543 ktruio
.uio_resid
= *retval
;
544 ktrgenio(p
->p_tracep
, fdes
, rw
, &ktruio
, error
,
547 FREE(ktriov
, M_TEMP
);
557 #ifndef _SYS_SYSPROTO_H_
565 ioctl(p
, uap
, retval
)
567 register struct ioctl_args
*uap
;
576 #define STK_PARAMS 128
577 char stkbuf
[STK_PARAMS
];
579 if (error
= fdgetf(p
, uap
->fd
, &fp
))
582 if ((fp
->f_flag
& (FREAD
| FWRITE
)) == 0)
587 * ### LD 6/11/97 Hack Alert: this is to get AppleTalk to work
588 * while implementing an ATioctl system call
591 extern int appletalk_inited
;
593 if (appletalk_inited
&& ((uap
->com
& 0x0000FFFF) == 0xff99)) {
594 #ifdef APPLETALK_DEBUG
595 kprintf("ioctl: special AppleTalk \n");
597 error
= fo_ioctl(fp
, uap
->com
, uap
->data
, p
);
605 switch (com
= uap
->com
) {
607 *fdflags(p
, uap
->fd
) &= ~UF_EXCLOSE
;
610 *fdflags(p
, uap
->fd
) |= UF_EXCLOSE
;
615 * Interpret high order word to find amount of data to be
616 * copied to/from the user's address space.
618 size
= IOCPARM_LEN(com
);
619 if (size
> IOCPARM_MAX
)
622 if (size
> sizeof (stkbuf
)) {
623 if ((memp
= (caddr_t
)kalloc(size
)) == 0)
630 error
= copyin(uap
->data
, data
, (u_int
)size
);
637 *(caddr_t
*)data
= uap
->data
;
638 } else if ((com
&IOC_OUT
) && size
)
640 * Zero the buffer so the user always
641 * gets back something deterministic.
644 else if (com
&IOC_VOID
)
645 *(caddr_t
*)data
= uap
->data
;
650 if (tmp
= *(int *)data
)
651 fp
->f_flag
|= FNONBLOCK
;
653 fp
->f_flag
&= ~FNONBLOCK
;
654 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
658 if (tmp
= *(int *)data
)
659 fp
->f_flag
|= FASYNC
;
661 fp
->f_flag
&= ~FASYNC
;
662 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, p
);
667 if (fp
->f_type
== DTYPE_SOCKET
) {
668 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
675 struct proc
*p1
= pfind(tmp
);
680 tmp
= p1
->p_pgrp
->pg_id
;
682 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, p
);
686 if (fp
->f_type
== DTYPE_SOCKET
) {
688 *(int *)data
= ((struct socket
*)fp
->f_data
)->so_pgid
;
691 error
= fo_ioctl(fp
, TIOCGPGRP
, data
, p
);
692 *(int *)data
= -*(int *)data
;
696 error
= fo_ioctl(fp
, com
, data
, p
);
698 * Copy any data to user, size was
699 * already set and checked above.
701 if (error
== 0 && (com
&IOC_OUT
) && size
)
702 error
= copyout(data
, uap
->data
, (u_int
)size
);
710 int selwait
, nselcoll
;
711 #define SEL_FIRSTPASS 1
712 #define SEL_SECONDPASS 2
713 extern int selcontinue(int error
);
714 extern int selprocess(int error
, int sel_pass
);
715 static int selscan(struct proc
*p
, struct _select
* sel
,
716 int nfd
, register_t
*retval
, int sel_pass
);
717 static int selcount(struct proc
*p
, u_int32_t
*ibits
, u_int32_t
*obits
,
718 int nfd
, int * count
, int * nfcount
);
719 extern uint64_t tvtoabstime(struct timeval
*tvp
);
722 * Select system call.
724 #ifndef _SYS_SYSPROTO_H_
734 select(p
, uap
, retval
)
735 register struct proc
*p
;
736 register struct select_args
*uap
;
744 int needzerofill
= 1;
749 th_act
= current_act();
750 uth
= get_bsdthread_info(th_act
);
751 sel
= &uth
->uu_state
.ss_select
;
752 retval
= (int *)get_bsduthreadrval(th_act
);
759 if (uap
->nd
> p
->p_fd
->fd_nfiles
)
760 uap
->nd
= p
->p_fd
->fd_nfiles
; /* forgiving; slightly wrong */
762 nw
= howmany(uap
->nd
, NFDBITS
);
763 ni
= nw
* sizeof(fd_mask
);
766 * if this is the first select by the thread
767 * allocate the space for bits.
769 if (sel
->nbytes
== 0) {
770 sel
->nbytes
= 3 * ni
;
771 MALLOC(sel
->ibits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
);
772 MALLOC(sel
->obits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
);
773 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
774 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
779 * if the previously allocated space for the bits
780 * is smaller than what is requested. Reallocate.
782 if (sel
->nbytes
< (3 * ni
)) {
783 sel
->nbytes
= (3 * ni
);
784 FREE(sel
->ibits
, M_TEMP
);
785 FREE(sel
->obits
, M_TEMP
);
786 MALLOC(sel
->ibits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
);
787 MALLOC(sel
->obits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
);
788 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
789 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
794 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
795 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
799 * get the bits from the user address space
801 #define getbits(name, x) \
803 if (uap->name && (error = copyin((caddr_t)uap->name, \
804 (caddr_t)&sel->ibits[(x) * nw], ni))) \
816 error
= copyin((caddr_t
)uap
->tv
, (caddr_t
)&atv
, sizeof (atv
));
819 if (itimerfix(&atv
)) {
824 clock_absolutetime_interval_to_deadline(
825 tvtoabstime(&atv
), &sel
->abstime
);
831 if (error
= selcount(p
, sel
->ibits
, sel
->obits
, uap
->nd
, &count
, &nfcount
)) {
835 sel
->nfcount
= nfcount
;
837 size
= SIZEOF_WAITQUEUE_SUB
+ (count
* SIZEOF_WAITQUEUE_LINK
);
838 if (sel
->allocsize
) {
839 if (uth
->uu_wqsub
== 0)
840 panic("select: wql memory smashed");
841 /* needed for the select now */
842 if (size
> sel
->allocsize
) {
843 kfree(uth
->uu_wqsub
, sel
->allocsize
);
844 sel
->allocsize
= size
;
845 uth
->uu_wqsub
= (wait_queue_sub_t
)kalloc(sel
->allocsize
);
846 if (uth
->uu_wqsub
== (wait_queue_sub_t
)NULL
)
847 panic("failed to allocate memory for waitqueue\n");
848 sel
->wql
= (char *)uth
->uu_wqsub
+ SIZEOF_WAITQUEUE_SUB
;
852 sel
->allocsize
= size
;
853 uth
->uu_wqsub
= (wait_queue_sub_t
)kalloc(sel
->allocsize
);
854 if (uth
->uu_wqsub
== (wait_queue_sub_t
)NULL
)
855 panic("failed to allocate memory for waitqueue\n");
856 sel
->wql
= (char *)uth
->uu_wqsub
+ SIZEOF_WAITQUEUE_SUB
;
858 bzero(uth
->uu_wqsub
, size
);
859 wait_queue_sub_init(uth
->uu_wqsub
, (SYNC_POLICY_FIFO
| SYNC_POLICY_PREPOST
));
862 return selprocess(error
, SEL_FIRSTPASS
);
866 selcontinue(int error
)
868 return selprocess(error
, SEL_SECONDPASS
);
872 selprocess(error
, sel_pass
)
879 struct select_args
*uap
;
886 wait_result_t wait_result
;
889 th_act
= current_act();
890 uap
= (struct select_args
*)get_bsduthreadarg(th_act
);
891 retval
= (int *)get_bsduthreadrval(th_act
);
892 uth
= get_bsdthread_info(th_act
);
893 sel
= &uth
->uu_state
.ss_select
;
895 /* if it is first pass wait queue is not setup yet */
896 if ((error
!= 0) && (sel_pass
== SEL_FIRSTPASS
))
906 p
->p_flag
|= P_SELECT
;
907 /* skip scans if the select is just for timeouts */
909 if (sel_pass
== SEL_FIRSTPASS
)
910 wait_queue_sub_clearrefs(uth
->uu_wqsub
);
912 error
= selscan(p
, sel
, uap
->nd
, retval
, sel_pass
);
913 if (error
|| *retval
) {
917 /* if the select of log, then we canwakeup and discover some one
918 * else already read the data; go toselct again if time permits
932 clock_get_uptime(&now
);
933 if (now
>= sel
->abstime
)
938 /* cleanup obits and try again */
940 sel_pass
= SEL_FIRSTPASS
;
945 * To effect a poll, the timeout argument should be
946 * non-nil, pointing to a zero-valued timeval structure.
948 if (uap
->tv
&& sel
->abstime
== 0) {
952 /* No spurious wakeups due to colls,no need to check for them */
953 if ((sel_pass
== SEL_SECONDPASS
) || ((p
->p_flag
& P_SELECT
) == 0)) {
954 sel_pass
= SEL_FIRSTPASS
;
958 p
->p_flag
&= ~P_SELECT
;
960 /* if the select is just for timeout skip check */
961 if (sel
->count
&&(sel_pass
== SEL_SECONDPASS
))
962 panic("selprocess: 2nd pass assertwaiting");
964 /* Wait Queue Subordinate has waitqueue as first element */
965 wait_result
= wait_queue_assert_wait((wait_queue_t
)uth
->uu_wqsub
,
966 &selwait
, THREAD_ABORTSAFE
);
967 if (wait_result
!= THREAD_AWAKENED
) {
968 /* there are no preposted events */
969 error
= tsleep1(NULL
, PSOCK
| PCATCH
,
970 "select", sel
->abstime
, selcontinue
);
976 sel_pass
= SEL_SECONDPASS
;
984 wait_subqueue_unlink_all(uth
->uu_wqsub
);
985 p
->p_flag
&= ~P_SELECT
;
986 /* select is not restarted after signals... */
987 if (error
== ERESTART
)
989 if (error
== EWOULDBLOCK
)
991 nw
= howmany(uap
->nd
, NFDBITS
);
992 ni
= nw
* sizeof(fd_mask
);
994 #define putbits(name, x) \
996 if (uap->name && (error2 = copyout((caddr_t)&sel->obits[(x) * nw], \
997 (caddr_t)uap->name, ni))) \
1013 selscan(p
, sel
, nfd
, retval
, sel_pass
)
1015 struct _select
*sel
;
1020 register struct filedesc
*fdp
= p
->p_fd
;
1021 register int msk
, i
, j
, fd
;
1022 register u_int32_t bits
;
1026 static int flag
[3] = { FREAD
, FWRITE
, 0 };
1027 u_int32_t
*iptr
, *optr
;
1029 u_int32_t
*ibits
, *obits
;
1036 * Problems when reboot; due to MacOSX signal probs
1037 * in Beaker1C ; verify that the p->p_fd is valid
1049 nfcount
= sel
->nfcount
;
1051 if (nfcount
> count
)
1052 panic("selcount count<nfcount");
1054 nw
= howmany(nfd
, NFDBITS
);
1057 if ( nfcount
< count
) {
1058 /* some or all in kernel funnel */
1059 for (msk
= 0; msk
< 3; msk
++) {
1060 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1061 optr
= (u_int32_t
*)&obits
[msk
* nw
];
1062 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1063 bits
= iptr
[i
/NFDBITS
];
1064 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1066 fp
= fdp
->fd_ofiles
[fd
];
1068 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1071 if (sel_pass
== SEL_SECONDPASS
)
1072 wql_ptr
= (char *)0;
1074 wql_ptr
= (wql
+ nc
* SIZEOF_WAITQUEUE_LINK
);
1075 if (fp
->f_ops
&& (fp
->f_type
!= DTYPE_SOCKET
)
1076 && fo_select(fp
, flag
[msk
], wql_ptr
, p
)) {
1077 optr
[fd
/NFDBITS
] |= (1 << (fd
% NFDBITS
));
1087 /* socket file descriptors for scan */
1088 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
1091 for (msk
= 0; msk
< 3; msk
++) {
1092 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1093 optr
= (u_int32_t
*)&obits
[msk
* nw
];
1094 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1095 bits
= iptr
[i
/NFDBITS
];
1096 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1098 fp
= fdp
->fd_ofiles
[fd
];
1100 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1101 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1104 if (sel_pass
== SEL_SECONDPASS
)
1105 wql_ptr
= (char *)0;
1107 wql_ptr
= (wql
+ nc
* SIZEOF_WAITQUEUE_LINK
);
1108 if (fp
->f_ops
&& (fp
->f_type
== DTYPE_SOCKET
) &&
1109 fo_select(fp
, flag
[msk
], wql_ptr
, p
)) {
1110 optr
[fd
/NFDBITS
] |= (1 << (fd
% NFDBITS
));
1117 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
1126 seltrue(dev
, flag
, p
)
1136 selcount(p
, ibits
, obits
, nfd
, count
, nfcount
)
1138 u_int32_t
*ibits
, *obits
;
1143 register struct filedesc
*fdp
= p
->p_fd
;
1144 register int msk
, i
, j
, fd
;
1145 register u_int32_t bits
;
1150 static int flag
[3] = { FREAD
, FWRITE
, 0 };
1151 u_int32_t
*iptr
, *fptr
, *fbits
;
1155 * Problems when reboot; due to MacOSX signal probs
1156 * in Beaker1C ; verify that the p->p_fd is valid
1164 nw
= howmany(nfd
, NFDBITS
);
1167 for (msk
= 0; msk
< 3; msk
++) {
1168 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
1169 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
1170 bits
= iptr
[i
/NFDBITS
];
1171 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
1173 fp
= fdp
->fd_ofiles
[fd
];
1175 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1180 if (fp
->f_type
== DTYPE_SOCKET
)
1192 * Record a select request.
1195 selrecord(selector
, sip
, p_wql
)
1196 struct proc
*selector
;
1197 struct selinfo
*sip
;
1200 thread_act_t cur_act
= current_act();
1201 struct uthread
* ut
= get_bsdthread_info(cur_act
);
1203 /* need to look at collisions */
1205 if ((p_wql
== (void *)0) && ((sip
->si_flags
& SI_INITED
) == 0)) {
1209 /*do not record if this is second pass of select */
1210 if((p_wql
== (void *)0)) {
1214 if ((sip
->si_flags
& SI_INITED
) == 0) {
1215 wait_queue_init(&sip
->wait_queue
, SYNC_POLICY_FIFO
);
1216 sip
->si_flags
|= SI_INITED
;
1217 sip
->si_flags
&= ~SI_CLEAR
;
1220 if (sip
->si_flags
& SI_RECORDED
) {
1221 sip
->si_flags
|= SI_COLL
;
1223 sip
->si_flags
&= ~SI_COLL
;
1225 sip
->si_flags
|= SI_RECORDED
;
1226 if (!wait_queue_member(&sip
->wait_queue
, ut
->uu_wqsub
))
1227 wait_queue_link_noalloc(&sip
->wait_queue
, ut
->uu_wqsub
, (wait_queue_link_t
)p_wql
);
1234 register struct selinfo
*sip
;
1237 if ((sip
->si_flags
& SI_INITED
) == 0) {
1241 if (sip
->si_flags
& SI_COLL
) {
1243 sip
->si_flags
&= ~SI_COLL
;
1245 /* will not support */
1246 //wakeup((caddr_t)&selwait);
1250 if (sip
->si_flags
& SI_RECORDED
) {
1251 wait_queue_wakeup_all(&sip
->wait_queue
, &selwait
, THREAD_AWAKENED
);
1252 sip
->si_flags
&= ~SI_RECORDED
;
1259 register struct selinfo
*sip
;
1262 if ((sip
->si_flags
& SI_INITED
) == 0) {
1265 if (sip
->si_flags
& SI_RECORDED
) {
1267 sip
->si_flags
&= ~(SI_RECORDED
| SI_COLL
);
1269 sip
->si_flags
|= SI_CLEAR
;
1270 wait_queue_unlinkall_nofree(&sip
->wait_queue
);
1274 extern struct eventqelt
*evprocdeque(struct proc
*p
, struct eventqelt
*eqp
);
1277 * called upon socket close. deque and free all events for
1281 evsofree(struct socket
*sp
)
1283 struct eventqelt
*eqp
, *next
;
1285 if (sp
== NULL
) return;
1287 for (eqp
= sp
->so_evlist
.tqh_first
; eqp
!= NULL
; eqp
= next
) {
1288 next
= eqp
->ee_slist
.tqe_next
;
1289 evprocdeque(eqp
->ee_proc
, eqp
); // remove from proc q if there
1290 TAILQ_REMOVE(&sp
->so_evlist
, eqp
, ee_slist
); // remove from socket q
1296 #define DBG_EVENT 0x10
1298 #define DBG_POST 0x10
1299 #define DBG_WATCH 0x11
1300 #define DBG_WAIT 0x12
1301 #define DBG_MOD 0x13
1302 #define DBG_EWAKEUP 0x14
1303 #define DBG_ENQUEUE 0x15
1304 #define DBG_DEQUEUE 0x16
1306 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
1307 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
1308 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
1309 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
1310 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
1311 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
1312 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
1316 * enque this event if it's not already queued. wakeup
1317 the proc if we do queue this event to it.
1320 evprocenque(struct eventqelt
*eqp
)
1325 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_START
, eqp
, eqp
->ee_flags
, eqp
->ee_eventmask
,0,0);
1326 if (eqp
->ee_flags
& EV_QUEUED
) {
1327 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1330 eqp
->ee_flags
|= EV_QUEUED
;
1331 eqp
->ee_eventmask
= 0; // disarm
1333 TAILQ_INSERT_TAIL(&p
->p_evlist
, eqp
, ee_plist
);
1334 KERNEL_DEBUG(DBG_MISC_EWAKEUP
,0,0,0,eqp
,0);
1335 wakeup(&p
->p_evlist
);
1336 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
1340 * given either a sockbuf or a socket run down the
1341 * event list and queue ready events found
1344 postevent(struct socket
*sp
, struct sockbuf
*sb
, int event
)
1347 struct eventqelt
*evq
;
1348 register struct tcpcb
*tp
;
1350 if (sb
) sp
= sb
->sb_so
;
1351 if (!sp
|| sp
->so_evlist
.tqh_first
== NULL
) return;
1353 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, event
,0,0,0,0);
1355 for (evq
= sp
->so_evlist
.tqh_first
;
1356 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1360 /* ready for reading:
1361 - byte cnt >= receive low water mark
1362 - read-half of conn closed
1363 - conn pending for listening sock
1364 - socket error pending
1367 - byte cnt avail >= send low water mark
1368 - write half of conn closed
1369 - socket error pending
1370 - non-blocking conn completed successfully
1374 - sock at out of band mark
1377 switch (event
& EV_DMASK
) {
1381 case EV_RWBYTES
|EV_OOB
:
1382 if (event
& EV_OOB
) {
1383 if ((evq
->ee_eventmask
& EV_EX
)) {
1384 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
))) {
1385 mask
|= EV_EX
|EV_OOB
;
1389 if (event
& EV_RWBYTES
) {
1390 if ((evq
->ee_eventmask
& EV_RE
) && soreadable(sp
)) {
1391 if ((sp
->so_type
== SOCK_STREAM
) && (sp
->so_error
== ECONNREFUSED
) ||
1392 (sp
->so_error
== ECONNRESET
)) {
1393 if ((sp
->so_pcb
== 0) ||
1394 !(tp
= sototcpcb(sp
)) ||
1395 (tp
->t_state
== TCPS_CLOSED
)) {
1396 mask
|= EV_RE
|EV_RESET
;
1400 if (sp
->so_state
& SS_CANTRCVMORE
) {
1401 mask
|= EV_RE
|EV_FIN
;
1402 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
1406 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
1409 if ((evq
->ee_eventmask
& EV_WR
) && sowriteable(sp
)) {
1410 if ((sp
->so_type
== SOCK_STREAM
) &&(sp
->so_error
== ECONNREFUSED
) ||
1411 (sp
->so_error
== ECONNRESET
)) {
1412 if ((sp
->so_pcb
== 0) ||
1413 !(tp
= sototcpcb(sp
)) ||
1414 (tp
->t_state
== TCPS_CLOSED
)) {
1415 mask
|= EV_WR
|EV_RESET
;
1420 evq
->ee_req
.er_wcnt
= sbspace(&sp
->so_snd
);
1426 if ((evq
->ee_eventmask
& EV_RE
)) {
1427 evq
->ee_req
.er_rcnt
= sp
->so_qlen
+ 1; // incl this one
1428 mask
|= EV_RE
|EV_RCONN
;
1433 if ((evq
->ee_eventmask
& EV_WR
)) {
1434 mask
|= EV_WR
|EV_WCONN
;
1439 if ((evq
->ee_eventmask
& EV_RE
)) {
1440 mask
|= EV_RE
|EV_RCLOSED
;
1445 if ((evq
->ee_eventmask
& EV_WR
)) {
1446 mask
|= EV_WR
|EV_WCLOSED
;
1451 if (evq
->ee_eventmask
& EV_RE
) {
1452 mask
|= EV_RE
|EV_FIN
;
1458 if (evq
->ee_eventmask
& EV_RE
) {
1459 mask
|= EV_RE
| event
;
1461 if (evq
->ee_eventmask
& EV_WR
) {
1462 mask
|= EV_WR
| event
;
1471 evq
->ee_req
.er_eventbits
|= mask
;
1472 KERNEL_DEBUG(DBG_MISC_POST
, evq
, evq
->ee_req
.er_eventbits
, mask
,0,0);
1476 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, 0,0,0,0,0);
1480 * remove and return the first event (eqp=NULL) or a specific
1481 * event, or return NULL if no events found
1484 evprocdeque(struct proc
*p
, struct eventqelt
*eqp
)
1487 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_START
,p
,eqp
,0,0,0);
1489 if (eqp
&& ((eqp
->ee_flags
& EV_QUEUED
) == NULL
)) {
1490 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_END
,0,0,0,0,0);
1493 if (p
->p_evlist
.tqh_first
== NULL
) {
1494 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_END
,0,0,0,0,0);
1497 if (eqp
== NULL
) { // remove first
1498 eqp
= p
->p_evlist
.tqh_first
;
1500 TAILQ_REMOVE(&p
->p_evlist
, eqp
, ee_plist
);
1501 eqp
->ee_flags
&= ~EV_QUEUED
;
1502 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_END
,eqp
,0,0,0,0);
1506 struct evwatch_args
{
1507 struct eventreq
*u_req
;
1513 * watchevent system call. user passes us an event to watch
1514 * for. we malloc an event object, initialize it, and queue
1515 * it to the open socket. when the event occurs, postevent()
1516 * will enque it back to our proc where we can retrieve it
1519 * should this prevent duplicate events on same socket?
1522 watchevent(p
, uap
, retval
)
1524 struct evwatch_args
*uap
;
1527 struct eventqelt
*eqp
= (struct eventqelt
*)0;
1528 struct eventqelt
*np
;
1529 struct eventreq
*erp
;
1534 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_START
, 0,0,0,0,0);
1536 // get a qelt and fill with users req
1537 MALLOC(eqp
, struct eventqelt
*, sizeof(struct eventqelt
), M_TEMP
, M_WAITOK
);
1538 if (!eqp
) panic("can't MALLOC eqp");
1540 // get users request pkt
1541 if (error
= copyin((caddr_t
)uap
->u_req
, (caddr_t
)erp
,
1542 sizeof(struct eventreq
))) {
1544 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
1547 KERNEL_DEBUG(DBG_MISC_WATCH
, erp
->er_handle
,uap
->u_eventmask
,eqp
,0,0);
1548 // validate, freeing qelt if errors
1550 if (erp
->er_type
!= EV_FD
) {
1552 } else if (erp
->er_handle
< 0) {
1554 } else if (erp
->er_handle
> p
->p_fd
->fd_nfiles
) {
1556 } else if ((fp
= *fdfile(p
, erp
->er_handle
)) == NULL
) {
1558 } else if (fp
->f_type
!= DTYPE_SOCKET
) {
1563 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
1567 erp
->er_rcnt
= erp
->er_wcnt
= erp
->er_eventbits
= 0;
1569 eqp
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
1572 sp
= (struct socket
*)fp
->f_data
;
1575 // only allow one watch per file per proc
1576 for (np
= sp
->so_evlist
.tqh_first
; np
!= NULL
; np
= np
->ee_slist
.tqe_next
) {
1577 if (np
->ee_proc
== p
) {
1579 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
1584 TAILQ_INSERT_TAIL(&sp
->so_evlist
, eqp
, ee_slist
);
1585 postevent(sp
, 0, EV_RWBYTES
); // catch existing events
1586 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, 0,0,0,0,0);
1590 struct evwait_args
{
1591 struct eventreq
*u_req
;
1596 * waitevent system call.
1597 * grabs the next waiting event for this proc and returns
1598 * it. if no events, user can request to sleep with timeout
1599 * or poll mode (tv=NULL);
1602 waitevent(p
, uap
, retval
)
1604 struct evwait_args
*uap
;
1608 struct eventqelt
*eqp
;
1609 uint64_t abstime
, interval
;
1614 error
= copyin((caddr_t
)uap
->tv
, (caddr_t
)&atv
, sizeof (atv
));
1617 if (itimerfix(&atv
)) {
1622 interval
= tvtoabstime(&atv
);
1625 abstime
= interval
= 0;
1627 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_START
, 0,0,0,0,0);
1630 if ((eqp
= evprocdeque(p
,NULL
)) != NULL
) {
1631 error
= copyout((caddr_t
)&eqp
->ee_req
,
1632 (caddr_t
)uap
->u_req
, sizeof(struct eventreq
));
1633 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,
1634 eqp
->ee_req
.er_handle
,eqp
->ee_req
.er_eventbits
,eqp
,0);
1639 if (uap
->tv
&& interval
== 0) {
1640 *retval
= 1; // poll failed
1641 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,0,0,0,0);
1647 clock_absolutetime_interval_to_deadline(interval
, &abstime
)
1649 KERNEL_DEBUG(DBG_MISC_WAIT
, 1,&p
->p_evlist
,0,0,0);
1650 error
= tsleep1(&p
->p_evlist
, PSOCK
| PCATCH
,
1651 "waitevent", abstime
, (int (*)(int))0);
1652 KERNEL_DEBUG(DBG_MISC_WAIT
, 2,&p
->p_evlist
,0,0,0);
1655 if (error
== ERESTART
)
1657 if (error
== EWOULDBLOCK
) {
1663 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, 0,0,0,0,0);
1668 struct modwatch_args
{
1669 struct eventreq
*u_req
;
1674 * modwatch system call. user passes in event to modify.
1675 * if we find it we reset the event bits and que/deque event
1679 modwatch(p
, uap
, retval
)
1681 struct modwatch_args
*uap
;
1685 struct eventreq
*erp
= &er
;
1686 struct eventqelt
*evq
;
1692 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_START
, 0,0,0,0,0);
1694 // get users request pkt
1695 if (error
= copyin((caddr_t
)uap
->u_req
, (caddr_t
)erp
,
1696 sizeof(struct eventreq
))) return(error
);
1698 if (erp
->er_type
!= EV_FD
) return(EINVAL
);
1699 if (erp
->er_handle
< 0) return(EBADF
);
1700 if (erp
->er_handle
> p
->p_fd
->fd_nfiles
) return(EBADF
);
1701 if ((fp
= *fdfile(p
, erp
->er_handle
)) == NULL
)
1703 if (fp
->f_type
!= DTYPE_SOCKET
) return(EINVAL
); // for now must be sock
1704 sp
= (struct socket
*)fp
->f_data
;
1708 // locate event if possible
1709 for (evq
= sp
->so_evlist
.tqh_first
;
1710 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1711 if (evq
->ee_proc
== p
) break;
1715 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
1718 KERNEL_DEBUG(DBG_MISC_MOD
, erp
->er_handle
,uap
->u_eventmask
,evq
,0,0);
1720 if (uap
->u_eventmask
== EV_RM
) {
1721 evprocdeque(p
, evq
);
1722 TAILQ_REMOVE(&sp
->so_evlist
, evq
, ee_slist
);
1724 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, 0,0,0,0,0);
1728 switch (uap
->u_eventmask
& EV_MASK
) {
1746 case EV_EX
|EV_RE
|EV_WR
:
1747 flag
= EV_OOB
|EV_RWBYTES
;
1754 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
1755 evprocdeque(p
, evq
);
1756 evq
->ee_req
.er_eventbits
= 0;
1757 postevent(sp
, 0, flag
);
1758 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, evq
->ee_req
.er_handle
,evq
->ee_eventmask
,sp
,flag
,0);