signals.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 <stdio.h>
  18. #include <sys/syscall.h>
  19. #include "pthread.h"
  20. #include "internals.h"
  21. #include "spinlock.h"
  22. #include <bits/sigcontextinfo.h>
  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. /* User-provided signal handlers */
  70. typedef void (*arch_sighandler_t) __PMT ((int, SIGCONTEXT));
  71. static union
  72. {
  73. arch_sighandler_t old;
  74. void (*rt) (int, struct siginfo *, struct ucontext *);
  75. } sighandler[NSIG];
  76. /* The wrapper around user-provided signal handlers */
  77. static void pthread_sighandler(int signo, SIGCONTEXT ctx)
  78. {
  79. pthread_descr self = thread_self();
  80. char * in_sighandler;
  81. /* If we're in a sigwait operation, just record the signal received
  82. and return without calling the user's handler */
  83. if (THREAD_GETMEM(self, p_sigwaiting)) {
  84. THREAD_SETMEM(self, p_sigwaiting, 0);
  85. THREAD_SETMEM(self, p_signal, signo);
  86. return;
  87. }
  88. /* Record that we're in a signal handler and call the user's
  89. handler function */
  90. in_sighandler = THREAD_GETMEM(self, p_in_sighandler);
  91. if (in_sighandler == NULL)
  92. THREAD_SETMEM(self, p_in_sighandler, CURRENT_STACK_FRAME);
  93. sighandler[signo].old(signo, SIGCONTEXT_EXTRA_ARGS ctx);
  94. if (in_sighandler == NULL)
  95. THREAD_SETMEM(self, p_in_sighandler, NULL);
  96. }
  97. /* The same, this time for real-time signals. */
  98. static void pthread_sighandler_rt(int signo, struct siginfo *si,
  99. struct ucontext *uc)
  100. {
  101. pthread_descr self = thread_self();
  102. char * in_sighandler;
  103. /* If we're in a sigwait operation, just record the signal received
  104. and return without calling the user's handler */
  105. if (THREAD_GETMEM(self, p_sigwaiting)) {
  106. THREAD_SETMEM(self, p_sigwaiting, 0);
  107. THREAD_SETMEM(self, p_signal, signo);
  108. return;
  109. }
  110. /* Record that we're in a signal handler and call the user's
  111. handler function */
  112. in_sighandler = THREAD_GETMEM(self, p_in_sighandler);
  113. if (in_sighandler == NULL)
  114. THREAD_SETMEM(self, p_in_sighandler, CURRENT_STACK_FRAME);
  115. sighandler[signo].rt(signo, si, uc);
  116. if (in_sighandler == NULL)
  117. THREAD_SETMEM(self, p_in_sighandler, NULL);
  118. }
  119. /* The wrapper around sigaction. Install our own signal handler
  120. around the signal. */
  121. libpthread_hidden_proto(sigaction)
  122. int sigaction(int sig, const struct sigaction * act,
  123. struct sigaction * oact)
  124. {
  125. struct sigaction newact;
  126. struct sigaction *newactp;
  127. #ifdef DEBUG_PT
  128. printf(__FUNCTION__": pthreads wrapper!\n");
  129. #endif
  130. if (sig == __pthread_sig_restart ||
  131. sig == __pthread_sig_cancel ||
  132. (sig == __pthread_sig_debug && __pthread_sig_debug > 0))
  133. return EINVAL;
  134. if (act)
  135. {
  136. newact = *act;
  137. if (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL
  138. && sig > 0 && sig < NSIG)
  139. {
  140. if (act->sa_flags & SA_SIGINFO)
  141. newact.sa_handler = (__sighandler_t) pthread_sighandler_rt;
  142. else
  143. newact.sa_handler = (__sighandler_t) pthread_sighandler;
  144. }
  145. newactp = &newact;
  146. }
  147. else
  148. newactp = NULL;
  149. if (__libc_sigaction(sig, newactp, oact) == -1)
  150. return -1;
  151. #ifdef DEBUG_PT
  152. printf(__FUNCTION__": sighandler installed, sigaction successful\n");
  153. #endif
  154. if (sig > 0 && sig < NSIG)
  155. {
  156. if (oact != NULL)
  157. oact->sa_handler = (__sighandler_t) sighandler[sig].old;
  158. if (act)
  159. /* For the assignment is does not matter whether it's a normal
  160. or real-time signal. */
  161. sighandler[sig].old = (arch_sighandler_t) act->sa_handler;
  162. }
  163. return 0;
  164. }
  165. libpthread_hidden_def(sigaction)
  166. /* A signal handler that does nothing */
  167. static void pthread_null_sighandler(int sig attribute_unused) { }
  168. /* sigwait -- synchronously wait for a signal */
  169. int sigwait(const sigset_t * set, int * sig)
  170. {
  171. volatile pthread_descr self = thread_self();
  172. sigset_t mask;
  173. int s;
  174. sigjmp_buf jmpbuf;
  175. struct sigaction sa;
  176. /* Get ready to block all signals except those in set
  177. and the cancellation signal.
  178. Also check that handlers are installed on all signals in set,
  179. and if not, install our dummy handler. This is conformant to
  180. POSIX: "The effect of sigwait() on the signal actions for the
  181. signals in set is unspecified." */
  182. __sigfillset(&mask);
  183. sigdelset(&mask, __pthread_sig_cancel);
  184. for (s = 1; s <= NSIG; s++) {
  185. if (sigismember(set, s) &&
  186. s != __pthread_sig_restart &&
  187. s != __pthread_sig_cancel &&
  188. s != __pthread_sig_debug) {
  189. sigdelset(&mask, s);
  190. if (sighandler[s].old == NULL ||
  191. sighandler[s].old == (arch_sighandler_t) SIG_DFL ||
  192. sighandler[s].old == (arch_sighandler_t) SIG_IGN) {
  193. memset(&sa, 0, sizeof(sa));
  194. sa.sa_handler = pthread_null_sighandler;
  195. sigaction(s, &sa, NULL);
  196. }
  197. }
  198. }
  199. /* Test for cancellation */
  200. if (sigsetjmp(jmpbuf, 1) == 0) {
  201. THREAD_SETMEM(self, p_cancel_jmp, &jmpbuf);
  202. if (! (THREAD_GETMEM(self, p_canceled)
  203. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE)) {
  204. /* Reset the signal count */
  205. THREAD_SETMEM(self, p_signal, 0);
  206. /* Say we're in sigwait */
  207. THREAD_SETMEM(self, p_sigwaiting, 1);
  208. /* Unblock the signals and wait for them */
  209. sigsuspend(&mask);
  210. }
  211. }
  212. THREAD_SETMEM(self, p_cancel_jmp, NULL);
  213. /* The signals are now reblocked. Check for cancellation */
  214. pthread_testcancel();
  215. /* We should have self->p_signal != 0 and equal to the signal received */
  216. *sig = THREAD_GETMEM(self, p_signal);
  217. return 0;
  218. }
  219. /* Redefine raise() to send signal to calling thread only,
  220. as per POSIX 1003.1c */
  221. libpthread_hidden_proto(raise)
  222. int raise (int sig) {
  223. int ret;
  224. pid_t tid;
  225. tid = INLINE_SYSCALL(gettid, 0);
  226. ret = INLINE_SYSCALL(tkill, 2, tid, sig);
  227. return ret;
  228. }
  229. libpthread_hidden_def(raise)