third update from main archive 960907
[glibc/history.git] / sysdeps / posix / sleep.c
blob20f78a08bb6736e2d788638f36a123332db8b44e
1 /* Copyright (C) 1991, 1992, 1993 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <ansidecl.h>
20 #include <signal.h>
21 #include <time.h>
22 #include <unistd.h>
23 #include <errno.h>
26 /* SIGALRM signal handler for `sleep'. This does nothing but return,
27 but SIG_IGN isn't supposed to break `pause'. */
28 static void
29 DEFUN(sleep_handler, (sig), int sig)
31 return;
34 /* Make the process sleep for SECONDS seconds, or until a signal arrives
35 and is not ignored. The function returns the number of seconds less
36 than SECONDS which it actually slept (zero if it slept the full time).
37 If a signal handler does a `longjmp' or modifies the handling of the
38 SIGALRM signal while inside `sleep' call, the handling of the SIGALRM
39 signal afterwards is undefined. There is no return value to indicate
40 error, but if `sleep' returns SECONDS, it probably didn't work. */
41 unsigned int
42 DEFUN(sleep, (seconds), unsigned int seconds)
44 unsigned int remaining, slept;
45 time_t before, after;
46 sigset_t set, oset;
47 struct sigaction act, oact;
48 int save = errno;
50 if (seconds == 0)
51 return 0;
53 /* Block SIGALRM signals while frobbing the handler. */
54 if (sigemptyset (&set) < 0 ||
55 sigaddset (&set, SIGALRM) < 0 ||
56 sigprocmask (SIG_BLOCK, &set, &oset))
57 return seconds;
59 act.sa_handler = sleep_handler;
60 act.sa_flags = 0;
61 act.sa_mask = oset; /* execute handler with original mask */
62 if (sigaction (SIGALRM, &act, &oact) < 0)
63 return seconds;
65 before = time ((time_t *) NULL);
66 remaining = alarm (seconds);
68 if (remaining > 0 && remaining < seconds)
70 /* The user's alarm will expire before our own would.
71 Restore the user's signal action state and let his alarm happen. */
72 (void) sigaction (SIGALRM, &oact, (struct sigaction *) NULL);
73 alarm (remaining); /* Restore sooner alarm. */
74 sigsuspend (&oset); /* Wait for it to go off. */
75 after = time ((time_t *) NULL);
77 else
79 /* Atomically restore the old signal mask
80 (which had better not block SIGALRM),
81 and wait for a signal to arrive. */
82 sigsuspend (&oset);
84 after = time ((time_t *) NULL);
86 /* Restore the old signal action state. */
87 (void) sigaction (SIGALRM, &oact, (struct sigaction *) NULL);
90 /* Notice how long we actually slept. */
91 slept = after - before;
93 /* Restore the user's alarm if we have not already past it.
94 If we have, be sure to turn off the alarm in case a signal
95 other than SIGALRM was what woke us up. */
96 (void) alarm (remaining > slept ? remaining - slept : 0);
98 /* Restore the original signal mask. */
99 (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
101 /* Restore the `errno' value we started with.
102 Some of the calls we made might have failed, but we didn't care. */
103 errno = save;
105 return slept > seconds ? 0 : seconds - slept;