[systemd-devel] [PATCH] timesyncd: do not keep listening socket open forever

Sean Young sean at mess.org
Wed Nov 26 01:33:30 PST 2014


This also makes the source port less predicatable.
---
 src/timesync/timesyncd-manager.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/src/timesync/timesyncd-manager.c b/src/timesync/timesyncd-manager.c
index 3ae01eb..03cfb24 100644
--- a/src/timesync/timesyncd-manager.c
+++ b/src/timesync/timesyncd-manager.c
@@ -132,6 +132,7 @@ struct ntp_msg {
 
 static int manager_arm_timer(Manager *m, usec_t next);
 static int manager_clock_watch_setup(Manager *m);
+static int manager_listen_setup(Manager *m);
 
 static double ntp_ts_short_to_d(const struct ntp_ts_short *ts) {
         return be16toh(ts->sec) + (be16toh(ts->frac) / 65536.0);
@@ -184,6 +185,14 @@ static int manager_send_request(Manager *m) {
 
         m->event_timeout = sd_event_source_unref(m->event_timeout);
 
+        if (m->server_socket < 0) {
+                r = manager_listen_setup(m);
+                if (r < 0) {
+                        log_warning("Failed to setup connection socket: %s", strerror(-r));
+                        return r;
+                }
+        }
+
         /*
          * Set transmit timestamp, remember it; the server will send that back
          * as the origin timestamp and we have an indication that this is the
@@ -250,7 +259,6 @@ static int manager_arm_timer(Manager *m, usec_t next) {
         int r;
 
         assert(m);
-        assert(m->event_receive);
 
         if (next == 0) {
                 m->event_timer = sd_event_source_unref(m->event_timer);
@@ -610,6 +618,10 @@ static int manager_receive_response(sd_event_source *source, int fd, uint32_t re
         m->pending = false;
         m->retry_interval = 0;
 
+        /* Stop listening */
+        m->event_receive = sd_event_source_unref(m->event_receive);
+        m->server_socket = safe_close(m->server_socket);
+
         /* announce leap seconds */
         if (NTP_FIELD_LEAP(ntpmsg.field) & NTP_LEAP_PLUSSEC)
                 leap_sec = 1;
@@ -741,12 +753,6 @@ static int manager_begin(Manager *m) {
         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);
 
-        r = manager_listen_setup(m);
-        if (r < 0) {
-                log_warning("Failed to setup connection socket: %s", strerror(-r));
-                return r;
-        }
-
         r = manager_clock_watch_setup(m);
         if (r < 0)
                 return r;
-- 
2.1.3



More information about the systemd-devel mailing list