sleep.c 4.6 KB

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