2 * Copyright (c) 2000-2006 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <mach/port.h>
30 #include <mach/message.h>
31 #include <mach/kern_return.h>
32 #include <mach/host_priv.h>
34 #include <kern/kern_types.h>
35 #include <kern/kalloc.h>
36 #include <kern/host.h>
37 #include <kern/ipc_kobject.h>
39 #include <ipc/ipc_port.h>
41 #include <UserNotification/UNDTypes.h>
42 #include <UserNotification/UNDRequest.h>
43 #include <UserNotification/UNDReplyServer.h>
44 #include <UserNotification/KUNCUserNotifications.h>
48 #include <IOKit/IOCFSerialize.h>
49 #include <IOKit/IOCFUnserialize.h>
53 * DEFINES AND STRUCTURES
57 decl_lck_mtx_data(, lock
) /* UNDReply lock */
58 int userLandNotificationKey
;
59 KUNCUserNotificationCallBack callback
;
61 ipc_port_t self_port
; /* Our port */
64 #define UNDReply_lock(reply) lck_mtx_lock(&reply->lock)
65 #define UNDReply_unlock(reply) lck_mtx_unlock(&reply->lock)
67 extern lck_grp_t LockCompatGroup
;
69 /* forward declarations */
70 void UNDReply_deallocate(
81 port
= reply
->self_port
;
82 assert(IP_VALID(port
));
83 ipc_kobject_set(port
, IKO_NULL
, IKOT_NONE
);
84 reply
->self_port
= IP_NULL
;
85 UNDReply_unlock(reply
);
87 ipc_port_dealloc_kernel(port
);
88 lck_mtx_destroy(&reply
->lock
, &LockCompatGroup
);
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
);
118 UNDAlertCompletedWithResult_rpc(
121 xmlData_t keyRef
, /* raw XML bytes */
123 mach_msg_type_number_t keyLen
)
125 __unused mach_msg_type_number_t keyLen
)
129 CFStringRef xmlError
= NULL
;
130 CFDictionaryRef dict
= NULL
;
132 const void *dict
= (const void *)keyRef
;
135 if (reply
== UND_REPLY_NULL
|| !reply
->inprogress
) {
136 return KERN_INVALID_ARGUMENT
;
140 * JMM - No C vesion of the Unserialize code in-kernel
141 * and no C type for a CFDictionary either. For now,
142 * just pass the raw keyRef through.
145 if (keyRef
&& keyLen
) {
146 dict
= IOCFUnserialize(keyRef
, NULL
, NULL
, &xmlError
);
153 #endif /* KERNEL_CF */
155 if (reply
->callback
) {
156 (reply
->callback
)((int)(KUNCUserNotificationID
)reply
, result
, dict
);
159 UNDReply_lock(reply
);
160 reply
->inprogress
= FALSE
;
161 reply
->userLandNotificationKey
= -1;
162 UNDReply_unlock(reply
);
163 UNDReply_deallocate(reply
);
168 * Routine: UNDNotificationCreated_rpc
170 * Intermediate routine. Allows the kernel mechanism
171 * to be informed that the notification request IS
172 * being processed by the user-level daemon, and how
173 * to identify that request.
176 UNDNotificationCreated_rpc(
178 int userLandNotificationKey
)
180 if (reply
== UND_REPLY_NULL
) {
181 return KERN_INVALID_ARGUMENT
;
184 UNDReply_lock(reply
);
185 if (reply
->inprogress
|| reply
->userLandNotificationKey
!= -1) {
186 UNDReply_unlock(reply
);
187 return KERN_INVALID_ARGUMENT
;
189 reply
->userLandNotificationKey
= userLandNotificationKey
;
190 UNDReply_unlock(reply
);
199 KUNCUserNotificationID
200 KUNCGetNotificationID(void)
204 reply
= (UNDReplyRef
) kalloc(sizeof(struct UNDReply
));
205 if (reply
!= UND_REPLY_NULL
) {
206 reply
->self_port
= ipc_port_alloc_kernel();
207 if (reply
->self_port
== IP_NULL
) {
208 kfree(reply
, sizeof(struct UNDReply
));
209 reply
= UND_REPLY_NULL
;
211 lck_mtx_init(&reply
->lock
, &LockCompatGroup
, LCK_ATTR_NULL
);
212 reply
->userLandNotificationKey
= -1;
213 reply
->inprogress
= FALSE
;
214 ipc_kobject_set(reply
->self_port
,
215 (ipc_kobject_t
)reply
,
219 return (KUNCUserNotificationID
) reply
;
224 KUNCExecute(char executionPath
[1024], int uid
, int gid
)
226 UNDServerRef UNDServer
;
228 UNDServer
= UNDServer_reference();
229 if (IP_VALID(UNDServer
)) {
231 kr
= UNDExecute_rpc(UNDServer
, executionPath
, uid
, gid
);
232 UNDServer_deallocate(UNDServer
);
235 return MACH_SEND_INVALID_DEST
;
239 KUNCUserNotificationCancel(
240 KUNCUserNotificationID id
)
242 UNDReplyRef reply
= (UNDReplyRef
)id
;
246 if (reply
== UND_REPLY_NULL
) {
247 return KERN_INVALID_ARGUMENT
;
250 UNDReply_lock(reply
);
251 if (!reply
->inprogress
) {
252 UNDReply_unlock(reply
);
253 return KERN_INVALID_ARGUMENT
;
256 reply
->inprogress
= FALSE
;
257 if ((ulkey
= reply
->userLandNotificationKey
) != 0) {
258 UNDServerRef UNDServer
;
260 reply
->userLandNotificationKey
= 0;
261 UNDReply_unlock(reply
);
263 UNDServer
= UNDServer_reference();
264 if (IP_VALID(UNDServer
)) {
265 kr
= UNDCancelNotification_rpc(UNDServer
, ulkey
);
266 UNDServer_deallocate(UNDServer
);
268 kr
= MACH_SEND_INVALID_DEST
;
271 UNDReply_unlock(reply
);
274 UNDReply_deallocate(reply
);
279 KUNCUserNotificationDisplayNotice(
284 char *localizationPath
,
287 char *defaultButtonTitle
)
289 UNDServerRef UNDServer
;
291 UNDServer
= UNDServer_reference();
292 if (IP_VALID(UNDServer
)) {
294 kr
= UNDDisplayNoticeSimple_rpc(UNDServer
,
303 UNDServer_deallocate(UNDServer
);
306 return MACH_SEND_INVALID_DEST
;
310 KUNCUserNotificationDisplayAlert(
315 char *localizationPath
,
318 char *defaultButtonTitle
,
319 char *alternateButtonTitle
,
320 char *otherButtonTitle
,
321 unsigned *responseFlags
)
323 UNDServerRef UNDServer
;
325 UNDServer
= UNDServer_reference();
326 if (IP_VALID(UNDServer
)) {
328 kr
= UNDDisplayAlertSimple_rpc(UNDServer
,
337 alternateButtonTitle
,
340 UNDServer_deallocate(UNDServer
);
343 return MACH_SEND_INVALID_DEST
;
347 KUNCUserNotificationDisplayFromBundle(
348 KUNCUserNotificationID id
,
354 KUNCUserNotificationCallBack callback
,
355 __unused
int contextKey
)
357 UNDReplyRef reply
= (UNDReplyRef
)id
;
358 UNDServerRef UNDServer
;
359 ipc_port_t reply_port
;
361 if (reply
== UND_REPLY_NULL
) {
362 return KERN_INVALID_ARGUMENT
;
364 UNDReply_lock(reply
);
365 if (reply
->inprogress
== TRUE
|| reply
->userLandNotificationKey
!= -1) {
366 UNDReply_unlock(reply
);
367 return KERN_INVALID_ARGUMENT
;
369 reply
->inprogress
= TRUE
;
370 reply
->callback
= callback
;
371 reply_port
= ipc_port_make_send(reply
->self_port
);
372 UNDReply_unlock(reply
);
374 UNDServer
= UNDServer_reference();
375 if (IP_VALID(UNDServer
)) {
378 kr
= UNDDisplayCustomFromBundle_rpc(UNDServer
,
385 UNDServer_deallocate(UNDServer
);
388 return MACH_SEND_INVALID_DEST
;
392 * Routine: convert_port_to_UNDReply
394 * MIG helper routine to convert from a mach port to a
401 convert_port_to_UNDReply(
404 if (IP_VALID(port
)) {
408 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_UND_REPLY
)) {
410 return UND_REPLY_NULL
;
412 reply
= (UNDReplyRef
) port
->ip_kobject
;
413 assert(reply
!= UND_REPLY_NULL
);
417 return UND_REPLY_NULL
;
421 * User interface for setting the host UserNotification Daemon port.
426 host_priv_t host_priv
,
429 return host_set_user_notification_port(host_priv
, server
);
433 * User interface for retrieving the UserNotification Daemon port.
438 host_priv_t host_priv
,
439 UNDServerRef
*serverp
)
441 return host_get_user_notification_port(host_priv
, serverp
);