From 2d8b59c06007a50c93451a8bc7bfaee3926dc5a2 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Thu, 22 Jan 2015 16:50:33 +0000 Subject: [PATCH] systemd: avoid string comparisons on dbus error messages Add a virDBusErrorIsUnknownMethod helper so that callers don't need todo string comparisons themselves to detect standard error names. --- src/libvirt_private.syms | 1 + src/util/virdbus.c | 9 +++++++++ src/util/virdbus.h | 2 ++ src/util/virsystemd.c | 3 +-- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 528e93c20e..a8cd87f7ff 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1244,6 +1244,7 @@ virDBusCreateMethod; virDBusCreateMethodV; virDBusCreateReply; virDBusCreateReplyV; +virDBusErrorIsUnknownMethod; virDBusGetSessionBus; virDBusGetSystemBus; virDBusHasSystemBus; diff --git a/src/util/virdbus.c b/src/util/virdbus.c index 3522ae02e2..1cf1eef39a 100644 --- a/src/util/virdbus.c +++ b/src/util/virdbus.c @@ -1894,3 +1894,12 @@ void virDBusMessageUnref(DBusMessage *msg ATTRIBUTE_UNUSED) /* nothing */ } #endif /* ! WITH_DBUS */ + +bool virDBusErrorIsUnknownMethod(virErrorPtr err) +{ + return err->domain == VIR_FROM_DBUS && + err->code == VIR_ERR_DBUS_SERVICE && + err->level == VIR_ERR_ERROR && + STREQ_NULLABLE("org.freedesktop.DBus.Error.UnknownMethod", + err->str1); +} diff --git a/src/util/virdbus.h b/src/util/virdbus.h index e2b8d2b1bc..9e86538d5a 100644 --- a/src/util/virdbus.h +++ b/src/util/virdbus.h @@ -74,4 +74,6 @@ void virDBusMessageUnref(DBusMessage *msg); int virDBusIsServiceEnabled(const char *name); int virDBusIsServiceRegistered(const char *name); + +bool virDBusErrorIsUnknownMethod(virErrorPtr err); #endif /* __VIR_DBUS_H__ */ diff --git a/src/util/virsystemd.c b/src/util/virsystemd.c index 6de265be59..3ac399a1b4 100644 --- a/src/util/virsystemd.c +++ b/src/util/virsystemd.c @@ -281,8 +281,7 @@ int virSystemdCreateMachine(const char *name, goto cleanup; if (error.level == VIR_ERR_ERROR) { - if (STREQ_NULLABLE("org.freedesktop.DBus.Error.UnknownMethod", - error.str1)) { + if (virDBusErrorIsUnknownMethod(&error)) { VIR_INFO("CreateMachineWithNetwork isn't supported, switching " "to legacy CreateMachine method for systemd-machined"); virResetError(&error); -- GitLab