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 217 218 219 220 221 222 223 224 225 226 227 228 229 | /* * Copyright (c) 2008, 2010 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/notify.h> #include <ipc/ipc_port.h> #include <ipc/ipc_space.h> #include <kern/ipc_kobject.h> #include <kern/ipc_misc.h> #include <mach/mach_port.h> #include <mach/vm_map.h> #include <vm/vm_map.h> #include <vm/vm_kern.h> extern void fileport_releasefg(struct fileglob *); /* * fileport_alloc * * Description: Obtain a send right for the given fileglob, which must be * referenced. * * Parameters: fg A fileglob. * * Returns: Port of type IKOT_FILEPORT with fileglob set as its kobject. * Port is returned with a send right. */ ipc_port_t fileport_alloc(struct fileglob *fg) { return ipc_kobject_alloc_port(fg, IKOT_FILEPORT, IPC_KOBJECT_ALLOC_MAKE_SEND); } /* * fileport_get_fileglob * * Description: Obtain the fileglob associated with a given port. * * Parameters: port A Mach port of type IKOT_FILEPORT. * * Returns: NULL The given Mach port did not reference a * fileglob. * !NULL The fileglob that is associated with the * Mach port. * * Notes: The caller must have a reference on the fileport. */ struct fileglob * fileport_port_to_fileglob(ipc_port_t port) { if (IP_VALID(port)) { return ipc_kobject_get_stable(port, IKOT_FILEPORT); } return NULL; } /* * fileport_no_senders * * Description: Handle a no-senders notification for a fileport. Unless * the message is spoofed, destroys the port and releases * its reference on the fileglob. * * Parameters: msg A Mach no-senders notification message. */ static void fileport_no_senders(ipc_port_t port, mach_port_mscount_t mscount) { struct fileglob *fg; fg = ipc_kobject_dealloc_port(port, mscount, IKOT_FILEPORT); fileport_releasefg(fg); } IPC_KOBJECT_DEFINE(IKOT_FILEPORT, .iko_op_movable_send = true, .iko_op_stable = true, .iko_op_no_senders = fileport_no_senders); /* * fileport_invoke * * Description: Invoke a function with the fileglob underlying the fileport. * Returns the error code related to the fileglob lookup. * * Parameters: task The target task * action The function to invoke with the fileglob * arg Anonymous pointer to caller state * rval The value returned from calling 'action' */ kern_return_t fileport_invoke(task_t task, mach_port_name_t name, int (*action)(mach_port_name_t, struct fileglob *, void *), void *arg, int *rval) { kern_return_t kr; ipc_port_t fileport; struct fileglob *fg; kr = ipc_typed_port_copyin_send(task->itk_space, name, IKOT_FILEPORT, &fileport); if (kr != KERN_SUCCESS) { return kr; } if ((fg = fileport_port_to_fileglob(fileport)) != NULL) { *rval = (*action)(name, fg, arg); } else { kr = KERN_FAILURE; } ipc_typed_port_release_send(fileport, IKOT_FILEPORT); return kr; } /* * fileport_walk * * Description: Invoke the action function on every fileport in the task. * * Parameters: task The target task * countp Returns how many ports were found * action The function to invoke on each fileport */ kern_return_t fileport_walk(task_t task, size_t *countp, bool (^cb)(size_t i, mach_port_name_t, struct fileglob *)) { const uint32_t BATCH_SIZE = 4 << 10; ipc_space_t space = task->itk_space; ipc_entry_table_t table; ipc_entry_num_t index; ipc_entry_t entry; size_t count = 0; is_read_lock(space); if (!is_active(space)) { is_read_unlock(space); return KERN_INVALID_TASK; } table = is_active_table(space); entry = ipc_entry_table_base(table); /* skip the first element which is not a real entry */ index = 1; entry = ipc_entry_table_next_elem(table, entry); for (;;) { ipc_entry_bits_t bits = entry->ie_bits; mach_port_name_t name; struct fileglob *fg; if (IE_BITS_TYPE(bits) & MACH_PORT_TYPE_SEND) { ipc_port_t port = entry->ie_port; name = MACH_PORT_MAKE(index, IE_BITS_GEN(bits)); fg = fileport_port_to_fileglob(port); if (fg) { if (cb && !cb(count, name, fg)) { cb = NULL; if (countp == NULL) { break; } } count++; } } index++; entry = ipc_entry_table_next_elem(table, entry); if (!entry) { break; } if (index % BATCH_SIZE == 0) { /* * Give the system some breathing room, * validate that the space is still valid, * and reload the pointer and length. */ is_read_unlock(space); is_read_lock(space); if (!is_active(space)) { is_read_unlock(space); return KERN_INVALID_TASK; } table = is_active_table(space); entry = ipc_entry_table_get_nocheck(table, index); } } is_read_unlock(space); if (countp) { *countp = count; } return KERN_SUCCESS; } |