[ofa-general] ***SPAM*** [PATCH] infiniband-diags: Fix memory leaks on IBERROR and IBPANIC

Hal Rosenstock hnrose at comcast.net
Thu Mar 12 09:55:30 PDT 2009


Since these macros exit, now need to call mad_rpc_close_port
prior to invoking them

Also, some cosmetic formatting changes

Signed-off-by: Hal Rosenstock <hal.rosenstock at gmail.com>

---
diff --git a/infiniband-diags/src/grouping.c b/infiniband-diags/src/grouping.c
index 0c30726..24c33ad 100644
--- a/infiniband-diags/src/grouping.c
+++ b/infiniband-diags/src/grouping.c
@@ -1,6 +1,7 @@
 /*
  * Copyright (c) 2004-2007 Voltaire Inc.  All rights reserved.
  * Copyright (c) 2007 Xsigo Systems Inc.  All rights reserved.
+ * Copyright (c) 2009 HNR Consulting.  All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
@@ -55,6 +56,7 @@ extern Node *nodesdist[MAXHOPS+1];	/* last is CA list */
 extern Node *mynode;
 extern Port *myport;
 extern int maxhops_discovered;
+extern struct ibmad_port *srcport;
 
 AllChassisList mylist;
 
@@ -302,7 +304,9 @@ static void get_sfb_slot(Node *node, Port *lineport)
 		ch->slotnum = spine4_slot_2_slb[lineport->portnum];
 		ch->anafanum = anafa_spine4_slot_2_slb[lineport->portnum];
 	} else {
-		IBPANIC("Unexpected node found: guid 0x%016" PRIx64, node->nodeguid);
+		mad_rpc_close_port(srcport);
+		IBPANIC("Unexpected node found: guid 0x%016" PRIx64,
+			node->nodeguid);
 	}
 }
 
@@ -312,8 +316,10 @@ static void get_router_slot(Node *node, Port *spineport)
 	uint64_t guessnum = 0;
 
 	if (!ch) {
-		if (!(node->chrecord = calloc(1, sizeof(ChassisRecord))))
+		if (!(node->chrecord = calloc(1, sizeof(ChassisRecord)))) {
+			mad_rpc_close_port(srcport);
 			IBPANIC("out of mem");
+		}
 		ch = node->chrecord;
 	}
 
@@ -345,7 +351,9 @@ static void get_router_slot(Node *node, Port *spineport)
 		ch->slotnum = line_slot_2_sfb4[spineport->portnum];
 		ch->anafanum = ipr_slot_2_sfb4_port[spineport->portnum];
 	} else {
-		IBPANIC("Unexpected node found: guid 0x%016" PRIx64, spineport->node->nodeguid);
+		mad_rpc_close_port(srcport);
+		IBPANIC("Unexpected node found: guid 0x%016" PRIx64,
+			spineport->node->nodeguid);
 	}
 }
 
@@ -369,7 +377,9 @@ static void get_slb_slot(ChassisRecord *ch, Port *spineport)
 		ch->slotnum = line_slot_2_sfb4[spineport->portnum];
 		ch->anafanum = anafa_line_slot_2_sfb4[spineport->portnum];
 	} else {
-		IBPANIC("Unexpected node found: guid 0x%016" PRIx64, spineport->node->nodeguid);
+		mad_rpc_close_port(srcport);
+		IBPANIC("Unexpected node found: guid 0x%016" PRIx64,
+			spineport->node->nodeguid);
 	}
 }
 
@@ -387,8 +397,10 @@ static void fill_chassis_record(Node *node)
 	if (node->chrecord) /* somehow this node has already been passed */
 		return;
 
-	if (!(node->chrecord = calloc(1, sizeof(ChassisRecord))))
+	if (!(node->chrecord = calloc(1, sizeof(ChassisRecord)))) {
+		mad_rpc_close_port(srcport);
 		IBPANIC("out of mem");
+	}
 
 	ch = node->chrecord;
 
@@ -433,8 +445,10 @@ static int get_line_index(Node *node)
 {
 	int retval = 3 * (node->chrecord->slotnum - 1) + node->chrecord->anafanum;
 
-	if (retval > LINES_MAX_NUM || retval < 1)
+	if (retval > LINES_MAX_NUM || retval < 1) {
+		mad_rpc_close_port(srcport);
 		IBPANIC("Internal error");
+	}
 	return retval;
 }
 
@@ -447,8 +461,10 @@ static int get_spine_index(Node *node)
 	else
 		retval = node->chrecord->slotnum;
 
-	if (retval > SPINES_MAX_NUM || retval < 1)
+	if (retval > SPINES_MAX_NUM || retval < 1) {
+		mad_rpc_close_port(srcport);
 		IBPANIC("Internal error");
+	}
 	return retval;
 }
 
@@ -677,8 +693,10 @@ char *portmapstring(Port *port)
 
 static void add_chassislist()
 {
-	if (!(mylist.current = calloc(1, sizeof(ChassisList))))
+	if (!(mylist.current = calloc(1, sizeof(ChassisList)))) {
+		mad_rpc_close_port(srcport);
 		IBPANIC("out of mem");
+	}
 
 	if (mylist.first == NULL) {
 		mylist.first = mylist.current;
@@ -768,8 +786,10 @@ ChassisList *group_nodes()
 					if (!chassis->chassisnum)
 						chassis->chassisnum = ++chassisnum;
 					if (!node->chrecord) {
-						if (!(node->chrecord = calloc(1, sizeof(ChassisRecord))))
+						if (!(node->chrecord = calloc(1, sizeof(ChassisRecord)))) {
+							mad_rpc_close_port(srcport);
 							IBPANIC("out of mem");
+						}
 						node->chrecord->chassisnum = chassis->chassisnum;
 					}
 				}
diff --git a/infiniband-diags/src/ibaddr.c b/infiniband-diags/src/ibaddr.c
index bb22be9..99bd33f 100644
--- a/infiniband-diags/src/ibaddr.c
+++ b/infiniband-diags/src/ibaddr.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
+ * Copyright (c) 2009 HNR Consulting.  All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
@@ -145,15 +146,21 @@ int main(int argc, char **argv)
 
 	if (argc) {
 		if (ib_resolve_portid_str_via(&portid, argv[0], ibd_dest_type,
-						ibd_sm_id, srcport) < 0)
+						ibd_sm_id, srcport) < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("can't resolve destination port %s", argv[0]);
+		}
 	} else {
-		if (ib_resolve_self_via(&portid, &port, 0, srcport) < 0)
+		if (ib_resolve_self_via(&portid, &port, 0, srcport) < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("can't resolve self port %s", argv[0]);
+		}
 	}
 
-	if (ib_resolve_addr(&portid, port, show_lid, show_gid) < 0)
+	if (ib_resolve_addr(&portid, port, show_lid, show_gid) < 0) {
+		mad_rpc_close_port(srcport);
 		IBERROR("can't resolve requested address");
+	}
 
 	mad_rpc_close_port(srcport);
 	exit(0);
