[BZ #406]
[glibc.git] / mach / msg-destroy.c
blob9ecbdc051209701e9dee1d68ab9dce75c69b19ac
1 /*
2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
4 * All Rights Reserved.
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie Mellon
24 * the rights to redistribute these changes.
27 * HISTORY
28 * $Log$
29 * Revision 1.6 2002/02/17 07:13:36 roland
30 * 2002-02-16 Roland McGrath <roland@frob.com>
32 * * mach/msg-destroy.c (__mach_msg_destroy) [MACH_MSG_PORT_DESCRIPTOR]:
33 * Grok the OSF flavor of message format.
34 * (mach_msg_destroy_port): For MAKE_SEND and
35 * MAKE_SEND_ONCE rights, create an destroy a right to ensure proper
36 * no-senders notification.
38 * Revision 1.5 1997/06/21 01:40:07 drepper
39 * More 64bit changes.
41 * Revision 1.4 1996/11/15 19:44:43 thomas
42 * Tue Nov 12 16:58:41 1996 Thomas Bushnell, n/BSG <thomas@gnu.ai.mit.edu>
44 * * mach/mach.h (__mach_msg_destroy, mach_msg_destroy, __mach_msg):
45 * Provide prototypes.
47 * * mach/msg-destroy.c (mach_msg_destroy_port,
48 * mach_msg_destroy_memory): Use prototype syntax.
49 * * hurd/hurdmalloc.c (more_memory, malloc_fork_prepare,
50 * malloc_fork_parent, malloc_fork_child): Likewise.
52 * Revision 1.3 1995/01/23 22:16:52 roland
53 * (mach_msg_destroy): Define as weak alias for __mach_msg_destroy.
55 * Revision 1.2 1993/08/03 06:13:18 roland
56 * entered into RCS
58 * Revision 2.4 91/05/14 17:53:15 mrt
59 * Correcting copyright
61 * Revision 2.3 91/02/14 14:17:43 mrt
62 * Added new Mach copyright
63 * [91/02/13 12:44:15 mrt]
65 * Revision 2.2 90/08/06 17:24:22 rpd
66 * Created.
70 #if 1
71 #include <mach.h>
72 #else
73 /* This is what CMU did, but that fails to declare some used functions. */
74 #include <mach/port.h>
75 #include <mach/message.h>
76 #include <mach_init.h>
77 #endif
79 static void mach_msg_destroy_port(mach_port_t, mach_msg_type_name_t);
80 static void mach_msg_destroy_memory(vm_offset_t, vm_size_t);
83 * Routine: mach_msg_destroy
84 * Purpose:
85 * Deallocates all port rights and out-of-line memory
86 * found in a received message.
89 void
90 __mach_msg_destroy(msg)
91 mach_msg_header_t *msg;
93 mach_msg_bits_t mbits = msg->msgh_bits;
96 * The msgh_local_port field doesn't hold a port right.
97 * The receive operation consumes the destination port right.
100 mach_msg_destroy_port(msg->msgh_remote_port, MACH_MSGH_BITS_REMOTE(mbits));
102 if (mbits & MACH_MSGH_BITS_COMPLEX) {
103 #ifdef MACH_MSG_PORT_DESCRIPTOR
104 mach_msg_body_t *body;
105 mach_msg_descriptor_t *saddr, *eaddr;
107 body = (mach_msg_body_t *) (msg + 1);
108 saddr = (mach_msg_descriptor_t *)
109 ((mach_msg_base_t *) msg + 1);
110 eaddr = saddr + body->msgh_descriptor_count;
112 for ( ; saddr < eaddr; saddr++) {
113 switch (saddr->type.type) {
115 case MACH_MSG_PORT_DESCRIPTOR: {
116 mach_msg_port_descriptor_t *dsc;
119 * Destroy port rights carried in the message
121 dsc = &saddr->port;
122 mach_msg_destroy_port(dsc->name, dsc->disposition);
123 break;
126 case MACH_MSG_OOL_DESCRIPTOR : {
127 mach_msg_ool_descriptor_t *dsc;
130 * Destroy memory carried in the message
132 dsc = &saddr->out_of_line;
133 if (dsc->deallocate) {
134 mach_msg_destroy_memory((vm_offset_t)dsc->address,
135 dsc->size);
137 break;
140 case MACH_MSG_OOL_PORTS_DESCRIPTOR : {
141 mach_port_t *ports;
142 mach_msg_ool_ports_descriptor_t *dsc;
143 mach_msg_type_number_t j;
146 * Destroy port rights carried in the message
148 dsc = &saddr->ool_ports;
149 ports = (mach_port_t *) dsc->address;
150 for (j = 0; j < dsc->count; j++, ports++) {
151 mach_msg_destroy_port(*ports, dsc->disposition);
155 * Destroy memory carried in the message
157 if (dsc->deallocate) {
158 mach_msg_destroy_memory((vm_offset_t)dsc->address,
159 dsc->count * sizeof(mach_port_t));
161 break;
165 #else
166 vm_offset_t saddr;
167 vm_offset_t eaddr;
169 saddr = (vm_offset_t) (msg + 1);
170 eaddr = (vm_offset_t) msg + msg->msgh_size;
172 while (saddr < eaddr) {
173 mach_msg_type_long_t *type;
174 mach_msg_type_name_t name;
175 mach_msg_type_size_t size;
176 mach_msg_type_number_t number;
177 boolean_t is_inline;
178 vm_size_t length;
179 vm_offset_t addr;
181 type = (mach_msg_type_long_t *) saddr;
182 is_inline = type->msgtl_header.msgt_inline;
183 if (type->msgtl_header.msgt_longform) {
184 name = type->msgtl_name;
185 size = type->msgtl_size;
186 number = type->msgtl_number;
187 saddr += sizeof(mach_msg_type_long_t);
188 } else {
189 name = type->msgtl_header.msgt_name;
190 size = type->msgtl_header.msgt_size;
191 number = type->msgtl_header.msgt_number;
192 saddr += sizeof(mach_msg_type_t);
195 /* calculate length of data in bytes, rounding up */
196 length = (((((number * size) + 7) >> 3) + sizeof (int) - 1)
197 &~ (sizeof (int) - 1));
199 addr = is_inline ? saddr : * (vm_offset_t *) saddr;
201 if (MACH_MSG_TYPE_PORT_ANY(name)) {
202 mach_port_t *ports = (mach_port_t *) addr;
203 mach_msg_type_number_t i;
205 for (i = 0; i < number; i++)
206 mach_msg_destroy_port(*ports++, name);
209 if (is_inline) {
210 /* inline data sizes round up to int boundaries */
211 saddr += length;
212 } else {
213 mach_msg_destroy_memory(addr, length);
214 saddr += sizeof(vm_offset_t);
217 #endif
221 weak_alias (__mach_msg_destroy, mach_msg_destroy)
223 static void
224 mach_msg_destroy_port(port, type)
225 mach_port_t port;
226 mach_msg_type_name_t type;
228 if (MACH_PORT_VALID(port)) switch (type) {
229 case MACH_MSG_TYPE_MOVE_SEND:
230 case MACH_MSG_TYPE_MOVE_SEND_ONCE:
231 /* destroy the send/send-once right */
232 (void) __mach_port_deallocate(mach_task_self(), port);
233 break;
235 case MACH_MSG_TYPE_MOVE_RECEIVE:
236 /* destroy the receive right */
237 (void) __mach_port_mod_refs(mach_task_self(), port,
238 MACH_PORT_RIGHT_RECEIVE, -1);
239 break;
241 case MACH_MSG_TYPE_MAKE_SEND:
242 /* create a send right and then destroy it */
243 (void) __mach_port_insert_right(mach_task_self(), port,
244 port, MACH_MSG_TYPE_MAKE_SEND);
245 (void) __mach_port_deallocate(mach_task_self(), port);
246 break;
248 case MACH_MSG_TYPE_MAKE_SEND_ONCE:
249 /* create a send-once right and then destroy it */
250 (void) __mach_port_extract_right(mach_task_self(), port,
251 MACH_MSG_TYPE_MAKE_SEND_ONCE,
252 &port, &type);
253 (void) __mach_port_deallocate(mach_task_self(), port);
254 break;
258 static void
259 mach_msg_destroy_memory(addr, size)
260 vm_offset_t addr;
261 vm_size_t size;
263 if (size > 0)
264 (void) __vm_deallocate(__mach_task_self(), addr, size);