]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
ntp: close client socket when offline
authorMiroslav Lichvar <mlichvar@redhat.com>
Thu, 10 Apr 2014 15:59:08 +0000 (17:59 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 10 Apr 2014 16:02:29 +0000 (18:02 +0200)
ntp_core.c

index 8933c496ffff3edd48219e4d6400a7ceb51b6962..4cb176f92d80e32671cdd153b680c68e82bef007 100644 (file)
@@ -263,6 +263,10 @@ start_initial_timeout(NCR_Instance inst)
 
   /* Mark source active */
   SRC_SetActive(inst->source);
+
+  /* Open client socket */
+  if (inst->mode == MODE_CLIENT)
+    inst->local_addr.sock_fd = NIO_GetClientSocket(&inst->remote_addr);
 }
 
 /* ================================================== */
@@ -281,6 +285,10 @@ take_offline(NCR_Instance inst)
 
   /* And inactive */
   SRC_UnsetActive(inst->source);
+
+  /* Close client socket */
+  if (inst->mode == MODE_CLIENT)
+    NIO_CloseClientSocket(inst->local_addr.sock_fd);
 }
 
 /* ================================================== */
@@ -297,7 +305,7 @@ NCR_GetInstance(NTP_Remote_Address *remote_addr, NTP_Source_Type type, SourcePar
 
   switch (type) {
     case NTP_SERVER:
-      result->local_addr.sock_fd = NIO_GetClientSocket(remote_addr);
+      /* Client socket will be obtained when timer is started */
       result->mode = MODE_CLIENT;
       break;
     case NTP_PEER:
@@ -384,20 +392,14 @@ NCR_GetInstance(NTP_Remote_Address *remote_addr, NTP_Source_Type type, SourcePar
 void
 NCR_DestroyInstance(NCR_Instance instance)
 {
+  if (instance->opmode != MD_OFFLINE)
+    take_offline(instance);
+
   /* This will destroy the source instance inside the
      structure, which will cause reselection if this was the
      synchronising source etc. */
   SRC_DestroyInstance(instance->source);
 
-  /* Cancel any pending timeouts */
-  if (instance->timer_running) {
-    SCH_RemoveTimeout(instance->timeout_id);
-    instance->timer_running = 0;
-  }
-
-  if (instance->mode == MODE_CLIENT)
-    NIO_CloseClientSocket(instance->local_addr.sock_fd);
-
   /* Free the data structure */
   Free(instance);
 }