signals.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 "pthread.h"
  19. #include "internals.h"
  20. #include "spinlock.h"
  21. #include <ucontext.h>
  22. #include "debug.h"
  23. #include <bits/sigcontextinfo.h>
  24. /* mods for uClibc: __libc_sigaction is not in any standard headers */
  25. extern int __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact);
  26. int pthread_sigmask(int how, const sigset_t * newmask, sigset_t * oldmask)
  27. {
  28. sigset_t mask;
  29. if (newmask != NULL) {
  30. mask = *newmask;
  31. /* Don't allow __pthread_sig_restart to be unmasked.
  32. Don't allow __pthread_sig_cancel to be masked. */
  33. switch(how) {
  34. case SIG_SETMASK:
  35. sigaddset(&mask, __pthread_sig_restart);
  36. sigdelset(&mask, __pthread_sig_cancel);
  37. if (__pthread_sig_debug > 0)
  38. sigdelset(&mask, __pthread_sig_debug);
  39. break;
  40. case SIG_BLOCK:
  41. sigdelset(&mask, __pthread_sig_cancel);
  42. if (__pthread_sig_debug > 0)
  43. sigdelset(&mask, __pthread_sig_debug);
  44. break;
  45. case SIG_UNBLOCK:
  46. sigdelset(&mask, __pthread_sig_restart);
  47. break;
  48. }
  49. newmask = &mask;
  50. }
  51. if (sigprocmask(how, newmask, oldmask) == -1)
  52. return errno;
  53. else
  54. return 0;
  55. }
  56. int pthread_kill(pthread_t thread, int signo)
  57. {
  58. pthread_handle handle = thread_handle(thread);
  59. int pid;
  60. __pthread_lock(&handle->h_lock, NULL);
  61. if (nonexisting_handle(handle, thread)) {
  62. __pthread_unlock(&handle->h_lock);
  63. return ESRCH;
  64. }
  65. pid = handle->h_descr->p_pid;
  66. __pthread_unlock(&handle->h_lock);
  67. if (kill(pid, signo) == -1)
  68. return errno;
  69. else
  70. return 0;
  71. }
  72. union sighandler __sighandler[NSIG] =
  73. { [1 ... NSIG - 1] = { (arch_sighandler_t) SIG_ERR } };
  74. /* The wrapper around user-provided signal handlers */
  75. static void __pthread_sighandler(int signo, SIGCONTEXT ctx)
  76. {
  77. pthread_descr self = thread_self();
  78. char * in_sighandler;
  79. /* If we're in a sigwait operation, just record the signal received
  80. and return without calling the user's handler */
  81. if (THREAD_GETMEM(self, p_sigwaiting)) {
  82. THREAD_SETMEM(self, p_sigwaiting, 0);
  83. THREAD_SETMEM(self, p_signal, signo);
  84. return;
  85. }
  86. /* Record that we're in a signal handler and call the user's
  87. handler function */
  88. in_sighandler = THREAD_GETMEM(self, p_in_sighandler);
  89. if (in_sighandler == NULL)
  90. THREAD_SETMEM(self, p_in_sighandler, CURRENT_STACK_FRAME);
  91. __sighandler[signo].old(signo, SIGCONTEXT_EXTRA_ARGS ctx);
  92. if (in_sighandler == NULL)
  93. THREAD_SETMEM(self, p_in_sighandler, NULL);
  94. }
  95. /* The same, this time for real-time signals. */
  96. static void __pthread_sighandler_rt(int signo, struct siginfo *si,
  97. struct ucontext *uc)
  98. {
  99. pthread_descr self = thread_self();
  100. char * in_sighandler;
  101. /* If we're in a sigwait operation, just record the signal received
  102. and return without calling the user's handler */
  103. if (THREAD_GETMEM(self, p_sigwaiting)) {
  104. THREAD_SETMEM(self, p_sigwaiting, 0);
  105. THREAD_SETMEM(self, p_signal, signo);
  106. return;
  107. }
  108. /* Record that we're in a signal handler and call the user's
  109. handler function */
  110. in_sighandler = THREAD_GETMEM(self, p_in_sighandler);
  111. if (in_sighandler == NULL)
  112. THREAD_SETMEM(self, p_in_sighandler, CURRENT_STACK_FRAME);
  113. __sighandler[signo].rt(signo, si, uc);
  114. if (in_sighandler == NULL)
  115. THREAD_SETMEM(self, p_in_sighandler, NULL);
  116. }
  117. /* The wrapper around sigaction. Install our own signal handler
  118. around the signal. */
  119. int __sigaction(int sig, const struct sigaction * act,
  120. struct sigaction * oact)
  121. {
  122. struct sigaction newact;
  123. struct sigaction *newactp;
  124. __sighandler_t old = SIG_DFL;
  125. PDEBUG("pthreads wrapper!\n");
  126. if (sig == __pthread_sig_restart ||
  127. sig == __pthread_sig_cancel ||
  128. (sig == __pthread_sig_debug && __pthread_sig_debug > 0))
  129. {
  130. __set_errno (EINVAL);
  131. return -1;
  132. }
  133. if (sig > 0 && sig < NSIG)
  134. old = (__sighandler_t) __sighandler[sig].old;
  135. if (act)
  136. {
  137. newact = *act;
  138. if (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL
  139. && sig > 0 && sig < NSIG)
  140. {
  141. if (act->sa_flags & SA_SIGINFO)
  142. newact.sa_handler = (__sighandler_t) __pthread_sighandler_rt;
  143. else
  144. newact.sa_handler = (__sighandler_t) __pthread_sighandler;
  145. if (old == SIG_IGN || old == SIG_DFL || old == SIG_ERR)
  146. __sighandler[sig].old = (arch_sighandler_t) act->sa_handler;
  147. }
  148. newactp = &newact;
  149. }
  150. else
  151. newactp = NULL;
  152. if (__libc_sigaction(sig, newactp, oact) == -1)
  153. {
  154. if (act)
  155. __sighandler[sig].old = (arch_sighandler_t) old;
  156. return -1;
  157. }
  158. PDEBUG("signahdler installed, __sigaction successful\n");
  159. if (sig > 0 && sig < NSIG)
  160. {
  161. if (oact != NULL
  162. /* We may have inherited SIG_IGN from the parent, so return the
  163. kernel's idea of the signal handler the first time
  164. through. */
  165. && old != SIG_ERR)
  166. oact->sa_handler = old;
  167. if (act)
  168. /* For the assignment it does not matter whether it's a normal
  169. or real-time signal. */
  170. __sighandler[sig].old = (arch_sighandler_t) act->sa_handler;
  171. }
  172. return 0;
  173. }
  174. strong_alias(__sigaction, sigaction)
  175. /* A signal handler that does nothing */
  176. static void pthread_null_sighandler(int sig) { }
  177. /* sigwait -- synchronously wait for a signal */
  178. int sigwait(const sigset_t * set, int * sig)
  179. {
  180. volatile pthread_descr self = thread_self();
  181. sigset_t mask;
  182. int s;
  183. sigjmp_buf jmpbuf;
  184. struct sigaction sa;
  185. /* Get ready to block all signals except those in set
  186. and the cancellation signal.
  187. Also check that handlers are installed on all signals in set,
  188. and if not, install our dummy handler. This is conformant to
  189. POSIX: "The effect of sigwait() on the signal actions for the
  190. signals in set is unspecified." */
  191. sigfillset(&mask);
  192. sigdelset(&mask, __pthread_sig_cancel);
  193. for (s = 1; s < NSIG; s++) {
  194. if (sigismember(set, s) &&
  195. s != __pthread_sig_restart &&
  196. s != __pthread_sig_cancel &&
  197. s != __pthread_sig_debug) {
  198. sigdelset(&mask, s);
  199. if (__sighandler[s].old == (arch_sighandler_t) SIG_ERR ||
  200. __sighandler[s].old == (arch_sighandler_t) SIG_DFL ||
  201. __sighandler[s].old == (arch_sighandler_t) SIG_IGN) {
  202. sa.sa_handler = pthread_null_sighandler;
  203. sigfillset(&sa.sa_mask);
  204. sa.sa_flags = 0;
  205. sigaction(s, &sa, NULL);
  206. }
  207. }
  208. }
  209. /* Test for cancellation */
  210. if (sigsetjmp(jmpbuf, 1) == 0) {
  211. THREAD_SETMEM(self, p_cancel_jmp, &jmpbuf);
  212. if (! (THREAD_GETMEM(self, p_canceled)
  213. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE)) {
  214. /* Reset the signal count */
  215. THREAD_SETMEM(self, p_signal, 0);
  216. /* Say we're in sigwait */
  217. THREAD_SETMEM(self, p_sigwaiting, 1);
  218. /* Unblock the signals and wait for them */
  219. sigsuspend(&mask);
  220. }
  221. }
  222. THREAD_SETMEM(self, p_cancel_jmp, NULL);
  223. /* The signals are now reblocked. Check for cancellation */
  224. pthread_testcancel();
  225. /* We should have self->p_signal != 0 and equal to the signal received */
  226. *sig = THREAD_GETMEM(self, p_signal);
  227. return 0;
  228. }
  229. /* Redefine raise() to send signal to calling thread only,
  230. as per POSIX 1003.1c */
  231. int raise (int sig)
  232. {
  233. int retcode = pthread_kill(pthread_self(), sig);
  234. if (retcode == 0)
  235. return 0;
  236. else {
  237. errno = retcode;
  238. return -1;
  239. }
  240. }