fix network devices cleanup on error

Delete the network devices when an error occurs before they are moved
to the network namespace (network namespace destruction triggers the
network devices deletion). Otherwise they stay in the system.

Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
This commit is contained in:
Daniel Lezcano 2010-02-25 10:24:13 +01:00 committed by Daniel Lezcano
parent c547a83527
commit 7fef7a06d8
3 changed files with 20 additions and 4 deletions

View File

@ -1279,6 +1279,18 @@ int lxc_create_network(struct lxc_list *network)
return 0;
}
void lxc_delete_network(struct lxc_list *network)
{
struct lxc_list *iterator;
struct lxc_netdev *netdev;
lxc_list_for_each(iterator, network) {
netdev = iterator->elem;
if (netdev->ifindex > 0)
lxc_device_delete_index(netdev->ifindex);
}
}
int lxc_assign_network(struct lxc_list *network, pid_t pid)
{
struct lxc_list *iterator;

View File

@ -197,6 +197,7 @@ struct lxc_conf {
extern struct lxc_conf *lxc_conf_init(void);
extern int lxc_create_network(struct lxc_list *networks);
extern void lxc_delete_network(struct lxc_list *networks);
extern int lxc_assign_network(struct lxc_list *networks, pid_t pid);
extern int lxc_create_tty(const char *name, struct lxc_conf *conf);

View File

@ -367,7 +367,7 @@ int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[])
handler->pid = lxc_clone(do_start, &start_arg, clone_flags);
if (handler->pid < 0) {
SYSERROR("failed to fork into a new namespace");
goto out_close;
goto out_delete_net;
}
close(sv[0]);
@ -375,17 +375,17 @@ int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[])
/* Wait for the child to be ready */
if (read(sv[1], &sync, sizeof(sync)) < 0) {
SYSERROR("failed to read the socket");
goto out_abort;
goto out_delete_net;
}
if (lxc_rename_nsgroup(name, handler))
goto out_abort;
goto out_delete_net;
/* Create the network configuration */
if (clone_flags & CLONE_NEWNET) {
if (lxc_assign_network(&handler->conf->network, handler->pid)) {
ERROR("failed to create the configured network");
goto out_abort;
goto out_delete_net;
}
}
@ -416,6 +416,9 @@ out_close:
close(sv[1]);
return err;
out_delete_net:
if (clone_flags & CLONE_NEWNET)
lxc_delete_network(&handler->conf->network);
out_abort:
lxc_abort(name, handler);
close(sv[1]);