signals.c 6.4 KB

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