Re: [PATCH 1/2] dom event example: init before register event impl

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 09.05.2013 23:17, Jesse J. Cook wrote:
> From: "Jesse J. Cook" <jesse.j.cook@xxxxxxxxxxxxxx>
> 
> In the domain-events example C code virEventRegisterDefaultImpl was being
> called before virConnectOpen without first calling virInitialize. While this
> code worked, it is incorrect. Adding a call to g_string_new prior to the call
> to virEventRegisterDefaultImpl would cause the code to break. This fix will
> help avoid unintentional misue of the API.

Not even g_string_new but even our doc require virInitialize to be called prior any libvirt API other than virConnectOpen*(). This rule includes VirEventRegisterDefaultImpl().

> 
> Relates to: Ret Hat Bugzilla - Bug 961155
> ---
>  examples/domain-events/events-c/event-test.c |    6 ++++++
>  1 file changed, 6 insertions(+)
> 
> diff --git a/examples/domain-events/events-c/event-test.c b/examples/domain-events/events-c/event-test.c
> index ede9796..09ec6aa 100644
> --- a/examples/domain-events/events-c/event-test.c
> +++ b/examples/domain-events/events-c/event-test.c
> @@ -468,6 +468,12 @@ int main(int argc, char **argv)
>          return -1;
>      }
>  
> +    if(0 != virInitialize()) {

Actually, we prefer the other way:
  if (virInitialize() < 0) {

> +        virErrorPtr err = virGetLastError();
> +        fprintf(stderr, "Failed to initialize: %s\n",
> +                err && err->message ? err->message: "Unknown error");

s/:/ :/
Moreover, we should return here, shouldn't we?
And are we save to call virGetLastError()? I don't think so. I mean, the first thing
that virInitialize() does, is thread library initialize (on linux it's nada function,
but not on windows), then it initialize thread local variable - the virError object.
And what if this fails for some reason - then virgetLastError may access invalid memory.
So I thing we should just fprintf(stderr, "Failed to initialize libvirt");

> +    }
> +
>      virEventRegisterDefaultImpl();
>  
>      virConnectPtr dconn = NULL;
> 

However, this is a nasty violation of our own rules, so ACK with this squashed in:

diff --git a/examples/domain-events/events-c/event-test.c b/examples/domain-events/events-c/event-test.c
index 09ec6aa..0f7be55 100644
--- a/examples/domain-events/events-c/event-test.c
+++ b/examples/domain-events/events-c/event-test.c
@@ -468,10 +468,9 @@ int main(int argc, char **argv)
         return -1;
     }
 
-    if(0 != virInitialize()) {
-        virErrorPtr err = virGetLastError();
-        fprintf(stderr, "Failed to initialize: %s\n",
-                err && err->message ? err->message: "Unknown error");
+    if (virInitialize() < 0) {
+        fprintf(stderr, "Failed to initialize libvirt");
+        return -1;
     }
 
     virEventRegisterDefaultImpl();


Michal

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]