]> git.saurik.com Git - apple/configd.git/blob - configd.tproj/notify.c
configd-84.6.tar.gz
[apple/configd.git] / configd.tproj / notify.c
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * Modification History
26 *
27 * June 1, 2001 Allan Nathanson <ajn@apple.com>
28 * - public API conversion
29 *
30 * March 31, 2000 Allan Nathanson <ajn@apple.com>
31 * - initial revision
32 */
33
34
35 #include <unistd.h>
36
37 #include "configd.h"
38 #include "configd_server.h"
39 #include "session.h"
40
41
42 __private_extern__
43 void
44 pushNotifications()
45 {
46 const void **sessionsToNotify;
47 CFIndex notifyCnt;
48 int server;
49 serverSessionRef theSession;
50 SCDynamicStorePrivateRef storePrivate;
51
52 if (needsNotification == NULL)
53 return; /* if no sessions need to be kicked */
54
55 notifyCnt = CFSetGetCount(needsNotification);
56 sessionsToNotify = malloc(notifyCnt * sizeof(CFNumberRef));
57 CFSetGetValues(needsNotification, sessionsToNotify);
58 while (--notifyCnt >= 0) {
59 (void) CFNumberGetValue(sessionsToNotify[notifyCnt],
60 kCFNumberIntType,
61 &server);
62 theSession = getSession(server);
63 storePrivate = (SCDynamicStorePrivateRef)theSession->store;
64
65 /*
66 * deliver notifications to client sessions
67 */
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;
72 kern_return_t status;
73 /*
74 * Post notification as mach message
75 */
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);
80 }
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 */
90 0, /* rcv_size */
91 MACH_PORT_NULL, /* rcv_name */
92 0, /* timeout */
93 MACH_PORT_NULL); /* notify */
94 }
95
96 if ((storePrivate->notifyStatus == Using_NotifierInformViaFD) &&
97 (storePrivate->notifyFile >= 0)) {
98 ssize_t written;
99
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);
104 }
105
106 written = write(storePrivate->notifyFile,
107 &storePrivate->notifyFileIdentifier,
108 sizeof(storePrivate->notifyFileIdentifier));
109 if (written == -1) {
110 if (errno == EWOULDBLOCK) {
111 SCLog(_configd_verbose, LOG_DEBUG,
112 CFSTR("sorry, only one outstanding notification per session."));
113 } else {
114 SCLog(_configd_verbose, LOG_DEBUG,
115 CFSTR("could not send notification, write() failed: %s"),
116 strerror(errno));
117 storePrivate->notifyFile = -1;
118 }
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;
123 }
124 }
125
126 if ((storePrivate->notifyStatus == Using_NotifierInformViaSignal) &&
127 (storePrivate->notifySignal > 0)) {
128 kern_return_t status;
129 pid_t pid;
130 /*
131 * Post notification as signal
132 */
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);
139 }
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;
143 }
144 } else {
145 mach_port_type_t pt;
146
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"));
150 } else {
151 SCLog(_configd_verbose, LOG_DEBUG, CFSTR("could not send signal: %s"), mach_error_string(status));
152 }
153 }
154
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;
160 }
161 }
162 }
163 free(sessionsToNotify);
164
165 /*
166 * this list of notifications have been posted, wait for some more.
167 */
168 CFRelease(needsNotification);
169 needsNotification = NULL;
170
171 return;
172 }