]>
Commit | Line | Data |
---|---|---|
1 | /* Replacement for mach_msg used in interruptible Hurd RPCs. | |
2 | Copyright (C) 1995-2024 Free Software Foundation, Inc. | |
3 | This file is part of the GNU C Library. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
17 | <https://www.gnu.org/licenses/>. */ | |
18 | ||
19 | #include <mach.h> | |
20 | #include <mach_rpc.h> | |
21 | #include <mach/mig_errors.h> | |
22 | #include <mach/mig_support.h> | |
23 | #include <hurd/signal.h> | |
24 | #include <assert.h> | |
25 | ||
26 | #include "intr-msg.h" | |
27 | ||
28 | error_t | |
29 | _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg, | |
30 | mach_msg_option_t option, | |
31 | mach_msg_size_t send_size, | |
32 | mach_msg_size_t rcv_size, | |
33 | mach_port_t rcv_name, | |
34 | mach_msg_timeout_t timeout, | |
35 | mach_port_t notify) | |
36 | { | |
37 | error_t err; | |
38 | struct hurd_sigstate *ss; | |
39 | const mach_msg_option_t user_option = option; | |
40 | const mach_msg_timeout_t user_timeout = timeout; | |
41 | ||
42 | struct clobber | |
43 | { | |
44 | mach_msg_type_t type; | |
45 | union { | |
46 | error_t err; | |
47 | uintptr_t align; | |
48 | }; | |
49 | }; | |
50 | union msg | |
51 | { | |
52 | mach_msg_header_t header; | |
53 | mig_reply_header_t reply; | |
54 | struct | |
55 | { | |
56 | mach_msg_header_t header; | |
57 | mach_msg_type_t type; | |
58 | int code; | |
59 | } check; | |
60 | struct | |
61 | { | |
62 | mach_msg_header_t header; | |
63 | struct clobber data; | |
64 | } request; | |
65 | }; | |
66 | union msg *const m = (void *) msg; | |
67 | mach_msg_bits_t msgh_bits; | |
68 | mach_port_t remote_port; | |
69 | mach_msg_id_t msgid; | |
70 | struct clobber save_data; | |
71 | ||
72 | if ((option & (MACH_SEND_MSG|MACH_RCV_MSG)) != (MACH_SEND_MSG|MACH_RCV_MSG) | |
73 | || _hurd_msgport_thread == MACH_PORT_NULL) | |
74 | { | |
75 | /* Either this is not an RPC (i.e., only a send or only a receive), | |
76 | so it can't be interruptible; or, the signal thread is not set up | |
77 | yet, so we cannot do the normal signal magic. Do a normal, | |
78 | uninterruptible mach_msg call instead. */ | |
79 | return __mach_msg (&m->header, option, send_size, rcv_size, rcv_name, | |
80 | timeout, notify); | |
81 | } | |
82 | ||
83 | ss = _hurd_self_sigstate (); | |
84 | ||
85 | /* Save state that gets clobbered by an EINTR reply message. | |
86 | We will need to restore it if we want to retry the RPC. */ | |
87 | msgh_bits = m->header.msgh_bits; | |
88 | remote_port = m->header.msgh_remote_port; | |
89 | msgid = m->header.msgh_id; | |
90 | assert (rcv_size >= sizeof m->request); | |
91 | save_data = m->request.data; | |
92 | ||
93 | /* Tell the signal thread that we are doing an interruptible RPC on | |
94 | this port. If we get a signal and should return EINTR, the signal | |
95 | thread will set this variable to MACH_PORT_NULL. The RPC might | |
96 | return EINTR when some other thread gets a signal, in which case we | |
97 | want to restart our call. */ | |
98 | ss->intr_port = m->header.msgh_remote_port; | |
99 | ||
100 | /* A signal may arrive here, after intr_port is set, but before the | |
101 | mach_msg system call. The signal handler might do an interruptible | |
102 | RPC, and clobber intr_port; then it would not be set properly when we | |
103 | actually did send the RPC, and a later signal wouldn't interrupt that | |
104 | RPC. So, _hurd_setup_sighandler saves intr_port in the sigcontext, | |
105 | and sigreturn restores it. */ | |
106 | ||
107 | message: | |
108 | ||
109 | /* Note that the signal trampoline code might modify our OPTION! */ | |
110 | err = INTR_MSG_TRAP (msg, option, send_size, | |
111 | rcv_size, rcv_name, timeout, notify, | |
112 | &ss->cancel, &ss->intr_port); | |
113 | ||
114 | switch (err) | |
115 | { | |
116 | case MACH_RCV_TIMED_OUT: | |
117 | if (user_option & MACH_RCV_TIMEOUT) | |
118 | /* The real user RPC timed out. */ | |
119 | break; | |
120 | else | |
121 | /* The operation was supposedly interrupted, but still has | |
122 | not returned. Declare it interrupted. */ | |
123 | goto dead; | |
124 | ||
125 | case MACH_SEND_INTERRUPTED: /* RPC didn't get out. */ | |
126 | if (!(option & MACH_SEND_MSG)) | |
127 | { | |
128 | /* Oh yes, it did! Since we were not doing a message send, | |
129 | this return code cannot have come from the kernel! | |
130 | Instead, it was the signal thread mutating our state to tell | |
131 | us not to enter this RPC. However, we are already in the receive! | |
132 | Since the signal thread thought we weren't in the RPC yet, | |
133 | it didn't do an interrupt_operation. | |
134 | XXX */ | |
135 | goto retry_receive; | |
136 | } | |
137 | if (!(option & MACH_SEND_INTERRUPT)) | |
138 | { | |
139 | option = user_option; | |
140 | timeout = user_timeout; | |
141 | goto message; | |
142 | } | |
143 | /* FALLTHROUGH */ | |
144 | ||
145 | /* These are the other codes that mean a pseudo-receive modified | |
146 | the message buffer and we might need to clean up the port rights. */ | |
147 | case MACH_SEND_TIMED_OUT: | |
148 | case MACH_SEND_INVALID_NOTIFY: | |
149 | #ifdef MACH_SEND_NO_NOTIFY | |
150 | case MACH_SEND_NO_NOTIFY: | |
151 | #endif | |
152 | #ifdef MACH_SEND_NOTIFY_IN_PROGRESS | |
153 | case MACH_SEND_NOTIFY_IN_PROGRESS: | |
154 | #endif | |
155 | if (MACH_MSGH_BITS_REMOTE (msg->msgh_bits) == MACH_MSG_TYPE_MOVE_SEND) | |
156 | { | |
157 | __mach_port_deallocate (__mach_task_self (), msg->msgh_remote_port); | |
158 | msg->msgh_bits | |
159 | = (MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND, | |
160 | MACH_MSGH_BITS_LOCAL (msg->msgh_bits)) | |
161 | | MACH_MSGH_BITS_OTHER (msg->msgh_bits)); | |
162 | } | |
163 | if (msg->msgh_bits & MACH_MSGH_BITS_COMPLEX) | |
164 | { | |
165 | /* Check for MOVE_SEND rights in the message. These hold refs | |
166 | that we need to release in case the message is in fact never | |
167 | re-sent later. Since it might in fact be re-sent, we turn | |
168 | these into COPY_SEND's after deallocating the extra user ref; | |
169 | the caller is responsible for still holding a ref to go with | |
170 | the original COPY_SEND right, so the resend copies it again. */ | |
171 | ||
172 | mach_msg_type_long_t *ty = (void *) (msg + 1); | |
173 | while ((void *) ty < (void *) msg + msg->msgh_size) | |
174 | { | |
175 | mach_msg_type_name_t name; | |
176 | mach_msg_type_size_t size; | |
177 | mach_msg_type_number_t number; | |
178 | ||
179 | inline void clean_ports_and_memory (char *data, const vm_size_t length, | |
180 | int dealloc) | |
181 | { | |
182 | mach_msg_type_number_t i; | |
183 | switch (name) | |
184 | { | |
185 | case MACH_MSG_TYPE_MOVE_SEND: | |
186 | mach_port_t *ports = (mach_port_t *) data; | |
187 | for (i = 0; i < number; i++) | |
188 | __mach_port_deallocate (__mach_task_self (), *ports++); | |
189 | if (ty->msgtl_header.msgt_longform) | |
190 | ty->msgtl_name = MACH_MSG_TYPE_COPY_SEND; | |
191 | else | |
192 | ty->msgtl_header.msgt_name = MACH_MSG_TYPE_COPY_SEND; | |
193 | break; | |
194 | case MACH_MSG_TYPE_COPY_SEND: | |
195 | case MACH_MSG_TYPE_MOVE_RECEIVE: | |
196 | break; | |
197 | default: | |
198 | if (MACH_MSG_TYPE_PORT_ANY (name)) | |
199 | assert (! "unexpected port type in interruptible RPC"); | |
200 | } | |
201 | if (dealloc) | |
202 | __vm_deallocate (__mach_task_self (), (vm_address_t) data, length); | |
203 | } | |
204 | ||
205 | inline void clean_inlined_ports (mach_port_name_inlined_t *ports) | |
206 | { | |
207 | mach_msg_type_number_t i; | |
208 | switch (name) | |
209 | { | |
210 | case MACH_MSG_TYPE_MOVE_SEND: | |
211 | for (i = 0; i < number; i++) | |
212 | __mach_port_deallocate (__mach_task_self (), ports[i].name); | |
213 | if (ty->msgtl_header.msgt_longform) | |
214 | ty->msgtl_name = MACH_MSG_TYPE_COPY_SEND; | |
215 | else | |
216 | ty->msgtl_header.msgt_name = MACH_MSG_TYPE_COPY_SEND; | |
217 | break; | |
218 | case MACH_MSG_TYPE_COPY_SEND: | |
219 | case MACH_MSG_TYPE_MOVE_RECEIVE: | |
220 | break; | |
221 | default: | |
222 | if (MACH_MSG_TYPE_PORT_ANY (name)) | |
223 | assert (! "unexpected port type in interruptible RPC"); | |
224 | } | |
225 | } | |
226 | ||
227 | char *data; | |
228 | if (ty->msgtl_header.msgt_longform) | |
229 | { | |
230 | name = ty->msgtl_name; | |
231 | size = ty->msgtl_size; | |
232 | number = ty->msgtl_number; | |
233 | data = (char *) ty + sizeof (mach_msg_type_long_t); | |
234 | } | |
235 | else | |
236 | { | |
237 | name = ty->msgtl_header.msgt_name; | |
238 | size = ty->msgtl_header.msgt_size; | |
239 | number = ty->msgtl_header.msgt_number; | |
240 | data = (char *) ty + sizeof (mach_msg_type_t); | |
241 | } | |
242 | ||
243 | /* Calculate length of data in bytes. */ | |
244 | const vm_size_t length = ((number * size) + 7) >> 3; | |
245 | if (ty->msgtl_header.msgt_inline) | |
246 | { | |
247 | clean_inlined_ports ((mach_port_name_inlined_t *) data); | |
248 | /* Move to the next argument. */ | |
249 | ty = (void *) PTR_ALIGN_UP (data + length, __alignof__ (uintptr_t)); | |
250 | } | |
251 | else | |
252 | { | |
253 | clean_ports_and_memory (*(void **) data, length, | |
254 | ty->msgtl_header.msgt_deallocate); | |
255 | ty = (void *) data + sizeof (void *); | |
256 | } | |
257 | } | |
258 | } | |
259 | break; | |
260 | ||
261 | case EINTR: | |
262 | /* Either the process was stopped and continued, | |
263 | or the server doesn't support interrupt_operation. */ | |
264 | if (ss->intr_port != MACH_PORT_NULL) | |
265 | /* If this signal was for us and it should interrupt calls, the | |
266 | signal thread will have cleared SS->intr_port. | |
267 | Since it's not cleared, the signal was for another thread, | |
268 | or SA_RESTART is set. Restart the interrupted call. */ | |
269 | { | |
270 | /* Make sure we have a valid reply port. The one we were using | |
271 | may have been destroyed by interruption. */ | |
272 | __mig_dealloc_reply_port (rcv_name); | |
273 | m->header.msgh_local_port = rcv_name = __mig_get_reply_port (); | |
274 | m->header.msgh_bits = msgh_bits; | |
275 | option = user_option; | |
276 | timeout = user_timeout; | |
277 | goto message; | |
278 | } | |
279 | err = EINTR; | |
280 | ||
281 | /* The EINTR return indicates cancellation, so clear the flag. */ | |
282 | ss->cancel = 0; | |
283 | break; | |
284 | ||
285 | case MACH_RCV_PORT_DIED: | |
286 | /* Server didn't respond to interrupt_operation, | |
287 | so the signal thread destroyed the reply port. */ | |
288 | /* FALLTHROUGH */ | |
289 | ||
290 | dead: | |
291 | err = EIEIO; | |
292 | ||
293 | /* The EIEIO return indicates cancellation, so clear the flag. */ | |
294 | ss->cancel = 0; | |
295 | break; | |
296 | ||
297 | case MACH_RCV_INTERRUPTED: /* RPC sent; no reply. */ | |
298 | option &= ~MACH_SEND_MSG; /* Don't send again. */ | |
299 | retry_receive: | |
300 | if (ss->intr_port == MACH_PORT_NULL) | |
301 | { | |
302 | /* This signal or cancellation was for us. We need to wait for | |
303 | the reply, but not hang forever. */ | |
304 | option |= MACH_RCV_TIMEOUT; | |
305 | /* Never decrease the user's timeout. */ | |
306 | if (!(user_option & MACH_RCV_TIMEOUT) | |
307 | || timeout > _hurd_interrupted_rpc_timeout) | |
308 | timeout = _hurd_interrupted_rpc_timeout; | |
309 | } | |
310 | else | |
311 | { | |
312 | option = user_option; | |
313 | timeout = user_timeout; | |
314 | } | |
315 | goto message; /* Retry the receive. */ | |
316 | ||
317 | case MACH_MSG_SUCCESS: | |
318 | { | |
319 | /* We got a reply. Was it EINTR? */ | |
320 | #ifdef MACH_MSG_TYPE_BIT | |
321 | static const mach_msg_type_t type_check = { | |
322 | .msgt_name = MACH_MSG_TYPE_INTEGER_T, | |
323 | .msgt_size = sizeof (integer_t) * 8, | |
324 | .msgt_number = 1, | |
325 | .msgt_inline = TRUE, | |
326 | .msgt_longform = FALSE, | |
327 | .msgt_deallocate = FALSE, | |
328 | .msgt_unused = 0 | |
329 | }; | |
330 | #endif | |
331 | ||
332 | if (m->reply.RetCode == EINTR | |
333 | && m->header.msgh_size == sizeof m->reply | |
334 | #ifdef MACH_MSG_TYPE_BIT | |
335 | && !BAD_TYPECHECK(&m->check.type, &type_check) | |
336 | #endif | |
337 | && !(m->header.msgh_bits & MACH_MSGH_BITS_COMPLEX)) | |
338 | { | |
339 | /* It is indeed EINTR. Is the interrupt for us? */ | |
340 | if (ss->intr_port != MACH_PORT_NULL) | |
341 | { | |
342 | /* Nope; repeat the RPC. | |
343 | XXX Resources moved? */ | |
344 | ||
345 | assert (m->header.msgh_id == msgid + 100); | |
346 | ||
347 | /* We know we have a valid reply port, because we just | |
348 | received the EINTR reply on it. Restore it and the | |
349 | other fields in the message header needed for send, | |
350 | since the header now reflects receipt of the reply. */ | |
351 | m->header.msgh_local_port = rcv_name; | |
352 | m->header.msgh_remote_port = remote_port; | |
353 | m->header.msgh_id = msgid; | |
354 | m->header.msgh_bits = msgh_bits; | |
355 | /* Restore the two words clobbered by the reply data. */ | |
356 | m->request.data = save_data; | |
357 | ||
358 | /* Restore the original mach_msg options. | |
359 | OPTION may have had MACH_RCV_TIMEOUT added, | |
360 | and/or MACH_SEND_MSG removed. */ | |
361 | option = user_option; | |
362 | timeout = user_timeout; | |
363 | ||
364 | /* Now we are ready to repeat the original message send. */ | |
365 | goto message; | |
366 | } | |
367 | else | |
368 | /* The EINTR return indicates cancellation, | |
369 | so clear the flag. */ | |
370 | ss->cancel = 0; | |
371 | } | |
372 | } | |
373 | break; | |
374 | ||
375 | default: /* Quiet -Wswitch-enum. */ | |
376 | break; | |
377 | } | |
378 | ||
379 | ss->intr_port = MACH_PORT_NULL; | |
380 | ||
381 | return err; | |
382 | } | |
383 | libc_hidden_def (_hurd_intr_rpc_mach_msg) |