sigaction.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Copyright (C) 1997,1998,1999,2000,2002,2003 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 Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the 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. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 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. extern int __syscall_sigaction (int, const struct old_kernel_sigaction *__unbounded,
  23. struct old_kernel_sigaction *__unbounded);
  24. extern int __syscall_rt_sigaction (int, const struct kernel_sigaction *__unbounded,
  25. struct kernel_sigaction *__unbounded, size_t);
  26. #define SA_RESTORER 0x04000000
  27. extern void __default_sa_restorer(void);
  28. extern void __default_rt_sa_restorer(void);
  29. /* When RT signals are in use we need to use a different return stub. */
  30. #ifdef __NR_rt_sigreturn
  31. #define choose_restorer(flags) \
  32. (flags & SA_SIGINFO) ? __default_rt_sa_restorer \
  33. : __default_sa_restorer
  34. #else
  35. #define choose_restorer(flags) \
  36. __default_sa_restorer
  37. #endif
  38. #ifdef __NR_rt_sigaction
  39. /* If ACT is not NULL, change the action for SIG to *ACT.
  40. If OACT is not NULL, put the old action for SIG in *OACT. */
  41. int __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
  42. {
  43. int result;
  44. struct kernel_sigaction kact, koact;
  45. if (act) {
  46. kact.k_sa_handler = act->sa_handler;
  47. memcpy (&kact.sa_mask, &act->sa_mask, sizeof (sigset_t));
  48. kact.sa_flags = act->sa_flags;
  49. # ifdef HAVE_SA_RESTORER
  50. /* If the user specified SA_ONSTACK this means she is trying to
  51. use the old-style stack switching. Unfortunately this
  52. requires the sa_restorer field so we cannot install our own
  53. handler. (In fact the user is likely to be out of luck anyway
  54. since the kernel currently only supports stack switching via
  55. the X/Open sigaltstack interface, but we allow for the
  56. possibility that this might change in the future.) */
  57. if (kact.sa_flags & (SA_RESTORER | SA_ONSTACK)) {
  58. kact.sa_restorer = act->sa_restorer;
  59. } else {
  60. kact.sa_restorer = choose_restorer (kact.sa_flags);
  61. kact.sa_flags |= SA_RESTORER;
  62. }
  63. # endif
  64. }
  65. /* XXX The size argument hopefully will have to be changed to the
  66. real size of the user-level sigset_t. */
  67. result = __syscall_rt_sigaction(sig, act ? __ptrvalue (&kact) : NULL,
  68. oact ? __ptrvalue (&koact) : NULL, _NSIG / 8);
  69. if (oact && result >= 0) {
  70. oact->sa_handler = koact.k_sa_handler;
  71. memcpy (&oact->sa_mask, &koact.sa_mask, sizeof (sigset_t));
  72. oact->sa_flags = koact.sa_flags;
  73. # ifdef HAVE_SA_RESTORER
  74. oact->sa_restorer = koact.sa_restorer;
  75. # endif
  76. }
  77. return result;
  78. }
  79. #else
  80. /* If ACT is not NULL, change the action for SIG to *ACT.
  81. If OACT is not NULL, put the old action for SIG in *OACT. */
  82. int __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
  83. {
  84. int result;
  85. struct old_kernel_sigaction kact, koact;
  86. if (act) {
  87. kact.k_sa_handler = act->sa_handler;
  88. kact.sa_mask = act->sa_mask.__val[0];
  89. kact.sa_flags = act->sa_flags;
  90. # ifdef HAVE_SA_RESTORER
  91. /* See the comments above for why we test SA_ONSTACK. */
  92. if (kact.sa_flags & (SA_RESTORER | SA_ONSTACK)) {
  93. kact.sa_restorer = act->sa_restorer;
  94. } else {
  95. kact.sa_restorer = choose_restorer (kact.sa_flags);
  96. kact.sa_flags |= SA_RESTORER;
  97. }
  98. # endif
  99. }
  100. result = __syscall_sigaction(sig, act ? __ptrvalue (&kact) : NULL,
  101. oact ? __ptrvalue (&koact) : NULL);
  102. if (oact && result >= 0) {
  103. oact->sa_handler = koact.k_sa_handler;
  104. oact->sa_mask.__val[0] = koact.sa_mask;
  105. oact->sa_flags = koact.sa_flags;
  106. # ifdef HAVE_SA_RESTORER
  107. oact->sa_restorer = koact.sa_restorer;
  108. # endif
  109. }
  110. return result;
  111. }
  112. #endif
  113. weak_alias(__libc_sigaction, sigaction)