assert(m->current_server_name);
assert(m->current_server_address);
- sockaddr_pretty(&m->current_server_address->sockaddr.sa, m->current_server_address->socklen, true, &pretty);
+ server_address_pretty(m->current_server_address, &pretty);
log_info("Timed out waiting for reply from %s (%s).", strna(pretty), m->current_server_name->string);
return manager_connect(m);
ntpmsg.trans_time.sec = htobe32(m->trans_time.tv_sec + OFFSET_1900_1970);
ntpmsg.trans_time.frac = htobe32(m->trans_time.tv_nsec);
- sockaddr_pretty(&m->current_server_address->sockaddr.sa, m->current_server_address->socklen, true, &pretty);
+ server_address_pretty(m->current_server_address, &pretty);
len = sendto(m->server_socket, &ntpmsg, sizeof(ntpmsg), MSG_DONTWAIT, &m->current_server_address->sockaddr.sa, m->current_server_address->socklen);
if (len == sizeof(ntpmsg)) {
m->poll_interval_usec = NTP_POLL_INTERVAL_MIN_SEC * USEC_PER_SEC;
- sockaddr_pretty(&m->current_server_address->sockaddr.sa, m->current_server_address->socklen, true, &pretty);
+ server_address_pretty(m->current_server_address, &pretty);
log_info("Using NTP server %s (%s).", strna(pretty), m->current_server_name->string);
sd_notifyf(false, "STATUS=Using Time Server %s (%s).", strna(pretty), m->current_server_name->string);
m->resolve_query = sd_resolve_query_unref(m->resolve_query);
if (ret != 0) {
- log_info("Failed to resolve %s: %s", m->current_server_name->string, gai_strerror(ret));
+ log_debug("Failed to resolve %s: %s", m->current_server_name->string, gai_strerror(ret));
/* Try next host */
return manager_connect(m);
_cleanup_manager_free_ Manager *m = NULL;
int r;
+ assert(ret);
+
m = new0(Manager, 1);
if (!m)
return -ENOMEM;
if (r < 0)
return r;
- r = manager_clock_watch_setup(m);
- if (r < 0)
- return r;
-
*ret = m;
m = NULL;
}
static int manager_parse_config_file(Manager *m) {
- static const char fn[] = "/etc/systemd/timesyncd.conf";
- _cleanup_fclose_ FILE *f = NULL;
- int r;
-
- assert(m);
-
- f = fopen(fn, "re");
- if (!f) {
- if (errno == ENOENT)
- return 0;
-
- log_warning("Failed to open configuration file %s: %m", fn);
- return -errno;
- }
-
- r = config_parse(NULL, fn, f, "Time\0", config_item_perf_lookup,
- (void*) timesyncd_gperf_lookup, false, false, m);
- if (r < 0)
- log_warning("Failed to parse configuration file: %s", strerror(-r));
-
- return r;
+ return config_parse(NULL, "/etc/systemd/timesyncd.conf", NULL,
+ "Time\0",
+ config_item_perf_lookup, timesyncd_gperf_lookup,
+ false, false, true, m);
}
static bool network_is_online(void) {
connected = (m->server_socket != -1);
if (connected && !online) {
- log_info("No network connectivity. Suspending.");
+ log_info("No network connectivity, watching for changes.");
manager_disconnect(m);
} else if (!connected && online) {
- log_info("Network connectivity detected. Resuming.");
+ log_info("Network configuration changed, trying to establish connection.");
if (m->current_server_address) {
r = manager_begin(m);
if (r < 0)
_cleanup_network_monitor_unref_ sd_network_monitor *monitor = NULL;
int r, fd, events;
- r = sd_network_monitor_new(NULL, &monitor);
+ r = sd_network_monitor_new(&monitor, NULL);
if (r < 0)
return r;
return 0;
}
-static int drop_privileges(uid_t uid, gid_t gid) {
-
- static const cap_value_t bits[] = {
- CAP_SYS_TIME,
- };
-
- _cleanup_cap_free_ cap_t d = NULL;
- int r;
-
- /* Unfortunately we cannot leave privilege dropping to PID 1
- * here, since we want to run as user but want to keep te
- * CAP_SYS_TIME capability. Since file capabilities have been
- * introduced this cannot be done across exec() anymore,
- * unless our binary has the capability configured in the file
- * system, which we want to avoid. */
-
- if (setresgid(gid, gid, gid) < 0) {
- log_error("Failed change group ID: %m");
- return -errno;
- }
-
- if (setgroups(0, NULL) < 0) {
- log_error("Failed to drop auxiliary groups list: %m");
- return -errno;
- }
-
- if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
- log_error("Failed to enable keep capabilities flag: %m");
- return -errno;
- }
-
- r = setresuid(uid, uid, uid);
- if (r < 0) {
- log_error("Failed change user ID: %m");
- return -errno;
- }
-
- if (prctl(PR_SET_KEEPCAPS, 0) < 0) {
- log_error("Failed to disable keep capabilities flag: %m");
- return -errno;
- }
-
- r = capability_bounding_set_drop(~(1ULL << CAP_SYS_TIME), true);
- if (r < 0) {
- log_error("Failed to drop capabilities: %s", strerror(-r));
- return r;
- }
-
- d = cap_init();
- if (!d)
- return log_oom();
-
- if (cap_set_flag(d, CAP_EFFECTIVE, ELEMENTSOF(bits), bits, CAP_SET) < 0 ||
- cap_set_flag(d, CAP_PERMITTED, ELEMENTSOF(bits), bits, CAP_SET) < 0) {
- log_error("Failed to enable capabilities bits: %m");
- return -errno;
- }
-
- if (cap_set_proc(d) < 0) {
- log_error("Failed to increase capabilities: %m");
- return -errno;
- }
-
- return 0;
-}
-
int main(int argc, char *argv[]) {
const char *user = "systemd-timesync";
_cleanup_manager_free_ Manager *m = NULL;
if (r < 0)
goto out;
- r = drop_privileges(uid, gid);
+ r = drop_privileges(uid, gid, (1ULL << CAP_SYS_TIME));
if (r < 0)
goto out;