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. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. Boston, MA 02111-1307, USA.
  18. Totally hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  19. */
  20. #include <errno.h>
  21. #include <signal.h>
  22. #include <string.h>
  23. #include <sys/syscall.h>
  24. #include <bits/kernel_sigaction.h>
  25. #define SA_RESTORER 0x04000000
  26. extern __typeof(sigaction) __libc_sigaction;
  27. extern void restore_rt(void) __asm__ ("__restore_rt") attribute_hidden;
  28. extern void restore(void) __asm__ ("__restore") attribute_hidden;
  29. /* If ACT is not NULL, change the action for SIG to *ACT.
  30. If OACT is not NULL, put the old action for SIG in *OACT. */
  31. int __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
  32. {
  33. int result;
  34. struct kernel_sigaction kact, koact;
  35. #ifdef SIGCANCEL
  36. if (sig == SIGCANCEL) {
  37. __set_errno (EINVAL);
  38. return -1;
  39. }
  40. #endif
  41. if (act) {
  42. kact.k_sa_handler = act->sa_handler;
  43. memcpy (&kact.sa_mask, &act->sa_mask, sizeof (kact.sa_mask));
  44. kact.sa_flags = act->sa_flags;
  45. kact.sa_flags = act->sa_flags | SA_RESTORER;
  46. kact.sa_restorer = ((act->sa_flags & SA_SIGINFO)
  47. ? &restore_rt : &restore);
  48. }
  49. /* XXX The size argument hopefully will have to be changed to the
  50. real size of the user-level sigset_t. */
  51. result = __syscall_rt_sigaction(sig, act ? __ptrvalue (&kact) : NULL,
  52. oact ? __ptrvalue (&koact) : NULL, _NSIG / 8);
  53. if (oact && result >= 0) {
  54. oact->sa_handler = koact.k_sa_handler;
  55. memcpy (&oact->sa_mask, &koact.sa_mask, sizeof (oact->sa_mask));
  56. oact->sa_flags = koact.sa_flags;
  57. oact->sa_restorer = koact.sa_restorer;
  58. }
  59. return result;
  60. }
  61. #ifndef LIBC_SIGACTION
  62. # ifndef __UCLIBC_HAS_THREADS__
  63. strong_alias(__libc_sigaction,sigaction)
  64. libc_hidden_def(sigaction)
  65. # else
  66. weak_alias(__libc_sigaction,sigaction)
  67. libc_hidden_weak(sigaction)
  68. # endif
  69. #endif
  70. /* NOTE: Please think twice before making any changes to the bits of
  71. code below. GDB needs some intimate knowledge about it to
  72. recognize them as signal trampolines, and make backtraces through
  73. signal handlers work right. Important are both the names
  74. (__restore and __restore_rt) and the exact instruction sequence.
  75. If you ever feel the need to make any changes, please notify the
  76. appropriate GDB maintainer. */
  77. #define RESTORE(name, syscall) RESTORE2 (name, syscall)
  78. #define RESTORE2(name, syscall) \
  79. __asm__ \
  80. ( \
  81. " .text\n" \
  82. " .global " #name "\n" \
  83. "__" #name ":\n" \
  84. " MVK " #syscall ",B0\n" \
  85. " SWE\n" \
  86. " NOP\n" \
  87. " NOP\n" \
  88. " NOP\n" \
  89. " NOP\n" \
  90. " NOP\n" \
  91. " NOP\n" \
  92. );
  93. #ifdef __NR_rt_sigaction
  94. /* The return code for realtime-signals. */
  95. RESTORE (restore_rt, __NR_rt_sigreturn)
  96. #endif
  97. #ifdef __NR_sigreturn
  98. /* For the boring old signals. */
  99. RESTORE (restore, __NR_sigreturn)
  100. #endif