All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pekka Enberg <penberg@kernel.org>
To: kvm@vger.kernel.org
Cc: Pekka Enberg <penberg@kernel.org>,
	Asias He <asias.hejun@gmail.com>,
	Cyrill Gorcunov <gorcunov@openvz.org>,
	Ingo Molnar <mingo@elte.hu>,
	Sasha Levin <levinsasha928@gmail.com>
Subject: [PATCH] kvm tools: Make 'vm sandbox' more user-friendly
Date: Wed,  7 Mar 2012 20:53:59 +0200	[thread overview]
Message-ID: <1331146439-21710-1-git-send-email-penberg@kernel.org> (raw)

This patch changes 'vm sandbox' to automatically prefix a program path with
"/host" in the guest side making this, for example, work as expected:

  $ ./vm sandbox -- ~/trinity/trinity --mode=random --dangerous

Cc: Asias He <asias.hejun@gmail.com>
Cc: Cyrill Gorcunov <gorcunov@openvz.org>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
---
 tools/kvm/builtin-run.c |   29 ++++++++++++++++++++++++++---
 1 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/tools/kvm/builtin-run.c b/tools/kvm/builtin-run.c
index 6acc490..ce76b69 100644
--- a/tools/kvm/builtin-run.c
+++ b/tools/kvm/builtin-run.c
@@ -847,9 +847,26 @@ static void kvm_write_sandbox_cmd_exactly(int fd, const char *arg)
 	}
 }
 
+static void resolve_program(const char *src, char *dst, size_t len)
+{
+	struct stat st;
+
+	stat(src, &st);
+
+	if (S_ISREG(st.st_mode)) {
+		char resolved_path[PATH_MAX];
+
+		realpath(src, resolved_path);
+
+		snprintf(dst, len, "/host%s", resolved_path);
+	} else
+		strncpy(dst, src, len);
+}
+
 static void kvm_run_write_sandbox_cmd(const char **argv, int argc)
 {
 	const char script_hdr[] = "#! /bin/bash\n\n";
+	char program[PATH_MAX];
 	int fd;
 
 	remove(sandbox);
@@ -861,11 +878,17 @@ static void kvm_run_write_sandbox_cmd(const char **argv, int argc)
 	if (write(fd, script_hdr, sizeof(script_hdr) - 1) <= 0)
 		die("Failed writing sandbox script");
 
+	resolve_program(argv[0], program, PATH_MAX);
+	kvm_write_sandbox_cmd_exactly(fd, program);
+
+	argv++;
+	argc--;
+
 	while (argc) {
+		if (write(fd, " ", 1) <= 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");
 		argv++;
 		argc--;
 	}
-- 
1.7.6.5


             reply	other threads:[~2012-03-07 18:54 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-07 18:53 Pekka Enberg [this message]
2012-03-07 21:50 ` [PATCH] kvm tools: Make 'vm sandbox' more user-friendly Cyrill Gorcunov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1331146439-21710-1-git-send-email-penberg@kernel.org \
    --to=penberg@kernel.org \
    --cc=asias.hejun@gmail.com \
    --cc=gorcunov@openvz.org \
    --cc=kvm@vger.kernel.org \
    --cc=levinsasha928@gmail.com \
    --cc=mingo@elte.hu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.