mirror of
https://git.proxmox.com/git/mirror_lxc
synced 2025-07-27 09:48:32 +00:00
lxccontainer: cleanup {attach,detach}_interface()
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
This commit is contained in:
parent
acbfeda88b
commit
c7d76c0947
@ -4291,8 +4291,10 @@ static bool do_add_remove_node(pid_t init_pid, const char *path, bool add,
|
|||||||
ret = faccessat(AT_FDCWD, path, F_OK, AT_SYMLINK_NOFOLLOW);
|
ret = faccessat(AT_FDCWD, path, F_OK, AT_SYMLINK_NOFOLLOW);
|
||||||
if(ret == 0) {
|
if(ret == 0) {
|
||||||
ret = unlink(path);
|
ret = unlink(path);
|
||||||
if (ret < 0)
|
if (ret < 0) {
|
||||||
|
ERROR("%s - Failed to remove \"%s\"", strerror(errno), path);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!add)
|
if (!add)
|
||||||
@ -4306,6 +4308,7 @@ static bool do_add_remove_node(pid_t init_pid, const char *path, bool add,
|
|||||||
directory_path = dirname(tmp);
|
directory_path = dirname(tmp);
|
||||||
ret = mkdir_p(directory_path, 0755);
|
ret = mkdir_p(directory_path, 0755);
|
||||||
if (ret < 0 && errno != EEXIST) {
|
if (ret < 0 && errno != EEXIST) {
|
||||||
|
ERROR("%s - Failed to create path \"%s\"", strerror(errno), directory_path);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -4313,8 +4316,10 @@ static bool do_add_remove_node(pid_t init_pid, const char *path, bool add,
|
|||||||
/* create the device node */
|
/* create the device node */
|
||||||
ret = mknod(path, st->st_mode, st->st_rdev);
|
ret = mknod(path, st->st_mode, st->st_rdev);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
if (ret < 0)
|
if (ret < 0) {
|
||||||
|
ERROR("%s - Failed to create device node at \"%s\"", strerror(errno), path);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
@ -4392,10 +4397,13 @@ static bool do_lxcapi_remove_device_node(struct lxc_container *c, const char *sr
|
|||||||
|
|
||||||
WRAP_API_2(bool, lxcapi_remove_device_node, const char *, const char *)
|
WRAP_API_2(bool, lxcapi_remove_device_node, const char *, const char *)
|
||||||
|
|
||||||
static bool do_lxcapi_attach_interface(struct lxc_container *c, const char *ifname,
|
static bool do_lxcapi_attach_interface(struct lxc_container *c,
|
||||||
const char *dst_ifname)
|
const char *ifname,
|
||||||
|
const char *dst_ifname)
|
||||||
{
|
{
|
||||||
|
pid_t init_pid;
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
if (am_unpriv()) {
|
if (am_unpriv()) {
|
||||||
ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__);
|
ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__);
|
||||||
return false;
|
return false;
|
||||||
@ -4407,7 +4415,6 @@ static bool do_lxcapi_attach_interface(struct lxc_container *c, const char *ifna
|
|||||||
}
|
}
|
||||||
|
|
||||||
ret = lxc_netdev_isup(ifname);
|
ret = lxc_netdev_isup(ifname);
|
||||||
|
|
||||||
if (ret > 0) {
|
if (ret > 0) {
|
||||||
/* netdev of ifname is up. */
|
/* netdev of ifname is up. */
|
||||||
ret = lxc_netdev_down(ifname);
|
ret = lxc_netdev_down(ifname);
|
||||||
@ -4415,10 +4422,12 @@ static bool do_lxcapi_attach_interface(struct lxc_container *c, const char *ifna
|
|||||||
goto err;
|
goto err;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = lxc_netdev_move_by_name(ifname, do_lxcapi_init_pid(c), dst_ifname);
|
init_pid = do_lxcapi_init_pid(c);
|
||||||
|
ret = lxc_netdev_move_by_name(ifname, init_pid, dst_ifname);
|
||||||
if (ret)
|
if (ret)
|
||||||
goto err;
|
goto err;
|
||||||
|
|
||||||
|
INFO("Moved network device \"%s\" to network namespace of %d", ifname, init_pid);
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
err:
|
err:
|
||||||
@ -4427,9 +4436,11 @@ err:
|
|||||||
|
|
||||||
WRAP_API_2(bool, lxcapi_attach_interface, const char *, const char *)
|
WRAP_API_2(bool, lxcapi_attach_interface, const char *, const char *)
|
||||||
|
|
||||||
static bool do_lxcapi_detach_interface(struct lxc_container *c, const char *ifname,
|
static bool do_lxcapi_detach_interface(struct lxc_container *c,
|
||||||
const char *dst_ifname)
|
const char *ifname,
|
||||||
|
const char *dst_ifname)
|
||||||
{
|
{
|
||||||
|
int ret;
|
||||||
pid_t pid, pid_outside;
|
pid_t pid, pid_outside;
|
||||||
|
|
||||||
if (am_unpriv()) {
|
if (am_unpriv()) {
|
||||||
@ -4445,7 +4456,7 @@ static bool do_lxcapi_detach_interface(struct lxc_container *c, const char *ifna
|
|||||||
pid_outside = getpid();
|
pid_outside = getpid();
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid < 0) {
|
if (pid < 0) {
|
||||||
ERROR("failed to fork task to get interfaces information");
|
ERROR("Failed to fork");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -4459,28 +4470,38 @@ static bool do_lxcapi_detach_interface(struct lxc_container *c, const char *ifna
|
|||||||
}
|
}
|
||||||
|
|
||||||
ret = lxc_netdev_isup(ifname);
|
ret = lxc_netdev_isup(ifname);
|
||||||
if (ret < 0)
|
if (ret < 0) {
|
||||||
exit(ret);
|
ERROR("Failed to determine whether network device \"%s\" is up", ifname);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
/* netdev of ifname is up. */
|
/* netdev of ifname is up. */
|
||||||
if (ret) {
|
if (ret) {
|
||||||
ret = lxc_netdev_down(ifname);
|
ret = lxc_netdev_down(ifname);
|
||||||
if (ret)
|
if (ret) {
|
||||||
exit(ret);
|
ERROR("Failed to set network device \"%s\" down", ifname);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = lxc_netdev_move_by_name(ifname, pid_outside, dst_ifname);
|
ret = lxc_netdev_move_by_name(ifname, pid_outside, dst_ifname);
|
||||||
|
/* -EINVAL means there is no netdev named as ifname. */
|
||||||
/* -EINVAL means there is no netdev named as ifanme. */
|
if (ret < 0) {
|
||||||
if (ret == -EINVAL) {
|
if (ret == -EINVAL)
|
||||||
ERROR("No network device named as %s.", ifname);
|
ERROR("Network device \"%s\" not found", ifname);
|
||||||
|
else
|
||||||
|
ERROR("Failed to remove network device \"%s\"", ifname);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
exit(ret);
|
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (wait_for_pid(pid) != 0)
|
ret = wait_for_pid(pid);
|
||||||
|
if (ret != 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
INFO("Moved network device \"%s\" to network namespace of %d", ifname, pid_outside);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user