annotate lib/nanosleep.c @ 7302:8a1a9361108c

* _fpending.c: Include <config.h> unconditionally, since we no longer worry about uses that don't define HAVE_CONFIG_H. * acl.c, alloca.c, argmatch.c, atexit.c, backupfile.c: * basename.c, c-stack.c, c-strtod.c, calloc.c, canon-host.c: * canonicalize.c, chdir-long.c, chdir-safer.c, chown.c: * cloexec.c, close-stream.c, closeout.c, creat-safer.c: * cycle-check.c, diacrit.c, dirchownmod.c, dirfd.c, dirname.c: * dup-safer.c, dup2.c, error.c, euidaccess.c, exclude.c: * exitfail.c, fchmodat.c, fchown-stub.c, fd-safer.c: * file-type.c, fileblocks.c, filemode.c, filenamecat.c: * fnmatch.c, fopen-safer.c, fprintftime.c, free.c, fsusage.c: * ftruncate.c, fts-cycle.c, fts.c, full-write.c, gai_strerror.c: * getcwd.c, getdate.y, getdomainname.c, getgroups.c: * gethostname.c, gethrxtime.c, getloadavg.c, getlogin_r.c: * getndelim2.c, getnline.c, getopt.c, getopt1.c, getpass.c: * gettime.c, gettimeofday.c, getugroups.c, getusershell.c: * glob.c, group-member.c, hard-locale.c, hash-pjw.c, hash.c: * human.c, idcache.c, inet_ntop.c, inet_pton.c, inttostr.c: * isdir.c, lchown.c, linebuffer.c, long-options.c, lstat.c: * malloc.c, md5.c, memcasecmp.c, memchr.c, memcmp.c, memcoll.c: * memcpy.c, memmove.c, memrchr.c, mkancesdirs.c, mkdir-p.c: * mkdir.c, mkdirat.c, mkstemp-safer.c, mkstemp.c, modechange.c: * mountlist.c, nanosleep.c, obstack.c, open-safer.c: * openat-die.c, openat.c, pagealign_alloc.c, physmem.c: * pipe-safer.c, posixtm.c, posixver.c, putenv.c, quote.c: * quotearg.c, raise.c, readtokens.c, readtokens0.c, readutmp.c: * realloc.c, regex.c, rename.c, rmdir.c, rpmatch.c, safe-read.c: * same.c, save-cwd.c, savedir.c, setenv.c, settime.c, sha1.c: * sig2str.c, snprintf.c, strdup.c, strerror.c, strftime.c: * stripslash.c, strndup.c, strnlen.c, strpbrk.c, strtod.c: * strtoimax.c, strtol.c, strverscmp.c, tempname.c, time_r.c: * timegm.c, tmpfile-safer.c, unlinkdir.c, userspec.c, utime.c: * utimecmp.c, utimens.c, version-etc-fsf.c, version-etc.c: * xalloc-die.c, xgetcwd.c, xgethostname.c, xmalloc.c: * xmemcoll.c, xnanosleep.c, xreadlink.c, xstrtod.c: * xstrtoimax.c, xstrtol.c, xstrtoumax.c, yesno.c: Likewise.
author Paul Eggert <eggert@cs.ucla.edu>
date Wed, 13 Sep 2006 22:38:14 +0000
parents 6948138d9f3a
children b7a83a69ac23
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
7302
8a1a9361108c * _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents: 7298
diff changeset
22 #include <config.h>
2270
cf5eb0c29b04 #undef nanosleep.
Jim Meyering <jim@meyering.net>
parents: 2151
diff changeset
23
cf5eb0c29b04 #undef nanosleep.
Jim Meyering <jim@meyering.net>
parents: 2151
diff changeset
24 /* Undefine nanosleep here so any prototype is not redefined to be a
cf5eb0c29b04 #undef nanosleep.
Jim Meyering <jim@meyering.net>
parents: 2151
diff changeset
25 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
26 #undef nanosleep
cf5eb0c29b04 #undef nanosleep.
Jim Meyering <jim@meyering.net>
parents: 2151
diff changeset
27
5159
a535859efd14 Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5060
diff changeset
28 #include <stdbool.h>
2053
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
29 #include <stdio.h>
7298
6948138d9f3a * nanosleep.c: Include <sys/types.h> before sys/select.h, to avoid
Jim Meyering <jim@meyering.net>
parents: 7237
diff changeset
30 #include <sys/types.h>
6795
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
31 #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
32 # 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
33 #endif
2053
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
34 #include <signal.h>
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
35
6795
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
36 #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
37 # 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
38 # 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
39 #else
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
40 # 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
41 # 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
42 # else
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
43 # 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
44 # endif
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
45 #endif
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
46
2057
50da17269b1b *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2056
diff changeset
47 #include <errno.h>
50da17269b1b *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2056
diff changeset
48
6275
fd0ccce602e4 Sync from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6259
diff changeset
49 #include <unistd.h>
2056
cf3d523888c5 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2053
diff changeset
50
5813
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
51 #include "timespec.h"
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
52
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
53 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
54
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 #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
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 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
58 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
59 {
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 # 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
61 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
62 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
63 # 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
64 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
65 }
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
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 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
68 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
69 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
70 {
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 /* 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
72 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
73 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
74
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 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
76 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
77 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
78
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 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
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 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
82 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
83 {
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 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
85 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
86 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
87
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 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
89 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
90 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
91 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
92 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
93 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
94 {
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_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
96 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
97 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
98 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
99 }
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 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
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 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
103 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
104 }
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 }
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 #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
110
2687
34703e371994 (SIGCONT): Define if not already defined.
Jim Meyering <jim@meyering.net>
parents: 2341
diff changeset
111 /* Some systems (MSDOS) don't have SIGCONT.
34703e371994 (SIGCONT): Define if not already defined.
Jim Meyering <jim@meyering.net>
parents: 2341
diff changeset
112 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
113 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
114 # 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
115 # 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
116 # endif
2687
34703e371994 (SIGCONT): Define if not already defined.
Jim Meyering <jim@meyering.net>
parents: 2341
diff changeset
117
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
118 # 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
119 # 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
120 # endif
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
121
5060
4e59cddb690f nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents: 3767
diff changeset
122 static sig_atomic_t volatile suspended;
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
123
2053
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
124 /* Handle SIGCONT. */
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
125
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
126 static void
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
127 sighandler (int sig)
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
128 {
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
129 suspended = 1;
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
6795
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
132 /* Suspend execution for at least *TS_DELAY seconds. */
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
133
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
134 static void
2056
cf3d523888c5 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2053
diff changeset
135 my_usleep (const struct timespec *ts_delay)
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
136 {
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
137 struct timeval tv_delay;
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
138 tv_delay.tv_sec = ts_delay->tv_sec;
5060
4e59cddb690f nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents: 3767
diff changeset
139 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
140 if (tv_delay.tv_usec == 1000000)
4e59cddb690f nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents: 3767
diff changeset
141 {
6795
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
142 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
143 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
144 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
145 else
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
146 {
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
147 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
148 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
149 }
5060
4e59cddb690f nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents: 3767
diff changeset
150 }
5813
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
151 select (0, NULL, NULL, NULL, &tv_delay);
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
152 }
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
153
6795
9dc5bb3f3359 * lib/nanosleep.c [HAVE_SYS_SELECT_H]: Include <sys/select.h>.
Paul Eggert <eggert@cs.ucla.edu>
parents: 6275
diff changeset
154 /* 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
155 *REMAINING_DELAY part isn't implemented yet. */
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
156
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
157 int
2270
cf5eb0c29b04 #undef nanosleep.
Jim Meyering <jim@meyering.net>
parents: 2151
diff changeset
158 rpl_nanosleep (const struct timespec *requested_delay,
2341
12885d185e68 tweak indentation
Jim Meyering <jim@meyering.net>
parents: 2270
diff changeset
159 struct timespec *remaining_delay)
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
160 {
5159
a535859efd14 Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5060
diff changeset
161 static bool initialized;
5060
4e59cddb690f nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents: 3767
diff changeset
162
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
163 /* set up sig handler */
5060
4e59cddb690f nanosleep merge from coreutils
Paul Eggert <eggert@cs.ucla.edu>
parents: 3767
diff changeset
164 if (! initialized)
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
165 {
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
166 # ifdef SA_NOCLDSTOP
5813
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
167 struct sigaction oldact, newact;
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
168 newact.sa_handler = sighandler;
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
169 sigemptyset (&newact.sa_mask);
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
170 newact.sa_flags = 0;
2053
0d23f740f281 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2052
diff changeset
171
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
172 sigaction (SIGCONT, NULL, &oldact);
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
173 if (oldact.sa_handler != SIG_IGN)
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
174 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
175 # else
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
176 if (signal (SIGCONT, SIG_IGN) != SIG_IGN)
5813
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
177 {
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
178 signal (SIGCONT, sighandler);
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
179 siginterrupt (SIGCONT, 1);
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
180 }
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
181 # endif
5159
a535859efd14 Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5060
diff changeset
182 initialized = true;
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
183 }
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
184
5813
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
185 suspended = 0;
6962b5c5069f Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 5159
diff changeset
186
2056
cf3d523888c5 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2053
diff changeset
187 my_usleep (requested_delay);
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
188
2056
cf3d523888c5 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2053
diff changeset
189 if (suspended)
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
190 {
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
191 /* Calculate time remaining. */
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
192 /* 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
193 rush to implement it. */
2058
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
194
640074e7d6cd *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2057
diff changeset
195 errno = EINTR;
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
196 }
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
197
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
198 /* FIXME: Restore sig handler? */
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
199
2056
cf3d523888c5 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents: 2053
diff changeset
200 return suspended;
2052
3c79d37078c2 *** empty log message ***
Jim Meyering <jim@meyering.net>
parents:
diff changeset
201 }
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
202 #endif