sigwait.c 4.3 KB

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