1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <dbus/dbus.h>
30 #include "dbus-common.h"
33 #define NULL_ADJTIME_UTC "0.0 0 0\n0\nUTC\n"
34 #define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
36 #define INTROSPECTION \
37 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
39 " <interface name=\"org.freedesktop.timedate1\">\n" \
40 " <property name=\"Timezone\" type=\"s\" access=\"read\"/>\n" \
41 " <property name=\"LocalRTC\" type=\"b\" access=\"read\"/>\n" \
42 " <method name=\"SetTime\">\n" \
43 " <arg name=\"usec_utc\" type=\"x\" direction=\"in\"/>\n" \
44 " <arg name=\"relative\" type=\"b\" direction=\"in\"/>\n" \
45 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
47 " <method name=\"SetTimezone\">\n" \
48 " <arg name=\"timezone\" type=\"s\" direction=\"in\"/>\n" \
49 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
51 " <method name=\"SetLocalRTC\">\n" \
52 " <arg name=\"local_rtc\" type=\"b\" direction=\"in\"/>\n" \
53 " <arg name=\"correct_system\" type=\"b\" direction=\"in\"/>\n" \
54 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
57 BUS_PROPERTIES_INTERFACE \
58 BUS_INTROSPECTABLE_INTERFACE \
62 #define INTERFACES_LIST \
63 BUS_GENERIC_INTERFACES_LIST \
64 "org.freedesktop.locale1\0"
66 static char *zone = NULL;
67 static bool local_rtc = false;
69 static void free_data(void) {
76 static bool valid_timezone(const char *name) {
85 if (*name == '/' || *name == 0)
88 for (p = name; *p; p++) {
89 if (!(*p >= '0' && *p <= '9') &&
90 !(*p >= 'a' && *p <= 'z') &&
91 !(*p >= 'A' && *p <= 'Z') &&
92 !(*p == '-' || *p == '_' || *p == '+' || *p == '/'))
108 t = strappend("/usr/share/zoneinfo/", name);
118 if (!S_ISREG(st.st_mode))
124 static void verify_timezone(void) {
125 char *p, *a = NULL, *b = NULL;
132 p = strappend("/usr/share/zoneinfo/", zone);
134 log_error("Out of memory");
138 j = read_full_file("/etc/localtime", &a, &l);
139 k = read_full_file(p, &b, &q);
143 if (j < 0 || k < 0 || l != q || memcmp(a, b, l)) {
144 log_warning("/etc/localtime and /etc/timezone out of sync.");
153 static int read_data(void) {
158 r = read_one_line_file("/etc/timezone", &zone);
159 if (r < 0 && r != -ENOENT)
164 local_rtc = hwclock_is_localtime() > 0;
169 static int write_data_timezone(void) {
174 if (unlink("/etc/timezone") < 0 && errno != ENOENT)
177 if (unlink("/etc/localtime") < 0 && errno != ENOENT)
183 p = strappend("/usr/share/zoneinfo/", zone);
185 log_error("Out of memory");
189 r = symlink_or_copy_atomic(p, "/etc/localtime");
195 r = write_one_line_file_atomic("/etc/timezone", zone);
202 static int write_data_local_rtc(void) {
206 r = read_full_file("/etc/adjtime", &s, NULL);
214 w = strdup(NULL_ADJTIME_LOCAL);
227 p = strchr(p+1, '\n');
243 w = new(char, a + (local_rtc ? 5 : 3) + b + 1);
249 *(char*) mempcpy(stpcpy(mempcpy(w, s, a), local_rtc ? "LOCAL" : "UTC"), e, b) = 0;
251 if (streq(w, NULL_ADJTIME_UTC)) {
254 if (unlink("/etc/adjtime") < 0) {
263 r = write_one_line_file_atomic("/etc/adjtime", w);
269 static DBusHandlerResult timedate_message_handler(
270 DBusConnection *connection,
271 DBusMessage *message,
274 const BusProperty properties[] = {
275 { "org.freedesktop.timedate1", "Timezone", bus_property_append_string, "s", zone },
276 { "org.freedesktop.timedate1", "LocalRTC", bus_property_append_bool, "b", &local_rtc },
277 { NULL, NULL, NULL, NULL, NULL }
280 DBusMessage *reply = NULL, *changed = NULL;
287 dbus_error_init(&error);
289 if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetTimezone")) {
291 dbus_bool_t interactive;
293 if (!dbus_message_get_args(
296 DBUS_TYPE_STRING, &z,
297 DBUS_TYPE_BOOLEAN, &interactive,
299 return bus_send_error_reply(connection, message, &error, -EINVAL);
301 if (!valid_timezone(z))
302 return bus_send_error_reply(connection, message, NULL, -EINVAL);
304 if (!streq_ptr(z, zone)) {
307 r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-timezone", interactive, &error);
309 return bus_send_error_reply(connection, message, &error, r);
318 /* 1. Write new configuration file */
319 r = write_data_timezone();
321 log_error("Failed to set timezone: %s", strerror(-r));
322 return bus_send_error_reply(connection, message, NULL, r);
329 /* 2. Teach kernel new timezone */
330 hwclock_apply_localtime_delta();
332 /* 3. Sync RTC from system clock, with the new delta */
333 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
334 assert_se(tm = localtime(&ts.tv_sec));
335 hwclock_set_time(tm);
338 log_info("Changed timezone to '%s'.", zone);
340 changed = bus_properties_changed_new(
341 "/org/freedesktop/timedate1",
342 "org.freedesktop.timedate1",
348 } else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetLocalRTC")) {
350 dbus_bool_t correct_system;
351 dbus_bool_t interactive;
353 if (!dbus_message_get_args(
356 DBUS_TYPE_BOOLEAN, &lrtc,
357 DBUS_TYPE_BOOLEAN, &correct_system,
358 DBUS_TYPE_BOOLEAN, &interactive,
360 return bus_send_error_reply(connection, message, &error, -EINVAL);
362 if (lrtc != local_rtc) {
365 r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-local-rtc", interactive, &error);
367 return bus_send_error_reply(connection, message, &error, r);
371 /* 1. Write new configuration file */
372 r = write_data_local_rtc();
374 log_error("Failed to set RTC to local/UTC: %s", strerror(-r));
375 return bus_send_error_reply(connection, message, NULL, r);
378 /* 2. Teach kernel new timezone */
380 hwclock_apply_localtime_delta();
382 hwclock_reset_localtime_delta();
384 /* 3. Synchronize clocks */
385 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
387 if (correct_system) {
390 /* Sync system clock from RTC; first,
391 * initialize the timezone fields of
394 tm = *localtime(&ts.tv_sec);
396 tm = *gmtime(&ts.tv_sec);
398 /* Override the main fields of
399 * struct tm, but not the timezone
401 if (hwclock_get_time(&tm) >= 0) {
403 /* And set the system clock
406 ts.tv_sec = mktime(&tm);
408 ts.tv_sec = timegm(&tm);
410 clock_settime(CLOCK_REALTIME, &ts);
416 /* Sync RTC from system clock */
418 tm = localtime(&ts.tv_sec);
420 tm = gmtime(&ts.tv_sec);
422 hwclock_set_time(tm);
425 log_info("Changed local RTC setting to '%s'.", yes_no(local_rtc));
427 changed = bus_properties_changed_new(
428 "/org/freedesktop/timedate1",
429 "org.freedesktop.timedate1",
435 } else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetTime")) {
437 dbus_bool_t relative;
438 dbus_bool_t interactive;
440 if (!dbus_message_get_args(
443 DBUS_TYPE_INT64, &utc,
444 DBUS_TYPE_BOOLEAN, &relative,
445 DBUS_TYPE_BOOLEAN, &interactive,
447 return bus_send_error_reply(connection, message, &error, -EINVAL);
449 if (!relative && utc <= 0)
450 return bus_send_error_reply(connection, message, NULL, -EINVAL);
452 if (!relative || utc != 0) {
456 r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-time", interactive, &error);
458 return bus_send_error_reply(connection, message, &error, r);
461 timespec_store(&ts, now(CLOCK_REALTIME) + utc);
463 timespec_store(&ts, utc);
465 /* Set system clock */
466 if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
467 log_error("Failed to set local time: %m");
468 return bus_send_error_reply(connection, message, NULL, -errno);
471 /* Sync down to RTC */
473 tm = localtime(&ts.tv_sec);
475 tm = gmtime(&ts.tv_sec);
477 hwclock_set_time(tm);
479 log_info("Changed local time to %s", ctime(&ts.tv_sec));
483 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
485 if (!(reply = dbus_message_new_method_return(message)))
488 if (!dbus_connection_send(connection, reply, NULL))
491 dbus_message_unref(reply);
496 if (!dbus_connection_send(connection, changed, NULL))
499 dbus_message_unref(changed);
502 return DBUS_HANDLER_RESULT_HANDLED;
506 dbus_message_unref(reply);
509 dbus_message_unref(changed);
511 dbus_error_free(&error);
513 return DBUS_HANDLER_RESULT_NEED_MEMORY;
516 static int connect_bus(DBusConnection **_bus) {
517 static const DBusObjectPathVTable timedate_vtable = {
518 .message_function = timedate_message_handler
521 DBusConnection *bus = NULL;
526 dbus_error_init(&error);
528 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
530 log_error("Failed to get system D-Bus connection: %s", error.message);
535 if (!dbus_connection_register_object_path(bus, "/org/freedesktop/timedate1", &timedate_vtable, NULL)) {
536 log_error("Not enough memory");
541 if (dbus_bus_request_name(bus, "org.freedesktop.timedate1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) < 0) {
542 log_error("Failed to register name on bus: %s", error.message);
553 dbus_connection_close(bus);
554 dbus_connection_unref(bus);
556 dbus_error_free(&error);
561 int main(int argc, char *argv[]) {
563 DBusConnection *bus = NULL;
565 log_set_target(LOG_TARGET_AUTO);
566 log_parse_environment();
570 log_error("This program takes no arguments.");
579 log_error("Failed to read timezone data: %s", strerror(-r));
583 r = connect_bus(&bus);
587 while (dbus_connection_read_write_dispatch(bus, -1))
596 dbus_connection_flush(bus);
597 dbus_connection_close(bus);
598 dbus_connection_unref(bus);
601 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;