Commit d685aa80 by Daniel Lezcano Committed by Daniel Lezcano

clean up and factor out some code

Factor out some code and fix a memory corruption when dupping the arguments. Signed-off-by: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent a059591e
...@@ -214,7 +214,7 @@ extern char **lxc_arguments_dup(const char *file, struct lxc_arguments *args) ...@@ -214,7 +214,7 @@ extern char **lxc_arguments_dup(const char *file, struct lxc_arguments *args)
if (args->quiet) if (args->quiet)
nbargs += 1; nbargs += 1;
argv = malloc(nbargs * sizeof(*argv)); argv = malloc((nbargs + 1) * sizeof(*argv));
if (!argv) if (!argv)
return NULL; return NULL;
......
...@@ -917,7 +917,6 @@ out: ...@@ -917,7 +917,6 @@ out:
int lxc_conf_init(struct lxc_conf *conf) int lxc_conf_init(struct lxc_conf *conf)
{ {
conf->rcfile = NULL;
conf->rootfs = NULL; conf->rootfs = NULL;
conf->fstab = NULL; conf->fstab = NULL;
conf->utsname = NULL; conf->utsname = NULL;
......
...@@ -129,7 +129,6 @@ struct lxc_tty_info { ...@@ -129,7 +129,6 @@ struct lxc_tty_info {
* @utsname : the container utsname * @utsname : the container utsname
*/ */
struct lxc_conf { struct lxc_conf {
const char *rcfile;
char *rootfs; char *rootfs;
char *fstab; char *fstab;
int tty; int tty;
......
...@@ -538,8 +538,6 @@ int lxc_config_read(const char *file, struct lxc_conf *conf) ...@@ -538,8 +538,6 @@ int lxc_config_read(const char *file, struct lxc_conf *conf)
{ {
char buffer[MAXPATHLEN]; char buffer[MAXPATHLEN];
conf->rcfile = file;
return lxc_file_for_each_line(file, parse_line, buffer, return lxc_file_for_each_line(file, parse_line, buffer,
sizeof(buffer), conf); sizeof(buffer), conf);
} }
...@@ -101,7 +101,7 @@ static int remove_lxc_directory(const char *dirname) ...@@ -101,7 +101,7 @@ static int remove_lxc_directory(const char *dirname)
static int copy_config_file(const char *name, const char *file) static int copy_config_file(const char *name, const char *file)
{ {
char *dst; char *dst;
int ret; int ret = -1;
if (!asprintf(&dst, LXCPATH "/%s/config", name)) { if (!asprintf(&dst, LXCPATH "/%s/config", name)) {
ERROR("failed to allocate memory"); ERROR("failed to allocate memory");
...@@ -111,23 +111,27 @@ static int copy_config_file(const char *name, const char *file) ...@@ -111,23 +111,27 @@ static int copy_config_file(const char *name, const char *file)
ret = lxc_copy_file(file, dst); ret = lxc_copy_file(file, dst);
if (ret) if (ret)
ERROR("failed to copy '%s' to '%s'", file, dst); ERROR("failed to copy '%s' to '%s'", file, dst);
free(dst); free(dst);
return ret; return ret;
} }
int lxc_create(const char *name, struct lxc_conf *conf) int lxc_create(const char *name, const char *confile)
{ {
int lock, err = -1; int lock, err = -1;
if (create_lxc_directory(name)) if (create_lxc_directory(name))
return err; return err;
if (!confile)
return 0;
lock = lxc_get_lock(name); lock = lxc_get_lock(name);
if (lock < 0) if (lock < 0)
goto err; goto err;
if (conf->rcfile && copy_config_file(name, conf->rcfile)) { if (copy_config_file(name, confile)) {
ERROR("failed to copy the configuration file"); ERROR("failed to copy the configuration file");
goto err_state; goto err_state;
} }
......
...@@ -54,7 +54,7 @@ extern "C" { ...@@ -54,7 +54,7 @@ extern "C" {
* @conf : the configuration data for the container * @conf : the configuration data for the container
* Returns 0 on success, < 0 otherwise * Returns 0 on success, < 0 otherwise
*/ */
extern int lxc_create(const char *name, struct lxc_conf *conf); extern int lxc_create(const char *name, const char *confile);
/* /*
* Destroy the container object. Removes the files into the /lxc/<name> * Destroy the container object. Removes the files into the /lxc/<name>
......
...@@ -88,7 +88,7 @@ int main(int argc, char *argv[]) ...@@ -88,7 +88,7 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
if (lxc_create(my_args.name, &lxc_conf)) { if (lxc_create(my_args.name, my_args.rcfile)) {
ERROR("failed to create the container"); ERROR("failed to create the container");
return -1; return -1;
} }
......
...@@ -81,7 +81,6 @@ int main(int argc, char *argv[]) ...@@ -81,7 +81,6 @@ int main(int argc, char *argv[])
char path[MAXPATHLEN]; char path[MAXPATHLEN];
int autodestroy = 0; int autodestroy = 0;
int ret = -1; int ret = -1;
struct lxc_conf lxc_conf;
if (lxc_arguments_parse(&my_args, argc, argv)) if (lxc_arguments_parse(&my_args, argc, argv))
goto out; goto out;
...@@ -90,15 +89,10 @@ int main(int argc, char *argv[]) ...@@ -90,15 +89,10 @@ int main(int argc, char *argv[])
my_args.progname, my_args.quiet)) my_args.progname, my_args.quiet))
goto out; goto out;
if (lxc_conf_init(&lxc_conf)) /* the container is not created, let's create it */
goto out;
if (my_args.rcfile && lxc_config_read(my_args.rcfile, &lxc_conf))
goto out;
snprintf(path, MAXPATHLEN, LXCPATH "/%s", my_args.name); snprintf(path, MAXPATHLEN, LXCPATH "/%s", my_args.name);
if (access(path, R_OK)) { if (access(path, R_OK)) {
if (lxc_create(my_args.name, &lxc_conf)) if (lxc_create(my_args.name, my_args.rcfile))
goto out; goto out;
autodestroy = 1; autodestroy = 1;
} }
......
...@@ -263,15 +263,13 @@ struct lxc_handler *lxc_init(const char *name) ...@@ -263,15 +263,13 @@ struct lxc_handler *lxc_init(const char *name)
snprintf(path, sizeof(path), LXCPATH "/%s/config", name); snprintf(path, sizeof(path), LXCPATH "/%s/config", name);
if (!access(path, F_OK)) { if (!access(path, F_OK) && lxc_config_read(path, &handler->conf)) {
ERROR("failed to read the configuration file");
if (lxc_config_read(path, &handler->conf)) { goto out_aborting;
ERROR("failed to read the configuration file");
goto out_aborting;
}
} }
if (console_init(handler->conf.console, sizeof(handler->conf.console))) { if (console_init(handler->conf.console,
sizeof(handler->conf.console))) {
ERROR("failed to initialize the console"); ERROR("failed to initialize the console");
goto out_aborting; goto out_aborting;
} }
......
...@@ -70,7 +70,7 @@ int main(int argc, char *argv[]) ...@@ -70,7 +70,7 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
if (lxc_create(name, &lxc_conf)) { if (lxc_create(name, file)) {
fprintf(stderr, "failed to create <%s>\n", name); fprintf(stderr, "failed to create <%s>\n", name);
return 1; return 1;
} }
......
...@@ -58,7 +58,7 @@ int main(int argc, char *argv[]) ...@@ -58,7 +58,7 @@ int main(int argc, char *argv[])
lxc_conf_init(&lxc_conf); lxc_conf_init(&lxc_conf);
if (lxc_create(name, &lxc_conf)) { if (lxc_create(name, NULL)) {
fprintf(stderr, "failed to create the container %s\n", name); fprintf(stderr, "failed to create the container %s\n", name);
return 1; return 1;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment