From 95044acb5d93d3c66cb7a1fd85309552a3e98e3b Mon Sep 17 00:00:00 2001 From: Duncan Laurie Date: Fri, 26 Mar 2004 21:49:45 +0000 Subject: [PATCH] fix memory leak when interfaces are compiled statically --- ipmitool/src/ipmitool.c | 47 ++++++++++++++++++-------------- ipmitool/src/plugins/ipmi_intf.c | 3 ++ 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/ipmitool/src/ipmitool.c b/ipmitool/src/ipmitool.c index 2f6c775..006e15c 100644 --- a/ipmitool/src/ipmitool.c +++ b/ipmitool/src/ipmitool.c @@ -44,7 +44,6 @@ #include #include -#include #include #include #include @@ -59,15 +58,21 @@ #include +#ifdef HAVE_CONFIG_H +# include +#else +# define IPMITOOL_BIN "ipmitool" +#endif + struct ipmi_session lan_session; int csv_output = 0; int verbose = 0; void usage(void) { - printf("ipmitool version %s\n", VERSION); + printf("%s version %s\n", IPMITOOL_BIN, VERSION); printf("\n"); - printf("usage: ipmitool [options...] \n"); + printf("usage: %s [options...] \n", IPMITOOL_BIN); printf("\n"); printf(" -h This help\n"); printf(" -V Show version information\n"); @@ -78,12 +83,10 @@ void usage(void) printf(" -p port Remote RMCP port (default is 623)\n"); printf(" -U username Remote username\n"); printf(" -a Prompt for remote password\n"); - printf(" -E Read remote password from environment\n"); + printf(" -E Read remote password from environment variable IPMITOOL_PASSWORD\n"); printf(" -P password Remote password\n"); printf(" -I intf Inteface to use\n"); printf("\n\n"); - - exit(EXIT_SUCCESS); } int ipmi_raw_main(struct ipmi_intf * intf, int argc, char ** argv) @@ -259,21 +262,21 @@ int main(int argc, char ** argv) int (*submain)(struct ipmi_intf *, int, char **); struct ipmi_intf * intf = NULL; char * hostname = NULL, * password = NULL, * username = NULL, * tmp; - int argflag, i, rc=0, port = 623, pedantic = 0; + int argflag, intfarg = 0, rc = 0, port = 623, pedantic = 0; char intfname[32]; - if (ipmi_intf_init() < 0) - exit(EXIT_FAILURE); + memset(intfname, 0, sizeof(intfname)); while ((argflag = getopt(argc, (char **)argv, "hVvcgEaI:H:P:U:p:")) != -1) { switch (argflag) { case 'h': usage(); + goto out_free; break; case 'V': - printf("ipmitool version %s\n", VERSION); - exit(EXIT_SUCCESS); + printf("%s version %s\n", IPMITOOL_BIN, VERSION); + goto out_free; break; case 'g': pedantic = 1; @@ -285,13 +288,7 @@ int main(int argc, char ** argv) csv_output = 1; break; case 'I': - memset(intfname, 0, sizeof(intfname)); - i = snprintf(intfname, sizeof(intfname), "intf_%s", optarg); - intf = ipmi_intf_load(intfname); - if (!intf) { - printf("Error loading interface %s\n", optarg); - exit(EXIT_FAILURE); - } + intfarg = snprintf(intfname, sizeof(intfname), "intf_%s", optarg); break; case 'H': hostname = strdup(optarg); @@ -340,17 +337,27 @@ int main(int argc, char ** argv) break; default: usage(); + goto out_free; } } if (argc-optind <= 0) { printf("No command provided!\n"); usage(); + goto out_free; } - if (!intf) { + if (intfarg) { + intf = ipmi_intf_load(intfname); + if (!intf) { + printf("Error loading interface %s\n", optarg); + goto out_free; + } + } + else { printf("No interface specified!\n"); usage(); + goto out_free; } intf->pedantic = pedantic; @@ -436,9 +443,9 @@ int main(int argc, char ** argv) if (intf->close) intf->close(intf); + out_free: ipmi_intf_exit(); - out_free: if (hostname) free(hostname); if (username) diff --git a/ipmitool/src/plugins/ipmi_intf.c b/ipmitool/src/plugins/ipmi_intf.c index 92652ae..fff2026 100644 --- a/ipmitool/src/plugins/ipmi_intf.c +++ b/ipmitool/src/plugins/ipmi_intf.c @@ -97,6 +97,9 @@ struct ipmi_intf * ipmi_intf_load(char * name) i++; } + if (ipmi_intf_init() < 0) + return NULL; + memset(libname, 0, 16); if (snprintf(libname, sizeof(libname), "lib%s", name) <= 0) { printf("ERROR: Unable to find plugin '%s' in '%s'\n",