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.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
26 libc_hidden_proto(sleep)
28 libc_hidden_proto(sigaction)
29 libc_hidden_proto(sigprocmask)
31 /* version perusing nanosleep */
32 #if defined __UCLIBC_HAS_REALTIME__
33 //libc_hidden_proto(__sigaddset)
34 //libc_hidden_proto(__sigemptyset)
35 //libc_hidden_proto(__sigismember)
36 /*libc_hidden_proto(nanosleep) need the reloc for cancellation*/
39 /* This is a quick and dirty, but not 100% compliant with
40 * the stupid SysV SIGCHLD vs. SIG_IGN behaviour. It is
41 * fine unless you are messing with SIGCHLD... */
42 unsigned int sleep (unsigned int sec)
45 struct timespec ts = { .tv_sec = (long int) seconds, .tv_nsec = 0 };
46 res = nanosleep(&ts, &ts);
47 if (res) res = (unsigned int) ts.tv_sec + (ts.tv_nsec >= 500000000L);
53 /* We are going to use the `nanosleep' syscall of the kernel. But the
54 kernel does not implement the sstupid SysV SIGCHLD vs. SIG_IGN
55 behaviour for this syscall. Therefore we have to emulate it here. */
56 unsigned int sleep (unsigned int seconds)
58 struct timespec ts = { .tv_sec = (long int) seconds, .tv_nsec = 0 };
62 /* This is not necessary but some buggy programs depend on this. */
66 /* Linux will wake up the system call, nanosleep, when SIGCHLD
67 arrives even if SIGCHLD is ignored. We have to deal with it
68 in libc. We block SIGCHLD first. */
69 if (__sigemptyset (&set) < 0
70 || __sigaddset (&set, SIGCHLD) < 0
71 || sigprocmask (SIG_BLOCK, &set, &oset))
74 /* If SIGCHLD is already blocked, we don't have to do anything. */
75 if (!__sigismember (&oset, SIGCHLD))
78 struct sigaction oact;
80 if (__sigemptyset (&set) < 0 || __sigaddset (&set, SIGCHLD) < 0)
83 /* We get the signal handler for SIGCHLD. */
84 if (sigaction (SIGCHLD, (struct sigaction *) NULL, &oact) < 0)
87 /* Restore the original signal mask. */
88 (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
89 __set_errno (saved_errno);
93 if (oact.sa_handler == SIG_IGN)
95 /* We should leave SIGCHLD blocked. */
96 result = nanosleep (&ts, &ts);
99 /* Restore the original signal mask. */
100 (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
101 __set_errno (saved_errno);
105 /* We should unblock SIGCHLD. Restore the original signal mask. */
106 (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
107 result = nanosleep (&ts, &ts);
111 result = nanosleep (&ts, &ts);
114 /* Round remaining time. */
115 result = (unsigned int) ts.tv_sec + (ts.tv_nsec >= 500000000L);
120 #else /* __UCLIBC_HAS_REALTIME__ */
121 libc_hidden_proto(sigaction)
122 /* no nanosleep, use signals and alarm() */
123 static void sleep_alarm_handler(int attribute_unused sig)
126 unsigned int sleep (unsigned int seconds)
128 struct sigaction act, oact;
130 unsigned int result, remaining;
131 time_t before, after;
132 int old_errno = errno;
134 /* This is not necessary but some buggy programs depend on this. */
139 if (__sigemptyset (&set) < 0
140 || __sigaddset (&set, SIGALRM) < 0
141 || sigprocmask (SIG_BLOCK, &set, &oset))
144 act.sa_handler = sleep_alarm_handler;
147 if (sigaction(SIGALRM, &act, &oact) < 0)
151 remaining = alarm(seconds);
152 if (remaining && remaining > seconds) {
153 /* restore user's alarm */
154 (void) sigaction(SIGALRM, &oact, (struct sigaction *) NULL);
155 alarm(remaining); /* restore old alarm */
161 (void) sigaction (SIGALRM, &oact, NULL);
163 result = after - before;
164 alarm(remaining > result ? remaining - result : 0);
165 sigprocmask (SIG_SETMASK, &oset, NULL);
167 __set_errno(old_errno);
169 return result > seconds ? 0 : seconds - result;
171 #endif /* __UCLIBC_HAS_REALTIME__ */
172 libc_hidden_def(sleep)