Blob Blame History Raw
From 81df977dc0c2a04d5fa0c18dee130490d84a92f5 Mon Sep 17 00:00:00 2001
From: Hangbin Liu <haliu@redhat.com>
Date: Thu, 20 Dec 2018 11:48:16 +0800
Subject: [PATCH 03/04] libndp: close sockfd after using to avoid handle leak

Fixes: acccd780df517 ("ndptool: add -T target support")
Signed-off-by: Hangbin Liu <haliu@redhat.com>
Signed-off-by: Jiri Pirko <jiri@mellanox.com>
---
 libndp/libndp.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/libndp/libndp.c b/libndp/libndp.c
index bec55d4..f327d45 100644
--- a/libndp/libndp.c
+++ b/libndp/libndp.c
@@ -736,7 +736,7 @@ void ndp_msg_target_set(struct ndp_msg *msg, struct in6_addr *target)
 
 static int ndp_get_iface_mac(int ifindex, char *ptr)
 {
-	int sockfd;
+	int sockfd, err = 0;
 	struct ifreq ifr;
 
 	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
@@ -747,17 +747,21 @@ static int ndp_get_iface_mac(int ifindex, char *ptr)
 
 	if (if_indextoname(ifindex, (char *)&ifr.ifr_name) == NULL) {
 		pr_err("%s: Failed to get iface name with index %d", __func__, ifindex);
-		return -errno;
+		err = -errno;
+		goto close_sock;
 	}
 
 	if (ioctl(sockfd, SIOCGIFHWADDR, &ifr) < 0) {
 		pr_err("%s: Failed to get iface mac with index %d\n", __func__, ifindex);
-		return -errno;
+		err = -errno;
+		goto close_sock;
 	}
 
 	memcpy(ptr, &ifr.ifr_hwaddr.sa_data, sizeof(ifr.ifr_hwaddr.sa_data));
 
-	return 0;
+close_sock:
+	close(sockfd);
+	return err;
 }
 
 static void ndp_msg_opt_set_linkaddr(struct ndp_msg *msg, int ndp_opt)
-- 
2.19.2