linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] Tools: hv
@ 2012-09-05 21:37 K. Y. Srinivasan
  2012-09-05 21:37 ` [PATCH 1/3] tools/hv: Fix file handle leak K. Y. Srinivasan
  0 siblings, 1 reply; 4+ messages in thread
From: K. Y. Srinivasan @ 2012-09-05 21:37 UTC (permalink / raw)
  To: gregkh, linux-kernel, devel, olaf, apw, ben, thozza, dcbw
  Cc: K. Y. Srinivasan

This patchset fixes some bugs in the KVP daemon code.

Ben Hutchings (3):
  tools/hv: Fix file handle leak
  tools/hv: Fix exit() error code
  tools/hv: Check for read/write errors

 tools/hv/hv_kvp_daemon.c |   45 +++++++++++++++++++++++++++++++--------------
 1 files changed, 31 insertions(+), 14 deletions(-)

-- 
1.7.4.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] tools/hv: Fix file handle leak
  2012-09-05 21:37 [PATCH 0/3] Tools: hv K. Y. Srinivasan
@ 2012-09-05 21:37 ` K. Y. Srinivasan
  2012-09-05 21:37   ` [PATCH 2/3] tools/hv: Fix exit() error code K. Y. Srinivasan
  2012-09-05 21:37   ` [PATCH 3/3] tools/hv: Check for read/write errors K. Y. Srinivasan
  0 siblings, 2 replies; 4+ messages in thread
From: K. Y. Srinivasan @ 2012-09-05 21:37 UTC (permalink / raw)
  To: gregkh, linux-kernel, devel, olaf, apw, ben, thozza, dcbw
  Cc: K. Y. Srinivasan, stable

From: Ben Hutchings <ben@decadent.org.uk>

Match up each fopen() with an fclose().

Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Cc: stable@vger.kernel.org
---
 tools/hv/hv_kvp_daemon.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c
index c8e1013..4514fb4 100644
--- a/tools/hv/hv_kvp_daemon.c
+++ b/tools/hv/hv_kvp_daemon.c
@@ -160,7 +160,7 @@ static void kvp_update_file(int pool)
 				sizeof(struct kvp_record),
 				kvp_file_info[pool].num_records, filep);
 
-	fflush(filep);
+	fclose(filep);
 	kvp_release_lock(pool);
 }
 
@@ -207,6 +207,7 @@ static void kvp_update_mem_state(int pool)
 	kvp_file_info[pool].records = record;
 	kvp_file_info[pool].num_records = records_read;
 
+	fclose(filep);
 	kvp_release_lock(pool);
 }
 static int kvp_file_init(void)
-- 
1.7.4.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] tools/hv: Fix exit() error code
  2012-09-05 21:37 ` [PATCH 1/3] tools/hv: Fix file handle leak K. Y. Srinivasan
@ 2012-09-05 21:37   ` K. Y. Srinivasan
  2012-09-05 21:37   ` [PATCH 3/3] tools/hv: Check for read/write errors K. Y. Srinivasan
  1 sibling, 0 replies; 4+ messages in thread
From: K. Y. Srinivasan @ 2012-09-05 21:37 UTC (permalink / raw)
  To: gregkh, linux-kernel, devel, olaf, apw, ben, thozza, dcbw
  Cc: K. Y. Srinivasan, stable

From: Ben Hutchings <ben@decadent.org.uk>

Linux native exit codes are 8-bit unsigned values.  exit(-1) results
in an exit code of 255, which is usually reserved for shells reporting
'command not found'.  Use the portable value EXIT_FAILURE.  (Not that
this matters much for a daemon.)

Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Cc: stable@vger.kernel.org
---
 tools/hv/hv_kvp_daemon.c |   22 +++++++++++-----------
 1 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c
index 4514fb4..01b3ca5 100644
--- a/tools/hv/hv_kvp_daemon.c
+++ b/tools/hv/hv_kvp_daemon.c
@@ -122,7 +122,7 @@ static void kvp_acquire_lock(int pool)
 
 	if (fcntl(kvp_file_info[pool].fd, F_SETLKW, &fl) == -1) {
 		syslog(LOG_ERR, "Failed to acquire the lock pool: %d", pool);
-		exit(-1);
+		exit(EXIT_FAILURE);
 	}
 }
 
@@ -134,7 +134,7 @@ static void kvp_release_lock(int pool)
 	if (fcntl(kvp_file_info[pool].fd, F_SETLK, &fl) == -1) {
 		perror("fcntl");
 		syslog(LOG_ERR, "Failed to release the lock pool: %d", pool);
-		exit(-1);
+		exit(EXIT_FAILURE);
 	}
 }
 
@@ -153,7 +153,7 @@ static void kvp_update_file(int pool)
 	if (!filep) {
 		kvp_release_lock(pool);
 		syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
-		exit(-1);
+		exit(EXIT_FAILURE);
 	}
 
 	bytes_written = fwrite(kvp_file_info[pool].records,
@@ -179,7 +179,7 @@ static void kvp_update_mem_state(int pool)
 	if (!filep) {
 		kvp_release_lock(pool);
 		syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
-		exit(-1);
+		exit(EXIT_FAILURE);
 	}
 	while (!feof(filep)) {
 		readp = &record[records_read];
@@ -196,7 +196,7 @@ static void kvp_update_mem_state(int pool)
 
 			if (record == NULL) {
 				syslog(LOG_ERR, "malloc failed");
-				exit(-1);
+				exit(EXIT_FAILURE);
 			}
 			continue;
 		}
@@ -225,7 +225,7 @@ static int kvp_file_init(void)
 	if (access("/var/opt/hyperv", F_OK)) {
 		if (mkdir("/var/opt/hyperv", S_IRUSR | S_IWUSR | S_IROTH)) {
 			syslog(LOG_ERR, " Failed to create /var/opt/hyperv");
-			exit(-1);
+			exit(EXIT_FAILURE);
 		}
 	}
 
@@ -1358,13 +1358,13 @@ int main(void)
 
 	if (kvp_file_init()) {
 		syslog(LOG_ERR, "Failed to initialize the pools");
-		exit(-1);
+		exit(EXIT_FAILURE);
 	}
 
 	fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_CONNECTOR);
 	if (fd < 0) {
 		syslog(LOG_ERR, "netlink socket creation failed; error:%d", fd);
-		exit(-1);
+		exit(EXIT_FAILURE);
 	}
 	addr.nl_family = AF_NETLINK;
 	addr.nl_pad = 0;
@@ -1376,7 +1376,7 @@ int main(void)
 	if (error < 0) {
 		syslog(LOG_ERR, "bind failed; error:%d", error);
 		close(fd);
-		exit(-1);
+		exit(EXIT_FAILURE);
 	}
 	sock_opt = addr.nl_groups;
 	setsockopt(fd, 270, 1, &sock_opt, sizeof(sock_opt));
@@ -1396,7 +1396,7 @@ int main(void)
 	if (len < 0) {
 		syslog(LOG_ERR, "netlink_send failed; error:%d", len);
 		close(fd);
-		exit(-1);
+		exit(EXIT_FAILURE);
 	}
 
 	pfd.fd = fd;
@@ -1608,7 +1608,7 @@ kvp_done:
 		len = netlink_send(fd, incoming_cn_msg);
 		if (len < 0) {
 			syslog(LOG_ERR, "net_link send failed; error:%d", len);
-			exit(-1);
+			exit(EXIT_FAILURE);
 		}
 	}
 
-- 
1.7.4.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] tools/hv: Check for read/write errors
  2012-09-05 21:37 ` [PATCH 1/3] tools/hv: Fix file handle leak K. Y. Srinivasan
  2012-09-05 21:37   ` [PATCH 2/3] tools/hv: Fix exit() error code K. Y. Srinivasan
@ 2012-09-05 21:37   ` K. Y. Srinivasan
  1 sibling, 0 replies; 4+ messages in thread
