sigaction.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. Boston, MA 02111-1307, USA.
  15. Totally hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  16. */
  17. #include <errno.h>
  18. #include <signal.h>
  19. #include <string.h>
  20. #include <sys/syscall.h>
  21. #include <bits/kernel_sigaction.h>
  22. #define SA_RESTORER 0x04000000
  23. #if defined __NR_rt_sigaction
  24. libc_hidden_proto(memcpy)
  25. extern void restore_rt (void) asm ("__restore_rt") attribute_hidden;
  26. extern void restore (void) asm ("__restore") attribute_hidden;
  27. /* If ACT is not NULL, change the action for SIG to *ACT.
  28. If OACT is not NULL, put the old action for SIG in *OACT. */
  29. int __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
  30. {
  31. int result;
  32. struct kernel_sigaction kact, koact;
  33. #ifdef SIGCANCEL
  34. if (sig == SIGCANCEL) {
  35. __set_errno (EINVAL);
  36. return -1;
  37. }
  38. #endif
  39. if (act) {
  40. kact.k_sa_handler = act->sa_handler;
  41. memcpy (&kact.sa_mask, &act->sa_mask, sizeof (kact.sa_mask));
  42. kact.sa_flags = act->sa_flags;
  43. kact.sa_flags = act->sa_flags | SA_RESTORER;
  44. kact.sa_restorer = ((act->sa_flags & SA_SIGINFO)
  45. ? &restore_rt : &restore);
  46. }
  47. /* XXX The size argument hopefully will have to be changed to the
  48. real size of the user-level sigset_t. */
  49. result = __syscall_rt_sigaction(sig, act ? __ptrvalue (&kact) : NULL,
  50. oact ? __ptrvalue (&koact) : NULL, _NSIG / 8);
  51. if (oact && result >= 0) {
  52. oact->sa_handler = koact.k_sa_handler;
  53. memcpy (&oact->sa_mask, &koact.sa_mask, sizeof (oact->sa_mask));
  54. oact->sa_flags = koact.sa_flags;
  55. oact->sa_restorer = koact.sa_restorer;
  56. }
  57. return result;
  58. }
  59. #else
  60. extern void restore (void) asm ("__restore") attribute_hidden;
  61. /* If ACT is not NULL, change the action for SIG to *ACT.
  62. If OACT is not NULL, put the old action for SIG in *OACT. */
  63. int __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 ("pushl %%ebx\n"
  80. "movl %2, %%ebx\n"
  81. "int $0x80\n"
  82. "popl %%ebx"
  83. : "=a" (result)
  84. : "0" (__NR_sigaction), "mr" (sig),
  85. "c" (act ? __ptrvalue (&kact) : 0),
  86. "d" (oact ? __ptrvalue (&koact) : 0));
  87. if (result < 0) {
  88. __set_errno(-result);
  89. return -1;
  90. }
  91. if (oact) {
  92. oact->sa_handler = koact.k_sa_handler;
  93. oact->sa_mask.__val[0] = koact.sa_mask;
  94. oact->sa_flags = koact.sa_flags;
  95. oact->sa_restorer = koact.sa_restorer;
  96. }
  97. return result;
  98. }
  99. #endif
  100. #ifndef LIBC_SIGACTION
  101. libc_hidden_proto(sigaction)
  102. strong_alias(__libc_sigaction,sigaction)
  103. libc_hidden_def(sigaction)
  104. #endif
  105. /* NOTE: Please think twice before making any changes to the bits of
  106. code below. GDB needs some intimate knowledge about it to
  107. recognize them as signal trampolines, and make backtraces through
  108. signal handlers work right. Important are both the names
  109. (__restore and __restore_rt) and the exact instruction sequence.
  110. If you ever feel the need to make any changes, please notify the
  111. appropriate GDB maintainer. */
  112. #define RESTORE(name, syscall) RESTORE2 (name, syscall)
  113. #define RESTORE2(name, syscall) \
  114. asm \
  115. ( \
  116. ".text\n" \
  117. " .align 16\n" \
  118. "__" #name ":\n" \
  119. " movl $" #syscall ", %eax\n" \
  120. " int $0x80" \
  121. );
  122. #ifdef __NR_rt_sigaction
  123. /* The return code for realtime-signals. */
  124. RESTORE (restore_rt, __NR_rt_sigreturn)
  125. #endif
  126. /* For the boring old signals. */
  127. # undef RESTORE2
  128. # define RESTORE2(name, syscall) \
  129. asm \
  130. ( \
  131. ".text\n" \
  132. " .align 8\n" \
  133. "__" #name ":\n" \
  134. " popl %eax\n" \
  135. " movl $" #syscall ", %eax\n" \
  136. " int $0x80" \
  137. );
  138. RESTORE (restore, __NR_sigreturn)