Blob Blame History Raw
From 545284051b48f787db28a26359b545b4b2d1faae Mon Sep 17 00:00:00 2001
From: Cole Robinson <crobinso@redhat.com>
Date: Thu, 5 Dec 2013 14:20:07 +0100
Subject: [RHEL-7.0 virt-manager PATCH] virt-manager: Yet more fixes for
 DISPLAY unset error

Since these days we have to import Gtk _after_ forking, _and_ Gtk
doesn't raise a nice error for us, we need to postpone dropping stdio
so the user has any chance of seeing the error. Also try to add an
explicit error about DISPLAY again, since we don't get it from Gtk
these days.

Closes: https://bugzilla.redhat.com/show_bug.cgi?id=1038496
(cherry picked from commit 395a20edd82f1072d23e80ecbb25dbeed9359304)

Conflicts:
	virt-manager
---
 virt-manager | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/virt-manager b/virt-manager
index 656c84b..55cc30a 100755
--- a/virt-manager
+++ b/virt-manager
@@ -209,15 +209,16 @@ def main():
         virtManager.module_trace.wrap_module(libvirt)

     # Now we've got basic environment up & running we can fork
+    do_drop_stdio = False
     if not options.nofork and not options.debug:
         drop_tty()
-        drop_stdio()
+        do_drop_stdio = True

         # Ignore SIGHUP, otherwise a serial console closing drops the whole app
         signal.signal(signal.SIGHUP, signal.SIG_IGN)

     # The never ending fork+gconf/gsettings problems now require
-    # us to import Gtk before the fork. This creates a funny race,
+    # us to import Gtk _after_ the fork. This creates a funny race,
     # since we need to parse the command line arguments to know if
     # we need to fork, but need to import Gtk before cli processing
     # so it can handle --g-fatal-args. We strip out our flags first
@@ -226,20 +227,32 @@ def main():
     try:
         sys.argv = origargv[:1] + leftovers[:]
         from gi.repository import Gtk  # pylint: disable=E0611
-        globals()["Gtk"] = Gtk
         leftovers = sys.argv[1:]

+        # This will error if Gtk wasn't correctly initialized
+        Gtk.Window()
+
+        globals()["Gtk"] = Gtk
         import virtManager.config
         import virtManager.util
-    except:
+    except Exception, e:
         # Don't just let the exception raise here. abrt reports bugs
         # when users mess up su/sudo and DISPLAY isn't set. Printing
         # it avoids the issue
-        print "".join(traceback.format_exc())
+        display = os.environ.get("DISPLAY", "")
+        msg = str(e)
+        if not display:
+            msg += ": Could not open display: %s" % display
+        logging.debug("".join(traceback.format_exc()))
+        print msg
         return 1
     finally:
         sys.argv = origargv

+    # Do this after the Gtk import so the user has a chance of seeing any error
+    if do_drop_stdio:
+        drop_stdio()
+
     if leftovers:
         raise RuntimeError("Unhandled command line options '%s'" % leftovers)

-- 
1.8.5.1