1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "OsUtil"
18 
19 #include "core_jni_helpers.h"
20 
21 #include <processgroup/processgroup.h>
22 #include <utils/Log.h>
23 
24 #include <nativehelper/JNIHelp.h>
25 
26 using namespace android;
27 
com_android_internal_car_os_Util_setProcessProfile(JNIEnv * env,jobject,jint pid,jint uid,jstring profile)28 void com_android_internal_car_os_Util_setProcessProfile(JNIEnv* env, jobject /*clazz*/, jint pid,
29     jint uid, jstring profile) {
30     if (profile == nullptr) {
31         jniThrowNullPointerException(env, NULL);
32         return;
33     }
34 
35     const char* profileStr = env->GetStringUTFChars(profile, nullptr);
36     if (profileStr == nullptr) {
37         jniThrowNullPointerException(env, NULL);
38         return;
39     }
40     bool success = SetProcessProfiles(uid, pid, {profileStr});
41     env->ReleaseStringUTFChars(profile, profileStr);
42 
43     if (!success) {
44         jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
45             "setProcessProfile for pid %d, uid %d, profile %s failed", pid, uid, profileStr);
46     }
47 }
48 
49 static const JNINativeMethod methods[] = {
50         {"setProcessProfile", "(IILjava/lang/String;)V",
51          (void*)com_android_internal_car_os_Util_setProcessProfile},
52 };
53 
register_com_android_internal_car_os_Util(JNIEnv * env)54 int register_com_android_internal_car_os_Util(JNIEnv* env)
55 {
56     return RegisterMethodsOrDie(env, "com/android/internal/car/os/Util", methods, NELEM(methods));
57 }
58