Commit-ID: b793fc0f29cb6b010a4ae595b0209bab6c829f9c Gitweb: http://git.kernel.org/tip/b793fc0f29cb6b010a4ae595b0209bab6c829f9c Author: Lai Jiangshan <laijs@xxxxxxxxxxxxxx> AuthorDate: Fri, 16 Dec 2011 17:12:05 +0800 Committer: Pekka Enberg <penberg@xxxxxxxxxx> CommitDate: Sat, 17 Dec 2011 09:36:03 +0200 kvm-tools: write the sandbox command exactly as expect sandbox.sh is a shell script, it will translate its meta chars to anather things. example(before this patch applied): ./lkvm sandbox -- echo 'success; please try again!' it fails with: /virt/sandbox.sh: line 3: please: command not found Acked-by: Sasha Levin <levinsasha928@xxxxxxxxx> Signed-off-by: Lai Jiangshan <laijs@xxxxxxxxxxxxxx> Signed-off-by: Pekka Enberg <penberg@xxxxxxxxxx> --- tools/kvm/builtin-run.c | 35 +++++++++++++++++++++++++++++++++-- 1 files changed, 33 insertions(+), 2 deletions(-) diff --git a/tools/kvm/builtin-run.c b/tools/kvm/builtin-run.c index 4d3f5ec..524ca16 100644 --- a/tools/kvm/builtin-run.c +++ b/tools/kvm/builtin-run.c @@ -785,6 +785,38 @@ static int kvm_run_set_sandbox(void) return symlink(script, path); } +static void kvm_write_sandbox_cmd_exactly(int fd, const char *arg) +{ + const char *single_quote; + + if (!*arg) { /* zero length string */ + if (write(fd, "''", 2) <= 0) + die("Failed writing sandbox script"); + return; + } + + while (*arg) { + single_quote = strchrnul(arg, '\''); + + /* write non-single-quote string as #('string') */ + if (arg != single_quote) { + if (write(fd, "'", 1) <= 0 || + write(fd, arg, single_quote - arg) <= 0 || + write(fd, "'", 1) <= 0) + die("Failed writing sandbox script"); + } + + /* write single quote as #("'") */ + if (*single_quote) { + if (write(fd, "\"'\"", 3) <= 0) + die("Failed writing sandbox script"); + } else + break; + + arg = single_quote + 1; + } +} + static void kvm_run_write_sandbox_cmd(const char **argv, int argc) { const char script_hdr[] = "#! /bin/bash\n\n"; @@ -800,8 +832,7 @@ static void kvm_run_write_sandbox_cmd(const char **argv, int argc) die("Failed writing sandbox script"); while (argc) { - if (write(fd, argv[0], strlen(argv[0])) <= 0) - die("Failed writing sandbox script"); + kvm_write_sandbox_cmd_exactly(fd, argv[0]); if (argc - 1) if (write(fd, " ", 1) <= 0) die("Failed writing sandbox script"); -- To unsubscribe from this list: send the line "unsubscribe linux-tip-commits" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html