sigwait.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* vi: set sw=4 ts=4: */
  2. /* sigwait
  3. *
  4. * Copyright (C) 2003 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; if not, write to the Free
  18. * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  19. * 02111-1307 USA. */
  20. #include <errno.h>
  21. #include <signal.h>
  22. #include <string.h>
  23. #if defined __UCLIBC_HAS_REALTIME__
  24. libc_hidden_proto(sigwaitinfo)
  25. int __sigwait (const sigset_t *set, int *sig) attribute_hidden;
  26. int __sigwait (const sigset_t *set, int *sig)
  27. {
  28. int ret = 1;
  29. if ((ret = sigwaitinfo(set, NULL)) != -1) {
  30. *sig = ret;
  31. return 0;
  32. }
  33. return 1;
  34. }
  35. #else /* __UCLIBC_HAS_REALTIME__ */
  36. /* variant without REALTIME extensions */
  37. libc_hidden_proto(sigfillset)
  38. libc_hidden_proto(sigaction)
  39. libc_hidden_proto(sigsuspend)
  40. static int was_sig; /* obviously not thread-safe */
  41. static void ignore_signal(int sig)
  42. {
  43. was_sig = sig;
  44. }
  45. int __sigwait (const sigset_t *set, int *sig) attribute_hidden;
  46. int __sigwait (const sigset_t *set, int *sig)
  47. {
  48. sigset_t tmp_mask;
  49. struct sigaction saved[NSIG];
  50. struct sigaction action;
  51. int save_errno;
  52. int this;
  53. /* Prepare set. */
  54. sigfillset (&tmp_mask);
  55. /* Unblock all signals in the SET and register our nice handler. */
  56. action.sa_handler = ignore_signal;
  57. action.sa_flags = 0;
  58. sigfillset (&action.sa_mask); /* Block all signals for handler. */
  59. /* Make sure we recognize error conditions by setting WAS_SIG to a
  60. value which does not describe a legal signal number. */
  61. was_sig = -1;
  62. for (this = 1; this < NSIG; ++this)
  63. if (__sigismember (set, this))
  64. {
  65. /* Unblock this signal. */
  66. __sigdelset (&tmp_mask, this);
  67. /* Register temporary action handler. */
  68. if (sigaction (this, &action, &saved[this]) != 0)
  69. goto restore_handler;
  70. }
  71. /* Now we can wait for signals. */
  72. sigsuspend (&tmp_mask);
  73. restore_handler:
  74. save_errno = errno;
  75. while (--this >= 1)
  76. if (__sigismember (set, this))
  77. /* We ignore errors here since we must restore all handlers. */
  78. sigaction (this, &saved[this], NULL);
  79. __set_errno (save_errno);
  80. /* Store the result and return. */
  81. *sig = was_sig;
  82. return was_sig == -1 ? -1 : 0;
  83. }
  84. #endif /* __UCLIBC_HAS_REALTIME__ */
  85. libc_hidden_proto(sigwait)
  86. weak_alias(__sigwait,sigwait)
  87. libc_hidden_def(sigwait)