sleep.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. int cancelhandling;
  91. CANCELLATION_P (THREAD_SELF);
  92. # endif
  93. return 0;
  94. }
  95. /* Linux will wake up the system call, nanosleep, when SIGCHLD
  96. arrives even if SIGCHLD is ignored. We have to deal with it
  97. in libc. */
  98. __sigemptyset (&set);
  99. __sigaddset (&set, SIGCHLD);
  100. /* Is SIGCHLD set to SIG_IGN? */
  101. sigaction (SIGCHLD, NULL, &oact); /* never fails */
  102. if (oact.sa_handler == SIG_IGN) {
  103. /* Yes. Block SIGCHLD, save old mask. */
  104. sigprocmask (SIG_BLOCK, &set, &set); /* never fails */
  105. }
  106. /* Run nanosleep, with SIGCHLD blocked if SIGCHLD is SIG_IGNed. */
  107. result = nanosleep (&ts, &ts);
  108. if (result != 0) {
  109. /* Got EINTR. Return remaining time. */
  110. result = (unsigned int) ts.tv_sec + (ts.tv_nsec >= 500000000L);
  111. }
  112. if (!__sigismember (&set, SIGCHLD)) {
  113. /* We did block SIGCHLD, and old mask had no SIGCHLD bit.
  114. IOW: we need to unblock SIGCHLD now. Do it. */
  115. /* this sigprocmask call never fails, thus never updates errno,
  116. and therefore we don't need to save/restore it. */
  117. sigprocmask (SIG_SETMASK, &set, NULL); /* never fails */
  118. }
  119. return result;
  120. }
  121. # endif
  122. #else /* __UCLIBC_HAS_REALTIME__ */
  123. /* no nanosleep, use signals and alarm() */
  124. static void sleep_alarm_handler(int attribute_unused sig)
  125. {
  126. }
  127. unsigned int sleep (unsigned int seconds)
  128. {
  129. struct sigaction act, oact;
  130. sigset_t set, oset;
  131. unsigned int result, remaining;
  132. time_t before, after;
  133. int old_errno = errno;
  134. /* This is not necessary but some buggy programs depend on this. */
  135. if (seconds == 0)
  136. return 0;
  137. /* block SIGALRM */
  138. __sigemptyset (&set);
  139. __sigaddset (&set, SIGALRM);
  140. sigprocmask (SIG_BLOCK, &set, &oset); /* can't fail */
  141. act.sa_handler = sleep_alarm_handler;
  142. act.sa_flags = 0;
  143. act.sa_mask = oset;
  144. sigaction(SIGALRM, &act, &oact); /* never fails */
  145. before = time(NULL);
  146. remaining = alarm(seconds);
  147. if (remaining && remaining > seconds) {
  148. /* restore user's alarm */
  149. sigaction(SIGALRM, &oact, NULL);
  150. alarm(remaining); /* restore old alarm */
  151. sigsuspend(&oset);
  152. after = time(NULL);
  153. } else {
  154. sigsuspend (&oset);
  155. after = time(NULL);
  156. sigaction (SIGALRM, &oact, NULL);
  157. }
  158. result = after - before;
  159. alarm(remaining > result ? remaining - result : 0);
  160. sigprocmask (SIG_SETMASK, &oset, NULL);
  161. __set_errno(old_errno);
  162. return result > seconds ? 0 : seconds - result;
  163. }
  164. #endif /* __UCLIBC_HAS_REALTIME__ */
  165. libc_hidden_def(sleep)