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>
52 #if CONFIG_USER_NOTIFICATION
54 * DEFINES AND STRUCTURES
58 decl_lck_mtx_data(, lock
); /* UNDReply lock */
59 int userLandNotificationKey
;
60 KUNCUserNotificationCallBack callback
;
62 ipc_port_t self_port
; /* Our port */
65 #define UNDReply_lock(reply) lck_mtx_lock(&reply->lock)
66 #define UNDReply_unlock(reply) lck_mtx_unlock(&reply->lock)
68 extern lck_grp_t LockCompatGroup
;
70 /* forward declarations */
71 void UNDReply_deallocate(
82 port
= reply
->self_port
;
83 assert(IP_VALID(port
));
84 ipc_kobject_set(port
, IKO_NULL
, IKOT_NONE
);
85 reply
->self_port
= IP_NULL
;
86 UNDReply_unlock(reply
);
88 ipc_port_dealloc_kernel(port
);
89 lck_mtx_destroy(&reply
->lock
, &LockCompatGroup
);
90 kfree(reply
, sizeof(struct UNDReply
));
95 UNDServer_reference(void)
97 UNDServerRef UNDServer
;
100 kr
= host_get_user_notification_port(host_priv_self(), &UNDServer
);
101 assert(kr
== KERN_SUCCESS
);
106 UNDServer_deallocate(
107 UNDServerRef UNDServer
)
109 if (IP_VALID(UNDServer
)) {
110 ipc_port_release_send(UNDServer
);
119 UNDAlertCompletedWithResult_rpc(
122 xmlData_t keyRef
, /* raw XML bytes */
124 mach_msg_type_number_t keyLen
)
126 __unused mach_msg_type_number_t keyLen
)
130 CFStringRef xmlError
= NULL
;
131 CFDictionaryRef dict
= NULL
;
133 const void *dict
= (const void *)keyRef
;
136 if (reply
== UND_REPLY_NULL
|| !reply
->inprogress
) {
137 return KERN_INVALID_ARGUMENT
;
141 * JMM - No C vesion of the Unserialize code in-kernel
142 * and no C type for a CFDictionary either. For now,
143 * just pass the raw keyRef through.
146 if (keyRef
&& keyLen
) {
147 dict
= IOCFUnserialize(keyRef
, NULL
, NULL
, &xmlError
);
154 #endif /* KERNEL_CF */
156 if (reply
->callback
) {
157 (reply
->callback
)((int)(KUNCUserNotificationID
)reply
, result
, dict
);
160 UNDReply_lock(reply
);
161 reply
->inprogress
= FALSE
;
162 reply
->userLandNotificationKey
= -1;
163 UNDReply_unlock(reply
);
164 UNDReply_deallocate(reply
);
169 * Routine: UNDNotificationCreated_rpc
171 * Intermediate routine. Allows the kernel mechanism
172 * to be informed that the notification request IS
173 * being processed by the user-level daemon, and how
174 * to identify that request.
177 UNDNotificationCreated_rpc(
179 int userLandNotificationKey
)
181 if (reply
== UND_REPLY_NULL
) {
182 return KERN_INVALID_ARGUMENT
;
185 UNDReply_lock(reply
);
186 if (reply
->inprogress
|| reply
->userLandNotificationKey
!= -1) {
187 UNDReply_unlock(reply
);
188 return KERN_INVALID_ARGUMENT
;
190 reply
->userLandNotificationKey
= userLandNotificationKey
;
191 UNDReply_unlock(reply
);
200 KUNCUserNotificationID
201 KUNCGetNotificationID(void)
205 reply
= (UNDReplyRef
) kalloc(sizeof(struct UNDReply
));
206 if (reply
!= UND_REPLY_NULL
) {
207 reply
->self_port
= ipc_kobject_alloc_port((ipc_kobject_t
)reply
,
208 IKOT_UND_REPLY
, IPC_KOBJECT_ALLOC_NONE
);
209 lck_mtx_init(&reply
->lock
, &LockCompatGroup
, LCK_ATTR_NULL
);
210 reply
->userLandNotificationKey
= -1;
211 reply
->inprogress
= FALSE
;
213 return (KUNCUserNotificationID
) reply
;
218 KUNCExecute(char executionPath
[1024], int uid
, int gid
)
220 UNDServerRef UNDServer
;
222 UNDServer
= UNDServer_reference();
223 if (IP_VALID(UNDServer
)) {
225 kr
= UNDExecute_rpc(UNDServer
, executionPath
, uid
, gid
);
226 UNDServer_deallocate(UNDServer
);
229 return MACH_SEND_INVALID_DEST
;
233 KUNCUserNotificationCancel(
234 KUNCUserNotificationID id
)
236 UNDReplyRef reply
= (UNDReplyRef
)id
;
240 if (reply
== UND_REPLY_NULL
) {
241 return KERN_INVALID_ARGUMENT
;
244 UNDReply_lock(reply
);
245 if (!reply
->inprogress
) {
246 UNDReply_unlock(reply
);
247 return KERN_INVALID_ARGUMENT
;
250 reply
->inprogress
= FALSE
;
251 if ((ulkey
= reply
->userLandNotificationKey
) != 0) {
252 UNDServerRef UNDServer
;
254 reply
->userLandNotificationKey
= 0;
255 UNDReply_unlock(reply
);
257 UNDServer
= UNDServer_reference();
258 if (IP_VALID(UNDServer
)) {
259 kr
= UNDCancelNotification_rpc(UNDServer
, ulkey
);
260 UNDServer_deallocate(UNDServer
);
262 kr
= MACH_SEND_INVALID_DEST
;
265 UNDReply_unlock(reply
);
268 UNDReply_deallocate(reply
);
273 KUNCUserNotificationDisplayNotice(
278 char *localizationPath
,
281 char *defaultButtonTitle
)
283 UNDServerRef UNDServer
;
285 UNDServer
= UNDServer_reference();
286 if (IP_VALID(UNDServer
)) {
288 kr
= UNDDisplayNoticeSimple_rpc(UNDServer
,
297 UNDServer_deallocate(UNDServer
);
300 return MACH_SEND_INVALID_DEST
;
304 KUNCUserNotificationDisplayAlert(
309 char *localizationPath
,
312 char *defaultButtonTitle
,
313 char *alternateButtonTitle
,
314 char *otherButtonTitle
,
315 unsigned *responseFlags
)
317 UNDServerRef UNDServer
;
319 UNDServer
= UNDServer_reference();
320 if (IP_VALID(UNDServer
)) {
322 kr
= UNDDisplayAlertSimple_rpc(UNDServer
,
331 alternateButtonTitle
,
334 UNDServer_deallocate(UNDServer
);
337 return MACH_SEND_INVALID_DEST
;
341 KUNCUserNotificationDisplayFromBundle(
342 KUNCUserNotificationID id
,
348 KUNCUserNotificationCallBack callback
,
349 __unused
int contextKey
)
351 UNDReplyRef reply
= (UNDReplyRef
)id
;
352 UNDServerRef UNDServer
;
353 ipc_port_t reply_port
;
355 if (reply
== UND_REPLY_NULL
) {
356 return KERN_INVALID_ARGUMENT
;
358 UNDReply_lock(reply
);
359 if (reply
->inprogress
== TRUE
|| reply
->userLandNotificationKey
!= -1) {
360 UNDReply_unlock(reply
);
361 return KERN_INVALID_ARGUMENT
;
363 reply
->inprogress
= TRUE
;
364 reply
->callback
= callback
;
365 reply_port
= ipc_port_make_send(reply
->self_port
);
366 UNDReply_unlock(reply
);
368 UNDServer
= UNDServer_reference();
369 if (IP_VALID(UNDServer
)) {
372 kr
= UNDDisplayCustomFromBundle_rpc(UNDServer
,
379 UNDServer_deallocate(UNDServer
);
382 return MACH_SEND_INVALID_DEST
;
386 * Routine: convert_port_to_UNDReply
388 * MIG helper routine to convert from a mach port to a
395 convert_port_to_UNDReply(
398 if (IP_VALID(port
)) {
402 if (!ip_active(port
) || (ip_kotype(port
) != IKOT_UND_REPLY
)) {
404 return UND_REPLY_NULL
;
406 reply
= (UNDReplyRef
) ip_get_kobject(port
);
407 assert(reply
!= UND_REPLY_NULL
);
411 return UND_REPLY_NULL
;
416 * User interface for setting the host UserNotification Daemon port.
421 host_priv_t host_priv
,
424 #if CONFIG_USER_NOTIFICATION
425 return host_set_user_notification_port(host_priv
, server
);
427 #pragma unused(host_priv, server)
428 return KERN_NOT_SUPPORTED
;
433 * User interface for retrieving the UserNotification Daemon port.
438 host_priv_t host_priv
,
439 UNDServerRef
*serverp
)
441 #if CONFIG_USER_NOTIFICATION
442 return host_get_user_notification_port(host_priv
, serverp
);
444 #pragma unused(host_priv, serverp)
445 return KERN_NOT_SUPPORTED
;