sigaction.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If not,
  13. see <http://www.gnu.org/licenses/>.
  14. Totally hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  15. */
  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. extern void restore_rt(void) __asm__ ("__restore_rt") attribute_hidden;
  24. extern void restore(void) __asm__ ("__restore") attribute_hidden;
  25. /* If ACT is not NULL, change mask, handler and flags for SIG to ACT's
  26. If OACT is not NULL, put the old action for SIG in *OACT. */
  27. int __libc_sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
  28. {
  29. struct sigaction kact;
  30. if (act) {
  31. memcpy(&kact.sa_mask, &act->sa_mask, sizeof(sigset_t));
  32. kact.sa_handler = act->sa_handler;
  33. kact.sa_flags = act->sa_flags;
  34. kact.sa_restorer = (act->sa_flags & SA_SIGINFO) ? &restore_rt : &restore;
  35. act = &kact;
  36. }
  37. /* NB: kernel (as of 2.6.25) will return EINVAL
  38. * if sizeof(act->sa_mask) does not match kernel's sizeof(sigset_t) */
  39. return __syscall_rt_sigaction(sig, act, oact, sizeof(act->sa_mask));
  40. }
  41. #else
  42. extern void restore(void) __asm__ ("__restore") attribute_hidden;
  43. /* If ACT is not NULL, change the action for SIG to *ACT.
  44. If OACT is not NULL, put the old action for SIG in *OACT. */
  45. int __libc_sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
  46. {
  47. int result;
  48. struct old_kernel_sigaction kact, koact;
  49. if (act) {
  50. kact.k_sa_handler = act->sa_handler;
  51. kact.sa_mask = act->sa_mask.__val[0];
  52. kact.sa_flags = act->sa_flags | SA_RESTORER;
  53. kact.sa_restorer = &restore;
  54. }
  55. __asm__ __volatile__ (
  56. " pushl %%ebx\n"
  57. " movl %3, %%ebx\n"
  58. " int $0x80\n"
  59. " popl %%ebx\n"
  60. : "=a" (result), "=m" (koact)
  61. : "0" (__NR_sigaction), "r" (sig), "m" (kact),
  62. "c" (act ? &kact : NULL),
  63. "d" (oact ? &koact : NULL));
  64. if (result < 0) {
  65. __set_errno(-result);
  66. return -1;
  67. }
  68. if (oact) {
  69. oact->sa_handler = koact.k_sa_handler;
  70. oact->sa_mask.__val[0] = koact.sa_mask;
  71. oact->sa_flags = koact.sa_flags;
  72. oact->sa_restorer = koact.sa_restorer;
  73. }
  74. return result;
  75. }
  76. #endif
  77. #ifndef LIBC_SIGACTION
  78. # ifndef __UCLIBC_HAS_THREADS__
  79. strong_alias(__libc_sigaction,sigaction)
  80. libc_hidden_def(sigaction)
  81. # else
  82. weak_alias(__libc_sigaction,sigaction)
  83. libc_hidden_weak(sigaction)
  84. # endif
  85. #endif
  86. /* NOTE: Please think twice before making any changes to the bits of
  87. code below. GDB needs some intimate knowledge about it to
  88. recognize them as signal trampolines, and make backtraces through
  89. signal handlers work right. Important are both the names
  90. (__restore and __restore_rt) and the exact instruction sequence.
  91. If you ever feel the need to make any changes, please notify the
  92. appropriate GDB maintainer. */
  93. #define RESTORE(name, syscall) RESTORE2(name, syscall)
  94. #ifdef __NR_rt_sigaction
  95. /* The return code for realtime-signals. */
  96. # define RESTORE2(name, syscall) \
  97. __asm__ ( \
  98. "nop\n" \
  99. ".text\n" \
  100. "__" #name ":\n" \
  101. " movl $" #syscall ", %eax\n" \
  102. " int $0x80\n" \
  103. );
  104. RESTORE(restore_rt, __NR_rt_sigreturn)
  105. #endif
  106. #ifdef __NR_sigreturn
  107. /* For the boring old signals. */
  108. # undef RESTORE2
  109. # define RESTORE2(name, syscall) \
  110. __asm__ ( \
  111. "nop\n" \
  112. ".text\n" \
  113. "__" #name ":\n" \
  114. " popl %eax\n" \
  115. " movl $" #syscall ", %eax\n" \
  116. " int $0x80\n" \
  117. );
  118. RESTORE(restore, __NR_sigreturn)
  119. #endif