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 " <property name=\"NTP\" type=\"b\" access=\"read\"/>\n" \
41 " <method name=\"SetTime\">\n" \
42 " <arg name=\"usec_utc\" type=\"x\" direction=\"in\"/>\n" \
43 " <arg name=\"relative\" type=\"b\" direction=\"in\"/>\n" \
44 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
46 " <method name=\"SetTimezone\">\n" \
47 " <arg name=\"timezone\" type=\"s\" direction=\"in\"/>\n" \
48 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
50 " <method name=\"SetLocalRTC\">\n" \
51 " <arg name=\"local_rtc\" type=\"b\" direction=\"in\"/>\n" \
52 " <arg name=\"fix_system\" type=\"b\" direction=\"in\"/>\n" \
53 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
55 " <method name=\"SetNTP\">\n" \
56 " <arg name=\"use_ntp\" type=\"b\" direction=\"in\"/>\n" \
57 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
61 #define INTROSPECTION \
62 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
65 BUS_PROPERTIES_INTERFACE \
66 BUS_INTROSPECTABLE_INTERFACE \
70 #define INTERFACES_LIST \
71 BUS_GENERIC_INTERFACES_LIST \
72 "org.freedesktop.locale1\0"
74 const char timedate_interface[] _introspect_("timedate1") = INTERFACE;
76 static char *zone = NULL;
77 static bool local_rtc = false;
78 static int use_ntp = -1;
80 static void free_data(void) {
87 static bool valid_timezone(const char *name) {
96 if (*name == '/' || *name == 0)
99 for (p = name; *p; p++) {
100 if (!(*p >= '0' && *p <= '9') &&
101 !(*p >= 'a' && *p <= 'z') &&
102 !(*p >= 'A' && *p <= 'Z') &&
103 !(*p == '-' || *p == '_' || *p == '+' || *p == '/'))
119 t = strappend("/usr/share/zoneinfo/", name);
129 if (!S_ISREG(st.st_mode))
135 static void verify_timezone(void) {
136 char *p, *a = NULL, *b = NULL;
143 p = strappend("/usr/share/zoneinfo/", zone);
145 log_error("Out of memory");
149 j = read_full_file("/etc/localtime", &a, &l);
150 k = read_full_file(p, &b, &q);
154 if (j < 0 || k < 0 || l != q || memcmp(a, b, l)) {
155 log_warning("/etc/localtime and /etc/timezone out of sync.");
164 static int read_data(void) {
169 r = read_one_line_file("/etc/timezone", &zone);
170 if (r < 0 && r != -ENOENT)
175 local_rtc = hwclock_is_localtime() > 0;
180 static int write_data_timezone(void) {
185 if (unlink("/etc/timezone") < 0 && errno != ENOENT)
188 if (unlink("/etc/localtime") < 0 && errno != ENOENT)
194 p = strappend("/usr/share/zoneinfo/", zone);
196 log_error("Out of memory");
200 r = symlink_or_copy_atomic(p, "/etc/localtime");
206 r = write_one_line_file_atomic("/etc/timezone", zone);
213 static int write_data_local_rtc(void) {
217 r = read_full_file("/etc/adjtime", &s, NULL);
225 w = strdup(NULL_ADJTIME_LOCAL);
238 p = strchr(p+1, '\n');
254 w = new(char, a + (local_rtc ? 5 : 3) + b + 1);
260 *(char*) mempcpy(stpcpy(mempcpy(w, s, a), local_rtc ? "LOCAL" : "UTC"), e, b) = 0;
262 if (streq(w, NULL_ADJTIME_UTC)) {
265 if (unlink("/etc/adjtime") < 0) {
274 r = write_one_line_file_atomic("/etc/adjtime", w);
280 static int read_ntp(DBusConnection *bus) {
281 DBusMessage *m = NULL, *reply = NULL;
282 const char *name = "ntpd.service", *s;
288 dbus_error_init(&error);
290 m = dbus_message_new_method_call(
291 "org.freedesktop.systemd1",
292 "/org/freedesktop/systemd1",
293 "org.freedesktop.systemd1.Manager",
297 log_error("Out of memory");
302 if (!dbus_message_append_args(m,
303 DBUS_TYPE_STRING, &name,
304 DBUS_TYPE_INVALID)) {
305 log_error("Could not append arguments to message.");
310 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
312 log_error("Failed to issue method call: %s", bus_error_message(&error));
317 if (!dbus_message_get_args(reply, &error,
318 DBUS_TYPE_STRING, &s,
319 DBUS_TYPE_INVALID)) {
320 log_error("Failed to parse reply: %s", bus_error_message(&error));
326 streq(s, "enabled") ||
327 streq(s, "enabled-runtime");
332 dbus_message_unref(m);
335 dbus_message_unref(reply);
337 dbus_error_free(&error);
342 static int start_ntp(DBusConnection *bus, DBusError *error) {
343 DBusMessage *m = NULL, *reply = NULL;
344 const char *name = "ntpd.service", *mode = "replace";
350 m = dbus_message_new_method_call(
351 "org.freedesktop.systemd1",
352 "/org/freedesktop/systemd1",
353 "org.freedesktop.systemd1.Manager",
354 use_ntp ? "StartUnit" : "StopUnit");
356 log_error("Could not allocate message.");
361 if (!dbus_message_append_args(m,
362 DBUS_TYPE_STRING, &name,
363 DBUS_TYPE_STRING, &mode,
364 DBUS_TYPE_INVALID)) {
365 log_error("Could not append arguments to message.");
370 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
372 log_error("Failed to issue method call: %s", bus_error_message(error));
381 dbus_message_unref(m);
384 dbus_message_unref(reply);
389 static int enable_ntp(DBusConnection *bus, DBusError *error) {
390 DBusMessage *m = NULL, *reply = NULL;
391 const char * const names[] = { "ntpd.service", NULL };
393 DBusMessageIter iter;
394 dbus_bool_t f = FALSE, t = TRUE;
399 m = dbus_message_new_method_call(
400 "org.freedesktop.systemd1",
401 "/org/freedesktop/systemd1",
402 "org.freedesktop.systemd1.Manager",
403 use_ntp ? "EnableUnitFiles" : "DisableUnitFiles");
406 log_error("Could not allocate message.");
411 dbus_message_iter_init_append(m, &iter);
413 r = bus_append_strv_iter(&iter, (char**) names);
415 log_error("Failed to append unit files.");
418 /* send runtime bool */
419 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &f)) {
420 log_error("Failed to append runtime boolean.");
426 /* send force bool */
427 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &t)) {
428 log_error("Failed to append force boolean.");
434 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
436 log_error("Failed to issue method call: %s", bus_error_message(error));
441 dbus_message_unref(m);
442 m = dbus_message_new_method_call(
443 "org.freedesktop.systemd1",
444 "/org/freedesktop/systemd1",
445 "org.freedesktop.systemd1.Manager",
448 log_error("Could not allocate message.");
453 dbus_message_unref(reply);
454 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
456 log_error("Failed to issue method call: %s", bus_error_message(error));
465 dbus_message_unref(m);
468 dbus_message_unref(reply);
473 static int property_append_ntp(DBusMessageIter *i, const char *property, void *data) {
481 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
487 static DBusHandlerResult timedate_message_handler(
488 DBusConnection *connection,
489 DBusMessage *message,
492 const BusProperty properties[] = {
493 { "org.freedesktop.timedate1", "Timezone", bus_property_append_string, "s", zone },
494 { "org.freedesktop.timedate1", "LocalRTC", bus_property_append_bool, "b", &local_rtc },
495 { "org.freedesktop.timedate1", "NTP", property_append_ntp, "b", NULL },
496 { NULL, NULL, NULL, NULL, NULL }
499 DBusMessage *reply = NULL, *changed = NULL;
506 dbus_error_init(&error);
508 if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetTimezone")) {
510 dbus_bool_t interactive;
512 if (!dbus_message_get_args(
515 DBUS_TYPE_STRING, &z,
516 DBUS_TYPE_BOOLEAN, &interactive,
518 return bus_send_error_reply(connection, message, &error, -EINVAL);
520 if (!valid_timezone(z))
521 return bus_send_error_reply(connection, message, NULL, -EINVAL);
523 if (!streq_ptr(z, zone)) {
526 r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-timezone", interactive, &error);
528 return bus_send_error_reply(connection, message, &error, r);
537 /* 1. Write new configuration file */
538 r = write_data_timezone();
540 log_error("Failed to set timezone: %s", strerror(-r));
541 return bus_send_error_reply(connection, message, NULL, r);
548 /* 2. Teach kernel new timezone */
549 hwclock_apply_localtime_delta(NULL);
551 /* 3. Sync RTC from system clock, with the new delta */
552 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
553 assert_se(tm = localtime(&ts.tv_sec));
554 hwclock_set_time(tm);
557 log_info("Changed timezone to '%s'.", zone);
559 changed = bus_properties_changed_new(
560 "/org/freedesktop/timedate1",
561 "org.freedesktop.timedate1",
567 } else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetLocalRTC")) {
569 dbus_bool_t fix_system;
570 dbus_bool_t interactive;
572 if (!dbus_message_get_args(
575 DBUS_TYPE_BOOLEAN, &lrtc,
576 DBUS_TYPE_BOOLEAN, &fix_system,
577 DBUS_TYPE_BOOLEAN, &interactive,
579 return bus_send_error_reply(connection, message, &error, -EINVAL);
581 if (lrtc != local_rtc) {
584 r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-local-rtc", interactive, &error);
586 return bus_send_error_reply(connection, message, &error, r);
590 /* 1. Write new configuration file */
591 r = write_data_local_rtc();
593 log_error("Failed to set RTC to local/UTC: %s", strerror(-r));
594 return bus_send_error_reply(connection, message, NULL, r);
597 /* 2. Teach kernel new timezone */
599 hwclock_apply_localtime_delta(NULL);
601 hwclock_reset_localtime_delta();
603 /* 3. Synchronize clocks */
604 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
609 /* Sync system clock from RTC; first,
610 * initialize the timezone fields of
613 tm = *localtime(&ts.tv_sec);
615 tm = *gmtime(&ts.tv_sec);
617 /* Override the main fields of
618 * struct tm, but not the timezone
620 if (hwclock_get_time(&tm) >= 0) {
622 /* And set the system clock
625 ts.tv_sec = mktime(&tm);
627 ts.tv_sec = timegm(&tm);
629 clock_settime(CLOCK_REALTIME, &ts);
635 /* Sync RTC from system clock */
637 tm = localtime(&ts.tv_sec);
639 tm = gmtime(&ts.tv_sec);
641 hwclock_set_time(tm);
644 log_info("RTC configured to %s time.", local_rtc ? "local" : "UTC");
646 changed = bus_properties_changed_new(
647 "/org/freedesktop/timedate1",
648 "org.freedesktop.timedate1",
654 } else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetTime")) {
656 dbus_bool_t relative;
657 dbus_bool_t interactive;
659 if (!dbus_message_get_args(
662 DBUS_TYPE_INT64, &utc,
663 DBUS_TYPE_BOOLEAN, &relative,
664 DBUS_TYPE_BOOLEAN, &interactive,
666 return bus_send_error_reply(connection, message, &error, -EINVAL);
668 if (!relative && utc <= 0)
669 return bus_send_error_reply(connection, message, NULL, -EINVAL);
671 if (!relative || utc != 0) {
675 r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-time", interactive, &error);
677 return bus_send_error_reply(connection, message, &error, r);
680 timespec_store(&ts, now(CLOCK_REALTIME) + utc);
682 timespec_store(&ts, utc);
684 /* Set system clock */
685 if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
686 log_error("Failed to set local time: %m");
687 return bus_send_error_reply(connection, message, NULL, -errno);
690 /* Sync down to RTC */
692 tm = localtime(&ts.tv_sec);
694 tm = gmtime(&ts.tv_sec);
696 hwclock_set_time(tm);
698 log_info("Changed local time to %s", ctime(&ts.tv_sec));
700 } else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetNTP")) {
702 dbus_bool_t interactive;
704 if (!dbus_message_get_args(
707 DBUS_TYPE_BOOLEAN, &ntp,
708 DBUS_TYPE_BOOLEAN, &interactive,
710 return bus_send_error_reply(connection, message, &error, -EINVAL);
712 if (ntp != !!use_ntp) {
714 r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-ntp", interactive, &error);
716 return bus_send_error_reply(connection, message, &error, r);
720 r = enable_ntp(connection, &error);
722 return bus_send_error_reply(connection, message, &error, r);
724 r = start_ntp(connection, &error);
726 return bus_send_error_reply(connection, message, &error, r);
728 log_info("Set NTP to %s", use_ntp ? "enabled" : "disabled");
730 changed = bus_properties_changed_new(
731 "/org/freedesktop/timedate1",
732 "org.freedesktop.timedate1",
739 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
741 if (!(reply = dbus_message_new_method_return(message)))
744 if (!dbus_connection_send(connection, reply, NULL))
747 dbus_message_unref(reply);
752 if (!dbus_connection_send(connection, changed, NULL))
755 dbus_message_unref(changed);
758 return DBUS_HANDLER_RESULT_HANDLED;
762 dbus_message_unref(reply);
765 dbus_message_unref(changed);
767 dbus_error_free(&error);
769 return DBUS_HANDLER_RESULT_NEED_MEMORY;
772 static int connect_bus(DBusConnection **_bus) {
773 static const DBusObjectPathVTable timedate_vtable = {
774 .message_function = timedate_message_handler
777 DBusConnection *bus = NULL;
782 dbus_error_init(&error);
784 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
786 log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error));
791 if (!dbus_connection_register_object_path(bus, "/org/freedesktop/timedate1", &timedate_vtable, NULL)) {
792 log_error("Not enough memory");
797 r = dbus_bus_request_name(bus, "org.freedesktop.timedate1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
798 if (dbus_error_is_set(&error)) {
799 log_error("Failed to register name on bus: %s", bus_error_message(&error));
804 if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
805 log_error("Failed to acquire name.");
816 dbus_connection_close(bus);
817 dbus_connection_unref(bus);
819 dbus_error_free(&error);
824 int main(int argc, char *argv[]) {
826 DBusConnection *bus = NULL;
828 log_set_target(LOG_TARGET_AUTO);
829 log_parse_environment();
834 if (argc == 2 && streq(argv[1], "--introspect")) {
835 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
837 fputs(timedate_interface, stdout);
838 fputs("</node>\n", stdout);
843 log_error("This program takes no arguments.");
850 log_error("Failed to read timezone data: %s", strerror(-r));
854 r = connect_bus(&bus);
860 log_error("Failed to determine whether NTP is enabled: %s", strerror(-r));
864 while (dbus_connection_read_write_dispatch(bus, -1))
873 dbus_connection_flush(bus);
874 dbus_connection_close(bus);
875 dbus_connection_unref(bus);
878 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;