sleep.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #if 0
  24. /* This is a quick and dirty, but not 100% compliant with
  25. * the stupid SysV SIGCHLD vs. SIG_IGN behaviour. It is
  26. * fine unless you are messing with SIGCHLD... */
  27. unsigned int sleep (unsigned int sec)
  28. {
  29. unsigned int res;
  30. struct timespec ts = { .tv_sec = (long int) seconds, .tv_nsec = 0 };
  31. res = nanosleep(&ts, &ts);
  32. if (res) res = (unsigned int) ts.tv_sec + (ts.tv_nsec >= 500000000L);
  33. return res;
  34. }
  35. #else
  36. /* We are going to use the `nanosleep' syscall of the kernel. But the
  37. kernel does not implement the sstupid SysV SIGCHLD vs. SIG_IGN
  38. behaviour for this syscall. Therefore we have to emulate it here. */
  39. unsigned int sleep (unsigned int seconds)
  40. {
  41. struct timespec ts = { .tv_sec = (long int) seconds, .tv_nsec = 0 };
  42. sigset_t set;
  43. unsigned int result;
  44. /* This is not necessary but some buggy programs depend on this. */
  45. if (seconds == 0) {
  46. # ifdef CANCELLATION_P
  47. CANCELLATION_P (THREAD_SELF);
  48. # endif
  49. return 0;
  50. }
  51. /* Linux will wake up the system call, nanosleep, when SIGCHLD
  52. arrives even if SIGCHLD is ignored. We have to deal with it
  53. in libc. We block SIGCHLD first. */
  54. __sigemptyset (&set);
  55. __sigaddset (&set, SIGCHLD);
  56. sigprocmask (SIG_BLOCK, &set, &set); /* never fails */
  57. /* If SIGCHLD was already blocked, no need to check SIG_IGN. Else... */
  58. if (!__sigismember (&set, SIGCHLD)) {
  59. struct sigaction oact;
  60. /* Is SIGCHLD set to SIG_IGN? */
  61. sigaction (SIGCHLD, NULL, &oact); /* never fails */
  62. if (oact.sa_handler == SIG_IGN) {
  63. //int saved_errno;
  64. /* Yes, run nanosleep with SIGCHLD blocked. */
  65. result = nanosleep (&ts, &ts);
  66. /* Unblock SIGCHLD by restoring signal mask. */
  67. /* this sigprocmask call never fails, thus never updates errno,
  68. and therefore we don't need to save/restore it. */
  69. //saved_errno = errno;
  70. sigprocmask (SIG_SETMASK, &set, NULL);
  71. //__set_errno (saved_errno);
  72. } else {
  73. /* No workaround needed, unblock SIGCHLD by restoring signal mask. */
  74. sigprocmask (SIG_SETMASK, &set, NULL);
  75. result = nanosleep (&ts, &ts);
  76. }
  77. }
  78. else
  79. result = nanosleep (&ts, &ts);
  80. if (result != 0)
  81. /* Round remaining time. */
  82. result = (unsigned int) ts.tv_sec + (ts.tv_nsec >= 500000000L);
  83. return result;
  84. }
  85. #endif
  86. #else /* __UCLIBC_HAS_REALTIME__ */
  87. /* no nanosleep, use signals and alarm() */
  88. static void sleep_alarm_handler(int attribute_unused sig)
  89. {
  90. }
  91. unsigned int sleep (unsigned int seconds)
  92. {
  93. struct sigaction act, oact;
  94. sigset_t set, oset;
  95. unsigned int result, remaining;
  96. time_t before, after;
  97. int old_errno = errno;
  98. /* This is not necessary but some buggy programs depend on this. */
  99. if (seconds == 0)
  100. return 0;
  101. /* block SIGALRM */
  102. __sigemptyset (&set);
  103. __sigaddset (&set, SIGALRM);
  104. sigprocmask (SIG_BLOCK, &set, &oset); /* can't fail */
  105. act.sa_handler = sleep_alarm_handler;
  106. act.sa_flags = 0;
  107. act.sa_mask = oset;
  108. sigaction(SIGALRM, &act, &oact); /* never fails */
  109. before = time(NULL);
  110. remaining = alarm(seconds);
  111. if (remaining && remaining > seconds) {
  112. /* restore user's alarm */
  113. sigaction(SIGALRM, &oact, NULL);
  114. alarm(remaining); /* restore old alarm */
  115. sigsuspend(&oset);
  116. after = time(NULL);
  117. } else {
  118. sigsuspend (&oset);
  119. after = time(NULL);
  120. sigaction (SIGALRM, &oact, NULL);
  121. }
  122. result = after - before;
  123. alarm(remaining > result ? remaining - result : 0);
  124. sigprocmask (SIG_SETMASK, &oset, NULL);
  125. __set_errno(old_errno);
  126. return result > seconds ? 0 : seconds - result;
  127. }
  128. #endif /* __UCLIBC_HAS_REALTIME__ */
  129. libc_hidden_def(sleep)