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@
24 * @(#)kern_event.c 1.0 (3/31/2000)
27 #include <sys/param.h>
28 #include <sys/socket.h>
29 #include <sys/protosw.h>
30 #include <sys/domain.h>
32 #include <sys/kern_event.h>
33 #include <sys/malloc.h>
36 extern struct domain systemdomain
;
41 struct pr_usrreqs event_usrreqs
;
43 struct protosw eventsw
[] = {
45 SOCK_RAW
, &systemdomain
, SYSPROTO_EVENT
, PR_ATOMIC
,
54 struct kern_event_head kern_event_head
;
56 static u_long static_event_id
= 0;
58 int kev_attach(struct socket
*so
, int proto
, struct proc
*p
)
61 struct kern_event_pcb
*ev_pcb
;
63 ev_pcb
= _MALLOC(sizeof(struct kern_event_pcb
), M_PCB
, M_WAITOK
);
67 ev_pcb
->ev_socket
= so
;
68 ev_pcb
->vendor_code_filter
= 0xffffffff;
70 so
->so_pcb
= (caddr_t
) ev_pcb
;
71 LIST_INSERT_HEAD(&kern_event_head
, ev_pcb
, ev_link
);
72 error
= soreserve(so
, KEV_SNDSPACE
, KEV_RECVSPACE
);
80 int kev_detach(struct socket
*so
)
82 struct kern_event_pcb
*ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
84 LIST_REMOVE(ev_pcb
, ev_link
);
92 int kev_post_msg(struct kev_msg
*event_msg
)
95 struct kern_event_pcb
*ev_pcb
;
96 struct kern_event_msg
*ev
;
102 m
= m_get(M_DONTWAIT
, MT_DATA
);
106 ev
= mtod(m
, struct kern_event_msg
*);
107 total_size
= KEV_MSG_HEADER_SIZE
;
109 tmp
= (char *) &ev
->event_data
[0];
110 for (i
= 0; i
< 5; i
++) {
111 if (event_msg
->dv
[i
].data_length
== 0)
114 total_size
+= event_msg
->dv
[i
].data_length
;
115 bcopy(event_msg
->dv
[i
].data_ptr
, tmp
,
116 event_msg
->dv
[i
].data_length
);
117 tmp
+= event_msg
->dv
[i
].data_length
;
121 ev
->id
= ++static_event_id
;
122 ev
->total_size
= total_size
;
123 ev
->vendor_code
= event_msg
->vendor_code
;
124 ev
->kev_class
= event_msg
->kev_class
;
125 ev
->kev_subclass
= event_msg
->kev_subclass
;
126 ev
->event_code
= event_msg
->event_code
;
128 m
->m_len
= total_size
;
129 ev_pcb
= LIST_FIRST(&kern_event_head
);
130 for (ev_pcb
= LIST_FIRST(&kern_event_head
);
132 ev_pcb
= LIST_NEXT(ev_pcb
, ev_link
)) {
134 if (ev_pcb
->vendor_code_filter
!= KEV_ANY_VENDOR
) {
135 if (ev_pcb
->vendor_code_filter
!= ev
->vendor_code
)
138 if (ev_pcb
->class_filter
!= KEV_ANY_CLASS
) {
139 if (ev_pcb
->class_filter
!= ev
->kev_class
)
142 if ((ev_pcb
->subclass_filter
!= KEV_ANY_SUBCLASS
) &&
143 (ev_pcb
->subclass_filter
!= ev
->kev_subclass
))
148 m2
= m_copym(m
, 0, m
->m_len
, M_NOWAIT
);
154 sbappendrecord(&ev_pcb
->ev_socket
->so_rcv
, m2
);
155 sorwakeup(ev_pcb
->ev_socket
);
164 int kev_control(so
, cmd
, data
, ifp
, p
)
168 register struct ifnet
*ifp
;
171 struct kev_request
*kev_req
= (struct kev_request
*) data
;
173 struct kern_event_pcb
*ev_pcb
;
174 u_long
*id_value
= (u_long
*) data
;
180 *id_value
= static_event_id
;
184 ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
185 ev_pcb
->vendor_code_filter
= kev_req
->vendor_code
;
186 ev_pcb
->class_filter
= kev_req
->kev_class
;
187 ev_pcb
->subclass_filter
= kev_req
->kev_subclass
;
191 ev_pcb
= (struct kern_event_pcb
*) so
->so_pcb
;
192 kev_req
->vendor_code
= ev_pcb
->vendor_code_filter
;
193 kev_req
->kev_class
= ev_pcb
->class_filter
;
194 kev_req
->kev_subclass
= ev_pcb
->subclass_filter
;
205 struct pr_usrreqs event_usrreqs
= {
206 pru_abort_notsupp
, pru_accept_notsupp
, kev_attach
, pru_bind_notsupp
, pru_connect_notsupp
,
207 pru_connect2_notsupp
, kev_control
, kev_detach
, pru_disconnect_notsupp
,
208 pru_listen_notsupp
, pru_peeraddr_notsupp
, pru_rcvd_notsupp
, pru_rcvoob_notsupp
,
209 pru_send_notsupp
, pru_sense_null
, pru_shutdown_notsupp
, pru_sockaddr_notsupp
,
210 pru_sosend_notsupp
, soreceive
, sopoll