diff options
author | Daniel Lezcano <daniel.lezcano@free.fr> | 2010-12-17 11:43:37 +0100 |
---|---|---|
committer | Daniel Lezcano <dlezcano@fr.ibm.com> | 2010-12-17 11:43:37 +0100 |
commit | ef342abb22c1354b3523ce3c46e613e13ab79399 (patch) | |
tree | 65b6db221cca80634c7369916a3a760b1a0eaa95 | |
parent | encapsulate the ns_cgroup (diff) | |
download | lxc-ef342abb22c1354b3523ce3c46e613e13ab79399.tar.gz lxc-ef342abb22c1354b3523ce3c46e613e13ab79399.tar.bz2 lxc-ef342abb22c1354b3523ce3c46e613e13ab79399.zip |
Move common code to lxc_cgroup_create
For both the ns_cgroup and the usual cgroup creation, we have to
check if a previous does not exist and remove it if it is empty.
Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
-rw-r--r-- | src/lxc/cgroup.c | 82 |
1 files changed, 38 insertions, 44 deletions
diff --git a/src/lxc/cgroup.c b/src/lxc/cgroup.c index 4b54906..b711c64 100644 --- a/src/lxc/cgroup.c +++ b/src/lxc/cgroup.c @@ -85,71 +85,65 @@ out: return err; } -int lxc_rename_nsgroup(const char *name, pid_t pid) +int lxc_rename_nsgroup(const char *mnt, const char *name, pid_t pid) { char oldname[MAXPATHLEN]; - char newname[MAXPATHLEN]; - char cgroup[MAXPATHLEN]; - int ret; - if (get_cgroup_mount(MTAB, cgroup)) { - ERROR("cgroup is not mounted"); - return -1; - } + snprintf(oldname, MAXPATHLEN, "%s/%d", mnt, pid); - snprintf(oldname, MAXPATHLEN, "%s/%d", cgroup, pid); - snprintf(newname, MAXPATHLEN, "%s/%s", cgroup, name); - - /* there is a previous cgroup, assume it is empty, otherwise - * that fails */ - if (!access(newname, F_OK)) { - ret = rmdir(newname); - if (ret) { - SYSERROR("failed to remove previous cgroup '%s'", - newname); - return ret; - } + if (rename(oldname, name)) { + SYSERROR("failed to rename cgroup %s->%s", oldname, name); + return -1; } - ret = rename(oldname, newname); - if (ret) - SYSERROR("failed to rename cgroup %s->%s", oldname, newname); - else - DEBUG("'%s' renamed to '%s'", oldname, newname); - + DEBUG("'%s' renamed to '%s'", oldname, name); - return ret; + return 0; } -int lxc_unlink_nsgroup(const char *name) +int lxc_cgroup_create(const char *name, pid_t pid) { - char nsgroup[MAXPATHLEN]; - char cgroup[MAXPATHLEN]; - int ret; + char cgmnt[MAXPATHLEN]; + char cgname[MAXPATHLEN]; - if (get_cgroup_mount(MTAB, cgroup)) { + if (get_cgroup_mount(MTAB, cgmnt)) { ERROR("cgroup is not mounted"); return -1; } - snprintf(nsgroup, MAXPATHLEN, "%s/%s", cgroup, name); - ret = rmdir(nsgroup); - if (ret) - SYSERROR("failed to remove cgroup '%s'", nsgroup); - else - DEBUG("'%s' unlinked", nsgroup); + snprintf(cgname, MAXPATHLEN, "%s/%s", cgmnt, name); - return ret; -} + /* + * There is a previous cgroup, assume it is empty, + * otherwise that fails + */ + if (!access(cgname, F_OK) && rmdir(cgname)) { + SYSERROR("failed to remove previous cgroup '%s'", cgname); + return -1; + } -int lxc_cgroup_create(const char *name, pid_t pid) -{ - return lxc_rename_nsgroup(name, pid); + return lxc_rename_nsgroup(cgmnt, cgname, pid); } int lxc_cgroup_destroy(const char *name) { - return lxc_unlink_nsgroup(name); + char cgmnt[MAXPATHLEN]; + char cgname[MAXPATHLEN]; + + if (get_cgroup_mount(MTAB, cgmnt)) { + ERROR("cgroup is not mounted"); + return -1; + } + + snprintf(cgname, MAXPATHLEN, "%s/%s", cgmnt, name); + if (rmdir(cgmnt)) { + SYSERROR("failed to remove cgroup '%s'", cgname); + return -1; + } + + DEBUG("'%s' unlinked", cgname); + + return 0; } int lxc_cgroup_path_get(char **path, const char *name) |