Clean up memory management when loading IMC/IMVs from files

This commit is contained in:
Martin Willi
2012-11-30 15:47:34 +01:00
parent db9c8b6fba
commit cd74959465
5 changed files with 38 additions and 58 deletions
+22 -29
View File
@@ -37,11 +37,6 @@ struct private_tnc_imc_t {
*/ */
imc_t public; imc_t public;
/**
* Path of loaded IMC
*/
char *path;
/** /**
* Name of loaded IMC * Name of loaded IMC
*/ */
@@ -291,10 +286,10 @@ METHOD(imc_t, type_supported, bool,
for (i = 0; i < this->type_count; i++) for (i = 0; i < this->type_count; i++)
{ {
vid = this->supported_vids[i]; vid = this->supported_vids[i];
subtype = this->supported_subtypes[i]; subtype = this->supported_subtypes[i];
if ((vid == TNC_VENDORID_ANY && subtype == TNC_SUBTYPE_ANY) || if ((vid == TNC_VENDORID_ANY && subtype == TNC_SUBTYPE_ANY) ||
(vid == msg_vid && (subtype == TNC_SUBTYPE_ANY || (vid == msg_vid && (subtype == TNC_SUBTYPE_ANY ||
subtype == msg_subtype))) subtype == msg_subtype)))
{ {
@@ -307,13 +302,15 @@ METHOD(imc_t, type_supported, bool,
METHOD(imc_t, destroy, void, METHOD(imc_t, destroy, void,
private_tnc_imc_t *this) private_tnc_imc_t *this)
{ {
dlclose(this->handle); if (this->handle)
{
dlclose(this->handle);
}
this->mutex->destroy(this->mutex); this->mutex->destroy(this->mutex);
this->additional_ids->destroy(this->additional_ids); this->additional_ids->destroy(this->additional_ids);
free(this->supported_vids); free(this->supported_vids);
free(this->supported_subtypes); free(this->supported_subtypes);
free(this->name); free(this->name);
free(this->path);
free(this); free(this);
} }
@@ -335,9 +332,8 @@ imc_t* tnc_imc_create(char *name, char *path)
.set_message_types_long = _set_message_types_long, .set_message_types_long = _set_message_types_long,
.type_supported = _type_supported, .type_supported = _type_supported,
.destroy = _destroy, .destroy = _destroy,
}, },
.name = name, .name = strdup(name),
.path = path,
.additional_ids = linked_list_create(), .additional_ids = linked_list_create(),
.mutex = mutex_create(MUTEX_TYPE_DEFAULT), .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
); );
@@ -346,46 +342,43 @@ imc_t* tnc_imc_create(char *name, char *path)
if (!this->handle) if (!this->handle)
{ {
DBG1(DBG_TNC, "IMC \"%s\" failed to load: %s", name, dlerror()); DBG1(DBG_TNC, "IMC \"%s\" failed to load: %s", name, dlerror());
free(this); destroy(this);
return NULL; return NULL;
} }
this->public.initialize = dlsym(this->handle, "TNC_IMC_Initialize"); this->public.initialize = dlsym(this->handle, "TNC_IMC_Initialize");
if (!this->public.initialize) if (!this->public.initialize)
{ {
DBG1(DBG_TNC, "could not resolve TNC_IMC_Initialize in %s: %s\n", DBG1(DBG_TNC, "could not resolve TNC_IMC_Initialize in %s: %s\n",
path, dlerror()); path, dlerror());
dlclose(this->handle); destroy(this);
free(this);
return NULL; return NULL;
} }
this->public.notify_connection_change = this->public.notify_connection_change =
dlsym(this->handle, "TNC_IMC_NotifyConnectionChange"); dlsym(this->handle, "TNC_IMC_NotifyConnectionChange");
this->public.begin_handshake = dlsym(this->handle, "TNC_IMC_BeginHandshake"); this->public.begin_handshake = dlsym(this->handle, "TNC_IMC_BeginHandshake");
if (!this->public.begin_handshake) if (!this->public.begin_handshake)
{ {
DBG1(DBG_TNC, "could not resolve TNC_IMC_BeginHandshake in %s: %s\n", DBG1(DBG_TNC, "could not resolve TNC_IMC_BeginHandshake in %s: %s\n",
path, dlerror()); path, dlerror());
dlclose(this->handle); destroy(this);
free(this);
return NULL; return NULL;
} }
this->public.receive_message = this->public.receive_message =
dlsym(this->handle, "TNC_IMC_ReceiveMessage"); dlsym(this->handle, "TNC_IMC_ReceiveMessage");
this->public.receive_message_long = this->public.receive_message_long =
dlsym(this->handle, "TNC_IMC_ReceiveMessageLong"); dlsym(this->handle, "TNC_IMC_ReceiveMessageLong");
this->public.batch_ending = this->public.batch_ending =
dlsym(this->handle, "TNC_IMC_BatchEnding"); dlsym(this->handle, "TNC_IMC_BatchEnding");
this->public.terminate = this->public.terminate =
dlsym(this->handle, "TNC_IMC_Terminate"); dlsym(this->handle, "TNC_IMC_Terminate");
this->public.provide_bind_function = this->public.provide_bind_function =
dlsym(this->handle, "TNC_IMC_ProvideBindFunction"); dlsym(this->handle, "TNC_IMC_ProvideBindFunction");
if (!this->public.provide_bind_function) if (!this->public.provide_bind_function)
{ {
DBG1(DBG_TNC, "could not resolve TNC_IMC_ProvideBindFunction in %s: %s\n", DBG1(DBG_TNC, "could not resolve TNC_IMC_ProvideBindFunction in %s: %s\n",
path, dlerror()); path, dlerror());
dlclose(this->handle); destroy(this);
free(this);
return NULL; return NULL;
} }
@@ -103,8 +103,6 @@ METHOD(imc_manager_t, load, bool,
imc = tnc_imc_create(name, path); imc = tnc_imc_create(name, path);
if (!imc) if (!imc)
{ {
free(name);
free(path);
return FALSE; return FALSE;
} }
if (!add(this, imc)) if (!add(this, imc))
+12 -19
View File
@@ -37,11 +37,6 @@ struct private_tnc_imv_t {
*/ */
imv_t public; imv_t public;
/**
* Path of loaded IMV
*/
char *path;
/** /**
* Name of loaded IMV * Name of loaded IMV
*/ */
@@ -287,10 +282,10 @@ METHOD(imv_t, type_supported, bool,
for (i = 0; i < this->type_count; i++) for (i = 0; i < this->type_count; i++)
{ {
vid = this->supported_vids[i]; vid = this->supported_vids[i];
subtype = this->supported_subtypes[i]; subtype = this->supported_subtypes[i];
if ((vid == TNC_VENDORID_ANY && subtype == TNC_SUBTYPE_ANY) || if ((vid == TNC_VENDORID_ANY && subtype == TNC_SUBTYPE_ANY) ||
(vid == msg_vid && (subtype == TNC_SUBTYPE_ANY || (vid == msg_vid && (subtype == TNC_SUBTYPE_ANY ||
subtype == msg_subtype))) subtype == msg_subtype)))
{ {
@@ -303,13 +298,15 @@ METHOD(imv_t, type_supported, bool,
METHOD(imv_t, destroy, void, METHOD(imv_t, destroy, void,
private_tnc_imv_t *this) private_tnc_imv_t *this)
{ {
dlclose(this->handle); if (this->handle)
{
dlclose(this->handle);
}
this->mutex->destroy(this->mutex); this->mutex->destroy(this->mutex);
this->additional_ids->destroy_function(this->additional_ids, free); this->additional_ids->destroy_function(this->additional_ids, free);
free(this->supported_vids); free(this->supported_vids);
free(this->supported_subtypes); free(this->supported_subtypes);
free(this->name); free(this->name);
free(this->path);
free(this); free(this);
} }
@@ -332,8 +329,7 @@ imv_t* tnc_imv_create(char *name, char *path)
.type_supported = _type_supported, .type_supported = _type_supported,
.destroy = _destroy, .destroy = _destroy,
}, },
.name = name, .name = strdup(name),
.path = path,
.additional_ids = linked_list_create(), .additional_ids = linked_list_create(),
.mutex = mutex_create(MUTEX_TYPE_DEFAULT), .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
); );
@@ -342,7 +338,7 @@ imv_t* tnc_imv_create(char *name, char *path)
if (!this->handle) if (!this->handle)
{ {
DBG1(DBG_TNC, "IMV \"%s\" failed to load: %s", name, dlerror()); DBG1(DBG_TNC, "IMV \"%s\" failed to load: %s", name, dlerror());
free(this); destroy(this);
return NULL; return NULL;
} }
@@ -351,8 +347,7 @@ imv_t* tnc_imv_create(char *name, char *path)
{ {
DBG1(DBG_TNC, "could not resolve TNC_IMV_Initialize in %s: %s\n", DBG1(DBG_TNC, "could not resolve TNC_IMV_Initialize in %s: %s\n",
path, dlerror()); path, dlerror());
dlclose(this->handle); destroy(this);
free(this);
return NULL; return NULL;
} }
this->public.notify_connection_change = this->public.notify_connection_change =
@@ -363,8 +358,7 @@ imv_t* tnc_imv_create(char *name, char *path)
{ {
DBG1(DBG_TNC, "could not resolve TNC_IMV_SolicitRecommendation in %s: %s\n", DBG1(DBG_TNC, "could not resolve TNC_IMV_SolicitRecommendation in %s: %s\n",
path, dlerror()); path, dlerror());
dlclose(this->handle); destroy(this);
free(this);
return NULL; return NULL;
} }
this->public.receive_message = this->public.receive_message =
@@ -381,8 +375,7 @@ imv_t* tnc_imv_create(char *name, char *path)
{ {
DBG1(DBG_TNC, "could not resolve TNC_IMV_ProvideBindFunction in %s: %s\n", DBG1(DBG_TNC, "could not resolve TNC_IMV_ProvideBindFunction in %s: %s\n",
path, dlerror()); path, dlerror());
dlclose(this->handle); destroy(this);
free(this);
return NULL; return NULL;
} }
@@ -119,8 +119,6 @@ METHOD(imv_manager_t, load, bool,
imv = tnc_imv_create(name, path); imv = tnc_imv_create(name, path);
if (!imv) if (!imv)
{ {
free(name);
free(path);
return FALSE; return FALSE;
} }
if (!add(this, imv)) if (!add(this, imv))
+4 -6
View File
@@ -163,9 +163,7 @@ static bool load_imcvs_from_config(char *filename, bool is_imc)
} }
/* copy the IMC/IMV name */ /* copy the IMC/IMV name */
name = malloc(token.len + 1); name = strndup(token.ptr, token.len);
memcpy(name, token.ptr, token.len);
name[token.len] = '\0';
/* advance to the IMC/IMV path and extract it */ /* advance to the IMC/IMV path and extract it */
if (!eat_whitespace(&line)) if (!eat_whitespace(&line))
@@ -180,9 +178,7 @@ static bool load_imcvs_from_config(char *filename, bool is_imc)
} }
/* copy the IMC/IMV path */ /* copy the IMC/IMV path */
path = malloc(token.len + 1); path = strndup(token.ptr, token.len);
memcpy(path, token.ptr, token.len);
path[token.len] = '\0';
/* load and register an IMC/IMV instance */ /* load and register an IMC/IMV instance */
if (is_imc) if (is_imc)
@@ -193,6 +189,8 @@ static bool load_imcvs_from_config(char *filename, bool is_imc)
{ {
success = tnc->imvs->load(tnc->imvs, name, path); success = tnc->imvs->load(tnc->imvs, name, path);
} }
free(name);
free(path);
if (!success) if (!success)
{ {
break; break;