2 * Copyright (c) 1999-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
30 * Kernel Control domain - allows control connections to
31 * and to read/write data.
33 * Vincent Lubet, 040506
34 * Christophe Allie, 010928
35 * Justin C. Walker, 990319
38 #include <sys/types.h>
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/syslog.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/protosw.h>
45 #include <sys/domain.h>
46 #include <sys/malloc.h>
48 #include <sys/sys_domain.h>
49 #include <sys/kern_event.h>
50 #include <sys/kern_control.h>
51 #include <net/if_var.h>
53 #include <mach/vm_types.h>
54 #include <mach/kmod.h>
56 #include <kern/thread.h>
59 * Definitions and vars for we support
62 #define CTL_SENDSIZE (2 * 1024) /* default buffer size */
63 #define CTL_RECVSIZE (8 * 1024) /* default buffer size */
66 * Definitions and vars for we support
69 static u_int32_t ctl_last_id
= 0;
70 static u_int32_t ctl_max
= 256;
71 static u_int32_t ctl_maxunit
= 65536;
72 static lck_grp_attr_t
*ctl_lck_grp_attr
= 0;
73 static lck_attr_t
*ctl_lck_attr
= 0;
74 static lck_grp_t
*ctl_lck_grp
= 0;
75 static lck_mtx_t
*ctl_mtx
;
78 /* all the controllers are chained */
79 TAILQ_HEAD(, kctl
) ctl_head
;
81 static int ctl_attach(struct socket
*, int, struct proc
*);
82 static int ctl_detach(struct socket
*);
83 static int ctl_sofreelastref(struct socket
*so
);
84 static int ctl_connect(struct socket
*, struct sockaddr
*, struct proc
*);
85 static int ctl_disconnect(struct socket
*);
86 static int ctl_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
87 struct ifnet
*ifp
, struct proc
*p
);
88 static int ctl_send(struct socket
*, int, struct mbuf
*,
89 struct sockaddr
*, struct mbuf
*, struct proc
*);
90 static int ctl_ctloutput(struct socket
*, struct sockopt
*);
91 static int ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
);
93 static struct kctl
*ctl_find_by_id(u_int32_t
);
94 static struct kctl
*ctl_find_by_name(const char *);
95 static struct kctl
*ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
);
97 static struct ctl_cb
*kcb_find(struct kctl
*, u_int32_t unit
);
98 static void ctl_post_msg(u_long event_code
, u_int32_t id
);
100 static int ctl_lock(struct socket
*, int, int);
101 static int ctl_unlock(struct socket
*, int, int);
102 static lck_mtx_t
* ctl_getlock(struct socket
*, int);
104 static struct pr_usrreqs ctl_usrreqs
=
106 pru_abort_notsupp
, pru_accept_notsupp
, ctl_attach
, pru_bind_notsupp
,
107 ctl_connect
, pru_connect2_notsupp
, ctl_ioctl
, ctl_detach
,
108 ctl_disconnect
, pru_listen_notsupp
, ctl_peeraddr
,
109 pru_rcvd_notsupp
, pru_rcvoob_notsupp
, ctl_send
,
110 pru_sense_null
, pru_shutdown_notsupp
, pru_sockaddr_notsupp
,
111 sosend
, soreceive
, pru_sopoll_notsupp
114 static struct protosw kctlswk_dgram
=
116 SOCK_DGRAM
, &systemdomain
, SYSPROTO_CONTROL
,
117 PR_ATOMIC
|PR_CONNREQUIRED
|PR_PCBLOCK
,
118 NULL
, NULL
, NULL
, ctl_ctloutput
,
120 NULL
, NULL
, NULL
, NULL
, &ctl_usrreqs
,
121 ctl_lock
, ctl_unlock
, ctl_getlock
, { 0, 0 } , 0, { 0 }
124 static struct protosw kctlswk_stream
=
126 SOCK_STREAM
, &systemdomain
, SYSPROTO_CONTROL
,
127 PR_CONNREQUIRED
|PR_PCBLOCK
,
128 NULL
, NULL
, NULL
, ctl_ctloutput
,
130 NULL
, NULL
, NULL
, NULL
, &ctl_usrreqs
,
131 ctl_lock
, ctl_unlock
, ctl_getlock
, { 0, 0 } , 0, { 0 }
136 * Install the protosw's for the Kernel Control manager.
138 __private_extern__
int
139 kern_control_init(void)
143 ctl_lck_grp_attr
= lck_grp_attr_alloc_init();
144 if (ctl_lck_grp_attr
== 0) {
145 printf(": lck_grp_attr_alloc_init failed\n");
150 ctl_lck_grp
= lck_grp_alloc_init("Kernel Control Protocol", ctl_lck_grp_attr
);
151 if (ctl_lck_grp
== 0) {
152 printf("kern_control_init: lck_grp_alloc_init failed\n");
157 ctl_lck_attr
= lck_attr_alloc_init();
158 if (ctl_lck_attr
== 0) {
159 printf("kern_control_init: lck_attr_alloc_init failed\n");
164 ctl_mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
166 printf("kern_control_init: lck_mtx_alloc_init failed\n");
170 TAILQ_INIT(&ctl_head
);
172 error
= net_add_proto(&kctlswk_dgram
, &systemdomain
);
174 log(LOG_WARNING
, "kern_control_init: net_add_proto dgram failed (%d)\n", error
);
176 error
= net_add_proto(&kctlswk_stream
, &systemdomain
);
178 log(LOG_WARNING
, "kern_control_init: net_add_proto stream failed (%d)\n", error
);
184 lck_mtx_free(ctl_mtx
, ctl_lck_grp
);
188 lck_grp_free(ctl_lck_grp
);
191 if (ctl_lck_grp_attr
) {
192 lck_grp_attr_free(ctl_lck_grp_attr
);
193 ctl_lck_grp_attr
= 0;
196 lck_attr_free(ctl_lck_attr
);
204 kcb_delete(struct ctl_cb
*kcb
)
208 lck_mtx_free(kcb
->mtx
, ctl_lck_grp
);
215 * Kernel Controller user-request functions
216 * attach function must exist and succeed
217 * detach not necessary
218 * we need a pcb for the per socket mutex
221 ctl_attach(__unused
struct socket
*so
, __unused
int proto
, __unused
struct proc
*p
)
224 struct ctl_cb
*kcb
= 0;
226 MALLOC(kcb
, struct ctl_cb
*, sizeof(struct ctl_cb
), M_TEMP
, M_WAITOK
);
231 bzero(kcb
, sizeof(struct ctl_cb
));
233 kcb
->mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
234 if (kcb
->mtx
== NULL
) {
239 so
->so_pcb
= (caddr_t
)kcb
;
250 ctl_sofreelastref(struct socket
*so
)
252 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
258 if ((kctl
= kcb
->kctl
) != 0) {
259 lck_mtx_lock(ctl_mtx
);
260 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
261 lck_mtx_lock(ctl_mtx
);
269 ctl_detach(struct socket
*so
)
271 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
276 soisdisconnected(so
);
277 so
->so_flags
|= SOF_PCBCLEARING
;
283 ctl_connect(struct socket
*so
, struct sockaddr
*nam
, __unused
struct proc
*p
)
287 struct sockaddr_ctl sa
;
288 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
291 panic("ctl_connect so_pcb null\n");
293 if (nam
->sa_len
!= sizeof(struct sockaddr_ctl
))
296 bcopy(nam
, &sa
, sizeof(struct sockaddr_ctl
));
298 lck_mtx_lock(ctl_mtx
);
299 kctl
= ctl_find_by_id_unit(sa
.sc_id
, sa
.sc_unit
);
301 lck_mtx_unlock(ctl_mtx
);
305 if (((kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) && (so
->so_type
!= SOCK_STREAM
)) ||
306 (!(kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) && (so
->so_type
!= SOCK_DGRAM
))) {
307 lck_mtx_unlock(ctl_mtx
);
311 if (kctl
->flags
& CTL_FLAG_PRIVILEGED
) {
313 lck_mtx_unlock(ctl_mtx
);
316 if ((error
= proc_suser(p
))) {
317 lck_mtx_unlock(ctl_mtx
);
322 if ((kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) || sa
.sc_unit
!= 0) {
323 if (kcb_find(kctl
, sa
.sc_unit
) != NULL
) {
324 lck_mtx_unlock(ctl_mtx
);
328 u_int32_t unit
= kctl
->lastunit
+ 1;
331 if (unit
== ctl_maxunit
)
333 if (kcb_find(kctl
, unit
) == NULL
) {
334 kctl
->lastunit
= sa
.sc_unit
= unit
;
337 if (unit
++ == kctl
->lastunit
) {
338 lck_mtx_unlock(ctl_mtx
);
344 kcb
->unit
= sa
.sc_unit
;
346 TAILQ_INSERT_TAIL(&kctl
->kcb_head
, kcb
, next
);
347 lck_mtx_unlock(ctl_mtx
);
349 error
= soreserve(so
, kctl
->sendbufsize
, kctl
->recvbufsize
);
354 socket_unlock(so
, 0);
355 error
= (*kctl
->connect
)(kctl
, &sa
, &kcb
->userdata
);
364 soisdisconnected(so
);
365 lck_mtx_lock(ctl_mtx
);
368 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
369 lck_mtx_unlock(ctl_mtx
);
375 ctl_disconnect(struct socket
*so
)
377 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
379 if ((kcb
= (struct ctl_cb
*)so
->so_pcb
)) {
380 struct kctl
*kctl
= kcb
->kctl
;
382 if (kctl
&& kctl
->disconnect
) {
383 socket_unlock(so
, 0);
384 (*kctl
->disconnect
)(kctl
, kcb
->unit
, kcb
->userdata
);
387 lck_mtx_lock(ctl_mtx
);
390 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
391 soisdisconnected(so
);
392 lck_mtx_unlock(ctl_mtx
);
398 ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
400 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
402 struct sockaddr_ctl sc
;
404 if (kcb
== NULL
) /* sanity check */
407 if ((kctl
= kcb
->kctl
) == NULL
)
410 bzero(&sc
, sizeof(struct sockaddr_ctl
));
411 sc
.sc_len
= sizeof(struct sockaddr_ctl
);
412 sc
.sc_family
= AF_SYSTEM
;
413 sc
.ss_sysaddr
= AF_SYS_CONTROL
;
415 sc
.sc_unit
= kcb
->unit
;
417 *nam
= dup_sockaddr((struct sockaddr
*)&sc
, 1);
423 ctl_send(struct socket
*so
, int flags
, struct mbuf
*m
,
424 __unused
struct sockaddr
*addr
, __unused
struct mbuf
*control
,
425 __unused
struct proc
*p
)
428 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
431 if (kcb
== NULL
) /* sanity check */
434 if ((kctl
= kcb
->kctl
) == NULL
)
438 socket_unlock(so
, 0);
439 error
= (*kctl
->send
)(kctl
, kcb
->unit
, kcb
->userdata
, m
, flags
);
446 ctl_enqueuembuf(void *kctlref
, u_int32_t unit
, struct mbuf
*m
, u_int32_t flags
)
451 struct kctl
*kctl
= (struct kctl
*)kctlref
;
456 kcb
= kcb_find(kctl
, unit
);
460 so
= (struct socket
*)kcb
->so
;
465 if (sbspace(&so
->so_rcv
) < m
->m_pkthdr
.len
) {
469 if ((flags
& CTL_DATA_EOR
))
471 if (sbappend(&so
->so_rcv
, m
) && (flags
& CTL_DATA_NOWAKEUP
) == 0)
474 socket_unlock(so
, 1);
479 ctl_enqueuedata(void *kctlref
, u_int32_t unit
, void *data
, size_t len
, u_int32_t flags
)
485 struct kctl
*kctl
= (struct kctl
*)kctlref
;
486 unsigned int num_needed
;
493 kcb
= kcb_find(kctl
, unit
);
497 so
= (struct socket
*)kcb
->so
;
502 if ((size_t)sbspace(&so
->so_rcv
) < len
) {
508 m
= m_allocpacket_internal(&num_needed
, len
, NULL
, M_NOWAIT
, 1, 0);
510 printf("ctl_enqueuedata: m_allocpacket_internal(%lu) failed\n", len
);
515 for (n
= m
; n
!= NULL
; n
= n
->m_next
) {
516 size_t mlen
= mbuf_maxlen(n
);
518 if (mlen
+ curlen
> len
)
521 bcopy((char *)data
+ curlen
, n
->m_data
, mlen
);
524 mbuf_pkthdr_setlen(m
, curlen
);
526 if ((flags
& CTL_DATA_EOR
))
528 if (sbappend(&so
->so_rcv
, m
) && (flags
& CTL_DATA_NOWAKEUP
) == 0)
531 socket_unlock(so
, 1);
537 ctl_getenqueuespace(kern_ctl_ref kctlref
, u_int32_t unit
, size_t *space
)
540 struct kctl
*kctl
= (struct kctl
*)kctlref
;
543 if (kctlref
== NULL
|| space
== NULL
)
546 kcb
= kcb_find(kctl
, unit
);
550 so
= (struct socket
*)kcb
->so
;
555 *space
= sbspace(&so
->so_rcv
);
556 socket_unlock(so
, 1);
562 ctl_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
564 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
570 if (sopt
->sopt_level
!= SYSPROTO_CONTROL
) {
574 if (kcb
== NULL
) /* sanity check */
577 if ((kctl
= kcb
->kctl
) == NULL
)
580 switch (sopt
->sopt_dir
) {
582 if (kctl
->setopt
== NULL
)
584 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
, M_WAITOK
);
587 error
= sooptcopyin(sopt
, data
, sopt
->sopt_valsize
, sopt
->sopt_valsize
);
589 socket_unlock(so
, 0);
590 error
= (*kctl
->setopt
)(kcb
->kctl
, kcb
->unit
, kcb
->userdata
, sopt
->sopt_name
,
591 data
, sopt
->sopt_valsize
);
598 if (kctl
->getopt
== NULL
)
601 if (sopt
->sopt_valsize
&& sopt
->sopt_val
) {
602 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
, M_WAITOK
);
605 /* 4108337 - copy in data for get socket option */
606 error
= sooptcopyin(sopt
, data
, sopt
->sopt_valsize
, sopt
->sopt_valsize
);
608 len
= sopt
->sopt_valsize
;
609 socket_unlock(so
, 0);
610 error
= (*kctl
->getopt
)(kcb
->kctl
, kcb
->unit
, kcb
->userdata
, sopt
->sopt_name
,
615 error
= sooptcopyout(sopt
, data
, len
);
617 sopt
->sopt_valsize
= len
;
627 ctl_ioctl(__unused
struct socket
*so
, u_long cmd
, caddr_t data
,
628 __unused
struct ifnet
*ifp
, __unused
struct proc
*p
)
633 /* get the number of controllers */
638 lck_mtx_lock(ctl_mtx
);
639 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
641 lck_mtx_unlock(ctl_mtx
);
643 *(u_int32_t
*)data
= n
;
648 struct ctl_info
*ctl_info
= (struct ctl_info
*)data
;
649 struct kctl
*kctl
= 0;
650 size_t name_len
= strlen(ctl_info
->ctl_name
);
652 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
) {
656 lck_mtx_lock(ctl_mtx
);
657 kctl
= ctl_find_by_name(ctl_info
->ctl_name
);
658 lck_mtx_unlock(ctl_mtx
);
663 ctl_info
->ctl_id
= kctl
->id
;
668 /* add controls to get list of NKEs */
676 * Register/unregister a NKE
679 ctl_register(struct kern_ctl_reg
*userkctl
, kern_ctl_ref
*kctlref
)
681 struct kctl
*kctl
= 0;
686 if (userkctl
== NULL
) /* sanity check */
688 if (userkctl
->ctl_connect
== NULL
)
690 name_len
= strlen(userkctl
->ctl_name
);
691 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
)
694 MALLOC(kctl
, struct kctl
*, sizeof(*kctl
), M_TEMP
, M_WAITOK
);
697 bzero((char *)kctl
, sizeof(*kctl
));
699 lck_mtx_lock(ctl_mtx
);
701 if ((userkctl
->ctl_flags
& CTL_FLAG_REG_ID_UNIT
) == 0) {
702 if (ctl_find_by_name(userkctl
->ctl_name
) != NULL
) {
703 lck_mtx_unlock(ctl_mtx
);
707 for (n
= 0, id
= ctl_last_id
+ 1; n
< ctl_max
; id
++, n
++) {
712 if (ctl_find_by_id(id
) == 0)
716 lck_mtx_unlock(ctl_mtx
);
720 userkctl
->ctl_id
=id
;
724 if (ctl_find_by_id_unit(userkctl
->ctl_id
, userkctl
->ctl_unit
) != NULL
) {
725 lck_mtx_unlock(ctl_mtx
);
729 kctl
->id
= userkctl
->ctl_id
;
730 kctl
->reg_unit
= userkctl
->ctl_unit
;
732 strcpy(kctl
->name
, userkctl
->ctl_name
);
733 kctl
->flags
= userkctl
->ctl_flags
;
735 /* Let the caller know the default send and receive sizes */
736 if (userkctl
->ctl_sendsize
== 0)
737 userkctl
->ctl_sendsize
= CTL_SENDSIZE
;
738 kctl
->sendbufsize
= userkctl
->ctl_sendsize
;
740 if (userkctl
->ctl_recvsize
== 0)
741 userkctl
->ctl_recvsize
= CTL_RECVSIZE
;
742 kctl
->recvbufsize
= userkctl
->ctl_recvsize
;
744 kctl
->connect
= userkctl
->ctl_connect
;
745 kctl
->disconnect
= userkctl
->ctl_disconnect
;
746 kctl
->send
= userkctl
->ctl_send
;
747 kctl
->setopt
= userkctl
->ctl_setopt
;
748 kctl
->getopt
= userkctl
->ctl_getopt
;
750 TAILQ_INIT(&kctl
->kcb_head
);
752 TAILQ_INSERT_TAIL(&ctl_head
, kctl
, next
);
755 lck_mtx_unlock(ctl_mtx
);
759 ctl_post_msg(KEV_CTL_REGISTERED
, kctl
->id
);
764 ctl_deregister(void *kctlref
)
768 if (kctlref
== NULL
) /* sanity check */
771 lck_mtx_lock(ctl_mtx
);
772 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
773 if (kctl
== (struct kctl
*)kctlref
)
776 if (kctl
!= (struct kctl
*)kctlref
) {
777 lck_mtx_unlock(ctl_mtx
);
780 if (!TAILQ_EMPTY(&kctl
->kcb_head
)) {
781 lck_mtx_unlock(ctl_mtx
);
785 TAILQ_REMOVE(&ctl_head
, kctl
, next
);
788 lck_mtx_unlock(ctl_mtx
);
790 ctl_post_msg(KEV_CTL_DEREGISTERED
, kctl
->id
);
796 * Must be called with global lock taked
799 ctl_find_by_id(u_int32_t id
)
803 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
811 * Must be called with global ctl_mtx lock taked
814 ctl_find_by_name(const char *name
)
818 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
819 if (strcmp(kctl
->name
, name
) == 0)
826 * Must be called with global ctl_mtx lock taked
830 ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
)
834 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
835 if (kctl
->id
== id
&& (kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) == 0)
837 else if (kctl
->id
== id
&& kctl
->reg_unit
== unit
)
844 * Must be called with kernel controller lock taken
846 static struct ctl_cb
*
847 kcb_find(struct kctl
*kctl
, u_int32_t unit
)
851 TAILQ_FOREACH(kcb
, &kctl
->kcb_head
, next
)
852 if ((kcb
->unit
== unit
))
859 * Must be called witout lock
862 ctl_post_msg(u_long event_code
, u_int32_t id
)
864 struct ctl_event_data ctl_ev_data
;
865 struct kev_msg ev_msg
;
867 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
869 ev_msg
.kev_class
= KEV_SYSTEM_CLASS
;
870 ev_msg
.kev_subclass
= KEV_CTL_SUBCLASS
;
871 ev_msg
.event_code
= event_code
;
873 /* common nke subclass data */
874 bzero(&ctl_ev_data
, sizeof(ctl_ev_data
));
875 ctl_ev_data
.ctl_id
= id
;
876 ev_msg
.dv
[0].data_ptr
= &ctl_ev_data
;
877 ev_msg
.dv
[0].data_length
= sizeof(ctl_ev_data
);
879 ev_msg
.dv
[1].data_length
= 0;
881 kev_post_msg(&ev_msg
);
885 ctl_lock(struct socket
*so
, int refcount
, int lr
)
889 lr_saved
= (unsigned int) __builtin_return_address(0);
893 lck_mtx_lock(((struct ctl_cb
*)so
->so_pcb
)->mtx
);
895 panic("ctl_lock: so=%x NO PCB! lr=%x\n", so
, lr_saved
);
896 lck_mtx_lock(so
->so_proto
->pr_domain
->dom_mtx
);
899 if (so
->so_usecount
< 0)
900 panic("ctl_lock: so=%x so_pcb=%x lr=%x ref=%x\n",
901 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
);
906 so
->lock_lr
[so
->next_lock_lr
] = (void *)lr_saved
;
907 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
912 ctl_unlock(struct socket
*so
, int refcount
, int lr
)
915 lck_mtx_t
* mutex_held
;
918 lr_saved
= (unsigned int) __builtin_return_address(0);
921 #ifdef MORE_KCTLLOCK_DEBUG
922 printf("ctl_unlock: so=%x sopcb=%x lock=%x ref=%x lr=%x\n",
923 so
, so
->so_pcb
, ((struct ctl_cb
*)so
->so_pcb
)->mtx
, so
->so_usecount
, lr_saved
);
928 if (so
->so_usecount
< 0)
929 panic("ctl_unlock: so=%x usecount=%x\n", so
, so
->so_usecount
);
930 if (so
->so_pcb
== NULL
) {
931 panic("ctl_unlock: so=%x NO PCB usecount=%x lr=%x\n", so
, so
->so_usecount
, lr_saved
);
932 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
934 mutex_held
= ((struct ctl_cb
*)so
->so_pcb
)->mtx
;
936 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
937 so
->unlock_lr
[so
->next_unlock_lr
] = (void *)lr_saved
;
938 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
939 lck_mtx_unlock(mutex_held
);
941 if (so
->so_usecount
== 0)
942 ctl_sofreelastref(so
);
948 ctl_getlock(struct socket
*so
, __unused
int locktype
)
950 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
953 if (so
->so_usecount
< 0)
954 panic("ctl_getlock: so=%x usecount=%x\n", so
, so
->so_usecount
);
957 panic("ctl_getlock: so=%x NULL so_pcb\n", so
);
958 return (so
->so_proto
->pr_domain
->dom_mtx
);