Commit 7fef7a06 by Daniel Lezcano Committed by Daniel Lezcano

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: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent c547a835
...@@ -1279,6 +1279,18 @@ int lxc_create_network(struct lxc_list *network) ...@@ -1279,6 +1279,18 @@ int lxc_create_network(struct lxc_list *network)
return 0; 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) int lxc_assign_network(struct lxc_list *network, pid_t pid)
{ {
struct lxc_list *iterator; struct lxc_list *iterator;
......
...@@ -197,6 +197,7 @@ struct lxc_conf { ...@@ -197,6 +197,7 @@ struct lxc_conf {
extern struct lxc_conf *lxc_conf_init(void); extern struct lxc_conf *lxc_conf_init(void);
extern int lxc_create_network(struct lxc_list *networks); 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_assign_network(struct lxc_list *networks, pid_t pid);
extern int lxc_create_tty(const char *name, struct lxc_conf *conf); extern int lxc_create_tty(const char *name, struct lxc_conf *conf);
......
...@@ -367,7 +367,7 @@ int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[]) ...@@ -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); handler->pid = lxc_clone(do_start, &start_arg, clone_flags);
if (handler->pid < 0) { if (handler->pid < 0) {
SYSERROR("failed to fork into a new namespace"); SYSERROR("failed to fork into a new namespace");
goto out_close; goto out_delete_net;
} }
close(sv[0]); close(sv[0]);
...@@ -375,17 +375,17 @@ int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[]) ...@@ -375,17 +375,17 @@ int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[])
/* Wait for the child to be ready */ /* Wait for the child to be ready */
if (read(sv[1], &sync, sizeof(sync)) < 0) { if (read(sv[1], &sync, sizeof(sync)) < 0) {
SYSERROR("failed to read the socket"); SYSERROR("failed to read the socket");
goto out_abort; goto out_delete_net;
} }
if (lxc_rename_nsgroup(name, handler)) if (lxc_rename_nsgroup(name, handler))
goto out_abort; goto out_delete_net;
/* Create the network configuration */ /* Create the network configuration */
if (clone_flags & CLONE_NEWNET) { if (clone_flags & CLONE_NEWNET) {
if (lxc_assign_network(&handler->conf->network, handler->pid)) { if (lxc_assign_network(&handler->conf->network, handler->pid)) {
ERROR("failed to create the configured network"); ERROR("failed to create the configured network");
goto out_abort; goto out_delete_net;
} }
} }
...@@ -416,6 +416,9 @@ out_close: ...@@ -416,6 +416,9 @@ out_close:
close(sv[1]); close(sv[1]);
return err; return err;
out_delete_net:
if (clone_flags & CLONE_NEWNET)
lxc_delete_network(&handler->conf->network);
out_abort: out_abort:
lxc_abort(name, handler); lxc_abort(name, handler);
close(sv[1]); close(sv[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