From f6ed7844e1ac8c25fe9ba84232c563f302beba30 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Tue, 23 Sep 2014 13:35:38 +0200 Subject: [PATCH] Free allocated memory on exit 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. --- clientlog.c | 5 +++++ conf.c | 45 +++++++++++++++++++++++++++++++++++++++------ conf.h | 3 ++- hash.h | 2 ++ hash_intmd5.c | 5 +++++ hash_nss.c | 14 ++++++++++++++ hash_tomcrypt.c | 5 +++++ keys.c | 5 +++++ local.c | 7 +++++++ main.c | 10 ++++++++-- ntp_sources.c | 17 +++++++++++++++++ refclock.c | 1 + sources.c | 6 ++++++ 13 files changed, 116 insertions(+), 9 deletions(-) diff --git a/clientlog.c b/clientlog.c index 84900b4..b895c59 100644 --- a/clientlog.c +++ b/clientlog.c @@ -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 240b56b..e0311ed 100644 --- 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 2f9db00..f65f87e 100644 --- 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 f73f59d..185da66 100644 --- 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 diff --git a/hash_intmd5.c b/hash_intmd5.c index 90b3bff..64e0b9c 100644 --- a/hash_intmd5.c +++ b/hash_intmd5.c @@ -62,3 +62,8 @@ HSH_Hash(int id, const unsigned char *in1, unsigned int in1_len, return 16; } + +void +HSH_Finalise(void) +{ +} diff --git a/hash_nss.c b/hash_nss.c index a6a3c81..6e62304 100644 --- a/hash_nss.c +++ b/hash_nss.c @@ -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); +} diff --git a/hash_tomcrypt.c b/hash_tomcrypt.c index 82c4d1c..db2f9f0 100644 --- a/hash_tomcrypt.c +++ b/hash_tomcrypt.c @@ -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 6c326ad..3ef1ed5 100644 --- a/keys.c +++ b/keys.c @@ -137,6 +137,11 @@ KEY_Initialise(void) void KEY_Finalise(void) { + int i; + + for (i=0; ihandler, + 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 2831d67..82bcbd7 100644 --- 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) { diff --git a/ntp_sources.c b/ntp_sources.c index c98053c..2d38f4b 100644 --- a/ntp_sources.c +++ b/ntp_sources.c @@ -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; } diff --git a/refclock.c b/refclock.c index 5c21f12..66b231a 100644 --- a/refclock.c +++ b/refclock.c @@ -140,6 +140,7 @@ RCL_Finalise(void) filter_fini(&inst->filter); Free(inst->driver_parameter); + SRC_DestroyInstance(inst->source); } if (n_sources > 0) { diff --git a/sources.c b/sources.c index 0af3345..1ab0a99 100644 --- 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; }