nanosleep merge from coreutils
[gnulib.git] / lib / nanosleep.c
1 /* Provide a replacement for the POSIX nanosleep function.
2    Copyright (C) 1999, 2000, 2002, 2004 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 <stdio.h>
27 #include <sys/types.h>
28 #include <signal.h>
29
30 #include <errno.h>
31 #ifndef errno
32 extern int errno;
33 #endif
34
35 #if HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
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 #include "timespec.h"
47
48 static sig_atomic_t volatile suspended;
49
50 /* Handle SIGCONT. */
51
52 static void
53 sighandler (int sig)
54 {
55   suspended = 1;
56 }
57
58 /* FIXME: comment */
59
60 static void
61 my_usleep (const struct timespec *ts_delay)
62 {
63   struct timeval tv_delay;
64   tv_delay.tv_sec = ts_delay->tv_sec;
65   tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000;
66   if (tv_delay.tv_usec == 1000000)
67     {
68       tv_delay.tv_sec++;
69       tv_delay.tv_usec = 0;
70     }
71   select (0, (void *) 0, (void *) 0, (void *) 0, &tv_delay);
72 }
73
74 /* FIXME: comment */
75
76 int
77 rpl_nanosleep (const struct timespec *requested_delay,
78                struct timespec *remaining_delay)
79 {
80   static int initialized;
81
82 #ifdef SA_NOCLDSTOP
83   struct sigaction oldact, newact;
84 #endif
85
86   suspended = 0;
87
88   /* set up sig handler */
89   if (! initialized)
90     {
91 #ifdef SA_NOCLDSTOP
92       newact.sa_handler = sighandler;
93       sigemptyset (&newact.sa_mask);
94       newact.sa_flags = 0;
95
96       sigaction (SIGCONT, NULL, &oldact);
97       if (oldact.sa_handler != SIG_IGN)
98         sigaction (SIGCONT, &newact, NULL);
99 #else
100       if (signal (SIGCONT, SIG_IGN) != SIG_IGN)
101         signal (SIGCONT, sighandler);
102 #endif
103       initialized = 1;
104     }
105
106   my_usleep (requested_delay);
107
108   if (suspended)
109     {
110       /* Calculate time remaining.  */
111       /* FIXME: the code in sleep doesn't use this, so there's no
112          rush to implement it.  */
113
114       errno = EINTR;
115     }
116
117   /* FIXME: Restore sig handler?  */
118
119   return suspended;
120 }