diff --git a/infiniband-diags/src/ibnetdiscover.c b/infiniband-diags/src/ibnetdiscover.c
index 25c1f7f..ecb2956 100644
--- a/infiniband-diags/src/ibnetdiscover.c
+++ b/infiniband-diags/src/ibnetdiscover.c
@@ -1,6 +1,7 @@
 /*
  * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
  * Copyright (c) 2007 Xsigo Systems Inc.  All rights reserved.
+ * Copyright (c) 2009 HNR Consulting.  All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
@@ -136,7 +137,6 @@ decode_port_info(void *pi, Port *port)
 	mad_decode_field(pi, IB_PORT_LINK_SPEED_ACTIVE_F, &port->linkspeed);
 }
 
-
 int
 get_port(Port *port, int portnum, ib_portid_t *portid)
 {
@@ -146,12 +146,14 @@ get_port(Port *port, int portnum, ib_portid_t *portid)
 	port->portnum = portnum;
 
 	if (!smp_query_via(pi, portid, IB_ATTR_PORT_INFO, portnum, timeout,
-			srcport))
+			   srcport))
 		return -1;
 	decode_port_info(pi, port);
 
 	DEBUG("portid %s portnum %d: lid %d state %d physstate %d %s %s",
-		portid2str(portid), portnum, port->lid, port->state, port->physstate, get_linkwidth_str(port->linkwidth), get_linkspeed_str(port->linkspeed));
+	      portid2str(portid), portnum, port->lid, port->state,
+	      port->physstate, get_linkwidth_str(port->linkwidth),
+	      get_linkspeed_str(port->linkspeed));
 	return 1;
 }
 /*
@@ -314,8 +316,8 @@ static void
 link_ports(Node *node, Port *port, Node *remotenode, Port *remoteport)
 {
 	DEBUG("linking: 0x%" PRIx64 " %p->%p:%u and 0x%" PRIx64 " %p->%p:%u",
-		node->nodeguid, node, port, port->portnum,
-		remotenode->nodeguid, remotenode, remoteport, remoteport->portnum);
+	      node->nodeguid, node, port, port->portnum,
+	      remotenode->nodeguid, remotenode, remoteport, remoteport->portnum);
 	if (port->remoteport)
 		port->remoteport->remoteport = NULL;
 	if (remoteport->remoteport)
@@ -352,16 +354,20 @@ handle_port(Node *node, Port *port, ib_portid_t *path, int portnum, int dist)
 	oldnode = find_node(&node_buf);
 	if (oldnode)
 		remotenode = oldnode;
-	else if (!(remotenode = create_node(&node_buf, path, dist + 1)))
+	else if (!(remotenode = create_node(&node_buf, path, dist + 1))) {
+		mad_rpc_close_port(srcport);
 		IBERROR("no memory");
+	}
 
 	oldport = find_port(remotenode, &port_buf);
 	if (oldport) {
 		remoteport = oldport;
 		if (node != remotenode || port != remoteport)
 			IBWARN("port moving...");
-	} else if (!(remoteport = create_port(remotenode, &port_buf)))
+	} else if (!(remoteport = create_port(remotenode, &port_buf))) {
+		mad_rpc_close_port(srcport);
 		IBERROR("no memory");
+	}
 
 	dump_endnode(path, oldnode ? "known remote" : "new remote",
 		     remotenode, remoteport);
@@ -397,14 +403,18 @@ discover(ib_portid_t *from)
 	}
 
 	node = create_node(&node_buf, from, 0);
-	if (!node)
+	if (!node) {
+		mad_rpc_close_port(srcport);
 		IBERROR("out of memory");
+	}
 
 	mynode = node;
 
 	port = create_port(node, &port_buf);
-	if (!port)
+	if (!port) {
+		mad_rpc_close_port(srcport);
 		IBERROR("out of memory");
+	}
 
 	if (node->type != SWITCH_NODE &&
 	    handle_port(node, port, from, node->localport, 0) < 0)
@@ -424,7 +434,8 @@ discover(ib_portid_t *from)
 					continue;
 
 				if (get_port(&port_buf, i, path) < 0) {
-					IBWARN("can't reach node %s port %d", portid2str(path), i);
+					IBWARN("can't reach node %s port %d",
+					       portid2str(path), i);
 					continue;
 				}
 
@@ -433,8 +444,10 @@ discover(ib_portid_t *from)
 					continue;
 
 				port = create_port(node, &port_buf);
-				if (!port)
+				if (!port) {
+					mad_rpc_close_port(srcport);
 					IBERROR("out of memory");
+				}
 
 				/* If switch, set port GUID to node GUID */
 				if (node->type == SWITCH_NODE)
@@ -477,7 +490,7 @@ list_node(Node *node)
 {
 	char *node_type;
 	char *nodename = remap_node_name(node_name_map, node->nodeguid,
-					      node->nodedesc);
+					 node->nodedesc);
 
 	switch(node->type) {
 	case SWITCH_NODE:
@@ -494,9 +507,8 @@ list_node(Node *node)
 		break;
 	}
 	fprintf(f, "%s\t : 0x%016" PRIx64 " ports %d devid 0x%x vendid 0x%x \"%s\"\n",
-		node_type,
-		node->nodeguid, node->numports, node->devid, node->vendid,
-		nodename);
+		node_type, node->nodeguid, node->numports, node->devid,
+		node->vendid, nodename);
 
 	free(nodename);
 }
@@ -550,15 +562,15 @@ out_switch(Node *node, int group, char *chname)
 		str = get_chassis_slot(node->chrecord->chassisslot);
 		if (str)
 			fprintf(f, "%s ", str);
-		fprintf(f, "%d Chip %d", node->chrecord->slotnum, node->chrecord->anafanum);
+		fprintf(f, "%d Chip %d", node->chrecord->slotnum,
+			node->chrecord->anafanum);
 	}
 
 	nodename = remap_node_name(node_name_map, node->nodeguid,
-				node->nodedesc);
+				   node->nodedesc);
 
 	fprintf(f, "\nSwitch\t%d %s\t\t# \"%s\" %s port 0 lid %d lmc %d\n",
-		node->numports, node_name(node),
-		nodename,
+		node->numports, node_name(node), nodename,
 		node->smaenhsp0 ? "enhanced" : "base",
 		node->smalid, node->smalmc);
 
@@ -571,7 +583,7 @@ out_ca(Node *node, int group, char *chname)
 	char *node_type;
 	char *node_type2;
 	char *nodename = remap_node_name(node_name_map, node->nodeguid,
-					      node->nodedesc);
+					 node->nodedesc);
 
 	out_ids(node, group, chname);
 	switch(node->type) {
@@ -591,8 +603,7 @@ out_ca(Node *node, int group, char *chname)
 
 	fprintf(f, "%sguid=0x%" PRIx64 "\n", node_type, node->nodeguid);
 	fprintf(f, "%s\t%d %s\t\t# \"%s\"",
-		node_type2, node->numports, node_name(node),
-		nodename);
+		node_type2, node->numports, node_name(node), nodename);
 	if (group && is_xsigo_hca(node->nodeguid))
 		fprintf(f, " (scp)");
 	fprintf(f, "\n");
