home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / glibc-1.09 / glibc-1 / glibc-1.09.1 / mach / __msgserver.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-10  |  5.7 KB  |  191 lines

  1. /* Copyright (C) 1993, 1994 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3.  
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8.  
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12. Library General Public License for more details.
  13.  
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB.  If
  16. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  17. Cambridge, MA 02139, USA.  */
  18.  
  19. /* Based on CMU's mach_msg_server.c revision 2.4 of 91/05/14, and thus
  20.    under the following copyright.  Rewritten by Roland McGrath (FSF)
  21.    93/12/06 to use stack space instead of malloc, and to handle
  22.    large messages with MACH_RCV_LARGE.  */
  23.  
  24. /* 
  25.  * Mach Operating System
  26.  * Copyright (c) 1991,1990 Carnegie Mellon University
  27.  * All Rights Reserved.
  28.  * 
  29.  * Permission to use, copy, modify and distribute this software and its
  30.  * documentation is hereby granted, provided that both the copyright
  31.  * notice and this permission notice appear in all copies of the
  32.  * software, derivative works or modified versions, and any portions
  33.  * thereof, and that both notices appear in supporting documentation.
  34.  * 
  35.  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
  36.  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
  37.  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  38.  * 
  39.  * Carnegie Mellon requests users of this software to return to
  40.  * 
  41.  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
  42.  *  School of Computer Science
  43.  *  Carnegie Mellon University
  44.  *  Pittsburgh PA 15213-3890
  45.  * 
  46.  * any improvements or extensions that they make and grant Carnegie Mellon
  47.  * the rights to redistribute these changes.
  48.  */
  49. /*
  50.  * HISTORY
  51.  * $Log: __msgserver.c,v $
  52.  * Revision 1.2  1994/10/10  07:20:14  roland
  53.  * Increase default MAX_SIZE to two pages.
  54.  *
  55.  * Revision 1.1  1993/12/06  23:25:25  roland
  56.  * entered into RCS
  57.  *
  58.  * Revision 2.4  91/05/14  17:53:22  mrt
  59.  *     Correcting copyright
  60.  * 
  61.  * Revision 2.3  91/02/14  14:17:47  mrt
  62.  *     Added new Mach copyright
  63.  *     [91/02/13  12:44:20  mrt]
  64.  * 
  65.  * Revision 2.2  90/08/06  17:23:58  rpd
  66.  *     Created.
  67.  * 
  68.  */
  69.  
  70.  
  71. #include <mach.h>
  72. #include <mach/mig_errors.h>
  73. #include <stdlib.h>        /* For malloc and free.  */
  74.  
  75. mach_msg_return_t
  76. __mach_msg_server_timeout (boolean_t (*demux) (mach_msg_header_t *request,
  77.                            mach_msg_header_t *reply),
  78.                mach_msg_size_t max_size,
  79.                mach_port_t rcv_name,
  80.                mach_msg_option_t option,
  81.                mach_msg_timeout_t timeout)
  82. {
  83.   register mig_reply_header_t *request, *reply;
  84.   register mach_msg_return_t mr;
  85.  
  86.   if (max_size == 0)
  87.     {
  88.       option |= MACH_RCV_LARGE;
  89.       max_size = 2 * __vm_page_size; /* Generic.  Good? XXX */
  90.     }
  91.  
  92.   request = __alloca (max_size);
  93.   reply = __alloca (max_size);
  94.  
  95.   while (1)
  96.     {
  97.     get_request:
  98.       mr = __mach_msg (&request->Head, MACH_RCV_MSG|option,
  99.                0, max_size, rcv_name,
  100.                timeout, MACH_PORT_NULL);
  101.       while (mr == MACH_MSG_SUCCESS)
  102.     {
  103.       /* We have a request message.
  104.          Pass it to DEMUX for processing.  */
  105.  
  106.       (void) (*demux) (&request->Head, &reply->Head);
  107.  
  108.       switch (reply->RetCode)
  109.         {
  110.         case KERN_SUCCESS:
  111.           /* Hunky dory.  */
  112.           break;
  113.  
  114.         case MIG_NO_REPLY:
  115.           /* The server function wanted no reply sent.
  116.          Loop for another request.  */
  117.           goto get_request;
  118.  
  119.         default:
  120.           /* Some error; destroy the request message to release any
  121.          port rights or VM it holds.  Don't destroy the reply port
  122.          right, so we can send an error message.  */
  123.           request->Head.msgh_remote_port = MACH_PORT_NULL;
  124.           __mach_msg_destroy (&request->Head);
  125.           break;
  126.         }
  127.  
  128.       if (reply->Head.msgh_remote_port == MACH_PORT_NULL)
  129.         {
  130.           /* No reply port, so destroy the reply.  */
  131.           if (reply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX)
  132.         __mach_msg_destroy (&reply->Head);
  133.           goto get_request;
  134.         }
  135.  
  136.       /* Send the reply and the get next request.  */
  137.  
  138.       {
  139.         /* Swap the request and reply buffers.  mach_msg will read the
  140.            reply message from the buffer we pass and write the new
  141.            request message to the same buffer.  */
  142.         void *tmp = request;
  143.         request = reply;
  144.         reply = tmp;
  145.       }
  146.  
  147.       mr = __mach_msg (&request->Head,
  148.                MACH_SEND_MSG|MACH_RCV_MSG|option,
  149.                request->Head.msgh_size, max_size, rcv_name,
  150.                timeout, MACH_PORT_NULL);
  151.     }
  152.  
  153.       /* A message error occurred.  */
  154.  
  155.       switch (mr)
  156.     {
  157.     case MACH_RCV_TOO_LARGE:
  158.       /* The request message is larger than MAX_SIZE, and has not
  159.          been dequued.  The message header has the actual size of
  160.          the message.  We recurse here in hopes that the compiler
  161.          will optimize the tail-call and allocate some more stack
  162.          space instead of way too much.  */
  163.       return __mach_msg_server_timeout (demux, request->Head.msgh_size,
  164.                         rcv_name, option, timeout);
  165.  
  166.     case MACH_SEND_INVALID_DEST:
  167.       /* The reply can't be delivered, so destroy it.  This error
  168.          indicates only that the requestor went away, so we
  169.          continue and get the next request.  */
  170.       __mach_msg_destroy (&request->Head);
  171.       break;
  172.  
  173.     default:
  174.       /* Some other form of lossage; return to caller.  */
  175.       return mr;
  176.     }
  177.     }
  178. }
  179.  
  180.  
  181. mach_msg_return_t
  182. __mach_msg_server (demux, max_size, rcv_name)
  183.      boolean_t (*demux) ();
  184.      mach_msg_size_t max_size;
  185.      mach_port_t rcv_name;
  186. {
  187.   return __mach_msg_server_timeout (demux, max_size, rcv_name,
  188.                     MACH_MSG_OPTION_NONE,
  189.                     MACH_MSG_TIMEOUT_NONE);
  190. }
  191.