Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Mon, 01 Sep 2014 00:46:05 +0200
From: Jens Gustedt <Jens.Gustedt@...ia.fr>
To: musl@...ts.openwall.com
Subject: [PATCH 2/9] additions to src/time and some implied minor changes
 here and there

This refactors clock_gettime and adds two functions, thrd_sleep and
timespec_get.

The refactoring of clock_gettime introduces a function "  clock_gettime"
that has the same signature as clock_gettime, but that returns an
eventual error instead of returning -1 and setting errno. All places that
use clock_gettime functionality internally in musl do this
unconditionally, so there is no reason to mess around with errno.

As an additional fallout for kernels that don't implement the
clock_gettime syscall (yet!), the syscall that results in ENOSYS is
effected only once (instead, as previously, for every call) and a
fallback to gettimeofday is used.

The two newly introduced C11 functions have easy functional equivalences
in POSIX, but these have subtle differences in the handling of errors.

thrd_sleep forces concrete numerical values as error return

timespec_get has a call interface that is incompatible with POSIX because
it has a bogus coding for its clock constants, and also this clock
constants must be returned in case of success. For the moment we only
implement one single clock, TIME_UTC, and map this to
CLOCK_REALTIME. This is the clock that we later need to measure time for
the timedlock and timedwait.

Also, the error handling is different than the one for clock_gettime: the
possible reason for failure are returned and not set in errno.
---
 src/aio/aio_suspend.c      |    4 +++-
 src/network/res_mkquery.c  |    4 +++-
 src/network/res_msend.c    |    4 +++-
 src/time/__clock_gettime.c |   44 ++++++++++++++++++++++++++++++++++++++++++++
 src/time/clock_gettime.c   |   41 +++++++----------------------------------
 src/time/ftime.c           |    4 +++-
 src/time/gettimeofday.c    |    4 +++-
 src/time/thrd_sleep.c      |   26 ++++++++++++++++++++++++++
 src/time/timespec_get.c    |   12 ++++++++++++
 9 files changed, 104 insertions(+), 39 deletions(-)
 create mode 100644 src/time/__clock_gettime.c
 create mode 100644 src/time/thrd_sleep.c
 create mode 100644 src/time/timespec_get.c

diff --git a/src/aio/aio_suspend.c b/src/aio/aio_suspend.c
index 39a1d3a..1e3b7d2 100644
--- a/src/aio/aio_suspend.c
+++ b/src/aio/aio_suspend.c
@@ -2,6 +2,8 @@
 #include <errno.h>
 #include "pthread_impl.h"
 
+int __clock_gettime(clockid_t clk, struct timespec *ts);
+
 /* Due to the requirement that aio_suspend be async-signal-safe, we cannot
  * use any locks, wait queues, etc. that would make it more efficient. The
  * only obviously-correct algorithm is to generate a wakeup every time any
@@ -35,7 +37,7 @@ int aio_suspend(const struct aiocb *const cbs[], int cnt, const struct timespec
 		}
 
 		if (first && ts) {
-			clock_gettime(CLOCK_MONOTONIC, &at);
+			__clock_gettime(CLOCK_MONOTONIC, &at);
 			at.tv_sec += ts->tv_sec;
 			if ((at.tv_nsec += ts->tv_nsec) >= 1000000000) {
 				at.tv_nsec -= 1000000000;
diff --git a/src/network/res_mkquery.c b/src/network/res_mkquery.c
index ec4568a..8bbaf23 100644
--- a/src/network/res_mkquery.c
+++ b/src/network/res_mkquery.c
@@ -3,6 +3,8 @@
 #include <time.h>
 #include "libc.h"
 
+int __clock_gettime(clockid_t clk, struct timespec *ts);
+
 int __res_mkquery(int op, const char *dname, int class, int type,
 	const unsigned char *data, int datalen,
 	const unsigned char *newrr, unsigned char *buf, int buflen)
@@ -32,7 +34,7 @@ int __res_mkquery(int op, const char *dname, int class, int type,
 	q[i+3] = class;
 
 	/* Make a reasonably unpredictable id */
