sigaction.c 3.7 KB

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