render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
0a7476
From 58a67e037eefae16bf2f831de482e9070d590151 Mon Sep 17 00:00:00 2001
0a7476
Message-Id: <58a67e037eefae16bf2f831de482e9070d590151@dist-git>
0a7476
From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= <berrange@redhat.com>
0a7476
Date: Tue, 21 May 2019 11:02:13 +0100
0a7476
Subject: [PATCH] qemu: use line breaks in command line args written to log
0a7476
MIME-Version: 1.0
0a7476
Content-Type: text/plain; charset=UTF-8
0a7476
Content-Transfer-Encoding: 8bit
0a7476
0a7476
The QEMU command line arguments are very long and currently all written
0a7476
on a single line to /var/log/libvirt/qemu/$GUEST.log. This introduces
0a7476
logic to add line breaks after every env variable and "-" optional
0a7476
argument, and every positional argument. This will create a clearer log
0a7476
file, which will in turn present better in bug reports when people cut +
0a7476
paste from the log into a bug comment.
0a7476
0a7476
An example log file entry now looks like this:
0a7476
0a7476
  2018-12-14 12:57:03.677+0000: starting up libvirt version: 5.0.0, qemu version: 3.0.0qemu-3.0.0-1.fc29, kernel: 4.19.5-300.fc29.x86_64, hostname: localhost.localdomain
0a7476
  LC_ALL=C \
0a7476
  PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin \
0a7476
  HOME=/home/berrange \
0a7476
  USER=berrange \
0a7476
  LOGNAME=berrange \
0a7476
  QEMU_AUDIO_DRV=none \
0a7476
  /usr/bin/qemu-system-ppc64 \
0a7476
  -name guest=guest,debug-threads=on \
0a7476
  -S \
0a7476
  -object secret,id=masterKey0,format=raw,file=/home/berrange/.config/libvirt/qemu/lib/domain-33-guest/master-key.aes \
0a7476
  -machine pseries-2.10,accel=tcg,usb=off,dump-guest-core=off \
0a7476
  -m 1024 \
0a7476
  -realtime mlock=off \
0a7476
  -smp 1,sockets=1,cores=1,threads=1 \
0a7476
  -uuid c8a74977-ab18-41d0-ae3b-4041c7fffbcd \
0a7476
  -display none \
0a7476
  -no-user-config \
0a7476
  -nodefaults \
0a7476
  -chardev socket,id=charmonitor,fd=23,server,nowait \
0a7476
  -mon chardev=charmonitor,id=monitor,mode=control \
0a7476
  -rtc base=utc \
0a7476
  -no-shutdown \
0a7476
  -boot strict=on \
0a7476
  -device qemu-xhci,id=usb,bus=pci.0,addr=0x1 \
0a7476
  -device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x2 \
0a7476
  -sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,resourcecontrol=deny \
0a7476
  -msg timestamp=on
0a7476
  2018-12-14 12:57:03.730+0000: shutting down, reason=failed
0a7476
0a7476
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
0a7476
(cherry picked from commit 07c9d6601d0b440acd13794b258bdcb570ae93f4)
0a7476
0a7476
https://bugzilla.redhat.com/show_bug.cgi?id=1672957
0a7476
0a7476
Conflicts:
0a7476
    src/util/vircommand.c
0a7476
    src/util/virfirewall.c
0a7476
      - Not using auto cleanup macros in old branch
0a7476
0a7476
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
0a7476
Message-Id: <20190521100213.16875-3-berrange@redhat.com>
0a7476
Reviewed-by: Ján Tomko <jtomko@redhat.com>
0a7476
---
0a7476
 docs/internals/command.html.in   |  2 +-
0a7476
 src/bhyve/bhyve_driver.c         |  4 +-
0a7476
 src/qemu/qemu_driver.c           |  2 +-
0a7476
 src/qemu/qemu_interface.c        |  2 +-
0a7476
 src/qemu/qemu_process.c          |  2 +-
0a7476
 src/security/security_apparmor.c |  2 +-
0a7476
 src/util/vircommand.c            | 19 ++++++++--
