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