@@ -627,8 +638,8 @@ out_switch_port(Port *port, int group)
 		fprintf(f, "%s", ext_port_str);
 
 	rem_nodename = remap_node_name(node_name_map,
-				port->remoteport->node->nodeguid,
-				port->remoteport->node->nodedesc);
+				       port->remoteport->node->nodeguid,
+				       port->remoteport->node->nodedesc);
 
 	ext_port_str = out_ext_port(port->remoteport, group);
 	fprintf(f, "\t%s[%d]%s",
@@ -671,8 +682,8 @@ out_ca_port(Port *port, int group)
 		fprintf(f, " (%" PRIx64 ") ", port->remoteport->portguid);
 
 	rem_nodename = remap_node_name(node_name_map,
-				port->remoteport->node->nodeguid,
-				port->remoteport->node->nodedesc);
+				       port->remoteport->node->nodeguid,
+				       port->remoteport->node->nodedesc);
 
 	fprintf(f, "\t\t# lid %d lmc %d \"%s\" lid %d %s%s\n",
 		port->lid, port->lmc, rem_nodename,
@@ -696,7 +707,8 @@ dump_topology(int listtype, int group)
 	if (!listtype) {
 		fprintf(f, "#\n# Topology file: generated on %s#\n", ctime(&t));
 		fprintf(f, "# Max of %d hops discovered\n", maxhops_discovered);
-		fprintf(f, "# Initiated from node %016" PRIx64 " port %016" PRIx64 "\n", mynode->nodeguid, mynode->portguid);
+		fprintf(f, "# Initiated from node %016" PRIx64 " port %016" PRIx64 "\n",
+			mynode->nodeguid, mynode->portguid);
 	}
 
 	/* Make pass on switches */
@@ -1007,8 +1019,10 @@ int main(int argc, char **argv)
 
 	node_name_map = open_node_name_map(node_name_map_file);
 
-	if (discover(&my_portid) < 0)
+	if (discover(&my_portid) < 0) {
+		mad_rpc_close_port(srcport);
 		IBERROR("discover");
+	}
 
 	if (group)
 		chassis = group_nodes();
diff --git a/infiniband-diags/src/ibping.c b/infiniband-diags/src/ibping.c
index 901079f..fb9772c 100644
--- a/infiniband-diags/src/ibping.c
+++ b/infiniband-diags/src/ibping.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
+ * Copyright (c) 2009 HNR Consulting.  All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
@@ -215,22 +216,31 @@ int main(int argc, char **argv)
 		IBERROR("Failed to open '%s' port '%d'", ibd_ca, ibd_ca_port);
 
 	if (server) {
-		if (mad_register_server_via(ping_class, 0, 0, oui, srcport) < 0)
+		if (mad_register_server_via(ping_class, 0, 0, oui, srcport) < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("can't serve class %d on this port", ping_class);
+		}
 
 		get_host_and_domain(host_and_domain, sizeof host_and_domain);
 
-		if ((err = ibping_serv()))
+		if ((err = ibping_serv())) {
+			mad_rpc_close_port(srcport);
 			IBERROR("ibping to %s: %s", portid2str(&portid), err);
+		}
+		mad_rpc_close_port(srcport);
 		exit(0);
 	}
 
-	if (mad_register_client_via(ping_class, 0, srcport) < 0)
+	if (mad_register_client_via(ping_class, 0, srcport) < 0) {
+		mad_rpc_close_port(srcport);
 		IBERROR("can't register ping class %d on this port", ping_class);
+	}
 
 	if (ib_resolve_portid_str_via(&portid, argv[0], ibd_dest_type,
-					ibd_sm_id, srcport) < 0)
+					ibd_sm_id, srcport) < 0) {
+		mad_rpc_close_port(srcport);
 		IBERROR("can't resolve destination port %s", argv[0]);
+	}
 
 	signal(SIGINT, report);
 	signal(SIGTERM, report);
diff --git a/infiniband-diags/src/ibportstate.c b/infiniband-diags/src/ibportstate.c
index 65c9ca1..c23ce84 100644
--- a/infiniband-diags/src/ibportstate.c
+++ b/infiniband-diags/src/ibportstate.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
+ * Copyright (c) 2009 HNR Consulting.  All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
@@ -230,8 +231,10 @@ int main(int argc, char **argv)
 		IBERROR("Failed to open '%s' port '%d'", ibd_ca, ibd_ca_port);
 
 	if (ib_resolve_portid_str_via(&portid, argv[0], ibd_dest_type,
-				ibd_sm_id, srcport) < 0)
+				ibd_sm_id, srcport) < 0) {
+		mad_rpc_close_port(srcport);
 		IBERROR("can't resolve destination port %s", argv[0]);
+	}
 
 	/* First, make sure it is a switch port if it is a "set" */
 	if (argc >= 3) {
@@ -242,24 +245,32 @@ int main(int argc, char **argv)
 		else if (!strcmp(argv[2], "reset"))
 			port_op = 3;
 		else if (!strcmp(argv[2], "speed")) {
-			if (argc < 4)
+			if (argc < 4) {
+				mad_rpc_close_port(srcport);
 				IBERROR("speed requires an additional parameter");
+			}
 			port_op = 4;
 			/* Parse speed value */
 			speed = strtoul(argv[3], 0, 0);
-			if (speed > 15)
+			if (speed > 15) {
+				mad_rpc_close_port(srcport);
 				IBERROR("invalid speed value %d", speed);
+			}
 		}
 	}
 
 	err = get_node_info(&portid, data);
-	if (err < 0)
+	if (err < 0) {
+		mad_rpc_close_port(srcport);
 		IBERROR("smp query nodeinfo failed");
+	}
 	if (err) {		/* not switch */
 		if (port_op == 0)	/* query op */
 			is_switch = 0;
-		else if (port_op != 4)	/* other than speed op */
+		else if (port_op != 4) {	/* other than speed op */
+			mad_rpc_close_port(srcport);
 			IBERROR("smp query nodeinfo: Node type not switch");
+		}
 	}
 
 	if (argc-1 > 0)
@@ -270,8 +281,10 @@ int main(int argc, char **argv)
 	else
 		printf("PortInfo:\n");
 	err = get_port_info(&portid, data, portnum, port_op);
-	if (err < 0)
+	if (err < 0) {
+		mad_rpc_close_port(srcport);
 		IBERROR("smp query portinfo failed");
+	}
 
 	/* Only if one of the "set" options is chosen */
 	if (port_op) {
@@ -287,14 +300,18 @@ int main(int argc, char **argv)
 		}
 
 		err = set_port_info(&portid, data, portnum, port_op);
