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