timedated.c revision 72edcff5db936e54cfc322d9392ec46e2428fd9b
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen This file is part of systemd.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen Copyright 2011 Lennart Poettering
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen systemd is free software; you can redistribute it and/or modify it
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen under the terms of the GNU Lesser General Public License as published by
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen the Free Software Foundation; either version 2.1 of the License, or
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen (at your option) any later version.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen systemd is distributed in the hope that it will be useful, but
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen Lesser General Public License for more details.
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen You should have received a copy of the GNU Lesser General Public License
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
39d8db043b599a7382f94bfc904d5e108af438bdLennart Poettering#define NULL_ADJTIME_UTC "0.0 0 0\n0\nUTC\n"
39d8db043b599a7382f94bfc904d5e108af438bdLennart Poettering#define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " <interface name=\"org.freedesktop.timedate1\">\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " <property name=\"Timezone\" type=\"s\" access=\"read\"/>\n" \
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen " <property name=\"LocalRTC\" type=\"b\" access=\"read\"/>\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " <property name=\"NTP\" type=\"b\" access=\"read\"/>\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " <method name=\"SetTime\">\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " <arg name=\"usec_utc\" type=\"x\" direction=\"in\"/>\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " <arg name=\"relative\" type=\"b\" direction=\"in\"/>\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " </method>\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " <method name=\"SetTimezone\">\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " <arg name=\"timezone\" type=\"s\" direction=\"in\"/>\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen " </method>\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " <method name=\"SetLocalRTC\">\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " <arg name=\"local_rtc\" type=\"b\" direction=\"in\"/>\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " <arg name=\"fix_system\" type=\"b\" direction=\"in\"/>\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " </method>\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " <method name=\"SetNTP\">\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " <arg name=\"use_ntp\" type=\"b\" direction=\"in\"/>\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " <arg name=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " </method>\n" \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering " </interface>\n"
a2a416f768e2aa7db5b975cd50eb19237cac9cceLennart Poettering DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
21d73c87b09ec2b8642424bc714ce9af3da4fc40Lennart Poetteringconst char timedate_interface[] _introspect_("timedate1") = INTERFACE;
21d73c87b09ec2b8642424bc714ce9af3da4fc40Lennart Poetteringtypedef struct TZ {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void free_data(void) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic bool valid_timezone(const char *name) {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt const char *p;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering for (p = name; *p; p++) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering !(*p == '-' || *p == '_' || *p == '+' || *p == '/'))
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen return false;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (*p == '/') {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering t = strappend("/usr/share/zoneinfo/", name);
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersenstatic int read_data(void) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = readlink_malloc("/etc/localtime", &t);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("/etc/localtime should be a symbolic link to a timezone data file in /usr/share/zoneinfo/.");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Failed to get target of /etc/localtime: %s", strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *e;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering e = path_startswith(t, "/usr/share/zoneinfo/");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering e = path_startswith(t, "../usr/share/zoneinfo/");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("/etc/localtime should be a symbolic link to a timezone data file in /usr/share/zoneinfo/.");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = parse_env_file("/etc/sysconfig/clock", NEWLINE,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (r < 0 && r != -ENOENT)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_warning("Failed to read /etc/sysconfig/clock: %s", strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = read_one_line_file("/etc/timezone", &tz.zone);
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen log_warning("Failed to read /etc/timezone: %s", strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering tz.local_rtc = hwclock_is_localtime() > 0;
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersenstatic int write_data_timezone(void) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (unlink("/etc/localtime") < 0 && errno != ENOENT)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (unlink("/etc/timezone") < 0 && errno != ENOENT)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering p = strappend("../usr/share/zoneinfo/", tz.zone);
1c4baffc1895809bae9ac36b670af90a4cb9cd7dTom Gundersen if (stat("/etc/timezone", &st) == 0 && S_ISREG(st.st_mode)) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = write_one_line_file_atomic("/etc/timezone", tz.zone);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering w = new(char, a + (tz.local_rtc ? 5 : 3) + b + 1);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering *(char*) mempcpy(stpcpy(mempcpy(w, s, a), tz.local_rtc ? "LOCAL" : "UTC"), e, b) = 0;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = write_one_line_file_atomic("/etc/adjtime", w);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic char** get_ntp_services(void) {
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering log_error("Failed to read NTP units file: %m");
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering if (l[0] == 0 || l[0] == '#')
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poetteringstatic int read_ntp(DBusConnection *bus) {
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering char **i, **l;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering const char *s;
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering "org.freedesktop.systemd1.Manager",
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering "GetUnitFileState");
c92e531c82a9815ec349aa1bf31236b86b2d5311Lennart Poettering reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen if (streq(error.name, "org.freedesktop.DBus.Error.FileNotFound")) {
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen /* This implementation does not exist, try next one */
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering log_error("Failed to issue method call: %s", bus_error_message(&error));
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen log_error("Failed to parse reply: %s", bus_error_message(&error));
eb60f9cd4e93ff5016dc1b5486fd1b7e1565fd92Lennart Poettering /* NTP is not installed. */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int start_ntp(DBusConnection *bus, DBusError *error) {
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen char **i, **l;
edc501d4674dadc304d45a7e1c5b69e207eb8cd4Lennart Poettering "org.freedesktop.systemd1.Manager",
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Could not allocate message.");
f0e1546763304aedc90e91d70dab9eeb7c966cf8Lennart Poettering log_error("Could not append arguments to message.");
cab5b05903096e1c9cf5575ccc73f89d15c8db69Lennart Poettering reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
f0e1546763304aedc90e91d70dab9eeb7c966cf8Lennart Poettering if (streq(error->name, "org.freedesktop.DBus.Error.FileNotFound") ||
f0e1546763304aedc90e91d70dab9eeb7c966cf8Lennart Poettering streq(error->name, "org.freedesktop.systemd1.LoadFailed") ||
096b6773886bd7a0c8c97aa684b0b67dfae58355Lennart Poettering streq(error->name, "org.freedesktop.systemd1.NoSuchUnit")) {
096b6773886bd7a0c8c97aa684b0b67dfae58355Lennart Poettering /* This implementation does not exist, try next one */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to issue method call: %s", bus_error_message(error));
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering /* No implementaiton available... */
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poetteringstatic int enable_ntp(DBusConnection *bus, DBusError *error) {
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering char **i, **l;
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering "org.freedesktop.systemd1.Manager",
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering tz.use_ntp ? "EnableUnitFiles" : "DisableUnitFiles");
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering log_error("Could not allocate message.");
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering log_error("Failed to append unit files.");
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering /* send runtime bool */
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &f)) {
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt log_error("Failed to append runtime boolean.");
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt /* send force bool */
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &t)) {
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering log_error("Failed to append force boolean.");
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering if (streq(error->name, "org.freedesktop.DBus.Error.FileNotFound")) {
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering /* This implementation does not exist, try next one */
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering log_error("Failed to issue method call: %s", bus_error_message(error));
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering "org.freedesktop.systemd1.Manager",
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering log_error("Could not allocate message.");
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poettering log_error("Failed to issue method call: %s", bus_error_message(error));
5cb36f41f01cf4b1f4395abfffd1b33116591e58Lennart Poetteringstatic int property_append_ntp(DBusMessageIter *i, const char *property, void *data) {
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poetteringstatic const BusProperty bus_timedate_properties[] = {
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt { "Timezone", bus_property_append_string, "s", offsetof(TZ, zone), true },
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen { "LocalRTC", bus_property_append_bool, "b", offsetof(TZ, local_rtc) },
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen { "NTP", property_append_ntp, "b", offsetof(TZ, use_ntp) },
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering { "org.freedesktop.timedate1", bus_timedate_properties, &tz },
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poetteringstatic DBusHandlerResult timedate_message_handler(
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetTimezone")) {
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen const char *z;
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen return bus_send_error_reply(connection, message, &error, -EINVAL);
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek return bus_send_error_reply(connection, message, NULL, -EINVAL);
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-timezone", interactive, NULL, &error);
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering return bus_send_error_reply(connection, message, &error, r);
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek /* 1. Write new configuration file */
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek log_error("Failed to set timezone: %s", strerror(-r));
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek return bus_send_error_reply(connection, message, NULL, r);
4713135eae4f7b6b670a98476fe413edfa1d9f41Zbigniew Jędrzejewski-Szmek /* 2. Tell the kernel our time zone */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* 3. Sync RTC from system clock, with the new delta */
4e945a6f7971fd7d1f6b2c62ee3afdaff3c95ce4Lennart Poettering assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(SD_MESSAGE_TIMEZONE_CHANGE),
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering "Timezone\0");
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering } else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetLocalRTC")) {
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return bus_send_error_reply(connection, message, &error, -EINVAL);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-local-rtc", interactive, NULL, &error);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering return bus_send_error_reply(connection, message, &error, r);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering /* 1. Write new configuration file */
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen log_error("Failed to set RTC to local/UTC: %s", strerror(-r));
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen return bus_send_error_reply(connection, message, NULL, r);
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen /* 2. Tell the kernel our time zone */
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen /* 3. Synchronize clocks */
bda2c408f8a739c19161818bcc842107f60652a2Tom Gundersen assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering /* Sync system clock from RTC; first,
822db23cfa98a9fbc48f41e11caafb6f1017e052Lennart Poettering * initialize the timezone fields of
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering * struct tm. */
87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3Lennart Poettering /* Override the main fields of
a5a807e63a50314e190e9166d8a453cd8dd258e3Zbigniew Jędrzejewski-Szmek * struct tm, but not the timezone
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen /* And set the system clock
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen * with this */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Sync RTC from system clock */
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering log_info("RTC configured to %s time.", tz.local_rtc ? "local" : "UTC");
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering "LocalRTC\0");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetTime")) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering return bus_send_error_reply(connection, message, &error, -EINVAL);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return bus_send_error_reply(connection, message, NULL, -EINVAL);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-time", interactive, NULL, &error);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering return bus_send_error_reply(connection, message, &error, r);
a38d99451f2bf8026ec51aee91662292e823c6a8Lennart Poettering timespec_store(&ts, now(CLOCK_REALTIME) + utc);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Set system clock */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen return bus_send_error_reply(connection, message, NULL, -errno);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen /* Sync down to RTC */
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(SD_MESSAGE_TIME_CHANGE),
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering "REALTIME=%llu", (unsigned long long) timespec_load(&ts),
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering "MESSAGE=Changed local time to %s", ctime(&ts.tv_sec),
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering } else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetNTP")) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return bus_send_error_reply(connection, message, &error, -EINVAL);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-ntp", interactive, NULL, &error);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return bus_send_error_reply(connection, message, &error, r);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering return bus_send_error_reply(connection, message, &error, r);
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering return bus_send_error_reply(connection, message, &error, r);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_info("Set NTP to %s", tz.use_ntp ? "enabled" : "disabled");
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, bps);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (!(reply = dbus_message_new_method_return(message)))
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (!dbus_connection_send(connection, reply, NULL))
a5f035960006556beab51c42e6948985635e261aLennart Poettering if (!dbus_connection_send(connection, changed, NULL))
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poetteringstatic int connect_bus(DBusConnection **_bus) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering static const DBusObjectPathVTable timedate_vtable = {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .message_function = timedate_message_handler
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering dbus_connection_set_exit_on_disconnect(bus, FALSE);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen if (!dbus_connection_register_object_path(bus, "/org/freedesktop/timedate1", &timedate_vtable, NULL) ||
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen !dbus_connection_add_filter(bus, bus_exit_idle_filter, &remain_until, NULL)) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = dbus_bus_request_name(bus, "org.freedesktop.timedate1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
091a364c802e34a58f3260c9cb5db9b75c62215cTom Gundersen log_error("Failed to register name on bus: %s", bus_error_message(&error));
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering if (argc == 2 && streq(argv[1], "--introspect")) {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering log_error("This program takes no arguments.");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to read timezone data: %s", strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Failed to determine whether NTP is enabled: %s", strerror(-r));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering remain_until = now(CLOCK_MONOTONIC) + DEFAULT_EXIT_USEC;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (!dbus_connection_read_write_dispatch(bus, exiting ? -1 : (int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC)))
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering if (!exiting && remain_until < now(CLOCK_MONOTONIC)) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering bus_async_unregister_and_exit(bus, "org.freedesktop.hostname1");