sigaction.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <stddef.h>
  17. #include <signal.h>
  18. #include <string.h>
  19. #include <sys/syscall.h>
  20. #include <bits/kernel_sigaction.h>
  21. /* We do not globally define the SA_RESTORER flag so do it here. */
  22. #define SA_RESTORER 0x04000000
  23. #ifdef __NR_rt_sigaction
  24. /* Using the hidden attribute here does not change the code but it
  25. helps to avoid warnings. */
  26. extern void restore_rt(void) __asm__ ("__restore_rt") attribute_hidden;
  27. extern void restore(void) __asm__ ("__restore") attribute_hidden;
  28. /* If ACT is not NULL, change the action for SIG to *ACT.
  29. If OACT is not NULL, put the old action for SIG in *OACT. */
  30. int
  31. __libc_sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
  32. {
  33. struct sigaction kact;
  34. if (act) {
  35. memcpy(&kact, act, sizeof(kact));
  36. kact.sa_flags |= SA_RESTORER;
  37. kact.sa_restorer = &restore_rt;
  38. act = &kact;
  39. }
  40. /* NB: kernel (as of 2.6.25) will return EINVAL
  41. * if sizeof(act->sa_mask) does not match kernel's sizeof(sigset_t) */
  42. return INLINE_SYSCALL(rt_sigaction, 4, sig, act, oact, sizeof(act->sa_mask));
  43. }
  44. #else
  45. extern void restore(void) __asm__ ("__restore") attribute_hidden;
  46. /* If ACT is not NULL, change the action for SIG to *ACT.
  47. If OACT is not NULL, put the old action for SIG in *OACT. */
  48. int
  49. __libc_sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
  50. {
  51. int result;
  52. struct old_kernel_sigaction kact, koact;
  53. if (act) {
  54. kact.k_sa_handler = act->sa_handler;
  55. kact.sa_mask = act->sa_mask.__val[0];
  56. kact.sa_flags = act->sa_flags | SA_RESTORER;
  57. kact.sa_restorer = &restore;
  58. }
  59. __asm__ __volatile__ (
  60. "syscall\n"
  61. : "=a" (result)
  62. : "0" (__NR_sigaction), "mr" (sig),
  63. "c" (act ? &kact : NULL),
  64. "d" (oact ? &koact : NULL));
  65. if (result < 0) {
  66. __set_errno(-result);
  67. return -1;
  68. }
  69. if (oact) {
  70. oact->sa_handler = koact.k_sa_handler;
  71. oact->sa_mask.__val[0] = koact.sa_mask;
  72. oact->sa_flags = koact.sa_flags;
  73. oact->sa_restorer = koact.sa_restorer;
  74. }
  75. return result;
  76. }
  77. #endif
  78. #ifndef LIBC_SIGACTION
  79. # ifndef __UCLIBC_HAS_THREADS__
  80. strong_alias(__libc_sigaction,sigaction)
  81. libc_hidden_def(sigaction)
  82. # else
  83. weak_alias(__libc_sigaction,sigaction)
  84. libc_hidden_weak(sigaction)
  85. # endif
  86. #endif
  87. /* NOTE: Please think twice before making any changes to the bits of
  88. code below. GDB needs some intimate knowledge about it to
  89. recognize them as signal trampolines, and make backtraces through
  90. signal handlers work right. Important are both the names
  91. (__restore_rt) and the exact instruction sequence.
  92. If you ever feel the need to make any changes, please notify the
  93. appropriate GDB maintainer.
  94. The unwind information starts a byte before __restore_rt, so that
  95. it is found when unwinding, to get an address the unwinder assumes
  96. will be in the middle of a call instruction. See the Linux kernel
  97. (the i386 vsyscall, in particular) for an explanation of the complex
  98. unwind information used here in order to get the traditional CFA.
  99. */
  100. #define RESTORE(name, syscall) RESTORE2(name, syscall)
  101. #define RESTORE2(name, syscall) \
  102. __asm__ ( \
  103. "nop\n" \
  104. ".text\n" \
  105. "__" #name ":\n" \
  106. " movq $" #syscall ", %rax\n" \
  107. " syscall\n" \
  108. );
  109. #ifdef __NR_rt_sigaction
  110. /* The return code for realtime-signals. */
  111. RESTORE(restore_rt, __NR_rt_sigreturn)
  112. #endif
  113. #ifdef __NR_sigreturn
  114. RESTORE(restore, __NR_sigreturn)
  115. #endif