Update.
[platform/upstream/glibc.git] / sysdeps / posix / sleep.c
1 /* Copyright (C) 1991, 1992, 1993, 1996, 1997 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
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.
8
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
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 not,
16    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17    Boston, MA 02111-1307, USA.  */
18
19 #include <signal.h>
20 #include <time.h>
21 #include <unistd.h>
22 #include <errno.h>
23
24
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)
29 {
30   return;
31 }
32
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)
42 {
43   unsigned int remaining, slept;
44   time_t before, after;
45   sigset_t set, oset;
46   struct sigaction act, oact;
47   int save = errno;
48
49   if (seconds == 0)
50     return 0;
51
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;
57
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;
63
64   before = time ((time_t *) NULL);
65   remaining = alarm (seconds);
66
67   if (remaining > 0 && remaining < seconds)
68     {
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);
75     }
76   else
77     {
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);
82
83       after = time ((time_t *) NULL);
84
85       /* Restore the old signal action state.  */
86       (void) sigaction (SIGALRM, &oact, (struct sigaction *) NULL);
87     }
88
89   /* Notice how long we actually slept.  */
90   slept = after - before;
91
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);
96
97   /* Restore the original signal mask.  */
98   (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
99
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);
103
104   return slept > seconds ? 0 : seconds - slept;
105 }
106 weak_alias (__sleep, sleep)