intr-msg.c 13 KB

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