]>
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
;
723 act_reference(current_act());
725 act_deallocate(getact_thread(selthread
));
734 register struct selinfo
*sip
;
736 register thread_t the_thread
= (thread_t
)sip
->si_thread
;
744 if (sip
->si_flags
& SI_COLL
) {
746 sip
->si_flags
&= ~SI_COLL
;
747 wakeup((caddr_t
)&selwait
);
752 th_act
= (thread_act_t
)getact_thread(the_thread
);
754 if (is_thread_active(the_thread
)) {
755 if (get_thread_waitevent(the_thread
) == &selwait
)
756 clear_wait(the_thread
, THREAD_AWAKENED
);
757 if (p
= current_proc())
758 p
->p_flag
&= ~P_SELECT
;
761 /* th_act = (thread_act_t)getact_thread(the_thread); */
763 act_deallocate(th_act
);
772 register struct selinfo
*sip
;
776 if (sip
->si_thread
) {
777 th_act
= (thread_act_t
)getact_thread(sip
->si_thread
);
778 act_deallocate(th_act
);
783 extern struct eventqelt
*evprocdeque(struct proc
*p
, struct eventqelt
*eqp
);
786 * called upon socket close. deque and free all events for
789 evsofree(struct socket
*sp
)
791 struct eventqelt
*eqp
, *next
;
793 if (sp
== NULL
) return;
795 for (eqp
= sp
->so_evlist
.tqh_first
; eqp
!= NULL
; eqp
= next
) {
796 next
= eqp
->ee_slist
.tqe_next
;
797 evprocdeque(eqp
->ee_proc
, eqp
); // remove from proc q if there
798 TAILQ_REMOVE(&sp
->so_evlist
, eqp
, ee_slist
); // remove from socket q
804 #define DBG_EVENT 0x10
806 #define DBG_POST 0x10
807 #define DBG_WATCH 0x11
808 #define DBG_WAIT 0x12
810 #define DBG_EWAKEUP 0x14
811 #define DBG_ENQUEUE 0x15
812 #define DBG_DEQUEUE 0x16
814 #define DBG_MISC_POST MISCDBG_CODE(DBG_EVENT,DBG_POST)
815 #define DBG_MISC_WATCH MISCDBG_CODE(DBG_EVENT,DBG_WATCH)
816 #define DBG_MISC_WAIT MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
817 #define DBG_MISC_MOD MISCDBG_CODE(DBG_EVENT,DBG_MOD)
818 #define DBG_MISC_EWAKEUP MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
819 #define DBG_MISC_ENQUEUE MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE)
820 #define DBG_MISC_DEQUEUE MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE)
824 * enque this event if it's not already queued. wakeup
825 the proc if we do queue this event to it.
827 evprocenque(struct eventqelt
*eqp
)
832 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_START
, eqp
, eqp
->ee_flags
, eqp
->ee_eventmask
,0,0);
833 if (eqp
->ee_flags
& EV_QUEUED
) {
834 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
837 eqp
->ee_flags
|= EV_QUEUED
;
838 eqp
->ee_eventmask
= 0; // disarm
840 TAILQ_INSERT_TAIL(&p
->p_evlist
, eqp
, ee_plist
);
841 KERNEL_DEBUG(DBG_MISC_EWAKEUP
,0,0,0,eqp
,0);
842 wakeup(&p
->p_evlist
);
843 KERNEL_DEBUG(DBG_MISC_ENQUEUE
|DBG_FUNC_END
, 0,0,0,0,0);
847 * given either a sockbuf or a socket run down the
848 * event list and queue ready events found
850 postevent(struct socket
*sp
, struct sockbuf
*sb
, int event
)
853 struct eventqelt
*evq
;
854 register struct tcpcb
*tp
;
856 if (sb
) sp
= sb
->sb_so
;
857 if (!sp
|| sp
->so_evlist
.tqh_first
== NULL
) return;
859 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_START
, event
,0,0,0,0);
861 for (evq
= sp
->so_evlist
.tqh_first
;
862 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
866 /* ready for reading:
867 - byte cnt >= receive low water mark
868 - read-half of conn closed
869 - conn pending for listening sock
870 - socket error pending
873 - byte cnt avail >= send low water mark
874 - write half of conn closed
875 - socket error pending
876 - non-blocking conn completed successfully
880 - sock at out of band mark
883 switch (event
& EV_DMASK
) {
887 case EV_RWBYTES
|EV_OOB
:
888 if (event
& EV_OOB
) {
889 if ((evq
->ee_eventmask
& EV_EX
)) {
890 if (sp
->so_oobmark
|| ((sp
->so_state
& SS_RCVATMARK
))) {
891 mask
|= EV_EX
|EV_OOB
;
895 if (event
& EV_RWBYTES
) {
896 if ((evq
->ee_eventmask
& EV_RE
) && soreadable(sp
)) {
897 if ((sp
->so_type
== SOCK_STREAM
) && (sp
->so_error
== ECONNREFUSED
) ||
898 (sp
->so_error
== ECONNRESET
)) {
899 if ((sp
->so_pcb
== 0) ||
900 !(tp
= sototcpcb(sp
)) ||
901 (tp
->t_state
== TCPS_CLOSED
)) {
902 mask
|= EV_RE
|EV_RESET
;
906 if (sp
->so_state
& SS_CANTRCVMORE
) {
907 mask
|= EV_RE
|EV_FIN
;
908 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
912 evq
->ee_req
.er_rcnt
= sp
->so_rcv
.sb_cc
;
915 if ((evq
->ee_eventmask
& EV_WR
) && sowriteable(sp
)) {
916 if ((sp
->so_type
== SOCK_STREAM
) &&(sp
->so_error
== ECONNREFUSED
) ||
917 (sp
->so_error
== ECONNRESET
)) {
918 if ((sp
->so_pcb
== 0) ||
919 !(tp
= sototcpcb(sp
)) ||
920 (tp
->t_state
== TCPS_CLOSED
)) {
921 mask
|= EV_WR
|EV_RESET
;
926 evq
->ee_req
.er_wcnt
= sbspace(&sp
->so_snd
);
932 if ((evq
->ee_eventmask
& EV_RE
)) {
933 evq
->ee_req
.er_rcnt
= sp
->so_qlen
+ 1; // incl this one
934 mask
|= EV_RE
|EV_RCONN
;
939 if ((evq
->ee_eventmask
& EV_WR
)) {
940 mask
|= EV_WR
|EV_WCONN
;
945 if ((evq
->ee_eventmask
& EV_RE
)) {
946 mask
|= EV_RE
|EV_RCLOSED
;
951 if ((evq
->ee_eventmask
& EV_WR
)) {
952 mask
|= EV_WR
|EV_WCLOSED
;
957 if (evq
->ee_eventmask
& EV_RE
) {
958 mask
|= EV_RE
|EV_FIN
;
964 if (evq
->ee_eventmask
& EV_RE
) {
965 mask
|= EV_RE
| event
;
967 if (evq
->ee_eventmask
& EV_WR
) {
968 mask
|= EV_WR
| event
;
977 evq
->ee_req
.er_eventbits
|= mask
;
978 KERNEL_DEBUG(DBG_MISC_POST
, evq
, evq
->ee_req
.er_eventbits
, mask
,0,0);
982 KERNEL_DEBUG(DBG_MISC_POST
|DBG_FUNC_END
, 0,0,0,0,0);
986 * remove and return the first event (eqp=NULL) or a specific
987 * event, or return NULL if no events found
990 evprocdeque(struct proc
*p
, struct eventqelt
*eqp
)
993 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_START
,p
,eqp
,0,0,0);
995 if (eqp
&& ((eqp
->ee_flags
& EV_QUEUED
) == NULL
)) {
996 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_END
,0,0,0,0,0);
999 if (p
->p_evlist
.tqh_first
== NULL
) {
1000 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_END
,0,0,0,0,0);
1003 if (eqp
== NULL
) { // remove first
1004 eqp
= p
->p_evlist
.tqh_first
;
1006 TAILQ_REMOVE(&p
->p_evlist
, eqp
, ee_plist
);
1007 eqp
->ee_flags
&= ~EV_QUEUED
;
1008 KERNEL_DEBUG(DBG_MISC_DEQUEUE
|DBG_FUNC_END
,eqp
,0,0,0,0);
1012 struct evwatch_args
{
1013 struct eventreq
*u_req
;
1019 * watchevent system call. user passes us an event to watch
1020 * for. we malloc an event object, initialize it, and queue
1021 * it to the open socket. when the event occurs, postevent()
1022 * will enque it back to our proc where we can retrieve it
1025 * should this prevent duplicate events on same socket?
1028 watchevent(p
, uap
, retval
)
1030 struct evwatch_args
*uap
;
1033 struct eventqelt
*eqp
= (struct eventqelt
*)0;
1034 struct eventqelt
*np
;
1035 struct eventreq
*erp
;
1040 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_START
, 0,0,0,0,0);
1042 // get a qelt and fill with users req
1043 MALLOC(eqp
, struct eventqelt
*, sizeof(struct eventqelt
), M_TEMP
, M_WAITOK
);
1044 if (!eqp
) panic("can't MALLOC eqp");
1046 // get users request pkt
1047 if (error
= copyin((caddr_t
)uap
->u_req
, (caddr_t
)erp
,
1048 sizeof(struct eventreq
))) {
1050 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
1053 KERNEL_DEBUG(DBG_MISC_WATCH
, erp
->er_handle
,uap
->u_eventmask
,eqp
,0,0);
1054 // validate, freeing qelt if errors
1056 if (erp
->er_type
!= EV_FD
) {
1058 } else if (erp
->er_handle
< 0) {
1060 } else if (erp
->er_handle
> p
->p_fd
->fd_nfiles
) {
1062 } else if ((fp
= *fdfile(p
, erp
->er_handle
)) == NULL
) {
1064 } else if (fp
->f_type
!= DTYPE_SOCKET
) {
1069 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, error
,0,0,0,0);
1073 erp
->er_rcnt
= erp
->er_wcnt
= erp
->er_eventbits
= 0;
1075 eqp
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
1078 sp
= (struct socket
*)fp
->f_data
;
1081 // only allow one watch per file per proc
1082 for (np
= sp
->so_evlist
.tqh_first
; np
!= NULL
; np
= np
->ee_slist
.tqe_next
) {
1083 if (np
->ee_proc
== p
) {
1085 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
1090 TAILQ_INSERT_TAIL(&sp
->so_evlist
, eqp
, ee_slist
);
1091 postevent(sp
, 0, EV_RWBYTES
); // catch existing events
1092 KERNEL_DEBUG(DBG_MISC_WATCH
|DBG_FUNC_END
, 0,0,0,0,0);
1096 struct evwait_args
{
1097 struct eventreq
*u_req
;
1102 * waitevent system call.
1103 * grabs the next waiting event for this proc and returns
1104 * it. if no events, user can request to sleep with timeout
1105 * or poll mode (tv=NULL);
1108 waitevent(p
, uap
, retval
)
1110 struct evwait_args
*uap
;
1114 struct eventqelt
*eqp
;
1120 error
= copyin((caddr_t
)uap
->tv
, (caddr_t
)&atv
,
1124 if (itimerfix(&atv
)) {
1129 timeradd(&atv
, &time
, &atv
);
1135 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_START
, 0,0,0,0,0);
1139 if ((eqp
= evprocdeque(p
,NULL
)) != NULL
) {
1141 error
= copyout((caddr_t
)&eqp
->ee_req
, (caddr_t
)uap
->u_req
,
1142 sizeof(struct eventreq
));
1143 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,
1144 eqp
->ee_req
.er_handle
,eqp
->ee_req
.er_eventbits
,eqp
,0);
1147 if (uap
->tv
&& (timo
== 0)) {
1149 *retval
= 1; // poll failed
1150 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, error
,0,0,0,0);
1154 KERNEL_DEBUG(DBG_MISC_WAIT
, 1,&p
->p_evlist
,0,0,0);
1155 error
= tsleep(&p
->p_evlist
, PSOCK
| PCATCH
, "waitevent", timo
);
1156 KERNEL_DEBUG(DBG_MISC_WAIT
, 2,&p
->p_evlist
,0,0,0);
1160 if (error
== ERESTART
)
1162 if (error
== EWOULDBLOCK
) {
1167 KERNEL_DEBUG(DBG_MISC_WAIT
|DBG_FUNC_END
, 0,0,0,0,0);
1171 struct modwatch_args
{
1172 struct eventreq
*u_req
;
1177 * modwatch system call. user passes in event to modify.
1178 * if we find it we reset the event bits and que/deque event
1182 modwatch(p
, uap
, retval
)
1184 struct modwatch_args
*uap
;
1188 struct eventreq
*erp
= &er
;
1189 struct eventqelt
*evq
;
1195 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_START
, 0,0,0,0,0);
1197 // get users request pkt
1198 if (error
= copyin((caddr_t
)uap
->u_req
, (caddr_t
)erp
,
1199 sizeof(struct eventreq
))) return(error
);
1201 if (erp
->er_type
!= EV_FD
) return(EINVAL
);
1202 if (erp
->er_handle
< 0) return(EBADF
);
1203 if (erp
->er_handle
> p
->p_fd
->fd_nfiles
) return(EBADF
);
1204 if ((fp
= *fdfile(p
, erp
->er_handle
)) == NULL
)
1206 if (fp
->f_type
!= DTYPE_SOCKET
) return(EINVAL
); // for now must be sock
1207 sp
= (struct socket
*)fp
->f_data
;
1211 // locate event if possible
1212 for (evq
= sp
->so_evlist
.tqh_first
;
1213 evq
!= NULL
; evq
= evq
->ee_slist
.tqe_next
) {
1214 if (evq
->ee_proc
== p
) break;
1218 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, EINVAL
,0,0,0,0);
1221 KERNEL_DEBUG(DBG_MISC_MOD
, erp
->er_handle
,uap
->u_eventmask
,evq
,0,0);
1223 if (uap
->u_eventmask
== EV_RM
) {
1224 evprocdeque(p
, evq
);
1225 TAILQ_REMOVE(&sp
->so_evlist
, evq
, ee_slist
);
1227 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, 0,0,0,0,0);
1231 switch (uap
->u_eventmask
& EV_MASK
) {
1249 case EV_EX
|EV_RE
|EV_WR
:
1250 flag
= EV_OOB
|EV_RWBYTES
;
1257 evq
->ee_eventmask
= uap
->u_eventmask
& EV_MASK
;
1258 evprocdeque(p
, evq
);
1259 evq
->ee_req
.er_eventbits
= 0;
1260 postevent(sp
, 0, flag
);
1261 KERNEL_DEBUG(DBG_MISC_MOD
|DBG_FUNC_END
, evq
->ee_req
.er_handle
,evq
->ee_eventmask
,sp
,flag
,0);