summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBart Van Assche <bvanassche@google.com>2024-01-22 18:16:31 +0000
committerAndroid Build Coastguard Worker <android-build-coastguard-worker@google.com>2024-01-23 21:55:26 +0000
commit5cf6fa37b81c85784c03e4384cc58e9424bc7873 (patch)
treefcfa839c955d7bb4b3f9df73df35a096add58f9e
parent3a3c22c90b003ec2a4d13446fe369888b7dd3619 (diff)
downloadbase-5cf6fa37b81c85784c03e4384cc58e9424bc7873.tar.gz
Pass the correct user ID to SetProcessProfilesCached()
SetProcessProfilesCached() can only set cgroup attributes in the v2 cgroup hierarchy if it is passed the correct user ID. Hence this CL. This CL makes SetProcessProfilesCached() apply IO controller cgroup attributes for processes with a user ID that is not zero. Bug: 320456702 Bug: 213617178 Signed-off-by: Bart Van Assche <bvanassche@google.com> (cherry picked from https://android-review.googlesource.com/q/commit:16c09591abe1f982887b05d89e264f69316f383d) (cherry picked from https://googleplex-android-review.googlesource.com/q/commit:046510b52692a1967050803c28b1bf7d2b61351f) Merged-In: I84d072c14ff01422560c241a69cb9f4e02c47e74 Change-Id: I84d072c14ff01422560c241a69cb9f4e02c47e74
-rw-r--r--core/jni/android_util_Process.cpp25
1 files changed, 24 insertions, 1 deletions
diff --git a/core/jni/android_util_Process.cpp b/core/jni/android_util_Process.cpp
index 91dfc6023e42..fc059109e9b2 100644
--- a/core/jni/android_util_Process.cpp
+++ b/core/jni/android_util_Process.cpp
@@ -28,6 +28,7 @@
#include <meminfo/sysmeminfo.h>
#include <processgroup/processgroup.h>
#include <processgroup/sched_policy.h>
+#include <android-base/stringprintf.h>
#include <android-base/unique_fd.h>
#include <algorithm>
@@ -64,6 +65,7 @@
#define GUARD_THREAD_PRIORITY 0
using namespace android;
+using ::android::base::StringPrintf;
static constexpr bool kDebugPolicy = false;
static constexpr bool kDebugProc = false;
@@ -232,6 +234,26 @@ void android_os_Process_setThreadGroupAndCpuset(JNIEnv* env, jobject clazz, int
}
}
+// Look up the user ID of a process in /proc/${pid/}status. The Uid: line is present in
+// /proc/${pid}/status since at least kernel v2.5.
+static int uid_from_pid(int pid)
+{
+ int uid = 0;
+ std::string path = StringPrintf("/proc/%d/status", pid);
+ FILE* f = fopen(path.c_str(), "r");
+ if (!f) {
+ return uid;
+ }
+ char line[256];
+ while (fgets(line, sizeof(line), f)) {
+ if (sscanf(line, "Uid: %d", &uid) == 1) {
+ break;
+ }
+ }
+ fclose(f);
+ return uid;
+}
+
void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jint grp)
{
ALOGV("%s pid=%d grp=%" PRId32, __func__, pid, grp);
@@ -275,7 +297,8 @@ void android_os_Process_setProcessGroup(JNIEnv* env, jobject clazz, int pid, jin
}
}
- if (!SetProcessProfilesCached(0, pid, {get_cpuset_policy_profile_name((SchedPolicy)grp)}))
+ if (!SetProcessProfilesCached(uid_from_pid(pid), pid,
+ {get_cpuset_policy_profile_name((SchedPolicy)grp)}))
signalExceptionForGroupError(env, errno ? errno : EPERM, pid);
}