signals.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Linuxthreads - a simple clone()-based implementation of Posix */
  2. /* threads for Linux. */
  3. /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
  4. /* */
  5. /* This program is free software; you can redistribute it and/or */
  6. /* modify it under the terms of the GNU Library General Public License */
  7. /* as published by the Free Software Foundation; either version 2 */
  8. /* of the License, or (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU Library General Public License for more details. */
  14. /* Handling of signals */
  15. #include <errno.h>
  16. #include <signal.h>
  17. #include "pthread.h"
  18. #include "internals.h"
  19. #include "spinlock.h"
  20. #include <ucontext.h>
  21. /* mods for uClibc: __libc_sigaction is not in any standard headers */
  22. extern __typeof(sigaction) __libc_sigaction;
  23. int pthread_sigmask(int how, const sigset_t * newmask, sigset_t * oldmask)
  24. {
  25. sigset_t mask;
  26. if (newmask != NULL) {
  27. mask = *newmask;
  28. /* Don't allow __pthread_sig_restart to be unmasked.
  29. Don't allow __pthread_sig_cancel to be masked. */
  30. switch(how) {
  31. case SIG_SETMASK:
  32. sigaddset(&mask, __pthread_sig_restart);
  33. sigdelset(&mask, __pthread_sig_cancel);
  34. if (__pthread_sig_debug > 0)
  35. sigdelset(&mask, __pthread_sig_debug);
  36. break;
  37. case SIG_BLOCK:
  38. sigdelset(&mask, __pthread_sig_cancel);
  39. if (__pthread_sig_debug > 0)
  40. sigdelset(&mask, __pthread_sig_debug);
  41. break;
  42. case SIG_UNBLOCK:
  43. sigdelset(&mask, __pthread_sig_restart);
  44. break;
  45. }
  46. newmask = &mask;
  47. }
  48. if (sigprocmask(how, newmask, oldmask) == -1)
  49. return errno;
  50. else
  51. return 0;
  52. }
  53. int pthread_kill(pthread_t thread, int signo)
  54. {
  55. pthread_handle handle = thread_handle(thread);
  56. int pid;
  57. __pthread_lock(&handle->h_lock, NULL);
  58. if (invalid_handle(handle, thread)) {
  59. __pthread_unlock(&handle->h_lock);
  60. return ESRCH;
  61. }
  62. pid = handle->h_descr->p_pid;
  63. __pthread_unlock(&handle->h_lock);
  64. if (kill(pid, signo) == -1)
  65. return errno;
  66. else
  67. return 0;
  68. }
  69. union sighandler __sighandler[NSIG] =
  70. { [1 ... NSIG - 1] = { (arch_sighandler_t) SIG_ERR } };
  71. /* The wrapper around sigaction. Install our own signal handler
  72. around the signal. */
  73. int __pthread_sigaction(int sig, const struct sigaction * act,
  74. struct sigaction * oact)
  75. {
  76. struct sigaction newact;
  77. struct sigaction *newactp;
  78. __sighandler_t old = SIG_DFL;
  79. if (sig == __pthread_sig_restart ||
  80. sig == __pthread_sig_cancel ||
  81. (sig == __pthread_sig_debug && __pthread_sig_debug > 0))
  82. {
  83. __set_errno (EINVAL);
  84. return -1;
  85. }
  86. if (sig > 0 && sig < NSIG)
  87. old = (__sighandler_t) __sighandler[sig].old;
  88. if (act)
  89. {
  90. newact = *act;
  91. if (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL
  92. && sig > 0 && sig < NSIG)
  93. {
  94. if (act->sa_flags & SA_SIGINFO)
  95. newact.sa_handler = (__sighandler_t) __pthread_sighandler_rt;
  96. else
  97. newact.sa_handler = (__sighandler_t) __pthread_sighandler;
  98. if (old == SIG_IGN || old == SIG_DFL || old == SIG_ERR)
  99. __sighandler[sig].old = (arch_sighandler_t) act->sa_handler;
  100. }
  101. newactp = &newact;
  102. }
  103. else
  104. newactp = NULL;
  105. if (__libc_sigaction(sig, newactp, oact) == -1)
  106. {
  107. if (act)
  108. __sighandler[sig].old = (arch_sighandler_t) old;
  109. return -1;
  110. }
  111. if (sig > 0 && sig < NSIG)
  112. {
  113. if (oact != NULL
  114. /* We may have inherited SIG_IGN from the parent, so return the
  115. kernel's idea of the signal handler the first time
  116. through. */
  117. && old != SIG_ERR)
  118. oact->sa_handler = old;
  119. if (act)
  120. /* For the assignment it does not matter whether it's a normal
  121. or real-time signal. */
  122. __sighandler[sig].old = (arch_sighandler_t) act->sa_handler;
  123. }
  124. return 0;
  125. }
  126. #ifdef SHARED
  127. strong_alias(__pthread_sigaction, __sigaction)
  128. strong_alias(__pthread_sigaction, sigaction)
  129. #endif
  130. /* sigwait -- synchronously wait for a signal */
  131. int __pthread_sigwait(const sigset_t * set, int * sig)
  132. {
  133. __volatile__ pthread_descr self = thread_self();
  134. sigset_t mask;
  135. int s;
  136. sigjmp_buf jmpbuf;
  137. struct sigaction sa;
  138. /* Get ready to block all signals except those in set
  139. and the cancellation signal.
  140. Also check that handlers are installed on all signals in set,
  141. and if not, install our dummy handler. This is conformant to
  142. POSIX: "The effect of sigwait() on the signal actions for the
  143. signals in set is unspecified." */
  144. sigfillset(&mask);
  145. sigdelset(&mask, __pthread_sig_cancel);
  146. for (s = 1; s < NSIG; s++) {
  147. if (sigismember(set, s) &&
  148. s != __pthread_sig_restart &&
  149. s != __pthread_sig_cancel &&
  150. s != __pthread_sig_debug) {
  151. sigdelset(&mask, s);
  152. if (__sighandler[s].old == (arch_sighandler_t) SIG_ERR ||
  153. __sighandler[s].old == (arch_sighandler_t) SIG_DFL ||
  154. __sighandler[s].old == (arch_sighandler_t) SIG_IGN) {
  155. sa.sa_handler = __pthread_null_sighandler;
  156. sigfillset(&sa.sa_mask);
  157. sa.sa_flags = 0;
  158. sigaction(s, &sa, NULL);
  159. }
  160. }
  161. }
  162. /* Test for cancellation */
  163. if (sigsetjmp(jmpbuf, 1) == 0) {
  164. THREAD_SETMEM(self, p_cancel_jmp, &jmpbuf);
  165. if (! (THREAD_GETMEM(self, p_canceled)
  166. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE)) {
  167. /* Reset the signal count */
  168. THREAD_SETMEM(self, p_signal, 0);
  169. /* Say we're in sigwait */
  170. THREAD_SETMEM(self, p_sigwaiting, 1);
  171. /* Unblock the signals and wait for them */
  172. sigsuspend(&mask);
  173. }
  174. }
  175. THREAD_SETMEM(self, p_cancel_jmp, NULL);
  176. /* The signals are now reblocked. Check for cancellation */
  177. pthread_testcancel();
  178. /* We should have self->p_signal != 0 and equal to the signal received */
  179. *sig = THREAD_GETMEM(self, p_signal);
  180. return 0;
  181. }
  182. #ifdef SHARED
  183. strong_alias (__pthread_sigwait, sigwait)
  184. #endif
  185. /* Redefine raise() to send signal to calling thread only,
  186. as per POSIX 1003.1c */
  187. int __pthread_raise (int sig)
  188. {
  189. int retcode = pthread_kill(pthread_self(), sig);
  190. if (retcode == 0)
  191. return 0;
  192. else {
  193. errno = retcode;
  194. return -1;
  195. }
  196. }
  197. #ifdef SHARED
  198. strong_alias (__pthread_raise, raise)
  199. #endif
  200. /* This files handles cancellation internally. */
  201. LIBC_CANCEL_HANDLED ();