]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
cb323159 | 2 | * Copyright (c) 2000-2019 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
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. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | ||
29 | #include <mach/port.h> | |
30 | #include <mach/message.h> | |
31 | #include <mach/kern_return.h> | |
91447636 | 32 | #include <mach/host_priv.h> |
1c79356b | 33 | |
91447636 A |
34 | #include <kern/kern_types.h> |
35 | #include <kern/kalloc.h> | |
1c79356b | 36 | #include <kern/host.h> |
1c79356b A |
37 | #include <kern/ipc_kobject.h> |
38 | ||
91447636 A |
39 | #include <ipc/ipc_port.h> |
40 | ||
1c79356b A |
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 | ||
1c79356b | 56 | struct UNDReply { |
cb323159 | 57 | decl_lck_mtx_data(, lock); /* UNDReply lock */ |
0a7de745 A |
58 | int userLandNotificationKey; |
59 | KUNCUserNotificationCallBack callback; | |
60 | boolean_t inprogress; | |
61 | ipc_port_t self_port; /* Our port */ | |
1c79356b A |
62 | }; |
63 | ||
0a7de745 A |
64 | #define UNDReply_lock(reply) lck_mtx_lock(&reply->lock) |
65 | #define UNDReply_unlock(reply) lck_mtx_unlock(&reply->lock) | |
1c79356b | 66 | |
39236c6e A |
67 | extern lck_grp_t LockCompatGroup; |
68 | ||
91447636 A |
69 | /* forward declarations */ |
70 | void UNDReply_deallocate( | |
0a7de745 | 71 | UNDReplyRef reply); |
91447636 A |
72 | |
73 | ||
1c79356b A |
74 | void |
75 | UNDReply_deallocate( | |
0a7de745 | 76 | UNDReplyRef reply) |
1c79356b A |
77 | { |
78 | ipc_port_t port; | |
55e303ae | 79 | |
1c79356b A |
80 | UNDReply_lock(reply); |
81 | port = reply->self_port; | |
55e303ae | 82 | assert(IP_VALID(port)); |
1c79356b A |
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); | |
39236c6e | 88 | lck_mtx_destroy(&reply->lock, &LockCompatGroup); |
91447636 | 89 | kfree(reply, sizeof(struct UNDReply)); |
1c79356b A |
90 | return; |
91 | } | |
92 | ||
55e303ae A |
93 | static UNDServerRef |
94 | UNDServer_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 | ||
104 | static void | |
105 | UNDServer_deallocate( | |
0a7de745 | 106 | UNDServerRef UNDServer) |
55e303ae | 107 | { |
0a7de745 | 108 | if (IP_VALID(UNDServer)) { |
55e303ae | 109 | ipc_port_release_send(UNDServer); |
0a7de745 | 110 | } |
55e303ae A |
111 | } |
112 | ||
0a7de745 | 113 | /* |
1c79356b | 114 | * UND Mig Callbacks |
0a7de745 | 115 | */ |
1c79356b A |
116 | |
117 | kern_return_t | |
0a7de745 A |
118 | UNDAlertCompletedWithResult_rpc( |
119 | UNDReplyRef reply, | |
120 | int result, | |
121 | xmlData_t keyRef, /* raw XML bytes */ | |
2d21ac55 | 122 | #ifdef KERNEL_CF |
0a7de745 | 123 | mach_msg_type_number_t keyLen) |
2d21ac55 | 124 | #else |
0a7de745 | 125 | __unused mach_msg_type_number_t keyLen) |
2d21ac55 | 126 | #endif |
1c79356b A |
127 | { |
128 | #ifdef KERNEL_CF | |
0a7de745 A |
129 | CFStringRef xmlError = NULL; |
130 | CFDictionaryRef dict = NULL; | |
1c79356b | 131 | #else |
91447636 | 132 | const void *dict = (const void *)keyRef; |
1c79356b A |
133 | #endif |
134 | ||
0a7de745 | 135 | if (reply == UND_REPLY_NULL || !reply->inprogress) { |
1c79356b | 136 | return KERN_INVALID_ARGUMENT; |
0a7de745 | 137 | } |
1c79356b A |
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 | */ | |
0a7de745 | 144 | #ifdef KERNEL_CF |
1c79356b A |
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) { | |
b0d623f7 | 156 | (reply->callback)((int)(KUNCUserNotificationID)reply, result, dict); |
1c79356b A |
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 | */ | |
175 | kern_return_t | |
0a7de745 A |
176 | UNDNotificationCreated_rpc( |
177 | UNDReplyRef reply, | |
178 | int userLandNotificationKey) | |
1c79356b | 179 | { |
0a7de745 | 180 | if (reply == UND_REPLY_NULL) { |
1c79356b | 181 | return KERN_INVALID_ARGUMENT; |
0a7de745 | 182 | } |
1c79356b A |
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 | |
0a7de745 | 196 | */ |
1c79356b A |
197 | |
198 | ||
199 | KUNCUserNotificationID | |
2d21ac55 | 200 | KUNCGetNotificationID(void) |
1c79356b A |
201 | { |
202 | UNDReplyRef reply; | |
203 | ||
204 | reply = (UNDReplyRef) kalloc(sizeof(struct UNDReply)); | |
205 | if (reply != UND_REPLY_NULL) { | |
cb323159 A |
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; | |
1c79356b A |
211 | } |
212 | return (KUNCUserNotificationID) reply; | |
213 | } | |
214 | ||
215 | ||
0a7de745 A |
216 | kern_return_t |
217 | KUNCExecute(char executionPath[1024], int uid, int gid) | |
1c79356b | 218 | { |
55e303ae A |
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; | |
1c79356b A |
229 | } |
230 | ||
0a7de745 A |
231 | kern_return_t |
232 | KUNCUserNotificationCancel( | |
1c79356b A |
233 | KUNCUserNotificationID id) |
234 | { | |
235 | UNDReplyRef reply = (UNDReplyRef)id; | |
236 | kern_return_t kr; | |
237 | int ulkey; | |
238 | ||
0a7de745 | 239 | if (reply == UND_REPLY_NULL) { |
1c79356b | 240 | return KERN_INVALID_ARGUMENT; |
0a7de745 | 241 | } |
1c79356b A |
242 | |
243 | UNDReply_lock(reply); | |
244 | if (!reply->inprogress) { | |
245 | UNDReply_unlock(reply); | |
246 | return KERN_INVALID_ARGUMENT; | |
247 | } | |
248 | ||
249 | reply->inprogress = FALSE; | |
91447636 | 250 | if ((ulkey = reply->userLandNotificationKey) != 0) { |
55e303ae A |
251 | UNDServerRef UNDServer; |
252 | ||
1c79356b A |
253 | reply->userLandNotificationKey = 0; |
254 | UNDReply_unlock(reply); | |
55e303ae A |
255 | |
256 | UNDServer = UNDServer_reference(); | |
257 | if (IP_VALID(UNDServer)) { | |
0a7de745 | 258 | kr = UNDCancelNotification_rpc(UNDServer, ulkey); |
55e303ae | 259 | UNDServer_deallocate(UNDServer); |
0a7de745 | 260 | } else { |
55e303ae | 261 | kr = MACH_SEND_INVALID_DEST; |
0a7de745 | 262 | } |
1c79356b A |
263 | } else { |
264 | UNDReply_unlock(reply); | |
265 | kr = KERN_SUCCESS; | |
266 | } | |
267 | UNDReply_deallocate(reply); | |
268 | return kr; | |
269 | } | |
270 | ||
271 | kern_return_t | |
272 | KUNCUserNotificationDisplayNotice( | |
0a7de745 A |
273 | int noticeTimeout, |
274 | unsigned flags, | |
275 | char *iconPath, | |
276 | char *soundPath, | |
277 | char *localizationPath, | |
278 | char *alertHeader, | |
279 | char *alertMessage, | |
280 | char *defaultButtonTitle) | |
1c79356b | 281 | { |
55e303ae A |
282 | UNDServerRef UNDServer; |
283 | ||
284 | UNDServer = UNDServer_reference(); | |
285 | if (IP_VALID(UNDServer)) { | |
286 | kern_return_t kr; | |
287 | kr = UNDDisplayNoticeSimple_rpc(UNDServer, | |
0a7de745 A |
288 | noticeTimeout, |
289 | flags, | |
290 | iconPath, | |
291 | soundPath, | |
292 | localizationPath, | |
293 | alertHeader, | |
294 | alertMessage, | |
295 | defaultButtonTitle); | |
55e303ae A |
296 | UNDServer_deallocate(UNDServer); |
297 | return kr; | |
298 | } | |
299 | return MACH_SEND_INVALID_DEST; | |
1c79356b A |
300 | } |
301 | ||
302 | kern_return_t | |
303 | KUNCUserNotificationDisplayAlert( | |
0a7de745 A |
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) | |
1c79356b | 315 | { |
0a7de745 A |
316 | UNDServerRef UNDServer; |
317 | ||
55e303ae A |
318 | UNDServer = UNDServer_reference(); |
319 | if (IP_VALID(UNDServer)) { | |
0a7de745 | 320 | kern_return_t kr; |
55e303ae | 321 | kr = UNDDisplayAlertSimple_rpc(UNDServer, |
0a7de745 A |
322 | alertTimeout, |
323 | flags, | |
324 | iconPath, | |
325 | soundPath, | |
326 | localizationPath, | |
327 | alertHeader, | |
328 | alertMessage, | |
329 | defaultButtonTitle, | |
330 | alternateButtonTitle, | |
331 | otherButtonTitle, | |
332 | responseFlags); | |
55e303ae A |
333 | UNDServer_deallocate(UNDServer); |
334 | return kr; | |
335 | } | |
336 | return MACH_SEND_INVALID_DEST; | |
1c79356b A |
337 | } |
338 | ||
339 | kern_return_t | |
340 | KUNCUserNotificationDisplayFromBundle( | |
0a7de745 A |
341 | KUNCUserNotificationID id, |
342 | char *bundlePath, | |
343 | char *fileName, | |
344 | char *fileExtension, | |
345 | char *messageKey, | |
346 | char *tokenString, | |
1c79356b | 347 | KUNCUserNotificationCallBack callback, |
0a7de745 | 348 | __unused int contextKey) |
1c79356b A |
349 | { |
350 | UNDReplyRef reply = (UNDReplyRef)id; | |
55e303ae | 351 | UNDServerRef UNDServer; |
1c79356b | 352 | ipc_port_t reply_port; |
1c79356b | 353 | |
0a7de745 | 354 | if (reply == UND_REPLY_NULL) { |
1c79356b | 355 | return KERN_INVALID_ARGUMENT; |
0a7de745 | 356 | } |
1c79356b A |
357 | UNDReply_lock(reply); |
358 | if (reply->inprogress == TRUE || reply->userLandNotificationKey != -1) { | |
359 | UNDReply_unlock(reply); | |
360 | return KERN_INVALID_ARGUMENT; | |
361 | } | |
91447636 | 362 | reply->inprogress = TRUE; |
1c79356b A |
363 | reply->callback = callback; |
364 | reply_port = ipc_port_make_send(reply->self_port); | |
365 | UNDReply_unlock(reply); | |
366 | ||
55e303ae A |
367 | UNDServer = UNDServer_reference(); |
368 | if (IP_VALID(UNDServer)) { | |
369 | kern_return_t kr; | |
370 | ||
371 | kr = UNDDisplayCustomFromBundle_rpc(UNDServer, | |
0a7de745 A |
372 | reply_port, |
373 | bundlePath, | |
374 | fileName, | |
375 | fileExtension, | |
376 | messageKey, | |
377 | tokenString); | |
55e303ae A |
378 | UNDServer_deallocate(UNDServer); |
379 | return kr; | |
380 | } | |
381 | return MACH_SEND_INVALID_DEST; | |
1c79356b A |
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 | */ | |
393 | UNDReplyRef | |
394 | convert_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); | |
55e303ae | 408 | return reply; |
1c79356b A |
409 | } |
410 | return UND_REPLY_NULL; | |
411 | } | |
412 | ||
413 | /* | |
414 | * User interface for setting the host UserNotification Daemon port. | |
415 | */ | |
416 | ||
417 | kern_return_t | |
418 | host_set_UNDServer( | |
0a7de745 A |
419 | host_priv_t host_priv, |
420 | UNDServerRef server) | |
1c79356b | 421 | { |
0a7de745 | 422 | return host_set_user_notification_port(host_priv, server); |
1c79356b A |
423 | } |
424 | ||
425 | /* | |
426 | * User interface for retrieving the UserNotification Daemon port. | |
427 | */ | |
428 | ||
429 | kern_return_t | |
430 | host_get_UNDServer( | |
431 | host_priv_t host_priv, | |
0a7de745 | 432 | UNDServerRef *serverp) |
1c79356b | 433 | { |
0a7de745 | 434 | return host_get_user_notification_port(host_priv, serverp); |
1c79356b | 435 | } |