Commit ef342abb by Daniel Lezcano Committed by Daniel Lezcano

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: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent 36b86299
......@@ -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)
......
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