Thu Feb 15 13:57:08 1996 Roland McGrath <roland@charlie-brown.gnu.ai.mit.edu>
[platform/upstream/glibc.git] / mach / msgserver.c
1 /* Copyright (C) 1993, 1994, 1995 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$
52  * Revision 1.4  1996/01/29 15:44:23  roland
53  * Declare DEMUX arg with prototype.
54  *
55  * Revision 1.3  1995/01/21  15:00:57  roland
56  * Converted to use weak aliases with macros from libc-symbols.h.
57  *
58  * Revision 1.2  1994/10/10  07:20:14  roland
59  * Increase default MAX_SIZE to two pages.
60  *
61  * Revision 1.1  1993/12/06  23:25:25  roland
62  * entered into RCS
63  *
64  * Revision 2.4  91/05/14  17:53:22  mrt
65  *      Correcting copyright
66  *
67  * Revision 2.3  91/02/14  14:17:47  mrt
68  *      Added new Mach copyright
69  *      [91/02/13  12:44:20  mrt]
70  *
71  * Revision 2.2  90/08/06  17:23:58  rpd
72  *      Created.
73  *
74  */
75
76
77 #include <mach.h>
78 #include <mach/mig_errors.h>
79 #include <stdlib.h>             /* For malloc and free.  */
80
81 mach_msg_return_t
82 __mach_msg_server_timeout (boolean_t (*demux) (mach_msg_header_t *request,
83                                                mach_msg_header_t *reply),
84                            mach_msg_size_t max_size,
85                            mach_port_t rcv_name,
86                            mach_msg_option_t option,
87                            mach_msg_timeout_t timeout)
88 {
89   register mig_reply_header_t *request, *reply;
90   register mach_msg_return_t mr;
91
92   if (max_size == 0)
93     {
94       option |= MACH_RCV_LARGE;
95       max_size = 2 * __vm_page_size; /* Generic.  Good? XXX */
96     }
97
98   request = __alloca (max_size);
99   reply = __alloca (max_size);
100
101   while (1)
102     {
103     get_request:
104       mr = __mach_msg (&request->Head, MACH_RCV_MSG|option,
105                        0, max_size, rcv_name,
106                        timeout, MACH_PORT_NULL);
107       while (mr == MACH_MSG_SUCCESS)
108         {
109           /* We have a request message.
110              Pass it to DEMUX for processing.  */
111
112           (void) (*demux) (&request->Head, &reply->Head);
113
114           switch (reply->RetCode)
115             {
116             case KERN_SUCCESS:
117               /* Hunky dory.  */
118               break;
119
120             case MIG_NO_REPLY:
121               /* The server function wanted no reply sent.
122                  Loop for another request.  */
123               goto get_request;
124
125             default:
126               /* Some error; destroy the request message to release any
127                  port rights or VM it holds.  Don't destroy the reply port
128                  right, so we can send an error message.  */
129               request->Head.msgh_remote_port = MACH_PORT_NULL;
130               __mach_msg_destroy (&request->Head);
131               break;
132             }
133
134           if (reply->Head.msgh_remote_port == MACH_PORT_NULL)
135             {
136               /* No reply port, so destroy the reply.  */
137               if (reply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX)
138                 __mach_msg_destroy (&reply->Head);
139               goto get_request;
140             }
141
142           /* Send the reply and the get next request.  */
143
144           {
145             /* Swap the request and reply buffers.  mach_msg will read the
146                reply message from the buffer we pass and write the new
147                request message to the same buffer.  */
148             void *tmp = request;
149             request = reply;
150             reply = tmp;
151           }
152
153           mr = __mach_msg (&request->Head,
154                            MACH_SEND_MSG|MACH_RCV_MSG|option,
155                            request->Head.msgh_size, max_size, rcv_name,
156                            timeout, MACH_PORT_NULL);
157         }
158
159       /* A message error occurred.  */
160
161       switch (mr)
162         {
163         case MACH_RCV_TOO_LARGE:
164           /* The request message is larger than MAX_SIZE, and has not
165              been dequued.  The message header has the actual size of
166              the message.  We recurse here in hopes that the compiler
167              will optimize the tail-call and allocate some more stack
168              space instead of way too much.  */
169           return __mach_msg_server_timeout (demux, request->Head.msgh_size,
170                                             rcv_name, option, timeout);
171
172         case MACH_SEND_INVALID_DEST:
173           /* The reply can't be delivered, so destroy it.  This error
174              indicates only that the requestor went away, so we
175              continue and get the next request.  */
176           __mach_msg_destroy (&request->Head);
177           break;
178
179         default:
180           /* Some other form of lossage; return to caller.  */
181           return mr;
182         }
183     }
184 }
185 weak_alias (__mach_msg_server_timeout, mach_msg_server_timeout)
186
187 mach_msg_return_t
188 __mach_msg_server (demux, max_size, rcv_name)
189      boolean_t (*demux) (mach_msg_header_t *in, mach_msg_header_t *out);
190      mach_msg_size_t max_size;
191      mach_port_t rcv_name;
192 {
193   return __mach_msg_server_timeout (demux, max_size, rcv_name,
194                                     MACH_MSG_OPTION_NONE,
195                                     MACH_MSG_TIMEOUT_NONE);
196 }
197 weak_alias (__mach_msg_server, mach_msg_server)