-		if (err < 0)
+		if (err < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("smp set portinfo failed");
+		}
 
 		if (port_op == 3) {	/* Reset port - so also enable */
 			mad_set_field(data, 0, IB_PORT_PHYS_STATE_F, 2);	/* Polling */
 			err = set_port_info(&portid, data, portnum, port_op);
-			if (err < 0)
+			if (err < 0) {
+				mad_rpc_close_port(srcport);
 				IBERROR("smp set portinfo failed");
+			}
 		}
 	} else {	/* query op */
 		/* only compare peer port if switch port */
@@ -320,23 +337,29 @@ int main(int argc, char **argv)
 
 					/* Set DrSLID to local lid */
 					if (ib_resolve_self_via(&selfportid,
-							&selfport, 0, srcport) < 0)
+							&selfport, 0, srcport) < 0) {
+						mad_rpc_close_port(srcport);
 						IBERROR("could not resolve self");
+					}
 					peerportid.drpath.drslid = (uint16_t) selfportid.lid;
 					peerportid.drpath.drdlid = 0xffff;
 
 					/* Get peer port NodeInfo to obtain peer port number */
 					err = get_node_info(&peerportid, data);
-					if (err < 0)
+					if (err < 0) {
+						mad_rpc_close_port(srcport);
 						IBERROR("smp query nodeinfo failed");
+					}
 
 					mad_decode_field(data, IB_NODE_LOCAL_PORT_F, &peerlocalportnum);
 
 					printf("Peer PortInfo:\n");
 					/* Get peer port characteristics */
 					err = get_port_info(&peerportid, data, peerlocalportnum, port_op);
-					if (err < 0)
+					if (err < 0) {
+						mad_rpc_close_port(srcport);
 						IBERROR("smp query peer portinfofailed");
+					}
 
 					mad_decode_field(data, IB_PORT_LINK_WIDTH_ENABLED_F, &peerlwe );
 					mad_decode_field(data, IB_PORT_LINK_WIDTH_SUPPORTED_F, &peerlws);
diff --git a/infiniband-diags/src/ibroute.c b/infiniband-diags/src/ibroute.c
index 31b8e4c..feab823 100644
--- a/infiniband-diags/src/ibroute.c
+++ b/infiniband-diags/src/ibroute.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
+ * Copyright (c) 2009 HNR Consulting.  All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
@@ -413,19 +414,25 @@ int main(int argc, char **argv)
 		IBERROR("Failed to open '%s' port '%d'", ibd_ca, ibd_ca_port);
 
 	if (!argc) {
-		if (ib_resolve_self_via(&portid, 0, 0, srcport) < 0)
+		if (ib_resolve_self_via(&portid, 0, 0, srcport) < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("can't resolve self addr");
+		}
 	} else if (ib_resolve_portid_str_via(&portid, argv[0], ibd_dest_type,
-			ibd_sm_id, srcport) < 0)
+			ibd_sm_id, srcport) < 0) {
+		mad_rpc_close_port(srcport);
 		IBERROR("can't resolve destination port %s", argv[1]);
+	}
 
 	if (multicast)
 		err = dump_multicast_tables(&portid, startlid, endlid);
 	else
 		err = dump_unicast_tables(&portid, startlid, endlid);
 
-	if (err)
+	if (err) {
+		mad_rpc_close_port(srcport);
 		IBERROR("dump tables: %s", err);
+	}
 
 	mad_rpc_close_port(srcport);
 	exit(0);
diff --git a/infiniband-diags/src/ibsendtrap.c b/infiniband-diags/src/ibsendtrap.c
index f2b3e67..72aa3f4 100644
--- a/infiniband-diags/src/ibsendtrap.c
+++ b/infiniband-diags/src/ibsendtrap.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2008 Lawrence Livermore National Security
+ * Copyright (c) 2009 HNR Consulting.  All rights reserved.
  *
  * Produced at Lawrence Livermore National Laboratory.
  * Written by Ira Weiny <weiny2 at llnl.gov>.
@@ -57,11 +58,15 @@ static int send_144_node_desc_update(void)
 	ib_rpc_t trap_rpc;
 	ib_mad_notice_attr_t notice;
 
-	if (ib_resolve_self_via(&selfportid, &selfport, NULL, srcport))
+	if (ib_resolve_self_via(&selfportid, &selfport, NULL, srcport)) {
+		mad_rpc_close_port(srcport);
 		IBERROR("can't resolve self");
+	}
 
-	if (ib_resolve_smlid_via(&sm_port, 0, srcport))
+	if (ib_resolve_smlid_via(&sm_port, 0, srcport)) {
+		mad_rpc_close_port(srcport);
 		IBERROR("can't resolve SM destination port");
+	}
 
 	memset(&trap_rpc, 0, sizeof(trap_rpc));
 	trap_rpc.mgtclass = IB_SMI_CLASS;
diff --git a/infiniband-diags/src/ibsysstat.c b/infiniband-diags/src/ibsysstat.c
index 03c0515..738b1ab 100644
--- a/infiniband-diags/src/ibsysstat.c
+++ b/infiniband-diags/src/ibsysstat.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
+ * Copyright (c) 2009 HNR Consulting.  All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
@@ -230,19 +231,25 @@ static char *ibsystat(ib_portid_t *portid, int attr)
 	if (!portid->qkey)
 		portid->qkey = IB_DEFAULT_QP1_QKEY;
 
-	if ((len = mad_build_pkt(buf, &rpc, portid, NULL, NULL)) < 0)
+	if ((len = mad_build_pkt(buf, &rpc, portid, NULL, NULL)) < 0) {
+		mad_rpc_close_port(srcport);
 		IBPANIC("cannot build packet.");
+	}
 
 	fd = mad_rpc_portid(srcport);
 	agent = mad_rpc_class_agent(srcport, rpc.mgtclass);
 	timeout = ibd_timeout ? ibd_timeout : MAD_DEF_TIMEOUT_MS;
 
-	if (umad_send(fd, agent, buf, len, timeout, 0) < 0)
+	if (umad_send(fd, agent, buf, len, timeout, 0) < 0) {
+		mad_rpc_close_port(srcport);
 		IBPANIC("umad_send failed.");
+	}
 
 	len = sizeof(buf) - umad_size();
-	if (umad_recv(fd, buf, &len, timeout) < 0)
+	if (umad_recv(fd, buf, &len, timeout) < 0) {
+		mad_rpc_close_port(srcport);
 		IBPANIC("umad_recv failed.");
+	}
 
 	if (umad_status(buf))
 		return strerror(umad_status(buf));
@@ -342,25 +349,36 @@ int main(int argc, char **argv)
 		IBERROR("Failed to open '%s' port '%d'", ibd_ca, ibd_ca_port);
 
 	if (server) {
-		if (mad_register_server_via(sysstat_class, 1, 0, oui, srcport) < 0)
+		if (mad_register_server_via(sysstat_class, 1, 0, oui, srcport) < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("can't serve class %d", sysstat_class);
+		}
 
 		host_ncpu = build_cpuinfo();
 
-		if ((err = ibsystat_serv()))
+		if ((err = ibsystat_serv())) {
+			mad_rpc_close_port(srcport);
 			IBERROR("ibssystat to %s: %s", portid2str(&portid), err);
+		}
+		mad_rpc_close_port(srcport);
 		exit(0);
 	}
 
