]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Add a usage message for the "checkvm" utility.
authorJohn Wolfe <jwolfe@vmware.com>
Mon, 9 Nov 2020 20:29:03 +0000 (12:29 -0800)
committerJohn Wolfe <jwolfe@vmware.com>
Mon, 9 Nov 2020 20:29:03 +0000 (12:29 -0800)
This change alters the functionality of the -h option to print a
usage message by using glib options.  The previous functionality of -h
was to print hardware version, which prints invalid results.  This
change also removes a repeated header file and removes the -r option
which prints invalid results.

open-vm-tools/checkvm/Makefile.am
open-vm-tools/checkvm/checkvm.c

index 4a276da8a056db357b7a596aeada7c8d4274dc76..26641a8c36e369fc0f2724604f9d00e6881f6482 100644 (file)
@@ -1,5 +1,5 @@
 ################################################################################
-### Copyright (C) 2007-2016 VMware, Inc.  All rights reserved.
+### Copyright (c) 2007-2016,2020 VMware, Inc.  All rights reserved.
 ###
 ### This program is free software; you can redistribute it and/or modify
 ### it under the terms of version 2 of the GNU General Public License as
@@ -21,8 +21,12 @@ vmware_checkvm_SOURCES =
 vmware_checkvm_SOURCES += checkvm.c
 
 vmware_checkvm_LDADD =
+vmware_checkvm_LDADD += @GLIB2_LIBS@
 vmware_checkvm_LDADD += @VMTOOLS_LIBS@
 
+vmware_checkvm_CPPFLAGS =
+vmware_checkvm_CPPFLAGS += @GLIB2_CPPFLAGS@
+
 if HAVE_ICU
    vmware_checkvm_LDADD += @ICU_LIBS@
    vmware_checkvm_LINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS)     \
index 31694f4ece8bc95701f6bd641ddcdbeda2d5f4ed..164e19b28d2fd81b2c4293a362eaa00bf2cdc423 100644 (file)
@@ -1,5 +1,5 @@
 /*********************************************************
- * Copyright (C) 2007-2019 VMware, Inc. All rights reserved.
+ * Copyright (C) 2007-2020 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
 
 #include <stdlib.h>
 #include <stdio.h>
+#include <glib.h>
 
 #if !defined(_WIN32)
 #include <unistd.h>
 #endif
 
-#include "vm_version.h"
-#include "backdoor.h"
-#include "backdoor_def.h"
 #include "vm_basic_types.h"
 #include "vmcheck.h"
 #if defined(_WIN32)
 #include "embed_version.h"
 VM_EMBED_VERSION(CHECKVM_VERSION_STRING);
 
-
-/*
- *  getHWVersion  -  Read VM HW version through backdoor
- */
-void
-getHWVersion(uint32 *hwVersion)
-{
-   Backdoor_proto bp;
-   bp.in.cx.halfs.low = BDOOR_CMD_GETHWVERSION;
-   Backdoor(&bp);
-   *hwVersion = bp.out.ax.word;
-}
-
-
-/*
- *  getScreenSize  -  Get screen size of the host
- */
-void
-getScreenSize(uint32 *screensize)
-{
-   Backdoor_proto bp;
-   bp.in.cx.halfs.low = BDOOR_CMD_GETSCREENSIZE;
-   Backdoor(&bp);
-   *screensize = bp.out.ax.word;
-}
-
-
 /*
  *  Start of main program.  Check if we are in a VM, by reading
  *  a backdoor port.  Then process any other commands.
@@ -80,71 +51,74 @@ main(int argc,
      char *argv[])
 {
    uint32 version[2];
-   int opt;
-   int width, height;
-   uint32 screensize = 0;
-   uint32 hwVersion;
+   gchar *gAppName;
+   GError *gErr = NULL;
+   gboolean product = FALSE;
+   int success = 1;
+
+   GOptionEntry options[] = {
+      {"prod", 'p', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE, &product,
+       "print VMware hypervisor product.", NULL},
+      {NULL}
+   };
+   GOptionContext *optCtx;
 
 #if defined(_WIN32)
    WinUtil_EnableSafePathSearching(TRUE);
 #endif
 
+   /*
+    * set up glib options context.
+    */
+   gAppName = g_path_get_basename(argv[0]);
+
+   g_set_prgname(gAppName);
+   optCtx = g_option_context_new(NULL);
+   g_option_context_add_main_entries(optCtx, options, NULL);
+
    if (!VmCheck_IsVirtualWorld()) {
-      fprintf(stdout, "Not running in a virtual machine.\n");
-      return 1;
+      g_printerr("Error: %s must be run inside a virtual machine"
+                 " on a VMware hypervisor product.\n", gAppName);
+      goto out;
    }
 
    if (!VmCheck_GetVersion(&version[0], &version[1])) {
-      fprintf(stdout, "Couldn't get version\n");
-      return 1;
+      g_printerr("%s: Couldn't get version\n", gAppName);
+      goto out;
+   }
+
+   if (!g_option_context_parse(optCtx, &argc, &argv, &gErr)) {
+      g_printerr("%s: %s\n", gAppName, gErr->message);
+      g_error_free(gErr);
+      goto out;
    }
 
    /*
-    *  OK, we're in a VM, check if there are any other requests
+    * product is true if 'p' option was passed to parser
     */
-   while ((opt = getopt(argc, argv, "rph")) != EOF) {
-      switch (opt) {
-      case 'r':
-         getScreenSize(&screensize);
-         width = (screensize >> 16) & 0xffff;
-         height = screensize & 0xffff;
-         if ((width <= 0x7fff) && (height <= 0x7fff)) {
-            printf("%d %d\n", width, height);
-         } else {
-            printf("0 0\n");
-         }
-         return 0;
-
-      case 'p':
-         /*
-         * Print out product that we're running on based on code
-         * obtained from getVersion().
-         */
-         switch (version[1]) {
-         case VMX_TYPE_SCALABLE_SERVER:
-            printf("ESX Server\n");
-            break;
-
-         case VMX_TYPE_WORKSTATION:
-            printf("Workstation\n");
-            break;
-
-         default:
-            printf("Unknown\n");
-            break;
-         }
-         return 0;
-
-      case 'h':
-         getHWVersion(&hwVersion);
-         printf("VM's hw version is %u\n", hwVersion);
+   if (product) {
+      switch (version[1]) {
+      case VMX_TYPE_SCALABLE_SERVER:
+         g_print("ESX Server\n");
+         break;
+
+      case VMX_TYPE_WORKSTATION:
+         g_print("Workstation\n");
          break;
 
       default:
+         g_print("Unknown\n");
          break;
       }
+      success = 0;
+      goto out;
    }
 
-   printf("%s version %d (good)\n", PRODUCT_LINE_NAME, version[0]);
-   return 0;
+   g_print("%s version %d (good)\n", PRODUCT_LINE_NAME, version[0]);
+   success = 0;
+
+out:
+   g_option_context_free(optCtx);
+   g_free(gAppName);
+   return success;
 }