From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from [140.186.70.92] (port=48332 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PDbdg-0006lL-4z for qemu-devel@nongnu.org; Wed, 03 Nov 2010 07:36:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PDbdc-0005Rl-Oe for qemu-devel@nongnu.org; Wed, 03 Nov 2010 07:36:23 -0400 Received: from e5.ny.us.ibm.com ([32.97.182.145]:36238) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PDbdc-0005Re-Lz for qemu-devel@nongnu.org; Wed, 03 Nov 2010 07:36:20 -0400 Received: from d01relay07.pok.ibm.com (d01relay07.pok.ibm.com [9.56.227.147]) by e5.ny.us.ibm.com (8.14.4/8.13.1) with ESMTP id oA3BFBSa028058 for ; Wed, 3 Nov 2010 07:15:11 -0400 Received: from d01av01.pok.ibm.com (d01av01.pok.ibm.com [9.56.224.215]) by d01relay07.pok.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id oA3BaJFJ1970354 for ; Wed, 3 Nov 2010 07:36:19 -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 oA3BaJ9g013762 for ; Wed, 3 Nov 2010 07:36:19 -0400 From: Michael Roth Date: Wed, 3 Nov 2010 06:35:35 -0500 Message-Id: <1288784139-1110-7-git-send-email-mdroth@linux.vnet.ibm.com> In-Reply-To: <1288784139-1110-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1288784139-1110-1-git-send-email-mdroth@linux.vnet.ibm.com> Subject: [Qemu-devel] [RFC][PATCH v2 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, agl@linux.vnet.ibm.com, mdroth@linux.vnet.ibm.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 | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ virtagent.h | 4 ++ 4 files changed, 152 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 84dcf3b..187bb28 100644 --- a/virtagent.c +++ b/virtagent.c @@ -86,3 +86,102 @@ 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) +{ + VARPCData *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->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)) { + 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: + xmlrpc_DECREF(resp); +out_no_resp: + rpc_data->mon_cb(rpc_data->mon_data, QOBJECT(qdict)); +} + +/* + * 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; + VARPCData *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(VARPCData)); + rpc_data->cb = do_agent_viewfile_cb; + 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