]> git.saurik.com Git - apple/configd.git/blob - SystemConfiguration.fproj/SCDNotifierWait.c
c0992dcbbd0b04da05c609041d2b7e480cf98524
[apple/configd.git] / SystemConfiguration.fproj / SCDNotifierWait.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26 /*
27 * Modification History
28 *
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
31 *
32 * March 31, 2000 Allan Nathanson <ajn@apple.com>
33 * - initial revision
34 */
35
36 #include <mach/mach.h>
37 #include <mach/mach_error.h>
38
39 #include <SystemConfiguration/SystemConfiguration.h>
40 #include <SystemConfiguration/SCPrivate.h>
41 #include "SCDynamicStoreInternal.h"
42 #include "config.h" /* MiG generated file */
43
44 static mach_msg_id_t
45 waitForMachMessage(mach_port_t port)
46 {
47 kern_return_t status;
48 mach_msg_empty_rcv_t *buf;
49
50 mach_msg_size_t size = sizeof(mach_msg_empty_t) + MAX_TRAILER_SIZE;
51
52 status = vm_allocate(mach_task_self(), (vm_address_t *)&buf, size, TRUE);
53 if (status != KERN_SUCCESS) {
54 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("vm_allocate(): %s"), mach_error_string(status));
55 return -1;
56 }
57
58 status = mach_msg(&buf->header, /* msg */
59 MACH_RCV_MSG, /* options */
60 0, /* send_size */
61 size, /* rcv_size */
62 port, /* rcv_name */
63 MACH_MSG_TIMEOUT_NONE, /* timeout */
64 MACH_PORT_NULL); /* notify */
65 if (status != KERN_SUCCESS) {
66 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("mach_msg(): %s"), mach_error_string(status));
67 return -1;
68 }
69
70 return buf->header.msgh_id;
71 }
72
73
74 Boolean
75 SCDynamicStoreNotifyWait(SCDynamicStoreRef store)
76 {
77 SCDynamicStorePrivateRef storePrivate = (SCDynamicStorePrivateRef)store;
78 kern_return_t status;
79 mach_port_t port;
80 mach_port_t oldNotify;
81 int sc_status;
82 mach_msg_id_t msgid;
83
84 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("SCDynamicStoreNotifyWait:"));
85
86 if (!store) {
87 /* sorry, you must provide a session */
88 _SCErrorSet(kSCStatusNoStoreSession);
89 return FALSE;
90 }
91
92 if (storePrivate->server == MACH_PORT_NULL) {
93 /* sorry, you must have an open session to play */
94 _SCErrorSet(kSCStatusNoStoreServer);
95 return FALSE;
96 }
97
98 if (storePrivate->notifyStatus != NotifierNotRegistered) {
99 /* sorry, you can only have one notification registered at once */
100 _SCErrorSet(kSCStatusNotifierActive);
101 return FALSE;
102 }
103
104 /* Allocating port (for server response) */
105 status = mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE, &port);
106 if (status != KERN_SUCCESS) {
107 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("mach_port_allocate(): %s"), mach_error_string(status));
108 _SCErrorSet(status);
109 return FALSE;
110 }
111 SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" port = %d"), port);
112
113 status = mach_port_insert_right(mach_task_self(),
114 port,
115 port,
116 MACH_MSG_TYPE_MAKE_SEND);
117 if (status != KERN_SUCCESS) {
118 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("mach_port_insert_right(): %s"), mach_error_string(status));
119 (void) mach_port_destroy(mach_task_self(), port);
120 _SCErrorSet(status);
121 return FALSE;
122 }
123
124 /* Request a notification when/if the server dies */
125 status = mach_port_request_notification(mach_task_self(),
126 port,
127 MACH_NOTIFY_NO_SENDERS,
128 1,
129 port,
130 MACH_MSG_TYPE_MAKE_SEND_ONCE,
131 &oldNotify);
132 if (status != KERN_SUCCESS) {
133 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("mach_port_request_notification(): %s"), mach_error_string(status));
134 (void) mach_port_destroy(mach_task_self(), port);
135 _SCErrorSet(status);
136 return FALSE;
137 }
138
139 if (oldNotify != MACH_PORT_NULL) {
140 SCLog(_sc_verbose, LOG_ERR, CFSTR("SCDynamicStoreNotifyWait(): why is oldNotify != MACH_PORT_NULL?"));
141 }
142
143 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("Requesting notification via mach port %d"), port);
144 status = notifyviaport(storePrivate->server,
145 port,
146 0,
147 (int *)&sc_status);
148
149 if (status != KERN_SUCCESS) {
150 if (status != MACH_SEND_INVALID_DEST)
151 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("notifyviaport(): %s"), mach_error_string(status));
152 (void) mach_port_destroy(mach_task_self(), storePrivate->server);
153 storePrivate->server = MACH_PORT_NULL;
154 _SCErrorSet(status);
155 return FALSE;
156 }
157
158 if (sc_status != kSCStatusOK) {
159 _SCErrorSet(sc_status);
160 return FALSE;
161 }
162
163 /* set notifier active */
164 storePrivate->notifyStatus = Using_NotifierWait;
165
166 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("Waiting..."));
167
168 msgid = waitForMachMessage(port);
169
170 /* set notifier inactive */
171 storePrivate->notifyStatus = NotifierNotRegistered;
172
173 if (msgid == MACH_NOTIFY_NO_SENDERS) {
174 /* the server closed the notifier port */
175 SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" notifier port closed, destroying port %d"), port);
176 _SCErrorSet(kSCStatusNoStoreServer);
177 return FALSE;
178 }
179
180 if (msgid == -1) {
181 /* one of the mach routines returned an error */
182 SCLog(_sc_verbose, LOG_DEBUG, CFSTR(" communication with server failed, destroying port %d"), port);
183 (void) mach_port_destroy(mach_task_self(), port);
184 _SCErrorSet(kSCStatusNoStoreServer);
185 return FALSE;
186 }
187
188 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("Something changed, cancelling notification request"));
189 status = notifycancel(storePrivate->server,
190 (int *)&sc_status);
191
192 if (status != KERN_SUCCESS) {
193 if (status != MACH_SEND_INVALID_DEST)
194 SCLog(_sc_verbose, LOG_DEBUG, CFSTR("notifycancel(): %s"), mach_error_string(status));
195 (void) mach_port_destroy(mach_task_self(), storePrivate->server);
196 storePrivate->server = MACH_PORT_NULL;
197 _SCErrorSet(status);
198 return FALSE;
199 }
200
201 (void) mach_port_destroy(mach_task_self(), port);
202
203 return TRUE;
204 }