1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
14 #include "sd-messages.h"
16 #include "alloc-util.h"
17 #include "bus-common-errors.h"
18 #include "bus-error.h"
20 #include "clock-util.h"
22 #include "fileio-label.h"
24 #include "path-util.h"
25 #include "selinux-util.h"
27 #include "user-util.h"
30 #define NULL_ADJTIME_UTC "0.0 0 0\n0\nUTC\n"
31 #define NULL_ADJTIME_LOCAL "0.0 0 0\n0\nLOCAL\n"
33 static BUS_ERROR_MAP_ELF_REGISTER
const sd_bus_error_map timedated_errors
[] = {
34 SD_BUS_ERROR_MAP("org.freedesktop.timedate1.NoNTPSupport", EOPNOTSUPP
),
38 typedef struct Context
{
43 Hashmap
*polkit_registry
;
46 static void context_free(Context
*c
) {
50 bus_verify_polkit_async_registry_free(c
->polkit_registry
);
53 static int context_read_data(Context
*c
) {
54 _cleanup_free_
char *t
= NULL
;
61 log_warning_errno(r
, "/etc/localtime should be a symbolic link to a time zone data file in /usr/share/zoneinfo/.");
63 log_warning_errno(r
, "Failed to get target of /etc/localtime: %m");
65 free_and_replace(c
->zone
, t
);
67 c
->local_rtc
= clock_is_localtime(NULL
) > 0;
72 static int context_write_data_timezone(Context
*c
) {
73 _cleanup_free_
char *p
= NULL
;
78 if (isempty(c
->zone
)) {
79 if (unlink("/etc/localtime") < 0 && errno
!= ENOENT
)
85 p
= strappend("../usr/share/zoneinfo/", c
->zone
);
89 r
= symlink_atomic(p
, "/etc/localtime");
96 static int context_write_data_local_rtc(Context
*c
) {
98 _cleanup_free_
char *s
= NULL
, *w
= NULL
;
102 r
= read_full_file("/etc/adjtime", &s
, NULL
);
110 w
= strdup(NULL_ADJTIME_LOCAL
);
115 const char *e
= "\n"; /* default if there is less than 3 lines */
116 const char *prepend
= "";
119 p
= strchrnul(s
, '\n');
121 /* only one line, no \n terminator */
123 else if (p
[1] == '\0') {
124 /* only one line, with \n terminator */
128 p
= strchr(p
+1, '\n');
130 /* only two lines, no \n terminator */
135 /* third line might have a \n terminator or not */
137 end
= strchr(p
, '\n');
138 /* if we actually have a fourth line, use that as suffix "e", otherwise the default \n */
147 w
= new(char, a
+ (c
->local_rtc
? 5 : 3) + strlen(prepend
) + b
+ 1);
151 *(char*) mempcpy(stpcpy(stpcpy(mempcpy(w
, s
, a
), prepend
), c
->local_rtc
? "LOCAL" : "UTC"), e
, b
) = 0;
153 if (streq(w
, NULL_ADJTIME_UTC
)) {
154 if (unlink("/etc/adjtime") < 0)
163 return write_string_file_atomic_label("/etc/adjtime", w
);
166 static int context_read_ntp(Context
*c
, sd_bus
*bus
) {
167 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
168 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
175 r
= sd_bus_call_method(
177 "org.freedesktop.systemd1",
178 "/org/freedesktop/systemd1",
179 "org.freedesktop.systemd1.Manager",
184 "systemd-timesyncd.service");
187 if (sd_bus_error_has_name(&error
, SD_BUS_ERROR_FILE_NOT_FOUND
) ||
188 sd_bus_error_has_name(&error
, "org.freedesktop.systemd1.LoadFailed") ||
189 sd_bus_error_has_name(&error
, "org.freedesktop.systemd1.NoSuchUnit"))
195 r
= sd_bus_message_read(reply
, "s", &s
);
200 c
->use_ntp
= STR_IN_SET(s
, "enabled", "enabled-runtime");
205 static int context_start_ntp(sd_bus
*bus
, sd_bus_error
*error
, bool enabled
) {
211 r
= sd_bus_call_method(
213 "org.freedesktop.systemd1",
214 "/org/freedesktop/systemd1",
215 "org.freedesktop.systemd1.Manager",
216 enabled
? "StartUnit" : "StopUnit",
220 "systemd-timesyncd.service",
223 if (sd_bus_error_has_name(error
, SD_BUS_ERROR_FILE_NOT_FOUND
) ||
224 sd_bus_error_has_name(error
, "org.freedesktop.systemd1.LoadFailed") ||
225 sd_bus_error_has_name(error
, "org.freedesktop.systemd1.NoSuchUnit"))
226 return sd_bus_error_set_const(error
, "org.freedesktop.timedate1.NoNTPSupport", "NTP not supported.");
234 static int context_enable_ntp(sd_bus
*bus
, sd_bus_error
*error
, bool enabled
) {
241 r
= sd_bus_call_method(
243 "org.freedesktop.systemd1",
244 "/org/freedesktop/systemd1",
245 "org.freedesktop.systemd1.Manager",
250 "systemd-timesyncd.service",
253 r
= sd_bus_call_method(
255 "org.freedesktop.systemd1",
256 "/org/freedesktop/systemd1",
257 "org.freedesktop.systemd1.Manager",
262 "systemd-timesyncd.service",
266 if (sd_bus_error_has_name(error
, SD_BUS_ERROR_FILE_NOT_FOUND
))
267 return sd_bus_error_set_const(error
, "org.freedesktop.timedate1.NoNTPSupport", "NTP not supported.");
272 r
= sd_bus_call_method(
274 "org.freedesktop.systemd1",
275 "/org/freedesktop/systemd1",
276 "org.freedesktop.systemd1.Manager",
287 static int property_get_rtc_time(
290 const char *interface
,
291 const char *property
,
292 sd_bus_message
*reply
,
294 sd_bus_error
*error
) {
301 r
= clock_get_hwclock(&tm
);
303 log_warning("/dev/rtc is busy. Is somebody keeping it open continuously? That's not a good idea... Returning a bogus RTC timestamp.");
305 } else if (r
== -ENOENT
) {
306 log_debug("/dev/rtc not found.");
307 t
= 0; /* no RTC found */
309 return sd_bus_error_set_errnof(error
, r
, "Failed to read RTC: %m");
311 t
= (usec_t
) timegm(&tm
) * USEC_PER_SEC
;
313 return sd_bus_message_append(reply
, "t", t
);
316 static int property_get_time(
319 const char *interface
,
320 const char *property
,
321 sd_bus_message
*reply
,
323 sd_bus_error
*error
) {
325 return sd_bus_message_append(reply
, "t", now(CLOCK_REALTIME
));
328 static int property_get_ntp_sync(
331 const char *interface
,
332 const char *property
,
333 sd_bus_message
*reply
,
335 sd_bus_error
*error
) {
337 return sd_bus_message_append(reply
, "b", ntp_synced());
340 static int method_set_timezone(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
341 Context
*c
= userdata
;
350 r
= sd_bus_message_read(m
, "sb", &z
, &interactive
);
354 if (!timezone_is_valid(z
))
355 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid time zone '%s'", z
);
357 if (streq_ptr(z
, c
->zone
))
358 return sd_bus_reply_method_return(m
, NULL
);
360 r
= bus_verify_polkit_async(
363 "org.freedesktop.timedate1.set-timezone",
372 return 1; /* No authorization for now, but the async polkit stuff will call us again when it has it */
381 /* 1. Write new configuration file */
382 r
= context_write_data_timezone(c
);
384 log_error_errno(r
, "Failed to set time zone: %m");
385 return sd_bus_error_set_errnof(error
, r
, "Failed to set time zone: %m");
388 /* 2. Tell the kernel our timezone */
389 clock_set_timezone(NULL
);
395 /* 3. Sync RTC from system clock, with the new delta */
396 assert_se(clock_gettime(CLOCK_REALTIME
, &ts
) == 0);
397 assert_se(tm
= localtime(&ts
.tv_sec
));
398 clock_set_hwclock(tm
);
402 "MESSAGE_ID=" SD_MESSAGE_TIMEZONE_CHANGE_STR
,
403 "TIMEZONE=%s", c
->zone
,
404 LOG_MESSAGE("Changed time zone to '%s'.", c
->zone
),
407 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
), "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "Timezone", NULL
);
409 return sd_bus_reply_method_return(m
, NULL
);
412 static int method_set_local_rtc(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
413 int lrtc
, fix_system
, interactive
;
414 Context
*c
= userdata
;
421 r
= sd_bus_message_read(m
, "bbb", &lrtc
, &fix_system
, &interactive
);
425 if (lrtc
== c
->local_rtc
)
426 return sd_bus_reply_method_return(m
, NULL
);
428 r
= bus_verify_polkit_async(
431 "org.freedesktop.timedate1.set-local-rtc",
444 /* 1. Write new configuration file */
445 r
= context_write_data_local_rtc(c
);
447 log_error_errno(r
, "Failed to set RTC to local/UTC: %m");
448 return sd_bus_error_set_errnof(error
, r
, "Failed to set RTC to local/UTC: %m");
451 /* 2. Tell the kernel our timezone */
452 clock_set_timezone(NULL
);
454 /* 3. Synchronize clocks */
455 assert_se(clock_gettime(CLOCK_REALTIME
, &ts
) == 0);
460 /* Sync system clock from RTC; first,
461 * initialize the timezone fields of
464 tm
= *localtime(&ts
.tv_sec
);
466 tm
= *gmtime(&ts
.tv_sec
);
468 /* Override the main fields of
469 * struct tm, but not the timezone
471 if (clock_get_hwclock(&tm
) >= 0) {
473 /* And set the system clock
476 ts
.tv_sec
= mktime(&tm
);
478 ts
.tv_sec
= timegm(&tm
);
480 clock_settime(CLOCK_REALTIME
, &ts
);
486 /* Sync RTC from system clock */
488 tm
= localtime(&ts
.tv_sec
);
490 tm
= gmtime(&ts
.tv_sec
);
492 clock_set_hwclock(tm
);
495 log_info("RTC configured to %s time.", c
->local_rtc
? "local" : "UTC");
497 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
), "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "LocalRTC", NULL
);
499 return sd_bus_reply_method_return(m
, NULL
);
502 static int method_set_time(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
503 int relative
, interactive
;
504 Context
*c
= userdata
;
515 return sd_bus_error_setf(error
, BUS_ERROR_AUTOMATIC_TIME_SYNC_ENABLED
, "Automatic time synchronization is enabled");
517 /* this only gets used if dbus does not provide a timestamp */
518 start
= now(CLOCK_MONOTONIC
);
520 r
= sd_bus_message_read(m
, "xbb", &utc
, &relative
, &interactive
);
524 if (!relative
&& utc
<= 0)
525 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Invalid absolute time");
527 if (relative
&& utc
== 0)
528 return sd_bus_reply_method_return(m
, NULL
);
533 n
= now(CLOCK_REALTIME
);
536 if ((utc
> 0 && x
< n
) ||
538 return sd_bus_error_setf(error
, SD_BUS_ERROR_INVALID_ARGS
, "Time value overflow");
540 timespec_store(&ts
, x
);
542 timespec_store(&ts
, (usec_t
) utc
);
544 r
= bus_verify_polkit_async(
547 "org.freedesktop.timedate1.set-time",
558 /* adjust ts for time spent in program */
559 r
= sd_bus_message_get_monotonic_usec(m
, &start
);
560 /* when sd_bus_message_get_monotonic_usec() returns -ENODATA it does not modify &start */
561 if (r
< 0 && r
!= -ENODATA
)
564 timespec_store(&ts
, timespec_load(&ts
) + (now(CLOCK_MONOTONIC
) - start
));
566 /* Set system clock */
567 if (clock_settime(CLOCK_REALTIME
, &ts
) < 0) {
568 log_error_errno(errno
, "Failed to set local time: %m");
569 return sd_bus_error_set_errnof(error
, errno
, "Failed to set local time: %m");
572 /* Sync down to RTC */
574 tm
= localtime(&ts
.tv_sec
);
576 tm
= gmtime(&ts
.tv_sec
);
577 clock_set_hwclock(tm
);
580 "MESSAGE_ID=" SD_MESSAGE_TIME_CHANGE_STR
,
581 "REALTIME="USEC_FMT
, timespec_load(&ts
),
582 LOG_MESSAGE("Changed local time to %s", ctime(&ts
.tv_sec
)),
585 return sd_bus_reply_method_return(m
, NULL
);
588 static int method_set_ntp(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
589 int enabled
, interactive
;
590 Context
*c
= userdata
;
596 r
= sd_bus_message_read(m
, "bb", &enabled
, &interactive
);
600 if ((bool)enabled
== c
->use_ntp
)
601 return sd_bus_reply_method_return(m
, NULL
);
603 r
= bus_verify_polkit_async(
606 "org.freedesktop.timedate1.set-ntp",
617 r
= context_enable_ntp(sd_bus_message_get_bus(m
), error
, enabled
);
621 r
= context_start_ntp(sd_bus_message_get_bus(m
), error
, enabled
);
625 c
->use_ntp
= enabled
;
626 log_info("Set NTP to %sd", enable_disable(enabled
));
628 (void) sd_bus_emit_properties_changed(sd_bus_message_get_bus(m
), "/org/freedesktop/timedate1", "org.freedesktop.timedate1", "NTP", NULL
);
630 return sd_bus_reply_method_return(m
, NULL
);
633 static const sd_bus_vtable timedate_vtable
[] = {
634 SD_BUS_VTABLE_START(0),
635 SD_BUS_PROPERTY("Timezone", "s", NULL
, offsetof(Context
, zone
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
636 SD_BUS_PROPERTY("LocalRTC", "b", bus_property_get_bool
, offsetof(Context
, local_rtc
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
637 SD_BUS_PROPERTY("CanNTP", "b", bus_property_get_bool
, offsetof(Context
, can_ntp
), 0),
638 SD_BUS_PROPERTY("NTP", "b", bus_property_get_bool
, offsetof(Context
, use_ntp
), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE
),
639 SD_BUS_PROPERTY("NTPSynchronized", "b", property_get_ntp_sync
, 0, 0),
640 SD_BUS_PROPERTY("TimeUSec", "t", property_get_time
, 0, 0),
641 SD_BUS_PROPERTY("RTCTimeUSec", "t", property_get_rtc_time
, 0, 0),
642 SD_BUS_METHOD("SetTime", "xbb", NULL
, method_set_time
, SD_BUS_VTABLE_UNPRIVILEGED
),
643 SD_BUS_METHOD("SetTimezone", "sb", NULL
, method_set_timezone
, SD_BUS_VTABLE_UNPRIVILEGED
),
644 SD_BUS_METHOD("SetLocalRTC", "bbb", NULL
, method_set_local_rtc
, SD_BUS_VTABLE_UNPRIVILEGED
),
645 SD_BUS_METHOD("SetNTP", "bb", NULL
, method_set_ntp
, SD_BUS_VTABLE_UNPRIVILEGED
),
649 static int connect_bus(Context
*c
, sd_event
*event
, sd_bus
**_bus
) {
650 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
657 r
= sd_bus_default_system(&bus
);
659 return log_error_errno(r
, "Failed to get system bus connection: %m");
661 r
= sd_bus_add_object_vtable(bus
, NULL
, "/org/freedesktop/timedate1", "org.freedesktop.timedate1", timedate_vtable
, c
);
663 return log_error_errno(r
, "Failed to register object: %m");
665 r
= sd_bus_request_name_async(bus
, NULL
, "org.freedesktop.timedate1", 0, NULL
, NULL
);
667 return log_error_errno(r
, "Failed to request name: %m");
669 r
= sd_bus_attach_event(bus
, event
, 0);
671 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
673 *_bus
= TAKE_PTR(bus
);
678 int main(int argc
, char *argv
[]) {
679 Context context
= {};
680 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
681 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
684 log_set_target(LOG_TARGET_AUTO
);
685 log_parse_environment();
691 log_error("This program takes no arguments.");
696 r
= sd_event_default(&event
);
698 log_error_errno(r
, "Failed to allocate event loop: %m");
702 sd_event_set_watchdog(event
, true);
704 r
= connect_bus(&context
, event
, &bus
);
708 (void) sd_bus_negotiate_timestamp(bus
, true);
710 r
= context_read_data(&context
);
712 log_error_errno(r
, "Failed to read time zone data: %m");
716 r
= context_read_ntp(&context
, bus
);
718 log_error_errno(r
, "Failed to determine whether NTP is enabled: %m");
722 r
= bus_event_loop_with_idle(event
, bus
, "org.freedesktop.timedate1", DEFAULT_EXIT_USEC
, NULL
, NULL
);
724 log_error_errno(r
, "Failed to run event loop: %m");
729 context_free(&context
);
731 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;