signals.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. void *save = NULL;
  128. #ifdef DEBUG_PT
  129. printf(__FUNCTION__": pthreads wrapper!\n");
  130. #endif
  131. if (sig == __pthread_sig_restart ||
  132. sig == __pthread_sig_cancel ||
  133. (sig == __pthread_sig_debug && __pthread_sig_debug > 0))
  134. return EINVAL;
  135. if (sig > 0 && sig < NSIG)
  136. save = sighandler[sig].old;
  137. if (act)
  138. {
  139. newact = *act;
  140. if (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL
  141. && sig > 0 && sig < NSIG)
  142. {
  143. if (act->sa_flags & SA_SIGINFO)
  144. newact.sa_handler = (__sighandler_t) pthread_sighandler_rt;
  145. else
  146. newact.sa_handler = (__sighandler_t) pthread_sighandler;
  147. }
  148. newactp = &newact;
  149. if (sig > 0 && sig < NSIG)
  150. sighandler[sig].old = (arch_sighandler_t) act->sa_handler;
  151. }
  152. else
  153. newactp = NULL;
  154. if (__libc_sigaction(sig, newactp, oact) == -1)
  155. {
  156. if (act && sig > 0 && sig < NSIG)
  157. sighandler[sig].old = save;
  158. return -1;
  159. }
  160. #ifdef DEBUG_PT
  161. printf(__FUNCTION__": sighandler installed, sigaction successful\n");
  162. #endif
  163. if (sig > 0 && sig < NSIG)
  164. {
  165. if (oact != NULL)
  166. oact->sa_handler = save;
  167. }
  168. return 0;
  169. }
  170. libpthread_hidden_def(sigaction)
  171. /* A signal handler that does nothing */
  172. static void pthread_null_sighandler(int sig attribute_unused) { }
  173. /* sigwait -- synchronously wait for a signal */
  174. int sigwait(const sigset_t * set, int * sig)
  175. {
  176. volatile pthread_descr self = thread_self();
  177. sigset_t mask;
  178. int s;
  179. sigjmp_buf jmpbuf;
  180. struct sigaction sa;
  181. /* Get ready to block all signals except those in set
  182. and the cancellation signal.
  183. Also check that handlers are installed on all signals in set,
  184. and if not, install our dummy handler. This is conformant to
  185. POSIX: "The effect of sigwait() on the signal actions for the
  186. signals in set is unspecified." */
  187. __sigfillset(&mask);
  188. sigdelset(&mask, __pthread_sig_cancel);
  189. for (s = 1; s <= NSIG; s++) {
  190. if (sigismember(set, s) &&
  191. s != __pthread_sig_restart &&
  192. s != __pthread_sig_cancel &&
  193. s != __pthread_sig_debug) {
  194. sigdelset(&mask, s);
  195. if (sighandler[s].old == NULL ||
  196. sighandler[s].old == (arch_sighandler_t) SIG_DFL ||
  197. sighandler[s].old == (arch_sighandler_t) SIG_IGN) {
  198. memset(&sa, 0, sizeof(sa));
  199. sa.sa_handler = pthread_null_sighandler;
  200. sigaction(s, &sa, NULL);
  201. }
  202. }
  203. }
  204. /* Test for cancellation */
  205. if (sigsetjmp(jmpbuf, 1) == 0) {
  206. THREAD_SETMEM(self, p_cancel_jmp, &jmpbuf);
  207. if (! (THREAD_GETMEM(self, p_canceled)
  208. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE)) {
  209. /* Reset the signal count */
  210. THREAD_SETMEM(self, p_signal, 0);
  211. /* Say we're in sigwait */
  212. THREAD_SETMEM(self, p_sigwaiting, 1);
  213. /* Unblock the signals and wait for them */
  214. sigsuspend(&mask);
  215. }
  216. }
  217. THREAD_SETMEM(self, p_cancel_jmp, NULL);
  218. /* The signals are now reblocked. Check for cancellation */
  219. pthread_testcancel();
  220. /* We should have self->p_signal != 0 and equal to the signal received */
  221. *sig = THREAD_GETMEM(self, p_signal);
  222. return 0;
  223. }
  224. /* Redefine raise() to send signal to calling thread only,
  225. as per POSIX 1003.1c */
  226. libpthread_hidden_proto(raise)
  227. int raise (int sig) {
  228. int ret;
  229. pid_t tid;
  230. tid = INLINE_SYSCALL(gettid, 0);
  231. ret = INLINE_SYSCALL(tkill, 2, tid, sig);
  232. return ret;
  233. }
  234. libpthread_hidden_def(raise)