sleep.c 4.8 KB

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