sigwait.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* sigwait
  2. *
  3. * Copyright (C) 2006 by Steven J. Hill <sjhill@realitydiluted.com>
  4. * Copyright (C) 2003-2005 by Erik Andersen <andersen@uclibc.org>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * The GNU C Library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with the GNU C Library; see the file COPYING.LIB. If
  18. * not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #define __need_NULL
  21. #include <stddef.h>
  22. #include <sys/syscall.h>
  23. #include <signal.h>
  24. #include <cancel.h>
  25. #if defined __NR_rt_sigtimedwait && defined __UCLIBC_HAS_REALTIME__
  26. #include <string.h>
  27. /* Return any pending signal or wait for one for the given time. */
  28. static int __NC(sigwait)(const sigset_t *set, int *sig)
  29. {
  30. int ret;
  31. do
  32. /* we might as well use sigtimedwait and do not care about cancellation */
  33. ret = __NC(sigtimedwait)(set, NULL, NULL);
  34. while (ret == -1 && errno == EINTR);
  35. if (ret != -1) {
  36. *sig = ret;
  37. ret = 0;
  38. } else
  39. ret = errno;
  40. return ret;
  41. }
  42. #else /* __NR_rt_sigtimedwait */
  43. /* variant without REALTIME extensions */
  44. #include <unistd.h> /* smallint */
  45. static smallint was_sig; /* obviously not thread-safe */
  46. static void ignore_signal(int sig)
  47. {
  48. was_sig = sig;
  49. }
  50. static int __NC(sigwait)(const sigset_t *set, int *sig)
  51. {
  52. sigset_t tmp_mask;
  53. struct sigaction saved[NSIG];
  54. struct sigaction action;
  55. int save_errno;
  56. int this;
  57. /* Prepare set. */
  58. __sigfillset (&tmp_mask);
  59. /* Unblock all signals in the SET and register our nice handler. */
  60. action.sa_handler = ignore_signal;
  61. action.sa_flags = 0;
  62. __sigfillset (&action.sa_mask); /* Block all signals for handler. */
  63. /* Make sure we recognize error conditions by setting WAS_SIG to a
  64. value which does not describe a legal signal number. */
  65. was_sig = -1;
  66. for (this = 1; this < NSIG; ++this)
  67. if (__sigismember (set, this))
  68. {
  69. /* Unblock this signal. */
  70. __sigdelset (&tmp_mask, this);
  71. /* Register temporary action handler. */
  72. /* In Linux (as of 2.6.25), fails only if sig is SIGKILL or SIGSTOP */
  73. /* (so, will it work correctly if set has, say, SIGSTOP?) */
  74. if (sigaction (this, &action, &saved[this]) != 0)
  75. goto restore_handler;
  76. }
  77. /* Now we can wait for signals. */
  78. __NC(sigsuspend)(&tmp_mask);
  79. restore_handler:
  80. save_errno = errno;
  81. while (--this >= 1)
  82. if (__sigismember (set, this))
  83. /* We ignore errors here since we must restore all handlers. */
  84. sigaction (this, &saved[this], NULL);
  85. __set_errno (save_errno);
  86. /* Store the result and return. */
  87. *sig = was_sig;
  88. return was_sig == -1 ? -1 : 0;
  89. }
  90. #endif /* __NR_rt_sigtimedwait */
  91. CANCELLABLE_SYSCALL(int, sigwait, (const sigset_t *set, int *sig), (set, sig))