memory leak found during multiple reboots. it was found that in many cases the
reply was not unref'ed
--signed off by vishwanath@in.ibm.com---
diff --git a/transporthandler.C b/transporthandler.C
index d43f1f9..0fd25f9 100644
--- a/transporthandler.C
+++ b/transporthandler.C
@@ -55,7 +55,7 @@
ipmi_ret_t rc = IPMI_CC_OK;
*data_len = 0;
sd_bus *bus = ipmid_get_sd_bus_connection();
- sd_bus_message *reply = NULL, *m = NULL;
+ sd_bus_message *reply = NULL;
sd_bus_error error = SD_BUS_ERROR_NULL;
int r = 0;
@@ -84,20 +84,10 @@
reqptr->data[4],
reqptr->data[5]);
- r = sd_bus_message_new_method_call(bus,&m,app,obj,ifc,"SetHwAddress");
- if (r < 0) {
- fprintf(stderr, "Failed to add method object: %s\n", strerror(-r));
- return -1;
- }
- r = sd_bus_message_append(m, "s", mac);
- if (r < 0) {
- fprintf(stderr, "Failed to append message data: %s\n", strerror(-r));
- return -1;
- }
- r = sd_bus_call(bus, m, 0, &error, &reply);
+ r = sd_bus_call_method(bus,app,obj,ifc,"SetHwAddress",
+ &error, &reply, "s", mac);
if (r < 0) {
fprintf(stderr, "Failed to call method: %s\n", strerror(-r));
- return -1;
}
}
else if (reqptr->parameter == LAN_PARM_SUBNET)
@@ -112,84 +102,72 @@
}
else if (reqptr->parameter == LAN_PARM_INPROGRESS) // Apply config
{
- int rc = 0;
- sd_bus_message *req = NULL;
- sd_bus_message *res = NULL;
- sd_bus *bus1 = NULL;
- sd_bus_error err = SD_BUS_ERROR_NULL;
-
if (!strcmp(new_ipaddr, "") || !strcmp (new_netmask, "") || !strcmp (new_gateway, ""))
{
fprintf(stderr,"ERROR: Incomplete LAN Parameters\n");
return -1;
}
-
- rc = sd_bus_open_system(&bus1);
- if(rc < 0)
- {
- fprintf(stderr,"ERROR: Getting a SYSTEM bus hook\n");
- return -1;
- }
if (strcmp(cur_ipaddr, ""))
{
- sd_bus_error_free(&err);
- sd_bus_message_unref(req);
- sd_bus_message_unref(res);
-
- rc = sd_bus_call_method(bus1, // On the System Bus
- app, // Service to contact
- obj, // Object path
- ifc, // Interface name
- "DelAddress4", // Method to be called
- &err, // object to return error
- &res, // Response message on success
- "ssss", // input message (dev,ip,nm,gw)
- "eth0",
- cur_ipaddr,
- cur_netmask,
- cur_gateway);
+ r = sd_bus_call_method(bus, // On the System Bus
+ app, // Service to contact
+ obj, // Object path
+ ifc, // Interface name
+ "DelAddress4", // Method to be called
+ &error, // object to return error
+ &reply, // Response message on success
+ "ssss", // input message (dev,ip,nm,gw)
+ "eth0",
+ cur_ipaddr,
+ cur_netmask,
+ cur_gateway);
}
- if(rc < 0)
+ if(r < 0)
{
- fprintf(stderr, "Failed to remove existing IP %s: %s\n", cur_ipaddr, err.message);
- return -1;
+ fprintf(stderr, "Failed to remove existing IP %s: %s\n", cur_ipaddr, error.message);
+ goto finish;
}
- sd_bus_error_free(&err);
- sd_bus_message_unref(req);
- sd_bus_message_unref(res);
+ sd_bus_error_free(&error);
+ reply = sd_bus_message_unref(reply);
- rc = sd_bus_call_method(bus1, // On the System Bus
- app, // Service to contact
- obj, // Object path
- ifc, // Interface name
- "AddAddress4", // Method to be called
- &err, // object to return error
- &res, // Response message on success
- "ssss", // input message (dev,ip,nm,gw)
- "eth0",
- new_ipaddr,
- new_netmask,
- new_gateway);
- if(rc < 0)
+ r = sd_bus_call_method(bus, // On the System Bus
+ app, // Service to contact
+ obj, // Object path
+ ifc, // Interface name
+ "AddAddress4", // Method to be called
+ &error, // object to return error
+ &reply, // Response message on success
+ "ssss", // input message (dev,ip,nm,gw)
+ "eth0",
+ new_ipaddr,
+ new_netmask,
+ new_gateway);
+ if(r < 0)
{
- fprintf(stderr, "Failed to set IP %s: %s\n", new_ipaddr, err.message);
- return -1;
+ fprintf(stderr, "Failed to set IP %s: %s\n", new_ipaddr, error.message);
}
-
- strcpy (cur_ipaddr, new_ipaddr);
- strcpy (cur_netmask, new_netmask);
- strcpy (cur_gateway, new_gateway);
+ else
+ {
+ strcpy (cur_ipaddr, new_ipaddr);
+ strcpy (cur_netmask, new_netmask);
+ strcpy (cur_gateway, new_gateway);
+ }
}
else
{
fprintf(stderr, "Unsupported parameter 0x%x\n", reqptr->parameter);
- return IPMI_CC_PARM_NOT_SUPPORTED;
+ rc = IPMI_CC_PARM_NOT_SUPPORTED;
}
- return rc;
+finish:
+ // Clenaup the resources allocated reply and error
+ sd_bus_error_free(&error);
+ reply = sd_bus_message_unref(reply);
+
+ return (r < 0) ? -1 : rc;
}
struct get_lan_t {
@@ -206,7 +184,7 @@
ipmi_ret_t rc = IPMI_CC_OK;
*data_len = 0;
sd_bus *bus = ipmid_get_sd_bus_connection();
- sd_bus_message *reply = NULL, *m = NULL;
+ sd_bus_message *reply = NULL;
sd_bus_error error = SD_BUS_ERROR_NULL;
int r = 0;
const uint8_t current_revision = 0x11; // Current rev per IPMI Spec 2.0
@@ -259,32 +237,27 @@
const char* device = "eth0";
uint8_t buf[5]; // Size of expected IPMI response msg
- r = sd_bus_message_new_method_call(bus,&m,app,obj,ifc,"GetAddress4");
- if (r < 0) {
- fprintf(stderr, "Failed to add method object: %s\n", strerror(-r));
- return -1;
- }
- r = sd_bus_message_append(m, "s", device);
- if (r < 0) {
- fprintf(stderr, "Failed to append message data: %s\n", strerror(-r));
- return -1;
- }
- r = sd_bus_call(bus, m, 0, &error, &reply);
- if (r < 0) {
+ r = sd_bus_call_method(bus,app,obj,ifc,"GetAddress4",
+ &error, &reply, "s", device);
+ if (r < 0)
+ {
fprintf(stderr, "Failed to call method: %s\n", strerror(-r));
- return -1;
+ rc = -1;
+ goto finish;
}
- rc = sd_bus_message_enter_container (reply, 'a', "(iyyus)");
- if(rc < 0)
+ r = sd_bus_message_enter_container (reply, 'a', "(iyyus)");
+ if(r < 0)
{
fprintf(stderr, "Failed to parse response message:[%s]\n", strerror(-rc));
- return -1;
+ rc = -1;
+ goto finish;
}
- rc = sd_bus_message_read(reply, "(iyyus)", &family, &prefixlen, &scope, &flags, &saddr);
- if (rc < 0)
+ r = sd_bus_message_read(reply, "(iyyus)", &family, &prefixlen, &scope, &flags, &saddr);
+ if (r < 0)
{
fprintf(stderr, "Failed to receive response: %s\n", strerror(-r));
- return -1;
+ rc = -1;
+ goto finish;
}
printf("%s:%d:%d:%d:%s\n", family==AF_INET?"IPv4":"IPv6", prefixlen, scope, flags, saddr);
@@ -297,7 +270,8 @@
if (digit == NULL)
{
fprintf(stderr, "Unexpected IP format: %s", saddr);
- return IPMI_CC_RESPONSE_ERROR;
+ rc = IPMI_CC_RESPONSE_ERROR;
+ goto finish;
}
i = 0;
while (digit != NULL)
@@ -311,7 +285,7 @@
*data_len = sizeof(buf);
memcpy(response, &buf, *data_len);
- return IPMI_CC_OK;
+ rc = IPMI_CC_OK;
}
else if (reqptr->parameter == LAN_PARM_MAC)
{
@@ -321,30 +295,26 @@
uint8_t buf[7];
char *eaddr1 = NULL;
- r = sd_bus_message_new_method_call(bus,&m,app,obj,ifc,"GetHwAddress");
- if (r < 0) {
- fprintf(stderr, "Failed to add method object: %s\n", strerror(-r));
- return -1;
- }
- r = sd_bus_message_append(m, "s", device);
- if (r < 0) {
- fprintf(stderr, "Failed to append message data: %s\n", strerror(-r));
- return -1;
- }
- r = sd_bus_call(bus, m, 0, &error, &reply);
- if (r < 0) {
- fprintf(stderr, "Failed to call method: %s\n", strerror(-r));
- return -1;
+ r = sd_bus_call_method(bus,app,obj,ifc,"GetHwAddress",
+ &error, &reply, "s", device);
+ if (r < 0)
+ {
+ fprintf(stderr, "Failed to call GetHwAddress: %s\n", strerror(-r));
+ rc = -1;
+ goto finish;
}
r = sd_bus_message_read(reply, "s", &eaddr1);
- if (r < 0) {
+ if (r < 0)
+ {
fprintf(stderr, "Failed to get a response: %s", strerror(-r));
- return IPMI_CC_RESPONSE_ERROR;
+ rc = IPMI_CC_RESPONSE_ERROR;
+ goto finish;
}
if (eaddr1 == NULL)
{
fprintf(stderr, "Failed to get a valid response: %s", strerror(-r));
- return IPMI_CC_RESPONSE_ERROR;
+ rc = IPMI_CC_RESPONSE_ERROR;
+ goto finish;
}
memcpy((void*)&buf[0], ¤t_revision, 1);
@@ -354,7 +324,8 @@
if (digit == NULL)
{
fprintf(stderr, "Unexpected MAC format: %s", eaddr1);
- return IPMI_CC_RESPONSE_ERROR;
+ rc = IPMI_CC_RESPONSE_ERROR;
+ goto finish;
}
i=0;
@@ -369,14 +340,18 @@
*data_len = sizeof(buf);
memcpy(response, &buf, *data_len);
- return IPMI_CC_OK;
+ rc = IPMI_CC_OK;
}
else
{
fprintf(stderr, "Unsupported parameter 0x%x\n", reqptr->parameter);
- return IPMI_CC_PARM_NOT_SUPPORTED;
+ rc = IPMI_CC_PARM_NOT_SUPPORTED;
}
+finish:
+ sd_bus_error_free(&error);
+ reply = sd_bus_message_unref(reply);
+
return rc;
}