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)
if (args->quiet)
nbargs += 1;
argv = malloc(nbargs * sizeof(*argv));
argv = malloc((nbargs + 1) * sizeof(*argv));
if (!argv)
return NULL;
......
......@@ -917,7 +917,6 @@ out:
int lxc_conf_init(struct lxc_conf *conf)
{
conf->rcfile = NULL;
conf->rootfs = NULL;
conf->fstab = NULL;
conf->utsname = NULL;
......
......@@ -129,7 +129,6 @@ struct lxc_tty_info {
* @utsname : the container utsname
*/
struct lxc_conf {
const char *rcfile;
char *rootfs;
char *fstab;
int tty;
......
......@@ -538,8 +538,6 @@ 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);
}
......@@ -101,7 +101,7 @@ static int remove_lxc_directory(const char *dirname)
static int copy_config_file(const char *name, const char *file)
{
char *dst;
int ret;
int ret = -1;
if (!asprintf(&dst, LXCPATH "/%s/config", name)) {
ERROR("failed to allocate memory");
......@@ -111,23 +111,27 @@ static int copy_config_file(const char *name, const char *file)
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 lxc_create(const char *name, const char *confile)
{
int lock, err = -1;
if (create_lxc_directory(name))
return err;
if (!confile)
return 0;
lock = lxc_get_lock(name);
if (lock < 0)
goto err;
if (conf->rcfile && copy_config_file(name, conf->rcfile)) {
if (copy_config_file(name, confile)) {
ERROR("failed to copy the configuration file");
goto err_state;
}
......
......@@ -54,7 +54,7 @@ extern "C" {
* @conf : the configuration data for the container
* 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>
......
......@@ -88,7 +88,7 @@ int main(int argc, char *argv[])
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");
return -1;
}
......
......@@ -81,7 +81,6 @@ int main(int argc, char *argv[])
char path[MAXPATHLEN];
int autodestroy = 0;
int ret = -1;
struct lxc_conf lxc_conf;
if (lxc_arguments_parse(&my_args, argc, argv))
goto out;
......@@ -90,15 +89,10 @@ int main(int argc, char *argv[])
my_args.progname, my_args.quiet))
goto out;
if (lxc_conf_init(&lxc_conf))
goto out;
if (my_args.rcfile && lxc_config_read(my_args.rcfile, &lxc_conf))
goto out;
/* the container is not created, let's create it */
snprintf(path, MAXPATHLEN, LXCPATH "/%s", my_args.name);
if (access(path, R_OK)) {
if (lxc_create(my_args.name, &lxc_conf))
if (lxc_create(my_args.name, my_args.rcfile))
goto out;
autodestroy = 1;
}
......
......@@ -263,15 +263,13 @@ struct lxc_handler *lxc_init(const char *name)
snprintf(path, sizeof(path), LXCPATH "/%s/config", name);
if (!access(path, F_OK)) {
if (lxc_config_read(path, &handler->conf)) {
ERROR("failed to read the configuration file");
goto out_aborting;
}
if (!access(path, F_OK) && lxc_config_read(path, &handler->conf)) {
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");
goto out_aborting;
}
......
......@@ -70,7 +70,7 @@ int main(int argc, char *argv[])
return 1;
}
if (lxc_create(name, &lxc_conf)) {
if (lxc_create(name, file)) {
fprintf(stderr, "failed to create <%s>\n", name);
return 1;
}
......
......@@ -58,7 +58,7 @@ int main(int argc, char *argv[])
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);
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