shim_signal.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #ifndef _SHIM_SIGNAL_H_
  2. #define _SHIM_SIGNAL_H_
  3. #include <shim_types.h>
  4. #include <shim_defs.h>
  5. #include <shim_thread.h>
  6. struct shim_signal_handle {
  7. /* sigaction */
  8. struct __kernel_sigaction * action;
  9. };
  10. # define BITS_PER_WORD (8 * sizeof(unsigned long))
  11. /* The standard def of this macro is dumb */
  12. #undef _SIGSET_NWORDS
  13. # define _SIGSET_NWORDS (NUM_SIGS / BITS_PER_WORD)
  14. /* Return a mask that includes the bit for SIG only. */
  15. # define __sigmask(sig) \
  16. (((unsigned long int) 1) << (((sig) - 1) % (8 * sizeof (unsigned long int))))
  17. /* Return the word index for SIG. */
  18. # define __sigword(sig) (((sig) - 1) / (8 * sizeof (unsigned long int)))
  19. /* Clear all signals from SET. */
  20. # define __sigemptyset(set) \
  21. (__extension__ ({ int __cnt = _SIGSET_NWORDS; \
  22. __sigset_t *__set = (set); \
  23. while (--__cnt >= 0) __set->__val[__cnt] = 0; \
  24. 0; }))
  25. /* Set all signals in SET. */
  26. # define __sigfillset(set) \
  27. (__extension__ ({ int __cnt = _SIGSET_NWORDS; \
  28. __sigset_t *__set = (set); \
  29. while (--__cnt >= 0) __set->__val[__cnt] = ~0UL; \
  30. 0; }))
  31. # define __sigisemptyset(set) \
  32. (__extension__ ({ int __cnt = _SIGSET_NWORDS; \
  33. const __sigset_t *__set = (set); \
  34. int __ret = __set->__val[--__cnt]; \
  35. while (!__ret && --__cnt >= 0) \
  36. __ret = __set->__val[__cnt]; \
  37. __ret == 0; }))
  38. # define __sigandset(dest, left, right) \
  39. (__extension__ ({ int __cnt = _SIGSET_NWORDS; \
  40. __sigset_t *__dest = (dest); \
  41. const __sigset_t *__left = (left); \
  42. const __sigset_t *__right = (right); \
  43. while (--__cnt >= 0) \
  44. __dest->__val[__cnt] = (__left->__val[__cnt] \
  45. & __right->__val[__cnt]); \
  46. 0; }))
  47. # define __sigorset(dest, left, right) \
  48. (__extension__ ({ int __cnt = _SIGSET_NWORDS; \
  49. __sigset_t *__dest = (dest); \
  50. const __sigset_t *__left = (left); \
  51. const __sigset_t *__right = (right); \
  52. while (--__cnt >= 0) \
  53. __dest->__val[__cnt] = (__left->__val[__cnt] \
  54. | __right->__val[__cnt]); \
  55. 0; }))
  56. # define __signotset(dest, left, right) \
  57. (__extension__ ({ int __cnt = _SIGSET_NWORDS; \
  58. __sigset_t *__dest = (dest); \
  59. const __sigset_t *__left = (left); \
  60. const __sigset_t *__right = (right); \
  61. while (--__cnt >= 0) \
  62. __dest->__val[__cnt] = (__left->__val[__cnt] \
  63. & ~__right->__val[__cnt]); \
  64. 0; }))
  65. # define __SIGSETFN(NAME, BODY, CONST) \
  66. static inline int \
  67. NAME (CONST __sigset_t *__set, int __sig) \
  68. { \
  69. unsigned long int __mask = __sigmask (__sig); \
  70. unsigned long int __word = __sigword (__sig); \
  71. return BODY; \
  72. }
  73. __SIGSETFN (shim_sigismember, (__set->__val[__word] & __mask) ? 1 : 0, __const)
  74. __SIGSETFN (shim_sigaddset, ((__set->__val[__word] |= __mask), 0), )
  75. __SIGSETFN (shim_sigdelset, ((__set->__val[__word] &= ~__mask), 0), )
  76. #define __sigismember shim_sigismember
  77. #define __sigaddset shim_sigaddset
  78. #define __sigdelset shim_sigdelset
  79. /* NB: Check shim_signal.c if this changes. Some memset(0) elision*/
  80. struct shim_signal {
  81. siginfo_t info;
  82. bool context_stored;
  83. ucontext_t context;
  84. PAL_CONTEXT * pal_context;
  85. };
  86. #define MAX_SIGNAL_LOG 32
  87. struct shim_signal_log {
  88. struct atomic_int head, tail;
  89. struct shim_signal * logs[MAX_SIGNAL_LOG];
  90. };
  91. extern const char * const siglist[NUM_KNOWN_SIGS + 1];
  92. static_always_inline
  93. const char * signal_name (int sig)
  94. {
  95. if (sig <= NUM_KNOWN_SIGS)
  96. return siglist[sig];
  97. if (sig >= NUM_SIGS)
  98. return "BAD SIGNAL";
  99. char * str = __alloca(6);
  100. str[0] = 'S';
  101. str[1] = 'I';
  102. str[2] = 'G';
  103. str[3] = '0' + sig / 10;
  104. str[4] = '0' + sig % 10;
  105. str[5] = 0;
  106. return str;
  107. }
  108. struct shim_thread;
  109. int init_signal (void);
  110. void __store_context (shim_tcb_t * tcb, PAL_CONTEXT * pal_context,
  111. struct shim_signal * signal);
  112. // Need to hold thread->lock when calling this function
  113. void append_signal(struct shim_thread* thread, int sig, siginfo_t* info, bool need_interrupt);
  114. void deliver_signal(siginfo_t* info, PAL_CONTEXT* context);
  115. __sigset_t * get_sig_mask (struct shim_thread * thread);
  116. __sigset_t * set_sig_mask (struct shim_thread * thread,
  117. const __sigset_t * new_set);
  118. int do_kill_thread (IDTYPE sender, IDTYPE tgid, IDTYPE tid, int sig,
  119. bool use_ipc);
  120. int do_kill_proc (IDTYPE sender, IDTYPE tgid, int sig, bool use_ipc);
  121. int do_kill_pgroup (IDTYPE sender, IDTYPE pgid, int sig, bool use_ipc);
  122. int kill_all_threads (struct shim_thread * cur, IDTYPE sender, int sig);
  123. #endif /* _SHIM_SIGNAL_H_ */