[lxc-devel] [PATCH 2/2] lxcapi_create: fix template handling

Serge Hallyn serge.hallyn at ubuntu.com
Thu Jul 11 04:30:29 UTC 2013


1. If no template is passed in, then do not try to execute it.  The user
just wanted to write the configuration.

2. If template is passed in as a full path, then use that instead of
constructing '$templatedir/lxc-$template'.

Reported-by: Wanlong Gao <gaowanlong at cn.fujitsu.com>
Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
---
 src/lxc/lxccontainer.c | 64 +++++++++++++++++++++++++++++++++-----------------
 1 file changed, 43 insertions(+), 21 deletions(-)

diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c
index caf8c72..47d7df1 100644
--- a/src/lxc/lxccontainer.c
+++ b/src/lxc/lxccontainer.c
@@ -646,17 +646,6 @@ static bool lxcapi_stop(struct lxc_container *c)
 	return ret == 0;
 }
 
-static bool valid_template(char *t)
-{
-	struct stat statbuf;
-	int statret;
-
-	statret = stat(t, &statbuf);
-	if (statret == 0)
-		return true;
-	return false;
-}
-
 /*
  * create the standard expected container dir
  */
@@ -715,6 +704,46 @@ static struct bdev *do_bdev_create(struct lxc_container *c, const char *type,
 	return bdev;
 }
 
+/*
+ * Given the '-t' template option to lxc-create, figure out what to
+ * do.  If the template is a full executable path, use that.  If it
+ * is something like 'sshd', then return $templatepath/lxc-sshd.  If
+ * no template was passed in, return NULL  (this is ok).
+ * On error return (char *) -1.
+ */
+char *get_template_path(const char *t)
+{
+	int ret, len;
+	char *tpath;
+
+	if (!t)
+		return NULL;
+
+	if (t[0] == '/' && access(t, X_OK) == 0) {
+		tpath = strdup(t);
+		if (!tpath)
+			return (char *) -1;
+		return tpath;
+	}
+
+	len = strlen(LXCTEMPLATEDIR) + strlen(t) + strlen("/lxc-") + 1;
+	tpath = malloc(len);
+	if (!tpath)
+		return (char *) -1;
+	ret = snprintf(tpath, len, "%s/lxc-%s", LXCTEMPLATEDIR, t);
+	if (ret < 0 || ret >= len) {
+		free(tpath);
+		return (char *) -1;
+	}
+	if (access(tpath, X_OK) < 0) {
+		SYSERROR("bad template: %s\n", t);
+		free(tpath);
+		return (char *) -1;
+	}
+	
+	return tpath;
+}
+
 static char *lxcbasename(char *path)
 {
 	char *p = path + strlen(path) - 1;
@@ -854,20 +883,13 @@ static bool lxcapi_create(struct lxc_container *c, const char *t,
 {
 	bool bret = false;
 	pid_t pid;
-	char *tpath = NULL;
-	int partial_fd, ret, len;
+	char *tpath;
+	int partial_fd;
 
 	if (!c)
 		return false;
 
-	len = strlen(LXCTEMPLATEDIR) + strlen(t) + strlen("/lxc-") + 1;
-	tpath = malloc(len);
-	if (!tpath)
-		return false;
-	ret = snprintf(tpath, len, "%s/lxc-%s", LXCTEMPLATEDIR, t);
-	if (ret < 0 || ret >= len)
-		goto out;
-	if (!valid_template(tpath)) {
+	if ((tpath = get_template_path(t)) < 0) {
 		ERROR("bad template: %s\n", t);
 		goto out;
 	}
-- 
1.8.1.2





More information about the lxc-devel mailing list