HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/timedate/timedated.c
Git blob: e9c0c56d3a3ad2d9474c7b8d763aaea6c88d276f
Lines 1001–1242 of 1,242
r = bus_match_signal_async(
bus,
&slot,
bus_systemd_mgr,
"JobRemoved",
match_job_removed, NULL, c);
if (r < 0)
return r;
}
if (enable)
LIST_FOREACH(units, u, c->units) {
bool enable_this_one = !selected;
if (!streq(u->load_state, "loaded"))
continue;
LOG_CONTEXT_PUSH_UNIT(u);
r = unit_enable_or_disable(u, bus, error, enable_this_one);
if (r < 0)
/* If enablement failed, don't start this unit. */
enable_this_one = false;
r = unit_start_or_stop(u, bus, error, enable_this_one);
if (r < 0)
log_warning_errno(r, "Failed to %s %sd NTP unit, ignoring: %m",
enable_this_one ? "start" : "stop",
enable_disable(enable_this_one));
if (enable_this_one)
selected = u;
}
else
LIST_FOREACH(units, u, c->units) {
if (!streq(u->load_state, "loaded"))
continue;
q = unit_enable_or_disable(u, bus, error, false);
if (q < 0)
r = q;
q = unit_start_or_stop(u, bus, error, false);
if (q < 0)
r = q;
}
if (r < 0)
return r;
if (enable && !selected)
return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No NTP service found to enable.");
if (slot)
c->slot_job_removed = TAKE_PTR(slot);
if (selected)
log_info("Set NTP to be enabled (%s).", selected->name);
else
log_info("Set NTP to be disabled.");
/* Asynchronous reply to m in match_job_removed() */
return set_ensure_consume(&c->set_ntp_calls, &bus_message_hash_ops, sd_bus_message_ref(m));
}
static int method_list_timezones(sd_bus_message *m, void *userdata, sd_bus_error *error) {
_cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
_cleanup_strv_free_ char **zones = NULL;
int r;
assert(m);
r = get_timezones(&zones);
if (r < 0)
return sd_bus_error_set_errnof(error, r, "Failed to read list of time zones: %m");
r = sd_bus_message_new_method_return(m, &reply);
if (r < 0)
return r;
r = sd_bus_message_append_strv(reply, zones);
if (r < 0)
return r;
return sd_bus_message_send(reply);
}
static const sd_bus_vtable timedate_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_PROPERTY("Timezone", "s", NULL, offsetof(Context, zone), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("LocalRTC", "b", bus_property_get_bool, offsetof(Context, local_rtc), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("CanNTP", "b", property_get_can_ntp, 0, 0),
SD_BUS_PROPERTY("NTP", "b", property_get_ntp, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_PROPERTY("NTPSynchronized", "b", property_get_ntp_sync, 0, 0),
SD_BUS_PROPERTY("TimeUSec", "t", property_get_time, 0, 0),
SD_BUS_PROPERTY("RTCTimeUSec", "t", property_get_rtc_time, 0, 0),
SD_BUS_METHOD_WITH_ARGS("SetTime",
SD_BUS_ARGS("x", usec_utc, "b", relative, "b", interactive),
SD_BUS_NO_RESULT,
method_set_time,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("SetTimezone",
SD_BUS_ARGS("s", timezone, "b", interactive),
SD_BUS_NO_RESULT,
method_set_timezone,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("SetLocalRTC",
SD_BUS_ARGS("b", local_rtc, "b", fix_system, "b", interactive),
SD_BUS_NO_RESULT,
method_set_local_rtc,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("SetNTP",
SD_BUS_ARGS("b", use_ntp, "b", interactive),
SD_BUS_NO_RESULT,
method_set_ntp,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_METHOD_WITH_ARGS("ListTimezones",
SD_BUS_NO_ARGS,
SD_BUS_RESULT("as", timezones),
method_list_timezones,
SD_BUS_VTABLE_UNPRIVILEGED),
SD_BUS_VTABLE_END,
};
static const BusObjectImplementation manager_object = {
"/org/freedesktop/timedate1",
"org.freedesktop.timedate1",
.vtables = BUS_VTABLES(timedate_vtable),
};
static int connect_bus(Context *c, sd_event *event, sd_bus **_bus) {
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
assert(c);
assert(event);
assert(_bus);
r = sd_bus_default_system(&bus);
if (r < 0)
return log_error_errno(r, "Failed to get system bus connection: %m");
r = bus_add_implementation(bus, &manager_object, c);
if (r < 0)
return r;
r = bus_log_control_api_register(bus);
if (r < 0)
return r;
r = sd_bus_request_name_async(bus, NULL, "org.freedesktop.timedate1", 0, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to request name: %m");
r = sd_bus_attach_event(bus, event, 0);
if (r < 0)
return log_error_errno(r, "Failed to attach bus to event loop: %m");
*_bus = TAKE_PTR(bus);
return 0;
}
static bool context_check_idle(void *userdata) {
Context *c = ASSERT_PTR(userdata);
return hashmap_isempty(c->polkit_registry);
}
COMMAND(
"systemd-timedated\0",
"Manage the system clock and timezone and NTP enablement.",
.man_pages = "systemd-timedated.service(8)\0",
.option_namespace = "service",
.option_groups =
"Options\0"
"Bus introspection\0",
);
static int run(int argc, char *argv[]) {
_cleanup_(context_clear) Context context = {};
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
int r;
LIBSELINUX_NOTE(recommended);
log_setup();
r = service_parse_argv(BUS_IMPLEMENTATIONS(&manager_object,
&log_control_object),
/* runtime_scope= */ NULL,
argc, argv);
if (r <= 0)
return r;
umask(0022);
r = sd_event_default(&event);
if (r < 0)
return log_error_errno(r, "Failed to allocate event loop: %m");
(void) sd_event_set_watchdog(event, true);
r = sd_event_set_signal_exit(event, true);
if (r < 0)
return log_error_errno(r, "Failed to install SIGINT/SIGTERM handlers: %m");
r = connect_bus(&context, event, &bus);
if (r < 0)
return r;
(void) sd_bus_negotiate_timestamp(bus, true);
r = context_read_data(&context);
if (r < 0)
return log_error_errno(r, "Failed to read time zone data: %m");
r = context_parse_ntp_services(&context);
if (r < 0)
return r;
r = sd_notify(false, NOTIFY_READY_MESSAGE);
if (r < 0)
log_warning_errno(r, "Failed to send readiness notification, ignoring: %m");
r = bus_event_loop_with_idle(
event,
bus,
"org.freedesktop.timedate1",
DEFAULT_EXIT_USEC,
context_check_idle,
&context);
if (r < 0)
return log_error_errno(r, "Failed to run event loop: %m");
return 0;
}
DEFINE_MAIN_FUNCTION(run);