]> git.saurik.com Git - apple/xnu.git/blob - osfmk/UserNotification/KUNCUserNotifications.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / osfmk / UserNotification / KUNCUserNotifications.c
1 /*
2 * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <mach/port.h>
30 #include <mach/message.h>
31 #include <mach/kern_return.h>
32 #include <mach/host_priv.h>
33
34 #include <kern/kern_types.h>
35 #include <kern/kalloc.h>
36 #include <kern/host.h>
37 #include <kern/ipc_kobject.h>
38
39 #include <ipc/ipc_port.h>
40
41 #include <UserNotification/UNDTypes.h>
42 #include <UserNotification/UNDRequest.h>
43 #include <UserNotification/UNDReplyServer.h>
44 #include <UserNotification/KUNCUserNotifications.h>
45
46 #ifdef KERNEL_CF
47 // external
48 #include <IOKit/IOCFSerialize.h>
49 #include <IOKit/IOCFUnserialize.h>
50 #endif
51
52 #if CONFIG_USER_NOTIFICATION
53 /*
54 * DEFINES AND STRUCTURES
55 */
56
57 struct UNDReply {
58 decl_lck_mtx_data(, lock); /* UNDReply lock */
59 int userLandNotificationKey;
60 KUNCUserNotificationCallBack callback;
61 boolean_t inprogress;
62 ipc_port_t self_port; /* Our port */
63 };
64
65 #define UNDReply_lock(reply) lck_mtx_lock(&reply->lock)
66 #define UNDReply_unlock(reply) lck_mtx_unlock(&reply->lock)
67
68 extern lck_grp_t LockCompatGroup;
69
70 /* forward declarations */
71 void UNDReply_deallocate(
72 UNDReplyRef reply);
73
74
75 void
76 UNDReply_deallocate(
77 UNDReplyRef reply)
78 {
79 ipc_port_t port;
80
81 UNDReply_lock(reply);
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);
87
88 ipc_port_dealloc_kernel(port);
89 lck_mtx_destroy(&reply->lock, &LockCompatGroup);
90 kfree(reply, sizeof(struct UNDReply));
91 return;
92 }
93
94 static UNDServerRef
95 UNDServer_reference(void)
96 {
97 UNDServerRef UNDServer;
98 kern_return_t kr;
99
100 kr = host_get_user_notification_port(host_priv_self(), &UNDServer);
101 assert(kr == KERN_SUCCESS);
102 return UNDServer;
103 }
104
105 static void
106 UNDServer_deallocate(
107 UNDServerRef UNDServer)
108 {
109 if (IP_VALID(UNDServer)) {
110 ipc_port_release_send(UNDServer);
111 }
112 }
113
114 /*
115 * UND Mig Callbacks
116 */
117
118 kern_return_t
119 UNDAlertCompletedWithResult_rpc(
120 UNDReplyRef reply,
121 int result,
122 xmlData_t keyRef, /* raw XML bytes */
123 #ifdef KERNEL_CF
124 mach_msg_type_number_t keyLen)
125 #else
126 __unused mach_msg_type_number_t keyLen)
127 #endif
128 {
129 #ifdef KERNEL_CF
130 CFStringRef xmlError = NULL;
131 CFDictionaryRef dict = NULL;
132 #else
133 const void *dict = (const void *)keyRef;
134 #endif
135
136 if (reply == UND_REPLY_NULL || !reply->inprogress) {
137 return KERN_INVALID_ARGUMENT;
138 }
139
140 /*
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.
144 */
145 #ifdef KERNEL_CF
146 if (keyRef && keyLen) {
147 dict = IOCFUnserialize(keyRef, NULL, NULL, &xmlError);
148 }
149
150 if (xmlError) {
151 CFShow(xmlError);
152 CFRelease(xmlError);
153 }
154 #endif /* KERNEL_CF */
155
156 if (reply->callback) {
157 (reply->callback)((int)(KUNCUserNotificationID)reply, result, dict);
158 }
159
160 UNDReply_lock(reply);
161 reply->inprogress = FALSE;
162 reply->userLandNotificationKey = -1;
163 UNDReply_unlock(reply);
164 UNDReply_deallocate(reply);
165 return KERN_SUCCESS;
166 }
167
168 /*
169 * Routine: UNDNotificationCreated_rpc
170 *
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.
175 */
176 kern_return_t
177 UNDNotificationCreated_rpc(
178 UNDReplyRef reply,
179 int userLandNotificationKey)
180 {
181 if (reply == UND_REPLY_NULL) {
182 return KERN_INVALID_ARGUMENT;
183 }
184
185 UNDReply_lock(reply);
186 if (reply->inprogress || reply->userLandNotificationKey != -1) {
187 UNDReply_unlock(reply);
188 return KERN_INVALID_ARGUMENT;
189 }
190 reply->userLandNotificationKey = userLandNotificationKey;
191 UNDReply_unlock(reply);
192 return KERN_SUCCESS;
193 }
194
195 /*
196 * KUNC Functions
197 */
198
199
200 KUNCUserNotificationID
201 KUNCGetNotificationID(void)
202 {
203 UNDReplyRef reply;
204
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;
212 }
213 return (KUNCUserNotificationID) reply;
214 }
215
216
217 kern_return_t
218 KUNCExecute(char executionPath[1024], int uid, int gid)
219 {
220 UNDServerRef UNDServer;
221
222 UNDServer = UNDServer_reference();
223 if (IP_VALID(UNDServer)) {
224 kern_return_t kr;
225 kr = UNDExecute_rpc(UNDServer, executionPath, uid, gid);
226 UNDServer_deallocate(UNDServer);
227 return kr;
228 }
229 return MACH_SEND_INVALID_DEST;
230 }
231
232 kern_return_t
233 KUNCUserNotificationCancel(
234 KUNCUserNotificationID id)
235 {
236 UNDReplyRef reply = (UNDReplyRef)id;
237 kern_return_t kr;
238 int ulkey;
239
240 if (reply == UND_REPLY_NULL) {
241 return KERN_INVALID_ARGUMENT;
242 }
243
244 UNDReply_lock(reply);
245 if (!reply->inprogress) {
246 UNDReply_unlock(reply);
247 return KERN_INVALID_ARGUMENT;
248 }
249
250 reply->inprogress = FALSE;
251 if ((ulkey = reply->userLandNotificationKey) != 0) {
252 UNDServerRef UNDServer;
253
254 reply->userLandNotificationKey = 0;
255 UNDReply_unlock(reply);
256
257 UNDServer = UNDServer_reference();
258 if (IP_VALID(UNDServer)) {
259 kr = UNDCancelNotification_rpc(UNDServer, ulkey);
260 UNDServer_deallocate(UNDServer);
261 } else {
262 kr = MACH_SEND_INVALID_DEST;
263 }
264 } else {
265 UNDReply_unlock(reply);
266 kr = KERN_SUCCESS;
267 }
268 UNDReply_deallocate(reply);
269 return kr;
270 }
271
272 kern_return_t
273 KUNCUserNotificationDisplayNotice(
274 int noticeTimeout,
275 unsigned flags,
276 char *iconPath,
277 char *soundPath,
278 char *localizationPath,
279 char *alertHeader,
280 char *alertMessage,
281 char *defaultButtonTitle)
282 {
283 UNDServerRef UNDServer;
284
285 UNDServer = UNDServer_reference();
286 if (IP_VALID(UNDServer)) {
287 kern_return_t kr;
288 kr = UNDDisplayNoticeSimple_rpc(UNDServer,
289 noticeTimeout,
290 flags,
291 iconPath,
292 soundPath,
293 localizationPath,
294 alertHeader,
295 alertMessage,
296 defaultButtonTitle);
297 UNDServer_deallocate(UNDServer);
298 return kr;
299 }
300 return MACH_SEND_INVALID_DEST;
301 }
302
303 kern_return_t
304 KUNCUserNotificationDisplayAlert(
305 int alertTimeout,
306 unsigned flags,
307 char *iconPath,
308 char *soundPath,
309 char *localizationPath,
310 char *alertHeader,
311 char *alertMessage,
312 char *defaultButtonTitle,
313 char *alternateButtonTitle,
314 char *otherButtonTitle,
315 unsigned *responseFlags)
316 {
317 UNDServerRef UNDServer;
318
319 UNDServer = UNDServer_reference();
320 if (IP_VALID(UNDServer)) {
321 kern_return_t kr;
322 kr = UNDDisplayAlertSimple_rpc(UNDServer,
323 alertTimeout,
324 flags,
325 iconPath,
326 soundPath,
327 localizationPath,
328 alertHeader,
329 alertMessage,
330 defaultButtonTitle,
331 alternateButtonTitle,
332 otherButtonTitle,
333 responseFlags);
334 UNDServer_deallocate(UNDServer);
335 return kr;
336 }
337 return MACH_SEND_INVALID_DEST;
338 }
339
340 kern_return_t
341 KUNCUserNotificationDisplayFromBundle(
342 KUNCUserNotificationID id,
343 char *bundlePath,
344 char *fileName,
345 char *fileExtension,
346 char *messageKey,
347 char *tokenString,
348 KUNCUserNotificationCallBack callback,
349 __unused int contextKey)
350 {
351 UNDReplyRef reply = (UNDReplyRef)id;
352 UNDServerRef UNDServer;
353 ipc_port_t reply_port;
354
355 if (reply == UND_REPLY_NULL) {
356 return KERN_INVALID_ARGUMENT;
357 }
358 UNDReply_lock(reply);
359 if (reply->inprogress == TRUE || reply->userLandNotificationKey != -1) {
360 UNDReply_unlock(reply);
361 return KERN_INVALID_ARGUMENT;
362 }
363 reply->inprogress = TRUE;
364 reply->callback = callback;
365 reply_port = ipc_port_make_send(reply->self_port);
366 UNDReply_unlock(reply);
367
368 UNDServer = UNDServer_reference();
369 if (IP_VALID(UNDServer)) {
370 kern_return_t kr;
371
372 kr = UNDDisplayCustomFromBundle_rpc(UNDServer,
373 reply_port,
374 bundlePath,
375 fileName,
376 fileExtension,
377 messageKey,
378 tokenString);
379 UNDServer_deallocate(UNDServer);
380 return kr;
381 }
382 return MACH_SEND_INVALID_DEST;
383 }
384
385 /*
386 * Routine: convert_port_to_UNDReply
387 *
388 * MIG helper routine to convert from a mach port to a
389 * UNDReply object.
390 *
391 * Assumptions:
392 * Nothing locked.
393 */
394 UNDReplyRef
395 convert_port_to_UNDReply(
396 ipc_port_t port)
397 {
398 if (IP_VALID(port)) {
399 UNDReplyRef reply;
400
401 ip_lock(port);
402 if (!ip_active(port) || (ip_kotype(port) != IKOT_UND_REPLY)) {
403 ip_unlock(port);
404 return UND_REPLY_NULL;
405 }
406 reply = (UNDReplyRef) ip_get_kobject(port);
407 assert(reply != UND_REPLY_NULL);
408 ip_unlock(port);
409 return reply;
410 }
411 return UND_REPLY_NULL;
412 }
413 #endif
414
415 /*
416 * User interface for setting the host UserNotification Daemon port.
417 */
418
419 kern_return_t
420 host_set_UNDServer(
421 host_priv_t host_priv,
422 UNDServerRef server)
423 {
424 #if CONFIG_USER_NOTIFICATION
425 return host_set_user_notification_port(host_priv, server);
426 #else
427 #pragma unused(host_priv, server)
428 return KERN_NOT_SUPPORTED;
429 #endif
430 }
431
432 /*
433 * User interface for retrieving the UserNotification Daemon port.
434 */
435
436 kern_return_t
437 host_get_UNDServer(
438 host_priv_t host_priv,
439 UNDServerRef *serverp)
440 {
441 #if CONFIG_USER_NOTIFICATION
442 return host_get_user_notification_port(host_priv, serverp);
443 #else
444 #pragma unused(host_priv, serverp)
445 return KERN_NOT_SUPPORTED;
446 #endif
447 }