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@
24 * Modification History
26 * June 1, 2001 Allan Nathanson <ajn@apple.com>
27 * - public API conversion
29 * March 31, 2000 Allan Nathanson <ajn@apple.com>
33 #include <mach/mach.h>
34 #include <mach/mach_error.h>
36 #include <SystemConfiguration/SystemConfiguration.h>
37 #include <SystemConfiguration/SCPrivate.h>
38 #include "SCDynamicStoreInternal.h"
39 #include "config.h" /* MiG generated file */
42 waitForMachMessage(mach_port_t port
)
45 mach_msg_empty_rcv_t
*buf
;
47 mach_msg_size_t size
= sizeof(mach_msg_empty_t
) + MAX_TRAILER_SIZE
;
49 status
= vm_allocate(mach_task_self(), (vm_address_t
*)&buf
, size
, TRUE
);
50 if (status
!= KERN_SUCCESS
) {
51 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("vm_allocate(): %s"), mach_error_string(status
));
55 status
= mach_msg(&buf
->header
, /* msg */
56 MACH_RCV_MSG
, /* options */
60 MACH_MSG_TIMEOUT_NONE
, /* timeout */
61 MACH_PORT_NULL
); /* notify */
62 if (status
!= KERN_SUCCESS
) {
63 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("mach_msg(): %s"), mach_error_string(status
));
67 return buf
->header
.msgh_id
;
72 SCDynamicStoreNotifyWait(SCDynamicStoreRef store
)
74 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
77 mach_port_t oldNotify
;
81 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("SCDynamicStoreNotifyWait:"));
84 /* sorry, you must provide a session */
85 _SCErrorSet(kSCStatusNoStoreSession
);
89 if (storePrivate
->server
== MACH_PORT_NULL
) {
90 /* sorry, you must have an open session to play */
91 _SCErrorSet(kSCStatusNoStoreServer
);
95 if (storePrivate
->notifyStatus
!= NotifierNotRegistered
) {
96 /* sorry, you can only have one notification registered at once */
97 _SCErrorSet(kSCStatusNotifierActive
);
101 /* Allocating port (for server response) */
102 status
= mach_port_allocate(mach_task_self(), MACH_PORT_RIGHT_RECEIVE
, &port
);
103 if (status
!= KERN_SUCCESS
) {
104 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("mach_port_allocate(): %s"), mach_error_string(status
));
108 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" port = %d"), port
);
110 status
= mach_port_insert_right(mach_task_self(),
113 MACH_MSG_TYPE_MAKE_SEND
);
114 if (status
!= KERN_SUCCESS
) {
115 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("mach_port_insert_right(): %s"), mach_error_string(status
));
116 (void) mach_port_destroy(mach_task_self(), port
);
121 /* Request a notification when/if the server dies */
122 status
= mach_port_request_notification(mach_task_self(),
124 MACH_NOTIFY_NO_SENDERS
,
127 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
129 if (status
!= KERN_SUCCESS
) {
130 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("mach_port_request_notification(): %s"), mach_error_string(status
));
131 (void) mach_port_destroy(mach_task_self(), port
);
136 if (oldNotify
!= MACH_PORT_NULL
) {
137 SCLog(_sc_verbose
, LOG_ERR
, CFSTR("SCDynamicStoreNotifyWait(): why is oldNotify != MACH_PORT_NULL?"));
140 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("Requesting notification via mach port %d"), port
);
141 status
= notifyviaport(storePrivate
->server
,
146 if (status
!= KERN_SUCCESS
) {
147 if (status
!= MACH_SEND_INVALID_DEST
)
148 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("notifyviaport(): %s"), mach_error_string(status
));
149 (void) mach_port_destroy(mach_task_self(), storePrivate
->server
);
150 storePrivate
->server
= MACH_PORT_NULL
;
155 if (sc_status
!= kSCStatusOK
) {
156 _SCErrorSet(sc_status
);
160 /* set notifier active */
161 storePrivate
->notifyStatus
= Using_NotifierWait
;
163 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("Waiting..."));
165 msgid
= waitForMachMessage(port
);
167 /* set notifier inactive */
168 storePrivate
->notifyStatus
= NotifierNotRegistered
;
170 if (msgid
== MACH_NOTIFY_NO_SENDERS
) {
171 /* the server closed the notifier port */
172 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" notifier port closed, destroying port %d"), port
);
173 _SCErrorSet(kSCStatusNoStoreServer
);
178 /* one of the mach routines returned an error */
179 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR(" communication with server failed, destroying port %d"), port
);
180 (void) mach_port_destroy(mach_task_self(), port
);
181 _SCErrorSet(kSCStatusNoStoreServer
);
185 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("Something changed, cancelling notification request"));
186 status
= notifycancel(storePrivate
->server
,
189 if (status
!= KERN_SUCCESS
) {
190 if (status
!= MACH_SEND_INVALID_DEST
)
191 SCLog(_sc_verbose
, LOG_DEBUG
, CFSTR("notifycancel(): %s"), mach_error_string(status
));
192 (void) mach_port_destroy(mach_task_self(), storePrivate
->server
);
193 storePrivate
->server
= MACH_PORT_NULL
;
198 (void) mach_port_destroy(mach_task_self(), port
);