[SLOF] [PATCH v2 01/10] libnet: Make sure to close sockets when we're done

Thomas Huth thuth at redhat.com
Tue Sep 20 05:08:23 AEST 2016


If libnet is linked to the net-snk, it does not matter since the
whole stack is completely reloaded each time. But if we are
linking libnet to Paflof, we've got to make sure to properly
release the resources that we've allocated before, since the
code and data stays in memory.

Signed-off-by: Thomas Huth <thuth at redhat.com>
---
 lib/libnet/netload.c | 6 ++++++
 lib/libnet/ping.c    | 4 ++++
 2 files changed, 10 insertions(+)

diff --git a/lib/libnet/netload.c b/lib/libnet/netload.c
index d99aa29..7ded3e4 100644
--- a/lib/libnet/netload.c
+++ b/lib/libnet/netload.c
@@ -10,6 +10,7 @@
  *     IBM Corporation - initial implementation
  *****************************************************************************/
 
+#include <unistd.h>
 #include <tftp.h>
 #include <ethernet.h>
 #include <dhcp.h>
@@ -566,6 +567,7 @@ netboot(int argc, char *argv[])
 		bootmsg_error(0x3001, &buf[7]);
 
 		write_mm_log(buf, strlen(buf), 0x91);
+		close(fn_ip.fd);
 		return -101;
 	}
 
@@ -590,6 +592,7 @@ netboot(int argc, char *argv[])
 		bootmsg_error(0x3002, &buf[7]);
 
 		write_mm_log(buf, strlen(buf), 0x91);
+		close(fn_ip.fd);
 		return -102;
 	}
 	if (rc == -4 || rc == -3) {
@@ -597,6 +600,7 @@ netboot(int argc, char *argv[])
 		bootmsg_error(0x3008, &buf[7]);
 
 		write_mm_log(buf, strlen(buf), 0x91);
+		close(fn_ip.fd);
 		return -107;
 	}
 
@@ -635,6 +639,8 @@ netboot(int argc, char *argv[])
 	if(obp_tftp_args.ip_init == IP_INIT_DHCP)
 		dhcp_send_release(fn_ip.fd);
 
+	close(fn_ip.fd);
+
 	if (rc > 0) {
 		printf("  TFTP: Received %s (%d KBytes)\n", fn_ip.filename,
 		       rc / 1024);
diff --git a/lib/libnet/ping.c b/lib/libnet/ping.c
index def3179..2b6a1d7 100644
--- a/lib/libnet/ping.c
+++ b/lib/libnet/ping.c
@@ -10,6 +10,7 @@
  *     IBM Corporation - initial implementation
  *****************************************************************************/
 
+#include <unistd.h>
 #include <ipv4.h>
 #include <dhcp.h>
 #include <ethernet.h>
@@ -160,6 +161,7 @@ ping(int argc, char *argv[])
 
 		if (arp_failed == -1) {
 			printf("\n  DHCP: Could not get ip address\n");
+			close(fn_ip.fd);
 			return -1;
 		}
 
@@ -206,10 +208,12 @@ ping(int argc, char *argv[])
 		receive_ether(fd_device);
 		if(pong_ipv4() == 0) {
 			printf("success\n");
+			close(fn_ip.fd);
 			return 0;
 		}
 	}
 
 	printf("failed\n");
+	close(fn_ip.fd);
 	return -1;
 }
-- 
1.8.3.1



More information about the SLOF mailing list