From 7342f2ff2594db242bda9390e1b803c0a1d37940 Mon Sep 17 00:00:00 2001 From: =?utf8?q?St=C3=A9phane=20Graber?= Date: Wed, 28 Jan 2015 09:28:22 +0100 Subject: [PATCH] Fix lxc-create -h with absolute template path MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Close #421 Signed-off-by: Stéphane Graber Acked-by: Serge E. Hallyn --- src/lxc/lxc_create.c | 8 +------- src/lxc/lxccontainer.c | 34 ---------------------------------- src/lxc/utils.c | 34 ++++++++++++++++++++++++++++++++++ src/lxc/utils.h | 1 + 4 files changed, 36 insertions(+), 41 deletions(-) diff --git a/src/lxc/lxc_create.c b/src/lxc/lxc_create.c index d49704635..60e501003 100644 --- a/src/lxc/lxc_create.c +++ b/src/lxc/lxc_create.c @@ -101,8 +101,6 @@ static const struct option my_longopts[] = { static void create_helpfn(const struct lxc_arguments *args) { char *argv[3], *path; - size_t len; - int ret; pid_t pid; if (!args->template) @@ -114,11 +112,7 @@ static void create_helpfn(const struct lxc_arguments *args) { return; } - len = strlen(LXCTEMPLATEDIR) + strlen(args->template) + strlen("/lxc-") + 1; - path = alloca(len); - ret = snprintf(path, len, "%s/lxc-%s", LXCTEMPLATEDIR, args->template); - if (ret < 0 || ret >= len) - return; + path = get_template_path(args->template); argv[0] = path; argv[1] = "-h"; diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c index 8bd3d5c48..dfc255334 100644 --- a/src/lxc/lxccontainer.c +++ b/src/lxc/lxccontainer.c @@ -830,40 +830,6 @@ 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. - * On success return the template, on error return NULL. - */ -static char *get_template_path(const char *t) -{ - int ret, len; - char *tpath; - - if (t[0] == '/' && access(t, X_OK) == 0) { - tpath = strdup(t); - return tpath; - } - - len = strlen(LXCTEMPLATEDIR) + strlen(t) + strlen("/lxc-") + 1; - tpath = malloc(len); - if (!tpath) - return NULL; - ret = snprintf(tpath, len, "%s/lxc-%s", LXCTEMPLATEDIR, t); - if (ret < 0 || ret >= len) { - free(tpath); - return NULL; - } - if (access(tpath, X_OK) < 0) { - SYSERROR("bad template: %s", t); - free(tpath); - return NULL; - } - - return tpath; -} - static char *lxcbasename(char *path) { char *p = path + strlen(path) - 1; diff --git a/src/lxc/utils.c b/src/lxc/utils.c index abbec6c9d..2728edbeb 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -1322,3 +1322,37 @@ next_loop: free(path); return NULL; } + +/* + * 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. + * On success return the template, on error return NULL. + */ +char *get_template_path(const char *t) +{ + int ret, len; + char *tpath; + + if (t[0] == '/' && access(t, X_OK) == 0) { + tpath = strdup(t); + return tpath; + } + + len = strlen(LXCTEMPLATEDIR) + strlen(t) + strlen("/lxc-") + 1; + tpath = malloc(len); + if (!tpath) + return NULL; + ret = snprintf(tpath, len, "%s/lxc-%s", LXCTEMPLATEDIR, t); + if (ret < 0 || ret >= len) { + free(tpath); + return NULL; + } + if (access(tpath, X_OK) < 0) { + SYSERROR("bad template: %s", t); + free(tpath); + return NULL; + } + + return tpath; +} diff --git a/src/lxc/utils.h b/src/lxc/utils.h index 9bf678805..d15560c67 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -279,4 +279,5 @@ uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval); int detect_shared_rootfs(void); int detect_ramfs_rootfs(void); char *on_path(char *cmd); +char *get_template_path(const char *t); #endif /* __LXC_UTILS_H */ -- 2.47.2