[stgt] [PATCH 1/8] fix error handling in get_bsg_major() return error code if the device file in /sys/class/bsg failed to open; use fscanf to read an integer major number directly (instead of reading and parsing the string), return error and print appropriate messages when scanf fails.

nezhinsky at gmail.com nezhinsky at gmail.com
Wed Apr 25 16:52:14 CEST 2012


From: Alexander Nezhinsky <alexandern at mellanox.com>


Signed-off-by: Alexander Nezhinsky <alexandern at mellanox.com>
---
 usr/bs_sg.c |   18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/usr/bs_sg.c b/usr/bs_sg.c
index 4f78def..fe7dbec 100644
--- a/usr/bs_sg.c
+++ b/usr/bs_sg.c
@@ -301,19 +301,27 @@ static void bs_sg_cmd_complete(int fd, int events, void *data)
 static int get_bsg_major(char *path)
 {
 	FILE *devfd;
-	char majorno[8];
+	int majorno, n;
 	char dev[64];
 	char tmp[16];
 
 	sscanf(path, "/dev/bsg/%s", tmp);
 	sprintf(dev, "/sys/class/bsg/%s/dev", tmp);
 	devfd = fopen(dev, "r");
-	if (!devfd)
+	if (!devfd) {
+		eprintf("%s open failed errno: %d\n", dev, errno);
 		return -1;
-	fscanf(devfd, "%s:", majorno);
+	}
+	n = fscanf(devfd, "%d:", &majorno);
 	fclose(devfd);
-
-	return atoi(majorno);
+	if (n != 1) {
+		if (n < 0)
+			eprintf("reading major from %s failed errno: %d\n", dev, errno);
+		else
+			eprintf("reading major from %s failed: invalid input\n", dev);
+		return -1;
+	}
+	return majorno;
 }
 
 static int chk_sg_device(char *path)
-- 
1.7.9.6

--
To unsubscribe from this list: send the line "unsubscribe stgt" in
the body of a message to majordomo at vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html



More information about the stgt mailing list