From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=60123 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P9Mdc-00009X-4c for qemu-devel@nongnu.org; Fri, 22 Oct 2010 14:46:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P9MdY-0002h4-Ch for qemu-devel@nongnu.org; Fri, 22 Oct 2010 14:46:47 -0400 Received: from e4.ny.us.ibm.com ([32.97.182.144]:44207) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P9MdY-0002gm-A1 for qemu-devel@nongnu.org; Fri, 22 Oct 2010 14:46:44 -0400 Received: from d01relay01.pok.ibm.com (d01relay01.pok.ibm.com [9.56.227.233]) by e4.ny.us.ibm.com (8.14.4/8.13.1) with ESMTP id o9MIUarP017037 for ; Fri, 22 Oct 2010 14:30:36 -0400 Received: from d01av01.pok.ibm.com (d01av01.pok.ibm.com [9.56.224.215]) by d01relay01.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id o9MIkhrh395394 for ; Fri, 22 Oct 2010 14:46:43 -0400 Received: from d01av01.pok.ibm.com (loopback [127.0.0.1]) by d01av01.pok.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id o9MIkhp9027865 for ; Fri, 22 Oct 2010 14:46:43 -0400 From: Michael Roth Date: Fri, 22 Oct 2010 13:46:01 -0500 Message-Id: <1287773165-24855-7-git-send-email-mdroth@linux.vnet.ibm.com> In-Reply-To: <1287773165-24855-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1287773165-24855-1-git-send-email-mdroth@linux.vnet.ibm.com> Subject: [Qemu-devel] [RFC][PATCH 06/10] virtagent: add agent_viewfile command List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: aliguori@linux.vnet.ibm.com, ryanh@us.ibm.com, agl@linux.vnet.ibm.com, mdroth@linux.vnet.ibm.com, abeekhof@redhat.com Utilize the getfile RPC to provide a means to view text files in the guest. Getfile can handle binary files as well but we don't advertise that here due to the special handling requiring to store it and provide it back to the user (base64 encoding it for instance). Hence the potentially confusing "viewfile" as opposed to "getfile". Signed-off-by: Michael Roth --- hmp-commands.hx | 16 +++++++++ qmp-commands.hx | 33 ++++++++++++++++++ virtagent.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ virtagent.h | 4 ++ 4 files changed, 155 insertions(+), 0 deletions(-) diff --git a/hmp-commands.hx b/hmp-commands.hx index 81999aa..e9a7f4a 100644 --- a/hmp-commands.hx +++ b/hmp-commands.hx @@ -1211,6 +1211,22 @@ show available trace events and their state ETEXI #endif + { + .name = "agent_viewfile", + .args_type = "filepath:s", + .params = "filepath", + .help = "Echo a file from the guest filesystem", + .user_print = do_agent_viewfile_print, + .mhandler.cmd_async = do_agent_viewfile, + .flags = MONITOR_CMD_ASYNC, + }, + +STEXI +@item agent_viewfile @var{filepath} +@findex agent_viewfile +Echo the file identified by @var{filepath} on the guest filesystem +ETEXI + STEXI @end table ETEXI diff --git a/qmp-commands.hx b/qmp-commands.hx index 793cf1c..efa2137 100644 --- a/qmp-commands.hx +++ b/qmp-commands.hx @@ -738,6 +738,39 @@ Example: EQMP { + .name = "agent_viewfile", + .args_type = "filepath:s", + .params = "filepath", + .help = "Echo a file from the guest filesystem", + .user_print = monitor_user_noop, + .mhandler.cmd_async = do_agent_viewfile, + .flags = MONITOR_CMD_ASYNC, + }, + +STEXI +@item agent_viewfile @var{filepath} +@findex agent_viewfile +Echo the file identified by @var{filepath} on the guest filesystem +ETEXI +SQMP +agent_viewfile +-------- + +Echo the file identified by @var{filepath} from the guest filesystem. + +Arguments: + +- "filepath": Full guest path of the desired file + +Example: + +-> { "execute": "agent_viewfile", + "arguments": { "filepath": "/sys/kernel/kexec_loaded" } } +<- { "return": { "contents": "0" } } + +EQMP + + { .name = "qmp_capabilities", .args_type = "", .params = "", diff --git a/virtagent.c b/virtagent.c index 2234bef..8059d00 100644 --- a/virtagent.c +++ b/virtagent.c @@ -94,3 +94,105 @@ out_callxml: out: return ret; } + +void do_agent_viewfile_print(Monitor *mon, const QObject *data) +{ + QDict *qdict; + const char *contents = NULL; + int i; + + qdict = qobject_to_qdict(data); + if (!qdict_haskey(qdict, "contents")) { + return; + } + + contents = qdict_get_str(qdict, "contents"); + if (contents != NULL) { + /* monitor_printf truncates so do it in chunks. also, file_contents + * may not be null-termed at proper location so explicitly calc + * last chunk sizes */ + for (i = 0; i < strlen(contents); i += 1024) { + monitor_printf(mon, "%.1024s", contents + i); + } + } + monitor_printf(mon, "\n"); +} + +static void do_agent_viewfile_cb(void *opaque) +{ + RPCRequest *rpc_data = opaque; + xmlrpc_value *resp = NULL; + char *file_contents = NULL; + int file_size, ret; + xmlrpc_env env; + QDict *qdict = qdict_new(); + + if (rpc_data->resp_xml == NULL) { + monitor_printf(rpc_data->mon, "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)) { + ret = -1; + goto out_no_resp; + } + + xmlrpc_parse_value(&env, resp, "6", &file_contents, &file_size); + if (rpc_has_error(&env)) { + ret = -1; + goto out; + } + + if (file_contents != NULL) { + qdict_put(qdict, "contents", + qstring_from_substr(file_contents, 0, file_size-1)); + } + +out: + qemu_free(rpc_data->resp_xml); + xmlrpc_DECREF(resp); +out_no_resp: + rpc_data->mon_cb(rpc_data->mon_data, QOBJECT(qdict)); + qemu_free(rpc_data); +} + +/* + * do_agent_viewfile(): View a text file in the guest + */ +int do_agent_viewfile(Monitor *mon, const QDict *mon_params, + MonitorCompletion cb, void *opaque) +{ + xmlrpc_env env; + xmlrpc_value *params; + RPCRequest *rpc_data; + const char *filepath; + int ret; + + filepath = qdict_get_str(mon_params, "filepath"); + xmlrpc_env_init(&env); + params = xmlrpc_build_value(&env, "(s)", filepath); + if (rpc_has_error(&env)) { + return -1; + } + + rpc_data = qemu_mallocz(sizeof(RPCRequest)); + rpc_data->cb = do_agent_viewfile_cb; + rpc_data->mon = mon; + rpc_data->mon_cb = cb; + rpc_data->mon_data = opaque; + + ret = rpc_execute(&env, "getfile", 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 abdb32a..d027eac 100644 --- a/virtagent.h +++ b/virtagent.h @@ -22,4 +22,8 @@ #define HOST_AGENT_PATH_CLIENT "/tmp/virtagent-host-client.sock" #define VA_MAX_CHUNK_SIZE 4096 /* max bytes at a time for get/send file */ +void do_agent_viewfile_print(Monitor *mon, const QObject *qobject); +int do_agent_viewfile(Monitor *mon, const QDict *mon_params, + MonitorCompletion cb, void *opaque); + #endif /* VIRTAGENT_H */ -- 1.7.0.4