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.
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
);
202 inline void clean_inlined_ports (mach_port_name_inlined_t
*ports
)
204 mach_msg_type_number_t i
;
207 case MACH_MSG_TYPE_MOVE_SEND
:
208 for (i
= 0; i
< number
; i
++)
209 __mach_port_deallocate (__mach_task_self (), ports
[i
].name
);
210 if (ty
->msgtl_header
.msgt_longform
)
211 ty
->msgtl_name
= MACH_MSG_TYPE_COPY_SEND
;
213 ty
->msgtl_header
.msgt_name
= MACH_MSG_TYPE_COPY_SEND
;
215 case MACH_MSG_TYPE_COPY_SEND
:
216 case MACH_MSG_TYPE_MOVE_RECEIVE
:
219 if (MACH_MSG_TYPE_PORT_ANY (name
))
220 assert (! "unexpected port type in interruptible RPC");
225 if (ty
->msgtl_header
.msgt_longform
)
227 name
= ty
->msgtl_name
;
228 size
= ty
->msgtl_size
;
229 number
= ty
->msgtl_number
;
230 data
= (char *) ty
+ sizeof (mach_msg_type_long_t
);
234 name
= ty
->msgtl_header
.msgt_name
;
235 size
= ty
->msgtl_header
.msgt_size
;
236 number
= ty
->msgtl_header
.msgt_number
;
237 data
= (char *) ty
+ sizeof (mach_msg_type_t
);
240 /* Calculate length of data in bytes. */
241 const vm_size_t length
= ((number
* size
) + 7) >> 3;
242 if (ty
->msgtl_header
.msgt_inline
)
244 clean_inlined_ports ((mach_port_name_inlined_t
*) data
);
245 /* Move to the next argument. */
246 ty
= (void *) PTR_ALIGN_UP (data
+ length
, __alignof__ (uintptr_t));
250 clean_ports_and_memory (*(void **) data
, length
,
251 ty
->msgtl_header
.msgt_deallocate
);
252 ty
= (void *) data
+ sizeof (void *);
259 /* Either the process was stopped and continued,
260 or the server doesn't support interrupt_operation. */
261 if (ss
->intr_port
!= MACH_PORT_NULL
)
262 /* If this signal was for us and it should interrupt calls, the
263 signal thread will have cleared SS->intr_port.
264 Since it's not cleared, the signal was for another thread,
265 or SA_RESTART is set. Restart the interrupted call. */
267 /* Make sure we have a valid reply port. The one we were using
268 may have been destroyed by interruption. */
269 __mig_dealloc_reply_port (rcv_name
);
270 m
->header
.msgh_local_port
= rcv_name
= __mig_get_reply_port ();
271 m
->header
.msgh_bits
= msgh_bits
;
272 option
= user_option
;
273 timeout
= user_timeout
;
278 /* The EINTR return indicates cancellation, so clear the flag. */
282 case MACH_RCV_PORT_DIED
:
283 /* Server didn't respond to interrupt_operation,
284 so the signal thread destroyed the reply port. */
290 /* The EIEIO return indicates cancellation, so clear the flag. */
294 case MACH_RCV_INTERRUPTED
: /* RPC sent; no reply. */
295 option
&= ~MACH_SEND_MSG
; /* Don't send again. */
297 if (ss
->intr_port
== MACH_PORT_NULL
)
299 /* This signal or cancellation was for us. We need to wait for
300 the reply, but not hang forever. */
301 option
|= MACH_RCV_TIMEOUT
;
302 /* Never decrease the user's timeout. */
303 if (!(user_option
& MACH_RCV_TIMEOUT
)
304 || timeout
> _hurd_interrupted_rpc_timeout
)
305 timeout
= _hurd_interrupted_rpc_timeout
;
309 option
= user_option
;
310 timeout
= user_timeout
;
312 goto message
; /* Retry the receive. */
314 case MACH_MSG_SUCCESS
:
316 /* We got a reply. Was it EINTR? */
317 #ifdef MACH_MSG_TYPE_BIT
318 static const mach_msg_type_t type_check
= {
319 .msgt_name
= MACH_MSG_TYPE_INTEGER_T
,
320 .msgt_size
= sizeof (integer_t
) * 8,
323 .msgt_longform
= FALSE
,
324 .msgt_deallocate
= FALSE
,
329 if (m
->reply
.RetCode
== EINTR
330 && m
->header
.msgh_size
== sizeof m
->reply
331 #ifdef MACH_MSG_TYPE_BIT
332 && !BAD_TYPECHECK(&m
->check
.type
, &type_check
)
334 && !(m
->header
.msgh_bits
& MACH_MSGH_BITS_COMPLEX
))
336 /* It is indeed EINTR. Is the interrupt for us? */
337 if (ss
->intr_port
!= MACH_PORT_NULL
)
339 /* Nope; repeat the RPC.
340 XXX Resources moved? */
342 assert (m
->header
.msgh_id
== msgid
+ 100);
344 /* We know we have a valid reply port, because we just
345 received the EINTR reply on it. Restore it and the
346 other fields in the message header needed for send,
347 since the header now reflects receipt of the reply. */
348 m
->header
.msgh_local_port
= rcv_name
;
349 m
->header
.msgh_remote_port
= remote_port
;
350 m
->header
.msgh_id
= msgid
;
351 m
->header
.msgh_bits
= msgh_bits
;
352 /* Restore the two words clobbered by the reply data. */
353 m
->request
.data
= save_data
;
355 /* Restore the original mach_msg options.
356 OPTION may have had MACH_RCV_TIMEOUT added,
357 and/or MACH_SEND_MSG removed. */
358 option
= user_option
;
359 timeout
= user_timeout
;
361 /* Now we are ready to repeat the original message send. */
365 /* The EINTR return indicates cancellation,
366 so clear the flag. */
372 default: /* Quiet -Wswitch-enum. */
376 ss
->intr_port
= MACH_PORT_NULL
;
380 libc_hidden_def (_hurd_intr_rpc_mach_msg
)