genode/repos/libports/src/lib/libc/rtc.cc
Norman Feske 1be4a0aeaf 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 <libc> configuration is
defined. If not configured, 'clock_gettime' returns 0.

Fixes #2625
2018-01-17 12:14:33 +01:00

63 lines
1.1 KiB
C++

/*
* \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;
}