shim_signal.h 5.7 KB

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