On 11/29/2011 09:14 PM, Daniel P. Berrange wrote:
From: "Daniel P. Berrange" <berrange(a)redhat.com>
+
+
+/**
+ * virNodeSuspendSupportsTarget:
+ * @target: The power management target to check whether it is supported
+ * by the host. Values could be:
+ * VIR_NODE_SUSPEND_TARGET_MEM
+ * VIR_NODE_SUSPEND_TARGET_DISK
+ * VIR_NODE_SUSPEND_TARGET_HYBRID
+ * @supported: set to true if supported, false otherwise
+ *
+ * Run the script 'pm-is-supported' (from the pm-utils package)
+ * to find out if @target is supported by the host.
+ *
+ * Returns 0 if the query was successful, -1 on failure.
+ */
+int
+virNodeSuspendSupportsTarget(unsigned int target, bool *supported)
+{
We can mark this function as static, since it is only ever called by
virNodeSuspendGetTargetMask().
+ virCommandPtr cmd;
+ int status;
+ int ret = -1;
+
+ *supported = false;
+
+ switch (target) {
+ case VIR_NODE_SUSPEND_TARGET_MEM:
+ cmd = virCommandNewArgList("pm-is-supported", "--suspend",
NULL);
+ break;
+ case VIR_NODE_SUSPEND_TARGET_DISK:
+ cmd = virCommandNewArgList("pm-is-supported", "--hibernate",
NULL);
+ break;
+ case VIR_NODE_SUSPEND_TARGET_HYBRID:
+ cmd = virCommandNewArgList("pm-is-supported",
"--suspend-hybrid", NULL);
+ break;
+ default:
+ return ret;
+ }
+
+ if (virCommandRun(cmd, &status) < 0)
+ goto cleanup;
+
+ /*
+ * Check return code of command == 0 for success
+ * (i.e., the PM capability is supported)
+ */
+ *supported = (status == 0);
+ ret = 0;
+
+cleanup:
+ virCommandFree(cmd);
+ return ret;
+}
+
+/**
+ * virNodeSuspendGetTargetMask:
+ *
+ * Get the Power Management Capabilities that the host system supports,
+ * such as Suspend-to-RAM (S3), Suspend-to-Disk (S4) and Hybrid-Suspend
+ * (a combination of S3 and S4).
+ *
+ * @bitmask: Pointer to the bitmask which will be set appropriately to
+ * indicate all the supported host power management targets.
+ *
+ * Returns 0 if the query was successful, -1 on failure.
+ */
+int
+virNodeSuspendGetTargetMask(unsigned int *bitmask)
+{
+ int ret;
+ bool supported;
+
+ *bitmask = 0;
+
+ /* Check support for Suspend-to-RAM (S3) */
+ ret = virNodeSuspendSupportsTarget(VIR_NODE_SUSPEND_TARGET_MEM, &supported);
+ if (ret < 0)
+ return -1;
+ if (supported)
+ *bitmask |= (1 << VIR_NODE_SUSPEND_TARGET_MEM);
+
+ /* Check support for Suspend-to-Disk (S4) */
+ ret = virNodeSuspendSupportsTarget(VIR_NODE_SUSPEND_TARGET_DISK, &supported);
+ if (ret < 0)
+ return -1;
+ if (supported)
+ *bitmask |= (1 << VIR_NODE_SUSPEND_TARGET_DISK);
+
+ /* Check support for Hybrid-Suspend */
+ ret = virNodeSuspendSupportsTarget(VIR_NODE_SUSPEND_TARGET_HYBRID, &supported);
+ if (ret < 0)
+ return -1;
+ if (supported)
+ *bitmask |= (1 << VIR_NODE_SUSPEND_TARGET_HYBRID);
+
+ return 0;
+}
diff --git a/src/util/virnodesuspend.h b/src/util/virnodesuspend.h
index 66e3214..1e23ce8 100644
--- a/src/util/virnodesuspend.h
+++ b/src/util/virnodesuspend.h
@@ -32,5 +32,7 @@ int nodeSuspendForDuration(virConnectPtr conn,
int virNodeSuspendInit(void);
+int virNodeSuspendSupportsTarget(unsigned int target, bool *supported);
+int virNodeSuspendGetTargetMask(unsigned int *bitmask);
#endif /* __VIR_NODE_SUSPEND_H__ */
--
Regards,
Srivatsa S. Bhat
IBM Linux Technology Center