-	if (mad_register_client_via(sysstat_class, 1, srcport) < 0)
+	if (mad_register_client_via(sysstat_class, 1, srcport) < 0) {
+		mad_rpc_close_port(srcport);
 		IBERROR("can't register to sysstat class %d", sysstat_class);
+	}
 
 	if (ib_resolve_portid_str_via(&portid, argv[0], ibd_dest_type,
-			ibd_sm_id, srcport) < 0)
+				      ibd_sm_id, srcport) < 0) {
+		mad_rpc_close_port(srcport);
 		IBERROR("can't resolve destination port %s", argv[0]);
+	}
 
-	if ((err = ibsystat(&portid, attr)))
+	if ((err = ibsystat(&portid, attr))) {
+		mad_rpc_close_port(srcport);
 		IBERROR("ibsystat to %s: %s", portid2str(&portid), err);
+	}
 
 	mad_rpc_close_port(srcport);
 	exit(0);
diff --git a/infiniband-diags/src/ibtracert.c b/infiniband-diags/src/ibtracert.c
index a887157..ba0c1e6 100644
--- a/infiniband-diags/src/ibtracert.c
+++ b/infiniband-diags/src/ibtracert.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
+ * Copyright (c) 2009 HNR Consulting.  All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
@@ -490,11 +491,15 @@ find_mcpath(ib_portid_t *from, int mlid)
 
 	DEBUG("from %s", portid2str(from));
 
-	if (!(node = calloc(1, sizeof(Node))))
+	if (!(node = calloc(1, sizeof(Node)))) {
+		mad_rpc_close_port(srcport);
 		IBERROR("out of memory");
+	}
 
-	if (!(port = calloc(1, sizeof(Port))))
+	if (!(port = calloc(1, sizeof(Port)))) {
+		mad_rpc_close_port(srcport);
 		IBERROR("out of memory");
+	}
 
 	if (get_node(node, port, from) < 0) {
 		IBWARN("can't reach node %s", portid2str(from));
@@ -554,8 +559,10 @@ find_mcpath(ib_portid_t *from, int mlid)
 					if (from->drpath.cnt > 0)
 						path->drpath.cnt--;
 				} else {
-					if (!(port = calloc(1, sizeof(Port))))
+					if (!(port = calloc(1, sizeof(Port)))) {
+						mad_rpc_close_port(srcport);
 						IBERROR("out of memory");
+					}
 
 					if (get_port(port, i, path) < 0) {
 						IBWARN("can't reach node %s port %d", portid2str(path), i);
@@ -575,11 +582,15 @@ find_mcpath(ib_portid_t *from, int mlid)
 						return 0;
 				}
 
-				if (!(remotenode = calloc(1, sizeof(Node))))
+				if (!(remotenode = calloc(1, sizeof(Node)))) {
+					mad_rpc_close_port(srcport);
 					IBERROR("out of memory");
+				}
 
-				if (!(remoteport = calloc(1, sizeof(Port))))
+				if (!(remoteport = calloc(1, sizeof(Port)))) {
+					mad_rpc_close_port(srcport);
 					IBERROR("out of memory");
+				}
 
 				if (get_node(remotenode, remoteport, path) < 0) {
 					IBWARN("NodeInfo on %s port %d failed, skipping port",
@@ -757,20 +768,28 @@ int main(int argc, char **argv)
 	node_name_map = open_node_name_map(node_name_map_file);
 
 	if (ib_resolve_portid_str_via(&src_portid, argv[0], ibd_dest_type,
-			ibd_sm_id, srcport) < 0)
+				      ibd_sm_id, srcport) < 0) {
+		mad_rpc_close_port(srcport);
 		IBERROR("can't resolve source port %s", argv[0]);
+	}
 
 	if (ib_resolve_portid_str_via(&dest_portid, argv[1], ibd_dest_type,
-			ibd_sm_id, srcport) < 0)
+				      ibd_sm_id, srcport) < 0) {
+		mad_rpc_close_port(srcport);
 		IBERROR("can't resolve destination port %s", argv[1]);
+	}
 
 	if (ibd_dest_type == IB_DEST_DRPATH) {
-		if (resolve_lid(&src_portid, NULL) < 0)
+		if (resolve_lid(&src_portid, NULL) < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("cannot resolve lid for port \'%s\'",
 				portid2str(&src_portid));
-		if (resolve_lid(&dest_portid, NULL) < 0)
+		}
+		if (resolve_lid(&dest_portid, NULL) < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("cannot resolve lid for port \'%s\'",
 				portid2str(&dest_portid));
+		}
 	}
 
 	if (dest_portid.lid == 0 || src_portid.lid == 0) {
@@ -780,26 +799,35 @@ int main(int argc, char **argv)
 
 	if (ibd_dest_type != IB_DEST_DRPATH) {
 		/* first find a direct path to the src port */
-		if (find_route(&my_portid, &src_portid, 0) < 0)
+		if (find_route(&my_portid, &src_portid, 0) < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("can't find a route to the src port");
+		}
 
 		src_portid = my_portid;
 	}
 
 	if (!multicast) {
-		if (find_route(&src_portid, &dest_portid, dumplevel) < 0)
+		if (find_route(&src_portid, &dest_portid, dumplevel) < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("can't find a route from src to dest");
+		}
+		mad_rpc_close_port(srcport);
 		exit(0);
 	} else {
 		if (mlid < 0xc000)
 			IBWARN("invalid MLID; must be 0xc000 or larger");
 	}
 
-	if (!(target_portguid = find_target_portguid(&dest_portid)))
+	if (!(target_portguid = find_target_portguid(&dest_portid))) {
+		mad_rpc_close_port(srcport);
 		IBERROR("can't reach target lid %d", dest_portid.lid);
+	}
 
-	if (!(endnode = find_mcpath(&src_portid, mlid)))
+	if (!(endnode = find_mcpath(&src_portid, mlid))) {
+		mad_rpc_close_port(srcport);
 		IBERROR("can't find a multicast route from src to dest");
+	}
 
 	/* dump multicast path */
 	dump_mcpath(endnode, dumplevel);
diff --git a/infiniband-diags/src/perfquery.c b/infiniband-diags/src/perfquery.c
index d35e995..98b7642 100644
--- a/infiniband-diags/src/perfquery.c
+++ b/infiniband-diags/src/perfquery.c
@@ -1,6 +1,7 @@
 /*
  * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
  * Copyright (c) 2007 Xsigo Systems Inc.  All rights reserved.
+ * Copyright (c) 2009 HNR Consulting.  All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
@@ -262,7 +263,7 @@ static void output_aggregate_perfcounters_ext(ib_portid_t *portid)
 
 	mad_dump_perfcounters_ext(buf, sizeof buf, pc, sizeof pc);
 
-	printf("# Port counters: %s port %d\n%s", portid2str(portid), ALL_PORTS, buf);
+	printf("# Port extended counters: %s port %d\n%s", portid2str(portid), ALL_PORTS, buf);
 }
 
 static void dump_perfcounters(int extended, int timeout, uint16_t cap_mask,
@@ -272,10 +273,13 @@ static void dump_perfcounters(int extended, int timeout, uint16_t cap_mask,
 
 	if (extended != 1) {
 		if (!pma_query_via(pc, portid, port, timeout,
-				   IB_GSI_PORT_COUNTERS, srcport))
+				   IB_GSI_PORT_COUNTERS, srcport)) {
+			mad_rpc_close_port(srcport);
 			IBERROR("perfquery");
+		}
 		if (!(cap_mask & 0x1000)) {
 			/* if PortCounters:PortXmitWait not suppported clear this counter */
+			IBWARN("PortXmitWait not indicated so ignore this counter");
 			perf_count.xmtwait = 0;
 			mad_encode_field(pc, IB_PC_XMT_WAIT_F, &perf_count.xmtwait);
 		}
