Commit 341a9bd8 by Serge Hallyn Committed by Daniel Lezcano

recursively delete cgroups on container shutdown

If a container has created its own cgroups, i.e. by running libvirtd, then if we don't delete all child cgroups, then the rmdir will fail. Signed-off-by: 's avatarSerge Hallyn <serge.hallyn@canonical.com> Signed-off-by: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent 581092fc
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
#include <mntent.h> #include <mntent.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <dirent.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
...@@ -419,6 +420,48 @@ out: ...@@ -419,6 +420,48 @@ out:
return err; return err;
} }
int recursive_rmdir(char *dirname)
{
struct dirent dirent, *direntp;
int fddir;
DIR *dir;
int ret;
char pathname[MAXPATHLEN];
dir = opendir(dirname);
if (!dir) {
WARN("failed to open directory: %m");
return -1;
}
fddir = dirfd(dir);
while (!readdir_r(dir, &dirent, &direntp)) {
struct stat mystat;
if (!direntp)
break;
if (!strcmp(direntp->d_name, ".") ||
!strcmp(direntp->d_name, ".."))
continue;
snprintf(pathname, MAXPATHLEN, "%s/%s", dirname, direntp->d_name);
ret = stat(pathname, &mystat);
if (ret)
continue;
if (S_ISDIR(mystat.st_mode))
recursive_rmdir(pathname);
}
ret = rmdir(dirname);
if (closedir(dir))
ERROR("failed to close directory");
return ret;
}
int lxc_one_cgroup_destroy(struct mntent *mntent, const char *name) int lxc_one_cgroup_destroy(struct mntent *mntent, const char *name)
{ {
...@@ -428,7 +471,7 @@ int lxc_one_cgroup_destroy(struct mntent *mntent, const char *name) ...@@ -428,7 +471,7 @@ int lxc_one_cgroup_destroy(struct mntent *mntent, const char *name)
snprintf(cgname, MAXPATHLEN, "%s%s/lxc/%s", cgmnt, snprintf(cgname, MAXPATHLEN, "%s%s/lxc/%s", cgmnt,
get_init_cgroup(NULL, mntent, initcgroup), name); get_init_cgroup(NULL, mntent, initcgroup), name);
DEBUG("destroying %s\n", cgname); DEBUG("destroying %s\n", cgname);
if (rmdir(cgname)) { if (recursive_rmdir(cgname)) {
SYSERROR("failed to remove cgroup '%s'", cgname); SYSERROR("failed to remove cgroup '%s'", cgname);
return -1; return -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