]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
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. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
0a7de745 | 30 | * |
1c79356b A |
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> | |
3e170ce0 | 40 | #include <mach/mach_types.h> |
1c79356b A |
41 | #include <mach/semaphore.h> |
42 | #include <mach/lock_set_server.h> | |
43 | #include <mach/mach_port_server.h> | |
44 | #include <mach/port.h> | |
45 | ||
46 | ||
3e170ce0 A |
47 | /* |
48 | * Routine: port_name_to_semaphore | |
49 | * Purpose: | |
50 | * Convert from a port name in the current space to a semaphore. | |
51 | * Produces a semaphore ref, which may be null. | |
52 | * Conditions: | |
53 | * Nothing locked. | |
54 | */ | |
1c79356b A |
55 | kern_return_t |
56 | port_name_to_semaphore( | |
0a7de745 A |
57 | mach_port_name_t name, |
58 | semaphore_t *semaphorep) | |
1c79356b | 59 | { |
1c79356b A |
60 | ipc_port_t kern_port; |
61 | kern_return_t kr; | |
62 | ||
63 | if (!MACH_PORT_VALID(name)) { | |
64 | *semaphorep = SEMAPHORE_NULL; | |
65 | return KERN_INVALID_NAME; | |
66 | } | |
3e170ce0 | 67 | |
cb323159 | 68 | kr = ipc_port_translate_send(current_space(), name, &kern_port); |
1c79356b A |
69 | if (kr != KERN_SUCCESS) { |
70 | *semaphorep = SEMAPHORE_NULL; | |
71 | return kr; | |
72 | } | |
73 | /* have the port locked */ | |
74 | assert(IP_VALID(kern_port)); | |
75 | ||
3e170ce0 | 76 | *semaphorep = convert_port_to_semaphore(kern_port); |
490019cf A |
77 | if (*semaphorep == SEMAPHORE_NULL) { |
78 | /* the port is valid, but doesn't denote a semaphore */ | |
79 | kr = KERN_INVALID_CAPABILITY; | |
80 | } else { | |
81 | kr = KERN_SUCCESS; | |
82 | } | |
1c79356b A |
83 | ip_unlock(kern_port); |
84 | ||
490019cf | 85 | return kr; |
1c79356b | 86 | } |
3e170ce0 A |
87 | |
88 | /* | |
89 | * Routine: convert_port_to_semaphore | |
90 | * Purpose: | |
91 | * Convert from a port to a semaphore. | |
92 | * Doesn't consume the port [send-right] ref; | |
93 | * produces a semaphore ref, which may be null. | |
94 | * Conditions: | |
95 | * Caller has a send-right reference to port. | |
96 | * Port may or may not be locked. | |
97 | */ | |
1c79356b | 98 | semaphore_t |
0a7de745 | 99 | convert_port_to_semaphore(ipc_port_t port) |
1c79356b | 100 | { |
3e170ce0 A |
101 | if (IP_VALID(port)) { |
102 | semaphore_t semaphore; | |
103 | ||
104 | /* | |
105 | * No need to lock because we have a reference on the | |
106 | * port, and if it is a true semaphore port, that reference | |
107 | * keeps the semaphore bound to the port (and active). | |
108 | */ | |
109 | if (ip_kotype(port) == IKOT_SEMAPHORE) { | |
cb323159 | 110 | require_ip_active(port); |
ea3f0419 | 111 | semaphore = (semaphore_t) ip_get_kobject(port); |
1c79356b | 112 | semaphore_reference(semaphore); |
0a7de745 | 113 | return semaphore; |
1c79356b | 114 | } |
1c79356b | 115 | } |
3e170ce0 | 116 | return SEMAPHORE_NULL; |
1c79356b A |
117 | } |
118 | ||
119 | ||
3e170ce0 A |
120 | /* |
121 | * Routine: convert_semaphore_to_port | |
122 | * Purpose: | |
123 | * Convert a semaphore reference to a send right to a | |
124 | * semaphore port. | |
125 | * | |
126 | * Consumes the semaphore reference. If the semaphore | |
127 | * port currently has no send rights (or doesn't exist | |
128 | * yet), the reference is donated to the port to represent | |
129 | * all extant send rights collectively. | |
130 | */ | |
1c79356b | 131 | ipc_port_t |
0a7de745 | 132 | convert_semaphore_to_port(semaphore_t semaphore) |
1c79356b | 133 | { |
0a7de745 A |
134 | if (semaphore == SEMAPHORE_NULL) { |
135 | return IP_NULL; | |
136 | } | |
1c79356b | 137 | |
cb323159 A |
138 | /* |
139 | * make a send right and donate our reference for | |
140 | * semaphore_notify if this is the first send right | |
141 | */ | |
142 | if (!ipc_kobject_make_send_lazy_alloc_port(&semaphore->port, | |
c3c9b80d | 143 | (ipc_kobject_t) semaphore, IKOT_SEMAPHORE, IPC_KOBJECT_ALLOC_NONE, false, 0)) { |
3e170ce0 A |
144 | semaphore_dereference(semaphore); |
145 | } | |
cb323159 | 146 | return semaphore->port; |
3e170ce0 A |
147 | } |
148 | ||
149 | /* | |
150 | * Routine: semaphore_notify | |
151 | * Purpose: | |
152 | * Called whenever the Mach port system detects no-senders | |
153 | * on the semaphore port. | |
154 | * | |
155 | * When a send-right is first created, a no-senders | |
156 | * notification is armed (and a semaphore reference is donated). | |
157 | * | |
158 | * A no-senders notification will be posted when no one else holds a | |
159 | * send-right (reference) to the semaphore's port. This notification function | |
160 | * will consume the semaphore reference donated to the extant collection of | |
161 | * send-rights. | |
162 | */ | |
163 | void | |
164 | semaphore_notify(mach_msg_header_t *msg) | |
165 | { | |
166 | mach_no_senders_notification_t *notification = (void *)msg; | |
167 | ipc_port_t port = notification->not_header.msgh_remote_port; | |
3e170ce0 | 168 | |
cb323159 | 169 | require_ip_active(port); |
3e170ce0 | 170 | assert(IKOT_SEMAPHORE == ip_kotype(port)); |
3e170ce0 | 171 | |
ea3f0419 | 172 | semaphore_dereference((semaphore_t) ip_get_kobject(port)); |
1c79356b A |
173 | } |
174 | ||
175 | lock_set_t | |
0a7de745 | 176 | convert_port_to_lock_set(__unused ipc_port_t port) |
1c79356b | 177 | { |
0a7de745 | 178 | return LOCK_SET_NULL; |
1c79356b A |
179 | } |
180 | ||
181 | ipc_port_t | |
0a7de745 | 182 | convert_lock_set_to_port(__unused lock_set_t lock_set) |
1c79356b | 183 | { |
0a7de745 | 184 | return IP_NULL; |
1c79356b | 185 | } |