]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ipc_misc.c
xnu-1504.7.4.tar.gz
[apple/xnu.git] / osfmk / kern / ipc_misc.c
1 /*
2 * Copyright (c) 2008 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 extern void fileport_releasefg(struct fileglob *);
35
36 /*
37 * fileport_alloc
38 *
39 * Description: Obtain a send right for the given fileglob, which must be
40 * referenced.
41 *
42 * Parameters: fg A fileglob.
43 *
44 * Returns: Port of type IKOT_FILEPORT with fileglob set as its kobject.
45 * Port is returned with a send right.
46 */
47 ipc_port_t
48 fileport_alloc(struct fileglob *fg)
49 {
50 ipc_port_t fileport;
51 ipc_port_t sendport;
52 ipc_port_t notifyport;
53
54 fileport = ipc_port_alloc_kernel();
55 if (fileport == IP_NULL) {
56 goto out;
57 }
58
59 ipc_kobject_set(fileport, (ipc_kobject_t)fg, IKOT_FILEPORT);
60 notifyport = ipc_port_make_sonce(fileport);
61 ip_lock(fileport); /* unlocked by ipc_port_nsrequest */
62 ipc_port_nsrequest(fileport, 1, notifyport, &notifyport);
63
64 sendport = ipc_port_make_send(fileport);
65 if (!IP_VALID(sendport)) {
66 panic("Couldn't allocate send right for fileport!\n");
67 }
68
69 out:
70 return fileport;
71 }
72
73
74 /*
75 * fileport_get_fileglob
76 *
77 * Description: Obtain the fileglob associated with a given port.
78 *
79 * Parameters: port A Mach port of type IKOT_FILEPORT.
80 *
81 * Returns: NULL The given Mach port did not reference a
82 * fileglob.
83 * !NULL The fileglob that is associated with the
84 * Mach port.
85 *
86 * Notes: The caller must have a reference on the fileport.
87 */
88 struct fileglob *
89 fileport_port_to_fileglob(ipc_port_t port)
90 {
91 struct fileglob *fg = NULL;
92
93 if (!IP_VALID(port))
94 return NULL;
95
96 ip_lock(port);
97 if (ip_active(port) && IKOT_FILEPORT == ip_kotype(port))
98 fg = (void *)port->ip_kobject;
99 ip_unlock(port);
100
101 return fg;
102 }
103
104
105 /*
106 * fileport_notify
107 *
108 * Description: Handle a no-senders notification for a fileport. Unless
109 * the message is spoofed, destroys the port and releases
110 * its reference on the fileglob.
111 *
112 * Parameters: msg A Mach no-senders notification message.
113 */
114 void
115 fileport_notify(mach_msg_header_t *msg)
116 {
117 mach_no_senders_notification_t *notification = (void *)msg;
118 ipc_port_t port = notification->not_header.msgh_remote_port;
119 struct fileglob *fg = NULL;
120
121 if (!IP_VALID(port))
122 panic("Invalid port passed to fileport_notify()\n");
123
124 ip_lock(port);
125
126 fg = (struct fileglob *)port->ip_kobject;
127
128 if (!ip_active(port))
129 panic("Inactive port passed to fileport_notify()\n");
130 if (ip_kotype(port) != IKOT_FILEPORT)
131 panic("Port of type other than IKOT_FILEPORT passed to fileport_notify()\n");
132 if (fg == NULL)
133 panic("fileport without an assocated fileglob\n");
134
135 if (port->ip_srights == 0) {
136 ip_unlock(port);
137
138 fileport_releasefg(fg);
139 ipc_port_dealloc_kernel(port);
140 } else {
141 ip_unlock(port);
142 }
143
144 return;
145 }