From 1472f34ab7abc8204e9ea8a2078a84d84f48b1f1 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 1 Jan 1970 00:00:00 +0000 Subject: [PATCH] D.get_id returns -1 for inactive domains instead of throwing an error. --- examples/list_domains.ml | 10 ++++++---- libvirt/libvirt.mli | 8 ++------ libvirt/libvirt_c_oneoffs.c | 9 ++++----- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/examples/list_domains.ml b/examples/list_domains.ml index 6b04932..5cd93c7 100644 --- a/examples/list_domains.ml +++ b/examples/list_domains.ml @@ -32,11 +32,13 @@ let () = let domains = D.get_domains_and_infos conn [D.ListAll] in List.iter ( fun (dom, info) -> - if info.D.state <> D.InfoShutoff then - printf "%8d %-20s %s\n%!" - (D.get_id dom) (D.get_name dom) (string_of_state info.D.state) + let id = D.get_id dom in + let name = D.get_name dom in + let state = string_of_state info.D.state in + if id >= 0 then + printf "%8d %-20s %s\n%!" id name state else - printf "%8s %-20s shutoff\n%!" "" (D.get_name dom) + printf "%8s %-20s %s\n%!" "inactive" name state ) domains with Libvirt.Virterror err -> diff --git a/libvirt/libvirt.mli b/libvirt/libvirt.mli index 1dfdc25..8de1a45 100644 --- a/libvirt/libvirt.mli +++ b/libvirt/libvirt.mli @@ -543,12 +543,8 @@ sig val get_uuid_string : [>`R] t -> string (** Get the domain UUID (as a printable string). *) val get_id : [>`R] t -> int - (** [getid dom] returns the ID of the domain. - - Do not call this on a defined but not running domain. Those - domains don't have IDs, and you'll get an error here. - *) - + (** [get_id dom] returns the ID of the domain. In most cases + this returns [-1] if the domain is not running. *) val get_os_type : [>`R] t -> string (** Get the operating system type. *) val get_max_memory : [>`R] t -> int64 diff --git a/libvirt/libvirt_c_oneoffs.c b/libvirt/libvirt_c_oneoffs.c index 02831da..d87dd21 100644 --- a/libvirt/libvirt_c_oneoffs.c +++ b/libvirt/libvirt_c_oneoffs.c @@ -284,15 +284,14 @@ ocaml_libvirt_domain_get_id (value domv) { CAMLparam1 (domv); virDomainPtr dom = Domain_val (domv); - virConnectPtr conn = Connect_domv (domv); + /*virConnectPtr conn = Connect_domv (domv);*/ unsigned int r; NONBLOCKING (r = virDomainGetID (dom)); - /* There's a bug in libvirt which means that if you try to get - * the ID of a defined-but-not-running domain, it returns -1, - * and there's no way to distinguish that from an error. + /* In theory this could return -1 on error, but in practice + * libvirt never does this unless you call it with a corrupted + * or NULL dom object. So ignore errors here. */ - CHECK_ERROR (r == (unsigned int) -1, conn, "virDomainGetID"); CAMLreturn (Val_int ((int) r)); } -- 1.8.3.1