2.9
[glibc/nacl-glibc.git] / sysdeps / posix / sleep.c
blobf961dcd2e3ba1dbe1c5ba922e145c6e210688747
1 /* Copyright (C) 1991, 1992, 1993, 1996, 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the 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 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <signal.h>
20 #include <time.h>
21 #include <unistd.h>
22 #include <errno.h>
25 /* SIGALRM signal handler for `sleep'. This does nothing but return,
26 but SIG_IGN isn't supposed to break `pause'. */
27 static void
28 sleep_handler (int sig)
30 return;
33 /* Make the process sleep for SECONDS seconds, or until a signal arrives
34 and is not ignored. The function returns the number of seconds less
35 than SECONDS which it actually slept (zero if it slept the full time).
36 If a signal handler does a `longjmp' or modifies the handling of the
37 SIGALRM signal while inside `sleep' call, the handling of the SIGALRM
38 signal afterwards is undefined. There is no return value to indicate
39 error, but if `sleep' returns SECONDS, it probably didn't work. */
40 unsigned int
41 __sleep (unsigned int seconds)
43 unsigned int remaining, slept;
44 time_t before, after;
45 sigset_t set, oset;
46 struct sigaction act, oact;
47 int save = errno;
49 if (seconds == 0)
50 return 0;
52 /* Block SIGALRM signals while frobbing the handler. */
53 if (sigemptyset (&set) < 0 ||
54 sigaddset (&set, SIGALRM) < 0 ||
55 sigprocmask (SIG_BLOCK, &set, &oset))
56 return seconds;
58 act.sa_handler = sleep_handler;
59 act.sa_flags = 0;
60 act.sa_mask = oset; /* execute handler with original mask */
61 if (sigaction (SIGALRM, &act, &oact) < 0)
62 return seconds;
64 before = time ((time_t *) NULL);
65 remaining = alarm (seconds);
67 if (remaining > 0 && remaining < seconds)
69 /* The user's alarm will expire before our own would.
70 Restore the user's signal action state and let his alarm happen. */
71 (void) sigaction (SIGALRM, &oact, (struct sigaction *) NULL);
72 alarm (remaining); /* Restore sooner alarm. */
73 sigsuspend (&oset); /* Wait for it to go off. */
74 after = time ((time_t *) NULL);
76 else
78 /* Atomically restore the old signal mask
79 (which had better not block SIGALRM),
80 and wait for a signal to arrive. */
81 sigsuspend (&oset);
83 after = time ((time_t *) NULL);
85 /* Restore the old signal action state. */
86 (void) sigaction (SIGALRM, &oact, (struct sigaction *) NULL);
89 /* Notice how long we actually slept. */
90 slept = after - before;
92 /* Restore the user's alarm if we have not already past it.
93 If we have, be sure to turn off the alarm in case a signal
94 other than SIGALRM was what woke us up. */
95 (void) alarm (remaining > slept ? remaining - slept : 0);
97 /* Restore the original signal mask. */
98 (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
100 /* Restore the `errno' value we started with.
101 Some of the calls we made might have failed, but we didn't care. */
102 __set_errno (save);
104 return slept > seconds ? 0 : seconds - slept;
106 weak_alias (__sleep, sleep)