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