]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCDPrivate.c
configd-24.1.tar.gz
[apple/configd.git] / SystemConfiguration.fproj / SCDPrivate.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22
23 #include <mach/mach.h>
24 #include <mach/notify.h>
25 #include <mach/mach_error.h>
26 #include <pthread.h>
27
28 #include "SCDPrivate.h"
29
30
31 __private_extern__ mach_msg_id_t
32 _waitForMachMessage(mach_port_t port)
33 {
34 kern_return_t status;
35 mach_msg_empty_rcv_t *buf;
36
37 mach_msg_size_t size = sizeof(mach_msg_empty_t) + MAX_TRAILER_SIZE;
38
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));
42 return -1;
43 }
44
45 status = mach_msg(&buf->header, /* msg */
46 MACH_RCV_MSG, /* options */
47 0, /* send_size */
48 size, /* rcv_size */
49 port, /* rcv_name */
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));
54 return -1;
55 }
56
57 return buf->header.msgh_id;
58 }
59
60
61 void
62 _showMachPortStatus()
63 {
64 #ifdef DEBUG
65 /* print status of in-use mach ports */
66 if (SCDOptionGet(NULL, kSCDOptionDebug) && SCDOptionGet(NULL, kSCDOptionVerbose)) {
67 kern_return_t status;
68 mach_port_name_array_t ports;
69 mach_port_type_array_t types;
70 int pi, pn, tn;
71 CFMutableStringRef str;
72
73 SCDLog(LOG_DEBUG, CFSTR("----------"));
74
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];
81
82 if (types[pi] != MACH_PORT_TYPE_NONE) {
83 *rp++ = ' ';
84 *rp++ = '(';
85 if (types[pi] & MACH_PORT_TYPE_SEND)
86 *rp++ = 'S';
87 if (types[pi] & MACH_PORT_TYPE_RECEIVE)
88 *rp++ = 'R';
89 if (types[pi] & MACH_PORT_TYPE_SEND_ONCE)
90 *rp++ = 'O';
91 if (types[pi] & MACH_PORT_TYPE_PORT_SET)
92 *rp++ = 'P';
93 if (types[pi] & MACH_PORT_TYPE_DEAD_NAME)
94 *rp++ = 'D';
95 *rp++ = ')';
96 }
97 *rp = '\0';
98 CFStringAppendFormat(str, NULL, CFSTR(" %d%s"), ports[pi], rights);
99 }
100 SCDLog(LOG_DEBUG, CFSTR("Task ports (n=%d):%@"), pn, str);
101 CFRelease(str);
102 } else {
103 /* log (but ignore) errors */
104 SCDLog(LOG_DEBUG, CFSTR("mach_port_names(): %s"), mach_error_string(status));
105 }
106 }
107 #endif /* DEBUG */
108 return;
109 }
110
111
112 void
113 _showMachPortReferences(mach_port_t port)
114 {
115 #ifdef DEBUG
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;
122
123 SCDLog(LOG_DEBUG, CFSTR("user references for mach port %d"), port);
124
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));
128 return;
129 }
130
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));
134 return;
135 }
136
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));
140 return;
141 }
142
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));
146 return;
147 }
148
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));
152 return;
153 }
154
155 SCDLog(LOG_DEBUG,
156 CFSTR(" send = %d, receive = %d, send once = %d, port set = %d, dead name = %d"),
157 refs_send,
158 refs_recv,
159 refs_once,
160 refs_pset,
161 refs_dead);
162
163 #endif /* DEBUG */
164 return;
165 }