Merge pull request #9120 from poettering/timedated-timezone-minifixes

Minor improvements to timedated
This commit is contained in:
Yu Watanabe 2018-05-30 12:56:23 +09:00 committed by GitHub
commit b014b3a524
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -552,23 +552,33 @@ static int method_set_timezone(sd_bus_message *m, void *userdata, sd_bus_error *
return sd_bus_error_set_errnof(error, r, "Failed to set time zone: %m");
}
/* 2. Tell the kernel our timezone */
clock_set_timezone(NULL);
/* 2. Make glibc notice the new timezone */
tzset();
/* 3. Tell the kernel our timezone */
r = clock_set_timezone(NULL);
if (r < 0)
log_debug_errno(r, "Failed to tell kernel about timezone, ignoring: %m");
if (c->local_rtc) {
struct timespec ts;
struct tm *tm;
/* 3. Sync RTC from system clock, with the new delta */
/* 4. Sync RTC from system clock, with the new delta */
assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
assert_se(tm = localtime(&ts.tv_sec));
clock_set_hwclock(tm);
r = clock_set_hwclock(tm);
if (r < 0)
log_debug_errno(r, "Failed to sync time to hardware clock, ignoring: %m");
}
log_struct(LOG_INFO,
"MESSAGE_ID=" SD_MESSAGE_TIMEZONE_CHANGE_STR,
"TIMEZONE=%s", c->zone,
LOG_MESSAGE("Changed time zone to '%s'.", c->zone),
"TIMEZONE_SHORTNAME=%s", tzname[daylight],
"DAYLIGHT=%i", daylight,
LOG_MESSAGE("Changed time zone to '%s' (%s).", c->zone, tzname[daylight]),
NULL);
(void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m), "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "Timezone", NULL);
@ -616,7 +626,9 @@ static int method_set_local_rtc(sd_bus_message *m, void *userdata, sd_bus_error
}
/* 2. Tell the kernel our timezone */
clock_set_timezone(NULL);
r = clock_set_timezone(NULL);
if (r < 0)
log_debug_errno(r, "Failed to tell kernel about timezone, ignoring: %m");
/* 3. Synchronize clocks */
assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
@ -624,27 +636,25 @@ static int method_set_local_rtc(sd_bus_message *m, void *userdata, sd_bus_error
if (fix_system) {
struct tm tm;
/* Sync system clock from RTC; first,
* initialize the timezone fields of
* struct tm. */
/* Sync system clock from RTC; first, initialize the timezone fields of struct tm. */
if (c->local_rtc)
tm = *localtime(&ts.tv_sec);
else
tm = *gmtime(&ts.tv_sec);
/* Override the main fields of
* struct tm, but not the timezone
* fields */
if (clock_get_hwclock(&tm) >= 0) {
/* And set the system clock
* with this */
/* Override the main fields of struct tm, but not the timezone fields */
r = clock_get_hwclock(&tm);
if (r < 0)
log_debug_errno(r, "Failed to get hardware clock, ignoring: %m");
else {
/* And set the system clock with this */
if (c->local_rtc)
ts.tv_sec = mktime(&tm);
else
ts.tv_sec = timegm(&tm);
clock_settime(CLOCK_REALTIME, &ts);
if (clock_settime(CLOCK_REALTIME, &ts) < 0)
log_debug_errno(errno, "Failed to update system clock, ignoring: %m");
}
} else {
@ -656,7 +666,9 @@ static int method_set_local_rtc(sd_bus_message *m, void *userdata, sd_bus_error
else
tm = gmtime(&ts.tv_sec);
clock_set_hwclock(tm);
r = clock_set_hwclock(tm);
if (r < 0)
log_debug_errno(r, "Failed to sync time to hardware clock, ignoring: %m");
}
log_info("RTC configured to %s time.", c->local_rtc ? "local" : "UTC");
@ -745,7 +757,10 @@ static int method_set_time(sd_bus_message *m, void *userdata, sd_bus_error *erro
tm = localtime(&ts.tv_sec);
else
tm = gmtime(&ts.tv_sec);
clock_set_hwclock(tm);
r = clock_set_hwclock(tm);
if (r < 0)
log_debug_errno(r, "Failed to update hardware clock, ignoring: %m");
log_struct(LOG_INFO,
"MESSAGE_ID=" SD_MESSAGE_TIME_CHANGE_STR,