sigaction.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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(__hppa__)
  24. #undef HAVE_SA_RESTORER
  25. /* This is the sigaction struction from the Linux 2.1.20 kernel. */
  26. /* Blah. This is bogus. We don't ever use it. */
  27. struct old_kernel_sigaction {
  28. __sighandler_t k_sa_handler;
  29. unsigned long sa_mask;
  30. unsigned long sa_flags;
  31. };
  32. /* This is the sigaction structure from the Linux 2.1.68 kernel. */
  33. struct kernel_sigaction {
  34. __sighandler_t k_sa_handler;
  35. unsigned long sa_flags;
  36. sigset_t sa_mask;
  37. };
  38. #elif defined(__mips__)
  39. /* This is the sigaction structure from the Linux 2.1.24 kernel. */
  40. #include <sgidefs.h>
  41. struct old_kernel_sigaction {
  42. unsigned int sa_flags;
  43. __sighandler_t k_sa_handler;
  44. unsigned long sa_mask;
  45. unsigned int __pad0[3]; /* reserved, keep size constant */
  46. /* Abi says here follows reserved int[2] */
  47. void (*sa_restorer)(void);
  48. #if (_MIPS_ISA == _MIPS_ISA_MIPS1) || (_MIPS_ISA == _MIPS_ISA_MIPS2)
  49. /* For 32 bit code we have to pad struct sigaction to get
  50. * constant size for the ABI */
  51. int pad1[1]; /* reserved */
  52. #endif
  53. };
  54. #define _KERNEL_NSIG 128
  55. #define _KERNEL_NSIG_BPW 32
  56. #define _KERNEL_NSIG_WORDS (_KERNEL_NSIG / _KERNEL_NSIG_BPW)
  57. typedef struct {
  58. unsigned long sig[_KERNEL_NSIG_WORDS];
  59. } kernel_sigset_t;
  60. /* This is the sigaction structure from the Linux 2.1.68 kernel. */
  61. struct kernel_sigaction {
  62. unsigned int sa_flags;
  63. __sighandler_t k_sa_handler;
  64. kernel_sigset_t sa_mask;
  65. void (*sa_restorer)(void);
  66. int s_resv[1]; /* reserved */
  67. };
  68. #else
  69. #undef HAVE_SA_RESTORER
  70. /* This is the sigaction struction from the Linux 2.1.20 kernel. */
  71. struct old_kernel_sigaction {
  72. __sighandler_t k_sa_handler;
  73. unsigned long sa_mask;
  74. unsigned int sa_flags;
  75. };
  76. /* This is the sigaction structure from the Linux 2.1.68 kernel. */
  77. struct kernel_sigaction {
  78. __sighandler_t k_sa_handler;
  79. unsigned int sa_flags;
  80. sigset_t sa_mask;
  81. };
  82. #endif
  83. #if defined __NR_rt_sigaction
  84. extern int __rt_sigaction (int, const struct kernel_sigaction *__unbounded,
  85. struct kernel_sigaction *__unbounded, size_t);
  86. /* If ACT is not NULL, change the action for SIG to *ACT.
  87. If OACT is not NULL, put the old action for SIG in *OACT. */
  88. int sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
  89. {
  90. int result;
  91. struct kernel_sigaction kact, koact;
  92. if (act) {
  93. kact.k_sa_handler = act->sa_handler;
  94. memcpy (&kact.sa_mask, &act->sa_mask, sizeof (sigset_t));
  95. kact.sa_flags = act->sa_flags;
  96. # ifdef HAVE_SA_RESTORER
  97. kact.sa_restorer = act->sa_restorer;
  98. # endif
  99. }
  100. /* XXX The size argument hopefully will have to be changed to the
  101. real size of the user-level sigset_t. */
  102. result = __rt_sigaction(sig, act ? __ptrvalue (&kact) : NULL,
  103. oact ? __ptrvalue (&koact) : NULL, _NSIG / 8);
  104. if (oact && result >= 0) {
  105. oact->sa_handler = koact.k_sa_handler;
  106. memcpy (&oact->sa_mask, &koact.sa_mask, sizeof (sigset_t));
  107. oact->sa_flags = koact.sa_flags;
  108. # ifdef HAVE_SA_RESTORER
  109. oact->sa_restorer = koact.sa_restorer;
  110. # endif
  111. }
  112. return result;
  113. }
  114. #else
  115. extern int __sigaction (int, const struct old_kernel_sigaction *__unbounded,
  116. struct old_kernel_sigaction *__unbounded);
  117. /* If ACT is not NULL, change the action for SIG to *ACT.
  118. If OACT is not NULL, put the old action for SIG in *OACT. */
  119. int sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
  120. {
  121. struct old_kernel_sigaction k_sigact, k_osigact;
  122. int result;
  123. if (act) {
  124. k_sigact.k_sa_handler = act->sa_handler;
  125. k_sigact.sa_mask = act->sa_mask.__val[0];
  126. k_sigact.sa_flags = act->sa_flags;
  127. # ifdef HAVE_SA_RESTORER
  128. k_sigact.sa_restorer = act->sa_restorer;
  129. # endif
  130. }
  131. result = __sigaction(sig, act ? __ptrvalue (&k_sigact) : NULL,
  132. oact ? __ptrvalue (&k_osigact) : NULL);
  133. if (oact && result >= 0) {
  134. oact->sa_handler = k_osigact.k_sa_handler;
  135. oact->sa_mask.__val[0] = k_osigact.sa_mask;
  136. oact->sa_flags = k_osigact.sa_flags;
  137. # ifdef HAVE_SA_RESTORER
  138. oact->sa_restorer = k_osigact.sa_restorer;
  139. # endif
  140. }
  141. return result;
  142. }
  143. #endif