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 /* all the controllers are chained */
73 TAILQ_HEAD(, kctl
) ctl_head
;
75 static int ctl_attach(struct socket
*, int, struct proc
*);
76 static int ctl_detach(struct socket
*);
77 static int ctl_sofreelastref(struct socket
*so
);
78 static int ctl_connect(struct socket
*, struct sockaddr
*, struct proc
*);
79 static int ctl_disconnect(struct socket
*);
80 static int ctl_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
81 struct ifnet
*ifp
, struct proc
*p
);
82 static int ctl_send(struct socket
*, int, struct mbuf
*,
83 struct sockaddr
*, struct mbuf
*, struct proc
*);
84 static int ctl_ctloutput(struct socket
*, struct sockopt
*);
85 static int ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
);
87 static struct kctl
*ctl_find_by_id(u_int32_t
);
88 static struct kctl
*ctl_find_by_name(const char *);
89 static struct kctl
*ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
);
91 static struct ctl_cb
*kcb_find(struct kctl
*, u_int32_t unit
);
92 static void ctl_post_msg(u_long event_code
, u_int32_t id
);
94 static int ctl_lock(struct socket
*, int, int);
95 static int ctl_unlock(struct socket
*, int, int);
96 static lck_mtx_t
* ctl_getlock(struct socket
*, int);
98 static struct pr_usrreqs ctl_usrreqs
=
100 pru_abort_notsupp
, pru_accept_notsupp
, ctl_attach
, pru_bind_notsupp
,
101 ctl_connect
, pru_connect2_notsupp
, ctl_ioctl
, ctl_detach
,
102 ctl_disconnect
, pru_listen_notsupp
, ctl_peeraddr
,
103 pru_rcvd_notsupp
, pru_rcvoob_notsupp
, ctl_send
,
104 pru_sense_null
, pru_shutdown_notsupp
, pru_sockaddr_notsupp
,
105 sosend
, soreceive
, pru_sopoll_notsupp
108 static struct protosw kctlswk_dgram
=
110 SOCK_DGRAM
, &systemdomain
, SYSPROTO_CONTROL
,
111 PR_ATOMIC
|PR_CONNREQUIRED
|PR_PCBLOCK
,
112 NULL
, NULL
, NULL
, ctl_ctloutput
,
114 NULL
, NULL
, NULL
, NULL
, &ctl_usrreqs
,
115 ctl_lock
, ctl_unlock
, ctl_getlock
, { 0, 0 } , 0, { 0 }
118 static struct protosw kctlswk_stream
=
120 SOCK_STREAM
, &systemdomain
, SYSPROTO_CONTROL
,
121 PR_CONNREQUIRED
|PR_PCBLOCK
,
122 NULL
, NULL
, NULL
, ctl_ctloutput
,
124 NULL
, NULL
, NULL
, NULL
, &ctl_usrreqs
,
125 ctl_lock
, ctl_unlock
, ctl_getlock
, { 0, 0 } , 0, { 0 }
130 * Install the protosw's for the Kernel Control manager.
132 __private_extern__
int
133 kern_control_init(void)
137 ctl_lck_grp_attr
= lck_grp_attr_alloc_init();
138 if (ctl_lck_grp_attr
== 0) {
139 printf(": lck_grp_attr_alloc_init failed\n");
144 ctl_lck_grp
= lck_grp_alloc_init("Kernel Control Protocol", ctl_lck_grp_attr
);
145 if (ctl_lck_grp
== 0) {
146 printf("kern_control_init: lck_grp_alloc_init failed\n");
151 ctl_lck_attr
= lck_attr_alloc_init();
152 if (ctl_lck_attr
== 0) {
153 printf("kern_control_init: lck_attr_alloc_init failed\n");
158 ctl_mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
160 printf("kern_control_init: lck_mtx_alloc_init failed\n");
164 TAILQ_INIT(&ctl_head
);
166 error
= net_add_proto(&kctlswk_dgram
, &systemdomain
);
168 log(LOG_WARNING
, "kern_control_init: net_add_proto dgram failed (%d)\n", error
);
170 error
= net_add_proto(&kctlswk_stream
, &systemdomain
);
172 log(LOG_WARNING
, "kern_control_init: net_add_proto stream failed (%d)\n", error
);
178 lck_mtx_free(ctl_mtx
, ctl_lck_grp
);
182 lck_grp_free(ctl_lck_grp
);
185 if (ctl_lck_grp_attr
) {
186 lck_grp_attr_free(ctl_lck_grp_attr
);
187 ctl_lck_grp_attr
= 0;
190 lck_attr_free(ctl_lck_attr
);
198 kcb_delete(struct ctl_cb
*kcb
)
202 lck_mtx_free(kcb
->mtx
, ctl_lck_grp
);
209 * Kernel Controller user-request functions
210 * attach function must exist and succeed
211 * detach not necessary
212 * we need a pcb for the per socket mutex
215 ctl_attach(__unused
struct socket
*so
, __unused
int proto
, __unused
struct proc
*p
)
218 struct ctl_cb
*kcb
= 0;
220 MALLOC(kcb
, struct ctl_cb
*, sizeof(struct ctl_cb
), M_TEMP
, M_WAITOK
);
225 bzero(kcb
, sizeof(struct ctl_cb
));
227 kcb
->mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
228 if (kcb
->mtx
== NULL
) {
233 so
->so_pcb
= (caddr_t
)kcb
;
244 ctl_sofreelastref(struct socket
*so
)
246 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
252 if ((kctl
= kcb
->kctl
) != 0) {
253 lck_mtx_lock(ctl_mtx
);
254 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
255 lck_mtx_lock(ctl_mtx
);
263 ctl_detach(struct socket
*so
)
265 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
270 soisdisconnected(so
);
271 so
->so_flags
|= SOF_PCBCLEARING
;
277 ctl_connect(struct socket
*so
, struct sockaddr
*nam
, __unused
struct proc
*p
)
281 struct sockaddr_ctl sa
;
282 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
285 panic("ctl_connect so_pcb null\n");
287 if (nam
->sa_len
!= sizeof(struct sockaddr_ctl
))
290 bcopy(nam
, &sa
, sizeof(struct sockaddr_ctl
));
292 lck_mtx_lock(ctl_mtx
);
293 kctl
= ctl_find_by_id_unit(sa
.sc_id
, sa
.sc_unit
);
295 lck_mtx_unlock(ctl_mtx
);
299 if (((kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) && (so
->so_type
!= SOCK_STREAM
)) ||
300 (!(kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) && (so
->so_type
!= SOCK_DGRAM
))) {
301 lck_mtx_unlock(ctl_mtx
);
305 if (kctl
->flags
& CTL_FLAG_PRIVILEGED
) {
307 lck_mtx_unlock(ctl_mtx
);
310 if ((error
= proc_suser(p
))) {
311 lck_mtx_unlock(ctl_mtx
);
316 if ((kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) || sa
.sc_unit
!= 0) {
317 if (kcb_find(kctl
, sa
.sc_unit
) != NULL
) {
318 lck_mtx_unlock(ctl_mtx
);
322 u_int32_t unit
= kctl
->lastunit
+ 1;
325 if (unit
== ctl_maxunit
)
327 if (kcb_find(kctl
, unit
) == NULL
) {
328 kctl
->lastunit
= sa
.sc_unit
= unit
;
331 if (unit
++ == kctl
->lastunit
) {
332 lck_mtx_unlock(ctl_mtx
);
338 kcb
->unit
= sa
.sc_unit
;
340 TAILQ_INSERT_TAIL(&kctl
->kcb_head
, kcb
, next
);
341 lck_mtx_unlock(ctl_mtx
);
343 error
= soreserve(so
, kctl
->sendbufsize
, kctl
->recvbufsize
);
348 socket_unlock(so
, 0);
349 error
= (*kctl
->connect
)(kctl
, &sa
, &kcb
->userdata
);
358 soisdisconnected(so
);
359 lck_mtx_lock(ctl_mtx
);
362 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
363 lck_mtx_unlock(ctl_mtx
);
369 ctl_disconnect(struct socket
*so
)
371 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
373 if ((kcb
= (struct ctl_cb
*)so
->so_pcb
)) {
374 struct kctl
*kctl
= kcb
->kctl
;
376 if (kctl
&& kctl
->disconnect
) {
377 socket_unlock(so
, 0);
378 (*kctl
->disconnect
)(kctl
, kcb
->unit
, kcb
->userdata
);
381 lck_mtx_lock(ctl_mtx
);
384 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
385 soisdisconnected(so
);
386 lck_mtx_unlock(ctl_mtx
);
392 ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
394 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
396 struct sockaddr_ctl sc
;
398 if (kcb
== NULL
) /* sanity check */
401 if ((kctl
= kcb
->kctl
) == NULL
)
404 bzero(&sc
, sizeof(struct sockaddr_ctl
));
405 sc
.sc_len
= sizeof(struct sockaddr_ctl
);
406 sc
.sc_family
= AF_SYSTEM
;
407 sc
.ss_sysaddr
= AF_SYS_CONTROL
;
409 sc
.sc_unit
= kcb
->unit
;
411 *nam
= dup_sockaddr((struct sockaddr
*)&sc
, 1);
417 ctl_send(struct socket
*so
, int flags
, struct mbuf
*m
,
418 __unused
struct sockaddr
*addr
, __unused
struct mbuf
*control
,
419 __unused
struct proc
*p
)
422 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
425 if (kcb
== NULL
) /* sanity check */
428 if ((kctl
= kcb
->kctl
) == NULL
)
432 socket_unlock(so
, 0);
433 error
= (*kctl
->send
)(kctl
, kcb
->unit
, kcb
->userdata
, m
, flags
);
440 ctl_enqueuembuf(void *kctlref
, u_int32_t unit
, struct mbuf
*m
, u_int32_t flags
)
445 struct kctl
*kctl
= (struct kctl
*)kctlref
;
450 kcb
= kcb_find(kctl
, unit
);
454 so
= (struct socket
*)kcb
->so
;
459 if (sbspace(&so
->so_rcv
) < m
->m_pkthdr
.len
) {
463 if ((flags
& CTL_DATA_EOR
))
465 if (sbappend(&so
->so_rcv
, m
) && (flags
& CTL_DATA_NOWAKEUP
) == 0)
468 socket_unlock(so
, 1);
473 ctl_enqueuedata(void *kctlref
, u_int32_t unit
, void *data
, size_t len
, u_int32_t flags
)
479 struct kctl
*kctl
= (struct kctl
*)kctlref
;
480 unsigned int num_needed
;
487 kcb
= kcb_find(kctl
, unit
);
491 so
= (struct socket
*)kcb
->so
;
496 if ((size_t)sbspace(&so
->so_rcv
) < len
) {
502 m
= m_allocpacket_internal(&num_needed
, len
, NULL
, M_NOWAIT
, 1, 0);
504 printf("ctl_enqueuedata: m_allocpacket_internal(%lu) failed\n", len
);
509 for (n
= m
; n
!= NULL
; n
= n
->m_next
) {
510 size_t mlen
= mbuf_maxlen(n
);
512 if (mlen
+ curlen
> len
)
515 bcopy((char *)data
+ curlen
, n
->m_data
, mlen
);
518 mbuf_pkthdr_setlen(m
, curlen
);
520 if ((flags
& CTL_DATA_EOR
))
522 if (sbappend(&so
->so_rcv
, m
) && (flags
& CTL_DATA_NOWAKEUP
) == 0)
525 socket_unlock(so
, 1);
531 ctl_getenqueuespace(kern_ctl_ref kctlref
, u_int32_t unit
, size_t *space
)
534 struct kctl
*kctl
= (struct kctl
*)kctlref
;
537 if (kctlref
== NULL
|| space
== NULL
)
540 kcb
= kcb_find(kctl
, unit
);
544 so
= (struct socket
*)kcb
->so
;
549 *space
= sbspace(&so
->so_rcv
);
550 socket_unlock(so
, 1);
556 ctl_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
558 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
564 if (sopt
->sopt_level
!= SYSPROTO_CONTROL
) {
568 if (kcb
== NULL
) /* sanity check */
571 if ((kctl
= kcb
->kctl
) == NULL
)
574 switch (sopt
->sopt_dir
) {
576 if (kctl
->setopt
== NULL
)
578 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
, M_WAITOK
);
581 error
= sooptcopyin(sopt
, data
, sopt
->sopt_valsize
, sopt
->sopt_valsize
);
583 socket_unlock(so
, 0);
584 error
= (*kctl
->setopt
)(kcb
->kctl
, kcb
->unit
, kcb
->userdata
, sopt
->sopt_name
,
585 data
, sopt
->sopt_valsize
);
592 if (kctl
->getopt
== NULL
)
595 if (sopt
->sopt_valsize
&& sopt
->sopt_val
) {
596 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
, M_WAITOK
);
599 /* 4108337 - copy in data for get socket option */
600 error
= sooptcopyin(sopt
, data
, sopt
->sopt_valsize
, sopt
->sopt_valsize
);
602 len
= sopt
->sopt_valsize
;
603 socket_unlock(so
, 0);
604 error
= (*kctl
->getopt
)(kcb
->kctl
, kcb
->unit
, kcb
->userdata
, sopt
->sopt_name
,
609 error
= sooptcopyout(sopt
, data
, len
);
611 sopt
->sopt_valsize
= len
;
621 ctl_ioctl(__unused
struct socket
*so
, u_long cmd
, caddr_t data
,
622 __unused
struct ifnet
*ifp
, __unused
struct proc
*p
)
627 /* get the number of controllers */
632 lck_mtx_lock(ctl_mtx
);
633 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
635 lck_mtx_unlock(ctl_mtx
);
637 *(u_int32_t
*)data
= n
;
642 struct ctl_info
*ctl_info
= (struct ctl_info
*)data
;
643 struct kctl
*kctl
= 0;
644 size_t name_len
= strlen(ctl_info
->ctl_name
);
646 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
) {
650 lck_mtx_lock(ctl_mtx
);
651 kctl
= ctl_find_by_name(ctl_info
->ctl_name
);
652 lck_mtx_unlock(ctl_mtx
);
657 ctl_info
->ctl_id
= kctl
->id
;
662 /* add controls to get list of NKEs */
670 * Register/unregister a NKE
673 ctl_register(struct kern_ctl_reg
*userkctl
, kern_ctl_ref
*kctlref
)
675 struct kctl
*kctl
= 0;
680 if (userkctl
== NULL
) /* sanity check */
682 if (userkctl
->ctl_connect
== NULL
)
684 name_len
= strlen(userkctl
->ctl_name
);
685 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
)
688 MALLOC(kctl
, struct kctl
*, sizeof(*kctl
), M_TEMP
, M_WAITOK
);
691 bzero((char *)kctl
, sizeof(*kctl
));
693 lck_mtx_lock(ctl_mtx
);
695 if ((userkctl
->ctl_flags
& CTL_FLAG_REG_ID_UNIT
) == 0) {
696 if (ctl_find_by_name(userkctl
->ctl_name
) != NULL
) {
697 lck_mtx_unlock(ctl_mtx
);
701 for (n
= 0, id
= ctl_last_id
+ 1; n
< ctl_max
; id
++, n
++) {
706 if (ctl_find_by_id(id
) == 0)
710 lck_mtx_unlock(ctl_mtx
);
714 userkctl
->ctl_id
=id
;
718 if (ctl_find_by_id_unit(userkctl
->ctl_id
, userkctl
->ctl_unit
) != NULL
) {
719 lck_mtx_unlock(ctl_mtx
);
723 kctl
->id
= userkctl
->ctl_id
;
724 kctl
->reg_unit
= userkctl
->ctl_unit
;
726 strcpy(kctl
->name
, userkctl
->ctl_name
);
727 kctl
->flags
= userkctl
->ctl_flags
;
729 /* Let the caller know the default send and receive sizes */
730 if (userkctl
->ctl_sendsize
== 0)
731 userkctl
->ctl_sendsize
= CTL_SENDSIZE
;
732 kctl
->sendbufsize
= userkctl
->ctl_sendsize
;
734 if (userkctl
->ctl_recvsize
== 0)
735 userkctl
->ctl_recvsize
= CTL_RECVSIZE
;
736 kctl
->recvbufsize
= userkctl
->ctl_recvsize
;
738 kctl
->connect
= userkctl
->ctl_connect
;
739 kctl
->disconnect
= userkctl
->ctl_disconnect
;
740 kctl
->send
= userkctl
->ctl_send
;
741 kctl
->setopt
= userkctl
->ctl_setopt
;
742 kctl
->getopt
= userkctl
->ctl_getopt
;
744 TAILQ_INIT(&kctl
->kcb_head
);
746 TAILQ_INSERT_TAIL(&ctl_head
, kctl
, next
);
749 lck_mtx_unlock(ctl_mtx
);
753 ctl_post_msg(KEV_CTL_REGISTERED
, kctl
->id
);
758 ctl_deregister(void *kctlref
)
762 if (kctlref
== NULL
) /* sanity check */
765 lck_mtx_lock(ctl_mtx
);
766 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
767 if (kctl
== (struct kctl
*)kctlref
)
770 if (kctl
!= (struct kctl
*)kctlref
) {
771 lck_mtx_unlock(ctl_mtx
);
774 if (!TAILQ_EMPTY(&kctl
->kcb_head
)) {
775 lck_mtx_unlock(ctl_mtx
);
779 TAILQ_REMOVE(&ctl_head
, kctl
, next
);
782 lck_mtx_unlock(ctl_mtx
);
784 ctl_post_msg(KEV_CTL_DEREGISTERED
, kctl
->id
);
790 * Must be called with global lock taked
793 ctl_find_by_id(u_int32_t id
)
797 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
805 * Must be called with global ctl_mtx lock taked
808 ctl_find_by_name(const char *name
)
812 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
813 if (strcmp(kctl
->name
, name
) == 0)
820 * Must be called with global ctl_mtx lock taked
824 ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
)
828 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
829 if (kctl
->id
== id
&& (kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) == 0)
831 else if (kctl
->id
== id
&& kctl
->reg_unit
== unit
)
838 * Must be called with kernel controller lock taken
840 static struct ctl_cb
*
841 kcb_find(struct kctl
*kctl
, u_int32_t unit
)
845 TAILQ_FOREACH(kcb
, &kctl
->kcb_head
, next
)
846 if ((kcb
->unit
== unit
))
853 * Must be called witout lock
856 ctl_post_msg(u_long event_code
, u_int32_t id
)
858 struct ctl_event_data ctl_ev_data
;
859 struct kev_msg ev_msg
;
861 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
863 ev_msg
.kev_class
= KEV_SYSTEM_CLASS
;
864 ev_msg
.kev_subclass
= KEV_CTL_SUBCLASS
;
865 ev_msg
.event_code
= event_code
;
867 /* common nke subclass data */
868 bzero(&ctl_ev_data
, sizeof(ctl_ev_data
));
869 ctl_ev_data
.ctl_id
= id
;
870 ev_msg
.dv
[0].data_ptr
= &ctl_ev_data
;
871 ev_msg
.dv
[0].data_length
= sizeof(ctl_ev_data
);
873 ev_msg
.dv
[1].data_length
= 0;
875 kev_post_msg(&ev_msg
);
879 ctl_lock(struct socket
*so
, int refcount
, int lr
)
883 lr_saved
= (unsigned int) __builtin_return_address(0);
887 lck_mtx_lock(((struct ctl_cb
*)so
->so_pcb
)->mtx
);
889 panic("ctl_lock: so=%x NO PCB! lr=%x\n", so
, lr_saved
);
890 lck_mtx_lock(so
->so_proto
->pr_domain
->dom_mtx
);
893 if (so
->so_usecount
< 0)
894 panic("ctl_lock: so=%x so_pcb=%x lr=%x ref=%x\n",
895 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
);
900 so
->lock_lr
[so
->next_lock_lr
] = (void *)lr_saved
;
901 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
906 ctl_unlock(struct socket
*so
, int refcount
, int lr
)
909 lck_mtx_t
* mutex_held
;
912 lr_saved
= (unsigned int) __builtin_return_address(0);
915 #ifdef MORE_KCTLLOCK_DEBUG
916 printf("ctl_unlock: so=%x sopcb=%x lock=%x ref=%x lr=%x\n",
917 so
, so
->so_pcb
, ((struct ctl_cb
*)so
->so_pcb
)->mtx
, so
->so_usecount
, lr_saved
);
922 if (so
->so_usecount
< 0)
923 panic("ctl_unlock: so=%x usecount=%x\n", so
, so
->so_usecount
);
924 if (so
->so_pcb
== NULL
) {
925 panic("ctl_unlock: so=%x NO PCB usecount=%x lr=%x\n", so
, so
->so_usecount
, lr_saved
);
926 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
928 mutex_held
= ((struct ctl_cb
*)so
->so_pcb
)->mtx
;
930 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
931 so
->unlock_lr
[so
->next_unlock_lr
] = (void *)lr_saved
;
932 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
933 lck_mtx_unlock(mutex_held
);
935 if (so
->so_usecount
== 0)
936 ctl_sofreelastref(so
);
942 ctl_getlock(struct socket
*so
, __unused
int locktype
)
944 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
947 if (so
->so_usecount
< 0)
948 panic("ctl_getlock: so=%x usecount=%x\n", so
, so
->so_usecount
);
951 panic("ctl_getlock: so=%x NULL so_pcb\n", so
);
952 return (so
->so_proto
->pr_domain
->dom_mtx
);