keep rcfile for lxc-execute as already done for lxc-create

The code previously added in lxc-create with
commit d7efa8fcbf
is also required in lxc-execute.
So make this code common for the two callers.

Signed-off-by: Michel Normand <normand@fr.ibm.com>
Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
This commit is contained in:
Michel Normand 2009-10-07 10:05:39 +02:00 committed by Daniel Lezcano
parent 70f7755e98
commit cfd1dc0932
7 changed files with 51 additions and 45 deletions

View File

@ -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;

View File

@ -117,6 +117,7 @@ struct lxc_cgroup {
* @utsname : the container utsname
*/
struct lxc_conf {
const char *rcfile;
char *rootfs;
char *fstab;
int tty;

View File

@ -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);
}

View File

@ -21,7 +21,10 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define _GNU_SOURCE
#include <stdio.h>
#undef _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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;