2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 #include <mach/port.h>
32 #include <mach/message.h>
33 #include <mach/kern_return.h>
34 #include <mach/host_priv.h>
36 #include <kern/kern_types.h>
37 #include <kern/kalloc.h>
38 #include <kern/host.h>
39 #include <kern/ipc_kobject.h>
41 #include <ipc/ipc_port.h>
43 #include <UserNotification/UNDTypes.h>
44 #include <UserNotification/UNDRequest.h>
45 #include <UserNotification/UNDReplyServer.h>
46 #include <UserNotification/KUNCUserNotifications.h>
50 #include <IOKit/IOCFSerialize.h>
51 #include <IOKit/IOCFUnserialize.h>
55 * DEFINES AND STRUCTURES
59 decl_mutex_data(,lock
) /* UNDReply lock */
60 int userLandNotificationKey
;
61 KUNCUserNotificationCallBack callback
;
63 ipc_port_t self_port
; /* Our port */
66 #define UNDReply_lock(reply) mutex_lock(&reply->lock)
67 #define UNDReply_lock_try(reply) mutex_lock_try(&(reply)->lock)
68 #define UNDReply_unlock(reply) mutex_unlock(&(reply)->lock)
70 /* forward declarations */
71 void UNDReply_deallocate(
82 port
= reply
->self_port
;
83 assert(IP_VALID(port
));
84 ipc_kobject_set(port
, IKO_NULL
, IKOT_NONE
);
85 reply
->self_port
= IP_NULL
;
86 UNDReply_unlock(reply
);
88 ipc_port_dealloc_kernel(port
);
89 kfree(reply
, sizeof(struct UNDReply
));
94 UNDServer_reference(void)
96 UNDServerRef UNDServer
;
99 kr
= host_get_user_notification_port(host_priv_self(), &UNDServer
);
100 assert(kr
== KERN_SUCCESS
);
105 UNDServer_deallocate(
106 UNDServerRef UNDServer
)
108 if (IP_VALID(UNDServer
))
109 ipc_port_release_send(UNDServer
);
117 UNDAlertCompletedWithResult_rpc (
120 xmlData_t keyRef
, /* raw XML bytes */
121 mach_msg_type_number_t keyLen
)
124 CFStringRef xmlError
= NULL
;
125 CFDictionaryRef dict
= NULL
;
127 const void *dict
= (const void *)keyRef
;
130 if (reply
== UND_REPLY_NULL
|| !reply
->inprogress
)
131 return KERN_INVALID_ARGUMENT
;
134 * JMM - No C vesion of the Unserialize code in-kernel
135 * and no C type for a CFDictionary either. For now,
136 * just pass the raw keyRef through.
139 if (keyRef
&& keyLen
) {
140 dict
= IOCFUnserialize(keyRef
, NULL
, NULL
, &xmlError
);
147 #endif /* KERNEL_CF */
149 if (reply
->callback
) {
150 (reply
->callback
)((KUNCUserNotificationID
) reply
, result
, dict
);
153 UNDReply_lock(reply
);
154 reply
->inprogress
= FALSE
;
155 reply
->userLandNotificationKey
= -1;
156 UNDReply_unlock(reply
);
157 UNDReply_deallocate(reply
);
162 * Routine: UNDNotificationCreated_rpc
164 * Intermediate routine. Allows the kernel mechanism
165 * to be informed that the notification request IS
166 * being processed by the user-level daemon, and how
167 * to identify that request.
170 UNDNotificationCreated_rpc (
172 int userLandNotificationKey
)
174 if (reply
== UND_REPLY_NULL
)
175 return KERN_INVALID_ARGUMENT
;
177 UNDReply_lock(reply
);
178 if (reply
->inprogress
|| reply
->userLandNotificationKey
!= -1) {
179 UNDReply_unlock(reply
);
180 return KERN_INVALID_ARGUMENT
;
182 reply
->userLandNotificationKey
= userLandNotificationKey
;
183 UNDReply_unlock(reply
);
192 KUNCUserNotificationID
193 KUNCGetNotificationID()
197 reply
= (UNDReplyRef
) kalloc(sizeof(struct UNDReply
));
198 if (reply
!= UND_REPLY_NULL
) {
199 reply
->self_port
= ipc_port_alloc_kernel();
200 if (reply
->self_port
== IP_NULL
) {
201 kfree(reply
, sizeof(struct UNDReply
));
202 reply
= UND_REPLY_NULL
;
204 mutex_init(&reply
->lock
, 0);
205 reply
->userLandNotificationKey
= -1;
206 reply
->inprogress
= FALSE
;
207 ipc_kobject_set(reply
->self_port
,
208 (ipc_kobject_t
)reply
,
212 return (KUNCUserNotificationID
) reply
;
216 kern_return_t
KUNCExecute(char executionPath
[1024], int uid
, int gid
)
219 UNDServerRef UNDServer
;
221 UNDServer
= UNDServer_reference();
222 if (IP_VALID(UNDServer
)) {
224 kr
= UNDExecute_rpc(UNDServer
, executionPath
, uid
, gid
);
225 UNDServer_deallocate(UNDServer
);
228 return MACH_SEND_INVALID_DEST
;
231 kern_return_t
KUNCUserNotificationCancel(
232 KUNCUserNotificationID id
)
234 UNDReplyRef reply
= (UNDReplyRef
)id
;
238 if (reply
== UND_REPLY_NULL
)
239 return KERN_INVALID_ARGUMENT
;
241 UNDReply_lock(reply
);
242 if (!reply
->inprogress
) {
243 UNDReply_unlock(reply
);
244 return KERN_INVALID_ARGUMENT
;
247 reply
->inprogress
= FALSE
;
248 if ((ulkey
= reply
->userLandNotificationKey
) != 0) {
249 UNDServerRef UNDServer
;
251 reply
->userLandNotificationKey
= 0;
252 UNDReply_unlock(reply
);
254 UNDServer
= UNDServer_reference();
255 if (IP_VALID(UNDServer
)) {
256 kr
= UNDCancelNotification_rpc(UNDServer
,ulkey
);
257 UNDServer_deallocate(UNDServer
);
259 kr
= MACH_SEND_INVALID_DEST
;
261 UNDReply_unlock(reply
);
264 UNDReply_deallocate(reply
);
269 KUNCUserNotificationDisplayNotice(
274 char *localizationPath
,
277 char *defaultButtonTitle
)
279 UNDServerRef UNDServer
;
281 UNDServer
= UNDServer_reference();
282 if (IP_VALID(UNDServer
)) {
284 kr
= UNDDisplayNoticeSimple_rpc(UNDServer
,
293 UNDServer_deallocate(UNDServer
);
296 return MACH_SEND_INVALID_DEST
;
300 KUNCUserNotificationDisplayAlert(
305 char *localizationPath
,
308 char *defaultButtonTitle
,
309 char *alternateButtonTitle
,
310 char *otherButtonTitle
,
311 unsigned *responseFlags
)
313 UNDServerRef UNDServer
;
315 UNDServer
= UNDServer_reference();
316 if (IP_VALID(UNDServer
)) {
318 kr
= UNDDisplayAlertSimple_rpc(UNDServer
,
327 alternateButtonTitle
,
330 UNDServer_deallocate(UNDServer
);
333 return MACH_SEND_INVALID_DEST
;
337 KUNCUserNotificationDisplayFromBundle(
338 KUNCUserNotificationID id
,
344 KUNCUserNotificationCallBack callback
,
345 __unused
int contextKey
)
347 UNDReplyRef reply
= (UNDReplyRef
)id
;
348 UNDServerRef UNDServer
;
349 ipc_port_t reply_port
;
351 if (reply
== UND_REPLY_NULL
)
352 return KERN_INVALID_ARGUMENT
;
353 UNDReply_lock(reply
);
354 if (reply
->inprogress
== TRUE
|| reply
->userLandNotificationKey
!= -1) {
355 UNDReply_unlock(reply
);
356 return KERN_INVALID_ARGUMENT
;
358 reply
->inprogress
= TRUE
;
359 reply
->callback
= callback
;
360 reply_port
= ipc_port_make_send(reply
->self_port
);
361 UNDReply_unlock(reply
);
363 UNDServer
= UNDServer_reference();
364 if (IP_VALID(UNDServer
)) {
367 kr
= UNDDisplayCustomFromBundle_rpc(UNDServer
,
374 UNDServer_deallocate(UNDServer
);
377 return MACH_SEND_INVALID_DEST
;
381 * Routine: convert_port_to_UNDReply
383 * MIG helper routine to convert from a mach port to a
390 convert_port_to_UNDReply(
393 if (IP_VALID(port
)) {
397 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_UND_REPLY
)) {
399 return UND_REPLY_NULL
;
401 reply
= (UNDReplyRef
) port
->ip_kobject
;
402 assert(reply
!= UND_REPLY_NULL
);
406 return UND_REPLY_NULL
;
410 * User interface for setting the host UserNotification Daemon port.
415 host_priv_t host_priv
,
418 return (host_set_user_notification_port(host_priv
, server
));
422 * User interface for retrieving the UserNotification Daemon port.
427 host_priv_t host_priv
,
428 UNDServerRef
*serverp
)
430 return (host_get_user_notification_port(host_priv
, serverp
));