2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
30 * March 31, 2000 Allan Nathanson <ajn@apple.com>
38 #include "configd_server.h"
46 const void **sessionsToNotify
;
49 serverSessionRef theSession
;
50 SCDynamicStorePrivateRef storePrivate
;
52 if (needsNotification
== NULL
)
53 return; /* if no sessions need to be kicked */
55 notifyCnt
= CFSetGetCount(needsNotification
);
56 sessionsToNotify
= malloc(notifyCnt
* sizeof(CFNumberRef
));
57 CFSetGetValues(needsNotification
, sessionsToNotify
);
58 while (--notifyCnt
>= 0) {
59 (void) CFNumberGetValue(sessionsToNotify
[notifyCnt
],
62 theSession
= getSession(server
);
63 storePrivate
= (SCDynamicStorePrivateRef
)theSession
->store
;
66 * deliver notifications to client sessions
68 if ((storePrivate
->notifyStatus
== Using_NotifierInformViaMachPort
) &&
69 (storePrivate
->notifyPort
!= MACH_PORT_NULL
)) {
70 mach_msg_empty_send_t msg
;
71 mach_msg_option_t options
;
74 * Post notification as mach message
76 if (_configd_verbose
) {
77 SCLog(TRUE
, LOG_DEBUG
, CFSTR("sending mach message notification."));
78 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" port = %d"), storePrivate
->notifyPort
);
79 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" msgid = %d"), storePrivate
->notifyPortIdentifier
);
81 msg
.header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0);
82 msg
.header
.msgh_size
= sizeof(msg
);
83 msg
.header
.msgh_remote_port
= storePrivate
->notifyPort
;
84 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
85 msg
.header
.msgh_id
= storePrivate
->notifyPortIdentifier
;
86 options
= MACH_SEND_TIMEOUT
;
87 status
= mach_msg(&msg
.header
, /* msg */
88 MACH_SEND_MSG
|options
, /* options */
89 msg
.header
.msgh_size
, /* send_size */
91 MACH_PORT_NULL
, /* rcv_name */
93 MACH_PORT_NULL
); /* notify */
96 if ((storePrivate
->notifyStatus
== Using_NotifierInformViaFD
) &&
97 (storePrivate
->notifyFile
>= 0)) {
100 if (_configd_verbose
) {
101 SCLog(TRUE
, LOG_DEBUG
, CFSTR("sending (UNIX domain) socket notification"));
102 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" fd = %d"), storePrivate
->notifyFile
);
103 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" msgid = %d"), storePrivate
->notifyFileIdentifier
);
106 written
= write(storePrivate
->notifyFile
,
107 &storePrivate
->notifyFileIdentifier
,
108 sizeof(storePrivate
->notifyFileIdentifier
));
110 if (errno
== EWOULDBLOCK
) {
111 SCLog(_configd_verbose
, LOG_DEBUG
,
112 CFSTR("sorry, only one outstanding notification per session."));
114 SCLog(_configd_verbose
, LOG_DEBUG
,
115 CFSTR("could not send notification, write() failed: %s"),
117 storePrivate
->notifyFile
= -1;
119 } else if (written
!= sizeof(storePrivate
->notifyFileIdentifier
)) {
120 SCLog(_configd_verbose
, LOG_DEBUG
,
121 CFSTR("could not send notification, incomplete write()"));
122 storePrivate
->notifyFile
= -1;
126 if ((storePrivate
->notifyStatus
== Using_NotifierInformViaSignal
) &&
127 (storePrivate
->notifySignal
> 0)) {
128 kern_return_t status
;
131 * Post notification as signal
133 status
= pid_for_task(storePrivate
->notifySignalTask
, &pid
);
134 if (status
== KERN_SUCCESS
) {
135 if (_configd_verbose
) {
136 SCLog(TRUE
, LOG_DEBUG
, CFSTR("sending signal notification"));
137 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" pid = %d"), pid
);
138 SCLog(TRUE
, LOG_DEBUG
, CFSTR(" signal = %d"), storePrivate
->notifySignal
);
140 if (kill(pid
, storePrivate
->notifySignal
) != 0) {
141 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("could not send signal: %s"), strerror(errno
));
142 status
= KERN_FAILURE
;
147 if ((mach_port_type(mach_task_self(), storePrivate
->notifySignalTask
, &pt
) == KERN_SUCCESS
) &&
148 (pt
& MACH_PORT_TYPE_DEAD_NAME
)) {
149 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("could not send signal, process died"));
151 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("could not send signal: %s"), mach_error_string(status
));
155 if (status
!= KERN_SUCCESS
) {
156 /* don't bother with any more attempts */
157 (void) mach_port_destroy(mach_task_self(), storePrivate
->notifySignalTask
);
158 storePrivate
->notifySignal
= 0;
159 storePrivate
->notifySignalTask
= TASK_NULL
;
163 free(sessionsToNotify
);
166 * this list of notifications have been posted, wait for some more.
168 CFRelease(needsNotification
);
169 needsNotification
= NULL
;