@@ -285,31 +289,44 @@ static void dump_perfcounters(int extended, int timeout, uint16_t cap_mask,
 			mad_dump_perfcounters(buf, sizeof buf, pc, sizeof pc);
 	} else {
 		if (!(cap_mask & 0x200)) /* 1.2 errata: bit 9 is extended counter support */
-			IBWARN("PerfMgt ClassPortInfo 0x%x extended counters not indicated\n", cap_mask);
+			IBWARN("PerfMgt ClassPortInfo 0x%x extended counters not indicated", cap_mask);
 
 		if (!pma_query_via(pc, portid, port, timeout,
-				   IB_GSI_PORT_COUNTERS_EXT, srcport))
+				   IB_GSI_PORT_COUNTERS_EXT, srcport)) {
+			mad_rpc_close_port(srcport);
 			IBERROR("perfextquery");
+		}
 		if (aggregate)
 			aggregate_perfcounters_ext();
 		else
 			mad_dump_perfcounters_ext(buf, sizeof buf, pc, sizeof pc);
 	}
 
-	if (!aggregate)
-		printf("# Port counters: %s port %d\n%s", portid2str(portid), port, buf);
+	if (!aggregate) {
+		if (extended)
+			printf("# Port extended counters: %s port %d\n%s",
+			       portid2str(portid), port, buf);
+		else
+			printf("# Port counters: %s port %d\n%s",
+			       portid2str(portid), port, buf);
+	}
 }
 
-static void reset_counters(int extended, int timeout, int mask, ib_portid_t *portid, int port)
+static void reset_counters(int extended, int timeout, int mask,
+			   ib_portid_t *portid, int port)
 {
 	if (extended != 1) {
 		if (!performance_reset_via(pc, portid, port, mask, timeout,
-					   IB_GSI_PORT_COUNTERS, srcport))
+					   IB_GSI_PORT_COUNTERS, srcport)) {
+			mad_rpc_close_port(srcport);
 			IBERROR("perf reset");
+		}
 	} else {
 		if (!performance_reset_via(pc, portid, port, mask, timeout,
-					   IB_GSI_PORT_COUNTERS_EXT, srcport))
+					   IB_GSI_PORT_COUNTERS_EXT, srcport)) {
+			mad_rpc_close_port(srcport);
 			IBERROR("perf ext reset");
+		}
 	}
 }
 
@@ -321,22 +338,28 @@ void xmt_sl_query(ib_portid_t *portid, int port, int mask)
 
 	if (reset_only) {
 		if (!performance_reset_via(pc, portid, port, mask, ibd_timeout,
-						IB_GSI_PORT_XMIT_DATA_SL, srcport))
+					   IB_GSI_PORT_XMIT_DATA_SL, srcport)) {
+			mad_rpc_close_port(srcport);
 			IBERROR("perfslreset");
+		}
 		return;
 	}
 
 	if (!pma_query_via(pc, portid, port, ibd_timeout,
-				IB_GSI_PORT_XMIT_DATA_SL, srcport))
+			   IB_GSI_PORT_XMIT_DATA_SL, srcport)) {
+		mad_rpc_close_port(srcport);
 		IBERROR("perfslquery");
+	}
 
 	mad_dump_perfcounters_xmt_sl(buf, sizeof buf, pc, sizeof pc);
 	printf("# Port counters: %s port %d\n%s", portid2str(portid), port, buf);
 
 	if(reset)
 		if (!performance_reset_via(pc, portid, port, mask, ibd_timeout,
-						IB_GSI_PORT_XMIT_DATA_SL, srcport))
+					   IB_GSI_PORT_XMIT_DATA_SL, srcport)) {
+			mad_rpc_close_port(srcport);
 			IBERROR("perfslreset");
+		}
 }
 
 void rcv_sl_query(ib_portid_t *portid, int port, int mask)
@@ -345,22 +368,28 @@ void rcv_sl_query(ib_portid_t *portid, int port, int mask)
 
 	if (reset_only) {
 		if (!performance_reset_via(pc, portid, port, mask, ibd_timeout,
-						IB_GSI_PORT_RCV_DATA_SL, srcport))
+					   IB_GSI_PORT_RCV_DATA_SL, srcport)) {
+			mad_rpc_close_port(srcport);
 			IBERROR("perfslreset");
+		}
 		return;
 	}
 
 	if (!pma_query_via(pc, portid, port, ibd_timeout,
-				IB_GSI_PORT_RCV_DATA_SL, srcport))
+			   IB_GSI_PORT_RCV_DATA_SL, srcport)) {
+		mad_rpc_close_port(srcport);
 		IBERROR("perfslquery");
+	}
 
 	mad_dump_perfcounters_rcv_sl(buf, sizeof buf, pc, sizeof pc);
 	printf("# Port counters: %s port %d\n%s", portid2str(portid), port, buf);
 
 	if(reset)
 		if (!performance_reset_via(pc, portid, port, mask, ibd_timeout,
-						IB_GSI_PORT_RCV_DATA_SL, srcport))
+					   IB_GSI_PORT_RCV_DATA_SL, srcport)) {
+			mad_rpc_close_port(srcport);
 			IBERROR("perfslreset");
+		}
 }
 
 static int process_opt(void *context, int ch, char *optarg)
@@ -396,7 +425,8 @@ static int process_opt(void *context, int ch, char *optarg)
 
 int main(int argc, char **argv)
 {
-	int mgmt_classes[4] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS, IB_PERFORMANCE_CLASS};
+	int mgmt_classes[4] = {IB_SMI_CLASS, IB_SMI_DIRECT_CLASS, IB_SA_CLASS,
+			       IB_PERFORMANCE_CLASS};
 	ib_portid_t portid = {0};
 	int mask = 0xffff;
 	uint16_t cap_mask;
@@ -450,23 +480,31 @@ int main(int argc, char **argv)
 
 	if (argc) {
 		if (ib_resolve_portid_str_via(&portid, argv[0], ibd_dest_type,
-				ibd_sm_id, srcport) < 0)
+					      ibd_sm_id, srcport) < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("can't resolve destination port %s", argv[0]);
+		}
 	} else {
-		if (ib_resolve_self_via(&portid, &port, 0, srcport) < 0)
+		if (ib_resolve_self_via(&portid, &port, 0, srcport) < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("can't resolve self port %s", argv[0]);
+		}
 	}
 
 	/* PerfMgt ClassPortInfo is a required attribute */
 	if (!pma_query_via(pc, &portid, port, ibd_timeout, CLASS_PORT_INFO,
-			   srcport))
+			   srcport)) {
+		mad_rpc_close_port(srcport);
 		IBERROR("classportinfo query");
+	}
 	/* ClassPortInfo should be supported as part of libibmad */
 	memcpy(&cap_mask, pc + 2, sizeof(cap_mask));	/* CapabilityMask */
 	cap_mask = ntohs(cap_mask);
 	if (!(cap_mask & 0x100)) { /* bit 8 is AllPortSelect */
-		if (!all_ports && port == ALL_PORTS)
+		if (!all_ports && port == ALL_PORTS) {
+			mad_rpc_close_port(srcport);
 			IBERROR("AllPortSelect not supported");
+		}
 		if (all_ports)
 			all_ports_loop = 1;
 	}