From: K. Y. Srinivasan @ 2012-09-05 21:37 UTC (permalink / raw)
  To: gregkh, linux-kernel, devel, olaf, apw, ben, thozza, dcbw
  Cc: K. Y. Srinivasan, stable

From: Ben Hutchings <ben@decadent.org.uk>

hv_kvp_daemon currently does not check whether fread() or fwrite()
succeed.  Add the necessary checks.  Also, remove the incorrect use of
feof() before fread().

Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
Signed-off-by: K. Y. Srinivasan <kys@microsoft.com>
Cc: stable@vger.kernel.org
---
 tools/hv/hv_kvp_daemon.c |   22 +++++++++++++++++++---
 1 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c
index 01b3ca5..3922abc 100644
--- a/tools/hv/hv_kvp_daemon.c
+++ b/tools/hv/hv_kvp_daemon.c
@@ -160,7 +160,12 @@ static void kvp_update_file(int pool)
 				sizeof(struct kvp_record),
 				kvp_file_info[pool].num_records, filep);
 
-	fclose(filep);
+	if (ferror(filep) || fclose(filep)) {
+		kvp_release_lock(pool);
+		syslog(LOG_ERR, "Failed to write file, pool: %d", pool);
+		exit(EXIT_FAILURE);
+	}
+
 	kvp_release_lock(pool);
 }
 
@@ -181,12 +186,17 @@ static void kvp_update_mem_state(int pool)
 		syslog(LOG_ERR, "Failed to open file, pool: %d", pool);
 		exit(EXIT_FAILURE);
 	}
-	while (!feof(filep)) {
+	for (;;) {
 		readp = &record[records_read];
 		records_read += fread(readp, sizeof(struct kvp_record),
 					ENTRIES_PER_BLOCK * num_blocks,
 					filep);
 
+		if (ferror(filep)) {
+			syslog(LOG_ERR, "Failed to read file, pool: %d", pool);
+			exit(EXIT_FAILURE);
+		}
+
 		if (!feof(filep)) {
 			/*
 			 * We have more data to read.
@@ -249,12 +259,18 @@ static int kvp_file_init(void)
 			fclose(filep);
 			return 1;
 		}
-		while (!feof(filep)) {
+		for (;;) {
 			readp = &record[records_read];
 			records_read += fread(readp, sizeof(struct kvp_record),
 					ENTRIES_PER_BLOCK,
 					filep);
 
+			if (ferror(filep)) {
+				syslog(LOG_ERR, "Failed to read file, pool: %d",
+				       i);
+				exit(EXIT_FAILURE);
+			}
+
 			if (!feof(filep)) {
 				/*
 				 * We have more data to read.
-- 
1.7.4.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2012-09-05 21:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-09-05 21:37 [PATCH 0/3] Tools: hv K. Y. Srinivasan
2012-09-05 21:37 ` [PATCH 1/3] tools/hv: Fix file handle leak K. Y. Srinivasan
2012-09-05 21:37   ` [PATCH 2/3] tools/hv: Fix exit() error code K. Y. Srinivasan
2012-09-05 21:37   ` [PATCH 3/3] tools/hv: Check for read/write errors K. Y. Srinivasan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).