]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/ipc_sync.c
xnu-1699.24.8.tar.gz
[apple/xnu.git] / osfmk / kern / ipc_sync.c
1 /*
2 * Copyright (c) 2000 Apple Computer, 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 /*
29 * @OSF_COPYRIGHT@
30 *
31 */
32
33 #include <kern/sync_sema.h>
34 #include <kern/sync_lock.h>
35 #include <kern/ipc_kobject.h>
36 #include <kern/ipc_sync.h>
37 #include <ipc/port.h>
38 #include <ipc/ipc_space.h>
39 #include <ipc/ipc_port.h>
40 #include <mach/semaphore.h>
41 #include <mach/lock_set_server.h>
42 #include <mach/mach_port_server.h>
43 #include <mach/port.h>
44
45
46 kern_return_t
47 port_name_to_semaphore(
48 mach_port_name_t name,
49 semaphore_t *semaphorep)
50 {
51 semaphore_t semaphore;
52 ipc_port_t kern_port;
53 kern_return_t kr;
54
55 if (!MACH_PORT_VALID(name)) {
56 *semaphorep = SEMAPHORE_NULL;
57 return KERN_INVALID_NAME;
58 }
59
60 kr = ipc_object_translate(current_space(), name, MACH_PORT_RIGHT_SEND,
61 (ipc_object_t *) &kern_port);
62 if (kr != KERN_SUCCESS) {
63 *semaphorep = SEMAPHORE_NULL;
64 return kr;
65 }
66 /* have the port locked */
67 assert(IP_VALID(kern_port));
68
69 if (!ip_active(kern_port) || (ip_kotype(kern_port) != IKOT_SEMAPHORE)) {
70 ip_unlock(kern_port);
71 *semaphorep = SEMAPHORE_NULL;
72 return KERN_INVALID_ARGUMENT;
73 }
74
75 semaphore = (semaphore_t) kern_port->ip_kobject;
76 assert(semaphore != SEMAPHORE_NULL);
77 semaphore_reference(semaphore);
78 ip_unlock(kern_port);
79
80 *semaphorep = semaphore;
81 return KERN_SUCCESS;
82 }
83
84 semaphore_t
85 convert_port_to_semaphore (ipc_port_t port)
86 {
87 semaphore_t semaphore = SEMAPHORE_NULL;
88
89 if (IP_VALID (port)) {
90 ip_lock(port);
91 if (ip_active(port) && (ip_kotype(port) == IKOT_SEMAPHORE)) {
92 semaphore = (semaphore_t) port->ip_kobject;
93 semaphore_reference(semaphore);
94 }
95 ip_unlock(port);
96 }
97
98 return (semaphore);
99 }
100
101
102 ipc_port_t
103 convert_semaphore_to_port (semaphore_t semaphore)
104 {
105 ipc_port_t port;
106
107 if (semaphore == SEMAPHORE_NULL)
108 return (IP_NULL);
109
110 /* caller is donating a reference */
111 port = ipc_port_make_send(semaphore->port);
112 semaphore_dereference(semaphore);
113 return (port);
114 }
115
116 lock_set_t
117 convert_port_to_lock_set (ipc_port_t port)
118 {
119 lock_set_t lock_set = LOCK_SET_NULL;
120
121 if (IP_VALID (port)) {
122 ip_lock(port);
123 if (ip_active(port) && (ip_kotype(port) == IKOT_LOCK_SET)) {
124 lock_set = (lock_set_t) port->ip_kobject;
125 lock_set_reference(lock_set);
126 }
127 ip_unlock(port);
128 }
129
130 return (lock_set);
131 }
132
133 ipc_port_t
134 convert_lock_set_to_port (lock_set_t lock_set)
135 {
136 ipc_port_t port;
137
138 if (lock_set == LOCK_SET_NULL)
139 return IP_NULL;
140
141 /* caller is donating a reference */
142 port = ipc_port_make_send(lock_set->port);
143 lock_set_dereference(lock_set);
144 return (port);
145 }
146