sigaction.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <stddef.h>
  18. #include <signal.h>
  19. #include <string.h>
  20. #include <sys/syscall.h>
  21. /* The difference here is that the sigaction structure used in the
  22. kernel is not the same as we use in the libc. Therefore we must
  23. translate it here. */
  24. #include <bits/kernel_sigaction.h>
  25. /* We do not globally define the SA_RESTORER flag so do it here. */
  26. #define SA_RESTORER 0x04000000
  27. #if defined __NR_rt_sigaction
  28. /* Using the hidden attribute here does not change the code but it
  29. helps to avoid warnings. */
  30. extern void restore_rt (void) asm ("__restore_rt") attribute_hidden;
  31. extern void restore (void) asm ("__restore") attribute_hidden;
  32. /* If ACT is not NULL, change the action for SIG to *ACT.
  33. If OACT is not NULL, put the old action for SIG in *OACT. */
  34. int
  35. __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
  36. {
  37. int result;
  38. struct kernel_sigaction kact, koact;
  39. if (act) {
  40. kact.k_sa_handler = act->sa_handler;
  41. memcpy (&kact.sa_mask, &act->sa_mask, sizeof (sigset_t));
  42. kact.sa_flags = act->sa_flags | SA_RESTORER;
  43. kact.sa_restorer = &restore_rt;
  44. }
  45. /* XXX The size argument hopefully will have to be changed to the
  46. real size of the user-level sigset_t. */
  47. result = INLINE_SYSCALL (rt_sigaction, 4,
  48. sig, act ? __ptrvalue (&kact) : NULL,
  49. oact ? __ptrvalue (&koact) : NULL, _NSIG / 8);
  50. if (oact && result >= 0) {
  51. oact->sa_handler = koact.k_sa_handler;
  52. memcpy (&oact->sa_mask, &koact.sa_mask, sizeof (sigset_t));
  53. oact->sa_flags = koact.sa_flags;
  54. oact->sa_restorer = koact.sa_restorer;
  55. }
  56. return result;
  57. }
  58. #else
  59. extern void restore (void) asm ("__restore") attribute_hidden;
  60. /* If ACT is not NULL, change the action for SIG to *ACT.
  61. If OACT is not NULL, put the old action for SIG in *OACT. */
  62. int
  63. __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
  64. {
  65. int result;
  66. struct old_kernel_sigaction kact, koact;
  67. #ifdef SIGCANCEL
  68. if (sig == SIGCANCEL) {
  69. __set_errno (EINVAL);
  70. return -1;
  71. }
  72. #endif
  73. if (act) {
  74. kact.k_sa_handler = act->sa_handler;
  75. kact.sa_mask = act->sa_mask.__val[0];
  76. kact.sa_flags = act->sa_flags | SA_RESTORER;
  77. kact.sa_restorer = &restore;
  78. }
  79. asm volatile ("syscall\n"
  80. : "=a" (result)
  81. : "0" (__NR_sigaction), "mr" (sig),
  82. "c" (act ? __ptrvalue (&kact) : 0),
  83. "d" (oact ? __ptrvalue (&koact) : 0));
  84. if (result < 0) {
  85. __set_errno(-result);
  86. return -1;
  87. }
  88. if (oact) {
  89. oact->sa_handler = koact.k_sa_handler;
  90. oact->sa_mask.__val[0] = koact.sa_mask;
  91. oact->sa_flags = koact.sa_flags;
  92. oact->sa_restorer = koact.sa_restorer;
  93. }
  94. return result;
  95. }
  96. #endif
  97. weak_alias (__libc_sigaction, sigaction)
  98. /* NOTE: Please think twice before making any changes to the bits of
  99. code below. GDB needs some intimate knowledge about it to
  100. recognize them as signal trampolines, and make backtraces through
  101. signal handlers work right. Important are both the names
  102. (__restore_rt) and the exact instruction sequence.
  103. If you ever feel the need to make any changes, please notify the
  104. appropriate GDB maintainer. */
  105. #define RESTORE(name, syscall) RESTORE2 (name, syscall)
  106. # define RESTORE2(name, syscall) \
  107. asm \
  108. ( \
  109. ".text\n" \
  110. ".align 16\n" \
  111. "__" #name ":\n" \
  112. " movq $" #syscall ", %rax\n" \
  113. " syscall\n" \
  114. );
  115. #ifdef __NR_rt_sigaction
  116. /* The return code for realtime-signals. */
  117. RESTORE (restore_rt, __NR_rt_sigreturn)
  118. #endif
  119. #ifdef __NR_sigreturn
  120. RESTORE (restore, __NR_sigreturn)
  121. #endif