@@ -483,17 +521,23 @@ int main(int argc, char **argv)
 
 	if (all_ports_loop || (loop_ports && (all_ports || port == ALL_PORTS))) {
 		if (smp_query_via(data, &portid, IB_ATTR_NODE_INFO, 0, 0,
-				srcport) < 0)
+				  srcport) < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("smp query nodeinfo failed");
+		}
 		node_type = mad_get_field(data, 0, IB_NODE_TYPE_F);
 		mad_decode_field(data, IB_NODE_NPORTS_F, &num_ports);
-		if (!num_ports)
+		if (!num_ports) {
+			mad_rpc_close_port(srcport);
 			IBERROR("smp query nodeinfo: num ports invalid");
+		}
 
 		if (node_type == IB_NODE_SWITCH) {
 			if (smp_query_via(data, &portid, IB_ATTR_SWITCH_INFO,
-					0, 0, srcport) < 0)
+					  0, 0, srcport) < 0) {
+				mad_rpc_close_port(srcport);
 				IBERROR("smp query nodeinfo failed");
+			}
 			enhancedport0 = mad_get_field(data, 0, IB_SW_ENHANCED_PORT0_F);
 			if (enhancedport0)
 				start_port = 0;
@@ -523,15 +567,13 @@ int main(int argc, char **argv)
 		goto done;
 
 do_reset:
-
 	if (argc <= 2 && !extended && (cap_mask & 0x1000))
 		mask |= (1<<16); /* reset portxmitwait */
 
 	if (all_ports_loop || (loop_ports && (all_ports || port == ALL_PORTS))) {
 		for (i = start_port; i <= num_ports; i++)
 			reset_counters(extended, ibd_timeout, mask, &portid, i);
-	}
-	else
+	} else
 		reset_counters(extended, ibd_timeout, mask, &portid, port);
 
 done:
diff --git a/infiniband-diags/src/saquery.c b/infiniband-diags/src/saquery.c
index dddebc1..e5f1287 100644
--- a/infiniband-diags/src/saquery.c
+++ b/infiniband-diags/src/saquery.c
@@ -3,6 +3,7 @@
  * Copyright (c) 2004-2008 Voltaire, Inc. All rights reserved.
  * Copyright (c) 2002-2005 Mellanox Technologies LTD. All rights reserved.
  * Copyright (c) 1996-2003 Intel Corporation. All rights reserved.
+ * Copyright (c) 2009 HNR Consulting. All rights reserved.
  *
  * Produced at Lawrence Livermore National Laboratory.
  * Written by Ira Weiny <weiny2 at llnl.gov>.
