2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <mach/port.h>
25 #include <mach/message.h>
26 #include <mach/kern_return.h>
27 #include <mach/host_priv.h>
29 #include <kern/kern_types.h>
30 #include <kern/kalloc.h>
31 #include <kern/host.h>
32 #include <kern/ipc_kobject.h>
34 #include <ipc/ipc_port.h>
36 #include <UserNotification/UNDTypes.h>
37 #include <UserNotification/UNDRequest.h>
38 #include <UserNotification/UNDReplyServer.h>
39 #include <UserNotification/KUNCUserNotifications.h>
43 #include <IOKit/IOCFSerialize.h>
44 #include <IOKit/IOCFUnserialize.h>
48 * DEFINES AND STRUCTURES
52 decl_mutex_data(,lock
) /* UNDReply lock */
53 int userLandNotificationKey
;
54 KUNCUserNotificationCallBack callback
;
56 ipc_port_t self_port
; /* Our port */
59 #define UNDReply_lock(reply) mutex_lock(&reply->lock)
60 #define UNDReply_lock_try(reply) mutex_lock_try(&(reply)->lock)
61 #define UNDReply_unlock(reply) mutex_unlock(&(reply)->lock)
63 /* forward declarations */
64 void UNDReply_deallocate(
75 port
= reply
->self_port
;
76 assert(IP_VALID(port
));
77 ipc_kobject_set(port
, IKO_NULL
, IKOT_NONE
);
78 reply
->self_port
= IP_NULL
;
79 UNDReply_unlock(reply
);
81 ipc_port_dealloc_kernel(port
);
82 kfree(reply
, sizeof(struct UNDReply
));
87 UNDServer_reference(void)
89 UNDServerRef UNDServer
;
92 kr
= host_get_user_notification_port(host_priv_self(), &UNDServer
);
93 assert(kr
== KERN_SUCCESS
);
99 UNDServerRef UNDServer
)
101 if (IP_VALID(UNDServer
))
102 ipc_port_release_send(UNDServer
);
110 UNDAlertCompletedWithResult_rpc (
113 xmlData_t keyRef
, /* raw XML bytes */
114 mach_msg_type_number_t keyLen
)
117 CFStringRef xmlError
= NULL
;
118 CFDictionaryRef dict
= NULL
;
120 const void *dict
= (const void *)keyRef
;
123 if (reply
== UND_REPLY_NULL
|| !reply
->inprogress
)
124 return KERN_INVALID_ARGUMENT
;
127 * JMM - No C vesion of the Unserialize code in-kernel
128 * and no C type for a CFDictionary either. For now,
129 * just pass the raw keyRef through.
132 if (keyRef
&& keyLen
) {
133 dict
= IOCFUnserialize(keyRef
, NULL
, NULL
, &xmlError
);
140 #endif /* KERNEL_CF */
142 if (reply
->callback
) {
143 (reply
->callback
)((KUNCUserNotificationID
) reply
, result
, dict
);
146 UNDReply_lock(reply
);
147 reply
->inprogress
= FALSE
;
148 reply
->userLandNotificationKey
= -1;
149 UNDReply_unlock(reply
);
150 UNDReply_deallocate(reply
);
155 * Routine: UNDNotificationCreated_rpc
157 * Intermediate routine. Allows the kernel mechanism
158 * to be informed that the notification request IS
159 * being processed by the user-level daemon, and how
160 * to identify that request.
163 UNDNotificationCreated_rpc (
165 int userLandNotificationKey
)
167 if (reply
== UND_REPLY_NULL
)
168 return KERN_INVALID_ARGUMENT
;
170 UNDReply_lock(reply
);
171 if (reply
->inprogress
|| reply
->userLandNotificationKey
!= -1) {
172 UNDReply_unlock(reply
);
173 return KERN_INVALID_ARGUMENT
;
175 reply
->userLandNotificationKey
= userLandNotificationKey
;
176 UNDReply_unlock(reply
);
185 KUNCUserNotificationID
186 KUNCGetNotificationID()
190 reply
= (UNDReplyRef
) kalloc(sizeof(struct UNDReply
));
191 if (reply
!= UND_REPLY_NULL
) {
192 reply
->self_port
= ipc_port_alloc_kernel();
193 if (reply
->self_port
== IP_NULL
) {
194 kfree(reply
, sizeof(struct UNDReply
));
195 reply
= UND_REPLY_NULL
;
197 mutex_init(&reply
->lock
, 0);
198 reply
->userLandNotificationKey
= -1;
199 reply
->inprogress
= FALSE
;
200 ipc_kobject_set(reply
->self_port
,
201 (ipc_kobject_t
)reply
,
205 return (KUNCUserNotificationID
) reply
;
209 kern_return_t
KUNCExecute(char executionPath
[1024], int uid
, int gid
)
212 UNDServerRef UNDServer
;
214 UNDServer
= UNDServer_reference();
215 if (IP_VALID(UNDServer
)) {
217 kr
= UNDExecute_rpc(UNDServer
, executionPath
, uid
, gid
);
218 UNDServer_deallocate(UNDServer
);
221 return MACH_SEND_INVALID_DEST
;
224 kern_return_t
KUNCUserNotificationCancel(
225 KUNCUserNotificationID id
)
227 UNDReplyRef reply
= (UNDReplyRef
)id
;
231 if (reply
== UND_REPLY_NULL
)
232 return KERN_INVALID_ARGUMENT
;
234 UNDReply_lock(reply
);
235 if (!reply
->inprogress
) {
236 UNDReply_unlock(reply
);
237 return KERN_INVALID_ARGUMENT
;
240 reply
->inprogress
= FALSE
;
241 if ((ulkey
= reply
->userLandNotificationKey
) != 0) {
242 UNDServerRef UNDServer
;
244 reply
->userLandNotificationKey
= 0;
245 UNDReply_unlock(reply
);
247 UNDServer
= UNDServer_reference();
248 if (IP_VALID(UNDServer
)) {
249 kr
= UNDCancelNotification_rpc(UNDServer
,ulkey
);
250 UNDServer_deallocate(UNDServer
);
252 kr
= MACH_SEND_INVALID_DEST
;
254 UNDReply_unlock(reply
);
257 UNDReply_deallocate(reply
);
262 KUNCUserNotificationDisplayNotice(
267 char *localizationPath
,
270 char *defaultButtonTitle
)
272 UNDServerRef UNDServer
;
274 UNDServer
= UNDServer_reference();
275 if (IP_VALID(UNDServer
)) {
277 kr
= UNDDisplayNoticeSimple_rpc(UNDServer
,
286 UNDServer_deallocate(UNDServer
);
289 return MACH_SEND_INVALID_DEST
;
293 KUNCUserNotificationDisplayAlert(
298 char *localizationPath
,
301 char *defaultButtonTitle
,
302 char *alternateButtonTitle
,
303 char *otherButtonTitle
,
304 unsigned *responseFlags
)
306 UNDServerRef UNDServer
;
308 UNDServer
= UNDServer_reference();
309 if (IP_VALID(UNDServer
)) {
311 kr
= UNDDisplayAlertSimple_rpc(UNDServer
,
320 alternateButtonTitle
,
323 UNDServer_deallocate(UNDServer
);
326 return MACH_SEND_INVALID_DEST
;
330 KUNCUserNotificationDisplayFromBundle(
331 KUNCUserNotificationID id
,
337 KUNCUserNotificationCallBack callback
,
338 __unused
int contextKey
)
340 UNDReplyRef reply
= (UNDReplyRef
)id
;
341 UNDServerRef UNDServer
;
342 ipc_port_t reply_port
;
344 if (reply
== UND_REPLY_NULL
)
345 return KERN_INVALID_ARGUMENT
;
346 UNDReply_lock(reply
);
347 if (reply
->inprogress
== TRUE
|| reply
->userLandNotificationKey
!= -1) {
348 UNDReply_unlock(reply
);
349 return KERN_INVALID_ARGUMENT
;
351 reply
->inprogress
= TRUE
;
352 reply
->callback
= callback
;
353 reply_port
= ipc_port_make_send(reply
->self_port
);
354 UNDReply_unlock(reply
);
356 UNDServer
= UNDServer_reference();
357 if (IP_VALID(UNDServer
)) {
360 kr
= UNDDisplayCustomFromBundle_rpc(UNDServer
,
367 UNDServer_deallocate(UNDServer
);
370 return MACH_SEND_INVALID_DEST
;
374 * Routine: convert_port_to_UNDReply
376 * MIG helper routine to convert from a mach port to a
383 convert_port_to_UNDReply(
386 if (IP_VALID(port
)) {
390 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_UND_REPLY
)) {
392 return UND_REPLY_NULL
;
394 reply
= (UNDReplyRef
) port
->ip_kobject
;
395 assert(reply
!= UND_REPLY_NULL
);
399 return UND_REPLY_NULL
;
403 * User interface for setting the host UserNotification Daemon port.
408 host_priv_t host_priv
,
411 return (host_set_user_notification_port(host_priv
, server
));
415 * User interface for retrieving the UserNotification Daemon port.
420 host_priv_t host_priv
,
421 UNDServerRef
*serverp
)
423 return (host_get_user_notification_port(host_priv
, serverp
));