(root)/
glibc-2.38/
mach/
msg-destroy.c
       1  /*
       2   * Mach Operating System
       3   * Copyright (c) 1991,1990 Carnegie Mellon University
       4   * All Rights Reserved.
       5   *
       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.
      11   *
      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.
      15   *
      16   * Carnegie Mellon requests users of this software to return to
      17   *
      18   *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
      19   *  School of Computer Science
      20   *  Carnegie Mellon University
      21   *  Pittsburgh PA 15213-3890
      22   *
      23   * any improvements or extensions that they make and grant Carnegie Mellon
      24   * the rights to redistribute these changes.
      25   */
      26  /*
      27   * (pre-GNU) HISTORY
      28   *
      29   * Revision 2.4  91/05/14  17:53:15  mrt
      30   * 	Correcting copyright
      31   *
      32   * Revision 2.3  91/02/14  14:17:43  mrt
      33   * 	Added new Mach copyright
      34   * 	[91/02/13  12:44:15  mrt]
      35   *
      36   * Revision 2.2  90/08/06  17:24:22  rpd
      37   * 	Created.
      38   *
      39   */
      40  
      41  #include <libc-pointer-arith.h>
      42  #if 1
      43  #include <mach.h>
      44  #else
      45  /* This is what CMU did, but that fails to declare some used functions.  */
      46  #include <mach/port.h>
      47  #include <mach/message.h>
      48  #include <mach_init.h>
      49  #endif
      50  
      51  static void mach_msg_destroy_port(mach_port_t, mach_msg_type_name_t);
      52  static void mach_msg_destroy_memory(vm_offset_t, vm_size_t);
      53  
      54  /*
      55   *	Routine:	mach_msg_destroy
      56   *	Purpose:
      57   *		Deallocates all port rights and out-of-line memory
      58   *		found in a received message.
      59   */
      60  
      61  void
      62  __mach_msg_destroy (mach_msg_header_t *msg)
      63  {
      64      mach_msg_bits_t mbits = msg->msgh_bits;
      65  
      66      /*
      67       *	The msgh_local_port field doesn't hold a port right.
      68       *	The receive operation consumes the destination port right.
      69       */
      70  
      71      mach_msg_destroy_port(msg->msgh_remote_port, MACH_MSGH_BITS_REMOTE(mbits));
      72  
      73      if (mbits & MACH_MSGH_BITS_COMPLEX) {
      74  #ifdef MACH_MSG_PORT_DESCRIPTOR
      75  	mach_msg_body_t		*body;
      76  	mach_msg_descriptor_t	*saddr, *eaddr;
      77  
      78  	body = (mach_msg_body_t *) (msg + 1);
      79  	saddr = (mach_msg_descriptor_t *)
      80  			((mach_msg_base_t *) msg + 1);
      81  	eaddr =  saddr + body->msgh_descriptor_count;
      82  
      83  	for  ( ; saddr < eaddr; saddr++) {
      84  	    switch (saddr->type.type) {
      85  
      86  	        case MACH_MSG_PORT_DESCRIPTOR: {
      87  		    mach_msg_port_descriptor_t *dsc;
      88  
      89  		    /*
      90  		     * Destroy port rights carried in the message
      91  		     */
      92  		    dsc = &saddr->port;
      93  		    mach_msg_destroy_port(dsc->name, dsc->disposition);
      94  		    break;
      95  	        }
      96  
      97  	        case MACH_MSG_OOL_DESCRIPTOR : {
      98  		    mach_msg_ool_descriptor_t *dsc;
      99  
     100  		    /*
     101  		     * Destroy memory carried in the message
     102  		     */
     103  		    dsc = &saddr->out_of_line;
     104  		    if (dsc->deallocate) {
     105  		        mach_msg_destroy_memory((vm_offset_t)dsc->address,
     106  						dsc->size);
     107  		    }
     108  		    break;
     109  	        }
     110  
     111  	        case MACH_MSG_OOL_PORTS_DESCRIPTOR : {
     112  		    mach_port_t             		*ports;
     113  		    mach_msg_ool_ports_descriptor_t	*dsc;
     114  		    mach_msg_type_number_t   		j;
     115  
     116  		    /*
     117  		     * Destroy port rights carried in the message
     118  		     */
     119  		    dsc = &saddr->ool_ports;
     120  		    ports = (mach_port_t *) dsc->address;
     121  		    for (j = 0; j < dsc->count; j++, ports++)  {
     122  		        mach_msg_destroy_port(*ports, dsc->disposition);
     123  		    }
     124  
     125  		    /*
     126  		     * Destroy memory carried in the message
     127  		     */
     128  		    if (dsc->deallocate) {
     129  		        mach_msg_destroy_memory((vm_offset_t)dsc->address,
     130  					dsc->count * sizeof(mach_port_t));
     131  		    }
     132  		    break;
     133  	        }
     134  	    }
     135  	}
     136  #else
     137  	vm_offset_t saddr;
     138  	vm_offset_t eaddr;
     139  
     140  	saddr = (vm_offset_t) (msg + 1);
     141  	eaddr = (vm_offset_t) msg + msg->msgh_size;
     142  
     143  	while (saddr < eaddr) {
     144  	    mach_msg_type_long_t *type;
     145  	    mach_msg_type_name_t name;
     146  	    mach_msg_type_size_t size;
     147  	    mach_msg_type_number_t number;
     148  	    boolean_t is_inline;
     149  	    vm_size_t length;
     150  	    vm_offset_t addr;
     151  
     152  	    type = (mach_msg_type_long_t *) saddr;
     153  	    is_inline = type->msgtl_header.msgt_inline;
     154  	    if (type->msgtl_header.msgt_longform) {
     155  		    name = type->msgtl_name;
     156  		    size = type->msgtl_size;
     157  		    number = type->msgtl_number;
     158  		    saddr += sizeof(mach_msg_type_long_t);
     159  	    } else {
     160  		    name = type->msgtl_header.msgt_name;
     161  		    size = type->msgtl_header.msgt_size;
     162  		    number = type->msgtl_header.msgt_number;
     163  		    saddr += sizeof(mach_msg_type_t);
     164  	    }
     165  
     166  	    /* Calculate length of data in bytes... */
     167  	    length = ((number * size) + 7) >> 3;
     168  	    /* ... and round up using uintptr_t alignment */
     169  	    length = ALIGN_UP (length, __alignof__ (uintptr_t));
     170  
     171  	    addr = is_inline ? saddr : * (vm_offset_t *) saddr;
     172  
     173  	    if (MACH_MSG_TYPE_PORT_ANY(name)) {
     174  		mach_port_t *ports = (mach_port_t *) addr;
     175  		mach_msg_type_number_t i;
     176  
     177  		for (i = 0; i < number; i++)
     178  		    mach_msg_destroy_port(*ports++, name);
     179  	    }
     180  
     181  	    if (is_inline) {
     182  		saddr += length;
     183  	    } else {
     184  		mach_msg_destroy_memory(addr, length);
     185  		saddr += sizeof(vm_offset_t);
     186  	    }
     187  	}
     188  #endif
     189      }
     190  }
     191  
     192  weak_alias (__mach_msg_destroy, mach_msg_destroy)
     193  libc_hidden_def (__mach_msg_destroy)
     194  
     195  static void
     196  mach_msg_destroy_port (mach_port_t port, mach_msg_type_name_t type)
     197  {
     198      if (MACH_PORT_VALID(port)) switch (type) {
     199        case MACH_MSG_TYPE_MOVE_SEND:
     200        case MACH_MSG_TYPE_MOVE_SEND_ONCE:
     201  	/* destroy the send/send-once right */
     202  	(void) __mach_port_deallocate(mach_task_self(), port);
     203  	break;
     204  
     205        case MACH_MSG_TYPE_MOVE_RECEIVE:
     206  	/* destroy the receive right */
     207  	(void) __mach_port_mod_refs(mach_task_self(), port,
     208  				    MACH_PORT_RIGHT_RECEIVE, -1);
     209  	break;
     210  
     211        case MACH_MSG_TYPE_MAKE_SEND:
     212  	/* create a send right and then destroy it */
     213  	(void) __mach_port_insert_right(mach_task_self(), port,
     214  					port, MACH_MSG_TYPE_MAKE_SEND);
     215  	(void) __mach_port_deallocate(mach_task_self(), port);
     216  	break;
     217  
     218        case MACH_MSG_TYPE_MAKE_SEND_ONCE:
     219  	/* create a send-once right and then destroy it */
     220  	(void) __mach_port_extract_right(mach_task_self(), port,
     221  					 MACH_MSG_TYPE_MAKE_SEND_ONCE,
     222  					 &port, &type);
     223  	(void) __mach_port_deallocate(mach_task_self(), port);
     224  	break;
     225      }
     226  }
     227  
     228  static void
     229  mach_msg_destroy_memory (vm_offset_t addr, vm_size_t size)
     230  {
     231      if (size > 0)
     232  	(void) __vm_deallocate(__mach_task_self(), addr, size);
     233  }