sigaction.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Copyright (C) 2010 Texas Instruments Incorporated
  3. Adapted from i386 version by Mark Salter <msalter@redhat.com>
  4. Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
  5. This file is part of the GNU C Library.
  6. The GNU C Library is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU Library General Public License as
  8. published by the Free Software Foundation; either version 2 of the
  9. License, or (at your option) any later version.
  10. The GNU C Library is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with the GNU C Library; see the file COPYING.LIB. If not,
  16. see <http://www.gnu.org/licenses/>.
  17. Totally hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  18. */
  19. #include <errno.h>
  20. #include <signal.h>
  21. #include <string.h>
  22. #include <sys/syscall.h>
  23. #include <bits/kernel_sigaction.h>
  24. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  25. # include <pthreadP.h> /* SIGCANCEL */
  26. #endif
  27. #define SA_RESTORER 0x04000000
  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 __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
  33. {
  34. int result;
  35. struct kernel_sigaction kact, koact;
  36. #ifdef SIGCANCEL
  37. if (sig == SIGCANCEL) {
  38. __set_errno (EINVAL);
  39. return -1;
  40. }
  41. #endif
  42. if (act) {
  43. kact.k_sa_handler = act->sa_handler;
  44. memcpy (&kact.sa_mask, &act->sa_mask, sizeof (kact.sa_mask));
  45. kact.sa_flags = act->sa_flags;
  46. kact.sa_flags = act->sa_flags | SA_RESTORER;
  47. kact.sa_restorer = ((act->sa_flags & SA_SIGINFO)
  48. ? &restore_rt : &restore);
  49. }
  50. /* XXX The size argument hopefully will have to be changed to the
  51. real size of the user-level sigset_t. */
  52. result = __syscall_rt_sigaction(sig, act ? __ptrvalue (&kact) : NULL,
  53. oact ? __ptrvalue (&koact) : NULL, _NSIG / 8);
  54. if (oact && result >= 0) {
  55. oact->sa_handler = koact.k_sa_handler;
  56. memcpy (&oact->sa_mask, &koact.sa_mask, sizeof (oact->sa_mask));
  57. oact->sa_flags = koact.sa_flags;
  58. oact->sa_restorer = koact.sa_restorer;
  59. }
  60. return result;
  61. }
  62. #ifndef LIBC_SIGACTION
  63. # ifndef __UCLIBC_HAS_THREADS__
  64. strong_alias(__libc_sigaction,sigaction)
  65. libc_hidden_def(sigaction)
  66. # else
  67. weak_alias(__libc_sigaction,sigaction)
  68. libc_hidden_weak(sigaction)
  69. # endif
  70. #endif
  71. /* NOTE: Please think twice before making any changes to the bits of
  72. code below. GDB needs some intimate knowledge about it to
  73. recognize them as signal trampolines, and make backtraces through
  74. signal handlers work right. Important are both the names
  75. (__restore and __restore_rt) and the exact instruction sequence.
  76. If you ever feel the need to make any changes, please notify the
  77. appropriate GDB maintainer. */
  78. #define RESTORE(name, syscall) RESTORE2 (name, syscall)
  79. #define RESTORE2(name, syscall) \
  80. __asm__ \
  81. ( \
  82. " .text\n" \
  83. " .global " #name "\n" \
  84. "__" #name ":\n" \
  85. " MVK " #syscall ",B0\n" \
  86. " SWE\n" \
  87. " NOP\n" \
  88. " NOP\n" \
  89. " NOP\n" \
  90. " NOP\n" \
  91. " NOP\n" \
  92. " NOP\n" \
  93. );
  94. #ifdef __NR_rt_sigaction
  95. /* The return code for realtime-signals. */
  96. RESTORE (restore_rt, __NR_rt_sigreturn)
  97. #endif
  98. #ifdef __NR_sigreturn
  99. /* For the boring old signals. */
  100. RESTORE (restore, __NR_sigreturn)
  101. #endif