2017-11-18 17:09:20 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1+ */
|
2012-05-07 21:06:55 +02:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2015-11-30 21:43:37 +01:00
|
|
|
#include <limits.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <time.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <linux/rtc.h>
|
|
|
|
#include <stdio.h>
|
2012-05-07 21:06:55 +02:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2015-10-25 13:14:12 +01:00
|
|
|
#include "clock-util.h"
|
|
|
|
#include "fd-util.h"
|
2012-05-07 21:06:55 +02:00
|
|
|
#include "macro.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2016-01-12 23:25:23 +01:00
|
|
|
#include "util.h"
|
2012-05-07 21:06:55 +02:00
|
|
|
|
2014-05-24 04:26:33 +02:00
|
|
|
int clock_get_hwclock(struct tm *tm) {
|
2013-11-19 01:12:03 +01:00
|
|
|
_cleanup_close_ int fd = -1;
|
2012-05-07 21:06:55 +02:00
|
|
|
|
|
|
|
assert(tm);
|
|
|
|
|
2013-04-25 02:02:40 +02:00
|
|
|
fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
|
2012-05-07 21:06:55 +02:00
|
|
|
if (fd < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
/* This leaves the timezone fields of struct tm
|
|
|
|
* uninitialized! */
|
|
|
|
if (ioctl(fd, RTC_RD_TIME, tm) < 0)
|
2013-11-19 01:12:03 +01:00
|
|
|
return -errno;
|
2012-05-07 21:06:55 +02:00
|
|
|
|
|
|
|
/* We don't know daylight saving, so we reset this in order not
|
2013-10-31 02:26:07 +01:00
|
|
|
* to confuse mktime(). */
|
2012-05-07 21:06:55 +02:00
|
|
|
tm->tm_isdst = -1;
|
|
|
|
|
2013-11-19 01:12:03 +01:00
|
|
|
return 0;
|
2012-05-07 21:06:55 +02:00
|
|
|
}
|
|
|
|
|
2014-05-24 04:26:33 +02:00
|
|
|
int clock_set_hwclock(const struct tm *tm) {
|
2013-11-19 01:12:03 +01:00
|
|
|
_cleanup_close_ int fd = -1;
|
2012-05-07 21:06:55 +02:00
|
|
|
|
|
|
|
assert(tm);
|
|
|
|
|
2013-04-25 02:02:40 +02:00
|
|
|
fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
|
2012-05-07 21:06:55 +02:00
|
|
|
if (fd < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
if (ioctl(fd, RTC_SET_TIME, tm) < 0)
|
2013-11-19 01:12:03 +01:00
|
|
|
return -errno;
|
2012-05-07 21:06:55 +02:00
|
|
|
|
2013-11-19 01:12:03 +01:00
|
|
|
return 0;
|
2012-05-07 21:06:55 +02:00
|
|
|
}
|
2012-09-17 17:42:13 +02:00
|
|
|
|
2016-02-26 11:25:22 +01:00
|
|
|
int clock_is_localtime(const char* adjtime_path) {
|
2013-04-18 09:11:22 +02:00
|
|
|
_cleanup_fclose_ FILE *f;
|
2012-05-07 21:06:55 +02:00
|
|
|
|
2017-12-08 20:52:38 +01:00
|
|
|
if (!adjtime_path)
|
2016-02-26 11:25:22 +01:00
|
|
|
adjtime_path = "/etc/adjtime";
|
|
|
|
|
2012-05-07 21:06:55 +02:00
|
|
|
/*
|
|
|
|
* The third line of adjtime is "UTC" or "LOCAL" or nothing.
|
|
|
|
* # /etc/adjtime
|
|
|
|
* 0.0 0 0
|
|
|
|
* 0
|
|
|
|
* UTC
|
|
|
|
*/
|
2016-02-26 11:25:22 +01:00
|
|
|
f = fopen(adjtime_path, "re");
|
2012-05-07 21:06:55 +02:00
|
|
|
if (f) {
|
|
|
|
char line[LINE_MAX];
|
|
|
|
bool b;
|
|
|
|
|
|
|
|
b = fgets(line, sizeof(line), f) &&
|
|
|
|
fgets(line, sizeof(line), f) &&
|
|
|
|
fgets(line, sizeof(line), f);
|
|
|
|
if (!b)
|
2016-02-26 12:33:41 +01:00
|
|
|
/* less than three lines -> default to UTC */
|
|
|
|
return 0;
|
2012-05-07 21:06:55 +02:00
|
|
|
|
|
|
|
truncate_nl(line);
|
2013-04-12 01:02:37 +02:00
|
|
|
return streq(line, "LOCAL");
|
2012-05-07 21:06:55 +02:00
|
|
|
|
2013-04-12 00:57:42 +02:00
|
|
|
} else if (errno != ENOENT)
|
2012-05-07 21:06:55 +02:00
|
|
|
return -errno;
|
|
|
|
|
2016-02-26 12:33:41 +01:00
|
|
|
/* adjtime not present -> default to UTC */
|
2013-04-12 01:02:37 +02:00
|
|
|
return 0;
|
2012-05-07 21:06:55 +02:00
|
|
|
}
|
|
|
|
|
2014-05-22 14:21:38 +02:00
|
|
|
int clock_set_timezone(int *min) {
|
2012-05-07 21:06:55 +02:00
|
|
|
const struct timeval *tv_null = NULL;
|
|
|
|
struct timespec ts;
|
2018-06-25 19:55:25 +02:00
|
|
|
struct tm tm;
|
2012-10-27 16:23:32 +02:00
|
|
|
int minutesdelta;
|
2012-05-07 21:06:55 +02:00
|
|
|
struct timezone tz;
|
|
|
|
|
|
|
|
assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
|
2018-06-25 19:55:25 +02:00
|
|
|
assert_se(localtime_r(&ts.tv_sec, &tm));
|
|
|
|
minutesdelta = tm.tm_gmtoff / 60;
|
2012-05-07 21:06:55 +02:00
|
|
|
|
2012-10-27 16:23:32 +02:00
|
|
|
tz.tz_minuteswest = -minutesdelta;
|
2014-12-10 20:00:07 +01:00
|
|
|
tz.tz_dsttime = 0; /* DST_NONE */
|
2012-05-07 21:06:55 +02:00
|
|
|
|
|
|
|
/*
|
2014-07-27 00:11:08 +02:00
|
|
|
* If the RTC does not run in UTC but in local time, the very first
|
|
|
|
* call to settimeofday() will set the kernel's timezone and will warp the
|
|
|
|
* system clock, so that it runs in UTC instead of the local time we
|
|
|
|
* have read from the RTC.
|
2012-05-07 21:06:55 +02:00
|
|
|
*/
|
|
|
|
if (settimeofday(tv_null, &tz) < 0)
|
2016-01-12 23:25:23 +01:00
|
|
|
return negative_errno();
|
|
|
|
|
2012-05-07 21:06:55 +02:00
|
|
|
if (min)
|
2012-10-27 16:23:32 +02:00
|
|
|
*min = minutesdelta;
|
2012-05-07 21:06:55 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-07-27 00:11:08 +02:00
|
|
|
int clock_reset_timewarp(void) {
|
2012-05-07 21:06:55 +02:00
|
|
|
const struct timeval *tv_null = NULL;
|
|
|
|
struct timezone tz;
|
|
|
|
|
|
|
|
tz.tz_minuteswest = 0;
|
2014-12-10 20:00:07 +01:00
|
|
|
tz.tz_dsttime = 0; /* DST_NONE */
|
2012-05-07 21:06:55 +02:00
|
|
|
|
2012-09-17 16:41:13 +02:00
|
|
|
/*
|
2014-07-27 00:11:08 +02:00
|
|
|
* The very first call to settimeofday() does time warp magic. Do a
|
|
|
|
* dummy call here, so the time warping is sealed and all later calls
|
|
|
|
* behave as expected.
|
2012-09-17 16:41:13 +02:00
|
|
|
*/
|
2012-05-07 21:06:55 +02:00
|
|
|
if (settimeofday(tv_null, &tz) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2016-02-03 18:28:40 +01:00
|
|
|
|
|
|
|
#define TIME_EPOCH_USEC ((usec_t) TIME_EPOCH * USEC_PER_SEC)
|
|
|
|
|
|
|
|
int clock_apply_epoch(void) {
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
if (now(CLOCK_REALTIME) >= TIME_EPOCH_USEC)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (clock_settime(CLOCK_REALTIME, timespec_store(&ts, TIME_EPOCH_USEC)) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|