From 1be4a0aeaf3888bb5f7a86fc4d2fef1df740c746 Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Wed, 20 Dec 2017 12:11:11 +0100 Subject: [PATCH] libc: prevent timer session if rtc not configured This patch makes the creation of the libc's timer session depend on whether or not the 'rtc' attribute of the configuration is defined. If not configured, 'clock_gettime' returns 0. Fixes #2625 --- repos/libports/lib/mk/libc.mk | 4 +- repos/libports/run/smartcard.run | 4 +- repos/libports/src/lib/libc/clock_gettime.cc | 44 ------------ repos/libports/src/lib/libc/gettimeofday.cc | 44 ------------ repos/libports/src/lib/libc/rtc.cc | 6 +- repos/libports/src/lib/libc/time.cc | 71 ++++++++++++++++++++ repos/ports/run/netperf.inc | 9 ++- 7 files changed, 85 insertions(+), 97 deletions(-) delete mode 100644 repos/libports/src/lib/libc/clock_gettime.cc delete mode 100644 repos/libports/src/lib/libc/gettimeofday.cc create mode 100644 repos/libports/src/lib/libc/time.cc diff --git a/repos/libports/lib/mk/libc.mk b/repos/libports/lib/mk/libc.mk index 9fb6e14d8c..98a1efa038 100644 --- a/repos/libports/lib/mk/libc.mk +++ b/repos/libports/lib/mk/libc.mk @@ -11,8 +11,8 @@ LIBS += base vfs # Back end # SRC_CC = atexit.cc dummies.cc rlimit.cc sysctl.cc \ - issetugid.cc errno.cc gai_strerror.cc clock_gettime.cc \ - gettimeofday.cc malloc.cc progname.cc fd_alloc.cc file_operations.cc \ + issetugid.cc errno.cc gai_strerror.cc time.cc \ + malloc.cc progname.cc fd_alloc.cc file_operations.cc \ plugin.cc plugin_registry.cc select.cc exit.cc environ.cc nanosleep.cc \ pread_pwrite.cc readv_writev.cc poll.cc \ libc_pdbg.cc vfs_plugin.cc rtc.cc dynamic_linker.cc signal.cc \ diff --git a/repos/libports/run/smartcard.run b/repos/libports/run/smartcard.run index d78b923404..1b664e9cee 100644 --- a/repos/libports/run/smartcard.run +++ b/repos/libports/run/smartcard.run @@ -87,7 +87,7 @@ append config { - + 2018-01-01 00:01 @@ -97,7 +97,7 @@ append config { - + } diff --git a/repos/libports/src/lib/libc/clock_gettime.cc b/repos/libports/src/lib/libc/clock_gettime.cc deleted file mode 100644 index 6a44e999e5..0000000000 --- a/repos/libports/src/lib/libc/clock_gettime.cc +++ /dev/null @@ -1,44 +0,0 @@ -/* - * \brief C-library back end - * \author Christian Prochaska - * \date 2010-05-19 - */ - -/* - * Copyright (C) 2010-2017 Genode Labs GmbH - * - * This file is part of the Genode OS framework, which is distributed - * under the terms of the GNU Affero General Public License version 3. - */ - -/* Libc includes */ -#include - -#include "task.h" - - -namespace Libc { time_t read_rtc(); } - - -extern "C" __attribute__((weak)) -int clock_gettime(clockid_t clk_id, struct timespec *tp) -{ - if (!tp) return 0; - - static bool read_rtc = false; - static time_t rtc = 0; - static unsigned long t0 = 0; - - if (!read_rtc) { - rtc = Libc::read_rtc(); - read_rtc = true; - t0 = Libc::current_time(); - } - - unsigned long time = Libc::current_time() - t0; - - tp->tv_sec = rtc + time/1000; - tp->tv_nsec = (time % 1000) * (1000*1000); - - return 0; -} diff --git a/repos/libports/src/lib/libc/gettimeofday.cc b/repos/libports/src/lib/libc/gettimeofday.cc deleted file mode 100644 index 059c11f342..0000000000 --- a/repos/libports/src/lib/libc/gettimeofday.cc +++ /dev/null @@ -1,44 +0,0 @@ -/* - * \brief C-library back end - * \author Norman Feske - * \date 2008-11-11 - */ - -/* - * Copyright (C) 2008-2017 Genode Labs GmbH - * - * This file is part of the Genode OS framework, which is distributed - * under the terms of the GNU Affero General Public License version 3. - */ - -/* Libc includes */ -#include - -#include "task.h" - - -namespace Libc { time_t read_rtc(); } - - -extern "C" __attribute__((weak)) -int gettimeofday(struct timeval *tv, struct timezone *) -{ - if (!tv) return 0; - - static bool read_rtc = false; - static time_t rtc = 0; - static unsigned long t0 = 0; - - if (!read_rtc) { - rtc = Libc::read_rtc(); - read_rtc = true; - t0 = Libc::current_time(); - } - - unsigned long time = Libc::current_time() - t0; - - tv->tv_sec = rtc + time/1000; - tv->tv_usec = (time % 1000) * 1000; - - return 0; -} diff --git a/repos/libports/src/lib/libc/rtc.cc b/repos/libports/src/lib/libc/rtc.cc index 769c2a6b87..a45d6d80f2 100644 --- a/repos/libports/src/lib/libc/rtc.cc +++ b/repos/libports/src/lib/libc/rtc.cc @@ -11,7 +11,7 @@ * under the terms of the GNU Affero General Public License version 3. */ -#include +#include #include #include @@ -32,13 +32,13 @@ time_t Libc::read_rtc() time_t rtc = 0; if (!Genode::strcmp(Libc::config_rtc(), "")) { - PWRN("%s: rtc not configured, returning %lld", __func__, (long long)rtc); + Genode::warning("rtc not configured, returning ", rtc); return rtc; } int fd = open(Libc::config_rtc(), O_RDONLY); if (fd == -1) { - PWRN("%s: %s not readable, returning %lld", __func__, Libc::config_rtc(), (long long)rtc); + Genode::warning(Genode::Cstring(Libc::config_rtc()), " not readable, returning ", rtc); return rtc; } diff --git a/repos/libports/src/lib/libc/time.cc b/repos/libports/src/lib/libc/time.cc new file mode 100644 index 0000000000..b0ac34a135 --- /dev/null +++ b/repos/libports/src/lib/libc/time.cc @@ -0,0 +1,71 @@ +/* + * \brief C-library back end + * \author Christian Prochaska + * \author Norman Feske + * \date 2010-05-19 + */ + +/* + * Copyright (C) 2010-2017 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU Affero General Public License version 3. + */ + +/* Libc includes */ +#include + +#include "task.h" +#include "libc_errno.h" + +namespace Libc { time_t read_rtc(); } + + +extern "C" __attribute__((weak)) +int clock_gettime(clockid_t clk_id, struct timespec *ts) +{ + if (!ts) return 0; + + static bool initial_rtc_requested = false; + static time_t initial_rtc = 0; + static unsigned long t0 = 0; + + ts->tv_sec = 0; + ts->tv_nsec = 0; + + /* try to read rtc once */ + if (!initial_rtc_requested) { + initial_rtc_requested = true; + + initial_rtc = Libc::read_rtc(); + + if (initial_rtc) + t0 = Libc::current_time(); + } + + if (!initial_rtc) + return Libc::Errno(EINVAL); + + unsigned long time = Libc::current_time() - t0; + + ts->tv_sec = initial_rtc + time/1000; + ts->tv_nsec = (time % 1000) * (1000*1000); + + return 0; +} + + +extern "C" __attribute__((weak)) +int gettimeofday(struct timeval *tv, struct timezone *) +{ + if (!tv) return 0; + + struct timespec ts; + + if (int ret = clock_gettime(0, &ts)) + return ret; + + tv->tv_sec = ts.tv_sec; + tv->tv_usec = ts.tv_nsec / 1000; + return 0; +} diff --git a/repos/ports/run/netperf.inc b/repos/ports/run/netperf.inc index 06c411f22d..5ba560d760 100644 --- a/repos/ports/run/netperf.inc +++ b/repos/ports/run/netperf.inc @@ -224,6 +224,7 @@ append_if $use_wifi_driver config { + 2018-01-01 00:01 @@ -253,8 +254,12 @@ append config { - + stdout="/dev/log" stderr="/dev/log" rtc="/dev/rtc"/> + + + 2018-01-01 00:01 + + } append_if $use_nic_bridge config {