From 496123c7e9c2fe790794b15eb84e1743be23d73e Mon Sep 17 00:00:00 2001
From: David Lehman <dlehman@redhat.com>
Date: Thu, 22 Dec 2011 13:01:37 -0600
Subject: [PATCH 3/9] Protect from tracebacks that occur when running as
 non-root.

---
 pyanaconda/iutil.py        |   39 +++++++++++++++++++++++++++------------
 pyanaconda/storage/udev.py |    2 +-
 2 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/pyanaconda/iutil.py b/pyanaconda/iutil.py
index d29bf1e..a8bccb8 100644
--- a/pyanaconda/iutil.py
+++ b/pyanaconda/iutil.py
@@ -102,16 +102,22 @@ def execWithRedirect(command, argv, stdin = None, stdout = None,
         stdin = sys.stdin.fileno()
 
     if isinstance(stdout, str):
-        stdout = os.open(stdout, os.O_RDWR|os.O_CREAT)
-        stdoutclose = lambda : os.close(stdout)
+        try:
+            stdout = os.open(stdout, os.O_RDWR|os.O_CREAT)
+            stdoutclose = lambda : os.close(stdout)
+        except OSError:
+            stdout = sys.stdout.fileno()
     elif isinstance(stdout, int):
         pass
     elif stdout is None or not isinstance(stdout, file):
         stdout = sys.stdout.fileno()
 
     if isinstance(stderr, str):
-        stderr = os.open(stderr, os.O_RDWR|os.O_CREAT)
-        stderrclose = lambda : os.close(stderr)
+        try:
+            stderr = os.open(stderr, os.O_RDWR|os.O_CREAT)
+            stderrclose = lambda : os.close(stderr)
+        except OSError:
+            stderr = sys.stderr.fileno()
     elif isinstance(stderr, int):
         pass
     elif stderr is None or not isinstance(stderr, file):
@@ -171,7 +177,7 @@ def execWithRedirect(command, argv, stdin = None, stdout = None,
         stdinclose()
         stdoutclose()
         stderrclose()
-        raise RuntimeError, errstr
+        raise RuntimeError(errstr)
 
     return ret
 
@@ -206,8 +212,11 @@ def execWithCapture(command, argv, stdin = None, stderr = None, root='/'):
         stdin = sys.stdin.fileno()
 
     if isinstance(stderr, str):
-        stderr = os.open(stderr, os.O_RDWR|os.O_CREAT)
-        stderrclose = lambda : os.close(stderr)
+        try:
+            stderr = os.open(stderr, os.O_RDWR|os.O_CREAT)
+            stderrclose = lambda : os.close(stderr)
+        except OSError:
+            stderr = sys.stderr.fileno()
     elif isinstance(stderr, int):
         pass
     elif stderr is None or not isinstance(stderr, file):
@@ -239,7 +248,7 @@ def execWithCapture(command, argv, stdin = None, stderr = None, root='/'):
     except OSError as e:
         log.error ("Error running " + command + ": " + e.strerror)
         closefds()
-        raise RuntimeError, "Error running " + command + ": " + e.strerror
+        raise RuntimeError("Error running " + command + ": " + e.strerror)
 
     closefds()
     return rc
@@ -270,16 +279,22 @@ def execWithCallback(command, argv, stdin = None, stdout = None,
         stdin = sys.stdin.fileno()
 
     if isinstance(stdout, str):
-        stdout = os.open(stdout, os.O_RDWR|os.O_CREAT)
-        stdoutclose = lambda : os.close(stdout)
+        try:
+            stdout = os.open(stdout, os.O_RDWR|os.O_CREAT)
+            stdoutclose = lambda : os.close(stdout)
+        except OSError:
+            stdout = sys.stdout.fileno()
     elif isinstance(stdout, int):
         pass
     elif stdout is None or not isinstance(stdout, file):
         stdout = sys.stdout.fileno()
 
     if isinstance(stderr, str):
-        stderr = os.open(stderr, os.O_RDWR|os.O_CREAT)
-        stderrclose = lambda : os.close(stderr)
+        try:
+            stderr = os.open(stderr, os.O_RDWR|os.O_CREAT)
+            stderrclose = lambda : os.close(stderr)
+        except OSError:
+            stderr = sys.stderr.fileno()
     elif isinstance(stderr, int):
         pass
     elif stderr is None or not isinstance(stderr, file):
diff --git a/pyanaconda/storage/udev.py b/pyanaconda/storage/udev.py
index 87d3cf8..53b6faf 100644
--- a/pyanaconda/storage/udev.py
+++ b/pyanaconda/storage/udev.py
@@ -88,7 +88,7 @@ def udev_get_block_devices():
                                stdout = "/dev/tty5", stderr="/dev/tty5")
         iutil.execWithRedirect("rmmod", [ "scsi_wait_scan" ],
                                stdout = "/dev/tty5", stderr="/dev/tty5")
-    except OSError:
+    except (OSError, RuntimeError):
         log.info("Skipping scsi_wait_scan due to running as non-root.")
 
     udev_settle()
-- 
1.7.8

