[virt-tools-list] [virt-viewer] [PATCH] Listen to virNetwork's Lifecycle events

Fabiano Fidêncio fabiano at fidencio.org
Wed Nov 19 04:24:37 UTC 2014


On Wed, Nov 19, 2014 at 4:31 AM, Fabiano Fidêncio <fidencio at redhat.com> wrote:
> Apart from the usual virDomain's Lifecycle events, we have to listen to
> the virNetwork's Lifecycle events in order to get notifications about
> libvirt deamon being started/stopped/restarted, which is useful when
> connecting to guest through qemu+ssh://
>
> Resolves: rhbz#1164052
> https://bugzilla.redhat.com/show_bug.cgi?id=1164052
> ---
>  src/virt-viewer.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++----
>  1 file changed, 49 insertions(+), 4 deletions(-)
>
> diff --git a/src/virt-viewer.c b/src/virt-viewer.c
> index dc16b3f..c785f75 100644
> --- a/src/virt-viewer.c
> +++ b/src/virt-viewer.c
> @@ -506,6 +506,38 @@ virt_viewer_domain_event(virConnectPtr conn G_GNUC_UNUSED,
>      return 0;
>  }
>
> +static int
> +virt_viewer_network_event(virConnectPtr conn G_GNUC_UNUSED,
> +                          virNetworkPtr net G_GNUC_UNUSED,
> +                          int event,
> +                          int detail G_GNUC_UNUSED,
> +                          void *opaque)
> +{
> +    VirtViewer *self = opaque;
> +    VirtViewerApp *app = VIRT_VIEWER_APP(self);
> +    GError *error = NULL;
> +
> +    g_debug("Got domain event %d %d", event, detail);
> +
> +    switch (event) {
> +    case VIR_NETWORK_EVENT_STOPPED:
> +        //virt_viewer_deactivate(self);
> +        break;
> +
> +    case VIR_NETWORK_EVENT_STARTED:
> +        virt_viewer_app_activate(app, &error);
> +        if (error) {
> +            /* we may want to consolidate error reporting in
> +               app_activate() instead */
> +            g_warning("%s", error->message);
> +            g_clear_error(&error);
> +        }
> +        break;
> +    }
> +
> +    return 0;
> +}
> +
>  static void
>  virt_viewer_conn_event(virConnectPtr conn G_GNUC_UNUSED,
>                         int reason,
> @@ -787,6 +819,8 @@ virt_viewer_connect(VirtViewerApp *app)
>          .cbdata = app,
>      };
>      int oflags = 0;
> +    int lifecycle_event = -1;
> +    int network_event = -1;
>      GError *error = NULL;
>
>      if (!virt_viewer_app_get_attach(app))
> @@ -820,10 +854,21 @@ virt_viewer_connect(VirtViewerApp *app)
>          return -1;
>      }
>
> -    if (virConnectDomainEventRegister(priv->conn,
> -                                      virt_viewer_domain_event,
> -                                      self,
> -                                      NULL) < 0)
> +    lifecycle_event = virConnectDomainEventRegisterAny(priv->conn,
> +                                                       priv->dom,
> +                                                       VIR_DOMAIN_EVENT_ID_LIFECYCLE,
> +                                                       VIR_DOMAIN_EVENT_CALLBACK(virt_viewer_domain_event),
> +                                                       self,
> +                                                       NULL);
> +
> +    network_event = virConnectNetworkEventRegisterAny(priv->conn,
> +                                                      NULL,
> +                                                      VIR_NETWORK_EVENT_ID_LIFECYCLE,
> +                                                      VIR_NETWORK_EVENT_CALLBACK(virt_viewer_network_event),
> +                                                      self,
> +                                                      NULL);
> +
> +    if (lifecycle_event < 0 || network_event < 0)
>          priv->withEvents = FALSE;
>      else
>          priv->withEvents = TRUE;
> --
> 2.1.0
>
> _______________________________________________
> virt-tools-list mailing list
> virt-tools-list at redhat.com
> https://www.redhat.com/mailman/listinfo/virt-tools-list

self-nack!
Missing the Deregister part.

-- 
Fabiano Fidêncio




More information about the virt-tools-list mailing list