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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
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
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
5
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
6 This program is free software; you can redistribute it and/or modify
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
7 it under the terms of the GNU General Public License as published by
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
8 the Free Software Foundation; either version 2, or (at your option)
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
9 any later version.
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
10
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
11 This program is distributed in the hope that it will be useful,
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
14 GNU General Public License for more details.
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
15
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
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
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
19
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
20 /* written by Jim Meyering */
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
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
cf5eb0c29b04 #undef nanosleep.
Jim Meyering <jim@meyering.net>
parents: 2151
diff changeset
25
cf5eb0c29b04 #undef nanosleep.
Jim Meyering <jim@meyering.net>
parents: 2151
diff changeset
26 /* Undefine nanosleep here so any prototype is not redefined to be a
cf5eb0c29b04 #undef nanosleep.
Jim Meyering <jim@meyering.net>
parents: 2151
diff changeset
27 prototype for rpl_nanosleep. (they'd conflict e.g., on alpha-dec-osf3.2) */
cf5eb0c29b04 #undef nanosleep.
Jim Meyering <jim@meyering.net>
parents: 2151
diff changeset
28 #undef nanosleep
cf5eb0c29b04 #undef nanosleep.
Jim Meyering <jim@meyering.net>
parents: 2151
diff changeset
29
5159
a535859efd14 Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5060
diff changeset
30 #include <stdbool.h>
2053
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
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
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
35 #include <sys/types.h>
2053
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
36 #include <signal.h>
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
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
50da17269b1b *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2056
diff changeset
49 #include <errno.h>
50da17269b1b *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2056
diff changeset
50
6275
fd0ccce602e4 Sync from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
51 #include <unistd.h>
2056
cf3d523888c5 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2053
diff changeset
52
5813
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
53 #include "timespec.h"
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
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
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
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
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
125
2053
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
126 /* Handle SIGCONT. */
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
127
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
128 static void
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
129 sighandler (int sig)
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
130 {
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
131 suspended = 1;
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
132 }
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
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
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
135
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
136 static void
2056
cf3d523888c5 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2053
diff changeset
137 my_usleep (const struct timespec *ts_delay)
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
138 {
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
139 struct timeval tv_delay;
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
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
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
153 select (0, NULL, NULL, NULL, &tv_delay);
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
154 }
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
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
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
158
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
159 int
2270
cf5eb0c29b04 #undef nanosleep.
Jim Meyering <jim@meyering.net>
parents: 2151
diff changeset
160 rpl_nanosleep (const struct timespec *requested_delay,
2341
12885d185e68 tweak indentation
Jim Meyering <jim@meyering.net>
parents: 2270
diff changeset
161 struct timespec *remaining_delay)
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
162 {
5159
a535859efd14 Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5060
diff changeset
163 static bool initialized;
5060
4e59cddb690f nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents: 3767
diff changeset
164
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
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
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
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
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
169 struct sigaction oldact, newact;
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
170 newact.sa_handler = sighandler;
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
171 sigemptyset (&newact.sa_mask);
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
172 newact.sa_flags = 0;
2053
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
173
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
174 sigaction (SIGCONT, NULL, &oldact);
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
175 if (oldact.sa_handler != SIG_IGN)
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
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
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
178 if (signal (SIGCONT, SIG_IGN) != SIG_IGN)
5813
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
179 {
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
180 signal (SIGCONT, sighandler);
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
181 siginterrupt (SIGCONT, 1);
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
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
a535859efd14 Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5060
diff changeset
184 initialized = true;
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
185 }
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
186
5813
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
187 suspended = 0;
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
188
2056
cf3d523888c5 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2053
diff changeset
189 my_usleep (requested_delay);
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
190
2056
cf3d523888c5 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2053
diff changeset
191 if (suspended)
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
192 {
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
193 /* Calculate time remaining. */
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
194 /* FIXME: the code in sleep doesn't use this, so there's no
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
195 rush to implement it. */
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
196
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
197 errno = EINTR;
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
198 }
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
199
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
200 /* FIXME: Restore sig handler? */
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
201
2056
cf3d523888c5 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2053
diff changeset
202 return suspended;
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
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