0a7476
 src/util/vircommand.h            |  2 +-
0a7476
 src/util/virfirewall.c           |  2 +-
0a7476
 tests/bhyvexml2argvtest.c        |  4 +-
0a7476
 tests/commanddata/test26.log     |  1 +
0a7476
 tests/commandtest.c              | 64 +++++++++++++++++++++++++++++++-
0a7476
 tests/qemuxml2argvtest.c         |  2 +-
0a7476
 tests/storagepoolxml2argvtest.c  |  2 +-
0a7476
 tests/storagevolxml2argvtest.c   |  4 +-
0a7476
 15 files changed, 95 insertions(+), 19 deletions(-)
0a7476
 create mode 100644 tests/commanddata/test26.log
0a7476
0a7476
diff --git a/docs/internals/command.html.in b/docs/internals/command.html.in
0a7476
index 43f51a49bb..8a9061e70f 100644
0a7476
--- a/docs/internals/command.html.in
0a7476
+++ b/docs/internals/command.html.in
0a7476
@@ -426,7 +426,7 @@ dprintf(logfd, "%s: ", timestamp);
0a7476
 VIR_FREE(timestamp);
0a7476
 virCommandWriteArgLog(cmd, logfd);
0a7476
 
0a7476
-string = virCommandToString(cmd);
0a7476
+string = virCommandToString(cmd, false);
0a7476
 if (string)
0a7476
     VIR_DEBUG("about to run %s", string);
0a7476
 VIR_FREE(string);
0a7476
diff --git a/src/bhyve/bhyve_driver.c b/src/bhyve/bhyve_driver.c
0a7476
index 9284b51783..ae611fdb60 100644
0a7476
--- a/src/bhyve/bhyve_driver.c
0a7476
+++ b/src/bhyve/bhyve_driver.c
0a7476
@@ -736,14 +736,14 @@ bhyveConnectDomainXMLToNative(virConnectPtr conn,
0a7476
                                                 NULL)))
0a7476
             goto cleanup;
0a7476
 
0a7476
-        virBufferAdd(&buf, virCommandToString(loadcmd), -1);
0a7476
+        virBufferAdd(&buf, virCommandToString(loadcmd, false), -1);
0a7476
         virBufferAddChar(&buf, '\n');
0a7476
     }
0a7476
 
0a7476
     if (!(cmd = virBhyveProcessBuildBhyveCmd(conn, def, true)))
0a7476
         goto cleanup;
0a7476
 
0a7476
-    virBufferAdd(&buf, virCommandToString(cmd), -1);
0a7476
+    virBufferAdd(&buf, virCommandToString(cmd, false), -1);
0a7476
 
0a7476
     if (virBufferCheckError(&buf) < 0)
0a7476
         goto cleanup;
0a7476
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
0a7476
index 3b3281c13f..704ba24215 100644
0a7476
--- a/src/qemu/qemu_driver.c
0a7476
+++ b/src/qemu/qemu_driver.c
0a7476
@@ -7171,7 +7171,7 @@ static char *qemuConnectDomainXMLToNative(virConnectPtr conn,
0a7476
                                             VIR_QEMU_PROCESS_START_COLD)))
0a7476
         goto cleanup;
0a7476
 
0a7476
-    ret = virCommandToString(cmd);
0a7476
+    ret = virCommandToString(cmd, false);
0a7476
 
0a7476
  cleanup:
0a7476
     virCommandFree(cmd);
