Commit 4c633005 authored by Lennart Poettering's avatar Lennart Poettering
Browse files

cgroup: treat non-existing cgroups like empty ones, to deal with races

parent edb9aaa8
...@@ -189,7 +189,7 @@ int cg_kill(const char *controller, const char *path, int sig, bool ignore_self) ...@@ -189,7 +189,7 @@ int cg_kill(const char *controller, const char *path, int sig, bool ignore_self)
done = true; done = true;
if ((r = cg_enumerate_processes(controller, path, &f)) < 0) { if ((r = cg_enumerate_processes(controller, path, &f)) < 0) {
if (ret >= 0) if (ret >= 0 && r != -ENOENT)
ret = r; ret = r;
goto finish; goto finish;
...@@ -205,8 +205,8 @@ int cg_kill(const char *controller, const char *path, int sig, bool ignore_self) ...@@ -205,8 +205,8 @@ int cg_kill(const char *controller, const char *path, int sig, bool ignore_self)
/* If we haven't killed this process yet, kill /* If we haven't killed this process yet, kill
* it */ * it */
if (kill(pid, sig) < 0 && errno != ESRCH) { if (kill(pid, sig) < 0) {
if (ret >= 0) if (ret >= 0 && errno != ESRCH)
ret = -errno; ret = -errno;
} else if (ret == 0) } else if (ret == 0)
ret = 1; ret = 1;
...@@ -258,7 +258,7 @@ int cg_kill_recursive(const char *controller, const char *path, int sig, bool ig ...@@ -258,7 +258,7 @@ int cg_kill_recursive(const char *controller, const char *path, int sig, bool ig
ret = cg_kill(controller, path, sig, ignore_self); ret = cg_kill(controller, path, sig, ignore_self);
if ((r = cg_enumerate_subgroups(controller, path, &d)) < 0) { if ((r = cg_enumerate_subgroups(controller, path, &d)) < 0) {
if (ret >= 0) if (ret >= 0 && r != -ENOENT)
ret = r; ret = r;
goto finish; goto finish;
...@@ -289,7 +289,7 @@ int cg_kill_recursive(const char *controller, const char *path, int sig, bool ig ...@@ -289,7 +289,7 @@ int cg_kill_recursive(const char *controller, const char *path, int sig, bool ig
if (rem) if (rem)
if ((r = cg_rmdir(controller, path)) < 0) { if ((r = cg_rmdir(controller, path)) < 0) {
if (ret >= 0) if (ret >= 0 && r != -ENOENT)
ret = r; ret = r;
} }
...@@ -351,7 +351,7 @@ int cg_migrate(const char *controller, const char *from, const char *to, bool ig ...@@ -351,7 +351,7 @@ int cg_migrate(const char *controller, const char *from, const char *to, bool ig
done = true; done = true;
if ((r = cg_enumerate_tasks(controller, from, &f)) < 0) { if ((r = cg_enumerate_tasks(controller, from, &f)) < 0) {
if (ret >= 0) if (ret >= 0 && r != -ENOENT)
ret = r; ret = r;
goto finish; goto finish;
...@@ -369,7 +369,7 @@ int cg_migrate(const char *controller, const char *from, const char *to, bool ig ...@@ -369,7 +369,7 @@ int cg_migrate(const char *controller, const char *from, const char *to, bool ig
continue; continue;
if ((r = cg_attach(controller, to, pid)) < 0) { if ((r = cg_attach(controller, to, pid)) < 0) {
if (ret >= 0) if (ret >= 0 && r != -ESRCH)
ret = r; ret = r;
} else if (ret == 0) } else if (ret == 0)
ret = 1; ret = 1;
...@@ -417,7 +417,7 @@ int cg_migrate_recursive(const char *controller, const char *from, const char *t ...@@ -417,7 +417,7 @@ int cg_migrate_recursive(const char *controller, const char *from, const char *t
ret = cg_migrate(controller, from, to, ignore_self); ret = cg_migrate(controller, from, to, ignore_self);
if ((r = cg_enumerate_subgroups(controller, from, &d)) < 0) { if ((r = cg_enumerate_subgroups(controller, from, &d)) < 0) {
if (ret >= 0) if (ret >= 0 && r != -ENOENT)
ret = r; ret = r;
goto finish; goto finish;
} }
...@@ -447,7 +447,7 @@ int cg_migrate_recursive(const char *controller, const char *from, const char *t ...@@ -447,7 +447,7 @@ int cg_migrate_recursive(const char *controller, const char *from, const char *t
if (rem) if (rem)
if ((r = cg_rmdir(controller, from)) < 0) { if ((r = cg_rmdir(controller, from)) < 0) {
if (ret >= 0) if (ret >= 0 && r != -ENOENT)
ret = r; ret = r;
} }
...@@ -517,7 +517,7 @@ int cg_trim(const char *controller, const char *path, bool delete_root) { ...@@ -517,7 +517,7 @@ int cg_trim(const char *controller, const char *path, bool delete_root) {
r = rm_rf(fs, true, delete_root); r = rm_rf(fs, true, delete_root);
free(fs); free(fs);
return r; return r == -ENOENT ? 0 : r;
} }
int cg_delete(const char *controller, const char *path) { int cg_delete(const char *controller, const char *path) {
...@@ -533,7 +533,7 @@ int cg_delete(const char *controller, const char *path) { ...@@ -533,7 +533,7 @@ int cg_delete(const char *controller, const char *path) {
r = cg_migrate_recursive(controller, path, parent, false, true); r = cg_migrate_recursive(controller, path, parent, false, true);
free(parent); free(parent);
return r; return r == -ENOENT ? 0 : r;
} }
int cg_create(const char *controller, const char *path) { int cg_create(const char *controller, const char *path) {
...@@ -646,6 +646,9 @@ int cg_get_by_pid(const char *controller, pid_t pid, char **path) { ...@@ -646,6 +646,9 @@ int cg_get_by_pid(const char *controller, pid_t pid, char **path) {
f = fopen(fs, "re"); f = fopen(fs, "re");
free(fs); free(fs);
if (!f)
return errno == ENOENT ? -ESRCH : -errno;
cs = strlen(controller); cs = strlen(controller);
while (!feof(f)) { while (!feof(f)) {
...@@ -763,7 +766,7 @@ int cg_is_empty(const char *controller, const char *path, bool ignore_self) { ...@@ -763,7 +766,7 @@ int cg_is_empty(const char *controller, const char *path, bool ignore_self) {
assert(path); assert(path);
if ((r = cg_enumerate_tasks(controller, path, &f)) < 0) if ((r = cg_enumerate_tasks(controller, path, &f)) < 0)
return r; return r == -ENOENT ? 1 : r;
while ((r = cg_read_pid(f, &pid)) > 0) { while ((r = cg_read_pid(f, &pid)) > 0) {
...@@ -794,7 +797,7 @@ int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_ ...@@ -794,7 +797,7 @@ int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_
return r; return r;
if ((r = cg_enumerate_subgroups(controller, path, &d)) < 0) if ((r = cg_enumerate_subgroups(controller, path, &d)) < 0)
return r; return r == -ENOENT ? 1 : r;
while ((r = cg_read_subgroup(d, &fn)) > 0) { while ((r = cg_read_subgroup(d, &fn)) > 0) {
char *p = NULL; char *p = NULL;
......
...@@ -2565,7 +2565,8 @@ static int rm_rf_children(int fd, bool only_dirs) { ...@@ -2565,7 +2565,8 @@ static int rm_rf_children(int fd, bool only_dirs) {
if (!(d = fdopendir(fd))) { if (!(d = fdopendir(fd))) {
close_nointr_nofail(fd); close_nointr_nofail(fd);
return -errno;
return errno == ENOENT ? 0 : -errno;
} }
for (;;) { for (;;) {
...@@ -2589,7 +2590,7 @@ static int rm_rf_children(int fd, bool only_dirs) { ...@@ -2589,7 +2590,7 @@ static int rm_rf_children(int fd, bool only_dirs) {
struct stat st; struct stat st;
if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) { if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
if (ret == 0) if (ret == 0 && errno != ENOENT)
ret = -errno; ret = -errno;
continue; continue;
} }
...@@ -2602,7 +2603,7 @@ static int rm_rf_children(int fd, bool only_dirs) { ...@@ -2602,7 +2603,7 @@ static int rm_rf_children(int fd, bool only_dirs) {
int subdir_fd; int subdir_fd;
if ((subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) { if ((subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC)) < 0) {
if (ret == 0) if (ret == 0 && errno != ENOENT)
ret = -errno; ret = -errno;
continue; continue;
} }
...@@ -2613,13 +2614,13 @@ static int rm_rf_children(int fd, bool only_dirs) { ...@@ -2613,13 +2614,13 @@ static int rm_rf_children(int fd, bool only_dirs) {
} }
if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) { if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
if (ret == 0) if (ret == 0 && errno != ENOENT)
ret = -errno; ret = -errno;
} }
} else if (!only_dirs) { } else if (!only_dirs) {
if (unlinkat(fd, de->d_name, 0) < 0) { if (unlinkat(fd, de->d_name, 0) < 0) {
if (ret == 0) if (ret == 0 && errno != ENOENT)
ret = -errno; ret = -errno;
} }
} }
......
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