@@ -99,6 +100,7 @@ struct query_cmd {
 static char *node_name_map_file = NULL;
 static nn_map_t *node_name_map = NULL;
 static uint64_t smkey = 1;
+static struct ibmad_port *srcport;
 
 /**
  * Declare some globals because I don't want this to be too complex.
@@ -141,8 +143,10 @@ static int sa_query(struct bind_handle *h, uint8_t method,
 	rpc.dataoffs = IB_SA_DATA_OFFS;
 
 	umad = calloc(1, len + umad_size());
-	if (!umad)
+	if (!umad) {
+		mad_rpc_close_port(srcport);
 		IBPANIC("cannot alloc mem for umad: %s\n", strerror(errno));
+	}
 
 	mad_build_pkt(umad, &rpc, &h->dport, NULL, data);
 
@@ -152,9 +156,11 @@ static int sa_query(struct bind_handle *h, uint8_t method,
 		xdump(stdout, "SA Request:\n", umad_get_mad(umad), len);
 
 	ret = umad_send(h->fd, h->agent, umad, len, ibd_timeout, 0);
-	if (ret < 0)
+	if (ret < 0) {
+		mad_rpc_close_port(srcport);
 		IBPANIC("umad_send failed: attr %u: %s\n",
 			attr, strerror(errno));
+	}
 
 recv_mad:
 	ret = umad_recv(h->fd, umad, &len, ibd_timeout);
@@ -163,6 +169,7 @@ recv_mad:
 			umad = realloc(umad, umad_size() + len);
 			goto recv_mad;
 		}
+		mad_rpc_close_port(srcport);
 		IBPANIC("umad_recv failed: attr %u: %s\n", attr,
 			strerror(errno));
 	}
@@ -1324,7 +1331,6 @@ static int query_mft_records(const struct query_cmd *q, bind_handle_t h,
 
 static bind_handle_t get_bind_handle(void)
 {
-	static struct ibmad_port *srcport;
 	static struct bind_handle handle;
 	int mgmt_classes[2] = { IB_SMI_CLASS, IB_SMI_DIRECT_CLASS };
 
@@ -1333,8 +1339,10 @@ static bind_handle_t get_bind_handle(void)
 		IBERROR("Failed to open '%s' port '%d'", ibd_ca, ibd_ca_port);
 
 	ib_resolve_smlid_via(&handle.dport, ibd_timeout, srcport);
-	if (!handle.dport.lid)
+	if (!handle.dport.lid) {
+		mad_rpc_close_port(srcport);
 		IBPANIC("No SM found.");
+	}
 
 	handle.dport.qp = 1;
 	if (!handle.dport.qkey)
diff --git a/infiniband-diags/src/sminfo.c b/infiniband-diags/src/sminfo.c
index ebf6a47..f987445 100644
--- a/infiniband-diags/src/sminfo.c
+++ b/infiniband-diags/src/sminfo.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
+ * Copyright (c) 2009 HNR Consulting.  All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
@@ -121,11 +122,15 @@ int main(int argc, char **argv)
 
 	if (argc) {
 		if (ib_resolve_portid_str_via(&portid, argv[0], ibd_dest_type,
-				0, srcport) < 0)
+					      0, srcport) < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("can't resolve destination port %s", argv[0]);
+		}
 	} else {
-		if (ib_resolve_smlid_via(&portid, ibd_timeout, srcport) < 0)
+		if (ib_resolve_smlid_via(&portid, ibd_timeout, srcport) < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("can't resolve sm port %s", argv[0]);
+		}
 	}
 
 	mad_encode_field(sminfo, IB_SMINFO_GUID_F, &guid);
@@ -136,12 +141,15 @@ int main(int argc, char **argv)
 
 	if (mod) {
 		if (!(p = smp_set_via(sminfo, &portid, IB_ATTR_SMINFO, mod,
-				ibd_timeout, srcport)))
-			IBERROR("query");
-	} else
-		if (!(p = smp_query_via(sminfo, &portid, IB_ATTR_SMINFO, 0,
-				ibd_timeout, srcport)))
+				      ibd_timeout, srcport))) {
+			mad_rpc_close_port(srcport);
 			IBERROR("query");
+		}
+	} else if (!(p = smp_query_via(sminfo, &portid, IB_ATTR_SMINFO, 0,
+				       ibd_timeout, srcport))) {
+		mad_rpc_close_port(srcport);
+		IBERROR("query");
+	}
 
 	mad_decode_field(sminfo, IB_SMINFO_GUID_F, &guid);
 	mad_decode_field(sminfo, IB_SMINFO_ACT_F, &act);
diff --git a/infiniband-diags/src/smpquery.c b/infiniband-diags/src/smpquery.c
index 2ed1e65..dc6f0fa 100644
--- a/infiniband-diags/src/smpquery.c
+++ b/infiniband-diags/src/smpquery.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
+ * Copyright (c) 2009 HNR Consulting.  All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
@@ -460,20 +461,28 @@ int main(int argc, char **argv)
 
 	if (ibd_dest_type != IB_DEST_DRSLID) {
 		if (ib_resolve_portid_str_via(&portid, argv[1], ibd_dest_type,
-				ibd_sm_id, srcport) < 0)
+					      ibd_sm_id, srcport) < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("can't resolve destination port %s", argv[1]);
-		if ((err = fn(&portid, argv+2, argc-2)))
+		}
+		if ((err = fn(&portid, argv+2, argc-2))) {
+			mad_rpc_close_port(srcport);
 			IBERROR("operation %s: %s", argv[0], err);
+		}
 	} else {
 		char concat[64];
 
 		memset(concat, 0, 64);
 		snprintf(concat, sizeof(concat), "%s %s", argv[1], argv[2]);
 		if (ib_resolve_portid_str_via(&portid, concat, ibd_dest_type,
-				ibd_sm_id, srcport) < 0)
+					      ibd_sm_id, srcport) < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("can't resolve destination port %s", concat);
-		if ((err = fn(&portid, argv+3, argc-3)))
+		}
+		if ((err = fn(&portid, argv+3, argc-3))) {
+			mad_rpc_close_port(srcport);
 			IBERROR("operation %s: %s", argv[0], err);
+		}
 	}
 	close_node_name_map(node_name_map);
 	mad_rpc_close_port(srcport);
diff --git a/infiniband-diags/src/vendstat.c b/infiniband-diags/src/vendstat.c
index 0f09039..b4e3825 100644
--- a/infiniband-diags/src/vendstat.c
+++ b/infiniband-diags/src/vendstat.c
@@ -1,5 +1,6 @@
 /*
  * Copyright (c) 2004-2008 Voltaire Inc.  All rights reserved.
+ * Copyright (c) 2009 HNR Consulting.  All rights reserved.
  *
  * This software is available to you under a choice of one of two
  * licenses.  You may choose to be licensed under the terms of the GNU
@@ -160,17 +161,23 @@ int main(int argc, char **argv)
 
 	if (argc) {
 		if (ib_resolve_portid_str_via(&portid, argv[0], ibd_dest_type,
-				ibd_sm_id, srcport) < 0)
+					      ibd_sm_id, srcport) < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("can't resolve destination port %s", argv[0]);
+		}
 	} else {
-		if (ib_resolve_self_via(&portid, &port, 0, srcport) < 0)
+		if (ib_resolve_self_via(&portid, &port, 0, srcport) < 0) {
+			mad_rpc_close_port(srcport);
 			IBERROR("can't resolve self port %s", argv[0]);
+		}
 	}
 
 	/* Only General Info and Port Xmit Wait Counters */
 	/* queries are currently supported */
-	if (!general_info && !xmit_wait)
+	if (!general_info && !xmit_wait) {
+		mad_rpc_close_port(srcport);
 		IBERROR("at least one of -N and -w must be specified");
+	}
 
 	/* These are Mellanox specific vendor MADs */
 	/* but vendors change the VendorId so how know for sure ? */
@@ -185,13 +192,18 @@ int main(int argc, char **argv)
 	memset(&buf, 0, sizeof(buf));
 	/* vendor ClassPortInfo is required attribute if class supported */
 	call.attrid = CLASS_PORT_INFO;
-	if (!ib_vendor_call_via(&buf, &portid, &call, srcport))
+	if (!ib_vendor_call_via(&buf, &portid, &call, srcport)) {
+		mad_rpc_close_port(srcport);
 		IBERROR("classportinfo query");
+	}
 
 	memset(&buf, 0, sizeof(buf));
 	call.attrid = IB_MLX_IS3_GENERAL_INFO;
-	if (!ib_vendor_call_via(&buf, &portid, &call, srcport))
+	if (!ib_vendor_call_via(&buf, &portid, &call, srcport)) {
+		mad_rpc_close_port(srcport);
 		IBERROR("vendstat");
+	}
+
 	gi = (is3_general_info_t *)&buf;
 
 	if (general_info) {
@@ -211,8 +223,10 @@ int main(int argc, char **argv)
 	}
 
 	if (xmit_wait) {
-		if (ntohs(gi->hw_info.device_id) != IS3_DEVICE_ID)
+		if (ntohs(gi->hw_info.device_id) != IS3_DEVICE_ID) {
+			mad_rpc_close_port(srcport);
 			IBERROR("Unsupported device ID 0x%x", ntohs(gi->hw_info.device_id));
+		}
 
 		memset(&buf, 0, sizeof(buf));
 		call.attrid = IB_MLX_IS3_CONFIG_SPACE_ACCESS;
@@ -222,8 +236,10 @@ int main(int argc, char **argv)
 		cs = (is3_config_space_t *)&buf;
 		for (i = 0; i < 16; i++)
 			cs->record[i].address = htonl(IB_MLX_IS3_PORT_XMIT_WAIT + ((i + 1) << 12));
-		if (!ib_vendor_call_via(&buf, &portid, &call, srcport))
+		if (!ib_vendor_call_via(&buf, &portid, &call, srcport)) {
+			mad_rpc_close_port(srcport);
 			IBERROR("vendstat");
+		}
 
 		for (i = 0; i < 16; i++)
 			if (cs->record[i].data)	/* PortXmitWait is 32 bit counter */
@@ -237,8 +253,10 @@ int main(int argc, char **argv)
 		cs = (is3_config_space_t *)&buf;
 		for (i = 0; i < 8; i++)
 			cs->record[i].address = htonl(IB_MLX_IS3_PORT_XMIT_WAIT + ((i + 17) << 12));
-		if (!ib_vendor_call_via(&buf, &portid, &call, srcport))
+		if (!ib_vendor_call_via(&buf, &portid, &call, srcport)) {
+			mad_rpc_close_port(srcport);
 			IBERROR("vendstat");
+		}
 
 		for (i = 0; i < 8; i++)
 			if (cs->record[i].data) /* PortXmitWait is 32 bit counter */



More information about the general mailing list