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"
34 #define NULL_ADJTIME_UTC "0.0 0 0\n0\nUTC\n"
35 #define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
38 " <interface name=\"org.freedesktop.timedate1\">\n" \
39 " <property name=\"Timezone\" type=\"s\" access=\"read\"/>\n" \
40 " <property name=\"LocalRTC\" type=\"b\" access=\"read\"/>\n" \
41 " <property name=\"NTP\" 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=\"fix_system\" type=\"b\" direction=\"in\"/>\n" \
54 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
56 " <method name=\"SetNTP\">\n" \
57 " <arg name=\"use_ntp\" type=\"b\" direction=\"in\"/>\n" \
58 " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
62 #define INTROSPECTION \
63 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
66 BUS_PROPERTIES_INTERFACE \
67 BUS_INTROSPECTABLE_INTERFACE \
71 #define INTERFACES_LIST \
72 BUS_GENERIC_INTERFACES_LIST \
73 "org.freedesktop.timedate1\0"
75 const char timedate_interface[] _introspect_("timedate1") = INTERFACE;
77 static char *zone = NULL;
78 static bool local_rtc = false;
79 static int use_ntp = -1;
81 static usec_t remain_until = 0;
83 static void free_data(void) {
90 static bool valid_timezone(const char *name) {
99 if (*name == '/' || *name == 0)
102 for (p = name; *p; p++) {
103 if (!(*p >= '0' && *p <= '9') &&
104 !(*p >= 'a' && *p <= 'z') &&
105 !(*p >= 'A' && *p <= 'Z') &&
106 !(*p == '-' || *p == '_' || *p == '+' || *p == '/'))
122 t = strappend("/usr/share/zoneinfo/", name);
132 if (!S_ISREG(st.st_mode))
138 static void verify_timezone(void) {
139 char *p, *a = NULL, *b = NULL;
146 p = strappend("/usr/share/zoneinfo/", zone);
148 log_error("Out of memory");
152 j = read_full_file("/etc/localtime", &a, &l);
153 k = read_full_file(p, &b, &q);
157 if (j < 0 || k < 0 || l != q || memcmp(a, b, l)) {
158 log_warning("/etc/localtime and /etc/timezone out of sync.");
167 static int read_data(void) {
172 r = read_one_line_file("/etc/timezone", &zone);
173 if (r < 0 && r != -ENOENT)
178 local_rtc = hwclock_is_localtime() > 0;
183 static int write_data_timezone(void) {
188 if (unlink("/etc/timezone") < 0 && errno != ENOENT)
191 if (unlink("/etc/localtime") < 0 && errno != ENOENT)
197 p = strappend("/usr/share/zoneinfo/", zone);
199 log_error("Out of memory");
203 r = symlink_or_copy_atomic(p, "/etc/localtime");
209 r = write_one_line_file_atomic("/etc/timezone", zone);
216 static int write_data_local_rtc(void) {
220 r = read_full_file("/etc/adjtime", &s, NULL);
228 w = strdup(NULL_ADJTIME_LOCAL);
241 p = strchr(p+1, '\n');
257 w = new(char, a + (local_rtc ? 5 : 3) + b + 1);
263 *(char*) mempcpy(stpcpy(mempcpy(w, s, a), local_rtc ? "LOCAL" : "UTC"), e, b) = 0;
265 if (streq(w, NULL_ADJTIME_UTC)) {
268 if (unlink("/etc/adjtime") < 0) {
277 r = write_one_line_file_atomic("/etc/adjtime", w);
283 static int read_ntp(DBusConnection *bus) {
284 DBusMessage *m = NULL, *reply = NULL;
285 const char *name = "ntpd.service", *s;
291 dbus_error_init(&error);
293 m = dbus_message_new_method_call(
294 "org.freedesktop.systemd1",
295 "/org/freedesktop/systemd1",
296 "org.freedesktop.systemd1.Manager",
300 log_error("Out of memory");
305 if (!dbus_message_append_args(m,
306 DBUS_TYPE_STRING, &name,
307 DBUS_TYPE_INVALID)) {
308 log_error("Could not append arguments to message.");
313 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
315 log_error("Failed to issue method call: %s", bus_error_message(&error));
320 if (!dbus_message_get_args(reply, &error,
321 DBUS_TYPE_STRING, &s,
322 DBUS_TYPE_INVALID)) {
323 log_error("Failed to parse reply: %s", bus_error_message(&error));
329 streq(s, "enabled") ||
330 streq(s, "enabled-runtime");
335 dbus_message_unref(m);
338 dbus_message_unref(reply);
340 dbus_error_free(&error);
345 static int start_ntp(DBusConnection *bus, DBusError *error) {
346 DBusMessage *m = NULL, *reply = NULL;
347 const char *name = "ntpd.service", *mode = "replace";
353 m = dbus_message_new_method_call(
354 "org.freedesktop.systemd1",
355 "/org/freedesktop/systemd1",
356 "org.freedesktop.systemd1.Manager",
357 use_ntp ? "StartUnit" : "StopUnit");
359 log_error("Could not allocate message.");
364 if (!dbus_message_append_args(m,
365 DBUS_TYPE_STRING, &name,
366 DBUS_TYPE_STRING, &mode,
367 DBUS_TYPE_INVALID)) {
368 log_error("Could not append arguments to message.");
373 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
375 log_error("Failed to issue method call: %s", bus_error_message(error));
384 dbus_message_unref(m);
387 dbus_message_unref(reply);
392 static int enable_ntp(DBusConnection *bus, DBusError *error) {
393 DBusMessage *m = NULL, *reply = NULL;
394 const char * const names[] = { "ntpd.service", NULL };
396 DBusMessageIter iter;
397 dbus_bool_t f = FALSE, t = TRUE;
402 m = dbus_message_new_method_call(
403 "org.freedesktop.systemd1",
404 "/org/freedesktop/systemd1",
405 "org.freedesktop.systemd1.Manager",
406 use_ntp ? "EnableUnitFiles" : "DisableUnitFiles");
409 log_error("Could not allocate message.");
414 dbus_message_iter_init_append(m, &iter);
416 r = bus_append_strv_iter(&iter, (char**) names);
418 log_error("Failed to append unit files.");
421 /* send runtime bool */
422 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &f)) {
423 log_error("Failed to append runtime boolean.");
429 /* send force bool */
430 if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &t)) {
431 log_error("Failed to append force boolean.");
437 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
439 log_error("Failed to issue method call: %s", bus_error_message(error));
444 dbus_message_unref(m);
445 m = dbus_message_new_method_call(
446 "org.freedesktop.systemd1",
447 "/org/freedesktop/systemd1",
448 "org.freedesktop.systemd1.Manager",
451 log_error("Could not allocate message.");
456 dbus_message_unref(reply);
457 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
459 log_error("Failed to issue method call: %s", bus_error_message(error));
468 dbus_message_unref(m);
471 dbus_message_unref(reply);
476 static int property_append_ntp(DBusMessageIter *i, const char *property, void *data) {
484 if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
490 static DBusHandlerResult timedate_message_handler(
491 DBusConnection *connection,
492 DBusMessage *message,
495 const BusProperty properties[] = {
496 { "org.freedesktop.timedate1", "Timezone", bus_property_append_string, "s", zone },
497 { "org.freedesktop.timedate1", "LocalRTC", bus_property_append_bool, "b", &local_rtc },
498 { "org.freedesktop.timedate1", "NTP", property_append_ntp, "b", NULL },
499 { NULL, NULL, NULL, NULL, NULL }
502 DBusMessage *reply = NULL, *changed = NULL;
509 dbus_error_init(&error);
511 if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetTimezone")) {
513 dbus_bool_t interactive;
515 if (!dbus_message_get_args(
518 DBUS_TYPE_STRING, &z,
519 DBUS_TYPE_BOOLEAN, &interactive,
521 return bus_send_error_reply(connection, message, &error, -EINVAL);
523 if (!valid_timezone(z))
524 return bus_send_error_reply(connection, message, NULL, -EINVAL);
526 if (!streq_ptr(z, zone)) {
529 r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-timezone", interactive, &error);
531 return bus_send_error_reply(connection, message, &error, r);
540 /* 1. Write new configuration file */
541 r = write_data_timezone();
543 log_error("Failed to set timezone: %s", strerror(-r));
544 return bus_send_error_reply(connection, message, NULL, r);
551 /* 2. Teach kernel new timezone */
552 hwclock_apply_localtime_delta(NULL);
554 /* 3. Sync RTC from system clock, with the new delta */
555 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
556 assert_se(tm = localtime(&ts.tv_sec));
557 hwclock_set_time(tm);
560 log_info("Changed timezone to '%s'.", zone);
562 changed = bus_properties_changed_new(
563 "/org/freedesktop/timedate1",
564 "org.freedesktop.timedate1",
570 } else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetLocalRTC")) {
572 dbus_bool_t fix_system;
573 dbus_bool_t interactive;
575 if (!dbus_message_get_args(
578 DBUS_TYPE_BOOLEAN, &lrtc,
579 DBUS_TYPE_BOOLEAN, &fix_system,
580 DBUS_TYPE_BOOLEAN, &interactive,
582 return bus_send_error_reply(connection, message, &error, -EINVAL);
584 if (lrtc != local_rtc) {
587 r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-local-rtc", interactive, &error);
589 return bus_send_error_reply(connection, message, &error, r);
593 /* 1. Write new configuration file */
594 r = write_data_local_rtc();
596 log_error("Failed to set RTC to local/UTC: %s", strerror(-r));
597 return bus_send_error_reply(connection, message, NULL, r);
600 /* 2. Teach kernel new timezone */
602 hwclock_apply_localtime_delta(NULL);
604 hwclock_reset_localtime_delta();
606 /* 3. Synchronize clocks */
607 assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
612 /* Sync system clock from RTC; first,
613 * initialize the timezone fields of
616 tm = *localtime(&ts.tv_sec);
618 tm = *gmtime(&ts.tv_sec);
620 /* Override the main fields of
621 * struct tm, but not the timezone
623 if (hwclock_get_time(&tm) >= 0) {
625 /* And set the system clock
628 ts.tv_sec = mktime(&tm);
630 ts.tv_sec = timegm(&tm);
632 clock_settime(CLOCK_REALTIME, &ts);
638 /* Sync RTC from system clock */
640 tm = localtime(&ts.tv_sec);
642 tm = gmtime(&ts.tv_sec);
644 hwclock_set_time(tm);
647 log_info("RTC configured to %s time.", local_rtc ? "local" : "UTC");
649 changed = bus_properties_changed_new(
650 "/org/freedesktop/timedate1",
651 "org.freedesktop.timedate1",
657 } else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetTime")) {
659 dbus_bool_t relative;
660 dbus_bool_t interactive;
662 if (!dbus_message_get_args(
665 DBUS_TYPE_INT64, &utc,
666 DBUS_TYPE_BOOLEAN, &relative,
667 DBUS_TYPE_BOOLEAN, &interactive,
669 return bus_send_error_reply(connection, message, &error, -EINVAL);
671 if (!relative && utc <= 0)
672 return bus_send_error_reply(connection, message, NULL, -EINVAL);
674 if (!relative || utc != 0) {
678 r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-time", interactive, &error);
680 return bus_send_error_reply(connection, message, &error, r);
683 timespec_store(&ts, now(CLOCK_REALTIME) + utc);
685 timespec_store(&ts, utc);
687 /* Set system clock */
688 if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
689 log_error("Failed to set local time: %m");
690 return bus_send_error_reply(connection, message, NULL, -errno);
693 /* Sync down to RTC */
695 tm = localtime(&ts.tv_sec);
697 tm = gmtime(&ts.tv_sec);
699 hwclock_set_time(tm);
701 log_info("Changed local time to %s", ctime(&ts.tv_sec));
703 } else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetNTP")) {
705 dbus_bool_t interactive;
707 if (!dbus_message_get_args(
710 DBUS_TYPE_BOOLEAN, &ntp,
711 DBUS_TYPE_BOOLEAN, &interactive,
713 return bus_send_error_reply(connection, message, &error, -EINVAL);
715 if (ntp != !!use_ntp) {
717 r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-ntp", interactive, &error);
719 return bus_send_error_reply(connection, message, &error, r);
723 r = enable_ntp(connection, &error);
725 return bus_send_error_reply(connection, message, &error, r);
727 r = start_ntp(connection, &error);
729 return bus_send_error_reply(connection, message, &error, r);
731 log_info("Set NTP to %s", use_ntp ? "enabled" : "disabled");
733 changed = bus_properties_changed_new(
734 "/org/freedesktop/timedate1",
735 "org.freedesktop.timedate1",
742 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
744 if (!(reply = dbus_message_new_method_return(message)))
747 if (!dbus_connection_send(connection, reply, NULL))
750 dbus_message_unref(reply);
755 if (!dbus_connection_send(connection, changed, NULL))
758 dbus_message_unref(changed);
761 return DBUS_HANDLER_RESULT_HANDLED;
765 dbus_message_unref(reply);
768 dbus_message_unref(changed);
770 dbus_error_free(&error);
772 return DBUS_HANDLER_RESULT_NEED_MEMORY;
775 static int connect_bus(DBusConnection **_bus) {
776 static const DBusObjectPathVTable timedate_vtable = {
777 .message_function = timedate_message_handler
780 DBusConnection *bus = NULL;
785 dbus_error_init(&error);
787 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
789 log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error));
794 dbus_connection_set_exit_on_disconnect(bus, FALSE);
796 if (!dbus_connection_register_object_path(bus, "/org/freedesktop/timedate1", &timedate_vtable, NULL) ||
797 !dbus_connection_add_filter(bus, bus_exit_idle_filter, &remain_until, NULL)) {
798 log_error("Not enough memory");
803 r = dbus_bus_request_name(bus, "org.freedesktop.timedate1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
804 if (dbus_error_is_set(&error)) {
805 log_error("Failed to register name on bus: %s", bus_error_message(&error));
810 if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
811 log_error("Failed to acquire name.");
822 dbus_connection_close(bus);
823 dbus_connection_unref(bus);
825 dbus_error_free(&error);
830 int main(int argc, char *argv[]) {
832 DBusConnection *bus = NULL;
833 bool exiting = false;
835 log_set_target(LOG_TARGET_AUTO);
836 log_parse_environment();
841 if (argc == 2 && streq(argv[1], "--introspect")) {
842 fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
844 fputs(timedate_interface, stdout);
845 fputs("</node>\n", stdout);
850 log_error("This program takes no arguments.");
857 log_error("Failed to read timezone data: %s", strerror(-r));
861 r = connect_bus(&bus);
867 log_error("Failed to determine whether NTP is enabled: %s", strerror(-r));
871 remain_until = now(CLOCK_MONOTONIC) + DEFAULT_EXIT_USEC;
874 if (!dbus_connection_read_write_dispatch(bus, exiting ? -1 : (int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC)))
877 if (!exiting && remain_until < now(CLOCK_MONOTONIC)) {
879 bus_async_unregister_and_exit(bus, "org.freedesktop.hostname1");
889 dbus_connection_flush(bus);
890 dbus_connection_close(bus);
891 dbus_connection_unref(bus);
894 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;