sleep.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. see <http://www.gnu.org/licenses/>. */
  16. #include <errno.h>
  17. #include <time.h>
  18. #include <signal.h>
  19. #include <unistd.h>
  20. /* version perusing nanosleep */
  21. #if defined __UCLIBC_HAS_REALTIME__
  22. /* I am unable to reproduce alleged "Linux quirk".
  23. * I used the following test program:
  24. #include <unistd.h>
  25. #include <time.h>
  26. #include <signal.h>
  27. static void dummy(int sig) {}
  28. int main() {
  29. struct timespec t = { 2, 0 };
  30. if (fork() == 0) {
  31. sleep(1);
  32. return 0;
  33. }
  34. signal(SIGCHLD, SIG_DFL); //
  35. signal(SIGCHLD, dummy); // Pick one
  36. signal(SIGCHLD, SIG_IGN); //
  37. nanosleep(&t, &t);
  38. return 0;
  39. }
  40. * Testing on 2.4.20 and on 2.6.35-rc4:
  41. * With SIG_DFL, nanosleep is not interrupted by SIGCHLD. Ok.
  42. * With dummy handler, nanosleep is interrupted by SIGCHLD. Ok.
  43. * With SIG_IGN, nanosleep is NOT interrupted by SIGCHLD.
  44. * It looks like sleep's workaround for SIG_IGN is no longer needed?
  45. * The only emails I can find are from 1998 (!):
  46. * ----------
  47. * Subject: Re: sleep ignore sigchld
  48. * From: Linus Torvalds <torvalds@transmeta.com>
  49. * Date: Mon, 16 Nov 1998 11:02:15 -0800 (PST)
  50. *
  51. * On Mon, 16 Nov 1998, H. J. Lu wrote:
  52. * > That is a kernel bug. SIGCHLD is a special one. Usually it cannot
  53. * > be ignored. [snip...]
  54. *
  55. * No can do.
  56. *
  57. * "nanosleep()" is implemented in a bad way that makes it impossible to
  58. * restart it cleanly. It was done that way because glibc wanted it that way,
  59. * not because it's a good idea. [snip...]
  60. * ----------
  61. * I assume that in the passed twelve+ years, nanosleep got fixed,
  62. * but the hack in sleep to work around broken nanosleep was never removed.
  63. */
  64. # if 0
  65. /* This is a quick and dirty, but not 100% compliant with
  66. * the stupid SysV SIGCHLD vs. SIG_IGN behaviour. It is
  67. * fine unless you are messing with SIGCHLD... */
  68. unsigned int sleep (unsigned int sec)
  69. {
  70. unsigned int res;
  71. struct timespec ts = { .tv_sec = (long int) seconds, .tv_nsec = 0 };
  72. res = nanosleep(&ts, &ts);
  73. if (res) res = (unsigned int) ts.tv_sec + (ts.tv_nsec >= 500000000L);
  74. return res;
  75. }
  76. # else
  77. /* We are going to use the `nanosleep' syscall of the kernel. But the
  78. kernel does not implement the sstupid SysV SIGCHLD vs. SIG_IGN
  79. behaviour for this syscall. Therefore we have to emulate it here. */
  80. unsigned int sleep (unsigned int seconds)
  81. {
  82. struct timespec ts = { .tv_sec = (long int) seconds, .tv_nsec = 0 };
  83. sigset_t set;
  84. struct sigaction oact;
  85. unsigned int result;
  86. /* This is not necessary but some buggy programs depend on this. */
  87. if (seconds == 0) {
  88. # ifdef CANCELLATION_P
  89. int cancelhandling;
  90. CANCELLATION_P (THREAD_SELF);
  91. # endif
  92. return 0;
  93. }
  94. /* Linux will wake up the system call, nanosleep, when SIGCHLD
  95. arrives even if SIGCHLD is ignored. We have to deal with it
  96. in libc. */
  97. __sigemptyset (&set);
  98. __sigaddset (&set, SIGCHLD);
  99. /* Is SIGCHLD set to SIG_IGN? */
  100. sigaction (SIGCHLD, NULL, &oact); /* never fails */
  101. if (oact.sa_handler == SIG_IGN) {
  102. /* Yes. Block SIGCHLD, save old mask. */
  103. sigprocmask (SIG_BLOCK, &set, &set); /* never fails */
  104. }
  105. /* Run nanosleep, with SIGCHLD blocked if SIGCHLD is SIG_IGNed. */
  106. result = nanosleep (&ts, &ts);
  107. if (result != 0) {
  108. /* Got EINTR. Return remaining time. */
  109. result = (unsigned int) ts.tv_sec + (ts.tv_nsec >= 500000000L);
  110. }
  111. if (!__sigismember (&set, SIGCHLD)) {
  112. /* We did block SIGCHLD, and old mask had no SIGCHLD bit.
  113. IOW: we need to unblock SIGCHLD now. Do it. */
  114. /* this sigprocmask call never fails, thus never updates errno,
  115. and therefore we don't need to save/restore it. */
  116. sigprocmask (SIG_SETMASK, &set, NULL); /* never fails */
  117. }
  118. return result;
  119. }
  120. # endif
  121. #else /* __UCLIBC_HAS_REALTIME__ */
  122. /* no nanosleep, use signals and alarm() */
  123. static void sleep_alarm_handler(int attribute_unused sig)
  124. {
  125. }
  126. unsigned int sleep (unsigned int seconds)
  127. {
  128. struct sigaction act, oact;
  129. sigset_t set, oset;
  130. unsigned int result, remaining;
  131. time_t before, after;
  132. int old_errno = errno;
  133. /* This is not necessary but some buggy programs depend on this. */
  134. if (seconds == 0)
  135. return 0;
  136. /* block SIGALRM */
  137. __sigemptyset (&set);
  138. __sigaddset (&set, SIGALRM);
  139. sigprocmask (SIG_BLOCK, &set, &oset); /* can't fail */
  140. act.sa_handler = sleep_alarm_handler;
  141. act.sa_flags = 0;
  142. act.sa_mask = oset;
  143. sigaction(SIGALRM, &act, &oact); /* never fails */
  144. before = time(NULL);
  145. remaining = alarm(seconds);
  146. if (remaining && remaining > seconds) {
  147. /* restore user's alarm */
  148. sigaction(SIGALRM, &oact, NULL);
  149. alarm(remaining); /* restore old alarm */
  150. sigsuspend(&oset);
  151. after = time(NULL);
  152. } else {
  153. sigsuspend (&oset);
  154. after = time(NULL);
  155. sigaction (SIGALRM, &oact, NULL);
  156. }
  157. result = after - before;
  158. alarm(remaining > result ? remaining - result : 0);
  159. sigprocmask (SIG_SETMASK, &oset, NULL);
  160. __set_errno(old_errno);
  161. return result > seconds ? 0 : seconds - result;
  162. }
  163. #endif /* __UCLIBC_HAS_REALTIME__ */
  164. libc_hidden_def(sleep)