signals.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 <sigcontextinfo.h>
  23. /* mods for uClibc: __libc_sigaction is not in any standard headers */
  24. extern int __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact);
  25. int pthread_sigmask(int how, const sigset_t * newmask, sigset_t * oldmask)
  26. {
  27. sigset_t mask;
  28. if (newmask != NULL) {
  29. mask = *newmask;
  30. /* Don't allow __pthread_sig_restart to be unmasked.
  31. Don't allow __pthread_sig_cancel to be masked. */
  32. switch(how) {
  33. case SIG_SETMASK:
  34. sigaddset(&mask, __pthread_sig_restart);
  35. sigdelset(&mask, __pthread_sig_cancel);
  36. break;
  37. case SIG_BLOCK:
  38. sigdelset(&mask, __pthread_sig_cancel);
  39. break;
  40. case SIG_UNBLOCK:
  41. sigdelset(&mask, __pthread_sig_restart);
  42. break;
  43. }
  44. newmask = &mask;
  45. }
  46. if (sigprocmask(how, newmask, oldmask) == -1)
  47. return errno;
  48. else
  49. return 0;
  50. }
  51. int pthread_kill(pthread_t thread, int signo)
  52. {
  53. pthread_handle handle = thread_handle(thread);
  54. int pid;
  55. __pthread_lock(&handle->h_lock, NULL);
  56. if (invalid_handle(handle, thread)) {
  57. __pthread_unlock(&handle->h_lock);
  58. return ESRCH;
  59. }
  60. pid = handle->h_descr->p_pid;
  61. __pthread_unlock(&handle->h_lock);
  62. if (kill(pid, signo) == -1)
  63. return errno;
  64. else
  65. return 0;
  66. }
  67. /* User-provided signal handlers */
  68. typedef void (*arch_sighandler_t) __PMT ((int, SIGCONTEXT));
  69. static union
  70. {
  71. arch_sighandler_t old;
  72. void (*rt) (int, struct siginfo *, struct ucontext *);
  73. } sighandler[NSIG];
  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. #ifdef DEBUG_PT
  125. printf(__FUNCTION__": pthreads wrapper!\n");
  126. #endif
  127. if (sig == __pthread_sig_restart ||
  128. sig == __pthread_sig_cancel ||
  129. (sig == __pthread_sig_debug && __pthread_sig_debug > 0))
  130. return EINVAL;
  131. if (act)
  132. {
  133. newact = *act;
  134. if (act->sa_handler != SIG_IGN && act->sa_handler != SIG_DFL
  135. && sig > 0 && sig < NSIG)
  136. {
  137. if (act->sa_flags & SA_SIGINFO)
  138. newact.sa_handler = (__sighandler_t) pthread_sighandler_rt;
  139. else
  140. newact.sa_handler = (__sighandler_t) pthread_sighandler;
  141. }
  142. newactp = &newact;
  143. }
  144. else
  145. newactp = NULL;
  146. if (__libc_sigaction(sig, newactp, oact) == -1)
  147. return -1;
  148. #ifdef DEBUG_PT
  149. printf(__FUNCTION__": signahdler installed, __sigaction successful\n");
  150. #endif
  151. if (sig > 0 && sig < NSIG)
  152. {
  153. if (oact != NULL)
  154. oact->sa_handler = (__sighandler_t) sighandler[sig].old;
  155. if (act)
  156. /* For the assignment is does not matter whether it's a normal
  157. or real-time signal. */
  158. sighandler[sig].old = (arch_sighandler_t) act->sa_handler;
  159. }
  160. return 0;
  161. }
  162. /* A signal handler that does nothing */
  163. static void pthread_null_sighandler(int sig) { }
  164. /* sigwait -- synchronously wait for a signal */
  165. int sigwait(const sigset_t * set, int * sig)
  166. {
  167. volatile pthread_descr self = thread_self();
  168. sigset_t mask;
  169. int s;
  170. sigjmp_buf jmpbuf;
  171. struct sigaction sa;
  172. /* Get ready to block all signals except those in set
  173. and the cancellation signal.
  174. Also check that handlers are installed on all signals in set,
  175. and if not, install our dummy handler. This is conformant to
  176. POSIX: "The effect of sigwait() on the signal actions for the
  177. signals in set is unspecified." */
  178. sigfillset(&mask);
  179. sigdelset(&mask, __pthread_sig_cancel);
  180. for (s = 1; s <= NSIG; s++) {
  181. if (sigismember(set, s) &&
  182. s != __pthread_sig_restart &&
  183. s != __pthread_sig_cancel &&
  184. s != __pthread_sig_debug) {
  185. sigdelset(&mask, s);
  186. if (sighandler[s].old == NULL ||
  187. sighandler[s].old == (arch_sighandler_t) SIG_DFL ||
  188. sighandler[s].old == (arch_sighandler_t) SIG_IGN) {
  189. sa.sa_handler = pthread_null_sighandler;
  190. sigemptyset(&sa.sa_mask);
  191. sa.sa_flags = 0;
  192. sigaction(s, &sa, NULL);
  193. }
  194. }
  195. }
  196. /* Test for cancellation */
  197. if (sigsetjmp(jmpbuf, 1) == 0) {
  198. THREAD_SETMEM(self, p_cancel_jmp, &jmpbuf);
  199. if (! (THREAD_GETMEM(self, p_canceled)
  200. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE)) {
  201. /* Reset the signal count */
  202. THREAD_SETMEM(self, p_signal, 0);
  203. /* Say we're in sigwait */
  204. THREAD_SETMEM(self, p_sigwaiting, 1);
  205. /* Unblock the signals and wait for them */
  206. sigsuspend(&mask);
  207. }
  208. }
  209. THREAD_SETMEM(self, p_cancel_jmp, NULL);
  210. /* The signals are now reblocked. Check for cancellation */
  211. pthread_testcancel();
  212. /* We should have self->p_signal != 0 and equal to the signal received */
  213. *sig = THREAD_GETMEM(self, p_signal);
  214. return 0;
  215. }
  216. /* Redefine raise() to send signal to calling thread only,
  217. as per POSIX 1003.1c */
  218. int raise (int sig)
  219. {
  220. int retcode = pthread_kill(pthread_self(), sig);
  221. if (retcode == 0)
  222. return 0;
  223. else {
  224. errno = retcode;
  225. return -1;
  226. }
  227. }