Patchwork [RFC,v4,10/18] virtagent: add agent_shutdown monitor command

login
register
mail settings
Submitter Michael Roth
Date Nov. 16, 2010, 4:01 p.m.
Message ID <1289923320-5638-11-git-send-email-mdroth@linux.vnet.ibm.com>
Download mbox | patch
Permalink /patch/71433/
State New
Headers show

Comments

Michael Roth - Nov. 16, 2010, 4:01 p.m.
Provide monitor command to initiate guest shutdown/reboot/powerdown

Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
 hmp-commands.hx |   16 +++++++++++++
 qmp-commands.hx |   32 ++++++++++++++++++++++++++
 virtagent.c     |   66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 virtagent.h     |    2 +
 4 files changed, 116 insertions(+), 0 deletions(-)
Jes Sorensen - Nov. 18, 2010, 2:19 p.m.
On 11/16/10 17:01, Michael Roth wrote:
> Provide monitor command to initiate guest shutdown/reboot/powerdown

> +int do_agent_shutdown(Monitor *mon, const QDict *mon_params,
> +                      MonitorCompletion cb, void *opaque)
> +{
> +    xmlrpc_env env;
> +    xmlrpc_value *params;
> +    VARPCData *rpc_data;
> +    const char *shutdown_type;
> +    int ret;
> +
> +    TRACE("called");
> +
> +    xmlrpc_env_init(&env);
> +    shutdown_type = qdict_get_str(mon_params, "shutdown_type");
> +    params = xmlrpc_build_value(&env, "(s)", shutdown_type);
> +    if (rpc_has_error(&env)) {
> +        return -1;
> +    }
> +
> +    rpc_data = qemu_mallocz(sizeof(VARPCData));
> +    rpc_data->cb = do_agent_shutdown_cb;
> +    rpc_data->mon_cb = cb;
> +    rpc_data->mon_data = opaque;
> +
> +    ret = rpc_execute(&env, "va_shutdown", params, rpc_data);
> +    if (ret == -EREMOTE) {
> +        monitor_printf(mon, "RPC Failed (%i): %s\n", env.fault_code,
> +                       env.fault_string);
> +        return -1;
> +    } else if (ret == -1) {
> +        monitor_printf(mon, "RPC communication error\n");
> +        return -1;
> +    }

I would think you could put a lot of this into a utility function
instead of having it open coded for each command you want to support?

Cheers,
Jes


> +
> +    return 0;
> +}
> diff --git a/virtagent.h b/virtagent.h
> index c077582..96c6260 100644
> --- a/virtagent.h
> +++ b/virtagent.h
> @@ -29,5 +29,7 @@ int do_agent_viewfile(Monitor *mon, const QDict *mon_params,
>  void do_agent_viewdmesg_print(Monitor *mon, const QObject *qobject);
>  int do_agent_viewdmesg(Monitor *mon, const QDict *mon_params,
>                        MonitorCompletion cb, void *opaque);
> +int do_agent_shutdown(Monitor *mon, const QDict *mon_params,
> +                      MonitorCompletion cb, void *opaque);
>  
>  #endif /* VIRTAGENT_H */

Patch

diff --git a/hmp-commands.hx b/hmp-commands.hx
index 5b9db62..3250e41 100644
--- a/hmp-commands.hx
+++ b/hmp-commands.hx
@@ -1244,6 +1244,22 @@  STEXI
 View guest dmesg output
 ETEXI
 
+    {
+        .name       = "agent_shutdown",
+        .args_type  = "shutdown_type:s",
+        .params     = "[reboot|shutdown|poweroff]",
+        .help       = "Shutdown/reboot a guest locally",
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_async = do_agent_shutdown,
+        .flags      = MONITOR_CMD_ASYNC,
+    },
+
+STEXI
+@item agent_shutdown
+@findex agent_shutdown
+Shutdown/reboot a guest locally
+ETEXI
+
 STEXI
 @end table
 ETEXI
diff --git a/qmp-commands.hx b/qmp-commands.hx
index dc319b7..0f983cc 100644
--- a/qmp-commands.hx
+++ b/qmp-commands.hx
@@ -806,6 +806,38 @@  Example:
 EQMP
 
     {
+        .name       = "agent_shutdown",
+        .args_type  = "shutdown_type:s",
+        .params     = "[reboot|shutdown|poweroff]",
+        .help       = "Shutdown/reboot the guest locally",
+        .user_print = monitor_user_noop,
+        .mhandler.cmd_async = do_agent_shutdown,
+        .flags      = MONITOR_CMD_ASYNC,
+    },
+
+STEXI
+@item agent_shutdown
+@findex agent_shutdown
+Shutdown/reboot the guest locally
+ETEXI
+SQMP
+agent_shutdown
+--------
+
+Shutdown the guest locally
+
+Arguments:
+
+(none)
+
+Example:
+
+-> { "execute": "agent_shutdown" }
+<- { "return": {} }
+
+EQMP
+
+    {
         .name       = "qmp_capabilities",
         .args_type  = "",
         .params     = "",
diff --git a/virtagent.c b/virtagent.c
index e0298b9..a56aeac 100644
--- a/virtagent.c
+++ b/virtagent.c
@@ -437,3 +437,69 @@  int do_agent_viewdmesg(Monitor *mon, const QDict *mon_params,
 
     return 0;
 }
+
+static void do_agent_shutdown_cb(void *opaque)
+{
+    VARPCData *rpc_data = opaque;
+    xmlrpc_value *resp = NULL;
+    xmlrpc_env env;
+
+    TRACE("called");
+
+    if (rpc_data->status != VA_RPC_STATUS_OK) {
+        LOG("error handling RPC request");
+        goto out_no_resp;
+    }
+
+    xmlrpc_env_init(&env);
+    resp = xmlrpc_parse_response(&env, rpc_data->resp_xml,
+                                 rpc_data->resp_xml_len);
+    if (rpc_has_error(&env)) {
+        LOG("RPC Failed (%i): %s\n", env.fault_code,
+            env.fault_string);
+        goto out_no_resp;
+    }
+
+    xmlrpc_DECREF(resp);
+out_no_resp:
+    rpc_data->mon_cb(rpc_data->mon_data, NULL);
+}
+
+/*
+ * do_agent_shutdown(): Shutdown a guest
+ */
+int do_agent_shutdown(Monitor *mon, const QDict *mon_params,
+                      MonitorCompletion cb, void *opaque)
+{
+    xmlrpc_env env;
+    xmlrpc_value *params;
+    VARPCData *rpc_data;
+    const char *shutdown_type;
+    int ret;
+
+    TRACE("called");
+
+    xmlrpc_env_init(&env);
+    shutdown_type = qdict_get_str(mon_params, "shutdown_type");
+    params = xmlrpc_build_value(&env, "(s)", shutdown_type);
+    if (rpc_has_error(&env)) {
+        return -1;
+    }
+
+    rpc_data = qemu_mallocz(sizeof(VARPCData));
+    rpc_data->cb = do_agent_shutdown_cb;
+    rpc_data->mon_cb = cb;
+    rpc_data->mon_data = opaque;
+
+    ret = rpc_execute(&env, "va_shutdown", params, rpc_data);
+    if (ret == -EREMOTE) {
+        monitor_printf(mon, "RPC Failed (%i): %s\n", env.fault_code,
+                       env.fault_string);
+        return -1;
+    } else if (ret == -1) {
+        monitor_printf(mon, "RPC communication error\n");
+        return -1;
+    }
+
+    return 0;
+}
diff --git a/virtagent.h b/virtagent.h
index c077582..96c6260 100644
--- a/virtagent.h
+++ b/virtagent.h
@@ -29,5 +29,7 @@  int do_agent_viewfile(Monitor *mon, const QDict *mon_params,
 void do_agent_viewdmesg_print(Monitor *mon, const QObject *qobject);
 int do_agent_viewdmesg(Monitor *mon, const QDict *mon_params,
                       MonitorCompletion cb, void *opaque);
+int do_agent_shutdown(Monitor *mon, const QDict *mon_params,
+                      MonitorCompletion cb, void *opaque);
 
 #endif /* VIRTAGENT_H */