]> xenbits.xensource.com Git - libvirt.git/commitdiff
qemu: Fix escape_monitor(escape_shell(command))
authorPhilipp Hahn <hahn@univention.de>
Fri, 11 Feb 2011 12:59:11 +0000 (13:59 +0100)
committerEric Blake <eblake@redhat.com>
Fri, 11 Feb 2011 19:07:35 +0000 (12:07 -0700)
Suspending a VM which contains shell meta characters doesn't work with
libvirt-0.8.7:
/var/log/libvirt/qemu/andreas_231-ne\ doch\ nicht.log:
  sh: -c: line 0: syntax error near unexpected token `doch'
  sh: -c: line 0: `cat | { dd bs=4096 seek=1 if=/dev/null && dd bs=1048576; }

Although target="andreas_231-ne doch nicht" contains shell meta
characters (here: blanks), they are not properly escaped by
src/qemu/qemu_monitor_{json,text}.c#qemuMonitor{JSON,Text}MigrateToFile()

First, the filename needs to be properly escaped for the shell, than
this command line has to be properly escaped for qemu again.

For this to work, remove the old qemuMonitorEscapeArg() wrapper, rename
qemuMonitorEscape() to it removing the handling for shell=TRUE, and
implement a new qemuMonitorEscapeShell() returning strings using single
quotes.

Using double quotes or escaping special shell characters with backslashes
would also be possible, but the set of special characters heavily
depends on the concrete shell (dsh, bash, zsh) and its setting (history
expansion, interactive use, ...)

Signed-off-by: Philipp Hahn <hahn@univention.de>
src/qemu/qemu_monitor.c

index fdb6b79924ccb14b79b3771d431f64885e19ec31..e3d0a0f64d1c0076eeeb2871ecf7ed9da0b0be96 100644 (file)
@@ -79,7 +79,7 @@ VIR_ENUM_IMPL(qemuMonitorMigrationStatus,
               QEMU_MONITOR_MIGRATION_STATUS_LAST,
               "inactive", "active", "completed", "failed", "cancelled")
 
-static char *qemuMonitorEscape(const char *in, int shell)
+char *qemuMonitorEscapeArg(const char *in)
 {
     int len = 0;
     int i, j;
@@ -87,9 +87,6 @@ static char *qemuMonitorEscape(const char *in, int shell)
 
     /* To pass through the QEMU monitor, we need to use escape
        sequences: \r, \n, \", \\
-
-       To pass through both QEMU + the shell, we need to escape
-       the single character ' as the five characters '\\''
     */
 
     for (i = 0; in[i] != '\0'; i++) {
@@ -100,12 +97,6 @@ static char *qemuMonitorEscape(const char *in, int shell)
         case '\\':
             len += 2;
             break;
-        case '\'':
-            if (shell)
-                len += 5;
-            else
-                len += 1;
-            break;
         default:
             len += 1;
             break;
@@ -130,17 +121,6 @@ static char *qemuMonitorEscape(const char *in, int shell)
             out[j++] = '\\';
             out[j++] = in[i];
             break;
-        case '\'':
-            if (shell) {
-                out[j++] = '\'';
-                out[j++] = '\\';
-                out[j++] = '\\';
-                out[j++] = '\'';
-                out[j++] = '\'';
-            } else {
-                out[j++] = in[i];
-            }
-            break;
         default:
             out[j++] = in[i];
             break;
@@ -151,14 +131,45 @@ static char *qemuMonitorEscape(const char *in, int shell)
     return out;
 }
 
-char *qemuMonitorEscapeArg(const char *in)
-{
-    return qemuMonitorEscape(in, 0);
-}
-
 char *qemuMonitorEscapeShell(const char *in)
 {
-    return qemuMonitorEscape(in, 1);
+    int len = 2; /* leading and trailing single quote */
+    int i, j;
+    char *out;
+
+    for (i = 0; in[i] != '\0'; i++) {
+        switch(in[i]) {
+        case '\'':
+            len += 4; /* '\'' */
+            break;
+        default:
+            len += 1;
+            break;
+        }
+    }
+
+    if (VIR_ALLOC_N(out, len + 1) < 0)
+        return NULL;
+
+    j = 0;
+    out[j++] = '\'';
+    for (i = 0; in[i] != '\0'; i++) {
+        switch(in[i]) {
+        case '\'':
+            out[j++] = '\'';
+            out[j++] = '\\';
+            out[j++] = '\'';
+            out[j++] = '\'';
+            break;
+        default:
+            out[j++] = in[i];
+            break;
+        }
+    }
+    out[j++] = '\'';
+    out[j] = '\0';
+
+    return out;
 }