]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
Free allocated memory on exit
authorMiroslav Lichvar <mlichvar@redhat.com>
Tue, 23 Sep 2014 11:35:38 +0000 (13:35 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 25 Sep 2014 08:57:55 +0000 (10:57 +0200)
This should reduce the number of possible memory leaks reported by
valgrind. The remaining reported leaks are sched tqe allocation, async
DNS instance allocation, cmdmon response/timestamp cell allocation, and
clientlog subnet allocation.

13 files changed:
clientlog.c
conf.c
conf.h
hash.h
hash_intmd5.c
hash_nss.c
hash_tomcrypt.c
keys.c
local.c
main.c
ntp_sources.c
refclock.c
sources.c

index 84900b4fc60829a600517830278047c93d90989e..b895c590ff85cd8b31c3e9be79376e8b5956be09 100644 (file)
@@ -174,6 +174,11 @@ CLG_Initialise(void)
 void
 CLG_Finalise(void)
 {
+  int i;
+  
+  for (i = 0; i < n_nodes; i++)
+    Free(nodes[i]);
+  Free(nodes);
 }
 
 /* ================================================== */
diff --git a/conf.c b/conf.c
index 240b56b82d8ce9c3e4534008199a565006e981c3..e0311ed2449c4727c04754ef7da781175ba0ba54 100644 (file)
--- a/conf.c
+++ b/conf.c
@@ -78,7 +78,7 @@ static void parse_tempcomp(char *);
 
 static int restarted = 0;
 static int generate_command_key = 0;
-static char *rtc_device = "/dev/rtc";
+static char *rtc_device;
 static int acquisition_port = -1;
 static int ntp_port = 123;
 static char *keys_file = NULL;
@@ -104,8 +104,8 @@ static int do_log_refclocks = 0;
 static int do_log_tempcomp = 0;
 static int do_dump_on_exit = 0;
 static int log_banner = 32;
-static char *logdir = ".";
-static char *dumpdir = ".";
+static char *logdir;
+static char *dumpdir;
 
 static int enable_local=0;
 static int local_stratum;
@@ -180,7 +180,7 @@ static IPAddr bind_cmd_address4, bind_cmd_address6;
 
 /* Filename to use for storing pid of running chronyd, to prevent multiple
  * chronyds being started. */
-static char *pidfile = "/var/run/chronyd.pid";
+static char *pidfile;
 
 /* Temperature sensor, update interval and compensation coefficients */
 static char *tempcomp_file = NULL;
@@ -194,7 +194,7 @@ static int lock_memory = 0;
 static char *leapsec_tz = NULL;
 
 /* Name of the user to which will be dropped root privileges. */
-static char *user = DEFAULT_USER;
+static char *user;
 
 typedef struct {
   NTP_Source_Type type;
@@ -286,9 +286,39 @@ check_number_of_args(char *line, int num)
 /* ================================================== */
 
 void
-CNF_SetRestarted(int r)
+CNF_Initialise(int r)
 {
   restarted = r;
+
+  dumpdir = Strdup(".");
+  logdir = Strdup(".");
+  pidfile = Strdup("/var/run/chronyd.pid");
+  rtc_device = Strdup("/dev/rtc");
+  user = Strdup(DEFAULT_USER);
+}
+
+/* ================================================== */
+
+void
+CNF_Finalise(void)
+{
+  unsigned int i;
+
+  for (i = 0; i < n_ntp_sources; i++)
+    Free(ntp_sources[i].params.name);
+
+  Free(drift_file);
+  Free(dumpdir);
+  Free(hwclock_file);
+  Free(keys_file);
+  Free(leapsec_tz);
+  Free(logdir);
+  Free(pidfile);
+  Free(rtc_device);
+  Free(rtc_file);
+  Free(user);
+  Free(mail_user_on_change);
+  Free(tempcomp_file);
 }
 
 /* ================================================== */
@@ -462,6 +492,7 @@ static int
 parse_string(char *line, char **result)
 {
   check_number_of_args(line, 1);
+  Free(*result);
   *result = Strdup(line);
   return 1;
 }
@@ -856,6 +887,7 @@ parse_mailonchange(char *line)
   check_number_of_args(line, 2);
   address = line;
   line = CPS_SplitWord(line);
+  Free(mail_user_on_change);
   if (sscanf(line, "%lf", &mail_change_threshold) == 1) {
     mail_user_on_change = Strdup(address);
   } else {
@@ -1139,6 +1171,7 @@ parse_tempcomp(char *line)
     return;
   }
 
+  Free(tempcomp_file);
   tempcomp_file = Strdup(p);
 }
 
diff --git a/conf.h b/conf.h
index 2f9db0053fd84bbc32ae8ec45a86728df7d26acb..f65f87efa8441075b53ed86843db0b616055357f 100644 (file)
--- a/conf.h
+++ b/conf.h
@@ -30,7 +30,8 @@
 
 #include "addressing.h"
 
-extern void CNF_SetRestarted(int);
+extern void CNF_Initialise(int restarted);
+extern void CNF_Finalise(void);
 
 extern char *CNF_GetRtcDevice(void);
 
diff --git a/hash.h b/hash.h
index f73f59d6d09212a90841af0618cde7064bc7e777..185da661533c1c79a5c4400fe223fa2003e03295 100644 (file)
--- a/hash.h
+++ b/hash.h
@@ -38,4 +38,6 @@ extern unsigned int HSH_Hash(int id,
     const unsigned char *in2, unsigned int in2_len,
     unsigned char *out, unsigned int out_len);
 
+extern void HSH_Finalise(void);
+
 #endif
index 90b3bff6594418dfb00098bd381aa8ed7ef1e37a..64e0b9ced252cf643bcd524ab621b7f37c3948c8 100644 (file)
@@ -62,3 +62,8 @@ HSH_Hash(int id, const unsigned char *in1, unsigned int in1_len,
 
   return 16;
 }
+
+void
+HSH_Finalise(void)
+{
+}
index a6a3c81ddef64b55a784994b90df25515d7f9ea4..6e62304d5ea0c75e604e9805736edb531cd80c08 100644 (file)
@@ -87,3 +87,17 @@ HSH_Hash(int id, const unsigned char *in1, unsigned int in1_len,
 
   return ret;
 }
+
+void
+HSH_Finalise(void)
+{
+  int i;
+
+  for (i = 0; hashes[i].name; i++) {
+    if (hashes[i].context)
+      NSSLOWHASH_Destroy(hashes[i].context);
+  }
+
+  if (ictx)
+    NSSLOW_Shutdown(ictx);
+}
index 82c4d1ccea47e1c6acc9b4fddd2992c2a07e20bc..db2f9f04cdcb3543c9ac9021b57860143010ee5f 100644 (file)
@@ -114,3 +114,8 @@ HSH_Hash(int id, const unsigned char *in1, unsigned int in1_len,
 
   return len;
 }
+
+void
+HSH_Finalise(void)
+{
+}
diff --git a/keys.c b/keys.c
index 6c326ad3ecb02058966dbe76d3c590c60a16ea3d..3ef1ed5a12edf084a288cf40d979f71563523b84 100644 (file)
--- a/keys.c
+++ b/keys.c
@@ -137,6 +137,11 @@ KEY_Initialise(void)
 void
 KEY_Finalise(void)
 {
+  int i;
+
+  for (i=0; i<n_keys; i++) {
+    Free(keys[i].val);
+  }
 }
 
 /* ================================================== */
diff --git a/local.c b/local.c
index f6e87936b8be3183b851d07ecfc6adcc6fdc2f84..d5762d9158c95bc0ab8680b4521483fc02c4e814 100644 (file)
--- a/local.c
+++ b/local.c
@@ -168,6 +168,13 @@ LCL_Initialise(void)
 void
 LCL_Finalise(void)
 {
+  while (change_list.next != &change_list)
+    LCL_RemoveParameterChangeHandler(change_list.next->handler,
+                                     change_list.next->anything);
+
+  while (dispersion_notify_list.next != &dispersion_notify_list)
+    LCL_RemoveDispersionNotifyHandler(dispersion_notify_list.next->handler,
+                                      dispersion_notify_list.next->anything);
 }
 
 /* ================================================== */
diff --git a/main.c b/main.c
index 2831d67386ff9778c0f60ea80e127baac798abd7..82bcbd7c22e3552b5e29640b136c63c2766d73c2 100644 (file)
--- a/main.c
+++ b/main.c
@@ -86,6 +86,9 @@ MAI_CleanupAndExit(void)
     SRC_DumpSources();
   }
 
