lwp_signal.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-02-23 Jesven first version.
  9. * 2023-07-06 Shell update the generation, pending and delivery API
  10. */
  11. #ifndef __LWP_SIGNAL_H__
  12. #define __LWP_SIGNAL_H__
  13. #include "syscall_generic.h"
  14. #include <rtthread.h>
  15. #include <sys/signal.h>
  16. struct timespec;
  17. struct itimerspec;
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. #define _USIGNAL_SIGMASK(signo) (1u << ((signo)-1))
  22. #define LWP_SIG_IGNORE_SET (_USIGNAL_SIGMASK(SIGCHLD) | _USIGNAL_SIGMASK(SIGURG))
  23. #define LWP_SIG_ACT_DFL ((lwp_sighandler_t)0)
  24. #define LWP_SIG_ACT_IGN ((lwp_sighandler_t)1)
  25. #define LWP_SIG_USER_SA_FLAGS \
  26. (SA_NOCLDSTOP | SA_NOCLDWAIT | SA_SIGINFO | SA_ONSTACK | SA_RESTART | \
  27. SA_NODEFER | SA_RESETHAND | SA_EXPOSE_TAGBITS)
  28. #define LWP_SIG_INVALID_TIMER ((timer_t)-1)
  29. typedef enum {
  30. LWP_SIG_MASK_CMD_BLOCK,
  31. LWP_SIG_MASK_CMD_UNBLOCK,
  32. LWP_SIG_MASK_CMD_SET_MASK,
  33. __LWP_SIG_MASK_CMD_WATERMARK
  34. } lwp_sig_mask_cmd_t;
  35. /**
  36. * LwP implementation of POSIX signal
  37. */
  38. struct lwp_signal {
  39. timer_t real_timer;
  40. struct lwp_sigqueue sig_queue;
  41. rt_thread_t sig_dispatch_thr[_LWP_NSIG];
  42. lwp_sighandler_t sig_action[_LWP_NSIG];
  43. lwp_sigset_t sig_action_mask[_LWP_NSIG];
  44. lwp_sigset_t sig_action_nodefer;
  45. lwp_sigset_t sig_action_onstack;
  46. lwp_sigset_t sig_action_restart;
  47. lwp_sigset_t sig_action_siginfo;
  48. };
  49. struct rt_lwp;
  50. #ifndef ARCH_MM_MMU
  51. void lwp_sighandler_set(int sig, lwp_sighandler_t func);
  52. void lwp_thread_sighandler_set(int sig, lwp_sighandler_t func);
  53. #endif
  54. rt_inline void lwp_sigqueue_init(lwp_sigqueue_t sigq)
  55. {
  56. rt_memset(&sigq->sigset_pending, 0, sizeof(lwp_sigset_t));
  57. rt_list_init(&sigq->siginfo_list);
  58. }
  59. /**
  60. * @brief release the signal queue
  61. *
  62. * @param sigq target signal queue
  63. */
  64. void lwp_sigqueue_clear(lwp_sigqueue_t sigq);
  65. rt_err_t lwp_signal_init(struct lwp_signal *sig);
  66. rt_err_t lwp_signal_detach(struct lwp_signal *signal);
  67. rt_inline void lwp_thread_signal_detach(struct lwp_thread_signal *tsig)
  68. {
  69. lwp_sigqueue_clear(&tsig->sig_queue);
  70. }
  71. /**
  72. * @brief send a signal to the process
  73. *
  74. * @param lwp the process to be killed
  75. * @param signo the signal number
  76. * @param code as in siginfo
  77. * @param value as in siginfo
  78. * @return rt_err_t RT_EINVAL if the parameter is invalid, RT_EOK as successful
  79. *
  80. * @note the *signal_kill have the same definition of a successful return as
  81. * kill() in IEEE Std 1003.1-2017
  82. */
  83. rt_err_t lwp_signal_kill(struct rt_lwp *lwp, long signo, long code, long value);
  84. /**
  85. * @brief set or examine the signal action of signo
  86. *
  87. * @param signo signal number
  88. * @param act the signal action
  89. * @param oact the old signal action
  90. * @return rt_err_t
  91. */
  92. rt_err_t lwp_signal_action(struct rt_lwp *lwp, int signo,
  93. const struct lwp_sigaction *restrict act,
  94. struct lwp_sigaction *restrict oact);
  95. /**
  96. * @brief send a signal to the thread
  97. *
  98. * @param thread target thread
  99. * @param signo the signal number
  100. * @param code as in siginfo
  101. * @param value as in siginfo
  102. * @return rt_err_t RT_EINVAL if the parameter is invalid, RT_EOK as successful
  103. */
  104. rt_err_t lwp_thread_signal_kill(rt_thread_t thread, long signo, long code, long value);
  105. /**
  106. * @brief set signal mask of target thread
  107. *
  108. * @param thread the target thread
  109. * @param how command
  110. * @param sigset operand
  111. * @param oset the address to old set
  112. * @return rt_err_t
  113. */
  114. rt_err_t lwp_thread_signal_mask(rt_thread_t thread, lwp_sig_mask_cmd_t how,
  115. const lwp_sigset_t *sigset, lwp_sigset_t *oset);
  116. /**
  117. * @brief Catch signal if exists and no return, otherwise return with no side effect
  118. *
  119. * @param exp_frame the exception frame on kernel stack
  120. */
  121. void lwp_thread_signal_catch(void *exp_frame);
  122. /**
  123. * @brief Check if it's okay to suspend for current lwp thread
  124. *
  125. * @param thread target thread
  126. * @param suspend_flag suspend flag of target thread
  127. * @return int 1 if can be suspended, otherwise not
  128. */
  129. int lwp_thread_signal_suspend_check(rt_thread_t thread, int suspend_flag);
  130. /**
  131. * @brief Asynchronously wait for signal
  132. *
  133. * @param thread target thread
  134. * @param sigset the signals to be waited
  135. * @param info address of user siginfo
  136. * @param timeout timeout of waiting
  137. * @return rt_err_t
  138. */
  139. rt_err_t lwp_thread_signal_timedwait(rt_thread_t thread, lwp_sigset_t *sigset,
  140. siginfo_t *usi, struct timespec *timeout);
  141. /**
  142. * @brief Examine the set of signals that are blocked from delivery to the
  143. * calling thread and that are pending on the process or the calling thread
  144. *
  145. * @param thread target thread
  146. * @param sigset where mask of pending signals is returned
  147. */
  148. void lwp_thread_signal_pending(rt_thread_t thread, lwp_sigset_t *sigset);
  149. rt_err_t lwp_signal_setitimer(struct rt_lwp *lwp, int which,
  150. const struct itimerspec *restrict new,
  151. struct itimerspec *restrict old);
  152. #ifdef __cplusplus
  153. }
  154. #endif
  155. #endif /* __LWP_SIGNAL_H__ */