nanosleep: work around cygwin bug
[gnulib.git] / lib / nanosleep.c
1 /* Provide a replacement for the POSIX nanosleep function.
2
3    Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006, 2007, 2008, 2009
4    Free 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 3 of the License, or
9    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
18
19 /* written by Jim Meyering */
20
21 #include <config.h>
22
23 #include <time.h>
24
25 #include "intprops.h"
26 #include "sig-handler.h"
27 #include "verify.h"
28
29 #include <stdbool.h>
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <sys/select.h>
33 #include <signal.h>
34
35 #include <sys/time.h>
36 #include <errno.h>
37
38 #include <unistd.h>
39
40 #undef nanosleep
41
42 enum { BILLION = 1000 * 1000 * 1000 };
43
44 #if HAVE_BUG_BIG_NANOSLEEP
45
46 int
47 rpl_nanosleep (const struct timespec *requested_delay,
48                struct timespec *remaining_delay)
49 {
50   /* nanosleep mishandles large sleeps due to internal overflow
51      problems.  The worst known case of this is cygwin 1.5.x, which
52      can't sleep more than 49.7 days (2**32 milliseconds).  Solve this
53      by breaking the sleep up into smaller chunks.  Verify that time_t
54      is large enough.  */
55   verify (TYPE_MAXIMUM (time_t) / 49 / 24 / 60 / 60);
56   const time_t limit = 49 * 24 * 60 * 60;
57   time_t seconds = requested_delay->tv_sec;
58   struct timespec intermediate;
59   intermediate.tv_nsec = 0;
60
61   while (limit < seconds)
62     {
63       int result;
64       intermediate.tv_sec = limit;
65       result = nanosleep (&intermediate, remaining_delay);
66       seconds -= limit;
67       if (result)
68         {
69           if (remaining_delay)
70             {
71               remaining_delay->tv_sec += seconds;
72               remaining_delay->tv_nsec += requested_delay->tv_nsec;
73               if (BILLION <= requested_delay->tv_nsec)
74                 {
75                   remaining_delay->tv_sec++;
76                   remaining_delay->tv_nsec -= BILLION;
77                 }
78             }
79           return result;
80         }
81     }
82   intermediate.tv_sec = seconds;
83   intermediate.tv_nsec = requested_delay->tv_nsec;
84   return nanosleep (&intermediate, remaining_delay);
85 }
86
87 #else
88
89 /* Some systems (MSDOS) don't have SIGCONT.
90    Using SIGTERM here turns the signal-handling code below
91    into a no-op on such systems. */
92 # ifndef SIGCONT
93 #  define SIGCONT SIGTERM
94 # endif
95
96 static sig_atomic_t volatile suspended;
97
98 /* Handle SIGCONT. */
99
100 static void
101 sighandler (int sig)
102 {
103   suspended = 1;
104 }
105
106 /* Suspend execution for at least *TS_DELAY seconds.  */
107
108 static void
109 my_usleep (const struct timespec *ts_delay)
110 {
111   struct timeval tv_delay;
112   tv_delay.tv_sec = ts_delay->tv_sec;
113   tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000;
114   if (tv_delay.tv_usec == 1000000)
115     {
116       time_t t1 = tv_delay.tv_sec + 1;
117       if (t1 < tv_delay.tv_sec)
118         tv_delay.tv_usec = 1000000 - 1; /* close enough */
119       else
120         {
121           tv_delay.tv_sec = t1;
122           tv_delay.tv_usec = 0;
123         }
124     }
125   select (0, NULL, NULL, NULL, &tv_delay);
126 }
127
128 /* Suspend execution for at least *REQUESTED_DELAY seconds.  The
129    *REMAINING_DELAY part isn't implemented yet.  */
130
131 int
132 rpl_nanosleep (const struct timespec *requested_delay,
133                struct timespec *remaining_delay)
134 {
135   static bool initialized;
136
137   /* set up sig handler */
138   if (! initialized)
139     {
140       struct sigaction oldact;
141
142       sigaction (SIGCONT, NULL, &oldact);
143       if (get_handler (&oldact) != SIG_IGN)
144         {
145           struct sigaction newact;
146
147           newact.sa_handler = sighandler;
148           sigemptyset (&newact.sa_mask);
149           newact.sa_flags = 0;
150           sigaction (SIGCONT, &newact, NULL);
151         }
152       initialized = true;
153     }
154
155   suspended = 0;
156
157   my_usleep (requested_delay);
158
159   if (suspended)
160     {
161       /* Calculate time remaining.  */
162       /* FIXME: the code in sleep doesn't use this, so there's no
163          rush to implement it.  */
164
165       errno = EINTR;
166     }
167
168   /* FIXME: Restore sig handler?  */
169
170   return suspended;
171 }
172 #endif