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);
175
log_warning("Failed to read /etc/timezone: %s", strerror(-r));
178
r = parse_env_file("/etc/sysconfig/clock", NEWLINE,
182
if (r < 0 && r != -ENOENT)
183
log_warning("Failed to read /etc/sysconfig/clock: %s", strerror(-r));
194
local_rtc = hwclock_is_localtime() > 0;
199
static int write_data_timezone(void) {
204
if (unlink("/etc/timezone") < 0 && errno != ENOENT)
207
if (unlink("/etc/localtime") < 0 && errno != ENOENT)
213
p = strappend("/usr/share/zoneinfo/", zone);
215
log_error("Out of memory");
219
r = symlink_or_copy_atomic(p, "/etc/localtime");
225
r = write_one_line_file_atomic("/etc/timezone", zone);
232
static int write_data_local_rtc(void) {
236
r = read_full_file("/etc/adjtime", &s, NULL);
244
w = strdup(NULL_ADJTIME_LOCAL);
257
p = strchr(p+1, '\n');
273
w = new(char, a + (local_rtc ? 5 : 3) + b + 1);
279
*(char*) mempcpy(stpcpy(mempcpy(w, s, a), local_rtc ? "LOCAL" : "UTC"), e, b) = 0;
281
if (streq(w, NULL_ADJTIME_UTC)) {
284
if (unlink("/etc/adjtime") < 0) {
293
r = write_one_line_file_atomic("/etc/adjtime", w);
299
static int read_ntp(DBusConnection *bus) {
300
DBusMessage *m = NULL, *reply = NULL;
301
const char *name = "ntpd.service", *s;
307
dbus_error_init(&error);
309
m = dbus_message_new_method_call(
310
"org.freedesktop.systemd1",
311
"/org/freedesktop/systemd1",
312
"org.freedesktop.systemd1.Manager",
316
log_error("Out of memory");
321
if (!dbus_message_append_args(m,
322
DBUS_TYPE_STRING, &name,
323
DBUS_TYPE_INVALID)) {
324
log_error("Could not append arguments to message.");
329
reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
331
log_error("Failed to issue method call: %s", bus_error_message(&error));
336
if (!dbus_message_get_args(reply, &error,
337
DBUS_TYPE_STRING, &s,
338
DBUS_TYPE_INVALID)) {
339
log_error("Failed to parse reply: %s", bus_error_message(&error));
345
streq(s, "enabled") ||
346
streq(s, "enabled-runtime");
351
dbus_message_unref(m);
354
dbus_message_unref(reply);
356
dbus_error_free(&error);
361
static int start_ntp(DBusConnection *bus, DBusError *error) {
362
DBusMessage *m = NULL, *reply = NULL;
363
const char *name = "ntpd.service", *mode = "replace";
369
m = dbus_message_new_method_call(
370
"org.freedesktop.systemd1",
371
"/org/freedesktop/systemd1",
372
"org.freedesktop.systemd1.Manager",
373
use_ntp ? "StartUnit" : "StopUnit");
375
log_error("Could not allocate message.");
380
if (!dbus_message_append_args(m,
381
DBUS_TYPE_STRING, &name,
382
DBUS_TYPE_STRING, &mode,
383
DBUS_TYPE_INVALID)) {
384
log_error("Could not append arguments to message.");
389
reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
391
log_error("Failed to issue method call: %s", bus_error_message(error));
400
dbus_message_unref(m);
403
dbus_message_unref(reply);
408
static int enable_ntp(DBusConnection *bus, DBusError *error) {
409
DBusMessage *m = NULL, *reply = NULL;
410
const char * const names[] = { "ntpd.service", NULL };
412
DBusMessageIter iter;
413
dbus_bool_t f = FALSE, t = TRUE;
418
m = dbus_message_new_method_call(
419
"org.freedesktop.systemd1",
420
"/org/freedesktop/systemd1",
421
"org.freedesktop.systemd1.Manager",
422
use_ntp ? "EnableUnitFiles" : "DisableUnitFiles");
425
log_error("Could not allocate message.");
430
dbus_message_iter_init_append(m, &iter);
432
r = bus_append_strv_iter(&iter, (char**) names);
434
log_error("Failed to append unit files.");
437
/* send runtime bool */
438
if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &f)) {
439
log_error("Failed to append runtime boolean.");
445
/* send force bool */
446
if (!dbus_message_iter_append_basic(&iter, DBUS_TYPE_BOOLEAN, &t)) {
447
log_error("Failed to append force boolean.");
453
reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
455
log_error("Failed to issue method call: %s", bus_error_message(error));
460
dbus_message_unref(m);
461
m = dbus_message_new_method_call(
462
"org.freedesktop.systemd1",
463
"/org/freedesktop/systemd1",
464
"org.freedesktop.systemd1.Manager",
467
log_error("Could not allocate message.");
472
dbus_message_unref(reply);
473
reply = dbus_connection_send_with_reply_and_block(bus, m, -1, error);
475
log_error("Failed to issue method call: %s", bus_error_message(error));
484
dbus_message_unref(m);
487
dbus_message_unref(reply);
492
static int property_append_ntp(DBusMessageIter *i, const char *property, void *data) {
500
if (!dbus_message_iter_append_basic(i, DBUS_TYPE_BOOLEAN, &db))
506
static DBusHandlerResult timedate_message_handler(
507
DBusConnection *connection,
508
DBusMessage *message,
511
const BusProperty properties[] = {
512
{ "org.freedesktop.timedate1", "Timezone", bus_property_append_string, "s", zone },
513
{ "org.freedesktop.timedate1", "LocalRTC", bus_property_append_bool, "b", &local_rtc },
514
{ "org.freedesktop.timedate1", "NTP", property_append_ntp, "b", NULL },
515
{ NULL, NULL, NULL, NULL, NULL }
518
DBusMessage *reply = NULL, *changed = NULL;
525
dbus_error_init(&error);
527
if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetTimezone")) {
529
dbus_bool_t interactive;
531
if (!dbus_message_get_args(
534
DBUS_TYPE_STRING, &z,
535
DBUS_TYPE_BOOLEAN, &interactive,
537
return bus_send_error_reply(connection, message, &error, -EINVAL);
539
if (!valid_timezone(z))
540
return bus_send_error_reply(connection, message, NULL, -EINVAL);
542
if (!streq_ptr(z, zone)) {
545
r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-timezone", interactive, &error);
547
return bus_send_error_reply(connection, message, &error, r);
556
/* 1. Write new configuration file */
557
r = write_data_timezone();
559
log_error("Failed to set timezone: %s", strerror(-r));
560
return bus_send_error_reply(connection, message, NULL, r);
567
/* 2. Teach kernel new timezone */
568
hwclock_apply_localtime_delta(NULL);
570
/* 3. Sync RTC from system clock, with the new delta */
571
assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
572
assert_se(tm = localtime(&ts.tv_sec));
573
hwclock_set_time(tm);
576
log_info("Changed timezone to '%s'.", zone);
578
changed = bus_properties_changed_new(
579
"/org/freedesktop/timedate1",
580
"org.freedesktop.timedate1",
586
} else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetLocalRTC")) {
588
dbus_bool_t fix_system;
589
dbus_bool_t interactive;
591
if (!dbus_message_get_args(
594
DBUS_TYPE_BOOLEAN, &lrtc,
595
DBUS_TYPE_BOOLEAN, &fix_system,
596
DBUS_TYPE_BOOLEAN, &interactive,
598
return bus_send_error_reply(connection, message, &error, -EINVAL);
600
if (lrtc != local_rtc) {
603
r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-local-rtc", interactive, &error);
605
return bus_send_error_reply(connection, message, &error, r);
609
/* 1. Write new configuration file */
610
r = write_data_local_rtc();
612
log_error("Failed to set RTC to local/UTC: %s", strerror(-r));
613
return bus_send_error_reply(connection, message, NULL, r);
616
/* 2. Teach kernel new timezone */
618
hwclock_apply_localtime_delta(NULL);
620
hwclock_reset_localtime_delta();
622
/* 3. Synchronize clocks */
623
assert_se(clock_gettime(CLOCK_REALTIME, &ts) == 0);
628
/* Sync system clock from RTC; first,
629
* initialize the timezone fields of
632
tm = *localtime(&ts.tv_sec);
634
tm = *gmtime(&ts.tv_sec);
636
/* Override the main fields of
637
* struct tm, but not the timezone
639
if (hwclock_get_time(&tm) >= 0) {
641
/* And set the system clock
644
ts.tv_sec = mktime(&tm);
646
ts.tv_sec = timegm(&tm);
648
clock_settime(CLOCK_REALTIME, &ts);
654
/* Sync RTC from system clock */
656
tm = localtime(&ts.tv_sec);
658
tm = gmtime(&ts.tv_sec);
660
hwclock_set_time(tm);
663
log_info("RTC configured to %s time.", local_rtc ? "local" : "UTC");
665
changed = bus_properties_changed_new(
666
"/org/freedesktop/timedate1",
667
"org.freedesktop.timedate1",
673
} else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetTime")) {
675
dbus_bool_t relative;
676
dbus_bool_t interactive;
678
if (!dbus_message_get_args(
681
DBUS_TYPE_INT64, &utc,
682
DBUS_TYPE_BOOLEAN, &relative,
683
DBUS_TYPE_BOOLEAN, &interactive,
685
return bus_send_error_reply(connection, message, &error, -EINVAL);
687
if (!relative && utc <= 0)
688
return bus_send_error_reply(connection, message, NULL, -EINVAL);
690
if (!relative || utc != 0) {
694
r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-time", interactive, &error);
696
return bus_send_error_reply(connection, message, &error, r);
699
timespec_store(&ts, now(CLOCK_REALTIME) + utc);
701
timespec_store(&ts, utc);
703
/* Set system clock */
704
if (clock_settime(CLOCK_REALTIME, &ts) < 0) {
705
log_error("Failed to set local time: %m");
706
return bus_send_error_reply(connection, message, NULL, -errno);
709
/* Sync down to RTC */
711
tm = localtime(&ts.tv_sec);
713
tm = gmtime(&ts.tv_sec);
715
hwclock_set_time(tm);
717
log_info("Changed local time to %s", ctime(&ts.tv_sec));
719
} else if (dbus_message_is_method_call(message, "org.freedesktop.timedate1", "SetNTP")) {
721
dbus_bool_t interactive;
723
if (!dbus_message_get_args(
726
DBUS_TYPE_BOOLEAN, &ntp,
727
DBUS_TYPE_BOOLEAN, &interactive,
729
return bus_send_error_reply(connection, message, &error, -EINVAL);
731
if (ntp != !!use_ntp) {
733
r = verify_polkit(connection, message, "org.freedesktop.timedate1.set-ntp", interactive, &error);
735
return bus_send_error_reply(connection, message, &error, r);
739
r = enable_ntp(connection, &error);
741
return bus_send_error_reply(connection, message, &error, r);
743
r = start_ntp(connection, &error);
745
return bus_send_error_reply(connection, message, &error, r);
747
log_info("Set NTP to %s", use_ntp ? "enabled" : "disabled");
749
changed = bus_properties_changed_new(
750
"/org/freedesktop/timedate1",
751
"org.freedesktop.timedate1",
758
return bus_default_message_handler(connection, message, INTROSPECTION, INTERFACES_LIST, properties);
760
if (!(reply = dbus_message_new_method_return(message)))
763
if (!dbus_connection_send(connection, reply, NULL))
766
dbus_message_unref(reply);
771
if (!dbus_connection_send(connection, changed, NULL))
774
dbus_message_unref(changed);
777
return DBUS_HANDLER_RESULT_HANDLED;
781
dbus_message_unref(reply);
784
dbus_message_unref(changed);
786
dbus_error_free(&error);
788
return DBUS_HANDLER_RESULT_NEED_MEMORY;
791
static int connect_bus(DBusConnection **_bus) {
792
static const DBusObjectPathVTable timedate_vtable = {
793
.message_function = timedate_message_handler
796
DBusConnection *bus = NULL;
801
dbus_error_init(&error);
803
bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
805
log_error("Failed to get system D-Bus connection: %s", bus_error_message(&error));
810
dbus_connection_set_exit_on_disconnect(bus, FALSE);
812
if (!dbus_connection_register_object_path(bus, "/org/freedesktop/timedate1", &timedate_vtable, NULL) ||
813
!dbus_connection_add_filter(bus, bus_exit_idle_filter, &remain_until, NULL)) {
814
log_error("Not enough memory");
819
r = dbus_bus_request_name(bus, "org.freedesktop.timedate1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error);
820
if (dbus_error_is_set(&error)) {
821
log_error("Failed to register name on bus: %s", bus_error_message(&error));
826
if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
827
log_error("Failed to acquire name.");
838
dbus_connection_close(bus);
839
dbus_connection_unref(bus);
841
dbus_error_free(&error);
846
int main(int argc, char *argv[]) {
848
DBusConnection *bus = NULL;
849
bool exiting = false;
851
log_set_target(LOG_TARGET_AUTO);
852
log_parse_environment();
857
if (argc == 2 && streq(argv[1], "--introspect")) {
858
fputs(DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE
860
fputs(timedate_interface, stdout);
861
fputs("</node>\n", stdout);
866
log_error("This program takes no arguments.");
873
log_error("Failed to read timezone data: %s", strerror(-r));
877
r = connect_bus(&bus);
883
log_error("Failed to determine whether NTP is enabled: %s", strerror(-r));
887
remain_until = now(CLOCK_MONOTONIC) + DEFAULT_EXIT_USEC;
890
if (!dbus_connection_read_write_dispatch(bus, exiting ? -1 : (int) (DEFAULT_EXIT_USEC/USEC_PER_MSEC)))
893
if (!exiting && remain_until < now(CLOCK_MONOTONIC)) {
895
bus_async_unregister_and_exit(bus, "org.freedesktop.hostname1");
905
dbus_connection_flush(bus);
906
dbus_connection_close(bus);
907
dbus_connection_unref(bus);
910
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;