sleep.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* Implementation of the POSIX sleep function using nanosleep.
  2. Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with the GNU C Library; see the file COPYING.LIB. If not,
  15. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. Boston, MA 02111-1307, USA. */
  17. #include <errno.h>
  18. #include <time.h>
  19. #include <signal.h>
  20. #include <unistd.h>
  21. /* version perusing nanosleep */
  22. #if defined __UCLIBC_HAS_REALTIME__
  23. /* I am unable to reproduce alleged "Linux quirk".
  24. * I used the following test program:
  25. #include <unistd.h>
  26. #include <time.h>
  27. #include <signal.h>
  28. static void dummy(int sig) {}
  29. int main() {
  30. struct timespec t = { 2, 0 };
  31. if (fork() == 0) {
  32. sleep(1);
  33. return 0;
  34. }
  35. signal(SIGCHLD, SIG_DFL); //
  36. signal(SIGCHLD, dummy); // Pick one
  37. signal(SIGCHLD, SIG_IGN); //
  38. nanosleep(&t, &t);
  39. return 0;
  40. }
  41. * Testing on 2.4.20 and on 2.6.35-rc4:
  42. * With SIG_DFL, nanosleep() is not interrupted by SIGCHLD. Ok.
  43. * With dummy handler, nanosleep() is interrupted by SIGCHLD. Ok.
  44. * With SIG_IGN, nanosleep() is NOT interrupted by SIGCHLD.
  45. * It looks like sleep's workaround for SIG_IGN is no longer needed?
  46. * The only emails I can find are from 1998 -
  47. * google for "sleep ignore sigchld".
  48. */
  49. # if 0
  50. /* This is a quick and dirty, but not 100% compliant with
  51. * the stupid SysV SIGCHLD vs. SIG_IGN behaviour. It is
  52. * fine unless you are messing with SIGCHLD... */
  53. unsigned int sleep (unsigned int sec)
  54. {
  55. unsigned int res;
  56. struct timespec ts = { .tv_sec = (long int) seconds, .tv_nsec = 0 };
  57. res = nanosleep(&ts, &ts);
  58. if (res) res = (unsigned int) ts.tv_sec + (ts.tv_nsec >= 500000000L);
  59. return res;
  60. }
  61. # else
  62. /* We are going to use the `nanosleep' syscall of the kernel. But the
  63. kernel does not implement the sstupid SysV SIGCHLD vs. SIG_IGN
  64. behaviour for this syscall. Therefore we have to emulate it here. */
  65. unsigned int sleep (unsigned int seconds)
  66. {
  67. struct timespec ts = { .tv_sec = (long int) seconds, .tv_nsec = 0 };
  68. sigset_t set;
  69. struct sigaction oact;
  70. unsigned int result;
  71. /* This is not necessary but some buggy programs depend on this. */
  72. if (seconds == 0) {
  73. # ifdef CANCELLATION_P
  74. CANCELLATION_P (THREAD_SELF);
  75. # endif
  76. return 0;
  77. }
  78. /* Linux will wake up the system call, nanosleep, when SIGCHLD
  79. arrives even if SIGCHLD is ignored. We have to deal with it
  80. in libc. */
  81. __sigemptyset (&set);
  82. __sigaddset (&set, SIGCHLD);
  83. /* Is SIGCHLD set to SIG_IGN? */
  84. sigaction (SIGCHLD, NULL, &oact); /* never fails */
  85. if (oact.sa_handler == SIG_IGN) {
  86. /* Yes. Block SIGCHLD, save old mask. */
  87. sigprocmask (SIG_BLOCK, &set, &set); /* never fails */
  88. }
  89. /* Run nanosleep, with SIGCHLD blocked if SIGCHLD is SIG_IGNed. */
  90. result = nanosleep (&ts, &ts);
  91. if (result != 0) {
  92. /* Got EINTR. Return remaining time. */
  93. result = (unsigned int) ts.tv_sec + (ts.tv_nsec >= 500000000L);
  94. }
  95. if (!__sigismember (&set, SIGCHLD)) {
  96. /* We did block SIGCHLD, and old mask had no SIGCHLD bit.
  97. IOW: we need to unblock SIGCHLD now. Do it. */
  98. /* this sigprocmask call never fails, thus never updates errno,
  99. and therefore we don't need to save/restore it. */
  100. sigprocmask (SIG_SETMASK, &set, NULL); /* never fails */
  101. }
  102. return result;
  103. }
  104. # endif
  105. #else /* __UCLIBC_HAS_REALTIME__ */
  106. /* no nanosleep, use signals and alarm() */
  107. static void sleep_alarm_handler(int attribute_unused sig)
  108. {
  109. }
  110. unsigned int sleep (unsigned int seconds)
  111. {
  112. struct sigaction act, oact;
  113. sigset_t set, oset;
  114. unsigned int result, remaining;
  115. time_t before, after;
  116. int old_errno = errno;
  117. /* This is not necessary but some buggy programs depend on this. */
  118. if (seconds == 0)
  119. return 0;
  120. /* block SIGALRM */
  121. __sigemptyset (&set);
  122. __sigaddset (&set, SIGALRM);
  123. sigprocmask (SIG_BLOCK, &set, &oset); /* can't fail */
  124. act.sa_handler = sleep_alarm_handler;
  125. act.sa_flags = 0;
  126. act.sa_mask = oset;
  127. sigaction(SIGALRM, &act, &oact); /* never fails */
  128. before = time(NULL);
  129. remaining = alarm(seconds);
  130. if (remaining && remaining > seconds) {
  131. /* restore user's alarm */
  132. sigaction(SIGALRM, &oact, NULL);
  133. alarm(remaining); /* restore old alarm */
  134. sigsuspend(&oset);
  135. after = time(NULL);
  136. } else {
  137. sigsuspend (&oset);
  138. after = time(NULL);
  139. sigaction (SIGALRM, &oact, NULL);
  140. }
  141. result = after - before;
  142. alarm(remaining > result ? remaining - result : 0);
  143. sigprocmask (SIG_SETMASK, &oset, NULL);
  144. __set_errno(old_errno);
  145. return result > seconds ? 0 : seconds - result;
  146. }
  147. #endif /* __UCLIBC_HAS_REALTIME__ */
  148. libc_hidden_def(sleep)