1 /* Provide a replacement for the POSIX nanosleep function.
3 Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006, 2007, 2008 Free
4 Software Foundation, Inc.
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.
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.
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/>. */
19 /* written by Jim Meyering */
25 #include "sig-handler.h"
30 #include <sys/types.h>
31 #include <sys/select.h>
41 enum { BILLION = 1000 * 1000 * 1000 };
43 #if HAVE_BUG_BIG_NANOSLEEP
46 getnow (struct timespec *t)
48 # if defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME
49 if (clock_gettime (CLOCK_MONOTONIC, t) == 0)
56 rpl_nanosleep (const struct timespec *requested_delay,
57 struct timespec *remaining_delay)
59 /* nanosleep mishandles large sleeps due to internal overflow
60 problems, so check that the proper amount of time has actually
63 struct timespec delay = *requested_delay;
69 int r = nanosleep (&delay, remaining_delay);
76 secs_sofar = now.tv_sec - t0.tv_sec;
77 if (requested_delay->tv_sec < secs_sofar)
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)
83 if (delay.tv_sec == 0)
85 delay.tv_nsec += BILLION;
88 else if (BILLION <= delay.tv_nsec)
90 delay.tv_nsec -= BILLION;
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. */
103 # define SIGCONT SIGTERM
106 static sig_atomic_t volatile suspended;
108 /* Handle SIGCONT. */
116 /* Suspend execution for at least *TS_DELAY seconds. */
119 my_usleep (const struct timespec *ts_delay)
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)
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 */
131 tv_delay.tv_sec = t1;
132 tv_delay.tv_usec = 0;
135 select (0, NULL, NULL, NULL, &tv_delay);
138 /* Suspend execution for at least *REQUESTED_DELAY seconds. The
139 *REMAINING_DELAY part isn't implemented yet. */
142 rpl_nanosleep (const struct timespec *requested_delay,
143 struct timespec *remaining_delay)
145 static bool initialized;
147 /* set up sig handler */
150 struct sigaction oldact;
152 sigaction (SIGCONT, NULL, &oldact);
153 if (get_handler (&oldact) != SIG_IGN)
155 struct sigaction newact;
157 newact.sa_handler = sighandler;
158 sigemptyset (&newact.sa_mask);
160 sigaction (SIGCONT, &newact, NULL);
167 my_usleep (requested_delay);
171 /* Calculate time remaining. */
172 /* FIXME: the code in sleep doesn't use this, so there's no
173 rush to implement it. */
178 /* FIXME: Restore sig handler? */