2 * Copyright (c) 1999-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * Kernel Control domain - allows control connections to
26 * and to read/write data.
28 * Vincent Lubet, 040506
29 * Christophe Allie, 010928
30 * Justin C. Walker, 990319
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/syslog.h>
37 #include <sys/socket.h>
38 #include <sys/socketvar.h>
39 #include <sys/protosw.h>
40 #include <sys/domain.h>
41 #include <sys/malloc.h>
43 #include <sys/sys_domain.h>
44 #include <sys/kern_event.h>
45 #include <sys/kern_control.h>
46 #include <net/if_var.h>
48 #include <mach/vm_types.h>
49 #include <mach/kmod.h>
51 #include <kern/thread.h>
54 * Definitions and vars for we support
57 #define CTL_SENDSIZE (2 * 1024) /* default buffer size */
58 #define CTL_RECVSIZE (8 * 1024) /* default buffer size */
61 * Definitions and vars for we support
64 static u_int32_t ctl_last_id
= 0;
65 static u_int32_t ctl_max
= 256;
66 static u_int32_t ctl_maxunit
= 65536;
67 static lck_grp_attr_t
*ctl_lck_grp_attr
= 0;
68 static lck_attr_t
*ctl_lck_attr
= 0;
69 static lck_grp_t
*ctl_lck_grp
= 0;
70 static lck_mtx_t
*ctl_mtx
;
73 * internal structure maintained for each register controller
80 TAILQ_ENTRY(kctl
) next
; /* controller chain */
82 /* controller information provided when registering */
83 char name
[MAX_KCTL_NAME
]; /* unique nke identifier, provided by DTS */
87 /* misc communication information */
88 u_int32_t flags
; /* support flags */
89 u_int32_t recvbufsize
; /* request more than the default buffer size */
90 u_int32_t sendbufsize
; /* request more than the default buffer size */
92 /* Dispatch functions */
93 ctl_connect_func connect
; /* Make contact */
94 ctl_disconnect_func disconnect
; /* Break contact */
95 ctl_send_func send
; /* Send data to nke */
96 ctl_setopt_func setopt
; /* set kctl configuration */
97 ctl_getopt_func getopt
; /* get kctl configuration */
99 TAILQ_HEAD(, ctl_cb
) kcb_head
;
104 TAILQ_ENTRY(ctl_cb
) next
; /* controller chain */
106 struct socket
*so
; /* controlling socket */
107 struct kctl
*kctl
; /* back pointer to controller */
112 /* all the controllers are chained */
113 TAILQ_HEAD(, kctl
) ctl_head
;
115 static int ctl_attach(struct socket
*, int, struct proc
*);
116 static int ctl_detach(struct socket
*);
117 static int ctl_sofreelastref(struct socket
*so
);
118 static int ctl_connect(struct socket
*, struct sockaddr
*, struct proc
*);
119 static int ctl_disconnect(struct socket
*);
120 static int ctl_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
121 struct ifnet
*ifp
, struct proc
*p
);
122 static int ctl_send(struct socket
*, int, struct mbuf
*,
123 struct sockaddr
*, struct mbuf
*, struct proc
*);
124 static int ctl_ctloutput(struct socket
*, struct sockopt
*);
125 static int ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
);
127 static struct kctl
*ctl_find_by_id(u_int32_t
);
128 static struct kctl
*ctl_find_by_name(const char *);
129 static struct kctl
*ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
);
131 static struct ctl_cb
*kcb_find(struct kctl
*, u_int32_t unit
);
132 static void ctl_post_msg(u_long event_code
, u_int32_t id
);
134 static int ctl_lock(struct socket
*, int, int);
135 static int ctl_unlock(struct socket
*, int, int);
136 static lck_mtx_t
* ctl_getlock(struct socket
*, int);
138 static struct pr_usrreqs ctl_usrreqs
=
140 pru_abort_notsupp
, pru_accept_notsupp
, ctl_attach
, pru_bind_notsupp
,
141 ctl_connect
, pru_connect2_notsupp
, ctl_ioctl
, ctl_detach
,
142 ctl_disconnect
, pru_listen_notsupp
, ctl_peeraddr
,
143 pru_rcvd_notsupp
, pru_rcvoob_notsupp
, ctl_send
,
144 pru_sense_null
, pru_shutdown_notsupp
, pru_sockaddr_notsupp
,
145 sosend
, soreceive
, pru_sopoll_notsupp
148 static struct protosw kctlswk_dgram
=
150 SOCK_DGRAM
, &systemdomain
, SYSPROTO_CONTROL
,
151 PR_ATOMIC
|PR_CONNREQUIRED
|PR_PCBLOCK
,
152 NULL
, NULL
, NULL
, ctl_ctloutput
,
154 NULL
, NULL
, NULL
, NULL
, &ctl_usrreqs
,
155 ctl_lock
, ctl_unlock
, ctl_getlock
, { 0, 0 } , 0, { 0 }
158 static struct protosw kctlswk_stream
=
160 SOCK_STREAM
, &systemdomain
, SYSPROTO_CONTROL
,
161 PR_CONNREQUIRED
|PR_PCBLOCK
,
162 NULL
, NULL
, NULL
, ctl_ctloutput
,
164 NULL
, NULL
, NULL
, NULL
, &ctl_usrreqs
,
165 ctl_lock
, ctl_unlock
, ctl_getlock
, { 0, 0 } , 0, { 0 }
170 * Install the protosw's for the Kernel Control manager.
172 __private_extern__
int
173 kern_control_init(void)
177 ctl_lck_grp_attr
= lck_grp_attr_alloc_init();
178 if (ctl_lck_grp_attr
== 0) {
179 printf(": lck_grp_attr_alloc_init failed\n");
183 lck_grp_attr_setdefault(ctl_lck_grp_attr
);
185 ctl_lck_grp
= lck_grp_alloc_init("Kernel Control Protocol", ctl_lck_grp_attr
);
186 if (ctl_lck_grp
== 0) {
187 printf("kern_control_init: lck_grp_alloc_init failed\n");
192 ctl_lck_attr
= lck_attr_alloc_init();
193 if (ctl_lck_attr
== 0) {
194 printf("kern_control_init: lck_attr_alloc_init failed\n");
198 lck_attr_setdefault(ctl_lck_attr
);
200 ctl_mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
202 printf("kern_control_init: lck_mtx_alloc_init failed\n");
206 TAILQ_INIT(&ctl_head
);
208 error
= net_add_proto(&kctlswk_dgram
, &systemdomain
);
210 log(LOG_WARNING
, "kern_control_init: net_add_proto dgram failed (%d)\n", error
);
212 error
= net_add_proto(&kctlswk_stream
, &systemdomain
);
214 log(LOG_WARNING
, "kern_control_init: net_add_proto stream failed (%d)\n", error
);
220 lck_mtx_free(ctl_mtx
, ctl_lck_grp
);
224 lck_grp_free(ctl_lck_grp
);
227 if (ctl_lck_grp_attr
) {
228 lck_grp_attr_free(ctl_lck_grp_attr
);
229 ctl_lck_grp_attr
= 0;
232 lck_attr_free(ctl_lck_attr
);
240 kcb_delete(struct ctl_cb
*kcb
)
244 lck_mtx_free(kcb
->mtx
, ctl_lck_grp
);
251 * Kernel Controller user-request functions
252 * attach function must exist and succeed
253 * detach not necessary
254 * we need a pcb for the per socket mutex
257 ctl_attach(__unused
struct socket
*so
, __unused
int proto
, __unused
struct proc
*p
)
260 struct ctl_cb
*kcb
= 0;
262 MALLOC(kcb
, struct ctl_cb
*, sizeof(struct ctl_cb
), M_TEMP
, M_WAITOK
);
267 bzero(kcb
, sizeof(struct ctl_cb
));
269 kcb
->mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
270 if (kcb
->mtx
== NULL
) {
275 so
->so_pcb
= (caddr_t
)kcb
;
286 ctl_sofreelastref(struct socket
*so
)
288 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
294 if ((kctl
= kcb
->kctl
) != 0) {
295 lck_mtx_lock(ctl_mtx
);
296 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
297 lck_mtx_lock(ctl_mtx
);
305 ctl_detach(struct socket
*so
)
307 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
312 soisdisconnected(so
);
313 so
->so_flags
|= SOF_PCBCLEARING
;
319 ctl_connect(struct socket
*so
, struct sockaddr
*nam
, __unused
struct proc
*p
)
323 struct sockaddr_ctl sa
;
324 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
327 panic("ctl_connect so_pcb null\n");
329 if (nam
->sa_len
!= sizeof(struct sockaddr_ctl
))
332 bcopy(nam
, &sa
, sizeof(struct sockaddr_ctl
));
334 lck_mtx_lock(ctl_mtx
);
335 kctl
= ctl_find_by_id_unit(sa
.sc_id
, sa
.sc_unit
);
337 lck_mtx_unlock(ctl_mtx
);
341 if (((kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) && (so
->so_type
!= SOCK_STREAM
)) ||
342 (!(kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) && (so
->so_type
!= SOCK_DGRAM
))) {
343 lck_mtx_unlock(ctl_mtx
);
347 if (kctl
->flags
& CTL_FLAG_PRIVILEGED
) {
349 lck_mtx_unlock(ctl_mtx
);
352 if ((error
= proc_suser(p
))) {
353 lck_mtx_unlock(ctl_mtx
);
358 if ((kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) || sa
.sc_unit
!= 0) {
359 if (kcb_find(kctl
, sa
.sc_unit
) != NULL
) {
360 lck_mtx_unlock(ctl_mtx
);
364 u_int32_t unit
= kctl
->lastunit
+ 1;
367 if (unit
== ctl_maxunit
)
369 if (kcb_find(kctl
, unit
) == NULL
) {
370 kctl
->lastunit
= sa
.sc_unit
= unit
;
373 if (unit
++ == kctl
->lastunit
) {
374 lck_mtx_unlock(ctl_mtx
);
380 kcb
->unit
= sa
.sc_unit
;
382 TAILQ_INSERT_TAIL(&kctl
->kcb_head
, kcb
, next
);
383 lck_mtx_unlock(ctl_mtx
);
385 error
= soreserve(so
, kctl
->sendbufsize
, kctl
->recvbufsize
);
390 socket_unlock(so
, 0);
391 error
= (*kctl
->connect
)(kctl
, &sa
, &kcb
->userdata
);
400 soisdisconnected(so
);
401 lck_mtx_lock(ctl_mtx
);
404 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
405 lck_mtx_unlock(ctl_mtx
);
411 ctl_disconnect(struct socket
*so
)
413 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
415 if ((kcb
= (struct ctl_cb
*)so
->so_pcb
)) {
416 struct kctl
*kctl
= kcb
->kctl
;
418 if (kctl
&& kctl
->disconnect
) {
419 socket_unlock(so
, 0);
420 (*kctl
->disconnect
)(kctl
, kcb
->unit
, kcb
->userdata
);
423 lck_mtx_lock(ctl_mtx
);
426 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
427 soisdisconnected(so
);
428 lck_mtx_unlock(ctl_mtx
);
434 ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
436 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
438 struct sockaddr_ctl sc
;
440 if (kcb
== NULL
) /* sanity check */
443 if ((kctl
= kcb
->kctl
) == NULL
)
446 bzero(&sc
, sizeof(struct sockaddr_ctl
));
447 sc
.sc_len
= sizeof(struct sockaddr_ctl
);
448 sc
.sc_family
= AF_SYSTEM
;
449 sc
.ss_sysaddr
= AF_SYS_CONTROL
;
451 sc
.sc_unit
= kcb
->unit
;
453 *nam
= dup_sockaddr((struct sockaddr
*)&sc
, 1);
459 ctl_send(struct socket
*so
, int flags
, struct mbuf
*m
,
460 __unused
struct sockaddr
*addr
, __unused
struct mbuf
*control
,
461 __unused
struct proc
*p
)
464 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
467 if (kcb
== NULL
) /* sanity check */
470 if ((kctl
= kcb
->kctl
) == NULL
)
474 socket_unlock(so
, 0);
475 error
= (*kctl
->send
)(kctl
, kcb
->unit
, kcb
->userdata
, m
, flags
);
482 ctl_enqueuembuf(void *kctlref
, u_int32_t unit
, struct mbuf
*m
, u_int32_t flags
)
487 struct kctl
*kctl
= (struct kctl
*)kctlref
;
492 kcb
= kcb_find(kctl
, unit
);
496 so
= (struct socket
*)kcb
->so
;
501 if (sbspace(&so
->so_rcv
) < m
->m_pkthdr
.len
) {
505 if ((flags
& CTL_DATA_EOR
))
507 if (sbappend(&so
->so_rcv
, m
) && (flags
& CTL_DATA_NOWAKEUP
) == 0)
510 socket_unlock(so
, 1);
515 ctl_enqueuedata(void *kctlref
, u_int32_t unit
, void *data
, size_t len
, u_int32_t flags
)
521 struct kctl
*kctl
= (struct kctl
*)kctlref
;
522 unsigned int num_needed
;
529 kcb
= kcb_find(kctl
, unit
);
533 so
= (struct socket
*)kcb
->so
;
538 if ((size_t)sbspace(&so
->so_rcv
) < len
) {
544 m
= m_allocpacket_internal(&num_needed
, len
, NULL
, M_NOWAIT
, 1, 0);
546 printf("ctl_enqueuedata: m_allocpacket_internal(%lu) failed\n", len
);
551 for (n
= m
; n
!= NULL
; n
= n
->m_next
) {
552 size_t mlen
= mbuf_maxlen(n
);
554 if (mlen
+ curlen
> len
)
557 bcopy((char *)data
+ curlen
, n
->m_data
, mlen
);
560 mbuf_pkthdr_setlen(m
, curlen
);
562 if ((flags
& CTL_DATA_EOR
))
564 if (sbappend(&so
->so_rcv
, m
) && (flags
& CTL_DATA_NOWAKEUP
) == 0)
567 socket_unlock(so
, 1);
573 ctl_getenqueuespace(kern_ctl_ref kctlref
, u_int32_t unit
, size_t *space
)
576 struct kctl
*kctl
= (struct kctl
*)kctlref
;
579 if (kctlref
== NULL
|| space
== NULL
)
582 kcb
= kcb_find(kctl
, unit
);
586 so
= (struct socket
*)kcb
->so
;
591 *space
= sbspace(&so
->so_rcv
);
592 socket_unlock(so
, 1);
598 ctl_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
600 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
606 if (sopt
->sopt_level
!= SYSPROTO_CONTROL
) {
610 if (kcb
== NULL
) /* sanity check */
613 if ((kctl
= kcb
->kctl
) == NULL
)
616 switch (sopt
->sopt_dir
) {
618 if (kctl
->setopt
== NULL
)
620 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
, M_WAITOK
);
623 error
= sooptcopyin(sopt
, data
, sopt
->sopt_valsize
, sopt
->sopt_valsize
);
625 socket_unlock(so
, 0);
626 error
= (*kctl
->setopt
)(kcb
->kctl
, kcb
->unit
, kcb
->userdata
, sopt
->sopt_name
,
627 data
, sopt
->sopt_valsize
);
634 if (kctl
->getopt
== NULL
)
637 if (sopt
->sopt_valsize
&& sopt
->sopt_val
) {
638 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
, M_WAITOK
);
641 /* 4108337 - copy in data for get socket option */
642 error
= sooptcopyin(sopt
, data
, sopt
->sopt_valsize
, sopt
->sopt_valsize
);
644 len
= sopt
->sopt_valsize
;
645 socket_unlock(so
, 0);
646 error
= (*kctl
->getopt
)(kcb
->kctl
, kcb
->unit
, kcb
->userdata
, sopt
->sopt_name
,
651 error
= sooptcopyout(sopt
, data
, len
);
653 sopt
->sopt_valsize
= len
;
663 ctl_ioctl(__unused
struct socket
*so
, u_long cmd
, caddr_t data
,
664 __unused
struct ifnet
*ifp
, __unused
struct proc
*p
)
669 /* get the number of controllers */
674 lck_mtx_lock(ctl_mtx
);
675 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
677 lck_mtx_unlock(ctl_mtx
);
679 *(u_int32_t
*)data
= n
;
684 struct ctl_info
*ctl_info
= (struct ctl_info
*)data
;
685 struct kctl
*kctl
= 0;
686 size_t name_len
= strlen(ctl_info
->ctl_name
);
688 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
) {
692 lck_mtx_lock(ctl_mtx
);
693 kctl
= ctl_find_by_name(ctl_info
->ctl_name
);
694 lck_mtx_unlock(ctl_mtx
);
699 ctl_info
->ctl_id
= kctl
->id
;
704 /* add controls to get list of NKEs */
712 * Register/unregister a NKE
715 ctl_register(struct kern_ctl_reg
*userkctl
, kern_ctl_ref
*kctlref
)
717 struct kctl
*kctl
= 0;
722 if (userkctl
== NULL
) /* sanity check */
724 if (userkctl
->ctl_connect
== NULL
)
726 name_len
= strlen(userkctl
->ctl_name
);
727 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
)
730 MALLOC(kctl
, struct kctl
*, sizeof(*kctl
), M_TEMP
, M_WAITOK
);
733 bzero((char *)kctl
, sizeof(*kctl
));
735 lck_mtx_lock(ctl_mtx
);
737 if ((userkctl
->ctl_flags
& CTL_FLAG_REG_ID_UNIT
) == 0) {
738 if (ctl_find_by_name(userkctl
->ctl_name
) != NULL
) {
739 lck_mtx_unlock(ctl_mtx
);
743 for (n
= 0, id
= ctl_last_id
+ 1; n
< ctl_max
; id
++, n
++) {
748 if (ctl_find_by_id(id
) == 0)
752 lck_mtx_unlock(ctl_mtx
);
756 userkctl
->ctl_id
=id
;
760 if (ctl_find_by_id_unit(userkctl
->ctl_id
, userkctl
->ctl_unit
) != NULL
) {
761 lck_mtx_unlock(ctl_mtx
);
765 kctl
->id
= userkctl
->ctl_id
;
766 kctl
->reg_unit
= userkctl
->ctl_unit
;
768 strcpy(kctl
->name
, userkctl
->ctl_name
);
769 kctl
->flags
= userkctl
->ctl_flags
;
771 /* Let the caller know the default send and receive sizes */
772 if (userkctl
->ctl_sendsize
== 0)
773 userkctl
->ctl_sendsize
= CTL_SENDSIZE
;
774 kctl
->sendbufsize
= userkctl
->ctl_sendsize
;
776 if (userkctl
->ctl_recvsize
== 0)
777 userkctl
->ctl_recvsize
= CTL_RECVSIZE
;
778 kctl
->recvbufsize
= userkctl
->ctl_recvsize
;
780 kctl
->connect
= userkctl
->ctl_connect
;
781 kctl
->disconnect
= userkctl
->ctl_disconnect
;
782 kctl
->send
= userkctl
->ctl_send
;
783 kctl
->setopt
= userkctl
->ctl_setopt
;
784 kctl
->getopt
= userkctl
->ctl_getopt
;
786 TAILQ_INIT(&kctl
->kcb_head
);
788 TAILQ_INSERT_TAIL(&ctl_head
, kctl
, next
);
791 lck_mtx_unlock(ctl_mtx
);
795 ctl_post_msg(KEV_CTL_REGISTERED
, kctl
->id
);
800 ctl_deregister(void *kctlref
)
804 if (kctlref
== NULL
) /* sanity check */
807 lck_mtx_lock(ctl_mtx
);
808 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
809 if (kctl
== (struct kctl
*)kctlref
)
812 if (kctl
!= (struct kctl
*)kctlref
) {
813 lck_mtx_unlock(ctl_mtx
);
816 if (!TAILQ_EMPTY(&kctl
->kcb_head
)) {
817 lck_mtx_unlock(ctl_mtx
);
821 TAILQ_REMOVE(&ctl_head
, kctl
, next
);
824 lck_mtx_unlock(ctl_mtx
);
826 ctl_post_msg(KEV_CTL_DEREGISTERED
, kctl
->id
);
832 * Must be called with global lock taked
835 ctl_find_by_id(u_int32_t id
)
839 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
847 * Must be called with global ctl_mtx lock taked
850 ctl_find_by_name(const char *name
)
854 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
855 if (strcmp(kctl
->name
, name
) == 0)
862 * Must be called with global ctl_mtx lock taked
866 ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
)
870 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
871 if (kctl
->id
== id
&& (kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) == 0)
873 else if (kctl
->id
== id
&& kctl
->reg_unit
== unit
)
880 * Must be called with kernel controller lock taken
882 static struct ctl_cb
*
883 kcb_find(struct kctl
*kctl
, u_int32_t unit
)
887 TAILQ_FOREACH(kcb
, &kctl
->kcb_head
, next
)
888 if ((kcb
->unit
== unit
))
895 * Must be called witout lock
898 ctl_post_msg(u_long event_code
, u_int32_t id
)
900 struct ctl_event_data ctl_ev_data
;
901 struct kev_msg ev_msg
;
903 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
905 ev_msg
.kev_class
= KEV_SYSTEM_CLASS
;
906 ev_msg
.kev_subclass
= KEV_CTL_SUBCLASS
;
907 ev_msg
.event_code
= event_code
;
909 /* common nke subclass data */
910 bzero(&ctl_ev_data
, sizeof(ctl_ev_data
));
911 ctl_ev_data
.ctl_id
= id
;
912 ev_msg
.dv
[0].data_ptr
= &ctl_ev_data
;
913 ev_msg
.dv
[0].data_length
= sizeof(ctl_ev_data
);
915 ev_msg
.dv
[1].data_length
= 0;
917 kev_post_msg(&ev_msg
);
921 ctl_lock(struct socket
*so
, int refcount
, int lr
)
926 __asm__
volatile("mflr %0" : "=r" (lr_saved
));
932 lck_mtx_lock(((struct ctl_cb
*)so
->so_pcb
)->mtx
);
934 panic("ctl_lock: so=%x NO PCB! lr=%x\n", so
, lr_saved
);
935 lck_mtx_lock(so
->so_proto
->pr_domain
->dom_mtx
);
938 if (so
->so_usecount
< 0)
939 panic("ctl_lock: so=%x so_pcb=%x lr=%x ref=%x\n",
940 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
);
944 so
->reserved3
= (void *)lr_saved
;
949 ctl_unlock(struct socket
*so
, int refcount
, int lr
)
952 lck_mtx_t
* mutex_held
;
956 __asm__
volatile("mflr %0" : "=r" (lr_saved
));
961 #ifdef MORE_KCTLLOCK_DEBUG
962 printf("ctl_unlock: so=%x sopcb=%x lock=%x ref=%x lr=%x\n",
963 so
, so
->so_pcb
, ((struct ctl_cb
*)so
->so_pcb
)->mtx
, so
->so_usecount
, lr_saved
);
968 if (so
->so_usecount
< 0)
969 panic("ctl_unlock: so=%x usecount=%x\n", so
, so
->so_usecount
);
970 if (so
->so_pcb
== NULL
) {
971 panic("ctl_unlock: so=%x NO PCB usecount=%x lr=%x\n", so
, so
->so_usecount
, lr_saved
);
972 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
974 mutex_held
= ((struct ctl_cb
*)so
->so_pcb
)->mtx
;
976 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
977 lck_mtx_unlock(mutex_held
);
978 so
->reserved4
= (void *)lr_saved
;
980 if (so
->so_usecount
== 0)
981 ctl_sofreelastref(so
);
987 ctl_getlock(struct socket
*so
, __unused
int locktype
)
989 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
992 if (so
->so_usecount
< 0)
993 panic("ctl_getlock: so=%x usecount=%x\n", so
, so
->so_usecount
);
996 panic("ctl_getlock: so=%x NULL so_pcb\n", so
);
997 return (so
->so_proto
->pr_domain
->dom_mtx
);