]> git.saurik.com Git - apple/xnu.git/blob - osfmk/UserNotification/KUNCUserNotifications.c
xnu-792.21.3.tar.gz
[apple/xnu.git] / osfmk / UserNotification / KUNCUserNotifications.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <mach/port.h>
30 #include <mach/message.h>
31 #include <mach/kern_return.h>
32 #include <mach/host_priv.h>
33
34 #include <kern/kern_types.h>
35 #include <kern/kalloc.h>
36 #include <kern/host.h>
37 #include <kern/ipc_kobject.h>
38
39 #include <ipc/ipc_port.h>
40
41 #include <UserNotification/UNDTypes.h>
42 #include <UserNotification/UNDRequest.h>
43 #include <UserNotification/UNDReplyServer.h>
44 #include <UserNotification/KUNCUserNotifications.h>
45
46 #ifdef KERNEL_CF
47 // external
48 #include <IOKit/IOCFSerialize.h>
49 #include <IOKit/IOCFUnserialize.h>
50 #endif
51
52 /*
53 * DEFINES AND STRUCTURES
54 */
55
56 struct UNDReply {
57 decl_mutex_data(,lock) /* UNDReply lock */
58 int userLandNotificationKey;
59 KUNCUserNotificationCallBack callback;
60 boolean_t inprogress;
61 ipc_port_t self_port; /* Our port */
62 };
63
64 #define UNDReply_lock(reply) mutex_lock(&reply->lock)
65 #define UNDReply_lock_try(reply) mutex_lock_try(&(reply)->lock)
66 #define UNDReply_unlock(reply) mutex_unlock(&(reply)->lock)
67
68 /* forward declarations */
69 void UNDReply_deallocate(
70 UNDReplyRef reply);
71
72
73 void
74 UNDReply_deallocate(
75 UNDReplyRef reply)
76 {
77 ipc_port_t port;
78
79 UNDReply_lock(reply);
80 port = reply->self_port;
81 assert(IP_VALID(port));
82 ipc_kobject_set(port, IKO_NULL, IKOT_NONE);
83 reply->self_port = IP_NULL;
84 UNDReply_unlock(reply);
85
86 ipc_port_dealloc_kernel(port);
87 kfree(reply, sizeof(struct UNDReply));
88 return;
89 }
90
91 static UNDServerRef
92 UNDServer_reference(void)
93 {
94 UNDServerRef UNDServer;
95 kern_return_t kr;
96
97 kr = host_get_user_notification_port(host_priv_self(), &UNDServer);
98 assert(kr == KERN_SUCCESS);
99 return UNDServer;
100 }
101
102 static void
103 UNDServer_deallocate(
104 UNDServerRef UNDServer)
105 {
106 if (IP_VALID(UNDServer))
107 ipc_port_release_send(UNDServer);
108 }
109
110 /*
111 * UND Mig Callbacks
112 */
113
114 kern_return_t
115 UNDAlertCompletedWithResult_rpc (
116 UNDReplyRef reply,
117 int result,
118 xmlData_t keyRef, /* raw XML bytes */
119 mach_msg_type_number_t keyLen)
120 {
121 #ifdef KERNEL_CF
122 CFStringRef xmlError = NULL;
123 CFDictionaryRef dict = NULL;
124 #else
125 const void *dict = (const void *)keyRef;
126 #endif
127
128 if (reply == UND_REPLY_NULL || !reply->inprogress)
129 return KERN_INVALID_ARGUMENT;
130
131 /*
132 * JMM - No C vesion of the Unserialize code in-kernel
133 * and no C type for a CFDictionary either. For now,
134 * just pass the raw keyRef through.
135 */
136 #ifdef KERNEL_CF
137 if (keyRef && keyLen) {
138 dict = IOCFUnserialize(keyRef, NULL, NULL, &xmlError);
139 }
140
141 if (xmlError) {
142 CFShow(xmlError);
143 CFRelease(xmlError);
144 }
145 #endif /* KERNEL_CF */
146
147 if (reply->callback) {
148 (reply->callback)((KUNCUserNotificationID) reply, result, dict);
149 }
150
151 UNDReply_lock(reply);
152 reply->inprogress = FALSE;
153 reply->userLandNotificationKey = -1;
154 UNDReply_unlock(reply);
155 UNDReply_deallocate(reply);
156 return KERN_SUCCESS;
157 }
158
159 /*
160 * Routine: UNDNotificationCreated_rpc
161 *
162 * Intermediate routine. Allows the kernel mechanism
163 * to be informed that the notification request IS
164 * being processed by the user-level daemon, and how
165 * to identify that request.
166 */
167 kern_return_t
168 UNDNotificationCreated_rpc (
169 UNDReplyRef reply,
170 int userLandNotificationKey)
171 {
172 if (reply == UND_REPLY_NULL)
173 return KERN_INVALID_ARGUMENT;
174
175 UNDReply_lock(reply);
176 if (reply->inprogress || reply->userLandNotificationKey != -1) {
177 UNDReply_unlock(reply);
178 return KERN_INVALID_ARGUMENT;
179 }
180 reply->userLandNotificationKey = userLandNotificationKey;
181 UNDReply_unlock(reply);
182 return KERN_SUCCESS;
183 }
184
185 /*
186 * KUNC Functions
187 */
188
189
190 KUNCUserNotificationID
191 KUNCGetNotificationID()
192 {
193 UNDReplyRef reply;
194
195 reply = (UNDReplyRef) kalloc(sizeof(struct UNDReply));
196 if (reply != UND_REPLY_NULL) {
197 reply->self_port = ipc_port_alloc_kernel();
198 if (reply->self_port == IP_NULL) {
199 kfree(reply, sizeof(struct UNDReply));
200 reply = UND_REPLY_NULL;
201 } else {
202 mutex_init(&reply->lock, 0);
203 reply->userLandNotificationKey = -1;
204 reply->inprogress = FALSE;
205 ipc_kobject_set(reply->self_port,
206 (ipc_kobject_t)reply,
207 IKOT_UND_REPLY);
208 }
209 }
210 return (KUNCUserNotificationID) reply;
211 }
212
213
214 kern_return_t KUNCExecute(char executionPath[1024], int uid, int gid)
215 {
216
217 UNDServerRef UNDServer;
218
219 UNDServer = UNDServer_reference();
220 if (IP_VALID(UNDServer)) {
221 kern_return_t kr;
222 kr = UNDExecute_rpc(UNDServer, executionPath, uid, gid);
223 UNDServer_deallocate(UNDServer);
224 return kr;
225 }
226 return MACH_SEND_INVALID_DEST;
227 }
228
229 kern_return_t KUNCUserNotificationCancel(
230 KUNCUserNotificationID id)
231 {
232 UNDReplyRef reply = (UNDReplyRef)id;
233 kern_return_t kr;
234 int ulkey;
235
236 if (reply == UND_REPLY_NULL)
237 return KERN_INVALID_ARGUMENT;
238
239 UNDReply_lock(reply);
240 if (!reply->inprogress) {
241 UNDReply_unlock(reply);
242 return KERN_INVALID_ARGUMENT;
243 }
244
245 reply->inprogress = FALSE;
246 if ((ulkey = reply->userLandNotificationKey) != 0) {
247 UNDServerRef UNDServer;
248
249 reply->userLandNotificationKey = 0;
250 UNDReply_unlock(reply);
251
252 UNDServer = UNDServer_reference();
253 if (IP_VALID(UNDServer)) {
254 kr = UNDCancelNotification_rpc(UNDServer,ulkey);
255 UNDServer_deallocate(UNDServer);
256 } else
257 kr = MACH_SEND_INVALID_DEST;
258 } else {
259 UNDReply_unlock(reply);
260 kr = KERN_SUCCESS;
261 }
262 UNDReply_deallocate(reply);
263 return kr;
264 }
265
266 kern_return_t
267 KUNCUserNotificationDisplayNotice(
268 int noticeTimeout,
269 unsigned flags,
270 char *iconPath,
271 char *soundPath,
272 char *localizationPath,
273 char *alertHeader,
274 char *alertMessage,
275 char *defaultButtonTitle)
276 {
277 UNDServerRef UNDServer;
278
279 UNDServer = UNDServer_reference();
280 if (IP_VALID(UNDServer)) {
281 kern_return_t kr;
282 kr = UNDDisplayNoticeSimple_rpc(UNDServer,
283 noticeTimeout,
284 flags,
285 iconPath,
286 soundPath,
287 localizationPath,
288 alertHeader,
289 alertMessage,
290 defaultButtonTitle);
291 UNDServer_deallocate(UNDServer);
292 return kr;
293 }
294 return MACH_SEND_INVALID_DEST;
295 }
296
297 kern_return_t
298 KUNCUserNotificationDisplayAlert(
299 int alertTimeout,
300 unsigned flags,
301 char *iconPath,
302 char *soundPath,
303 char *localizationPath,
304 char *alertHeader,
305 char *alertMessage,
306 char *defaultButtonTitle,
307 char *alternateButtonTitle,
308 char *otherButtonTitle,
309 unsigned *responseFlags)
310 {
311 UNDServerRef UNDServer;
312
313 UNDServer = UNDServer_reference();
314 if (IP_VALID(UNDServer)) {
315 kern_return_t kr;
316 kr = UNDDisplayAlertSimple_rpc(UNDServer,
317 alertTimeout,
318 flags,
319 iconPath,
320 soundPath,
321 localizationPath,
322 alertHeader,
323 alertMessage,
324 defaultButtonTitle,
325 alternateButtonTitle,
326 otherButtonTitle,
327 responseFlags);
328 UNDServer_deallocate(UNDServer);
329 return kr;
330 }
331 return MACH_SEND_INVALID_DEST;
332 }
333
334 kern_return_t
335 KUNCUserNotificationDisplayFromBundle(
336 KUNCUserNotificationID id,
337 char *bundlePath,
338 char *fileName,
339 char *fileExtension,
340 char *messageKey,
341 char *tokenString,
342 KUNCUserNotificationCallBack callback,
343 __unused int contextKey)
344 {
345 UNDReplyRef reply = (UNDReplyRef)id;
346 UNDServerRef UNDServer;
347 ipc_port_t reply_port;
348
349 if (reply == UND_REPLY_NULL)
350 return KERN_INVALID_ARGUMENT;
351 UNDReply_lock(reply);
352 if (reply->inprogress == TRUE || reply->userLandNotificationKey != -1) {
353 UNDReply_unlock(reply);
354 return KERN_INVALID_ARGUMENT;
355 }
356 reply->inprogress = TRUE;
357 reply->callback = callback;
358 reply_port = ipc_port_make_send(reply->self_port);
359 UNDReply_unlock(reply);
360
361 UNDServer = UNDServer_reference();
362 if (IP_VALID(UNDServer)) {
363 kern_return_t kr;
364
365 kr = UNDDisplayCustomFromBundle_rpc(UNDServer,
366 reply_port,
367 bundlePath,
368 fileName,
369 fileExtension,
370 messageKey,
371 tokenString);
372 UNDServer_deallocate(UNDServer);
373 return kr;
374 }
375 return MACH_SEND_INVALID_DEST;
376 }
377
378 /*
379 * Routine: convert_port_to_UNDReply
380 *
381 * MIG helper routine to convert from a mach port to a
382 * UNDReply object.
383 *
384 * Assumptions:
385 * Nothing locked.
386 */
387 UNDReplyRef
388 convert_port_to_UNDReply(
389 ipc_port_t port)
390 {
391 if (IP_VALID(port)) {
392 UNDReplyRef reply;
393
394 ip_lock(port);
395 if (!ip_active(port) || (ip_kotype(port) != IKOT_UND_REPLY)) {
396 ip_unlock(port);
397 return UND_REPLY_NULL;
398 }
399 reply = (UNDReplyRef) port->ip_kobject;
400 assert(reply != UND_REPLY_NULL);
401 ip_unlock(port);
402 return reply;
403 }
404 return UND_REPLY_NULL;
405 }
406
407 /*
408 * User interface for setting the host UserNotification Daemon port.
409 */
410
411 kern_return_t
412 host_set_UNDServer(
413 host_priv_t host_priv,
414 UNDServerRef server)
415 {
416 return (host_set_user_notification_port(host_priv, server));
417 }
418
419 /*
420 * User interface for retrieving the UserNotification Daemon port.
421 */
422
423 kern_return_t
424 host_get_UNDServer(
425 host_priv_t host_priv,
426 UNDServerRef *serverp)
427 {
428 return (host_get_user_notification_port(host_priv, serverp));
429 }