hurdsig.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  1. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <assert.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <lock-intern.h> /* For `struct mutex'. */
  19. #include <pthreadP.h>
  20. #include <mach.h>
  21. #include <mach/setup-thread.h>
  22. #include <mach/thread_switch.h>
  23. #include <mach/mig_support.h>
  24. #include <mach/vm_param.h>
  25. #include <hurd.h>
  26. #include <hurd/id.h>
  27. #include <hurd/signal.h>
  28. #include "hurdfault.h"
  29. #include "hurdmalloc.h" /* XXX */
  30. #include "../locale/localeinfo.h"
  31. #include <libc-diag.h>
  32. const char *_hurdsig_getenv (const char *);
  33. struct mutex _hurd_siglock;
  34. int _hurd_stopped;
  35. /* Port that receives signals and other miscellaneous messages. */
  36. mach_port_t _hurd_msgport;
  37. /* Thread listening on it. */
  38. thread_t _hurd_msgport_thread;
  39. /* These are set up by _hurdsig_init. */
  40. unsigned long int __hurd_sigthread_stack_base;
  41. unsigned long int __hurd_sigthread_stack_end;
  42. /* Linked-list of per-thread signal state. */
  43. struct hurd_sigstate *_hurd_sigstates;
  44. /* Sigstate for the task-global signals. */
  45. struct hurd_sigstate *_hurd_global_sigstate;
  46. /* Timeout for RPC's after interrupt_operation. */
  47. mach_msg_timeout_t _hurd_interrupted_rpc_timeout = 60000;
  48. static void
  49. default_sigaction (struct sigaction actions[NSIG])
  50. {
  51. int signo;
  52. __sigemptyset (&actions[0].sa_mask);
  53. actions[0].sa_flags = SA_RESTART;
  54. actions[0].sa_handler = SIG_DFL;
  55. for (signo = 1; signo < NSIG; ++signo)
  56. actions[signo] = actions[0];
  57. }
  58. struct hurd_sigstate *
  59. _hurd_thread_sigstate (thread_t thread)
  60. {
  61. struct hurd_sigstate *ss;
  62. __mutex_lock (&_hurd_siglock);
  63. for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
  64. if (ss->thread == thread)
  65. break;
  66. if (ss == NULL)
  67. {
  68. ss = malloc (sizeof (*ss));
  69. if (ss == NULL)
  70. __libc_fatal ("hurd: Can't allocate sigstate\n");
  71. __spin_lock_init (&ss->critical_section_lock);
  72. __spin_lock_init (&ss->lock);
  73. ss->thread = thread;
  74. /* Initialize default state. */
  75. __sigemptyset (&ss->blocked);
  76. __sigemptyset (&ss->pending);
  77. memset (&ss->sigaltstack, 0, sizeof (ss->sigaltstack));
  78. ss->sigaltstack.ss_flags |= SS_DISABLE;
  79. ss->preemptors = NULL;
  80. ss->suspended = MACH_PORT_NULL;
  81. ss->intr_port = MACH_PORT_NULL;
  82. ss->context = NULL;
  83. ss->active_resources = NULL;
  84. ss->cancel = 0;
  85. ss->cancel_hook = NULL;
  86. if (thread == MACH_PORT_NULL)
  87. {
  88. /* Process-wide sigstate, use the system defaults. */
  89. default_sigaction (ss->actions);
  90. /* The global sigstate is not added to the _hurd_sigstates list.
  91. It is created with _hurd_thread_sigstate (MACH_PORT_NULL)
  92. but should be accessed through _hurd_global_sigstate. */
  93. }
  94. else
  95. {
  96. error_t err;
  97. /* Use the global actions as a default for new threads. */
  98. struct hurd_sigstate *s = _hurd_global_sigstate;
  99. if (s)
  100. {
  101. __spin_lock (&s->lock);
  102. memcpy (ss->actions, s->actions, sizeof (s->actions));
  103. __spin_unlock (&s->lock);
  104. }
  105. else
  106. default_sigaction (ss->actions);
  107. ss->next = _hurd_sigstates;
  108. _hurd_sigstates = ss;
  109. err = __mach_port_mod_refs (__mach_task_self (), thread,
  110. MACH_PORT_RIGHT_SEND, 1);
  111. if (err)
  112. __libc_fatal ("hurd: Can't add reference on Mach thread\n");
  113. }
  114. }
  115. __mutex_unlock (&_hurd_siglock);
  116. return ss;
  117. }
  118. libc_hidden_def (_hurd_thread_sigstate)
  119. /* Destroy a sigstate structure. Called by libpthread just before the
  120. * corresponding thread is terminated. */
  121. void
  122. _hurd_sigstate_delete (thread_t thread)
  123. {
  124. struct hurd_sigstate **ssp, *ss;
  125. __mutex_lock (&_hurd_siglock);
  126. for (ssp = &_hurd_sigstates; *ssp; ssp = &(*ssp)->next)
  127. if ((*ssp)->thread == thread)
  128. break;
  129. ss = *ssp;
  130. if (ss)
  131. *ssp = ss->next;
  132. __mutex_unlock (&_hurd_siglock);
  133. if (ss)
  134. {
  135. if (ss->thread != MACH_PORT_NULL)
  136. __mach_port_deallocate (__mach_task_self (), ss->thread);
  137. free (ss);
  138. }
  139. }
  140. libc_hidden_def (_hurd_sigstate_delete)
  141. /* Make SS a global receiver, with pthread signal semantics. */
  142. void
  143. _hurd_sigstate_set_global_rcv (struct hurd_sigstate *ss)
  144. {
  145. assert (ss->thread != MACH_PORT_NULL);
  146. ss->actions[0].sa_handler = SIG_IGN;
  147. }
  148. libc_hidden_def (_hurd_sigstate_set_global_rcv)
  149. /* Retrieve a thread's full set of pending signals, including the global
  150. ones if appropriate. SS must be locked. */
  151. sigset_t
  152. _hurd_sigstate_pending (const struct hurd_sigstate *ss)
  153. {
  154. sigset_t pending = ss->pending;
  155. if (_hurd_sigstate_is_global_rcv (ss))
  156. __sigorset (&pending, &pending, &_hurd_global_sigstate->pending);
  157. return pending;
  158. }
  159. libc_hidden_def (_hurd_sigstate_pending)
  160. /* Clear a pending signal and return the associated detailed
  161. signal information. SS must be locked, and must have signal SIGNO
  162. pending, either directly or through the global sigstate. */
  163. static struct hurd_signal_detail
  164. sigstate_clear_pending (struct hurd_sigstate *ss, int signo)
  165. {
  166. if (_hurd_sigstate_is_global_rcv (ss)
  167. && __sigismember (&_hurd_global_sigstate->pending, signo))
  168. {
  169. __sigdelset (&_hurd_global_sigstate->pending, signo);
  170. return _hurd_global_sigstate->pending_data[signo];
  171. }
  172. assert (__sigismember (&ss->pending, signo));
  173. __sigdelset (&ss->pending, signo);
  174. return ss->pending_data[signo];
  175. }
  176. /* Retrieve a thread's action vector. SS must be locked. */
  177. struct sigaction *
  178. _hurd_sigstate_actions (struct hurd_sigstate *ss)
  179. {
  180. if (_hurd_sigstate_is_global_rcv (ss))
  181. return _hurd_global_sigstate->actions;
  182. else
  183. return ss->actions;
  184. }
  185. /* Signal delivery itself is on this page. */
  186. #include <hurd/fd.h>
  187. #include <hurd/crash.h>
  188. #include <hurd/resource.h>
  189. #include <hurd/paths.h>
  190. #include <setjmp.h>
  191. #include <fcntl.h>
  192. #include <sys/wait.h>
  193. #include <thread_state.h>
  194. #include <hurd/msg_server.h>
  195. #include <hurd/msg_reply.h> /* For __msg_sig_post_reply. */
  196. #include <hurd/interrupt.h>
  197. #include <unistd.h>
  198. /* Call the crash dump server to mummify us before we die.
  199. Returns nonzero if a core file was written. */
  200. static int
  201. write_corefile (int signo, const struct hurd_signal_detail *detail)
  202. {
  203. error_t err;
  204. mach_port_t coreserver;
  205. file_t file, coredir;
  206. const char *name;
  207. /* Don't bother locking since we just read the one word. */
  208. rlim_t corelimit = _hurd_rlimits[RLIMIT_CORE].rlim_cur;
  209. if (corelimit == 0)
  210. /* No core dumping, thank you very much. Note that this makes
  211. `ulimit -c 0' prevent crash-suspension too, which is probably
  212. what the user wanted. */
  213. return 0;
  214. /* XXX RLIMIT_CORE:
  215. When we have a protocol to make the server return an error
  216. for RLIMIT_FSIZE, then tell the corefile fs server the RLIMIT_CORE
  217. value in place of the RLIMIT_FSIZE value. */
  218. /* First get a port to the core dumping server. */
  219. coreserver = MACH_PORT_NULL;
  220. name = _hurdsig_getenv ("CRASHSERVER");
  221. if (name != NULL)
  222. coreserver = __file_name_lookup (name, 0, 0);
  223. if (coreserver == MACH_PORT_NULL)
  224. coreserver = __file_name_lookup (_SERVERS_CRASH, 0, 0);
  225. if (coreserver == MACH_PORT_NULL)
  226. return 0;
  227. /* Get a port to the directory where the new core file will reside. */
  228. file = MACH_PORT_NULL;
  229. name = _hurdsig_getenv ("COREFILE");
  230. if (name == NULL)
  231. name = "core";
  232. coredir = __file_name_split (name, (char **) &name);
  233. if (coredir != MACH_PORT_NULL)
  234. /* Create the new file, but don't link it into the directory yet. */
  235. __dir_mkfile (coredir, O_WRONLY|O_CREAT,
  236. 0600 & ~_hurd_umask, /* XXX ? */
  237. &file);
  238. /* Call the core dumping server to write the core file. */
  239. err = __crash_dump_task (coreserver,
  240. __mach_task_self (),
  241. file,
  242. signo, detail->code, detail->error,
  243. detail->exc, detail->exc_code, detail->exc_subcode,
  244. _hurd_ports[INIT_PORT_CTTYID].port,
  245. MACH_MSG_TYPE_COPY_SEND);
  246. __mach_port_deallocate (__mach_task_self (), coreserver);
  247. if (! err && file != MACH_PORT_NULL)
  248. /* The core dump into FILE succeeded, so now link it into the
  249. directory. */
  250. err = __dir_link (coredir, file, name, 1);
  251. __mach_port_deallocate (__mach_task_self (), file);
  252. __mach_port_deallocate (__mach_task_self (), coredir);
  253. return !err && file != MACH_PORT_NULL;
  254. }
  255. /* The lowest-numbered thread state flavor value is 1,
  256. so we use bit 0 in machine_thread_all_state.set to
  257. record whether we have done thread_abort. */
  258. #define THREAD_ABORTED 1
  259. /* SS->thread is suspended. Abort the thread and get its basic state. */
  260. static void
  261. abort_thread (struct hurd_sigstate *ss, struct machine_thread_all_state *state,
  262. void (*reply) (void))
  263. {
  264. assert (ss->thread != MACH_PORT_NULL);
  265. if (!(state->set & THREAD_ABORTED))
  266. {
  267. error_t err = __thread_abort (ss->thread);
  268. assert_perror (err);
  269. /* Clear all thread state flavor set bits, because thread_abort may
  270. have changed the state. */
  271. state->set = THREAD_ABORTED;
  272. }
  273. if (reply)
  274. (*reply) ();
  275. machine_get_basic_state (ss->thread, state);
  276. }
  277. /* Find the location of the MiG reply port cell in use by the thread whose
  278. state is described by THREAD_STATE. If SIGTHREAD is nonzero, make sure
  279. that this location can be set without faulting, or else return NULL. */
  280. static mach_port_t *
  281. interrupted_reply_port_location (thread_t thread,
  282. struct machine_thread_all_state *thread_state,
  283. int sigthread)
  284. {
  285. mach_port_t *portloc = &THREAD_TCB(thread, thread_state)->reply_port;
  286. if (sigthread && _hurdsig_catch_memory_fault (portloc))
  287. /* Faulted trying to read the TCB. */
  288. return NULL;
  289. DIAG_PUSH_NEEDS_COMMENT;
  290. /* GCC 6 and before seem to be confused by the setjmp call inside
  291. _hurdsig_catch_memory_fault and think that we may be returning a second
  292. time to here with portloc uninitialized (but we never do). */
  293. DIAG_IGNORE_NEEDS_COMMENT_GCC (6, "-Wmaybe-uninitialized");
  294. /* Fault now if this pointer is bogus. */
  295. *(volatile mach_port_t *) portloc = *portloc;
  296. DIAG_POP_NEEDS_COMMENT;
  297. if (sigthread)
  298. _hurdsig_end_catch_fault ();
  299. return portloc;
  300. }
  301. #include <hurd/sigpreempt.h>
  302. #include <intr-msg.h>
  303. /* Timeout on interrupt_operation calls. */
  304. mach_msg_timeout_t _hurdsig_interrupt_timeout = 1000;
  305. /* SS->thread is suspended.
  306. Abort any interruptible RPC operation the thread is doing.
  307. This uses only the constant member SS->thread and the unlocked, atomically
  308. set member SS->intr_port, so no locking is needed.
  309. If successfully sent an interrupt_operation and therefore the thread should
  310. wait for its pending RPC to return (possibly EINTR) before taking the
  311. incoming signal, returns the reply port to be received on. Otherwise
  312. returns MACH_PORT_NULL.
  313. SIGNO is used to find the applicable SA_RESTART bit. If SIGNO is zero,
  314. the RPC fails with EINTR instead of restarting (thread_cancel).
  315. *STATE_CHANGE is set nonzero if STATE->basic was modified and should
  316. be applied back to the thread if it might ever run again, else zero. */
  317. mach_port_t
  318. _hurdsig_abort_rpcs (struct hurd_sigstate *ss, int signo, int sigthread,
  319. struct machine_thread_all_state *state, int *state_change,
  320. void (*reply) (void))
  321. {
  322. extern const void _hurd_intr_rpc_msg_about_to attribute_hidden;
  323. extern const void _hurd_intr_rpc_msg_setup_done attribute_hidden;
  324. extern const void _hurd_intr_rpc_msg_in_trap attribute_hidden;
  325. mach_port_t rcv_port = MACH_PORT_NULL;
  326. mach_port_t intr_port;
  327. *state_change = 0;
  328. intr_port = ss->intr_port;
  329. if (intr_port == MACH_PORT_NULL)
  330. /* No interruption needs done. */
  331. return MACH_PORT_NULL;
  332. /* Abort the thread's kernel context, so any pending message send or
  333. receive completes immediately or aborts. */
  334. abort_thread (ss, state, reply);
  335. if (state->basic.PC >= (uintptr_t) &_hurd_intr_rpc_msg_about_to
  336. && state->basic.PC < (uintptr_t) &_hurd_intr_rpc_msg_in_trap)
  337. {
  338. /* The thread is about to do the RPC, but hasn't yet entered
  339. mach_msg. Importantly, it may have already checked ss->cancel for
  340. the last time before doing the RPC, so setting that is not enough
  341. to make it not enter mach_msg. Instead, mutate the thread's state
  342. so it knows not to try the RPC.
  343. If the thread is past _hurd_intr_rpc_msg_setup_done, just make it
  344. jump to after the trap, since we know it's safe to do so. Otherwise,
  345. we know that the thread is yet to check for the MACH_SEND_INTERRUPTED
  346. value we set below, and will skip the trap by itself. */
  347. if (state->basic.PC >= (uintptr_t) &_hurd_intr_rpc_msg_setup_done)
  348. MACHINE_THREAD_STATE_SET_PC (&state->basic,
  349. &_hurd_intr_rpc_msg_in_trap);
  350. state->basic.SYSRETURN = MACH_SEND_INTERRUPTED;
  351. *state_change = 1;
  352. }
  353. else if (state->basic.PC == (uintptr_t) &_hurd_intr_rpc_msg_in_trap
  354. /* The thread was blocked in the system call. After thread_abort,
  355. the return value register indicates what state the RPC was in
  356. when interrupted. */
  357. && state->basic.SYSRETURN == MACH_RCV_INTERRUPTED)
  358. {
  359. /* The RPC request message was sent and the thread was waiting for the
  360. reply message; now the message receive has been aborted, so the
  361. mach_msg call will return MACH_RCV_INTERRUPTED. We must tell the
  362. server to interrupt the pending operation. The thread must wait for
  363. the reply message before running the signal handler (to guarantee that
  364. the operation has finished being interrupted), so our nonzero return
  365. tells the trampoline code to finish the message receive operation
  366. before running the handler. */
  367. mach_port_t *reply = interrupted_reply_port_location (ss->thread,
  368. state,
  369. sigthread);
  370. error_t err = __interrupt_operation (intr_port,
  371. _hurdsig_interrupt_timeout);
  372. if (err)
  373. {
  374. if (reply)
  375. {
  376. /* The interrupt didn't work.
  377. Destroy the receive right the thread is blocked on, and
  378. replace it with a dead name to keep the name from reuse until
  379. the therad is done with it. To do this atomically, first
  380. insert a send right, and then destroy the receive right,
  381. turning the send right into a dead name. */
  382. err = __mach_port_insert_right (__mach_task_self (),
  383. *reply, *reply,
  384. MACH_MSG_TYPE_MAKE_SEND);
  385. assert_perror (err);
  386. err = __mach_port_mod_refs (__mach_task_self (), *reply,
  387. MACH_PORT_RIGHT_RECEIVE, -1);
  388. assert_perror (err);
  389. }
  390. /* The system call return value register now contains
  391. MACH_RCV_INTERRUPTED; when mach_msg resumes, it will retry the
  392. call. Since we have just destroyed the receive right, the retry
  393. will fail with MACH_RCV_INVALID_NAME. Instead, just change the
  394. return value here to EINTR so mach_msg will not retry and the
  395. EINTR error code will propagate up. */
  396. state->basic.SYSRETURN = EINTR;
  397. *state_change = 1;
  398. }
  399. else if (reply)
  400. rcv_port = *reply;
  401. /* All threads whose RPCs were interrupted by the interrupt_operation
  402. call above will retry their RPCs unless we clear SS->intr_port. So we
  403. clear it for the thread taking a signal when SA_RESTART is clear, so
  404. that its call returns EINTR. */
  405. if (! signo || !(_hurd_sigstate_actions (ss) [signo].sa_flags & SA_RESTART))
  406. ss->intr_port = MACH_PORT_NULL;
  407. }
  408. return rcv_port;
  409. }
  410. /* Abort the RPCs being run by all threads but this one;
  411. all other threads should be suspended. If LIVE is nonzero, those
  412. threads may run again, so they should be adjusted as necessary to be
  413. happy when resumed. STATE is clobbered as a scratch area; its initial
  414. contents are ignored, and its contents on return are not useful. */
  415. static void
  416. abort_all_rpcs (int signo, struct machine_thread_all_state *state, int live)
  417. {
  418. /* We can just loop over the sigstates. Any thread doing something
  419. interruptible must have one. We needn't bother locking because all
  420. other threads are stopped. */
  421. struct hurd_sigstate *ss;
  422. size_t nthreads;
  423. mach_port_t *reply_ports;
  424. /* First loop over the sigstates to count them.
  425. We need to know how big a vector we will need for REPLY_PORTS. */
  426. nthreads = 0;
  427. for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
  428. ++nthreads;
  429. reply_ports = alloca (nthreads * sizeof *reply_ports);
  430. nthreads = 0;
  431. for (ss = _hurd_sigstates; ss != NULL; ss = ss->next, ++nthreads)
  432. if (ss->thread == _hurd_msgport_thread)
  433. reply_ports[nthreads] = MACH_PORT_NULL;
  434. else
  435. {
  436. int state_changed;
  437. state->set = 0; /* Reset scratch area. */
  438. /* Abort any operation in progress with interrupt_operation.
  439. Record the reply port the thread is waiting on.
  440. We will wait for all the replies below. */
  441. reply_ports[nthreads] = _hurdsig_abort_rpcs (ss, signo, 1,
  442. state, &state_changed,
  443. NULL);
  444. if (live)
  445. {
  446. if (reply_ports[nthreads] != MACH_PORT_NULL)
  447. {
  448. /* We will wait for the reply to this RPC below, so the
  449. thread must issue a new RPC rather than waiting for the
  450. reply to the one it sent. */
  451. state->basic.SYSRETURN = EINTR;
  452. state_changed = 1;
  453. }
  454. if (state_changed)
  455. /* Aborting the RPC needed to change this thread's state,
  456. and it might ever run again. So write back its state. */
  457. __thread_set_state (ss->thread, MACHINE_THREAD_STATE_FLAVOR,
  458. (natural_t *) &state->basic,
  459. MACHINE_THREAD_STATE_COUNT);
  460. }
  461. }
  462. /* Wait for replies from all the successfully interrupted RPCs. */
  463. while (nthreads-- > 0)
  464. if (reply_ports[nthreads] != MACH_PORT_NULL)
  465. {
  466. error_t err;
  467. mach_msg_header_t head;
  468. err = __mach_msg (&head, MACH_RCV_MSG|MACH_RCV_TIMEOUT, 0, sizeof head,
  469. reply_ports[nthreads],
  470. _hurd_interrupted_rpc_timeout, MACH_PORT_NULL);
  471. switch (err)
  472. {
  473. case MACH_RCV_TIMED_OUT:
  474. case MACH_RCV_TOO_LARGE:
  475. break;
  476. default:
  477. assert_perror (err);
  478. }
  479. }
  480. }
  481. /* Wake up any sigsuspend or pselect call that is blocking SS->thread. SS must
  482. be locked. */
  483. static void
  484. wake_sigsuspend (struct hurd_sigstate *ss)
  485. {
  486. error_t err;
  487. mach_msg_header_t msg;
  488. if (ss->suspended == MACH_PORT_NULL)
  489. return;
  490. /* There is a sigsuspend waiting. Tell it to wake up. */
  491. msg.msgh_bits = MACH_MSGH_BITS (MACH_MSG_TYPE_MAKE_SEND, 0);
  492. msg.msgh_remote_port = ss->suspended;
  493. msg.msgh_local_port = MACH_PORT_NULL;
  494. /* These values do not matter. */
  495. msg.msgh_id = 8675309; /* Jenny, Jenny. */
  496. ss->suspended = MACH_PORT_NULL;
  497. err = __mach_msg (&msg, MACH_SEND_MSG, sizeof msg, 0,
  498. MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE,
  499. MACH_PORT_NULL);
  500. assert_perror (err);
  501. }
  502. struct hurd_signal_preemptor *_hurdsig_preemptors = 0;
  503. sigset_t _hurdsig_preempted_set;
  504. /* XXX temporary to deal with spelling fix */
  505. weak_alias (_hurdsig_preemptors, _hurdsig_preempters)
  506. /* Mask of stop signals. */
  507. #define STOPSIGS (__sigmask (SIGTTIN) | __sigmask (SIGTTOU) \
  508. | __sigmask (SIGSTOP) | __sigmask (SIGTSTP))
  509. /* Actual delivery of a single signal. Called with SS unlocked. When
  510. the signal is delivered, return SS, locked (or, if SS was originally
  511. _hurd_global_sigstate, the sigstate of the actual thread the signal
  512. was delivered to). If the signal is being traced, return NULL with
  513. SS unlocked. */
  514. static struct hurd_sigstate *
  515. post_signal (struct hurd_sigstate *ss,
  516. int signo, struct hurd_signal_detail *detail,
  517. int untraced, void (*reply) (void))
  518. {
  519. struct machine_thread_all_state thread_state;
  520. enum { stop, ignore, core, term, handle } act;
  521. int ss_suspended;
  522. /* sigaction for preemptors */
  523. struct sigaction preempt_sigaction = {
  524. .sa_flags = SA_RESTART
  525. };
  526. struct sigaction *action;
  527. /* Mark the signal as pending. */
  528. void mark_pending (void)
  529. {
  530. __sigaddset (&ss->pending, signo);
  531. /* Save the details to be given to the handler when SIGNO is
  532. unblocked. */
  533. ss->pending_data[signo] = *detail;
  534. }
  535. /* Suspend the process with SIGNO. */
  536. void suspend (void)
  537. {
  538. /* Stop all other threads and mark ourselves stopped. */
  539. __USEPORT (PROC,
  540. ({
  541. /* Hold the siglock while stopping other threads to be
  542. sure it is not held by another thread afterwards. */
  543. __mutex_lock (&_hurd_siglock);
  544. __proc_dostop (port, _hurd_msgport_thread);
  545. __mutex_unlock (&_hurd_siglock);
  546. abort_all_rpcs (signo, &thread_state, 1);
  547. reply ();
  548. __proc_mark_stop (port, signo, detail->code);
  549. }));
  550. _hurd_stopped = 1;
  551. }
  552. /* Resume the process after a suspension. */
  553. void resume (void)
  554. {
  555. /* Resume the process from being stopped. */
  556. thread_t *threads;
  557. mach_msg_type_number_t nthreads, i;
  558. error_t err;
  559. if (! _hurd_stopped)
  560. return;
  561. /* Tell the proc server we are continuing. */
  562. __USEPORT (PROC, __proc_mark_cont (port));
  563. /* Fetch ports to all our threads and resume them. */
  564. err = __task_threads (__mach_task_self (), &threads, &nthreads);
  565. assert_perror (err);
  566. for (i = 0; i < nthreads; ++i)
  567. {
  568. if (act == handle && threads[i] == ss->thread)
  569. {
  570. /* The thread that will run the handler is kept suspended. */
  571. ss_suspended = 1;
  572. }
  573. else if (threads[i] != _hurd_msgport_thread)
  574. {
  575. err = __thread_resume (threads[i]);
  576. assert_perror (err);
  577. }
  578. err = __mach_port_deallocate (__mach_task_self (),
  579. threads[i]);
  580. assert_perror (err);
  581. }
  582. __vm_deallocate (__mach_task_self (),
  583. (vm_address_t) threads,
  584. nthreads * sizeof *threads);
  585. _hurd_stopped = 0;
  586. }
  587. error_t err;
  588. sighandler_t handler;
  589. if (signo == 0)
  590. {
  591. if (untraced)
  592. {
  593. /* This is PTRACE_CONTINUE. */
  594. act = ignore;
  595. resume ();
  596. }
  597. /* This call is just to check for pending signals. */
  598. _hurd_sigstate_lock (ss);
  599. return ss;
  600. }
  601. thread_state.set = 0; /* We know nothing. */
  602. _hurd_sigstate_lock (ss);
  603. /* If this is a global signal, try to find a thread ready to accept
  604. it right away. This is especially important for untraced signals,
  605. since going through the global pending mask would de-untrace them. */
  606. if (ss->thread == MACH_PORT_NULL)
  607. {
  608. struct hurd_sigstate *rss;
  609. __mutex_lock (&_hurd_siglock);
  610. for (rss = _hurd_sigstates; rss != NULL; rss = rss->next)
  611. {
  612. if (! _hurd_sigstate_is_global_rcv (rss))
  613. continue;
  614. /* The global sigstate is already locked. */
  615. __spin_lock (&rss->lock);
  616. if (! __sigismember (&rss->blocked, signo))
  617. {
  618. ss = rss;
  619. break;
  620. }
  621. __spin_unlock (&rss->lock);
  622. }
  623. __mutex_unlock (&_hurd_siglock);
  624. }
  625. /* We want the preemptors to be able to update the blocking mask
  626. without affecting the delivery of this signal, so we save the
  627. current value to test against later. */
  628. sigset_t blocked = ss->blocked;
  629. /* Check for a preempted signal. Preempted signals can arrive during
  630. critical sections. */
  631. {
  632. inline sighandler_t try_preemptor (struct hurd_signal_preemptor *pe)
  633. { /* PE cannot be null. */
  634. do
  635. {
  636. if (HURD_PREEMPT_SIGNAL_P (pe, signo, detail->exc_subcode))
  637. {
  638. if (pe->preemptor)
  639. {
  640. sighandler_t handler = (*pe->preemptor) (pe, ss,
  641. &signo, detail);
  642. if (handler != SIG_ERR)
  643. return handler;
  644. }
  645. else
  646. return pe->handler;
  647. }
  648. pe = pe->next;
  649. } while (pe != 0);
  650. return SIG_ERR;
  651. }
  652. handler = ss->preemptors ? try_preemptor (ss->preemptors) : SIG_ERR;
  653. /* If no thread-specific preemptor, check for a global one. */
  654. if (handler == SIG_ERR && __sigismember (&_hurdsig_preempted_set, signo))
  655. {
  656. __mutex_lock (&_hurd_siglock);
  657. handler = try_preemptor (_hurdsig_preemptors);
  658. __mutex_unlock (&_hurd_siglock);
  659. }
  660. }
  661. ss_suspended = 0;
  662. if (handler == SIG_IGN)
  663. /* Ignore the signal altogether. */
  664. act = ignore;
  665. else if (handler != SIG_ERR)
  666. {
  667. /* Run the preemption-provided handler. */
  668. action = &preempt_sigaction;
  669. act = handle;
  670. }
  671. else
  672. {
  673. /* No preemption. Do normal handling. */
  674. action = & _hurd_sigstate_actions (ss) [signo];
  675. if (!untraced && __sigismember (&_hurdsig_traced, signo))
  676. {
  677. /* We are being traced. Stop to tell the debugger of the signal. */
  678. if (_hurd_stopped)
  679. /* Already stopped. Mark the signal as pending;
  680. when resumed, we will notice it and stop again. */
  681. mark_pending ();
  682. else
  683. suspend ();
  684. _hurd_sigstate_unlock (ss);
  685. reply ();
  686. return NULL;
  687. }
  688. handler = action->sa_handler;
  689. if (handler == SIG_DFL)
  690. /* Figure out the default action for this signal. */
  691. switch (signo)
  692. {
  693. case 0:
  694. /* A sig_post msg with SIGNO==0 is sent to
  695. tell us to check for pending signals. */
  696. act = ignore;
  697. break;
  698. case SIGTTIN:
  699. case SIGTTOU:
  700. case SIGSTOP:
  701. case SIGTSTP:
  702. act = stop;
  703. break;
  704. case SIGCONT:
  705. case SIGIO:
  706. case SIGURG:
  707. case SIGCHLD:
  708. case SIGWINCH:
  709. act = ignore;
  710. break;
  711. case SIGQUIT:
  712. case SIGILL:
  713. case SIGTRAP:
  714. case SIGIOT:
  715. case SIGEMT:
  716. case SIGFPE:
  717. case SIGBUS:
  718. case SIGSEGV:
  719. case SIGSYS:
  720. act = core;
  721. break;
  722. case SIGINFO:
  723. if (_hurd_pgrp == _hurd_pid)
  724. {
  725. /* We are the process group leader. Since there is no
  726. user-specified handler for SIGINFO, we use a default one
  727. which prints something interesting. We use the normal
  728. handler mechanism instead of just doing it here to avoid
  729. the signal thread faulting or blocking in this
  730. potentially hairy operation. */
  731. act = handle;
  732. handler = _hurd_siginfo_handler;
  733. }
  734. else
  735. act = ignore;
  736. break;
  737. default:
  738. act = term;
  739. break;
  740. }
  741. else if (handler == SIG_IGN)
  742. act = ignore;
  743. else
  744. act = handle;
  745. if (__sigmask (signo) & STOPSIGS)
  746. /* Stop signals clear a pending SIGCONT even if they
  747. are handled or ignored (but not if preempted). */
  748. __sigdelset (&ss->pending, SIGCONT);
  749. else
  750. {
  751. if (signo == SIGCONT)
  752. /* Even if handled or ignored (but not preempted), SIGCONT clears
  753. stop signals and resumes the process. */
  754. ss->pending &= ~STOPSIGS;
  755. if (_hurd_stopped && act != stop && (untraced || signo == SIGCONT))
  756. resume ();
  757. }
  758. }
  759. if (_hurd_orphaned && act == stop
  760. && (__sigmask (signo) & (__sigmask (SIGTTIN) | __sigmask (SIGTTOU)
  761. | __sigmask (SIGTSTP))))
  762. {
  763. /* If we would ordinarily stop for a job control signal, but we are
  764. orphaned so no one would ever notice and continue us again, we just
  765. quietly die, alone and in the dark. */
  766. detail->code = signo;
  767. signo = SIGKILL;
  768. act = term;
  769. }
  770. /* Handle receipt of a blocked signal, or any signal while stopped. */
  771. if (__sigismember (&blocked, signo) || (signo != SIGKILL && _hurd_stopped))
  772. {
  773. mark_pending ();
  774. act = ignore;
  775. }
  776. /* Perform the chosen action for the signal. */
  777. switch (act)
  778. {
  779. case stop:
  780. if (_hurd_stopped)
  781. {
  782. /* We are already stopped, but receiving an untraced stop
  783. signal. Instead of resuming and suspending again, just
  784. notify the proc server of the new stop signal. */
  785. error_t err = __USEPORT (PROC, __proc_mark_stop
  786. (port, signo, detail->code));
  787. assert_perror (err);
  788. }
  789. else
  790. /* Suspend the process. */
  791. suspend ();
  792. break;
  793. case ignore:
  794. if (detail->exc)
  795. /* Blocking or ignoring a machine exception is fatal.
  796. Otherwise we could just spin on the faulting instruction. */
  797. goto fatal;
  798. /* Nobody cares about this signal. If there was a call to resume
  799. above in SIGCONT processing and we've left a thread suspended,
  800. now's the time to set it going. */
  801. if (ss_suspended)
  802. {
  803. assert (ss->thread != MACH_PORT_NULL);
  804. err = __thread_resume (ss->thread);
  805. assert_perror (err);
  806. ss_suspended = 0;
  807. }
  808. break;
  809. sigbomb:
  810. /* We got a fault setting up the stack frame for the handler.
  811. Nothing to do but die; BSD gets SIGILL in this case. */
  812. detail->code = signo; /* XXX ? */
  813. signo = SIGILL;
  814. fatal:
  815. act = core;
  816. /* FALLTHROUGH */
  817. case term: /* Time to die. */
  818. case core: /* And leave a rotting corpse. */
  819. /* Have the proc server stop all other threads in our task. */
  820. err = __USEPORT (PROC, __proc_dostop (port, _hurd_msgport_thread));
  821. assert_perror (err);
  822. /* No more user instructions will be executed.
  823. The signal can now be considered delivered. */
  824. reply ();
  825. /* Abort all server operations now in progress. */
  826. abort_all_rpcs (signo, &thread_state, 0);
  827. {
  828. int status = W_EXITCODE (0, signo);
  829. /* Do a core dump if desired. Only set the wait status bit saying we
  830. in fact dumped core if the operation was actually successful. */
  831. if (act == core && write_corefile (signo, detail))
  832. status |= WCOREFLAG;
  833. /* Tell proc how we died and then stick the saber in the gut. */
  834. _hurd_exit (status);
  835. /* NOTREACHED */
  836. }
  837. case handle:
  838. /* Call a handler for this signal. */
  839. {
  840. struct sigcontext *scp, ocontext;
  841. int wait_for_reply, state_changed;
  842. assert (ss->thread != MACH_PORT_NULL);
  843. /* Stop the thread and abort its pending RPC operations. */
  844. if (! ss_suspended)
  845. {
  846. err = __thread_suspend (ss->thread);
  847. assert_perror (err);
  848. }
  849. /* Abort the thread's kernel context, so any pending message send
  850. or receive completes immediately or aborts. If an interruptible
  851. RPC is in progress, abort_rpcs will do this. But we must always
  852. do it before fetching the thread's state, because
  853. thread_get_state is never kosher before thread_abort. */
  854. abort_thread (ss, &thread_state, NULL);
  855. if (ss->context)
  856. {
  857. /* We have a previous sigcontext that sigreturn was about
  858. to restore when another signal arrived. */
  859. mach_port_t *loc;
  860. if (_hurdsig_catch_memory_fault (ss->context))
  861. {
  862. /* We faulted reading the thread's stack. Forget that
  863. context and pretend it wasn't there. It almost
  864. certainly crash if this handler returns, but that's it's
  865. problem. */
  866. ss->context = NULL;
  867. }
  868. else
  869. {
  870. /* Copy the context from the thread's stack before
  871. we start diddling the stack to set up the handler. */
  872. ocontext = *ss->context;
  873. ss->context = &ocontext;
  874. }
  875. _hurdsig_end_catch_fault ();
  876. if (! machine_get_basic_state (ss->thread, &thread_state))
  877. goto sigbomb;
  878. loc = interrupted_reply_port_location (ss->thread,
  879. &thread_state, 1);
  880. if (loc && *loc != MACH_PORT_NULL)
  881. /* This is the reply port for the context which called
  882. sigreturn. Since we are abandoning that context entirely
  883. and restoring SS->context instead, destroy this port. */
  884. __mach_port_destroy (__mach_task_self (), *loc);
  885. /* The thread was in sigreturn, not in any interruptible RPC. */
  886. wait_for_reply = 0;
  887. assert (! __spin_lock_locked (&ss->critical_section_lock));
  888. }
  889. else
  890. {
  891. int crit = __spin_lock_locked (&ss->critical_section_lock);
  892. wait_for_reply
  893. = (_hurdsig_abort_rpcs (ss,
  894. /* In a critical section, any RPC
  895. should be cancelled instead of
  896. restarted, regardless of
  897. SA_RESTART, so the entire
  898. "atomic" operation can be aborted
  899. as a unit. */
  900. crit ? 0 : signo, 1,
  901. &thread_state, &state_changed,
  902. reply)
  903. != MACH_PORT_NULL);
  904. if (crit)
  905. {
  906. /* The thread is in a critical section. Mark the signal as
  907. pending. When it finishes the critical section, it will
  908. check for pending signals. */
  909. mark_pending ();
  910. if (state_changed)
  911. /* Some cases of interrupting an RPC must change the
  912. thread state to back out the call. Normally this
  913. change is rolled into the warping to the handler and
  914. sigreturn, but we are not running the handler now
  915. because the thread is in a critical section. Instead,
  916. mutate the thread right away for the RPC interruption
  917. and resume it; the RPC will return early so the
  918. critical section can end soon. */
  919. __thread_set_state (ss->thread, MACHINE_THREAD_STATE_FLAVOR,
  920. (natural_t *) &thread_state.basic,
  921. MACHINE_THREAD_STATE_COUNT);
  922. /* */
  923. ss->intr_port = MACH_PORT_NULL;
  924. __thread_resume (ss->thread);
  925. break;
  926. }
  927. }
  928. /* Call the machine-dependent function to set the thread up
  929. to run the signal handler, and preserve its old context. */
  930. scp = _hurd_setup_sighandler (ss, action, handler, signo, detail,
  931. wait_for_reply, &thread_state);
  932. if (scp == NULL)
  933. goto sigbomb;
  934. /* Set the machine-independent parts of the signal context. */
  935. {
  936. /* Fetch the thread variable for the MiG reply port,
  937. and set it to MACH_PORT_NULL. */
  938. mach_port_t *loc = interrupted_reply_port_location (ss->thread,
  939. &thread_state,
  940. 1);
  941. if (loc)
  942. {
  943. scp->sc_reply_port = *loc;
  944. *loc = MACH_PORT_NULL;
  945. }
  946. else
  947. scp->sc_reply_port = MACH_PORT_NULL;
  948. /* Save the intr_port in use by the interrupted code,
  949. and clear the cell before running the trampoline. */
  950. scp->sc_intr_port = ss->intr_port;
  951. ss->intr_port = MACH_PORT_NULL;
  952. if (ss->context)
  953. {
  954. /* After the handler runs we will restore to the state in
  955. SS->context, not the state of the thread now. So restore
  956. that context's reply port and intr port. */
  957. scp->sc_reply_port = ss->context->sc_reply_port;
  958. scp->sc_intr_port = ss->context->sc_intr_port;
  959. ss->context = NULL;
  960. }
  961. }
  962. /* Backdoor extra argument to signal handler. */
  963. scp->sc_error = detail->error;
  964. /* Block requested signals while running the handler. */
  965. scp->sc_mask = ss->blocked;
  966. __sigorset (&ss->blocked, &ss->blocked, &action->sa_mask);
  967. /* Also block SIGNO unless we're asked not to. */
  968. if (! (action->sa_flags & (SA_RESETHAND | SA_NODEFER)))
  969. __sigaddset (&ss->blocked, signo);
  970. /* Reset to SIG_DFL if requested. SIGILL and SIGTRAP cannot
  971. be automatically reset when delivered; the system silently
  972. enforces this restriction. */
  973. if (action->sa_flags & SA_RESETHAND
  974. && signo != SIGILL && signo != SIGTRAP)
  975. action->sa_handler = SIG_DFL;
  976. /* Any sigsuspend call must return after the handler does. */
  977. wake_sigsuspend (ss);
  978. /* Start the thread running the handler (or possibly waiting for an
  979. RPC reply before running the handler). */
  980. err = __thread_set_state (ss->thread, MACHINE_THREAD_STATE_FLAVOR,
  981. (natural_t *) &thread_state.basic,
  982. MACHINE_THREAD_STATE_COUNT);
  983. assert_perror (err);
  984. err = __thread_resume (ss->thread);
  985. assert_perror (err);
  986. thread_state.set = 0; /* Everything we know is now wrong. */
  987. break;
  988. }
  989. }
  990. return ss;
  991. }
  992. /* Return the set of pending signals in SS which should be delivered. */
  993. static sigset_t
  994. pending_signals (struct hurd_sigstate *ss)
  995. {
  996. /* We don't worry about any pending signals if we are stopped, nor if
  997. SS is in a critical section. We are guaranteed to get a sig_post
  998. message before any of them become deliverable: either the SIGCONT
  999. signal, or a sig_post with SIGNO==0 as an explicit poll when the
  1000. thread finishes its critical section. */
  1001. if (_hurd_stopped || __spin_lock_locked (&ss->critical_section_lock))
  1002. return 0;
  1003. return _hurd_sigstate_pending (ss) & ~ss->blocked;
  1004. }
  1005. /* Post the specified pending signals in SS and return 1. If one of
  1006. them is traced, abort immediately and return 0. SS must be locked on
  1007. entry and will be unlocked in all cases. */
  1008. static int
  1009. post_pending (struct hurd_sigstate *ss, sigset_t pending, void (*reply) (void))
  1010. {
  1011. int signo;
  1012. struct hurd_signal_detail detail;
  1013. /* Make sure SS corresponds to an actual thread, since we assume it won't
  1014. change in post_signal. */
  1015. assert (ss->thread != MACH_PORT_NULL);
  1016. for (signo = 1; signo < NSIG; ++signo)
  1017. if (__sigismember (&pending, signo))
  1018. {
  1019. detail = sigstate_clear_pending (ss, signo);
  1020. _hurd_sigstate_unlock (ss);
  1021. /* Will reacquire the lock, except if the signal is traced. */
  1022. if (! post_signal (ss, signo, &detail, 0, reply))
  1023. return 0;
  1024. }
  1025. /* No more signals pending; SS->lock is still locked. */
  1026. _hurd_sigstate_unlock (ss);
  1027. return 1;
  1028. }
  1029. /* Post all the pending signals of all threads and return 1. If a traced
  1030. signal is encountered, abort immediately and return 0. */
  1031. static int
  1032. post_all_pending_signals (void (*reply) (void))
  1033. {
  1034. struct hurd_sigstate *ss;
  1035. sigset_t pending = 0;
  1036. for (;;)
  1037. {
  1038. __mutex_lock (&_hurd_siglock);
  1039. for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
  1040. {
  1041. _hurd_sigstate_lock (ss);
  1042. pending = pending_signals (ss);
  1043. if (pending)
  1044. /* post_pending() below will unlock SS. */
  1045. break;
  1046. _hurd_sigstate_unlock (ss);
  1047. }
  1048. __mutex_unlock (&_hurd_siglock);
  1049. if (! pending)
  1050. return 1;
  1051. if (! post_pending (ss, pending, reply))
  1052. return 0;
  1053. }
  1054. }
  1055. /* Deliver a signal. SS is not locked. */
  1056. void
  1057. _hurd_internal_post_signal (struct hurd_sigstate *ss,
  1058. int signo, struct hurd_signal_detail *detail,
  1059. mach_port_t reply_port,
  1060. mach_msg_type_name_t reply_port_type,
  1061. int untraced)
  1062. {
  1063. /* Reply to this sig_post message. */
  1064. __typeof (__msg_sig_post_reply) *reply_rpc
  1065. = (untraced ? __msg_sig_post_untraced_reply : __msg_sig_post_reply);
  1066. void reply (void)
  1067. {
  1068. error_t err;
  1069. if (reply_port == MACH_PORT_NULL)
  1070. return;
  1071. err = (*reply_rpc) (reply_port, reply_port_type, 0);
  1072. reply_port = MACH_PORT_NULL;
  1073. if (err != MACH_SEND_INVALID_DEST) /* Ignore dead reply port. */
  1074. assert_perror (err);
  1075. }
  1076. ss = post_signal (ss, signo, detail, untraced, reply);
  1077. if (! ss)
  1078. return;
  1079. /* The signal was neither fatal nor traced. We still hold SS->lock. */
  1080. if (signo != 0 && ss->thread != MACH_PORT_NULL)
  1081. {
  1082. /* The signal has either been ignored or is now being handled. We can
  1083. consider it delivered and reply to the killer. */
  1084. reply ();
  1085. /* Post any pending signals for this thread. */
  1086. if (! post_pending (ss, pending_signals (ss), reply))
  1087. return;
  1088. }
  1089. else
  1090. {
  1091. /* If this was a process-wide signal or a poll request, we need
  1092. to check for pending signals for all threads. */
  1093. _hurd_sigstate_unlock (ss);
  1094. if (! post_all_pending_signals (reply))
  1095. return;
  1096. /* All pending signals delivered to all threads.
  1097. Now we can send the reply message even for signal 0. */
  1098. reply ();
  1099. }
  1100. }
  1101. /* Decide whether REFPORT enables the sender to send us a SIGNO signal.
  1102. Returns zero if so, otherwise the error code to return to the sender. */
  1103. static error_t
  1104. signal_allowed (int signo, mach_port_t refport)
  1105. {
  1106. if (signo < 0 || signo >= NSIG)
  1107. return EINVAL;
  1108. if (refport == __mach_task_self ())
  1109. /* Can send any signal. */
  1110. goto win;
  1111. /* Avoid needing to check for this below. */
  1112. if (refport == MACH_PORT_NULL)
  1113. return EPERM;
  1114. switch (signo)
  1115. {
  1116. case SIGINT:
  1117. case SIGQUIT:
  1118. case SIGTSTP:
  1119. case SIGHUP:
  1120. case SIGINFO:
  1121. case SIGTTIN:
  1122. case SIGTTOU:
  1123. case SIGWINCH:
  1124. /* Job control signals can be sent by the controlling terminal. */
  1125. if (__USEPORT (CTTYID, port == refport))
  1126. goto win;
  1127. break;
  1128. case SIGCONT:
  1129. {
  1130. /* A continue signal can be sent by anyone in the session. */
  1131. mach_port_t sessport;
  1132. if (! __USEPORT (PROC, __proc_getsidport (port, &sessport)))
  1133. {
  1134. __mach_port_deallocate (__mach_task_self (), sessport);
  1135. if (refport == sessport)
  1136. goto win;
  1137. }
  1138. }
  1139. break;
  1140. case SIGIO:
  1141. case SIGURG:
  1142. {
  1143. /* Any io object a file descriptor refers to might send us
  1144. one of these signals using its async ID port for REFPORT.
  1145. This is pretty wide open; it is not unlikely that some random
  1146. process can at least open for reading something we have open,
  1147. get its async ID port, and send us a spurious SIGIO or SIGURG
  1148. signal. But BSD is actually wider open than that!--you can set
  1149. the owner of an io object to any process or process group
  1150. whatsoever and send them gratuitous signals.
  1151. Someday we could implement some reasonable scheme for
  1152. authorizing SIGIO and SIGURG signals properly. */
  1153. int d;
  1154. int lucky = 0; /* True if we find a match for REFPORT. */
  1155. __mutex_lock (&_hurd_dtable_lock);
  1156. for (d = 0; !lucky && (unsigned) d < (unsigned) _hurd_dtablesize; ++d)
  1157. {
  1158. struct hurd_userlink ulink;
  1159. io_t port;
  1160. mach_port_t asyncid;
  1161. if (_hurd_dtable[d] == NULL)
  1162. continue;
  1163. port = _hurd_port_get (&_hurd_dtable[d]->port, &ulink);
  1164. if (! __io_get_icky_async_id (port, &asyncid))
  1165. {
  1166. if (refport == asyncid)
  1167. /* Break out of the loop on the next iteration. */
  1168. lucky = 1;
  1169. __mach_port_deallocate (__mach_task_self (), asyncid);
  1170. }
  1171. _hurd_port_free (&_hurd_dtable[d]->port, &ulink, port);
  1172. }
  1173. __mutex_unlock (&_hurd_dtable_lock);
  1174. /* If we found a lucky winner, we've set D to -1 in the loop. */
  1175. if (lucky)
  1176. goto win;
  1177. }
  1178. }
  1179. /* If this signal is legit, we have done `goto win' by now.
  1180. When we return the error, mig deallocates REFPORT. */
  1181. return EPERM;
  1182. win:
  1183. /* Deallocate the REFPORT send right; we are done with it. */
  1184. __mach_port_deallocate (__mach_task_self (), refport);
  1185. return 0;
  1186. }
  1187. /* Implement the sig_post RPC from <hurd/msg.defs>;
  1188. sent when someone wants us to get a signal. */
  1189. kern_return_t
  1190. _S_msg_sig_post (mach_port_t me,
  1191. mach_port_t reply_port, mach_msg_type_name_t reply_port_type,
  1192. int signo, natural_t sigcode,
  1193. mach_port_t refport)
  1194. {
  1195. error_t err;
  1196. struct hurd_signal_detail d;
  1197. if (err = signal_allowed (signo, refport))
  1198. return err;
  1199. d.code = d.exc_subcode = sigcode;
  1200. d.exc = 0;
  1201. /* Post the signal to a global receiver thread (or mark it pending in
  1202. the global sigstate). This will reply when the signal can be
  1203. considered delivered. */
  1204. _hurd_internal_post_signal (_hurd_global_sigstate,
  1205. signo, &d, reply_port, reply_port_type,
  1206. 0); /* Stop if traced. */
  1207. return MIG_NO_REPLY; /* Already replied. */
  1208. }
  1209. /* Implement the sig_post_untraced RPC from <hurd/msg.defs>;
  1210. sent when the debugger wants us to really get a signal
  1211. even if we are traced. */
  1212. kern_return_t
  1213. _S_msg_sig_post_untraced (mach_port_t me,
  1214. mach_port_t reply_port,
  1215. mach_msg_type_name_t reply_port_type,
  1216. int signo, natural_t sigcode,
  1217. mach_port_t refport)
  1218. {
  1219. error_t err;
  1220. struct hurd_signal_detail d;
  1221. if (err = signal_allowed (signo, refport))
  1222. return err;
  1223. d.code = d.exc_subcode = sigcode;
  1224. d.exc = 0;
  1225. /* Post the signal to the designated signal-receiving thread. This will
  1226. reply when the signal can be considered delivered. */
  1227. _hurd_internal_post_signal (_hurd_global_sigstate,
  1228. signo, &d, reply_port, reply_port_type,
  1229. 1); /* Untraced flag. */
  1230. return MIG_NO_REPLY; /* Already replied. */
  1231. }
  1232. extern void __mig_init (void *);
  1233. #include <mach/task_special_ports.h>
  1234. /* Initialize the message port, _hurd_global_sigstate, and start the
  1235. signal thread. */
  1236. void
  1237. _hurdsig_init (const int *intarray, size_t intarraysize)
  1238. {
  1239. error_t err;
  1240. vm_size_t stacksize;
  1241. struct hurd_sigstate *ss;
  1242. __mutex_init (&_hurd_siglock);
  1243. err = __mach_port_allocate (__mach_task_self (),
  1244. MACH_PORT_RIGHT_RECEIVE,
  1245. &_hurd_msgport);
  1246. assert_perror (err);
  1247. /* Make a send right to the signal port. */
  1248. err = __mach_port_insert_right (__mach_task_self (),
  1249. _hurd_msgport,
  1250. _hurd_msgport,
  1251. MACH_MSG_TYPE_MAKE_SEND);
  1252. assert_perror (err);
  1253. /* Initialize the global signal state. */
  1254. _hurd_global_sigstate = _hurd_thread_sigstate (MACH_PORT_NULL);
  1255. /* We block all signals, and let actual threads pull them from the
  1256. pending mask. */
  1257. __sigfillset(& _hurd_global_sigstate->blocked);
  1258. /* Initialize the main thread's signal state. */
  1259. ss = _hurd_self_sigstate ();
  1260. /* Mark it as a process-wide signal receiver. Threads in this set use
  1261. the common action vector in _hurd_global_sigstate. */
  1262. _hurd_sigstate_set_global_rcv (ss);
  1263. /* Copy inherited signal settings from our parent (or pre-exec process
  1264. state) */
  1265. if (intarraysize > INIT_SIGMASK)
  1266. ss->blocked = intarray[INIT_SIGMASK];
  1267. if (intarraysize > INIT_SIGPENDING)
  1268. _hurd_global_sigstate->pending = intarray[INIT_SIGPENDING];
  1269. if (intarraysize > INIT_SIGIGN && intarray[INIT_SIGIGN] != 0)
  1270. {
  1271. int signo;
  1272. for (signo = 1; signo < NSIG; ++signo)
  1273. if (intarray[INIT_SIGIGN] & __sigmask(signo))
  1274. _hurd_global_sigstate->actions[signo].sa_handler = SIG_IGN;
  1275. }
  1276. /* Start the signal thread listening on the message port. */
  1277. #pragma weak __libc_pthread_create
  1278. if (!__libc_pthread_create)
  1279. {
  1280. err = __thread_create (__mach_task_self (), &_hurd_msgport_thread);
  1281. assert_perror (err);
  1282. stacksize = __vm_page_size * 8; /* Small stack for signal thread. */
  1283. err = __mach_setup_thread_call (__mach_task_self (),
  1284. _hurd_msgport_thread,
  1285. _hurd_msgport_receive,
  1286. (vm_address_t *) &__hurd_sigthread_stack_base,
  1287. &stacksize);
  1288. assert_perror (err);
  1289. err = __mach_setup_tls (_hurd_msgport_thread);
  1290. assert_perror (err);
  1291. __hurd_sigthread_stack_end = __hurd_sigthread_stack_base + stacksize;
  1292. /* Reinitialize the MiG support routines so they will use a per-thread
  1293. variable for the cached reply port. */
  1294. __mig_init ((void *) __hurd_sigthread_stack_base);
  1295. err = __thread_resume (_hurd_msgport_thread);
  1296. assert_perror (err);
  1297. }
  1298. else
  1299. {
  1300. pthread_t thread;
  1301. pthread_attr_t attr;
  1302. void *addr;
  1303. size_t size;
  1304. /* When pthread is being used, we need to make the signal thread a
  1305. proper pthread. Otherwise it cannot use mutex_lock et al, which
  1306. will be the pthread versions. Various of the message port RPC
  1307. handlers need to take locks, so we need to be able to call into
  1308. pthread code and meet its assumptions about how our thread and
  1309. its stack are arranged. Since pthread puts it there anyway,
  1310. we'll let the signal thread's per-thread variables be found as for
  1311. any normal pthread, and just leave the magic __hurd_sigthread_*
  1312. values all zero so they'll be ignored. */
  1313. #pragma weak __pthread_detach
  1314. #pragma weak __pthread_getattr_np
  1315. #pragma weak __pthread_attr_getstack
  1316. __libc_pthread_create (&thread, NULL, &_hurd_msgport_receive, NULL);
  1317. /* Record signal thread stack layout for fork() */
  1318. __pthread_getattr_np (thread, &attr);
  1319. __pthread_attr_getstack (&attr, &addr, &size);
  1320. __hurd_sigthread_stack_base = (uintptr_t) addr;
  1321. __hurd_sigthread_stack_end = __hurd_sigthread_stack_base + size;
  1322. __pthread_detach(thread);
  1323. /* XXX We need the thread port for the signal thread further on
  1324. in this thread (see hurdfault.c:_hurdsigfault_init).
  1325. Therefore we block until _hurd_msgport_thread is initialized
  1326. by the newly created thread. This really shouldn't be
  1327. necessary; we should be able to fetch the thread port for a
  1328. pthread from here. */
  1329. while (_hurd_msgport_thread == 0)
  1330. __swtch_pri (0);
  1331. }
  1332. /* Receive exceptions on the signal port. */
  1333. #ifdef TASK_EXCEPTION_PORT
  1334. __task_set_special_port (__mach_task_self (),
  1335. TASK_EXCEPTION_PORT, _hurd_msgport);
  1336. #elif defined (EXC_MASK_ALL)
  1337. __task_set_exception_ports (__mach_task_self (),
  1338. EXC_MASK_ALL & ~(EXC_MASK_SYSCALL
  1339. | EXC_MASK_MACH_SYSCALL
  1340. | EXC_MASK_RPC_ALERT),
  1341. _hurd_msgport,
  1342. EXCEPTION_DEFAULT, MACHINE_THREAD_STATE);
  1343. #else
  1344. # error task_set_exception_port?
  1345. #endif
  1346. /* Sanity check. Any pending, unblocked signals should have been
  1347. taken by our predecessor incarnation (i.e. parent or pre-exec state)
  1348. before packing up our init ints. This assert is last (not above)
  1349. so that signal handling is all set up to handle the abort. */
  1350. assert ((ss->pending &~ ss->blocked) == 0);
  1351. }
  1352. /* XXXX */
  1353. /* Reauthenticate with the proc server. */
  1354. static void
  1355. reauth_proc (mach_port_t new)
  1356. {
  1357. error_t err;
  1358. mach_port_t ref, newproc;
  1359. ref = __mach_reply_port ();
  1360. err = HURD_PORT_USE (&_hurd_ports[INIT_PORT_PROC],
  1361. __proc_reauthenticate (port, ref,
  1362. MACH_MSG_TYPE_MAKE_SEND));
  1363. if (err)
  1364. {
  1365. __mach_port_destroy (__mach_task_self (), ref);
  1366. return;
  1367. }
  1368. err = __auth_user_authenticate (new, ref,
  1369. MACH_MSG_TYPE_MAKE_SEND,
  1370. &newproc);
  1371. __mach_port_destroy (__mach_task_self (), ref);
  1372. if (err)
  1373. return;
  1374. if (newproc == MACH_PORT_NULL)
  1375. {
  1376. /* Old versions of the proc server did not recreate the process
  1377. port when reauthenticating, and passed MACH_PORT_NULL through
  1378. the auth server. That must be what we're dealing with. */
  1379. /* Set the owner of the process here too. */
  1380. __mutex_lock (&_hurd_id.lock);
  1381. if (!_hurd_check_ids ())
  1382. HURD_PORT_USE (&_hurd_ports[INIT_PORT_PROC],
  1383. __proc_setowner (port,
  1384. (_hurd_id.gen.nuids
  1385. ? _hurd_id.gen.uids[0] : 0),
  1386. !_hurd_id.gen.nuids));
  1387. __mutex_unlock (&_hurd_id.lock);
  1388. return;
  1389. }
  1390. err = __proc_reauthenticate_complete (newproc);
  1391. if (err)
  1392. {
  1393. __mach_port_deallocate (__mach_task_self (), newproc);
  1394. return;
  1395. }
  1396. _hurd_port_set (&_hurd_ports[INIT_PORT_PROC], newproc);
  1397. (void) &reauth_proc; /* Silence compiler warning. */
  1398. }
  1399. text_set_element (_hurd_reauth_hook, reauth_proc);
  1400. /* Like `getenv', but safe for the signal thread to run.
  1401. If the environment is trashed, this will just return NULL. */
  1402. const char *
  1403. _hurdsig_getenv (const char *variable)
  1404. {
  1405. if (__libc_enable_secure)
  1406. return NULL;
  1407. if (_hurdsig_catch_memory_fault (__environ))
  1408. /* We bombed in getenv. */
  1409. return NULL;
  1410. else
  1411. {
  1412. const size_t len = strlen (variable);
  1413. char *value = NULL;
  1414. char *volatile *ep = __environ;
  1415. while (*ep)
  1416. {
  1417. const char *p = *ep;
  1418. _hurdsig_fault_preemptor.first = (unsigned long int) p;
  1419. _hurdsig_fault_preemptor.last = (unsigned long int) -1;
  1420. if (! strncmp (p, variable, len) && p[len] == '=')
  1421. {
  1422. size_t valuelen;
  1423. p += len + 1;
  1424. valuelen = strlen (p);
  1425. _hurdsig_fault_preemptor.last = (long int) (p + valuelen);
  1426. value = malloc (++valuelen);
  1427. if (value)
  1428. memcpy (value, p, valuelen);
  1429. break;
  1430. }
  1431. _hurdsig_fault_preemptor.first = (unsigned long int) ++ep;
  1432. _hurdsig_fault_preemptor.last = (unsigned long int) (ep + 1);
  1433. }
  1434. _hurdsig_end_catch_fault ();
  1435. return value;
  1436. }
  1437. }