1 /* Provide a replacement for the POSIX nanosleep function.
3 Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006, 2007, 2008, 2009
4 Free 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 */
26 #include "sig-handler.h"
31 #include <sys/types.h>
32 #include <sys/select.h>
42 enum { BILLION = 1000 * 1000 * 1000 };
44 #if HAVE_BUG_BIG_NANOSLEEP
47 rpl_nanosleep (const struct timespec *requested_delay,
48 struct timespec *remaining_delay)
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
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;
61 while (limit < seconds)
64 intermediate.tv_sec = limit;
65 result = nanosleep (&intermediate, remaining_delay);
71 remaining_delay->tv_sec += seconds;
72 remaining_delay->tv_nsec += requested_delay->tv_nsec;
73 if (BILLION <= requested_delay->tv_nsec)
75 remaining_delay->tv_sec++;
76 remaining_delay->tv_nsec -= BILLION;
82 intermediate.tv_sec = seconds;
83 intermediate.tv_nsec = requested_delay->tv_nsec;
84 return nanosleep (&intermediate, remaining_delay);
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. */
93 # define SIGCONT SIGTERM
96 static sig_atomic_t volatile suspended;
106 /* Suspend execution for at least *TS_DELAY seconds. */
109 my_usleep (const struct timespec *ts_delay)
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)
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 */
121 tv_delay.tv_sec = t1;
122 tv_delay.tv_usec = 0;
125 select (0, NULL, NULL, NULL, &tv_delay);
128 /* Suspend execution for at least *REQUESTED_DELAY seconds. The
129 *REMAINING_DELAY part isn't implemented yet. */
132 rpl_nanosleep (const struct timespec *requested_delay,
133 struct timespec *remaining_delay)
135 static bool initialized;
137 /* set up sig handler */
140 struct sigaction oldact;
142 sigaction (SIGCONT, NULL, &oldact);
143 if (get_handler (&oldact) != SIG_IGN)
145 struct sigaction newact;
147 newact.sa_handler = sighandler;
148 sigemptyset (&newact.sa_mask);
150 sigaction (SIGCONT, &newact, NULL);
157 my_usleep (requested_delay);
161 /* Calculate time remaining. */
162 /* FIXME: the code in sleep doesn't use this, so there's no
163 rush to implement it. */
168 /* FIXME: Restore sig handler? */