Preserve historical pthread_create scheduler behavior better.
At the cost of two flag bits for what POSIX thinks should be a boolean
choice, plus somewhat confusing behavior from pthread_attr_getinheritsched
depending on when you call it/what specific scheduler attributes you've
set in the pthread_attr_t, we can emulate the old behavior exactly and
prevent annoying SELinux denial spam caused by calls to sched_setscheduler.
Bug: http://b/68391226
Test: adb logcat on boot contains no sys_nice avc denials
Change-Id: I4f759c2c4fd1d80cceb0912d7da09d35902e2e5e
diff --git a/libc/bionic/pthread_attr.cpp b/libc/bionic/pthread_attr.cpp
index 04d8112..93177f1 100644
--- a/libc/bionic/pthread_attr.cpp
+++ b/libc/bionic/pthread_attr.cpp
@@ -61,8 +61,10 @@
int pthread_attr_setinheritsched(pthread_attr_t* attr, int flag) {
if (flag == PTHREAD_EXPLICIT_SCHED) {
attr->flags &= ~PTHREAD_ATTR_FLAG_INHERIT;
+ attr->flags |= PTHREAD_ATTR_FLAG_EXPLICIT;
} else if (flag == PTHREAD_INHERIT_SCHED) {
attr->flags |= PTHREAD_ATTR_FLAG_INHERIT;
+ attr->flags &= ~PTHREAD_ATTR_FLAG_EXPLICIT;
} else {
return EINVAL;
}
@@ -71,8 +73,14 @@
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
int pthread_attr_getinheritsched(const pthread_attr_t* attr, int* flag) {
- *flag = (attr->flags & PTHREAD_ATTR_FLAG_INHERIT) ? PTHREAD_INHERIT_SCHED
- : PTHREAD_EXPLICIT_SCHED;
+ if ((attr->flags & PTHREAD_ATTR_FLAG_INHERIT) != 0) {
+ *flag = PTHREAD_INHERIT_SCHED;
+ } else if ((attr->flags & PTHREAD_ATTR_FLAG_EXPLICIT) != 0) {
+ *flag = PTHREAD_EXPLICIT_SCHED;
+ } else {
+ // Historical behavior before P, when pthread_attr_setinheritsched was added.
+ *flag = (attr->sched_policy != SCHED_NORMAL) ? PTHREAD_EXPLICIT_SCHED : PTHREAD_INHERIT_SCHED;
+ }
return 0;
}