From: VMware, Inc <> Date: Thu, 17 Jun 2010 21:05:56 +0000 (-0700) Subject: Add auto-upgrade command to toolbox CLI. X-Git-Tag: 2010.06.16-268169~165 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4b1ac046170c00fe232df83f03b03d2f1192d9fc;p=thirdparty%2Fopen-vm-tools.git Add auto-upgrade command to toolbox CLI. This change adds a new "upgrade" command to the toolbox CLI on Win32 and Linux (non-open-vm-tools), which are the only platforms where we have auto-upgrade. Add a few utility functions to the common toolbox CLI code to do a few useful things: . print messages based on the status of the "quiet" flag. . send RPCs to the host using the RpcChannel API. On top of that, put some common definitions of commands and replies in a shared header to avoid having duplicated strings in different parts of the source code. Signed-off-by: Marcelo Vanzin --- diff --git a/open-vm-tools/lib/include/vmware/guestrpc/tclodefs.h b/open-vm-tools/lib/include/vmware/guestrpc/tclodefs.h index 49b399506..7c89312c5 100644 --- a/open-vm-tools/lib/include/vmware/guestrpc/tclodefs.h +++ b/open-vm-tools/lib/include/vmware/guestrpc/tclodefs.h @@ -40,6 +40,8 @@ /** TCLO channel name for the HGFS driver. */ #define TOOLS_HGFS_NAME "tools-hgfs" +/** Reply from host when the command is not recognized. */ +#define RPCI_UNKNOWN_COMMAND "Unknown command" /* * Tools options. @@ -52,6 +54,19 @@ #define TOOLSOPTION_MAP_ROOT_HGFS_SHARE "mapRootHgfsShare" #define TOOLSOPTION_LINK_ROOT_HGFS_SHARE "linkRootHgfsShare" +/* + * Auto-upgrade commands. + */ + +#define AUTOUPGRADE_AVAILABLE_CMD "vmx.capability.tools_is_upgradable" +#define AUTOUPGRADE_START_CMD "guest.initiateAutoUpgrade" + +/* + * Shrink commands. + */ + +#define DISK_SHRINK_CMD "disk.shrink" + /* * The max selection buffer length has to be less than the diff --git a/open-vm-tools/toolbox/toolbox-cmd.c b/open-vm-tools/toolbox/toolbox-cmd.c index 19f431fd5..abfa3723c 100644 --- a/open-vm-tools/toolbox/toolbox-cmd.c +++ b/open-vm-tools/toolbox/toolbox-cmd.c @@ -32,6 +32,7 @@ #include "toolboxCmdInt.h" #include "toolboxcmd_version.h" #include "system.h" +#include "vmware/tools/guestrpc.h" #include "vmware/tools/i18n.h" #include "vmware/tools/utils.h" @@ -75,6 +76,7 @@ static struct option long_options[] = { { 0, 0, 0, 0 } }; #endif +static gboolean gQuiet = FALSE; static const char *options = "hqv"; /* @@ -101,6 +103,9 @@ static CmdTable commands[] = { { "disk", Disk_Command, TRUE, TRUE, Disk_Help}, { "stat", Stat_Command, TRUE, FALSE, Stat_Help}, { "device", Device_Command, TRUE, FALSE, Device_Help}, +#if (defined(_WIN32) || defined(linux)) && !defined(OPEN_VM_TOOLS) + { "upgrade", Upgrade_Command, TRUE, FALSE, Upgrade_Help}, +#endif { "help", HelpCommand, FALSE, FALSE, ToolboxCmdHelp}, }; @@ -129,6 +134,112 @@ ToolsCmd_MissingEntityError(const char *name, // IN: command name (argv[0]) } +/* + *----------------------------------------------------------------------------- + * + * ToolsCmd_Print -- + * + * Prints a message to stdout unless quiet output was requested. + * + * Results: + * None. + * + * Side effects: + * None. + * + *----------------------------------------------------------------------------- + */ + +void +ToolsCmd_Print(const char *fmt, + ...) +{ + if (!gQuiet) { + gchar *str; + va_list args; + + va_start(args, fmt); + g_vasprintf(&str, fmt, args); + va_end(args); + + g_print(str); + g_free(str); + } +} + + +/* + *----------------------------------------------------------------------------- + * + * ToolsCmd_PrintErr -- + * + * Prints a message to stderr unless quiet output was requested. + * + * Results: + * None. + * + * Side effects: + * None. + * + *----------------------------------------------------------------------------- + */ + +void +ToolsCmd_PrintErr(const char *fmt, + ...) +{ + if (!gQuiet) { + gchar *str; + va_list args; + + va_start(args, fmt); + g_vasprintf(&str, fmt, args); + va_end(args); + + g_printerr(str); + g_free(str); + } +} + + +/* + *----------------------------------------------------------------------------- + * + * ToolsCmd_SendRPC -- + * + * Sends an RPC message to the host. + * + * Results: + * The return value from the RPC. + * + * Side effects: + * None. + * + *----------------------------------------------------------------------------- + */ + +gboolean +ToolsCmd_SendRPC(const char *rpc, // IN + size_t rpcLen, // IN + char **result, // OUT + size_t *resultLen) // OUT +{ + RpcChannel *chan = BackdoorChannel_New(); + gboolean ret = RpcChannel_Start(chan); + + if (!ret) { + g_warning("Error starting RPC channel."); + goto exit; + } + + ret = RpcChannel_Send(chan, (char *) rpc, rpcLen, result, resultLen); + +exit: + RpcChannel_Destroy(chan); + return ret; +} + + /* *----------------------------------------------------------------------------- * @@ -184,7 +295,9 @@ ToolboxCmdHelp(const char *progName, // IN " disk\n" " script\n" " stat\n" - " timesync\n\n" + " timesync\n" + " upgrade (not available on all operating systems)\n" + "\n" "For additional information please visit http://www.vmware.com/support/\n\n"), progName, progName, cmd, progName); } @@ -293,7 +406,6 @@ main(int argc, // IN: length of command line arguments CmdTable *cmd = NULL; int c; int retval; - gboolean quiet = FALSE; setlocale(LC_ALL, ""); VMTools_ConfigLogging("toolboxcmd", NULL, FALSE, FALSE); @@ -335,7 +447,7 @@ main(int argc, // IN: length of command line arguments break; case 'q': - quiet = TRUE; + gQuiet = TRUE; break; case '?': @@ -380,7 +492,7 @@ main(int argc, // IN: length of command line arguments ToolsCmd_MissingEntityError(argv[0], SU_(arg.subcommand, "subcommand")); retval = EX_USAGE; } else { - retval = cmd->func(argv, argc, quiet); + retval = cmd->func(argv, argc, gQuiet); } if (retval == EX_USAGE && (cmd == NULL || strcmp(cmd->command, "help"))) { diff --git a/open-vm-tools/toolbox/toolboxCmdInt.h b/open-vm-tools/toolbox/toolboxCmdInt.h index 55b456921..693a3efc6 100644 --- a/open-vm-tools/toolbox/toolboxCmdInt.h +++ b/open-vm-tools/toolbox/toolboxCmdInt.h @@ -89,6 +89,23 @@ ToolsCmd_UnknownEntityError(const char *name, const char *entity, const char *str); +void +ToolsCmd_Print(const char *fmt, + ...) PRINTF_DECL(1, 2); + +void +ToolsCmd_PrintErr(const char *fmt, + ...) PRINTF_DECL(1, 2); + +gboolean +ToolsCmd_SendRPC(const char *rpc, + size_t rpcLen, + char **result, + size_t *resultLen); + +/* + * Command declarations. + */ /** * A shorthand macro for declaring a command entry. This just declares @@ -110,4 +127,8 @@ DECLARE_COMMAND(Script); DECLARE_COMMAND(Stat); DECLARE_COMMAND(TimeSync); +#if defined(_WIN32) || (defined(linux) && !defined(OPEN_VM_TOOLS)) +DECLARE_COMMAND(Upgrade); +#endif + #endif /*_TOOLBOX_CMD_H_*/ diff --git a/open-vm-tools/toolbox/toolboxcmd-shrink.c b/open-vm-tools/toolbox/toolboxcmd-shrink.c index 9b1bca31e..60604291c 100644 --- a/open-vm-tools/toolbox/toolboxcmd-shrink.c +++ b/open-vm-tools/toolbox/toolboxcmd-shrink.c @@ -32,6 +32,7 @@ #include "toolboxCmdInt.h" #include "rpcout.h" +#include "vmware/guestrpc/tclodefs.h" #include "vmware/tools/i18n.h" #ifndef _WIN32 @@ -243,7 +244,8 @@ ShrinkDoShrink(char *mountPoint, // IN: mount point char *result; size_t resultLen; - if (RpcOut_sendOne(&result, &resultLen, "disk.shrink")) { + if (ToolsCmd_SendRPC(DISK_SHRINK_CMD, sizeof DISK_SHRINK_CMD - 1, + &result, &resultLen)) { if (!quiet) { printf("\nDisk shrinking complete\n"); } @@ -251,7 +253,7 @@ ShrinkDoShrink(char *mountPoint, // IN: mount point goto out; } - fprintf(stderr, "%s\n", result); + fprintf(stderr, "\n%s\n", result); } fprintf(stderr, "Shrinking not completed\n");