Fixed typo in IPPORT_WHOIS.
[glibc.git] / mach / msgserver.c
blobe0dc29b754c89003284c5c254cc32461ffb29d1c
1 /* Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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. */
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. */
24 /*
25 * Mach Operating System
26 * Copyright (c) 1991,1990 Carnegie Mellon University
27 * All Rights Reserved.
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.
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.
39 * Carnegie Mellon requests users of this software to return to
41 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
42 * School of Computer Science
43 * Carnegie Mellon University
44 * Pittsburgh PA 15213-3890
46 * any improvements or extensions that they make and grant Carnegie Mellon
47 * the rights to redistribute these changes.
50 * HISTORY
51 * $Log$
52 * Revision 1.3 1995/01/21 15:00:57 roland
53 * Converted to use weak aliases with macros from libc-symbols.h.
55 * Revision 1.2 1994/10/10 07:20:14 roland
56 * Increase default MAX_SIZE to two pages.
58 * Revision 1.1 1993/12/06 23:25:25 roland
59 * entered into RCS
61 * Revision 2.4 91/05/14 17:53:22 mrt
62 * Correcting copyright
64 * Revision 2.3 91/02/14 14:17:47 mrt
65 * Added new Mach copyright
66 * [91/02/13 12:44:20 mrt]
68 * Revision 2.2 90/08/06 17:23:58 rpd
69 * Created.
74 #include <mach.h>
75 #include <mach/mig_errors.h>
76 #include <stdlib.h> /* For malloc and free. */
78 mach_msg_return_t
79 __mach_msg_server_timeout (boolean_t (*demux) (mach_msg_header_t *request,
80 mach_msg_header_t *reply),
81 mach_msg_size_t max_size,
82 mach_port_t rcv_name,
83 mach_msg_option_t option,
84 mach_msg_timeout_t timeout)
86 register mig_reply_header_t *request, *reply;
87 register mach_msg_return_t mr;
89 if (max_size == 0)
91 option |= MACH_RCV_LARGE;
92 max_size = 2 * __vm_page_size; /* Generic. Good? XXX */
95 request = __alloca (max_size);
96 reply = __alloca (max_size);
98 while (1)
100 get_request:
101 mr = __mach_msg (&request->Head, MACH_RCV_MSG|option,
102 0, max_size, rcv_name,
103 timeout, MACH_PORT_NULL);
104 while (mr == MACH_MSG_SUCCESS)
106 /* We have a request message.
107 Pass it to DEMUX for processing. */
109 (void) (*demux) (&request->Head, &reply->Head);
111 switch (reply->RetCode)
113 case KERN_SUCCESS:
114 /* Hunky dory. */
115 break;
117 case MIG_NO_REPLY:
118 /* The server function wanted no reply sent.
119 Loop for another request. */
120 goto get_request;
122 default:
123 /* Some error; destroy the request message to release any
124 port rights or VM it holds. Don't destroy the reply port
125 right, so we can send an error message. */
126 request->Head.msgh_remote_port = MACH_PORT_NULL;
127 __mach_msg_destroy (&request->Head);
128 break;
131 if (reply->Head.msgh_remote_port == MACH_PORT_NULL)
133 /* No reply port, so destroy the reply. */
134 if (reply->Head.msgh_bits & MACH_MSGH_BITS_COMPLEX)
135 __mach_msg_destroy (&reply->Head);
136 goto get_request;
139 /* Send the reply and the get next request. */
142 /* Swap the request and reply buffers. mach_msg will read the
143 reply message from the buffer we pass and write the new
144 request message to the same buffer. */
145 void *tmp = request;
146 request = reply;
147 reply = tmp;
150 mr = __mach_msg (&request->Head,
151 MACH_SEND_MSG|MACH_RCV_MSG|option,
152 request->Head.msgh_size, max_size, rcv_name,
153 timeout, MACH_PORT_NULL);
156 /* A message error occurred. */
158 switch (mr)
160 case MACH_RCV_TOO_LARGE:
161 /* The request message is larger than MAX_SIZE, and has not
162 been dequued. The message header has the actual size of
163 the message. We recurse here in hopes that the compiler
164 will optimize the tail-call and allocate some more stack
165 space instead of way too much. */
166 return __mach_msg_server_timeout (demux, request->Head.msgh_size,
167 rcv_name, option, timeout);
169 case MACH_SEND_INVALID_DEST:
170 /* The reply can't be delivered, so destroy it. This error
171 indicates only that the requestor went away, so we
172 continue and get the next request. */
173 __mach_msg_destroy (&request->Head);
174 break;
176 default:
177 /* Some other form of lossage; return to caller. */
178 return mr;
182 weak_alias (__mach_msg_server_timeout, mach_msg_server_timeout)
184 mach_msg_return_t
185 __mach_msg_server (demux, max_size, rcv_name)
186 boolean_t (*demux) ();
187 mach_msg_size_t max_size;
188 mach_port_t rcv_name;
190 return __mach_msg_server_timeout (demux, max_size, rcv_name,
191 MACH_MSG_OPTION_NONE,
192 MACH_MSG_TIMEOUT_NONE);
194 weak_alias (__mach_msg_server, mach_msg_server)