summaryrefslogtreecommitdiff
path: root/usr
diff options
context:
space:
mode:
authorMarcel Telka <marcel@telka.sk>2019-12-10 15:01:42 +0100
committerRobert Mustacchi <rm@fingolfin.org>2019-12-17 23:10:01 +0000
commit5947648b7f5c085635051e1f7aa083a309542467 (patch)
treeace8e18a4da2fe0552f9117828244d66e6e560c0 /usr
parentfc5c75cf5edb072564020725faa0c4313714f09f (diff)
downloadillumos-gate-5947648b7f5c085635051e1f7aa083a309542467.tar.gz
12069 Backport sh_delay() and tvsleep() from ksh-2020.0.0
Reviewed by: Dan McDonald <danmcd@joyent.com> Reviewed by: Andy Fiddaman <omnios@citrus-it.co.uk> Approved by: Robert Mustacchi <rm@fingolfin.org>
Diffstat (limited to 'usr')
-rw-r--r--usr/src/lib/libast/common/tm/tvsleep.c174
-rw-r--r--usr/src/lib/libshell/common/bltins/sleep.c114
2 files changed, 70 insertions, 218 deletions
diff --git a/usr/src/lib/libast/common/tm/tvsleep.c b/usr/src/lib/libast/common/tm/tvsleep.c
index 46d3d6ac59..7ada03eacb 100644
--- a/usr/src/lib/libast/common/tm/tvsleep.c
+++ b/usr/src/lib/libast/common/tm/tvsleep.c
@@ -1,144 +1,48 @@
/***********************************************************************
-* *
-* This software is part of the ast package *
-* Copyright (c) 1985-2010 AT&T Intellectual Property *
-* and is licensed under the *
-* Common Public License, Version 1.0 *
-* by AT&T Intellectual Property *
-* *
-* A copy of the License is available at *
-* http://www.opensource.org/licenses/cpl1.0.txt *
-* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
-* *
-* Information and Software Systems Research *
-* AT&T Research *
-* Florham Park NJ *
-* *
-* Glenn Fowler <gsf@research.att.com> *
-* David Korn <dgk@research.att.com> *
-* Phong Vo <kpv@research.att.com> *
-* *
-***********************************************************************/
-#pragma prototyped
-
-#include <tv.h>
-#include <tm.h>
-
-#include "FEATURE/tvlib"
-
-#if !_lib_nanosleep
-# if _lib_select
-# if _sys_select
-# include <sys/select.h>
-# else
-# include <sys/socket.h>
-# endif
-# else
-# if !_lib_usleep
-# if _lib_poll_notimer
-# undef _lib_poll
-# endif
-# if _lib_poll
-# include <poll.h>
-# endif
-# endif
-# endif
-#endif
+ * *
+ * This software is part of the ast package *
+ * Copyright (c) 1985-2013 AT&T Intellectual Property *
+ * and is licensed under the *
+ * Eclipse Public License, Version 1.0 *
+ * by AT&T Intellectual Property *
+ * *
+ * A copy of the License is available at *
+ * http://www.eclipse.org/org/documents/epl-v10.html *
+ * (with md5 checksum b35adb5213ca9657e911e9befb180842) *
+ * *
+ * Information and Software Systems Research *
+ * AT&T Research *
+ * Florham Park NJ *
+ * *
+ * Glenn Fowler <glenn.s.fowler@gmail.com> *
+ * David Korn <dgkorn@gmail.com> *
+ * Phong Vo <phongvo@gmail.com> *
+ * *
+ ***********************************************************************/
+
+#include <errno.h>
+#include <time.h>
+
+#include "tv.h"
/*
* sleep for tv
* non-zero exit if sleep did not complete
* with remaining time in rv
+ *
+ * NOTE: some systems hide nanosleep() ouside of -lc -- puleeze
*/
-int
-tvsleep(register const Tv_t* tv, register Tv_t* rv)
-{
-
-#if _lib_nanosleep
-
- struct timespec stv;
- struct timespec srv;
- int r;
-
- stv.tv_sec = tv->tv_sec;
- stv.tv_nsec = tv->tv_nsec;
- if ((r = nanosleep(&stv, &srv)) && rv)
- {
- rv->tv_sec = srv.tv_sec;
- rv->tv_nsec = srv.tv_nsec;
- }
- return r;
-
-#else
-
-#if _lib_select
-
- struct timeval stv;
-
- stv.tv_sec = tv->tv_sec;
- stv.tv_usec = tv->tv_nsec / 1000;
- if (select(0, NiL, NiL, NiL, &stv) < 0)
- {
- if (rv)
- *rv = *tv;
- return -1;
- }
- if (rv)
- {
- rv->tv_sec = stv.tv_sec;
- rv->tv_nsec = stv.tv_usec * 1000;
- }
- return 0;
-
-#else
-
- unsigned int s = tv->tv_sec;
- uint32_t n = tv->tv_nsec;
-
-#if _lib_usleep
-
-
- unsigned long t;
-
- if (t = (n + 999L) / 1000L)
- {
- usleep(t);
- s -= t / 1000000L;
- n = 0;
- }
-
-#else
-
-#if _lib_poll
-
- struct pollfd pfd;
- int t;
-
- if ((t = (n + 999999L) / 1000000L) > 0)
- {
- poll(&pfd, 0, t);
- s -= t / 1000L;
- n = 0;
- }
-
-#endif
-
-#endif
-
- if ((s += (n + 999999999L) / 1000000000L) && (s = sleep(s)))
- {
- if (rv)
- {
- rv->tv_sec = s;
- rv->tv_nsec = 0;
- }
- return -1;
- }
- return 0;
-
-#endif
-
-#endif
-
+int tvsleep(const Tv_t *tv, Tv_t *rv) {
+ struct timespec stv;
+ struct timespec srv;
+ int r;
+
+ stv.tv_sec = tv->tv_sec;
+ stv.tv_nsec = tv->tv_nsec;
+ if ((r = nanosleep(&stv, &srv)) && errno == EINTR && rv) {
+ rv->tv_sec = srv.tv_sec;
+ rv->tv_nsec = srv.tv_nsec;
+ }
+ return r;
}
diff --git a/usr/src/lib/libshell/common/bltins/sleep.c b/usr/src/lib/libshell/common/bltins/sleep.c
index 6121cfe62b..8938dc536c 100644
--- a/usr/src/lib/libshell/common/bltins/sleep.c
+++ b/usr/src/lib/libshell/common/bltins/sleep.c
@@ -1,22 +1,22 @@
/***********************************************************************
-* *
-* This software is part of the ast package *
-* Copyright (c) 1982-2010 AT&T Intellectual Property *
-* and is licensed under the *
-* Common Public License, Version 1.0 *
-* by AT&T Intellectual Property *
-* *
-* A copy of the License is available at *
-* http://www.opensource.org/licenses/cpl1.0.txt *
-* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
-* *
-* Information and Software Systems Research *
-* AT&T Research *
-* Florham Park NJ *
-* *
-* David Korn <dgk@research.att.com> *
-* *
-***********************************************************************/
+ * *
+ * This software is part of the ast package *
+ * Copyright (c) 1982-2013 AT&T Intellectual Property *
+ * and is licensed under the *
+ * Eclipse Public License, Version 1.0 *
+ * by AT&T Intellectual Property *
+ * *
+ * A copy of the License is available at *
+ * http://www.eclipse.org/org/documents/epl-v10.html *
+ * (with md5 checksum b35adb5213ca9657e911e9befb180842) *
+ * *
+ * Information and Software Systems Research *
+ * AT&T Research *
+ * Florham Park NJ *
+ * *
+ * David Korn <dgkorn@gmail.com> *
+ * *
+ ***********************************************************************/
#pragma prototyped
/*
* sleep delay
@@ -156,70 +156,18 @@ unsigned int sleep(unsigned int sec)
return(0);
}
-/*
- * delay execution for time <t>
- */
+//
+// Delay execution for time <t>.
+//
+void sh_delay(double t) {
+ Shell_t *shp = sh_getinterp();
+ int n = (int)t;
+ Tv_t ts, tx;
-void sh_delay(double t)
-{
- register int n = (int)t;
- Shell_t *shp = &sh;
-#ifdef _lib_poll
- struct pollfd fd;
- if(t<=0)
- return;
- else if(n > 30)
- {
- sleep(n);
- t -= n;
- }
- if(n=(int)(1000*t))
- {
- if(!shp->waitevent || (*shp->waitevent)(-1,(long)n,0)==0)
- poll(&fd,0,n);
- }
-#else
-# if defined(_lib_select) && defined(_mem_tv_usec_timeval)
- struct timeval timeloc;
- if(t<=0)
- return;
- if(n=(int)(1000*t) && shp->waitevent && (*shp->waitevent)(-1,(long)n,0))
- return;
- n = (int)t;
- timeloc.tv_sec = n;
- timeloc.tv_usec = 1000000*(t-(double)n);
- select(0,(fd_set*)0,(fd_set*)0,(fd_set*)0,&timeloc);
-# else
-# ifdef _lib_select
- /* for 9th edition machines */
- if(t<=0)
- return;
- if(n > 30)
- {
- sleep(n);
- t -= n;
- }
- if(n=(int)(1000*t))
- {
- if(!shp->waitevent || (*shp->waitevent)(-1,(long)n,0)==0)
- select(0,(fd_set*)0,(fd_set*)0,n);
- }
-# else
- struct tms tt;
- if(t<=0)
- return;
- sleep(n);
- t -= n;
- if(t)
- {
- clock_t begin = times(&tt);
- if(begin==0)
- return;
- t *= shp->lim.clk_tck;
- n += (t+.5);
- while((times(&tt)-begin) < n);
- }
-# endif
-# endif
-#endif /* _lib_poll */
+ ts.tv_sec = n;
+ ts.tv_nsec = 1000000000 * (t - (double)n);
+ while (tvsleep(&ts, &tx) < 0 && errno == EINTR) {
+ if (shp->trapnote & (SH_SIGSET | SH_SIGTRAP)) return;
+ ts = tx;
+ }
}