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_lock(&reply->lock)
67 /* forward declarations */
68 void UNDReply_deallocate(
79 port
= reply
->self_port
;
80 assert(IP_VALID(port
));
81 ipc_kobject_set(port
, IKO_NULL
, IKOT_NONE
);
82 reply
->self_port
= IP_NULL
;
83 UNDReply_unlock(reply
);
85 ipc_port_dealloc_kernel(port
);
86 kfree(reply
, sizeof(struct UNDReply
));
91 UNDServer_reference(void)
93 UNDServerRef UNDServer
;
96 kr
= host_get_user_notification_port(host_priv_self(), &UNDServer
);
97 assert(kr
== KERN_SUCCESS
);
102 UNDServer_deallocate(
103 UNDServerRef UNDServer
)
105 if (IP_VALID(UNDServer
))
106 ipc_port_release_send(UNDServer
);
114 UNDAlertCompletedWithResult_rpc (
117 xmlData_t keyRef
, /* raw XML bytes */
119 mach_msg_type_number_t keyLen
)
121 __unused mach_msg_type_number_t keyLen
)
125 CFStringRef xmlError
= NULL
;
126 CFDictionaryRef dict
= NULL
;
128 const void *dict
= (const void *)keyRef
;
131 if (reply
== UND_REPLY_NULL
|| !reply
->inprogress
)
132 return KERN_INVALID_ARGUMENT
;
135 * JMM - No C vesion of the Unserialize code in-kernel
136 * and no C type for a CFDictionary either. For now,
137 * just pass the raw keyRef through.
140 if (keyRef
&& keyLen
) {
141 dict
= IOCFUnserialize(keyRef
, NULL
, NULL
, &xmlError
);
148 #endif /* KERNEL_CF */
150 if (reply
->callback
) {
151 (reply
->callback
)((int)(KUNCUserNotificationID
)reply
, result
, dict
);
154 UNDReply_lock(reply
);
155 reply
->inprogress
= FALSE
;
156 reply
->userLandNotificationKey
= -1;
157 UNDReply_unlock(reply
);
158 UNDReply_deallocate(reply
);
163 * Routine: UNDNotificationCreated_rpc
165 * Intermediate routine. Allows the kernel mechanism
166 * to be informed that the notification request IS
167 * being processed by the user-level daemon, and how
168 * to identify that request.
171 UNDNotificationCreated_rpc (
173 int userLandNotificationKey
)
175 if (reply
== UND_REPLY_NULL
)
176 return KERN_INVALID_ARGUMENT
;
178 UNDReply_lock(reply
);
179 if (reply
->inprogress
|| reply
->userLandNotificationKey
!= -1) {
180 UNDReply_unlock(reply
);
181 return KERN_INVALID_ARGUMENT
;
183 reply
->userLandNotificationKey
= userLandNotificationKey
;
184 UNDReply_unlock(reply
);
192 extern lck_grp_t LockCompatGroup
;
194 KUNCUserNotificationID
195 KUNCGetNotificationID(void)
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
) {
203 kfree(reply
, sizeof(struct UNDReply
));
204 reply
= UND_REPLY_NULL
;
206 lck_mtx_init(&reply
->lock
, &LockCompatGroup
, LCK_ATTR_NULL
);
207 reply
->userLandNotificationKey
= -1;
208 reply
->inprogress
= FALSE
;
209 ipc_kobject_set(reply
->self_port
,
210 (ipc_kobject_t
)reply
,
214 return (KUNCUserNotificationID
) reply
;
218 kern_return_t
KUNCExecute(char executionPath
[1024], int uid
, int gid
)
221 UNDServerRef UNDServer
;
223 UNDServer
= UNDServer_reference();
224 if (IP_VALID(UNDServer
)) {
226 kr
= UNDExecute_rpc(UNDServer
, executionPath
, uid
, gid
);
227 UNDServer_deallocate(UNDServer
);
230 return MACH_SEND_INVALID_DEST
;
233 kern_return_t
KUNCUserNotificationCancel(
234 KUNCUserNotificationID id
)
236 UNDReplyRef reply
= (UNDReplyRef
)id
;
240 if (reply
== UND_REPLY_NULL
)
241 return KERN_INVALID_ARGUMENT
;
243 UNDReply_lock(reply
);
244 if (!reply
->inprogress
) {
245 UNDReply_unlock(reply
);
246 return KERN_INVALID_ARGUMENT
;
249 reply
->inprogress
= FALSE
;
250 if ((ulkey
= reply
->userLandNotificationKey
) != 0) {
251 UNDServerRef UNDServer
;
253 reply
->userLandNotificationKey
= 0;
254 UNDReply_unlock(reply
);
256 UNDServer
= UNDServer_reference();
257 if (IP_VALID(UNDServer
)) {
258 kr
= UNDCancelNotification_rpc(UNDServer
,ulkey
);
259 UNDServer_deallocate(UNDServer
);
261 kr
= MACH_SEND_INVALID_DEST
;
263 UNDReply_unlock(reply
);
266 UNDReply_deallocate(reply
);
271 KUNCUserNotificationDisplayNotice(
276 char *localizationPath
,
279 char *defaultButtonTitle
)
281 UNDServerRef UNDServer
;
283 UNDServer
= UNDServer_reference();
284 if (IP_VALID(UNDServer
)) {
286 kr
= UNDDisplayNoticeSimple_rpc(UNDServer
,
295 UNDServer_deallocate(UNDServer
);
298 return MACH_SEND_INVALID_DEST
;
302 KUNCUserNotificationDisplayAlert(
307 char *localizationPath
,
310 char *defaultButtonTitle
,
311 char *alternateButtonTitle
,
312 char *otherButtonTitle
,
313 unsigned *responseFlags
)
315 UNDServerRef UNDServer
;
317 UNDServer
= UNDServer_reference();
318 if (IP_VALID(UNDServer
)) {
320 kr
= UNDDisplayAlertSimple_rpc(UNDServer
,
329 alternateButtonTitle
,
332 UNDServer_deallocate(UNDServer
);
335 return MACH_SEND_INVALID_DEST
;
339 KUNCUserNotificationDisplayFromBundle(
340 KUNCUserNotificationID id
,
346 KUNCUserNotificationCallBack callback
,
347 __unused
int contextKey
)
349 UNDReplyRef reply
= (UNDReplyRef
)id
;
350 UNDServerRef UNDServer
;
351 ipc_port_t reply_port
;
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
;
360 reply
->inprogress
= TRUE
;
361 reply
->callback
= callback
;
362 reply_port
= ipc_port_make_send(reply
->self_port
);
363 UNDReply_unlock(reply
);
365 UNDServer
= UNDServer_reference();
366 if (IP_VALID(UNDServer
)) {
369 kr
= UNDDisplayCustomFromBundle_rpc(UNDServer
,
376 UNDServer_deallocate(UNDServer
);
379 return MACH_SEND_INVALID_DEST
;
383 * Routine: convert_port_to_UNDReply
385 * MIG helper routine to convert from a mach port to a
392 convert_port_to_UNDReply(
395 if (IP_VALID(port
)) {
399 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_UND_REPLY
)) {
401 return UND_REPLY_NULL
;
403 reply
= (UNDReplyRef
) port
->ip_kobject
;
404 assert(reply
!= UND_REPLY_NULL
);
408 return UND_REPLY_NULL
;
412 * User interface for setting the host UserNotification Daemon port.
417 host_priv_t host_priv
,
420 return (host_set_user_notification_port(host_priv
, server
));
424 * User interface for retrieving the UserNotification Daemon port.
429 host_priv_t host_priv
,
430 UNDServerRef
*serverp
)
432 return (host_get_user_notification_port(host_priv
, serverp
));