sleep.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. if (__sigemptyset (&set) < 0
  58. || __sigaddset (&set, SIGCHLD) < 0
  59. || sigprocmask (SIG_BLOCK, &set, &oset))
  60. return -1;
  61. /* If SIGCHLD is already blocked, we don't have to do anything. */
  62. if (!__sigismember (&oset, SIGCHLD))
  63. {
  64. int saved_errno;
  65. struct sigaction oact;
  66. if (__sigemptyset (&set) < 0 || __sigaddset (&set, SIGCHLD) < 0)
  67. return -1;
  68. /* We get the signal handler for SIGCHLD. */
  69. if (sigaction (SIGCHLD, (struct sigaction *) NULL, &oact) < 0)
  70. {
  71. saved_errno = errno;
  72. /* Restore the original signal mask. */
  73. (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
  74. __set_errno (saved_errno);
  75. return -1;
  76. }
  77. if (oact.sa_handler == SIG_IGN)
  78. {
  79. /* We should leave SIGCHLD blocked. */
  80. result = nanosleep (&ts, &ts);
  81. saved_errno = errno;
  82. /* Restore the original signal mask. */
  83. (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
  84. __set_errno (saved_errno);
  85. }
  86. else
  87. {
  88. /* We should unblock SIGCHLD. Restore the original signal mask. */
  89. (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
  90. result = nanosleep (&ts, &ts);
  91. }
  92. }
  93. else
  94. result = nanosleep (&ts, &ts);
  95. if (result != 0)
  96. /* Round remaining time. */
  97. result = (unsigned int) ts.tv_sec + (ts.tv_nsec >= 500000000L);
  98. return result;
  99. }
  100. #endif
  101. #else /* __UCLIBC_HAS_REALTIME__ */
  102. /* libc_hidden_proto(sigaction) */
  103. /* no nanosleep, use signals and alarm() */
  104. static void sleep_alarm_handler(int attribute_unused sig)
  105. {
  106. }
  107. unsigned int sleep (unsigned int seconds)
  108. {
  109. struct sigaction act, oact;
  110. sigset_t set, oset;
  111. unsigned int result, remaining;
  112. time_t before, after;
  113. int old_errno = errno;
  114. /* This is not necessary but some buggy programs depend on this. */
  115. if (seconds == 0)
  116. return 0;
  117. /* block SIGALRM */
  118. if (__sigemptyset (&set) < 0
  119. || __sigaddset (&set, SIGALRM) < 0
  120. || sigprocmask (SIG_BLOCK, &set, &oset))
  121. return seconds;
  122. act.sa_handler = sleep_alarm_handler;
  123. act.sa_flags = 0;
  124. act.sa_mask = oset;
  125. if (sigaction(SIGALRM, &act, &oact) < 0)
  126. return seconds;
  127. before = time(NULL);
  128. remaining = alarm(seconds);
  129. if (remaining && remaining > seconds) {
  130. /* restore user's alarm */
  131. (void) sigaction(SIGALRM, &oact, (struct sigaction *) NULL);
  132. alarm(remaining); /* restore old alarm */
  133. sigsuspend(&oset);
  134. after = time(NULL);
  135. } else {
  136. sigsuspend (&oset);
  137. after = time(NULL);
  138. (void) sigaction (SIGALRM, &oact, NULL);
  139. }
  140. result = after - before;
  141. alarm(remaining > result ? remaining - result : 0);
  142. sigprocmask (SIG_SETMASK, &oset, NULL);
  143. __set_errno(old_errno);
  144. return result > seconds ? 0 : seconds - result;
  145. }
  146. #endif /* __UCLIBC_HAS_REALTIME__ */
  147. libc_hidden_def(sleep)