1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
|
/*********************************************************
* Copyright (C) 2008 VMware, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation version 2.1 and no later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*********************************************************/
/*
* toolbox-cmd.c --
*
* The toolbox app with a command line interface.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "toolboxCmdInt.h"
#include "toolboxcmd_version.h"
#include "system.h"
#include "embed_version.h"
VM_EMBED_VERSION(TOOLBOXCMD_VERSION_STRING);
typedef int (*ToolboxCmdFunc)(char **argv, int argc);
typedef void (*ToolboxHelpFunc)(const char *progName);
/*
* Local Data
*/
const typedef struct CmdTable {
const char *command; /* The name of the command. */
ToolboxCmdFunc func; /* The function to execute. */
Bool requireArguments; /* The function requires arguments. */
Bool requireRoot; /* Indicates whether root is required. */
ToolboxHelpFunc helpFunc; /* The help function associated with the command. */
} CmdTable;
static Bool quiet_flag; /* Flag set by `--quiet'. */
/*
* Sadly, our home-brewed implementation of getopt() doesn't come with an
* implementation of getopt_long().
*/
#ifndef _WIN32
static struct option long_options[] = {
/* quiet sets a flag */
{ "quiet", no_argument, 0, 'q' },
/* These options don't set a flag.
We distinguish them by their indices. */
{ "help", no_argument, 0, 'h' },
{ "version", no_argument, 0, 'v' },
{ 0, 0, 0, 0 } };
#endif
static const char *options = "hqv";
/*
* Local Functions
*/
static int HelpCommand(char **argv, int argc);
static int DeviceCommand(char **argv, int argc);
static int DiskCommand(char **argv, int argc);
static int StatCommand(char **argv, int argc);
static int ScriptCommand(char **argv, int argc);
static int TimeSyncCommand(char **argv, int argc);
static void DeviceHelp(const char *progName);
static void DiskHelp(const char *progName);
static void ScriptHelp(const char *progName);
static void StatHelp(const char *progName);
static void TimeSyncHelp(const char *progName);
static void ToolboxCmdHelp(const char *progName);
static CmdTable *ParseCommand(char **argv, int argc);
/*
* The commands table.
* Must go after function declarations
*/
static CmdTable commands[] = {
{ "timesync", TimeSyncCommand, TRUE, FALSE, TimeSyncHelp},
{ "script", ScriptCommand, FALSE /* We will handle argument checks ourselves */,
TRUE, ScriptHelp},
{ "disk", DiskCommand, TRUE, TRUE, DiskHelp},
{ "stat", StatCommand, TRUE, FALSE, StatHelp},
{ "device", DeviceCommand, TRUE, FALSE, DeviceHelp},
{ "help", HelpCommand, FALSE, FALSE, ToolboxCmdHelp},
};
/*
*-----------------------------------------------------------------------------
*
* ToolboxMissingEntityError --
*
* Print out error message regarding missing argument.
*
* Results:
* None.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
static void
ToolboxMissingEntityError(const char *name, // IN: command name (argv[0])
const char *entity) // IN: what is missing
{
fprintf(stderr, "%s: Missing %s\n", name, entity);
}
/*
*-----------------------------------------------------------------------------
*
* ToolboxUnknownEntityError --
*
* Print out error message regarding unknown argument.
*
* Results:
* None.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
static void
ToolboxUnknownEntityError(const char *name, // IN: command name (argv[0])
const char *entity, // IN: what is unknown
const char *str) // IN: errorneous string
{
fprintf(stderr, "%s: Unknown %s '%s'\n", name, entity, str);
}
/*
*-----------------------------------------------------------------------------
*
* DeviceHelp --
*
* Prints the help for device commands.
*
* Results:
* None.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
static void
DeviceHelp(const char *progName) // IN: The name of the program obtained from argv[0]
{
printf("device: functions related to the virtual machine's hardware devices\n"
"Usage: %s device <subcommand> [args]\n"
" dev is the name of the device.\n\n"
"Subcommands:\n"
" enable <dev>: enable the device dev\n"
" disable <dev>: disable the device dev\n"
" list: list all available devices\n"
" status <dev>: print the status of a device\n", progName);
}
/*
*-----------------------------------------------------------------------------
*
* ToolboxCmdHelp --
*
* Print out usage information to stdout.
*
* Results:
* None.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
static void
ToolboxCmdHelp(const char *progName)
{
printf("Usage: %s <command> [options] [subcommand]\n"
"Type \'%s help <command>\' for help on a specific command.\n"
"Type \'%s -v' to see the VMware Tools version.\n"
"Use '-q' option to suppress stdout output.\n"
"Most commands take a subcommand.\n\n"
"Available commands:\n"
" device\n"
" disk\n"
" script\n"
" stat\n"
" timesync\n"
"\n"
"For additional information please visit http://www.vmware.com/support/\n\n",
progName, progName, progName);
}
/*
*-----------------------------------------------------------------------------
*
* TimeSyncHelp --
*
* Prints the help for timesync command.
*
* Results:
* None.
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/
static void
TimeSyncHelp(const char *progName) // IN: The name of the program obtained from argv[0]
{
printf("timesync: functions for controlling time synchronization on the guest OS\n"
"Usage: %s timesync <subcommand>\n\n"
"Subcommands\n"
" enable: enable time synchronization\n"
" disable: disable time synchronization\n"
" status: print the time synchronization status\n", progName);
}
/*
*-----------------------------------------------------------------------------
*
* ScriptHelp --
*
* Prints the help for the script command.
*
* Results:
* None.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
static void
ScriptHelp(const char *progName) // IN: The name of the program obtained from argv[0]
{
printf("script: control the scripts run in response to power operations\n"
"Usage: %s script <power|resume|suspend|shutdown> <subcommand> [args]\n\n"
"Subcommands:\n"
" enable: enable the given script and restore its path to the default\n"
" disable: disable the given script\n"
" set <full_path>: set the given script to the given path\n"
" default: print the default path of the given script\n"
" current: print the current path of the given script\n", progName);
}
/*
*-----------------------------------------------------------------------------
*
* DiskHelp --
*
* Prints the help for the disk command.
*
* Results:
* None.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
static void
DiskHelp(const char *progName) // IN: The name of the program obtained from argv[0]
{
printf("disk: perform disk shrink operations\n"
"Usage: %s disk <subcommand> [args]\n\n"
"Subcommands\n"
" list: list available mountpoints\n"
" shrink <mount-point>: shrinks a file system at the given mountpoint\n",
progName);
}
/*
*-----------------------------------------------------------------------------
*
* StatHelp --
*
* Prints the help for the stat command.
*
* Results:
* None.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
static void
StatHelp(const char *progName) // IN: The name of the program obtained from argv[0]
{
printf("stat: print useful guest and host information\n"
"Usage: %s stat <subcommand>\n\n"
"Subcommands\n"
" hosttime: print the host time\n"
" speed: print the CPU speed in MHz\n"
"ESX guests only subcommands\n"
" sessionid: print the current session id\n"
" balloon: print memory ballooning information\n"
" swap: print memory swapping information\n"
" memlimit: print memory limit information\n"
" memres: print memory reservation information\n"
" cpures: print CPU reservation information\n"
" cpulimit: print CPU limit information\n", progName);
}
/*
*-----------------------------------------------------------------------------
*
* HelpCommand --
*
* Handle and parse help commands.
*
* Results:
* Returns EXIT_SUCCESS on success.
* Returns other exit codes on errors.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
static int
HelpCommand(char **argv, // IN: Command line arguments
int argc) // IN: Length of argv
{
int retval = EXIT_SUCCESS;
if (++optind < argc) {
int i;
for (i = 0; i < ARRAYSIZE(commands); i++) {
if (toolbox_strcmp(commands[i].command, argv[optind]) == 0) {
commands[i].helpFunc(argv[0]);
return EXIT_SUCCESS;
}
}
ToolboxUnknownEntityError(argv[0], "subcommand", argv[optind]);
retval = EX_USAGE;
}
ToolboxCmdHelp(argv[0]);
return retval;
}
/*
*-----------------------------------------------------------------------------
*
* DeviceCommand --
*
* Handle and parse device commands.
*
* Results:
* Returns EXIT_SUCCESS on success.
* Returns the exit code on errors.
*
* Side effects:
* Might enable or disable a device.
*
*-----------------------------------------------------------------------------
*/
static int
DeviceCommand(char **argv, // IN: Command line arguments
int argc) // IN: Length of command line arguments
{
char *subcommand = argv[optind];
Bool haveDeviceArg = optind + 1 < argc;
if (toolbox_strcmp(subcommand, "list") == 0) {
return Devices_ListDevices();
} else if (toolbox_strcmp(subcommand, "status") == 0) {
if (haveDeviceArg) {
return Devices_DeviceStatus(argv[optind + 1]);
}
} else if (toolbox_strcmp(subcommand, "enable") == 0) {
if (haveDeviceArg) {
return Devices_EnableDevice(argv[optind + 1], quiet_flag);
}
} else if (toolbox_strcmp(subcommand, "disable") == 0) {
if (haveDeviceArg) {
return Devices_DisableDevice(argv[optind + 1], quiet_flag);
}
} else {
ToolboxUnknownEntityError(argv[0], "subcommand", subcommand);
return EX_USAGE;
}
ToolboxMissingEntityError(argv[0], "device name");
return EX_USAGE;
}
/*
*-----------------------------------------------------------------------------
*
* DiskCommand --
*
* Handle and parse disk commands.
*
* Results:
* Returns EXIT_SUCCESS on success.
* Returns the appropriate exit code on errors.
*
* Side effects:
* Might shrink disk
*
*-----------------------------------------------------------------------------
*/
static int
DiskCommand(char **argv, // IN: command line arguments
int argc) // IN: The length of the command line arguments
{
if (toolbox_strcmp(argv[optind], "list") == 0) {
return Shrink_List();
} else if (toolbox_strcmp(argv[optind], "shrink") == 0) {
if (++optind >= argc) {
ToolboxMissingEntityError(argv[0], "mount point");
} else {
return Shrink_DoShrink(argv[optind], quiet_flag);
}
} else {
ToolboxUnknownEntityError(argv[0], "subcommand", argv[optind]);
}
return EX_USAGE;
}
/*
*-----------------------------------------------------------------------------
*
* StatCommand --
*
* Handle and parse stat commands.
*
* Results:
* Returns EXIT_SUCCESS on success.
* Returns the appropriate exit codes on errors.
*
* Side effects:
* None.
*
*-----------------------------------------------------------------------------
*/
static int
StatCommand(char **argv, // IN: Command line arguments
int argc) // IN: Length of command line arguments
{
if (toolbox_strcmp(argv[optind], "hosttime") == 0) {
return Stat_HostTime();
} else if (toolbox_strcmp(argv[optind], "sessionid") == 0) {
return Stat_GetSessionID();
} else if (toolbox_strcmp(argv[optind], "balloon") == 0) {
return Stat_GetMemoryBallooned();
} else if (toolbox_strcmp(argv[optind], "swap") == 0) {
return Stat_GetMemorySwapped();
} else if (toolbox_strcmp(argv[optind], "memlimit") == 0) {
return Stat_GetMemoryLimit();
} else if (toolbox_strcmp(argv[optind], "memres") == 0) {
return Stat_GetMemoryReservation();
} else if (toolbox_strcmp(argv[optind], "cpures") == 0) {
return Stat_GetCpuReservation();
} else if (toolbox_strcmp(argv[optind], "cpulimit") == 0) {
return Stat_GetCpuLimit();
} else if (toolbox_strcmp(argv[optind], "speed") == 0) {
return Stat_ProcessorSpeed();
} else {
ToolboxUnknownEntityError(argv[0], "subcommand", argv[optind]);
return EX_USAGE;
}
}
/*
*-----------------------------------------------------------------------------
*
* ScriptCommand --
*
* Handle and parse script commands.
*
* Results:
* Returns EXIT_SUCCESS on success.
* Returns the exit code on errors.
*
* Side effects:
* Might enables, disables, or change APM scripts.
*
*-----------------------------------------------------------------------------
*/
static int
ScriptCommand(char **argv, // IN: command line arguments.
int argc) // IN: the length of the command line arguments.
{
const char *apm;
if (++optind >= argc) {
ToolboxMissingEntityError(argv[0], "script type");
return EX_USAGE;
}
apm = argv[optind++];
if (!Script_CheckName(apm)) {
ToolboxUnknownEntityError(argv[0], "script type", apm);
return EX_USAGE;
}
if (optind >= argc) {
ToolboxMissingEntityError(argv[0], "subcommand");
return EX_USAGE;
}
if (toolbox_strcmp(argv[optind], "default") == 0) {
return Script_GetDefault(apm);
} else if (toolbox_strcmp(argv[optind], "current") == 0) {
return Script_GetCurrent(apm);
} else if (toolbox_strcmp(argv[optind], "set") == 0) {
if (++optind >= argc) {
ToolboxMissingEntityError(argv[0], "script path");
return EX_USAGE;
}
return Script_Set(apm, argv[optind], quiet_flag);
} else if (toolbox_strcmp(argv[optind], "enable") == 0) {
return Script_Enable(apm, quiet_flag);
} else if (toolbox_strcmp(argv[optind], "disable") == 0) {
return Script_Disable(apm, quiet_flag);
} else {
ToolboxUnknownEntityError(argv[0], "subcommand", argv[optind]);
return EX_USAGE;
}
}
/*
*-----------------------------------------------------------------------------
*
* TimeSyncCommand --
*
* Parse and Handle timesync commands.
*
* Results:
* Returns EXIT_SUCCESS on success.
* Returns the appropriate exit code errors.
*
* Side effects:
* Might enable time sync, which would change the time in the guest os.
*
*-----------------------------------------------------------------------------
*/
static int
TimeSyncCommand(char **argv, // IN: command line arguments
int argc) // IN: The length of the command line arguments
{
if (toolbox_strcmp(argv[optind], "enable") == 0) {
return TimeSync_Enable(quiet_flag);
} else if (toolbox_strcmp(argv[optind], "disable") == 0) {
return TimeSync_Disable(quiet_flag);
} else if (toolbox_strcmp(argv[optind], "status") == 0) {
return TimeSync_Status();
} else {
ToolboxUnknownEntityError(argv[0], "subcommand", argv[optind]);
return EX_USAGE;
}
}
/*
*-----------------------------------------------------------------------------
*
* ParseCommand --
*
* Parse the non optional command line arguments.
*
* Results:
* Returns the CmdTable pointer on success or calls exit on error.
*
* Side effects:
* Calls exit on parse errors.
*
*-----------------------------------------------------------------------------
*/
static CmdTable *
ParseCommand(char **argv, // IN: Command line arguments
int argc) // IN: Length of command line arguments
{
int i;
for (i = 0; i < ARRAYSIZE(commands); i++) {
if (toolbox_strcmp(commands[i].command, argv[optind]) == 0) {
return &commands[i];
}
}
return NULL;
}
/*
*-----------------------------------------------------------------------------
*
* main --
*
* This is main
*
* Results:
* Returns EXIT_SUCCESS on success.
* Returns different exit code on failure.
*
* Side effects:
* The vmware-toolbox-cmd will run and do a variety of tricks for your
* amusement.
*
*-----------------------------------------------------------------------------
*/
int
main(int argc, // IN: length of command line arguments
char **argv) // IN: Command line arguments
{
Bool show_help = FALSE;
Bool show_version = FALSE;
CmdTable *cmd = NULL;
int c;
int retval;
/*
* Check if we are in a VM
*/
if (!VmCheck_IsVirtualWorld()) {
fprintf(stderr, "%s must be run inside a virtual machine.\n", argv[0]);
exit(EXIT_FAILURE);
}
/*
* Parse the command line optional arguments
*/
while (1) {
int option_index = 0;
#ifdef _WIN32
c = getopt(argc, argv, options);
#else
c = getopt_long(argc, argv, options, long_options, &option_index);
#endif
/* Detect the end of the options. */
if (c == -1) {
break;
}
switch (c) {
case 'h':
show_help = TRUE;
break;
case 'v':
show_version = TRUE;
break;
case 'q':
quiet_flag = TRUE;
break;
case '?':
/* getopt_long already printed an error message. */
fprintf(stderr, "Try '%s -h' for more information.\n", argv[0]);
return EXIT_FAILURE;
default:
return EXIT_FAILURE;
}
}
if (show_version) {
printf("%s (%s)\n", TOOLBOXCMD_VERSION_STRING, BUILD_NUMBER);
} else if (show_help) {
ToolboxCmdHelp(argv[0]);
} else {
/* Process any remaining command line arguments (not options), and
* execute corresponding command
*/
if (optind >= argc) {
ToolboxMissingEntityError(argv[0], "command");
retval = EX_USAGE;
} else if ((cmd = ParseCommand(argv, argc)) == NULL) {
ToolboxUnknownEntityError(argv[0], "command", argv[optind]);
retval = EX_USAGE;
} else if (cmd->requireRoot && !System_IsUserAdmin()) {
fprintf(stderr,
#ifdef _WIN32
"%s: Administrator permissions are needed to perform %s "
"operations. Use an administrator command prompt to "
"complete these tasks.",
#else
"%s: You must be root to perform %s operations",
#endif
argv[0], cmd->command);
retval = EX_NOPERM;
} else if (cmd->requireArguments && ++optind >= argc) {
ToolboxMissingEntityError(argv[0], "subcommand");
retval = EX_USAGE;
} else {
retval = cmd->func(argv, argc);
}
if (retval == EX_USAGE && (cmd == NULL || strcmp(cmd->command, "help"))) {
fprintf(stderr, "Try '%s help%s%s' for more information.\n",
argv[0], cmd ? " " : "", cmd ? cmd->command : "");
}
return retval;
}
return EXIT_SUCCESS;
}
|