sigaction.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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, see
  13. <http://www.gnu.org/licenses/>.
  14. Totally hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  15. */
  16. #include <errno.h>
  17. #include <signal.h>
  18. #include <string.h>
  19. #include <sys/syscall.h>
  20. #include <bits/kernel_sigaction.h>
  21. #define SA_RESTORER 0x04000000
  22. extern void __default_sa_restorer(void);
  23. extern void __default_rt_sa_restorer(void);
  24. /* When RT signals are in use we need to use a different return stub. */
  25. #ifdef __NR_rt_sigreturn
  26. #define choose_restorer(flags) \
  27. (flags & SA_SIGINFO) ? __default_rt_sa_restorer \
  28. : __default_sa_restorer
  29. #else
  30. #define choose_restorer(flags) \
  31. __default_sa_restorer
  32. #endif
  33. #ifdef __NR_rt_sigaction
  34. /* If ACT is not NULL, change the action for SIG to *ACT.
  35. If OACT is not NULL, put the old action for SIG in *OACT. */
  36. int __libc_sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
  37. {
  38. struct sigaction kact;
  39. if (act && !(act->sa_flags & SA_RESTORER)) {
  40. memcpy(&kact, act, sizeof(kact));
  41. kact.sa_restorer = choose_restorer(kact.sa_flags);
  42. kact.sa_flags |= SA_RESTORER;
  43. act = &kact;
  44. }
  45. /* NB: kernel (as of 2.6.25) will return EINVAL
  46. * if sizeof(act->sa_mask) does not match kernel's sizeof(sigset_t) */
  47. return __syscall_rt_sigaction(sig, act, oact, sizeof(act->sa_mask));
  48. }
  49. #else
  50. /* If ACT is not NULL, change the action for SIG to *ACT.
  51. If OACT is not NULL, put the old action for SIG in *OACT. */
  52. int __libc_sigaction(int sig, const struct sigaction *act, struct sigaction *oact)
  53. {
  54. int result;
  55. struct old_kernel_sigaction kact, koact;
  56. if (act) {
  57. kact.k_sa_handler = act->sa_handler;
  58. kact.sa_mask = act->sa_mask.__val[0];
  59. kact.sa_flags = act->sa_flags;
  60. if (kact.sa_flags & SA_RESTORER) {
  61. kact.sa_restorer = act->sa_restorer;
  62. } else {
  63. kact.sa_restorer = choose_restorer(kact.sa_flags);
  64. kact.sa_flags |= SA_RESTORER;
  65. }
  66. }
  67. result = __syscall_sigaction(sig,
  68. act ? &kact : NULL,
  69. oact ? &koact : NULL);
  70. if (oact && result >= 0) {
  71. oact->sa_handler = koact.k_sa_handler;
  72. oact->sa_mask.__val[0] = koact.sa_mask;
  73. oact->sa_flags = koact.sa_flags;
  74. oact->sa_restorer = koact.sa_restorer;
  75. }
  76. return result;
  77. }
  78. #endif
  79. #ifndef LIBC_SIGACTION
  80. # ifndef __UCLIBC_HAS_THREADS__
  81. strong_alias(__libc_sigaction,sigaction)
  82. libc_hidden_def(sigaction)
  83. # else
  84. weak_alias(__libc_sigaction,sigaction)
  85. libc_hidden_weak(sigaction)
  86. # endif
  87. #endif