]> git.saurik.com Git - apple/xnu.git/blame - osfmk/UserNotification/KUNCUserNotifications.c
xnu-792.22.5.tar.gz
[apple/xnu.git] / osfmk / UserNotification / KUNCUserNotifications.c
CommitLineData
1c79356b 1/*
91447636 2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
A
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
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28
29#include <mach/port.h>
30#include <mach/message.h>
31#include <mach/kern_return.h>
91447636 32#include <mach/host_priv.h>
1c79356b 33
91447636
A
34#include <kern/kern_types.h>
35#include <kern/kalloc.h>
1c79356b 36#include <kern/host.h>
1c79356b
A
37#include <kern/ipc_kobject.h>
38
91447636
A
39#include <ipc/ipc_port.h>
40
1c79356b
A
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
1c79356b
A
56struct 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
91447636
A
68/* forward declarations */
69void UNDReply_deallocate(
70 UNDReplyRef reply);
71
72
1c79356b
A
73void
74UNDReply_deallocate(
75 UNDReplyRef reply)
76{
77 ipc_port_t port;
55e303ae 78
1c79356b
A
79 UNDReply_lock(reply);
80 port = reply->self_port;
55e303ae 81 assert(IP_VALID(port));
1c79356b
A
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);
91447636 87 kfree(reply, sizeof(struct UNDReply));
1c79356b
A
88 return;
89}
90
55e303ae
A
91static UNDServerRef
92UNDServer_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
102static void
103UNDServer_deallocate(
104 UNDServerRef UNDServer)
105{
106 if (IP_VALID(UNDServer))
107 ipc_port_release_send(UNDServer);
108}
109
1c79356b
A
110/*
111 * UND Mig Callbacks
112*/
113
114kern_return_t
115UNDAlertCompletedWithResult_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
91447636 125 const void *dict = (const void *)keyRef;
1c79356b
A
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 */
167kern_return_t
168UNDNotificationCreated_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
190KUNCUserNotificationID
191KUNCGetNotificationID()
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) {
91447636 199 kfree(reply, sizeof(struct UNDReply));
1c79356b
A
200 reply = UND_REPLY_NULL;
201 } else {
91447636 202 mutex_init(&reply->lock, 0);
1c79356b
A
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
214kern_return_t KUNCExecute(char executionPath[1024], int uid, int gid)
215{
55e303ae
A
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;
1c79356b
A
227}
228
229kern_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;
91447636 246 if ((ulkey = reply->userLandNotificationKey) != 0) {
55e303ae
A
247 UNDServerRef UNDServer;
248
1c79356b
A
249 reply->userLandNotificationKey = 0;
250 UNDReply_unlock(reply);
55e303ae
A
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;
1c79356b
A
258 } else {
259 UNDReply_unlock(reply);
260 kr = KERN_SUCCESS;
261 }
262 UNDReply_deallocate(reply);
263 return kr;
264}
265
266kern_return_t
267KUNCUserNotificationDisplayNotice(
91447636 268 int noticeTimeout,
1c79356b
A
269 unsigned flags,
270 char *iconPath,
271 char *soundPath,
272 char *localizationPath,
273 char *alertHeader,
274 char *alertMessage,
275 char *defaultButtonTitle)
276{
55e303ae
A
277 UNDServerRef UNDServer;
278
279 UNDServer = UNDServer_reference();
280 if (IP_VALID(UNDServer)) {
281 kern_return_t kr;
282 kr = UNDDisplayNoticeSimple_rpc(UNDServer,
91447636 283 noticeTimeout,
1c79356b
A
284 flags,
285 iconPath,
286 soundPath,
287 localizationPath,
288 alertHeader,
289 alertMessage,
290 defaultButtonTitle);
55e303ae
A
291 UNDServer_deallocate(UNDServer);
292 return kr;
293 }
294 return MACH_SEND_INVALID_DEST;
1c79356b
A
295}
296
297kern_return_t
298KUNCUserNotificationDisplayAlert(
91447636 299 int alertTimeout,
1c79356b
A
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{
55e303ae 311 UNDServerRef UNDServer;
1c79356b 312
55e303ae
A
313 UNDServer = UNDServer_reference();
314 if (IP_VALID(UNDServer)) {
315 kern_return_t kr;
316 kr = UNDDisplayAlertSimple_rpc(UNDServer,
91447636 317 alertTimeout,
1c79356b
A
318 flags,
319 iconPath,
320 soundPath,
321 localizationPath,
322 alertHeader,
323 alertMessage,
324 defaultButtonTitle,
325 alternateButtonTitle,
326 otherButtonTitle,
327 responseFlags);
55e303ae
A
328 UNDServer_deallocate(UNDServer);
329 return kr;
330 }
331 return MACH_SEND_INVALID_DEST;
1c79356b
A
332}
333
334kern_return_t
335KUNCUserNotificationDisplayFromBundle(
336 KUNCUserNotificationID id,
337 char *bundlePath,
338 char *fileName,
339 char *fileExtension,
340 char *messageKey,
341 char *tokenString,
342 KUNCUserNotificationCallBack callback,
91447636 343 __unused int contextKey)
1c79356b
A
344{
345 UNDReplyRef reply = (UNDReplyRef)id;
55e303ae 346 UNDServerRef UNDServer;
1c79356b 347 ipc_port_t reply_port;
1c79356b
A
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 }
91447636 356 reply->inprogress = TRUE;
1c79356b
A
357 reply->callback = callback;
358 reply_port = ipc_port_make_send(reply->self_port);
359 UNDReply_unlock(reply);
360
55e303ae
A
361 UNDServer = UNDServer_reference();
362 if (IP_VALID(UNDServer)) {
363 kern_return_t kr;
364
365 kr = UNDDisplayCustomFromBundle_rpc(UNDServer,
1c79356b
A
366 reply_port,
367 bundlePath,
368 fileName,
369 fileExtension,
370 messageKey,
371 tokenString);
55e303ae
A
372 UNDServer_deallocate(UNDServer);
373 return kr;
374 }
375 return MACH_SEND_INVALID_DEST;
1c79356b
A
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 */
387UNDReplyRef
388convert_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);
55e303ae 402 return reply;
1c79356b
A
403 }
404 return UND_REPLY_NULL;
405}
406
407/*
408 * User interface for setting the host UserNotification Daemon port.
409 */
410
411kern_return_t
412host_set_UNDServer(
413 host_priv_t host_priv,
414 UNDServerRef server)
415{
55e303ae 416 return (host_set_user_notification_port(host_priv, server));
1c79356b
A
417}
418
419/*
420 * User interface for retrieving the UserNotification Daemon port.
421 */
422
423kern_return_t
424host_get_UNDServer(
425 host_priv_t host_priv,
55e303ae 426 UNDServerRef *serverp)
1c79356b 427{
55e303ae 428 return (host_get_user_notification_port(host_priv, serverp));
1c79356b 429}