As a previous run may have created a cgroup but died unexpectedly,
the cgroup can be still there when we try to launch the container
again with the same name. This patch removes the directory if it is
present, if this one is not owned by caller or it is in use (that should
not happen), the rmdir will fail with the corresponding errno.
Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
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;
+ }
+ }
+
ret = rename(oldname, newname);
if (ret)
SYSERROR("failed to rename cgroup %s->%s", oldname, newname);