OSDN Git Service

- adds several config-options to allow for turning off certain features
[uclinux-h8/uClibc.git] / libc / unistd / sleep.c
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.
5
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.
10
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.
15
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.  */
20
21 #include <errno.h>
22 #include <time.h>
23 #include <signal.h>
24 #include <unistd.h>
25
26 libc_hidden_proto(sleep)
27
28 libc_hidden_proto(sigaction)
29 libc_hidden_proto(sigprocmask)
30
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*/
37
38 #if 0
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)
43 {
44         unsigned int res;
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);
48         return res;
49 }
50
51 #else
52
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)
57 {
58     struct timespec ts = { .tv_sec = (long int) seconds, .tv_nsec = 0 };
59     sigset_t set, oset;
60     unsigned int result;
61
62     /* This is not necessary but some buggy programs depend on this.  */
63     if (seconds == 0)
64         return 0;
65
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))
72         return -1;
73
74     /* If SIGCHLD is already blocked, we don't have to do anything.  */
75     if (!__sigismember (&oset, SIGCHLD))
76     {
77         int saved_errno;
78         struct sigaction oact;
79
80         if (__sigemptyset (&set) < 0 || __sigaddset (&set, SIGCHLD) < 0)
81             return -1;
82
83         /* We get the signal handler for SIGCHLD.  */
84         if (sigaction (SIGCHLD, (struct sigaction *) NULL, &oact) < 0)
85         {
86             saved_errno = errno;
87             /* Restore the original signal mask.  */
88             (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
89             __set_errno (saved_errno);
90             return -1;
91         }
92
93         if (oact.sa_handler == SIG_IGN)
94         {
95             /* We should leave SIGCHLD blocked.  */
96             result = nanosleep (&ts, &ts);
97
98             saved_errno = errno;
99             /* Restore the original signal mask.  */
100             (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
101             __set_errno (saved_errno);
102         }
103         else
104         {
105             /* We should unblock SIGCHLD.  Restore the original signal mask.  */
106             (void) sigprocmask (SIG_SETMASK, &oset, (sigset_t *) NULL);
107             result = nanosleep (&ts, &ts);
108         }
109     }
110     else
111         result = nanosleep (&ts, &ts);
112
113     if (result != 0)
114         /* Round remaining time.  */
115         result = (unsigned int) ts.tv_sec + (ts.tv_nsec >= 500000000L);
116
117     return result;
118 }
119 #endif
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)
124 {
125 }
126 unsigned int sleep (unsigned int seconds)
127 {
128     struct sigaction act, oact;
129     sigset_t set, oset;
130     unsigned int result, remaining;
131     time_t before, after;
132     int old_errno = errno;
133
134     /* This is not necessary but some buggy programs depend on this.  */
135     if (seconds == 0)
136         return 0;
137
138     /* block SIGALRM */
139     if (__sigemptyset (&set) < 0
140             || __sigaddset (&set, SIGALRM) < 0
141             || sigprocmask (SIG_BLOCK, &set, &oset))
142         return seconds;
143
144     act.sa_handler = sleep_alarm_handler;
145     act.sa_flags = 0;
146     act.sa_mask = oset;
147     if (sigaction(SIGALRM, &act, &oact) < 0)
148         return seconds;
149
150     before = time(NULL);
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 */
156         sigsuspend(&oset);
157         after = time(NULL);
158     } else {
159         sigsuspend (&oset);
160         after = time(NULL);
161         (void) sigaction (SIGALRM, &oact, NULL);
162     }
163     result = after - before;
164     alarm(remaining > result ? remaining - result : 0);
165     sigprocmask (SIG_SETMASK, &oset, NULL);
166
167     __set_errno(old_errno);
168
169     return result > seconds ? 0 : seconds - result;
170 }
171 #endif /* __UCLIBC_HAS_REALTIME__ */
172 libc_hidden_def(sleep)