2 * Copyright (c) 2000-2003 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 24, 2000 Allan Nathanson <ajn@apple.com>
37 #include "configd_server.h"
42 __SCDynamicStoreNotifySignal(SCDynamicStoreRef store
, pid_t pid
, int sig
)
44 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)store
;
45 CFStringRef sessionKey
;
48 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("__SCDynamicStoreNotifySignal:"));
50 if (!store
|| (storePrivate
->server
== MACH_PORT_NULL
)) {
51 return kSCStatusNoStoreSession
; /* you must have an open session to play */
54 if (storePrivate
->notifyStatus
!= NotifierNotRegistered
) {
55 /* sorry, you can only have one notification registered at once */
56 return kSCStatusNotifierActive
;
59 if (pid
== getpid()) {
60 /* sorry, you can't request that configd be signalled */
61 return kSCStatusInvalidArgument
;
64 if ((sig
<= 0) || (sig
> NSIG
)) {
65 /* sorry, you must specify a valid signal */
66 return kSCStatusInvalidArgument
;
69 /* push out a notification if any changes are pending */
70 sessionKey
= CFStringCreateWithFormat(NULL
, NULL
, CFSTR("%d"), storePrivate
->server
);
71 info
= CFDictionaryGetValue(sessionData
, sessionKey
);
72 CFRelease(sessionKey
);
73 if (info
&& CFDictionaryContainsKey(info
, kSCDChangedKeys
)) {
74 CFNumberRef sessionNum
;
76 if (needsNotification
== NULL
)
77 needsNotification
= CFSetCreateMutable(NULL
,
79 &kCFTypeSetCallBacks
);
81 sessionNum
= CFNumberCreate(NULL
, kCFNumberIntType
, &storePrivate
->server
);
82 CFSetAddValue(needsNotification
, sessionNum
);
83 CFRelease(sessionNum
);
92 _notifyviasignal(mach_port_t server
,
97 serverSessionRef mySession
= getSession(server
);
100 SCDynamicStorePrivateRef storePrivate
= (SCDynamicStorePrivateRef
)mySession
->store
;
102 mach_port_t oldNotify
;
105 if (_configd_verbose
) {
106 SCLog(TRUE
, LOG_DEBUG
, CFSTR("Send signal when a notification key changes."));
107 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" server = %d"), server
);
108 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" task = %d"), task
);
109 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" signal = %d"), sig
);
112 status
= pid_for_task(task
, &pid
);
113 if (status
!= KERN_SUCCESS
) {
114 *sc_status
= kSCStatusFailed
; /* could not determine pid for task */
119 *sc_status
= kSCStatusNoStoreSession
; /* you must have an open session to play */
123 *sc_status
= __SCDynamicStoreNotifySignal(mySession
->store
, pid
, sig
);
124 if (*sc_status
!= kSCStatusOK
) {
125 if (task
!= TASK_NULL
) {
126 (void) mach_port_destroy(mach_task_self(), task
);
132 { mach_port_type_t pt
;
134 status
= mach_port_type(mach_task_self(), task
, &pt
);
135 if (status
== MACH_MSG_SUCCESS
) {
136 char rights
[8], *rp
= &rights
[0];
138 if (pt
& MACH_PORT_TYPE_SEND
)
140 if (pt
& MACH_PORT_TYPE_RECEIVE
)
142 if (pt
& MACH_PORT_TYPE_SEND_ONCE
)
144 if (pt
& MACH_PORT_TYPE_PORT_SET
)
146 if (pt
& MACH_PORT_TYPE_DEAD_NAME
)
150 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("Task %d, port rights = %s"), task
, rights
);
156 /* Request a notification when/if the client dies */
157 status
= mach_port_request_notification(mach_task_self(),
159 MACH_NOTIFY_DEAD_NAME
,
162 MACH_MSG_TYPE_MAKE_SEND_ONCE
,
164 if (status
!= KERN_SUCCESS
) {
165 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("mach_port_request_notification(): %s"), mach_error_string(status
));
166 *sc_status
= kSCStatusFailed
;
170 if (oldNotify
!= MACH_PORT_NULL
) {
171 SCLog(_configd_verbose
, LOG_ERR
, CFSTR("_notifyviasignal(): why is oldNotify != MACH_PORT_NULL?"));
174 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("Adding task notification port %d to the server's port set"), task
);
175 status
= mach_port_move_member(mach_task_self(), task
, server_ports
);
176 if (status
!= KERN_SUCCESS
) {
177 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("mach_port_move_member(): %s"), mach_error_string(status
));
178 *sc_status
= kSCStatusFailed
;
183 storePrivate
->notifyStatus
= Using_NotifierInformViaSignal
;
184 storePrivate
->notifySignal
= sig
;
185 storePrivate
->notifySignalTask
= task
;