If there is a file in the CWD named '1' and you were trying to run
'iscsiadm -m session -r 1 ...', the command would fail with "1 is not a
directory".

Root cause: The code that parses the -r option's argument tries lstat(2)
first, falling back to atoi(3) only if lstat fails.

This change inverts the order of checks, first with strtol(3) to see if
the argument given is a positive integer, then falling back to lstat(2)
only if it is not.

Signed-off-by: Jim Ramsay <jim_ram...@dell.com>
---
 usr/iscsi_sysfs.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

diff --git a/usr/iscsi_sysfs.c b/usr/iscsi_sysfs.c
index 123dde3..4015b35 100644
--- a/usr/iscsi_sysfs.c
+++ b/usr/iscsi_sysfs.c
@@ -740,7 +740,7 @@ int iscsi_sysfs_session_has_leadconn(uint32_t sid)
  * /sys/devices/platform/hostH/sessionS/targetH:B:I
  * /sys/devices/platform/hostH/sessionS
  *
- * return the sid S. If just the sid is passed in it will be covnerted
+ * return the sid S. If just the sid is passed in it will be converted
  * to a int.
  */
 int iscsi_sysfs_get_sid_from_path(char *session)
@@ -748,15 +748,16 @@ int iscsi_sysfs_get_sid_from_path(char *session)
        struct sysfs_device *dev_parent, *dev;
        struct stat statb;
        char devpath[PATH_SIZE];
+       char *end;
+       int sid;
+
+       sid = strtol(session, &end, 10);
+       if (sid > 0 && *session != '\0' && *end == '\0')
+               return sid;
 
        if (lstat(session, &statb)) {
-               log_debug(1, "Could not stat %s failed with %d",
-                         session, errno);
-               if (index(session, '/')) {
-                       log_error("%s is an invalid session path\n", session);
-                       exit(1);
-               }
-               return atoi(session);
+               log_error("%s is an invalid session ID or path\n", session);
+               exit(1);
        }
 
        if (!S_ISDIR(statb.st_mode) && !S_ISLNK(statb.st_mode)) {
-- 
1.7.12

-- 
You received this message because you are subscribed to the Google Groups 
"open-iscsi" group.
To post to this group, send email to open-iscsi@googlegroups.com.
To unsubscribe from this group, send email to 
open-iscsi+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/open-iscsi?hl=en.

Reply via email to