*** empty log message ***
[gnulib.git] / lib / nanosleep.c
1 /* Provide a replacement for the POSIX nanosleep function.
2    Copyright (C) 1999, 2000, 2002, 2004, 2005 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* written by Jim Meyering */
19
20 #include <config.h>
21
22 /* Undefine nanosleep here so any prototype is not redefined to be a
23    prototype for rpl_nanosleep. (they'd conflict e.g., on alpha-dec-osf3.2)  */
24 #undef nanosleep
25
26 #include <stdbool.h>
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include <signal.h>
30
31 #include <errno.h>
32
33 #if HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36
37 #include "timespec.h"
38
39 /* Some systems (MSDOS) don't have SIGCONT.
40    Using SIGTERM here turns the signal-handling code below
41    into a no-op on such systems. */
42 #ifndef SIGCONT
43 # define SIGCONT SIGTERM
44 #endif
45
46 #if ! HAVE_SIGINTERRUPT
47 # define siginterrupt(sig, flag) /* empty */
48 #endif
49
50 static sig_atomic_t volatile suspended;
51
52 /* Handle SIGCONT. */
53
54 static void
55 sighandler (int sig)
56 {
57   suspended = 1;
58 }
59
60 /* FIXME: comment */
61
62 static void
63 my_usleep (const struct timespec *ts_delay)
64 {
65   struct timeval tv_delay;
66   tv_delay.tv_sec = ts_delay->tv_sec;
67   tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000;
68   if (tv_delay.tv_usec == 1000000)
69     {
70       tv_delay.tv_sec++;
71       tv_delay.tv_usec = 0;
72     }
73   select (0, NULL, NULL, NULL, &tv_delay);
74 }
75
76 /* FIXME: comment */
77
78 int
79 rpl_nanosleep (const struct timespec *requested_delay,
80                struct timespec *remaining_delay)
81 {
82   static bool initialized;
83
84   /* set up sig handler */
85   if (! initialized)
86     {
87 #ifdef SA_NOCLDSTOP
88       struct sigaction oldact, newact;
89       newact.sa_handler = sighandler;
90       sigemptyset (&newact.sa_mask);
91       newact.sa_flags = 0;
92
93       sigaction (SIGCONT, NULL, &oldact);
94       if (oldact.sa_handler != SIG_IGN)
95         sigaction (SIGCONT, &newact, NULL);
96 #else
97       if (signal (SIGCONT, SIG_IGN) != SIG_IGN)
98         {
99           signal (SIGCONT, sighandler);
100           siginterrupt (SIGCONT, 1);
101         }
102 #endif
103       initialized = true;
104     }
105
106   suspended = 0;
107
108   my_usleep (requested_delay);
109
110   if (suspended)
111     {
112       /* Calculate time remaining.  */
113       /* FIXME: the code in sleep doesn't use this, so there's no
114          rush to implement it.  */
115
116       errno = EINTR;
117     }
118
119   /* FIXME: Restore sig handler?  */
120
121   return suspended;
122 }