signals.c 6.5 KB

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