qemu: Fix escape_monitor(escape_shell(command))

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>
This commit is contained in:
Philipp Hahn 2011-02-11 13:59:11 +01:00 committed by Eric Blake
parent cee61fc298
commit f370fc37f9

View 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;
}