sleep.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. //libc_hidden_proto(__sigaddset)
  25. //libc_hidden_proto(__sigemptyset)
  26. //libc_hidden_proto(__sigismember)
  27. libc_hidden_proto(nanosleep)
  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, oset;
  48. unsigned int result;
  49. /* This is not necessary but some buggy programs depend on this. */
  50. if (seconds == 0)
  51. return 0;
  52. /* Linux will wake up the system call, nanosleep, when SIGCHLD
  53. arrives even if SIGCHLD is ignored. We have to deal with it
  54. in libc. We block SIGCHLD first. */
  55. if (__sigemptyset (&set) < 0
  56. || __sigaddset (&set, SIGCHLD) < 0
  57. || sigprocmask (SIG_BLOCK, &set, &oset))
  58. return -1;
  59. /* If SIGCHLD is already blocked, we don't have to do anything. */
  60. if (!__sigismember (&oset, SIGCHLD))
  61. {
  62. int saved_errno;
  63. struct sigaction oact;
  64. if (__sigemptyset (&set) < 0 || __sigaddset (&set, SIGCHLD) < 0)
  65. return -1;
  66. /* We get the signal handler for SIGCHLD. */
  67. if (sigaction (SIGCHLD, (struct sigaction *) NULL, &oact) < 0)
  68. {
  69. saved_errno = errno;
  70. /* Restore the original signal mask. */
  71. (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
  72. __set_errno (saved_errno);
  73. return -1;
  74. }
  75. if (oact.sa_handler == SIG_IGN)
  76. {
  77. /* We should leave SIGCHLD blocked. */
  78. result = nanosleep (&ts, &ts);
  79. saved_errno = errno;
  80. /* Restore the original signal mask. */
  81. (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
  82. __set_errno (saved_errno);
  83. }
  84. else
  85. {
  86. /* We should unblock SIGCHLD. Restore the original signal mask. */
  87. (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
  88. result = nanosleep (&ts, &ts);
  89. }
  90. }
  91. else
  92. result = nanosleep (&ts, &ts);
  93. if (result != 0)
  94. /* Round remaining time. */
  95. result = (unsigned int) ts.tv_sec + (ts.tv_nsec >= 500000000L);
  96. return result;
  97. }
  98. #endif
  99. libc_hidden_def(sleep)