Convert all DBus usage to Gio GDBUS

And drop a whole bunch of legacy dbus fallback stuff. Now that we
are bumping the host dependencies to very recent gtk, none of the
back compat should matter.
This commit is contained in:
Cole Robinson 2013-04-16 13:38:19 -04:00
parent 8b31a10117
commit c60ad16b3b
8 changed files with 108 additions and 167 deletions

View File

@ -40,10 +40,6 @@ List debugging output to the console (normally this is only logged in
Don't fork C<virt-manager> off into the backround: run it blocking the
current terminal. Useful for seeing possible errors dumped to stdout/stderr.
=item --no-dbus
Disable the DBus API used for remote control of the C<virt-manager> UI
=item --no-conn-autostart
Don't autostart any libvirt connections when launching C<virt-manager>.
@ -86,7 +82,7 @@ Please see L<http://virt-manager.org/page/BugReporting>
=head1 COPYRIGHT
Copyright (C) 2006-2007 Red Hat, Inc, and various contributors.
Copyright (C) 2006-2013 Red Hat, Inc, and various contributors.
This is free software. You may redistribute copies of it under the terms of the GNU General
Public License C<http://www.gnu.org/licenses/gpl.html>. There is NO WARRANTY, to the extent
permitted by law.

View File

@ -114,8 +114,6 @@ def parse_commandline():
optParser.add_option("--debug", action="store_true", dest="debug",
help="Print debug output to stdout (implies --no-fork)",
default=False)
optParser.add_option("--no-dbus", action="store_true", dest="nodbus",
help="Disable DBus service for controlling UI")
optParser.add_option("--no-fork", action="store_true", dest="nofork",
help="Don't fork into background on startup")
optParser.add_option("--no-conn-autostart", action="store_true",
@ -216,11 +214,6 @@ def main():
import libvirt
virtManager.module_trace.wrap_module(libvirt)
import dbus
import dbus.mainloop.glib
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
dbus.mainloop.glib.threads_init()
# Specifically init config/gconf before the fork, so that pam
# doesn't think we closed the app, therefor robbing us of
# display access

View File

@ -79,9 +79,6 @@ Requires: gnome-python2-gconf >= 1.99.11-7
# This version not strictly required: virt-manager should work with older,
# however varying amounts of functionality will not be enabled.
Requires: libvirt-python >= 0.7.0
# Definitely does not work with earlier due to python API changes
Requires: dbus-python >= 0.61
Requires: dbus-x11
%if 0%{?rhel} > 6
# Might work with earlier, but this is what we've tested
Requires: gnome-keyring >= 0.4.9

View File

@ -22,12 +22,12 @@ import logging
import socket
# pylint: disable=E0611
from gi.repository import Gio
from gi.repository import GObject
from gi.repository import Gtk
# pylint: enable=E0611
import virtinst
import dbus
from virtManager.baseclass import vmmGObjectUI
@ -79,7 +79,6 @@ class vmmConnect(vmmGObjectUI):
self.browser = None
self.browser_sigs = []
self.can_browse = False
# Set this if we can't resolve 'hostname.local': means avahi
# prob isn't configured correctly, and we should strip .local
@ -90,15 +89,13 @@ class vmmConnect(vmmGObjectUI):
self.set_initial_state()
self.bus = None
self.server = None
self.can_browse = False
self.dbus = None
self.avahiserver = None
try:
self.bus = dbus.SystemBus()
self.server = dbus.Interface(
self.bus.get_object("org.freedesktop.Avahi", "/"),
"org.freedesktop.Avahi.Server")
self.can_browse = True
self.dbus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
self.avahiserver = Gio.DBusProxy.new_sync(self.dbus, 0, None,
"org.freedesktop.Avahi", "/",
"org.freedesktop.Avahi.Server", None)
except Exception, e:
logging.debug("Couldn't contact avahi: %s", str(e))
@ -166,13 +163,22 @@ class vmmConnect(vmmGObjectUI):
ignore = flags
try:
# Async service resolving
res = self.server.ServiceResolverNew(interface, protocol, name,
typ, domain, -1, 0)
resint = dbus.Interface(self.bus.get_object(
"org.freedesktop.Avahi", res),
"org.freedesktop.Avahi.ServiceResolver")
self.browser_sigs.append(
resint.connect_to_signal("Found", self.add_conn_to_list))
res = self.avahiserver.ServiceResolverNew("(iisssiu)",
interface, protocol,
name, typ, domain, -1, 0)
resint = Gio.DBusProxy.new_sync(self.dbus, 0, None,
"org.freedesktop.Avahi", res,
"org.freedesktop.Avahi.ServiceResolver",
None)
def cb(proxy, sender, signal, args):
ignore = proxy
ignore = sender
if signal == "Found":
self.add_conn_to_list(*args)
sig = resint.connect("g-signal", cb)
self.browser_sigs.append((resint, sig))
except Exception, e:
logging.exception(e)
@ -216,7 +222,7 @@ class vmmConnect(vmmGObjectUI):
logging.exception(e)
def start_browse(self):
if self.browser or not self.can_browse:
if self.browser or not self.avahiserver:
return
# Call method to create new browser, and get back an object path for it.
interface = -1 # physical interface to use? -1 is unspec
@ -224,23 +230,31 @@ class vmmConnect(vmmGObjectUI):
service = '_libvirt._tcp' # Service name to poll for
flags = 0 # Extra option flags
domain = "" # Domain to browse in. NULL uses default
bpath = self.server.ServiceBrowserNew(interface, protocol, service,
domain, flags)
bpath = self.avahiserver.ServiceBrowserNew("(iissu)",
interface, protocol,
service, domain, flags)
# Create browser interface for the new object
self.browser = dbus.Interface(self.bus.get_object(
"org.freedesktop.Avahi", bpath),
"org.freedesktop.Avahi.ServiceBrowser")
self.browser = Gio.DBusProxy.new_sync(self.dbus, 0, None,
"org.freedesktop.Avahi", bpath,
"org.freedesktop.Avahi.ServiceBrowser",
None)
self.browser_sigs.append(
self.browser.connect_to_signal("ItemNew", self.add_service))
self.browser_sigs.append(
self.browser.connect_to_signal("ItemRemove", self.remove_service))
def cb(proxy, sender, signal, args):
ignore = proxy
ignore = sender
if signal == "ItemNew":
self.add_service(*args)
elif signal == "ItemRemove":
self.remove_service(*args)
self.browser_sigs.append((self.browser,
self.browser.connect("g-signal", cb)))
def stop_browse(self):
if self.browser:
for sig in self.browser_sigs:
sig.remove()
for obj, sig in self.browser_sigs:
obj.disconnect(sig)
self.browser_sigs = []
self.browser = None
@ -280,7 +294,7 @@ class vmmConnect(vmmGObjectUI):
self.widget("connection").set_sensitive(is_remote)
self.widget("autoconnect").set_active(not is_remote)
self.widget("username-entry").set_sensitive(is_remote)
if is_remote and self.can_browse:
if is_remote and self.avahiserver:
self.start_browse()
else:
self.stop_browse()

View File

@ -20,86 +20,40 @@
# pylint: disable=E0611
from gi.repository import GLib
from gi.repository import Gio
# pylint: enable=E0611
import logging
import os
import time
import dbus
import libvirt
def do_we_have_session():
pid = os.getpid()
try:
bus = dbus.SystemBus()
bus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
except:
logging.exception("Error getting system bus handle")
return
# Check systemd
try:
manager = dbus.Interface(bus.get_object(
"org.freedesktop.login1",
"/org/freedesktop/login1"),
"org.freedesktop.login1.Manager")
ret = manager.GetSessionByPID(pid)
manager = Gio.DBusProxy.new_sync(bus, 0, None,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager", None)
ret = manager.GetSessionByPID("(u)", pid)
logging.debug("Found login1 session=%s", ret)
return True
except:
logging.exception("Couldn't connect to logind")
# Check ConsoleKit
try:
manager = dbus.Interface(bus.get_object(
"org.freedesktop.ConsoleKit",
"/org/freedesktop/ConsoleKit/Manager"),
"org.freedesktop.ConsoleKit.Manager")
ret = manager.GetSessionForUnixProcess(pid)
logging.debug("Found ConsoleKit session=%s", ret)
return True
except:
logging.exception("Couldn't connect to ConsoleKit")
return False
def creds_polkit(action):
"""
Libvirt openAuth callback for PolicyKit < 1.0
"""
if os.getuid() == 0:
logging.debug("Skipping policykit check as root")
return 0
logging.debug("Doing policykit for %s", action)
try:
# First try to use org.freedesktop.PolicyKit.AuthenticationAgent
# which is introduced with PolicyKit-0.7
bus = dbus.SessionBus()
obj = bus.get_object("org.freedesktop.PolicyKit.AuthenticationAgent",
"/")
pkit = dbus.Interface(obj,
"org.freedesktop.PolicyKit.AuthenticationAgent")
pkit.ObtainAuthorization(action, 0, os.getpid())
except dbus.exceptions.DBusException, e:
if (e.get_dbus_name() != "org.freedesktop.DBus.Error.ServiceUnknown"):
raise
# If PolicyKit < 0.7, fallback to org.gnome.PolicyKit
logging.debug("Falling back to org.gnome.PolicyKit")
obj = bus.get_object("org.gnome.PolicyKit",
"/org/gnome/PolicyKit/Manager")
pkit = dbus.Interface(obj, "org.gnome.PolicyKit.Manager")
pkit.ShowDialog(action, 0)
return 0
def creds_dialog(creds):
"""
Thread safe wrapper for libvirt openAuth user/pass callback
@ -199,10 +153,12 @@ def acquire_tgt():
"""
logging.debug("In acquire tgt.")
try:
bus = dbus.SessionBus()
ka = bus.get_object('org.gnome.KrbAuthDialog',
'/org/gnome/KrbAuthDialog')
ret = ka.acquireTgt("", dbus_interface='org.gnome.KrbAuthDialog')
bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
ka = Gio.DBusProxy.new_sync(bus, 0, None,
"org.gnome.KrbAuthDialog",
"/org/gnome/KrbAuthDialog",
"org.freedesktop.KrbAuthDialog", None)
ret = ka.acquireTgt("(s)", "")
except Exception, e:
logging.info("Cannot acquire tgt" + str(e))
ret = False

View File

@ -928,13 +928,10 @@ class vmmConnection(vmmGObject):
"""
ignore = cbdata
try:
if (len(creds) == 1 and
creds[0][0] == libvirt.VIR_CRED_EXTERNAL and
creds[0][2] == "PolicyKit"):
return connectauth.creds_polkit(creds[0][1])
for cred in creds:
if cred[0] == libvirt.VIR_CRED_EXTERNAL:
logging.debug("Don't know how to handle external cred %s",
cred[2])
return -1
return connectauth.creds_dialog(creds)

View File

@ -190,7 +190,6 @@ class vmmEngine(vmmGObject):
logging.debug("Determining default libvirt URI")
ret = None
did_install_libvirt = False
try:
libvirt_packages = self.config.libvirt_packages
packages = self.config.hv_packages + libvirt_packages

View File

@ -19,11 +19,10 @@
#
# pylint: disable=E0611
from gi.repository import Gio
from gi.repository import Gtk
# pylint: enable=E0611
import dbus
import logging
import time
import traceback
@ -40,18 +39,18 @@ def check_packagekit(errbox, packages, ishv):
Returns None when we determine nothing useful.
Returns (success, did we just install libvirt) otherwise.
"""
packages = ["avahi-tools"]
if not packages:
logging.debug("No PackageKit packages to search for.")
return
logging.debug("Asking PackageKit what's installed locally.")
try:
session = dbus.SystemBus()
pk_control = dbus.Interface(
session.get_object("org.freedesktop.PackageKit",
"/org/freedesktop/PackageKit"),
"org.freedesktop.PackageKit")
bus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
pk_control = Gio.DBusProxy.new_sync(bus, 0, None,
"org.freedesktop.PackageKit",
"/org/freedesktop/PackageKit",
"org.freedesktop.PackageKit", None)
except Exception:
logging.exception("Couldn't connect to packagekit")
return
@ -63,7 +62,7 @@ def check_packagekit(errbox, packages, ishv):
found = []
progWin = vmmAsyncJob(_do_async_search,
[session, pk_control, packages], msg, msg,
[bus, pk_control, packages], msg, msg,
errbox.get_parent(), async=False)
error, ignore = progWin.run()
if error:
@ -111,11 +110,11 @@ def check_packagekit(errbox, packages, ishv):
return do_install
def _do_async_search(asyncjob, session, pk_control, packages):
def _do_async_search(asyncjob, bus, pk_control, packages):
found = []
try:
for name in packages:
ret_found = packagekit_search(session, pk_control, name, packages)
ret_found = packagekit_search(bus, pk_control, name, packages)
found += ret_found
except Exception, e:
@ -126,34 +125,25 @@ def _do_async_search(asyncjob, session, pk_control, packages):
def packagekit_install(package_list):
session = dbus.SessionBus()
pk_control = dbus.Interface(
session.get_object("org.freedesktop.PackageKit",
"/org/freedesktop/PackageKit"),
"org.freedesktop.PackageKit.Modify")
bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
pk_control = Gio.DBusProxy.new_sync(bus, 0, None,
"org.freedesktop.PackageKit",
"/org/freedesktop/PackageKit",
"org.freedesktop.PackageKit.Modify", None)
# Set 2 hour timeout
timeout = 60 * 60 * 2
timeout = 1000 * 60 * 60 * 2
logging.debug("Installing packages: %s", package_list)
pk_control.InstallPackageNames(dbus.UInt32(0),
pk_control.InstallPackageNames("(uass)", 0,
package_list, "hide-confirm-search",
timeout=timeout)
def packagekit_search(session, pk_control, package_name, packages):
newstyle = False
try:
tid = pk_control.GetTid()
except dbus.exceptions.DBusException, e:
if e.get_dbus_name() != "org.freedesktop.DBus.Error.UnknownMethod":
raise
newstyle = True
tid = pk_control.CreateTransaction()
pk_trans = dbus.Interface(
session.get_object("org.freedesktop.PackageKit", tid),
"org.freedesktop.PackageKit.Transaction")
def packagekit_search(bus, pk_control, package_name, packages):
tid = pk_control.CreateTransaction()
pk_trans = Gio.DBusProxy.new_sync(bus, 0, None,
"org.freedesktop.PackageKit", tid,
"org.freedesktop.PackageKit.Transaction", None)
found = []
def package(info, package_id, summary):
@ -171,20 +161,18 @@ def packagekit_search(session, pk_control, package_name, packages):
def finished(ignore, runtime_ignore):
Gtk.main_quit()
pk_trans.connect_to_signal('Finished', finished)
pk_trans.connect_to_signal('ErrorCode', error)
pk_trans.connect_to_signal('Package', package)
try:
searchtype = "installed"
if newstyle:
searchtype = 2 ** 2
pk_trans.SearchNames(searchtype, [package_name])
except dbus.exceptions.DBusException, e:
if e.get_dbus_name() != "org.freedesktop.DBus.Error.UnknownMethod":
raise
def signal_cb(proxy, sender, signal, args):
ignore = proxy
sender = proxy
if signal == "Finished":
finished(*args)
elif signal == "ErrorCode":
error(*args)
elif signal == "Package":
package(*args)
# Try older search API
pk_trans.SearchName("installed", package_name)
pk_trans.connect("g-signal", signal_cb)
pk_trans.SearchNames("(tas)", 2 ** 2, [package_name])
# Call main() so this function is synchronous
Gtk.main()
@ -204,28 +192,29 @@ def start_libvirtd():
unitname = "libvirtd.service"
try:
bus = dbus.SystemBus()
bus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
except:
logging.exception("Error getting system bus handle")
return
try:
systemd = dbus.Interface(bus.get_object(
systemd = Gio.DBusProxy.new_sync(bus, 0, None,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1"),
"org.freedesktop.systemd1.Manager")
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager", None)
except:
logging.exception("Couldn't connect to systemd")
return
try:
unitpath = systemd.GetUnit(unitname)
proxy = bus.get_object("org.freedesktop.systemd1", unitpath)
props = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
state = props.Get("org.freedesktop.systemd1.Unit", "ActiveState")
unitpath = systemd.GetUnit("(s)", unitname)
unit = Gio.DBusProxy.new_sync(bus, 0, None,
"org.freedesktop.systemd1", unitpath,
"org.freedesktop.systemd1.Unit", None)
state = unit.get_cached_property("ActiveState")
logging.debug("libvirtd state=%s", state)
if str(state).lower() == "active":
if str(state).lower().strip("'") == "active":
logging.debug("libvirtd already active, not starting")
return True
except:
@ -236,11 +225,11 @@ def start_libvirtd():
try:
logging.debug("libvirtd not running, asking system-config-services "
"to start it")
scs = dbus.Interface(bus.get_object(
scs = Gio.DBusProxy.new_sync(bus, 0, None,
"org.fedoraproject.Config.Services",
"/org/fedoraproject/Config/Services/systemd1"),
"org.freedesktop.systemd1.Manager")
scs.StartUnit(unitname, "replace")
"/org/fedoraproject/Config/Services/systemd1",
"org.freedesktop.systemd1.Manager", None)
scs.StartUnit("(ss)", unitname, "replace")
time.sleep(2)
logging.debug("Starting libvirtd appeared to succeed")
return True