]> git.proxmox.com Git - mirror_lxc.git/commitdiff
clean up and factor out some code
authorDaniel Lezcano <daniel.lezcano@free.fr>
Mon, 12 Oct 2009 20:02:06 +0000 (22:02 +0200)
committerDaniel Lezcano <dlezcano@fr.ibm.com>
Mon, 12 Oct 2009 20:02:06 +0000 (22:02 +0200)
Factor out some code and fix a memory corruption when
dupping the arguments.

Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
src/lxc/arguments.c
src/lxc/conf.c
src/lxc/conf.h
src/lxc/confile.c
src/lxc/create.c
src/lxc/lxc.h
src/lxc/lxc_create.c
src/lxc/lxc_execute.c
src/lxc/start.c
test/confile.c
test/lxc_create.c

index 03ec2afeb258cd98cdba1527833e368775ccba88..b5ab5b4e273a67c407ecfbb0b7afabfbefdc56d7 100644 (file)
@@ -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;
 
index e2d2da7bb230e317f734cecf4602c4895a132507..c733bff15e72a5f58fbf97523326edc378f184c8 100644 (file)
@@ -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;
index 3b3a1d9667942ab656623e090a2a32815c93fcf7..c6e049663d7d03ef5e7ff195c362344150fc6d67 100644 (file)
@@ -129,7 +129,6 @@ struct lxc_tty_info {
  * @utsname : the container utsname
  */
 struct lxc_conf {
-       const char *rcfile;
        char *rootfs;
        char *fstab;
        int tty;
index 11667f859103c49ff07a1256cab2f058365dfccc..900ecc3e398029e168ca823185453f8832fd42af 100644 (file)
@@ -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);
 }
index 4f0d92ca987d9fe5eb14a3a06b505c318b77a2e7..2527c779f114d33f228c5e9d42bda28c0b5c0975 100644 (file)
@@ -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;
        }
index 89572f2c66a26876b0dd162a62ea7de6e09c7671..327a90becfb1372a2e4f86e76a488a0826a3462b 100644 (file)
@@ -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>
index f0167988088e25774f429606e28afc806f837b67..39cbaf5fbe84a87c24714594e430d1cd98644c44 100644 (file)
@@ -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;
        }
index a43d435c87e2767ed9335ddb1ee34cb407ef3502..22defbcb910239d4bd64019f7ae9d4822e5be104 100644 (file)
@@ -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;
        }
index 42745bc85389eafbccce391b1624a82bbe64e728..5759024082e6ee903d517a4151b29ab38eb34ca0 100644 (file)
@@ -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;
        }
index 10bed64196a64d5221b53900bf00a39ce213a616..f44d303d2cd6b3164108fdbc4401ee71b14dc8a9 100644 (file)
@@ -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;
        }
index d4aea981c3654ce6c8fd333261c7d0a092bad3e7..50ab1214441d0137ac873ba953b2c592fa52ee13 100644 (file)
@@ -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;
        }