On Tue, Sep 10, 2013 at 11:34:32AM +0800, Gao feng wrote: > This patch introduces virDBusIsServiceEnabled, we can use > this method to get if the service is supported. > > In one case, if org.freedesktop.machine1 is unavailable on > host, we should skip creating machine through systemd. > > Signed-off-by: Gao feng <gaofeng@xxxxxxxxxxxxxx> > --- > src/util/virdbus.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ > src/util/virdbus.h | 1 + > src/util/virsystemd.c | 11 ++-------- > 3 files changed, 62 insertions(+), 9 deletions(-) > > diff --git a/src/util/virdbus.c b/src/util/virdbus.c > index 62c31be..29068b0 100644 > --- a/src/util/virdbus.c > +++ b/src/util/virdbus.c > @@ -1207,6 +1207,58 @@ int virDBusMessageRead(DBusMessage *msg, > return ret; > } > > +/** > + * virDBusIsServiceEnabled: > + * @name: service name > + */ > +bool virDBusIsServiceEnabled(const char *name) IMHO this should be a tri-state so we can distinguish actual 'service not available' from other fatal errors. > +{ > + DBusConnection *conn; > + DBusMessage *reply = NULL; > + DBusMessageIter iter, sub; > + bool ret = false; > + > + if (!virDBusHasSystemBus()) > + return ret; > + > + conn = virDBusGetSystemBus(); > + > + if (virDBusCallMethod(conn, > + &reply, > + "org.freedesktop.DBus", > + "/org/freedesktop/DBus", > + "org.freedesktop.DBus", > + "ListActivatableNames", > + DBUS_TYPE_INVALID) < 0) { > + VIR_DEBUG("ListActivatableNames failed."); > + return ret; In particular this is a fatal error condition that should be treated as such. > + } > + > + if (!dbus_message_iter_init(reply, &iter) || > + dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) { > + VIR_DEBUG("Reply message incorrect."); > + goto cleanup; > + } > + > + dbus_message_iter_recurse(&iter, &sub); > + while (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_STRING) { > + const char *service = NULL; > + > + dbus_message_iter_get_basic(&sub, &service); > + dbus_message_iter_next(&sub); > + > + if (STREQ(service, name)) { > + ret = true; > + goto cleanup; > + } > + } > + > + cleanup: > + VIR_DEBUG("Service %s is %s", name, ret ? "available" : "unavailable"); > + dbus_message_unref(reply); > + return false; > +} > Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :| -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list