]> git.saurik.com Git - apple/xnu.git/blame - osfmk/UserNotification/KUNCUserNotifications.c
xnu-1228.15.4.tar.gz
[apple/xnu.git] / osfmk / UserNotification / KUNCUserNotifications.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
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.
8f6c56a5 14 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 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 */
2d21ac55 119#ifdef KERNEL_CF
1c79356b 120 mach_msg_type_number_t keyLen)
2d21ac55
A
121#else
122 __unused mach_msg_type_number_t keyLen)
123#endif
1c79356b
A
124{
125#ifdef KERNEL_CF
126 CFStringRef xmlError = NULL;
127 CFDictionaryRef dict = NULL;
128#else
91447636 129 const void *dict = (const void *)keyRef;
1c79356b
A
130#endif
131
132 if (reply == UND_REPLY_NULL || !reply->inprogress)
133 return KERN_INVALID_ARGUMENT;
134
135 /*
136 * JMM - No C vesion of the Unserialize code in-kernel
137 * and no C type for a CFDictionary either. For now,
138 * just pass the raw keyRef through.
139 */
140#ifdef KERNEL_CF
141 if (keyRef && keyLen) {
142 dict = IOCFUnserialize(keyRef, NULL, NULL, &xmlError);
143 }
144
145 if (xmlError) {
146 CFShow(xmlError);
147 CFRelease(xmlError);
148 }
149#endif /* KERNEL_CF */
150
151 if (reply->callback) {
152 (reply->callback)((KUNCUserNotificationID) reply, result, dict);
153 }
154
155 UNDReply_lock(reply);
156 reply->inprogress = FALSE;
157 reply->userLandNotificationKey = -1;
158 UNDReply_unlock(reply);
159 UNDReply_deallocate(reply);
160 return KERN_SUCCESS;
161}
162
163/*
164 * Routine: UNDNotificationCreated_rpc
165 *
166 * Intermediate routine. Allows the kernel mechanism
167 * to be informed that the notification request IS
168 * being processed by the user-level daemon, and how
169 * to identify that request.
170 */
171kern_return_t
172UNDNotificationCreated_rpc (
173 UNDReplyRef reply,
174 int userLandNotificationKey)
175{
176 if (reply == UND_REPLY_NULL)
177 return KERN_INVALID_ARGUMENT;
178
179 UNDReply_lock(reply);
180 if (reply->inprogress || reply->userLandNotificationKey != -1) {
181 UNDReply_unlock(reply);
182 return KERN_INVALID_ARGUMENT;
183 }
184 reply->userLandNotificationKey = userLandNotificationKey;
185 UNDReply_unlock(reply);
186 return KERN_SUCCESS;
187}
188
189/*
190 * KUNC Functions
191*/
192
193
194KUNCUserNotificationID
2d21ac55 195KUNCGetNotificationID(void)
1c79356b
A
196{
197 UNDReplyRef reply;
198
199 reply = (UNDReplyRef) kalloc(sizeof(struct UNDReply));
200 if (reply != UND_REPLY_NULL) {
201 reply->self_port = ipc_port_alloc_kernel();
202 if (reply->self_port == IP_NULL) {
91447636 203 kfree(reply, sizeof(struct UNDReply));
1c79356b
A
204 reply = UND_REPLY_NULL;
205 } else {
91447636 206 mutex_init(&reply->lock, 0);
1c79356b
A
207 reply->userLandNotificationKey = -1;
208 reply->inprogress = FALSE;
209 ipc_kobject_set(reply->self_port,
210 (ipc_kobject_t)reply,
211 IKOT_UND_REPLY);
212 }
213 }
214 return (KUNCUserNotificationID) reply;
215}
216
217
218kern_return_t KUNCExecute(char executionPath[1024], int uid, int gid)
219{
55e303ae
A
220
221 UNDServerRef UNDServer;
222
223 UNDServer = UNDServer_reference();
224 if (IP_VALID(UNDServer)) {
225 kern_return_t kr;
226 kr = UNDExecute_rpc(UNDServer, executionPath, uid, gid);
227 UNDServer_deallocate(UNDServer);
228 return kr;
229 }
230 return MACH_SEND_INVALID_DEST;
1c79356b
A
231}
232
233kern_return_t KUNCUserNotificationCancel(
234 KUNCUserNotificationID id)
235{
236 UNDReplyRef reply = (UNDReplyRef)id;
237 kern_return_t kr;
238 int ulkey;
239
240 if (reply == UND_REPLY_NULL)
241 return KERN_INVALID_ARGUMENT;
242
243 UNDReply_lock(reply);
244 if (!reply->inprogress) {
245 UNDReply_unlock(reply);
246 return KERN_INVALID_ARGUMENT;
247 }
248
249 reply->inprogress = FALSE;
91447636 250 if ((ulkey = reply->userLandNotificationKey) != 0) {
55e303ae
A
251 UNDServerRef UNDServer;
252
1c79356b
A
253 reply->userLandNotificationKey = 0;
254 UNDReply_unlock(reply);
55e303ae
A
255
256 UNDServer = UNDServer_reference();
257 if (IP_VALID(UNDServer)) {
258 kr = UNDCancelNotification_rpc(UNDServer,ulkey);
259 UNDServer_deallocate(UNDServer);
260 } else
261 kr = MACH_SEND_INVALID_DEST;
1c79356b
A
262 } else {
263 UNDReply_unlock(reply);
264 kr = KERN_SUCCESS;
265 }
266 UNDReply_deallocate(reply);
267 return kr;
268}
269
270kern_return_t
271KUNCUserNotificationDisplayNotice(
91447636 272 int noticeTimeout,
1c79356b
A
273 unsigned flags,
274 char *iconPath,
275 char *soundPath,
276 char *localizationPath,
277 char *alertHeader,
278 char *alertMessage,
279 char *defaultButtonTitle)
280{
55e303ae
A
281 UNDServerRef UNDServer;
282
283 UNDServer = UNDServer_reference();
284 if (IP_VALID(UNDServer)) {
285 kern_return_t kr;
286 kr = UNDDisplayNoticeSimple_rpc(UNDServer,
91447636 287 noticeTimeout,
1c79356b
A
288 flags,
289 iconPath,
290 soundPath,
291 localizationPath,
292 alertHeader,
293 alertMessage,
294 defaultButtonTitle);
55e303ae
A
295 UNDServer_deallocate(UNDServer);
296 return kr;
297 }
298 return MACH_SEND_INVALID_DEST;
1c79356b
A
299}
300
301kern_return_t
302KUNCUserNotificationDisplayAlert(
91447636 303 int alertTimeout,
1c79356b
A
304 unsigned flags,
305 char *iconPath,
306 char *soundPath,
307 char *localizationPath,
308 char *alertHeader,
309 char *alertMessage,
310 char *defaultButtonTitle,
311 char *alternateButtonTitle,
312 char *otherButtonTitle,
313 unsigned *responseFlags)
314{
55e303ae 315 UNDServerRef UNDServer;
1c79356b 316
55e303ae
A
317 UNDServer = UNDServer_reference();
318 if (IP_VALID(UNDServer)) {
319 kern_return_t kr;
320 kr = UNDDisplayAlertSimple_rpc(UNDServer,
91447636 321 alertTimeout,
1c79356b
A
322 flags,
323 iconPath,
324 soundPath,
325 localizationPath,
326 alertHeader,
327 alertMessage,
328 defaultButtonTitle,
329 alternateButtonTitle,
330 otherButtonTitle,
331 responseFlags);
55e303ae
A
332 UNDServer_deallocate(UNDServer);
333 return kr;
334 }
335 return MACH_SEND_INVALID_DEST;
1c79356b
A
336}
337
338kern_return_t
339KUNCUserNotificationDisplayFromBundle(
340 KUNCUserNotificationID id,
341 char *bundlePath,
342 char *fileName,
343 char *fileExtension,
344 char *messageKey,
345 char *tokenString,
346 KUNCUserNotificationCallBack callback,
91447636 347 __unused int contextKey)
1c79356b
A
348{
349 UNDReplyRef reply = (UNDReplyRef)id;
55e303ae 350 UNDServerRef UNDServer;
1c79356b 351 ipc_port_t reply_port;
1c79356b
A
352
353 if (reply == UND_REPLY_NULL)
354 return KERN_INVALID_ARGUMENT;
355 UNDReply_lock(reply);
356 if (reply->inprogress == TRUE || reply->userLandNotificationKey != -1) {
357 UNDReply_unlock(reply);
358 return KERN_INVALID_ARGUMENT;
359 }
91447636 360 reply->inprogress = TRUE;
1c79356b
A
361 reply->callback = callback;
362 reply_port = ipc_port_make_send(reply->self_port);
363 UNDReply_unlock(reply);
364
55e303ae
A
365 UNDServer = UNDServer_reference();
366 if (IP_VALID(UNDServer)) {
367 kern_return_t kr;
368
369 kr = UNDDisplayCustomFromBundle_rpc(UNDServer,
1c79356b
A
370 reply_port,
371 bundlePath,
372 fileName,
373 fileExtension,
374 messageKey,
375 tokenString);
55e303ae
A
376 UNDServer_deallocate(UNDServer);
377 return kr;
378 }
379 return MACH_SEND_INVALID_DEST;
1c79356b
A
380}
381
382/*
383 * Routine: convert_port_to_UNDReply
384 *
385 * MIG helper routine to convert from a mach port to a
386 * UNDReply object.
387 *
388 * Assumptions:
389 * Nothing locked.
390 */
391UNDReplyRef
392convert_port_to_UNDReply(
393 ipc_port_t port)
394{
395 if (IP_VALID(port)) {
396 UNDReplyRef reply;
397
398 ip_lock(port);
399 if (!ip_active(port) || (ip_kotype(port) != IKOT_UND_REPLY)) {
400 ip_unlock(port);
401 return UND_REPLY_NULL;
402 }
403 reply = (UNDReplyRef) port->ip_kobject;
404 assert(reply != UND_REPLY_NULL);
405 ip_unlock(port);
55e303ae 406 return reply;
1c79356b
A
407 }
408 return UND_REPLY_NULL;
409}
410
411/*
412 * User interface for setting the host UserNotification Daemon port.
413 */
414
415kern_return_t
416host_set_UNDServer(
417 host_priv_t host_priv,
418 UNDServerRef server)
419{
55e303ae 420 return (host_set_user_notification_port(host_priv, server));
1c79356b
A
421}
422
423/*
424 * User interface for retrieving the UserNotification Daemon port.
425 */
426
427kern_return_t
428host_get_UNDServer(
429 host_priv_t host_priv,
55e303ae 430 UNDServerRef *serverp)
1c79356b 431{
55e303ae 432 return (host_get_user_notification_port(host_priv, serverp));
1c79356b 433}