sigaction.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* Copyright (C) 1997, 1998, 1999, 2000 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 Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. 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. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If not,
  13. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  14. Boston, MA 02111-1307, USA. */
  15. #include <errno.h>
  16. #include <signal.h>
  17. #include <string.h>
  18. #include <sys/syscall.h>
  19. /* The difference here is that the sigaction structure used in the
  20. kernel is not the same as we use in the libc. Therefore we must
  21. translate it here. */
  22. #define HAVE_SA_RESTORER
  23. #if defined(__alpha__)
  24. #undef HAVE_SA_RESTORER
  25. /* This is the sigaction struction from the Linux 2.1.20 kernel. */
  26. struct old_kernel_sigaction {
  27. __sighandler_t k_sa_handler;
  28. unsigned long sa_mask;
  29. unsigned int sa_flags;
  30. };
  31. /* This is the sigaction structure from the Linux 2.1.68 kernel. */
  32. struct kernel_sigaction {
  33. __sighandler_t k_sa_handler;
  34. unsigned int sa_flags;
  35. sigset_t sa_mask;
  36. };
  37. #elif defined(__hppa__)
  38. /* We do not support SA_RESTORER on hppa. */
  39. #undef HAVE_SA_RESTORER
  40. /* This is the sigaction struction from the Linux 2.1.20 kernel. */
  41. /* Blah. This is bogus. We don't ever use it. */
  42. struct old_kernel_sigaction {
  43. __sighandler_t k_sa_handler;
  44. unsigned long sa_mask;
  45. unsigned long sa_flags;
  46. };
  47. /* This is the sigaction structure from the Linux 2.1.68 kernel. */
  48. struct kernel_sigaction {
  49. __sighandler_t k_sa_handler;
  50. unsigned long sa_flags;
  51. sigset_t sa_mask;
  52. };
  53. #elif defined(__mips__)
  54. /* This is the sigaction structure from the Linux 2.1.24 kernel. */
  55. #include <sgidefs.h>
  56. struct old_kernel_sigaction {
  57. unsigned int sa_flags;
  58. __sighandler_t k_sa_handler;
  59. unsigned long sa_mask;
  60. unsigned int __pad0[3]; /* reserved, keep size constant */
  61. /* Abi says here follows reserved int[2] */
  62. void (*sa_restorer)(void);
  63. #if (_MIPS_ISA == _MIPS_ISA_MIPS1) || (_MIPS_ISA == _MIPS_ISA_MIPS2)
  64. /* For 32 bit code we have to pad struct sigaction to get
  65. * constant size for the ABI */
  66. int pad1[1]; /* reserved */
  67. #endif
  68. };
  69. #define _KERNEL_NSIG 128
  70. #define _KERNEL_NSIG_BPW 32
  71. #define _KERNEL_NSIG_WORDS (_KERNEL_NSIG / _KERNEL_NSIG_BPW)
  72. typedef struct {
  73. unsigned long sig[_KERNEL_NSIG_WORDS];
  74. } kernel_sigset_t;
  75. /* This is the sigaction structure from the Linux 2.1.68 kernel. */
  76. struct kernel_sigaction {
  77. unsigned int sa_flags;
  78. __sighandler_t k_sa_handler;
  79. kernel_sigset_t sa_mask;
  80. void (*sa_restorer)(void);
  81. int s_resv[1]; /* reserved */
  82. };
  83. #else
  84. /* This is the sigaction structure from the Linux 2.1.20 kernel. */
  85. struct old_kernel_sigaction {
  86. __sighandler_t k_sa_handler;
  87. unsigned long sa_mask;
  88. unsigned long sa_flags;
  89. void (*sa_restorer) (void);
  90. };
  91. /* This is the sigaction structure from the Linux 2.1.68 kernel. */
  92. struct kernel_sigaction {
  93. __sighandler_t k_sa_handler;
  94. unsigned long sa_flags;
  95. void (*sa_restorer) (void);
  96. sigset_t sa_mask;
  97. };
  98. #endif
  99. #if defined __NR_rt_sigaction
  100. extern int __rt_sigaction (int, const struct kernel_sigaction *__unbounded,
  101. struct kernel_sigaction *__unbounded, size_t);
  102. /* If ACT is not NULL, change the action for SIG to *ACT.
  103. If OACT is not NULL, put the old action for SIG in *OACT. */
  104. int __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
  105. {
  106. int result;
  107. struct kernel_sigaction kact, koact;
  108. if (act) {
  109. kact.k_sa_handler = act->sa_handler;
  110. memcpy (&kact.sa_mask, &act->sa_mask, sizeof (kact.sa_mask));
  111. kact.sa_flags = act->sa_flags;
  112. # ifdef HAVE_SA_RESTORER
  113. kact.sa_restorer = act->sa_restorer;
  114. # endif
  115. }
  116. /* XXX The size argument hopefully will have to be changed to the
  117. real size of the user-level sigset_t. */
  118. result = __rt_sigaction(sig, act ? __ptrvalue (&kact) : NULL,
  119. oact ? __ptrvalue (&koact) : NULL, _NSIG / 8);
  120. if (oact && result >= 0) {
  121. oact->sa_handler = koact.k_sa_handler;
  122. memcpy (&oact->sa_mask, &koact.sa_mask, sizeof (oact->sa_mask));
  123. oact->sa_flags = koact.sa_flags;
  124. # ifdef HAVE_SA_RESTORER
  125. oact->sa_restorer = koact.sa_restorer;
  126. # endif
  127. }
  128. return result;
  129. }
  130. weak_alias(__libc_sigaction, sigaction)
  131. #else
  132. extern int __sigaction (int, const struct old_kernel_sigaction *__unbounded,
  133. struct old_kernel_sigaction *__unbounded);
  134. /* If ACT is not NULL, change the action for SIG to *ACT.
  135. If OACT is not NULL, put the old action for SIG in *OACT. */
  136. int __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
  137. {
  138. struct old_kernel_sigaction k_sigact, k_osigact;
  139. int result;
  140. if (act) {
  141. k_sigact.k_sa_handler = act->sa_handler;
  142. k_sigact.sa_mask = act->sa_mask.__val[0];
  143. k_sigact.sa_flags = act->sa_flags;
  144. # ifdef HAVE_SA_RESTORER
  145. k_sigact.sa_restorer = act->sa_restorer;
  146. # endif
  147. }
  148. result = __sigaction(sig, act ? __ptrvalue (&k_sigact) : NULL,
  149. oact ? __ptrvalue (&k_osigact) : NULL);
  150. if (oact && result >= 0) {
  151. oact->sa_handler = k_osigact.k_sa_handler;
  152. oact->sa_mask.__val[0] = k_osigact.sa_mask;
  153. oact->sa_flags = k_osigact.sa_flags;
  154. # ifdef HAVE_SA_RESTORER
  155. oact->sa_restorer = k_osigact.sa_restorer;
  156. # endif
  157. }
  158. return result;
  159. }
  160. weak_alias(__libc_sigaction, sigaction)
  161. #endif