Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | /* * Copyright (c) 2003-2020 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ #include <mach/mach_types.h> #include <mach/mach_host.h> #include <ipc/ipc_policy.h> #include <kern/kern_types.h> #include <kern/ipc_kobject.h> #include <kern/host_notify.h> #include <kern/queue.h> #include "mach/host_notify_reply.h" struct host_notify_entry { queue_chain_t entries; ipc_port_t port; ipc_port_request_index_t index; }; LCK_GRP_DECLARE(host_notify_lock_grp, "host_notify"); LCK_MTX_DECLARE(host_notify_lock, &host_notify_lock_grp); static KALLOC_TYPE_DEFINE(host_notify_zone, struct host_notify_entry, KT_DEFAULT); static queue_head_t host_notify_queue[HOST_NOTIFY_TYPE_MAX + 1] = { QUEUE_HEAD_INITIALIZER(host_notify_queue[HOST_NOTIFY_CALENDAR_CHANGE]), QUEUE_HEAD_INITIALIZER(host_notify_queue[HOST_NOTIFY_CALENDAR_SET]), }; static mach_msg_id_t host_notify_replyid[HOST_NOTIFY_TYPE_MAX + 1] = { HOST_CALENDAR_CHANGED_REPLYID, HOST_CALENDAR_SET_REPLYID, }; kern_return_t host_request_notification( host_t host, host_flavor_t notify_type, ipc_port_t port) { host_notify_t entry; kern_return_t kr; if (host == HOST_NULL) { return KERN_INVALID_ARGUMENT; } if (!IP_VALID(port)) { return KERN_INVALID_CAPABILITY; } if (notify_type > HOST_NOTIFY_TYPE_MAX || notify_type < 0) { return KERN_INVALID_ARGUMENT; } entry = zalloc_flags(host_notify_zone, Z_WAITOK | Z_ZERO | Z_NOFAIL); entry->port = port; again: lck_mtx_lock(&host_notify_lock); ip_mq_lock(port); if (ip_active(port)) { kr = ipc_port_request_hnotify_alloc(port, entry, &entry->index); } else { kr = KERN_INVALID_CAPABILITY; } if (kr == KERN_SUCCESS) { /* * Preserve original ABI of host-notify ports being immovable * as a side effect of being a kobject. */ if (!ip_is_immovable_receive(port)) { ipc_object_label_t label = ip_label_get(port); ipc_release_assert(label.io_state == IO_STATE_IN_SPACE); label.io_state = IO_STATE_IN_SPACE_IMMOVABLE; io_label_set_and_put(&port->ip_object, &label); } enqueue_tail(&host_notify_queue[notify_type], &entry->entries); } lck_mtx_unlock(&host_notify_lock); if (kr == KERN_NO_SPACE) { kr = ipc_port_request_grow(port); /* port unlocked */ if (kr == KERN_SUCCESS) { goto again; } } else { ip_mq_unlock(port); } if (kr != KERN_SUCCESS) { zfree(host_notify_zone, entry); } return kr; } void host_notify_cancel(host_notify_t entry) { ipc_port_t port; lck_mtx_lock(&host_notify_lock); remqueue((queue_entry_t)entry); port = entry->port; lck_mtx_unlock(&host_notify_lock); zfree(host_notify_zone, entry); ipc_port_release_sonce(port); } static void host_notify_all( host_flavor_t notify_type, mach_msg_header_t *msg, mach_msg_size_t msg_size) { queue_head_t send_queue = QUEUE_HEAD_INITIALIZER(send_queue); queue_entry_t e; host_notify_t entry; ipc_port_t port; lck_mtx_lock(&host_notify_lock); qe_foreach_safe(e, &host_notify_queue[notify_type]) { entry = (host_notify_t)e; port = entry->port; ip_mq_lock(port); if (ip_active(port)) { ipc_port_request_cancel(port, IPR_HOST_NOTIFY, entry->index); remqueue(e); enqueue_tail(&send_queue, e); } else { /* * leave the entry in place, * we're racing with ipc_port_dnnotify() * which will call host_notify_cancel(). */ } ip_mq_unlock(port); } lck_mtx_unlock(&host_notify_lock); if (queue_empty(&send_queue)) { return; } msg->msgh_bits = MACH_MSGH_BITS_SET(MACH_MSG_TYPE_MOVE_SEND_ONCE, 0, 0, 0); msg->msgh_local_port = MACH_PORT_NULL; msg->msgh_voucher_port = MACH_PORT_NULL; msg->msgh_id = host_notify_replyid[notify_type]; qe_foreach_safe(e, &send_queue) { entry = (host_notify_t)e; port = entry->port; zfree(host_notify_zone, entry); msg->msgh_remote_port = port; (void)mach_msg_send_from_kernel(msg, msg_size); } } void host_notify_calendar_change(void) { __Request__host_calendar_changed_t msg; host_notify_all(HOST_NOTIFY_CALENDAR_CHANGE, &msg.Head, sizeof(msg)); } void host_notify_calendar_set(void) { __Request__host_calendar_set_t msg; host_notify_all(HOST_NOTIFY_CALENDAR_SET, &msg.Head, sizeof(msg)); } |