-	clock_gettime(CLOCK_REALTIME, &ts);
+	__clock_gettime(CLOCK_REALTIME, &ts);
 	id = ts.tv_nsec + ts.tv_nsec/65536UL & 0xffff;
 	q[0] = id/256;
 	q[1] = id;
diff --git a/src/network/res_msend.c b/src/network/res_msend.c
index 35f106d..eff968a 100644
--- a/src/network/res_msend.c
+++ b/src/network/res_msend.c
@@ -14,6 +14,8 @@
 #include "syscall.h"
 #include "lookup.h"
 
+int __clock_gettime(clockid_t clk, struct timespec *ts);
+
 static void cleanup(void *p)
 {
 	__syscall(SYS_close, (intptr_t)p);
@@ -22,7 +24,7 @@ static void cleanup(void *p)
 static unsigned long mtime()
 {
 	struct timespec ts;
-	clock_gettime(CLOCK_REALTIME, &ts);
+	__clock_gettime(CLOCK_REALTIME, &ts);
 	return (unsigned long)ts.tv_sec * 1000
 		+ ts.tv_nsec / 1000000;
 }
diff --git a/src/time/__clock_gettime.c b/src/time/__clock_gettime.c
new file mode 100644
index 0000000..adbd4e6
--- /dev/null
+++ b/src/time/__clock_gettime.c
@@ -0,0 +1,44 @@
+#include <time.h>
+#include <errno.h>
+#include <stdint.h>
+#include "syscall.h"
+#include "libc.h"
+#include "atomic.h"
+
+static int sc_clock_gettime(clockid_t clk, struct timespec *ts)
+{
+	return __syscall(SYS_clock_gettime, clk, ts);
+}
+
+static int sc_gettimeofday(clockid_t clk, struct timespec *ts)
+{
+	if (clk != CLOCK_REALTIME)
+		return EINVAL;
+	__syscall(SYS_gettimeofday, ts, 0);
+	ts->tv_nsec = (int)ts->tv_nsec * 1000;
+	return 0;
+}
+
+void *__vdsosym(const char *, const char *);
+
+int __clock_gettime(clockid_t clk, struct timespec *ts)
+{
+	static int (*cgt)(clockid_t, struct timespec *);
+	if (!cgt) {
+		int (*f)(clockid_t, struct timespec *) = 0;
+#ifdef VDSO_CGT_SYM
+		f = __vdsosym(VDSO_CGT_VER, VDSO_CGT_SYM);
+#endif
+		if (!f) {
+			int r = sc_clock_gettime(clk, ts);
+			if (r == -ENOSYS) {
+				f = sc_gettimeofday;
+			} else {
+				a_cas_p(&cgt, 0, sc_clock_gettime);
+				return r;
+			}
+		}
+		a_cas_p(&cgt, 0, f);
+	}
+	return cgt(clk, ts);
+}
diff --git a/src/time/clock_gettime.c b/src/time/clock_gettime.c
index 799251d..13ddb89 100644
--- a/src/time/clock_gettime.c
+++ b/src/time/clock_gettime.c
@@ -1,41 +1,14 @@
 #include <time.h>
 #include <errno.h>
-#include <stdint.h>
-#include "syscall.h"
-#include "libc.h"
-#include "atomic.h"
 
-static int sc_clock_gettime(clockid_t clk, struct timespec *ts)
+int __clock_gettime(clockid_t clk, struct timespec *ts);
+
+int clock_gettime(clockid_t clk, struct timespec *ts)
 {
-	int r = __syscall(SYS_clock_gettime, clk, ts);
-	if (!r) return r;
-	if (r == -ENOSYS) {
-		if (clk == CLOCK_REALTIME) {
-			__syscall(SYS_gettimeofday, ts, 0);
-			ts->tv_nsec = (int)ts->tv_nsec * 1000;
-			return 0;
-		}
-		r = -EINVAL;
-	}
+	int r = __clock_gettime(clk, ts);
+	/* In case of -1 lower levels already have already taken care
+	* of setting errno. */
+	if (r >= -1) return r;
 	errno = -r;
 	return -1;
 }
-
-void *__vdsosym(const char *, const char *);
-
-int __clock_gettime(clockid_t clk, struct timespec *ts)
-{
-#ifdef VDSO_CGT_SYM
-	static int (*cgt)(clockid_t, struct timespec *);
-	if (!cgt) {
-		void *f = __vdsosym(VDSO_CGT_VER, VDSO_CGT_SYM);
-		if (!f) f = (void *)sc_clock_gettime;
-		a_cas_p(&cgt, 0, f);
-	}
-	return cgt(clk, ts);
-#else
-	return sc_clock_gettime(clk, ts);
-#endif
-}
-
-weak_alias(__clock_gettime, clock_gettime);
diff --git a/src/time/ftime.c b/src/time/ftime.c
index a1734d0..00af1ca 100644
--- a/src/time/ftime.c
+++ b/src/time/ftime.c
@@ -1,10 +1,12 @@
 #include <sys/timeb.h>
 #include <time.h>
 
+int __clock_gettime(clockid_t clk, struct timespec *ts);
+
 int ftime(struct timeb *tp)
 {
 	struct timespec ts;
-	clock_gettime(CLOCK_REALTIME, &ts);
+	__clock_gettime(CLOCK_REALTIME, &ts);
 	tp->time = ts.tv_sec;
 	tp->millitm = ts.tv_nsec / 1000000;
 	tp->timezone = tp->dstflag = 0;
diff --git a/src/time/gettimeofday.c b/src/time/gettimeofday.c
index 691f8e9..375ec0e 100644
--- a/src/time/gettimeofday.c
+++ b/src/time/gettimeofday.c
@@ -2,11 +2,13 @@
 #include <sys/time.h>
 #include "syscall.h"
 
+int __clock_gettime(clockid_t clk, struct timespec *ts);
+
 int gettimeofday(struct timeval *restrict tv, void *restrict tz)
 {
 	struct timespec ts;
 	if (!tv) return 0;
-	clock_gettime(CLOCK_REALTIME, &ts);
+	__clock_gettime(CLOCK_REALTIME, &ts);
 	tv->tv_sec = ts.tv_sec;
 	tv->tv_usec = (int)ts.tv_nsec / 1000;
 	return 0;
diff --git a/src/time/thrd_sleep.c b/src/time/thrd_sleep.c
new file mode 100644
index 0000000..3dbfe47
--- /dev/null
+++ b/src/time/thrd_sleep.c
@@ -0,0 +1,26 @@
+#include <time.h>
+#include <errno.h>
+#include "syscall.h"
+#include "libc.h"
+#include "threads.h"
+
+/* Roughly __syscall already returns the right thing: 0 if all went
+   well or a negative error indication, otherwise. */
+int thrd_sleep(const struct timespec *req, struct timespec *rem)
+{
+	int ret = __syscall(SYS_nanosleep, req, rem);
+	switch (ret) {
+	case 0:
+		return 0;
+		/* error described by POSIX:                                    */
+		/* EINTR  The nanosleep() function was interrupted by a signal. */
+		/* The C11 wording is:                                          */
+		/* -1 if it has been interrupted by a signal                    */
+	case -EINTR:
+		return -1;
+		/* EINVAL: described by POSIX */
+		/* EFAULT: described for linux */
+	default:
+		return -2;
+	}
+}
diff --git a/src/time/timespec_get.c b/src/time/timespec_get.c
new file mode 100644
index 0000000..601c3cc
--- /dev/null
+++ b/src/time/timespec_get.c
@@ -0,0 +1,12 @@
+#include <time.h>
+
+int __clock_gettime(clockid_t clk, struct timespec *ts);
+
+/* There is no other implemented value than TIME_UTC, all other values
+   are considered erroneous. */
+int timespec_get(struct timespec * ts, int base)
+{
+	if (base != TIME_UTC) return 0;
+	int ret = __clock_gettime(CLOCK_REALTIME, ts);
+	return ret < 0 ? 0 : base;
+}
-- 
1.7.10.4

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.