ret = faccessat(AT_FDCWD, path, F_OK, AT_SYMLINK_NOFOLLOW);
if(ret == 0) {
ret = unlink(path);
- if (ret < 0)
+ if (ret < 0) {
+ ERROR("%s - Failed to remove \"%s\"", strerror(errno), path);
exit(EXIT_FAILURE);
+ }
}
if (!add)
directory_path = dirname(tmp);
ret = mkdir_p(directory_path, 0755);
if (ret < 0 && errno != EEXIST) {
+ ERROR("%s - Failed to create path \"%s\"", strerror(errno), directory_path);
free(tmp);
exit(EXIT_FAILURE);
}
/* create the device node */
ret = mknod(path, st->st_mode, st->st_rdev);
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_SUCCESS);
}
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,
- const char *dst_ifname)
+static bool do_lxcapi_attach_interface(struct lxc_container *c,
+ const char *ifname,
+ const char *dst_ifname)
{
+ pid_t init_pid;
int ret = 0;
+
if (am_unpriv()) {
ERROR(NOT_SUPPORTED_ERROR, __FUNCTION__);
return false;
}
ret = lxc_netdev_isup(ifname);
-
if (ret > 0) {
/* netdev of ifname is up. */
ret = lxc_netdev_down(ifname);
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)
goto err;
+ INFO("Moved network device \"%s\" to network namespace of %d", ifname, init_pid);
return true;
err:
WRAP_API_2(bool, lxcapi_attach_interface, const char *, const char *)
-static bool do_lxcapi_detach_interface(struct lxc_container *c, const char *ifname,
- const char *dst_ifname)
+static bool do_lxcapi_detach_interface(struct lxc_container *c,
+ const char *ifname,
+ const char *dst_ifname)
{
+ int ret;
pid_t pid, pid_outside;
if (am_unpriv()) {
pid_outside = getpid();
pid = fork();
if (pid < 0) {
- ERROR("failed to fork task to get interfaces information");
+ ERROR("Failed to fork");
return false;
}
}
ret = lxc_netdev_isup(ifname);
- if (ret < 0)
- exit(ret);
+ if (ret < 0) {
+ ERROR("Failed to determine whether network device \"%s\" is up", ifname);
+ exit(EXIT_FAILURE);
+ }
/* netdev of ifname is up. */
if (ret) {
ret = lxc_netdev_down(ifname);
- if (ret)
- exit(ret);
+ if (ret) {
+ ERROR("Failed to set network device \"%s\" down", ifname);
+ exit(EXIT_FAILURE);
+ }
}
ret = lxc_netdev_move_by_name(ifname, pid_outside, dst_ifname);
-
- /* -EINVAL means there is no netdev named as ifanme. */
- if (ret == -EINVAL) {
- ERROR("No network device named as %s.", ifname);
+ /* -EINVAL means there is no netdev named as ifname. */
+ if (ret < 0) {
+ if (ret == -EINVAL)
+ 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;
+ INFO("Moved network device \"%s\" to network namespace of %d", ifname, pid_outside);
return true;
}