0e55d5
From 9dd181dcb1ca299cd82075b8e598fc57d87ee1c0 Mon Sep 17 00:00:00 2001
0e55d5
From: Jim Ramsay <jim_ramsay@dell.com>
0e55d5
Date: Wed, 3 Oct 2012 09:57:43 -0400
0e55d5
Subject: iscsi tools: Convert '-r' argument to an integer before checking if
0e55d5
 it is a path
0e55d5
0e55d5
If there is a file in the CWD named '1' and you were trying to run
0e55d5
'iscsiadm -m session -r 1 ...', the command would fail with "1 is not a
0e55d5
directory".
0e55d5
0e55d5
Root cause: The code that parses the -r option's argument tries lstat(2)
0e55d5
first, falling back to atoi(3) only if lstat fails.
0e55d5
0e55d5
This change inverts the order of checks, first with strtol(3) to see if
0e55d5
the argument given is a positive integer, then falling back to lstat(2)
0e55d5
only if it is not.
0e55d5
0e55d5
Signed-off-by: Jim Ramsay <jim_ramsay@dell.com>
0e55d5
---
0e55d5
 usr/iscsi_sysfs.c | 17 +++++++++--------
0e55d5
 1 file changed, 9 insertions(+), 8 deletions(-)
0e55d5
0e55d5
diff --git a/usr/iscsi_sysfs.c b/usr/iscsi_sysfs.c
0e55d5
index 123dde3..4015b35 100644
0e55d5
--- a/usr/iscsi_sysfs.c
0e55d5
+++ b/usr/iscsi_sysfs.c
0e55d5
@@ -740,7 +740,7 @@ int iscsi_sysfs_session_has_leadconn(uint32_t sid)
0e55d5
  * /sys/devices/platform/hostH/sessionS/targetH:B:I
0e55d5
  * /sys/devices/platform/hostH/sessionS
0e55d5
  *
0e55d5
- * return the sid S. If just the sid is passed in it will be covnerted
0e55d5
+ * return the sid S. If just the sid is passed in it will be converted
0e55d5
  * to a int.
0e55d5
  */
0e55d5
 int iscsi_sysfs_get_sid_from_path(char *session)
0e55d5
@@ -748,15 +748,16 @@ int iscsi_sysfs_get_sid_from_path(char *session)
0e55d5
 	struct sysfs_device *dev_parent, *dev;
0e55d5
 	struct stat statb;
0e55d5
 	char devpath[PATH_SIZE];
0e55d5
+	char *end;
0e55d5
+	int sid;
0e55d5
+
0e55d5
+	sid = strtol(session, &end, 10);
0e55d5
+	if (sid > 0 && *session != '\0' && *end == '\0')
0e55d5
+		return sid;
0e55d5
 
0e55d5
 	if (lstat(session, &statb)) {
0e55d5
-		log_debug(1, "Could not stat %s failed with %d",
0e55d5
-			  session, errno);
0e55d5
-		if (index(session, '/')) {
0e55d5
-			log_error("%s is an invalid session path\n", session);
0e55d5
-			exit(1);
0e55d5
-		}
0e55d5
-		return atoi(session);
0e55d5
+		log_error("%s is an invalid session ID or path\n", session);
0e55d5
+		exit(1);
0e55d5
 	}
0e55d5
 
0e55d5
 	if (!S_ISDIR(statb.st_mode) && !S_ISLNK(statb.st_mode)) {
0e55d5
-- 
0e55d5
1.7.11.7
0e55d5