]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ipc_misc.c
xnu-4903.270.47.tar.gz
[apple/xnu.git] / osfmk / kern / ipc_misc.c
1 /*
2 * Copyright (c) 2008, 2010 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 #include <mach/mach_types.h>
29 #include <mach/notify.h>
30 #include <ipc/ipc_port.h>
31 #include <kern/ipc_kobject.h>
32 #include <kern/ipc_misc.h>
33
34 #include <mach/mach_port.h>
35 #include <mach/vm_map.h>
36 #include <vm/vm_map.h>
37 #include <vm/vm_kern.h>
38
39 extern void fileport_releasefg(struct fileglob *);
40
41 /*
42 * fileport_alloc
43 *
44 * Description: Obtain a send right for the given fileglob, which must be
45 * referenced.
46 *
47 * Parameters: fg A fileglob.
48 *
49 * Returns: Port of type IKOT_FILEPORT with fileglob set as its kobject.
50 * Port is returned with a send right.
51 */
52 ipc_port_t
53 fileport_alloc(struct fileglob *fg)
54 {
55 ipc_port_t fileport;
56 ipc_port_t sendport;
57 ipc_port_t notifyport;
58
59 fileport = ipc_port_alloc_kernel();
60 if (fileport == IP_NULL) {
61 goto out;
62 }
63
64 ipc_kobject_set(fileport, (ipc_kobject_t)fg, IKOT_FILEPORT);
65 ip_lock(fileport); /* unlocked by ipc_port_nsrequest */
66 notifyport = ipc_port_make_sonce_locked(fileport);
67 ipc_port_nsrequest(fileport, 1, notifyport, &notifyport);
68
69 sendport = ipc_port_make_send(fileport);
70 if (!IP_VALID(sendport)) {
71 panic("Couldn't allocate send right for fileport!\n");
72 }
73
74 out:
75 return fileport;
76 }
77
78
79 /*
80 * fileport_get_fileglob
81 *
82 * Description: Obtain the fileglob associated with a given port.
83 *
84 * Parameters: port A Mach port of type IKOT_FILEPORT.
85 *
86 * Returns: NULL The given Mach port did not reference a
87 * fileglob.
88 * !NULL The fileglob that is associated with the
89 * Mach port.
90 *
91 * Notes: The caller must have a reference on the fileport.
92 */
93 struct fileglob *
94 fileport_port_to_fileglob(ipc_port_t port)
95 {
96 struct fileglob *fg = NULL;
97
98 if (!IP_VALID(port)) {
99 return NULL;
100 }
101
102 ip_lock(port);
103 if (ip_active(port) && IKOT_FILEPORT == ip_kotype(port)) {
104 fg = (void *)port->ip_kobject;
105 }
106 ip_unlock(port);
107
108 return fg;
109 }
110
111
112 /*
113 * fileport_notify
114 *
115 * Description: Handle a no-senders notification for a fileport. Unless
116 * the message is spoofed, destroys the port and releases
117 * its reference on the fileglob.
118 *
119 * Parameters: msg A Mach no-senders notification message.
120 */
121 void
122 fileport_notify(mach_msg_header_t *msg)
123 {
124 mach_no_senders_notification_t *notification = (void *)msg;
125 ipc_port_t port = notification->not_header.msgh_remote_port;
126 struct fileglob *fg = NULL;
127
128 if (!IP_VALID(port)) {
129 panic("Invalid port passed to fileport_notify()\n");
130 }
131
132 ip_lock(port);
133
134 fg = (struct fileglob *)port->ip_kobject;
135
136 if (!ip_active(port)) {
137 panic("Inactive port passed to fileport_notify()\n");
138 }
139 if (ip_kotype(port) != IKOT_FILEPORT) {
140 panic("Port of type other than IKOT_FILEPORT passed to fileport_notify()\n");
141 }
142 if (fg == NULL) {
143 panic("fileport without an assocated fileglob\n");
144 }
145
146 if (port->ip_srights == 0) {
147 ip_unlock(port);
148
149 fileport_releasefg(fg);
150 ipc_port_dealloc_kernel(port);
151 } else {
152 ip_unlock(port);
153 }
154 }
155
156 /*
157 * fileport_invoke
158 *
159 * Description: Invoke a function with the fileglob underlying the fileport.
160 * Returns the error code related to the fileglob lookup.
161 *
162 * Parameters: task The target task
163 * action The function to invoke with the fileglob
164 * arg Anonymous pointer to caller state
165 * rval The value returned from calling 'action'
166 */
167 kern_return_t
168 fileport_invoke(task_t task, mach_port_name_t name,
169 int (*action)(mach_port_name_t, struct fileglob *, void *),
170 void *arg, int *rval)
171 {
172 kern_return_t kr;
173 ipc_port_t fileport;
174 struct fileglob *fg;
175
176 kr = ipc_object_copyin(task->itk_space, name,
177 MACH_MSG_TYPE_COPY_SEND, (ipc_object_t *)&fileport);
178 if (kr != KERN_SUCCESS) {
179 return kr;
180 }
181
182 if ((fg = fileport_port_to_fileglob(fileport)) != NULL) {
183 *rval = (*action)(name, fg, arg);
184 } else {
185 kr = KERN_FAILURE;
186 }
187 ipc_port_release_send(fileport);
188 return kr;
189 }
190
191 /*
192 * fileport_walk
193 *
194 * Description: Invoke the action function on every fileport in the task.
195 *
196 * This could be more efficient if we refactored mach_port_names()
197 * so that (a) it didn't compute the type information unless asked
198 * and (b) it could be asked to -not- unwire/copyout the memory
199 * and (c) if we could ask for port names by kobject type. Not
200 * clear that it's worth all that complexity, though.
201 *
202 * Parameters: task The target task
203 * action The function to invoke on each fileport
204 * arg Anonymous pointer to caller state.
205 */
206 kern_return_t
207 fileport_walk(task_t task,
208 int (*action)(mach_port_name_t, struct fileglob *, void *arg),
209 void *arg)
210 {
211 mach_port_name_t *names;
212 mach_msg_type_number_t ncnt, tcnt;
213 vm_map_copy_t map_copy_names, map_copy_types;
214 vm_map_address_t map_names;
215 kern_return_t kr;
216 uint_t i;
217 int rval;
218
219 /*
220 * mach_port_names returns the 'name' and 'types' in copied-in
221 * form. Discard 'types' immediately, then copyout 'names'
222 * back into the kernel before walking the array.
223 */
224
225 kr = mach_port_names(task->itk_space,
226 (mach_port_name_t **)&map_copy_names, &ncnt,
227 (mach_port_type_t **)&map_copy_types, &tcnt);
228 if (kr != KERN_SUCCESS) {
229 return kr;
230 }
231
232 vm_map_copy_discard(map_copy_types);
233
234 kr = vm_map_copyout(ipc_kernel_map, &map_names, map_copy_names);
235 if (kr != KERN_SUCCESS) {
236 vm_map_copy_discard(map_copy_names);
237 return kr;
238 }
239 names = (mach_port_name_t *)(uintptr_t)map_names;
240
241 for (rval = 0, i = 0; i < ncnt; i++) {
242 if (fileport_invoke(task, names[i], action, arg,
243 &rval) == KERN_SUCCESS && -1 == rval) {
244 break; /* early termination clause */
245 }
246 }
247 vm_deallocate(ipc_kernel_map,
248 (vm_address_t)names, ncnt * sizeof(*names));
249 return KERN_SUCCESS;
250 }