sigaction.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright (C) 1997-2000,2002,2003,2005,2006 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. #include <features.h>
  16. #include <errno.h>
  17. #include <signal.h>
  18. #include <string.h>
  19. #include <sys/syscall.h>
  20. /* Experimentally off - libc_hidden_proto(memcpy) */
  21. /* The difference here is that the sigaction structure used in the
  22. kernel is not the same as we use in the libc. Therefore we must
  23. translate it here. */
  24. #include <bits/kernel_sigaction.h>
  25. #ifndef LIBC_SIGACTION
  26. extern __typeof(sigaction) __libc_sigaction;
  27. #endif
  28. #if defined __NR_rt_sigaction
  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
  32. __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
  33. {
  34. int result;
  35. struct kernel_sigaction kact, koact;
  36. enum {
  37. /* We try hard to actually have them equal,
  38. * but just for paranoid reasons, be safe */
  39. SIGSET_MIN_SIZE = sizeof(kact.sa_mask) < sizeof(act->sa_mask)
  40. ? sizeof(kact.sa_mask) : sizeof(act->sa_mask)
  41. };
  42. if (act) {
  43. kact.k_sa_handler = act->sa_handler;
  44. memcpy (&kact.sa_mask, &act->sa_mask, SIGSET_MIN_SIZE);
  45. kact.sa_flags = act->sa_flags;
  46. # ifdef HAVE_SA_RESTORER
  47. kact.sa_restorer = act->sa_restorer;
  48. # endif
  49. }
  50. /* NB: kernel (as of 2.6.25) will return EINVAL
  51. * if sizeof(kact.sa_mask) does not match kernel's sizeof(sigset_t) */
  52. result = __syscall_rt_sigaction(sig,
  53. act ? &kact : NULL,
  54. oact ? &koact : NULL,
  55. sizeof(kact.sa_mask));
  56. if (oact && result >= 0) {
  57. oact->sa_handler = koact.k_sa_handler;
  58. memcpy (&oact->sa_mask, &koact.sa_mask, SIGSET_MIN_SIZE);
  59. oact->sa_flags = koact.sa_flags;
  60. # ifdef HAVE_SA_RESTORER
  61. oact->sa_restorer = koact.sa_restorer;
  62. # endif
  63. }
  64. return result;
  65. }
  66. #else
  67. /* If ACT is not NULL, change the action for SIG to *ACT.
  68. If OACT is not NULL, put the old action for SIG in *OACT. */
  69. int
  70. __libc_sigaction (int sig, const struct sigaction *act, struct sigaction *oact)
  71. {
  72. int result;
  73. struct old_kernel_sigaction kact, koact;
  74. if (act) {
  75. kact.k_sa_handler = act->sa_handler;
  76. kact.sa_mask = act->sa_mask.__val[0];
  77. kact.sa_flags = act->sa_flags;
  78. # ifdef HAVE_SA_RESTORER
  79. kact.sa_restorer = act->sa_restorer;
  80. # endif
  81. }
  82. result = __syscall_sigaction(sig,
  83. act ? &kact : NULL,
  84. oact ? &koact : NULL);
  85. if (oact && result >= 0) {
  86. oact->sa_handler = koact.k_sa_handler;
  87. oact->sa_mask.__val[0] = koact.sa_mask;
  88. oact->sa_flags = koact.sa_flags;
  89. # ifdef HAVE_SA_RESTORER
  90. oact->sa_restorer = koact.sa_restorer;
  91. # endif
  92. }
  93. return result;
  94. }
  95. #endif
  96. #ifndef LIBC_SIGACTION
  97. /* libc_hidden_proto(sigaction) */
  98. weak_alias(__libc_sigaction,sigaction)
  99. libc_hidden_weak(sigaction)
  100. #endif