posix_signal.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. * 2017/10/1 Bernard The first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <sys/time.h>
  13. #include <sys/errno.h>
  14. #include "posix_signal.h"
  15. #define sig_valid(sig_no) (sig_no >= 0 && sig_no < RT_SIG_MAX)
  16. void (*signal(int sig, void (*func)(int))) (int)
  17. {
  18. return rt_signal_install(sig, func);
  19. }
  20. /**
  21. * @brief This function will examines, changes, or examines and changes the signal mask of the calling thread.
  22. *
  23. * @param how indicates the way in which the existing set of blocked signals should be changed.
  24. * The following are the possible values for option:
  25. *
  26. * SIG_BLOCK The set of blocked signals is the union of the current set and the set argument.
  27. *
  28. * SIG_UNBLOCK The signals in set are removed from the current set of blocked signals.
  29. * It is permissible to attempt to unblock a signal which is not blocked.
  30. *
  31. * SIG_SETMASK The set of blocked signals is set to the argument set.
  32. *
  33. * @param set is a pointer to a sigset_t object that specifies the new set of blocked signals.
  34. * If set is NULL, then the signal mask is unchanged (i.e., how is ignored)
  35. *
  36. * @param oset is a pointer to a sigset_t object that is used to return the previous set of blocked signals.
  37. * If oset is non-NULL, the previous value of the signal mask is stored in it.
  38. *
  39. * @return Returns 0 on success.
  40. */
  41. int sigprocmask (int how, const sigset_t *set, sigset_t *oset)
  42. {
  43. rt_base_t level;
  44. rt_thread_t tid;
  45. tid = rt_thread_self();
  46. level = rt_hw_interrupt_disable();
  47. if (oset) *oset = tid->sig_mask;
  48. if (set)
  49. {
  50. switch(how)
  51. {
  52. case SIG_BLOCK:
  53. tid->sig_mask |= *set;
  54. break;
  55. case SIG_UNBLOCK:
  56. tid->sig_mask &= ~*set;
  57. break;
  58. case SIG_SETMASK:
  59. tid->sig_mask = *set;
  60. break;
  61. default:
  62. break;
  63. }
  64. }
  65. rt_hw_interrupt_enable(level);
  66. return 0;
  67. }
  68. /**
  69. * @brief This function will examines the signal mask of the calling thread.
  70. *
  71. * @param set is a pointer to a sigset_t object that is used to return the previous set of blocked signals.
  72. * If set is non-NULL, the previous value of the signal mask is stored in it.
  73. *
  74. * @return Returns 0 on success.
  75. */
  76. int sigpending (sigset_t *set)
  77. {
  78. sigprocmask(SIG_SETMASK, RT_NULL, set);
  79. return 0;
  80. }
  81. /**
  82. * @brief This function will temporarily replace the signal mask of the calling thread
  83. * with the mask given and then suspends the thread until delivery of an expected signal
  84. * or a signal whose action is to terminate a process.
  85. *
  86. * @param set is a pointer of a sigset_t object that is used to replace the original mask of the calling thread.
  87. *
  88. * @return Returns 0 on success.
  89. * If the return value is any other values, it means that the signal wait failed.
  90. */
  91. int sigsuspend (const sigset_t *set)
  92. {
  93. int ret = 0;
  94. sigset_t origin_set;
  95. sigset_t suspend_set;
  96. siginfo_t info; /* unless paremeter */
  97. /* get the origin signal information */
  98. sigpending(&origin_set);
  99. /* set the new signal information */
  100. sigprocmask(SIG_BLOCK, set, RT_NULL);
  101. sigpending(&suspend_set);
  102. ret = rt_signal_wait(&suspend_set, &info, RT_WAITING_FOREVER);
  103. /* restore the original sigprocmask */
  104. sigprocmask(SIG_UNBLOCK, (sigset_t *)0xffffUL, RT_NULL);
  105. sigprocmask(SIG_BLOCK, &origin_set, RT_NULL);
  106. return ret;
  107. }
  108. /**
  109. * @brief This function will install or confirm action for specified signal.
  110. *
  111. * @param signum is the signal to be handled.
  112. *
  113. * @param act is the new signal action, or NULL to restore default action.
  114. *
  115. * @param oldact returns the previous signal action, or NULL if not required.
  116. *
  117. * @return Returns 0 on success or -1 on failure.
  118. */
  119. int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact)
  120. {
  121. rt_sighandler_t old = RT_NULL;
  122. if (!sig_valid(signum)) return -RT_ERROR;
  123. if (act)
  124. old = rt_signal_install(signum, act->sa_handler);
  125. else
  126. {
  127. old = rt_signal_install(signum, RT_NULL);
  128. rt_signal_install(signum, old);
  129. }
  130. if (oldact)
  131. oldact->sa_handler = old;
  132. return 0;
  133. }
  134. /**
  135. * @brief This function will suspends execution of the calling thread until one of
  136. * the signals in the given set is pending. If none of the signals specified
  137. * are pending, it will wait for the specified time interval.
  138. *
  139. * @param set is the set of signal values to be waited for.
  140. *
  141. * @param info is a pointer to the received signal info.
  142. *
  143. * @param timeout is a pointer to a timespec structure that specifys the waiting time.
  144. *
  145. * @return Return 0 on success. Otherwise, return -1 and set errno to indicate the error.
  146. */
  147. int sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec *timeout)
  148. {
  149. int ret = 0;
  150. int tick = RT_WAITING_FOREVER;
  151. if (timeout)
  152. {
  153. tick = timeout->tv_sec * RT_TICK_PER_SECOND + timeout->tv_nsec * RT_TICK_PER_SECOND / NANOSECOND_PER_SECOND;
  154. }
  155. ret = rt_signal_wait(set, info, tick);
  156. if (ret == 0) return 0;
  157. errno = ret;
  158. return -1;
  159. }
  160. /**
  161. * @brief This function will suspend execution of the calling thread until one of
  162. * the specified signal becomes pending and return the signal number.
  163. *
  164. * @param set is the set of signal values to be waited for.
  165. *
  166. * @param sig is a pointer to the received signal number.
  167. *
  168. * @return Return 0 on success or -1 on failure.
  169. */
  170. int sigwait(const sigset_t *set, int *sig)
  171. {
  172. siginfo_t si;
  173. if (sigtimedwait(set, &si, 0) < 0)
  174. return -1;
  175. *sig = si.si_signo;
  176. return 0;
  177. }
  178. /**
  179. * @brief This function will suspend execution of the calling thread until one of
  180. * the specified signal is pending.
  181. *
  182. * @param set is the set of signal values to be waited for.
  183. *
  184. * @param info is a pointer to the received signal info.
  185. *
  186. * @return Return 0 on success or -1 on failure.
  187. */
  188. int sigwaitinfo(const sigset_t *set, siginfo_t *info)
  189. {
  190. return sigtimedwait(set, info, NULL);
  191. }
  192. /**
  193. * @brief This function willsend a signal to the caller
  194. *
  195. * @param sig is the signal that is to be sent.
  196. *
  197. * @return Returns 0 on success.
  198. */
  199. int raise(int sig)
  200. {
  201. rt_thread_kill(rt_thread_self(), sig);
  202. return 0;
  203. }
  204. #include <sys/types.h>
  205. /**
  206. * @brief Sends a signal to the caller.
  207. *
  208. * This function sends the signal specified by @p sig to the caller.
  209. *
  210. * @param sig The signal to be sent.
  211. * This should be one of the standard signal macros such as SIGUSR1, SIGUSR2, etc.
  212. *
  213. * @return Returns 0 on success. If an error occurs, -1 is returned and errno is set appropriately.
  214. */
  215. int sigqueue (pid_t pid, int signo, const union sigval value)
  216. {
  217. /* no support, signal queue */
  218. return -1;
  219. }