libc: merge RTC and time backends

Issue #3450
This commit is contained in:
Josef Söntgen 2019-08-12 16:28:29 +02:00 committed by Christian Helmuth
parent 1fdd5b636b
commit 18e586daed
3 changed files with 47 additions and 64 deletions

View File

@ -16,7 +16,7 @@ SRC_CC = atexit.cc dummies.cc rlimit.cc sysctl.cc \
malloc.cc progname.cc fd_alloc.cc file_operations.cc \
plugin.cc plugin_registry.cc select.cc exit.cc environ.cc sleep.cc \
pread_pwrite.cc readv_writev.cc poll.cc \
vfs_plugin.cc rtc.cc dynamic_linker.cc signal.cc \
vfs_plugin.cc dynamic_linker.cc signal.cc \
socket_operations.cc task.cc socket_fs_plugin.cc syscall.cc \
getpwent.cc getrandom.cc

View File

@ -1,62 +0,0 @@
/*
* \brief C-library back end
* \author Josef Soentgen
* \date 2014-08-20
*/
/*
* Copyright (C) 2014-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.
*/
#include <base/log.h>
#include <util/string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
namespace Libc {
extern char const *config_rtc();
time_t read_rtc();
}
time_t Libc::read_rtc()
{
time_t rtc = 0;
if (!Genode::strcmp(Libc::config_rtc(), "")) {
Genode::warning("rtc not configured, returning ", rtc);
return rtc;
}
int fd = open(Libc::config_rtc(), O_RDONLY);
if (fd == -1) {
Genode::warning(Genode::Cstring(Libc::config_rtc()), " not readable, returning ", rtc);
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;
}

View File

@ -14,6 +14,12 @@
/* Libc includes */
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "task.h"
#include "libc_errno.h"
@ -21,7 +27,46 @@
/* Genode includes */
#include <base/log.h>
namespace Libc { time_t read_rtc(); }
namespace Libc {
extern char const *config_rtc();
time_t read_rtc();
}
time_t Libc::read_rtc()
{
time_t rtc = 0;
if (!Genode::strcmp(Libc::config_rtc(), "")) {
Genode::warning("rtc not configured, returning ", rtc);
return rtc;
}
int fd = open(Libc::config_rtc(), O_RDONLY);
if (fd == -1) {
Genode::warning(Genode::Cstring(Libc::config_rtc()), " not readable, returning ", rtc);
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;
}
extern "C" __attribute__((weak))