timesync: expose manager properties on bus

This commit is contained in:
Yu Watanabe 2018-04-30 23:02:09 +09:00
parent b296797f1c
commit e7dd394767
9 changed files with 217 additions and 0 deletions

View File

@ -4,6 +4,8 @@
systemd_timesyncd_sources = files('''
timesyncd.c
timesyncd-bus.c
timesyncd-bus.h
timesyncd-conf.c
timesyncd-conf.h
timesyncd-manager.c
@ -27,6 +29,10 @@ if conf.get('ENABLE_TIMESYNCD') == 1
configuration : substs)
install_data(timesyncd_conf,
install_dir : pkgsysconfdir)
install_data('org.freedesktop.timesync1.conf',
install_dir : dbuspolicydir)
install_data('org.freedesktop.timesync1.service',
install_dir : dbussystemservicedir)
endif
############################################################

View File

@ -0,0 +1,42 @@
<?xml version="1.0"?> <!--*-nxml-*-->
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<!--
This file is part of systemd.
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
-->
<busconfig>
<policy user="systemd-timesync">
<allow own="org.freedesktop.timesync1"/>
<allow send_destination="org.freedesktop.timesync1"/>
<allow receive_sender="org.freedesktop.timesync1"/>
</policy>
<policy context="default">
<deny send_destination="org.freedesktop.timesync1"/>
<allow send_destination="org.freedesktop.timesync1"
send_interface="org.freedesktop.DBus.Introspectable"/>
<allow send_destination="org.freedesktop.timesync1"
send_interface="org.freedesktop.DBus.Peer"/>
<allow send_destination="org.freedesktop.timesync1"
send_interface="org.freedesktop.DBus.Properties"
send_member="Get"/>
<allow send_destination="org.freedesktop.timesync1"
send_interface="org.freedesktop.DBus.Properties"
send_member="GetAll"/>
<allow receive_sender="org.freedesktop.timesync1"/>
</policy>
</busconfig>

View File

@ -0,0 +1,14 @@
# SPDX-License-Identifier: LGPL-2.1+
#
# This file is part of systemd.
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
[D-BUS Service]
Name=org.freedesktop.timesync1
Exec=/bin/false
User=root
SystemdService=dbus-org.freedesktop.timesync1.service

View File

@ -0,0 +1,137 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#include "sd-bus.h"
#include "alloc-util.h"
#include "bus-util.h"
#include "in-addr-util.h"
#include "log.h"
#include "macro.h"
#include "time-util.h"
#include "timesyncd-bus.h"
static int property_get_servers(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
ServerName *p, **s = userdata;
int r;
assert(s);
assert(bus);
assert(reply);
r = sd_bus_message_open_container(reply, 'a', "s");
if (r < 0)
return r;
LIST_FOREACH(names, p, *s) {
r = sd_bus_message_append(reply, "s", p->string);
if (r < 0)
return r;
}
return sd_bus_message_close_container(reply);
}
static int property_get_current_server_name(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
ServerName **s = userdata;
assert(s);
assert(bus);
assert(reply);
return sd_bus_message_append(reply, "s", *s ? (*s)->string : "");
}
static int property_get_current_server_address(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
ServerAddress *a;
int r;
assert(bus);
assert(reply);
assert(userdata);
a = *(ServerAddress **) userdata;
if (!a)
return sd_bus_message_append(reply, "(iay)", AF_UNSPEC, 0);
r = sd_bus_message_open_container(reply, 'r', "iay");
if (r < 0)
return r;
r = sd_bus_message_append(reply, "i", a->sockaddr.sa.sa_family);
if (r < 0)
return r;
r = sd_bus_message_append_array(reply, 'y', &a->sockaddr.in.sin_addr, FAMILY_ADDRESS_SIZE(a->sockaddr.sa.sa_family));
if (r < 0)
return r;
return sd_bus_message_close_container(reply);
}
static const sd_bus_vtable manager_vtable[] = {
SD_BUS_VTABLE_START(0),
SD_BUS_PROPERTY("LinkNTPServers", "as", property_get_servers, offsetof(Manager, link_servers), 0),
SD_BUS_PROPERTY("SystemNTPServers", "as", property_get_servers, offsetof(Manager, system_servers), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("FallbackNTPServers", "as", property_get_servers, offsetof(Manager, fallback_servers), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("ServerName", "s", property_get_current_server_name, offsetof(Manager, current_server_name), 0),
SD_BUS_PROPERTY("ServerAddress", "(iay)", property_get_current_server_address, offsetof(Manager, current_server_address), 0),
SD_BUS_PROPERTY("RootDistanceMaxUSec", "t", bus_property_get_usec, offsetof(Manager, max_root_distance_usec), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("PollIntervalMinUSec", "t", bus_property_get_usec, offsetof(Manager, poll_interval_min_usec), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("PollIntervalMaxUSec", "t", bus_property_get_usec, offsetof(Manager, poll_interval_max_usec), SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("PollIntervalUSec", "t", bus_property_get_usec, offsetof(Manager, poll_interval_usec), 0),
SD_BUS_VTABLE_END
};
int manager_connect_bus(Manager *m) {
int r;
assert(m);
if (m->bus)
return 0;
r = bus_open_system_watch_bind_with_description(&m->bus, "bus-api-timesync");
if (r < 0)
return log_error_errno(r, "Failed to connect to bus: %m");
r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/timesync1", "org.freedesktop.timesync1.Manager", manager_vtable, m);
if (r < 0)
return log_error_errno(r, "Failed to add manager object vtable: %m");
r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.timesync1", 0, NULL, NULL);
if (r < 0)
return log_error_errno(r, "Failed to request name: %m");
r = sd_bus_attach_event(m->bus, m->event, 0);
if (r < 0)
return log_error_errno(r, "Failed to attach bus to event loop: %m");
return 0;
}

View File

@ -0,0 +1,6 @@
/* SPDX-License-Identifier: LGPL-2.1+ */
#pragma once
#include "timesyncd-manager.h"
int manager_connect_bus(Manager *m);

View File

@ -986,6 +986,8 @@ void manager_free(Manager *m) {
sd_resolve_unref(m->resolve);
sd_event_unref(m->event);
sd_bus_unref(m->bus);
free(m);
}

View File

@ -7,6 +7,7 @@
Copyright 2014 Kay Sievers, Lennart Poettering
***/
#include "sd-bus.h"
#include "sd-event.h"
#include "sd-network.h"
#include "sd-resolve.h"
@ -27,6 +28,7 @@ typedef struct Manager Manager;
#define NTP_POLL_INTERVAL_MAX_USEC (2048 * USEC_PER_SEC)
struct Manager {
sd_bus *bus;
sd_event *event;
sd_resolve *resolve;

View File

@ -16,6 +16,7 @@
#include "network-util.h"
#include "process-util.h"
#include "signal-util.h"
#include "timesyncd-bus.h"
#include "timesyncd-conf.h"
#include "timesyncd-manager.h"
#include "user-util.h"
@ -133,6 +134,12 @@ int main(int argc, char *argv[]) {
goto finish;
}
r = manager_connect_bus(m);
if (r < 0) {
log_error_errno(r, "Could not connect to bus: %m");
goto finish;
}
if (clock_is_localtime(NULL) > 0) {
log_info("The system is configured to read the RTC time in the local time zone. "
"This mode cannot be fully supported. All system time to RTC updates are disabled.");

View File

@ -45,3 +45,4 @@ StateDirectory=systemd/timesync
[Install]
WantedBy=sysinit.target
Alias=dbus-org.freedesktop.timesync1.service