intr-msg.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /* Replacement for mach_msg used in interruptible Hurd RPCs.
  2. Copyright (C) 1995-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <mach.h>
  16. #include <mach_rpc.h>
  17. #include <mach/mig_errors.h>
  18. #include <mach/mig_support.h>
  19. #include <hurd/signal.h>
  20. #include <assert.h>
  21. #include "intr-msg.h"
  22. error_t
  23. _hurd_intr_rpc_mach_msg (mach_msg_header_t *msg,
  24. mach_msg_option_t option,
  25. mach_msg_size_t send_size,
  26. mach_msg_size_t rcv_size,
  27. mach_port_t rcv_name,
  28. mach_msg_timeout_t timeout,
  29. mach_port_t notify)
  30. {
  31. error_t err;
  32. struct hurd_sigstate *ss;
  33. const mach_msg_option_t user_option = option;
  34. const mach_msg_timeout_t user_timeout = timeout;
  35. struct clobber
  36. {
  37. mach_msg_type_t type;
  38. union {
  39. error_t err;
  40. uintptr_t align;
  41. };
  42. };
  43. union msg
  44. {
  45. mach_msg_header_t header;
  46. mig_reply_header_t reply;
  47. struct
  48. {
  49. mach_msg_header_t header;
  50. mach_msg_type_t type;
  51. int code;
  52. } check;
  53. struct
  54. {
  55. mach_msg_header_t header;
  56. struct clobber data;
  57. } request;
  58. };
  59. union msg *const m = (void *) msg;
  60. mach_msg_bits_t msgh_bits;
  61. mach_port_t remote_port;
  62. mach_msg_id_t msgid;
  63. struct clobber save_data;
  64. if ((option & (MACH_SEND_MSG|MACH_RCV_MSG)) != (MACH_SEND_MSG|MACH_RCV_MSG)
  65. || _hurd_msgport_thread == MACH_PORT_NULL)
  66. {
  67. /* Either this is not an RPC (i.e., only a send or only a receive),
  68. so it can't be interruptible; or, the signal thread is not set up
  69. yet, so we cannot do the normal signal magic. Do a normal,
  70. uninterruptible mach_msg call instead. */
  71. return __mach_msg (&m->header, option, send_size, rcv_size, rcv_name,
  72. timeout, notify);
  73. }
  74. ss = _hurd_self_sigstate ();
  75. /* Save state that gets clobbered by an EINTR reply message.
  76. We will need to restore it if we want to retry the RPC. */
  77. msgh_bits = m->header.msgh_bits;
  78. remote_port = m->header.msgh_remote_port;
  79. msgid = m->header.msgh_id;
  80. assert (rcv_size >= sizeof m->request);
  81. save_data = m->request.data;
  82. /* Tell the signal thread that we are doing an interruptible RPC on
  83. this port. If we get a signal and should return EINTR, the signal
  84. thread will set this variable to MACH_PORT_NULL. The RPC might
  85. return EINTR when some other thread gets a signal, in which case we
  86. want to restart our call. */
  87. ss->intr_port = m->header.msgh_remote_port;
  88. /* A signal may arrive here, after intr_port is set, but before the
  89. mach_msg system call. The signal handler might do an interruptible
  90. RPC, and clobber intr_port; then it would not be set properly when we
  91. actually did send the RPC, and a later signal wouldn't interrupt that
  92. RPC. So, _hurd_setup_sighandler saves intr_port in the sigcontext,
  93. and sigreturn restores it. */
  94. message:
  95. /* Note that the signal trampoline code might modify our OPTION! */
  96. err = INTR_MSG_TRAP (msg, option, send_size,
  97. rcv_size, rcv_name, timeout, notify,
  98. &ss->cancel, &ss->intr_port);
  99. switch (err)
  100. {
  101. case MACH_RCV_TIMED_OUT:
  102. if (user_option & MACH_RCV_TIMEOUT)
  103. /* The real user RPC timed out. */
  104. break;
  105. else
  106. /* The operation was supposedly interrupted, but still has
  107. not returned. Declare it interrupted. */
  108. goto dead;
  109. case MACH_SEND_INTERRUPTED: /* RPC didn't get out. */
  110. if (!(option & MACH_SEND_MSG))
  111. {
  112. /* Oh yes, it did! Since we were not doing a message send,
  113. this return code cannot have come from the kernel!
  114. Instead, it was the signal thread mutating our state to tell
  115. us not to enter this RPC. However, we are already in the receive!
  116. Since the signal thread thought we weren't in the RPC yet,
  117. it didn't do an interrupt_operation.
  118. XXX */
  119. goto retry_receive;
  120. }
  121. if (!(option & MACH_SEND_INTERRUPT))
  122. {
  123. option = user_option;
  124. timeout = user_timeout;
  125. goto message;
  126. }
  127. /* FALLTHROUGH */
  128. /* These are the other codes that mean a pseudo-receive modified
  129. the message buffer and we might need to clean up the port rights. */
  130. case MACH_SEND_TIMED_OUT:
  131. case MACH_SEND_INVALID_NOTIFY:
  132. #ifdef MACH_SEND_NO_NOTIFY
  133. case MACH_SEND_NO_NOTIFY:
  134. #endif
  135. #ifdef MACH_SEND_NOTIFY_IN_PROGRESS
  136. case MACH_SEND_NOTIFY_IN_PROGRESS:
  137. #endif
  138. if (MACH_MSGH_BITS_REMOTE (msg->msgh_bits) == MACH_MSG_TYPE_MOVE_SEND)
  139. {
  140. __mach_port_deallocate (__mach_task_self (), msg->msgh_remote_port);
  141. msg->msgh_bits
  142. = (MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND,
  143. MACH_MSGH_BITS_LOCAL (msg->msgh_bits))
  144. | MACH_MSGH_BITS_OTHER (msg->msgh_bits));
  145. }
  146. if (msg->msgh_bits & MACH_MSGH_BITS_COMPLEX)
  147. {
  148. /* Check for MOVE_SEND rights in the message. These hold refs
  149. that we need to release in case the message is in fact never
  150. re-sent later. Since it might in fact be re-sent, we turn
  151. these into COPY_SEND's after deallocating the extra user ref;
  152. the caller is responsible for still holding a ref to go with
  153. the original COPY_SEND right, so the resend copies it again. */
  154. mach_msg_type_long_t *ty = (void *) (msg + 1);
  155. while ((void *) ty < (void *) msg + msg->msgh_size)
  156. {
  157. mach_msg_type_name_t name;
  158. mach_msg_type_size_t size;
  159. mach_msg_type_number_t number;
  160. inline void clean_ports_and_memory (char *data, const vm_size_t length,
  161. int dealloc)
  162. {
  163. mach_msg_type_number_t i;
  164. switch (name)
  165. {
  166. case MACH_MSG_TYPE_MOVE_SEND:
  167. {
  168. mach_port_t *ports = (mach_port_t *) data;
  169. for (i = 0; i < number; i++)
  170. __mach_port_deallocate (__mach_task_self (),
  171. *ports++);
  172. }
  173. if (ty->msgtl_header.msgt_longform)
  174. ty->msgtl_name = MACH_MSG_TYPE_COPY_SEND;
  175. else
  176. ty->msgtl_header.msgt_name = MACH_MSG_TYPE_COPY_SEND;
  177. break;
  178. case MACH_MSG_TYPE_COPY_SEND:
  179. case MACH_MSG_TYPE_MOVE_RECEIVE:
  180. break;
  181. default:
  182. if (MACH_MSG_TYPE_PORT_ANY (name))
  183. assert (! "unexpected port type in interruptible RPC");
  184. }
  185. if (dealloc)
  186. __vm_deallocate (__mach_task_self (), (vm_address_t) data, length);
  187. }
  188. inline void clean_inlined_ports (mach_port_name_inlined_t *ports)
  189. {
  190. mach_msg_type_number_t i;
  191. switch (name)
  192. {
  193. case MACH_MSG_TYPE_MOVE_SEND:
  194. for (i = 0; i < number; i++)
  195. __mach_port_deallocate (__mach_task_self (), ports[i].name);
  196. if (ty->msgtl_header.msgt_longform)
  197. ty->msgtl_name = MACH_MSG_TYPE_COPY_SEND;
  198. else
  199. ty->msgtl_header.msgt_name = MACH_MSG_TYPE_COPY_SEND;
  200. break;
  201. case MACH_MSG_TYPE_COPY_SEND:
  202. case MACH_MSG_TYPE_MOVE_RECEIVE:
  203. break;
  204. default:
  205. if (MACH_MSG_TYPE_PORT_ANY (name))
  206. assert (! "unexpected port type in interruptible RPC");
  207. }
  208. }
  209. char *data;
  210. if (ty->msgtl_header.msgt_longform)
  211. {
  212. name = ty->msgtl_name;
  213. size = ty->msgtl_size;
  214. number = ty->msgtl_number;
  215. data = (char *) ty + sizeof (mach_msg_type_long_t);
  216. }
  217. else
  218. {
  219. name = ty->msgtl_header.msgt_name;
  220. size = ty->msgtl_header.msgt_size;
  221. number = ty->msgtl_header.msgt_number;
  222. data = (char *) ty + sizeof (mach_msg_type_t);
  223. }
  224. /* Calculate length of data in bytes. */
  225. const vm_size_t length = ((number * size) + 7) >> 3;
  226. if (ty->msgtl_header.msgt_inline)
  227. {
  228. clean_inlined_ports ((mach_port_name_inlined_t *) data);
  229. /* Move to the next argument. */
  230. ty = (void *) PTR_ALIGN_UP (data + length, __alignof__ (uintptr_t));
  231. }
  232. else
  233. {
  234. clean_ports_and_memory (*(void **) data, length,
  235. ty->msgtl_header.msgt_deallocate);
  236. ty = (void *) data + sizeof (void *);
  237. }
  238. }
  239. }
  240. break;
  241. case EINTR:
  242. /* Either the process was stopped and continued,
  243. or the server doesn't support interrupt_operation. */
  244. if (ss->intr_port != MACH_PORT_NULL)
  245. /* If this signal was for us and it should interrupt calls, the
  246. signal thread will have cleared SS->intr_port.
  247. Since it's not cleared, the signal was for another thread,
  248. or SA_RESTART is set. Restart the interrupted call. */
  249. {
  250. /* Make sure we have a valid reply port. The one we were using
  251. may have been destroyed by interruption. */
  252. __mig_dealloc_reply_port (rcv_name);
  253. m->header.msgh_local_port = rcv_name = __mig_get_reply_port ();
  254. m->header.msgh_bits = msgh_bits;
  255. option = user_option;
  256. timeout = user_timeout;
  257. goto message;
  258. }
  259. err = EINTR;
  260. /* The EINTR return indicates cancellation, so clear the flag. */
  261. ss->cancel = 0;
  262. break;
  263. case MACH_RCV_PORT_DIED:
  264. /* Server didn't respond to interrupt_operation,
  265. so the signal thread destroyed the reply port. */
  266. /* FALLTHROUGH */
  267. dead:
  268. err = EIEIO;
  269. /* The EIEIO return indicates cancellation, so clear the flag. */
  270. ss->cancel = 0;
  271. break;
  272. case MACH_RCV_INTERRUPTED: /* RPC sent; no reply. */
  273. option &= ~MACH_SEND_MSG; /* Don't send again. */
  274. retry_receive:
  275. if (ss->intr_port == MACH_PORT_NULL)
  276. {
  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;
  284. }
  285. else
  286. {
  287. option = user_option;
  288. timeout = user_timeout;
  289. }
  290. goto message; /* Retry the receive. */
  291. case MACH_MSG_SUCCESS:
  292. {
  293. /* We got a reply. Was it EINTR? */
  294. #ifdef MACH_MSG_TYPE_BIT
  295. static const mach_msg_type_t type_check = {
  296. .msgt_name = MACH_MSG_TYPE_INTEGER_T,
  297. .msgt_size = sizeof (integer_t) * 8,
  298. .msgt_number = 1,
  299. .msgt_inline = TRUE,
  300. .msgt_longform = FALSE,
  301. .msgt_deallocate = FALSE,
  302. .msgt_unused = 0
  303. };
  304. #endif
  305. if (m->reply.RetCode == EINTR
  306. && m->header.msgh_size == sizeof m->reply
  307. #ifdef MACH_MSG_TYPE_BIT
  308. && !BAD_TYPECHECK(&m->check.type, &type_check)
  309. #endif
  310. && !(m->header.msgh_bits & MACH_MSGH_BITS_COMPLEX))
  311. {
  312. /* It is indeed EINTR. Is the interrupt for us? */
  313. if (ss->intr_port != MACH_PORT_NULL)
  314. {
  315. /* Nope; repeat the RPC.
  316. XXX Resources moved? */
  317. assert (m->header.msgh_id == msgid + 100);
  318. /* We know we have a valid reply port, because we just
  319. received the EINTR reply on it. Restore it and the
  320. other fields in the message header needed for send,
  321. since the header now reflects receipt of the reply. */
  322. m->header.msgh_local_port = rcv_name;
  323. m->header.msgh_remote_port = remote_port;
  324. m->header.msgh_id = msgid;
  325. m->header.msgh_bits = msgh_bits;
  326. /* Restore the two words clobbered by the reply data. */
  327. m->request.data = save_data;
  328. /* Restore the original mach_msg options.
  329. OPTION may have had MACH_RCV_TIMEOUT added,
  330. and/or MACH_SEND_MSG removed. */
  331. option = user_option;
  332. timeout = user_timeout;
  333. /* Now we are ready to repeat the original message send. */
  334. goto message;
  335. }
  336. else
  337. /* The EINTR return indicates cancellation,
  338. so clear the flag. */
  339. ss->cancel = 0;
  340. }
  341. }
  342. break;
  343. default: /* Quiet -Wswitch-enum. */
  344. break;
  345. }
  346. ss->intr_port = MACH_PORT_NULL;
  347. return err;
  348. }
  349. libc_hidden_def (_hurd_intr_rpc_mach_msg)