shim_signal.h 5.9 KB

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