sigwait.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #include <unistd.h>
  24. #if defined __UCLIBC_HAS_REALTIME__
  25. int sigwait(const sigset_t *set, int *sig)
  26. {
  27. int ret = sigwaitinfo(set, NULL);
  28. if (ret != -1) {
  29. *sig = ret;
  30. return 0;
  31. }
  32. return 1;
  33. }
  34. #else /* __UCLIBC_HAS_REALTIME__ */
  35. /* variant without REALTIME extensions */
  36. static smallint was_sig; /* obviously not thread-safe */
  37. static void ignore_signal(int sig)
  38. {
  39. was_sig = sig;
  40. }
  41. int sigwait (const sigset_t *set, int *sig)
  42. {
  43. sigset_t tmp_mask;
  44. struct sigaction saved[NSIG];
  45. struct sigaction action;
  46. int save_errno;
  47. int this;
  48. /* Prepare set. */
  49. __sigfillset (&tmp_mask);
  50. /* Unblock all signals in the SET and register our nice handler. */
  51. action.sa_handler = ignore_signal;
  52. action.sa_flags = 0;
  53. __sigfillset (&action.sa_mask); /* Block all signals for handler. */
  54. /* Make sure we recognize error conditions by setting WAS_SIG to a
  55. value which does not describe a legal signal number. */
  56. was_sig = -1;
  57. for (this = 1; this < NSIG; ++this)
  58. if (__sigismember (set, this))
  59. {
  60. /* Unblock this signal. */
  61. __sigdelset (&tmp_mask, this);
  62. /* Register temporary action handler. */
  63. /* In Linux (as of 2.6.25), fails only if sig is SIGKILL or SIGSTOP */
  64. /* (so, will it work correctly if set has, say, SIGSTOP?) */
  65. if (sigaction (this, &action, &saved[this]) != 0)
  66. goto restore_handler;
  67. }
  68. /* Now we can wait for signals. */
  69. sigsuspend (&tmp_mask);
  70. restore_handler:
  71. save_errno = errno;
  72. while (--this >= 1)
  73. if (__sigismember (set, this))
  74. /* We ignore errors here since we must restore all handlers. */
  75. sigaction (this, &saved[this], NULL);
  76. __set_errno (save_errno);
  77. /* Store the result and return. */
  78. *sig = was_sig;
  79. return was_sig == -1 ? -1 : 0;
  80. }
  81. #endif /* __UCLIBC_HAS_REALTIME__ */