sigwait.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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; if not, write to the Free
  19. * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  20. * 02111-1307 USA. */
  21. #include <errno.h>
  22. #include <signal.h>
  23. #include <string.h>
  24. #ifdef __UCLIBC_HAS_THREADS_NATIVE__
  25. # include <sysdep-cancel.h>
  26. # ifdef __NR_rt_sigtimedwait
  27. /* Return any pending signal or wait for one for the given time. */
  28. static int do_sigwait(const sigset_t *set, int *sig)
  29. {
  30. int ret;
  31. # ifdef SIGCANCEL
  32. sigset_t tmpset;
  33. if (set != NULL
  34. && (__builtin_expect (__sigismember (set, SIGCANCEL), 0)
  35. # ifdef SIGSETXID
  36. || __builtin_expect (__sigismember (set, SIGSETXID), 0)
  37. # endif
  38. ))
  39. {
  40. /* Create a temporary mask without the bit for SIGCANCEL set. */
  41. // We are not copying more than we have to.
  42. memcpy(&tmpset, set, _NSIG / 8);
  43. __sigdelset(&tmpset, SIGCANCEL);
  44. # ifdef SIGSETXID
  45. __sigdelset(&tmpset, SIGSETXID);
  46. # endif
  47. set = &tmpset;
  48. }
  49. # endif
  50. /* XXX The size argument hopefully will have to be changed to the
  51. real size of the user-level sigset_t. */
  52. INTERNAL_SYSCALL_DECL(err);
  53. do
  54. ret = INTERNAL_SYSCALL (rt_sigtimedwait, err, 4, set, NULL,
  55. NULL, _NSIG / 8);
  56. while (INTERNAL_SYSCALL_ERROR_P (ret, err)
  57. && INTERNAL_SYSCALL_ERRNO (ret, err) == EINTR);
  58. if (! INTERNAL_SYSCALL_ERROR_P (ret, err))
  59. {
  60. *sig = ret;
  61. ret = 0;
  62. }
  63. else
  64. ret = INTERNAL_SYSCALL_ERRNO (ret, err);
  65. return ret;
  66. }
  67. int sigwait (const sigset_t *set, int *sig)
  68. {
  69. if(SINGLE_THREAD_P)
  70. return do_sigwait(set, sig);
  71. int oldtype = LIBC_CANCEL_ASYNC();
  72. int result = do_sigwait(set, sig);
  73. LIBC_CANCEL_RESET(oldtype);
  74. return result;
  75. }
  76. # else /* __NR_rt_sigtimedwait */
  77. # error We must have rt_sigtimedwait defined!!!
  78. # endif
  79. #else /* __UCLIBC_HAS_THREADS_NATIVE__ */
  80. # if defined __UCLIBC_HAS_REALTIME__
  81. int sigwait (const sigset_t *set, int *sig)
  82. {
  83. int ret = 1;
  84. if ((ret = sigwaitinfo(set, NULL)) != -1) {
  85. *sig = ret;
  86. return 0;
  87. }
  88. return 1;
  89. }
  90. # else /* __UCLIBC_HAS_REALTIME__ */
  91. /* variant without REALTIME extensions */
  92. static smallint was_sig; /* obviously not thread-safe */
  93. static void ignore_signal(int sig)
  94. {
  95. was_sig = sig;
  96. }
  97. int sigwait (const sigset_t *set, int *sig)
  98. {
  99. sigset_t tmp_mask;
  100. struct sigaction saved[NSIG];
  101. struct sigaction action;
  102. int save_errno;
  103. int this;
  104. /* Prepare set. */
  105. __sigfillset (&tmp_mask);
  106. /* Unblock all signals in the SET and register our nice handler. */
  107. action.sa_handler = ignore_signal;
  108. action.sa_flags = 0;
  109. __sigfillset (&action.sa_mask); /* Block all signals for handler. */
  110. /* Make sure we recognize error conditions by setting WAS_SIG to a
  111. value which does not describe a legal signal number. */
  112. was_sig = -1;
  113. for (this = 1; this < NSIG; ++this)
  114. if (__sigismember (set, this))
  115. {
  116. /* Unblock this signal. */
  117. __sigdelset (&tmp_mask, this);
  118. /* Register temporary action handler. */
  119. /* In Linux (as of 2.6.25), fails only if sig is SIGKILL or SIGSTOP */
  120. /* (so, will it work correctly if set has, say, SIGSTOP?) */
  121. if (sigaction (this, &action, &saved[this]) != 0)
  122. goto restore_handler;
  123. }
  124. /* Now we can wait for signals. */
  125. sigsuspend (&tmp_mask);
  126. restore_handler:
  127. save_errno = errno;
  128. while (--this >= 1)
  129. if (__sigismember (set, this))
  130. /* We ignore errors here since we must restore all handlers. */
  131. sigaction (this, &saved[this], NULL);
  132. __set_errno (save_errno);
  133. /* Store the result and return. */
  134. *sig = was_sig;
  135. return was_sig == -1 ? -1 : 0;
  136. }
  137. # endif /* __UCLIBC_HAS_REALTIME__ */
  138. #endif /* __UCLIBC_HAS_THREADS_NATIVE__ */