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