1
0
mirror of https://github.com/systemd/systemd synced 2024-07-01 07:34:28 +00:00

Move two functions only used in timesyncd from libshared to the binary

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2024-06-12 10:22:58 +02:00
parent 4d47aa8c04
commit 8e215e938a
6 changed files with 62 additions and 45 deletions

View File

@ -1,6 +1,5 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <fcntl.h>
#include <linux/rtc.h>
#include <stdio.h>
#include <sys/ioctl.h>
@ -14,40 +13,6 @@
#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;

View File

@ -1,12 +1,8 @@
/* 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/"

View File

@ -0,0 +1,44 @@
/* 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));
}

View File

@ -0,0 +1,7 @@
/* 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);

View File

@ -5,7 +5,11 @@ executables += [
'name' : 'systemd-timedated',
'dbus' : true,
'conditions' : ['ENABLE_TIMEDATED'],
'sources' : files('timedated.c'),
'sources' : files(
'timedated.c',
'hwclock-util.c',
),
},
executable_template + {
'name' : 'timedatectl',

View File

@ -28,6 +28,7 @@
#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"
@ -591,7 +592,7 @@ static int property_get_rtc_time(
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)
@ -719,7 +720,7 @@ static int method_set_timezone(sd_bus_message *m, void *userdata, sd_bus_error *
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");
}
@ -792,7 +793,7 @@ static int method_set_local_rtc(sd_bus_message *m, void *userdata, sd_bus_error
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 {
@ -809,7 +810,7 @@ static int method_set_local_rtc(sd_bus_message *m, void *userdata, sd_bus_error
/* 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");
}
@ -902,7 +903,7 @@ static int method_set_time(sd_bus_message *m, void *userdata, sd_bus_error *erro
/* 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");