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: ...@@ -85,71 +85,65 @@ out:
return err; 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 oldname[MAXPATHLEN];
char newname[MAXPATHLEN];
char cgroup[MAXPATHLEN];
int ret;
if (get_cgroup_mount(MTAB, cgroup)) { snprintf(oldname, MAXPATHLEN, "%s/%d", mnt, pid);
ERROR("cgroup is not mounted");
return -1;
}
snprintf(oldname, MAXPATHLEN, "%s/%d", cgroup, pid); if (rename(oldname, name)) {
snprintf(newname, MAXPATHLEN, "%s/%s", cgroup, name); SYSERROR("failed to rename cgroup %s->%s", oldname, name);
return -1;
/* 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;
}
} }
ret = rename(oldname, newname); DEBUG("'%s' renamed to '%s'", oldname, name);
if (ret)
SYSERROR("failed to rename cgroup %s->%s", oldname, newname);
else
DEBUG("'%s' renamed to '%s'", oldname, newname);
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 cgmnt[MAXPATHLEN];
char cgroup[MAXPATHLEN]; char cgname[MAXPATHLEN];
int ret;
if (get_cgroup_mount(MTAB, cgroup)) { if (get_cgroup_mount(MTAB, cgmnt)) {
ERROR("cgroup is not mounted"); ERROR("cgroup is not mounted");
return -1; return -1;
} }
snprintf(nsgroup, MAXPATHLEN, "%s/%s", cgroup, name); snprintf(cgname, MAXPATHLEN, "%s/%s", cgmnt, name);
ret = rmdir(nsgroup);
if (ret)
SYSERROR("failed to remove cgroup '%s'", nsgroup);
else
DEBUG("'%s' unlinked", nsgroup);
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(cgmnt, cgname, pid);
{
return lxc_rename_nsgroup(name, pid);
} }
int lxc_cgroup_destroy(const char *name) 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) 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