sigaction.c 3.8 KB

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