[PATCH] Handle exception during non-existent files

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This could occur if the kernel moduel is not loaded

Also fix, major typo, where I completely missed the try:
block in the previously submitted patch. Sorry.

Signed-off-by: Ritesh Raj Sarraf <rrs@xxxxxxxxxxxxxx>
---
 tcm-py/tcm_fabric.py |   37 ++++++++++++++++++++-----------------
 tcm-py/tcm_node.py   |   21 ++++++++++++---------
 2 files changed, 32 insertions(+), 26 deletions(-)

diff --git a/tcm-py/tcm_fabric.py b/tcm-py/tcm_fabric.py
index 2bdc77a..4e6dab1 100755
--- a/tcm-py/tcm_fabric.py
+++ b/tcm-py/tcm_fabric.py
@@ -9,6 +9,10 @@ import optparse
 target_root = "/sys/kernel/config/target/"
 spec_root = "/var/target/fabric/"
 
+def fabric_err(msg):
+	print >> sys.stderr, msg
+	sys.exit(1)
+
 def fabric_configfs_dump(fabric_name, fabric_root, module_name):
 
 	if not os.path.isdir(fabric_root):
@@ -455,28 +459,27 @@ def fabric_unloadall():
 
 	module_name = ""
 
-	for fabric_name in os.listdir(target_root):
-		if fabric_name == "version":
-			continue
-		if fabric_name == "core":
-			continue
-		# FIXME: currently using lio_node --unload
-		if fabric_name == "iscsi":
-			continue
+	try:
+		for fabric_name in os.listdir(target_root):
+			if fabric_name == "version":
+				continue
+			if fabric_name == "core":
+				continue
+			# FIXME: currently using lio_node --unload
+			if fabric_name == "iscsi":
+				continue
 
-		fabric_root = target_root + fabric_name
-		module_name = fabric_get_module_name(fabric_name)
-#		print "fabric_get_module_name() using: " + module_name
+			fabric_root = target_root + fabric_name
+			module_name = fabric_get_module_name(fabric_name)
+			#print "fabric_get_module_name() using: " + module_name
 		
-		if module_name == "":
-			continue
+			if module_name == "":
+				continue
 
-		fabric_unload(fabric_name, fabric_root, module_name)
+			fabric_unload(fabric_name, fabric_root, module_name)
 	except OSError, (errno, strerror):
 		if errno == 2:
-			print "%s %s\n" % (target_root, strerror)
-			print "Is kernel module loaded?\n"
-			exit 1
+			fabric_err("%s %s\n%s" % (target_root, strerror, "Is kernel module loaded?") )
 		
 
 def do_work(stdout_enable, stdout_enable_all, date_time, unload, unloadall, fabric_name, fabric_root, module_name):
diff --git a/tcm-py/tcm_node.py b/tcm-py/tcm_node.py
index b7c05cf..997009c 100755
--- a/tcm-py/tcm_node.py
+++ b/tcm-py/tcm_node.py
@@ -23,19 +23,22 @@ def tcm_err(msg):
 	sys.exit(1)
 
 def tcm_read(filename):
-	with open(filename) as f:
-		return f.read()
+	try:
+		with open(filename) as f:
+			return f.read()
 	except IOError, (errno, strerror):
 		if errno == 2:
-			print "%s %s\n" % (filename, strerror)
-			print "Is kernel module loaded?\n"
-			exit 1
+			tcm_err("%s %s\n%s" % (filename, strerror, "Is kernel module loaded?") )
 
 def tcm_write(filename, value, newline=True):
-	with open(filename, "w") as f:
-		f.write(value)
-		if newline:
-			f.write("\n")
+	try:
+		with open(filename, "w") as f:
+			f.write(value)
+			if newline:
+				f.write("\n")
+	except IOError, (errno, strerror):
+		if errno == 2:
+			tcm_err("%s %s\n%s" % (filename, strerror, "Is kernel module loaded?") )
 
 def tcm_full_path(arg):
 	return tcm_root + "/" + arg
-- 
1.7.10

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


[Index of Archives]     [Linux SCSI]     [Kernel Newbies]     [Linux SCSI Target Infrastructure]     [Share Photos]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Linux IIO]     [Device Mapper]

  Powered by Linux