Unverified Commit 6821739c by Stéphane Graber Committed by GitHub

Merge pull request #3338 from brauner/2020-03-28/fixes

tree-wide: fixes
parents 24e84b34 8408a9cc
...@@ -945,7 +945,7 @@ static void lxc_cgfsng_print_basecg_debuginfo(char *basecginfo, char **klist, ...@@ -945,7 +945,7 @@ static void lxc_cgfsng_print_basecg_debuginfo(char *basecginfo, char **klist,
TRACE("named subsystem %d: %s", k, *it); TRACE("named subsystem %d: %s", k, *it);
} }
static int cgroup_rmdir(struct hierarchy **hierarchies, static int cgroup_tree_remove(struct hierarchy **hierarchies,
const char *container_cgroup) const char *container_cgroup)
{ {
if (!container_cgroup || !hierarchies) if (!container_cgroup || !hierarchies)
...@@ -958,7 +958,7 @@ static int cgroup_rmdir(struct hierarchy **hierarchies, ...@@ -958,7 +958,7 @@ static int cgroup_rmdir(struct hierarchy **hierarchies,
if (!h->container_full_path) if (!h->container_full_path)
continue; continue;
ret = recursive_destroy(h->container_full_path); ret = lxc_rm_rf(h->container_full_path);
if (ret < 0) if (ret < 0)
WARN("Failed to destroy \"%s\"", h->container_full_path); WARN("Failed to destroy \"%s\"", h->container_full_path);
...@@ -976,7 +976,7 @@ struct generic_userns_exec_data { ...@@ -976,7 +976,7 @@ struct generic_userns_exec_data {
char *path; char *path;
}; };
static int cgroup_rmdir_wrapper(void *data) static int cgroup_tree_remove_wrapper(void *data)
{ {
struct generic_userns_exec_data *arg = data; struct generic_userns_exec_data *arg = data;
uid_t nsuid = (arg->conf->root_nsuid_map != NULL) ? 0 : arg->conf->init_uid; uid_t nsuid = (arg->conf->root_nsuid_map != NULL) ? 0 : arg->conf->init_uid;
...@@ -996,7 +996,7 @@ static int cgroup_rmdir_wrapper(void *data) ...@@ -996,7 +996,7 @@ static int cgroup_rmdir_wrapper(void *data)
return log_error_errno(-1, errno, "Failed to setresuid(%d, %d, %d)", return log_error_errno(-1, errno, "Failed to setresuid(%d, %d, %d)",
(int)nsuid, (int)nsuid, (int)nsuid); (int)nsuid, (int)nsuid, (int)nsuid);
return cgroup_rmdir(arg->hierarchies, arg->container_cgroup); return cgroup_tree_remove(arg->hierarchies, arg->container_cgroup);
} }
__cgfsng_ops static void cgfsng_payload_destroy(struct cgroup_ops *ops, __cgfsng_ops static void cgfsng_payload_destroy(struct cgroup_ops *ops,
...@@ -1035,10 +1035,10 @@ __cgfsng_ops static void cgfsng_payload_destroy(struct cgroup_ops *ops, ...@@ -1035,10 +1035,10 @@ __cgfsng_ops static void cgfsng_payload_destroy(struct cgroup_ops *ops,
.hierarchies = ops->hierarchies, .hierarchies = ops->hierarchies,
.origuid = 0, .origuid = 0,
}; };
ret = userns_exec_1(handler->conf, cgroup_rmdir_wrapper, &wrap, ret = userns_exec_1(handler->conf, cgroup_tree_remove_wrapper,
"cgroup_rmdir_wrapper"); &wrap, "cgroup_tree_remove_wrapper");
} else { } else {
ret = cgroup_rmdir(ops->hierarchies, ops->container_cgroup); ret = cgroup_tree_remove(ops->hierarchies, ops->container_cgroup);
} }
if (ret < 0) if (ret < 0)
SYSWARN("Failed to destroy cgroups"); SYSWARN("Failed to destroy cgroups");
...@@ -1082,6 +1082,12 @@ __cgfsng_ops static void cgfsng_monitor_destroy(struct cgroup_ops *ops, ...@@ -1082,6 +1082,12 @@ __cgfsng_ops static void cgfsng_monitor_destroy(struct cgroup_ops *ops,
if (!h->monitor_full_path) if (!h->monitor_full_path)
continue; continue;
/* Monitor might have died before we entered the cgroup. */
if (handler->monitor_pid <= 0) {
WARN("No valid monitor process found while destroying cgroups");
goto try_lxc_rm_rf;
}
if (conf && conf->cgroup_meta.dir) if (conf && conf->cgroup_meta.dir)
pivot_path = must_make_path(h->mountpoint, pivot_path = must_make_path(h->mountpoint,
h->container_base_path, h->container_base_path,
...@@ -1095,19 +1101,17 @@ __cgfsng_ops static void cgfsng_monitor_destroy(struct cgroup_ops *ops, ...@@ -1095,19 +1101,17 @@ __cgfsng_ops static void cgfsng_monitor_destroy(struct cgroup_ops *ops,
ret = mkdir_p(pivot_path, 0755); ret = mkdir_p(pivot_path, 0755);
if (ret < 0 && errno != EEXIST) { if (ret < 0 && errno != EEXIST) {
ERROR("Failed to create %s", pivot_path); ERROR("Failed to create %s", pivot_path);
goto try_recursive_destroy; goto try_lxc_rm_rf;
} }
if (handler->monitor_pid != 0) { ret = lxc_write_openat(pivot_path, "cgroup.procs", pidstr, len);
ret = lxc_write_openat(pivot_path, "cgroup.procs", pidstr, len); if (ret != 0) {
if (ret != 0) { SYSWARN("Failed to move monitor %s to \"%s\"", pidstr, pivot_path);
SYSWARN("Failed to move monitor %s to \"%s\"", pidstr, pivot_path); continue;
continue;
}
} }
try_recursive_destroy: try_lxc_rm_rf:
ret = recursive_destroy(h->monitor_full_path); ret = lxc_rm_rf(h->monitor_full_path);
if (ret < 0) if (ret < 0)
WARN("Failed to destroy \"%s\"", h->monitor_full_path); WARN("Failed to destroy \"%s\"", h->monitor_full_path);
} }
...@@ -1141,7 +1145,7 @@ static int mkdir_eexist_on_last(const char *dir, mode_t mode) ...@@ -1141,7 +1145,7 @@ static int mkdir_eexist_on_last(const char *dir, mode_t mode)
return 0; return 0;
} }
static bool create_cgroup_tree(struct hierarchy *h, const char *cgroup_tree, static bool cgroup_tree_create(struct hierarchy *h, const char *cgroup_tree,
const char *cgroup_leaf, bool payload) const char *cgroup_leaf, bool payload)
{ {
__do_free char *path = NULL; __do_free char *path = NULL;
...@@ -1181,7 +1185,7 @@ static bool create_cgroup_tree(struct hierarchy *h, const char *cgroup_tree, ...@@ -1181,7 +1185,7 @@ static bool create_cgroup_tree(struct hierarchy *h, const char *cgroup_tree,
return true; return true;
} }
static void cgroup_remove_leaf(struct hierarchy *h, bool payload) static void cgroup_tree_leaf_remove(struct hierarchy *h, bool payload)
{ {
__do_free char *full_path = NULL; __do_free char *full_path = NULL;
...@@ -1253,12 +1257,12 @@ __cgfsng_ops static inline bool cgfsng_monitor_create(struct cgroup_ops *ops, ...@@ -1253,12 +1257,12 @@ __cgfsng_ops static inline bool cgfsng_monitor_create(struct cgroup_ops *ops,
sprintf(suffix, "-%d", idx); sprintf(suffix, "-%d", idx);
for (i = 0; ops->hierarchies[i]; i++) { for (i = 0; ops->hierarchies[i]; i++) {
if (create_cgroup_tree(ops->hierarchies[i], cgroup_tree, monitor_cgroup, false)) if (cgroup_tree_create(ops->hierarchies[i], cgroup_tree, monitor_cgroup, false))
continue; continue;
ERROR("Failed to create cgroup \"%s\"", ops->hierarchies[i]->monitor_full_path ?: "(null)"); ERROR("Failed to create cgroup \"%s\"", ops->hierarchies[i]->monitor_full_path ?: "(null)");
for (int j = 0; j < i; j++) for (int j = 0; j < i; j++)
cgroup_remove_leaf(ops->hierarchies[j], false); cgroup_tree_leaf_remove(ops->hierarchies[j], false);
idx++; idx++;
break; break;
...@@ -1332,12 +1336,12 @@ __cgfsng_ops static inline bool cgfsng_payload_create(struct cgroup_ops *ops, ...@@ -1332,12 +1336,12 @@ __cgfsng_ops static inline bool cgfsng_payload_create(struct cgroup_ops *ops,
sprintf(suffix, "-%d", idx); sprintf(suffix, "-%d", idx);
for (i = 0; ops->hierarchies[i]; i++) { for (i = 0; ops->hierarchies[i]; i++) {
if (create_cgroup_tree(ops->hierarchies[i], cgroup_tree, container_cgroup, true)) if (cgroup_tree_create(ops->hierarchies[i], cgroup_tree, container_cgroup, true))
continue; continue;
ERROR("Failed to create cgroup \"%s\"", ops->hierarchies[i]->container_full_path ?: "(null)"); ERROR("Failed to create cgroup \"%s\"", ops->hierarchies[i]->container_full_path ?: "(null)");
for (int j = 0; j < i; j++) for (int j = 0; j < i; j++)
cgroup_remove_leaf(ops->hierarchies[j], true); cgroup_tree_leaf_remove(ops->hierarchies[j], true);
idx++; idx++;
break; break;
......
...@@ -730,7 +730,7 @@ static bool do_remove_btrfs_children(struct my_btrfs_tree *tree, u64 root_id, ...@@ -730,7 +730,7 @@ static bool do_remove_btrfs_children(struct my_btrfs_tree *tree, u64 root_id,
return true; return true;
} }
static int btrfs_recursive_destroy(const char *path) static int btrfs_lxc_rm_rf(const char *path)
{ {
u64 root_id; u64 root_id;
int fd; int fd;
...@@ -893,7 +893,7 @@ bool btrfs_try_remove_subvol(const char *path) ...@@ -893,7 +893,7 @@ bool btrfs_try_remove_subvol(const char *path)
if (!btrfs_detect(path)) if (!btrfs_detect(path))
return false; return false;
return btrfs_recursive_destroy(path) == 0; return btrfs_lxc_rm_rf(path) == 0;
} }
int btrfs_destroy(struct lxc_storage *orig) int btrfs_destroy(struct lxc_storage *orig)
...@@ -902,7 +902,7 @@ int btrfs_destroy(struct lxc_storage *orig) ...@@ -902,7 +902,7 @@ int btrfs_destroy(struct lxc_storage *orig)
src = lxc_storage_get_path(orig->src, "btrfs"); src = lxc_storage_get_path(orig->src, "btrfs");
return btrfs_recursive_destroy(src); return btrfs_lxc_rm_rf(src);
} }
int btrfs_create(struct lxc_storage *bdev, const char *dest, const char *n, int btrfs_create(struct lxc_storage *bdev, const char *dest, const char *n,
......
...@@ -1071,7 +1071,7 @@ static int ls_remove_lock(const char *path, const char *name, ...@@ -1071,7 +1071,7 @@ static int ls_remove_lock(const char *path, const char *name,
if (check < 0 || (size_t)check >= *len_lockpath) if (check < 0 || (size_t)check >= *len_lockpath)
goto out; goto out;
ret = recursive_destroy(*lockpath); ret = lxc_rm_rf(*lockpath);
if (ret < 0) if (ret < 0)
WARN("Failed to destroy \"%s\"", *lockpath); WARN("Failed to destroy \"%s\"", *lockpath);
......
...@@ -1747,7 +1747,7 @@ int fd_cloexec(int fd, bool cloexec) ...@@ -1747,7 +1747,7 @@ int fd_cloexec(int fd, bool cloexec)
return 0; return 0;
} }
int recursive_destroy(const char *dirname) int lxc_rm_rf(const char *dirname)
{ {
__do_closedir DIR *dir = NULL; __do_closedir DIR *dir = NULL;
int fret = 0; int fret = 0;
...@@ -1779,7 +1779,7 @@ int recursive_destroy(const char *dirname) ...@@ -1779,7 +1779,7 @@ int recursive_destroy(const char *dirname)
if (!S_ISDIR(mystat.st_mode)) if (!S_ISDIR(mystat.st_mode))
continue; continue;
ret = recursive_destroy(pathname); ret = lxc_rm_rf(pathname);
if (ret < 0) if (ret < 0)
fret = -1; fret = -1;
} }
......
...@@ -235,7 +235,7 @@ extern uint64_t lxc_find_next_power2(uint64_t n); ...@@ -235,7 +235,7 @@ extern uint64_t lxc_find_next_power2(uint64_t n);
/* Set a signal the child process will receive after the parent has died. */ /* Set a signal the child process will receive after the parent has died. */
extern int lxc_set_death_signal(int signal, pid_t parent, int parent_status_fd); extern int lxc_set_death_signal(int signal, pid_t parent, int parent_status_fd);
extern int fd_cloexec(int fd, bool cloexec); extern int fd_cloexec(int fd, bool cloexec);
extern int recursive_destroy(const char *dirname); extern int lxc_rm_rf(const char *dirname);
extern int lxc_setup_keyring(char *keyring_label); extern int lxc_setup_keyring(char *keyring_label);
extern bool lxc_can_use_pidfd(int pidfd); extern bool lxc_can_use_pidfd(int pidfd);
......
...@@ -415,7 +415,7 @@ static bool lxc_setup_shmount(const char *shmount_path) ...@@ -415,7 +415,7 @@ static bool lxc_setup_shmount(const char *shmount_path)
static void lxc_teardown_shmount(char *shmount_path) static void lxc_teardown_shmount(char *shmount_path)
{ {
(void)umount2(shmount_path, MNT_DETACH); (void)umount2(shmount_path, MNT_DETACH);
(void)recursive_destroy(shmount_path); (void)lxc_rm_rf(shmount_path);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
......
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