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"
37 " <interface name=\"org.freedesktop.timedate1\">\n" \
38 " <property name=\"Timezone\" type=\"s\" access=\"read\"/>\n" \
39 " <property name=\"LocalRTC\" type=\"b\" access=\"read\"/>\n" \
40 " <method name=\"SetTime\">\n" \
41 " <arg name=\"usec_utc\" type=\"x\" direction=\"in\"/>\n" \
42 " <arg name=\"relative\" type=\"b\" direction=\"in\"/>\n" \
43 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
45 " <method name=\"SetTimezone\">\n" \
46 " <arg name=\"timezone\" type=\"s\" direction=\"in\"/>\n" \
47 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
49 " <method name=\"SetLocalRTC\">\n" \
50 " <arg name=\"local_rtc\" type=\"b\" direction=\"in\"/>\n" \
51 " <arg name=\"fix_system\" type=\"b\" direction=\"in\"/>\n" \
52 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
56 #define INTROSPECTION \
57 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
60 BUS_PROPERTIES_INTERFACE \
61 BUS_INTROSPECTABLE_INTERFACE \
65 #define INTERFACES_LIST \
66 BUS_GENERIC_INTERFACES_LIST \
67 "org.freedesktop.locale1\0"
69 const char timedate_interface[] _introspect_("timedate1") = INTERFACE;
71 static char *zone = NULL;
72 static bool local_rtc = false;
74 static void free_data(void) {
81 static bool valid_timezone(const char *name) {
90 if (*name == '/' || *name == 0)
93 for (p = name; *p; p++) {
94 if (!(*p >= '0' && *p <= '9') &&
95 !(*p >= 'a' && *p <= 'z') &&
96 !(*p >= 'A' && *p <= 'Z') &&
97 !(*p == '-' || *p == '_' || *p == '+' || *p == '/'))
113 t = strappend("/usr/share/zoneinfo/", name);
123 if (!S_ISREG(st.st_mode))
129 static void verify_timezone(void) {
130 char *p, *a = NULL, *b = NULL;
137 p = strappend("/usr/share/zoneinfo/", zone);
139 log_error("Out of memory");
143 j = read_full_file("/etc/localtime", &a, &l);
144 k = read_full_file(p, &b, &q);
148 if (j < 0 || k < 0 || l != q || memcmp(a, b, l)) {
149 log_warning("/etc/localtime and /etc/timezone out of sync.");
158 static int read_data(void) {
163 r = read_one_line_file("/etc/timezone", &zone);
164 if (r < 0 && r != -ENOENT)
169 local_rtc = hwclock_is_localtime() > 0;
174 static int write_data_timezone(void) {
179 if (unlink("/etc/timezone") < 0 && errno != ENOENT)
182 if (unlink("/etc/localtime") < 0 && errno != ENOENT)
188 p = strappend("/usr/share/zoneinfo/", zone);
190 log_error("Out of memory");
194 r = symlink_or_copy_atomic(p, "/etc/localtime");
200 r = write_one_line_file_atomic("/etc/timezone", zone);
207 static int write_data_local_rtc(void) {
211 r = read_full_file("/etc/adjtime", &s, NULL);
219 w = strdup(NULL_ADJTIME_LOCAL);
232 p = strchr(p+1, '\n');
248 w = new(char, a + (local_rtc ? 5 : 3) + b + 1);
254 *(char*) mempcpy(stpcpy(mempcpy(w, s, a), local_rtc ? "LOCAL" : "UTC"), e, b) = 0;
256 if (streq(w, NULL_ADJTIME_UTC)) {
259 if (unlink("/etc/adjtime") < 0) {
268 r = write_one_line_file_atomic("/etc/adjtime", w);
274 static DBusHandlerResult timedate_message_handler(
275 DBusConnection *connection,
276 DBusMessage *message,
279 const BusProperty properties[] = {
280 { "org.freedesktop.timedate1", "Timezone", bus_property_append_string, "s", zone },
281 { "org.freedesktop.timedate1", "LocalRTC", bus_property_append_bool, "b", &local_rtc },
282 { NULL, NULL, NULL, NULL, NULL }
285 DBusMessage *reply = NULL, *changed = NULL;
292 dbus_error_init(&error);
294 if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetTimezone")) {
296 dbus_bool_t interactive;
298 if (!dbus_message_get_args(
301 DBUS_TYPE_STRING, &z,
302 DBUS_TYPE_BOOLEAN, &interactive,
304 return bus_send_error_reply(connection, message, &error, -EINVAL);
306 if (!valid_timezone(z))
307 return bus_send_error_reply(connection, message, NULL, -EINVAL);
309 if (!streq_ptr(z, zone)) {
312 r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-timezone", interactive, &error);
314 return bus_send_error_reply(connection, message, &error, r);
323 /* 1. Write new configuration file */
324 r = write_data_timezone();
326 log_error("Failed to set timezone: %s", strerror(-r));
327 return bus_send_error_reply(connection, message, NULL, r);
334 /* 2. Teach kernel new timezone */
335 hwclock_apply_localtime_delta();
337 /* 3. Sync RTC from system clock, with the new delta */
338 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
339 assert_se(tm = localtime(&ts.tv_sec));
340 hwclock_set_time(tm);
343 log_info("Changed timezone to '%s'.", zone);
345 changed = bus_properties_changed_new(
346 "/org/freedesktop/timedate1",
347 "org.freedesktop.timedate1",
353 } else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetLocalRTC")) {
355 dbus_bool_t fix_system;
356 dbus_bool_t interactive;
358 if (!dbus_message_get_args(
361 DBUS_TYPE_BOOLEAN, &lrtc,
362 DBUS_TYPE_BOOLEAN, &fix_system,
363 DBUS_TYPE_BOOLEAN, &interactive,
365 return bus_send_error_reply(connection, message, &error, -EINVAL);
367 if (lrtc != local_rtc) {
370 r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-local-rtc", interactive, &error);
372 return bus_send_error_reply(connection, message, &error, r);
376 /* 1. Write new configuration file */
377 r = write_data_local_rtc();
379 log_error("Failed to set RTC to local/UTC: %s", strerror(-r));
380 return bus_send_error_reply(connection, message, NULL, r);
383 /* 2. Teach kernel new timezone */
385 hwclock_apply_localtime_delta();
387 hwclock_reset_localtime_delta();
389 /* 3. Synchronize clocks */
390 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
395 /* Sync system clock from RTC; first,
396 * initialize the timezone fields of
399 tm = *localtime(&ts.tv_sec);
401 tm = *gmtime(&ts.tv_sec);
403 /* Override the main fields of
404 * struct tm, but not the timezone
406 if (hwclock_get_time(&tm) >= 0) {
408 /* And set the system clock
411 ts.tv_sec = mktime(&tm);
413 ts.tv_sec = timegm(&tm);
415 clock_settime(CLOCK_REALTIME, &ts);
421 /* Sync RTC from system clock */
423 tm = localtime(&ts.tv_sec);
425 tm = gmtime(&ts.tv_sec);
427 hwclock_set_time(tm);
430 log_error("RTC configured to %s time.", local_rtc ? "local" : "UTC");
432 changed = bus_properties_changed_new(
433 "/org/freedesktop/timedate1",
434 "org.freedesktop.timedate1",
440 } else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetTime")) {
442 dbus_bool_t relative;
443 dbus_bool_t interactive;
445 if (!dbus_message_get_args(
448 DBUS_TYPE_INT64, &utc,
449 DBUS_TYPE_BOOLEAN, &relative,
450 DBUS_TYPE_BOOLEAN, &interactive,
452 return bus_send_error_reply(connection, message, &error, -EINVAL);
454 if (!relative && utc <= 0)
455 return bus_send_error_reply(connection, message, NULL, -EINVAL);
457 if (!relative || utc != 0) {
461 r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-time", interactive, &error);
463 return bus_send_error_reply(connection, message, &error, r);
466 timespec_store(&ts, now(CLOCK_REALTIME) + utc);
468 timespec_store(&ts, utc);
470 /* Set system clock */
471 if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
472 log_error("Failed to set local time: %m");
473 return bus_send_error_reply(connection, message, NULL, -errno);
476 /* Sync down to RTC */
478 tm = localtime(&ts.tv_sec);
480 tm = gmtime(&ts.tv_sec);
482 hwclock_set_time(tm);
484 log_info("Changed local time to %s", ctime(&ts.tv_sec));
488 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
490 if (!(reply = dbus_message_new_method_return(message)))
493 if (!dbus_connection_send(connection, reply, NULL))
496 dbus_message_unref(reply);
501 if (!dbus_connection_send(connection, changed, NULL))
504 dbus_message_unref(changed);
507 return DBUS_HANDLER_RESULT_HANDLED;
511 dbus_message_unref(reply);
514 dbus_message_unref(changed);
516 dbus_error_free(&error);
518 return DBUS_HANDLER_RESULT_NEED_MEMORY;
521 static int connect_bus(DBusConnection **_bus) {
522 static const DBusObjectPathVTable timedate_vtable = {
523 .message_function = timedate_message_handler
526 DBusConnection *bus = NULL;
531 dbus_error_init(&error);
533 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
535 log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error));
540 if (!dbus_connection_register_object_path(bus, "/org/freedesktop/timedate1", &timedate_vtable, NULL)) {
541 log_error("Not enough memory");
546 r = dbus_bus_request_name(bus, "org.freedesktop.timedate1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
547 if (dbus_error_is_set(&error)) {
548 log_error("Failed to register name on bus: %s", bus_error_message(&error));
553 if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
554 log_error("Failed to acquire name.");
565 dbus_connection_close(bus);
566 dbus_connection_unref(bus);
568 dbus_error_free(&error);
573 int main(int argc, char *argv[]) {
575 DBusConnection *bus = NULL;
577 log_set_target(LOG_TARGET_AUTO);
578 log_parse_environment();
581 if (argc == 2 && streq(argv[1], "--introspect")) {
582 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
584 fputs(timedate_interface, stdout);
585 fputs("</node>\n", stdout);
590 log_error("This program takes no arguments.");
599 log_error("Failed to read timezone data: %s", strerror(-r));
603 r = connect_bus(&bus);
607 while (dbus_connection_read_write_dispatch(bus, -1))
616 dbus_connection_flush(bus);
617 dbus_connection_close(bus);
618 dbus_connection_unref(bus);
621 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;