From cfd1dc09320c200210901b98e88faeab99e9fc9f Mon Sep 17 00:00:00 2001 From: Michel Normand Date: Wed, 7 Oct 2009 10:05:39 +0200 Subject: [PATCH] keep rcfile for lxc-execute as already done for lxc-create The code previously added in lxc-create with commit d7efa8fcbf0911f93c83dc06a708e7d73833dce3 is also required in lxc-execute. So make this code common for the two callers. Signed-off-by: Michel Normand Signed-off-by: Daniel Lezcano --- src/lxc/conf.c | 1 + src/lxc/conf.h | 1 + src/lxc/confile.c | 2 ++ src/lxc/create.c | 26 ++++++++++++++++++++++++++ src/lxc/destroy.c | 21 +++++++++++++++++++++ src/lxc/lxc_create.c | 24 ------------------------ src/lxc/lxc_destroy.c | 21 --------------------- 7 files changed, 51 insertions(+), 45 deletions(-) diff --git a/src/lxc/conf.c b/src/lxc/conf.c index 83ea2c331..78b2e24f2 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -1393,6 +1393,7 @@ out: int lxc_conf_init(struct lxc_conf *conf) { + conf->rcfile = NULL; conf->rootfs = NULL; conf->fstab = NULL; conf->utsname = NULL; diff --git a/src/lxc/conf.h b/src/lxc/conf.h index 1f555880a..9635ec914 100644 --- a/src/lxc/conf.h +++ b/src/lxc/conf.h @@ -117,6 +117,7 @@ struct lxc_cgroup { * @utsname : the container utsname */ struct lxc_conf { + const char *rcfile; char *rootfs; char *fstab; int tty; diff --git a/src/lxc/confile.c b/src/lxc/confile.c index a1896c9a7..72f8103f8 100644 --- a/src/lxc/confile.c +++ b/src/lxc/confile.c @@ -564,6 +564,8 @@ int lxc_config_read(const char *file, struct lxc_conf *conf) { char buffer[MAXPATHLEN]; + conf->rcfile = file; + return lxc_file_for_each_line(file, parse_line, buffer, sizeof(buffer), conf); } diff --git a/src/lxc/create.c b/src/lxc/create.c index 64746b362..41ba5734a 100644 --- a/src/lxc/create.c +++ b/src/lxc/create.c @@ -21,7 +21,10 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ +#define _GNU_SOURCE #include +#undef _GNU_SOURCE +#include #include #include #include @@ -95,6 +98,24 @@ static int remove_lxc_directory(const char *dirname) return 0; } +static int copy_config_file(const char *name, const char *file) +{ + char *dst; + int ret; + + if (!asprintf(&dst, LXCPATH "/%s/config", name)) { + ERROR("failed to allocate memory"); + return -1; + } + + ret = lxc_copy_file(file, dst); + if (ret) + ERROR("failed to copy '%s' to '%s'", file, dst); + free(dst); + + return ret; +} + int lxc_create(const char *name, struct lxc_conf *conf) { int lock, err = -1; @@ -121,6 +142,11 @@ int lxc_create(const char *name, struct lxc_conf *conf) goto err_state; } + if (conf->rcfile && copy_config_file(name, conf->rcfile)) { + ERROR("failed to copy the configuration file"); + goto err_state; + } + err = 0; out: lxc_put_lock(lock); diff --git a/src/lxc/destroy.c b/src/lxc/destroy.c index a06d23b9e..63d7c3a12 100644 --- a/src/lxc/destroy.c +++ b/src/lxc/destroy.c @@ -49,6 +49,24 @@ static int remove_lxc_directory(const char *dirname) return 0; } +static int remove_config_file(const char *name) +{ + char path[MAXPATHLEN]; + + snprintf(path, MAXPATHLEN, LXCPATH "/%s/config", name); + + /* config file does not exists */ + if (access(path, F_OK)) + return 0; + + if (unlink(path)) { + ERROR("failed to unlink '%s'", path); + return -1; + } + + return 0; +} + int lxc_destroy(const char *name) { int lock, ret = -1; @@ -58,6 +76,9 @@ int lxc_destroy(const char *name) if (lock < 0) return ret; + if (remove_config_file(name)) + WARN("failed to remove the configuration file"); + if (lxc_rmstate(name)) { ERROR("failed to remove state file for %s", name); goto out_lock; diff --git a/src/lxc/lxc_create.c b/src/lxc/lxc_create.c index 110d68274..f01679880 100644 --- a/src/lxc/lxc_create.c +++ b/src/lxc/lxc_create.c @@ -67,24 +67,6 @@ Options :\n\ .checker = NULL, }; -static int copy_config_file(const char *name, const char *file) -{ - char *src; - int ret; - - if (!asprintf(&src, LXCPATH "/%s/config", name)) { - ERROR("failed to allocate memory"); - return -1; - } - - ret = lxc_copy_file(file, src); - if (ret) - ERROR("failed to copy '%s' to '%s'", file, src); - free(src); - - return ret; -} - int main(int argc, char *argv[]) { struct lxc_conf lxc_conf; @@ -111,12 +93,6 @@ int main(int argc, char *argv[]) return -1; } - if (my_args.rcfile && copy_config_file(my_args.name, my_args.rcfile)) { - ERROR("failed to copy the configuration file"); - lxc_destroy(my_args.name); - return -1; - } - INFO("'%s' created", my_args.name); return 0; diff --git a/src/lxc/lxc_destroy.c b/src/lxc/lxc_destroy.c index 7aef61687..89b7e34fb 100644 --- a/src/lxc/lxc_destroy.c +++ b/src/lxc/lxc_destroy.c @@ -48,24 +48,6 @@ Options :\n\ .checker = NULL, }; -static int remove_config_file(const char *name) -{ - char path[MAXPATHLEN]; - - snprintf(path, MAXPATHLEN, LXCPATH "/%s/config", name); - - /* config file does not exists */ - if (access(path, F_OK)) - return 0; - - if (unlink(path)) { - ERROR("failed to unlink '%s'", path); - return -1; - } - - return 0; -} - int main(int argc, char *argv[]) { if (lxc_arguments_parse(&my_args, argc, argv)) @@ -75,9 +57,6 @@ int main(int argc, char *argv[]) my_args.progname, my_args.quiet)) return -1; - if (remove_config_file(my_args.name)) - WARN("failed to remove the configuration file"); - if (lxc_destroy(my_args.name)) { ERROR("failed to destroy the container"); return -1;