+  /* Don't update clock when removing sources */
+  REF_SetMode(REF_ModeIgnore);
+
   TMC_Finalise();
   MNL_Finalise();
   CLG_Finalise();
@@ -93,10 +96,10 @@ MAI_CleanupAndExit(void)
   NCR_Finalise();
   BRD_Finalise();
   SST_Finalise();
-  REF_Finalise();
   KEY_Finalise();
   RCL_Finalise();
   SRC_Finalise();
+  REF_Finalise();
   RTC_Finalise();
   CAM_Finalise();
   NIO_Finalise();
@@ -106,8 +109,11 @@ MAI_CleanupAndExit(void)
 
   delete_pidfile();
   
+  CNF_Finalise();
   LOG_Finalise();
 
+  HSH_Finalise();
+
   exit(exit_status);
 }
 
@@ -430,7 +436,7 @@ int main
 
   DNS_SetAddressFamily(address_family);
 
-  CNF_SetRestarted(restarted);
+  CNF_Initialise(restarted);
 
   /* Parse the config file or the remaining command line arguments */
   if (!config_args) {
index c98053c121935233437f2b4e56d632e175051d30..2d38f4b24a14b35ea0b78a3820fe961858b8b89b 100644 (file)
@@ -122,6 +122,23 @@ NSR_Initialise(void)
 void
 NSR_Finalise(void)
 {
+  int i;
+  struct UnresolvedSource *us;
+
+  for (i = 0; i < N_RECORDS; i++) {
+    if (!records[i].remote_addr)
+      continue;
+    records[i].remote_addr = NULL;
+    NCR_DestroyInstance(records[i].data);
+  }
+
+  while (unresolved_sources) {
+    us = unresolved_sources;
+    unresolved_sources = us->next;
+    Free(us->name);
+    Free(us);
+  }
+
   initialised = 0;
 }
 
index 5c21f125312fb7ded115b13a1a5ba55565c40b5d..66b231a860c8bb13ea4ed55a395b9c5f5affe6ce 100644 (file)
@@ -140,6 +140,7 @@ RCL_Finalise(void)
 
     filter_fini(&inst->filter);
     Free(inst->driver_parameter);
+    SRC_DestroyInstance(inst->source);
   }
 
   if (n_sources > 0) {
index 0af3345c4229d1b86f68827a198f9ea7d50666aa..1ab0a9910676a22a4cf2251dc2f35b3f60d6de8f 100644 (file)
--- a/sources.c
+++ b/sources.c
@@ -165,6 +165,7 @@ source_to_string(SRC_Instance inst);
 void SRC_Initialise(void) {
   sources = NULL;
   sort_list = NULL;
+  sel_sources = NULL;
   n_sources = 0;
   max_n_sources = 0;
   selected_source_index = INVALID_SOURCE;
@@ -183,6 +184,11 @@ void SRC_Finalise(void)
 {
   LCL_RemoveParameterChangeHandler(slew_sources, NULL);
   LCL_RemoveDispersionNotifyHandler(add_dispersion, NULL);
+
+  Free(sources);
+  Free(sort_list);
+  Free(sel_sources);
+
   initialised = 0;
 }