signal.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C) 2000, 2001, 2002 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. #include "linux/stddef.h"
  6. #include "linux/sys.h"
  7. #include "linux/sched.h"
  8. #include "linux/wait.h"
  9. #include "linux/kernel.h"
  10. #include "linux/smp_lock.h"
  11. #include "linux/module.h"
  12. #include "linux/slab.h"
  13. #include "linux/tty.h"
  14. #include "linux/binfmts.h"
  15. #include "linux/ptrace.h"
  16. #include "asm/signal.h"
  17. #include "asm/uaccess.h"
  18. #include "asm/unistd.h"
  19. #include "asm/ucontext.h"
  20. #include "kern_util.h"
  21. #include "signal_kern.h"
  22. #include "kern.h"
  23. #include "frame_kern.h"
  24. #include "sigcontext.h"
  25. #include "mode.h"
  26. EXPORT_SYMBOL(block_signals);
  27. EXPORT_SYMBOL(unblock_signals);
  28. #define _S(nr) (1<<((nr)-1))
  29. #define _BLOCKABLE (~(_S(SIGKILL) | _S(SIGSTOP)))
  30. /*
  31. * OK, we're invoking a handler
  32. */
  33. static int handle_signal(struct pt_regs *regs, unsigned long signr,
  34. struct k_sigaction *ka, siginfo_t *info,
  35. sigset_t *oldset)
  36. {
  37. unsigned long sp;
  38. int err;
  39. /* Always make any pending restarted system calls return -EINTR */
  40. current_thread_info()->restart_block.fn = do_no_restart_syscall;
  41. /* Did we come from a system call? */
  42. if(PT_REGS_SYSCALL_NR(regs) >= 0){
  43. /* If so, check system call restarting.. */
  44. switch(PT_REGS_SYSCALL_RET(regs)){
  45. case -ERESTART_RESTARTBLOCK:
  46. case -ERESTARTNOHAND:
  47. PT_REGS_SYSCALL_RET(regs) = -EINTR;
  48. break;
  49. case -ERESTARTSYS:
  50. if (!(ka->sa.sa_flags & SA_RESTART)) {
  51. PT_REGS_SYSCALL_RET(regs) = -EINTR;
  52. break;
  53. }
  54. /* fallthrough */
  55. case -ERESTARTNOINTR:
  56. PT_REGS_RESTART_SYSCALL(regs);
  57. PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
  58. break;
  59. }
  60. }
  61. sp = PT_REGS_SP(regs);
  62. if((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags(sp) == 0))
  63. sp = current->sas_ss_sp + current->sas_ss_size;
  64. #ifdef CONFIG_ARCH_HAS_SC_SIGNALS
  65. if(!(ka->sa.sa_flags & SA_SIGINFO))
  66. err = setup_signal_stack_sc(sp, signr, ka, regs, oldset);
  67. else
  68. #endif
  69. err = setup_signal_stack_si(sp, signr, ka, regs, info, oldset);
  70. if(err){
  71. spin_lock_irq(&current->sighand->siglock);
  72. current->blocked = *oldset;
  73. recalc_sigpending();
  74. spin_unlock_irq(&current->sighand->siglock);
  75. force_sigsegv(signr, current);
  76. } else {
  77. spin_lock_irq(&current->sighand->siglock);
  78. sigorsets(&current->blocked, &current->blocked,
  79. &ka->sa.sa_mask);
  80. if(!(ka->sa.sa_flags & SA_NODEFER))
  81. sigaddset(&current->blocked, signr);
  82. recalc_sigpending();
  83. spin_unlock_irq(&current->sighand->siglock);
  84. }
  85. return err;
  86. }
  87. static int kern_do_signal(struct pt_regs *regs)
  88. {
  89. struct k_sigaction ka_copy;
  90. siginfo_t info;
  91. sigset_t *oldset;
  92. int sig, handled_sig = 0;
  93. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  94. oldset = &current->saved_sigmask;
  95. else
  96. oldset = &current->blocked;
  97. while((sig = get_signal_to_deliver(&info, &ka_copy, regs, NULL)) > 0){
  98. handled_sig = 1;
  99. /* Whee! Actually deliver the signal. */
  100. if(!handle_signal(regs, sig, &ka_copy, &info, oldset)){
  101. /* a signal was successfully delivered; the saved
  102. * sigmask will have been stored in the signal frame,
  103. * and will be restored by sigreturn, so we can simply
  104. * clear the TIF_RESTORE_SIGMASK flag */
  105. if (test_thread_flag(TIF_RESTORE_SIGMASK))
  106. clear_thread_flag(TIF_RESTORE_SIGMASK);
  107. break;
  108. }
  109. }
  110. /* Did we come from a system call? */
  111. if(!handled_sig && (PT_REGS_SYSCALL_NR(regs) >= 0)){
  112. /* Restart the system call - no handlers present */
  113. switch(PT_REGS_SYSCALL_RET(regs)){
  114. case -ERESTARTNOHAND:
  115. case -ERESTARTSYS:
  116. case -ERESTARTNOINTR:
  117. PT_REGS_ORIG_SYSCALL(regs) = PT_REGS_SYSCALL_NR(regs);
  118. PT_REGS_RESTART_SYSCALL(regs);
  119. break;
  120. case -ERESTART_RESTARTBLOCK:
  121. PT_REGS_ORIG_SYSCALL(regs) = __NR_restart_syscall;
  122. PT_REGS_RESTART_SYSCALL(regs);
  123. break;
  124. }
  125. }
  126. /* This closes a way to execute a system call on the host. If
  127. * you set a breakpoint on a system call instruction and singlestep
  128. * from it, the tracing thread used to PTRACE_SINGLESTEP the process
  129. * rather than PTRACE_SYSCALL it, allowing the system call to execute
  130. * on the host. The tracing thread will check this flag and
  131. * PTRACE_SYSCALL if necessary.
  132. */
  133. if(current->ptrace & PT_DTRACE)
  134. current->thread.singlestep_syscall =
  135. is_syscall(PT_REGS_IP(&current->thread.regs));
  136. /* if there's no signal to deliver, we just put the saved sigmask
  137. * back */
  138. if (!handled_sig && test_thread_flag(TIF_RESTORE_SIGMASK)) {
  139. clear_thread_flag(TIF_RESTORE_SIGMASK);
  140. sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
  141. }
  142. return handled_sig;
  143. }
  144. int do_signal(void)
  145. {
  146. return kern_do_signal(&current->thread.regs);
  147. }
  148. /*
  149. * Atomically swap in the new signal mask, and wait for a signal.
  150. */
  151. long sys_sigsuspend(int history0, int history1, old_sigset_t mask)
  152. {
  153. mask &= _BLOCKABLE;
  154. spin_lock_irq(&current->sighand->siglock);
  155. current->saved_sigmask = current->blocked;
  156. siginitset(&current->blocked, mask);
  157. recalc_sigpending();
  158. spin_unlock_irq(&current->sighand->siglock);
  159. current->state = TASK_INTERRUPTIBLE;
  160. schedule();
  161. set_thread_flag(TIF_RESTORE_SIGMASK);
  162. return -ERESTARTNOHAND;
  163. }
  164. long sys_sigaltstack(const stack_t __user *uss, stack_t __user *uoss)
  165. {
  166. return do_sigaltstack(uss, uoss, PT_REGS_SP(&current->thread.regs));
  167. }