1 /* Provide a replacement for the POSIX nanosleep function.
3 Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006 Free Software
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)
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, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 /* written by Jim Meyering */
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) */
33 # include <sys/select.h>
35 #include <sys/types.h>
38 #if TIME_WITH_SYS_TIME
39 # include <sys/time.h>
43 # include <sys/time.h>
55 /* Some systems (MSDOS) don't have SIGCONT.
56 Using SIGTERM here turns the signal-handling code below
57 into a no-op on such systems. */
59 # define SIGCONT SIGTERM
62 #if ! HAVE_SIGINTERRUPT
63 # define siginterrupt(sig, flag) /* empty */
66 static sig_atomic_t volatile suspended;
76 /* Suspend execution for at least *TS_DELAY seconds. */
79 my_usleep (const struct timespec *ts_delay)
81 struct timeval tv_delay;
82 tv_delay.tv_sec = ts_delay->tv_sec;
83 tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000;
84 if (tv_delay.tv_usec == 1000000)
86 time_t t1 = tv_delay.tv_sec + 1;
87 if (t1 < tv_delay.tv_sec)
88 tv_delay.tv_usec = 1000000 - 1; /* close enough */
95 select (0, NULL, NULL, NULL, &tv_delay);
98 /* Suspend execution for at least *REQUESTED_DELAY seconds. The
99 *REMAINING_DELAY part isn't implemented yet. */
102 rpl_nanosleep (const struct timespec *requested_delay,
103 struct timespec *remaining_delay)
105 static bool initialized;
107 /* set up sig handler */
111 struct sigaction oldact, newact;
112 newact.sa_handler = sighandler;
113 sigemptyset (&newact.sa_mask);
116 sigaction (SIGCONT, NULL, &oldact);
117 if (oldact.sa_handler != SIG_IGN)
118 sigaction (SIGCONT, &newact, NULL);
120 if (signal (SIGCONT, SIG_IGN) != SIG_IGN)
122 signal (SIGCONT, sighandler);
123 siginterrupt (SIGCONT, 1);
131 my_usleep (requested_delay);
135 /* Calculate time remaining. */
136 /* FIXME: the code in sleep doesn't use this, so there's no
137 rush to implement it. */
142 /* FIXME: Restore sig handler? */