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
);
117 UNDAlertCompletedWithResult_rpc (
120 xmlData_t keyRef
, /* raw XML bytes */
122 mach_msg_type_number_t keyLen
)
124 __unused mach_msg_type_number_t keyLen
)
128 CFStringRef xmlError
= NULL
;
129 CFDictionaryRef dict
= NULL
;
131 const void *dict
= (const void *)keyRef
;
134 if (reply
== UND_REPLY_NULL
|| !reply
->inprogress
)
135 return KERN_INVALID_ARGUMENT
;
138 * JMM - No C vesion of the Unserialize code in-kernel
139 * and no C type for a CFDictionary either. For now,
140 * just pass the raw keyRef through.
143 if (keyRef
&& keyLen
) {
144 dict
= IOCFUnserialize(keyRef
, NULL
, NULL
, &xmlError
);
151 #endif /* KERNEL_CF */
153 if (reply
->callback
) {
154 (reply
->callback
)((int)(KUNCUserNotificationID
)reply
, result
, dict
);
157 UNDReply_lock(reply
);
158 reply
->inprogress
= FALSE
;
159 reply
->userLandNotificationKey
= -1;
160 UNDReply_unlock(reply
);
161 UNDReply_deallocate(reply
);
166 * Routine: UNDNotificationCreated_rpc
168 * Intermediate routine. Allows the kernel mechanism
169 * to be informed that the notification request IS
170 * being processed by the user-level daemon, and how
171 * to identify that request.
174 UNDNotificationCreated_rpc (
176 int userLandNotificationKey
)
178 if (reply
== UND_REPLY_NULL
)
179 return KERN_INVALID_ARGUMENT
;
181 UNDReply_lock(reply
);
182 if (reply
->inprogress
|| reply
->userLandNotificationKey
!= -1) {
183 UNDReply_unlock(reply
);
184 return KERN_INVALID_ARGUMENT
;
186 reply
->userLandNotificationKey
= userLandNotificationKey
;
187 UNDReply_unlock(reply
);
196 KUNCUserNotificationID
197 KUNCGetNotificationID(void)
201 reply
= (UNDReplyRef
) kalloc(sizeof(struct UNDReply
));
202 if (reply
!= UND_REPLY_NULL
) {
203 reply
->self_port
= ipc_port_alloc_kernel();
204 if (reply
->self_port
== IP_NULL
) {
205 kfree(reply
, sizeof(struct UNDReply
));
206 reply
= UND_REPLY_NULL
;
208 lck_mtx_init(&reply
->lock
, &LockCompatGroup
, LCK_ATTR_NULL
);
209 reply
->userLandNotificationKey
= -1;
210 reply
->inprogress
= FALSE
;
211 ipc_kobject_set(reply
->self_port
,
212 (ipc_kobject_t
)reply
,
216 return (KUNCUserNotificationID
) reply
;
220 kern_return_t
KUNCExecute(char executionPath
[1024], int uid
, int gid
)
223 UNDServerRef UNDServer
;
225 UNDServer
= UNDServer_reference();
226 if (IP_VALID(UNDServer
)) {
228 kr
= UNDExecute_rpc(UNDServer
, executionPath
, uid
, gid
);
229 UNDServer_deallocate(UNDServer
);
232 return MACH_SEND_INVALID_DEST
;
235 kern_return_t
KUNCUserNotificationCancel(
236 KUNCUserNotificationID id
)
238 UNDReplyRef reply
= (UNDReplyRef
)id
;
242 if (reply
== UND_REPLY_NULL
)
243 return KERN_INVALID_ARGUMENT
;
245 UNDReply_lock(reply
);
246 if (!reply
->inprogress
) {
247 UNDReply_unlock(reply
);
248 return KERN_INVALID_ARGUMENT
;
251 reply
->inprogress
= FALSE
;
252 if ((ulkey
= reply
->userLandNotificationKey
) != 0) {
253 UNDServerRef UNDServer
;
255 reply
->userLandNotificationKey
= 0;
256 UNDReply_unlock(reply
);
258 UNDServer
= UNDServer_reference();
259 if (IP_VALID(UNDServer
)) {
260 kr
= UNDCancelNotification_rpc(UNDServer
,ulkey
);
261 UNDServer_deallocate(UNDServer
);
263 kr
= MACH_SEND_INVALID_DEST
;
265 UNDReply_unlock(reply
);
268 UNDReply_deallocate(reply
);
273 KUNCUserNotificationDisplayNotice(
278 char *localizationPath
,
281 char *defaultButtonTitle
)
283 UNDServerRef UNDServer
;
285 UNDServer
= UNDServer_reference();
286 if (IP_VALID(UNDServer
)) {
288 kr
= UNDDisplayNoticeSimple_rpc(UNDServer
,
297 UNDServer_deallocate(UNDServer
);
300 return MACH_SEND_INVALID_DEST
;
304 KUNCUserNotificationDisplayAlert(
309 char *localizationPath
,
312 char *defaultButtonTitle
,
313 char *alternateButtonTitle
,
314 char *otherButtonTitle
,
315 unsigned *responseFlags
)
317 UNDServerRef UNDServer
;
319 UNDServer
= UNDServer_reference();
320 if (IP_VALID(UNDServer
)) {
322 kr
= UNDDisplayAlertSimple_rpc(UNDServer
,
331 alternateButtonTitle
,
334 UNDServer_deallocate(UNDServer
);
337 return MACH_SEND_INVALID_DEST
;
341 KUNCUserNotificationDisplayFromBundle(
342 KUNCUserNotificationID id
,
348 KUNCUserNotificationCallBack callback
,
349 __unused
int contextKey
)
351 UNDReplyRef reply
= (UNDReplyRef
)id
;
352 UNDServerRef UNDServer
;
353 ipc_port_t reply_port
;
355 if (reply
== UND_REPLY_NULL
)
356 return KERN_INVALID_ARGUMENT
;
357 UNDReply_lock(reply
);
358 if (reply
->inprogress
== TRUE
|| reply
->userLandNotificationKey
!= -1) {
359 UNDReply_unlock(reply
);
360 return KERN_INVALID_ARGUMENT
;
362 reply
->inprogress
= TRUE
;
363 reply
->callback
= callback
;
364 reply_port
= ipc_port_make_send(reply
->self_port
);
365 UNDReply_unlock(reply
);
367 UNDServer
= UNDServer_reference();
368 if (IP_VALID(UNDServer
)) {
371 kr
= UNDDisplayCustomFromBundle_rpc(UNDServer
,
378 UNDServer_deallocate(UNDServer
);
381 return MACH_SEND_INVALID_DEST
;
385 * Routine: convert_port_to_UNDReply
387 * MIG helper routine to convert from a mach port to a
394 convert_port_to_UNDReply(
397 if (IP_VALID(port
)) {
401 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_UND_REPLY
)) {
403 return UND_REPLY_NULL
;
405 reply
= (UNDReplyRef
) port
->ip_kobject
;
406 assert(reply
!= UND_REPLY_NULL
);
410 return UND_REPLY_NULL
;
414 * User interface for setting the host UserNotification Daemon port.
419 host_priv_t host_priv
,
422 return (host_set_user_notification_port(host_priv
, server
));
426 * User interface for retrieving the UserNotification Daemon port.
431 host_priv_t host_priv
,
432 UNDServerRef
*serverp
)
434 return (host_get_user_notification_port(host_priv
, serverp
));