" </method>\n" \
" <method name=\"SetLocalRTC\">\n" \
" <arg name=\"local_rtc\" type=\"b\" direction=\"in\"/>\n" \
+ " <arg name=\"correct_system\" type=\"b\" direction=\"in\"/>\n" \
" <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
" </method>\n" \
" </interface>\n" \
static int read_data(void) {
int r;
- FILE *f;
free_data();
verify_timezone();
- f = fopen("/etc/adjtime", "r");
- if (f) {
- char line[LINE_MAX];
- bool b;
-
- b = fgets(line, sizeof(line), f) &&
- fgets(line, sizeof(line), f) &&
- fgets(line, sizeof(line), f);
-
- fclose(f);
-
- if (!b)
- return -EIO;
-
- truncate_nl(line);
- local_rtc = streq(line, "LOCAL");
-
- } else if (errno != ENOENT)
- return -errno;
+ local_rtc = hwclock_is_localtime() > 0;
return 0;
}
free(zone);
zone = t;
+ /* 1. Write new configuration file */
r = write_data_timezone();
if (r < 0) {
log_error("Failed to set timezone: %s", strerror(-r));
return bus_send_error_reply(connection, message, NULL, r);
}
+ if (local_rtc) {
+ struct timespec ts;
+ struct tm *tm;
+
+ /* 2. Teach kernel new timezone */
+ hwclock_apply_localtime_delta();
+
+ /* 3. Sync RTC from system clock, with the new delta */
+ assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
+ assert_se(tm = localtime(&ts.tv_sec));
+ hwclock_set_time(tm);
+ }
+
log_info("Changed timezone to '%s'.", zone);
changed = bus_properties_changed_new(
} else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetLocalRTC")) {
dbus_bool_t lrtc;
+ dbus_bool_t correct_system;
dbus_bool_t interactive;
if (!dbus_message_get_args(
message,
&error,
DBUS_TYPE_BOOLEAN, &lrtc,
+ DBUS_TYPE_BOOLEAN, &correct_system,
DBUS_TYPE_BOOLEAN, &interactive,
DBUS_TYPE_INVALID))
return bus_send_error_reply(connection, message, &error, -EINVAL);
if (lrtc != local_rtc) {
+ struct timespec ts;
+
r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-local-rtc", interactive, &error);
if (r < 0)
return bus_send_error_reply(connection, message, &error, r);
local_rtc = lrtc;
+ /* 1. Write new configuration file */
r = write_data_local_rtc();
if (r < 0) {
log_error("Failed to set RTC to local/UTC: %s", strerror(-r));
return bus_send_error_reply(connection, message, NULL, r);
}
+ /* 2. Teach kernel new timezone */
+ if (local_rtc)
+ hwclock_apply_localtime_delta();
+ else
+ hwclock_reset_localtime_delta();
+
+ /* 3. Synchronize clocks */
+ assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
+
+ if (correct_system) {
+ struct tm tm;
+
+ /* Sync system clock from RTC; first,
+ * initialize the timezone fields of
+ * struct tm. */
+ if (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 (hwclock_get_time(&tm) >= 0) {
+
+ /* And set the system clock
+ * with this */
+ if (local_rtc)
+ ts.tv_sec = mktime(&tm);
+ else
+ ts.tv_sec = timegm(&tm);
+
+ clock_settime(CLOCK_REALTIME, &ts);
+ }
+
+ } else {
+ struct tm *tm;
+
+ /* Sync RTC from system clock */
+ if (local_rtc)
+ tm = localtime(&ts.tv_sec);
+ else
+ tm = gmtime(&ts.tv_sec);
+
+ hwclock_set_time(tm);
+ }
+
log_info("Changed local RTC setting to '%s'.", yes_no(local_rtc));
changed = bus_properties_changed_new(
if (!relative || utc != 0) {
struct timespec ts;
+ struct tm* tm;
r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-time", interactive, &error);
if (r < 0)
else
timespec_store(&ts, utc);
+ /* Set system clock */
if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
log_error("Failed to set local time: %m");
return bus_send_error_reply(connection, message, NULL, -errno);
}
+ /* Sync down to RTC */
+ if (local_rtc)
+ tm = localtime(&ts.tv_sec);
+ else
+ tm = gmtime(&ts.tv_sec);
+
+ hwclock_set_time(tm);
+
log_info("Changed local time to %s", ctime(&ts.tv_sec));
}
return r;
}
-bool hwclock_is_localtime(void) {
+int hwclock_is_localtime(void) {
FILE *f;
- char line[LINE_MAX];
bool local = false;
/*
* The third line of adjtime is "UTC" or "LOCAL" or nothing.
* # /etc/adjtime
- * 0.0 0 0.0
+ * 0.0 0 0
* 0
* UTC
*/
f = fopen("/etc/adjtime", "re");
if (f) {
- if (fgets(line, sizeof(line), f) &&
- fgets(line, sizeof(line), f) &&
- fgets(line, sizeof(line), f) ) {
- if (!strcmp(line, "LOCAL\n"))
- local = true;
- }
+ char line[LINE_MAX];
+ bool b;
+
+ b = fgets(line, sizeof(line), f) &&
+ fgets(line, sizeof(line), f) &&
+ fgets(line, sizeof(line), f);
+
fclose(f);
- }
+
+ if (!b)
+ return -EIO;
+
+
+ truncate_nl(line);
+ local = streq(line, "LOCAL");
+
+ } else if (errno != -ENOENT)
+ return -errno;
+
return local;
}
int hwclock_apply_localtime_delta(void) {
const struct timeval *tv_null = NULL;
- struct timeval tv;
+ struct timespec ts;
struct tm *tm;
int minuteswest;
struct timezone tz;
- gettimeofday(&tv, NULL);
- tm = localtime(&tv.tv_sec);
+ assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
+ assert_se(tm = localtime(&ts.tv_sec));
minuteswest = tm->tm_gmtoff / 60;
tz.tz_minuteswest = -minuteswest;
*/
if (settimeofday(tv_null, &tz) < 0)
return -errno;
- else
- return minuteswest;
+
+ return minuteswest;
+}
+
+int hwclock_reset_localtime_delta(void) {
+ const struct timeval *tv_null = NULL;
+ struct timezone tz;
+
+ tz.tz_minuteswest = 0;
+ tz.tz_dsttime = 0; /* DST_NONE*/
+
+ if (settimeofday(tv_null, &tz) < 0)
+ return -errno;
+
+ return 0;
}
int hwclock_get_time(struct tm *tm) {
int fd;
int err = 0;
+ assert(tm);
+
fd = open("/dev/rtc0", 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)
err = -errno;
- close(fd);
+
+ /* We don't now daylight saving, so we reset this in order not
+ * to confused mktime(). */
+ tm->tm_isdst = -1;
+
+ close_nointr_nofail(fd);
return err;
}
int fd;
int err = 0;
+ assert(tm);
+
fd = open("/dev/rtc0", O_RDONLY|O_CLOEXEC);
if (fd < 0)
return -errno;
+
if (ioctl(fd, RTC_SET_TIME, tm) < 0)
err = -errno;
- close(fd);
+
+ close_nointr_nofail(fd);
return err;
}