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