/* SPDX-License-Identifier: LGPL-2.1-or-later */
-#include <fcntl.h>
#include <linux/rtc.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include "macro.h"
#include "string-util.h"
-int clock_get_hwclock(struct tm *tm) {
- _cleanup_close_ int fd = -EBADF;
-
- assert(tm);
-
- fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
- if (fd < 0)
- return -errno;
-
- /* This leaves the timezone fields of struct tm uninitialized! */
- if (ioctl(fd, RTC_RD_TIME, tm) < 0)
- /* Some drivers return -EINVAL in case the time could not be kept, i.e. power loss
- * happened. Let's turn that into a clearly recognizable error */
- return errno == EINVAL ? -ENODATA : -errno;
-
- /* We don't know daylight saving, so we reset this in order not
- * to confuse mktime(). */
- tm->tm_isdst = -1;
-
- return 0;
-}
-
-int clock_set_hwclock(const struct tm *tm) {
- _cleanup_close_ int fd = -EBADF;
-
- assert(tm);
-
- fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
- if (fd < 0)
- return -errno;
-
- return RET_NERRNO(ioctl(fd, RTC_SET_TIME, tm));
-}
-
int clock_is_localtime(const char* adjtime_path) {
_cleanup_fclose_ FILE *f = NULL;
int r;
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
-#include <time.h>
-
int clock_is_localtime(const char* adjtime_path);
int clock_set_timezone(int *ret_minutesdelta);
-int clock_get_hwclock(struct tm *tm);
-int clock_set_hwclock(const struct tm *tm);
#define EPOCH_CLOCK_FILE "/usr/lib/clock-epoch"
#define TIMESYNCD_CLOCK_FILE_DIR "/var/lib/systemd/timesync/"
--- /dev/null
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <fcntl.h>
+#include <linux/rtc.h>
+#include <sys/ioctl.h>
+#include <sys/time.h>
+
+#include "errno-util.h"
+#include "fd-util.h"
+#include "hwclock-util.h"
+
+int hwclock_get(struct tm *ret) {
+ _cleanup_close_ int fd = -EBADF;
+
+ assert(ret);
+
+ fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
+ if (fd < 0)
+ return -errno;
+
+ /* This leaves the timezone fields of struct ret uninitialized! */
+ if (ioctl(fd, RTC_RD_TIME, ret) < 0)
+ /* Some drivers return -EINVAL in case the time could not be kept, i.e. power loss
+ * happened. Let's turn that into a clearly recognizable error */
+ return errno == EINVAL ? -ENODATA : -errno;
+
+ /* We don't know daylight saving, so we reset this in order not
+ * to confuse mktime(). */
+ ret->tm_isdst = -1;
+
+ return 0;
+}
+
+int hwclock_set(const struct tm *tm) {
+ _cleanup_close_ int fd = -EBADF;
+
+ assert(tm);
+
+ fd = open("/dev/rtc", O_RDONLY|O_CLOEXEC);
+ if (fd < 0)
+ return -errno;
+
+ return RET_NERRNO(ioctl(fd, RTC_SET_TIME, tm));
+}
--- /dev/null
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+#include <time.h>
+
+int hwclock_get(struct tm *ret);
+int hwclock_set(const struct tm *tm);
'name' : 'systemd-timedated',
'dbus' : true,
'conditions' : ['ENABLE_TIMEDATED'],
- 'sources' : files('timedated.c'),
+ 'sources' : files(
+ 'timedated.c',
+ 'hwclock-util.c',
+ ),
+
},
executable_template + {
'name' : 'timedatectl',
#include "fileio.h"
#include "fs-util.h"
#include "hashmap.h"
+#include "hwclock-util.h"
#include "list.h"
#include "main-func.h"
#include "memory-util.h"
usec_t t = 0;
int r;
- r = clock_get_hwclock(&tm);
+ r = hwclock_get(&tm);
if (r == -EBUSY)
log_warning("/dev/rtc is busy. Is somebody keeping it open continuously? That's not a good idea... Returning a bogus RTC timestamp.");
else if (r == -ENOENT)
assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
assert_se(localtime_r(&ts.tv_sec, &tm));
- r = clock_set_hwclock(&tm);
+ r = hwclock_set(&tm);
if (r < 0)
log_debug_errno(r, "Failed to sync time to hardware clock, ignoring: %m");
}
localtime_or_gmtime_r(&ts.tv_sec, &tm, !c->local_rtc);
/* Override the main fields of struct tm, but not the timezone fields */
- r = clock_get_hwclock(&tm);
+ r = hwclock_get(&tm);
if (r < 0)
log_debug_errno(r, "Failed to get hardware clock, ignoring: %m");
else {
/* Sync RTC from system clock */
localtime_or_gmtime_r(&ts.tv_sec, &tm, !c->local_rtc);
- r = clock_set_hwclock(&tm);
+ r = hwclock_set(&tm);
if (r < 0)
log_debug_errno(r, "Failed to sync time to hardware clock, ignoring: %m");
}
/* Sync down to RTC */
localtime_or_gmtime_r(&ts.tv_sec, &tm, !c->local_rtc);
- r = clock_set_hwclock(&tm);
+ r = hwclock_set(&tm);
if (r < 0)
log_debug_errno(r, "Failed to update hardware clock, ignoring: %m");