timedated.c revision 89f134406af6a4b4c7493f624a89dcd654b48e81
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek This file is part of systemd.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Copyright 2011 Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek under the terms of the GNU General Public License as published by
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2 of the License, or
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (at your option) any later version.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek General Public License for more details.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU General Public License
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek#define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " <interface name=\"org.freedesktop.timedate1\">\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " <property name=\"Timezone\" type=\"s\" access=\"read\"/>\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " <property name=\"LocalRTC\" type=\"b\" access=\"read\"/>\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " <property name=\"NTP\" type=\"b\" access=\"read\"/>\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " <method name=\"SetTime\">\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " <arg name=\"usec_utc\" type=\"x\" direction=\"in\"/>\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " <arg name=\"relative\" type=\"b\" direction=\"in\"/>\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " </method>\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " <method name=\"SetTimezone\">\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " <arg name=\"timezone\" type=\"s\" direction=\"in\"/>\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " </method>\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " <method name=\"SetLocalRTC\">\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " <arg name=\"local_rtc\" type=\"b\" direction=\"in\"/>\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " <arg name=\"fix_system\" type=\"b\" direction=\"in\"/>\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " </method>\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " <method name=\"SetNTP\">\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " <arg name=\"use_ntp\" type=\"b\" direction=\"in\"/>\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " </method>\n" \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek " </interface>\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekconst char timedate_interface[] _introspect_("timedate1") = INTERFACE;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void free_data(void) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic bool valid_timezone(const char *name) {
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer bool slash = false;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek for (p = name; *p; p++) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!(*p >= '0' && *p <= '9') &&
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek !(*p == '-' || *p == '_' || *p == '+' || *p == '/'))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (*p == '/') {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek t = strappend("/usr/share/zoneinfo/", name);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void verify_timezone(void) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p = strappend("/usr/share/zoneinfo/", tz.zone);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek j = read_full_file("/etc/localtime", &a, &l);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek if (j < 0 || k < 0 || l != q || memcmp(a, b, l)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning("/etc/localtime and /etc/timezone out of sync.");
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmekstatic int read_data(void) {
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek r = read_one_line_file("/etc/timezone", &tz.zone);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning("Failed to read /etc/timezone: %s", strerror(-r));
a24c64f03f9c5c0304451d8542fee853187a5168Lennart Poettering r = parse_env_file("/etc/sysconfig/clock", NEWLINE,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r < 0 && r != -ENOENT)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_warning("Failed to read /etc/sysconfig/clock: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek tz.local_rtc = hwclock_is_localtime() > 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int write_data_timezone(void) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (unlink("/etc/timezone") < 0 && errno != ENOENT)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (unlink("/etc/localtime") < 0 && errno != ENOENT)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p = strappend("/usr/share/zoneinfo/", tz.zone);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = symlink_or_copy_atomic(p, "/etc/localtime");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = write_one_line_file_atomic("/etc/timezone", tz.zone);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int write_data_local_rtc(void) {
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) r = read_full_file("/etc/adjtime", &s, NULL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek w = new(char, a + (tz.local_rtc ? 5 : 3) + b + 1);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *(char*) mempcpy(stpcpy(mempcpy(w, s, a), tz.local_rtc ? "LOCAL" : "UTC"), e, b) = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (streq(w, NULL_ADJTIME_UTC)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (unlink("/etc/adjtime") < 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = write_one_line_file_atomic("/etc/adjtime", w);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int read_ntp(DBusConnection *bus) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek DBusMessage *m = NULL, *reply = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char *name = "ntpd.service", *s;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m = dbus_message_new_method_call(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "org.freedesktop.systemd1.Manager",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "GetUnitFileState");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!dbus_message_append_args(m,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Could not append arguments to message.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to issue method call: %s", bus_error_message(&error));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!dbus_message_get_args(reply, &error,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to parse reply: %s", bus_error_message(&error));
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchukstatic int start_ntp(DBusConnection *bus, DBusError *error) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk const char *name = "ntpd.service", *mode = "replace";
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk "org.freedesktop.systemd1.Manager",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Could not allocate message.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!dbus_message_append_args(m,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Could not append arguments to message.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to issue method call: %s", bus_error_message(error));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int enable_ntp(DBusConnection *bus, DBusError *error) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek DBusMessage *m = NULL, *reply = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek const char * const names[] = { "ntpd.service", NULL };
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dbus_bool_t f = FALSE, t = TRUE;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m = dbus_message_new_method_call(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "org.freedesktop.systemd1.Manager",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek tz.use_ntp ? "EnableUnitFiles" : "DisableUnitFiles");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Could not allocate message.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dbus_message_iter_init_append(m, &iter);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = bus_append_strv_iter(&iter, (char**) names);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to append unit files.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* send runtime bool */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &f)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to append runtime boolean.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* send force bool */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &t)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to append force boolean.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to issue method call: %s", bus_error_message(error));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek m = dbus_message_new_method_call(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "org.freedesktop.systemd1.Manager",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Could not allocate message.");
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmek log_error("Failed to issue method call: %s", bus_error_message(error));
e40ec7aec5e64cd0cfa5fc556d6a9747229b5794Zbigniew Jędrzejewski-Szmekstatic int property_append_ntp(DBusMessageIter *i, const char *property, void *data) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekstatic const BusProperty bus_timedate_properties[] = {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek { "Timezone", bus_property_append_string, "s", offsetof(TZ, zone), true },
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek { "LocalRTC", bus_property_append_bool, "b", offsetof(TZ, local_rtc) },
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering { "NTP", property_append_ntp, "b", offsetof(TZ, use_ntp) },
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek { "org.freedesktop.timedate1", bus_timedate_properties, &tz },
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmekstatic DBusHandlerResult timedate_message_handler(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek DBusMessage *reply = NULL, *changed = NULL;
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetTimezone")) {
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering const char *z;
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek DBUS_TYPE_BOOLEAN, &interactive,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return bus_send_error_reply(connection, message, &error, -EINVAL);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return bus_send_error_reply(connection, message, NULL, -EINVAL);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-timezone", interactive, NULL, &error);
c2457105d76e3daf159f554a9bafb9751b23d756Holger Hans Peter Freyther return bus_send_error_reply(connection, message, &error, r);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* 1. Write new configuration file */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to set timezone: %s", strerror(-r));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return bus_send_error_reply(connection, message, NULL, r);
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden /* 2. Teach kernel new timezone */
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden /* 3. Sync RTC from system clock, with the new delta */
3a83211689bdf4ab617a4fb79e11980c50918123Shawn Landden assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("Changed timezone to '%s'.", tz.zone);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek changed = bus_properties_changed_new(
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering "Timezone\0");
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering } else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetLocalRTC")) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek DBUS_TYPE_BOOLEAN, &interactive,
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return bus_send_error_reply(connection, message, &error, -EINVAL);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-local-rtc", interactive, NULL, &error);
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering return bus_send_error_reply(connection, message, &error, r);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* 1. Write new configuration file */
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering log_error("Failed to set RTC to local/UTC: %s", strerror(-r));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return bus_send_error_reply(connection, message, NULL, r);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering /* 2. Teach kernel new timezone */
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek /* 3. Synchronize clocks */
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
19cace379f3f680d3201cd257ab3ca6708b2d45dLennart Poettering /* Sync system clock from RTC; first,
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering * initialize the timezone fields of
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering * struct tm. */
ae018d9bc900d6355dea4af05119b49c67945184Lennart Poettering /* Override the main fields of
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek * struct tm, but not the timezone
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (hwclock_get_time(&tm) >= 0) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* And set the system clock
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek clock_settime(CLOCK_REALTIME, &ts);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek /* Sync RTC from system clock */
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_info("RTC configured to %s time.", tz.local_rtc ? "local" : "UTC");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek changed = bus_properties_changed_new(
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek } else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetTime")) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek DBUS_TYPE_BOOLEAN, &interactive,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return bus_send_error_reply(connection, message, &error, -EINVAL);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return bus_send_error_reply(connection, message, NULL, -EINVAL);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-time", interactive, NULL, &error);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return bus_send_error_reply(connection, message, &error, r);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek timespec_store(&ts, now(CLOCK_REALTIME) + utc);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek /* Set system clock */
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_error("Failed to set local time: %m");
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return bus_send_error_reply(connection, message, NULL, -errno);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek /* Sync down to RTC */
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek log_info("Changed local time to %s", ctime(&ts.tv_sec));
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek } else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetNTP")) {
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek DBUS_TYPE_BOOLEAN, &interactive,
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return bus_send_error_reply(connection, message, &error, -EINVAL);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-ntp", interactive, NULL, &error);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return bus_send_error_reply(connection, message, &error, r);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = enable_ntp(connection, &error);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return bus_send_error_reply(connection, message, &error, r);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = start_ntp(connection, &error);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek return bus_send_error_reply(connection, message, &error, r);
adb435bb70815461eeddf44dd5d6f1fc2ad9026dLennart Poettering log_info("Set NTP to %s", tz.use_ntp ? "enabled" : "disabled");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek changed = bus_properties_changed_new(
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, bps);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!(reply = dbus_message_new_method_return(message)))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!dbus_connection_send(connection, reply, NULL))
82499507b369fea3033a74c22813bf423301aef4Lennart Poettering if (!dbus_connection_send(connection, changed, NULL))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek return DBUS_HANDLER_RESULT_NEED_MEMORY;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic int connect_bus(DBusConnection **_bus) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek static const DBusObjectPathVTable timedate_vtable = {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek .message_function = timedate_message_handler
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek dbus_connection_set_exit_on_disconnect(bus, FALSE);
968f319679d9069af037240d0c3bcd126181cdacZbigniew Jędrzejewski-Szmek if (!dbus_connection_register_object_path(bus, "/org/freedesktop/timedate1", &timedate_vtable, NULL) ||
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek !dbus_connection_add_filter(bus, bus_exit_idle_filter, &remain_until, NULL)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = dbus_bus_request_name(bus, "org.freedesktop.timedate1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (dbus_error_is_set(&error)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to register name on bus: %s", bus_error_message(&error));
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek log_error("Failed to acquire name.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_set_target(LOG_TARGET_AUTO);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (argc == 2 && streq(argv[1], "--introspect")) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fputs(timedate_interface, stdout);
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek log_error("This program takes no arguments.");
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to read timezone data: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_error("Failed to determine whether NTP is enabled: %s", strerror(-r));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek remain_until = now(CLOCK_MONOTONIC) + DEFAULT_EXIT_USEC;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!dbus_connection_read_write_dispatch(bus, exiting ? -1 : (int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC)))
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (!exiting && remain_until < now(CLOCK_MONOTONIC)) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek bus_async_unregister_and_exit(bus, "org.freedesktop.hostname1");