sigaction.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* POSIX.1 `sigaction' call for Linux/x86-64.
  2. Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <errno.h>
  17. #include <signal.h>
  18. #include <string.h>
  19. #include <sys/syscall.h>
  20. #include <bits/kernel_sigaction.h>
  21. #define SA_RESTORER 0x04000000
  22. #if defined __NR_rt_sigaction
  23. #warning Yes there are two warnings here. Don't worry about it.
  24. static void restore_rt (void) asm ("__restore_rt");
  25. static void restore (void) asm ("__restore");
  26. /* If ACT is not NULL, change the action for SIG to *ACT.
  27. If OACT is not NULL, put the old action for SIG in *OACT. */
  28. int __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
  29. {
  30. int result;
  31. struct kernel_sigaction kact, koact;
  32. if (act) {
  33. kact.k_sa_handler = act->sa_handler;
  34. memcpy (&kact.sa_mask, &act->sa_mask, sizeof (sigset_t));
  35. kact.sa_flags = act->sa_flags | SA_RESTORER;
  36. kact.sa_restorer = &restore_rt;
  37. }
  38. /* XXX The size argument hopefully will have to be changed to the
  39. real size of the user-level sigset_t. */
  40. result = INLINE_SYSCALL (rt_sigaction, 4,
  41. sig, act ? __ptrvalue (&kact) : NULL,
  42. oact ? __ptrvalue (&koact) : NULL, _NSIG / 8);
  43. if (oact && result >= 0) {
  44. oact->sa_handler = koact.k_sa_handler;
  45. memcpy (&oact->sa_mask, &koact.sa_mask, sizeof (sigset_t));
  46. oact->sa_flags = koact.sa_flags;
  47. oact->sa_restorer = koact.sa_restorer;
  48. }
  49. return result;
  50. }
  51. #else
  52. #warning "Yes there is a warning here. Don't worry about it."
  53. static void restore (void) asm ("__restore");
  54. /* If ACT is not NULL, change the action for SIG to *ACT.
  55. If OACT is not NULL, put the old action for SIG in *OACT. */
  56. int __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
  57. {
  58. int result;
  59. struct old_kernel_sigaction kact, koact;
  60. #ifdef SIGCANCEL
  61. if (sig == SIGCANCEL) {
  62. __set_errno (EINVAL);
  63. return -1;
  64. }
  65. #endif
  66. if (act) {
  67. kact.k_sa_handler = act->sa_handler;
  68. kact.sa_mask = act->sa_mask.__val[0];
  69. kact.sa_flags = act->sa_flags | SA_RESTORER;
  70. kact.sa_restorer = &restore;
  71. }
  72. asm volatile ("syscall\n"
  73. : "=a" (result)
  74. : "0" (__NR_sigaction), "mr" (sig),
  75. "c" (act ? __ptrvalue (&kact) : 0),
  76. "d" (oact ? __ptrvalue (&koact) : 0));
  77. if (result < 0) {
  78. __set_errno(-result);
  79. return -1;
  80. }
  81. if (oact) {
  82. oact->sa_handler = koact.k_sa_handler;
  83. oact->sa_mask.__val[0] = koact.sa_mask;
  84. oact->sa_flags = koact.sa_flags;
  85. oact->sa_restorer = koact.sa_restorer;
  86. }
  87. return result;
  88. }
  89. #endif
  90. weak_alias (__libc_sigaction, sigaction)
  91. /* NOTE: Please think twice before making any changes to the bits of
  92. code below. GDB needs some intimate knowledge about it to
  93. recognize them as signal trampolines, and make backtraces through
  94. signal handlers work right. Important are both the names
  95. (__restore_rt) and the exact instruction sequence.
  96. If you ever feel the need to make any changes, please notify the
  97. appropriate GDB maintainer. */
  98. #define RESTORE(name, syscall) RESTORE2 (name, syscall)
  99. # define RESTORE2(name, syscall) \
  100. asm \
  101. ( \
  102. ".text\n" \
  103. ".align 16\n" \
  104. "__" #name ":\n" \
  105. " movq $" #syscall ", %rax\n" \
  106. " syscall\n" \
  107. );
  108. #ifdef __NR_rt_sigaction
  109. /* The return code for realtime-signals. */
  110. RESTORE (restore_rt, __NR_rt_sigreturn)
  111. #endif
  112. #ifdef __NR_sigreturn
  113. RESTORE (restore, __NR_sigreturn)
  114. #endif