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