2 * Copyright (c) 1999-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
32 * Kernel Control domain - allows control connections to
33 * and to read/write data.
35 * Vincent Lubet, 040506
36 * Christophe Allie, 010928
37 * Justin C. Walker, 990319
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/syslog.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/protosw.h>
47 #include <sys/domain.h>
48 #include <sys/malloc.h>
50 #include <sys/sys_domain.h>
51 #include <sys/kern_event.h>
52 #include <sys/kern_control.h>
53 #include <net/if_var.h>
55 #include <mach/vm_types.h>
56 #include <mach/kmod.h>
58 #include <kern/thread.h>
61 * Definitions and vars for we support
64 #define CTL_SENDSIZE (2 * 1024) /* default buffer size */
65 #define CTL_RECVSIZE (8 * 1024) /* default buffer size */
68 * Definitions and vars for we support
71 static u_int32_t ctl_last_id
= 0;
72 static u_int32_t ctl_max
= 256;
73 static u_int32_t ctl_maxunit
= 65536;
74 static lck_grp_attr_t
*ctl_lck_grp_attr
= 0;
75 static lck_attr_t
*ctl_lck_attr
= 0;
76 static lck_grp_t
*ctl_lck_grp
= 0;
77 static lck_mtx_t
*ctl_mtx
;
80 /* all the controllers are chained */
81 TAILQ_HEAD(, kctl
) ctl_head
;
83 static int ctl_attach(struct socket
*, int, struct proc
*);
84 static int ctl_detach(struct socket
*);
85 static int ctl_sofreelastref(struct socket
*so
);
86 static int ctl_connect(struct socket
*, struct sockaddr
*, struct proc
*);
87 static int ctl_disconnect(struct socket
*);
88 static int ctl_ioctl(struct socket
*so
, u_long cmd
, caddr_t data
,
89 struct ifnet
*ifp
, struct proc
*p
);
90 static int ctl_send(struct socket
*, int, struct mbuf
*,
91 struct sockaddr
*, struct mbuf
*, struct proc
*);
92 static int ctl_ctloutput(struct socket
*, struct sockopt
*);
93 static int ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
);
95 static struct kctl
*ctl_find_by_id(u_int32_t
);
96 static struct kctl
*ctl_find_by_name(const char *);
97 static struct kctl
*ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
);
99 static struct ctl_cb
*kcb_find(struct kctl
*, u_int32_t unit
);
100 static void ctl_post_msg(u_long event_code
, u_int32_t id
);
102 static int ctl_lock(struct socket
*, int, int);
103 static int ctl_unlock(struct socket
*, int, int);
104 static lck_mtx_t
* ctl_getlock(struct socket
*, int);
106 static struct pr_usrreqs ctl_usrreqs
=
108 pru_abort_notsupp
, pru_accept_notsupp
, ctl_attach
, pru_bind_notsupp
,
109 ctl_connect
, pru_connect2_notsupp
, ctl_ioctl
, ctl_detach
,
110 ctl_disconnect
, pru_listen_notsupp
, ctl_peeraddr
,
111 pru_rcvd_notsupp
, pru_rcvoob_notsupp
, ctl_send
,
112 pru_sense_null
, pru_shutdown_notsupp
, pru_sockaddr_notsupp
,
113 sosend
, soreceive
, pru_sopoll_notsupp
116 static struct protosw kctlswk_dgram
=
118 SOCK_DGRAM
, &systemdomain
, SYSPROTO_CONTROL
,
119 PR_ATOMIC
|PR_CONNREQUIRED
|PR_PCBLOCK
,
120 NULL
, NULL
, NULL
, ctl_ctloutput
,
122 NULL
, NULL
, NULL
, NULL
, &ctl_usrreqs
,
123 ctl_lock
, ctl_unlock
, ctl_getlock
, { 0, 0 } , 0, { 0 }
126 static struct protosw kctlswk_stream
=
128 SOCK_STREAM
, &systemdomain
, SYSPROTO_CONTROL
,
129 PR_CONNREQUIRED
|PR_PCBLOCK
,
130 NULL
, NULL
, NULL
, ctl_ctloutput
,
132 NULL
, NULL
, NULL
, NULL
, &ctl_usrreqs
,
133 ctl_lock
, ctl_unlock
, ctl_getlock
, { 0, 0 } , 0, { 0 }
138 * Install the protosw's for the Kernel Control manager.
140 __private_extern__
int
141 kern_control_init(void)
145 ctl_lck_grp_attr
= lck_grp_attr_alloc_init();
146 if (ctl_lck_grp_attr
== 0) {
147 printf(": lck_grp_attr_alloc_init failed\n");
152 ctl_lck_grp
= lck_grp_alloc_init("Kernel Control Protocol", ctl_lck_grp_attr
);
153 if (ctl_lck_grp
== 0) {
154 printf("kern_control_init: lck_grp_alloc_init failed\n");
159 ctl_lck_attr
= lck_attr_alloc_init();
160 if (ctl_lck_attr
== 0) {
161 printf("kern_control_init: lck_attr_alloc_init failed\n");
166 ctl_mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
168 printf("kern_control_init: lck_mtx_alloc_init failed\n");
172 TAILQ_INIT(&ctl_head
);
174 error
= net_add_proto(&kctlswk_dgram
, &systemdomain
);
176 log(LOG_WARNING
, "kern_control_init: net_add_proto dgram failed (%d)\n", error
);
178 error
= net_add_proto(&kctlswk_stream
, &systemdomain
);
180 log(LOG_WARNING
, "kern_control_init: net_add_proto stream failed (%d)\n", error
);
186 lck_mtx_free(ctl_mtx
, ctl_lck_grp
);
190 lck_grp_free(ctl_lck_grp
);
193 if (ctl_lck_grp_attr
) {
194 lck_grp_attr_free(ctl_lck_grp_attr
);
195 ctl_lck_grp_attr
= 0;
198 lck_attr_free(ctl_lck_attr
);
206 kcb_delete(struct ctl_cb
*kcb
)
210 lck_mtx_free(kcb
->mtx
, ctl_lck_grp
);
217 * Kernel Controller user-request functions
218 * attach function must exist and succeed
219 * detach not necessary
220 * we need a pcb for the per socket mutex
223 ctl_attach(__unused
struct socket
*so
, __unused
int proto
, __unused
struct proc
*p
)
226 struct ctl_cb
*kcb
= 0;
228 MALLOC(kcb
, struct ctl_cb
*, sizeof(struct ctl_cb
), M_TEMP
, M_WAITOK
);
233 bzero(kcb
, sizeof(struct ctl_cb
));
235 kcb
->mtx
= lck_mtx_alloc_init(ctl_lck_grp
, ctl_lck_attr
);
236 if (kcb
->mtx
== NULL
) {
241 so
->so_pcb
= (caddr_t
)kcb
;
252 ctl_sofreelastref(struct socket
*so
)
254 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
260 if ((kctl
= kcb
->kctl
) != 0) {
261 lck_mtx_lock(ctl_mtx
);
262 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
263 lck_mtx_lock(ctl_mtx
);
271 ctl_detach(struct socket
*so
)
273 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
278 soisdisconnected(so
);
279 so
->so_flags
|= SOF_PCBCLEARING
;
285 ctl_connect(struct socket
*so
, struct sockaddr
*nam
, __unused
struct proc
*p
)
289 struct sockaddr_ctl sa
;
290 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
293 panic("ctl_connect so_pcb null\n");
295 if (nam
->sa_len
!= sizeof(struct sockaddr_ctl
))
298 bcopy(nam
, &sa
, sizeof(struct sockaddr_ctl
));
300 lck_mtx_lock(ctl_mtx
);
301 kctl
= ctl_find_by_id_unit(sa
.sc_id
, sa
.sc_unit
);
303 lck_mtx_unlock(ctl_mtx
);
307 if (((kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) && (so
->so_type
!= SOCK_STREAM
)) ||
308 (!(kctl
->flags
& CTL_FLAG_REG_SOCK_STREAM
) && (so
->so_type
!= SOCK_DGRAM
))) {
309 lck_mtx_unlock(ctl_mtx
);
313 if (kctl
->flags
& CTL_FLAG_PRIVILEGED
) {
315 lck_mtx_unlock(ctl_mtx
);
318 if ((error
= proc_suser(p
))) {
319 lck_mtx_unlock(ctl_mtx
);
324 if ((kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) || sa
.sc_unit
!= 0) {
325 if (kcb_find(kctl
, sa
.sc_unit
) != NULL
) {
326 lck_mtx_unlock(ctl_mtx
);
330 u_int32_t unit
= kctl
->lastunit
+ 1;
333 if (unit
== ctl_maxunit
)
335 if (kcb_find(kctl
, unit
) == NULL
) {
336 kctl
->lastunit
= sa
.sc_unit
= unit
;
339 if (unit
++ == kctl
->lastunit
) {
340 lck_mtx_unlock(ctl_mtx
);
346 kcb
->unit
= sa
.sc_unit
;
348 TAILQ_INSERT_TAIL(&kctl
->kcb_head
, kcb
, next
);
349 lck_mtx_unlock(ctl_mtx
);
351 error
= soreserve(so
, kctl
->sendbufsize
, kctl
->recvbufsize
);
356 socket_unlock(so
, 0);
357 error
= (*kctl
->connect
)(kctl
, &sa
, &kcb
->userdata
);
366 soisdisconnected(so
);
367 lck_mtx_lock(ctl_mtx
);
370 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
371 lck_mtx_unlock(ctl_mtx
);
377 ctl_disconnect(struct socket
*so
)
379 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
381 if ((kcb
= (struct ctl_cb
*)so
->so_pcb
)) {
382 struct kctl
*kctl
= kcb
->kctl
;
384 if (kctl
&& kctl
->disconnect
) {
385 socket_unlock(so
, 0);
386 (*kctl
->disconnect
)(kctl
, kcb
->unit
, kcb
->userdata
);
389 lck_mtx_lock(ctl_mtx
);
392 TAILQ_REMOVE(&kctl
->kcb_head
, kcb
, next
);
393 soisdisconnected(so
);
394 lck_mtx_unlock(ctl_mtx
);
400 ctl_peeraddr(struct socket
*so
, struct sockaddr
**nam
)
402 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
404 struct sockaddr_ctl sc
;
406 if (kcb
== NULL
) /* sanity check */
409 if ((kctl
= kcb
->kctl
) == NULL
)
412 bzero(&sc
, sizeof(struct sockaddr_ctl
));
413 sc
.sc_len
= sizeof(struct sockaddr_ctl
);
414 sc
.sc_family
= AF_SYSTEM
;
415 sc
.ss_sysaddr
= AF_SYS_CONTROL
;
417 sc
.sc_unit
= kcb
->unit
;
419 *nam
= dup_sockaddr((struct sockaddr
*)&sc
, 1);
425 ctl_send(struct socket
*so
, int flags
, struct mbuf
*m
,
426 __unused
struct sockaddr
*addr
, __unused
struct mbuf
*control
,
427 __unused
struct proc
*p
)
430 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
433 if (kcb
== NULL
) /* sanity check */
436 if ((kctl
= kcb
->kctl
) == NULL
)
440 socket_unlock(so
, 0);
441 error
= (*kctl
->send
)(kctl
, kcb
->unit
, kcb
->userdata
, m
, flags
);
448 ctl_enqueuembuf(void *kctlref
, u_int32_t unit
, struct mbuf
*m
, u_int32_t flags
)
453 struct kctl
*kctl
= (struct kctl
*)kctlref
;
458 kcb
= kcb_find(kctl
, unit
);
462 so
= (struct socket
*)kcb
->so
;
467 if (sbspace(&so
->so_rcv
) < m
->m_pkthdr
.len
) {
471 if ((flags
& CTL_DATA_EOR
))
473 if (sbappend(&so
->so_rcv
, m
) && (flags
& CTL_DATA_NOWAKEUP
) == 0)
476 socket_unlock(so
, 1);
481 ctl_enqueuedata(void *kctlref
, u_int32_t unit
, void *data
, size_t len
, u_int32_t flags
)
487 struct kctl
*kctl
= (struct kctl
*)kctlref
;
488 unsigned int num_needed
;
495 kcb
= kcb_find(kctl
, unit
);
499 so
= (struct socket
*)kcb
->so
;
504 if ((size_t)sbspace(&so
->so_rcv
) < len
) {
510 m
= m_allocpacket_internal(&num_needed
, len
, NULL
, M_NOWAIT
, 1, 0);
512 printf("ctl_enqueuedata: m_allocpacket_internal(%lu) failed\n", len
);
517 for (n
= m
; n
!= NULL
; n
= n
->m_next
) {
518 size_t mlen
= mbuf_maxlen(n
);
520 if (mlen
+ curlen
> len
)
523 bcopy((char *)data
+ curlen
, n
->m_data
, mlen
);
526 mbuf_pkthdr_setlen(m
, curlen
);
528 if ((flags
& CTL_DATA_EOR
))
530 if (sbappend(&so
->so_rcv
, m
) && (flags
& CTL_DATA_NOWAKEUP
) == 0)
533 socket_unlock(so
, 1);
539 ctl_getenqueuespace(kern_ctl_ref kctlref
, u_int32_t unit
, size_t *space
)
542 struct kctl
*kctl
= (struct kctl
*)kctlref
;
545 if (kctlref
== NULL
|| space
== NULL
)
548 kcb
= kcb_find(kctl
, unit
);
552 so
= (struct socket
*)kcb
->so
;
557 *space
= sbspace(&so
->so_rcv
);
558 socket_unlock(so
, 1);
564 ctl_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
566 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
572 if (sopt
->sopt_level
!= SYSPROTO_CONTROL
) {
576 if (kcb
== NULL
) /* sanity check */
579 if ((kctl
= kcb
->kctl
) == NULL
)
582 switch (sopt
->sopt_dir
) {
584 if (kctl
->setopt
== NULL
)
586 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
, M_WAITOK
);
589 error
= sooptcopyin(sopt
, data
, sopt
->sopt_valsize
, sopt
->sopt_valsize
);
591 socket_unlock(so
, 0);
592 error
= (*kctl
->setopt
)(kcb
->kctl
, kcb
->unit
, kcb
->userdata
, sopt
->sopt_name
,
593 data
, sopt
->sopt_valsize
);
600 if (kctl
->getopt
== NULL
)
603 if (sopt
->sopt_valsize
&& sopt
->sopt_val
) {
604 MALLOC(data
, void *, sopt
->sopt_valsize
, M_TEMP
, M_WAITOK
);
607 /* 4108337 - copy in data for get socket option */
608 error
= sooptcopyin(sopt
, data
, sopt
->sopt_valsize
, sopt
->sopt_valsize
);
610 len
= sopt
->sopt_valsize
;
611 socket_unlock(so
, 0);
612 error
= (*kctl
->getopt
)(kcb
->kctl
, kcb
->unit
, kcb
->userdata
, sopt
->sopt_name
,
617 error
= sooptcopyout(sopt
, data
, len
);
619 sopt
->sopt_valsize
= len
;
629 ctl_ioctl(__unused
struct socket
*so
, u_long cmd
, caddr_t data
,
630 __unused
struct ifnet
*ifp
, __unused
struct proc
*p
)
635 /* get the number of controllers */
640 lck_mtx_lock(ctl_mtx
);
641 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
643 lck_mtx_unlock(ctl_mtx
);
645 *(u_int32_t
*)data
= n
;
650 struct ctl_info
*ctl_info
= (struct ctl_info
*)data
;
651 struct kctl
*kctl
= 0;
652 size_t name_len
= strlen(ctl_info
->ctl_name
);
654 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
) {
658 lck_mtx_lock(ctl_mtx
);
659 kctl
= ctl_find_by_name(ctl_info
->ctl_name
);
660 lck_mtx_unlock(ctl_mtx
);
665 ctl_info
->ctl_id
= kctl
->id
;
670 /* add controls to get list of NKEs */
678 * Register/unregister a NKE
681 ctl_register(struct kern_ctl_reg
*userkctl
, kern_ctl_ref
*kctlref
)
683 struct kctl
*kctl
= 0;
688 if (userkctl
== NULL
) /* sanity check */
690 if (userkctl
->ctl_connect
== NULL
)
692 name_len
= strlen(userkctl
->ctl_name
);
693 if (name_len
== 0 || name_len
+ 1 > MAX_KCTL_NAME
)
696 MALLOC(kctl
, struct kctl
*, sizeof(*kctl
), M_TEMP
, M_WAITOK
);
699 bzero((char *)kctl
, sizeof(*kctl
));
701 lck_mtx_lock(ctl_mtx
);
703 if ((userkctl
->ctl_flags
& CTL_FLAG_REG_ID_UNIT
) == 0) {
704 if (ctl_find_by_name(userkctl
->ctl_name
) != NULL
) {
705 lck_mtx_unlock(ctl_mtx
);
709 for (n
= 0, id
= ctl_last_id
+ 1; n
< ctl_max
; id
++, n
++) {
714 if (ctl_find_by_id(id
) == 0)
718 lck_mtx_unlock(ctl_mtx
);
722 userkctl
->ctl_id
=id
;
726 if (ctl_find_by_id_unit(userkctl
->ctl_id
, userkctl
->ctl_unit
) != NULL
) {
727 lck_mtx_unlock(ctl_mtx
);
731 kctl
->id
= userkctl
->ctl_id
;
732 kctl
->reg_unit
= userkctl
->ctl_unit
;
734 strcpy(kctl
->name
, userkctl
->ctl_name
);
735 kctl
->flags
= userkctl
->ctl_flags
;
737 /* Let the caller know the default send and receive sizes */
738 if (userkctl
->ctl_sendsize
== 0)
739 userkctl
->ctl_sendsize
= CTL_SENDSIZE
;
740 kctl
->sendbufsize
= userkctl
->ctl_sendsize
;
742 if (userkctl
->ctl_recvsize
== 0)
743 userkctl
->ctl_recvsize
= CTL_RECVSIZE
;
744 kctl
->recvbufsize
= userkctl
->ctl_recvsize
;
746 kctl
->connect
= userkctl
->ctl_connect
;
747 kctl
->disconnect
= userkctl
->ctl_disconnect
;
748 kctl
->send
= userkctl
->ctl_send
;
749 kctl
->setopt
= userkctl
->ctl_setopt
;
750 kctl
->getopt
= userkctl
->ctl_getopt
;
752 TAILQ_INIT(&kctl
->kcb_head
);
754 TAILQ_INSERT_TAIL(&ctl_head
, kctl
, next
);
757 lck_mtx_unlock(ctl_mtx
);
761 ctl_post_msg(KEV_CTL_REGISTERED
, kctl
->id
);
766 ctl_deregister(void *kctlref
)
770 if (kctlref
== NULL
) /* sanity check */
773 lck_mtx_lock(ctl_mtx
);
774 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
775 if (kctl
== (struct kctl
*)kctlref
)
778 if (kctl
!= (struct kctl
*)kctlref
) {
779 lck_mtx_unlock(ctl_mtx
);
782 if (!TAILQ_EMPTY(&kctl
->kcb_head
)) {
783 lck_mtx_unlock(ctl_mtx
);
787 TAILQ_REMOVE(&ctl_head
, kctl
, next
);
790 lck_mtx_unlock(ctl_mtx
);
792 ctl_post_msg(KEV_CTL_DEREGISTERED
, kctl
->id
);
798 * Must be called with global lock taked
801 ctl_find_by_id(u_int32_t id
)
805 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
813 * Must be called with global ctl_mtx lock taked
816 ctl_find_by_name(const char *name
)
820 TAILQ_FOREACH(kctl
, &ctl_head
, next
)
821 if (strcmp(kctl
->name
, name
) == 0)
828 * Must be called with global ctl_mtx lock taked
832 ctl_find_by_id_unit(u_int32_t id
, u_int32_t unit
)
836 TAILQ_FOREACH(kctl
, &ctl_head
, next
) {
837 if (kctl
->id
== id
&& (kctl
->flags
& CTL_FLAG_REG_ID_UNIT
) == 0)
839 else if (kctl
->id
== id
&& kctl
->reg_unit
== unit
)
846 * Must be called with kernel controller lock taken
848 static struct ctl_cb
*
849 kcb_find(struct kctl
*kctl
, u_int32_t unit
)
853 TAILQ_FOREACH(kcb
, &kctl
->kcb_head
, next
)
854 if ((kcb
->unit
== unit
))
861 * Must be called witout lock
864 ctl_post_msg(u_long event_code
, u_int32_t id
)
866 struct ctl_event_data ctl_ev_data
;
867 struct kev_msg ev_msg
;
869 ev_msg
.vendor_code
= KEV_VENDOR_APPLE
;
871 ev_msg
.kev_class
= KEV_SYSTEM_CLASS
;
872 ev_msg
.kev_subclass
= KEV_CTL_SUBCLASS
;
873 ev_msg
.event_code
= event_code
;
875 /* common nke subclass data */
876 bzero(&ctl_ev_data
, sizeof(ctl_ev_data
));
877 ctl_ev_data
.ctl_id
= id
;
878 ev_msg
.dv
[0].data_ptr
= &ctl_ev_data
;
879 ev_msg
.dv
[0].data_length
= sizeof(ctl_ev_data
);
881 ev_msg
.dv
[1].data_length
= 0;
883 kev_post_msg(&ev_msg
);
887 ctl_lock(struct socket
*so
, int refcount
, int lr
)
891 lr_saved
= (unsigned int) __builtin_return_address(0);
895 lck_mtx_lock(((struct ctl_cb
*)so
->so_pcb
)->mtx
);
897 panic("ctl_lock: so=%x NO PCB! lr=%x\n", so
, lr_saved
);
898 lck_mtx_lock(so
->so_proto
->pr_domain
->dom_mtx
);
901 if (so
->so_usecount
< 0)
902 panic("ctl_lock: so=%x so_pcb=%x lr=%x ref=%x\n",
903 so
, so
->so_pcb
, lr_saved
, so
->so_usecount
);
908 so
->lock_lr
[so
->next_lock_lr
] = (void *)lr_saved
;
909 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
914 ctl_unlock(struct socket
*so
, int refcount
, int lr
)
917 lck_mtx_t
* mutex_held
;
920 lr_saved
= (unsigned int) __builtin_return_address(0);
923 #ifdef MORE_KCTLLOCK_DEBUG
924 printf("ctl_unlock: so=%x sopcb=%x lock=%x ref=%x lr=%x\n",
925 so
, so
->so_pcb
, ((struct ctl_cb
*)so
->so_pcb
)->mtx
, so
->so_usecount
, lr_saved
);
930 if (so
->so_usecount
< 0)
931 panic("ctl_unlock: so=%x usecount=%x\n", so
, so
->so_usecount
);
932 if (so
->so_pcb
== NULL
) {
933 panic("ctl_unlock: so=%x NO PCB usecount=%x lr=%x\n", so
, so
->so_usecount
, lr_saved
);
934 mutex_held
= so
->so_proto
->pr_domain
->dom_mtx
;
936 mutex_held
= ((struct ctl_cb
*)so
->so_pcb
)->mtx
;
938 lck_mtx_assert(mutex_held
, LCK_MTX_ASSERT_OWNED
);
939 so
->unlock_lr
[so
->next_unlock_lr
] = (void *)lr_saved
;
940 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
941 lck_mtx_unlock(mutex_held
);
943 if (so
->so_usecount
== 0)
944 ctl_sofreelastref(so
);
950 ctl_getlock(struct socket
*so
, __unused
int locktype
)
952 struct ctl_cb
*kcb
= (struct ctl_cb
*)so
->so_pcb
;
955 if (so
->so_usecount
< 0)
956 panic("ctl_getlock: so=%x usecount=%x\n", so
, so
->so_usecount
);
959 panic("ctl_getlock: so=%x NULL so_pcb\n", so
);
960 return (so
->so_proto
->pr_domain
->dom_mtx
);