Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/nanosleep.c @ 7237:119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
nanosleep mishandles sleeps for longer than 2**31 seconds.
Problem reported by Frank v Waveren in
<http://lists.gnu.org/archive/html/bug-coreutils/2006-08/msg00298.html>.
* lib/nanosleep.c (BILLION): New constant.
(getnow) [HAVE_BUG_BIG_NANOSLEEP]: New functions.
(rpl_nanosleep) [HAVE_BUG_BIG_NANOSLEEP]: Completely new implementation.
* m4/nanosleep.m4 (gl_FUNC_NANOSLEEP): Require gl_CLOCK_TIME.
* modules/nanosleep (Depends-on): Add gettime.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Thu, 31 Aug 2006 07:00:50 +0000 |
parents | 9dc5bb3f3359 |
children | 6948138d9f3a |
rev | line source |
---|---|
2052 | 1 /* Provide a replacement for the POSIX nanosleep function. |
6795
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
2 |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
3 Copyright (C) 1999, 2000, 2002, 2004, 2005, 2006 Free Software |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
4 Foundation, Inc. |
2052 | 5 |
6 This program is free software; you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation; either version 2, or (at your option) | |
9 any later version. | |
10 | |
11 This program is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with this program; if not, write to the Free Software Foundation, | |
5848
a48fb0e98c8c
*** empty log message ***
Paul Eggert <eggert@cs.ucla.edu>
parents:
5813
diff
changeset
|
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ |
2052 | 19 |
20 /* written by Jim Meyering */ | |
21 | |
6259
96c32553b4c6
Use a consistent style for including <config.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
22 #ifdef HAVE_CONFIG_H |
96c32553b4c6
Use a consistent style for including <config.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
23 # include <config.h> |
96c32553b4c6
Use a consistent style for including <config.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
5848
diff
changeset
|
24 #endif |
2270 | 25 |
26 /* Undefine nanosleep here so any prototype is not redefined to be a | |
27 prototype for rpl_nanosleep. (they'd conflict e.g., on alpha-dec-osf3.2) */ | |
28 #undef nanosleep | |
29 | |
5159 | 30 #include <stdbool.h> |
2053 | 31 #include <stdio.h> |
6795
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
32 #if HAVE_SYS_SELECT_H |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
33 # include <sys/select.h> |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
34 #endif |
2052 | 35 #include <sys/types.h> |
2053 | 36 #include <signal.h> |
2052 | 37 |
6795
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
38 #if TIME_WITH_SYS_TIME |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
39 # include <sys/time.h> |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
40 # include <time.h> |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
41 #else |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
42 # if HAVE_SYS_TIME_H |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
43 # include <sys/time.h> |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
44 # else |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
45 # include <time.h> |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
46 # endif |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
47 #endif |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
48 |
2057 | 49 #include <errno.h> |
50 | |
6275 | 51 #include <unistd.h> |
2056 | 52 |
5813 | 53 #include "timespec.h" |
54 | |
7237
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
55 enum { BILLION = 1000 * 1000 * 1000 }; |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
56 |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
57 #if HAVE_BUG_BIG_NANOSLEEP |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
58 |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
59 void |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
60 getnow (struct timespec *t) |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
61 { |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
62 # if defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
63 if (clock_gettime (CLOCK_MONOTONIC, t) == 0) |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
64 return; |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
65 # endif |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
66 gettime (t); |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
67 } |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
68 |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
69 int |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
70 rpl_nanosleep (const struct timespec *requested_delay, |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
71 struct timespec *remaining_delay) |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
72 { |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
73 /* nanosleep mishandles large sleeps due to internal overflow |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
74 problems, so check that the proper amount of time has actually |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
75 elapsed. */ |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
76 |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
77 struct timespec delay = *requested_delay; |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
78 struct timespec t0; |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
79 getnow (&t0); |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
80 |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
81 for (;;) |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
82 { |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
83 int r = nanosleep (&delay, remaining_delay); |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
84 if (r == 0) |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
85 { |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
86 time_t secs_sofar; |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
87 struct timespec now; |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
88 getnow (&now); |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
89 |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
90 secs_sofar = now.tv_sec - t0.tv_sec; |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
91 if (requested_delay->tv_sec < secs_sofar) |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
92 return 0; |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
93 delay.tv_sec = requested_delay->tv_sec - secs_sofar; |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
94 delay.tv_nsec = requested_delay->tv_nsec - (now.tv_nsec - t0.tv_nsec); |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
95 if (delay.tv_nsec < 0) |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
96 { |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
97 if (delay.tv_sec == 0) |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
98 return 0; |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
99 delay.tv_nsec += BILLION; |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
100 delay.tv_sec--; |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
101 } |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
102 else if (BILLION <= delay.tv_nsec) |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
103 { |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
104 delay.tv_nsec -= BILLION; |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
105 delay.tv_sec++; |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
106 } |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
107 } |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
108 } |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
109 } |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
110 |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
111 #else |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
112 |
2687
34703e371994
(SIGCONT): Define if not already defined.
Jim Meyering <jim@meyering.net>
parents:
2341
diff
changeset
|
113 /* Some systems (MSDOS) don't have SIGCONT. |
34703e371994
(SIGCONT): Define if not already defined.
Jim Meyering <jim@meyering.net>
parents:
2341
diff
changeset
|
114 Using SIGTERM here turns the signal-handling code below |
34703e371994
(SIGCONT): Define if not already defined.
Jim Meyering <jim@meyering.net>
parents:
2341
diff
changeset
|
115 into a no-op on such systems. */ |
7237
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
116 # ifndef SIGCONT |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
117 # define SIGCONT SIGTERM |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
118 # endif |
2687
34703e371994
(SIGCONT): Define if not already defined.
Jim Meyering <jim@meyering.net>
parents:
2341
diff
changeset
|
119 |
7237
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
120 # if ! HAVE_SIGINTERRUPT |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
121 # define siginterrupt(sig, flag) /* empty */ |
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
122 # endif |
2052 | 123 |
5060
4e59cddb690f
nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
3767
diff
changeset
|
124 static sig_atomic_t volatile suspended; |
2052 | 125 |
2053 | 126 /* Handle SIGCONT. */ |
127 | |
128 static void | |
129 sighandler (int sig) | |
130 { | |
131 suspended = 1; | |
132 } | |
133 | |
6795
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
134 /* Suspend execution for at least *TS_DELAY seconds. */ |
2052 | 135 |
136 static void | |
2056 | 137 my_usleep (const struct timespec *ts_delay) |
2052 | 138 { |
139 struct timeval tv_delay; | |
140 tv_delay.tv_sec = ts_delay->tv_sec; | |
5060
4e59cddb690f
nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
3767
diff
changeset
|
141 tv_delay.tv_usec = (ts_delay->tv_nsec + 999) / 1000; |
4e59cddb690f
nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
3767
diff
changeset
|
142 if (tv_delay.tv_usec == 1000000) |
4e59cddb690f
nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
3767
diff
changeset
|
143 { |
6795
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
144 time_t t1 = tv_delay.tv_sec + 1; |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
145 if (t1 < tv_delay.tv_sec) |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
146 tv_delay.tv_usec = 1000000 - 1; /* close enough */ |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
147 else |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
148 { |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
149 tv_delay.tv_sec = t1; |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
150 tv_delay.tv_usec = 0; |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
151 } |
5060
4e59cddb690f
nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
3767
diff
changeset
|
152 } |
5813 | 153 select (0, NULL, NULL, NULL, &tv_delay); |
2052 | 154 } |
155 | |
6795
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
156 /* Suspend execution for at least *REQUESTED_DELAY seconds. The |
9dc5bb3f3359
* lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents:
6275
diff
changeset
|
157 *REMAINING_DELAY part isn't implemented yet. */ |
2058 | 158 |
2052 | 159 int |
2270 | 160 rpl_nanosleep (const struct timespec *requested_delay, |
2341 | 161 struct timespec *remaining_delay) |
2052 | 162 { |
5159 | 163 static bool initialized; |
5060
4e59cddb690f
nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
3767
diff
changeset
|
164 |
2058 | 165 /* set up sig handler */ |
5060
4e59cddb690f
nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents:
3767
diff
changeset
|
166 if (! initialized) |
2058 | 167 { |
7237
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
168 # ifdef SA_NOCLDSTOP |
5813 | 169 struct sigaction oldact, newact; |
2058 | 170 newact.sa_handler = sighandler; |
171 sigemptyset (&newact.sa_mask); | |
172 newact.sa_flags = 0; | |
2053 | 173 |
2058 | 174 sigaction (SIGCONT, NULL, &oldact); |
175 if (oldact.sa_handler != SIG_IGN) | |
176 sigaction (SIGCONT, &newact, NULL); | |
7237
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
177 # else |
2058 | 178 if (signal (SIGCONT, SIG_IGN) != SIG_IGN) |
5813 | 179 { |
180 signal (SIGCONT, sighandler); | |
181 siginterrupt (SIGCONT, 1); | |
182 } | |
7237
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
183 # endif |
5159 | 184 initialized = true; |
2058 | 185 } |
2052 | 186 |
5813 | 187 suspended = 0; |
188 | |
2056 | 189 my_usleep (requested_delay); |
2052 | 190 |
2056 | 191 if (suspended) |
2052 | 192 { |
193 /* Calculate time remaining. */ | |
194 /* FIXME: the code in sleep doesn't use this, so there's no | |
195 rush to implement it. */ | |
2058 | 196 |
197 errno = EINTR; | |
2052 | 198 } |
199 | |
200 /* FIXME: Restore sig handler? */ | |
201 | |
2056 | 202 return suspended; |
2052 | 203 } |
7237
119870bc83d7
Work around a bug in both the Linux and SunOS 64-bit kernels:
Paul Eggert <eggert@cs.ucla.edu>
parents:
6795
diff
changeset
|
204 #endif |