Back to index

glibc  2.9
msg-destroy.c
Go to the documentation of this file.
00001 /*
00002  * Mach Operating System
00003  * Copyright (c) 1991,1990 Carnegie Mellon University
00004  * All Rights Reserved.
00005  *
00006  * Permission to use, copy, modify and distribute this software and its
00007  * documentation is hereby granted, provided that both the copyright
00008  * notice and this permission notice appear in all copies of the
00009  * software, derivative works or modified versions, and any portions
00010  * thereof, and that both notices appear in supporting documentation.
00011  *
00012  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
00013  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
00014  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
00015  *
00016  * Carnegie Mellon requests users of this software to return to
00017  *
00018  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
00019  *  School of Computer Science
00020  *  Carnegie Mellon University
00021  *  Pittsburgh PA 15213-3890
00022  *
00023  * any improvements or extensions that they make and grant Carnegie Mellon
00024  * the rights to redistribute these changes.
00025  */
00026 /*
00027  * (pre-GNU) HISTORY
00028  *
00029  * Revision 2.4  91/05/14  17:53:15  mrt
00030  *     Correcting copyright
00031  *
00032  * Revision 2.3  91/02/14  14:17:43  mrt
00033  *     Added new Mach copyright
00034  *     [91/02/13  12:44:15  mrt]
00035  *
00036  * Revision 2.2  90/08/06  17:24:22  rpd
00037  *     Created.
00038  *
00039  */
00040 
00041 #if 1
00042 #include <mach.h>
00043 #else
00044 /* This is what CMU did, but that fails to declare some used functions.  */
00045 #include <mach/port.h>
00046 #include <mach/message.h>
00047 #include <mach_init.h>
00048 #endif
00049 
00050 static void mach_msg_destroy_port(mach_port_t, mach_msg_type_name_t);
00051 static void mach_msg_destroy_memory(vm_offset_t, vm_size_t);
00052 
00053 /*
00054  *     Routine:      mach_msg_destroy
00055  *     Purpose:
00056  *            Deallocates all port rights and out-of-line memory
00057  *            found in a received message.
00058  */
00059 
00060 void
00061 __mach_msg_destroy(msg)
00062     mach_msg_header_t *msg;
00063 {
00064     mach_msg_bits_t mbits = msg->msgh_bits;
00065 
00066     /*
00067      * The msgh_local_port field doesn't hold a port right.
00068      * The receive operation consumes the destination port right.
00069      */
00070 
00071     mach_msg_destroy_port(msg->msgh_remote_port, MACH_MSGH_BITS_REMOTE(mbits));
00072 
00073     if (mbits & MACH_MSGH_BITS_COMPLEX) {
00074 #ifdef MACH_MSG_PORT_DESCRIPTOR
00075        mach_msg_body_t             *body;
00076        mach_msg_descriptor_t       *saddr, *eaddr;
00077 
00078        body = (mach_msg_body_t *) (msg + 1);
00079        saddr = (mach_msg_descriptor_t *)
00080                      ((mach_msg_base_t *) msg + 1);
00081        eaddr =  saddr + body->msgh_descriptor_count;
00082 
00083        for  ( ; saddr < eaddr; saddr++) {
00084            switch (saddr->type.type) {
00085 
00086                case MACH_MSG_PORT_DESCRIPTOR: {
00087                   mach_msg_port_descriptor_t *dsc;
00088 
00089                   /*
00090                    * Destroy port rights carried in the message
00091                    */
00092                   dsc = &saddr->port;
00093                   mach_msg_destroy_port(dsc->name, dsc->disposition);
00094                   break;
00095                }
00096 
00097                case MACH_MSG_OOL_DESCRIPTOR : {
00098                   mach_msg_ool_descriptor_t *dsc;
00099 
00100                   /*
00101                    * Destroy memory carried in the message
00102                    */
00103                   dsc = &saddr->out_of_line;
00104                   if (dsc->deallocate) {
00105                       mach_msg_destroy_memory((vm_offset_t)dsc->address,
00106                                           dsc->size);
00107                   }
00108                   break;
00109                }
00110 
00111                case MACH_MSG_OOL_PORTS_DESCRIPTOR : {
00112                   mach_port_t                           *ports;
00113                   mach_msg_ool_ports_descriptor_t       *dsc;
00114                   mach_msg_type_number_t                j;
00115 
00116                   /*
00117                    * Destroy port rights carried in the message
00118                    */
00119                   dsc = &saddr->ool_ports;
00120                   ports = (mach_port_t *) dsc->address;
00121                   for (j = 0; j < dsc->count; j++, ports++)  {
00122                       mach_msg_destroy_port(*ports, dsc->disposition);
00123                   }
00124 
00125                   /*
00126                    * Destroy memory carried in the message
00127                    */
00128                   if (dsc->deallocate) {
00129                       mach_msg_destroy_memory((vm_offset_t)dsc->address,
00130                                    dsc->count * sizeof(mach_port_t));
00131                   }
00132                   break;
00133                }
00134            }
00135        }
00136 #else
00137        vm_offset_t saddr;
00138        vm_offset_t eaddr;
00139 
00140        saddr = (vm_offset_t) (msg + 1);
00141        eaddr = (vm_offset_t) msg + msg->msgh_size;
00142 
00143        while (saddr < eaddr) {
00144            mach_msg_type_long_t *type;
00145            mach_msg_type_name_t name;
00146            mach_msg_type_size_t size;
00147            mach_msg_type_number_t number;
00148            boolean_t is_inline;
00149            vm_size_t length;
00150            vm_offset_t addr;
00151 
00152            type = (mach_msg_type_long_t *) saddr;
00153            is_inline = type->msgtl_header.msgt_inline;
00154            if (type->msgtl_header.msgt_longform) {
00155                   name = type->msgtl_name;
00156                   size = type->msgtl_size;
00157                   number = type->msgtl_number;
00158                   saddr += sizeof(mach_msg_type_long_t);
00159            } else {
00160                   name = type->msgtl_header.msgt_name;
00161                   size = type->msgtl_header.msgt_size;
00162                   number = type->msgtl_header.msgt_number;
00163                   saddr += sizeof(mach_msg_type_t);
00164            }
00165 
00166            /* calculate length of data in bytes, rounding up */
00167            length = (((((number * size) + 7) >> 3) + sizeof (int) - 1)
00168                     &~ (sizeof (int) - 1));
00169 
00170            addr = is_inline ? saddr : * (vm_offset_t *) saddr;
00171 
00172            if (MACH_MSG_TYPE_PORT_ANY(name)) {
00173               mach_port_t *ports = (mach_port_t *) addr;
00174               mach_msg_type_number_t i;
00175 
00176               for (i = 0; i < number; i++)
00177                   mach_msg_destroy_port(*ports++, name);
00178            }
00179 
00180            if (is_inline) {
00181               /* inline data sizes round up to int boundaries */
00182               saddr += length;
00183            } else {
00184               mach_msg_destroy_memory(addr, length);
00185               saddr += sizeof(vm_offset_t);
00186            }
00187        }
00188 #endif
00189     }
00190 }
00191 
00192 weak_alias (__mach_msg_destroy, mach_msg_destroy)
00193 
00194 static void
00195 mach_msg_destroy_port(port, type)
00196     mach_port_t port;
00197     mach_msg_type_name_t type;
00198 {
00199     if (MACH_PORT_VALID(port)) switch (type) {
00200       case MACH_MSG_TYPE_MOVE_SEND:
00201       case MACH_MSG_TYPE_MOVE_SEND_ONCE:
00202        /* destroy the send/send-once right */
00203        (void) __mach_port_deallocate(mach_task_self(), port);
00204        break;
00205 
00206       case MACH_MSG_TYPE_MOVE_RECEIVE:
00207        /* destroy the receive right */
00208        (void) __mach_port_mod_refs(mach_task_self(), port,
00209                                 MACH_PORT_RIGHT_RECEIVE, -1);
00210        break;
00211 
00212       case MACH_MSG_TYPE_MAKE_SEND:
00213        /* create a send right and then destroy it */
00214        (void) __mach_port_insert_right(mach_task_self(), port,
00215                                    port, MACH_MSG_TYPE_MAKE_SEND);
00216        (void) __mach_port_deallocate(mach_task_self(), port);
00217        break;
00218 
00219       case MACH_MSG_TYPE_MAKE_SEND_ONCE:
00220        /* create a send-once right and then destroy it */
00221        (void) __mach_port_extract_right(mach_task_self(), port,
00222                                     MACH_MSG_TYPE_MAKE_SEND_ONCE,
00223                                     &port, &type);
00224        (void) __mach_port_deallocate(mach_task_self(), port);
00225        break;
00226     }
00227 }
00228 
00229 static void
00230 mach_msg_destroy_memory(addr, size)
00231     vm_offset_t addr;
00232     vm_size_t size;
00233 {
00234     if (size > 0)
00235        (void) __vm_deallocate(__mach_task_self(), addr, size);
00236 }