2 * Copyright (c) 2000-2004 Apple Computer, 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_mutex_data(,lock
) /* UNDReply lock */
58 int userLandNotificationKey
;
59 KUNCUserNotificationCallBack callback
;
61 ipc_port_t self_port
; /* Our port */
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)
68 /* forward declarations */
69 void UNDReply_deallocate(
80 port
= reply
->self_port
;
81 assert(IP_VALID(port
));
82 ipc_kobject_set(port
, IKO_NULL
, IKOT_NONE
);
83 reply
->self_port
= IP_NULL
;
84 UNDReply_unlock(reply
);
86 ipc_port_dealloc_kernel(port
);
87 kfree(reply
, sizeof(struct UNDReply
));
92 UNDServer_reference(void)
94 UNDServerRef UNDServer
;
97 kr
= host_get_user_notification_port(host_priv_self(), &UNDServer
);
98 assert(kr
== KERN_SUCCESS
);
103 UNDServer_deallocate(
104 UNDServerRef UNDServer
)
106 if (IP_VALID(UNDServer
))
107 ipc_port_release_send(UNDServer
);
115 UNDAlertCompletedWithResult_rpc (
118 xmlData_t keyRef
, /* raw XML bytes */
119 mach_msg_type_number_t keyLen
)
122 CFStringRef xmlError
= NULL
;
123 CFDictionaryRef dict
= NULL
;
125 const void *dict
= (const void *)keyRef
;
128 if (reply
== UND_REPLY_NULL
|| !reply
->inprogress
)
129 return KERN_INVALID_ARGUMENT
;
132 * JMM - No C vesion of the Unserialize code in-kernel
133 * and no C type for a CFDictionary either. For now,
134 * just pass the raw keyRef through.
137 if (keyRef
&& keyLen
) {
138 dict
= IOCFUnserialize(keyRef
, NULL
, NULL
, &xmlError
);
145 #endif /* KERNEL_CF */
147 if (reply
->callback
) {
148 (reply
->callback
)((KUNCUserNotificationID
) reply
, result
, dict
);
151 UNDReply_lock(reply
);
152 reply
->inprogress
= FALSE
;
153 reply
->userLandNotificationKey
= -1;
154 UNDReply_unlock(reply
);
155 UNDReply_deallocate(reply
);
160 * Routine: UNDNotificationCreated_rpc
162 * Intermediate routine. Allows the kernel mechanism
163 * to be informed that the notification request IS
164 * being processed by the user-level daemon, and how
165 * to identify that request.
168 UNDNotificationCreated_rpc (
170 int userLandNotificationKey
)
172 if (reply
== UND_REPLY_NULL
)
173 return KERN_INVALID_ARGUMENT
;
175 UNDReply_lock(reply
);
176 if (reply
->inprogress
|| reply
->userLandNotificationKey
!= -1) {
177 UNDReply_unlock(reply
);
178 return KERN_INVALID_ARGUMENT
;
180 reply
->userLandNotificationKey
= userLandNotificationKey
;
181 UNDReply_unlock(reply
);
190 KUNCUserNotificationID
191 KUNCGetNotificationID()
195 reply
= (UNDReplyRef
) kalloc(sizeof(struct UNDReply
));
196 if (reply
!= UND_REPLY_NULL
) {
197 reply
->self_port
= ipc_port_alloc_kernel();
198 if (reply
->self_port
== IP_NULL
) {
199 kfree(reply
, sizeof(struct UNDReply
));
200 reply
= UND_REPLY_NULL
;
202 mutex_init(&reply
->lock
, 0);
203 reply
->userLandNotificationKey
= -1;
204 reply
->inprogress
= FALSE
;
205 ipc_kobject_set(reply
->self_port
,
206 (ipc_kobject_t
)reply
,
210 return (KUNCUserNotificationID
) reply
;
214 kern_return_t
KUNCExecute(char executionPath
[1024], int uid
, int gid
)
217 UNDServerRef UNDServer
;
219 UNDServer
= UNDServer_reference();
220 if (IP_VALID(UNDServer
)) {
222 kr
= UNDExecute_rpc(UNDServer
, executionPath
, uid
, gid
);
223 UNDServer_deallocate(UNDServer
);
226 return MACH_SEND_INVALID_DEST
;
229 kern_return_t
KUNCUserNotificationCancel(
230 KUNCUserNotificationID id
)
232 UNDReplyRef reply
= (UNDReplyRef
)id
;
236 if (reply
== UND_REPLY_NULL
)
237 return KERN_INVALID_ARGUMENT
;
239 UNDReply_lock(reply
);
240 if (!reply
->inprogress
) {
241 UNDReply_unlock(reply
);
242 return KERN_INVALID_ARGUMENT
;
245 reply
->inprogress
= FALSE
;
246 if ((ulkey
= reply
->userLandNotificationKey
) != 0) {
247 UNDServerRef UNDServer
;
249 reply
->userLandNotificationKey
= 0;
250 UNDReply_unlock(reply
);
252 UNDServer
= UNDServer_reference();
253 if (IP_VALID(UNDServer
)) {
254 kr
= UNDCancelNotification_rpc(UNDServer
,ulkey
);
255 UNDServer_deallocate(UNDServer
);
257 kr
= MACH_SEND_INVALID_DEST
;
259 UNDReply_unlock(reply
);
262 UNDReply_deallocate(reply
);
267 KUNCUserNotificationDisplayNotice(
272 char *localizationPath
,
275 char *defaultButtonTitle
)
277 UNDServerRef UNDServer
;
279 UNDServer
= UNDServer_reference();
280 if (IP_VALID(UNDServer
)) {
282 kr
= UNDDisplayNoticeSimple_rpc(UNDServer
,
291 UNDServer_deallocate(UNDServer
);
294 return MACH_SEND_INVALID_DEST
;
298 KUNCUserNotificationDisplayAlert(
303 char *localizationPath
,
306 char *defaultButtonTitle
,
307 char *alternateButtonTitle
,
308 char *otherButtonTitle
,
309 unsigned *responseFlags
)
311 UNDServerRef UNDServer
;
313 UNDServer
= UNDServer_reference();
314 if (IP_VALID(UNDServer
)) {
316 kr
= UNDDisplayAlertSimple_rpc(UNDServer
,
325 alternateButtonTitle
,
328 UNDServer_deallocate(UNDServer
);
331 return MACH_SEND_INVALID_DEST
;
335 KUNCUserNotificationDisplayFromBundle(
336 KUNCUserNotificationID id
,
342 KUNCUserNotificationCallBack callback
,
343 __unused
int contextKey
)
345 UNDReplyRef reply
= (UNDReplyRef
)id
;
346 UNDServerRef UNDServer
;
347 ipc_port_t reply_port
;
349 if (reply
== UND_REPLY_NULL
)
350 return KERN_INVALID_ARGUMENT
;
351 UNDReply_lock(reply
);
352 if (reply
->inprogress
== TRUE
|| reply
->userLandNotificationKey
!= -1) {
353 UNDReply_unlock(reply
);
354 return KERN_INVALID_ARGUMENT
;
356 reply
->inprogress
= TRUE
;
357 reply
->callback
= callback
;
358 reply_port
= ipc_port_make_send(reply
->self_port
);
359 UNDReply_unlock(reply
);
361 UNDServer
= UNDServer_reference();
362 if (IP_VALID(UNDServer
)) {
365 kr
= UNDDisplayCustomFromBundle_rpc(UNDServer
,
372 UNDServer_deallocate(UNDServer
);
375 return MACH_SEND_INVALID_DEST
;
379 * Routine: convert_port_to_UNDReply
381 * MIG helper routine to convert from a mach port to a
388 convert_port_to_UNDReply(
391 if (IP_VALID(port
)) {
395 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_UND_REPLY
)) {
397 return UND_REPLY_NULL
;
399 reply
= (UNDReplyRef
) port
->ip_kobject
;
400 assert(reply
!= UND_REPLY_NULL
);
404 return UND_REPLY_NULL
;
408 * User interface for setting the host UserNotification Daemon port.
413 host_priv_t host_priv
,
416 return (host_set_user_notification_port(host_priv
, server
));
420 * User interface for retrieving the UserNotification Daemon port.
425 host_priv_t host_priv
,
426 UNDServerRef
*serverp
)
428 return (host_get_user_notification_port(host_priv
, serverp
));