]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/sys_generic.c
2 * Copyright (c) 2000 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>
76 #include <sys/ktrace.h>
79 #include <sys/mount.h>
80 #include <sys/protosw.h>
83 #include <sys/kdebug.h>
84 #include <kern/assert.h>
85 #include <kern/thread_act.h>
88 #include <sys/socket.h>
89 #include <sys/socketvar.h>
90 #include <sys/errno.h>
93 #include <net/route.h>
95 #include <netinet/in.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/ip.h>
98 #include <netinet/in_pcb.h>
99 #include <netinet/ip_var.h>
100 #include <netinet/ip6.h>
101 #include <netinet/tcp.h>
102 #include <netinet/tcp_fsm.h>
103 #include <netinet/tcp_seq.h>
104 #include <netinet/tcp_timer.h>
105 #include <netinet/tcp_var.h>
106 #include <netinet/tcpip.h>
107 #include <netinet/tcp_debug.h>
120 register struct read_args
*uap
;
126 aiov
.iov_base
= (caddr_t
)uap
->cbuf
;
127 aiov
.iov_len
= uap
->nbyte
;
128 auio
.uio_iov
= &aiov
;
130 auio
.uio_rw
= UIO_READ
;
131 return (rwuio(p
, uap
->fd
, &auio
, UIO_READ
, retval
));
139 readv(p
, uap
, retval
)
141 register struct readv_args
*uap
;
145 register struct iovec
*iov
;
147 struct iovec aiov
[UIO_SMALLIOV
];
149 if (uap
->iovcnt
> UIO_SMALLIOV
) {
150 if (uap
->iovcnt
> UIO_MAXIOV
)
152 if ((iov
= (struct iovec
*)
153 kalloc(sizeof(struct iovec
) * (uap
->iovcnt
))) == 0)
158 auio
.uio_iovcnt
= uap
->iovcnt
;
159 auio
.uio_rw
= UIO_READ
;
160 error
= copyin((caddr_t
)uap
->iovp
, (caddr_t
)iov
,
161 uap
->iovcnt
* sizeof (struct iovec
));
163 error
= rwuio(p
, uap
->fd
, &auio
, UIO_READ
, retval
);
164 if (uap
->iovcnt
> UIO_SMALLIOV
)
165 kfree(iov
, sizeof(struct iovec
)*uap
->iovcnt
);
177 write(p
, uap
, retval
)
179 register struct write_args
*uap
;
185 aiov
.iov_base
= uap
->cbuf
;
186 aiov
.iov_len
= uap
->nbyte
;
187 auio
.uio_iov
= &aiov
;
189 auio
.uio_rw
= UIO_WRITE
;
190 return (rwuio(p
, uap
->fd
, &auio
, UIO_WRITE
, retval
));
198 writev(p
, uap
, retval
)
200 register struct writev_args
*uap
;
204 register struct iovec
*iov
;
206 struct iovec aiov
[UIO_SMALLIOV
];
208 if (uap
->iovcnt
> UIO_SMALLIOV
) {
209 if (uap
->iovcnt
> UIO_MAXIOV
)
211 if ((iov
= (struct iovec
*)
212 kalloc(sizeof(struct iovec
) * (uap
->iovcnt
))) == 0)
217 auio
.uio_iovcnt
= uap
->iovcnt
;
218 auio
.uio_rw
= UIO_WRITE
;
219 error
= copyin((caddr_t
)uap
->iovp
, (caddr_t
)iov
,
220 uap
->iovcnt
* sizeof (struct iovec
));
222 error
= rwuio(p
, uap
->fd
, &auio
, UIO_WRITE
, retval
);
223 if (uap
->iovcnt
> UIO_SMALLIOV
)
224 kfree(iov
, sizeof(struct iovec
)*uap
->iovcnt
);
228 rwuio(p
, fdes
, uio
, rw
, retval
)
231 register struct uio
*uio
;
236 register struct iovec
*iov
;
237 int i
, count
, flag
, error
;
239 if (error
= fdgetf(p
, fdes
, &fp
))
242 if ((fp
->f_flag
&(rw
==UIO_READ
? FREAD
: FWRITE
)) == 0) {
246 uio
->uio_segflg
= UIO_USERSPACE
;
249 for (i
= 0; i
< uio
->uio_iovcnt
; i
++) {
250 if (iov
->iov_len
< 0) {
253 uio
->uio_resid
+= iov
->iov_len
;
254 if (uio
->uio_resid
< 0) {
259 count
= uio
->uio_resid
;
260 if (rw
== UIO_READ
) {
261 if (error
= (*fp
->f_ops
->fo_read
)(fp
, uio
, fp
->f_cred
))
262 if (uio
->uio_resid
!= count
&& (error
== ERESTART
||
263 error
== EINTR
|| error
== EWOULDBLOCK
))
266 if (error
= (*fp
->f_ops
->fo_write
)(fp
, uio
, fp
->f_cred
)) {
267 if (uio
->uio_resid
!= count
&& (error
== ERESTART
||
268 error
== EINTR
|| error
== EWOULDBLOCK
))
274 *retval
= count
- uio
->uio_resid
;
287 ioctl(p
, uap
, retval
)
289 register struct ioctl_args
*uap
;
298 #define STK_PARAMS 128
299 char stkbuf
[STK_PARAMS
];
301 if (error
= fdgetf(p
, uap
->fd
, &fp
))
304 if ((fp
->f_flag
& (FREAD
| FWRITE
)) == 0)
307 /*### LD 6/11/97 Hack Alert: this is to get AppleTalk to work
308 * while implementing an ATioctl system call
312 extern int appletalk_inited
;
314 if (appletalk_inited
&& ((uap
->com
& 0x0000FFFF) == 0xff99)) {
315 #ifdef APPLETALK_DEBUG
316 kprintf("ioctl: special AppleTalk \n");
318 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, uap
->com
, uap
->data
, p
);
326 switch (com
= uap
->com
) {
328 *fdflags(p
, uap
->fd
) &= ~UF_EXCLOSE
;
331 *fdflags(p
, uap
->fd
) |= UF_EXCLOSE
;
336 * Interpret high order word to find amount of data to be
337 * copied to/from the user's address space.
339 size
= IOCPARM_LEN(com
);
340 if (size
> IOCPARM_MAX
)
343 if (size
> sizeof (stkbuf
)) {
344 if ((memp
= (caddr_t
)kalloc(size
)) == 0)
351 error
= copyin(uap
->data
, data
, (u_int
)size
);
358 *(caddr_t
*)data
= uap
->data
;
359 } else if ((com
&IOC_OUT
) && size
)
361 * Zero the buffer so the user always
362 * gets back something deterministic.
365 else if (com
&IOC_VOID
)
366 *(caddr_t
*)data
= uap
->data
;
371 if (tmp
= *(int *)data
)
372 fp
->f_flag
|= FNONBLOCK
;
374 fp
->f_flag
&= ~FNONBLOCK
;
375 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
379 if (tmp
= *(int *)data
)
380 fp
->f_flag
|= FASYNC
;
382 fp
->f_flag
&= ~FASYNC
;
383 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, FIOASYNC
, (caddr_t
)&tmp
, p
);
388 if (fp
->f_type
== DTYPE_SOCKET
) {
389 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
396 struct proc
*p1
= pfind(tmp
);
401 tmp
= p1
->p_pgrp
->pg_id
;
403 error
= (*fp
->f_ops
->fo_ioctl
)
404 (fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, p
);
408 if (fp
->f_type
== DTYPE_SOCKET
) {
410 *(int *)data
= ((struct socket
*)fp
->f_data
)->so_pgid
;
413 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, TIOCGPGRP
, data
, p
);
414 *(int *)data
= -*(int *)data
;
418 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, p
);
420 * Copy any data to user, size was
421 * already set and checked above.
423 if (error
== 0 && (com
&IOC_OUT
) && size
)
424 error
= copyout(data
, uap
->data
, (u_int
)size
);
433 int selwait
, nselcoll
;
436 * Select system call.
446 extern int selcontinue(int error
);
447 static int selscan( struct proc
*p
, u_int32_t
*ibits
, u_int32_t
*obits
,
448 int nfd
, register_t
*retval
);
450 select(p
, uap
, retval
)
451 register struct proc
*p
;
452 register struct select_args
*uap
;
455 int s
, error
= 0, timo
;
460 int needzerofill
= 1;
462 th_act
= current_act();
463 uth
= get_bsdthread_info(th_act
);
464 sel
= &uth
->uu_state
.ss_select
;
465 retval
= (int *)get_bsduthreadrval(th_act
);
471 if (uap
->nd
> p
->p_fd
->fd_nfiles
)
472 uap
->nd
= p
->p_fd
->fd_nfiles
; /* forgiving; slightly wrong */
474 nw
= howmany(uap
->nd
, NFDBITS
);
475 ni
= nw
* sizeof(fd_mask
);
478 * if this is the first select by the thread
479 * allocate the space for bits.
481 if (sel
->nbytes
== 0) {
482 sel
->nbytes
= 3 * ni
;
483 MALLOC(sel
->ibits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
);
484 MALLOC(sel
->obits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
);
485 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
486 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
491 * if the previously allocated space for the bits
492 * is smaller than what is requested. Reallocate.
494 if (sel
->nbytes
< (3 * ni
)) {
495 sel
->nbytes
= (3 * ni
);
496 FREE(sel
->ibits
, M_TEMP
);
497 FREE(sel
->obits
, M_TEMP
);
498 MALLOC(sel
->ibits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
);
499 MALLOC(sel
->obits
, u_int32_t
*, sel
->nbytes
, M_TEMP
, M_WAITOK
);
500 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
501 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
506 bzero((caddr_t
)sel
->ibits
, sel
->nbytes
);
507 bzero((caddr_t
)sel
->obits
, sel
->nbytes
);
511 * get the bits from the user address space
513 #define getbits(name, x) \
515 if (uap->name && (error = copyin((caddr_t)uap->name, \
516 (caddr_t)&sel->ibits[(x) * nw], ni))) \
526 error
= copyin((caddr_t
)uap
->tv
, (caddr_t
)&sel
->atv
,
530 if (itimerfix(&sel
->atv
)) {
535 timeradd(&sel
->atv
, &time
, &sel
->atv
);
536 timo
= hzto(&sel
->atv
);
553 struct select_args
*uap
;
558 th_act
= current_act();
559 uap
= (struct select_args
*)get_bsduthreadarg(th_act
);
560 retval
= (int *)get_bsduthreadrval(th_act
);
561 uth
= get_bsdthread_info(th_act
);
562 sel
= &uth
->uu_state
.ss_select
;
568 p
->p_flag
|= P_SELECT
;
569 error
= selscan(p
, sel
->ibits
, sel
->obits
, uap
->nd
, retval
);
570 if (error
|| *retval
)
573 /* this should be timercmp(&time, &atv, >=) */
574 if (uap
->tv
&& (time
.tv_sec
> sel
->atv
.tv_sec
||
575 time
.tv_sec
== sel
->atv
.tv_sec
&& time
.tv_usec
>= sel
->atv
.tv_usec
)) {
580 * To effect a poll, the timeout argument should be
581 * non-nil, pointing to a zero-valued timeval structure.
585 if (uap
->tv
&& (timo
== 0)) {
589 if ((p
->p_flag
& P_SELECT
) == 0 || nselcoll
!= ncoll
) {
593 p
->p_flag
&= ~P_SELECT
;
595 #if 1 /* Use Continuations */
596 error
= tsleep0((caddr_t
)&selwait
, PSOCK
| PCATCH
, "select", timo
, selcontinue
);
599 error
= tsleep((caddr_t
)&selwait
, PSOCK
| PCATCH
, "select", timo
);
605 p
->p_flag
&= ~P_SELECT
;
606 /* select is not restarted after signals... */
607 if (error
== ERESTART
)
609 if (error
== EWOULDBLOCK
)
612 nw
= howmany(uap
->nd
, NFDBITS
);
613 ni
= nw
* sizeof(fd_mask
);
615 #define putbits(name, x) \
617 if (uap->name && (error2 = copyout((caddr_t)&sel->obits[(x) * nw], \
618 (caddr_t)uap->name, ni))) \
631 #if defined (__i386__)
634 unix_syscall_return(error
);
639 selscan(p
, ibits
, obits
, nfd
, retval
)
641 u_int32_t
*ibits
, *obits
;
645 register struct filedesc
*fdp
= p
->p_fd
;
646 register int msk
, i
, j
, fd
;
647 register u_int32_t bits
;
650 static int flag
[3] = { FREAD
, FWRITE
, 0 };
651 u_int32_t
*iptr
, *optr
;
655 * Problems when reboot; due to MacOSX signal probs
656 * in Beaker1C ; verify that the p->p_fd is valid
663 nw
= howmany(nfd
, NFDBITS
);
665 for (msk
= 0; msk
< 3; msk
++) {
666 iptr
= (u_int32_t
*)&ibits
[msk
* nw
];
667 optr
= (u_int32_t
*)&obits
[msk
* nw
];
668 for (i
= 0; i
< nfd
; i
+= NFDBITS
) {
669 bits
= iptr
[i
/NFDBITS
];
670 while ((j
= ffs(bits
)) && (fd
= i
+ --j
) < nfd
) {
672 fp
= fdp
->fd_ofiles
[fd
];
674 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
676 if (fp
->f_ops
&& (*fp
->f_ops
->fo_select
)(fp
, flag
[msk
], p
)) {
677 optr
[fd
/NFDBITS
] |= (1 << (fd
% NFDBITS
));
688 seltrue(dev
, flag
, p
)
698 * Record a select request.
701 selrecord(selector
, sip
)
702 struct proc
*selector
;
705 int oldpri
= splhigh();
706 thread_t my_thread
= current_thread();
709 selthread
= sip
->si_thread
;
711 if (selthread
== my_thread
) {
716 if (selthread
&& is_thread_active(selthread
) &&
717 get_thread_waitevent(selthread
) == (caddr_t
)&selwait
) {
718 sip
->si_flags
|= SI_COLL
;
721 sip
->si_thread
= my_thread
;
724 /* thread_deallocate(selthread); */
725 act_deallocate(getact_thread(selthread
));
727 /* do I need act reference ??? */
728 /* thread_reference(sip->si_thread); */
729 act_reference(getact_thread(sip
->si_thread
));
737 register struct selinfo
*sip
;
739 register thread_t the_thread
= (thread_t
)sip
->si_thread
;
747 if (sip
->si_flags
& SI_COLL
) {
749 sip
->si_flags
&= ~SI_COLL
;
750 wakeup((caddr_t
)&selwait
);
755 th_act
= (thread_act_t
)getact_thread(the_thread
);
757 if (is_thread_active(the_thread
)) {
758 if (get_thread_waitevent(the_thread
) == &selwait
)
759 clear_wait(the_thread
, THREAD_AWAKENED
);
760 if (p
= current_proc())
761 p
->p_flag
&= ~P_SELECT
;
764 /* th_act = (thread_act_t)getact_thread(the_thread); */
766 act_deallocate(th_act
);
775 register struct selinfo
*sip
;
779 if (sip
->si_thread
) {
780 th_act
= (thread_act_t
)getact_thread(sip
->si_thread
);
781 act_deallocate(th_act
);
786 extern struct eventqelt
*evprocdeque(struct proc
*p
, struct eventqelt
*eqp
);
789 * called upon socket close. deque and free all events for
792 evsofree(struct socket
*sp
)
794 struct eventqelt
*eqp
, *next
;
796 if (sp
== NULL
) return;
798 for (eqp
= sp
->so_evlist
.tqh_first
; eqp
!= NULL
; eqp
= next
) {
799 next
= eqp
->ee_slist
.tqe_next
;
800 evprocdeque(eqp
->ee_proc
, eqp
); // remove from proc q if there
801 TAILQ_REMOVE(&sp
->so_evlist
, eqp
, ee_slist
); // remove from socket q
807 #define DBG_EVENT 0x10
809 #define DBG_POST 0x10
810 #define DBG_WATCH 0x11
811 #define DBG_WAIT 0x12
813 #define DBG_EWAKEUP 0x14
814 #define DBG_ENQUEUE 0x15
815 #define DBG_DEQUEUE 0x16
817 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
818 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
819 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
820 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
821 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
822 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
823 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
827 * enque this event if it's not already queued. wakeup
828 the proc if we do queue this event to it.
830 evprocenque(struct eventqelt
*eqp
)
835 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_START
, eqp
, eqp
->ee_flags
, eqp
->ee_eventmask
,0,0);
836 if (eqp
->ee_flags
& EV_QUEUED
) {
837 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
840 eqp
->ee_flags
|= EV_QUEUED
;
841 eqp
->ee_eventmask
= 0; // disarm
843 TAILQ_INSERT_TAIL(&p
->p_evlist
, eqp
, ee_plist
);
844 KERNEL_DEBUG(DBG_MISC_EWAKEUP
,0,0,0,eqp
,0);
845 wakeup(&p
->p_evlist
);
846 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
850 * given either a sockbuf or a socket run down the
851 * event list and queue ready events found
853 postevent(struct socket
*sp
, struct sockbuf
*sb
, int event
)
856 struct eventqelt
*evq
;
857 register struct tcpcb
*tp
;
859 if (sb
) sp
= sb
->sb_so
;
860 if (!sp
|| sp
->so_evlist
.tqh_first
== NULL
) return;
862 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, event
,0,0,0,0);
864 for (evq
= sp
->so_evlist
.tqh_first
;
865 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
869 /* ready for reading:
870 - byte cnt >= receive low water mark
871 - read-half of conn closed
872 - conn pending for listening sock
873 - socket error pending
876 - byte cnt avail >= send low water mark
877 - write half of conn closed
878 - socket error pending
879 - non-blocking conn completed successfully
883 - sock at out of band mark
886 switch (event
& EV_DMASK
) {
890 case EV_RWBYTES
|EV_OOB
:
891 if (event
& EV_OOB
) {
892 if ((evq
->ee_eventmask
& EV_EX
)) {
893 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
))) {
894 mask
|= EV_EX
|EV_OOB
;
898 if (event
& EV_RWBYTES
) {
899 if ((evq
->ee_eventmask
& EV_RE
) && soreadable(sp
)) {
900 if ((sp
->so_type
== SOCK_STREAM
) && (sp
->so_error
== ECONNREFUSED
) ||
901 (sp
->so_error
== ECONNRESET
)) {
902 if ((sp
->so_pcb
== 0) ||
903 !(tp
= sototcpcb(sp
)) ||
904 (tp
->t_state
== TCPS_CLOSED
)) {
905 mask
|= EV_RE
|EV_RESET
;
909 if (sp
->so_state
& SS_CANTRCVMORE
) {
910 mask
|= EV_RE
|EV_FIN
;
911 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
915 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
918 if ((evq
->ee_eventmask
& EV_WR
) && sowriteable(sp
)) {
919 if ((sp
->so_type
== SOCK_STREAM
) &&(sp
->so_error
== ECONNREFUSED
) ||
920 (sp
->so_error
== ECONNRESET
)) {
921 if ((sp
->so_pcb
== 0) ||
922 !(tp
= sototcpcb(sp
)) ||
923 (tp
->t_state
== TCPS_CLOSED
)) {
924 mask
|= EV_WR
|EV_RESET
;
929 evq
->ee_req
.er_wcnt
= sbspace(&sp
->so_snd
);
935 if ((evq
->ee_eventmask
& EV_RE
)) {
936 evq
->ee_req
.er_rcnt
= sp
->so_qlen
+ 1; // incl this one
937 mask
|= EV_RE
|EV_RCONN
;
942 if ((evq
->ee_eventmask
& EV_WR
)) {
943 mask
|= EV_WR
|EV_WCONN
;
948 if ((evq
->ee_eventmask
& EV_RE
)) {
949 mask
|= EV_RE
|EV_RCLOSED
;
954 if ((evq
->ee_eventmask
& EV_WR
)) {
955 mask
|= EV_WR
|EV_WCLOSED
;
960 if (evq
->ee_eventmask
& EV_RE
) {
961 mask
|= EV_RE
|EV_FIN
;
967 if (evq
->ee_eventmask
& EV_RE
) {
968 mask
|= EV_RE
| event
;
970 if (evq
->ee_eventmask
& EV_WR
) {
971 mask
|= EV_WR
| event
;
980 evq
->ee_req
.er_eventbits
|= mask
;
981 KERNEL_DEBUG(DBG_MISC_POST
, evq
, evq
->ee_req
.er_eventbits
, mask
,0,0);
985 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, 0,0,0,0,0);
989 * remove and return the first event (eqp=NULL) or a specific
990 * event, or return NULL if no events found
993 evprocdeque(struct proc
*p
, struct eventqelt
*eqp
)
996 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_START
,p
,eqp
,0,0,0);
998 if (eqp
&& ((eqp
->ee_flags
& EV_QUEUED
) == NULL
)) {
999 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_END
,0,0,0,0,0);
1002 if (p
->p_evlist
.tqh_first
== NULL
) {
1003 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_END
,0,0,0,0,0);
1006 if (eqp
== NULL
) { // remove first
1007 eqp
= p
->p_evlist
.tqh_first
;
1009 TAILQ_REMOVE(&p
->p_evlist
, eqp
, ee_plist
);
1010 eqp
->ee_flags
&= ~EV_QUEUED
;
1011 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_END
,eqp
,0,0,0,0);
1015 struct evwatch_args
{
1016 struct eventreq
*u_req
;
1022 * watchevent system call. user passes us an event to watch
1023 * for. we malloc an event object, initialize it, and queue
1024 * it to the open socket. when the event occurs, postevent()
1025 * will enque it back to our proc where we can retrieve it
1028 * should this prevent duplicate events on same socket?
1031 watchevent(p
, uap
, retval
)
1033 struct evwatch_args
*uap
;
1036 struct eventqelt
*eqp
= (struct eventqelt
*)0;
1037 struct eventqelt
*np
;
1038 struct eventreq
*erp
;
1043 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_START
, 0,0,0,0,0);
1045 // get a qelt and fill with users req
1046 MALLOC(eqp
, struct eventqelt
*, sizeof(struct eventqelt
), M_TEMP
, M_WAITOK
);
1047 if (!eqp
) panic("can't MALLOC eqp");
1049 // get users request pkt
1050 if (error
= copyin((caddr_t
)uap
->u_req
, (caddr_t
)erp
,
1051 sizeof(struct eventreq
))) {
1053 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
1056 KERNEL_DEBUG(DBG_MISC_WATCH
, erp
->er_handle
,uap
->u_eventmask
,eqp
,0,0);
1057 // validate, freeing qelt if errors
1059 if (erp
->er_type
!= EV_FD
) {
1061 } else if (erp
->er_handle
< 0) {
1063 } else if (erp
->er_handle
> p
->p_fd
->fd_nfiles
) {
1065 } else if ((fp
= *fdfile(p
, erp
->er_handle
)) == NULL
) {
1067 } else if (fp
->f_type
!= DTYPE_SOCKET
) {
1072 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
1076 erp
->er_rcnt
= erp
->er_wcnt
= erp
->er_eventbits
= 0;
1078 eqp
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
1081 sp
= (struct socket
*)fp
->f_data
;
1084 // only allow one watch per file per proc
1085 for (np
= sp
->so_evlist
.tqh_first
; np
!= NULL
; np
= np
->ee_slist
.tqe_next
) {
1086 if (np
->ee_proc
== p
) {
1088 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
1093 TAILQ_INSERT_TAIL(&sp
->so_evlist
, eqp
, ee_slist
);
1094 postevent(sp
, 0, EV_RWBYTES
); // catch existing events
1095 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, 0,0,0,0,0);
1099 struct evwait_args
{
1100 struct eventreq
*u_req
;
1105 * waitevent system call.
1106 * grabs the next waiting event for this proc and returns
1107 * it. if no events, user can request to sleep with timeout
1108 * or poll mode (tv=NULL);
1111 waitevent(p
, uap
, retval
)
1113 struct evwait_args
*uap
;
1117 struct eventqelt
*eqp
;
1123 error
= copyin((caddr_t
)uap
->tv
, (caddr_t
)&atv
,
1127 if (itimerfix(&atv
)) {
1132 timeradd(&atv
, &time
, &atv
);
1138 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_START
, 0,0,0,0,0);
1142 if ((eqp
= evprocdeque(p
,NULL
)) != NULL
) {
1144 error
= copyout((caddr_t
)&eqp
->ee_req
, (caddr_t
)uap
->u_req
,
1145 sizeof(struct eventreq
));
1146 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,
1147 eqp
->ee_req
.er_handle
,eqp
->ee_req
.er_eventbits
,eqp
,0);
1150 if (uap
->tv
&& (timo
== 0)) {
1152 *retval
= 1; // poll failed
1153 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,0,0,0,0);
1157 KERNEL_DEBUG(DBG_MISC_WAIT
, 1,&p
->p_evlist
,0,0,0);
1158 error
= tsleep(&p
->p_evlist
, PSOCK
| PCATCH
, "waitevent", timo
);
1159 KERNEL_DEBUG(DBG_MISC_WAIT
, 2,&p
->p_evlist
,0,0,0);
1163 if (error
== ERESTART
)
1165 if (error
== EWOULDBLOCK
) {
1170 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, 0,0,0,0,0);
1174 struct modwatch_args
{
1175 struct eventreq
*u_req
;
1180 * modwatch system call. user passes in event to modify.
1181 * if we find it we reset the event bits and que/deque event
1185 modwatch(p
, uap
, retval
)
1187 struct modwatch_args
*uap
;
1191 struct eventreq
*erp
= &er
;
1192 struct eventqelt
*evq
;
1198 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_START
, 0,0,0,0,0);
1200 // get users request pkt
1201 if (error
= copyin((caddr_t
)uap
->u_req
, (caddr_t
)erp
,
1202 sizeof(struct eventreq
))) return(error
);
1204 if (erp
->er_type
!= EV_FD
) return(EINVAL
);
1205 if (erp
->er_handle
< 0) return(EBADF
);
1206 if (erp
->er_handle
> p
->p_fd
->fd_nfiles
) return(EBADF
);
1207 if ((fp
= *fdfile(p
, erp
->er_handle
)) == NULL
)
1209 if (fp
->f_type
!= DTYPE_SOCKET
) return(EINVAL
); // for now must be sock
1210 sp
= (struct socket
*)fp
->f_data
;
1214 // locate event if possible
1215 for (evq
= sp
->so_evlist
.tqh_first
;
1216 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1217 if (evq
->ee_proc
== p
) break;
1221 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
1224 KERNEL_DEBUG(DBG_MISC_MOD
, erp
->er_handle
,uap
->u_eventmask
,evq
,0,0);
1226 if (uap
->u_eventmask
== EV_RM
) {
1227 evprocdeque(p
, evq
);
1228 TAILQ_REMOVE(&sp
->so_evlist
, evq
, ee_slist
);
1230 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, 0,0,0,0,0);
1234 switch (uap
->u_eventmask
& EV_MASK
) {
1252 case EV_EX
|EV_RE
|EV_WR
:
1253 flag
= EV_OOB
|EV_RWBYTES
;
1260 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
1261 evprocdeque(p
, evq
);
1262 evq
->ee_req
.er_eventbits
= 0;
1263 postevent(sp
, 0, flag
);
1264 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, evq
->ee_req
.er_handle
,evq
->ee_eventmask
,sp
,flag
,0);