fix memory leak when interfaces are compiled statically

This commit is contained in:
Duncan Laurie 2004-03-26 21:49:45 +00:00
parent c199b86143
commit 95044acb5d
2 changed files with 30 additions and 20 deletions

View File

@ -44,7 +44,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <config.h>
#include <ipmitool/helper.h> #include <ipmitool/helper.h>
#include <ipmitool/ipmi_intf.h> #include <ipmitool/ipmi_intf.h>
#include <ipmitool/ipmi.h> #include <ipmitool/ipmi.h>
@ -59,15 +58,21 @@
#include <ipmitool/ipmi_channel.h> #include <ipmitool/ipmi_channel.h>
#ifdef HAVE_CONFIG_H
# include <config.h>
#else
# define IPMITOOL_BIN "ipmitool"
#endif
struct ipmi_session lan_session; struct ipmi_session lan_session;
int csv_output = 0; int csv_output = 0;
int verbose = 0; int verbose = 0;
void usage(void) void usage(void)
{ {
printf("ipmitool version %s\n", VERSION); printf("%s version %s\n", IPMITOOL_BIN, VERSION);
printf("\n"); printf("\n");
printf("usage: ipmitool [options...] <command>\n"); printf("usage: %s [options...] <command>\n", IPMITOOL_BIN);
printf("\n"); printf("\n");
printf(" -h This help\n"); printf(" -h This help\n");
printf(" -V Show version information\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(" -p port Remote RMCP port (default is 623)\n");
printf(" -U username Remote username\n"); printf(" -U username Remote username\n");
printf(" -a Prompt for remote password\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(" -P password Remote password\n");
printf(" -I intf Inteface to use\n"); printf(" -I intf Inteface to use\n");
printf("\n\n"); printf("\n\n");
exit(EXIT_SUCCESS);
} }
int ipmi_raw_main(struct ipmi_intf * intf, int argc, char ** argv) 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 **); int (*submain)(struct ipmi_intf *, int, char **);
struct ipmi_intf * intf = NULL; struct ipmi_intf * intf = NULL;
char * hostname = NULL, * password = NULL, * username = NULL, * tmp; 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]; char intfname[32];
if (ipmi_intf_init() < 0) memset(intfname, 0, sizeof(intfname));
exit(EXIT_FAILURE);
while ((argflag = getopt(argc, (char **)argv, "hVvcgEaI:H:P:U:p:")) != -1) while ((argflag = getopt(argc, (char **)argv, "hVvcgEaI:H:P:U:p:")) != -1)
{ {
switch (argflag) { switch (argflag) {
case 'h': case 'h':
usage(); usage();
goto out_free;
break; break;
case 'V': case 'V':
printf("ipmitool version %s\n", VERSION); printf("%s version %s\n", IPMITOOL_BIN, VERSION);
exit(EXIT_SUCCESS); goto out_free;
break; break;
case 'g': case 'g':
pedantic = 1; pedantic = 1;
@ -285,13 +288,7 @@ int main(int argc, char ** argv)
csv_output = 1; csv_output = 1;
break; break;
case 'I': case 'I':
memset(intfname, 0, sizeof(intfname)); intfarg = snprintf(intfname, sizeof(intfname), "intf_%s", optarg);
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);
}
break; break;
case 'H': case 'H':
hostname = strdup(optarg); hostname = strdup(optarg);
@ -340,17 +337,27 @@ int main(int argc, char ** argv)
break; break;
default: default:
usage(); usage();
goto out_free;
} }
} }
if (argc-optind <= 0) { if (argc-optind <= 0) {
printf("No command provided!\n"); printf("No command provided!\n");
usage(); usage();
goto out_free;
} }
if (intfarg) {
intf = ipmi_intf_load(intfname);
if (!intf) { if (!intf) {
printf("Error loading interface %s\n", optarg);
goto out_free;
}
}
else {
printf("No interface specified!\n"); printf("No interface specified!\n");
usage(); usage();
goto out_free;
} }
intf->pedantic = pedantic; intf->pedantic = pedantic;
@ -436,9 +443,9 @@ int main(int argc, char ** argv)
if (intf->close) if (intf->close)
intf->close(intf); intf->close(intf);
out_free:
ipmi_intf_exit(); ipmi_intf_exit();
out_free:
if (hostname) if (hostname)
free(hostname); free(hostname);
if (username) if (username)

View File

@ -97,6 +97,9 @@ struct ipmi_intf * ipmi_intf_load(char * name)
i++; i++;
} }
if (ipmi_intf_init() < 0)
return NULL;
memset(libname, 0, 16); memset(libname, 0, 16);
if (snprintf(libname, sizeof(libname), "lib%s", name) <= 0) { if (snprintf(libname, sizeof(libname), "lib%s", name) <= 0) {
printf("ERROR: Unable to find plugin '%s' in '%s'\n", printf("ERROR: Unable to find plugin '%s' in '%s'\n",