diff options
author | Pat Erley <perley@cyngn.com> | 2016-03-18 14:00:26 -0700 |
---|---|---|
committer | Pat Erley <perley@cyngn.com> | 2016-03-18 14:47:37 -0700 |
commit | 8b964f37d7826d7531d42a66ca446e2dc23aff74 (patch) | |
tree | 0a5b9da43c1356aca4835c8157982591873b5de1 | |
parent | fb7e634f3d725728d65e0c32c5d20c99d91a158e (diff) | |
download | system_core-8b964f37d7826d7531d42a66ca446e2dc23aff74.zip system_core-8b964f37d7826d7531d42a66ca446e2dc23aff74.tar.gz system_core-8b964f37d7826d7531d42a66ca446e2dc23aff74.tar.bz2 |
libcutils: make sched_policy more robust
If you compile with cpusets enabled but the permissions are wrong
the behavior becomes no real policy enforced at all. Fix this by
making sched_policy fall back gracefully.
Change-Id: Ife96ca64b991e23232a5bd0d6c21e30a5d16bde8
-rw-r--r-- | libcutils/sched_policy.c | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/libcutils/sched_policy.c b/libcutils/sched_policy.c index 70dc8c4..a243c90 100644 --- a/libcutils/sched_policy.c +++ b/libcutils/sched_policy.c @@ -55,6 +55,7 @@ static inline SchedPolicy _policy(SchedPolicy p) static pthread_once_t the_once = PTHREAD_ONCE_INIT; static int __sys_supports_schedgroups = -1; +static int __sys_supports_cpusets = -1; // File descriptors open to /dev/cpuctl/../tasks, setup by initialize, or -1 on error. static int bg_cgroup_fd = -1; @@ -109,12 +110,22 @@ static void __initialize(void) { fg_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC); if (fg_cgroup_fd < 0) { SLOGE("open of %s failed: %s\n", filename, strerror(errno)); + __sys_supports_schedgroups = 0; } filename = "/dev/cpuctl/bg_non_interactive/tasks"; bg_cgroup_fd = open(filename, O_WRONLY | O_CLOEXEC); if (bg_cgroup_fd < 0) { SLOGE("open of %s failed: %s\n", filename, strerror(errno)); + __sys_supports_schedgroups = 0; + } + + if (!__sys_supports_schedgroups) { + close(bg_cgroup_fd); + bg_cgroup_fd = -1; + + close(fg_cgroup_fd); + fg_cgroup_fd = -1; } } else { __sys_supports_schedgroups = 0; @@ -122,13 +133,41 @@ static void __initialize(void) { #ifdef USE_CPUSETS if (!access("/dev/cpuset/tasks", F_OK)) { + __sys_supports_cpusets = 1; filename = "/dev/cpuset/foreground/tasks"; fg_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC); + if (fg_cpuset_fd < 0) { + SLOGE("open of %s failed %s\n", filename, strerror(errno)); + __sys_supports_cpusets = 0; + } + filename = "/dev/cpuset/background/tasks"; bg_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC); + if (bg_cpuset_fd < 0) { + SLOGE("open of %s failed %s\n", filename, strerror(errno)); + __sys_supports_cpusets = 0; + } + filename = "/dev/cpuset/system-background/tasks"; system_bg_cpuset_fd = open(filename, O_WRONLY | O_CLOEXEC); + if (system_bg_cpuset_fd < 0) { + SLOGE("open of %s failed %s\n", filename, strerror(errno)); + __sys_supports_cpusets = 0; + } + + if (!__sys_supports_cpusets) { + close(fg_cpuset_fd); + fg_cpuset_fd = -1; + + close(bg_cpuset_fd); + bg_cpuset_fd = -1; + + close(system_bg_cpuset_fd); + system_bg_cpuset_fd = -1; + } + } else { + __sys_supports_cpusets = 0; } #endif @@ -250,9 +289,14 @@ int set_cpuset_policy(int tid, SchedPolicy policy) if (tid == 0) { tid = gettid(); } - policy = _policy(policy); + pthread_once(&the_once, __initialize); + if (!__sys_supports_cpusets) + return set_sched_policy(tid, policy); + + policy = _policy(policy); + int fd; switch (policy) { case SP_BACKGROUND: |