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; |
2058
|
39 int first_call = 1; |
2052
|
40 |
2053
|
41 /* Handle SIGCONT. */ |
|
42 |
|
43 static void |
|
44 sighandler (int sig) |
|
45 { |
|
46 suspended = 1; |
|
47 } |
|
48 |
2058
|
49 /* FIXME: comment */ |
2052
|
50 |
|
51 static void |
2056
|
52 my_usleep (const struct timespec *ts_delay) |
2052
|
53 { |
|
54 struct timeval tv_delay; |
|
55 tv_delay.tv_sec = ts_delay->tv_sec; |
2058
|
56 tv_delay.tv_usec = ts_delay->tv_nsec / 1000; |
2056
|
57 select (0, (void *) 0, (void *) 0, (void *) 0, &tv_delay); |
2052
|
58 } |
|
59 |
2058
|
60 /* FIXME: comment */ |
|
61 |
2052
|
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 |
2056
|
70 suspended = 0; |
2052
|
71 |
2058
|
72 /* set up sig handler */ |
|
73 if (first_call) |
|
74 { |
2053
|
75 #ifdef SA_INTERRUPT |
2058
|
76 newact.sa_handler = sighandler; |
|
77 sigemptyset (&newact.sa_mask); |
|
78 newact.sa_flags = 0; |
2053
|
79 |
2058
|
80 sigaction (SIGCONT, NULL, &oldact); |
|
81 if (oldact.sa_handler != SIG_IGN) |
|
82 sigaction (SIGCONT, &newact, NULL); |
2053
|
83 #else |
2058
|
84 if (signal (SIGCONT, SIG_IGN) != SIG_IGN) |
|
85 signal (SIGCONT, sighandler); |
2053
|
86 #endif |
2058
|
87 first_call = 0; |
|
88 } |
2052
|
89 |
2056
|
90 my_usleep (requested_delay); |
2052
|
91 |
2056
|
92 if (suspended) |
2052
|
93 { |
|
94 /* Calculate time remaining. */ |
|
95 /* FIXME: the code in sleep doesn't use this, so there's no |
|
96 rush to implement it. */ |
2058
|
97 |
|
98 errno = EINTR; |
2052
|
99 } |
|
100 |
|
101 /* FIXME: Restore sig handler? */ |
|
102 |
2056
|
103 return suspended; |
2052
|
104 } |