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"
36 #define INTROSPECTION \
37 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
39 " <interface name=\"org.freedesktop.timedate1\">\n" \
40 " <property name=\"Timezone\" type=\"s\" access=\"read\"/>\n" \
41 " <property name=\"LocalRTC\" 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=\"user_interaction\" type=\"b\" direction=\"in\"/>\n" \
56 BUS_PROPERTIES_INTERFACE \
57 BUS_INTROSPECTABLE_INTERFACE \
61 #define INTERFACES_LIST \
62 BUS_GENERIC_INTERFACES_LIST \
63 "org.freedesktop.locale1\0"
65 static char *zone = NULL;
66 static bool local_rtc = false;
68 static void free_data(void) {
75 static bool valid_timezone(const char *name) {
84 if (*name == '/' || *name == 0)
87 for (p = name; *p; p++) {
88 if (!(*p >= '0' && *p <= '9') &&
89 !(*p >= 'a' && *p <= 'z') &&
90 !(*p >= 'A' && *p <= 'Z') &&
91 !(*p == '-' || *p == '_' || *p == '+' || *p == '/'))
107 t = strappend("/usr/share/zoneinfo/", name);
117 if (!S_ISREG(st.st_mode))
123 static void verify_timezone(void) {
124 char *p, *a = NULL, *b = NULL;
131 p = strappend("/usr/share/zoneinfo/", zone);
133 log_error("Out of memory");
137 j = read_full_file("/etc/localtime", &a, &l);
138 k = read_full_file(p, &b, &q);
142 if (j < 0 || k < 0 || l != q || memcmp(a, b, l)) {
143 log_warning("/etc/localtime and /etc/timezone out of sync.");
152 static int read_data(void) {
158 r = read_one_line_file("/etc/timezone", &zone);
159 if (r < 0 && r != -ENOENT)
164 f = fopen("/etc/adjtime", "r");
169 b = fgets(line, sizeof(line), f) &&
170 fgets(line, sizeof(line), f) &&
171 fgets(line, sizeof(line), f);
179 local_rtc = streq(line, "LOCAL");
181 } else if (errno != ENOENT)
187 static int write_data_timezone(void) {
192 if (unlink("/etc/timezone") < 0 && errno != ENOENT)
195 if (unlink("/etc/localtime") < 0 && errno != ENOENT)
201 p = strappend("/usr/share/zoneinfo/", zone);
203 log_error("Out of memory");
207 r = symlink_or_copy_atomic(p, "/etc/localtime");
213 r = write_one_line_file_atomic("/etc/timezone", zone);
220 static int write_data_local_rtc(void) {
224 r = read_full_file("/etc/adjtime", &s, NULL);
232 w = strdup(NULL_ADJTIME_LOCAL);
245 p = strchr(p+1, '\n');
261 w = new(char, a + (local_rtc ? 5 : 3) + b + 1);
267 *(char*) mempcpy(stpcpy(mempcpy(w, s, a), local_rtc ? "LOCAL" : "UTC"), e, b) = 0;
269 if (streq(w, NULL_ADJTIME_UTC)) {
272 if (unlink("/etc/adjtime") < 0) {
281 r = write_one_line_file_atomic("/etc/adjtime", w);
287 static DBusHandlerResult timedate_message_handler(
288 DBusConnection *connection,
289 DBusMessage *message,
292 const BusProperty properties[] = {
293 { "org.freedesktop.timedate1", "Timezone", bus_property_append_string, "s", zone },
294 { "org.freedesktop.timedate1", "LocalRTC", bus_property_append_bool, "b", &local_rtc },
295 { NULL, NULL, NULL, NULL, NULL }
298 DBusMessage *reply = NULL, *changed = NULL;
305 dbus_error_init(&error);
307 if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetTimezone")) {
309 dbus_bool_t interactive;
311 if (!dbus_message_get_args(
314 DBUS_TYPE_STRING, &z,
315 DBUS_TYPE_BOOLEAN, &interactive,
317 return bus_send_error_reply(connection, message, &error, -EINVAL);
319 if (!valid_timezone(z))
320 return bus_send_error_reply(connection, message, NULL, -EINVAL);
322 if (!streq_ptr(z, zone)) {
325 r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-timezone", interactive, &error);
327 return bus_send_error_reply(connection, message, &error, r);
336 r = write_data_timezone();
338 log_error("Failed to set timezone: %s", strerror(-r));
339 return bus_send_error_reply(connection, message, NULL, r);
342 log_info("Changed timezone to '%s'.", zone);
344 changed = bus_properties_changed_new(
345 "/org/freedesktop/timedate1",
346 "org.freedesktop.timedate1",
352 } else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetLocalRTC")) {
354 dbus_bool_t interactive;
356 if (!dbus_message_get_args(
359 DBUS_TYPE_BOOLEAN, &lrtc,
360 DBUS_TYPE_BOOLEAN, &interactive,
362 return bus_send_error_reply(connection, message, &error, -EINVAL);
364 if (lrtc != local_rtc) {
365 r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-local-rtc", interactive, &error);
367 return bus_send_error_reply(connection, message, &error, r);
371 r = write_data_local_rtc();
373 log_error("Failed to set RTC to local/UTC: %s", strerror(-r));
374 return bus_send_error_reply(connection, message, NULL, r);
377 log_info("Changed local RTC setting to '%s'.", yes_no(local_rtc));
379 changed = bus_properties_changed_new(
380 "/org/freedesktop/timedate1",
381 "org.freedesktop.timedate1",
387 } else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetTime")) {
389 dbus_bool_t relative;
390 dbus_bool_t interactive;
392 if (!dbus_message_get_args(
395 DBUS_TYPE_INT64, &utc,
396 DBUS_TYPE_BOOLEAN, &relative,
397 DBUS_TYPE_BOOLEAN, &interactive,
399 return bus_send_error_reply(connection, message, &error, -EINVAL);
401 if (!relative && utc <= 0)
402 return bus_send_error_reply(connection, message, NULL, -EINVAL);
404 if (!relative || utc != 0) {
407 r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-time", interactive, &error);
409 return bus_send_error_reply(connection, message, &error, r);
412 timespec_store(&ts, now(CLOCK_REALTIME) + utc);
414 timespec_store(&ts, utc);
416 if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
417 log_error("Failed to set local time: %m");
418 return bus_send_error_reply(connection, message, NULL, -errno);
421 log_info("Changed local time to %s", ctime(&ts.tv_sec));
425 return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
427 if (!(reply = dbus_message_new_method_return(message)))
430 if (!dbus_connection_send(connection, reply, NULL))
433 dbus_message_unref(reply);
438 if (!dbus_connection_send(connection, changed, NULL))
441 dbus_message_unref(changed);
444 return DBUS_HANDLER_RESULT_HANDLED;
448 dbus_message_unref(reply);
451 dbus_message_unref(changed);
453 dbus_error_free(&error);
455 return DBUS_HANDLER_RESULT_NEED_MEMORY;
458 static int connect_bus(DBusConnection **_bus) {
459 static const DBusObjectPathVTable timedate_vtable = {
460 .message_function = timedate_message_handler
463 DBusConnection *bus = NULL;
468 dbus_error_init(&error);
470 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
472 log_error("Failed to get system D-Bus connection: %s", error.message);
477 if (!dbus_connection_register_object_path(bus, "/org/freedesktop/timedate1", &timedate_vtable, NULL)) {
478 log_error("Not enough memory");
483 if (dbus_bus_request_name(bus, "org.freedesktop.timedate1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) < 0) {
484 log_error("Failed to register name on bus: %s", error.message);
495 dbus_connection_close(bus);
496 dbus_connection_unref(bus);
498 dbus_error_free(&error);
503 int main(int argc, char *argv[]) {
505 DBusConnection *bus = NULL;
507 log_set_target(LOG_TARGET_AUTO);
508 log_parse_environment();
512 log_error("This program takes no arguments.");
521 log_error("Failed to read timezone data: %s", strerror(-r));
525 r = connect_bus(&bus);
529 while (dbus_connection_read_write_dispatch(bus, -1))
538 dbus_connection_flush(bus);
539 dbus_connection_close(bus);
540 dbus_connection_unref(bus);
543 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;