Tizen 2.0 Release
[external/tizen-coreutils.git] / lib / nanosleep.c
1 /* Provide a replacement for the POSIX nanosleep function.
2
3    Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006, 2007 Free
4    Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program 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
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software Foundation,
18    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
20 /* written by Jim Meyering */
21
22 #include <config.h>
23
24 #include <time.h>
25
26 #include "timespec.h"
27
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <sys/types.h>
31 #if HAVE_SYS_SELECT_H
32 # include <sys/select.h>
33 #endif
34 #include <signal.h>
35
36 #include <sys/time.h>
37 #include <errno.h>
38
39 #include <unistd.h>
40
41 #undef nanosleep
42
43 enum { BILLION = 1000 * 1000 * 1000 };
44
45 #if HAVE_BUG_BIG_NANOSLEEP
46
47 void
48 getnow (struct timespec *t)
49 {
50 # if defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME
51   if (clock_gettime (CLOCK_MONOTONIC, t) == 0)
52     return;
53 # endif
54   gettime (t);
55 }
56
57 int
58 rpl_nanosleep (const struct timespec *requested_delay,
59                struct timespec *remaining_delay)
60 {
61   /* nanosleep mishandles large sleeps due to internal overflow
62      problems, so check that the proper amount of time has actually
63      elapsed.  */
64
65   struct timespec delay = *requested_delay;
66   struct timespec t0;
67   getnow (&t0);
68
69   for (;;)
70     {
71       int r = nanosleep (&delay, remaining_delay);
72       if (r == 0)
73         {
74           time_t secs_sofar;
75           struct timespec now;
76           getnow (&now);
77
78           secs_sofar = now.tv_sec - t0.tv_sec;
79           if (requested_delay->tv_sec < secs_sofar)
80             return 0;
81           delay.tv_sec = requested_delay->tv_sec - secs_sofar;
82           delay.tv_nsec = requested_delay->tv_nsec - (now.tv_nsec - t0.tv_nsec);
83           if (delay.tv_nsec < 0)
84             {
85               if (delay.tv_sec == 0)
86                 return 0;
87               delay.tv_nsec += BILLION;
88               delay.tv_sec--;
89             }
90           else if (BILLION <= delay.tv_nsec)
91             {
92               delay.tv_nsec -= BILLION;
93               delay.tv_sec++;
94             }
95         }
96     }
97 }
98
99 #else
100
101 /* Some systems (MSDOS) don't have SIGCONT.
102    Using SIGTERM here turns the signal-handling code below
103    into a no-op on such systems. */
104 # ifndef SIGCONT
105 #  define SIGCONT SIGTERM
106 # endif
107
108 # if ! HAVE_SIGINTERRUPT
109 #  define siginterrupt(sig, flag) /* empty */
110 # endif
111
112 static sig_atomic_t volatile suspended;
113
114 /* Handle SIGCONT. */
115
116 static void
117 sighandler (int sig)
118 {
119   suspended = 1;
120 }
121
122 /* Suspend execution for at least *TS_DELAY seconds.  */
123
124 static void
125 my_usleep (const struct timespec *ts_delay)
126 {
127   struct timeval tv_delay;
128   tv_delay.tv_sec = ts_delay->tv_sec;
129   tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000;
130   if (tv_delay.tv_usec == 1000000)
131     {
132       time_t t1 = tv_delay.tv_sec + 1;
133       if (t1 < tv_delay.tv_sec)
134         tv_delay.tv_usec = 1000000 - 1; /* close enough */
135       else
136         {
137           tv_delay.tv_sec = t1;
138           tv_delay.tv_usec = 0;
139         }
140     }
141   select (0, NULL, NULL, NULL, &tv_delay);
142 }
143
144 /* Suspend execution for at least *REQUESTED_DELAY seconds.  The
145    *REMAINING_DELAY part isn't implemented yet.  */
146
147 int
148 rpl_nanosleep (const struct timespec *requested_delay,
149                struct timespec *remaining_delay)
150 {
151   static bool initialized;
152
153   /* set up sig handler */
154   if (! initialized)
155     {
156 # ifdef SA_NOCLDSTOP
157       struct sigaction oldact, newact;
158       newact.sa_handler = sighandler;
159       sigemptyset (&newact.sa_mask);
160       newact.sa_flags = 0;
161
162       sigaction (SIGCONT, NULL, &oldact);
163       if (oldact.sa_handler != SIG_IGN)
164         sigaction (SIGCONT, &newact, NULL);
165 # else
166       if (signal (SIGCONT, SIG_IGN) != SIG_IGN)
167         {
168           signal (SIGCONT, sighandler);
169           siginterrupt (SIGCONT, 1);
170         }
171 # endif
172       initialized = true;
173     }
174
175   suspended = 0;
176
177   my_usleep (requested_delay);
178
179   if (suspended)
180     {
181       /* Calculate time remaining.  */
182       /* FIXME: the code in sleep doesn't use this, so there's no
183          rush to implement it.  */
184
185       errno = EINTR;
186     }
187
188   /* FIXME: Restore sig handler?  */
189
190   return suspended;
191 }
192 #endif