1 /* Replacement for mach_msg used in interruptible Hurd RPCs.
2 Copyright (C) 1995-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
21 #include <mach/mig_errors.h>
22 #include <mach/mig_support.h>
23 #include <hurd/signal.h>
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
,
34 mach_msg_timeout_t timeout
,
38 struct hurd_sigstate
*ss
;
39 const mach_msg_option_t user_option
= option
;
40 const mach_msg_timeout_t user_timeout
= timeout
;
49 mach_msg_header_t header
;
50 mig_reply_header_t reply
;
53 mach_msg_header_t header
;
59 mach_msg_header_t header
;
63 union msg
*const m
= (void *) msg
;
64 mach_msg_bits_t msgh_bits
;
65 mach_port_t remote_port
;
67 struct clobber save_data
;
69 if ((option
& (MACH_SEND_MSG
|MACH_RCV_MSG
)) != (MACH_SEND_MSG
|MACH_RCV_MSG
)
70 || _hurd_msgport_thread
== MACH_PORT_NULL
)
72 /* Either this is not an RPC (i.e., only a send or only a receive),
73 so it can't be interruptible; or, the signal thread is not set up
74 yet, so we cannot do the normal signal magic. Do a normal,
75 uninterruptible mach_msg call instead. */
76 return __mach_msg (&m
->header
, option
, send_size
, rcv_size
, rcv_name
,
80 ss
= _hurd_self_sigstate ();
82 /* Save state that gets clobbered by an EINTR reply message.
83 We will need to restore it if we want to retry the RPC. */
84 msgh_bits
= m
->header
.msgh_bits
;
85 remote_port
= m
->header
.msgh_remote_port
;
86 msgid
= m
->header
.msgh_id
;
87 assert (rcv_size
>= sizeof m
->request
);
88 save_data
= m
->request
.data
;
90 /* Tell the signal thread that we are doing an interruptible RPC on
91 this port. If we get a signal and should return EINTR, the signal
92 thread will set this variable to MACH_PORT_NULL. The RPC might
93 return EINTR when some other thread gets a signal, in which case we
94 want to restart our call. */
95 ss
->intr_port
= m
->header
.msgh_remote_port
;
97 /* A signal may arrive here, after intr_port is set, but before the
98 mach_msg system call. The signal handler might do an interruptible
99 RPC, and clobber intr_port; then it would not be set properly when we
100 actually did send the RPC, and a later signal wouldn't interrupt that
101 RPC. So, _hurd_setup_sighandler saves intr_port in the sigcontext,
102 and sigreturn restores it. */
106 /* Note that the signal trampoline code might modify our OPTION! */
107 err
= INTR_MSG_TRAP (msg
, option
, send_size
,
108 rcv_size
, rcv_name
, timeout
, notify
,
109 &ss
->cancel
, &ss
->intr_port
);
113 case MACH_RCV_TIMED_OUT
:
114 if (user_option
& MACH_RCV_TIMEOUT
)
115 /* The real user RPC timed out. */
118 /* The operation was supposedly interrupted, but still has
119 not returned. Declare it interrupted. */
122 case MACH_SEND_INTERRUPTED
: /* RPC didn't get out. */
123 if (!(option
& MACH_SEND_MSG
))
125 /* Oh yes, it did! Since we were not doing a message send,
126 this return code cannot have come from the kernel!
127 Instead, it was the signal thread mutating our state to tell
128 us not to enter this RPC. However, we are already in the receive!
129 Since the signal thread thought we weren't in the RPC yet,
130 it didn't do an interrupt_operation.
134 if (!(option
& MACH_SEND_INTERRUPT
))
136 option
= user_option
;
137 timeout
= user_timeout
;
142 /* These are the other codes that mean a pseudo-receive modified
143 the message buffer and we might need to clean up the port rights. */
144 case MACH_SEND_TIMED_OUT
:
145 case MACH_SEND_INVALID_NOTIFY
:
146 #ifdef MACH_SEND_NO_NOTIFY
147 case MACH_SEND_NO_NOTIFY
:
149 #ifdef MACH_SEND_NOTIFY_IN_PROGRESS
150 case MACH_SEND_NOTIFY_IN_PROGRESS
:
152 if (MACH_MSGH_BITS_REMOTE (msg
->msgh_bits
) == MACH_MSG_TYPE_MOVE_SEND
)
154 __mach_port_deallocate (__mach_task_self (), msg
->msgh_remote_port
);
156 = (MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND
,
157 MACH_MSGH_BITS_LOCAL (msg
->msgh_bits
))
158 | MACH_MSGH_BITS_OTHER (msg
->msgh_bits
));
160 if (msg
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
)
162 /* Check for MOVE_SEND rights in the message. These hold refs
163 that we need to release in case the message is in fact never
164 re-sent later. Since it might in fact be re-sent, we turn
165 these into COPY_SEND's after deallocating the extra user ref;
166 the caller is responsible for still holding a ref to go with
167 the original COPY_SEND right, so the resend copies it again. */
169 mach_msg_type_long_t
*ty
= (void *) (msg
+ 1);
170 while ((void *) ty
< (void *) msg
+ msg
->msgh_size
)
172 mach_msg_type_name_t name
;
173 mach_msg_type_size_t size
;
174 mach_msg_type_number_t number
;
176 inline void clean_ports_and_memory (char *data
, const vm_size_t length
,
179 mach_msg_type_number_t i
;
182 case MACH_MSG_TYPE_MOVE_SEND
:
183 mach_port_t
*ports
= (mach_port_t
*) data
;
184 for (i
= 0; i
< number
; i
++)
185 __mach_port_deallocate (__mach_task_self (), *ports
++);
186 if (ty
->msgtl_header
.msgt_longform
)
187 ty
->msgtl_name
= MACH_MSG_TYPE_COPY_SEND
;
189 ty
->msgtl_header
.msgt_name
= MACH_MSG_TYPE_COPY_SEND
;
191 case MACH_MSG_TYPE_COPY_SEND
:
192 case MACH_MSG_TYPE_MOVE_RECEIVE
:
195 if (MACH_MSG_TYPE_PORT_ANY (name
))
196 assert (! "unexpected port type in interruptible RPC");
199 __vm_deallocate (__mach_task_self (), (vm_address_t
) data
, length
);
203 if (ty
->msgtl_header
.msgt_longform
)
205 name
= ty
->msgtl_name
;
206 size
= ty
->msgtl_size
;
207 number
= ty
->msgtl_number
;
208 data
= (char *) ty
+ sizeof (mach_msg_type_long_t
);
212 name
= ty
->msgtl_header
.msgt_name
;
213 size
= ty
->msgtl_header
.msgt_size
;
214 number
= ty
->msgtl_header
.msgt_number
;
215 data
= (char *) ty
+ sizeof (mach_msg_type_t
);
218 const vm_size_t length
= ((number
* size
) + 7) >> 3;
219 if (ty
->msgtl_header
.msgt_inline
)
221 /* Calculate length of data in bytes. */
222 clean_ports_and_memory (data
, length
, 0);
223 /* Move to the next argument. */
224 ty
= (void *) PTR_ALIGN_UP (data
+ length
, __alignof__ (uintptr_t));
228 clean_ports_and_memory (*(void **) data
, length
,
229 ty
->msgtl_header
.msgt_deallocate
);
230 ty
= (void *) data
+ sizeof (void *);
237 /* Either the process was stopped and continued,
238 or the server doesn't support interrupt_operation. */
239 if (ss
->intr_port
!= MACH_PORT_NULL
)
240 /* If this signal was for us and it should interrupt calls, the
241 signal thread will have cleared SS->intr_port.
242 Since it's not cleared, the signal was for another thread,
243 or SA_RESTART is set. Restart the interrupted call. */
245 /* Make sure we have a valid reply port. The one we were using
246 may have been destroyed by interruption. */
247 __mig_dealloc_reply_port (rcv_name
);
248 m
->header
.msgh_local_port
= rcv_name
= __mig_get_reply_port ();
249 m
->header
.msgh_bits
= msgh_bits
;
250 option
= user_option
;
251 timeout
= user_timeout
;
256 /* The EINTR return indicates cancellation, so clear the flag. */
260 case MACH_RCV_PORT_DIED
:
261 /* Server didn't respond to interrupt_operation,
262 so the signal thread destroyed the reply port. */
268 /* The EIEIO return indicates cancellation, so clear the flag. */
272 case MACH_RCV_INTERRUPTED
: /* RPC sent; no reply. */
273 option
&= ~MACH_SEND_MSG
; /* Don't send again. */
275 if (ss
->intr_port
== MACH_PORT_NULL
)
277 /* This signal or cancellation was for us. We need to wait for
278 the reply, but not hang forever. */
279 option
|= MACH_RCV_TIMEOUT
;
280 /* Never decrease the user's timeout. */
281 if (!(user_option
& MACH_RCV_TIMEOUT
)
282 || timeout
> _hurd_interrupted_rpc_timeout
)
283 timeout
= _hurd_interrupted_rpc_timeout
;
287 option
= user_option
;
288 timeout
= user_timeout
;
290 goto message
; /* Retry the receive. */
292 case MACH_MSG_SUCCESS
:
294 /* We got a reply. Was it EINTR? */
295 #ifdef MACH_MSG_TYPE_BIT
296 static const mach_msg_type_t type_check
= {
297 .msgt_name
= MACH_MSG_TYPE_INTEGER_T
,
298 .msgt_size
= sizeof (integer_t
) * 8,
301 .msgt_longform
= FALSE
,
302 .msgt_deallocate
= FALSE
,
307 if (m
->reply
.RetCode
== EINTR
308 && m
->header
.msgh_size
== sizeof m
->reply
309 #ifdef MACH_MSG_TYPE_BIT
310 && !BAD_TYPECHECK(&m
->check
.type
, &type_check
)
312 && !(m
->header
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
))
314 /* It is indeed EINTR. Is the interrupt for us? */
315 if (ss
->intr_port
!= MACH_PORT_NULL
)
317 /* Nope; repeat the RPC.
318 XXX Resources moved? */
320 assert (m
->header
.msgh_id
== msgid
+ 100);
322 /* We know we have a valid reply port, because we just
323 received the EINTR reply on it. Restore it and the
324 other fields in the message header needed for send,
325 since the header now reflects receipt of the reply. */
326 m
->header
.msgh_local_port
= rcv_name
;
327 m
->header
.msgh_remote_port
= remote_port
;
328 m
->header
.msgh_id
= msgid
;
329 m
->header
.msgh_bits
= msgh_bits
;
330 /* Restore the two words clobbered by the reply data. */
331 m
->request
.data
= save_data
;
333 /* Restore the original mach_msg options.
334 OPTION may have had MACH_RCV_TIMEOUT added,
335 and/or MACH_SEND_MSG removed. */
336 option
= user_option
;
337 timeout
= user_timeout
;
339 /* Now we are ready to repeat the original message send. */
343 /* The EINTR return indicates cancellation,
344 so clear the flag. */
350 default: /* Quiet -Wswitch-enum. */
354 ss
->intr_port
= MACH_PORT_NULL
;
358 libc_hidden_def (_hurd_intr_rpc_mach_msg
)