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, see
16 <http://www.gnu.org/licenses/>. */
24 /* SIGALRM signal handler for `sleep'. This does nothing but return,
25 but SIG_IGN isn't supposed to break `pause'. */
27 sleep_handler (int sig
)
32 /* Make the process sleep for SECONDS seconds, or until a signal arrives
33 and is not ignored. The function returns the number of seconds less
34 than SECONDS which it actually slept (zero if it slept the full time).
35 If a signal handler does a `longjmp' or modifies the handling of the
36 SIGALRM signal while inside `sleep' call, the handling of the SIGALRM
37 signal afterwards is undefined. There is no return value to indicate
38 error, but if `sleep' returns SECONDS, it probably didn't work. */
40 __sleep (unsigned int seconds
)
42 unsigned int remaining
, slept
;
45 struct sigaction act
, oact
;
51 /* Block SIGALRM signals while frobbing the handler. */
52 if (sigemptyset (&set
) < 0 ||
53 sigaddset (&set
, SIGALRM
) < 0 ||
54 sigprocmask (SIG_BLOCK
, &set
, &oset
))
57 act
.sa_handler
= sleep_handler
;
59 act
.sa_mask
= oset
; /* execute handler with original mask */
60 if (sigaction (SIGALRM
, &act
, &oact
) < 0)
63 before
= time ((time_t *) NULL
);
64 remaining
= alarm (seconds
);
66 if (remaining
> 0 && remaining
< seconds
)
68 /* The user's alarm will expire before our own would.
69 Restore the user's signal action state and let his alarm happen. */
70 (void) sigaction (SIGALRM
, &oact
, (struct sigaction
*) NULL
);
71 alarm (remaining
); /* Restore sooner alarm. */
72 sigsuspend (&oset
); /* Wait for it to go off. */
73 after
= time ((time_t *) NULL
);
77 /* Atomically restore the old signal mask
78 (which had better not block SIGALRM),
79 and wait for a signal to arrive. */
82 after
= time ((time_t *) NULL
);
84 /* Restore the old signal action state. */
85 (void) sigaction (SIGALRM
, &oact
, (struct sigaction
*) NULL
);
88 /* Notice how long we actually slept. */
89 slept
= after
- before
;
91 /* Restore the user's alarm if we have not already past it.
92 If we have, be sure to turn off the alarm in case a signal
93 other than SIGALRM was what woke us up. */
94 (void) alarm (remaining
> slept
? remaining
- slept
: 0);
96 /* Restore the original signal mask. */
97 (void) sigprocmask (SIG_SETMASK
, &oset
, (sigset_t
*) NULL
);
99 /* Restore the `errno' value we started with.
100 Some of the calls we made might have failed, but we didn't care. */
103 return slept
> seconds
? 0 : seconds
- slept
;
105 weak_alias (__sleep
, sleep
)