]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Add --cmdfile argument
authorJohn Wolfe <jwolfe@vmware.com>
Fri, 11 Sep 2020 19:11:02 +0000 (12:11 -0700)
committerJohn Wolfe <jwolfe@vmware.com>
Fri, 11 Sep 2020 19:11:02 +0000 (12:11 -0700)
Some OSes (Windows) have a relatively small cmdline limit.
Expose a cmdfile argument which works like --cmd, but
reads the input from a file.  This allows RPCs which can
have huge arguments (guestVars, namespaceDB, DataSets) to work.

open-vm-tools/services/vmtoolsd/cmdLine.c
open-vm-tools/services/vmtoolsd/l10n/en.vmsg

index 9e37eafef661cfa85aa6a79c5581d6ba4d24dc2a..d8c7dffe9bc93692290ef97cd1713c4fb758e61b 100644 (file)
@@ -1,5 +1,5 @@
 /*********************************************************
- * Copyright (C) 2008-2019 VMware, Inc. All rights reserved.
+ * Copyright (C) 2008-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
@@ -84,6 +84,60 @@ ToolsCoreRunCommand(const gchar *option,
 }
 
 
+/**
+ * Reads the contents of a file and runs it as a Tools RPC command,
+ * printing the result to the terminal and
+ * exiting the application afterwards.
+ *
+ * @param[in]  option      Unused.
+ * @param[in]  fileName    Name of the file containing an RPC command.
+ * @param[in]  data        Unused.
+ * @param[out] error       Unused.
+ *
+ * @return This function doesn't return.
+ */
+
+static gboolean
+ToolsCoreRunCommandFromFile(const gchar *option,
+                            const gchar *fileName,
+                            gpointer data,
+                            GError **error)
+{
+#if defined(_WIN32)
+   VMTools_AttachConsole();
+#endif
+   if (VmCheck_IsVirtualWorld()) {
+      char *result = NULL;
+      Bool status;
+      gchar *fileContents = NULL;
+
+      if (!g_file_get_contents(fileName, &fileContents, NULL, NULL)) {
+         g_printerr(SU_(cmdline.cmdfile.read,
+                        "Unable to read command from file %s.\n"),
+                        fileName);
+         exit(1);
+      }
+
+      g_strchomp(fileContents);
+
+      status = RpcChannel_SendOne(&result, NULL, "%s", fileContents);
+      if (!status) {
+         g_printerr("%s\n", (NULL != result) ? result : "NULL");
+      } else {
+         g_print("%s\n", result);
+      }
+
+      g_free(fileContents);
+      vm_free(result);
+      exit(status ? 0 : 1);
+   }
+   g_printerr("%s\n",
+              SU_(cmdline.cmdfile.rpcerror,
+                  "Unable to send command from file to VMware hypervisor."));
+   exit(1);
+}
+
+
 #if defined(G_PLATFORM_WIN32)
 
 /**
@@ -235,6 +289,9 @@ ToolsCore_ParseCommandLine(ToolsServiceState *state,
       { "cmd", '\0', 0, G_OPTION_ARG_CALLBACK, ToolsCoreRunCommand,
          SU_(cmdline.rpc, "Sends an RPC command to the host and exits."),
          SU_(cmdline.rpc.command, "command") },
+      { "cmdfile", '\0', 0, G_OPTION_ARG_CALLBACK, ToolsCoreRunCommandFromFile,
+         SU_(cmdline.cmdfile, "Sends an RPC command from a file to the host and exits."),
+         SU_(cmdline.cmdfile.command, "command file") },
 #if defined(G_PLATFORM_WIN32)
       { "dump-state", 's', 0, G_OPTION_ARG_NONE, &dumpState,
          SU_(cmdline.state, "Dumps the internal state of a running service instance to the logs."),
index e5d0ab651fa3e29af51fc29627f09af5282bb661..371006f2dae93ef841a766470105b4666ce9ffad 100644 (file)
@@ -1,5 +1,5 @@
 ##########################################################
-# Copyright (C) 2010 VMware, Inc. All rights reserved.
+# Copyright (C) 2010,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
@@ -56,6 +56,15 @@ cmdline.rpc.command = "command"
 
 cmdline.rpcerror = "Unable to send command to VMware hypervisor."
 
+cmdline.cmdfile = "Sends an RPC command from a file to the host and exits."
+
+cmdline.cmdfile.command = "command file"
+
+cmdline.cmdfile.rpcerror = "Unable to send command from file to VMware hypervisor."
+
+cmdline.cmdfile.read = "Unable to read command from file %1$s.\n"
+
+
 cmdline.state = "Dumps the internal state of a running service instance to the logs."
 
 cmdline.uinputfd = "File descriptor for the uinput device."