Update.
[glibc.git] / hurd / intr-msg.c
blob73bcc849d3da2889b3c0bcf45c0f6f4b57256116
1 /* Replacement for mach_msg used in interruptible Hurd RPCs.
2 Copyright (C) 95,96,97,98,99,2000 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <mach.h>
21 #include <mach/mig_errors.h>
22 #include <mach/mig_support.h>
23 #include <hurd/signal.h>
24 #include <assert.h>
26 #include "intr-msg.h"
29 error_t
30 _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
31 mach_msg_option_t option,
32 mach_msg_size_t send_size,
33 mach_msg_size_t rcv_size,
34 mach_port_t rcv_name,
35 mach_msg_timeout_t timeout,
36 mach_port_t notify)
38 error_t err;
39 struct hurd_sigstate *ss;
40 const mach_msg_option_t user_option = option;
41 const mach_msg_timeout_t user_timeout = timeout;
43 struct clobber { int i[2]; };
44 union msg
46 mach_msg_header_t header;
47 mig_reply_header_t reply;
48 struct
50 mach_msg_header_t header;
51 int type;
52 int code;
53 } check;
54 struct
56 mach_msg_header_t header;
57 struct clobber data;
58 } request;
60 union msg *const m = (void *) msg;
61 mach_msg_bits_t msgh_bits;
62 mach_port_t remote_port;
63 mach_msg_id_t msgid;
64 struct clobber save_data;
66 if ((option & (MACH_SEND_MSG|MACH_RCV_MSG)) != (MACH_SEND_MSG|MACH_RCV_MSG)
67 || _hurd_msgport_thread == MACH_PORT_NULL)
69 /* Either this is not an RPC (i.e., only a send or only a receive),
70 so it can't be interruptible; or, the signal thread is not set up
71 yet, so we cannot do the normal signal magic. Do a normal,
72 uninterruptible mach_msg call instead. */
73 return __mach_msg (&m->header, option, send_size, rcv_size, rcv_name,
74 timeout, notify);
77 ss = _hurd_self_sigstate ();
79 /* Save state that gets clobbered by an EINTR reply message.
80 We will need to restore it if we want to retry the RPC. */
81 msgh_bits = m->header.msgh_bits;
82 remote_port = m->header.msgh_remote_port;
83 msgid = m->header.msgh_id;
84 assert (rcv_size >= sizeof m->request);
85 save_data = m->request.data;
87 /* Tell the signal thread that we are doing an interruptible RPC on
88 this port. If we get a signal and should return EINTR, the signal
89 thread will set this variable to MACH_PORT_NULL. The RPC might
90 return EINTR when some other thread gets a signal, in which case we
91 want to restart our call. */
92 ss->intr_port = m->header.msgh_remote_port;
94 /* A signal may arrive here, after intr_port is set, but before the
95 mach_msg system call. The signal handler might do an interruptible
96 RPC, and clobber intr_port; then it would not be set properly when we
97 actually did send the RPC, and a later signal wouldn't interrupt that
98 RPC. So, _hurd_setup_sighandler saves intr_port in the sigcontext,
99 and sigreturn restores it. */
101 message:
103 /* XXX
104 At all points here (once SS->intr_port is set), the signal thread
105 thinks we are "about to enter the syscall", and might mutate our
106 return-value register. This is bogus.
109 if (ss->cancel)
111 /* We have been cancelled. Don't do an RPC at all. */
112 ss->intr_port = MACH_PORT_NULL;
113 ss->cancel = 0;
114 return EINTR;
117 /* Note that the signal trampoline code might modify our OPTION! */
118 err = INTR_MSG_TRAP (msg, option, send_size,
119 rcv_size, rcv_name, timeout, notify);
121 switch (err)
123 case MACH_RCV_TIMED_OUT:
124 if (user_option & MACH_RCV_TIMEOUT)
125 /* The real user RPC timed out. */
126 break;
127 else
128 /* The operation was supposedly interrupted, but still has
129 not returned. Declare it interrupted. */
130 goto interrupted;
132 case MACH_SEND_INTERRUPTED: /* RPC didn't get out. */
133 if (!(option & MACH_SEND_MSG))
135 /* Oh yes, it did! Since we were not doing a message send,
136 this return code cannot have come from the kernel!
137 Instead, it was the signal thread mutating our state to tell
138 us not to enter this RPC. However, we are already in the receive!
139 Since the signal thread thought we weren't in the RPC yet,
140 it didn't do an interrupt_operation.
141 XXX */
142 goto retry_receive;
144 /* FALLTHROUGH */
146 /* These are the other codes that mean a pseudo-receive modified
147 the message buffer and we might need to clean up the port rights. */
148 case MACH_SEND_TIMED_OUT:
149 case MACH_SEND_INVALID_NOTIFY:
150 case MACH_SEND_NO_NOTIFY:
151 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);
155 msg->msgh_bits
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 (mach_port_t *ports, int dealloc)
178 mach_msg_type_number_t i;
179 switch (name)
181 case MACH_MSG_TYPE_MOVE_SEND:
182 for (i = 0; i < number; i++)
183 __mach_port_deallocate (__mach_task_self (), *ports++);
184 (ty->msgtl_header.msgt_longform
185 ? ty->msgtl_name : ty->msgtl_header.msgt_name)
186 = MACH_MSG_TYPE_COPY_SEND;
187 break;
188 case MACH_MSG_TYPE_COPY_SEND:
189 case MACH_MSG_TYPE_MOVE_RECEIVE:
190 break;
191 default:
192 if (MACH_MSG_TYPE_PORT_ANY (name))
193 assert (! "unexpected port type in interruptible RPC");
195 if (dealloc)
196 __vm_deallocate (__mach_task_self (),
197 (vm_address_t) ports,
198 number * sizeof (mach_port_t));
201 if (ty->msgtl_header.msgt_longform)
203 name = ty->msgtl_name;
204 size = ty->msgtl_size;
205 number = ty->msgtl_number;
206 (char *) ty += sizeof (mach_msg_type_long_t);
208 else
210 name = ty->msgtl_header.msgt_name;
211 size = ty->msgtl_header.msgt_size;
212 number = ty->msgtl_header.msgt_number;
213 (char *) ty += sizeof (mach_msg_type_t);
216 if (ty->msgtl_header.msgt_inline)
218 clean_ports ((void *) ty, 0);
219 /* calculate length of data in bytes, rounding up */
220 (char *) ty += (((((number * size) + 7) >> 3)
221 + sizeof (mach_msg_type_t) - 1)
222 &~ (sizeof (mach_msg_type_t) - 1));
224 else
225 clean_ports (*((void **) ty)++,
226 ty->msgtl_header.msgt_deallocate);
229 break;
231 case EINTR:
232 /* Either the process was stopped and continued,
233 or the server doesn't support interrupt_operation. */
234 if (ss->intr_port != MACH_PORT_NULL)
235 /* If this signal was for us and it should interrupt calls, the
236 signal thread will have cleared SS->intr_port.
237 Since it's not cleared, the signal was for another thread,
238 or SA_RESTART is set. Restart the interrupted call. */
240 /* Make sure we have a valid reply port. The one we were using
241 may have been destroyed by interruption. */
242 m->header.msgh_local_port = rcv_name = __mig_get_reply_port ();
243 m->header.msgh_bits = msgh_bits;
244 option = user_option;
245 timeout = user_timeout;
246 goto message;
248 /* FALLTHROUGH */
250 case MACH_RCV_PORT_DIED:
251 /* Server didn't respond to interrupt_operation,
252 so the signal thread destroyed the reply port. */
253 /* FALLTHROUGH */
255 interrupted:
256 err = EINTR;
258 /* The EINTR return indicates cancellation, so clear the flag. */
259 ss->cancel = 0;
260 break;
262 case MACH_RCV_INTERRUPTED: /* RPC sent; no reply. */
263 option &= ~MACH_SEND_MSG; /* Don't send again. */
264 retry_receive:
265 if (ss->intr_port == MACH_PORT_NULL)
267 /* This signal or cancellation was for us. We need to wait for
268 the reply, but not hang forever. */
269 option |= MACH_RCV_TIMEOUT;
270 /* Never decrease the user's timeout. */
271 if (!(user_option & MACH_RCV_TIMEOUT)
272 || timeout > _hurd_interrupted_rpc_timeout)
273 timeout = _hurd_interrupted_rpc_timeout;
275 else
277 option = user_option;
278 timeout = user_timeout;
280 goto message; /* Retry the receive. */
282 case MACH_MSG_SUCCESS:
284 /* We got a reply. Was it EINTR? */
285 const union
287 mach_msg_type_t t;
288 int i;
289 } check =
290 { t: { MACH_MSG_TYPE_INTEGER_T, sizeof (integer_t) * 8,
291 1, TRUE, FALSE, FALSE, 0 } };
293 if (m->reply.RetCode == EINTR &&
294 m->header.msgh_size == sizeof m->reply &&
295 m->check.type == check.i &&
296 !(m->header.msgh_bits & MACH_MSGH_BITS_COMPLEX))
298 /* It is indeed EINTR. Is the interrupt for us? */
299 if (ss->intr_port != MACH_PORT_NULL)
301 /* Nope; repeat the RPC.
302 XXX Resources moved? */
304 assert (m->header.msgh_id == msgid + 100);
306 /* We know we have a valid reply port, because we just
307 received the EINTR reply on it. Restore it and the
308 other fields in the message header needed for send,
309 since the header now reflects receipt of the reply. */
310 m->header.msgh_local_port = rcv_name;
311 m->header.msgh_remote_port = remote_port;
312 m->header.msgh_id = msgid;
313 m->header.msgh_bits = msgh_bits;
314 /* Restore the two words clobbered by the reply data. */
315 m->request.data = save_data;
317 /* Restore the original mach_msg options.
318 OPTION may have had MACH_RCV_TIMEOUT added,
319 and/or MACH_SEND_MSG removed. */
320 option = user_option;
321 timeout = user_timeout;
323 /* Now we are ready to repeat the original message send. */
324 goto message;
326 else
327 /* The EINTR return indicates cancellation,
328 so clear the flag. */
329 ss->cancel = 0;
332 break;
334 default: /* Quiet -Wswitch-enum. */
337 ss->intr_port = MACH_PORT_NULL;
339 return err;