]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
conf: Add new helpers to resolve virDomainModificationImpact to domain defs
authorPeter Krempa <pkrempa@redhat.com>
Fri, 29 May 2015 12:37:20 +0000 (14:37 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Thu, 4 Jun 2015 08:52:31 +0000 (10:52 +0200)
virDomainLiveConfigHelperMethod that is used for this job now does
modify the flags but still requires the callers to extract the correct
definition objects.

In addition coverity and other static analyzers are usually unhappy as
they don't grasp the fact that @flags are upadted according to the
correct def to be present.

To work this issue around and simplify the calling chain let's add a new
helper that will work only on drivers that always copy the persistent
def to a transient at start of a vm. This will allow to drop a few
arguments. The new function syntax will also fill two definition
pointers rather than modifying the @flags parameter.

src/conf/domain_conf.c
src/conf/domain_conf.h
src/libvirt_private.syms

index 2a12a0653a743f579c419bc3cbbbbd6b259230f2..77e198cc22aaff9f9d2766d238f95b67f3a5f4ec 100644 (file)
@@ -2824,24 +2824,25 @@ virDomainObjGetPersistentDef(virCapsPtr caps,
         return domain->def;
 }
 
-/*
- * Helper method for --current, --live, and --config options, and check
- * whether domain is active or can get persistent domain configuration.
+
+/**
+ * virDomainObjUpdateModificationImpact:
  *
- * Return 0 if success, also change the flags and get the persistent
- * domain configuration if needed. Return -1 on error.
+ * @vm: domain object
+ * @flags: flags to update the modification impact on
+ *
+ * Resolves virDomainModificationImpact flags in @flags so that they correctly
+ * apply to the actual state of @vm. @flags may be modified after call to this
+ * function.
+ *
+ * Returns 0 on success if @flags point to a valid combination for @vm or -1 on
+ * error.
  */
 int
-virDomainLiveConfigHelperMethod(virCapsPtr caps,
-                                virDomainXMLOptionPtr xmlopt,
-                                virDomainObjPtr dom,
-                                unsigned int *flags,
-                                virDomainDefPtr *persistentDef)
+virDomainObjUpdateModificationImpact(virDomainObjPtr vm,
+                                     unsigned int *flags)
 {
-    bool isActive;
-    int ret = -1;
-
-    isActive = virDomainObjIsActive(dom);
+    bool isActive = virDomainObjIsActive(vm);
 
     if ((*flags & (VIR_DOMAIN_AFFECT_LIVE | VIR_DOMAIN_AFFECT_CONFIG)) ==
         VIR_DOMAIN_AFFECT_CURRENT) {
@@ -2854,29 +2855,99 @@ virDomainLiveConfigHelperMethod(virCapsPtr caps,
     if (!isActive && (*flags & VIR_DOMAIN_AFFECT_LIVE)) {
         virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                        _("domain is not running"));
-        goto cleanup;
+        return -1;
     }
 
     if (*flags & VIR_DOMAIN_AFFECT_CONFIG) {
-        if (!dom->persistent) {
+        if (!vm->persistent) {
             virReportError(VIR_ERR_OPERATION_INVALID, "%s",
                            _("transient domains do not have any "
                              "persistent config"));
-            goto cleanup;
+            return -1;
         }
+    }
+
+    return 0;
+}
+
+
+/*
+ * Helper method for --current, --live, and --config options, and check
+ * whether domain is active or can get persistent domain configuration.
+ *
+ * Return 0 if success, also change the flags and get the persistent
+ * domain configuration if needed. Return -1 on error.
+ */
+int
+virDomainLiveConfigHelperMethod(virCapsPtr caps,
+                                virDomainXMLOptionPtr xmlopt,
+                                virDomainObjPtr dom,
+                                unsigned int *flags,
+                                virDomainDefPtr *persistentDef)
+{
+    if (virDomainObjUpdateModificationImpact(dom, flags) < 0)
+        return -1;
+
+    if (*flags & VIR_DOMAIN_AFFECT_CONFIG) {
         if (!(*persistentDef = virDomainObjGetPersistentDef(caps, xmlopt, dom))) {
             virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                            _("Get persistent config failed"));
-            goto cleanup;
+            return -1;
         }
     }
 
-    ret = 0;
+    return 0;
+}
 
- cleanup:
-    return ret;
+
+/**
+ * virDomainObjGetDefs:
+ *
+ * @vm: domain object
+ * @flags: for virDomainModificationImpact
+ * @liveDef: Set to the pointer to the live definition of @vm.
+ * @persDef: Set to the pointer to the config definition of @vm.
+ *
+ * Helper function to resolve @flags and retrieve correct domain pointer
+ * objects. This function should be used only when the hypervisor driver always
+ * creates vm->newDef once the vm is started. (qemu driver does that)
+ *
+ * If @liveDef or @persDef are set it implies that @flags request modification
+ * of thereof.
+ *
+ * Returns 0 on success and sets @liveDef and @persDef; -1 if @flags are
+ * inappropriate.
+ */
+int
+virDomainObjGetDefs(virDomainObjPtr vm,
+                    unsigned int flags,
+                    virDomainDefPtr *liveDef,
+                    virDomainDefPtr *persDef)
+{
+    if (liveDef)
+        *liveDef = NULL;
+
+    if (*persDef)
+        *persDef = NULL;
+
+    if (virDomainObjUpdateModificationImpact(vm, &flags) < 0)
+        return -1;
+
+    if (flags & VIR_DOMAIN_AFFECT_LIVE) {
+        if (liveDef)
+            *liveDef = vm->def;
+
+        if (persDef)
+            *liveDef = vm->newDef;
+    } else {
+        if (persDef)
+            *persDef = vm->def;
+    }
+
+    return 0;
 }
 
+
 /*
  * The caller must hold a lock on the driver owning 'doms',
  * and must also have locked 'dom', to ensure no one else
index 906ec4f9c5be6515fadeefcf19dc5e52bd9c4d98..ba17a8d732d3a73489732bb50e55f04fcef71ba6 100644 (file)
@@ -2546,6 +2546,14 @@ virDomainObjGetPersistentDef(virCapsPtr caps,
                              virDomainXMLOptionPtr xmlopt,
                              virDomainObjPtr domain);
 
+int virDomainObjUpdateModificationImpact(virDomainObjPtr vm,
+                                         unsigned int *flags);
+
+int virDomainObjGetDefs(virDomainObjPtr vm,
+                        unsigned int flags,
+                        virDomainDefPtr *liveDef,
+                        virDomainDefPtr *persDef);
+
 int
 virDomainLiveConfigHelperMethod(virCapsPtr caps,
                                 virDomainXMLOptionPtr xmlopt,
index 7b502aa31d699bd1cbfa20673d1e66b8d4adf096..55a5e19ed02f36c85daf40fe5cd334bd6d21605d 100644 (file)
@@ -383,6 +383,7 @@ virDomainObjAssignDef;
 virDomainObjCopyPersistentDef;
 virDomainObjEndAPI;
 virDomainObjFormat;
+virDomainObjGetDefs;
 virDomainObjGetMetadata;
 virDomainObjGetPersistentDef;
 virDomainObjGetState;
@@ -408,6 +409,7 @@ virDomainObjSetDefTransient;
 virDomainObjSetMetadata;
 virDomainObjSetState;
 virDomainObjTaint;
+virDomainObjUpdateModificationImpact;
 virDomainOSTypeFromString;
 virDomainOSTypeToString;
 virDomainParseMemory;