2 * Copyright (c) 2000-2019 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_unlock(&reply->lock)
67 extern lck_grp_t LockCompatGroup
;
69 /* forward declarations */
70 void UNDReply_deallocate(
81 port
= reply
->self_port
;
82 assert(IP_VALID(port
));
83 ipc_kobject_set(port
, IKO_NULL
, IKOT_NONE
);
84 reply
->self_port
= IP_NULL
;
85 UNDReply_unlock(reply
);
87 ipc_port_dealloc_kernel(port
);
88 lck_mtx_destroy(&reply
->lock
, &LockCompatGroup
);
89 kfree(reply
, sizeof(struct UNDReply
));
94 UNDServer_reference(void)
96 UNDServerRef UNDServer
;
99 kr
= host_get_user_notification_port(host_priv_self(), &UNDServer
);
100 assert(kr
== KERN_SUCCESS
);
105 UNDServer_deallocate(
106 UNDServerRef UNDServer
)
108 if (IP_VALID(UNDServer
)) {
109 ipc_port_release_send(UNDServer
);
118 UNDAlertCompletedWithResult_rpc(
121 xmlData_t keyRef
, /* raw XML bytes */
123 mach_msg_type_number_t keyLen
)
125 __unused mach_msg_type_number_t keyLen
)
129 CFStringRef xmlError
= NULL
;
130 CFDictionaryRef dict
= NULL
;
132 const void *dict
= (const void *)keyRef
;
135 if (reply
== UND_REPLY_NULL
|| !reply
->inprogress
) {
136 return KERN_INVALID_ARGUMENT
;
140 * JMM - No C vesion of the Unserialize code in-kernel
141 * and no C type for a CFDictionary either. For now,
142 * just pass the raw keyRef through.
145 if (keyRef
&& keyLen
) {
146 dict
= IOCFUnserialize(keyRef
, NULL
, NULL
, &xmlError
);
153 #endif /* KERNEL_CF */
155 if (reply
->callback
) {
156 (reply
->callback
)((int)(KUNCUserNotificationID
)reply
, result
, dict
);
159 UNDReply_lock(reply
);
160 reply
->inprogress
= FALSE
;
161 reply
->userLandNotificationKey
= -1;
162 UNDReply_unlock(reply
);
163 UNDReply_deallocate(reply
);
168 * Routine: UNDNotificationCreated_rpc
170 * Intermediate routine. Allows the kernel mechanism
171 * to be informed that the notification request IS
172 * being processed by the user-level daemon, and how
173 * to identify that request.
176 UNDNotificationCreated_rpc(
178 int userLandNotificationKey
)
180 if (reply
== UND_REPLY_NULL
) {
181 return KERN_INVALID_ARGUMENT
;
184 UNDReply_lock(reply
);
185 if (reply
->inprogress
|| reply
->userLandNotificationKey
!= -1) {
186 UNDReply_unlock(reply
);
187 return KERN_INVALID_ARGUMENT
;
189 reply
->userLandNotificationKey
= userLandNotificationKey
;
190 UNDReply_unlock(reply
);
199 KUNCUserNotificationID
200 KUNCGetNotificationID(void)
204 reply
= (UNDReplyRef
) kalloc(sizeof(struct UNDReply
));
205 if (reply
!= UND_REPLY_NULL
) {
206 reply
->self_port
= ipc_kobject_alloc_port((ipc_kobject_t
)reply
,
207 IKOT_UND_REPLY
, IPC_KOBJECT_ALLOC_NONE
);
208 lck_mtx_init(&reply
->lock
, &LockCompatGroup
, LCK_ATTR_NULL
);
209 reply
->userLandNotificationKey
= -1;
210 reply
->inprogress
= FALSE
;
212 return (KUNCUserNotificationID
) reply
;
217 KUNCExecute(char executionPath
[1024], int uid
, int gid
)
219 UNDServerRef UNDServer
;
221 UNDServer
= UNDServer_reference();
222 if (IP_VALID(UNDServer
)) {
224 kr
= UNDExecute_rpc(UNDServer
, executionPath
, uid
, gid
);
225 UNDServer_deallocate(UNDServer
);
228 return MACH_SEND_INVALID_DEST
;
232 KUNCUserNotificationCancel(
233 KUNCUserNotificationID id
)
235 UNDReplyRef reply
= (UNDReplyRef
)id
;
239 if (reply
== UND_REPLY_NULL
) {
240 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
;
264 UNDReply_unlock(reply
);
267 UNDReply_deallocate(reply
);
272 KUNCUserNotificationDisplayNotice(
277 char *localizationPath
,
280 char *defaultButtonTitle
)
282 UNDServerRef UNDServer
;
284 UNDServer
= UNDServer_reference();
285 if (IP_VALID(UNDServer
)) {
287 kr
= UNDDisplayNoticeSimple_rpc(UNDServer
,
296 UNDServer_deallocate(UNDServer
);
299 return MACH_SEND_INVALID_DEST
;
303 KUNCUserNotificationDisplayAlert(
308 char *localizationPath
,
311 char *defaultButtonTitle
,
312 char *alternateButtonTitle
,
313 char *otherButtonTitle
,
314 unsigned *responseFlags
)
316 UNDServerRef UNDServer
;
318 UNDServer
= UNDServer_reference();
319 if (IP_VALID(UNDServer
)) {
321 kr
= UNDDisplayAlertSimple_rpc(UNDServer
,
330 alternateButtonTitle
,
333 UNDServer_deallocate(UNDServer
);
336 return MACH_SEND_INVALID_DEST
;
340 KUNCUserNotificationDisplayFromBundle(
341 KUNCUserNotificationID id
,
347 KUNCUserNotificationCallBack callback
,
348 __unused
int contextKey
)
350 UNDReplyRef reply
= (UNDReplyRef
)id
;
351 UNDServerRef UNDServer
;
352 ipc_port_t reply_port
;
354 if (reply
== UND_REPLY_NULL
) {
355 return KERN_INVALID_ARGUMENT
;
357 UNDReply_lock(reply
);
358 if (reply
->inprogress
== TRUE
|| reply
->userLandNotificationKey
!= -1) {
359 UNDReply_unlock(reply
);
360 return KERN_INVALID_ARGUMENT
;
362 reply
->inprogress
= TRUE
;
363 reply
->callback
= callback
;
364 reply_port
= ipc_port_make_send(reply
->self_port
);
365 UNDReply_unlock(reply
);
367 UNDServer
= UNDServer_reference();
368 if (IP_VALID(UNDServer
)) {
371 kr
= UNDDisplayCustomFromBundle_rpc(UNDServer
,
378 UNDServer_deallocate(UNDServer
);
381 return MACH_SEND_INVALID_DEST
;
385 * Routine: convert_port_to_UNDReply
387 * MIG helper routine to convert from a mach port to a
394 convert_port_to_UNDReply(
397 if (IP_VALID(port
)) {
401 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_UND_REPLY
)) {
403 return UND_REPLY_NULL
;
405 reply
= (UNDReplyRef
) ip_get_kobject(port
);
406 assert(reply
!= UND_REPLY_NULL
);
410 return UND_REPLY_NULL
;
414 * User interface for setting the host UserNotification Daemon port.
419 host_priv_t host_priv
,
422 return host_set_user_notification_port(host_priv
, server
);
426 * User interface for retrieving the UserNotification Daemon port.
431 host_priv_t host_priv
,
432 UNDServerRef
*serverp
)
434 return host_get_user_notification_port(host_priv
, serverp
);