1 /* Copyright (C) 1993-2023 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 Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 /* Based on CMU's mach_msg_server.c revision 2.4 of 91/05/14, and thus
19 under the following copyright. Rewritten by Roland McGrath (FSF)
20 93/12/06 to use stack space instead of malloc, and to handle
21 large messages with MACH_RCV_LARGE. */
24 * Mach Operating System
25 * Copyright (c) 1991,1990 Carnegie Mellon University
26 * All Rights Reserved.
28 * Permission to use, copy, modify and distribute this software and its
29 * documentation is hereby granted, provided that both the copyright
30 * notice and this permission notice appear in all copies of the
31 * software, derivative works or modified versions, and any portions
32 * thereof, and that both notices appear in supporting documentation.
34 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
35 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
36 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
38 * Carnegie Mellon requests users of this software to return to
40 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
41 * School of Computer Science
42 * Carnegie Mellon University
43 * Pittsburgh PA 15213-3890
45 * any improvements or extensions that they make and grant Carnegie Mellon
46 * the rights to redistribute these changes.
51 * Revision 2.4 91/05/14 17:53:22 mrt
52 * Correcting copyright
54 * Revision 2.3 91/02/14 14:17:47 mrt
55 * Added new Mach copyright
56 * [91/02/13 12:44:20 mrt]
58 * Revision 2.2 90/08/06 17:23:58 rpd
65 #include <mach/mig_errors.h>
66 #include <stdlib.h> /* For malloc and free. */
70 __mach_msg_server_timeout (boolean_t (*demux
) (mach_msg_header_t
*request
,
71 mach_msg_header_t
*reply
),
72 mach_msg_size_t max_size
,
74 mach_msg_option_t option
,
75 mach_msg_timeout_t timeout
)
77 mig_reply_header_t
*request
, *reply
;
83 option
|= MACH_RCV_LARGE
;
84 max_size
= 2 * __vm_page_size
; /* Generic. Good? XXX */
86 max_size
= 4 * __vm_page_size
; /* XXX */
90 request
= __alloca (max_size
);
91 reply
= __alloca (max_size
);
96 mr
= __mach_msg (&request
->Head
, MACH_RCV_MSG
|option
,
97 0, max_size
, rcv_name
,
98 timeout
, MACH_PORT_NULL
);
99 while (mr
== MACH_MSG_SUCCESS
)
101 /* We have a request message.
102 Pass it to DEMUX for processing. */
104 (void) (*demux
) (&request
->Head
, &reply
->Head
);
105 assert (reply
->Head
.msgh_size
<= max_size
);
107 switch (reply
->RetCode
)
114 /* The server function wanted no reply sent.
115 Loop for another request. */
119 /* Some error; destroy the request message to release any
120 port rights or VM it holds. Don't destroy the reply port
121 right, so we can send an error message. */
122 request
->Head
.msgh_remote_port
= MACH_PORT_NULL
;
123 __mach_msg_destroy (&request
->Head
);
127 if (reply
->Head
.msgh_remote_port
== MACH_PORT_NULL
)
129 /* No reply port, so destroy the reply. */
130 if (reply
->Head
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
)
131 __mach_msg_destroy (&reply
->Head
);
135 /* Send the reply and the get next request. */
138 /* Swap the request and reply buffers. mach_msg will read the
139 reply message from the buffer we pass and write the new
140 request message to the same buffer. */
146 mr
= __mach_msg (&request
->Head
,
147 MACH_SEND_MSG
|MACH_RCV_MSG
|option
,
148 request
->Head
.msgh_size
, max_size
, rcv_name
,
149 timeout
, MACH_PORT_NULL
);
152 /* A message error occurred. */
156 case MACH_RCV_TOO_LARGE
:
157 #ifdef MACH_RCV_LARGE
158 /* The request message is larger than MAX_SIZE, and has not
159 been dequeued. 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
);
166 /* XXX the kernel has destroyed the msg */
170 case MACH_SEND_INVALID_DEST
:
171 /* The reply can't be delivered, so destroy it. This error
172 indicates only that the requester went away, so we
173 continue and get the next request. */
174 __mach_msg_destroy (&request
->Head
);
178 /* Some other form of lossage; return to caller. */
183 weak_alias (__mach_msg_server_timeout
, mach_msg_server_timeout
)
186 __mach_msg_server (boolean_t (*demux
) (mach_msg_header_t
*in
,
187 mach_msg_header_t
*out
),
188 mach_msg_size_t max_size
,
189 mach_port_t rcv_name
)
191 return __mach_msg_server_timeout (demux
, max_size
, rcv_name
,
192 MACH_MSG_OPTION_NONE
,
193 MACH_MSG_TIMEOUT_NONE
);
195 weak_alias (__mach_msg_server
, mach_msg_server
)