2 * Copyright (c) 2000 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@
23 #include <mach/mach.h>
24 #include <mach/notify.h>
25 #include <mach/mach_error.h>
28 #include "SCDPrivate.h"
31 __private_extern__ mach_msg_id_t
32 _waitForMachMessage(mach_port_t port
)
35 mach_msg_empty_rcv_t
*buf
;
37 mach_msg_size_t size
= sizeof(mach_msg_empty_t
) + MAX_TRAILER_SIZE
;
39 status
= vm_allocate(mach_task_self(), (vm_address_t
*)&buf
, size
, TRUE
);
40 if (status
!= KERN_SUCCESS
) {
41 SCDLog(LOG_DEBUG
, CFSTR("vm_allocate(): %s"), mach_error_string(status
));
45 status
= mach_msg(&buf
->header
, /* msg */
46 MACH_RCV_MSG
, /* options */
50 MACH_MSG_TIMEOUT_NONE
, /* timeout */
51 MACH_PORT_NULL
); /* notify */
52 if (status
!= KERN_SUCCESS
) {
53 SCDLog(LOG_DEBUG
, CFSTR("mach_msg(): %s"), mach_error_string(status
));
57 return buf
->header
.msgh_id
;
65 /* print status of in-use mach ports */
66 if (SCDOptionGet(NULL
, kSCDOptionDebug
) && SCDOptionGet(NULL
, kSCDOptionVerbose
)) {
68 mach_port_name_array_t ports
;
69 mach_port_type_array_t types
;
71 CFMutableStringRef str
;
73 SCDLog(LOG_DEBUG
, CFSTR("----------"));
75 /* report on ALL mach ports associated with this task */
76 status
= mach_port_names(mach_task_self(), &ports
, &pn
, &types
, &tn
);
77 if (status
== MACH_MSG_SUCCESS
) {
78 str
= CFStringCreateMutable(NULL
, 0);
79 for (pi
=0; pi
< pn
; pi
++) {
80 char rights
[16], *rp
= &rights
[0];
82 if (types
[pi
] != MACH_PORT_TYPE_NONE
) {
85 if (types
[pi
] & MACH_PORT_TYPE_SEND
)
87 if (types
[pi
] & MACH_PORT_TYPE_RECEIVE
)
89 if (types
[pi
] & MACH_PORT_TYPE_SEND_ONCE
)
91 if (types
[pi
] & MACH_PORT_TYPE_PORT_SET
)
93 if (types
[pi
] & MACH_PORT_TYPE_DEAD_NAME
)
98 CFStringAppendFormat(str
, NULL
, CFSTR(" %d%s"), ports
[pi
], rights
);
100 SCDLog(LOG_DEBUG
, CFSTR("Task ports (n=%d):%@"), pn
, str
);
103 /* log (but ignore) errors */
104 SCDLog(LOG_DEBUG
, CFSTR("mach_port_names(): %s"), mach_error_string(status
));
113 _showMachPortReferences(mach_port_t port
)
116 kern_return_t status
;
117 mach_port_urefs_t refs_send
= 0;
118 mach_port_urefs_t refs_recv
= 0;
119 mach_port_urefs_t refs_once
= 0;
120 mach_port_urefs_t refs_pset
= 0;
121 mach_port_urefs_t refs_dead
= 0;
123 SCDLog(LOG_DEBUG
, CFSTR("user references for mach port %d"), port
);
125 status
= mach_port_get_refs(mach_task_self(), port
, MACH_PORT_RIGHT_SEND
, &refs_send
);
126 if (status
!= KERN_SUCCESS
) {
127 SCDLog(LOG_DEBUG
, CFSTR(" mach_port_get_refs(MACH_PORT_RIGHT_SEND): %s"), mach_error_string(status
));
131 status
= mach_port_get_refs(mach_task_self(), port
, MACH_PORT_RIGHT_RECEIVE
, &refs_recv
);
132 if (status
!= KERN_SUCCESS
) {
133 SCDLog(LOG_DEBUG
, CFSTR(" mach_port_get_refs(MACH_PORT_RIGHT_RECEIVE): %s"), mach_error_string(status
));
137 status
= mach_port_get_refs(mach_task_self(), port
, MACH_PORT_RIGHT_SEND_ONCE
, &refs_once
);
138 if (status
!= KERN_SUCCESS
) {
139 SCDLog(LOG_DEBUG
, CFSTR(" mach_port_get_refs(MACH_PORT_RIGHT_SEND_ONCE): %s"), mach_error_string(status
));
143 status
= mach_port_get_refs(mach_task_self(), port
, MACH_PORT_RIGHT_PORT_SET
, &refs_pset
);
144 if (status
!= KERN_SUCCESS
) {
145 SCDLog(LOG_DEBUG
, CFSTR(" mach_port_get_refs(MACH_PORT_RIGHT_PORT_SET): %s"), mach_error_string(status
));
149 status
= mach_port_get_refs(mach_task_self(), port
, MACH_PORT_RIGHT_DEAD_NAME
, &refs_dead
);
150 if (status
!= KERN_SUCCESS
) {
151 SCDLog(LOG_DEBUG
, CFSTR(" mach_port_get_refs(MACH_PORT_RIGHT_DEAD_NAME): %s"), mach_error_string(status
));
156 CFSTR(" send = %d, receive = %d, send once = %d, port set = %d, dead name = %d"),