2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
27 * Modification History
29 * June 1, 2001 Allan Nathanson <ajn@apple.com>
30 * - public API conversion
32 * March 31, 2000 Allan Nathanson <ajn@apple.com>
36 #include <mach/mach.h>
37 #include <mach/mach_error.h>
39 #include <SystemConfiguration/SystemConfiguration.h>
40 #include <SystemConfiguration/SCPrivate.h>
41 #include "SCDynamicStoreInternal.h"
42 #include "config.h" /* MiG generated file */
45 waitForMachMessage(mach_port_t port
)
48 mach_msg_empty_rcv_t
*buf
;
50 mach_msg_size_t size
= sizeof(mach_msg_empty_t
) + MAX_TRAILER_SIZE
;
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
));
58 status
= mach_msg(&buf
->header
, /* msg */
59 MACH_RCV_MSG
, /* options */
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
));
70 return buf
->header
.msgh_id
;
75 SCDynamicStoreNotifyWait(SCDynamicStoreRef store
)
77 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
80 mach_port_t oldNotify
;
84 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("SCDynamicStoreNotifyWait:"));
87 /* sorry, you must provide a session */
88 _SCErrorSet(kSCStatusNoStoreSession
);
92 if (storePrivate
->server
== MACH_PORT_NULL
) {
93 /* sorry, you must have an open session to play */
94 _SCErrorSet(kSCStatusNoStoreServer
);
98 if (storePrivate
->notifyStatus
!= NotifierNotRegistered
) {
99 /* sorry, you can only have one notification registered at once */
100 _SCErrorSet(kSCStatusNotifierActive
);
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
));
111 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" port = %d"), port
);
113 status
= mach_port_insert_right(mach_task_self(),
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
);
124 /* Request a notification when/if the server dies */
125 status
= mach_port_request_notification(mach_task_self(),
127 MACH_NOTIFY_NO_SENDERS
,
130 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
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
);
139 if (oldNotify
!= MACH_PORT_NULL
) {
140 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCDynamicStoreNotifyWait(): why is oldNotify != MACH_PORT_NULL?"));
143 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("Requesting notification via mach port %d"), port
);
144 status
= notifyviaport(storePrivate
->server
,
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
;
158 if (sc_status
!= kSCStatusOK
) {
159 _SCErrorSet(sc_status
);
163 /* set notifier active */
164 storePrivate
->notifyStatus
= Using_NotifierWait
;
166 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("Waiting..."));
168 msgid
= waitForMachMessage(port
);
170 /* set notifier inactive */
171 storePrivate
->notifyStatus
= NotifierNotRegistered
;
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
);
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
);
188 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("Something changed, cancelling notification request"));
189 status
= notifycancel(storePrivate
->server
,
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
;
201 (void) mach_port_destroy(mach_task_self(), port
);