From ebc07949ff8990dfbe8ee8b27871fec372f31c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josef=20S=C3=B6ntgen?= Date: Wed, 20 Aug 2014 16:48:12 +0200 Subject: [PATCH] libc: add support for Rtc_file_system Fixes #1241. --- repos/libports/lib/mk/libc.mk | 2 +- repos/libports/src/lib/libc/rtc.cc | 55 +++++++++++++++++++++++ repos/libports/src/lib/libc/vfs_plugin.cc | 9 ++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 repos/libports/src/lib/libc/rtc.cc diff --git a/repos/libports/lib/mk/libc.mk b/repos/libports/lib/mk/libc.mk index 1f759f60c..961df7e54 100644 --- a/repos/libports/lib/mk/libc.mk +++ b/repos/libports/lib/mk/libc.mk @@ -14,7 +14,7 @@ SRC_CC = atexit.cc dummies.cc rlimit.cc sysctl.cc \ gettimeofday.cc malloc.cc progname.cc fd_alloc.cc file_operations.cc \ plugin.cc plugin_registry.cc select.cc exit.cc environ.cc nanosleep.cc \ libc_mem_alloc.cc pread_pwrite.cc readv_writev.cc poll.cc \ - libc_pdbg.cc vfs_plugin.cc + libc_pdbg.cc vfs_plugin.cc rtc.cc INC_DIR += $(REP_DIR)/src/lib/libc diff --git a/repos/libports/src/lib/libc/rtc.cc b/repos/libports/src/lib/libc/rtc.cc new file mode 100644 index 000000000..d9826f256 --- /dev/null +++ b/repos/libports/src/lib/libc/rtc.cc @@ -0,0 +1,55 @@ +/* + * \brief C-library back end + * \author Josef Soentgen + * \date 2014-08-20 + */ + +/* + * Copyright (C) 2014 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU General Public License version 2. + */ + +#include +#include + +#include +#include +#include +#include +#include +#include + + +namespace Libc { + extern char const *config_rtc(); + time_t read_rtc(); +} + +time_t Libc::read_rtc() +{ + time_t rtc = 0; + + int fd = open(Libc::config_rtc(), O_RDONLY); + if (fd == -1) + return rtc; + + char buf[32]; + ssize_t n = read(fd, buf, sizeof(buf)); + if (n > 0) { + buf[n - 1] = '\0'; + struct tm tm; + Genode::memset(&tm, 0, sizeof(tm)); + + if (strptime(buf, "%Y-%m-%d %R", &tm)) { + rtc = mktime(&tm); + if (rtc == (time_t)-1) + rtc = 0; + } + } + + close(fd); + + return rtc; +} diff --git a/repos/libports/src/lib/libc/vfs_plugin.cc b/repos/libports/src/lib/libc/vfs_plugin.cc index 44c7c4cef..acb628b32 100644 --- a/repos/libports/src/lib/libc/vfs_plugin.cc +++ b/repos/libports/src/lib/libc/vfs_plugin.cc @@ -27,6 +27,7 @@ #include #include #include +#include /* libc includes */ #include @@ -158,6 +159,7 @@ class Libc_file_system_factory : public Vfs::File_system_factory _add_builtin_fs(); _add_builtin_fs(); _add_builtin_fs(); + _add_builtin_fs(); } }; @@ -228,6 +230,13 @@ namespace Libc { static Config_attr stderr("stderr", ""); return stderr.string(); } + + char const *config_rtc() __attribute__((weak)); + char const *config_rtc() + { + static Config_attr rtc("rtc", ""); + return rtc.string(); + } }