sigaction.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. weak_alias(__libc_sigaction,sigaction)
  63. libc_hidden_weak(sigaction)
  64. #endif
  65. /* NOTE: Please think twice before making any changes to the bits of
  66. code below. GDB needs some intimate knowledge about it to
  67. recognize them as signal trampolines, and make backtraces through
  68. signal handlers work right. Important are both the names
  69. (__restore and __restore_rt) and the exact instruction sequence.
  70. If you ever feel the need to make any changes, please notify the
  71. appropriate GDB maintainer. */
  72. #define RESTORE(name, syscall) RESTORE2 (name, syscall)
  73. #define RESTORE2(name, syscall) \
  74. __asm__ \
  75. ( \
  76. " .text\n" \
  77. " .global " #name "\n" \
  78. "__" #name ":\n" \
  79. " MVK " #syscall ",B0\n" \
  80. " SWE\n" \
  81. " NOP\n" \
  82. " NOP\n" \
  83. " NOP\n" \
  84. " NOP\n" \
  85. " NOP\n" \
  86. " NOP\n" \
  87. );
  88. #ifdef __NR_rt_sigaction
  89. /* The return code for realtime-signals. */
  90. RESTORE (restore_rt, __NR_rt_sigreturn)
  91. #endif
  92. #ifdef __NR_sigreturn
  93. /* For the boring old signals. */
  94. RESTORE (restore, __NR_sigreturn)
  95. #endif