Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 459edb0b authored by Kelvin Zhang's avatar Kelvin Zhang
Browse files

Make RemoveCgroup idempotent

When attempting to remove a cgroup, a ENOENT means this cgroup is
already removed. Treat such errno as success for idempotency.

Test: th
Bug: 308900853
Change-Id: I6ef3c25f03d185194205b3845784d284fdc4d444
parent 10993214
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -219,6 +219,12 @@ static int RemoveCgroup(const char* cgroup, uid_t uid, int pid, unsigned int ret

    while (retries--) {
        ret = rmdir(uid_pid_path.c_str());
        // If we get an error 2 'No such file or directory' , that means the
        // cgroup is already removed, treat it as success and return 0 for
        // idempotency.
        if (ret < 0 && errno == ENOENT) {
            ret = 0;
        }
        if (!ret || errno != EBUSY || !retries) break;
        std::this_thread::sleep_for(5ms);
    }
@@ -228,6 +234,9 @@ static int RemoveCgroup(const char* cgroup, uid_t uid, int pid, unsigned int ret
        // so free up the kernel resources for the UID level cgroup.
        const auto uid_path = ConvertUidToPath(cgroup, uid);
        ret = rmdir(uid_path.c_str());
        if (ret < 0 && errno == ENOENT) {
            ret = 0;
        }
    }

    return ret;