2 * Copyright (c) 2000 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/etap_events.h>
28 #include <kern/host.h>
29 #include <ipc/ipc_port.h>
30 #include <kern/ipc_kobject.h>
32 #include <UserNotification/UNDTypes.h>
33 #include <UserNotification/UNDRequest.h>
34 #include <UserNotification/UNDReplyServer.h>
35 #include <UserNotification/KUNCUserNotifications.h>
39 #include <IOKit/IOCFSerialize.h>
40 #include <IOKit/IOCFUnserialize.h>
44 * DEFINES AND STRUCTURES
47 UNDServerRef gUNDServer
= UND_SERVER_NULL
;
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)
68 port
= reply
->self_port
;
69 ipc_kobject_set(port
, IKO_NULL
, IKOT_NONE
);
70 reply
->self_port
= IP_NULL
;
71 UNDReply_unlock(reply
);
73 ipc_port_dealloc_kernel(port
);
74 kfree((vm_offset_t
)reply
, sizeof(struct UNDReply
));
83 UNDAlertCompletedWithResult_rpc (
86 xmlData_t keyRef
, /* raw XML bytes */
87 mach_msg_type_number_t keyLen
)
90 CFStringRef xmlError
= NULL
;
91 CFDictionaryRef dict
= NULL
;
93 void *dict
= (void *)keyRef
;
96 if (reply
== UND_REPLY_NULL
|| !reply
->inprogress
)
97 return KERN_INVALID_ARGUMENT
;
100 * JMM - No C vesion of the Unserialize code in-kernel
101 * and no C type for a CFDictionary either. For now,
102 * just pass the raw keyRef through.
105 if (keyRef
&& keyLen
) {
106 dict
= IOCFUnserialize(keyRef
, NULL
, NULL
, &xmlError
);
113 #endif /* KERNEL_CF */
115 if (reply
->callback
) {
116 (reply
->callback
)((KUNCUserNotificationID
) reply
, result
, dict
);
119 UNDReply_lock(reply
);
120 reply
->inprogress
= FALSE
;
121 reply
->userLandNotificationKey
= -1;
122 UNDReply_unlock(reply
);
123 UNDReply_deallocate(reply
);
128 * Routine: UNDNotificationCreated_rpc
130 * Intermediate routine. Allows the kernel mechanism
131 * to be informed that the notification request IS
132 * being processed by the user-level daemon, and how
133 * to identify that request.
136 UNDNotificationCreated_rpc (
138 int userLandNotificationKey
)
140 if (reply
== UND_REPLY_NULL
)
141 return KERN_INVALID_ARGUMENT
;
143 UNDReply_lock(reply
);
144 if (reply
->inprogress
|| reply
->userLandNotificationKey
!= -1) {
145 UNDReply_unlock(reply
);
146 return KERN_INVALID_ARGUMENT
;
148 reply
->userLandNotificationKey
= userLandNotificationKey
;
149 UNDReply_unlock(reply
);
158 KUNCUserNotificationID
159 KUNCGetNotificationID()
163 reply
= (UNDReplyRef
) kalloc(sizeof(struct UNDReply
));
164 if (reply
!= UND_REPLY_NULL
) {
165 reply
->self_port
= ipc_port_alloc_kernel();
166 if (reply
->self_port
== IP_NULL
) {
167 kfree((vm_offset_t
)reply
, sizeof(struct UNDReply
));
168 reply
= UND_REPLY_NULL
;
170 mutex_init(&reply
->lock
, ETAP_IO_UNDREPLY
);
171 reply
->userLandNotificationKey
= -1;
172 reply
->inprogress
= FALSE
;
173 ipc_kobject_set(reply
->self_port
,
174 (ipc_kobject_t
)reply
,
178 return (KUNCUserNotificationID
) reply
;
182 kern_return_t
KUNCExecute(char executionPath
[1024], int uid
, int gid
)
184 return UNDExecute_rpc(gUNDServer
, executionPath
, uid
, gid
);
187 kern_return_t
KUNCUserNotificationCancel(
188 KUNCUserNotificationID id
)
190 UNDReplyRef reply
= (UNDReplyRef
)id
;
194 if (reply
== UND_REPLY_NULL
)
195 return KERN_INVALID_ARGUMENT
;
197 UNDReply_lock(reply
);
198 if (!reply
->inprogress
) {
199 UNDReply_unlock(reply
);
200 return KERN_INVALID_ARGUMENT
;
203 reply
->inprogress
= FALSE
;
204 if (ulkey
= reply
->userLandNotificationKey
) {
205 reply
->userLandNotificationKey
= 0;
206 UNDReply_unlock(reply
);
207 kr
= UNDCancelNotification_rpc(gUNDServer
,ulkey
);
209 UNDReply_unlock(reply
);
212 UNDReply_deallocate(reply
);
217 KUNCUserNotificationDisplayNotice(
222 char *localizationPath
,
225 char *defaultButtonTitle
)
228 kr
= UNDDisplayNoticeSimple_rpc(gUNDServer
,
241 KUNCUserNotificationDisplayAlert(
246 char *localizationPath
,
249 char *defaultButtonTitle
,
250 char *alternateButtonTitle
,
251 char *otherButtonTitle
,
252 unsigned *responseFlags
)
256 kr
= UNDDisplayAlertSimple_rpc(gUNDServer
,
265 alternateButtonTitle
,
272 KUNCUserNotificationDisplayFromBundle(
273 KUNCUserNotificationID id
,
279 KUNCUserNotificationCallBack callback
,
282 UNDReplyRef reply
= (UNDReplyRef
)id
;
283 ipc_port_t reply_port
;
286 if (reply
== UND_REPLY_NULL
)
287 return KERN_INVALID_ARGUMENT
;
288 UNDReply_lock(reply
);
289 if (reply
->inprogress
== TRUE
|| reply
->userLandNotificationKey
!= -1) {
290 UNDReply_unlock(reply
);
291 return KERN_INVALID_ARGUMENT
;
293 reply
->inprogress
== TRUE
;
294 reply
->callback
= callback
;
295 reply_port
= ipc_port_make_send(reply
->self_port
);
296 UNDReply_unlock(reply
);
298 kr
= UNDDisplayCustomFromBundle_rpc(gUNDServer
,
309 * Routine: convert_port_to_UNDReply
311 * MIG helper routine to convert from a mach port to a
318 convert_port_to_UNDReply(
321 if (IP_VALID(port
)) {
325 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_UND_REPLY
)) {
327 return UND_REPLY_NULL
;
329 reply
= (UNDReplyRef
) port
->ip_kobject
;
330 assert(reply
!= UND_REPLY_NULL
);
333 return UND_REPLY_NULL
;
337 * User interface for setting the host UserNotification Daemon port.
342 host_priv_t host_priv
,
346 if (host_priv
== HOST_PRIV_NULL
|| server
== UND_SERVER_NULL
)
347 return KERN_INVALID_ARGUMENT
;
348 if (gUNDServer
!= UND_SERVER_NULL
)
349 ipc_port_dealloc_kernel(gUNDServer
);
355 * User interface for retrieving the UserNotification Daemon port.
360 host_priv_t host_priv
,
361 UNDServerRef
*server
)
363 if (host_priv
== HOST_PRIV_NULL
)
364 return KERN_INVALID_ARGUMENT
;
365 *server
= gUNDServer
;