lwp_signal.h 4.8 KB

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