2 * Copyright (c) 1999-2004 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 * Kernel Control domain - allows control connections to
25 * and to read/write data.
27 * Vincent Lubet, 040506
28 * Christophe Allie, 010928
29 * Justin C. Walker, 990319
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/syslog.h>
36 #include <sys/socket.h>
37 #include <sys/socketvar.h>
38 #include <sys/protosw.h>
39 #include <sys/domain.h>
40 #include <sys/malloc.h>
42 #include <sys/sys_domain.h>
43 #include <sys/kern_event.h>
44 #include <sys/kern_control.h>
45 #include <net/if_var.h>
47 #include <mach/vm_types.h>
48 #include <mach/kmod.h>
50 #include <kern/thread.h>
53 * Definitions and vars for we support
56 #define CTL_SENDSIZE (2 * 1024) /* default buffer size */
57 #define CTL_RECVSIZE (8 * 1024) /* default buffer size */
60 * Definitions and vars for we support
63 static u_int32_t ctl_last_id
= 0;
64 static u_int32_t ctl_max
= 256;
65 static u_int32_t ctl_maxunit
= 65536;
66 static lck_grp_attr_t
*ctl_lck_grp_attr
= 0;
67 static lck_attr_t
*ctl_lck_attr
= 0;
68 static lck_grp_t
*ctl_lck_grp
= 0;
69 static lck_mtx_t
*ctl_mtx
;
72 * internal structure maintained for each register controller
79 TAILQ_ENTRY(kctl
) next
; /* controller chain */
81 /* controller information provided when registering */
82 char name
[MAX_KCTL_NAME
]; /* unique nke identifier, provided by DTS */
86 /* misc communication information */
87 u_int32_t flags
; /* support flags */
88 u_int32_t recvbufsize
; /* request more than the default buffer size */
89 u_int32_t sendbufsize
; /* request more than the default buffer size */
91 /* Dispatch functions */
92 ctl_connect_func connect
; /* Make contact */
93 ctl_disconnect_func disconnect
; /* Break contact */
94 ctl_send_func send
; /* Send data to nke */
95 ctl_setopt_func setopt
; /* set kctl configuration */
96 ctl_getopt_func getopt
; /* get kctl configuration */
98 TAILQ_HEAD(, ctl_cb
) kcb_head
;
103 TAILQ_ENTRY(ctl_cb
) next
; /* controller chain */
105 struct socket
*so
; /* controlling socket */
106 struct kctl
*kctl
; /* back pointer to controller */
111 /* all the controllers are chained */
112 TAILQ_HEAD(, kctl
) ctl_head
;
114 static int ctl_attach(struct socket
*, int, struct proc
*);
115 static int ctl_detach(struct socket
*);
116 static int ctl_sofreelastref(struct socket
*so
);
117 static int ctl_connect(struct socket
*, struct sockaddr
*, struct proc
*);
118 static int ctl_disconnect(struct socket
*);
119 static int ctl_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
120 struct ifnet
*ifp
, struct proc
*p
);
121 static int ctl_send(struct socket
*, int, struct mbuf
*,
122 struct sockaddr
*, struct mbuf
*, struct proc
*);
123 static int ctl_ctloutput(struct socket
*, struct sockopt
*);
124 static int ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
);
126 static struct kctl
*ctl_find_by_id(u_int32_t
);
127 static struct kctl
*ctl_find_by_name(const char *);
128 static struct kctl
*ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
);
130 static struct ctl_cb
*kcb_find(struct kctl
*, u_int32_t unit
);
131 static void ctl_post_msg(u_long event_code
, u_int32_t id
);
133 static int ctl_lock(struct socket
*, int, int);
134 static int ctl_unlock(struct socket
*, int, int);
135 static lck_mtx_t
* ctl_getlock(struct socket
*, int);
137 static struct pr_usrreqs ctl_usrreqs
=
139 pru_abort_notsupp
, pru_accept_notsupp
, ctl_attach
, pru_bind_notsupp
,
140 ctl_connect
, pru_connect2_notsupp
, ctl_ioctl
, ctl_detach
,
141 ctl_disconnect
, pru_listen_notsupp
, ctl_peeraddr
,
142 pru_rcvd_notsupp
, pru_rcvoob_notsupp
, ctl_send
,
143 pru_sense_null
, pru_shutdown_notsupp
, pru_sockaddr_notsupp
,
144 sosend
, soreceive
, pru_sopoll_notsupp
147 static struct protosw kctlswk_dgram
=
149 SOCK_DGRAM
, &systemdomain
, SYSPROTO_CONTROL
,
150 PR_ATOMIC
|PR_CONNREQUIRED
|PR_PCBLOCK
,
151 NULL
, NULL
, NULL
, ctl_ctloutput
,
153 NULL
, NULL
, NULL
, NULL
, &ctl_usrreqs
,
154 ctl_lock
, ctl_unlock
, ctl_getlock
, { 0, 0 } , 0, { 0 }
157 static struct protosw kctlswk_stream
=
159 SOCK_STREAM
, &systemdomain
, SYSPROTO_CONTROL
,
160 PR_CONNREQUIRED
|PR_PCBLOCK
,
161 NULL
, NULL
, NULL
, ctl_ctloutput
,
163 NULL
, NULL
, NULL
, NULL
, &ctl_usrreqs
,
164 ctl_lock
, ctl_unlock
, ctl_getlock
, { 0, 0 } , 0, { 0 }
169 * Install the protosw's for the Kernel Control manager.
171 __private_extern__
int
172 kern_control_init(void)
176 ctl_lck_grp_attr
= lck_grp_attr_alloc_init();
177 if (ctl_lck_grp_attr
== 0) {
178 printf(": lck_grp_attr_alloc_init failed\n");
182 lck_grp_attr_setdefault(ctl_lck_grp_attr
);
184 ctl_lck_grp
= lck_grp_alloc_init("Kernel Control Protocol", ctl_lck_grp_attr
);
185 if (ctl_lck_grp
== 0) {
186 printf("kern_control_init: lck_grp_alloc_init failed\n");
191 ctl_lck_attr
= lck_attr_alloc_init();
192 if (ctl_lck_attr
== 0) {
193 printf("kern_control_init: lck_attr_alloc_init failed\n");
197 lck_attr_setdefault(ctl_lck_attr
);
199 ctl_mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
201 printf("kern_control_init: lck_mtx_alloc_init failed\n");
205 TAILQ_INIT(&ctl_head
);
207 error
= net_add_proto(&kctlswk_dgram
, &systemdomain
);
209 log(LOG_WARNING
, "kern_control_init: net_add_proto dgram failed (%d)\n", error
);
211 error
= net_add_proto(&kctlswk_stream
, &systemdomain
);
213 log(LOG_WARNING
, "kern_control_init: net_add_proto stream failed (%d)\n", error
);
219 lck_mtx_free(ctl_mtx
, ctl_lck_grp
);
223 lck_grp_free(ctl_lck_grp
);
226 if (ctl_lck_grp_attr
) {
227 lck_grp_attr_free(ctl_lck_grp_attr
);
228 ctl_lck_grp_attr
= 0;
231 lck_attr_free(ctl_lck_attr
);
239 kcb_delete(struct ctl_cb
*kcb
)
243 lck_mtx_free(kcb
->mtx
, ctl_lck_grp
);
250 * Kernel Controller user-request functions
251 * attach function must exist and succeed
252 * detach not necessary
253 * we need a pcb for the per socket mutex
256 ctl_attach(__unused
struct socket
*so
, __unused
int proto
, __unused
struct proc
*p
)
259 struct ctl_cb
*kcb
= 0;
261 MALLOC(kcb
, struct ctl_cb
*, sizeof(struct ctl_cb
), M_TEMP
, M_WAITOK
);
266 bzero(kcb
, sizeof(struct ctl_cb
));
268 kcb
->mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
269 if (kcb
->mtx
== NULL
) {
274 so
->so_pcb
= (caddr_t
)kcb
;
285 ctl_sofreelastref(struct socket
*so
)
287 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
293 if ((kctl
= kcb
->kctl
) != 0) {
294 lck_mtx_lock(ctl_mtx
);
295 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
296 lck_mtx_lock(ctl_mtx
);
304 ctl_detach(struct socket
*so
)
306 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
311 soisdisconnected(so
);
312 so
->so_flags
|= SOF_PCBCLEARING
;
318 ctl_connect(struct socket
*so
, struct sockaddr
*nam
, __unused
struct proc
*p
)
322 struct sockaddr_ctl sa
;
323 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
326 panic("ctl_connect so_pcb null\n");
328 if (nam
->sa_len
!= sizeof(struct sockaddr_ctl
))
331 bcopy(nam
, &sa
, sizeof(struct sockaddr_ctl
));
333 lck_mtx_lock(ctl_mtx
);
334 kctl
= ctl_find_by_id_unit(sa
.sc_id
, sa
.sc_unit
);
336 lck_mtx_unlock(ctl_mtx
);
340 if (((kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) && (so
->so_type
!= SOCK_STREAM
)) ||
341 (!(kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) && (so
->so_type
!= SOCK_DGRAM
))) {
342 lck_mtx_unlock(ctl_mtx
);
346 if (kctl
->flags
& CTL_FLAG_PRIVILEGED
) {
348 lck_mtx_unlock(ctl_mtx
);
351 if ((error
= proc_suser(p
))) {
352 lck_mtx_unlock(ctl_mtx
);
357 if ((kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) || sa
.sc_unit
!= 0) {
358 if (kcb_find(kctl
, sa
.sc_unit
) != NULL
) {
359 lck_mtx_unlock(ctl_mtx
);
363 u_int32_t unit
= kctl
->lastunit
+ 1;
366 if (unit
== ctl_maxunit
)
368 if (kcb_find(kctl
, unit
) == NULL
) {
369 kctl
->lastunit
= sa
.sc_unit
= unit
;
372 if (unit
++ == kctl
->lastunit
) {
373 lck_mtx_unlock(ctl_mtx
);
379 kcb
->unit
= sa
.sc_unit
;
381 TAILQ_INSERT_TAIL(&kctl
->kcb_head
, kcb
, next
);
382 lck_mtx_unlock(ctl_mtx
);
384 error
= soreserve(so
, kctl
->sendbufsize
, kctl
->recvbufsize
);
389 socket_unlock(so
, 0);
390 error
= (*kctl
->connect
)(kctl
, &sa
, &kcb
->userdata
);
399 soisdisconnected(so
);
400 lck_mtx_lock(ctl_mtx
);
403 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
404 lck_mtx_unlock(ctl_mtx
);
410 ctl_disconnect(struct socket
*so
)
412 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
414 if ((kcb
= (struct ctl_cb
*)so
->so_pcb
)) {
415 struct kctl
*kctl
= kcb
->kctl
;
417 if (kctl
&& kctl
->disconnect
) {
418 socket_unlock(so
, 0);
419 (*kctl
->disconnect
)(kctl
, kcb
->unit
, kcb
->userdata
);
422 lck_mtx_lock(ctl_mtx
);
425 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
426 soisdisconnected(so
);
427 lck_mtx_unlock(ctl_mtx
);
433 ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
435 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
437 struct sockaddr_ctl sc
;
439 if (kcb
== NULL
) /* sanity check */
442 if ((kctl
= kcb
->kctl
) == NULL
)
445 bzero(&sc
, sizeof(struct sockaddr_ctl
));
446 sc
.sc_len
= sizeof(struct sockaddr_ctl
);
447 sc
.sc_family
= AF_SYSTEM
;
448 sc
.ss_sysaddr
= AF_SYS_CONTROL
;
450 sc
.sc_unit
= kcb
->unit
;
452 *nam
= dup_sockaddr((struct sockaddr
*)&sc
, 1);
458 ctl_send(struct socket
*so
, int flags
, struct mbuf
*m
,
459 __unused
struct sockaddr
*addr
, __unused
struct mbuf
*control
,
460 __unused
struct proc
*p
)
463 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
466 if (kcb
== NULL
) /* sanity check */
469 if ((kctl
= kcb
->kctl
) == NULL
)
473 socket_unlock(so
, 0);
474 error
= (*kctl
->send
)(kctl
, kcb
->unit
, kcb
->userdata
, m
, flags
);
481 ctl_enqueuembuf(void *kctlref
, u_int32_t unit
, struct mbuf
*m
, u_int32_t flags
)
486 struct kctl
*kctl
= (struct kctl
*)kctlref
;
491 kcb
= kcb_find(kctl
, unit
);
495 so
= (struct socket
*)kcb
->so
;
500 if (sbspace(&so
->so_rcv
) < m
->m_pkthdr
.len
) {
504 if ((flags
& CTL_DATA_EOR
))
506 if (sbappend(&so
->so_rcv
, m
) && (flags
& CTL_DATA_NOWAKEUP
) == 0)
509 socket_unlock(so
, 1);
514 ctl_enqueuedata(void *kctlref
, u_int32_t unit
, void *data
, size_t len
, u_int32_t flags
)
520 struct kctl
*kctl
= (struct kctl
*)kctlref
;
521 unsigned int num_needed
;
528 kcb
= kcb_find(kctl
, unit
);
532 so
= (struct socket
*)kcb
->so
;
537 if ((size_t)sbspace(&so
->so_rcv
) < len
) {
543 m
= m_allocpacket_internal(&num_needed
, len
, NULL
, M_NOWAIT
, 1, 0);
545 printf("ctl_enqueuedata: m_allocpacket_internal(%lu) failed\n", len
);
550 for (n
= m
; n
!= NULL
; n
= n
->m_next
) {
551 size_t mlen
= mbuf_maxlen(n
);
553 if (mlen
+ curlen
> len
)
556 bcopy((char *)data
+ curlen
, n
->m_data
, mlen
);
559 mbuf_pkthdr_setlen(m
, curlen
);
561 if ((flags
& CTL_DATA_EOR
))
563 if (sbappend(&so
->so_rcv
, m
) && (flags
& CTL_DATA_NOWAKEUP
) == 0)
566 socket_unlock(so
, 1);
572 ctl_getenqueuespace(kern_ctl_ref kctlref
, u_int32_t unit
, size_t *space
)
575 struct kctl
*kctl
= (struct kctl
*)kctlref
;
578 if (kctlref
== NULL
|| space
== NULL
)
581 kcb
= kcb_find(kctl
, unit
);
585 so
= (struct socket
*)kcb
->so
;
590 *space
= sbspace(&so
->so_rcv
);
591 socket_unlock(so
, 1);
597 ctl_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
599 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
605 if (sopt
->sopt_level
!= SYSPROTO_CONTROL
) {
609 if (kcb
== NULL
) /* sanity check */
612 if ((kctl
= kcb
->kctl
) == NULL
)
615 switch (sopt
->sopt_dir
) {
617 if (kctl
->setopt
== NULL
)
619 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
, M_WAITOK
);
622 error
= sooptcopyin(sopt
, data
, sopt
->sopt_valsize
, sopt
->sopt_valsize
);
624 socket_unlock(so
, 0);
625 error
= (*kctl
->setopt
)(kcb
->kctl
, kcb
->unit
, kcb
->userdata
, sopt
->sopt_name
,
626 data
, sopt
->sopt_valsize
);
633 if (kctl
->getopt
== NULL
)
636 if (sopt
->sopt_valsize
&& sopt
->sopt_val
) {
637 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
, M_WAITOK
);
640 /* 4108337 - copy in data for get socket option */
641 error
= sooptcopyin(sopt
, data
, sopt
->sopt_valsize
, sopt
->sopt_valsize
);
643 len
= sopt
->sopt_valsize
;
644 socket_unlock(so
, 0);
645 error
= (*kctl
->getopt
)(kcb
->kctl
, kcb
->unit
, kcb
->userdata
, sopt
->sopt_name
,
650 error
= sooptcopyout(sopt
, data
, len
);
652 sopt
->sopt_valsize
= len
;
662 ctl_ioctl(__unused
struct socket
*so
, u_long cmd
, caddr_t data
,
663 __unused
struct ifnet
*ifp
, __unused
struct proc
*p
)
668 /* get the number of controllers */
673 lck_mtx_lock(ctl_mtx
);
674 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
676 lck_mtx_unlock(ctl_mtx
);
678 *(u_int32_t
*)data
= n
;
683 struct ctl_info
*ctl_info
= (struct ctl_info
*)data
;
684 struct kctl
*kctl
= 0;
685 size_t name_len
= strlen(ctl_info
->ctl_name
);
687 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
) {
691 lck_mtx_lock(ctl_mtx
);
692 kctl
= ctl_find_by_name(ctl_info
->ctl_name
);
693 lck_mtx_unlock(ctl_mtx
);
698 ctl_info
->ctl_id
= kctl
->id
;
703 /* add controls to get list of NKEs */
711 * Register/unregister a NKE
714 ctl_register(struct kern_ctl_reg
*userkctl
, kern_ctl_ref
*kctlref
)
716 struct kctl
*kctl
= 0;
721 if (userkctl
== NULL
) /* sanity check */
723 if (userkctl
->ctl_connect
== NULL
)
725 name_len
= strlen(userkctl
->ctl_name
);
726 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
)
729 MALLOC(kctl
, struct kctl
*, sizeof(*kctl
), M_TEMP
, M_WAITOK
);
732 bzero((char *)kctl
, sizeof(*kctl
));
734 lck_mtx_lock(ctl_mtx
);
736 if ((userkctl
->ctl_flags
& CTL_FLAG_REG_ID_UNIT
) == 0) {
737 if (ctl_find_by_name(userkctl
->ctl_name
) != NULL
) {
738 lck_mtx_unlock(ctl_mtx
);
742 for (n
= 0, id
= ctl_last_id
+ 1; n
< ctl_max
; id
++, n
++) {
747 if (ctl_find_by_id(id
) == 0)
751 lck_mtx_unlock(ctl_mtx
);
755 userkctl
->ctl_id
=id
;
759 if (ctl_find_by_id_unit(userkctl
->ctl_id
, userkctl
->ctl_unit
) != NULL
) {
760 lck_mtx_unlock(ctl_mtx
);
764 kctl
->id
= userkctl
->ctl_id
;
765 kctl
->reg_unit
= userkctl
->ctl_unit
;
767 strcpy(kctl
->name
, userkctl
->ctl_name
);
768 kctl
->flags
= userkctl
->ctl_flags
;
770 /* Let the caller know the default send and receive sizes */
771 if (userkctl
->ctl_sendsize
== 0)
772 userkctl
->ctl_sendsize
= CTL_SENDSIZE
;
773 kctl
->sendbufsize
= userkctl
->ctl_sendsize
;
775 if (kctl
->recvbufsize
== 0)
776 userkctl
->ctl_recvsize
= CTL_RECVSIZE
;
777 kctl
->recvbufsize
= userkctl
->ctl_recvsize
;
779 kctl
->connect
= userkctl
->ctl_connect
;
780 kctl
->disconnect
= userkctl
->ctl_disconnect
;
781 kctl
->send
= userkctl
->ctl_send
;
782 kctl
->setopt
= userkctl
->ctl_setopt
;
783 kctl
->getopt
= userkctl
->ctl_getopt
;
785 TAILQ_INIT(&kctl
->kcb_head
);
787 TAILQ_INSERT_TAIL(&ctl_head
, kctl
, next
);
790 lck_mtx_unlock(ctl_mtx
);
794 ctl_post_msg(KEV_CTL_REGISTERED
, kctl
->id
);
799 ctl_deregister(void *kctlref
)
803 if (kctlref
== NULL
) /* sanity check */
806 lck_mtx_lock(ctl_mtx
);
807 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
808 if (kctl
== (struct kctl
*)kctlref
)
811 if (kctl
!= (struct kctl
*)kctlref
) {
812 lck_mtx_unlock(ctl_mtx
);
815 if (!TAILQ_EMPTY(&kctl
->kcb_head
)) {
816 lck_mtx_unlock(ctl_mtx
);
820 TAILQ_REMOVE(&ctl_head
, kctl
, next
);
823 lck_mtx_unlock(ctl_mtx
);
825 ctl_post_msg(KEV_CTL_DEREGISTERED
, kctl
->id
);
831 * Must be called with global lock taked
834 ctl_find_by_id(u_int32_t id
)
838 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
846 * Must be called with global ctl_mtx lock taked
849 ctl_find_by_name(const char *name
)
853 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
854 if (strcmp(kctl
->name
, name
) == 0)
861 * Must be called with global ctl_mtx lock taked
865 ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
)
869 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
870 if (kctl
->id
== id
&& (kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) == 0)
872 else if (kctl
->id
== id
&& kctl
->reg_unit
== unit
)
879 * Must be called with kernel controller lock taken
881 static struct ctl_cb
*
882 kcb_find(struct kctl
*kctl
, u_int32_t unit
)
886 TAILQ_FOREACH(kcb
, &kctl
->kcb_head
, next
)
887 if ((kcb
->unit
== unit
))
894 * Must be called witout lock
897 ctl_post_msg(u_long event_code
, u_int32_t id
)
899 struct ctl_event_data ctl_ev_data
;
900 struct kev_msg ev_msg
;
902 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
904 ev_msg
.kev_class
= KEV_SYSTEM_CLASS
;
905 ev_msg
.kev_subclass
= KEV_CTL_SUBCLASS
;
906 ev_msg
.event_code
= event_code
;
908 /* common nke subclass data */
909 bzero(&ctl_ev_data
, sizeof(ctl_ev_data
));
910 ctl_ev_data
.ctl_id
= id
;
911 ev_msg
.dv
[0].data_ptr
= &ctl_ev_data
;
912 ev_msg
.dv
[0].data_length
= sizeof(ctl_ev_data
);
914 ev_msg
.dv
[1].data_length
= 0;
916 kev_post_msg(&ev_msg
);
920 ctl_lock(struct socket
*so
, int refcount
, int lr
)
925 __asm__
volatile("mflr %0" : "=r" (lr_saved
));
931 lck_mtx_lock(((struct ctl_cb
*)so
->so_pcb
)->mtx
);
933 panic("ctl_lock: so=%x NO PCB! lr=%x\n", so
, lr_saved
);
934 lck_mtx_lock(so
->so_proto
->pr_domain
->dom_mtx
);
937 if (so
->so_usecount
< 0)
938 panic("ctl_lock: so=%x so_pcb=%x lr=%x ref=%x\n",
939 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
);
943 so
->reserved3
= (void *)lr_saved
;
948 ctl_unlock(struct socket
*so
, int refcount
, int lr
)
951 lck_mtx_t
* mutex_held
;
955 __asm__
volatile("mflr %0" : "=r" (lr_saved
));
960 #ifdef MORE_KCTLLOCK_DEBUG
961 printf("ctl_unlock: so=%x sopcb=%x lock=%x ref=%x lr=%x\n",
962 so
, so
->so_pcb
, ((struct ctl_cb
*)so
->so_pcb
)->mtx
, so
->so_usecount
, lr_saved
);
967 if (so
->so_usecount
< 0)
968 panic("ctl_unlock: so=%x usecount=%x\n", so
, so
->so_usecount
);
969 if (so
->so_pcb
== NULL
) {
970 panic("ctl_unlock: so=%x NO PCB usecount=%x lr=%x\n", so
, so
->so_usecount
, lr_saved
);
971 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
973 mutex_held
= ((struct ctl_cb
*)so
->so_pcb
)->mtx
;
975 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
976 lck_mtx_unlock(mutex_held
);
977 so
->reserved4
= (void *)lr_saved
;
979 if (so
->so_usecount
== 0)
980 ctl_sofreelastref(so
);
986 ctl_getlock(struct socket
*so
, __unused
int locktype
)
988 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
991 if (so
->so_usecount
< 0)
992 panic("ctl_getlock: so=%x usecount=%x\n", so
, so
->so_usecount
);
995 panic("ctl_getlock: so=%x NULL so_pcb\n", so
);
996 return (so
->so_proto
->pr_domain
->dom_mtx
);