]> git.ipfire.org Git - thirdparty/glibc.git/blame - hurd/intr-msg.c
(_hurd_sig_post): When doing pgrp, make sure we do ourselves last.
[thirdparty/glibc.git] / hurd / intr-msg.c
CommitLineData
54da5be3
RM
1/* Replacement for mach_msg used in interruptible Hurd RPCs.
2Copyright (C) 1995 Free Software Foundation, Inc.
3This file is part of the GNU C Library.
4
5The GNU C Library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Library General Public License as
7published by the Free Software Foundation; either version 2 of the
8License, or (at your option) any later version.
9
10The GNU C Library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Library General Public License for more details.
14
15You should have received a copy of the GNU Library General Public
16License along with the GNU C Library; see the file COPYING.LIB. If
17not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18Cambridge, MA 02139, USA. */
19
20#include <mach.h>
21#include <mach/mig_errors.h>
22#include <hurd/signal.h>
23
3fe9de0d
RM
24#include "intr-msg.h"
25
26
54da5be3
RM
27error_t
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,
32 mach_port_t rcv_name,
33 mach_msg_timeout_t timeout,
34 mach_port_t notify)
35{
36 struct hurd_sigstate *ss = _hurd_self_sigstate ();
37 error_t err;
a993273c
RM
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;
54da5be3
RM
41
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;
48
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. */
56
57 message:
58
59 if (ss->cancel)
60 {
61 err = EINTR;
62 ss->cancel = 0;
63 }
64 else
3fe9de0d
RM
65 err = INTR_MSG_TRAP (msg, option, send_size,
66 rcv_size, rcv_name, timeout, notify);
54da5be3
RM
67
68 switch (err)
69 {
a993273c
RM
70 case MACH_RCV_TIMED_OUT:
71 if (user_timeout)
72 /* The real user RPC timed out. */
73 break;
74 else
75 /* The operation was supposedly interrupted, but still has
76 not returned. Declare it interrupted. */
77 goto interrupted;
78
54da5be3 79 case MACH_SEND_INTERRUPTED: /* RPC didn't get out. */
44c8d1a2 80 case EINTR: /* Server not cooperating with interrupt. */
54da5be3
RM
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. */
86 goto message;
87 /* FALLTHROUGH */
88
89 case MACH_RCV_PORT_DIED:
90 /* Server didn't respond to interrupt_operation,
91 so the signal thread destroyed the reply port. */
92 /* FALLTHROUGH */
93
a993273c 94 interrupted:
54da5be3
RM
95 err = EINTR;
96
97 /* The EINTR return indicates cancellation, so clear the flag. */
98 ss->cancel = 0;
99 break;
100
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)
104 {
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;
a993273c
RM
108 /* Never decrease the user's timeout. */
109 if (!user_timeout || timeout > _hurd_interrupted_rpc_timeout)
110 timeout = _hurd_interrupted_rpc_timeout;
54da5be3
RM
111 }
112 goto message; /* Retry the receive. */
113
114 case MACH_MSG_SUCCESS:
115 if (option & MACH_RCV_MSG)
116 {
117 /* We got a reply. Was it EINTR? */
118 mig_reply_header_t *const reply = (void *) msg;
119 const union
120 {
121 mach_msg_type_t t;
122 int i;
123 } check =
124 { t: {
125 MACH_MSG_TYPE_INTEGER_32,
126 32,
127 1,
128 TRUE,
129 FALSE,
130 FALSE,
131 0
132 } };
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)
137 {
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? */
142 goto message;
143 else
144 /* The EINTR return indicates cancellation, so clear the
145 flag. */
146 ss->cancel = 0;
147 }
148 }
149 break;
150
151 default: /* Quiet -Wswitch-enum. */
152 }
153
154 ss->intr_port = MACH_PORT_NULL;
155
156 return err;
157}