sigwait.c 3.2 KB

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