9d725c66d9cf6ed1c6b3f0b36186eca650fcc1cb
[gnulib.git] / lib / nanosleep.c
1 /* Provide a replacement for the POSIX nanosleep function.
2
3    Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006 Free Software
4    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 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 /* Undefine nanosleep here so any prototype is not redefined to be a
27    prototype for rpl_nanosleep. (they'd conflict e.g., on alpha-dec-osf3.2)  */
28 #undef nanosleep
29
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #if HAVE_SYS_SELECT_H
34 # include <sys/select.h>
35 #endif
36 #include <signal.h>
37
38 #if TIME_WITH_SYS_TIME
39 # include <sys/time.h>
40 # include <time.h>
41 #else
42 # if HAVE_SYS_TIME_H
43 #  include <sys/time.h>
44 # else
45 #  include <time.h>
46 # endif
47 #endif
48
49 #include <errno.h>
50
51 #include <unistd.h>
52
53 #include "timespec.h"
54
55 enum { BILLION = 1000 * 1000 * 1000 };
56
57 #if HAVE_BUG_BIG_NANOSLEEP
58
59 void
60 getnow (struct timespec *t)
61 {
62 # if defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME
63   if (clock_gettime (CLOCK_MONOTONIC, t) == 0)
64     return;
65 # endif
66   gettime (t);
67 }
68
69 int
70 rpl_nanosleep (const struct timespec *requested_delay,
71                struct timespec *remaining_delay)
72 {
73   /* nanosleep mishandles large sleeps due to internal overflow
74      problems, so check that the proper amount of time has actually
75      elapsed.  */
76
77   struct timespec delay = *requested_delay;
78   struct timespec t0;
79   getnow (&t0);
80
81   for (;;)
82     {
83       int r = nanosleep (&delay, remaining_delay);
84       if (r == 0)
85         {
86           time_t secs_sofar;
87           struct timespec now;
88           getnow (&now);
89
90           secs_sofar = now.tv_sec - t0.tv_sec;
91           if (requested_delay->tv_sec < secs_sofar)
92             return 0;
93           delay.tv_sec = requested_delay->tv_sec - secs_sofar;
94           delay.tv_nsec = requested_delay->tv_nsec - (now.tv_nsec - t0.tv_nsec);
95           if (delay.tv_nsec < 0)
96             {
97               if (delay.tv_sec == 0)
98                 return 0;
99               delay.tv_nsec += BILLION;
100               delay.tv_sec--;
101             }
102           else if (BILLION <= delay.tv_nsec)
103             {
104               delay.tv_nsec -= BILLION;
105               delay.tv_sec++;
106             }
107         }
108     }
109 }
110
111 #else
112
113 /* Some systems (MSDOS) don't have SIGCONT.
114    Using SIGTERM here turns the signal-handling code below
115    into a no-op on such systems. */
116 # ifndef SIGCONT
117 #  define SIGCONT SIGTERM
118 # endif
119
120 # if ! HAVE_SIGINTERRUPT
121 #  define siginterrupt(sig, flag) /* empty */
122 # endif
123
124 static sig_atomic_t volatile suspended;
125
126 /* Handle SIGCONT. */
127
128 static void
129 sighandler (int sig)
130 {
131   suspended = 1;
132 }
133
134 /* Suspend execution for at least *TS_DELAY seconds.  */
135
136 static void
137 my_usleep (const struct timespec *ts_delay)
138 {
139   struct timeval tv_delay;
140   tv_delay.tv_sec = ts_delay->tv_sec;
141   tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000;
142   if (tv_delay.tv_usec == 1000000)
143     {
144       time_t t1 = tv_delay.tv_sec + 1;
145       if (t1 < tv_delay.tv_sec)
146         tv_delay.tv_usec = 1000000 - 1; /* close enough */
147       else
148         {
149           tv_delay.tv_sec = t1;
150           tv_delay.tv_usec = 0;
151         }
152     }
153   select (0, NULL, NULL, NULL, &tv_delay);
154 }
155
156 /* Suspend execution for at least *REQUESTED_DELAY seconds.  The
157    *REMAINING_DELAY part isn't implemented yet.  */
158
159 int
160 rpl_nanosleep (const struct timespec *requested_delay,
161                struct timespec *remaining_delay)
162 {
163   static bool initialized;
164
165   /* set up sig handler */
166   if (! initialized)
167     {
168 # ifdef SA_NOCLDSTOP
169       struct sigaction oldact, newact;
170       newact.sa_handler = sighandler;
171       sigemptyset (&newact.sa_mask);
172       newact.sa_flags = 0;
173
174       sigaction (SIGCONT, NULL, &oldact);
175       if (oldact.sa_handler != SIG_IGN)
176         sigaction (SIGCONT, &newact, NULL);
177 # else
178       if (signal (SIGCONT, SIG_IGN) != SIG_IGN)
179         {
180           signal (SIGCONT, sighandler);
181           siginterrupt (SIGCONT, 1);
182         }
183 # endif
184       initialized = true;
185     }
186
187   suspended = 0;
188
189   my_usleep (requested_delay);
190
191   if (suspended)
192     {
193       /* Calculate time remaining.  */
194       /* FIXME: the code in sleep doesn't use this, so there's no
195          rush to implement it.  */
196
197       errno = EINTR;
198     }
199
200   /* FIXME: Restore sig handler?  */
201
202   return suspended;
203 }
204 #endif