diff mbox series

[18/72] libmultipath: replace_wwids(): fix possible fd leak

Message ID 20191012212703.12989-19-martin.wilck@suse.com (mailing list archive)
State Not Applicable, archived
Delegated to: christophe varoqui
Headers show
Series multipath-tools: cleanup and warning enablement | expand

Commit Message

Martin Wilck Oct. 12, 2019, 9:27 p.m. UTC
From: Martin Wilck <mwilck@suse.com>

... in case the thread is cancelled.

Signed-off-by: Martin Wilck <mwilck@suse.com>
---
 libmultipath/wwids.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/libmultipath/wwids.c b/libmultipath/wwids.c
index ef748125..f69ed6f5 100644
--- a/libmultipath/wwids.c
+++ b/libmultipath/wwids.c
@@ -7,6 +7,7 @@ 
 #include <sys/types.h>
 #include <sys/stat.h>
 
+#include "util.h"
 #include "checkers.h"
 #include "vector.h"
 #include "structs.h"
@@ -87,7 +88,8 @@  write_out_wwid(int fd, char *wwid) {
 int
 replace_wwids(vector mp)
 {
-	int i, fd, can_write;
+	int i, can_write;
+	long fd;
 	struct multipath * mpp;
 	size_t len;
 	int ret = -1;
@@ -99,6 +101,8 @@  replace_wwids(vector mp)
 	pthread_cleanup_pop(1);
 	if (fd < 0)
 		goto out;
+
+	pthread_cleanup_push(close_fd, (void*)fd);
 	if (!can_write) {
 		condlog(0, "cannot replace wwids. wwids file is read-only");
 		goto out_file;
@@ -132,7 +136,7 @@  replace_wwids(vector mp)
 	}
 	ret = 0;
 out_file:
-	close(fd);
+	pthread_cleanup_pop(1);
 out:
 	return ret;
 }