2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <mach/port.h>
24 #include <mach/message.h>
25 #include <mach/kern_return.h>
26 #include <mach/host_priv.h>
28 #include <kern/kern_types.h>
29 #include <kern/kalloc.h>
30 #include <kern/host.h>
31 #include <kern/ipc_kobject.h>
33 #include <ipc/ipc_port.h>
35 #include <UserNotification/UNDTypes.h>
36 #include <UserNotification/UNDRequest.h>
37 #include <UserNotification/UNDReplyServer.h>
38 #include <UserNotification/KUNCUserNotifications.h>
42 #include <IOKit/IOCFSerialize.h>
43 #include <IOKit/IOCFUnserialize.h>
47 * DEFINES AND STRUCTURES
51 decl_mutex_data(,lock
) /* UNDReply lock */
52 int userLandNotificationKey
;
53 KUNCUserNotificationCallBack callback
;
55 ipc_port_t self_port
; /* Our port */
58 #define UNDReply_lock(reply) mutex_lock(&reply->lock)
59 #define UNDReply_lock_try(reply) mutex_lock_try(&(reply)->lock)
60 #define UNDReply_unlock(reply) mutex_unlock(&(reply)->lock)
62 /* forward declarations */
63 void UNDReply_deallocate(
74 port
= reply
->self_port
;
75 assert(IP_VALID(port
));
76 ipc_kobject_set(port
, IKO_NULL
, IKOT_NONE
);
77 reply
->self_port
= IP_NULL
;
78 UNDReply_unlock(reply
);
80 ipc_port_dealloc_kernel(port
);
81 kfree(reply
, sizeof(struct UNDReply
));
86 UNDServer_reference(void)
88 UNDServerRef UNDServer
;
91 kr
= host_get_user_notification_port(host_priv_self(), &UNDServer
);
92 assert(kr
== KERN_SUCCESS
);
98 UNDServerRef UNDServer
)
100 if (IP_VALID(UNDServer
))
101 ipc_port_release_send(UNDServer
);
109 UNDAlertCompletedWithResult_rpc (
112 xmlData_t keyRef
, /* raw XML bytes */
113 mach_msg_type_number_t keyLen
)
116 CFStringRef xmlError
= NULL
;
117 CFDictionaryRef dict
= NULL
;
119 const void *dict
= (const void *)keyRef
;
122 if (reply
== UND_REPLY_NULL
|| !reply
->inprogress
)
123 return KERN_INVALID_ARGUMENT
;
126 * JMM - No C vesion of the Unserialize code in-kernel
127 * and no C type for a CFDictionary either. For now,
128 * just pass the raw keyRef through.
131 if (keyRef
&& keyLen
) {
132 dict
= IOCFUnserialize(keyRef
, NULL
, NULL
, &xmlError
);
139 #endif /* KERNEL_CF */
141 if (reply
->callback
) {
142 (reply
->callback
)((KUNCUserNotificationID
) reply
, result
, dict
);
145 UNDReply_lock(reply
);
146 reply
->inprogress
= FALSE
;
147 reply
->userLandNotificationKey
= -1;
148 UNDReply_unlock(reply
);
149 UNDReply_deallocate(reply
);
154 * Routine: UNDNotificationCreated_rpc
156 * Intermediate routine. Allows the kernel mechanism
157 * to be informed that the notification request IS
158 * being processed by the user-level daemon, and how
159 * to identify that request.
162 UNDNotificationCreated_rpc (
164 int userLandNotificationKey
)
166 if (reply
== UND_REPLY_NULL
)
167 return KERN_INVALID_ARGUMENT
;
169 UNDReply_lock(reply
);
170 if (reply
->inprogress
|| reply
->userLandNotificationKey
!= -1) {
171 UNDReply_unlock(reply
);
172 return KERN_INVALID_ARGUMENT
;
174 reply
->userLandNotificationKey
= userLandNotificationKey
;
175 UNDReply_unlock(reply
);
184 KUNCUserNotificationID
185 KUNCGetNotificationID()
189 reply
= (UNDReplyRef
) kalloc(sizeof(struct UNDReply
));
190 if (reply
!= UND_REPLY_NULL
) {
191 reply
->self_port
= ipc_port_alloc_kernel();
192 if (reply
->self_port
== IP_NULL
) {
193 kfree(reply
, sizeof(struct UNDReply
));
194 reply
= UND_REPLY_NULL
;
196 mutex_init(&reply
->lock
, 0);
197 reply
->userLandNotificationKey
= -1;
198 reply
->inprogress
= FALSE
;
199 ipc_kobject_set(reply
->self_port
,
200 (ipc_kobject_t
)reply
,
204 return (KUNCUserNotificationID
) reply
;
208 kern_return_t
KUNCExecute(char executionPath
[1024], int uid
, int gid
)
211 UNDServerRef UNDServer
;
213 UNDServer
= UNDServer_reference();
214 if (IP_VALID(UNDServer
)) {
216 kr
= UNDExecute_rpc(UNDServer
, executionPath
, uid
, gid
);
217 UNDServer_deallocate(UNDServer
);
220 return MACH_SEND_INVALID_DEST
;
223 kern_return_t
KUNCUserNotificationCancel(
224 KUNCUserNotificationID id
)
226 UNDReplyRef reply
= (UNDReplyRef
)id
;
230 if (reply
== UND_REPLY_NULL
)
231 return KERN_INVALID_ARGUMENT
;
233 UNDReply_lock(reply
);
234 if (!reply
->inprogress
) {
235 UNDReply_unlock(reply
);
236 return KERN_INVALID_ARGUMENT
;
239 reply
->inprogress
= FALSE
;
240 if ((ulkey
= reply
->userLandNotificationKey
) != 0) {
241 UNDServerRef UNDServer
;
243 reply
->userLandNotificationKey
= 0;
244 UNDReply_unlock(reply
);
246 UNDServer
= UNDServer_reference();
247 if (IP_VALID(UNDServer
)) {
248 kr
= UNDCancelNotification_rpc(UNDServer
,ulkey
);
249 UNDServer_deallocate(UNDServer
);
251 kr
= MACH_SEND_INVALID_DEST
;
253 UNDReply_unlock(reply
);
256 UNDReply_deallocate(reply
);
261 KUNCUserNotificationDisplayNotice(
266 char *localizationPath
,
269 char *defaultButtonTitle
)
271 UNDServerRef UNDServer
;
273 UNDServer
= UNDServer_reference();
274 if (IP_VALID(UNDServer
)) {
276 kr
= UNDDisplayNoticeSimple_rpc(UNDServer
,
285 UNDServer_deallocate(UNDServer
);
288 return MACH_SEND_INVALID_DEST
;
292 KUNCUserNotificationDisplayAlert(
297 char *localizationPath
,
300 char *defaultButtonTitle
,
301 char *alternateButtonTitle
,
302 char *otherButtonTitle
,
303 unsigned *responseFlags
)
305 UNDServerRef UNDServer
;
307 UNDServer
= UNDServer_reference();
308 if (IP_VALID(UNDServer
)) {
310 kr
= UNDDisplayAlertSimple_rpc(UNDServer
,
319 alternateButtonTitle
,
322 UNDServer_deallocate(UNDServer
);
325 return MACH_SEND_INVALID_DEST
;
329 KUNCUserNotificationDisplayFromBundle(
330 KUNCUserNotificationID id
,
336 KUNCUserNotificationCallBack callback
,
337 __unused
int contextKey
)
339 UNDReplyRef reply
= (UNDReplyRef
)id
;
340 UNDServerRef UNDServer
;
341 ipc_port_t reply_port
;
343 if (reply
== UND_REPLY_NULL
)
344 return KERN_INVALID_ARGUMENT
;
345 UNDReply_lock(reply
);
346 if (reply
->inprogress
== TRUE
|| reply
->userLandNotificationKey
!= -1) {
347 UNDReply_unlock(reply
);
348 return KERN_INVALID_ARGUMENT
;
350 reply
->inprogress
= TRUE
;
351 reply
->callback
= callback
;
352 reply_port
= ipc_port_make_send(reply
->self_port
);
353 UNDReply_unlock(reply
);
355 UNDServer
= UNDServer_reference();
356 if (IP_VALID(UNDServer
)) {
359 kr
= UNDDisplayCustomFromBundle_rpc(UNDServer
,
366 UNDServer_deallocate(UNDServer
);
369 return MACH_SEND_INVALID_DEST
;
373 * Routine: convert_port_to_UNDReply
375 * MIG helper routine to convert from a mach port to a
382 convert_port_to_UNDReply(
385 if (IP_VALID(port
)) {
389 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_UND_REPLY
)) {
391 return UND_REPLY_NULL
;
393 reply
= (UNDReplyRef
) port
->ip_kobject
;
394 assert(reply
!= UND_REPLY_NULL
);
398 return UND_REPLY_NULL
;
402 * User interface for setting the host UserNotification Daemon port.
407 host_priv_t host_priv
,
410 return (host_set_user_notification_port(host_priv
, server
));
414 * User interface for retrieving the UserNotification Daemon port.
419 host_priv_t host_priv
,
420 UNDServerRef
*serverp
)
422 return (host_get_user_notification_port(host_priv
, serverp
));