[libvirt] [libvirt-glib][PATCH 1/2] gobject: Introduce gvir_connection_get_hypervisor_name

which is basically a wrapper for virConnectGetType(). --- examples/conn-test.c | 12 ++++++ libvirt-gobject/libvirt-gobject-connection.c | 53 ++++++++++++++++++++++++++ libvirt-gobject/libvirt-gobject-connection.h | 3 + libvirt-gobject/libvirt-gobject.sym | 5 ++ 4 files changed, 73 insertions(+), 0 deletions(-) diff --git a/examples/conn-test.c b/examples/conn-test.c index 8c70997..b90d2b0 100644 --- a/examples/conn-test.c +++ b/examples/conn-test.c @@ -31,11 +31,23 @@ do_connection_open(GObject *source, { GVirConnection *conn = GVIR_CONNECTION(source); GError *err = NULL; + gchar *hv_name = NULL; if (!gvir_connection_open_finish(conn, res, &err)) { g_error("%s", err->message); + goto cleanup; } g_print("Connected to libvirt\n"); + + if (!(hv_name = gvir_connection_get_hypervisor_name(conn, &err))) { + g_error("%s", err->message); + goto cleanup; + } + + g_print("Hypervisor name: %s\n", hv_name); + +cleanup: + g_free(hv_name); g_object_unref(conn); } diff --git a/libvirt-gobject/libvirt-gobject-connection.c b/libvirt-gobject/libvirt-gobject-connection.c index d826905..f0be875 100644 --- a/libvirt-gobject/libvirt-gobject-connection.c +++ b/libvirt-gobject/libvirt-gobject-connection.c @@ -1025,6 +1025,59 @@ const gchar *gvir_connection_get_uri(GVirConnection *conn) return conn->priv->uri; } +/** + * gvir_connection_get_hypervisor_name: + * @conn: a #GVirConnection + * @err: return location for any #GError + * + * Get name of current hypervisor used. + * + * Return value: new string that should be freed when no longer needed, + * or NULL upon error. + */ +gchar * +gvir_connection_get_hypervisor_name(GVirConnection *conn, + GError **err) +{ + GVirConnectionPrivate *priv; + virConnectPtr vconn = NULL; + gchar *ret = NULL; + const char *type; + + g_return_val_if_fail(GVIR_IS_CONNECTION(conn), NULL); + + priv = conn->priv; + g_mutex_lock(priv->lock); + if (!priv->conn) { + g_set_error_literal(err, GVIR_CONNECTION_ERROR, 0, + "Connection is not open"); + g_mutex_unlock(priv->lock); + goto cleanup; + } + + vconn = priv->conn; + /* Stop another thread closing the connection just at the minute */ + virConnectRef(vconn); + g_mutex_unlock(priv->lock); + + type = virConnectGetType(priv->conn); + if (!type) { + gvir_set_error_literal(err, GVIR_CONNECTION_ERROR, 0, + "Unable to get hypervisor name"); + goto cleanup; + } + + ret = g_strdup(type); + +cleanup: + if (vconn) { + g_mutex_lock(priv->lock); + virConnectClose(vconn); + g_mutex_unlock(priv->lock); + } + return ret; +} + static void gvir_domain_ref(gpointer obj, gpointer ignore G_GNUC_UNUSED) { g_object_ref(obj); diff --git a/libvirt-gobject/libvirt-gobject-connection.h b/libvirt-gobject/libvirt-gobject-connection.h index d658b01..62eb024 100644 --- a/libvirt-gobject/libvirt-gobject-connection.h +++ b/libvirt-gobject/libvirt-gobject-connection.h @@ -112,6 +112,9 @@ gboolean gvir_connection_fetch_domains_finish(GVirConnection *conn, const gchar *gvir_connection_get_uri(GVirConnection *conn); +gchar *gvir_connection_get_hypervisor_name(GVirConnection *conn, + GError **err); + GList *gvir_connection_get_domains(GVirConnection *conn); GVirDomain *gvir_connection_get_domain(GVirConnection *conn, diff --git a/libvirt-gobject/libvirt-gobject.sym b/libvirt-gobject/libvirt-gobject.sym index 67e5a4f..2c2f1f4 100644 --- a/libvirt-gobject/libvirt-gobject.sym +++ b/libvirt-gobject/libvirt-gobject.sym @@ -194,4 +194,9 @@ LIBVIRT_GOBJECT_0.1.2 { gvir_domain_resume_finish; } LIBVIRT_GOBJECT_0.1.1; +LIBVIRT_GOBJECT_0.1.3 { + global: + gvir_connection_get_hypervisor_name; +} LIBVIRT_GOBJECT_0.1.2; + # .... define new API here using predicted next version number .... -- 1.7.8.6

which is basically a wrapper for virConnectGetVersion(). --- examples/conn-test.c | 13 ++++++ libvirt-gobject/libvirt-gobject-connection.c | 52 ++++++++++++++++++++++++++ libvirt-gobject/libvirt-gobject-connection.h | 4 +- libvirt-gobject/libvirt-gobject.sym | 1 + 4 files changed, 69 insertions(+), 1 deletions(-) diff --git a/examples/conn-test.c b/examples/conn-test.c index b90d2b0..b5d60c0 100644 --- a/examples/conn-test.c +++ b/examples/conn-test.c @@ -32,6 +32,8 @@ do_connection_open(GObject *source, GVirConnection *conn = GVIR_CONNECTION(source); GError *err = NULL; gchar *hv_name = NULL; + gulong hv_version = 0; + guint major, minor, micro; if (!gvir_connection_open_finish(conn, res, &err)) { g_error("%s", err->message); @@ -46,6 +48,17 @@ do_connection_open(GObject *source, g_print("Hypervisor name: %s\n", hv_name); + if (!gvir_connection_get_version(conn, &hv_version, &err)) { + g_error("%s", err->message); + goto cleanup; + } + + major = hv_version / 1000000; + hv_version %= 1000000; + minor = hv_version / 1000; + micro = hv_version % 1000; + g_print("Hypervisor version: %u.%u.%u\n", major, minor, micro); + cleanup: g_free(hv_name); g_object_unref(conn); diff --git a/libvirt-gobject/libvirt-gobject-connection.c b/libvirt-gobject/libvirt-gobject-connection.c index f0be875..bc11151 100644 --- a/libvirt-gobject/libvirt-gobject-connection.c +++ b/libvirt-gobject/libvirt-gobject-connection.c @@ -1078,6 +1078,58 @@ cleanup: return ret; } +/** + * gvir_connection_get_version: + * @conn: a #GVirConnection + * @version: return location for version + * @err: return location for any #GError + * + * Get version of current hypervisor used. + * + * Return value: TRUE on success, FALSE otherwise. + */ +gboolean +gvir_connection_get_version(GVirConnection *conn, + gulong *version, + GError **err) +{ + GVirConnectionPrivate *priv; + virConnectPtr vconn = NULL; + gboolean ret = FALSE; + + g_return_val_if_fail(GVIR_IS_CONNECTION(conn), FALSE); + + priv = conn->priv; + g_mutex_lock(priv->lock); + if (!priv->conn) { + g_set_error_literal(err, GVIR_CONNECTION_ERROR, 0, + "Connection is not open"); + g_mutex_unlock(priv->lock); + goto cleanup; + } + + vconn = priv->conn; + /* Stop another thread closing the connection just at the minute */ + virConnectRef(vconn); + g_mutex_unlock(priv->lock); + + if ( virConnectGetVersion(priv->conn, version) < 0) { + gvir_set_error_literal(err, GVIR_CONNECTION_ERROR, 0, + "Unable to get hypervisor version"); + goto cleanup; + } + + ret = TRUE; + +cleanup: + if (vconn) { + g_mutex_lock(priv->lock); + virConnectClose(vconn); + g_mutex_unlock(priv->lock); + } + return ret; +} + static void gvir_domain_ref(gpointer obj, gpointer ignore G_GNUC_UNUSED) { g_object_ref(obj); diff --git a/libvirt-gobject/libvirt-gobject-connection.h b/libvirt-gobject/libvirt-gobject-connection.h index 62eb024..42b12f3 100644 --- a/libvirt-gobject/libvirt-gobject-connection.h +++ b/libvirt-gobject/libvirt-gobject-connection.h @@ -114,7 +114,9 @@ const gchar *gvir_connection_get_uri(GVirConnection *conn); gchar *gvir_connection_get_hypervisor_name(GVirConnection *conn, GError **err); - +gboolean gvir_connection_get_version(GVirConnection *conn, + gulong *version, + GError **err); GList *gvir_connection_get_domains(GVirConnection *conn); GVirDomain *gvir_connection_get_domain(GVirConnection *conn, diff --git a/libvirt-gobject/libvirt-gobject.sym b/libvirt-gobject/libvirt-gobject.sym index 2c2f1f4..c496540 100644 --- a/libvirt-gobject/libvirt-gobject.sym +++ b/libvirt-gobject/libvirt-gobject.sym @@ -197,6 +197,7 @@ LIBVIRT_GOBJECT_0.1.2 { LIBVIRT_GOBJECT_0.1.3 { global: gvir_connection_get_hypervisor_name; + gvir_connection_get_version; } LIBVIRT_GOBJECT_0.1.2; # .... define new API here using predicted next version number .... -- 1.7.8.6

On Tue, Sep 11, 2012 at 07:42:42PM +0200, Michal Privoznik wrote:
which is basically a wrapper for virConnectGetVersion(). --- examples/conn-test.c | 13 ++++++ libvirt-gobject/libvirt-gobject-connection.c | 52 ++++++++++++++++++++++++++ libvirt-gobject/libvirt-gobject-connection.h | 4 +- libvirt-gobject/libvirt-gobject.sym | 1 + 4 files changed, 69 insertions(+), 1 deletions(-)
diff --git a/libvirt-gobject/libvirt-gobject-connection.c b/libvirt-gobject/libvirt-gobject-connection.c index f0be875..bc11151 100644 --- a/libvirt-gobject/libvirt-gobject-connection.c +++ b/libvirt-gobject/libvirt-gobject-connection.c @@ -1078,6 +1078,58 @@ cleanup: return ret; }
+/** + * gvir_connection_get_version: + * @conn: a #GVirConnection + * @version: return location for version + * @err: return location for any #GError + * + * Get version of current hypervisor used. + * + * Return value: TRUE on success, FALSE otherwise. + */ +gboolean +gvir_connection_get_version(GVirConnection *conn, + gulong *version, + GError **err) +{ + GVirConnectionPrivate *priv; + virConnectPtr vconn = NULL; + gboolean ret = FALSE; + + g_return_val_if_fail(GVIR_IS_CONNECTION(conn), FALSE); + + priv = conn->priv; + g_mutex_lock(priv->lock); + if (!priv->conn) { + g_set_error_literal(err, GVIR_CONNECTION_ERROR, 0, + "Connection is not open"); + g_mutex_unlock(priv->lock); + goto cleanup; + } + + vconn = priv->conn; + /* Stop another thread closing the connection just at the minute */ + virConnectRef(vconn); + g_mutex_unlock(priv->lock); + + if ( virConnectGetVersion(priv->conn, version) < 0) {
Bogus extra space after the first '('
+ gvir_set_error_literal(err, GVIR_CONNECTION_ERROR, 0, + "Unable to get hypervisor version"); + goto cleanup; + } + + ret = TRUE; + +cleanup: + if (vconn) { + g_mutex_lock(priv->lock); + virConnectClose(vconn); + g_mutex_unlock(priv->lock);
No need for this extra locking here 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 :|

On Tue, Sep 11, 2012 at 07:42:42PM +0200, Michal Privoznik wrote:
+gboolean +gvir_connection_get_version(GVirConnection *conn, + gulong *version, + GError **err)
gulong gvir_connection_get_version(GVirConnection *conn, GError **error) would feel more natural here as errors can be reported through the GError.
+{ + GVirConnectionPrivate *priv; + virConnectPtr vconn = NULL; + gboolean ret = FALSE; + + g_return_val_if_fail(GVIR_IS_CONNECTION(conn), FALSE);
Same comment as in the previous patch, I'd add a g_return_val_if_fail(err == NULL || *err == NULL, 0);
+ + priv = conn->priv; + g_mutex_lock(priv->lock); + if (!priv->conn) { + g_set_error_literal(err, GVIR_CONNECTION_ERROR, 0, + "Connection is not open"); + g_mutex_unlock(priv->lock); + goto cleanup; + } + + vconn = priv->conn; + /* Stop another thread closing the connection just at the minute */ + virConnectRef(vconn); + g_mutex_unlock(priv->lock); + + if ( virConnectGetVersion(priv->conn, version) < 0) {
extra space before 'virConnectGetVersion' Looks good otherwise. Christophe

On Tue, Sep 11, 2012 at 07:42:41PM +0200, Michal Privoznik wrote:
which is basically a wrapper for virConnectGetType(). --- examples/conn-test.c | 12 ++++++ libvirt-gobject/libvirt-gobject-connection.c | 53 ++++++++++++++++++++++++++ libvirt-gobject/libvirt-gobject-connection.h | 3 + libvirt-gobject/libvirt-gobject.sym | 5 ++ 4 files changed, 73 insertions(+), 0 deletions(-)
diff --git a/examples/conn-test.c b/examples/conn-test.c index 8c70997..b90d2b0 100644 --- a/examples/conn-test.c +++ b/examples/conn-test.c @@ -31,11 +31,23 @@ do_connection_open(GObject *source, { GVirConnection *conn = GVIR_CONNECTION(source); GError *err = NULL; + gchar *hv_name = NULL;
if (!gvir_connection_open_finish(conn, res, &err)) { g_error("%s", err->message); + goto cleanup; } g_print("Connected to libvirt\n"); + + if (!(hv_name = gvir_connection_get_hypervisor_name(conn, &err))) { + g_error("%s", err->message); + goto cleanup; + } + + g_print("Hypervisor name: %s\n", hv_name); + +cleanup: + g_free(hv_name); g_object_unref(conn); }
diff --git a/libvirt-gobject/libvirt-gobject-connection.c b/libvirt-gobject/libvirt-gobject-connection.c index d826905..f0be875 100644 --- a/libvirt-gobject/libvirt-gobject-connection.c +++ b/libvirt-gobject/libvirt-gobject-connection.c @@ -1025,6 +1025,59 @@ const gchar *gvir_connection_get_uri(GVirConnection *conn) return conn->priv->uri; }
+/** + * gvir_connection_get_hypervisor_name: + * @conn: a #GVirConnection + * @err: return location for any #GError + * + * Get name of current hypervisor used. + * + * Return value: new string that should be freed when no longer needed, + * or NULL upon error. + */ +gchar * +gvir_connection_get_hypervisor_name(GVirConnection *conn, + GError **err) +{ + GVirConnectionPrivate *priv; + virConnectPtr vconn = NULL; + gchar *ret = NULL; + const char *type; + + g_return_val_if_fail(GVIR_IS_CONNECTION(conn), NULL); + + priv = conn->priv; + g_mutex_lock(priv->lock); + if (!priv->conn) { + g_set_error_literal(err, GVIR_CONNECTION_ERROR, 0, + "Connection is not open"); + g_mutex_unlock(priv->lock); + goto cleanup; + } + + vconn = priv->conn; + /* Stop another thread closing the connection just at the minute */ + virConnectRef(vconn); + g_mutex_unlock(priv->lock); + + type = virConnectGetType(priv->conn); + if (!type) { + gvir_set_error_literal(err, GVIR_CONNECTION_ERROR, 0, + "Unable to get hypervisor name"); + goto cleanup; + } + + ret = g_strdup(type); + +cleanup: + if (vconn) { + g_mutex_lock(priv->lock); + virConnectClose(vconn); + g_mutex_unlock(priv->lock); + }
There's no need to lock/unlock here, since you're not touching anything in 'priv', and virConnectPtr has its own locking as needed. ACK to the rest 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 :|

On Tue, Sep 11, 2012 at 07:42:41PM +0200, Michal Privoznik wrote:
which is basically a wrapper for virConnectGetType(). --- examples/conn-test.c | 12 ++++++ libvirt-gobject/libvirt-gobject-connection.c | 53 ++++++++++++++++++++++++++ libvirt-gobject/libvirt-gobject-connection.h | 3 + libvirt-gobject/libvirt-gobject.sym | 5 ++ 4 files changed, 73 insertions(+), 0 deletions(-)
diff --git a/examples/conn-test.c b/examples/conn-test.c index 8c70997..b90d2b0 100644 --- a/examples/conn-test.c +++ b/examples/conn-test.c @@ -31,11 +31,23 @@ do_connection_open(GObject *source, { GVirConnection *conn = GVIR_CONNECTION(source); GError *err = NULL; + gchar *hv_name = NULL;
if (!gvir_connection_open_finish(conn, res, &err)) { g_error("%s", err->message); + goto cleanup; } g_print("Connected to libvirt\n"); + + if (!(hv_name = gvir_connection_get_hypervisor_name(conn, &err))) { + g_error("%s", err->message); + goto cleanup; + } + + g_print("Hypervisor name: %s\n", hv_name); + +cleanup: + g_free(hv_name); g_object_unref(conn); }
diff --git a/libvirt-gobject/libvirt-gobject-connection.c b/libvirt-gobject/libvirt-gobject-connection.c index d826905..f0be875 100644 --- a/libvirt-gobject/libvirt-gobject-connection.c +++ b/libvirt-gobject/libvirt-gobject-connection.c @@ -1025,6 +1025,59 @@ const gchar *gvir_connection_get_uri(GVirConnection *conn) return conn->priv->uri; }
+/** + * gvir_connection_get_hypervisor_name: + * @conn: a #GVirConnection + * @err: return location for any #GError + * + * Get name of current hypervisor used. + * + * Return value: new string that should be freed when no longer needed, + * or NULL upon error. + */ +gchar * +gvir_connection_get_hypervisor_name(GVirConnection *conn, + GError **err) +{ + GVirConnectionPrivate *priv; + virConnectPtr vconn = NULL; + gchar *ret = NULL; + const char *type; + + g_return_val_if_fail(GVIR_IS_CONNECTION(conn), NULL);
Minor nit, I'd add a g_return_val_if_fail(err == NULL || *err == NULL, NULL); Looks good to me otherwise. Christophe
participants (3)
-
Christophe Fergeau
-
Daniel P. Berrange
-
Michal Privoznik