2 * Copyright (c) 2000-2002 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>
37 #include "configd_server.h"
44 const void **sessionsToNotify
;
47 serverSessionRef theSession
;
48 SCDynamicStorePrivateRef storePrivate
;
50 if (needsNotification
== NULL
)
51 return; /* if no sessions need to be kicked */
53 notifyCnt
= CFSetGetCount(needsNotification
);
54 sessionsToNotify
= malloc(notifyCnt
* sizeof(CFNumberRef
));
55 CFSetGetValues(needsNotification
, sessionsToNotify
);
56 while (--notifyCnt
>= 0) {
57 (void) CFNumberGetValue(sessionsToNotify
[notifyCnt
],
60 theSession
= getSession(server
);
61 storePrivate
= (SCDynamicStorePrivateRef
)theSession
->store
;
64 * deliver notifications to client sessions
66 if ((storePrivate
->notifyStatus
== Using_NotifierInformViaMachPort
) &&
67 (storePrivate
->notifyPort
!= MACH_PORT_NULL
)) {
68 mach_msg_empty_send_t msg
;
69 mach_msg_option_t options
;
72 * Post notification as mach message
74 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("sending mach message notification."));
75 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" port = %d"), storePrivate
->notifyPort
);
76 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" msgid = %d"), storePrivate
->notifyPortIdentifier
);
77 msg
.header
.msgh_bits
= MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND
, 0);
78 msg
.header
.msgh_size
= sizeof(msg
);
79 msg
.header
.msgh_remote_port
= storePrivate
->notifyPort
;
80 msg
.header
.msgh_local_port
= MACH_PORT_NULL
;
81 msg
.header
.msgh_id
= storePrivate
->notifyPortIdentifier
;
82 options
= MACH_SEND_TIMEOUT
;
83 status
= mach_msg(&msg
.header
, /* msg */
84 MACH_SEND_MSG
|options
, /* options */
85 msg
.header
.msgh_size
, /* send_size */
87 MACH_PORT_NULL
, /* rcv_name */
89 MACH_PORT_NULL
); /* notify */
92 if ((storePrivate
->notifyStatus
== Using_NotifierInformViaFD
) &&
93 (storePrivate
->notifyFile
>= 0)) {
96 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("sending (UNIX domain) socket notification"));
97 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" fd = %d"), storePrivate
->notifyFile
);
98 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" msgid = %d"), storePrivate
->notifyFileIdentifier
);
100 written
= write(storePrivate
->notifyFile
,
101 &storePrivate
->notifyFileIdentifier
,
102 sizeof(storePrivate
->notifyFileIdentifier
));
104 if (errno
== EWOULDBLOCK
) {
105 SCLog(_configd_verbose
, LOG_DEBUG
,
106 CFSTR("sorry, only one outstanding notification per session."));
108 SCLog(_configd_verbose
, LOG_DEBUG
,
109 CFSTR("could not send notification, write() failed: %s"),
111 storePrivate
->notifyFile
= -1;
113 } else if (written
!= sizeof(storePrivate
->notifyFileIdentifier
)) {
114 SCLog(_configd_verbose
, LOG_DEBUG
,
115 CFSTR("could not send notification, incomplete write()"));
116 storePrivate
->notifyFile
= -1;
120 if ((storePrivate
->notifyStatus
== Using_NotifierInformViaSignal
) &&
121 (storePrivate
->notifySignal
> 0)) {
122 kern_return_t status
;
125 * Post notification as signal
127 status
= pid_for_task(storePrivate
->notifySignalTask
, &pid
);
128 if (status
== KERN_SUCCESS
) {
129 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("sending signal notification"));
130 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" pid = %d"), pid
);
131 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR(" signal = %d"), storePrivate
->notifySignal
);
132 if (kill(pid
, storePrivate
->notifySignal
) != 0) {
133 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("could not send signal: %s"), strerror(errno
));
134 status
= KERN_FAILURE
;
139 if ((mach_port_type(mach_task_self(), storePrivate
->notifySignalTask
, &pt
) == KERN_SUCCESS
) &&
140 (pt
& MACH_PORT_TYPE_DEAD_NAME
)) {
141 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("could not send signal, process died"));
143 SCLog(_configd_verbose
, LOG_DEBUG
, CFSTR("could not send signal: %s"), mach_error_string(status
));
147 if (status
!= KERN_SUCCESS
) {
148 /* don't bother with any more attempts */
149 (void) mach_port_destroy(mach_task_self(), storePrivate
->notifySignalTask
);
150 storePrivate
->notifySignal
= 0;
151 storePrivate
->notifySignalTask
= TASK_NULL
;
155 free(sessionsToNotify
);
158 * this list of notifications have been posted, wait for some more.
160 CFRelease(needsNotification
);
161 needsNotification
= NULL
;