sigwait.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 = sigwaitinfo(set, NULL);
  29. if (ret != -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 smallint 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. /* In Linux (as of 2.6.25), fails only if sig is SIGKILL or SIGSTOP */
  69. /* (so, will it work correctly if set has, say, SIGSTOP?) */
  70. if (sigaction (this, &action, &saved[this]) != 0)
  71. goto restore_handler;
  72. }
  73. /* Now we can wait for signals. */
  74. sigsuspend (&tmp_mask);
  75. restore_handler:
  76. save_errno = errno;
  77. while (--this >= 1)
  78. if (__sigismember (set, this))
  79. /* We ignore errors here since we must restore all handlers. */
  80. sigaction (this, &saved[this], NULL);
  81. __set_errno (save_errno);
  82. /* Store the result and return. */
  83. *sig = was_sig;
  84. return was_sig == -1 ? -1 : 0;
  85. }
  86. #endif /* __UCLIBC_HAS_REALTIME__ */
  87. /* libc_hidden_proto(sigwait) */
  88. weak_alias(__sigwait,sigwait)
  89. libc_hidden_def(sigwait)