0a7476
diff --git a/src/qemu/qemu_interface.c b/src/qemu/qemu_interface.c
0a7476
index a3f13093f5..afef30d7cf 100644
0a7476
--- a/src/qemu/qemu_interface.c
0a7476
+++ b/src/qemu/qemu_interface.c
0a7476
@@ -363,7 +363,7 @@ qemuCreateInBridgePortWithHelper(virQEMUDriverConfigPtr cfg,
0a7476
         char ebuf[1024];
0a7476
         char *errstr = NULL;
0a7476
 
0a7476
-        if (!(cmdstr = virCommandToString(cmd)))
0a7476
+        if (!(cmdstr = virCommandToString(cmd, false)))
0a7476
             goto cleanup;
0a7476
         virCommandAbort(cmd);
0a7476
 
0a7476
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
0a7476
index 8811aefb95..2795796166 100644
0a7476
--- a/src/qemu/qemu_process.c
0a7476
+++ b/src/qemu/qemu_process.c
0a7476
@@ -4327,7 +4327,7 @@ qemuLogOperation(virDomainObjPtr vm,
0a7476
         goto cleanup;
0a7476
 
0a7476
     if (cmd) {
0a7476
-        char *args = virCommandToString(cmd);
0a7476
+        char *args = virCommandToString(cmd, true);
0a7476
         qemuDomainLogContextWrite(logCtxt, "%s\n", args);
0a7476
         VIR_FREE(args);
0a7476
     }
0a7476
diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c
0a7476
index cb41df71a9..bb1742a686 100644
0a7476
--- a/src/security/security_apparmor.c
0a7476
+++ b/src/security/security_apparmor.c
0a7476
@@ -661,7 +661,7 @@ AppArmorSetSecurityChildProcessLabel(virSecurityManagerPtr mgr ATTRIBUTE_UNUSED,
0a7476
     if ((profile_name = get_profile_name(def)) == NULL)
0a7476
         goto cleanup;
0a7476
 
0a7476
-    cmd_str = virCommandToString(cmd);
0a7476
+    cmd_str = virCommandToString(cmd, false);
0a7476
     VIR_DEBUG("Changing AppArmor profile to %s on %s", profile_name, cmd_str);
0a7476
     virCommandSetAppArmorProfile(cmd, profile_name);
0a7476
     rc = 0;
0a7476
diff --git a/src/util/vircommand.c b/src/util/vircommand.c
0a7476
index 68cf63c28a..f539bafab6 100644
0a7476
--- a/src/util/vircommand.c
0a7476
+++ b/src/util/vircommand.c
0a7476
@@ -1971,6 +1971,7 @@ virCommandWriteArgLog(virCommandPtr cmd, int logfd)
0a7476
 /**
0a7476
  * virCommandToString:
0a7476
  * @cmd: the command to convert
0a7476
+ * @linebreaks: true to break line after each env var or option
0a7476
  *
0a7476
  * Call after adding all arguments and environment settings, but
0a7476
  * before Run/RunAsync, to return a string representation of the
0a7476
@@ -1980,10 +1981,11 @@ virCommandWriteArgLog(virCommandPtr cmd, int logfd)
0a7476
  * Caller is responsible for freeing the resulting string.
0a7476
  */
0a7476
 char *
0a7476
-virCommandToString(virCommandPtr cmd)
0a7476
+virCommandToString(virCommandPtr cmd, bool linebreaks)
0a7476
 {
0a7476
     size_t i;
0a7476
     virBuffer buf = VIR_BUFFER_INITIALIZER;
0a7476
+    bool prevopt = false;
0a7476
 
0a7476
     /* Cannot assume virCommandRun will be called; so report the error
0a7476
      * now.  If virCommandRun is called, it will report the same error. */
0a7476
@@ -2012,11 +2014,22 @@ virCommandToString(virCommandPtr cmd)
0a7476
         virBufferAdd(&buf, cmd->env[i], eq - cmd->env[i]);
0a7476
         virBufferEscapeShell(&buf, eq);
0a7476
         virBufferAddChar(&buf, ' ');
0a7476
+        if (linebreaks)
0a7476
+            virBufferAddLit(&buf, "\\\n");
0a7476
     }
0a7476
     virBufferEscapeShell(&buf, cmd->args[0]);
0a7476
     for (i = 1; i < cmd->nargs; i++) {
0a7476
         virBufferAddChar(&buf, ' ');
0a7476
+        if (linebreaks) {
0a7476
+            /* Line break if this is a --arg or if
0a7476
+             * the previous arg was a positional option
0a7476
+             */
0a7476
+            if (cmd->args[i][0] == '-' ||
0a7476
+                !prevopt)
0a7476
+                virBufferAddLit(&buf, "\\\n");
0a7476
+        }
0a7476
         virBufferEscapeShell(&buf, cmd->args[i]);
0a7476
+        prevopt = (cmd->args[i][0] == '-');
0a7476
     }
0a7476
 
0a7476
     if (virBufferCheckError(&buf) < 0)
0a7476
@@ -2459,7 +2472,7 @@ virCommandRunAsync(virCommandPtr cmd, pid_t *pid)
0a7476
         goto cleanup;
0a7476
     }
0a7476
 
0a7476
-    str = virCommandToString(cmd);
0a7476
+    str = virCommandToString(cmd, false);
0a7476
     if (dryRunBuffer || dryRunCallback) {
0a7476
         dryRunStatus = 0;
0a7476
         if (!str) {
0a7476
@@ -2599,7 +2612,7 @@ virCommandWait(virCommandPtr cmd, int *exitstatus)
0a7476
         if (exitstatus && (cmd->rawStatus || WIFEXITED(status))) {
0a7476
             *exitstatus = cmd->rawStatus ? status : WEXITSTATUS(status);
0a7476
         } else if (status) {
0a7476
-            char *str = virCommandToString(cmd);
0a7476
+            char *str = virCommandToString(cmd, false);
0a7476
             char *st = virProcessTranslateStatus(status);
0a7476
             bool haveErrMsg = cmd->errbuf && *cmd->errbuf && (*cmd->errbuf)[0];
0a7476
 
0a7476
diff --git a/src/util/vircommand.h b/src/util/vircommand.h
0a7476
index 883e212959..5ea2998f77 100644
0a7476
--- a/src/util/vircommand.h
0a7476
+++ b/src/util/vircommand.h
0a7476
@@ -171,7 +171,7 @@ void virCommandSetPreExecHook(virCommandPtr cmd,
0a7476
 void virCommandWriteArgLog(virCommandPtr cmd,
0a7476
                            int logfd);
0a7476
 
0a7476
-char *virCommandToString(virCommandPtr cmd) ATTRIBUTE_RETURN_CHECK;
0a7476
+char *virCommandToString(virCommandPtr cmd, bool linebreaks) ATTRIBUTE_RETURN_CHECK;
0a7476
 
0a7476
 int virCommandExec(virCommandPtr cmd, gid_t *groups, int ngroups) ATTRIBUTE_RETURN_CHECK;
0a7476
 
0a7476
diff --git a/src/util/virfirewall.c b/src/util/virfirewall.c
0a7476
index 10c370a2ae..79c2ef87e2 100644
0a7476
--- a/src/util/virfirewall.c
0a7476
+++ b/src/util/virfirewall.c
0a7476
@@ -703,7 +703,7 @@ virFirewallApplyRuleDirect(virFirewallRulePtr rule,
0a7476
         if (ignoreErrors) {
0a7476
             VIR_DEBUG("Ignoring error running command");
0a7476
         } else {
0a7476
-            char *args = virCommandToString(cmd);
0a7476
+            char *args = virCommandToString(cmd, false);
0a7476
             virReportError(VIR_ERR_INTERNAL_ERROR,
0a7476
                            _("Failed to apply firewall rules %s: %s"),
0a7476
                            NULLSTR(args), NULLSTR(error));
0a7476
diff --git a/tests/bhyvexml2argvtest.c b/tests/bhyvexml2argvtest.c
0a7476
index 6d5f19e2c6..d1b486fa64 100644
0a7476
--- a/tests/bhyvexml2argvtest.c
0a7476
+++ b/tests/bhyvexml2argvtest.c
0a7476
@@ -68,13 +68,13 @@ static int testCompareXMLToArgvFiles(const char *xml,
0a7476
         goto out;
0a7476
     }
0a7476
 
0a7476
-    if (!(actualargv = virCommandToString(cmd)))
0a7476
+    if (!(actualargv = virCommandToString(cmd, false)))
0a7476
         goto out;
0a7476
 
0a7476
     if (actualdm != NULL)
0a7476
         virTrimSpaces(actualdm, NULL);
0a7476
 
0a7476
-    if (!(actualld = virCommandToString(ldcmd)))
0a7476
+    if (!(actualld = virCommandToString(ldcmd, false)))
0a7476
         goto out;
0a7476
 
0a7476
     if (virTestCompareToFile(actualargv, cmdline) < 0)
0a7476
diff --git a/tests/commanddata/test26.log b/tests/commanddata/test26.log
0a7476
new file mode 100644
0a7476
index 0000000000..db0d424875
0a7476
--- /dev/null
0a7476
+++ b/tests/commanddata/test26.log
0a7476
@@ -0,0 +1 @@
0a7476
+A=B C=D  E true --foo bar --oooh -f --wizz eek eek -w -z -l --mmm flash bang wallop
0a7476
diff --git a/tests/commandtest.c b/tests/commandtest.c
0a7476
index 744a387aa0..4edef6157a 100644
0a7476
--- a/tests/commandtest.c
0a7476
+++ b/tests/commandtest.c
0a7476
@@ -633,7 +633,7 @@ static int test16(const void *unused ATTRIBUTE_UNUSED)
0a7476
     virCommandAddArg(cmd, "F");
0a7476
     virCommandAddArg(cmd, "G  H");
0a7476
 
0a7476
-    if ((outactual = virCommandToString(cmd)) == NULL) {
0a7476
+    if ((outactual = virCommandToString(cmd, false)) == NULL) {
0a7476
         printf("Cannot convert to string: %s\n", virGetLastErrorMessage());
0a7476
         goto cleanup;
0a7476
     }
0a7476
@@ -1138,6 +1138,67 @@ static int test25(const void *unused ATTRIBUTE_UNUSED)
0a7476
 }
0a7476
 
0a7476
 
0a7476
+/*
0a7476
+ * Don't run program; rather, log what would be run.
0a7476
+ */
0a7476
+static int test26(const void *unused ATTRIBUTE_UNUSED)
0a7476
+{
0a7476
+    virCommandPtr cmd = virCommandNew("true");
0a7476
+    char *outactual = NULL;
0a7476
+    const char *outexpect =
0a7476
+        "A=B \\\n"
0a7476
+        "C='D  E' \\\n"
0a7476
+        "true \\\n"
0a7476
+        "--foo bar \\\n"
0a7476
+        "--oooh \\\n"
0a7476
+        "-f \\\n"
0a7476
+        "--wizz 'eek eek' \\\n"
0a7476
+        "-w \\\n"
0a7476
+        "-z \\\n"
0a7476
+        "-l \\\n"
0a7476
+        "--mmm flash \\\n"
0a7476
+        "bang \\\n"
0a7476
+        "wallop";
0a7476
+
0a7476
+    int ret = -1;
0a7476
+    int fd = -1;
0a7476
+
0a7476
+    virCommandAddEnvPair(cmd, "A", "B");
0a7476
+    virCommandAddEnvPair(cmd, "C", "D  E");
0a7476
+    virCommandAddArgList(cmd, "--foo", "bar", "--oooh", "-f",
0a7476
+                         "--wizz", "eek eek", "-w", "-z", "-l",
0a7476
+                         "--mmm", "flash", "bang", "wallop",
0a7476
+                         NULL);
0a7476
+
0a7476
+    if ((outactual = virCommandToString(cmd, true)) == NULL) {
0a7476
+        printf("Cannot convert to string: %s\n", virGetLastErrorMessage());
0a7476
+        goto cleanup;
0a7476
+    }
0a7476
+    if ((fd = open(abs_builddir "/commandhelper.log",
0a7476
+                   O_CREAT | O_TRUNC | O_WRONLY, 0600)) < 0) {
0a7476
+        printf("Cannot open log file: %s\n", strerror(errno));
0a7476
+        goto cleanup;
0a7476
+    }
0a7476
+    virCommandWriteArgLog(cmd, fd);
0a7476
+    if (VIR_CLOSE(fd) < 0) {
0a7476
+        printf("Cannot close log file: %s\n", strerror(errno));
0a7476
+        goto cleanup;
0a7476
+    }
0a7476
+
0a7476
+    if (STRNEQ(outactual, outexpect)) {
0a7476
+        virTestDifference(stderr, outexpect, outactual);
0a7476
+        goto cleanup;
0a7476
+    }
0a7476
+
0a7476
+    ret = checkoutput("test26", NULL);
0a7476
+
0a7476
+ cleanup:
0a7476
+    virCommandFree(cmd);
0a7476
+    VIR_FORCE_CLOSE(fd);
0a7476
+    VIR_FREE(outactual);
0a7476
+    return ret;
0a7476
+}
0a7476
+
0a7476
 static void virCommandThreadWorker(void *opaque)
0a7476
 {
0a7476
     virCommandTestDataPtr test = opaque;
0a7476
@@ -1291,6 +1352,7 @@ mymain(void)
0a7476
     DO_TEST(test23);
0a7476
     DO_TEST(test24);
0a7476
     DO_TEST(test25);
0a7476
+    DO_TEST(test26);
0a7476
 
0a7476
     virMutexLock(&test->lock);
0a7476
     if (test->running) {
0a7476
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
0a7476
index a29e63d950..9933e4bde1 100644
0a7476
--- a/tests/qemuxml2argvtest.c
0a7476
+++ b/tests/qemuxml2argvtest.c
0a7476
@@ -575,7 +575,7 @@ testCompareXMLToArgv(const void *data)
0a7476
         goto cleanup;
0a7476
     }
0a7476
 
0a7476
-    if (!(actualargv = virCommandToString(cmd)))
0a7476
+    if (!(actualargv = virCommandToString(cmd, false)))
0a7476
         goto cleanup;
0a7476
 
0a7476
     if (virTestCompareToFile(actualargv, args) < 0)
0a7476
diff --git a/tests/storagepoolxml2argvtest.c b/tests/storagepoolxml2argvtest.c
0a7476
index 1e48910566..19a64303a7 100644
0a7476
--- a/tests/storagepoolxml2argvtest.c
0a7476
+++ b/tests/storagepoolxml2argvtest.c
0a7476
@@ -60,7 +60,7 @@ testCompareXMLToArgvFiles(bool shouldFail,
0a7476
         goto cleanup;
0a7476
     };
0a7476
 
0a7476
-    if (!(actualCmdline = virCommandToString(cmd))) {
0a7476
+    if (!(actualCmdline = virCommandToString(cmd, false))) {
0a7476
         VIR_TEST_DEBUG("pool type %d failed to get commandline\n", def->type);
0a7476
         goto cleanup;
0a7476
     }
0a7476
diff --git a/tests/storagevolxml2argvtest.c b/tests/storagevolxml2argvtest.c
0a7476
index 105705f348..bc2da37410 100644
0a7476
--- a/tests/storagevolxml2argvtest.c
0a7476
+++ b/tests/storagevolxml2argvtest.c
0a7476
@@ -104,14 +104,14 @@ testCompareXMLToArgvFiles(bool shouldFail,
0a7476
         }
0a7476
 
0a7476
         if (convertStep != VIR_STORAGE_VOL_ENCRYPT_CONVERT) {
0a7476
-            if (!(actualCmdline = virCommandToString(cmd)))
0a7476
+            if (!(actualCmdline = virCommandToString(cmd, false)))
0a7476
                 goto cleanup;
0a7476
         } else {
0a7476
             char *createCmdline = actualCmdline;
0a7476
             char *cvtCmdline;
0a7476
             int rc;
0a7476
 
0a7476
-            if (!(cvtCmdline = virCommandToString(cmd)))
0a7476
+            if (!(cvtCmdline = virCommandToString(cmd, false)))
0a7476
                 goto cleanup;
0a7476
 
0a7476
             rc = virAsprintf(&actualCmdline, "%s\n%s",
0a7476
-- 
0a7476
2.21.0
0a7476