2052
|
1 /* Provide a replacement for the POSIX nanosleep function. |
|
2 Copyright (C) 1999 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> |
2053
|
21 #include <stdio.h> |
2052
|
22 #include <sys/types.h> |
2053
|
23 #include <signal.h> |
2052
|
24 |
2057
|
25 #include <errno.h> |
|
26 #ifndef errno |
|
27 extern int errno; |
|
28 #endif |
|
29 |
2056
|
30 #if HAVE_UNISTD_H |
|
31 # include <unistd.h> |
|
32 #endif |
|
33 |
2052
|
34 #include <time.h> |
|
35 /* FIXME: is including both like this kosher? */ |
|
36 #include <sys/time.h> |
|
37 |
2056
|
38 static int suspended; |
2052
|
39 |
2053
|
40 /* Handle SIGCONT. */ |
|
41 |
|
42 static void |
|
43 sighandler (int sig) |
|
44 { |
|
45 #ifdef SA_INTERRUPT |
|
46 struct sigaction sigact; |
|
47 |
|
48 sigact.sa_handler = SIG_DFL; |
|
49 sigemptyset (&sigact.sa_mask); |
|
50 sigact.sa_flags = 0; |
|
51 sigaction (sig, &sigact, NULL); |
|
52 #else |
|
53 signal (sig, SIG_DFL); |
|
54 #endif |
|
55 |
|
56 suspended = 1; |
|
57 kill (getpid (), sig); |
|
58 } |
|
59 |
2052
|
60 /* Sleep for USEC microseconds. */ |
|
61 |
|
62 static void |
2056
|
63 my_usleep (const struct timespec *ts_delay) |
2052
|
64 { |
|
65 struct timeval tv_delay; |
|
66 tv_delay.tv_sec = ts_delay->tv_sec; |
|
67 tv_delay.tv_usec = 1000 * ts_delay->tv_nsec; |
2056
|
68 select (0, (void *) 0, (void *) 0, (void *) 0, &tv_delay); |
2052
|
69 } |
|
70 |
|
71 int |
|
72 nanosleep (const struct timespec *requested_delay, |
|
73 struct timespec *remaining_delay) |
|
74 { |
2053
|
75 #ifdef SA_INTERRUPT |
|
76 struct sigaction oldact, newact; |
|
77 #endif |
|
78 |
2056
|
79 suspended = 0; |
2052
|
80 |
|
81 /* set up sig handler -- but maybe only do this the first time? */ |
2053
|
82 #ifdef SA_INTERRUPT |
|
83 newact.sa_handler = sighandler; |
|
84 sigemptyset (&newact.sa_mask); |
|
85 newact.sa_flags = 0; |
|
86 |
|
87 sigaction (SIGCONT, NULL, &oldact); |
|
88 if (oldact.sa_handler != SIG_IGN) |
|
89 sigaction (SIGCONT, &newact, NULL); |
|
90 #else |
|
91 if (signal (SIGCONT, SIG_IGN) != SIG_IGN) |
|
92 signal (SIGCONT, sighandler); |
|
93 #endif |
2052
|
94 |
2056
|
95 my_usleep (requested_delay); |
2052
|
96 |
2056
|
97 if (suspended) |
2052
|
98 { |
|
99 /* Calculate time remaining. */ |
|
100 /* FIXME: the code in sleep doesn't use this, so there's no |
|
101 rush to implement it. */ |
|
102 } |
|
103 |
|
104 /* FIXME: Restore sig handler? */ |
|
105 |
2057
|
106 errno = EINTR; |
|
107 |
2056
|
108 return suspended; |
2052
|
109 } |