[lxc-devel] [PATCH 1/1] resolve lxcpath and rcfile arguments

Serge Hallyn serge.hallyn at ubuntu.com
Mon Nov 4 20:44:44 UTC 2013


So we don't always have to give full paths.

realpath(x,NULL) mallocs memory, but these paths shouldn't get called
from the API so not freeing should be ok.

Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
---
 src/lxc/arguments.c   |  9 ++++++++-
 src/lxc/lxc_execute.c | 10 +++++++---
 src/lxc/lxc_start.c   |  9 +++++++++
 3 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/src/lxc/arguments.c b/src/lxc/arguments.c
index adcf8fe..65b2284 100644
--- a/src/lxc/arguments.c
+++ b/src/lxc/arguments.c
@@ -155,6 +155,7 @@ See the %s man page for further information.\n\n",
 static int lxc_arguments_lxcpath_add(struct lxc_arguments *args,
 				     const char *lxcpath)
 {
+	char *resolved_path = NULL;
 	if (args->lxcpath_additional != -1 &&
 	    args->lxcpath_cnt > args->lxcpath_additional) {
 		fprintf(stderr, "This command only accepts %d -P,--lxcpath arguments\n",
@@ -168,7 +169,13 @@ static int lxc_arguments_lxcpath_add(struct lxc_arguments *args,
 		lxc_error(args, "no memory");
 		return -ENOMEM;
 	}
-	args->lxcpath[args->lxcpath_cnt++] = lxcpath;
+	resolved_path = realpath(lxcpath, NULL);
+	if (!resolved_path) {
+		perror("realpath");
+		fprintf(stderr, "Error resolving lxcpath %s", lxcpath);
+		return -1;
+	}
+	args->lxcpath[args->lxcpath_cnt++] = resolved_path;
 	return 0;
 }
 
diff --git a/src/lxc/lxc_execute.c b/src/lxc/lxc_execute.c
index 6a54bf6..083b5db 100644
--- a/src/lxc/lxc_execute.c
+++ b/src/lxc/lxc_execute.c
@@ -105,9 +105,13 @@ int main(int argc, char *argv[])
 		return -1;
 
 	/* rcfile is specified in the cli option */
-	if (my_args.rcfile)
-		rcfile = (char *)my_args.rcfile;
-	else {
+	if (my_args.rcfile) {
+		rcfile = realpath((char *)my_args.rcfile, NULL);
+		if (!rcfile) {
+			SYSERROR("Failed to resolve file path: %s", my_args.rcfile);
+			return -1;
+		}
+	} else {
 		int rc;
 
 		rc = asprintf(&rcfile, "%s/%s/config", my_args.lxcpath[0], my_args.name);
diff --git a/src/lxc/lxc_start.c b/src/lxc/lxc_start.c
index add2542..7eea26a 100644
--- a/src/lxc/lxc_start.c
+++ b/src/lxc/lxc_start.c
@@ -173,6 +173,15 @@ int main(int argc, char *argv[])
 
 	const char *lxcpath = my_args.lxcpath[0];
 
+	if (my_args.rcfile) {
+		char *r = realpath(my_args.rcfile, NULL);
+		if (!r) {
+			SYSERROR("Failure resolving path: %s", my_args.rcfile);
+			exit(1);
+		}
+		my_args.rcfile = r;
+	}
+
 	/*
 	 * rcfile possibilities:
 	 * 1. rcfile from random path specified in cli option
-- 
1.8.1.2





More information about the lxc-devel mailing list