1 /* Replacement for mach_msg used in interruptible Hurd RPCs.
2 Copyright (C) 1995 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
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
21 #include <mach/mig_errors.h>
22 #include <hurd/signal.h>
28 _hurd_intr_rpc_mach_msg (mach_msg_header_t
*msg
,
29 mach_msg_option_t option
,
30 mach_msg_size_t send_size
,
31 mach_msg_size_t rcv_size
,
33 mach_msg_timeout_t timeout
,
36 struct hurd_sigstate
*ss
= _hurd_self_sigstate ();
38 /* Notice now if the user requested a timeout. OPTION may have the bit
39 added by interruption semantics, and we must distinguish. */
40 int user_timeout
= option
& MACH_RCV_TIMEOUT
;
42 /* Tell the signal thread that we are doing an interruptible RPC on
43 this port. If we get a signal and should return EINTR, the signal
44 thread will set this variable to MACH_PORT_NULL. The RPC might
45 return EINTR when some other thread gets a signal, in which case we
46 want to restart our call. */
47 ss
->intr_port
= msg
->msgh_remote_port
;
49 /* A signal may arrive here, after intr_port is set, but before
50 the mach_msg system call. The signal handler might do an
51 interruptible RPC, and clobber intr_port; then it would not be
52 set properly when we actually did send the RPC, and a later
53 signal wouldn't interrupt that RPC. So,
54 _hurd_setup_sighandler saves intr_port in the sigcontext, and
55 sigreturn restores it. */
65 err
= INTR_MSG_TRAP (msg
, option
, send_size
,
66 rcv_size
, rcv_name
, timeout
, notify
);
70 case MACH_RCV_TIMED_OUT
:
72 /* The real user RPC timed out. */
75 /* The operation was supposedly interrupted, but still has
76 not returned. Declare it interrupted. */
79 case MACH_SEND_INTERRUPTED
: /* RPC didn't get out. */
80 case EINTR
: /* Server not cooperating with interrupt. */
81 if (ss
->intr_port
!= MACH_PORT_NULL
)
82 /* If this signal was for us and it should interrupt calls, the
83 signal thread will have cleared SS->intr_port.
84 Since it's not cleared, the signal was for another thread,
85 or SA_RESTART is set. Restart the interrupted call. */
89 case MACH_RCV_PORT_DIED
:
90 /* Server didn't respond to interrupt_operation,
91 so the signal thread destroyed the reply port. */
97 /* The EINTR return indicates cancellation, so clear the flag. */
101 case MACH_RCV_INTERRUPTED
: /* RPC sent; no reply. */
102 option
&= ~MACH_SEND_MSG
; /* Don't send again. */
103 if (ss
->intr_port
== MACH_PORT_NULL
)
105 /* This signal or cancellation was for us. We need to wait for
106 the reply, but not hang forever. */
107 option
|= MACH_RCV_TIMEOUT
;
108 /* Never decrease the user's timeout. */
109 if (!user_timeout
|| timeout
> _hurd_interrupted_rpc_timeout
)
110 timeout
= _hurd_interrupted_rpc_timeout
;
112 goto message
; /* Retry the receive. */
114 case MACH_MSG_SUCCESS
:
115 if (option
& MACH_RCV_MSG
)
117 /* We got a reply. Was it EINTR? */
118 mig_reply_header_t
*const reply
= (void *) msg
;
125 MACH_MSG_TYPE_INTEGER_32
,
133 if (msg
->msgh_size
== sizeof *reply
&&
134 !(msg
->msgh_bits
& MACH_MSGH_BITS_COMPLEX
) &&
135 *(int *) &reply
->RetCodeType
== check
.i
&&
136 reply
->RetCode
== EINTR
)
138 /* It is indeed EINTR. Is the interrupt for us? */
139 if (ss
->intr_port
!= MACH_PORT_NULL
)
140 /* Nope; repeat the RPC.
141 XXX Resources moved? */
144 /* The EINTR return indicates cancellation, so clear the
151 default: /* Quiet -Wswitch-enum. */
154 ss
->intr_port
= MACH_PORT_NULL
;