sleep.c 5.9 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. 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. * ----------
  48. * Subject: Re: sleep ignore sigchld
  49. * From: Linus Torvalds <torvalds@transmeta.com>
  50. * Date: Mon, 16 Nov 1998 11:02:15 -0800 (PST)
  51. *
  52. * On Mon, 16 Nov 1998, H. J. Lu wrote:
  53. * > That is a kernel bug. SIGCHLD is a special one. Usually it cannot
  54. * > be ignored. [snip...]
  55. *
  56. * No can do.
  57. *
  58. * "nanosleep()" is implemented in a bad way that makes it impossible to
  59. * restart it cleanly. It was done that way because glibc wanted it that way,
  60. * not because it's a good idea. [snip...]
  61. * ----------
  62. * I assume that in the passed twelve+ years, nanosleep got fixed,
  63. * but the hack in sleep to work around broken nanosleep was never removed.
  64. */
  65. # if 0
  66. /* This is a quick and dirty, but not 100% compliant with
  67. * the stupid SysV SIGCHLD vs. SIG_IGN behaviour. It is
  68. * fine unless you are messing with SIGCHLD... */
  69. unsigned int sleep (unsigned int sec)
  70. {
  71. unsigned int res;
  72. struct timespec ts = { .tv_sec = (long int) seconds, .tv_nsec = 0 };
  73. res = nanosleep(&ts, &ts);
  74. if (res) res = (unsigned int) ts.tv_sec + (ts.tv_nsec >= 500000000L);
  75. return res;
  76. }
  77. # else
  78. /* We are going to use the `nanosleep' syscall of the kernel. But the
  79. kernel does not implement the sstupid SysV SIGCHLD vs. SIG_IGN
  80. behaviour for this syscall. Therefore we have to emulate it here. */
  81. unsigned int sleep (unsigned int seconds)
  82. {
  83. struct timespec ts = { .tv_sec = (long int) seconds, .tv_nsec = 0 };
  84. sigset_t set;
  85. struct sigaction oact;
  86. unsigned int result;
  87. /* This is not necessary but some buggy programs depend on this. */
  88. if (seconds == 0) {
  89. # ifdef CANCELLATION_P
  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)